Skip to content

Commit

Permalink
zephyr: Examples support
Browse files Browse the repository at this point in the history
New examples/zephyr directory that contains a sample coap-client.
Building the coap-client assumes that the zephyrproject has already been
installed and set up (default directory ~/zephyrproject).
  • Loading branch information
mrdeep1 committed Oct 10, 2024
1 parent 7b4a20f commit 4803f16
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 16 deletions.
8 changes: 8 additions & 0 deletions BUILDING
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ Contiki-NG
Executable is ./server.native. See examples/contiki/README for further
information.

Zephyr
======

cd examples/zephyr
make

See examples/zephyr/README for further information.

Windows
=======

Expand Down
24 changes: 15 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ option(
BUILD_SHARED_LIBS
"Build shared libs"
OFF)
else()
else() # ! ZEPHYR_BASE
# provided by the zephyr build system
endif()
endif() # ! ZEPHYR_BASE

#
# global compiler options
# (need to do it before add_library())
#

if(NOT ZEPHYR_BASE)
add_compile_options(
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-pedantic>
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wall>
Expand All @@ -51,6 +52,9 @@ add_compile_options(
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wswitch-enum>
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wunused>
$<$<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wwrite-strings>)
else() # ! ZEPHYR_BASE
# provided by the zephyr build system
endif() # ! ZEPHYR_BASE

if(MINGW)
add_compile_options(
Expand All @@ -76,7 +80,9 @@ endif()
#

add_library(${COAP_LIBRARY_NAME})
set_property(TARGET ${COAP_LIBRARY_NAME} PROPERTY C_STANDARD 99)
if(NOT ZEPHYR_BASE)
# set_property(TARGET ${COAP_LIBRARY_NAME} PROPERTY C_STANDARD 99)
endif() # ! ZEPHYR_BASE
if(${CMAKE_VERSION} VERSION_GREATER "3.20.0")
cmake_policy(SET CMP0115 OLD) # Supresses libcoap configuration warning
endif()
Expand Down Expand Up @@ -392,7 +398,6 @@ else()
message(STATUS "compiling without Q-Block (RFC9177) support")
endif()


if(${WITH_OBSERVE_PERSIST})
set(COAP_WITH_OBSERVE_PERSIST "1")
message(STATUS "compiling with observe persistence support")
Expand All @@ -413,17 +418,17 @@ else()
message(STATUS "compiling without epoll support")
endif()

if(ENABLE_THREAD_SAFE)
if(${ENABLE_THREAD_SAFE})
set(COAP_THREAD_SAFE "${ENABLE_THREAD_SAFE}")
message(STATUS "compiling with thread safe support")
endif()

if(ENABLE_THREAD_RECURSIVE_LOCK_CHECK)
if(${ENABLE_THREAD_RECURSIVE_LOCK_CHECK})
set(COAP_THREAD_RECURSIVE_CHECK "${ENABLE_THREAD_RECURSIVE_LOCK_CHECK}")
message(STATUS "compiling with thread recursive lock detection support")
endif()

if(ENABLE_SMALL_STACK)
if(${ENABLE_SMALL_STACK})
set(COAP_CONSTRAINED_STACK "${ENABLE_SMALL_STACK}")
message(STATUS "compiling with small stack support")
endif()
Expand Down Expand Up @@ -646,6 +651,7 @@ if(WITH_WOLFSSL)
endif()

execute_process(COMMAND git describe --tags --dirty --always
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
RESULT_VARIABLE USING_GIT
OUTPUT_VARIABLE LIBCOAP_PACKAGE_BUILD
OUTPUT_STRIP_TRAILING_WHITESPACE
Expand Down Expand Up @@ -847,7 +853,7 @@ if(ZEPHYR_BASE)
target_compile_options(
${COAP_LIBRARY_NAME}
PUBLIC -DMBEDTLS_CONFIG_FILE="${CONFIG_MBEDTLS_CFG_FILE}" -I${ZEPHYR_MBEDTLS_CMAKE_DIR}/configs)
endif()
endif() # ! ZEPHYR_BASE

add_library(
${PROJECT_NAME}::${COAP_LIBRARY_NAME}
Expand Down Expand Up @@ -984,7 +990,7 @@ install(
DESTINATION ${LIBCOAP_CONFIG_INSTALL_DIR}
NAMESPACE ${PROJECT_NAME}::
COMPONENT dev)
endif()
endif() # ! ZEPHYR_BASE

configure_package_config_file(
cmake/Config.cmake.in
Expand Down
2 changes: 2 additions & 0 deletions examples/zephyr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# never objects, and not the resulting binary
coap-client
16 changes: 16 additions & 0 deletions examples/zephyr/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ZEPHYR_PROJECT?=~/zephyrproject

all: coap-client

coap-client: client-src/src/main.c client-src/*
LIBCOAP_DIR=`pwd`/../.. ;\
(. $(ZEPHYR_PROJECT)/.venv/bin/activate ;\
cd $(ZEPHYR_PROJECT)/zephyr ;\
west build -p always -b native_sim \
$${LIBCOAP_DIR}/examples/zephyr/client-src -- \
-DCONF_FILE=prj.conf \
-DEXTRA_ZEPHYR_MODULES=$${LIBCOAP_DIR} \
-DEXTRA_CONF_FILE=$${LIBCOAP_DIR}/zephyr/libcoap-mbedtls.conf \
--fresh ;\
deactivate) ;\
cp -f $(ZEPHYR_PROJECT)/zephyr/build/zephyr/zephyr.exe coap-client
12 changes: 12 additions & 0 deletions examples/zephyr/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Example of libcoap running in Zephyr
====================================

The Zephyr project environment needs to be pre-installed. By default, the
libcoap Makefile assumes that this is located at ~/zephyrproject .

To build the coap-client example, do

$ make

The executable gets built in ~/zephyrproject/zephyr/build/zephyr/zephyr.exe
which is then copied to coap-client .
9 changes: 9 additions & 0 deletions examples/zephyr/client-src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(coap_client)

target_sources(app PRIVATE src/main.c)

include(${ZEPHYR_BASE}/samples/net/common/common.cmake)
33 changes: 33 additions & 0 deletions examples/zephyr/client-src/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Private config options for coap-client sample app

# Copyright (c) 2020 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

mainmenu "Networking coap-client sample application"

config LIBCOAP_TARGET_DOMAIN_URI
string "Target Uri"
default "coaps://libcoap.net"
help
Target uri for the example to use. Use coaps:// prefix for encrypted traffic
using Pre-Shared Key (PSK) or Public Key Infrastructure (PKI).

if MBEDTLS
config LIBCOAP_PSK_KEY
string "Preshared Key (PSK) to used in the connection to the CoAP server"
default "secretPSK"
help
The Preshared Key to use to encrypt the communicatons. The same key must be
used at both ends of the CoAP connection, and the CoaP client must request
an URI prefixed with coaps:// instead of coap:// for DTLS to be used.

config LIBCOAP_PSK_IDENTITY
string "PSK Client identity (username)"
default "user"
help
The identity (or username) to use to identify to the CoAP server which
PSK key to use.

endif # MBEDTLS

source "Kconfig.zephyr"
44 changes: 44 additions & 0 deletions examples/zephyr/client-src/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CONFIG_NETWORKING=y
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
#CONFIG_NET_DHCPV4=y

CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_INIT_STACKS=y

CONFIG_NET_LOG=y
CONFIG_LOG=y

CONFIG_NET_STATISTICS=y

CONFIG_NET_PKT_RX_COUNT=32
CONFIG_NET_PKT_TX_COUNT=32
CONFIG_NET_BUF_RX_COUNT=32
CONFIG_NET_BUF_TX_COUNT=32

CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=4
CONFIG_NET_MAX_CONTEXTS=10

CONFIG_NET_SHELL=y

CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV6=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"

CONFIG_NET_L2_ETHERNET=y

CONFIG_ETH_NATIVE_POSIX=y
CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=y
#CONFIG_ETH_NATIVE_POSIX_MAC_ADDR="00:00:5e:00:53:2a"

CONFIG_LIBCOAP=y

CONFIG_HEAP_MEM_POOL_SIZE=16384
Loading

0 comments on commit 4803f16

Please sign in to comment.