Skip to content
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

Zephyr: Initial Zephyr integration setup #1267

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repos:
hooks:
- id: astyle_py
files: '^.*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$'
exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$|examples/riot/examples_libcoap_.*$|examples/riot/tests_pkg_libcoap/.*$'
exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$|examples/riot/examples_libcoap_.*$|examples/riot/tests_pkg_libcoap/.*$|zephyr/.*$'
args: ['--style=google',
'--align-pointer=name',
'--align-reference=name',
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ set(LIBCOAP_ABI_VERSION 3.1.1)

set(COAP_LIBRARY_NAME "coap-${LIBCOAP_API_VERSION}")

if(NOT ZEPHYR_BASE)
option(
BUILD_SHARED_LIBS
"Build shared libs"
OFF)
else()
# provided by the zephyr build system
endif()

#
# global compiler options
Expand Down Expand Up @@ -84,6 +88,7 @@ set_target_properties(${COAP_LIBRARY_NAME} PROPERTIES VERSION ${LIBCOAP_ABI_VERS
# options to tweak the library
#

if(NOT ZEPHYR_BASE)
option(
ENABLE_DTLS
"enable building with DTLS support"
Expand Down Expand Up @@ -208,6 +213,10 @@ set_property(
"7"
"8")

else() # ! ZEPHYR_BASE
# provided by zephyr/CMakeLists.txt and zephyr/Kconfig
endif() # ! ZEPHYR_BASE

if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
endif()
Expand Down Expand Up @@ -766,6 +775,12 @@ target_compile_options(
${COAP_LIBRARY_NAME}
PUBLIC -DLIBCOAP_PACKAGE_BUILD="${LIBCOAP_PACKAGE_BUILD}")

if(ZEPHYR_BASE)
target_compile_options(
${COAP_LIBRARY_NAME}
PUBLIC -DMBEDTLS_CONFIG_FILE="${CONFIG_MBEDTLS_CFG_FILE}" -I${ZEPHYR_MBEDTLS_CMAKE_DIR}/configs)
endif()

add_library(
${PROJECT_NAME}::${COAP_LIBRARY_NAME}
ALIAS
Expand Down Expand Up @@ -895,11 +910,13 @@ install(
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT lib)

if(NOT ZEPHYR_BASE)
install(
EXPORT ${PROJECT_NAME}Targets
DESTINATION ${LIBCOAP_CONFIG_INSTALL_DIR}
NAMESPACE ${PROJECT_NAME}::
COMPONENT dev)
endif()

configure_package_config_file(
cmake/Config.cmake.in
Expand Down
9 changes: 9 additions & 0 deletions src/coap_mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ setup_pki_credentials(mbedtls_x509_crt *cacert,
}
switch (setup_data->pki_key.key_type) {
case COAP_PKI_KEY_PEM:
#if defined(MBEDTLS_FS_IO)
if (setup_data->pki_key.key.pem.public_cert &&
setup_data->pki_key.key.pem.public_cert[0] &&
setup_data->pki_key.key.pem.private_key &&
Expand Down Expand Up @@ -564,6 +565,10 @@ setup_pki_credentials(mbedtls_x509_crt *cacert,
}
mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
}
#else /* ! MBEDTLS_FS_IO */
(void)m_context;
coap_log_err("mbedtls_x509_crt_parse_file: MBEDTLS_FS_IO not set, so cannot handle files\n");
#endif /* ! MBEDTLS_FS_IO */
break;
case COAP_PKI_KEY_PEM_BUF:
if (setup_data->pki_key.key.pem_buf.public_cert &&
Expand Down Expand Up @@ -750,6 +755,7 @@ setup_pki_credentials(mbedtls_x509_crt *cacert,
return -1;
}

#if defined(MBEDTLS_FS_IO)
if (m_context->root_ca_file) {
ret = mbedtls_x509_crt_parse_file(cacert, m_context->root_ca_file);
if (ret < 0) {
Expand All @@ -768,6 +774,9 @@ setup_pki_credentials(mbedtls_x509_crt *cacert,
}
mbedtls_ssl_conf_ca_chain(&m_env->conf, cacert, NULL);
}
#else /* ! MBEDTLS_FS_IO */
coap_log_err("mbedtls_x509_crt_parse_file: MBEDTLS_FS_IO not set, so cannot handle files\n");
#endif /* ! MBEDTLS_FS_IO */

#if defined(MBEDTLS_SSL_SRV_C)
mbedtls_ssl_conf_cert_req_ca_list(&m_env->conf,
Expand Down
4 changes: 2 additions & 2 deletions src/coap_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ coap_delete_uri(coap_uri_t *uri) {
coap_free_type(COAP_STRING, uri);
}

COAP_STATIC_INLINE int
static int
is_unescaped_in_path(const uint8_t c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' ||
Expand All @@ -711,7 +711,7 @@ is_unescaped_in_path(const uint8_t c) {
c=='=' || c==':' || c=='@' || c == '&';
}

COAP_STATIC_INLINE int
static int
is_unescaped_in_query(const uint8_t c) {
return is_unescaped_in_path(c) || c=='/' || c=='?';
}
Expand Down
6 changes: 3 additions & 3 deletions src/oscore/oscore_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#include "coap3/coap_internal.h"
#include <string.h>

static inline void
static void
util_write_byte(uint8_t **buffer, size_t *buf_size, uint8_t value) {
assert(*buf_size >= 1);
(*buf_size)--;
Expand Down Expand Up @@ -207,7 +207,7 @@ oscore_cbor_put_unsigned(uint8_t **buffer, size_t *buf_size, uint64_t value) {
}
}

static inline uint8_t
static uint8_t
get_byte(const uint8_t **buffer, size_t *buf_len) {
#if NDEBUG
(void)buf_len;
Expand All @@ -216,7 +216,7 @@ get_byte(const uint8_t **buffer, size_t *buf_len) {
return (*buffer)[0];
}

static inline uint8_t
static uint8_t
get_byte_inc(const uint8_t **buffer, size_t *buf_len) {
assert((*buf_len) > 0);
(*buf_len)--;
Expand Down
99 changes: 99 additions & 0 deletions zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# zephyr/CMakeLists.txt for libcoap
#
# Copyright (C) 2023-2024 Jon Shallow <[email protected]>
#
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of the CoAP library libcoap. Please see README for terms
# of use.
#
###############################################################################

cmake_minimum_required(VERSION 3.20.0)

if(CONFIG_LIBCOAP)
if(CONFIG_MBEDTLS)
set(ENABLE_DTLS ON)
set(DTLS_BACKEND "zephyr" CACHE STRING "Zephyr build")
set_property(
CACHE DTLS_BACKEND
PROPERTY STRINGS
zephyr)
set(COAP_WITH_LIBMBEDTLS 1)
else()
set(ENABLE_DTLS OFF)
endif()
if (CONFIG_LIBCOAP_CLIENT_SUPPORT)
set (ENABLE_CLIENT_MODE ON)
else()
set (ENABLE_CLIENT_MODE OFF)
endif()
if (CONFIG_LIBCOAP_SERVER_SUPPORT)
set (ENABLE_SERVER_MODE ON)
else()
set (ENABLE_SERVER_MODE OFF)
endif()
if (CONFIG_LIBCOAP_OSCORE_SUPPORT)
set (ENABLE_OSCORE ON)
else()
set (ENABLE_OSCORE OFF)
endif()
if (CONFIG_LIBCOAP_OBSERVE_PERSIST)
set (WITH_OBSERVE_PERSIST ON)
else()
set (WITH_OBSERVE_PERSIST OFF)
endif()
if (CONFIG_LIBCOAP_OBSERVE_PERSIST)
set (WITH_OBSERVE_PERSIST ON)
else()
set (WITH_OBSERVE_PERSIST OFF)
endif()
if (CONFIG_LIBCOAP_TCP_SUPPORT)
set (ENABLE_TCP ON)
else()
set (ENABLE_TCP OFF)
endif()
if (CONFIG_LIBCOAP_IPV4_SUPPORT)
set (ENABLE_IPV4 ON)
else()
set (ENABLE_IPV4 OFF)
endif()
if (CONFIG_LIBCOAP_IPV6_SUPPORT)
set (ENABLE_IPV6 ON)
else()
set (ENABLE_IPV6 OFF)
endif()
if (CONFIG_LIBCOAP_WS_SOCKET)
set (ENABLE_WS ON)
else()
set (ENABLE_WS OFF)
endif()
if (CONFIG_LIBCOAP_ASYNC_SUPPORT)
set (ENABLE_ASYNC ON)
else()
set (ENABLE_ASYNC OFF)
endif()
if (CONFIG_LIBCOAP_Q_BLOCK_SUPPORT)
set (ENABLE_Q_BLOCK ON)
else()
set (ENABLE_Q_BLOCK OFF)
endif()

set (ENABLE_SMALL_STACK ON)
set (ENABLE_AF_UNIX OFF)
set (WITH_EPOLL OFF)
set (ENABLE_TESTS OFF)
set (ENABLE_EXAMPLES OFF)
set (WARNING_TO_ERROR OFF)

if(CONFIG_MBEDTLS)
target_include_directories(mbedTLS INTERFACE ${ZEPHYR_LIBCOAP_MODULE_DIR}/include)
endif()

add_subdirectory(.. build)
target_compile_definitions(coap-3 PUBLIC WITH_ZEPHYR)
target_link_libraries(coap-3 PUBLIC zephyr_interface)
set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS coap-3)

target_link_libraries(app PUBLIC coap-3)
endif()
Loading