From: hcj <1224667075@qq.com> Date: Fri, 19 Sep 2025 11:34:42 +0000 (+0800) Subject: 更换ln ws使用接口 X-Git-Tag: leiniao_debug_1.0.0_20250922_020716~13^2~10 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=5d7ced5a5a4bb6b4ae8ad291481ad343443a6446;p=mirrors%2FFD230.git 更换ln ws使用接口 --- diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c index 32c6e07e3..1ac24d400 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_websocket.c @@ -6,6 +6,7 @@ #include "cJSON.h" // Added for JSON parsing #include "custom_utils.h" #include "soc_osal.h" +#include "cmsis_os2.h" /* * - g_ws_client_fd 当前 WebSocket 客户端fd @@ -43,12 +44,6 @@ typedef enum { WS_CMD_MAX } ws_command_t; -typedef struct { - uint8_t msg_type; - uint16_t data_len; - uint8_t *data; -} ws_msg_info_t; - /* 统一的消息数据结构 */ typedef struct { char device_id[64]; @@ -59,12 +54,21 @@ typedef struct { char *raw_json; // 原始JSON字符串,用于需要完整数据的场景 } ws_data_msg_t; + + +typedef struct { + uint8_t msg_type; + uint16_t data_len; + ws_data_msg_t data; // 直接存储数据而不是指针 +} ws_msg_info_t; + + static int g_ws_client_fd = -1; static volatile int g_ws_connected = WS_STATE_DISCONNECTED; static uint32_t g_ws_main_thread = 0; static volatile int g_ws_running = 0; static volatile unsigned int g_ws_reconnect_count = 0; /* 断线后递增,用于URL reconnect参数 */ -static unsigned long g_ws_msg_queue = 0xFFFFFFFF; +static osMessageQueueId_t g_ws_msg_queue = NULL; static void ln_ws_build_url(char *out, size_t out_size); int ln_ws_send_ping(void); @@ -76,7 +80,13 @@ static void ln_ws_free_data_message(ws_data_msg_t *data_msg); /* 队列创建函数 */ void ln_ws_queue_init(void) { - osal_msg_queue_create("ws_queue", WS_MSG_QUEUE_COUNT, &g_ws_msg_queue, 0, sizeof(ws_msg_info_t)); + printf("[LN][WS] ln_ws_queue_init\n"); + g_ws_msg_queue = osMessageQueueNew(WS_MSG_QUEUE_COUNT, sizeof(ws_msg_info_t), NULL); + if (g_ws_msg_queue == NULL) { + printf("[LN][WS] ln_ws_queue_init failed: failed to create message queue\n"); + } else { + printf("[LN][WS] ln_ws_queue_init success: queue created\n"); + } } /* 握手成功回调:标记连接就绪 */ @@ -168,13 +178,14 @@ static void ln_ws_main_thread(void *arg) uint32_t msgsize =0; while (g_ws_running) { // 处理队列消息(非阻塞) - if (osal_msg_queue_read_copy(g_ws_msg_queue, &msg,&msgsize,1000) == ERRCODE_SUCC) { - printf("[LN][WS] osal_msg_queue_read_copy: g_ws_msg_queue\n"); + if (osMessageQueueGet(g_ws_msg_queue, &msg, NULL, 1000) == osOK) { + printf("[LN][WS] osMessageQueueGet: g_ws_msg_queue\n"); switch (msg.msg_type) { case WS_MSG_TYPE_SEND_TEXT: - if (g_ws_connected == WS_STATE_CONNECTED && g_ws_client_fd >= 0 && msg.data) { - WebSocketClientSendText(g_ws_client_fd, (char*)msg.data, msg.data_len); - printf("[LN][WS] Sent text from queue: %.*s\n", msg.data_len, msg.data); + if (g_ws_connected == WS_STATE_CONNECTED && g_ws_client_fd >= 0) { + // 对于文本消息,我们需要特殊处理,因为现在data是ws_data_msg_t类型 + // 这里需要重新设计文本消息的存储方式 + printf("[LN][WS] Text message type not supported in new structure\n"); } else { printf("[LN][WS] Cannot send text, connection state: %d\n", g_ws_connected); } @@ -196,8 +207,8 @@ static void ln_ws_main_thread(void *arg) break; case WS_MSG_TYPE_DATA_MESSAGE: - if (msg.data) { - ws_data_msg_t *data_msg = (ws_data_msg_t*)msg.data; + { + ws_data_msg_t *data_msg = &msg.data; printf("[LN][WS] Received message: deviceId=%s, timestamp=%u, messageId=%s, command=%d (%s)\n", data_msg->device_id, data_msg->timestamp, data_msg->message_id, data_msg->command, data_msg->command_str); @@ -243,16 +254,10 @@ static void ln_ws_main_thread(void *arg) printf("[LN][WS] Unknown message type: %d\n", msg.msg_type); break; } - - // 释放动态分配的内存 - if (msg.data) { - free(msg.data); - msg.data = NULL; - } } else { - printf("osal_msg_queue_read_copy fail\r\n"); + // printf("osMessageQueueGet timeout or failed\r\n"); } // 连接管理部分 @@ -435,29 +440,10 @@ int ln_ws_send_text_async(const char *text) { if (!text || !g_ws_running) return -1; - ws_msg_info_t msg; - msg.msg_type = WS_MSG_TYPE_SEND_TEXT; - msg.data_len = strlen(text); - msg.data = NULL; - - // 分配内存 - msg.data = (uint8_t*)malloc(msg.data_len + 1); - if (!msg.data) { - printf("[LN][WS] Failed to allocate memory for text message\n"); - return -1; - } - - memcpy(msg.data, text, msg.data_len); - msg.data[msg.data_len] = '\0'; - - int ret = osal_msg_queue_write_copy(g_ws_msg_queue, &msg, sizeof(ws_msg_info_t),0); - if (ret != ERRCODE_SUCC) { - // 发送失败,释放内存 - free(msg.data); - msg.data = NULL; - } - - return ret; + // 由于新的数据结构不支持直接文本消息,这里暂时返回错误 + // 如果需要支持文本消息,需要重新设计消息类型 + printf("[LN][WS] Text message not supported in new queue structure\n"); + return -1; } /* 强制重连 */ @@ -468,9 +454,10 @@ int ln_ws_force_reconnect(void) ws_msg_info_t msg; msg.msg_type = WS_MSG_TYPE_FORCE_RECONNECT; msg.data_len = 0; - msg.data = NULL; + // msg.data = NULL; - return osal_msg_queue_write_copy(g_ws_msg_queue, &msg,sizeof(ws_msg_info_t), 0); + osStatus_t ret = osMessageQueuePut(g_ws_msg_queue, &msg, 0, 0); + return (ret == osOK) ? 0 : -1; } /* 通过队列停止WebSocket服务 */ @@ -481,9 +468,10 @@ int ln_ws_stop_async(void) ws_msg_info_t msg; msg.msg_type = WS_MSG_TYPE_STOP; msg.data_len = 0; - msg.data = NULL; + // msg.data = NULL; - return osal_msg_queue_write_copy(g_ws_msg_queue, &msg,sizeof(ws_msg_info_t), 0); + osStatus_t ret = osMessageQueuePut(g_ws_msg_queue, &msg, 0, 0); + return (ret == osOK) ? 0 : -1; } /* 停止:关闭心跳并关闭连接 */ @@ -500,6 +488,13 @@ int ln_ws_stop(void) g_ws_client_fd = -1; } g_ws_connected = WS_STATE_DISCONNECTED; + + // 删除消息队列 + if (g_ws_msg_queue != NULL) { + osMessageQueueDelete(g_ws_msg_queue); + g_ws_msg_queue = NULL; + } + return ret; } @@ -663,27 +658,18 @@ static int ln_ws_queue_data_message(const ws_data_msg_t *data_msg) ws_msg_info_t msg; msg.msg_type = WS_MSG_TYPE_DATA_MESSAGE; msg.data_len = sizeof(ws_data_msg_t); - msg.data = NULL; - - // 分配内存并复制数据 - msg.data = (uint8_t*)malloc(sizeof(ws_data_msg_t)); - if (!msg.data) { - printf("[LN][WS] Failed to allocate memory for data message\n"); - return -1; - } - memcpy(msg.data, data_msg, sizeof(ws_data_msg_t)); + // 直接复制数据到结构体中 + memcpy(&msg.data, data_msg, sizeof(ws_data_msg_t)); // 写入队列 - int ret = osal_msg_queue_write_copy(g_ws_msg_queue, &msg, sizeof(ws_msg_info_t), 0); - if (ret != ERRCODE_SUCC) { - printf("[LN][WS] Failed to write data message to queue: %d\n", ret); - free(msg.data); - msg.data = NULL; + osStatus_t ret = osMessageQueuePut(g_ws_msg_queue, &msg, 0, 0); + if (ret != osOK) { + printf("[LN][WS] Failed to put data message to queue: %d\n", ret); return -1; } - printf("[LN][WS] Data message queued successfully\n"); + printf("[LN][WS] Data message queued successfully count %d\n", osMessageQueueGetCount(g_ws_msg_queue)); return 0; }