// 单个文件下载的结果处理 - 只保留成功信息
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);
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);
closedir(dir);
}
}
+
+ // 释放内存
+ free(filepath);
}
} else {
// AGNSS文件列表下载的结果处理
// 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
}
}
// 不在开始时更新进度,只在成功时更新
// 解析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 格式
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/");
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客户端
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);
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; // 跳过 "://"
// 跳过主机名部分
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);
// printf(" [WARNING] 文件夹创建失败: %s (错误码: %d)\n", full_path, result);
// }
+ free(full_path);
return 0;
}
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);
// 这是一个目录,递归遍历
closedir(sub_dir);
verify_files_recursive(full_path, total_count);
+ free(full_path);
} else {
// 这是一个文件,检查是否是AGNSS相关文件
if (strstr(entry->d_name, ".eph") != NULL ||
}
}
}
+ free(full_path);
}
}
// 提取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';
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';
}
}
}
+ free(folder_timestamp_str);
}
}
}
+ free(download_url);
}
search_pos = url_end + 1;
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);
// 文件夹已存在,关闭目录句柄
closedir(dir);
}
+ free(selected_folder_path);
} else {
printf("没有找到符合条件的时间戳文件夹\n");
}
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":[
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是否属于选定的时间戳文件夹
total_files_count++;
}
}
+ free(temp_url);
}
}
}
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;
}
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';
}
}
+ // 释放内存
+ free(filename);
+ free(download_url);
+
// 移动到下一个文件项
char *next_item = strstr(item_start, "},");
if (next_item) {
} else {
printf("没有找到符合条件的时间戳文件夹,跳过下载\n");
}
+ free(selected_timestamp_str);
} else {
printf("没有找到符合条件的时间戳文件夹\n");
}
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
// 验证下载的文件
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);
// 使用新的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;
//通讯开始,唤醒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
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);
cleanup_agnss_response_buffer();
// 添加重试机制
- int max_retries = 6;
+ int max_retries = 10;
int retry_count = 0;
HTTPC_RESULT result = HTTP_EDNS;
retry_count++;
}
+ // 释放URL内存
+ free(full_url);
+
if (result != HTTP_SUCCESS) {
agnss_download_result_handler(NULL, result, 0, NULL, 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
} else {
// 下载取消,清理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;
}
// 下载取消,清理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;
}
// 清理响应缓冲区
cleanup_agnss_response_buffer();
+
+ // 清理extract_folder_path的静态内存
+ cleanup_extract_folder_path();
// 终止发送线程
if (g_send_thread_id != NULL) {
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
// 重置任务配置结构体