Skip to content

Commit 1cc31c3

Browse files
committed
samples: net: sockets: Add CoAP Upload sample
Add a new sample demonstrating various ways of performing CoAP uploads with the CoAP Client library: * short uploads with payload pointer * short uploads with payload callback * block transfer uploads with payload pointer * block transfer uploads with payload callback The resource names have been chosen so that the sample can run out of the box against the Zephyr's CoAP server sample. Signed-off-by: Robert Lubos <[email protected]>
1 parent 7c7df36 commit 1cc31c3

File tree

6 files changed

+422
-0
lines changed

6 files changed

+422
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(coap_upload)
7+
8+
target_sources(app PRIVATE
9+
src/main.c
10+
)
11+
12+
include(${ZEPHYR_BASE}/samples/net/common/common.cmake)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "CoAP Upload Sample Application"
5+
6+
config NET_SAMPLE_COAP_SERVER_PORT
7+
int "CoAP server port"
8+
default 5683
9+
help
10+
CoAP server port that the application should send requests to.
11+
12+
source "Kconfig.zephyr"
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
.. zephyr:code-sample:: coap-upload
2+
:name: CoAP upload
3+
:relevant-api: coap
4+
5+
Use the CoAP client API to upload data via a PUT request
6+
7+
Overview
8+
********
9+
10+
This sample demonstrates the use of the CoAP client API to upload a content to a
11+
CoAP server with PUT requests. The sample showcases various methods of uploading
12+
a content:
13+
14+
* Short upload with payload pointer,
15+
* Short upload with payload callback,
16+
* Block transfer upload with payload pointer,
17+
* Block transfer upload with payload callback.
18+
19+
The sample is compatible with the :zephyr:code-sample:`coap-server` sample, or
20+
can run against a standalone CoAP server, for example `aiocoap`_.
21+
22+
Requirements
23+
************
24+
- :ref:`networking_with_host`, :ref:`networking_with_native_sim`
25+
- or, a board with hardware networking (tested on nrf7002dk board with ``wifi-ipv4`` snippet)
26+
- Network connection between the board and a host running a CoAP server
27+
28+
Build and Running
29+
*****************
30+
Build the CoAP upload sample application like this:
31+
32+
.. zephyr-app-commands::
33+
:zephyr-app: samples/net/sockets/coap_upload
34+
:board: <board to use>
35+
:goals: build
36+
:compact:
37+
38+
The easiest way to run this sample application is to build and run it as a
39+
native_sim application. Some setup is required as described in
40+
:ref:`networking_with_native_sim`.
41+
42+
Download a CoAP server application, for example `aiocoap`_ (Python).
43+
44+
Using ``aiocoap``:
45+
46+
.. code-block:: bash
47+
48+
python -m pip install "aiocoap[all]"
49+
mkdir file_root
50+
aiocoap-fileserver --write file_root
51+
52+
Launch :command:`net-setup.sh` in net-tools:
53+
54+
.. code-block:: bash
55+
56+
./net-setup.sh
57+
58+
Build and run the CoAP upload sample application for native_sim like this:
59+
60+
.. zephyr-app-commands::
61+
:zephyr-app: samples/net/sockets/coap_upload
62+
:host-os: unix
63+
:board: native_sim
64+
:goals: run
65+
:compact:
66+
67+
Sample output
68+
=============
69+
70+
.. code-block:: console
71+
72+
[00:00:00.000,000] <inf> net_config: Initializing network
73+
[00:00:00.000,000] <inf> net_config: IPv4 address: 192.0.2.1
74+
[00:00:00.110,000] <inf> net_config: IPv6 address: 2001:db8::1
75+
[00:00:00.110,000] <inf> net_config: IPv6 address: 2001:db8::1
76+
[00:00:00.110,000] <inf> net_samples_common: Network connectivity established and IP address assigned
77+
[00:00:00.110,000] <inf> net_samples_common: Waiting for network...
78+
[00:00:00.110,000] <inf> coap_upload:
79+
[00:00:00.110,000] <inf> coap_upload: * Starting CoAP upload using IPv4
80+
[00:00:00.110,000] <inf> coap_upload:
81+
[00:00:00.110,000] <inf> coap_upload: ** CoAP upload short
82+
[00:00:00.180,000] <inf> coap_upload: CoAP upload short done in 70 ms
83+
[00:00:00.180,000] <inf> coap_upload:
84+
[00:00:00.180,000] <inf> coap_upload: ** CoAP upload short with callback
85+
[00:00:00.240,000] <inf> coap_upload: CoAP upload short with callback done in 60 ms
86+
[00:00:00.240,000] <inf> coap_upload:
87+
[00:00:00.240,000] <inf> coap_upload: ** CoAP upload blockwise
88+
[00:00:00.300,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 128 bytes so far
89+
[00:00:00.360,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 256 bytes so far
90+
[00:00:00.420,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 384 bytes so far
91+
[00:00:00.480,000] <inf> coap_upload: CoAP upload blockwise done in 240 ms
92+
[00:00:00.480,000] <inf> coap_upload:
93+
[00:00:00.480,000] <inf> coap_upload: ** CoAP upload blockwise with callback
94+
[00:00:00.540,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 128 bytes so far
95+
[00:00:00.600,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 256 bytes so far
96+
[00:00:00.660,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 384 bytes so far
97+
[00:00:00.720,000] <inf> coap_upload: CoAP upload blockwise with callback done in 240 ms
98+
[00:00:01.230,000] <inf> coap_upload:
99+
[00:00:01.230,000] <inf> coap_upload: * Starting CoAP upload using IPv6
100+
[00:00:01.230,000] <inf> coap_upload:
101+
[00:00:01.230,000] <inf> coap_upload: ** CoAP upload short
102+
[00:00:01.320,000] <inf> coap_upload: CoAP upload short done in 90 ms
103+
[00:00:01.320,000] <inf> coap_upload:
104+
[00:00:01.320,000] <inf> coap_upload: ** CoAP upload short with callback
105+
[00:00:01.380,000] <inf> coap_upload: CoAP upload short with callback done in 60 ms
106+
[00:00:01.380,000] <inf> coap_upload:
107+
[00:00:01.380,000] <inf> coap_upload: ** CoAP upload blockwise
108+
[00:00:01.440,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 128 bytes so far
109+
[00:00:01.500,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 256 bytes so far
110+
[00:00:01.560,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 384 bytes so far
111+
[00:00:01.620,000] <inf> coap_upload: CoAP upload blockwise done in 240 ms
112+
[00:00:01.620,000] <inf> coap_upload:
113+
[00:00:01.620,000] <inf> coap_upload: ** CoAP upload blockwise with callback
114+
[00:00:01.680,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 128 bytes so far
115+
[00:00:01.740,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 256 bytes so far
116+
[00:00:01.800,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 384 bytes so far
117+
[00:00:01.860,000] <inf> coap_upload: CoAP upload blockwise with callback done in 240 ms
118+
119+
.. _aiocoap: https://github.com/chrysn/aiocoap
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Networking
2+
CONFIG_NETWORKING=y
3+
CONFIG_NET_UDP=y
4+
CONFIG_NET_SOCKETS=y
5+
CONFIG_NET_CONNECTION_MANAGER=y
6+
7+
# Logging
8+
CONFIG_LOG=y
9+
CONFIG_NET_LOG=y
10+
11+
# Kernel options
12+
CONFIG_ENTROPY_GENERATOR=y
13+
CONFIG_TEST_RANDOM_GENERATOR=y
14+
CONFIG_MAIN_STACK_SIZE=2048
15+
16+
# CoAP
17+
CONFIG_COAP=y
18+
CONFIG_COAP_CLIENT=y
19+
CONFIG_COAP_CLIENT_BLOCK_SIZE=128
20+
CONFIG_COAP_CLIENT_STACK_SIZE=2048
21+
22+
# Network application options and configuration
23+
CONFIG_NET_CONFIG_SETTINGS=y
24+
CONFIG_NET_CONFIG_NEED_IPV6=y
25+
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
26+
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
27+
CONFIG_NET_CONFIG_NEED_IPV4=y
28+
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
29+
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
description: CoAP client upload example
3+
name: socket_coap_upload
4+
common:
5+
harness: net
6+
depends_on: netif
7+
tags:
8+
- net
9+
- coap
10+
tests:
11+
sample.net.sockets.coap_upload:
12+
platform_allow:
13+
- native_sim
14+
- qemu_x86

0 commit comments

Comments
 (0)