修改pbuf_free 增加锁控范围 2094/2 2094/head 2104/target/1
authorhcj <1224667075@qq.com>
Tue, 19 Aug 2025 10:26:45 +0000 (18:26 +0800)
committerhcj <1224667075@qq.com>
Tue, 19 Aug 2025 10:26:45 +0000 (18:26 +0800)
code/sdk/drivers/peripheral/slip/cat1Service/bk_websocket.h
code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp
code/sdk/drivers/peripheral/slip/cat1Service/cat1_ws.cpp
code/sdk/open_source/openharmony/third_party/lwip/src/core/pbuf.c

index 69cbde0..1fb18dd 100644 (file)
@@ -20,7 +20,7 @@ typedef ws_opcode_t WebSocketMessageType;
 typedef void (*on_ws_packet_callback)(uint8_t* data, size_t datalen, WebSocketMessageType type);
 
 /* WebSocket消息队列系统 */
-#define WS_QUEUE_SIZE 48
+#define WS_QUEUE_SIZE 128
 #define WS_MESSAGE_MAX_SIZE 1024
 
 typedef struct {
index 59e749b..d581c4e 100644 (file)
@@ -371,14 +371,20 @@ uint8 tts_break =0;
 /**
  * @brief 初始化WebSocket消息队列
  */
-static int ws_queue_init(ws_message_queue_t* queue)
+static int ws_queue_init(ws_message_queue_t* queue,uint32_t queue_size)
 {
     if (!queue) return -1;
     
     memset(queue, 0, sizeof(ws_message_queue_t));
     
     /* 创建osal消息队列 */
-    int ret = osal_msg_queue_create("ws_queue", WS_QUEUE_SIZE, &queue->queue_id, 0, sizeof(ws_message_item_t));
+    uint32_t true_queue_size;
+    if(queue_size!=0)
+    true_queue_size=queue_size;
+    else
+    true_queue_size=WS_QUEUE_SIZE;
+
+    int ret = osal_msg_queue_create("ws_queue", true_queue_size, &queue->queue_id, 0, sizeof(ws_message_item_t));
     if (ret != OSAL_SUCCESS) {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create osal message queue for ws queue");
         return -1;
@@ -487,9 +493,9 @@ static int ws_queues_init_all(void)
         ws_queue_deinit(&tts_msg_queue);
     }
     
-    ret |= ws_queue_init(&chat_msg_queue);
-    ret |= ws_queue_init(&asr_msg_queue);
-    ret |= ws_queue_init(&tts_msg_queue);
+    ret |= ws_queue_init(&chat_msg_queue,256);
+    ret |= ws_queue_init(&asr_msg_queue,0);
+    ret |= ws_queue_init(&tts_msg_queue,0);
     // ret |= ws_queue_init(&voice_msg_queue);
     
     if (ret != 0) {
@@ -627,6 +633,8 @@ void uapi_UI_stop_asr(void)
     }
     #endif
 
+    uapi_start_tts();
+
     if(stop_AudioCaptureProcess_AI())
     {
         asr_is_stop=1;
@@ -1790,15 +1798,15 @@ bool tts_ws_url_init(char * put_buf)
    
 }
 
-
-
+char tts_task_id_buf[128]={0};
+uint8_t tts_is_ready=0;
 
 void uapi_start_tts(void)
 {
     char *jsonString =
     "{\"header\": \
      { \"event\": \"StartSynthesis\",\
-     \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\"},\
+     \"trace_id\": \"\"},\
      \"payload\": \
     {\"voice\":\"xiaoyun\",\
      \"format\":\"mp3\",\
@@ -1807,33 +1815,44 @@ void uapi_start_tts(void)
      \"speech_rate\":0}\
      }";
 
-     printf("uapi_start_tts : %s\r\n",jsonString);
+     printf("uapi_start_tts :%s\r\n",jsonString);
+
 
     if (send_websocket_message(tts_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0)
+{
+        memset_s(tts_task_id_buf,128,0,128);
+        tts_is_ready=0;
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
 }
+    
+}
 
 
 void uapi_stop_tts(void)
 {
     char *jsonString =
    " { \
-     \"header \": { \
+     \"header\": { \
         \"event\": \"StopSynthesis\", \
-        \"trace_id\": \" 3fa6b9e6a30b407ea38ad7ae69e6c291 \" \
+        \"trace_id\": \"\" \
     } \
     } ";
 
      printf("uapi_start_tts : %s\r\n",jsonString);
 
     if (send_websocket_message(tts_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0)
+{
+        tts_is_ready=0;
     WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"send_websocket_message succ 0\n");
 }
+   
+}
 
 
 
 void uapi_start_tts_single(void)
 {
+
     char *jsonString =
     " {  \"header\": {  \
         \"event\": \"LongTextStream\",\
@@ -1856,38 +1875,44 @@ void uapi_start_tts_single(void)
 }
 
  
-bool uapi_start_tts_single_txt(char *buf)
+void uapi_start_tts_single_txt(char *buf)
 {
+
+     if(tts_is_ready!=1)
+    {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"uapi_start_tts_single_txt no tts_is_ready \n");
+        return;
+    }
     
 
     char *jsonString = (char *)malloc(strlen(buf)+300);// [512] ={0};
  
     sprintf(jsonString,
 
-    // " {\" header\" : { \
-    //     \"event\": \" RunSynthesis\", \
-    //     \"trace_id \": \" 3fa6b9e6a30b407ea38ad7ae69e6c291 \" \
-    // }, \
-    // \"payload \": { \
-    //     \"text \":\" %s \" \
-    // }}"
-
-     " {  \"header\": { \
-        \"event\": \"LongTextStream\", \
-        \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\"\
-    },\
-    \"payload\": {\
-        \"voice\":\"xiaoyun\",\
-        \"format\":\"mp3\",\
-        \"sample_rate\":16000,\
-        \"volume\":80,\
-        \"speech_rate\":0,\
-        \"text\":\"%s\"\
-     } } "
+    "{\"header\" : { \
+    \"event\": \"RunSynthesis\", \
+    \"trace_id\": \"%s\" \
+    }, \
+    \"payload\": { \
+    \"text\":\"%s\" \
+    }}"
+
+     //  "{  \"header\": { \
+        //     \"event\": \"LongTextStream\", \
+        //     \"trace_id\": \"3fa6b9e6a30b407ea38ad7ae69e6c291\"\
+    // },\
+    // \"payload\": {\
+        //     \"voice\":\"xiaoyun\",\
+        //     \"format\":\"mp3\",\
+        //     \"sample_rate\":16000,\
+        //     \"volume\":80,\
+        //     \"speech_rate\":0,\
+        //     \"text\":\"%s\"\
+     //  } }"
      
-     ,buf);
+     ,tts_task_id_buf,buf);
 
-     printf("uapi_start_tts : %s\r\n",jsonString);
+     printf("uapi_start_tts_single_txt :%s\r\n",jsonString);
 
     if (send_websocket_message(tts_client,( uint8_t *)jsonString,strlen(jsonString),WS_TEXT)==0)
     {
@@ -1971,6 +1996,49 @@ void tts_paly()
         fclose(fp);
     }
 }
+//  {"header":{"event":"StartedSynthesis","msg":"success","status":20000000,"task_id":"8e8996f9148b66738309602ec3219412"}}
+bool tts_analysis(char *buf)
+{
+    cJSON* root = cJSON_Parse((const char *)buf);
+    if (root == NULL) {
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"解析失败\n");
+        return false;
+    }
+    char *event_str=NULL;
+    char *task_id_str=NULL;
+    char *msg_str=NULL;
+
+    
+
+     cJSON* header = cJSON_GetObjectItem(root, "header");
+     if(header)
+     {
+         cJSON* event = cJSON_GetObjectItem(header, "event");
+        if (event && cJSON_IsString(event)) {
+            event_str= event->valuestring;
+        }
+
+        cJSON* msg = cJSON_GetObjectItem(header, "msg");
+        if (msg && cJSON_IsString(msg)) {
+            msg_str= msg->valuestring;
+
+            if(strcmp("StartedSynthesis",event_str)==0 && strcmp("success",msg_str)==0)
+            {   
+                tts_is_ready=1;
+            }
+        }
+
+        cJSON* task_id = cJSON_GetObjectItem(header, "task_id");
+
+        if (task_id && cJSON_IsString(task_id)) 
+        {
+             memcpy_s(tts_task_id_buf,128,task_id->valuestring,strlen(task_id->valuestring));
+        }
+     }
+    cJSON_Delete(root);
+    return true;
+}
+
 
 uint8_t  is_bk_websocket_tts=0;
 
@@ -2151,9 +2219,13 @@ TTS_SATRT:
                     WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS binary data received: %d bytes", len);
                     msleep(1);
                 } else if (type == WS_TEXT) {
-                    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS text data received: %s %d %d", tts_recv_buf,bk_websocket_tts_tick,osKernelGetTickCount());
-                    send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf, len, 201);
+                    WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS text data received: %s", tts_recv_buf);
+                    // send_msg_data_into_Ai_msg_queue((uint8_t *)tts_recv_buf, len, 201);
                     bk_websocket_tts_tick = 0;
+                    if(tts_analysis(tts_recv_buf))
+                    {
+                        // WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "TTS text tts_analysis: %s", tts_recv_buf);
+                    }
                 } else if (type == WS_PONG) {
                     tts_client_ping = 0;
                 }
index 38f1edd..5491fdd 100644 (file)
@@ -594,7 +594,7 @@ int send_websocket_message(WebSocketClient *client, uint8_t *message, size_t mes
     offset += message_len;
 
     // 发送帧
-    WS_LOG_DEBUG("offset %d", offset);
+    WS_LOG_INFO("offset %d", offset);
     
     // 再次检查TCP句柄有效性
     if (client->tcp_handle == NULL) {
@@ -602,6 +602,8 @@ int send_websocket_message(WebSocketClient *client, uint8_t *message, size_t mes
         ret = -1;
         goto send_websocket_message_ERROR;
     }
+
+    
     
     ret_tcp_send_data=tcp_send_data(client->tcp_handle, frame, offset);
     if (ret_tcp_send_data < 0) {
@@ -689,6 +691,10 @@ void _tcp_on_disconnect_cb(tcp_handle_t handle)
     if(client != NULL)
     {
         WS_LOG_INFO("Cleaning up WebSocket client for disconnected handle %d", handle);
+
+        if (client->packet_callback) {
+            client->packet_callback((uint8_t*)("close"), strlen("close"), (WebSocketMessageType)WS_CLOSE);
+        }
         
         // 清理客户端状态
         client->handshake = 0;
@@ -698,6 +704,8 @@ void _tcp_on_disconnect_cb(tcp_handle_t handle)
         if (client->parser) {
             ws_parser_reset(client->parser);
         }
+
+
     }
 }
 void _tcp_on_data_received_cb(tcp_handle_t handle, uint8_t* data, uint16_t datalen)
index 7638dfd..b1c4857 100644 (file)
@@ -727,14 +727,22 @@ pbuf_free(struct pbuf *p)
   u8_t alloc_src;
   struct pbuf *q;
   u8_t count;
+  SYS_ARCH_DECL_PROTECT(global_level);
+
+  /* 立即获取全局保护锁,防止多线程同时调用pbuf_free */
+  SYS_ARCH_PROTECT(global_level);
 
   if (p == NULL) {
     LWIP_ASSERT("p != NULL", p != NULL);
     /* if assertions are disabled, proceed with debug output */
     LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
                 ("pbuf_free(p == NULL) was called.\n"));
+    
+    /* 释放全局保护锁 */
+    SYS_ARCH_UNPROTECT(global_level);
     return 0;
   }
+  
   LWIP_DEBUGF(PBUF_DEBUG | LWIP_DBG_TRACE, ("pbuf_free(%p)\n", (void *)p));
 
   PERF_START;
@@ -794,7 +802,12 @@ pbuf_free(struct pbuf *p)
       p = NULL;
     }
   }
+  
   PERF_STOP("pbuf_free");
+  
+  /* 释放全局保护锁 */
+  SYS_ARCH_UNPROTECT(global_level);
+  
   /* return number of de-allocated pbufs */
   return count;
 }