static int g_ln_is_file_downloading = 0; // 是否正在下载文件
char g_ln_character_id[64] = {0}; // 角色ID
static int g_ln_file_download_type = LN_FILE_DOWNLOAD_TYPE_NONE; // 文件下载类型
+static int g_ln_is_ota_downloading = 0; // 是否正在下载OTA升级包
/* 简单的字符串读写(以.bin形式读写原始字节,末尾补'\0')*/
static int ln_read_string_file(const char *file_path, char **out_str)
g_ln_file_download_type = type;
}
+/* 获取当前是否正在下载OTA升级包 */
+int uapi_ln_is_ota_downloading(void)
+{
+ return g_ln_is_ota_downloading;
+}
+
+/* 设置OTA下载状态(内部使用) */
+void uapi_ln_set_ota_downloading(int downloading)
+{
+ g_ln_is_ota_downloading = downloading;
+}
+
/* 获取ota包大小 */
static int g_ln_ota_package_size = 0;
int uapi_ln_get_ota_package_size(void)
// printf("[WS] cmd=%d message_id=%s cmd_str=%s\n", (int)type, message_id ? message_id : "", cmd_str ? cmd_str : "");
+ if (uapi_ln_is_ota_downloading() == 1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[WS] 正在下载OTA升级包,不处理业务消息");
+ return;
+ }
+
switch (type)
{
case WS_CMD_CHARACTER_CHANGE:
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"[OTA_DOWNLOAD] MD5校验失败,建议重新下载");
uapi_pm_remove_sleep_veto(PM_ID_DEBUG);
push_ota_status(OTA_UI_STATUS_FAILED);
+ /* 清除OTA下载状态 */
+ uapi_ln_set_ota_downloading(0);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] MD5校验失败,清除下载状态保护");
} else {
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"[OTA_DOWNLOAD] MD5校验出错");
uapi_pm_remove_sleep_veto(PM_ID_DEBUG);
push_ota_status(OTA_UI_STATUS_FAILED);
+ /* 清除OTA下载状态 */
+ uapi_ln_set_ota_downloading(0);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] MD5校验出错,清除下载状态保护");
}
}
break;
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"[OTA_DOWNLOAD] 下载失败: %s", msg->error_msg);
uapi_pm_remove_sleep_veto(PM_ID_DEBUG);
push_ota_status(OTA_UI_STATUS_FAILED);
+ /* 清除OTA下载状态 */
+ uapi_ln_set_ota_downloading(0);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] 下载失败,清除下载状态保护");
break;
default:
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"[OTA_DOWNLOAD] 未知状态: %d", msg->status);
(void)device_id;
(void)device_token;
(void)source_version;
+
+ /* 检查是否正在下载OTA升级包,避免重复下载 */
+ if (uapi_ln_is_ota_downloading() == 1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] OTA升级包正在下载中,拒绝重复下载命令");
+ return ERRCODE_FAIL;
+ }
+
if (!g_ln_last_target_version || !g_ln_last_package_url || g_ln_last_target_version[0] == '\0' || g_ln_last_package_url[0] == '\0')
{
printf("[OTA] no cached task to download, please GET_TASK first\n");
return ERRCODE_FAIL;
}
+
+ /* 设置OTA下载状态,防止被打断 */
+ uapi_ln_set_ota_downloading(1);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] 开始OTA升级包下载,设置下载状态保护");
/* 固定保存到统一升级包路径 */
size_t sp_cap = strlen(UPG_FILE_NAME) + 1;
printf("UPG_FILE_NAME:%s\n",UPG_FILE_NAME);
char *save_path = (char *)malloc(sp_cap);
if (!save_path)
{
+ /* 清除OTA下载状态 */
+ uapi_ln_set_ota_downloading(0);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] 内存分配失败,清除下载状态保护");
return ERRCODE_FAIL;
}
(void)snprintf(save_path, sp_cap, "%s", UPG_FILE_NAME);
if(remove(save_path) != 0)
{
printf("[OTA] remove file failed: %s\n", save_path);
+ /* 清除OTA下载状态 */
+ uapi_ln_set_ota_downloading(0);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] 删除旧文件失败,清除下载状态保护");
+ free(save_path);
return ERRCODE_FAIL;
}
}
om.status = OTA_DOWNLOAD_STATUS_FAILED;
snprintf(om.error_msg, sizeof(om.error_msg), "%s", "download failed");
ln_ota_handle_download_msg(&om);
+ /* 清除OTA下载状态 */
+ uapi_ln_set_ota_downloading(0);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "[OTA_DOWNLOAD] 下载失败,清除下载状态保护");
free(save_path);
return ERRCODE_FAIL;
}
if (g_ln_last_package_md5) snprintf(om.md5, sizeof(om.md5), "%s", g_ln_last_package_md5);
ln_ota_handle_download_msg(&om);
}
+ /* 注意:下载完成后的状态清除在 ln_ota_handle_download_msg 中处理(MD5校验成功后会重启,失败则清除) */
free(save_path);
return ERRCODE_SUCC;
}