Support zephyr.

Add initial support for zephyr.
Mutex is currently not supported, nor used.

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
This commit is contained in:
Achim Kraus
2022-05-25 13:34:39 +02:00
parent f38f8b4ea5
commit 16d3764191
16 changed files with 266 additions and 8 deletions

View File

@@ -41,14 +41,26 @@ check_include_file(string.h HAVE_STRING_H)
check_include_file(strings.h HAVE_STRINGS_H)
check_include_file(time.h HAVE_TIME_H)
check_include_file(sys/param.h HAVE_SYS_PARAM_H)
check_include_file(sys/random.h HAVE_SYS_RANDOM_H)
check_include_file(sys/socket.h HAVE_SYS_SOCKET_H)
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(sys/time.h HAVE_SYS_TIME_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(float.h HAVE_FLOAT_H)
check_include_file(dlfcn.h HAVE_DLFCN_H)
if(NOT ZEPHYR_BASE)
# zephyr/ncs 1.9.1 has issues including this header
check_include_file(sys/time.h HAVE_SYS_TIME_H)
endif()
if(ZEPHYR_BASE)
# zephyr/ncs 1.9.1 has issues with check_include_file
# https://github.com/zephyrproject-rtos/zephyr/issues/31193
# zephyr/ncs 1.9.1 has net/socket.h instead of sys/socket.h
set(HAVE_NET_SOCKET_H 1)
endif()
check_function_exists (memset HAVE_MEMSET)
check_function_exists (select HAVE_SELECT)
check_function_exists (socket HAVE_SOCKET)
@@ -57,7 +69,12 @@ check_function_exists (strerror HAVE_STRERROR)
check_function_exists (strnlen HAVE_STRNLEN)
check_function_exists (fls HAVE_FLS)
check_function_exists (vprintf HAVE_VPRINTF)
check_function_exists (getrandom HAVE_GETRANDOM)
check_function_exists (inet_ntop HAVE_INET_NTOP)
if(HAVE_SYS_RANDOM_H)
# zephyr/ncs 1.9.1 seems to link getrandom but doesn't offer a header
check_function_exists (getrandom HAVE_GETRANDOM)
endif()
if( ${make_tests} )
if(BUILD_SHARED_LIBS)

View File

@@ -29,8 +29,9 @@ option(BUILD_SHARED_LIBS "Link using shared libs" OFF)
option(make_tests "Make test programs and examples" OFF)
if(NOT PLATFORM)
# PLATFORM seems to be not used
set(PLATFORM "posix" CACHE STRING "Choose platform." FORCE)
set_property(CACHE PLATFORM PROPERTY STRINGS "contiki" "espidf" "posix" "riot")
set_property(CACHE PLATFORM PROPERTY STRINGS "contiki" "espidf" "posix" "riot" "zephyr")
endif()
set(PACKAGE_NAME "tinydtls")
@@ -62,7 +63,10 @@ target_sources(tinydtls PRIVATE
target_include_directories(tinydtls PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
target_compile_definitions(tinydtls PUBLIC DTLSv12 WITH_SHA256 SHA2_USE_INTTYPES_H DTLS_CHECK_CONTENTTYPE)
target_compile_options(tinydtls PRIVATE -fPIC -pedantic -std=c99 -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused)
if(NOT ZEPHYR_BASE)
target_compile_options(tinydtls PRIVATE -fPIC -pedantic -std=c99 -Wall -Wextra -Wformat-security -Winline -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wpointer-arith -Wshadow -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wunused)
endif()
set_target_properties(tinydtls PROPERTIES VERSION ${PACKAGE_VERSION} SOVERSION ${SOVERSION})
@@ -75,3 +79,7 @@ if(BUILD_SHARED_LIBS)
else()
install(TARGETS tinydtls DESTINATION ${CMAKE_INSTALL_LIBDIR} )
endif()
file(CONFIGURE OUTPUT .gitignore
NEWLINE_STYLE UNIX
CONTENT "*")

View File

@@ -37,6 +37,10 @@
#include "dtls_mutex.h"
#ifdef WITH_ZEPHYR
LOG_MODULE_DECLARE(TINYDTLS, CONFIG_TINYDTLS_LOG_LEVEL);
#endif /* WITH_ZEPHYR */
#if defined(RIOT_VERSION)
# include <memarray.h>

4
dtls.c
View File

@@ -60,6 +60,10 @@
# include "hmac.h"
#endif /* WITH_SHA256 */
#ifdef WITH_ZEPHYR
LOG_MODULE_DECLARE(TINYDTLS, CONFIG_TINYDTLS_LOG_LEVEL);
#endif /* WITH_ZEPHYR */
#define DTLS10_VERSION 0xfeff
/* Flags for dtls_destroy_peer()

View File

@@ -120,6 +120,12 @@
/* Define to 1 if you have the `vprintf' function. */
#cmakedefine HAVE_VPRINTF 1
/* Define to 1 if you have the `inet_ntop' function. */
#cmakedefine HAVE_INET_NTOP 1
/* Define to 1 if you have the <net/socket.h> header file. */
#cmakedefine HAVE_NET_SOCKET_H 1
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT ""

View File

@@ -32,6 +32,13 @@
#include <time.h>
#endif
#ifdef WITH_ZEPHYR
#ifdef HAVE_NET_SOCKET_H
#include <net/socket.h>
#endif /* HAVE_NET_SOCKET_H */
typedef int in_port_t;
#endif /* WITH_ZEPHYR */
#include "global.h"
#include "dtls_debug.h"
@@ -39,6 +46,10 @@
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifdef WITH_ZEPHYR
LOG_MODULE_REGISTER(TINYDTLS, CONFIG_TINYDTLS_LOG_LEVEL);
#endif /* WITH_ZEPHYR */
static int maxlog = DTLS_LOG_WARN; /* default maximum log level */
const char *dtls_package_name(void) {
@@ -119,7 +130,7 @@ dtls_strnlen(const char *s, size_t maxlen) {
*/
static size_t
dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
#ifdef HAVE_ARPA_INET_H
#ifdef HAVE_INET_NTOP
const void *addrptr = NULL;
in_port_t port;
char *p = buf;
@@ -171,7 +182,7 @@ dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
len -= err;
return p - buf;
#else /* HAVE_ARPA_INET_H */
#else /* HAVE_INET_NTOP */
#ifdef WITH_CONTIKI
char *p = buf;
@@ -308,7 +319,27 @@ void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr)
len = dsrv_print_addr(addr, addrbuf, sizeof(addrbuf));
if (!len)
return;
#ifdef WITH_ZEPHYR
switch(level) {
case DTLS_LOG_EMERG:
case DTLS_LOG_ALERT:
case DTLS_LOG_CRIT:
Z_LOG(LOG_LEVEL_ERR, "%s: %s\n", name, addrbuf);
break;
case DTLS_LOG_WARN:
Z_LOG(LOG_LEVEL_WRN, "%s: %s\n", name, addrbuf);
break;
case DTLS_LOG_NOTICE:
case DTLS_LOG_INFO:
Z_LOG(LOG_LEVEL_INF, "%s: %s\n", name, addrbuf);
break;
case DTLS_LOG_DEBUG:
Z_LOG(LOG_LEVEL_DBG, "%s: %s\n", name, addrbuf);
break;
}
#else /* WITH_ZEPHYR */
dsrv_log(level, "%s: %s\n", name, addrbuf);
#endif /* WITH_ZEPHYR */
}
#ifndef WITH_CONTIKI

View File

@@ -24,6 +24,10 @@
#include "global.h"
#include "session.h"
#ifdef WITH_ZEPHYR
#include <logging/log.h>
#endif /* WITH_ZEPHYR */
#ifdef WITH_CONTIKI
# ifndef DEBUG
# define DEBUG DEBUG_PRINT
@@ -94,6 +98,17 @@ void dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *b
void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr);
/* A set of convenience macros for common log levels. */
#ifdef WITH_ZEPHYR
#define dtls_emerg(...) LOG_ERR(__VA_ARGS__)
#define dtls_alert(...) LOG_ERR(__VA_ARGS__)
#define dtls_crit(...) LOG_ERR(__VA_ARGS__)
#define dtls_warn(...) LOG_WRN(__VA_ARGS__)
#define dtls_notice(...) LOG_INF(__VA_ARGS__)
#define dtls_info(...) LOG_INF(__VA_ARGS__)
#define dtls_debug(...) LOG_DBG(__VA_ARGS__)
#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, length); LOG_HEXDUMP_DBG(buf, length, name); }
#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, length); LOG_HEXDUMP_DBG(buf, length, name); }
#else /* WITH_ZEPHYR */
#define dtls_emerg(...) dsrv_log(DTLS_LOG_EMERG, __VA_ARGS__)
#define dtls_alert(...) dsrv_log(DTLS_LOG_ALERT, __VA_ARGS__)
#define dtls_crit(...) dsrv_log(DTLS_LOG_CRIT, __VA_ARGS__)
@@ -103,5 +118,6 @@ void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr);
#define dtls_debug(...) dsrv_log(DTLS_LOG_DEBUG, __VA_ARGS__)
#define dtls_debug_hexdump(name, buf, length) dtls_dsrv_hexdump_log(DTLS_LOG_DEBUG, name, buf, length, 1)
#define dtls_debug_dump(name, buf, length) dtls_dsrv_hexdump_log(DTLS_LOG_DEBUG, name, buf, length, 0)
#endif /* WITH_ZEPHYR */
#endif /* _DTLS_DEBUG_H_ */

View File

@@ -42,7 +42,18 @@ typedef int dtls_mutex_t;
#define dtls_mutex_trylock(a) *(a) = 1
#define dtls_mutex_unlock(a) *(a) = 0
#else /* ! RIOT_VERSION && ! WITH_CONTIKI */
#elif defined(WITH_ZEPHYR)
/* zephyr supports mutex, but this port doesn't use it */
typedef int dtls_mutex_t;
#define DTLS_MUTEX_INITIALIZER 0
#define dtls_mutex_lock(a) *(a) = 1
#define dtls_mutex_trylock(a) *(a) = 1
#define dtls_mutex_unlock(a) *(a) = 0
#else /* ! RIOT_VERSION && ! WITH_CONTIKI && ! WITH_ZEPHYR */
#include <pthread.h>

View File

@@ -27,6 +27,9 @@
#elif defined (RIOT_VERSION)
#include "platform-specific/dtls_prng_riot.c"
#elif defined (WITH_ZEPHYR)
#include "platform-specific/dtls_prng_zephyr.c"
#elif defined (WITH_POSIX)
#include "platform-specific/dtls_prng_posix.c"

View File

@@ -53,7 +53,19 @@ dtls_ticks(dtls_tick_t *t) {
#endif /* RIOT_VERSION */
#ifdef WITH_POSIX
#ifdef WITH_ZEPHYR
void
dtls_clock_init(void) {
}
void
dtls_ticks(dtls_tick_t *t) {
*t = k_uptime_get();
}
#elif defined(WITH_POSIX)
time_t dtls_clock_offset;
void

View File

@@ -53,6 +53,16 @@
typedef uint32_t clock_time_t;
#elif defined(WITH_ZEPHYR)
#include <zephyr.h>
#ifndef CLOCK_SECOND
# define CLOCK_SECOND 1000
#endif
typedef int64_t clock_time_t;
#else /* WITH_CONTIKI || RIOT_VERSION */
#ifdef HAVE_TIME_H

4
netq.c
View File

@@ -28,6 +28,10 @@
#endif
#endif
#ifdef WITH_ZEPHYR
LOG_MODULE_DECLARE(TINYDTLS, CONFIG_TINYDTLS_LOG_LEVEL);
#endif /* WITH_ZEPHYR */
#if !(defined (WITH_CONTIKI)) && !(defined (RIOT_VERSION))
#include <stdlib.h>

View File

@@ -0,0 +1,35 @@
/*******************************************************************************
*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* SPDX-License-Identifier: EPL-1.0
*
* Contributors:
* Achim Kraus - initial port for zephyr
*
*******************************************************************************/
#include "tinydtls.h"
#include "dtls_prng.h"
#include "random/rand32.h"
int
dtls_prng(unsigned char *buf, size_t len) {
sys_csrand_get(buf, len);
return len;
}
void
dtls_prng_init(unsigned seed) {
(void) seed;
}

View File

@@ -52,9 +52,16 @@ typedef struct {
} session_t;
#else /* ! WITH_CONTIKI && ! WITH_RIOT_SOCK */
#ifdef WITH_ZEPHYR
#include <zephyr.h>
#ifdef HAVE_NET_SOCKET_H
#include <net/socket.h>
#endif /* HAVE_NET_SOCKET_H */
#else /* WITH_ZEPHYR */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif /* WITH_ZEPHYR */
typedef struct {
socklen_t size; /**< size of addr */

43
zephyr/CMakeLists.txt Normal file
View File

@@ -0,0 +1,43 @@
###############################################################################
#
# Copyright (c) 2022 Contributors to the Eclipse Foundation
#
# See the LICENSE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 1.0
# which is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License v. 1.0
# available at http://www.eclipse.org/org/documents/edl-v10.php
#
# SPDX-License-Identifier: EPL-1.0
#
# Contributors:
# Achim Kraus - initial build file for supmodule
#
###############################################################################
cmake_minimum_required(VERSION 3.20.0)
if(CONFIG_LIBTINYDTLS)
enable_language(C)
# TEST_BIG_ENDIAN doesn't work for zephyr/ncs 1.9.1
if(${ARCH} STREQUAL "arm")
set(CMAKE_C_BYTE_ORDER LITTLE_ENDIAN)
endif()
if(CONFIG_LIBTINYDTLS_PSK)
set(DTLS_PSK On)
else()
set(DTLS_PSK Off)
endif()
if(CONFIG_LIBTINYDTLS_ECDHE_ECDSA)
set(DTLS_ECC On)
else()
set(DTLS_ECC Off)
endif()
add_subdirectory(.. build)
target_compile_definitions(tinydtls PUBLIC WITH_ZEPHYR)
target_link_libraries(tinydtls PUBLIC zephyr_interface)
set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS tinydtls)
endif()

47
zephyr/Kconfig Normal file
View File

@@ -0,0 +1,47 @@
###############################################################################
#
# Copyright (c) 2022 Contributors to the Eclipse Foundation
#
# See the LICENSE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 1.0
# which is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License v. 1.0
# available at http://www.eclipse.org/org/documents/edl-v10.php
#
# SPDX-License-Identifier: EPL-1.0
#
# Contributors:
# Achim Kraus - initial confiuration
#
###############################################################################
menu "tinyDTLS Settings"
config LIBTINYDTLS
bool "Enable libtinydtls"
help
This option enables the libtinydtls as a Zephyr module.
if LIBTINYDTLS
config LIBTINYDTLS_PSK
bool "Enable PSK"
default y
help
This option enables the PSK cipher suites.
config LIBTINYDTLS_ECDHE_ECDSA
bool "Enable ECDHE_ECDSA"
default n
help
This option enables the ECDHE_ECDSA cipher suites.
endif # LIBTINYDTLS
endmenu
if LIBTINYDTLS
module = TINYDTLS
module-str = tinyDTLS
source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config"
endif # LIBTINYDTLS