Skip to content

Commit

Permalink
samples: net: download: use new download client
Browse files Browse the repository at this point in the history
Use new download client.

Signed-off-by: Eivind Jølsgard <[email protected]>
  • Loading branch information
eivindj-nordic committed Nov 1, 2024
1 parent c81c6c2 commit 25caecf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions samples/net/download/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#

CONFIG_DOWNLOAD_CLIENT=y
CONFIG_DOWNLOAD_CLIENT_NEW_API=y
CONFIG_DOWNLOAD_CLIENT_STACK_SIZE=4096

# Networking
Expand Down
40 changes: 27 additions & 13 deletions samples/net/download/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <zephyr/net/conn_mgr_monitor.h>
#include <net/download_client.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(download, LOG_LEVEL_INF);


#if CONFIG_MODEM_KEY_MGMT
#include <modem/modem_key_mgmt.h>
#else
Expand Down Expand Up @@ -49,13 +53,22 @@ static int sec_tag_list[] = { SEC_TAG };
BUILD_ASSERT(sizeof(cert) < KB(4), "Certificate too large");
#endif

static char dlc_buf[2048];

static int callback(const struct download_client_evt *event);

static struct download_client downloader;
static struct download_client_cfg config = {
.callback = callback,
.buf = dlc_buf,
.buf_size = sizeof(dlc_buf),
};
static struct download_client_host_cfg host_config = {
#if CONFIG_SAMPLE_SECURE_SOCKET
.sec_tag_list = sec_tag_list,
.sec_tag_count = ARRAY_SIZE(sec_tag_list),
.set_tls_hostname = true,
#endif
.range_override = 0,
};

#if CONFIG_SAMPLE_COMPUTE_HASH
Expand Down Expand Up @@ -162,18 +175,16 @@ static void connectivity_event_handler(struct net_mgmt_event_callback *cb,

static void progress_print(size_t downloaded, size_t file_size)
{
static int prev_percent;
const int percent = (downloaded * 100) / file_size;
size_t lpad = (percent * PROGRESS_WIDTH) / 100;
size_t rpad = PROGRESS_WIDTH - lpad;

printk("\r[ %3d%% ] |", percent);
for (size_t i = 0; i < lpad; i++) {
printk("=");
}
for (size_t i = 0; i < rpad; i++) {
printk(" ");
if (percent >= prev_percent && percent == prev_percent) {
return;
}
printk("| (%d/%d bytes)", downloaded, file_size);

prev_percent = percent;

printk("[ %3d%% ] (%d/%d bytes)\r", percent, downloaded, file_size);
}

static int callback(const struct download_client_evt *event)
Expand All @@ -194,7 +205,7 @@ static int callback(const struct download_client_evt *event)
if (file_size) {
progress_print(downloaded, file_size);
} else {
printk("\r[ %d bytes ] ", downloaded);
printk("\r[ %d bytes ]\n", downloaded);
}

#if CONFIG_SAMPLE_COMPUTE_HASH
Expand Down Expand Up @@ -247,6 +258,9 @@ static int callback(const struct download_client_evt *event)
case DOWNLOAD_CLIENT_EVT_CLOSED:
printk("Socket closed\n");
break;
case DOWNLOAD_CLIENT_EVT_DEINITIALIZED:
printk("Client deinitialized\n");
break;
}

return 0;
Expand Down Expand Up @@ -302,7 +316,7 @@ int main(void)

printk("Network connected\n");

err = download_client_init(&downloader, callback);
err = download_client_init(&downloader, &config);
if (err) {
printk("Failed to initialize the client, err %d", err);
return 0;
Expand All @@ -315,7 +329,7 @@ int main(void)

ref_time = k_uptime_get();

err = download_client_get(&downloader, URL, &config, URL, STARTING_OFFSET);
err = download_client_get(&downloader, &host_config, URL, STARTING_OFFSET);
if (err) {
printk("Failed to start the downloader, err %d", err);
return 0;
Expand Down

0 comments on commit 25caecf

Please sign in to comment.