From 7c05a1b288e12c9cc3fa32e809ccd34159dc67a6 Mon Sep 17 00:00:00 2001 From: Tomi Fontanilles Date: Wed, 15 Nov 2023 11:42:51 +0200 Subject: [PATCH] net: fota_download: fix reported download progress The progress used to be the written data offset instead of the size of the downloaded data, and would in case of modem delta update block at 23% while the download was approaching 100%. Signed-off-by: Tomi Fontanilles --- include/net/download_client.h | 14 ++++++++++++++ .../net/lib/download_client/src/download_client.c | 13 +++++++++++++ subsys/net/lib/fota_download/src/fota_download.c | 6 ++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/include/net/download_client.h b/include/net/download_client.h index e225aeedd5ef..0138788be265 100644 --- a/include/net/download_client.h +++ b/include/net/download_client.h @@ -261,6 +261,7 @@ int download_client_set_host(struct download_client *client, const char *host, */ __deprecated int download_client_connect(struct download_client *client, const char *host, const struct download_client_cfg *config); + /** * @brief Download a file. * @@ -279,6 +280,7 @@ __deprecated int download_client_connect(struct download_client *client, const c */ int download_client_start(struct download_client *client, const char *file, size_t from); + /** * @brief Retrieve the size of the file being downloaded, in bytes. * @@ -291,6 +293,18 @@ int download_client_start(struct download_client *client, const char *file, */ int download_client_file_size_get(struct download_client *client, size_t *size); +/** + * @brief Retrieve the number of bytes downloaded so far. + * + * The progress is only available after the download has begun. + * + * @param[in] client Client instance. + * @param[out] size Number of bytes downloaded so far. + * + * @retval int Zero on success, a negative error code otherwise. + */ +int download_client_downloaded_size_get(struct download_client *client, size_t *size); + /** * @brief Initiate disconnection. * diff --git a/subsys/net/lib/download_client/src/download_client.c b/subsys/net/lib/download_client/src/download_client.c index 6b899de1f8b9..8bb83ad16c3d 100644 --- a/subsys/net/lib/download_client/src/download_client.c +++ b/subsys/net/lib/download_client/src/download_client.c @@ -1026,3 +1026,16 @@ int download_client_file_size_get(struct download_client *client, size_t *size) return 0; } + +int download_client_downloaded_size_get(struct download_client *client, size_t *size) +{ + if (!client || !size) { + return -EINVAL; + } + + k_mutex_lock(&client->mutex, K_FOREVER); + *size = client->progress; + k_mutex_unlock(&client->mutex); + + return 0; +} diff --git a/subsys/net/lib/fota_download/src/fota_download.c b/subsys/net/lib/fota_download/src/fota_download.c index a9401d34bc8e..17978bd61d72 100644 --- a/subsys/net/lib/fota_download/src/fota_download.c +++ b/subsys/net/lib/fota_download/src/fota_download.c @@ -231,11 +231,9 @@ static int download_client_callback(const struct download_client_evt *event) } if (IS_ENABLED(CONFIG_FOTA_DOWNLOAD_PROGRESS_EVT)) { - err = dfu_target_offset_get(&offset); + err = download_client_downloaded_size_get(&dlc, &offset); if (err != 0) { - LOG_DBG("unable to get dfu target " - "offset err: %d", - err); + LOG_DBG("unable to get downloaded size err: %d", err); set_error_state(FOTA_DOWNLOAD_ERROR_CAUSE_INTERNAL); goto error_and_close; }