瀏覽代碼

log组件修改

ryancw 2 年之前
父節點
當前提交
9043e9a5ae

+ 5 - 0
.vscode/settings.json

@@ -0,0 +1,5 @@
+{
+    "files.associations": {
+        "string.h": "c"
+    }
+}

+ 46 - 36
common/RyanMqttLog.h

@@ -1,18 +1,9 @@
 #ifndef __RyanMqttLog__
 #define __RyanMqttLog__
 
-#include <stdarg.h>
 #include <stdio.h>
-#include "RyanMqttPublic.h"
-
-#define RyanLogPrintf(fmt, ...) printf
-// #define RyanLogPrintf(fmt, ...)                                                                 \
-//     do                                                                                          \
-//     {                                                                                           \
-//         char str[256];                                                                          \
-//         snprintf(str, sizeof(str), fmt, ##__VA_ARGS__);                                         \
-//         Ql_UART_Write((Enum_SerialPort)(UART_PORT0), (u8 *)(str), strlen((const char *)(str))); \
-//     } while (0)
+#include <stdarg.h>
+#include "platformSystem.h"
 
 // 日志等级
 #define rlogLvlError 0
@@ -40,35 +31,54 @@
 #define rlogTag "LOG"
 #endif
 
-// RyanLogPrintf("\033[字背景颜色;字体颜色m  用户字符串 \033[0m" );
-#if rlogColorEnable > 0
-#define rlogColorStart(color_n) RyanLogPrintf("\033[" #color_n "m")
-#define rlogColorEnd RyanLogPrintf("\033[0m")
-#else
-#define rlogColorStart(color_n)
-#define rlogColorEnd
-#endif
-
-#if rlogEnable > 0
-
 /**
  * @brief 日志相关
  *
  */
-#define rlog_output(lvl, color_n, fmt, ...)   \
-    do                                        \
-    {                                         \
-        rlogColorStart(color_n);              \
-        RyanLogPrintf("[" lvl "/" rlogTag "]" \
-                      " %s:%d ",              \
-                      __FILE__,               \
-                      __LINE__);              \
-        RyanLogPrintf(fmt, ##__VA_ARGS__);    \
-        rlogColorEnd;                         \
-        RyanLogPrintf("\r\n");                \
-    } while (0)
-
-#define rlog_output_raw(...) RyanLogPrintf(__VA_ARGS__);
+#if rlogEnable > 0
+static void rlog_output(char *lvl, uint8_t color_n, char *const fmt, ...)
+{
+    // RyanLogPrintf("\033[字背景颜色;字体颜色m  用户字符串 \033[0m" );
+    char dbgBuffer[386];
+    uint16_t len;
+
+// 打印颜色
+#if rlogColorEnable > 0
+    len = snprintf(dbgBuffer, sizeof(dbgBuffer), "\033[%dm", color_n);
+    platformPrint(dbgBuffer, len);
+#endif
+
+    // 打印提示符
+    len = snprintf(dbgBuffer, sizeof(dbgBuffer), "[%s/%s]", lvl, rlogTag);
+    platformPrint(dbgBuffer, len);
+
+    // 打印用户输入
+    va_list args;
+    va_start(args, fmt);
+    len = vsnprintf(dbgBuffer, sizeof(dbgBuffer), fmt, args);
+    va_end(args);
+    platformPrint(dbgBuffer, len);
+
+// 打印颜色
+#if rlogColorEnable > 0
+    platformPrint("\033[0m", sizeof("\033[0m"));
+#endif
+
+    platformPrint("\r\n", sizeof("\r\n"));
+}
+
+static void rlog_output_raw(char *const fmt, ...)
+{
+    char dbgBuffer[256];
+    uint16_t len;
+
+    va_list args;
+    va_start(args, fmt);
+    len = vsnprintf(dbgBuffer, sizeof(dbgBuffer), fmt, args);
+    va_end(args);
+
+    platformPrint(dbgBuffer, len);
+}
 
 #else
 #define rlog_output(lvl, color_n, fmt, ...)

+ 16 - 16
example/RyanMqttTest.c

@@ -66,7 +66,7 @@ void mqttEventHandle(void *pclient, RyanMqttEventId_e event, const void const *e
         break;
 
     case RyanMqttEventDisconnected:
-        rlog_w("mqtt断开连接回调 %d", *(RyanMqttConnectAccepted *)eventData);
+        rlog_w("mqtt断开连接回调 %d", *(RyanMqttConnectStatus_e *)eventData);
         break;
 
     case RyanMqttEventSubscribed:
@@ -175,7 +175,7 @@ void mqttEventHandle(void *pclient, RyanMqttEventId_e event, const void const *e
     {
         // 这里选择直接丢弃该消息
         RyanMqttAckHandler_t *ackHandler = (RyanMqttAckHandler_t *)eventData;
-        rlog_w("ack重发次数超过警戒值回调 packetType: %d, packetId: %d, topic: %s, qos: %d" ackHandler->packetType, ackHandler->packetId, ackHandler->msgHandler->topic, ackHandler->msgHandler->qos);
+        rlog_w("ack重发次数超过警戒值回调 packetType: %d, packetId: %d, topic: %s, qos: %d", ackHandler->packetType, ackHandler->packetId, ackHandler->msgHandler->topic, ackHandler->msgHandler->qos);
         RyanMqttDiscardAckHandler(client, ackHandler->packetType, ackHandler->packetId);
 
         break;
@@ -222,10 +222,10 @@ int mqttConnectFun()
         .autoReconnectFlag = RyanMqttTrue,
         .cleanSessionFlag = RyanMqttFalse,
         .reconnectTimeout = 3000,
-        .recvTimeout = 11000,
+        .recvTimeout = 5000,
         .sendTimeout = 2000,
         .ackTimeout = 10000,
-        .keepaliveTimeoutS = 60,
+        .keepaliveTimeoutS = 120,
         .mqttEventHandle = mqttEventHandle,
         .userData = NULL};
 
@@ -585,18 +585,18 @@ static int Mqttdata(int argc, char *argv[])
 static const struct RyanMqttCmdDes cmdTab[] =
     {
         {"help", "打印帮助信息", MqttHelp},
-        {"state", "打印mqtt客户端状态", MqttState},
-        {"connect", "mqtt客户端连接服务器", MqttConnect},
-        {"disc", "mqtt客户端断开连接", MqttDisconnect},
-        {"reconnect", "mqtt断开连接时,重新连接mqtt服务器", MqttReconnect},
-        {"destory", "mqtt销毁客户端", MqttDestroy},
-        {"pub", "mqtt发布消息", Mqttpublish},
-        {"sub", "mqtt订阅主题", Mqttsubscribe},
-        {"unsub", "mqtt取消订阅主题", MqttUnSubscribe},
-        {"listsub", "mqtt获取已订阅主题", MqttListSubscribe},
-        {"listack", "打印ack链表", MqttListAck},
-        {"listmsg", "打印msg链表", MqttListMsg},
-        {"data", "打印测试信息,用户自定义的", Mqttdata},
+        {"state", "打印mqtt客户端状态   params: null", MqttState},
+        {"connect", "mqtt客户端连接服务器   params: null", MqttConnect},
+        {"disc", "mqtt客户端断开连接    params: null", MqttDisconnect},
+        {"reconnect", "mqtt断开连接时,重新连接mqtt服务器    params: null", MqttReconnect},
+        {"destory", "mqtt销毁客户端    params: null", MqttDestroy},
+        {"pub", "mqtt发布消息   params: topic、 qos、 payload内容、 发送条数、 间隔时间(可以为0)", Mqttpublish},
+        {"sub", "mqtt订阅主题   params: topic、 qos", Mqttsubscribe},
+        {"unsub", "mqtt取消订阅主题    params: 取消订阅主题", MqttUnSubscribe},
+        {"listsub", "mqtt获取已订阅主题    params: null", MqttListSubscribe},
+        {"listack", "打印ack链表    params: null", MqttListAck},
+        {"listmsg", "打印msg链表    params: null", MqttListMsg},
+        {"data", "打印测试信息,用户自定义的    params: null", Mqttdata},
 };
 
 static int MqttHelp(int argc, char *argv[])

+ 40 - 1
platform/quecOpen/platformNetwork.c

@@ -12,11 +12,32 @@
 #define tcpSend (RyanMqttBit2)
 #define tcpClose (RyanMqttBit3)
 #define tcpRecv (RyanMqttBit4)
+#define GetIPByHostName (RyanMqttBit5)
 
 static osEventFlagsId_t mqttNetEventHandle;
 static const osEventFlagsAttr_t mqttNetEvent_attributes = {
     .name = "mqttNetEvent"};
 
+static char resolveIp[64] = {0};
+
+static void callback_socket_GetIPByHostName(u8 contexId, s32 errCode, u32 ipAddrCnt, u8 *ipAddr)
+{
+    if (errCode == SOC_SUCCESS_OK)
+    {
+        memset(resolveIp, 0, sizeof(resolveIp));
+        for (int i = 0; i < ipAddrCnt; i++)
+        {
+            strcpy((char *)resolveIp, (char *)ipAddr);
+            rlog_i("socket 获取ip成功: num_entry=%d, resolve_ip:[%s]", i, (u8 *)resolveIp);
+        }
+        osEventFlagsSet(mqttNetEventHandle, GetIPByHostName);
+    }
+    else
+    {
+        rlog_e("socket 获取ip失败: %d", errCode);
+    }
+}
+
 static void callback_socket_connect(s32 socketId, s32 errCode, void *customParam)
 {
     platformNetwork_t *platformNetwork = (platformNetwork_t *)customParam;
@@ -94,6 +115,24 @@ RyanMqttError_e platformNetworkConnect(void *userData, platformNetwork_t *platfo
         goto __exit;
     }
 
+    // 解析域名
+    s32 getHostIpResult = Ql_IpHelper_GetIPByHostName(0,
+                                                      (u8 *)host,
+                                                      callback_socket_GetIPByHostName);
+    if (SOC_SUCCESS_OK != getHostIpResult && SOC_NONBLOCK != getHostIpResult)
+    {
+        rlog_w("aaaaaaaaaaaaaaa");
+        result = RyanMqttSocketConnectFailError;
+        goto __exit;
+    }
+
+    eventId = osEventFlagsWait(mqttNetEventHandle, GetIPByHostName, osFlagsWaitAny, 10000);
+    if (GetIPByHostName != eventId)
+    {
+        result = RyanMqttSocketConnectFailError;
+        goto __exit;
+    }
+
     // 创建socket
     platformNetwork->socket = Ql_SOC_Create(0, SOC_TYPE_TCP);
     if (platformNetwork->socket < 0)
@@ -103,7 +142,7 @@ RyanMqttError_e platformNetworkConnect(void *userData, platformNetwork_t *platfo
     }
 
     // 等待连接成功
-    s32 connectResult = Ql_SOC_Connect(platformNetwork->socket, (u8 *)host, atoi(port));
+    s32 connectResult = Ql_SOC_Connect(platformNetwork->socket, (u8 *)resolveIp, atoi(port));
     if (SOC_SUCCESS_OK != connectResult && SOC_NONBLOCK != connectResult)
     {
         platformNetworkClose(userData, platformNetwork);

+ 1 - 0
platform/quecOpen/platformNetwork.h

@@ -21,6 +21,7 @@ extern "C"
 #include "ql_socket.h"
 #include "ql_urc_register.h"
 
+
     typedef struct
     {
         int socket;

+ 6 - 1
platform/quecOpen/platformSystem.c

@@ -11,6 +11,11 @@ void platformMemoryFree(void *ptr)
     free(ptr);
 }
 
+void platformPrint(char *str, uint16_t strLen)
+{
+    printf("%.*s", strLen, str);
+}
+
 /**
  * @brief ms延时
  *
@@ -169,4 +174,4 @@ void platformCriticalExit(void)
 {
     // rt_exit_critical();
     osKernelUnlock();
-}
+}

+ 3 - 0
platform/quecOpen/platformSystem.h

@@ -8,6 +8,7 @@ extern "C"
 #endif
 
 #include <stdio.h>
+
 #include <stdint.h>
 #include <assert.h>
 #include "cmsis_os2.h"
@@ -17,6 +18,7 @@ extern "C"
 
     typedef struct
     {
+
         osThreadId_t thread;
     } platformThread_t;
 
@@ -28,6 +30,7 @@ extern "C"
     extern void *platformMemoryMalloc(size_t size);
     extern void platformMemoryFree(void *ptr);
 
+    extern void platformPrint(char *str, uint16_t strLen);
     extern void platformDelay(uint32_t ms);
 
     extern RyanMqttError_e platformThreadInit(void *userData,

+ 4 - 4
platform/rtthread/platformNetwork.c

@@ -1,7 +1,7 @@
-#define rlogEnable 1               // 是否使能日志
-#define rlogColorEnable 1          // 是否使能日志颜色
+#define rlogEnable 1             // 是否使能日志
+#define rlogColorEnable 1        // 是否使能日志颜色
 #define rlogLevel (rlogLvlWarning) // 日志打印等级
-#define rlogTag "RyanMqttNet"      // 日志tag
+#define rlogTag "RyanMqttNet"    // 日志tag
 
 #include "platformNetwork.h"
 #include "RyanMqttLog.h"
@@ -95,7 +95,7 @@ RyanMqttError_e platformNetworkRecvAsync(void *userData, platformNetwork_t *plat
 
         recvResult = recv(platformNetwork->socket, recvBuf + offset, recvLen - offset, 0);
 
-        if (recvResult < 0) // 小于零,表示错误,个别错误不代表socket错误
+        if (recvResult <= 0) // 小于零,表示错误,个别错误不代表socket错误
         {
             // 下列3种表示没问题,但需要推出发送
             if ((errno == EAGAIN ||      // 套接字已标记为非阻塞,而接收操作被阻塞或者接收超时

+ 8 - 1
platform/rtthread/platformSystem.c

@@ -22,6 +22,13 @@ void platformDelay(uint32_t ms)
     rt_thread_mdelay(ms);
 }
 
+void platformPrint(char *str, uint16_t strLen)
+{
+    printf("%.*s", strLen, str);
+}
+
+#define RyanLogPrintf(str, strlen) printf("%.*s", strlen, str)
+
 /**
  * @brief 初始化并运行线程
  *
@@ -166,4 +173,4 @@ void platformCriticalEnter(void)
 void platformCriticalExit(void)
 {
     rt_exit_critical();
-}
+}

+ 1 - 0
platform/rtthread/platformSystem.h

@@ -32,6 +32,7 @@ extern "C"
     extern void *platformMemoryMalloc(size_t size);
     extern void platformMemoryFree(void *ptr);
 
+    extern void platformPrint(char *str, uint16_t strLen);
     extern void platformDelay(uint32_t ms);
 
     extern RyanMqttError_e platformThreadInit(void *userData,