-
Notifications
You must be signed in to change notification settings - Fork 8k
net: coap_client: Add optional payload callback for uploads #97107
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b0ed56
net: coap_client: Handle block size negotiation on upload
rlubos 7c7df36
net: coap_client: Add optional payload callback for uploads
rlubos 1cc31c3
samples: net: sockets: Add CoAP Upload sample
rlubos 0c1c9f9
net: coap_client: Document payload callback
rlubos 7a8a65d
doc: release-notes-4.3: Add entry about CoAP client payload callback
rlubos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(coap_upload) | ||
|
||
target_sources(app PRIVATE | ||
src/main.c | ||
) | ||
|
||
include(${ZEPHYR_BASE}/samples/net/common/common.cmake) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright (c) 2025 Nordic Semiconductor ASA | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
mainmenu "CoAP Upload Sample Application" | ||
|
||
config NET_SAMPLE_COAP_SERVER_PORT | ||
int "CoAP server port" | ||
default 5683 | ||
help | ||
CoAP server port that the application should send requests to. | ||
|
||
source "Kconfig.zephyr" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
.. zephyr:code-sample:: coap-upload | ||
:name: CoAP upload | ||
:relevant-api: coap | ||
|
||
Use the CoAP client API to upload data via a PUT request | ||
|
||
Overview | ||
******** | ||
|
||
This sample demonstrates the use of the CoAP client API to upload a content to a | ||
CoAP server with PUT requests. The sample showcases various methods of uploading | ||
a content: | ||
|
||
* Short upload with payload pointer, | ||
* Short upload with payload callback, | ||
* Block transfer upload with payload pointer, | ||
* Block transfer upload with payload callback. | ||
|
||
The sample is compatible with the :zephyr:code-sample:`coap-server` sample, or | ||
can run against a standalone CoAP server, for example `aiocoap`_. | ||
|
||
Requirements | ||
************ | ||
- :ref:`networking_with_host`, :ref:`networking_with_native_sim` | ||
- or, a board with hardware networking (tested on nrf7002dk board with ``wifi-ipv4`` snippet) | ||
- Network connection between the board and a host running a CoAP server | ||
|
||
Build and Running | ||
***************** | ||
Build the CoAP upload sample application like this: | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/net/sockets/coap_upload | ||
:board: <board to use> | ||
:goals: build | ||
:compact: | ||
|
||
The easiest way to run this sample application is to build and run it as a | ||
native_sim application. Some setup is required as described in | ||
:ref:`networking_with_native_sim`. | ||
|
||
Download a CoAP server application, for example `aiocoap`_ (Python). | ||
|
||
Using ``aiocoap``: | ||
|
||
.. code-block:: bash | ||
|
||
python -m pip install "aiocoap[all]" | ||
mkdir file_root | ||
aiocoap-fileserver --write file_root | ||
|
||
Launch :command:`net-setup.sh` in net-tools: | ||
|
||
.. code-block:: bash | ||
|
||
./net-setup.sh | ||
|
||
Build and run the CoAP upload sample application for native_sim like this: | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/net/sockets/coap_upload | ||
:host-os: unix | ||
:board: native_sim | ||
:goals: run | ||
:compact: | ||
|
||
Sample output | ||
============= | ||
|
||
.. code-block:: console | ||
|
||
[00:00:00.000,000] <inf> net_config: Initializing network | ||
[00:00:00.000,000] <inf> net_config: IPv4 address: 192.0.2.1 | ||
[00:00:00.110,000] <inf> net_config: IPv6 address: 2001:db8::1 | ||
[00:00:00.110,000] <inf> net_config: IPv6 address: 2001:db8::1 | ||
[00:00:00.110,000] <inf> net_samples_common: Network connectivity established and IP address assigned | ||
[00:00:00.110,000] <inf> net_samples_common: Waiting for network... | ||
[00:00:00.110,000] <inf> coap_upload: | ||
[00:00:00.110,000] <inf> coap_upload: * Starting CoAP upload using IPv4 | ||
[00:00:00.110,000] <inf> coap_upload: | ||
[00:00:00.110,000] <inf> coap_upload: ** CoAP upload short | ||
[00:00:00.180,000] <inf> coap_upload: CoAP upload short done in 70 ms | ||
[00:00:00.180,000] <inf> coap_upload: | ||
[00:00:00.180,000] <inf> coap_upload: ** CoAP upload short with callback | ||
[00:00:00.240,000] <inf> coap_upload: CoAP upload short with callback done in 60 ms | ||
[00:00:00.240,000] <inf> coap_upload: | ||
[00:00:00.240,000] <inf> coap_upload: ** CoAP upload blockwise | ||
[00:00:00.300,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 128 bytes so far | ||
[00:00:00.360,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 256 bytes so far | ||
[00:00:00.420,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 384 bytes so far | ||
[00:00:00.480,000] <inf> coap_upload: CoAP upload blockwise done in 240 ms | ||
[00:00:00.480,000] <inf> coap_upload: | ||
[00:00:00.480,000] <inf> coap_upload: ** CoAP upload blockwise with callback | ||
[00:00:00.540,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 128 bytes so far | ||
[00:00:00.600,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 256 bytes so far | ||
[00:00:00.660,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 384 bytes so far | ||
[00:00:00.720,000] <inf> coap_upload: CoAP upload blockwise with callback done in 240 ms | ||
[00:00:01.230,000] <inf> coap_upload: | ||
[00:00:01.230,000] <inf> coap_upload: * Starting CoAP upload using IPv6 | ||
[00:00:01.230,000] <inf> coap_upload: | ||
[00:00:01.230,000] <inf> coap_upload: ** CoAP upload short | ||
[00:00:01.320,000] <inf> coap_upload: CoAP upload short done in 90 ms | ||
[00:00:01.320,000] <inf> coap_upload: | ||
[00:00:01.320,000] <inf> coap_upload: ** CoAP upload short with callback | ||
[00:00:01.380,000] <inf> coap_upload: CoAP upload short with callback done in 60 ms | ||
[00:00:01.380,000] <inf> coap_upload: | ||
[00:00:01.380,000] <inf> coap_upload: ** CoAP upload blockwise | ||
[00:00:01.440,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 128 bytes so far | ||
[00:00:01.500,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 256 bytes so far | ||
[00:00:01.560,000] <inf> coap_upload: CoAP upload blockwise ongoing, sent 384 bytes so far | ||
[00:00:01.620,000] <inf> coap_upload: CoAP upload blockwise done in 240 ms | ||
[00:00:01.620,000] <inf> coap_upload: | ||
[00:00:01.620,000] <inf> coap_upload: ** CoAP upload blockwise with callback | ||
[00:00:01.680,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 128 bytes so far | ||
[00:00:01.740,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 256 bytes so far | ||
[00:00:01.800,000] <inf> coap_upload: CoAP upload blockwise with callback ongoing, sent 384 bytes so far | ||
[00:00:01.860,000] <inf> coap_upload: CoAP upload blockwise with callback done in 240 ms | ||
|
||
.. _aiocoap: https://github.com/chrysn/aiocoap |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Networking | ||
CONFIG_NETWORKING=y | ||
CONFIG_NET_UDP=y | ||
CONFIG_NET_SOCKETS=y | ||
CONFIG_NET_CONNECTION_MANAGER=y | ||
|
||
# Logging | ||
CONFIG_LOG=y | ||
CONFIG_NET_LOG=y | ||
|
||
# Kernel options | ||
CONFIG_ENTROPY_GENERATOR=y | ||
CONFIG_TEST_RANDOM_GENERATOR=y | ||
CONFIG_MAIN_STACK_SIZE=2048 | ||
|
||
# CoAP | ||
CONFIG_COAP=y | ||
CONFIG_COAP_CLIENT=y | ||
CONFIG_COAP_CLIENT_BLOCK_SIZE=128 | ||
CONFIG_COAP_CLIENT_STACK_SIZE=2048 | ||
|
||
# Network application options and configuration | ||
CONFIG_NET_CONFIG_SETTINGS=y | ||
CONFIG_NET_CONFIG_NEED_IPV6=y | ||
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" | ||
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" | ||
CONFIG_NET_CONFIG_NEED_IPV4=y | ||
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" | ||
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
sample: | ||
description: CoAP client upload example | ||
name: socket_coap_upload | ||
common: | ||
harness: net | ||
depends_on: netif | ||
tags: | ||
- net | ||
- coap | ||
tests: | ||
sample.net.sockets.coap_upload: | ||
platform_allow: | ||
- native_sim | ||
- qemu_x86 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate to whine about this, but I dislike the fact that our code samples in the documentation do not adhere to our code style (tabs vs spaces). And it's an annoyance to do it properly, because rst file uses spaces for indentation, while our c code uses tabs.
I worked around this by using
literalinclude::
directives instead of inline code, but that isn't ideal either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but that probably would need to be brought to a broader audience, it's everywhere like this from what I see, it's not only this file.