From: hcj <1224667075@qq.com> Date: Mon, 27 Oct 2025 10:28:47 +0000 (+0800) Subject: n9ai问题修复 X-Git-Tag: V1.0.9_20251128_194606_LN~95^2~36^2^2~5 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=2dd48576dd6bc37911bd4596c446df3d7e267328;p=mirrors%2FFD230.git n9ai问题修复 --- diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp index 42ac17afb..1e4178807 100644 --- a/code/sdk/application/wearable/fd230/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp @@ -1237,7 +1237,7 @@ void GapAclStateChangedCallback(const bd_addr_t *bdAddr, gap_acl_state_t state, // send to ace network // SetAclConnected(true); #if defined(SUPPORT_LWIP) && defined(SUPPORT_BREDR) - bt_pan_open(); + // bt_pan_open(); #endif net_connect_status_change(NETWORK_TYPE_BLUETOOTH, CONNECTION_STATUS_CONNECTED); /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp index 42ac17afb..1e4178807 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp @@ -1237,7 +1237,7 @@ void GapAclStateChangedCallback(const bd_addr_t *bdAddr, gap_acl_state_t state, // send to ace network // SetAclConnected(true); #if defined(SUPPORT_LWIP) && defined(SUPPORT_BREDR) - bt_pan_open(); + // bt_pan_open(); #endif net_connect_status_change(NETWORK_TYPE_BLUETOOTH, CONNECTION_STATUS_CONNECTED); /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp index 42ac17afb..1e4178807 100644 --- a/code/sdk/application/wearable/n9/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/bluetooth/src/BluetoothModel.cpp @@ -1237,7 +1237,7 @@ void GapAclStateChangedCallback(const bd_addr_t *bdAddr, gap_acl_state_t state, // send to ace network // SetAclConnected(true); #if defined(SUPPORT_LWIP) && defined(SUPPORT_BREDR) - bt_pan_open(); + // bt_pan_open(); #endif net_connect_status_change(NETWORK_TYPE_BLUETOOTH, CONNECTION_STATUS_CONNECTED); /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/zhihui/ai_old/websocket_server_example.cpp b/code/sdk/zhihui/ai_old/websocket_server_example.cpp index 1cd3745e8..b1109468e 100644 --- a/code/sdk/zhihui/ai_old/websocket_server_example.cpp +++ b/code/sdk/zhihui/ai_old/websocket_server_example.cpp @@ -590,6 +590,8 @@ errcode_t start_all_websocket_servers(uint8_t chat_ai_type) */ void stop_all_websocket_servers(void) { + StopAIVoice(); + if (g_asr_server) { ws_server_stop(g_asr_server); } @@ -602,7 +604,6 @@ void stop_all_websocket_servers(void) ws_server_stop(g_chat_server); } - StopAIVoice(); printf("All WebSocket servers stopped\n"); } diff --git a/code/sdk/zhihui/ai_old/websocket_server_manager.cpp b/code/sdk/zhihui/ai_old/websocket_server_manager.cpp index 5103e07dd..4ed0fd81a 100644 --- a/code/sdk/zhihui/ai_old/websocket_server_manager.cpp +++ b/code/sdk/zhihui/ai_old/websocket_server_manager.cpp @@ -161,9 +161,6 @@ void ws_server_destroy(ws_server_handle_t *handle) return; } - // 停止服务器 - ws_server_stop(handle); - // 清理资源 ws_server_cleanup(handle); @@ -171,9 +168,9 @@ void ws_server_destroy(ws_server_handle_t *handle) if (g_ws_servers[handle->type] == handle) { g_ws_servers[handle->type] = NULL; } + printf("WebSocket server destroyed: type=%d\n", handle->type); free(handle); - printf("WebSocket server destroyed: type=%d\n", handle->type); } /** @@ -203,6 +200,7 @@ errcode_t ws_server_start(ws_server_handle_t *handle) task_cfg.attr.stack_size =0x2000;// handle->config.stack_size; task_cfg.func = ws_server_thread_func; task_cfg.use_mem = USE_EXTEND_MEM; + task_cfg.task_handle = NULL; // 初始化为NULL,osThreadNew会设置 // 创建线程 errcode_t ret = uapi_create_osThread_custom(&task_cfg); @@ -212,6 +210,9 @@ errcode_t ws_server_start(ws_server_handle_t *handle) return ret; } + // 保存线程句柄(task_cfg.task_handle 本身就是线程句柄) + handle->thread_handle = (osThreadId_t)task_cfg.task_handle; + handle->need_stop = 0; printf("WebSocket server started: type=%d %s\n", handle->type,handle->config.name); @@ -219,7 +220,7 @@ errcode_t ws_server_start(ws_server_handle_t *handle) } /** - * @brief 停止WebSocket服务器 + * @brief 停止WebSocket服务器(同步接口,等待线程退出) */ void ws_server_stop(ws_server_handle_t *handle) { @@ -235,6 +236,66 @@ void ws_server_stop(ws_server_handle_t *handle) send_websocket_message(handle->client, (uint8_t *)"WS_CLOSE", strlen("WS_CLOSE"), WS_CLOSE); } + printf("WebSocket server stopping: type=%d\n", handle->type); + + // 使用 osThreadGetState 判断线程状态,等待线程退出 + if (handle->thread_handle != NULL) { + uint32_t wait_count = 0; + const uint32_t max_wait_count = 1000; // 最多等待1000次,每次10ms,总共10秒 + const uint32_t check_interval_ms = 10; + + while (wait_count < max_wait_count) { + osThreadState_t thread_state = osThreadGetState(handle->thread_handle); + + // 检查线程是否已经退出 + // Running、Blocked、Ready 表示线程还在运行 + // Terminated、Inactive、Error 表示线程已经停止 + bool thread_stopped = (thread_state == osThreadTerminated || + thread_state == osThreadInactive || + thread_state == osThreadError); + + if (thread_stopped) { + // 线程状态已停止,检查线程名是否匹配,防止线程ID被复用 + const char *thread_name = NULL; + + // 只有在非Error状态下才尝试获取线程名 + if (thread_state != osThreadError) { + thread_name = osThreadGetName(handle->thread_handle); + } + + // 如果线程名为NULL(线程已删除或被错误状态)或线程名不匹配(ID被复用),认为线程已退出 + if (thread_name == NULL || thread_state == osThreadError) { + // 线程已被删除或处于错误状态 + printf("WebSocket server thread deleted/errored: type=%d, state=%d, wait_count=%d\n", + handle->type, thread_state, wait_count); + break; + } else if (strcmp(thread_name, handle->config.name) != 0) { + // 线程ID已被复用,原来的线程肯定已经退出 + printf("WebSocket server thread ID reused (expected:%s, got:%s), original thread exited: type=%d\n", + handle->config.name, thread_name, handle->type); + break; + } else { + // 线程名匹配且状态已停止,说明是本线程已停止 + printf("WebSocket server thread stopped: type=%d, state=%d, name=%s, wait_count=%d\n", + handle->type, thread_state, thread_name, wait_count); + break; + } + } + + // 线程还在运行(Running 或 Blocked),继续等待 + wait_count++; + msleep(check_interval_ms); + } + + if (wait_count >= max_wait_count) { + printf("WebSocket server thread wait timeout: type=%d\n", handle->type); + // 如果超时,尝试强制终止线程 + osThreadTerminate(handle->thread_handle); + } + + handle->thread_handle = NULL; + } + printf("WebSocket server stopped: type=%d\n", handle->type); } @@ -556,11 +617,16 @@ static void ws_server_thread_func(void *data) cleanup: msleep(100); - ws_server_disconnect(handle); - ws_server_cleanup(handle); + + // 断开连接 + if (handle && handle->client) { + close_websocket_client(handle->client); + handle->client = NULL; + } // 清理对应服务类型的全局变量 if (handle) { + handle->state = WS_SERVER_STATE_DISCONNECTED; g_thread_handles[handle->type] = NULL; printf("WebSocket server thread ended: type=%d\n", handle->type); }