3 changed files with 448 additions and 64 deletions
@@ -283,6 +283,7 @@ size_t url_mp3_data_fetch(const char *uri, uint8_t *buffer, size_t length)
int resp_status = -1; int resp_status = -1;
uint32_t nw = length; uint32_t nw = length;
uint32_t page_pos = 0; uint32_t page_pos = 0;
uint32_t content_length = 0;
int error_cnt = 0; int error_cnt = 0;
//printf("fd_fetch start\n"); //printf("fd_fetch start\n");
@@ -311,6 +312,7 @@ size_t url_mp3_data_fetch(const char *uri, uint8_t *buffer, size_t length)
printf("Request file size is 0!"); printf("Request file size is 0!");
goto __exit; goto __exit;
} }
content_length = session->content_length;
printf("start read\n"); printf("start read\n");
if(total_length < session->content_length) 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; page_pos = 0;
do do
{ {
if(vad_audio_is_recording()) { if(vad_audio_is_recording() || tts_playing_ret()==2) {
break; break;
} }
@@ -361,7 +363,7 @@ __exit:
session = NULL; session = NULL;
} }
if(total_length<session->content_length){ if(total_length<content_length){
total_length = 0; total_length = 0;
return -1; return -1;
} }
@@ -39,6 +39,14 @@
#define DEFAULT_MQTT_EVENT_TIME 5000 #define DEFAULT_MQTT_EVENT_TIME 5000
#define OTA_STATE_FILE "U:/ota_state.bin"
#define OTA_STATE_TMP_FILE "U:/ota_state.tmp"
#define OTA_STATE_MAGIC 0x4F544131U
#define OTA_STATE_FORMAT_VERSION 1U
#define OTA_PROMPT_PLAY_TIMEOUT_MS 10000
#define OTA_PROMPT_POLL_MS 50
#define OTA_ALLOWED_URL_PREFIX "http://101.35.224.118:8080/assets/firmware/"
static ql_timer_t mqtt_event_task_timer = NULL; static ql_timer_t mqtt_event_task_timer = NULL;
static char event_register[128] = {0}; static char event_register[128] = {0};
@@ -95,6 +103,21 @@ int app_mqtt_connect_sem(char flag);
static char payload[MQTT_PAYLOAD_SIZE] = {0}; static char payload[MQTT_PAYLOAD_SIZE] = {0};
extern int fota_start; extern int fota_start;
int app_play_ota_tips_file(void);
int xiaoice_get_play_counter(void);
typedef struct {
uint32_t magic;
uint16_t format_version;
uint16_t reserved;
char previous_version[16];
char target_version[16];
uint32_t checksum;
} ota_state_t;
int app_mqtt_publish(char *buf, int len, int type);
void build_register(void);
static void ota_report_pending_success(void);
enum{ enum{
MQTT_WORK_CONNECT, MQTT_WORK_CONNECT,
@@ -109,6 +132,177 @@ int get_mqtt_connect_status(void)
MQTT_CONN_UNLOCK(); MQTT_CONN_UNLOCK();
return status; return status;
} }
static uint32_t ota_state_checksum(const ota_state_t *state)
{
const uint8_t *data = (const uint8_t *)state;
uint32_t hash = 2166136261U;
size_t i;
for (i = 0; i < sizeof(*state) - sizeof(state->checksum); 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;
}
/* ==================== 解析函数 ==================== */ /* ==================== 解析函数 ==================== */
/** /**
@@ -163,6 +357,11 @@ int parse_command(const char *json_str, command_t *cmd)
// 解析 params // 解析 params
cJSON *params = cJSON_GetObjectItem(root, "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) { if (params != NULL) {
switch (cmd->type) { switch (cmd->type) {
case MSG_GPS_QUERY: case MSG_GPS_QUERY:
@@ -181,35 +380,27 @@ int parse_command(const char *json_str, command_t *cmd)
} }
case MSG_OTA_UPGRADE: { case MSG_OTA_UPGRADE: {
/*判断当前的固件版本与下发的是否一致,不一致,则触发升级*/
printf("MSG_OTA_UPGRADE\r\n"); printf("MSG_OTA_UPGRADE\r\n");
cJSON *url = cJSON_GetObjectItem(params, "url"); cJSON *url = cJSON_GetObjectItem(params, "url");
cJSON *version = cJSON_GetObjectItem(params, "version"); cJSON *version = cJSON_GetObjectItem(params, "version");
if (cJSON_IsString(url)) {
strncpy(cmd->params.ota.url, url->valuestring, if (!cJSON_IsString(url) || url->valuestring == NULL ||
sizeof(cmd->params.ota.url) - 1); !cJSON_IsString(version) || version->valuestring == NULL ||
} url->valuestring[0] == '\0' || version->valuestring[0] == '\0' ||
if (cJSON_IsString(version)) { strlen(url->valuestring) >= sizeof(cmd->params.ota.url) ||
strncpy(cmd->params.ota.version, version->valuestring, strlen(version->valuestring) >= sizeof(cmd->params.ota.version)) {
sizeof(cmd->params.ota.version) - 1); 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.url:%s\r\n", cmd->params.ota.url);
printf("ota.version:%s\r\n", cmd->params.ota.version); 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; break;
} }
@@ -556,6 +747,8 @@ int app_mqtt_connect()
if(ret!=0){ if(ret!=0){
build_register(); build_register();
} else {
ota_report_pending_success();
} }
RLOGD("========== app_mqtt_connect okay =========="); RLOGD("========== app_mqtt_connect okay ==========");
@@ -995,6 +1188,12 @@ char *build_ota_accepted(const char *msg_id, const char *cur_ver, const char *tg
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
cJSON *data = 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, "msg_id", msg_id);
cJSON_AddStringToObject(root, "status", "accepted"); cJSON_AddStringToObject(root, "status", "accepted");
cJSON_AddStringToObject(data, "current_version", cur_ver); cJSON_AddStringToObject(data, "current_version", cur_ver);
@@ -1006,6 +1205,66 @@ char *build_ota_accepted(const char *msg_id, const char *cur_ver, const char *tg
return json_str; return json_str;
} }
/**
* @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 构建音量调节响应 * @brief 构建音量调节响应
*/ */
@@ -1032,6 +1291,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 命令分发处理 * @brief 命令分发处理
*/ */
@@ -1106,26 +1454,7 @@ void handle_command(command_t *cmd)
} }
case MSG_OTA_UPGRADE: { case MSG_OTA_UPGRADE: {
// 立即响应accepted handle_ota_upgrade(cmd);
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);
break; break;
} }
@@ -49,7 +49,7 @@ static int16 *ppbuff=mp3_buff;
static int onmessage_status = -1; static int onmessage_status = -1;
volatile int tts_playing = 0; volatile int tts_playing = 0;
unsigned char wss_connect_start = 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 audio_in_task_cancel = 0;
static unsigned char xiaoice_wss_connect_status = 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; static ql_sem_t ws_connect_sem = NULL;
extern int fota_start; 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播放完成 static int tts_play_status = 0;//1播放中 2播放完成
@@ -236,6 +241,11 @@ void add_file_to_play_player_queue(char *path, int type)
//如果录音后 超过10s 还没有收到信息 提示本轮对话结束 //如果录音后 超过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); // printf("message(txt), %d(byte): %s ", len, text);
char url_buff[128]={0x00}; char url_buff[128]={0x00};
@@ -820,6 +830,49 @@ int app_play_tips_file(int index)
return 0; 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) void app_player_thread(void *arvg)
{ {
ice_work_item *player_item=NULL; ice_work_item *player_item=NULL;
@@ -1044,12 +1097,12 @@ void xiaoice_clear_player_mq(void)
free(player_item->data); free(player_item->data);
player_item->data = NULL; player_item->data = NULL;
} }
if(player_mq_couter>0) player_mq_couter--;
free(player_item); free(player_item);
player_item = NULL; player_item = NULL;
} }
} }
player_mq_couter=0;
} }
void xiaoice_close_all_audio(void) void xiaoice_close_all_audio(void)