Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nrfcloud coap: use downloader library #19908

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,18 @@ Libraries for networking

* Fixed the warning due to missing ``https`` download protocol.

* :ref:`lib_downloader` library:

* Updated to support Proxy-URI option and an authentication callback after connecting.

* :ref:`lib_fota_download` library:

* Updated to use the :ref:`lib_downloader` library for CoAP downloads.

* :ref:`lib_nrf_cloud` library:

* Updated to use the :ref:`lib_downloader` library for CoAP downloads.

Libraries for NFC
-----------------

Expand Down
10 changes: 10 additions & 0 deletions include/net/downloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ struct downloader_host_cfg {
* Set to @c AF_UNSPEC (0) to fallback to @c AF_INET if @c AF_INET6 does not work.
*/
int family;
/**
* Callback to do client authentication.
* This is called after connecting.
*/
int (*auth_cb)(int sock);
maxd-nordic marked this conversation as resolved.
Show resolved Hide resolved
/**
* CoAP Proxy-URI option.
* This string is used in case you are requesting a proxied file from a CoAP server.
*/
const char *proxy_uri;
};

/**
Expand Down
58 changes: 26 additions & 32 deletions include/net/fota_download.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ enum fota_download_evt_id {

/** FOTA download abandoned due to a cancellation request. */
FOTA_DOWNLOAD_EVT_CANCELLED,

/** Resume the download at offset.
* Only generated if @kconfig{CONFIG_FOTA_DOWNLOAD_EXTERNAL_DL} is enabled.
*/
FOTA_DOWNLOAD_EVT_RESUME_OFFSET,
};

/**
Expand Down Expand Up @@ -101,8 +96,6 @@ struct fota_download_evt {
enum fota_download_error_cause cause;
/** Download progress %. */
int progress;
/** Resume at offset @ref FOTA_DOWNLOAD_EVT_RESUME_OFFSET */
size_t resume_offset;
};
};

Expand Down Expand Up @@ -152,6 +145,32 @@ int fota_download(const char *host, const char *file, const int *sec_tag_list,
uint8_t sec_tag_count, uint8_t pdn_id, size_t fragment_size,
const enum dfu_target_image_type expected_type);

/**@brief Download the given file with the specified image type from the given host.
*
* Identical to fota_download_start_with_image_type(),
* but with additional host configuration options.
*
* @param host Name of host to start downloading from. Can include scheme
* and port number, for example https://google.com:443
* @param file Path to the file you wish to download. See fota_download_any()
* for details on expected format.
* @param sec_tag Security tag you want to use with COAPS. Pass -1 to disable DTLS.
* @param pdn_id Packet Data Network ID to use for the download, or 0 to use the default.
* @param fragment_size Fragment size to be used for the download. If 0, no fragmentation is used.
* @param expected_type Type of firmware file to be downloaded and installed.
* @param host_cfg Additional host configuration options.
*
* @retval 0 If download has started successfully.
* @retval -EALREADY If download is already ongoing.
* @retval -E2BIG If sec_tag_count is larger than
* @kconfig{CONFIG_FOTA_DOWNLOAD_SEC_TAG_LIST_SIZE_MAX}
maxd-nordic marked this conversation as resolved.
Show resolved Hide resolved
* Otherwise, a negative value is returned.
*/
int fota_download_with_host_cfg(const char *host, const char *file,
int sec_tag, uint8_t pdn_id, size_t fragment_size,
const enum dfu_target_image_type expected_type,
const struct downloader_host_cfg *host_cfg);


/**@brief Start downloading the given file of any image type from the given host.
*
Expand Down Expand Up @@ -281,31 +300,6 @@ int fota_download_s0_active_get(bool *const s0_active);
*/
int fota_download_b1_file_parse(char *s0_s1_files);

/**@brief Start a FOTA download using an external download client.
* Requires @kconfig{CONFIG_FOTA_DOWNLOAD_EXTERNAL_DL} to be enabled.
*
* @param host Name of host.
* @param file File path of the firmware image.
* @param expected_type Type of firmware image to be installed.
* @param image_size Size of the firmware image.
*
* @retval 0 If successful.
* Otherwise, a (negative) error code is returned.
*/
int fota_download_external_start(const char *host, const char *file,
const enum dfu_target_image_type expected_type,
const size_t image_size);

/**@brief Handle a download event from an external download client.
* Requires @kconfig{CONFIG_FOTA_DOWNLOAD_EXTERNAL_DL} to be enabled.
*
* @param evt Download event.
*
* @retval 0 If successful.
* Otherwise, a (negative) error code is returned.
*/
int fota_download_external_evt_handle(struct downloader_evt const *const evt);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/lib/downloader/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ config DOWNLOADER_SHELL

config DOWNLOADER_TRANSPORT_PARAMS_SIZE
int "Maximum transport parameter size"
default 128
default 256

config DOWNLOADER_TRANSPORT_HTTP
bool "HTTP transport"
Expand Down
40 changes: 31 additions & 9 deletions subsys/net/lib/downloader/src/transports/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ LOG_MODULE_DECLARE(downloader, CONFIG_DOWNLOADER_LOG_LEVEL);
#define FILENAME_SIZE CONFIG_DOWNLOADER_MAX_FILENAME_SIZE
#define COAP_PATH_ELEM_DELIM "/"

#define COAP "coap://"
#define COAPS "coaps://"

struct transport_params_coap {
/** Flag whether config is set */
bool cfg_set;
Expand Down Expand Up @@ -54,6 +57,10 @@ struct transport_params_coap {
bool new_data_req;
/** Request retransmission */
bool retransmission_req;
/* Proxy-URI option value */
const char *proxy_uri;
/* Client auth callback */
int (*auth_cb)(int sock);
};

BUILD_ASSERT(CONFIG_DOWNLOADER_TRANSPORT_PARAMS_SIZE >= sizeof(struct transport_params_coap));
Expand Down Expand Up @@ -280,11 +287,8 @@ static int coap_request_send(struct downloader *dl)
return err;
}

err = dl_parse_url_file(dl->file, file, sizeof(file));
if (err) {
LOG_ERR("Unable to parse url");
return err;
}
LOG_DBG("dl->file: %s", dl->file);
strncpy(file, dl->file, sizeof(file) - 1);

path_elem = strtok_r(file, COAP_PATH_ELEM_DELIM, &path_elem_saveptr);
do {
Expand All @@ -308,6 +312,15 @@ static int coap_request_send(struct downloader *dl)
return err;
}

if (coap->proxy_uri != NULL) {
err = coap_packet_append_option(&request, COAP_OPTION_PROXY_URI,
coap->proxy_uri, strlen(coap->proxy_uri));
if (err) {
LOG_ERR("Unable to add Proxy-URI option");
return err;
}
}

if (!has_pending(dl)) {
struct coap_transmission_parameters params = coap_get_transmission_parameters();

Expand Down Expand Up @@ -342,9 +355,9 @@ static int coap_request_send(struct downloader *dl)

static bool dl_coap_proto_supported(struct downloader *dl, const char *url)
{
if (strncmp(url, "coaps://", 8) == 0) {
if (strncmp(url, COAPS, (sizeof(COAPS) - 1)) == 0) {
return true;
} else if (strncmp(url, "coap://", 7) == 0) {
} else if (strncmp(url, COAP, (sizeof(COAP) - 1)) == 0) {
return true;
}

Expand All @@ -369,14 +382,14 @@ static int dl_coap_init(struct downloader *dl, struct downloader_host_cfg *dl_ho
coap->cfg = tmp_cfg;
coap->cfg_set = cfg_set;
} else {
coap->cfg.block_size = 5;
coap->cfg.block_size = COAP_BLOCK_1024;
coap->cfg.max_retransmission = 4;
}

coap->sock.proto = IPPROTO_UDP;
coap->sock.type = SOCK_DGRAM;

if (strncmp(url, "coaps://", 8) == 0 ||
if (strncmp(url, COAPS, (sizeof(COAPS) - 1)) == 0 ||
(dl_host_cfg->sec_tag_count != 0 && dl_host_cfg->sec_tag_list != NULL)) {
coap->sock.proto = IPPROTO_DTLS_1_2;
coap->sock.type = SOCK_DGRAM;
Expand Down Expand Up @@ -405,6 +418,10 @@ static int dl_coap_init(struct downloader *dl, struct downloader_host_cfg *dl_ho
coap->sock.type |= SOCK_NATIVE_TLS;
}

/* Copy proxy-uri and auth-cb to internal struct */
coap->proxy_uri = dl_host_cfg->proxy_uri;
coap->auth_cb = dl_host_cfg->auth_cb;

return 0;
}

Expand Down Expand Up @@ -448,6 +465,11 @@ static int dl_coap_connect(struct downloader *dl)

coap->new_data_req = true;

/* Run auth callback if set */
if (coap->auth_cb != NULL) {
return coap->auth_cb(coap->sock.fd);
}

return err;
}

Expand Down
8 changes: 4 additions & 4 deletions subsys/net/lib/downloader/src/transports/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,11 @@ static int http_parse(struct downloader *dl, size_t len)

static bool dl_http_proto_supported(struct downloader *dl, const char *url)
{
if (strncmp(url, HTTPS, strlen(HTTPS)) == 0) {
if (strncmp(url, HTTPS, (sizeof(HTTPS) - 1)) == 0) {
return true;
}

if (strncmp(url, HTTP, strlen(HTTP)) == 0) {
if (strncmp(url, HTTP, (sizeof(HTTP) - 1)) == 0) {
return true;
}

Expand All @@ -458,8 +458,8 @@ static int dl_http_init(struct downloader *dl, struct downloader_host_cfg *dl_ho
http->sock.proto = IPPROTO_TCP;
http->sock.type = SOCK_STREAM;

if (strncmp(url, HTTPS, strlen(HTTPS)) == 0 ||
(strncmp(url, HTTP, strlen(HTTP)) != 0 &&
if (strncmp(url, HTTPS, (sizeof(HTTPS) - 1)) == 0 ||
(strncmp(url, HTTP, (sizeof(HTTP) - 1)) != 0 &&
(dl_host_cfg->sec_tag_count != 0 && dl_host_cfg->sec_tag_list != NULL))) {
http->sock.proto = IPPROTO_TLS_1_2;
http->sock.type = SOCK_STREAM;
Expand Down
4 changes: 0 additions & 4 deletions subsys/net/lib/fota_download/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ config FOTA_DOWNLOAD_SEC_TAG_LIST_SIZE_MAX
help
Maximum size of the list of security tags used to store TLS credentials.

config FOTA_DOWNLOAD_EXTERNAL_DL
bool "Use external download events to perform FOTA updates"
select EXPERIMENTAL

module=FOTA_DOWNLOAD
module-dep=LOG
module-str=Firmware Over the Air Download
Expand Down
Loading