Skip to content

Commit

Permalink
Zephyr: Initial Zephyr integration setup
Browse files Browse the repository at this point in the history
zephyr/ Initial files added
  • Loading branch information
mrdeep1 committed Oct 21, 2023
1 parent adcc8e8 commit 1b2c02d
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 10 deletions.
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_.*$'
exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$|examples/riot/examples_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 @@ -200,6 +205,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 @@ -744,6 +753,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 @@ -873,11 +888,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
5 changes: 1 addition & 4 deletions include/coap3/coap_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,7 @@ coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request);
* @return The message id if RST was sent or @c
* COAP_INVALID_MID on error.
*/
COAP_STATIC_INLINE coap_mid_t
coap_send_rst(coap_session_t *session, const coap_pdu_t *request) {
return coap_send_message_type(session, request, COAP_MESSAGE_RST);
}
coap_mid_t coap_send_rst(coap_session_t *session, const coap_pdu_t *request);

/**
* Sends a CoAP message to given peer. The memory that is
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.map
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ global:
coap_send_ack;
coap_send_error;
coap_send_message_type;
coap_send_rst;
coap_session_disconnected;
coap_session_get_ack_random_factor;
coap_session_get_ack_timeout;
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ coap_send
coap_send_ack
coap_send_error
coap_send_message_type
coap_send_rst
coap_session_disconnected
coap_session_get_ack_random_factor
coap_session_get_ack_timeout
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
5 changes: 5 additions & 0 deletions src/coap_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,11 @@ coap_option_check_critical(coap_session_t *session,
return ok;
}

coap_mid_t
coap_send_rst(coap_session_t *session, const coap_pdu_t *request) {
return coap_send_message_type(session, request, COAP_MESSAGE_RST);
}

coap_mid_t
coap_send_ack(coap_session_t *session, const coap_pdu_t *request) {
coap_pdu_t *response;
Expand Down
4 changes: 2 additions & 2 deletions src/coap_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,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 @@ -709,7 +709,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 Jon Shallow <supjps-libcoap@jpshallow.com>
#
# 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

0 comments on commit 1b2c02d

Please sign in to comment.