From 3c313b0f9fc4aaa411dfd7c87c47f6c77b540069 Mon Sep 17 00:00:00 2001 From: cheng Date: Fri, 19 Sep 2025 11:20:32 +0800 Subject: [PATCH] =?utf8?q?=E5=87=8F=E5=B0=91ln=5Fhttp=5Fserver=5Fsend=5Fta?= =?utf8?q?sk=E7=BA=BF=E7=A8=8B=E5=A4=A7=E5=B0=8F=EF=BC=8C=E4=B8=80?= =?utf8?q?=E4=BA=9B=E8=83=BDmalloc=E7=9A=84=E6=93=8D=E4=BD=9C=E7=94=A8mall?= =?utf8?q?oc=20=E6=B7=BB=E5=8A=A0=E8=AF=AD=E9=9F=B3=E5=BC=80=E5=A7=8B?= =?utf8?q?=E5=92=8C=E7=BB=93=E6=9D=9F=E5=AF=B9=E8=AF=9D=E6=A0=87=E5=BF=97?= =?utf8?q?=E4=BD=8D=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../zhihui/4g/cat1Service/Service/cat1_ctl.c | 2 +- code/sdk/zhihui/leiniao/imsl_ln/ln_api.c | 27 +++++-- .../leiniao/imsl_ln/ln_api_device_info.c | 39 +++++++--- .../sdk/zhihui/leiniao/imsl_ln/ln_api_emoji.c | 8 +- code/sdk/zhihui/leiniao/imsl_ln/ln_api_ota.c | 78 ++++++++++++++----- .../leiniao/imsl_ln/ln_api_speech_chat.c | 20 +++-- .../leiniao/imsl_ln/ln_api_story_choose.c | 20 +++-- .../leiniao/imsl_ln/ln_api_story_exit.c | 11 ++- .../leiniao/imsl_ln/ln_api_story_finish.c | 10 ++- .../leiniao/imsl_ln/ln_api_story_random.c | 75 ++++++++++-------- .../sdk/zhihui/leiniao/imsl_ln/ln_api_token.c | 29 +++++-- .../leiniao/ln_api_cpp/ln_api_interface.cpp | 15 +++- .../leiniao/ln_api_cpp/ln_api_interface.h | 4 + code/sdk/zhihui/utils/custom_utils.c | 1 - 14 files changed, 241 insertions(+), 98 deletions(-) diff --git a/code/sdk/zhihui/4g/cat1Service/Service/cat1_ctl.c b/code/sdk/zhihui/4g/cat1Service/Service/cat1_ctl.c index c519f7fa2..d96883871 100644 --- a/code/sdk/zhihui/4g/cat1Service/Service/cat1_ctl.c +++ b/code/sdk/zhihui/4g/cat1Service/Service/cat1_ctl.c @@ -329,7 +329,7 @@ void cat1_api_wakeup_cat1(uint8_t wakeup) } // wstp_cat1_print_dbg("wakeup Cat1! L30 high \r\n"); }else{ - // cat1_print_warning("sleep cat1\n"); + cat1_print_warning("sleep cat1\n"); if(en_value == uapi_gpio_get_output_val(en_pin)) { if(en_value) diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c index 577758138..82f28bf66 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api.c @@ -28,7 +28,7 @@ static char *g_device_id = NULL; char *g_device_token = NULL;//设备token,服务器获取 static char *g_device_iccid = NULL; static char *g_device_model = NULL; -static char *g_device_version = NULL; +static char *g_device_version[128] = {0}; /* 记录最近一次 OTA 查询的升级类型与保存路径 */ int g_ln_last_update_type = -1; // -1:未知 0:可选 1:强制 @@ -65,9 +65,9 @@ char *uapi_ln_get_device_version(void) char version_buf[128] = {0}; uint32_t version_len = 128; uapi_get_buildversion((uint8_t *)version_buf, version_len); - g_device_version = version_buf; + strcpy(g_device_version, version_buf); printf("g_device_version: %s\n", g_device_version); - return g_device_version; + return g_device_version; } /* 获取设备token: 优先读本地缓存,判断过期,否则刷新 */ char *uapi_ln_get_device_token(void) @@ -150,6 +150,7 @@ const char *uapi_ln_get_story_choose_audio_path(void) return path; } +//中断语音对话播放 void uapi_ln_audio_download_is_cancel(int status) { g_ln_audio_download_cancel = status; @@ -565,8 +566,14 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char } // 构建完整保存路径 - char save_path[512]; - snprintf(save_path, sizeof(save_path), "%s/%s", save_dir, filename); + size_t sp_cap = strlen(save_dir) + 1 + strlen(filename) + 1; + char *save_path = (char*)malloc(sp_cap); + if (!save_path) + { + printf("[SSE] malloc save_path failed\n"); + return ERRCODE_FAIL; + } + snprintf(save_path, sp_cap, "%s/%s", save_dir, filename); // 确保目录存在 if (access(save_dir, F_OK) == -1) @@ -607,6 +614,7 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char #if TEST_MP3_DOWNLOAD fclose(fd); #endif + free(save_path); return ERRCODE_FAIL; } @@ -619,6 +627,7 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char fclose(fd); #endif HttpClientUnprepare(&client_data); + free(save_path); return ERRCODE_FAIL; } @@ -632,6 +641,7 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char #endif HttpClientClse(&client); HttpClientUnprepare(&client_data); + free(save_path); return ERRCODE_FAIL; } @@ -686,6 +696,7 @@ static uint32_t ln_download_mp3_file_advanced(const char *audio_url, const char HttpClientClse(&client); printf("[SSE] download completed, total bytes: %zu\n", total_written); + free(save_path); return ERRCODE_SUCC; } @@ -832,6 +843,8 @@ static uint32_t ln_parse_and_process_json_data(const char *json_data, int interf cJSON *textContent = cJSON_GetObjectItem(push_data, "textContent"); if (textContent && cJSON_IsString(textContent)) { + // 发送开始对话标志 + set_start_conversation_flag(1); printf("[JSON_PARSE] textContent: %s\n", textContent->valuestring); do_ln_http_server_recv_data(interface_type, textContent->valuestring, strlen(textContent->valuestring)); } @@ -987,6 +1000,8 @@ uint32_t ln_parse_file_and_process_data(const char *file_path, int interface_typ printf("[FILE_PARSE] processing completed: total lines=%d, success=%d, errors=%d\n", line_count, success_count, error_count); + //语音对话结束 + set_start_conversation_flag(0); return (error_count == 0) ? ERRCODE_SUCC : ERRCODE_FAIL; } /* ============================ mp3下载功能 End ============================ */ @@ -1291,7 +1306,7 @@ errcode_t uapi_ln_http_server_init(void) ln_send_taskcfg.create_queue_handle = create_ln_http_send_queue; ln_send_taskcfg.attr.name = "ln_http_server_send_task"; ln_send_taskcfg.attr.priority = osPriorityNormal; - ln_send_taskcfg.attr.stack_size = 0x4000; + ln_send_taskcfg.attr.stack_size = LN_HTTP_BOARD_TASK_SIZE; ln_send_taskcfg.func = ln_http_server_send_task; ln_send_taskcfg.use_mem = USE_EXTEND_MEM; diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_device_info.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_device_info.c index 5048ac198..ba854bcf2 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_device_info.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_device_info.c @@ -152,14 +152,18 @@ uint32_t ln_device_get_character_http(const char *device_id, client.isHttp = false; client.remotePort = 443; - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(512); + if (!header_buf) return ERRCODE_FAIL; + int hdr = snprintf(header_buf, 512, "deviceId: %s\r\n" "deviceToken: %s\r\n" "Content-Type: application/json\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) + { + free(header_buf); return ERRCODE_FAIL; + } HttpClientSetCustomHeader(&client, header_buf); printf("device getCharacter header %s\n", header_buf); @@ -199,6 +203,7 @@ uint32_t ln_device_get_character_http(const char *device_id, end: HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(header_buf); return ERRCODE_SUCC; } @@ -223,17 +228,21 @@ uint32_t ln_device_get_user_id_http(const char *device_id, client.isHttp = false; client.remotePort = 443; - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf2 = (char*)malloc(512); + if (!header_buf2) return ERRCODE_FAIL; + int hdr = snprintf(header_buf2, 512, "deviceId: %s\r\n" "deviceToken: %s\r\n" "Content-Type: application/json\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) + { + free(header_buf2); return ERRCODE_FAIL; - HttpClientSetCustomHeader(&client, header_buf); + } + HttpClientSetCustomHeader(&client, header_buf2); - printf("device getUserId header %s\n", header_buf); + printf("device getUserId header %s\n", header_buf2); HTTPC_RESULT prep = ln_http_prepare_auto(&clientData, 2048, 8 * 1024); if (prep != HTTP_SUCCESS) @@ -269,6 +278,7 @@ uint32_t ln_device_get_user_id_http(const char *device_id, end: HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(header_buf2); return ERRCODE_SUCC; } @@ -310,17 +320,21 @@ uint32_t uapi_ln_device_get_user_id(LnDeviceBindStatus *out_status, char *out_us client.isHttp = false; client.remotePort = 443; - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf3 = (char*)malloc(512); + if (!header_buf3) { + return ERRCODE_FAIL; + } + int hdr = snprintf(header_buf3, 512, "deviceId: %s\r\n" "deviceToken: %s\r\n" "Content-Type: application/json\r\n", device_id, token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) { + free(header_buf3); return ERRCODE_FAIL; } - HttpClientSetCustomHeader(&client, header_buf); + HttpClientSetCustomHeader(&client, header_buf3); HTTPC_RESULT prep = ln_http_prepare_auto(&clientData, 2048, LN_CLIENT_PREPARE_SIZE * 2); if (prep != HTTP_SUCCESS) @@ -387,5 +401,6 @@ uint32_t uapi_ln_device_get_user_id(LnDeviceBindStatus *out_status, char *out_us HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(header_buf3); return ERRCODE_SUCC; } diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_emoji.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_emoji.c index 2fea535e9..a9cfff267 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_emoji.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_emoji.c @@ -542,15 +542,17 @@ static inline int ln_fill_auth_header(HttpClient *client, { if (!client || !device_id || !device_token) return -1; - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(512); + if (!header_buf) return -1; + int hdr = snprintf(header_buf, 512, "deviceId: %s\r\n" "deviceToken: %s\r\n" "Content-Type: application/json\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) return -1; HttpClientSetCustomHeader(client, header_buf); + free(header_buf); return 0; } diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_ota.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_ota.c index 738c46a24..1b76473e9 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_ota.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_ota.c @@ -1,5 +1,7 @@ #include "ln_api.h" #include +#include +#include uint32_t ln_ota_get_task_http(const char *device_id, const char *device_token, @@ -23,8 +25,13 @@ uint32_t ln_ota_get_task_http(const char *device_id, client.remotePort = 443; /* 头:与 device info 一致,额外带 model/version 可辅助服务端日志定位 */ - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + size_t hdr_cap = 64 + strlen(device_id) + strlen(device_token) + + (uapi_ln_get_device_model() ? strlen(uapi_ln_get_device_model()) : 5) + + (uapi_ln_get_device_version() ? strlen(uapi_ln_get_device_version()) : 5); + char *header_buf = (char *)malloc(hdr_cap + 128); + if (!header_buf) + return ERRCODE_FAIL; + int hdr = snprintf(header_buf, hdr_cap + 128, "deviceId: %s\r\n" "deviceToken: %s\r\n" "deviceModel: %s\r\n" @@ -33,8 +40,11 @@ uint32_t ln_ota_get_task_http(const char *device_id, device_id, device_token, (uapi_ln_get_device_model() ? uapi_ln_get_device_model() : "AB101"), (uapi_ln_get_device_version() ? uapi_ln_get_device_version() : "1.0.0")); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) + { + free(header_buf); return ERRCODE_FAIL; + } HttpClientSetCustomHeader(&client, header_buf); HTTPC_RESULT prep = ln_http_prepare_auto(&clientData, 2048, 8 * 1024); @@ -45,13 +55,22 @@ uint32_t ln_ota_get_task_http(const char *device_id, } /* 拼 GET URL 参数 */ - char url_with_params[512] = {0}; - int ulen = snprintf(url_with_params, sizeof(url_with_params), + size_t url_cap = strlen(LN_OTA_GET_TASK_URL) + strlen(device_id) + strlen(source_version) + 64; + char *url_with_params = (char *)malloc(url_cap); + if (!url_with_params) + { + HttpClientUnprepare(&clientData); + free(header_buf); + return ERRCODE_FAIL; + } + int ulen = snprintf(url_with_params, url_cap, "%s?deviceId=%s&sourceVersion=%s", LN_OTA_GET_TASK_URL, device_id, source_version); - if (ulen <= 0 || ulen >= (int)sizeof(url_with_params)) + if (ulen <= 0 || (size_t)ulen >= url_cap) { HttpClientUnprepare(&clientData); + free(header_buf); + free(url_with_params); return ERRCODE_FAIL; } @@ -85,6 +104,8 @@ uint32_t ln_ota_get_task_http(const char *device_id, end: HttpClientUnprepare(&clientData); HttpClientClse(&client); + if (header_buf) free(header_buf); + if (url_with_params) free(url_with_params); return ERRCODE_SUCC; } @@ -122,6 +143,11 @@ static int ln_download_file_to_path(const char *full_url, const char *save_path, int ret = -1; size_t total_written = 0; // 已写入文件的总字节数 int last_percent = -1; // 上次上报的进度百分比 + char *prog = (char *)malloc(64); + if (!prog) + { + return -1; + } // 打开文件准备写入 FILE *fd = fopen(save_path, "wb"); @@ -187,8 +213,7 @@ static int ln_download_file_to_path(const char *full_url, const char *save_path, if (percent != last_percent) { last_percent = percent; - char prog[64]; - int n = snprintf(prog, sizeof(prog), + int n = snprintf(prog, 64, "{\"status\":%d,\"progress\":%d}", OTA_DOWNLOAD_STATUS_PROGRESS, percent); if (n > 0) @@ -244,6 +269,7 @@ static int ln_download_file_to_path(const char *full_url, const char *save_path, fclose(fd); HttpClientClse(&client); HttpClientUnprepare(&client_data); + if (prog) free(prog); printf("[OTA] download process finished\n"); return ret; @@ -261,8 +287,13 @@ uint32_t ln_ota_download_flow_http(const char *device_id, printf("[OTA] no cached task to download, please GET_TASK first\n"); return ERRCODE_FAIL; } - char save_path[256]; - snprintf(save_path, sizeof(save_path), LN_OTA_SAVE_DIR "/%s.fwpkg", g_ln_last_target_version); + size_t sp_cap = strlen(LN_OTA_SAVE_DIR) + 1 + strlen(g_ln_last_target_version) + strlen(".fwpkg") + 1; + char *save_path = (char *)malloc(sp_cap); + if (!save_path) + { + return ERRCODE_FAIL; + } + snprintf(save_path, sp_cap, LN_OTA_SAVE_DIR "/%s.fwpkg", g_ln_last_target_version); memset(g_ln_last_saved_path, 0, sizeof(g_ln_last_saved_path)); strncpy(g_ln_last_saved_path, save_path, sizeof(g_ln_last_saved_path) - 1); @@ -274,16 +305,27 @@ uint32_t ln_ota_download_flow_http(const char *device_id, } if (dres != 0) { - char msg[256]; - int n = snprintf(msg, sizeof(msg), "{\"status\":%d,\"error\":\"download failed\"}", 2); - if (n > 0) - do_ln_http_server_recv_data(LN_HTTP_RECV_OTA_DOWNLOAD_MSG, msg, (uint32_t)n); + char *msg = (char *)malloc(64); + if (msg) + { + int n = snprintf(msg, 64, "{\"status\":%d,\"error\":\"download failed\"}", 2); + if (n > 0) + do_ln_http_server_recv_data(LN_HTTP_RECV_OTA_DOWNLOAD_MSG, msg, (uint32_t)n); + free(msg); + } + free(save_path); return ERRCODE_FAIL; } - char done[512]; - int m = snprintf(done, sizeof(done), "{\"status\":%d,\"filePath\":\"%s\",\"md5\":\"%s\"}", 1, save_path, g_ln_last_package_md5); - if (m > 0) - do_ln_http_server_recv_data(LN_HTTP_RECV_OTA_DOWNLOAD_MSG, done, (uint32_t)m); + size_t done_cap = 64 + strlen(save_path) + strlen(g_ln_last_package_md5); + char *done = (char *)malloc(done_cap); + if (done) + { + int m = snprintf(done, done_cap, "{\"status\":%d,\"filePath\":\"%s\",\"md5\":\"%s\"}", 1, save_path, g_ln_last_package_md5); + if (m > 0) + do_ln_http_server_recv_data(LN_HTTP_RECV_OTA_DOWNLOAD_MSG, done, (uint32_t)m); + free(done); + } + free(save_path); return ERRCODE_SUCC; } diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_speech_chat.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_speech_chat.c index 418832a48..589b7ebcc 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_speech_chat.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_speech_chat.c @@ -51,14 +51,16 @@ static int ln_build_multipart_form_speech_chat(const char *boundary, { if (!boundary || !filename || !file_data || file_len <= 0 || !out_body || !out_len) return -1; - char head[512]; - int head_len = snprintf(head, sizeof(head), + char *head = (char*)malloc(512); + if (!head) return -1; + int head_len = snprintf(head, 512, "--%s\r\n" "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" "Content-Type: audio/mpeg\r\n\r\n", boundary, filename); - char tail[128]; - int tail_len = snprintf(tail, sizeof(tail), "\r\n--%s--\r\n", boundary); + char *tail = (char*)malloc(128); + if (!tail) { free(head); return -1; } + int tail_len = snprintf(tail, 128, "\r\n--%s--\r\n", boundary); int total = head_len + file_len + tail_len; char *body = (char *)malloc((size_t)total); if (!body) @@ -66,6 +68,8 @@ static int ln_build_multipart_form_speech_chat(const char *boundary, memcpy(body, head, head_len); memcpy(body + head_len, file_data, (size_t)file_len); memcpy(body + head_len + file_len, tail, tail_len); + free(head); + free(tail); *out_body = body; *out_len = total; if (out_content_type && ctype_len > 0) @@ -149,16 +153,18 @@ uint32_t ln_speech_chat_stream_http(const char *device_id, HttpClientData clientData = (HttpClientData){0}; /* 自定义头:仅保留自定义业务头,Content-Type 交由 postContentType 指定 */ - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(512); + if (!header_buf) { free(body); return ERRCODE_FAIL; } + int hdr = snprintf(header_buf, 512, "deviceId: %s\r\n" "deviceToken: %s\r\n" "Accept: text/event-stream\r\n" "Cache-Control: no-cache\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) { free(body); + free(header_buf); return ERRCODE_FAIL; } HttpClientSetCustomHeader(&client, header_buf); diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_choose.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_choose.c index 1f2ecac1a..33c5d3a0a 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_choose.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_choose.c @@ -51,14 +51,16 @@ static int ln_build_multipart_form_story(const char *boundary, { if (!boundary || !filename || !file_data || file_len <= 0 || !out_body || !out_len) return -1; - char head[512]; - int head_len = snprintf(head, sizeof(head), + char *head = (char*)malloc(512); + if (!head) return -1; + int head_len = snprintf(head, 512, "--%s\r\n" "Content-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n" "Content-Type: audio/mpeg\r\n\r\n", boundary, filename); - char tail[128]; - int tail_len = snprintf(tail, sizeof(tail), "\r\n--%s--\r\n", boundary); + char *tail = (char*)malloc(128); + if (!tail) { free(head); return -1; } + int tail_len = snprintf(tail, 128, "\r\n--%s--\r\n", boundary); int total = head_len + file_len + tail_len; char *body = (char *)malloc((size_t)total); if (!body) @@ -66,6 +68,8 @@ static int ln_build_multipart_form_story(const char *boundary, memcpy(body, head, head_len); memcpy(body + head_len, file_data, (size_t)file_len); memcpy(body + head_len + file_len, tail, tail_len); + free(head); + free(tail); *out_body = body; *out_len = total; if (out_content_type && ctype_len > 0) @@ -146,17 +150,19 @@ uint32_t ln_story_choose_http(const char *device_id, HttpClientData clientData = {0}; /* 设置请求头:精简头部,避免重复(Host/Connection 交由库设置) */ - char header_buf[1024]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(1024); + if (!header_buf) { free(body); return ERRCODE_FAIL; } + int hdr = snprintf(header_buf, 1024, "deviceId: %s\r\n" "deviceToken: %s\r\n" "Accept: text/event-stream\r\n" "Cache-Control: no-cache\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) { printf("story choose: build header failed\n"); free(body); + free(header_buf); return ERRCODE_FAIL; } HttpClientSetCustomHeader(&client, header_buf); diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_exit.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_exit.c index 6af2917ac..022781f17 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_exit.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_exit.c @@ -21,13 +21,17 @@ uint32_t ln_story_exit_inquiry_http(const char *device_id, client.remotePort = 443; /* 普通JSON头 */ - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(512); + if (!header_buf) return ERRCODE_FAIL; + int hdr = snprintf(header_buf, 512, "deviceId: %s\r\ndeviceToken: %s\r\nContent-Type: application/json\r\n", device_id, device_token); printf("ln_story_finish_http header_buf: %s\n", header_buf); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) + { + free(header_buf); return ERRCODE_FAIL; + } HttpClientSetCustomHeader(&client, header_buf); HTTPC_RESULT prep = ln_http_prepare_auto(&clientData, 2048, 8 * 1024); @@ -64,5 +68,6 @@ uint32_t ln_story_exit_inquiry_http(const char *device_id, end: HttpClientUnprepare(&clientData); HttpClientClse(&client); + if (header_buf) free(header_buf); return ERRCODE_SUCC; } diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_finish.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_finish.c index 9f3e3e108..12e3f0b06 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_finish.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_finish.c @@ -21,12 +21,15 @@ uint32_t ln_story_finish_http(const char *device_id, client.isHttp = false; client.remotePort = 443; - char header_buf[512]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(512); + if (!header_buf) return ERRCODE_FAIL; + int hdr = snprintf(header_buf, 512, "deviceId: %s\r\ndeviceToken: %s\r\nContent-Type: application/json\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) + { return ERRCODE_FAIL; + } HttpClientSetCustomHeader(&client, header_buf); HTTPC_RESULT prep = ln_http_prepare_auto(&clientData, 2048, 8 * 1024); @@ -64,5 +67,6 @@ uint32_t ln_story_finish_http(const char *device_id, end: HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(header_buf); return ERRCODE_SUCC; } diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_random.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_random.c index 99d42f8e6..5803588e8 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_random.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_story_random.c @@ -1,5 +1,6 @@ #include "ln_api.h" #include +#include #define story_random_file_path "/user/leiniao/story/random.txt" @@ -44,17 +45,16 @@ uint32_t ln_story_random_http(const char *device_id, client.remotePort = 443; /* 设置请求头(包含Content-Type,保持与Python一致) */ - char header_buf[1024]; - int hdr = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(1024); + if (!header_buf) return ERRCODE_FAIL; + int hdr = snprintf(header_buf, 1024, "deviceId: %s\r\n" "deviceToken: %s\r\n" - "Accept: text/event-stream\r\n" - "Cache-Control: no-cache\r\n" "Content-Type: application/json\r\n", device_id, device_token); - if (hdr <= 0 || hdr >= (int)sizeof(header_buf)) + if (hdr <= 0) { - printf("story random: build header failed\n"); + free(header_buf); return ERRCODE_FAIL; } HttpClientSetCustomHeader(&client, header_buf); @@ -64,46 +64,44 @@ uint32_t ln_story_random_http(const char *device_id, if (prep != HTTP_SUCCESS) { printf("story random: prepare HTTP buffer failed (code: %d)\n", prep); + free(header_buf); return ERRCODE_FAIL; } /* 构建 JSON 请求体(增加转义处理,避免格式错误)*/ - char json_body[256] = {0}; - char escaped_theme[256] = {0}; + char *json_body = (char*)malloc(256); + char *escaped_theme = (char*)malloc(256); + if (!json_body || !escaped_theme) + { + if (json_body) free(json_body); + if (escaped_theme) free(escaped_theme); + free(header_buf); + return ERRCODE_FAIL; + } int json_len = 0; if (story_theme && story_theme[0]) { - int i = 0, j = 0; - while (story_theme[i] && j < (int)sizeof(escaped_theme) - 1) + /* 对 theme 做简单转义,避免引号破坏 JSON */ + int ei = 0; + for (const char *p = story_theme; *p && ei < 255; ++p) { - if (story_theme[i] == '"') - { - escaped_theme[j++] = '\\'; - escaped_theme[j++] = '"'; - } - else if (story_theme[i] == '\\') - { - escaped_theme[j++] = '\\'; - escaped_theme[j++] = '\\'; - } - else - { - escaped_theme[j++] = story_theme[i]; - } - i++; + if (*p == '"' || *p == '\\') + escaped_theme[ei++] = '\\'; + escaped_theme[ei++] = *p; } - escaped_theme[j] = '\0'; - json_len = snprintf(json_body, sizeof(json_body), "{\"storyTheme\":\"%s\"}", escaped_theme); + escaped_theme[ei] = '\0'; + json_len = snprintf(json_body, 256, "{\"storyTheme\":\"%s\"}", escaped_theme); } else { - json_len = snprintf(json_body, sizeof(json_body), "{}"); + json_len = snprintf(json_body, 256, "{}"); } - if (json_len <= 0 || json_len >= (int)sizeof(json_body)) + if (json_len <= 0) { - printf("story random: build JSON body failed\n"); - HttpClientUnprepare(&clientData); + free(header_buf); + free(json_body); + free(escaped_theme); return ERRCODE_FAIL; } @@ -117,6 +115,9 @@ uint32_t ln_story_random_http(const char *device_id, if (ret != 0) { printf("[OTA] connection failed, ret=0x%x\n", ret); + free(header_buf); + free(json_body); + free(escaped_theme); HttpClientUnprepare(&clientData); return ret; } @@ -128,6 +129,9 @@ uint32_t ln_story_random_http(const char *device_id, printf("[OTA] send request failed, ret=0x%x\n", ret); HttpClientClse(&client); HttpClientUnprepare(&clientData); + free(header_buf); + free(json_body); + free(escaped_theme); return ret; } @@ -144,6 +148,9 @@ uint32_t ln_story_random_http(const char *device_id, printf("story choose: fopen failed for %s:\n", story_random_file_path); HttpClientClse(&client); HttpClientUnprepare(&clientData); + free(header_buf); + free(json_body); + free(escaped_theme); return ERRCODE_FAIL; } @@ -165,6 +172,9 @@ uint32_t ln_story_random_http(const char *device_id, HttpClientClse(&client); HttpClientUnprepare(&clientData); fclose(fd); + free(header_buf); + free(json_body); + free(escaped_theme); return ERRCODE_FAIL; } total_written += written; @@ -190,5 +200,8 @@ uint32_t ln_story_random_http(const char *device_id, ln_parse_story_random_file(); } + free(header_buf); + free(json_body); + free(escaped_theme); return ERRCODE_SUCC; } diff --git a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_token.c b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_token.c index edec9b592..aadd2a570 100644 --- a/code/sdk/zhihui/leiniao/imsl_ln/ln_api_token.c +++ b/code/sdk/zhihui/leiniao/imsl_ln/ln_api_token.c @@ -244,8 +244,10 @@ uint32_t ln_get_device_token_http(const char *device_id, ln_gen_nonce(nonce, sizeof(nonce)); /* 2. 计算签名: deviceId:deviceIccid:timestamp:nonce */ - char sign_data[256] = {0}; - (void)snprintf(sign_data, sizeof(sign_data), "%s:%s:%s:%s", device_id, device_iccid, timestamp, nonce); + size_t sd_cap = strlen(device_id) + strlen(device_iccid) + strlen(timestamp) + strlen(nonce) + 4 + 1; + char *sign_data = (char*)malloc(sd_cap); + if (!sign_data) return ERRCODE_FAIL; + (void)snprintf(sign_data, sd_cap, "%s:%s:%s:%s", device_id, device_iccid, timestamp, nonce); char signature[65] = {0}; if (ln_hmac_sha256_hex(LN_SIGN_SECRET, sign_data, signature, sizeof(signature)) != 0) { @@ -254,8 +256,9 @@ uint32_t ln_get_device_token_http(const char *device_id, } /* 3. 组织自定义请求头 */ - char header_buf[1024] = {0}; - int hdr_len = snprintf(header_buf, sizeof(header_buf), + char *header_buf = (char*)malloc(1024); + if (!header_buf) { free(sign_data); return ERRCODE_FAIL; } + int hdr_len = snprintf(header_buf, 1024, "deviceId: %s\r\n" "deviceIccid: %s\r\n" "timestamp: %s\r\n" @@ -267,9 +270,11 @@ uint32_t ln_get_device_token_http(const char *device_id, device_id, device_iccid, timestamp, nonce, signature, (device_model && device_model[0]) ? device_model : "AB101", (device_version && device_version[0]) ? device_version : "1.0.0"); - if (hdr_len <= 0 || hdr_len >= (int)sizeof(header_buf)) + if (hdr_len <= 0) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"ln_get_device_token_http: header build failed"); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } @@ -291,6 +296,8 @@ uint32_t ln_get_device_token_http(const char *device_id, if (prep != HTTP_SUCCESS) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"ln_get_device_token_http: HttpClientPrepare failed %d", prep); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } @@ -300,6 +307,8 @@ uint32_t ln_get_device_token_http(const char *device_id, WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"ln_get_device_token_http: request failed %d", res); HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } @@ -309,6 +318,8 @@ uint32_t ln_get_device_token_http(const char *device_id, WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"ln_get_device_token_http: HTTP %d", code); HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } @@ -318,6 +329,8 @@ uint32_t ln_get_device_token_http(const char *device_id, WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"ln_get_device_token_http: empty body"); HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } @@ -330,6 +343,8 @@ uint32_t ln_get_device_token_http(const char *device_id, { HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } memcpy(json_text, clientData.responseBuf, (size_t)body_len); @@ -342,6 +357,8 @@ uint32_t ln_get_device_token_http(const char *device_id, free(json_text); HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(sign_data); + free(header_buf); return ERRCODE_FAIL; } @@ -399,5 +416,7 @@ uint32_t ln_get_device_token_http(const char *device_id, free(json_text); HttpClientUnprepare(&clientData); HttpClientClse(&client); + free(sign_data); + free(header_buf); return ERRCODE_SUCC; } diff --git a/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.cpp b/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.cpp index 7388bfdb1..364810745 100644 --- a/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.cpp +++ b/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.cpp @@ -14,6 +14,20 @@ void push_text_to_ui(const char *text) { OHOS::ThunderBirdModel::GetInstance()->SetTextStr(text); } +// 是否开始对话标志 +static int g_start_conversation_flag = 0; +void set_start_conversation_flag(int flag) +{ + printf("set_start_conversation_flag: %d\n", flag); + g_start_conversation_flag = flag; +} + +// 获取是否开始对话标志 +int get_start_conversation_flag(void) +{ + printf("get_start_conversation_flag: %d\n", g_start_conversation_flag); + return g_start_conversation_flag; +} void push_ota_status(int status) { @@ -22,7 +36,6 @@ void push_ota_status(int status) void push_ota_progress(int progress) { - printf("push_ota_progress: %d\n", progress); } void do_restore_factory_setting(void) diff --git a/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.h b/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.h index 265585395..dd461e0c0 100644 --- a/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.h +++ b/code/sdk/zhihui/leiniao/ln_api_cpp/ln_api_interface.h @@ -7,6 +7,10 @@ extern "C" // 推送文本给ui void push_text_to_ui(const char *text); +//设置是否开始对话标志 +void set_start_conversation_flag(int flag); +//获取开始对话标志 +int get_start_conversation_flag(void); /* ota升级状态 diff --git a/code/sdk/zhihui/utils/custom_utils.c b/code/sdk/zhihui/utils/custom_utils.c index f19980695..06e26aa2f 100644 --- a/code/sdk/zhihui/utils/custom_utils.c +++ b/code/sdk/zhihui/utils/custom_utils.c @@ -1945,7 +1945,6 @@ errcode_t uapi_get_buildversion(uint8_t *para, uint32_t para_len) get_version_string(), BUILD_TIME); #endif - printf(str); memset(para,0,para_len); memcpy_s(para,para_len,str,strlen(str)); -- 2.17.1