nRF9160: Download client

The Download client sample demonstrates how to download a file from an HTTP or a CoAP server, with optional TLS or DTLS. It uses the Download client library.

Requirements

The sample supports the following development kit:

Hardware platforms

PCA

Board name

Build target

nRF9160 DK

PCA10090

nrf9160dk_nrf9160

nrf9160dk_nrf9160ns

The sample is configured to compile and run as a non-secure application on nRF91’s Cortex-M33. Therefore, it automatically includes the Secure Partition Manager that prepares the required peripherals to be available for the application.

Overview

The sample first initializes the BSD library and AT communications. Next, it provisions a certificate to the modem using the Modem key management library if the CONFIG_SAMPLE_SECURE_SOCKET option is set. The provisioning of the certificates must be done before connecting to the LTE network since the certificates can only be provisioned when the device is not connected. The certificate file name and security tag can be configured via the CONFIG_SAMPLE_SEC_TAG and the CONFIG_SAMPLE_CERT_FILE options, respectively.

The sample then performs the following actions:

  1. Establishes a connection to the LTE network

  2. Optionally sets up the secure socket options

  3. Uses the Download client library to download a file from an HTTP server.

Downloading from a CoAP server

To enable CoAP block-wise transfer, it is necessary to enable Zephyr’s CoAP stack via the CONFIG_COAP option.

Using TLS and DTLS

When the CONFIG_SAMPLE_SECURE_SOCKET option is set, the sample provisions the certificate found in the samples/nrf9160/download/cert folder. The certificate file name is indicated by the CONFIG_SAMPLE_CERT_FILE option. This certificate will work for the default test files. If you are using a custom download test file, you have to provision the correct certificate for the servers from which the certificates will be downloaded.

See Certificates for more information.

Configuration

See Configuring your application for information about how to permanently or temporarily change the configuration.

Configuration options

Check and configure the following configuration options for the sample:

CONFIG_SAMPLE_SECURE_SOCKET - Secure socket configuration

If enabled, this option provisions the certificate to the modem.

CONFIG_SAMPLE_SEC_TAG - Security tag configuration

This option configures the security tag.

CONFIG_SAMPLE_CERT_FILE - Certificate file name configuration

This option sets the certificate file name.

CONFIG_SAMPLE_COMPUTE_HASH - Hash compute configuration

If enabled, this option computes the SHA256 hash of the downloaded file.

CONFIG_SAMPLE_COMPARE_HASH - Hash compare configuration

If enabled, this option compares the hash against the SHA256 hash set by CONFIG_SAMPLE_SHA256_HASH for a match.

CONFIG_SAMPLE_SHA256_HASH - Hash configuration

This option sets the SHA256 hash to be compared with CONFIG_SAMPLE_COMPUTE_HASH.

Building and running

This sample can be found under samples/nrf9160/download in the nRF Connect SDK folder structure.

The sample is built as a non-secure firmware image for the nrf9160dk_nrf9160ns build target. Because of this, it automatically includes the Secure Partition Manager.

See Building and programming a sample application for information about how to build and program the application.

Testing

After programming the sample to your board, test it by performing the following steps:

  1. Connect the development kit to your PC using a USB cable and power on or reset the kit.

  2. Open a terminal emulator and observe that the sample starts, provisions certificates, and starts to download.

  3. Observe that the progress bar fills up as the download progresses.

  4. Observe that the sample displays the message “Download completed” on the terminal when the download completes.

Sample Output

The following output is logged on the terminal when the sample downloads a file from an HTTPS server:

Download client sample started
Provisioning certificate
Waiting for network.. OK
Downloading https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_100kB.jpg
[ 100% ] |==================================================| (102117/102117 bytes)
Download completed in 13302 ms @ 7676 bytes per sec, total 102117 bytes
Bye

Dependencies

This sample uses the following libraries:

From nRF Connect SDK
From nrfxlib

In addition, it uses the following samples:

From nRF Connect SDK