-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: net: sockets: Add CoAP service example
Created a copy from the CoAP server example but using CoAP services. Signed-off-by: Pieter De Gendt <pieter.degendt@basalte.be>
- Loading branch information
Showing
17 changed files
with
1,277 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,11 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(coap_service) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) | ||
target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/net/ip) | ||
|
||
zephyr_linker_sources(DATA_SECTIONS sections-ram.ld) |
This file contains 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,57 @@ | ||
.. zephyr:code-sample:: coap-service | ||
:name: CoAP service | ||
:relevant-api: coap udp | ||
|
||
Use the CoAP server subsystem that exposes CoAP resources. | ||
|
||
Overview | ||
******** | ||
|
||
This sample is a simple CoAP server showing how to expose a simple resource. | ||
|
||
This demo assumes that the platform of choice has networking support, | ||
some adjustments to the configuration may be needed. | ||
|
||
The sample will listen for requests in the CoAP UDP port (5683) in the | ||
site-local IPv6 multicast address reserved for CoAP nodes. | ||
|
||
The sample exports the following resources: | ||
|
||
.. code-block:: none | ||
/test | ||
/seg1/seg2/seg3 | ||
/query | ||
/separate | ||
/large | ||
/location-query | ||
/large-update | ||
These resources allow a good part of the ETSI test cases to be run | ||
against coap-server. | ||
|
||
Building And Running | ||
******************** | ||
|
||
This project has no output in case of success, the correct | ||
functionality can be verified by using some external tool such as tcpdump | ||
or wireshark. | ||
|
||
See the `net-tools`_ project for more details | ||
|
||
This sample can be built and executed on QEMU or native_posix board as | ||
described in :ref:`networking_with_host`. | ||
|
||
Use this command on the host to run the `libcoap`_ implementation of | ||
the ETSI test cases: | ||
|
||
.. code-block:: console | ||
sudo ./examples/etsi_coaptest.sh -i tap0 2001:db8::1 | ||
To build the version supporting the TI CC2520 radio, use the supplied | ||
prj_cc2520.conf configuration file enabling IEEE 802.15.4. | ||
|
||
.. _`net-tools`: https://github.com/zephyrproject-rtos/net-tools | ||
|
||
.. _`libcoap`: https://github.com/obgm/libcoap |
This file contains 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,3 @@ | ||
CONFIG_NET_L2_ETHERNET=y | ||
CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME=y | ||
CONFIG_NATIVE_UART_0_ON_STDINOUT=y |
This file contains 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,3 @@ | ||
CONFIG_NET_L2_ETHERNET=y | ||
CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME=y | ||
CONFIG_NATIVE_UART_0_ON_STDINOUT=y |
This file contains 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,4 @@ | ||
CONFIG_NET_L2_ETHERNET=y | ||
CONFIG_ETH_DRIVER=y | ||
CONFIG_ETH_STELLARIS=y | ||
CONFIG_NET_QEMU_ETHERNET=y |
This file contains 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,19 @@ | ||
# Copyright (c) 2020 Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
if [ -z "$RUNNING_FROM_MAIN_SCRIPT" ]; then | ||
echo "Do not run this script directly!" | ||
echo "Run $ZEPHYR_BASE/scripts/net/run-sample-tests.sh instead." | ||
exit 1 | ||
fi | ||
|
||
start_configuration || return $? | ||
start_zephyr | ||
start_docker "/net-tools/libcoap/examples/etsi_coaptest.sh -i eth0 192.0.2.1" \ | ||
|| return $? | ||
|
||
wait $docker_pid | ||
result=$? | ||
|
||
stop_docker | ||
stop_zephyr |
This file contains 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,43 @@ | ||
# Generic networking options | ||
CONFIG_NETWORKING=y | ||
CONFIG_NEWLIB_LIBC=y | ||
CONFIG_NET_UDP=y | ||
|
||
# Socket | ||
CONFIG_NET_SOCKETS=y | ||
CONFIG_NET_SOCKETS_POSIX_NAMES=y | ||
CONFIG_NET_SOCKETS_POLL_MAX=4 | ||
|
||
# CoAP | ||
CONFIG_COAP=y | ||
CONFIG_COAP_SERVER=y | ||
CONFIG_COAP_SERVER_WELL_KNOWN_CORE=y | ||
CONFIG_COAP_WELL_KNOWN_BLOCK_WISE=n | ||
|
||
# Kernel options | ||
CONFIG_ENTROPY_GENERATOR=y | ||
CONFIG_TEST_RANDOM_GENERATOR=y | ||
|
||
# Logging | ||
CONFIG_PRINTK=y | ||
CONFIG_NET_LOG=y | ||
|
||
# Network Shell | ||
CONFIG_NET_SHELL=y | ||
CONFIG_COAP_SERVER_SHELL=y | ||
|
||
# Configuration | ||
CONFIG_NET_CONFIG_SETTINGS=y | ||
|
||
# Enable only one protocol, if you enable both sources | ||
# won't compile. | ||
# IPv6 Support | ||
CONFIG_NET_IPV6=y | ||
CONFIG_NET_CONFIG_NEED_IPV6=y | ||
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" | ||
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5 | ||
|
||
#IPv4 Support | ||
CONFIG_NET_IPV4=n | ||
CONFIG_NET_CONFIG_NEED_IPV4=n | ||
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" |
This file contains 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 @@ | ||
sample: | ||
description: BSD Sockets API CoAP service example | ||
name: socket_coap_service | ||
tests: | ||
sample.net.sockets.coap_service: | ||
harness: net | ||
tags: | ||
- net | ||
- socket | ||
platform_allow: | ||
- native_posix | ||
- qemu_x86 |
This file contains 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,5 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 */ | ||
|
||
#include <zephyr/linker/iterable_sections.h> | ||
|
||
ITERABLE_SECTION_RAM(coap_resource_coap_server, 4) |
This file contains 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,81 @@ | ||
/* | ||
* Copyright (c) 2018 Intel Corporation | ||
* Copyright (c) 2023 Basalte bv | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/logging/log.h> | ||
LOG_MODULE_DECLARE(net_coap_service_sample); | ||
|
||
#include <zephyr/sys/printk.h> | ||
#include <zephyr/net/coap_service.h> | ||
#include <zephyr/net/coap_link_format.h> | ||
|
||
static int core_get(struct coap_resource *resource, | ||
struct coap_packet *request, | ||
struct sockaddr *addr, socklen_t addr_len) | ||
{ | ||
static const char dummy_str[] = "Just a test\n"; | ||
uint8_t data[CONFIG_COAP_SERVER_MESSAGE_SIZE]; | ||
struct coap_packet response; | ||
uint8_t token[COAP_TOKEN_MAX_LEN]; | ||
uint16_t id; | ||
uint8_t tkl; | ||
int r; | ||
|
||
id = coap_header_get_id(request); | ||
tkl = coap_header_get_token(request, token); | ||
|
||
r = coap_packet_init(&response, data, sizeof(data), | ||
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token, | ||
COAP_RESPONSE_CODE_CONTENT, id); | ||
if (r < 0) { | ||
return r; | ||
} | ||
|
||
r = coap_packet_append_payload_marker(&response); | ||
if (r < 0) { | ||
return r; | ||
} | ||
|
||
r = coap_packet_append_payload(&response, (uint8_t *)dummy_str, | ||
sizeof(dummy_str)); | ||
if (r < 0) { | ||
return r; | ||
} | ||
|
||
r = coap_resource_send(resource, &response, addr, addr_len); | ||
|
||
return r; | ||
} | ||
|
||
static const char * const core_1_path[] = { "core1", NULL }; | ||
static const char * const core_1_attributes[] = { | ||
"title=\"Core 1\"", | ||
"rt=core1", | ||
NULL, | ||
}; | ||
COAP_RESOURCE_DEFINE(core_1, coap_server, | ||
{ | ||
.get = core_get, | ||
.path = core_1_path, | ||
.user_data = &((struct coap_core_metadata) { | ||
.attributes = core_1_attributes, | ||
}), | ||
}); | ||
|
||
static const char * const core_2_path[] = { "core2", NULL }; | ||
static const char * const core_2_attributes[] = { | ||
"title=\"Core 2\"", | ||
"rt=core2", | ||
NULL, | ||
}; | ||
COAP_RESOURCE_DEFINE(core_2, coap_server, | ||
{ | ||
.get = core_get, | ||
.path = core_2_path, | ||
.user_data = &((struct coap_core_metadata) { | ||
.attributes = core_2_attributes, | ||
}), | ||
}); |
Oops, something went wrong.