if (!device_id || !device_token || !source_version)
return ERRCODE_FAIL;
+ #ifdef SUPPORT_CAT1_SERVICE
/* CAT1 保活 */
cat1_api_add_cat1_vote();
cat1_api_wakeup_cat1(1);
cat1_api_keep_wakeup(1);
- cat1_api_disable_cat1_cmd(0);
+ cat1_api_disable_cat1_cmd(1);
+ LOS_Msleep(200);
+ #endif
HttpClient client = (HttpClient){0};
HttpClientData clientData = (HttpClientData){0};
return ERRCODE_SUCC;
}
-/* 分片下载:使用 Range 头按固定分片大小请求,边下边写并上报进度 */
-static int ln_download_file_to_path_range(const char *full_url, const char *save_path, int total_size)
-{
- if (!full_url || !save_path || total_size <= 0)
- {
- printf("[OTA] range download: url=%s -> %s, total=%d\n", full_url, save_path, total_size);
- return -1;
- }
- printf("[OTA] range download: url=%s -> %s, total=%d\n", full_url, save_path, total_size);
-
- /* CAT1 网络保活处理 */
- cat1_api_add_cat1_vote();
- cat1_api_wakeup_cat1(1);
- cat1_api_keep_wakeup(1);
- cat1_api_disable_cat1_cmd(0);
-
- FILE *fp = fopen(save_path, "wb");
- if (!fp)
- {
- printf("[OTA] open file failed: %s\n", save_path);
- return -1;
- }
-
- const int chunk_size = 256 * 1024; /* 256KB 分片,兼顾内存与吞吐 */
- int64_t downloaded = 0;
-
- while (downloaded < total_size)
- {
- int start = (int)downloaded;
- int remaining = total_size - start;
- int this_chunk = remaining > chunk_size ? chunk_size : remaining;
- int end = start + this_chunk - 1;
-
- HttpClient client = (HttpClient){0};
- HttpClientData clientData = (HttpClientData){0};
-
- /* 判断协议 */
- bool is_https = (strstr(full_url, "https://") == full_url);
- client.isHttp = is_https ? false : true;
- client.remotePort = is_https ? 443 : 80;
-
- /* 自定义 Range 头 */
- char header_buf[256];
- int hdr = snprintf(header_buf, sizeof(header_buf), "Range: bytes=%d-%d\r\n", start, end);
- if (hdr <= 0 || hdr >= (int)sizeof(header_buf))
- {
- printf("[OTA] range header failed: %d\n", hdr);
- fclose(fp);
- return -1;
- }
- HttpClientSetCustomHeader(&client, header_buf);
-
- /* 预留足够的响应缓冲区(分片大小+余量)*/
- HTTPC_RESULT prep = ln_http_prepare_auto(&clientData, 2048, this_chunk + 16 * 1024);
- if (prep != HTTP_SUCCESS)
- {
- printf("[OTA] range prepare failed: %d\n", prep);
- fclose(fp);
- return -1;
- }
-
- HTTPC_RESULT res = HttpClientGetRequest(&client, full_url, &clientData);
- if (res != HTTP_SUCCESS)
- {
- printf("[OTA] range request failed: %d\n", res);
- HttpClientUnprepare(&clientData);
- HttpClientClse(&client);
- fclose(fp);
- return -1;
- }
-
- int code = HttpClientGetResponseCode(&client);
- if (!(code == 206 || code == 200))
- {
- printf("[OTA] range HTTP code=%d\n", code);
- HttpClientUnprepare(&clientData);
- HttpClientClse(&client);
- fclose(fp);
- return -1;
- }
-
- if (clientData.responseBuf == NULL || clientData.responseBufLen <= 0)
- {
- printf("[OTA] range empty body\n");
- HttpClientUnprepare(&clientData);
- HttpClientClse(&client);
- fclose(fp);
- return -1;
- }
-
- int buf_len = clientData.responseContentLen > 0 ? clientData.responseContentLen : clientData.responseBufLen;
- size_t written = fwrite(clientData.responseBuf, 1, (size_t)buf_len, fp);
- if ((int)written != buf_len)
- {
- printf("[OTA] range fwrite failed\n");
- HttpClientUnprepare(&clientData);
- HttpClientClse(&client);
- fclose(fp);
- return -1;
- }
-
- downloaded += buf_len;
- printf("[OTA] range downloaded: %d/%d\n", downloaded, total_size);
-
- /* 进度上报 */
- int percent = (int)((downloaded * 100) / total_size);
- if (percent > 100)
- percent = 100;
- char prog[128];
- int pn = snprintf(prog, sizeof(prog), "{\"status\":%d,\"progress\":%d}", OTA_DOWNLOAD_STATUS_PROGRESS, percent);
- if (pn > 0)
- do_ln_http_server_recv_data(LN_HTTP_RECV_OTA_DOWNLOAD_MSG, prog, (uint32_t)pn);
-
- HttpClientUnprepare(&clientData);
- HttpClientClse(&client);
- }
-
- fclose(fp);
- return 0;
-}
-
/* 采用http_download风格实现的文件下载函数
* 功能:从指定URL下载文件到指定路径(已知总大小)
* 参数:full_url - 完整下载URL
printf("[OTA] starting download: %s -> %s, total_size=%d bytes\n",
full_url, save_path, total_size);
+ #ifdef SUPPORT_CAT1_SERVICE
/* CAT1 网络保活处理 */
cat1_api_add_cat1_vote();
cat1_api_wakeup_cat1(1);
cat1_api_keep_wakeup(1);
- cat1_api_disable_cat1_cmd(0);
- LOS_Msleep(300); // 等待模块就绪
+ cat1_api_disable_cat1_cmd(1);
+ LOS_Msleep(500); // 等待模块就绪
+ #endif
/* 初始化HTTP客户端(与http_download风格一致) */
HttpClient client = {0};