feat: report OTA download progress over MQTT
This commit is contained in:
@@ -16,8 +16,11 @@ typedef enum {
|
||||
} banban_fullfota2_result_t;
|
||||
|
||||
typedef void (*banban_fullfota2_finished_cb_t)(int result);
|
||||
/* Download percentage in 10% steps; reporting must not cancel the download. */
|
||||
typedef void (*banban_fullfota2_progress_cb_t)(int progress);
|
||||
|
||||
int banban_fullfota2_start(const char *url,
|
||||
banban_fullfota2_progress_cb_t progress_cb,
|
||||
banban_fullfota2_finished_cb_t finished_cb);
|
||||
int banban_fullfota2_is_running(void);
|
||||
void banban_fullfota2_discard_staged_package(void);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
typedef struct {
|
||||
char url[BANBAN_FULLOTA2_URL_MAX];
|
||||
banban_fullfota2_progress_cb_t progress_cb;
|
||||
banban_fullfota2_finished_cb_t finished_cb;
|
||||
} banban_fullfota2_request_t;
|
||||
|
||||
@@ -171,6 +172,9 @@ static void banban_fullfota2_task(void *argument)
|
||||
printf("[FULLOTA2] download progress=%d%% bytes=%d/%d\r\n",
|
||||
progress, written, content_length);
|
||||
while (next_progress <= progress) {
|
||||
if (g_fullfota2_request.progress_cb != NULL) {
|
||||
g_fullfota2_request.progress_cb(next_progress);
|
||||
}
|
||||
next_progress += 10;
|
||||
}
|
||||
}
|
||||
@@ -219,6 +223,7 @@ failed:
|
||||
}
|
||||
|
||||
int banban_fullfota2_start(const char *url,
|
||||
banban_fullfota2_progress_cb_t progress_cb,
|
||||
banban_fullfota2_finished_cb_t finished_cb)
|
||||
{
|
||||
int ret;
|
||||
@@ -239,6 +244,7 @@ int banban_fullfota2_start(const char *url,
|
||||
|
||||
memset(&g_fullfota2_request, 0, sizeof(g_fullfota2_request));
|
||||
memcpy(g_fullfota2_request.url, url, url_length + 1);
|
||||
g_fullfota2_request.progress_cb = progress_cb;
|
||||
g_fullfota2_request.finished_cb = finished_cb;
|
||||
g_fullfota2_running = 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user