Skip to content

Commit

Permalink
tmp separate transport
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindj-nordic committed Oct 17, 2024
1 parent b6e82cf commit 3e9412e
Show file tree
Hide file tree
Showing 20 changed files with 1,345 additions and 1,031 deletions.
88 changes: 20 additions & 68 deletions include/net/download_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <zephyr/types.h>
#include <zephyr/net/coap.h>

/* Predefinition of download client struct used in download client transport. */

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -133,10 +135,6 @@ struct download_client_cfg {
* @brief Download client configuration options.
*/
struct download_client_host_cfg {
/** Server hosting the file, null-terminated.
* The host name must be kept in scope while download is going on.
*/
const char *hostname;
/** TLS security tag list.
* Pass NULL to disable TLS.
* The list must be kept in scope while download is going on.
Expand All @@ -151,17 +149,10 @@ struct download_client_host_cfg {
* Zero is the default PDN.
*/
uint8_t pdn_id;
/**
* Address family to be used for the download, AF_INET6 or AF_INET.
* Set to AF_UNSPEC (0) to fallback to AF_INET if AF_INET6 does not work.
*/
int family;
/** Maximum fragment size to download. 0 indicates that values
* configured using Kconfig shall be used.
*/
size_t range_override;
/** Set hostname for TLS Server Name Indication extension */
bool set_tls_hostname;
/** Set socket to native TLS */
bool set_native_tls;
/** Close connection when done */
Expand All @@ -172,12 +163,12 @@ struct download_client_host_cfg {
* @brief Download client state.
*/
enum download_client_state {
DOWNLOAD_CLIENT_DEINITIALIZED,
DOWNLOAD_CLIENT_IDLE,
DOWNLOAD_CLIENT_CONNECTING,
DOWNLOAD_CLIENT_CONNECTED,
DOWNLOAD_CLIENT_DOWNLOADING,
DOWNLOAD_CLIENT_DEINITIALIZING,
DOWNLOAD_CLIENT_DEINITIALIZED,
DOWNLOAD_CLIENT_IDLE,
DOWNLOAD_CLIENT_CONNECTING,
DOWNLOAD_CLIENT_CONNECTED,
DOWNLOAD_CLIENT_DOWNLOADING,
DOWNLOAD_CLIENT_DEINITIALIZING,
};

/**
Expand All @@ -190,64 +181,25 @@ struct download_client {
struct download_client_cfg config;
/** Host configuration options. */
struct download_client_host_cfg host_config;

/** Host name, null-terminated.
*/
char hostname[CONFIG_DOWNLOAD_CLIENT_MAX_HOSTNAME_SIZE];
/** File name, null-terminated.
* The file name must be kept in scope while download is going on.
*/
const char *file;
char file[CONFIG_DOWNLOAD_CLIENT_MAX_FILENAME_SIZE];
/** Size of the file being downloaded, in bytes. */
size_t file_size;
/** Download progress, number of bytes downloaded. */
size_t progress;
/** Buffer offset. */
size_t buf_offset;
/** Request new data */
bool new_data_req;

struct {
/** Socket descriptor. */
int fd;
/** Protocol for current download. */
int proto;
/** Socket type */
int type;
/** Port */
uint16_t port;
/** Destination address storage */
struct sockaddr remote_addr;
} sock;

/** Application protocols */
union {
struct {
/** The server has closed the connection. */
bool connection_close;
/** Is using ranged query. */
bool ranged;
/** Ranged progress */
size_t ranged_progress;
/** HTTP header */
struct {
/** Header length */
size_t hdr_len;
/** Status code */
unsigned long status_code;
/** Whether the HTTP header for
* the current fragment has been processed.
*/
bool has_end;
} header;
} http;

struct {
bool initialized;
/** CoAP block context. */
struct coap_block_context block_ctx;

/** CoAP pending object. */
struct coap_pending pending;
} coap;
};
/** Download client transport, http, CoAP, MQTT, ...
* Store a pointer to the selected transport per DLC instance to avoid looking it up each call.
*/
void *transport;
/** Transport parameters. */
uint8_t transport_internal[CONFIG_DOWNLOAD_CLIENT_TRANSPORT_PARAMS_SIZE];

/** Protect shared variables. */
struct k_mutex mutex;
Expand Down Expand Up @@ -309,7 +261,7 @@ int download_client_deinit(struct download_client *client);
*/
int download_client_start(struct download_client *client,
const struct download_client_host_cfg *host_config,
const char *file, size_t from);
const char *url, size_t from);

/**
* @brief Stop file download and disconnect from server.
Expand Down Expand Up @@ -376,7 +328,7 @@ int download_client_downloaded_size_get(struct download_client *client, size_t *
*/
int download_client_get(struct download_client *client,
const struct download_client_host_cfg *config,
const char *file, size_t from);
const char *url, size_t from);

#ifdef __cplusplus
}
Expand Down
78 changes: 78 additions & 0 deletions include/net/download_client_transport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef DOWNLOAD_CLIENT_TRANSPORT_H
#define DOWNLOAD_CLIENT_TRANSPORT_H

#include <net/download_client.h>

int dlc_transport_evt_connected(struct download_client *dlc);
int dlc_transport_evt_disconnected(struct download_client *dlc);
int dlc_transport_evt_data(struct download_client *dlc, void *data, size_t len);
int dlc_transport_evt_download_complete(struct download_client *dlc);

struct dlc_transport {
//TODO Consider whether the transports should not have access to the DLC struct, but get the parameters they need?
/**
* Parse protocol
*
*/
bool (*proto_supported)(struct download_client *dlc, const char *uri);
/**
* Initialize DLC transport
*
* @param ...
* @returns 0 on success, negative error on failure.
*/
int (*init)(struct download_client *dlc, struct download_client_host_cfg *host_cgf, const char *uri);
/**
* Deinitialize DLC transport
*
* @param ...
* @returns 0 on success, negative error on failure.
*/
int (*deinit)(struct download_client *dlc);
/**
* Connect DLC transport.
*
* Connection result is given by callback to @c dlc_transport_event_connected.
*
* @param ...
* @returns 0 on success, negative error on failure.
*/
int (*connect)(struct download_client *dlc);
/**
* Close DLC transport
*
* @param ...
* @returns 0 on success, negative error on failure.
*/
int (*close)(struct download_client *dlc);
/**
* Download data with DLC transport
*
* @param ...
* @returns 0 on success, negative error on failure.
*/
int (*download)(struct download_client *dlc);
};

struct dlc_transport_entry {
struct dlc_transport *transport;
};

/**
* @brief Define a DLC transport.
*
* @param entry The entry name.
* @param _transport The transport.
*/
#define DLC_TRANSPORT(entry, _transport) \
static STRUCT_SECTION_ITERABLE(dlc_transport_entry, entry) = { \
.transport = _transport, \
}

#endif /* DOWNLOAD_CLIENT_TRANSPORT_H */
3 changes: 3 additions & 0 deletions lib/nrf_modem_lib/trace_backends/flash/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ static int buffer_flush_to_flash(void)
loc_flush.fe_sector = 0;
loc_flush.fe_elem_off = 0;
err = fcb_getnext(&trace_fcb, &loc_flush);
if (err) {
LOG_ERR("fcb_getnext failed, err %d", err);
}

/* Walk sector to remove unread trace data from count. */
err = fcb_walk(&trace_fcb, loc_flush.fe_sector, fcb_walk_callback, NULL);
Expand Down
5 changes: 0 additions & 5 deletions samples/net/download/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ CONFIG_POSIX_API=y
CONFIG_NET_IPV4=y
CONFIG_HEAP_MEM_POOL_SIZE=1024
CONFIG_MBEDTLS_LEGACY_CRYPTO_C=y

CONFIG_LOG=y
CONFIG_DOWNLOAD_CLIENT_LOG_LEVEL_ERR=y

#CONFIG_LOG_MODE_IMMEDIATE=y
26 changes: 13 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 @@ -60,11 +64,9 @@ static struct download_client_cfg config = {
.buf_size = sizeof(dlc_buf),
};
static struct download_client_host_cfg host_config = {
.hostname = URL,
#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,
};
Expand Down Expand Up @@ -173,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 = 0;
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 + 5) {
return;
}
printk("| (%d/%d bytes)", downloaded, file_size);

prev_percent = percent;

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

static int callback(const struct download_client_evt *event)
Expand All @@ -205,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 @@ -332,7 +332,7 @@ int main(void)
err = download_client_get(&downloader, &host_config, URL, STARTING_OFFSET);
if (err) {
printk("Failed to start the downloader, err %d", err);
return 0;
k_sleep(K_FOREVER);
}

printk("Downloading %s\n", URL);
Expand Down
11 changes: 8 additions & 3 deletions subsys/net/lib/download_client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ zephyr_library()
zephyr_library_sources(
src/download_client.c
src/parse.c
src/http.c
src/sanity.c
src/client_socket.c
)

zephyr_library_sources_ifdef(
CONFIG_COAP
src/coap.c
CONFIG_DOWNLOAD_CLIENT_TRANSPORT_HTTP
src/transports/http.c
)

zephyr_library_sources_ifdef(
CONFIG_DOWNLOAD_CLIENT_TRANSPORT_COAP
src/transports/coap.c
)

zephyr_library_sources_ifdef(
Expand All @@ -23,3 +27,4 @@ zephyr_library_sources_ifdef(
)

zephyr_include_directories(./include)
zephyr_linker_sources(RODATA dlc_transports.ld)
Loading

0 comments on commit 3e9412e

Please sign in to comment.