FOTA Download resume after reboot

Hi, 

I am trying to implement FOTA download in SDK3.1.0 with an nrf9160 board. I had this working on an older SDK (1.6.1) so I know it's possible to save the progress and restart the download from an offset if there is a disconnect, or a reboot. 

Excerpt from my conf file 

# Image manager
CONFIG_IMG_MANAGER=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_STREAM_FLASH=y
CONFIG_IMG_ERASE_PROGRESSIVELY=y
CONFIG_FOTA_DOWNLOAD_PROGRESS_EVT=y
CONFIG_RESET_ON_FATAL_ERROR=y
CONFIG_REBOOT=y
CONFIG_DFU_TARGET_STREAM=y
CONFIG_DFU_TARGET_STREAM_SAVE_PROGRESS=y        //This is what you must enable for resuming download
CONFIG_STREAM_FLASH_PROGRESS=y

# Required plumbing for persistence
CONFIG_SETTINGS=y
CONFIG_SETTINGS_NVS=y
CONFIG_NVS=y

# FOTA library
CONFIG_FOTA_DOWNLOAD=y

# Download client
CONFIG_DOWNLOADER=y
CONFIG_DOWNLOADER_STACK_SIZE=4096

# DFU Target
CONFIG_DFU_TARGET=y

# Modem key management
CONFIG_MODEM_KEY_MGMT=y

# Application Upgrade support
CONFIG_BOOTLOADER_MCUBOOT=y


I also had to make sure there was a partition in the flash called "settings_storage", so that the progress can be saved. 

Now what I find, is that on a reboot, the download always starts from the beginning despite correctly saving the progress  to the flash. I have spent a bit of time stepping through fota_download.c in the SDK, and think I know what is happening. 

When the download is started, it checks the host and file strings, to make sure they are the same as the previous download.
(fota_download.c Line 497)

static void set_host_and_file(char const *const host, char const *const file)
{
	uint32_t host_hash;
	uint32_t file_hash;

	host_hash = sys_hash32(host, strlen(host));
	file_hash = sys_hash32(file, strlen(file));

	LOG_DBG("URI checksums %d,%d,%d,%d\r\n", host_hash, file_hash, dl_host_hash, dl_file_hash);

	/* Verify if the URI is same as last time, if not, prevent resuming. */
	if (dl_host_hash != host_hash || dl_file_hash != file_hash) {
		atomic_set_bit(&flags, FLAG_NEW_URI);
	} else {
		atomic_clear_bit(&flags, FLAG_NEW_URI);
	}

	dl_host_hash = host_hash;
	dl_file_hash = file_hash;

	dl_host = host;
	dl_file = file;
}


I noticed dl_file_hash, and dl_host_hash are always 0 after a reboot, so the NEW_URI flag is set, which means it will ignore any progress as it thinks it's a different file to download. After searching through the code, it looks like they are only ever set within the above function, AFTER they have been checked, so to me it looks like it is impossible to resume a download after a reboot, as it stands - Unless these variables are externed somewhere and not being correctly read back, but I don't know why the developers would put that in an unrelated file. 

It seems like this must be an oversight, after I don't know what the point in storing the progress in flash, if you don't also store the string hashes in Flash as well.

If it is not meant to be resumed after a reset, then why bother storing the progress in the flash?

If I am missing something, please let me know. 

Thanks, 

Damien

Parents Reply Children
Related