Jelajahi Sumber

refactor(setConfig): 现在用户名和密码可以为NULL了

RyanCW 1 tahun lalu
induk
melakukan
3cfcff8eaf
3 mengubah file dengan 29 tambahan dan 14 penghapusan
  1. 2 2
      example/RyanMqttTest.c
  2. 20 6
      mqttclient/RyanMqttClient.c
  3. 7 6
      test/RyanMqttTestLinux.c

+ 2 - 2
example/RyanMqttTest.c

@@ -3,8 +3,8 @@
 #define RyanMqttClientId ("RyanMqttTessdfwrt") // 填写mqtt客户端id,要求唯一
 #define RyanMqttHost ("broker.emqx.io")        // 填写你的mqtt服务器ip
 #define RyanMqttPort ("1883")                  // mqtt服务器端口
-#define RyanMqttUserName ("")                  // 为空时填写""
-#define RyanMqttPassword ("")                  // 为空时填写""
+#define RyanMqttUserName (NULL)                // 填写你的用户名
+#define RyanMqttPassword (NULL)                // 填写你的密码
 
 #ifdef PKG_USING_RYANMQTT_EXAMPLE
 #include <stdio.h>

+ 20 - 6
mqttclient/RyanMqttClient.c

@@ -495,8 +495,8 @@ RyanMqttError_e RyanMqttSetConfig(RyanMqttClient_t *client, RyanMqttClientConfig
     RyanMqttCheck(NULL != clientConfig->clientId, RyanMqttParamInvalidError, rlog_d);
     RyanMqttCheck(NULL != clientConfig->host, RyanMqttParamInvalidError, rlog_d);
     RyanMqttCheck(NULL != clientConfig->port, RyanMqttParamInvalidError, rlog_d);
-    RyanMqttCheck(NULL != clientConfig->userName, RyanMqttParamInvalidError, rlog_d);
-    RyanMqttCheck(NULL != clientConfig->password, RyanMqttParamInvalidError, rlog_d);
+    // RyanMqttCheck(NULL != clientConfig->userName, RyanMqttParamInvalidError, rlog_d);
+    // RyanMqttCheck(NULL != clientConfig->password, RyanMqttParamInvalidError, rlog_d);
     RyanMqttCheck(NULL != clientConfig->taskName, RyanMqttParamInvalidError, rlog_d);
     RyanMqttCheck(13 < clientConfig->recvBufferSize && (RyanMqttMaxPayloadLen + 5) >= clientConfig->recvBufferSize, RyanMqttParamInvalidError, rlog_d);
     RyanMqttCheck(13 < clientConfig->sendBufferSize && (RyanMqttMaxPayloadLen + 5) >= clientConfig->sendBufferSize, RyanMqttParamInvalidError, rlog_d);
@@ -504,11 +504,25 @@ RyanMqttError_e RyanMqttSetConfig(RyanMqttClient_t *client, RyanMqttClientConfig
     result = setConfigValue(&client->config.clientId, clientConfig->clientId);
     RyanMqttCheckCode(RyanMqttSuccessError == result, result, rlog_d, { goto __exit; });
 
-    result = setConfigValue(&client->config.userName, clientConfig->userName);
-    RyanMqttCheckCode(RyanMqttSuccessError == result, result, rlog_d, { goto __exit; });
+    if (NULL == clientConfig->userName)
+    {
+        client->config.userName = NULL;
+    }
+    else
+    {
+        result = setConfigValue(&client->config.userName, clientConfig->userName);
+        RyanMqttCheckCode(RyanMqttSuccessError == result, result, rlog_d, { goto __exit; });
+    }
 
-    result = setConfigValue(&client->config.password, clientConfig->password);
-    RyanMqttCheckCode(RyanMqttSuccessError == result, result, rlog_d, { goto __exit; });
+    if (NULL == clientConfig->password)
+    {
+        client->config.password = NULL;
+    }
+    else
+    {
+        result = setConfigValue(&client->config.password, clientConfig->password);
+        RyanMqttCheckCode(RyanMqttSuccessError == result, result, rlog_d, { goto __exit; });
+    }
 
     result = setConfigValue(&client->config.host, clientConfig->host);
     RyanMqttCheckCode(RyanMqttSuccessError == result, result, rlog_d, { goto __exit; });

+ 7 - 6
test/RyanMqttTestLinux.c

@@ -3,8 +3,13 @@
 // #define RyanMqttHost ("broker.emqx.io")    // 填写你的mqtt服务器ip
 #define RyanMqttHost ("127.0.0.1") // 填写你的mqtt服务器ip
 #define RyanMqttPort ("1883")      // mqtt服务器端口
-#define RyanMqttUserName ("")      // 为空时填写""
-#define RyanMqttPassword ("")      // 为空时填写""
+#define RyanMqttUserName (NULL)    // 填写你的用户名
+#define RyanMqttPassword (NULL)    // 填写你的密码
+
+#define rlogEnable 1             // 是否使能日志
+#define rlogColorEnable 1        // 是否使能日志颜色
+#define rlogLevel (rlogLvlDebug) // 日志打印等级
+#define rlogTag "RyanMqttTest"   // 日志tag
 
 #include <stdio.h>
 #include <stdint.h>
@@ -13,10 +18,6 @@
 #include <unistd.h>
 #include <semaphore.h>
 
-#define rlogEnable 1             // 是否使能日志
-#define rlogColorEnable 1        // 是否使能日志颜色
-#define rlogLevel (rlogLvlDebug) // 日志打印等级
-#define rlogTag "RyanMqttTest"   // 日志tag
 #include "RyanMqttLog.h"
 #include "RyanMqttClient.h"