From 246dcb2c00583927bb7512c2b6e7535a17ea2a43 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=AD=99=E6=98=A5=E9=98=B3?= Date: Sat, 16 Aug 2025 08:24:23 +0800 Subject: [PATCH] =?utf8?q?=E4=BC=98=E5=8C=96ai=5Fserver=EF=BC=9A=201?= =?utf8?q?=E3=80=81asr=E6=9C=8D=E5=8A=A1=E4=BF=9D=E6=8C=81=E8=BF=90?= =?utf8?q?=E8=A1=8Cai=E5=AF=B9=E8=AF=9D=E6=9C=9F=E9=97=B4=E4=B8=8D?= =?utf8?q?=E4=B8=AD=E6=96=AD=202=E3=80=81=E4=BF=AE=E5=A4=8Dqueue=20semapho?= =?utf8?q?nre=20=EF=BC=8Ctcp=20manager=20=E6=B8=85=E9=99=A4=E4=B8=8D?= =?utf8?q?=E5=87=80=203=E3=80=81=E5=A2=9E=E5=A4=A7=E5=BD=95=E9=9F=B3?= =?utf8?q?=E5=8F=91=E9=80=81=E7=BC=93=E5=AD=98=EF=BC=8C=E4=BC=98=E5=8C=96?= =?utf8?q?=E5=A3=B0=E9=9F=B3=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../ble_service/bleservice/bleService.c | 2 +- .../slip/cat1Service/cat1_ai_server.cpp | 66 ++++++++++++++----- 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c index 1b2097d81..12d13d4ce 100644 --- a/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c +++ b/code/sdk/application/wearable/service/ble_service/bleservice/bleService.c @@ -2617,7 +2617,7 @@ int send_msg_data_into_Ai_msg_queue(void *buff,uint16_t len,uint8_t type) // uint32_t singlebuflen= ble_nus_max_data_len-20; uint32_t singlebuflen = 0; if(getselect4gorbtai() == 1) { - singlebuflen = 288*2;//4G + singlebuflen = 288*4;//4G } else { singlebuflen = ble_nus_max_data_len-20;//BT } diff --git a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp index 2d8126554..a7d322422 100644 --- a/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp +++ b/code/sdk/drivers/peripheral/slip/cat1Service/cat1_ai_server.cpp @@ -250,6 +250,7 @@ static bool wait_for_tcp_connection(WebSocketClient* client, const char* client_ extern bool read_dev_token(char *data_token); errcode_t ai_system_init(void); +void ai_system_deinit(void); void ai_cleanup_resources(void); uint32_t ai_get_memory_usage(void); uint32_t ai_get_memory_usage_percent(void); @@ -386,8 +387,12 @@ static int ws_queue_init(ws_message_queue_t* queue) /* 创建信号量 */ queue->semaphore = osSemaphoreNew(WS_QUEUE_SIZE, 0, NULL); if (!queue->semaphore) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create semaphore for ws queue"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create semaphore for ws queue (size=%d)", WS_QUEUE_SIZE); + + + osMutexDelete(queue->mutex); + queue->mutex = NULL; return -1; } @@ -530,6 +535,14 @@ static int ws_queues_init_all(void) { int ret = 0; + // 检查是否已经初始化,避免重复初始化 + if (chat_msg_queue.initialized || asr_msg_queue.initialized || tts_msg_queue.initialized) { + WEARABLE_LOGW(WEARABLE_LOG_MODULE_APP, "WebSocket queues already initialized, deinitializing first"); + ws_queue_deinit(&chat_msg_queue); + ws_queue_deinit(&asr_msg_queue); + 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); @@ -614,6 +627,7 @@ void uapi_UI_start_asr(uint8_t type) asr_need_stop=0; asr_server_init(); + uapi_start_asr(); if(chat_client==NULL) { @@ -931,13 +945,14 @@ void bk_websocket_demo(void *data) { uint32_t start_time = (uint32_t)osKernelGetTickCount(); uint32_t retry_count = 0; - // 初始化互斥锁 + // 检查互斥锁是否已存在,避免重复创建 if (!g_ai_mutex) { g_ai_mutex = osMutexNew(NULL); if (!g_ai_mutex) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create AI mutex"); return; } + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI mutex created in bk_websocket_demo"); } AI_LOCK(); @@ -1434,7 +1449,7 @@ bool aer_result_analysis(char *buf) if(strcmp(event_str,"TranscriptionCompleted")==0) //asr 结束 { cJSON_Delete(root); - return false; + return true; // 返回true asr不断连 } if(strcmp(event_str,"Failed")==0) { @@ -1486,7 +1501,7 @@ void bk_websocket_asr(void *data) { WebSocketMessageType type; bool state = true; - +ASR_SATRT: AI_LOCK(); is_bk_websocket_asr = 1; AI_UNLOCK(); @@ -1556,7 +1571,7 @@ void bk_websocket_asr(void *data) { break; } -msleep(1000); + msleep(1000); continue; } else { WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "ASR WebSocket connection established"); @@ -1639,7 +1654,7 @@ WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "等待ASR WebSocket握手完成... (%d m // timeout.tv_usec = (AI_RECEIVE_TIMEOUT_MS % 1000) * 1000; // lwip_setsockopt(asr_client->sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - uapi_start_asr(); + // uapi_start_asr(); uint32_t last_activity = (uint32_t)osKernelGetTickCount(); @@ -1699,7 +1714,10 @@ WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "等待ASR WebSocket握手完成... (%d m msleep(10); // 避免过度占用CPU } - + if (disconnect_server == 0) { + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "Reconnecting ASR client..."); + goto ASR_SATRT; + } AI_LOCK(); set_ai_asr_service_init_flag(0); AI_UNLOCK(); @@ -2435,7 +2453,12 @@ bool init_ai_task(uint8_t type) ai_cleanup_resources(); return false; } - + // 初始化ASR服务器 + if (asr_server_init() != ERRCODE_SUCC) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize asr server"); + ai_cleanup_resources(); + return false; + } WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI task initialized successfully, type: %d", type); } @@ -2469,8 +2492,9 @@ bool dinit_ai_task(void) tts_server_deinit(); // 清理AI系统资源 - ai_cleanup_resources(); + ai_cleanup_resources(); + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI task deinitialized successfully"); } @@ -2996,7 +3020,7 @@ void ai_cleanup_resources(void) { // } // 清理WebSocket消息队列 - // ws_queues_deinit_all(); + ws_queues_deinit_all(); // 清理互斥锁 if (g_ai_mutex) { @@ -3016,7 +3040,7 @@ void ai_cleanup_resources(void) { AI_UNLOCK(); // 最后清理TCP管理器 - // tcp_simple_manager_deinit(); + tcp_simple_manager_deinit(); WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI resources cleaned up"); } @@ -3030,12 +3054,17 @@ errcode_t ai_system_init(void) { return ERRCODE_FAIL; } - // 创建互斥锁 - g_ai_mutex = osMutexNew(NULL); + // 创建互斥锁(检查是否已存在) if (!g_ai_mutex) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create AI mutex"); - tcp_simple_manager_deinit(); // 清理TCP管理器 - return ERRCODE_FAIL; + g_ai_mutex = osMutexNew(NULL); + if (!g_ai_mutex) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create AI mutex"); + // tcp_simple_manager_deinit(); // 清理TCP管理器 + return ERRCODE_FAIL; + } + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI mutex created in ai_system_init"); + } else { + WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "AI mutex already exists, reusing"); } // 初始化内存使用统计 @@ -3044,7 +3073,10 @@ errcode_t ai_system_init(void) { // 初始化WebSocket消息队列 if (ws_queues_init_all() != 0) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to initialize all WebSocket queues"); - osMutexDelete(g_ai_mutex); + if (g_ai_mutex) { + osMutexDelete(g_ai_mutex); + g_ai_mutex = NULL; + } tcp_simple_manager_deinit(); // 清理TCP管理器 return ERRCODE_FAIL; } -- 2.17.1