diff --git a/ql-application/threadx/interface/custom_app/battery.c b/ql-application/threadx/interface/custom_app/battery.c index 7ca6d88..142e3db 100644 --- a/ql-application/threadx/interface/custom_app/battery.c +++ b/ql-application/threadx/interface/custom_app/battery.c @@ -19,9 +19,11 @@ #include "ql_atcmd.h" #include "app_inc.h" #include "ql_uart.h" -#include "ql_application.h" -#include "ql_log.h" -#include "hal_api.h" +#include "ql_application.h" +#include "ql_log.h" +#include "ql_adc.h" +#include "ql_power.h" +#include "hal_api.h" // 电量阈值定义 #define BAT_VOLTAGE_100 4200 @@ -36,25 +38,24 @@ #define BAT_VOLTAGE_10 3550 #define BAT_VOLTAGE_MIN 3500 -// ========== 新增:10分钟电压历史记录用于判断充放电状态 ========== -#define VOLTAGE_HISTORY_SIZE 60 // 10分钟 = 60个10秒采样点 -#define VOLTAGE_RISE_THRESHOLD 15 // 10分钟内上升超过15mV判定为充电 -#define VOLTAGE_DROP_THRESHOLD 15 // 10分钟内下降超过15mV判定为放电 -#define VOLTAGE_STABLE_RANGE 10 // 变化在±10mV内判定为稳定 +#define BAT_LOW_WARN_MV 3550 +#define BAT_LOW_PROBE_EXIT_MV 3600 +#define BAT_SHUTDOWN_MV 3500 +#define BAT_VALID_MIN_MV 2500 +#define BAT_VALID_MAX_MV 5000 +#define BAT_SHUTDOWN_CONFIRM_COUNT 3 + +#define BAT_NORMAL_SAMPLE_CALLS 6 unsigned short g_vbat_vol = 0; unsigned short vbat_sample[6] = {0}; // 修改:样本数改为6 unsigned char battery_first_update_flag = 0; -static unsigned short g_vbat_vol_last = 0; -static bat_level_t g_bat_level_last = BAT_LEVEL_100; - -// ========== 新增:电压历史记录环形缓冲区 ========== -static unsigned short g_voltage_history[VOLTAGE_HISTORY_SIZE] = {0}; -static unsigned char g_history_index = 0; -static unsigned char g_history_count = 0; // 已记录的有效点数 -static int g_charging_state = 0; // 当前判定的充电状态:1-充电, 0-未充电 -static unsigned char g_charging_stable_cnt = 0; // 状态稳定计数 +static bat_level_t g_bat_level_last = BAT_LEVEL_100; +static unsigned char g_bat_level_valid = 0; +static unsigned char g_low_voltage_count = 0; +static unsigned char g_low_probe_active = 0; +static unsigned char g_low_power_warned = 0; bat_level_t calc_battery_level(uint16_t voltage_mv) { @@ -71,101 +72,64 @@ bat_level_t calc_battery_level(uint16_t voltage_mv) else return BAT_LEVEL_0; } -/** - * @brief 基于10分钟电压历史判断充放电状态 - * @return 1-充电中, 0-未充电/放电 - */ -static int detect_charging_by_trend(void) -{ - if (g_history_count < VOLTAGE_HISTORY_SIZE) { - printf("[BAT] history not full (%d/%d), keep state:%d\r\n", - g_history_count, VOLTAGE_HISTORY_SIZE, g_charging_state); - return g_charging_state; - } - - unsigned char old_index = (g_history_index + 1) % VOLTAGE_HISTORY_SIZE; - unsigned short vbat_now = g_voltage_history[(g_history_index + VOLTAGE_HISTORY_SIZE - 1) % VOLTAGE_HISTORY_SIZE]; - unsigned short vbat_10min_ago = g_voltage_history[old_index]; - - short delta = (short)vbat_now - (short)vbat_10min_ago; - - printf("[BAT] trend: now=%d, 10min_ago=%d, delta=%d\r\n", - vbat_now, vbat_10min_ago, delta); - - int new_state = g_charging_state; - - if (delta > VOLTAGE_RISE_THRESHOLD) { - new_state = 1; - printf("[BAT] trend: CHARGING detected (rise %dmV)\r\n", delta); - } - else if (delta < -VOLTAGE_DROP_THRESHOLD) { - new_state = 0; - printf("[BAT] trend: DISCHARGING detected (drop %dmV)\r\n", -delta); - } - else { - printf("[BAT] trend: STABLE (delta %dmV), keep state:%d\r\n", delta, g_charging_state); - } - - if (new_state == g_charging_state) { - g_charging_stable_cnt = 0; - } else { - g_charging_stable_cnt++; - if (g_charging_stable_cnt < 3) { - printf("[BAT] state change pending: %d -> %d (%d/3)\r\n", - g_charging_state, new_state, g_charging_stable_cnt); - return g_charging_state; - } - g_charging_stable_cnt = 0; - printf("[BAT] state changed: %d -> %d (confirmed)\r\n", g_charging_state, new_state); - } - - return new_state; -} - -static void update_voltage_history(unsigned short voltage) -{ - g_voltage_history[g_history_index] = voltage; - g_history_index = (g_history_index + 1) % VOLTAGE_HISTORY_SIZE; - if (g_history_count < VOLTAGE_HISTORY_SIZE) { - g_history_count++; - } -} - -static int is_charging(void) -{ - // 如有硬件检测引脚,可优先使用 - return detect_charging_by_trend(); -} - -static bat_level_t limit_level_jump(bat_level_t raw_level, bat_level_t last_level, int charging) -{ - bat_level_t result = last_level; - - if (charging) { - if (raw_level > last_level) { - if (raw_level - last_level == 1) { - result = raw_level; - printf("[BAT] level up (charging): %d -> %d\r\n", last_level, raw_level); - } else if (raw_level - last_level > 1) { - result = (bat_level_t)(last_level + 1); - printf("[BAT] level up limited (charging): %d -> %d (raw:%d)\r\n", - last_level, result, raw_level); - } - } - } else { - if (raw_level < last_level) { - if (last_level - raw_level == 1) { - result = raw_level; - printf("[BAT] level down (discharging): %d -> %d\r\n", last_level, raw_level); - } else if (last_level - raw_level > 1) { - result = (bat_level_t)(last_level - 1); - printf("[BAT] level down limited (discharging): %d -> %d (raw:%d)\r\n", - last_level, result, raw_level); - } - } - } - return result; -} +bat_level_t battery_get_level(void) +{ + return g_bat_level_valid ? g_bat_level_last : calc_battery_level(g_vbat_vol); +} + +static bat_level_t limit_level_jump(bat_level_t raw_level) +{ + bat_level_t result = g_bat_level_last; + + if (!g_bat_level_valid) { + g_bat_level_valid = 1; + g_bat_level_last = raw_level; + printf("[BAT] first level initialized: %d\r\n", raw_level); + return raw_level; + } + + if (raw_level > g_bat_level_last) { + result = (raw_level >= g_bat_level_last + BAT_LEVEL_10) + ? (bat_level_t)(g_bat_level_last + BAT_LEVEL_10) + : raw_level; + } else if (raw_level < g_bat_level_last) { + result = (raw_level <= g_bat_level_last - BAT_LEVEL_10) + ? (bat_level_t)(g_bat_level_last - BAT_LEVEL_10) + : raw_level; + } + + printf("[BAT] level update: last=%d raw=%d result=%d\r\n", + g_bat_level_last, raw_level, result); + g_bat_level_last = result; + return result; +} + +static void battery_check_critical_voltage(unsigned short voltage_mv) +{ + if (voltage_mv < BAT_LOW_WARN_MV) { + g_low_probe_active = 1; + } else if (voltage_mv >= BAT_LOW_PROBE_EXIT_MV) { + g_low_probe_active = 0; + } + + if (voltage_mv < BAT_SHUTDOWN_MV) { + if (g_low_voltage_count < BAT_SHUTDOWN_CONFIRM_COUNT) { + g_low_voltage_count++; + } + + printf("[BAT] critical voltage:%d, confirm:%d/%d\r\n", + voltage_mv, g_low_voltage_count, BAT_SHUTDOWN_CONFIRM_COUNT); + + if (g_low_voltage_count >= BAT_SHUTDOWN_CONFIRM_COUNT) { + printf("[BAT] critical voltage confirmed, poweroff now\r\n"); + app_play_tips_file(15); + ql_rtos_task_sleep_s(5); + ql_power_down(1); + } + } else { + g_low_voltage_count = 0; + } +} /* 修改后的 battery_check:分阶段采样,采样窗口为6次 */ void battery_check(void) @@ -174,15 +138,17 @@ void battery_check(void) static unsigned char sample_idx = 0; // 当前已采集样本数 (0~5) static unsigned short sample_buf[6] = {0}; // 本地缓冲(也可用全局 vbat_sample,此处直接用局部) - call_count++; + call_count++; /* 判断本次是否执行ADC采样 */ unsigned char do_sample = 0; - if (call_count <= 24) { // 开机2分钟内 (24次*5s=120s) - do_sample = 1; - } else { - // 2分钟后,每36次调用采样一次 (36*5s=180s=3分钟) - if ((call_count - 25) % 36 == 0) { + if (call_count <= 24) { // 开机2分钟内 (24次*5s=120s) + do_sample = 1; + } else if (g_low_probe_active) { + do_sample = 1; // 低电风险时每5秒快速确认 + } else { + // 2分钟后,每6次调用采样一次 (6*5s=30秒) + if (call_count % BAT_NORMAL_SAMPLE_CALLS == 0) { do_sample = 1; } } @@ -195,9 +161,21 @@ void battery_check(void) u16 vbat_vol = 0; ql_adc_onff(1); ql_rtos_task_sleep_ms(5); - ql_adc_onff(0); - vbat_vol = ql_get_battery_vol(); - sample_buf[sample_idx] = vbat_vol; + ql_adc_onff(0); + vbat_vol = ql_get_battery_vol(); + + printf("[BAT][SAMPLE] voltage=%d, low_probe=%d\r\n", + vbat_vol, g_low_probe_active); + + if (vbat_vol < BAT_VALID_MIN_MV || vbat_vol > BAT_VALID_MAX_MV) { + printf("[BAT] invalid voltage sample ignored:%d\r\n", vbat_vol); + g_low_voltage_count = 0; + return; + } + + battery_check_critical_voltage(vbat_vol); + + sample_buf[sample_idx] = vbat_vol; sample_idx++; /* 当收集满6个样本时进行平均处理 */ @@ -215,47 +193,29 @@ void battery_check(void) vol_all = vol_all - min - max; vbat_vol = vol_all / 4; // 6个样本去掉最大最小,剩余4个平均 - /* 更新电压历史(用于充电趋势判断) */ - update_voltage_history(vbat_vol); + g_vbat_vol = vbat_vol; + printf("vbat_vol:%d\r\n", g_vbat_vol); - /* 判断充电状态 */ - int charging = is_charging(); - g_charging_state = charging; - - /* 非充电状态下,电压不允许上升 */ - if (!charging) { - if (g_vbat_vol_last > 0 && vbat_vol > g_vbat_vol_last) { - printf("[BAT] voltage rise blocked: %d -> %d (not charging)\r\n", - g_vbat_vol_last, vbat_vol); - vbat_vol = g_vbat_vol_last; - } - } - - g_vbat_vol = vbat_vol; - g_vbat_vol_last = vbat_vol; - printf("vbat_vol:%d, charging:%d\r\n", g_vbat_vol, charging); - - /* 计算电量等级并限制跳变 */ - bat_level_t raw_level = calc_battery_level(g_vbat_vol); - bat_level_t level = limit_level_jump(raw_level, g_bat_level_last, charging); - g_bat_level_last = level; - - printf("[BAT] raw_level:%d, limited_level:%d, charging:%d\r\n", - raw_level, level, charging); + /* 计算电量等级并限制跳变 */ + bat_level_t raw_level = calc_battery_level(g_vbat_vol); + bat_level_t level = limit_level_jump(raw_level); + + printf("[BAT] raw_level:%d, limited_level:%d\r\n", + raw_level, level); if (battery_first_update_flag == 0) { battery_first_update_flag = 1; } - /* 低电量处理 */ - if (level == BAT_LEVEL_0) { - printf("poweroff now\r\n"); - app_play_tips_file(15); - ql_rtos_task_sleep_s(5); - ql_power_down(1); - } else if (level <= BAT_LEVEL_10) { - app_play_tips_file(14); - } + /* 极低电关机由原始电压独立处理,普通低电提示只播放一次 */ + if (level <= BAT_LEVEL_10) { + if (!g_low_power_warned) { + g_low_power_warned = 1; + app_play_tips_file(14); + } + } else if (level >= BAT_LEVEL_20) { + g_low_power_warned = 0; + } sample_idx = 0; // 重置样本索引,准备下一轮采集 } diff --git a/ql-application/threadx/interface/custom_app/mp3_player.c b/ql-application/threadx/interface/custom_app/mp3_player.c index be5046a..d11092a 100644 --- a/ql-application/threadx/interface/custom_app/mp3_player.c +++ b/ql-application/threadx/interface/custom_app/mp3_player.c @@ -280,10 +280,11 @@ u32 local_mp3_data_fetch(const char *uri, uint8_t *buffer, u32 length) size_t url_mp3_data_fetch(const char *uri, uint8_t *buffer, size_t length) { - int resp_status = -1; - uint32_t nw = length; - uint32_t page_pos = 0; - int error_cnt = 0; + int resp_status = -1; + uint32_t nw = length; + uint32_t page_pos = 0; + uint32_t content_length = 0; + int error_cnt = 0; //printf("fd_fetch start\n"); session = webclient_session_create(1024); @@ -306,11 +307,12 @@ size_t url_mp3_data_fetch(const char *uri, uint8_t *buffer, size_t length) goto __exit; } - if (session->content_length == 0) - { - printf("Request file size is 0!"); - goto __exit; - } + if (session->content_length == 0) + { + printf("Request file size is 0!"); + goto __exit; + } + content_length = session->content_length; printf("start read\n"); if(total_length < session->content_length) @@ -325,7 +327,7 @@ size_t url_mp3_data_fetch(const char *uri, uint8_t *buffer, size_t length) page_pos = 0; do { - if(vad_audio_is_recording()) { + if(vad_audio_is_recording() || tts_playing_ret()==2) { break; } @@ -361,7 +363,7 @@ __exit: session = NULL; } - if(total_lengthcontent_length){ + if(total_lengthchecksum); i++) { + hash ^= data[i]; + hash *= 16777619U; + } + + return hash; +} + +static int ota_state_save(const char *previous_version, const char *target_version) +{ + ota_state_t state; + QFILE *fp; + int ret; + + if (previous_version == NULL || target_version == NULL) { + return -1; + } + + memset(&state, 0, sizeof(state)); + state.magic = OTA_STATE_MAGIC; + state.format_version = OTA_STATE_FORMAT_VERSION; + strncpy(state.previous_version, previous_version, sizeof(state.previous_version) - 1); + strncpy(state.target_version, target_version, sizeof(state.target_version) - 1); + state.checksum = ota_state_checksum(&state); + + fp = ql_fopen(OTA_STATE_TMP_FILE, "wb+"); + if (fp == NULL) { + RLOGE("OTA state open failed: %s", OTA_STATE_TMP_FILE); + return -1; + } + + ret = ql_fwrite(&state, sizeof(state), 1, fp); + if (ret != (int)sizeof(state)) { + RLOGE("OTA state write failed: %d", ret); + ql_fclose(fp); + ql_remove(OTA_STATE_TMP_FILE); + return -1; + } + + ret = ql_fsync(fp); + ql_fclose(fp); + if (ret != 0) { + RLOGE("OTA state sync failed: %d", ret); + ql_remove(OTA_STATE_TMP_FILE); + return -1; + } + + ret = ql_rename(OTA_STATE_TMP_FILE, OTA_STATE_FILE); + if (ret != 0) { + RLOGE("OTA state rename failed: %d", ret); + ql_remove(OTA_STATE_TMP_FILE); + return -1; + } + + return 0; +} + +static int ota_state_load(ota_state_t *state) +{ + QFILE *fp; + int ret; + uint32_t checksum; + + if (state == NULL || ql_access(OTA_STATE_FILE, 0) != 0) { + return -1; + } + + fp = ql_fopen(OTA_STATE_FILE, "rb"); + if (fp == NULL) { + return -1; + } + + memset(state, 0, sizeof(*state)); + ret = ql_fread(state, sizeof(*state), 1, fp); + ql_fclose(fp); + if (ret != (int)sizeof(*state)) { + RLOGE("OTA state read failed: %d", ret); + return -1; + } + + checksum = state->checksum; + state->checksum = 0; + if (state->magic != OTA_STATE_MAGIC || + state->format_version != OTA_STATE_FORMAT_VERSION || + checksum != ota_state_checksum(state)) { + RLOGE("OTA state validation failed"); + return -1; + } + + state->checksum = checksum; + state->previous_version[sizeof(state->previous_version) - 1] = '\0'; + state->target_version[sizeof(state->target_version) - 1] = '\0'; + return 0; +} + +static void ota_state_clear(void) +{ + ql_remove(OTA_STATE_TMP_FILE); + if (ql_access(OTA_STATE_FILE, 0) == 0) { + ql_remove(OTA_STATE_FILE); + } +} + +static int ota_parse_version(const char *version, unsigned int parts[3]) +{ + char tail; + + if (version == NULL || version[0] == '\0') { + return -1; + } + + if (sscanf(version, "%u.%u.%u%c", + &parts[0], &parts[1], &parts[2], &tail) != 3) { + return -1; + } + + return 0; +} + +static int ota_compare_version(const char *left, const char *right, int *result) +{ + unsigned int left_parts[3]; + unsigned int right_parts[3]; + int i; + + if (result == NULL || + ota_parse_version(left, left_parts) != 0 || + ota_parse_version(right, right_parts) != 0) { + return -1; + } + + *result = 0; + for (i = 0; i < 3; i++) { + if (left_parts[i] > right_parts[i]) { + *result = 1; + break; + } + if (left_parts[i] < right_parts[i]) { + *result = -1; + break; + } + } + + return 0; +} + +static int ota_url_is_allowed(const char *url) +{ + size_t prefix_len = strlen(OTA_ALLOWED_URL_PREFIX); + + if (url == NULL || url[0] == '\0') { + return 0; + } + + if (strncmp(url, OTA_ALLOWED_URL_PREFIX, prefix_len) != 0) { + return 0; + } + + if (url[prefix_len] == '\0' || strstr(url + prefix_len, "..") != NULL) { + return 0; + } + + return strstr(url + prefix_len, ".bin") != NULL; } /* ==================== 解析函数 ==================== */ @@ -162,6 +357,11 @@ int parse_command(const char *json_str, command_t *cmd) // 解析 params cJSON *params = cJSON_GetObjectItem(root, "params"); + if (cmd->type == MSG_OTA_UPGRADE && params == NULL) { + RLOGE("OTA command has no params"); + cJSON_Delete(root); + return -1; + } if (params != NULL) { switch (cmd->type) { case MSG_GPS_QUERY: @@ -180,35 +380,27 @@ int parse_command(const char *json_str, command_t *cmd) } case MSG_OTA_UPGRADE: { - /*判断当前的固件版本与下发的是否一致,不一致,则触发升级*/ printf("MSG_OTA_UPGRADE\r\n"); cJSON *url = cJSON_GetObjectItem(params, "url"); cJSON *version = cJSON_GetObjectItem(params, "version"); - if (cJSON_IsString(url)) { - strncpy(cmd->params.ota.url, url->valuestring, - sizeof(cmd->params.ota.url) - 1); - } - if (cJSON_IsString(version)) { - strncpy(cmd->params.ota.version, version->valuestring, - sizeof(cmd->params.ota.version) - 1); + + if (!cJSON_IsString(url) || url->valuestring == NULL || + !cJSON_IsString(version) || version->valuestring == NULL || + url->valuestring[0] == '\0' || version->valuestring[0] == '\0' || + strlen(url->valuestring) >= sizeof(cmd->params.ota.url) || + strlen(version->valuestring) >= sizeof(cmd->params.ota.version)) { + RLOGE("invalid OTA command parameters"); + cJSON_Delete(root); + return -1; } + + strncpy(cmd->params.ota.url, url->valuestring, + sizeof(cmd->params.ota.url) - 1); + strncpy(cmd->params.ota.version, version->valuestring, + sizeof(cmd->params.ota.version) - 1); printf("ota.url:%s\r\n", cmd->params.ota.url); printf("ota.version:%s\r\n", cmd->params.ota.version); - - app_play_tips_file(3); - - fota_start = 1; - - ql_rtos_task_sleep_ms(6000); - - app_file_check_del(); - - ql_fullfota_app_start(cmd->params.ota.url); - /* - {'msg_id': '003', 'params': {'url': 'http://175.24.73.253/assets/firmware/20260425203347-fota.bin', 'version': '2.0.1'}} - - */ break; } @@ -555,6 +747,8 @@ int app_mqtt_connect() if(ret!=0){ build_register(); + } else { + ota_report_pending_success(); } RLOGD("========== app_mqtt_connect okay =========="); @@ -989,12 +1183,18 @@ char *build_wake_response(const char *msg_id, const char *switch_state) /** * @brief 构建OTA接受响应 */ -char *build_ota_accepted(const char *msg_id, const char *cur_ver, const char *tgt_ver) -{ - cJSON *root = cJSON_CreateObject(); - cJSON *data = cJSON_CreateObject(); - - cJSON_AddStringToObject(root, "msg_id", msg_id); +char *build_ota_accepted(const char *msg_id, const char *cur_ver, const char *tgt_ver) +{ + cJSON *root = cJSON_CreateObject(); + cJSON *data = cJSON_CreateObject(); + + if (root == NULL || data == NULL) { + cJSON_Delete(root); + cJSON_Delete(data); + return NULL; + } + + cJSON_AddStringToObject(root, "msg_id", msg_id); cJSON_AddStringToObject(root, "status", "accepted"); cJSON_AddStringToObject(data, "current_version", cur_ver); cJSON_AddStringToObject(data, "target_version", tgt_ver); @@ -1006,6 +1206,66 @@ char *build_ota_accepted(const char *msg_id, const char *cur_ver, const char *tg } /** + * @brief 构建OTA升级成功响应 + */ +static char *build_ota_success(const char *new_ver) +{ + cJSON *root = cJSON_CreateObject(); + cJSON *data = cJSON_CreateObject(); + + if (root == NULL || data == NULL) { + cJSON_Delete(root); + cJSON_Delete(data); + return NULL; + } + + cJSON_AddStringToObject(root, "msg_id", "003"); + cJSON_AddStringToObject(root, "status", "success"); + cJSON_AddStringToObject(data, "new_version", new_ver); + cJSON_AddItemToObject(root, "data", data); + + char *json_str = cJSON_PrintUnformatted(root); + cJSON_Delete(root); + return json_str; +} + +static void ota_report_pending_success(void) +{ + ota_state_t state; + char current_version[64] = {0}; + char *resp; + int compare_result; + int ret; + + if (ota_state_load(&state) != 0) { + return; + } + + hal_os_get_build_ver(current_version); + if (ota_compare_version(current_version, state.target_version, &compare_result) != 0 || + compare_result != 0) { + RLOGE("OTA pending state version mismatch: current=%s target=%s", + current_version, state.target_version); + return; + } + + resp = build_ota_success(current_version); + if (resp == NULL) { + RLOGE("build OTA success response failed"); + return; + } + + ret = app_mqtt_publish(resp, strlen(resp), 0); + free(resp); + if (ret == 0) { + printf("OTA success response published, version=%s\r\n", current_version); + ota_state_clear(); + } else { + RLOGE("OTA success response publish failed: %d", ret); + } +} + +/** * @brief 构建音量调节响应 */ char *build_volume_response(const char *msg_id, int current_level, const char *time) @@ -1030,6 +1290,95 @@ char *build_volume_response(const char *msg_id, int current_level, const char *t } /* ==================== 业务处理函数 ==================== */ + +static void handle_ota_upgrade(command_t *cmd) +{ + char current_version[64] = {0}; + char *resp; + int compare_result; + int ret; + uint32_t prompt_start_time; + + if (cmd == NULL || fota_start == 1) { + RLOGE("OTA request ignored: invalid command or OTA already running"); + return; + } + + if (!ota_url_is_allowed(cmd->params.ota.url)) { + RLOGE("OTA URL is not allowed: %s", cmd->params.ota.url); + return; + } + + hal_os_get_build_ver(current_version); + if (ota_compare_version(current_version, cmd->params.ota.version, &compare_result) != 0) { + RLOGE("invalid OTA version: current=%s target=%s", + current_version, cmd->params.ota.version); + return; + } + + if (compare_result > 0) { + RLOGE("OTA downgrade rejected: current=%s target=%s", + current_version, cmd->params.ota.version); + return; + } + + if (ota_state_save(current_version, cmd->params.ota.version) != 0) { + RLOGE("OTA state save failed"); + return; + } + + resp = build_ota_accepted(cmd->msg_id, + current_version, + cmd->params.ota.version); + if (resp == NULL) { + RLOGE("build OTA accepted response failed"); + ota_state_clear(); + return; + } + + ret = app_mqtt_publish(resp, strlen(resp), 0); + free(resp); + if (ret != 0) { + RLOGE("OTA accepted response publish failed: %d", ret); + ota_state_clear(); + return; + } + + if (compare_result == 0) { + printf("OTA target version is already installed: %s\r\n", current_version); + ota_report_pending_success(); + return; + } + + fota_start = 1; + ret = app_play_ota_tips_file(); + if (ret != 0) { + RLOGE("OTA prompt could not start: %d", ret); + } else { + prompt_start_time = hal_os_up_time_ms(); + while (xiaoice_get_play_counter() > 0) { + ql_rtos_task_sleep_ms(OTA_PROMPT_POLL_MS); + if (hal_os_up_time_ms() - prompt_start_time > OTA_PROMPT_PLAY_TIMEOUT_MS) { + RLOGE("OTA prompt playback timeout, counter=%d", + xiaoice_get_play_counter()); + break; + } + } + if (xiaoice_get_play_counter() == 0) { + printf("OTA prompt playback completed\r\n"); + } + } + + /* Product requirement: keep deleting the configured audio files before OTA. */ + app_file_check_del(); + + ret = ql_fullfota_app_start(cmd->params.ota.url); + if (ret != 0) { + RLOGE("start OTA failed: %d", ret); + fota_start = 0; + ota_state_clear(); + } +} /** * @brief 命令分发处理 @@ -1105,26 +1454,7 @@ void handle_command(command_t *cmd) } case MSG_OTA_UPGRADE: { - // 立即响应accepted - char *resp = build_ota_accepted(cmd->msg_id, - hal_os_get_current_version(), - cmd->params.ota.version); - - /*while(get_mqtt_public_mutex()!=0){ - hal_os_task_sleep_ms(100); - }*/ - /* - 添加删除所有本地音频动作 - */ - app_mqtt_publish(resp, strlen(resp), 0); - free(resp); - - // 启动OTA任务 - fota_start = 1; - - ql_rtos_task_sleep_ms(5000); - - ql_fullfota_app_start(cmd->params.ota.url); + handle_ota_upgrade(cmd); break; } @@ -1477,7 +1807,7 @@ static void app_mqtt_task_event_thread(void *arvg) app_heartbeat_time_record = hal_os_up_time_ms(); // 采集设备信息 - info.power = calc_battery_level(g_vbat_vol); // 10,20,30...100 + info.power = battery_get_level(); // 使用与设备内部一致的最终电量档位 info.voice = g_cur_volume; // 40,60,80,100 diff --git a/ql-application/threadx/interface/custom_app/ws_talkingq.c b/ql-application/threadx/interface/custom_app/ws_talkingq.c index 47abe9c..65c1d41 100644 --- a/ql-application/threadx/interface/custom_app/ws_talkingq.c +++ b/ql-application/threadx/interface/custom_app/ws_talkingq.c @@ -49,7 +49,7 @@ static int16 *ppbuff=mp3_buff; static int onmessage_status = -1; volatile int tts_playing = 0; unsigned char wss_connect_start = 0; -static int player_mq_couter = 0; +static volatile int player_mq_couter = 0; static unsigned char audio_in_task_cancel = 0; static unsigned char xiaoice_wss_connect_status = 0; @@ -105,6 +105,11 @@ static librws_app_t app = &_app; static ql_sem_t ws_connect_sem = NULL; extern int fota_start; +#define OTA_AUDIO_STOP_TIMEOUT_MS 3000 +#define OTA_AUDIO_STOP_POLL_MS 20 + +void xiaoice_clear_player_mq(void); + static int tts_play_status = 0;//1播放中 2播放完成 @@ -234,8 +239,13 @@ void add_file_to_play_player_queue(char *path, int type) //如果录音后 超过10s 还没有收到信息 提示本轮对话结束 -static void onmessage_text(rws_socket socket, const char *text, const unsigned int len) -{ +static void onmessage_text(rws_socket socket, const char *text, const unsigned int len) +{ + if (fota_start == 1) { + printf("ignore websocket audio during OTA\r\n"); + return; + } + // printf("message(txt), %d(byte): %s ", len, text); char url_buff[128]={0x00}; @@ -820,6 +830,49 @@ int app_play_tips_file(int index) return 0; } +int app_play_ota_tips_file(void) +{ + char filename_buff[64] = {0}; + uint32_t timeout; + + /* OTA owns the audio path from this point on. Stop the active conversation + * before queuing the prompt, otherwise recording/TTS rejects or masks it. */ + if (vad_audio_is_recording()) { + vad_audio_stop_record(); + } + + if (get_xiaoice_connected_status() == 2) { + talkingq_ws_disconnect(); + } + + tts_playing = 2; + xiaoice_clear_player_mq(); + + timeout = hal_os_up_time_ms(); + while (player_mq_couter > 0) { + ql_rtos_task_sleep_ms(OTA_AUDIO_STOP_POLL_MS); + if (hal_os_up_time_ms() - timeout > OTA_AUDIO_STOP_TIMEOUT_MS) { + printf("OTA wait previous audio timeout, counter:%d\r\n", + player_mq_couter); + return -3; + } + } + + tts_play_status = 0; + tts_playing = 0; + + /* Normal prompts are blocked while fota_start is set. Queue the dedicated + * OTA prompt directly after the old conversation has fully stopped. */ + snprintf(filename_buff, sizeof(filename_buff), "U:/%s", mp3_file[3]); + printf("xiaoice_play_index:3\n"); + if (ql_access(filename_buff, 0) != 0) { + return -2; + } + + add_file_to_play_player_queue(filename_buff, 0); + return player_mq_couter > 0 ? 0 : -1; +} + void app_player_thread(void *arvg) { ice_work_item *player_item=NULL; @@ -1044,12 +1097,12 @@ void xiaoice_clear_player_mq(void) free(player_item->data); player_item->data = NULL; } + if(player_mq_couter>0) player_mq_couter--; free(player_item); player_item = NULL; } } - player_mq_couter=0; } void xiaoice_close_all_audio(void)