Skip to content

Commit

Permalink
fw_update: separate report from observe
Browse files Browse the repository at this point in the history
Separate the reporting from the observation registration when the fw_update
thread starts running. The IDLE/READY state will now be reported at the top
of every loop.

Instead of reporting IDLE twice in a row after a successful update, report
success as the end of the UPDATING state. IDLE will then be reported at the
top of the loop and both will be shown in the OTA history for the device.

Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Dec 10, 2024
1 parent 1fac927 commit 16fbd8c
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/fw_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,22 @@ static bool received_new_target_component(const struct golioth_ota_manifest *man
return true;
}
}
else
{
GLTH_LOGI(TAG,
"Manifest does not contain target component: %s",
ctx->config.fw_package_name);
/* TODO: Report state/reason here.
* This can't be done directly because it would call a sync func from a callback
* Consider adding a new reason code: GOLIOTH_OTA_REASON_COMPONENT_NOT_FOUND
*/
}

return false;
}

static void fw_report_and_observe(void)
static void fw_register_observe(void)

Check warning on line 198 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L198

Added line #L198 was not covered by tests
{
golioth_fw_update_report_state_sync(&_component_ctx,
GOLIOTH_OTA_STATE_IDLE,
GOLIOTH_OTA_REASON_READY,
true,
true,
false);

enum golioth_status status = GOLIOTH_ERR_NULL;

Check warning on line 200 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L200

Added line #L200 was not covered by tests
uint32_t retry_delay_s = 5;

Expand Down Expand Up @@ -294,22 +298,29 @@ static void fw_update_thread(void *arg)
GLTH_LOGI(TAG, "Firmware updated successfully!");
fw_update_cancel_rollback();

GLTH_LOGI(TAG, "State = Idle");
golioth_fw_update_report_state_sync(&_component_ctx,

Check warning on line 301 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L301

Added line #L301 was not covered by tests
GOLIOTH_OTA_STATE_IDLE,
GOLIOTH_OTA_STATE_UPDATING,
GOLIOTH_OTA_REASON_FIRMWARE_UPDATED_SUCCESSFULLY,
true,
true,
false);
}
}

fw_report_and_observe();
fw_register_observe();

Check warning on line 310 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L310

Added line #L310 was not covered by tests

struct download_progress_context download_ctx;

while (1)
{
GLTH_LOGI(TAG, "State = Idle");
golioth_fw_update_report_state_sync(&_component_ctx,

Check warning on line 317 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L317

Added line #L317 was not covered by tests
GOLIOTH_OTA_STATE_IDLE,
GOLIOTH_OTA_REASON_READY,
true,
true,
true);

GLTH_LOGI(TAG, "Waiting to receive OTA manifest");
golioth_sys_sem_take(_manifest_rcvd, GOLIOTH_SYS_WAIT_FOREVER);
GLTH_LOGI(TAG, "Received OTA manifest");
Expand Down Expand Up @@ -388,11 +399,25 @@ static void fw_update_thread(void *arg)
else
{
GLTH_LOGI(TAG,
"Failed to download block idx: %" PRIu32 "; status: %s; retrying",
"Block download failed at block idx: %" PRIu32 "; status: %s; resuming",
next_block,
golioth_status_to_str(err));

golioth_fw_update_report_state_sync(&_component_ctx,

Check warning on line 406 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L406

Added line #L406 was not covered by tests
GOLIOTH_OTA_STATE_DOWNLOADING,
GOLIOTH_OTA_REASON_CONNECTION_LOST,
true,
true,
true);

golioth_sys_msleep(FW_UPDATE_RESUME_DELAY_S * 1000);

golioth_fw_update_report_state_sync(&_component_ctx,

Check warning on line 415 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L415

Added line #L415 was not covered by tests
GOLIOTH_OTA_STATE_DOWNLOADING,
GOLIOTH_OTA_REASON_READY,
true,
true,
true);
}
}

Expand All @@ -401,7 +426,7 @@ static void fw_update_thread(void *arg)
switch (err)
{
case GOLIOTH_ERR_IO:
fw_download_failed(GOLIOTH_OTA_REASON_NOT_ENOUGH_FLASH_MEMORY);
fw_download_failed(GOLIOTH_OTA_REASON_IO);

Check warning on line 429 in src/fw_update.c

View check run for this annotation

Codecov / codecov/patch

src/fw_update.c#L429

Added line #L429 was not covered by tests
break;
default:
fw_download_failed(GOLIOTH_OTA_REASON_FIRMWARE_UPDATE_FAILED);
Expand Down

0 comments on commit 16fbd8c

Please sign in to comment.