From: penglei <1624952103@qq.com> Date: Tue, 28 Oct 2025 09:51:19 +0000 (+0800) Subject: 优化gnss星历下载 优化n9表冠初始化 X-Git-Tag: V1.0.9_20251128_194606_LN~157^2~3 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=9e3bff987fe1918613815c2a85c8b87e67878d56;p=mirrors%2FFD230.git 优化gnss星历下载 优化n9表冠初始化 --- diff --git a/code/sdk/zhihui/drivers/rotary_encoder/mixo/mixo_ots_driver.c b/code/sdk/zhihui/drivers/rotary_encoder/mixo/mixo_ots_driver.c index b52d5b178..0979449e4 100644 --- a/code/sdk/zhihui/drivers/rotary_encoder/mixo/mixo_ots_driver.c +++ b/code/sdk/zhihui/drivers/rotary_encoder/mixo/mixo_ots_driver.c @@ -226,6 +226,7 @@ void mixo_ots_enter_suspend_mode(uint8_t true_or_false) } else { + mixo_ots_init(); // bit 3 写 0, 退出Suspend mixo_ots_reg_read(0x06, &tmp); mixo_ots_reg_write(0x06, (tmp & 0xF7)); diff --git a/code/sdk/zhihui/services/gnss/src/gnss_http_server.c b/code/sdk/zhihui/services/gnss/src/gnss_http_server.c index c65a3f719..29e06e057 100644 --- a/code/sdk/zhihui/services/gnss/src/gnss_http_server.c +++ b/code/sdk/zhihui/services/gnss/src/gnss_http_server.c @@ -102,11 +102,15 @@ static void agnss_download_result_handler(const char *filename, HTTPC_RESULT res // 单个文件下载的结果处理 - 只保留成功信息 if (result == HTTP_SUCCESS && response_code == 200) { // 验证文件是否真的写入成功 - 在时间戳文件夹中查找 - char filepath[512]; + char *filepath = malloc(512); + if (filepath == NULL) { + printf("Error: Failed to allocate memory for filepath\n"); + return; + } int file_found = 0; // 首先尝试在根目录查找 - snprintf(filepath, sizeof(filepath), "%s/%s", PGNSS_DATA_PATH, filename); + snprintf(filepath, 512, "%s/%s", PGNSS_DATA_PATH, filename); FILE *fp = fopen(filepath, "rb"); if (fp != NULL) { fseek(fp, 0, SEEK_END); @@ -135,7 +139,7 @@ static void agnss_download_result_handler(const char *filename, HTTPC_RESULT res uint32_t folder_timestamp = strtoul(entry->d_name, &endptr, 10); if (*endptr == '\0' && folder_timestamp > 0) { // 在时间戳文件夹中查找文件 - snprintf(filepath, sizeof(filepath), "%s/%s/%s", PGNSS_DATA_PATH, entry->d_name, filename); + snprintf(filepath, 512, "%s/%s/%s", PGNSS_DATA_PATH, entry->d_name, filename); fp = fopen(filepath, "rb"); if (fp != NULL) { fseek(fp, 0, SEEK_END); @@ -153,6 +157,9 @@ static void agnss_download_result_handler(const char *filename, HTTPC_RESULT res closedir(dir); } } + + // 释放内存 + free(filepath); } } else { // AGNSS文件列表下载的结果处理 @@ -177,8 +184,9 @@ static void agnss_download_result_handler(const char *filename, HTTPC_RESULT res // HTTP请求失败,清理CAT1资源 #ifdef SUPPORT_CAT1_SERVICE - cat1_api_remove_cat1_vote(); - cat1_api_disable_cat1_cmd(0); + cat1_api_remove_cat1_vote(); + cat1_api_keep_wakeup(0); + cat1_api_wakeup_cat1(0); #endif } } @@ -304,8 +312,17 @@ static HTTPC_RESULT download_file_with_httpclient(const char *filename, const ch // 不在开始时更新进度,只在成功时更新 // 解析URL - char host[256] = {0}; - char path[512] = {0}; + char *host = malloc(256); + char *path = malloc(512); + if (host == NULL || path == NULL) { + printf("Error: Failed to allocate memory for URL parsing\n"); + if (host) free(host); + if (path) free(path); + g_download_status = -1; + return HTTP_EARG; + } + memset(host, 0, 256); + memset(path, 0, 512); int port = 80; // 简单解析 http://host/path 格式 @@ -314,34 +331,51 @@ static HTTPC_RESULT download_file_with_httpclient(const char *filename, const ch const char *path_start = strchr(url_path, '/'); if (path_start) { int host_len = path_start - url_path; - if (host_len >= sizeof(host)) { + if (host_len >= 256) { printf("Host name too long: %d bytes\n", host_len); + free(host); + free(path); g_download_status = -1; return HTTP_EARG; } strncpy(host, url_path, host_len); host[host_len] = '\0'; - if (strlen(path_start) >= sizeof(path)) { + if (strlen(path_start) >= 512) { printf("Path too long: %zu bytes\n", strlen(path_start)); + free(host); + free(path); g_download_status = -1; return HTTP_EARG; } strcpy(path, path_start); } else { printf("Invalid URL format - no path found\n"); + free(host); + free(path); g_download_status = -1; return HTTP_EARG; } } else { printf("Unsupported URL scheme: %s\n", url); + free(host); + free(path); g_download_status = -1; return HTTP_EARG; } // 保存到时间戳文件夹中 - char filepath[512]; - char timestamp_folder[64]; + char *filepath = malloc(512); + char *timestamp_folder = malloc(64); + if (filepath == NULL || timestamp_folder == NULL) { + printf("Error: Failed to allocate memory for filepath/timestamp_folder\n"); + free(host); + free(path); + if (filepath) free(filepath); + if (timestamp_folder) free(timestamp_folder); + g_download_status = -1; + return HTTP_EARG; + } // 从URL中提取时间戳文件夹名 char *folder_pos = strstr(url, "/iot_gnss/"); @@ -350,27 +384,37 @@ static HTTPC_RESULT download_file_with_httpclient(const char *filename, const ch char *next_slash = strchr(folder_pos, '/'); if (next_slash) { int folder_len = next_slash - folder_pos; - if (folder_len > 0 && folder_len < sizeof(timestamp_folder) - 1) { + if (folder_len > 0 && folder_len < 63) { strncpy(timestamp_folder, folder_pos, folder_len); timestamp_folder[folder_len] = '\0'; // 文件夹已在下载开始前创建,直接使用路径 - char full_folder_path[512]; - snprintf(full_folder_path, sizeof(full_folder_path), "%s/%s", PGNSS_DATA_PATH, timestamp_folder); + char *full_folder_path = malloc(512); + if (full_folder_path == NULL) { + printf("Error: Failed to allocate memory for full_folder_path\n"); + free(host); + free(path); + free(filepath); + free(timestamp_folder); + g_download_status = -1; + return HTTP_EARG; + } + snprintf(full_folder_path, 512, "%s/%s", PGNSS_DATA_PATH, timestamp_folder); + free(full_folder_path); // 这里只是用来构建路径,可以立即释放 // 保存文件到时间戳文件夹 - snprintf(filepath, sizeof(filepath), "%s/%s/%s", PGNSS_DATA_PATH, timestamp_folder, filename); + snprintf(filepath, 512, "%s/%s/%s", PGNSS_DATA_PATH, timestamp_folder, filename); } else { // 如果无法提取时间戳,使用默认路径 - snprintf(filepath, sizeof(filepath), "%s/%s", PGNSS_DATA_PATH, filename); + snprintf(filepath, 512, "%s/%s", PGNSS_DATA_PATH, filename); } } else { // 如果无法找到第二个斜杠,使用默认路径 - snprintf(filepath, sizeof(filepath), "%s/%s", PGNSS_DATA_PATH, filename); + snprintf(filepath, 512, "%s/%s", PGNSS_DATA_PATH, filename); } } else { // 如果无法找到 /iot_gnss/,使用默认路径 - snprintf(filepath, sizeof(filepath), "%s/%s", PGNSS_DATA_PATH, filename); + snprintf(filepath, 512, "%s/%s", PGNSS_DATA_PATH, filename); } // 初始化HTTP客户端 @@ -385,13 +429,27 @@ static HTTPC_RESULT download_file_with_httpclient(const char *filename, const ch HTTPC_RESULT prep_result = gnss_http_prepare_auto(&clientData, 2048, 256 * 1024); if (prep_result != HTTP_SUCCESS) { printf("HTTP客户端准备失败: %d\n", prep_result); + free(host); + free(path); + free(filepath); + free(timestamp_folder); g_download_status = -1; return prep_result; } // 构建完整URL - char full_url[512]; - snprintf(full_url, sizeof(full_url), "http://%s%s", host, path); + char *full_url = malloc(512); + if (full_url == NULL) { + printf("Error: Failed to allocate memory for full_url\n"); + free(host); + free(path); + free(filepath); + free(timestamp_folder); + g_download_status = -1; + return HTTP_EARG; + } + snprintf(full_url, 512, "http://%s%s", host, path); + printf("Attempting to connect to: %s (port %d)\n", full_url, port); // 执行HTTP GET请求 HTTPC_RESULT result = HttpClientGetRequest(&client, full_url, &clientData); @@ -615,16 +673,43 @@ static HTTPC_RESULT download_file_with_httpclient(const char *filename, const ch HttpClientUnprepare(&clientData); HttpClientClse(&client); + // 释放动态分配的内存 + free(host); + free(path); + free(filepath); + free(timestamp_folder); + free(full_url); + return result; } // 从URL中提取文件夹路径 static char* extract_folder_path(const char *download_url) { - static char folder_path[256] = {0}; + static char *folder_path = NULL; + + // 释放之前分配的内存 + if (folder_path != NULL) { + free(folder_path); + folder_path = NULL; + } + + // 如果传入NULL,只是清理内存 + if (download_url == NULL) { + return NULL; + } + + // 分配新的内存 + folder_path = malloc(256); + if (folder_path == NULL) { + return NULL; + } + memset(folder_path, 0, 256); // 跳过协议部分 (http://) const char *path_start = strstr(download_url, "://"); if (!path_start) { + free(folder_path); + folder_path = NULL; return NULL; } path_start += 3; // 跳过 "://" @@ -632,34 +717,50 @@ static char* extract_folder_path(const char *download_url) { // 跳过主机名部分 const char *first_slash = strchr(path_start, '/'); if (!first_slash) { + free(folder_path); + folder_path = NULL; return NULL; } // 查找最后一个斜杠(文件名之前) const char *last_slash = strrchr(first_slash, '/'); if (!last_slash || last_slash == first_slash) { + free(folder_path); + folder_path = NULL; return NULL; } - + // 提取文件夹路径 int folder_len = last_slash - first_slash; - if (folder_len > 0 && folder_len < sizeof(folder_path) - 1) { + if (folder_len > 0 && folder_len < 255) { strncpy(folder_path, first_slash, folder_len); folder_path[folder_len] = '\0'; return folder_path; } + free(folder_path); + folder_path = NULL; return NULL; } +// 清理extract_folder_path的静态内存 +static void cleanup_extract_folder_path(void) { + // 调用extract_folder_path(NULL)来触发内存清理 + extract_folder_path(NULL); +} + // 创建文件夹路径 static int create_folder_structure(const char *folder_path) { if (!folder_path || strlen(folder_path) == 0) { return 0; // 没有文件夹路径,直接返回成功 } - char full_path[512]; - snprintf(full_path, sizeof(full_path), "%s%s", PGNSS_DATA_PATH, folder_path); + char *full_path = malloc(512); + if (full_path == NULL) { + printf("Error: Failed to allocate memory for full_path\n"); + return -1; + } + snprintf(full_path, 512, "%s%s", PGNSS_DATA_PATH, folder_path); printf(" [DEBUG] 创建文件夹: %s\n", full_path); @@ -671,6 +772,7 @@ static int create_folder_structure(const char *folder_path) { // printf(" [WARNING] 文件夹创建失败: %s (错误码: %d)\n", full_path, result); // } + free(full_path); return 0; } @@ -779,8 +881,12 @@ static void verify_files_recursive(const char *base_path, int *total_count) continue; } - char full_path[512]; - snprintf(full_path, sizeof(full_path), "%s/%s", base_path, entry->d_name); + char *full_path = malloc(512); + if (full_path == NULL) { + printf("Error: Failed to allocate memory for full_path in verify_files_recursive\n"); + continue; + } + snprintf(full_path, 512, "%s/%s", base_path, entry->d_name); // 检查是否是目录 DIR *sub_dir = opendir(full_path); @@ -788,6 +894,7 @@ static void verify_files_recursive(const char *base_path, int *total_count) // 这是一个目录,递归遍历 closedir(sub_dir); verify_files_recursive(full_path, total_count); + free(full_path); } else { // 这是一个文件,检查是否是AGNSS相关文件 if (strstr(entry->d_name, ".eph") != NULL || @@ -815,6 +922,7 @@ static void verify_files_recursive(const char *base_path, int *total_count) } } } + free(full_path); } } @@ -1156,7 +1264,12 @@ static void agnss_process_complete_response(void) // 提取URL int url_len = url_end - url_start; if (url_len > 0 && url_len < 512) { - char download_url[512] = {0}; + char *download_url = malloc(512); + if (download_url == NULL) { + printf("Error: Failed to allocate memory for download_url\n"); + continue; + } + memset(download_url, 0, 512); strncpy(download_url, url_start, url_len); download_url[url_len] = '\0'; @@ -1168,7 +1281,13 @@ static void agnss_process_complete_response(void) if (next_slash) { int folder_len = next_slash - folder_pos; if (folder_len > 0 && folder_len < 32) { - char folder_timestamp_str[32] = {0}; + char *folder_timestamp_str = malloc(32); + if (folder_timestamp_str == NULL) { + printf("Error: Failed to allocate memory for folder_timestamp_str\n"); + free(download_url); + continue; + } + memset(folder_timestamp_str, 0, 32); strncpy(folder_timestamp_str, folder_pos, folder_len); folder_timestamp_str[folder_len] = '\0'; @@ -1187,9 +1306,11 @@ static void agnss_process_complete_response(void) } } } + free(folder_timestamp_str); } } } + free(download_url); } search_pos = url_end + 1; @@ -1200,8 +1321,12 @@ static void agnss_process_complete_response(void) valid_folder_count, selected_timestamp, min_time_diff); // 一次性创建选定的时间戳文件夹,避免重复创建 - char selected_folder_path[512]; - snprintf(selected_folder_path, sizeof(selected_folder_path), "%s/%u", PGNSS_DATA_PATH, selected_timestamp); + char *selected_folder_path = malloc(512); + if (selected_folder_path == NULL) { + printf("Error: Failed to allocate memory for selected_folder_path\n"); + return; + } + snprintf(selected_folder_path, 512, "%s/%u", PGNSS_DATA_PATH, selected_timestamp); // 检查文件夹是否已存在 DIR *dir = opendir(selected_folder_path); @@ -1212,6 +1337,7 @@ static void agnss_process_complete_response(void) // 文件夹已存在,关闭目录句柄 closedir(dir); } + free(selected_folder_path); } else { printf("没有找到符合条件的时间戳文件夹\n"); } @@ -1226,9 +1352,13 @@ static void agnss_process_complete_response(void) if (selected_timestamp > 0) { printf("开始下载时间戳 %u 的文件...\n", selected_timestamp); - // 将选定的时间戳转换为字符串,用于字符串比较 - char selected_timestamp_str[32]; - snprintf(selected_timestamp_str, sizeof(selected_timestamp_str), "%u", selected_timestamp); + // 将选定的时间戳转换为字符串,用于字符串比较 + char *selected_timestamp_str = malloc(32); + if (selected_timestamp_str == NULL) { + printf("Error: Failed to allocate memory for selected_timestamp_str\n"); + return; + } + snprintf(selected_timestamp_str, 32, "%u", selected_timestamp); if (data_start) { data_start += 8; // 跳过 "data":[ @@ -1248,7 +1378,12 @@ static void agnss_process_complete_response(void) if (url_end) { int url_len = url_end - url_start; if (url_len > 0 && url_len < 512) { - char temp_url[512] = {0}; + char *temp_url = malloc(512); + if (temp_url == NULL) { + printf("Error: Failed to allocate memory for temp_url\n"); + continue; + } + memset(temp_url, 0, 512); strncpy(temp_url, url_start, url_len); // 检查URL是否属于选定的时间戳文件夹 @@ -1259,6 +1394,7 @@ static void agnss_process_complete_response(void) total_files_count++; } } + free(temp_url); } } } @@ -1312,13 +1448,20 @@ static void agnss_process_complete_response(void) continue; } - char filename[256] = {0}; + char *filename = malloc(256); + if (filename == NULL) { + printf("Error: Failed to allocate memory for filename\n"); + item_start++; + continue; + } + memset(filename, 0, 256); strncpy(filename, filename_start, filename_len); filename[filename_len] = '\0'; // 提取downloadUrl char *url_start = strstr(item_start, "\"downloadUrl\":\""); if (!url_start) { + free(filename); item_start++; continue; } @@ -1326,17 +1469,26 @@ static void agnss_process_complete_response(void) char *url_end = strchr(url_start, '"'); if (!url_end) { + free(filename); item_start++; continue; } int url_len = url_end - url_start; if (url_len <= 0 || url_len >= 512) { + free(filename); item_start++; continue; } - char download_url[512] = {0}; + char *download_url = malloc(512); + if (download_url == NULL) { + printf("Error: Failed to allocate memory for download_url\n"); + free(filename); + item_start++; + continue; + } + memset(download_url, 0, 512); strncpy(download_url, url_start, url_len); download_url[url_len] = '\0'; @@ -1353,6 +1505,10 @@ static void agnss_process_complete_response(void) } } + // 释放内存 + free(filename); + free(download_url); + // 移动到下一个文件项 char *next_item = strstr(item_start, "},"); if (next_item) { @@ -1371,6 +1527,7 @@ static void agnss_process_complete_response(void) } else { printf("没有找到符合条件的时间戳文件夹,跳过下载\n"); } + free(selected_timestamp_str); } else { printf("没有找到符合条件的时间戳文件夹\n"); } @@ -1389,8 +1546,8 @@ static void agnss_process_complete_response(void) g_processing_count = 0; #ifdef SUPPORT_CAT1_SERVICE - cat1_api_remove_cat1_vote(); - cat1_api_disable_cat1_cmd(0); + cat1_api_keep_wakeup(0); + cat1_api_wakeup_cat1(0); #endif // 验证下载的文件 @@ -1411,8 +1568,12 @@ int download_agnss_file(const char *filename) uapi_get_timestampe_ss(×tamp); // 构建完整URL - char full_url[512]; - snprintf(full_url, sizeof(full_url), + char *full_url = malloc(512); + if (full_url == NULL) { + printf("Error: Failed to allocate memory for full_url in download_agnss_file\n"); + return -1; + } + snprintf(full_url, 512, "http://iot-pub.tos-cn-guangzhou.volces.com/dev/AGNSS/geteph?deviceId=121314243×tamp=%u", timestamp); @@ -1421,6 +1582,9 @@ int download_agnss_file(const char *filename) // 使用新的httpclient.h接口下载文件 HTTPC_RESULT result = download_file_with_httpclient(filename, full_url); + // 释放内存 + free(full_url); + if (result == HTTP_SUCCESS) { printf("File %s downloaded successfully\n", filename); return 0; @@ -1541,8 +1705,9 @@ int download_agnss_files(void) //通讯开始,唤醒cat1 #ifdef SUPPORT_CAT1_SERVICE + cat1_api_wakeup_cat1(1); + cat1_api_keep_wakeup(1); cat1_api_add_cat1_vote(); - cat1_api_disable_cat1_cmd(0); // 修改:允许CAT1命令,确保DNS查询可用 #endif // 等待CAT1就绪 LOS_Msleep(500); // 修改:减少等待时间,参考ln_api_device_info.c @@ -1552,8 +1717,12 @@ int download_agnss_files(void) uapi_get_timestampe_ss(×tamp); // 构建完整URL - char full_url[512]; - snprintf(full_url, sizeof(full_url), + char *full_url = malloc(512); + if (full_url == NULL) { + printf("Error: Failed to allocate memory for full_url in download_agnss_files\n"); + return -1; + } + snprintf(full_url, 512, "http://app.api.thisiot.com/dev/AGNSS/geteph?deviceId=121314243×tamp=%u", timestamp); @@ -1561,7 +1730,7 @@ int download_agnss_files(void) cleanup_agnss_response_buffer(); // 添加重试机制 - int max_retries = 6; + int max_retries = 10; int retry_count = 0; HTTPC_RESULT result = HTTP_EDNS; @@ -1628,6 +1797,9 @@ int download_agnss_files(void) retry_count++; } + // 释放URL内存 + free(full_url); + if (result != HTTP_SUCCESS) { agnss_download_result_handler(NULL, result, 0, NULL, 0); // 下载失败,设置失败状态并清除标志位 @@ -1645,7 +1817,8 @@ int download_agnss_files(void) #ifdef SUPPORT_CAT1_SERVICE cat1_api_remove_cat1_vote(); - cat1_api_disable_cat1_cmd(0); + cat1_api_keep_wakeup(0); + cat1_api_wakeup_cat1(0); #endif } else { @@ -2360,7 +2533,8 @@ int gnss_start_agnss_download_simple(void) // 下载取消,清理CAT1资源 #ifdef SUPPORT_CAT1_SERVICE cat1_api_remove_cat1_vote(); - cat1_api_disable_cat1_cmd(0); + cat1_api_keep_wakeup(0); + cat1_api_wakeup_cat1(0); #endif return -1; } @@ -2373,8 +2547,9 @@ int gnss_start_agnss_download_simple(void) // 下载取消,清理CAT1资源 #ifdef SUPPORT_CAT1_SERVICE - cat1_api_remove_cat1_vote(); - cat1_api_disable_cat1_cmd(0); + cat1_api_remove_cat1_vote(); + cat1_api_keep_wakeup(0); + cat1_api_wakeup_cat1(0); #endif return -1; } @@ -2418,6 +2593,9 @@ errcode_t uapi_gnss_http_server_deinit(void) // 清理响应缓冲区 cleanup_agnss_response_buffer(); + + // 清理extract_folder_path的静态内存 + cleanup_extract_folder_path(); // 终止发送线程 if (g_send_thread_id != NULL) { @@ -2455,11 +2633,10 @@ errcode_t uapi_gnss_http_server_deinit(void) g_app_recv_queue = 0xFFFFFFFF; } - // 清理CAT1资源 #ifdef SUPPORT_CAT1_SERVICE - cat1_api_remove_cat1_vote(); - cat1_api_disable_cat1_cmd(0); - PRINT("CAT1 resources cleaned up\r\n"); + cat1_api_remove_cat1_vote(); + cat1_api_keep_wakeup(0); + cat1_api_wakeup_cat1(0); #endif // 重置任务配置结构体