@@ -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
0 commit comments