Skip to content

Commit 4878240

Browse files
SeppoTakalorlubos
authored andcommitted
net: lwm2m_client_utils: Handle erase properly
Handle erasing when a new FOTA process starts, but only if offset is showing that we need it. Signed-off-by: Seppo Takalo <[email protected]>
1 parent fe1105b commit 4878240

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

subsys/net/lib/lwm2m_client_utils/lwm2m/lwm2m_firmware.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ static int firmware_update_state(uint16_t obj_inst_id, uint16_t res_id, uint16_t
640640
if (*data == STATE_IDLE) {
641641
/* Cancel Only object is same than ongoing update */
642642
if (obj_inst_id == ongoing_obj_id) {
643+
client_acknowledge(); /* Erasing might take some time, so acknoledge now.*/
643644
ongoing_obj_id = UNUSED_OBJ_ID;
644645
fota_download_util_download_cancel();
645646
ret = firmware_target_reset(obj_inst_id);
@@ -713,6 +714,11 @@ static int firmware_block_received_cb(uint16_t obj_inst_id, uint16_t res_id, uin
713714
return -EINVAL;
714715
}
715716

717+
if (offset < bytes_downloaded) {
718+
LOG_DBG("Skipping already downloaded bytes");
719+
return 0;
720+
}
721+
716722
if (bytes_downloaded == 0 && offset == 0) {
717723
if (ongoing_obj_id != UNUSED_OBJ_ID) {
718724
LOG_INF("DFU is allocated already");
@@ -740,28 +746,40 @@ static int firmware_block_received_cb(uint16_t obj_inst_id, uint16_t res_id, uin
740746

741747
/* Store Started DFU type */
742748
target_image_type_store(obj_inst_id, image_type);
743-
ret = dfu_target_init(image_type, 0, total_size, dfu_target_cb);
744-
if (ret < 0) {
745-
LOG_ERR("Failed to init DFU target, err: %d", ret);
746-
goto cleanup;
747-
}
749+
do {
750+
ret = dfu_target_init(image_type, 0, total_size, dfu_target_cb);
751+
if (ret < 0) {
752+
LOG_ERR("Failed to init DFU target, err: %d", ret);
753+
goto cleanup;
754+
}
755+
/* Check if we need to erase */
756+
ret = dfu_target_offset_get(&target_offset);
757+
if (ret < 0) {
758+
LOG_ERR("Failed to obtain current offset, err: %d", ret);
759+
goto cleanup;
760+
}
761+
762+
if (target_offset != 0) {
763+
LOG_INF("Erasing target");
764+
ret = dfu_target_reset();
765+
if (ret < 0) {
766+
LOG_ERR("Failed to erase target, err: %d", ret);
767+
goto cleanup;
768+
}
769+
}
770+
} while (target_offset != 0);
748771

749772
LOG_INF("%s firmware download started.",
750773
image_type == DFU_TARGET_IMAGE_TYPE_MODEM_DELTA ||
751774
image_type == DFU_TARGET_IMAGE_TYPE_FULL_MODEM ?
752775
"Modem" :
753776
"Application");
754-
}
755-
756-
if (offset < bytes_downloaded) {
757-
LOG_DBG("Skipping already downloaded bytes");
758-
return 0;
759-
}
760-
761-
ret = dfu_target_offset_get(&target_offset);
762-
if (ret < 0) {
763-
LOG_ERR("Failed to obtain current offset, err: %d", ret);
764-
goto cleanup;
777+
} else {
778+
ret = dfu_target_offset_get(&target_offset);
779+
if (ret < 0) {
780+
LOG_ERR("Failed to obtain current offset, err: %d", ret);
781+
goto cleanup;
782+
}
765783
}
766784

767785
/* Display a % downloaded or byte progress, if no total size was

tests/subsys/net/lib/lwm2m_fota_utils/src/firmware.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ int fota_download_util_image_schedule_stub(enum dfu_target_image_type dfu_target
250250
static int fota_download_util_image_reset_stub(enum dfu_target_image_type dfu_target_type)
251251
{
252252
target_reset_done = true;
253+
target_offset = 0;
253254
return 0;
254255
}
255256

0 commit comments

Comments
 (0)