feat: report OTA download progress over MQTT
This commit is contained in:
@@ -123,6 +123,7 @@ typedef struct {
|
||||
int app_mqtt_publish(char *buf, int len, int type);
|
||||
void build_register(void);
|
||||
static void ota_report_pending_success(void);
|
||||
static char ota_progress_target_version[16] = {0};
|
||||
|
||||
enum{
|
||||
MQTT_WORK_CONNECT,
|
||||
@@ -260,7 +261,39 @@ static void ota_state_clear(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void ota_fullfota2_finished(int result)
|
||||
static void ota_fullfota2_progress(int progress)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
cJSON *data = cJSON_CreateObject();
|
||||
char *payload;
|
||||
|
||||
if (root == NULL || data == NULL) {
|
||||
cJSON_Delete(root);
|
||||
cJSON_Delete(data);
|
||||
RLOGE("allocate OTA progress response failed");
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON_AddStringToObject(root, "msg_id", "003");
|
||||
cJSON_AddStringToObject(root, "status", "updating");
|
||||
cJSON_AddStringToObject(data, "target_version", ota_progress_target_version);
|
||||
cJSON_AddNumberToObject(data, "progress", progress);
|
||||
cJSON_AddItemToObject(root, "data", data);
|
||||
payload = cJSON_PrintUnformatted(root);
|
||||
cJSON_Delete(root);
|
||||
if (payload == NULL) {
|
||||
RLOGE("build OTA progress response failed");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Progress is best effort; a publish failure must not abort FULLOTA2. */
|
||||
if (app_mqtt_publish(payload, strlen(payload), 0) != 0) {
|
||||
RLOGE("OTA progress publish failed: %d%%", progress);
|
||||
}
|
||||
free(payload);
|
||||
}
|
||||
|
||||
static void ota_fullfota2_finished(int result)
|
||||
{
|
||||
if (result == BANBAN_FULLOTA2_OK) {
|
||||
return;
|
||||
@@ -1415,7 +1448,10 @@ static void handle_ota_upgrade(command_t *cmd)
|
||||
}
|
||||
}
|
||||
|
||||
ret = banban_fullfota2_start(cmd->params.ota.url,
|
||||
snprintf(ota_progress_target_version, sizeof(ota_progress_target_version),
|
||||
"%s", cmd->params.ota.version);
|
||||
ret = banban_fullfota2_start(cmd->params.ota.url,
|
||||
ota_fullfota2_progress,
|
||||
ota_fullfota2_finished);
|
||||
if (ret != 0) {
|
||||
RLOGE("start FULLOTA2 task failed: %d", ret);
|
||||
|
||||
Reference in New Issue
Block a user