From beab377c28afde22b1021a5fbdc4bef4077c5c27 Mon Sep 17 00:00:00 2001 From: Silje Susort Date: Tue, 30 May 2023 13:45:06 +0200 Subject: [PATCH 1/2] Add libraries and macros for federated on Zephyr platform --- core/federated/clock-sync.c | 7 +++-- core/federated/federate.c | 3 +++ core/federated/net_util.c | 10 +++++++ core/platform/lf_zephyr_support.c | 33 ++++++++++++++++++++++- include/core/federated/net_util.h | 5 ++++ include/core/platform/lf_zephyr_support.h | 2 ++ 6 files changed, 57 insertions(+), 3 deletions(-) diff --git a/core/federated/clock-sync.c b/core/federated/clock-sync.c index fdbf0642b..d21c06859 100644 --- a/core/federated/clock-sync.c +++ b/core/federated/clock-sync.c @@ -31,12 +31,15 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef FEDERATED +#ifdef PLATFORM_ZEPHYR +#else +#include +#include +#endif #include #include #include #include -#include -#include #include "platform.h" #include "clock-sync.h" diff --git a/core/federated/federate.c b/core/federated/federate.c index 7f6ecd400..aa820eab1 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -33,6 +33,8 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef FEDERATED #ifdef PLATFORM_ARDUINO #error To be implemented. No support for federation on Arduino yet. +#elif PLATFORM_ZEPHYR +#warning Federated support on Zephyr is still experimental. #else #include // inet_ntop & inet_pton #include // Defines getaddrinfo(), freeaddrinfo() and struct addrinfo. @@ -2414,6 +2416,7 @@ void terminate_execution() { // possibility of deadlock. To ensure this, this // function should NEVER be called while holding any mutex lock. lf_mutex_lock(&outbound_socket_mutex); + // FIXME: Should this be _fed.number_of_outbound_p2p_connections instead? for (int i=0; i < NUMBER_OF_FEDERATES; i++) { // Close outbound connections, in case they have not closed themselves. // This will result in EOF being sent to the remote federate, I think. diff --git a/core/federated/net_util.c b/core/federated/net_util.c index 91cdc5427..8a52c2346 100644 --- a/core/federated/net_util.c +++ b/core/federated/net_util.c @@ -576,6 +576,7 @@ void encode_tag( * @return true if there is a match, false otherwise. */ bool match_regex(const char* str, char* regex) { + #ifndef PLATFORM_ZEPHYR regex_t regex_compiled; regmatch_t group; bool valid = false; @@ -591,6 +592,9 @@ bool match_regex(const char* str, char* regex) { } regfree(®ex_compiled); return valid; + #else + return true; + #endif } @@ -637,6 +641,8 @@ bool validate_user(const char* user) { return match_regex(user, username_regex); } +#ifndef PLATFORM_ZEPHYR + /** * Extract one match group from the rti_addr regex . * @return true if SUCCESS, else false. @@ -671,10 +677,13 @@ bool extract_match_groups(const char* rti_addr, char** rti_addr_strs, bool** rti return true; } +#endif + /** * Extract the host, port and user from rti_addr. */ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) { + #ifndef PLATFORM_ZEPHYR const char* regex_str = "(([a-zA-Z0-9_-]{1,254})@)?([a-zA-Z0-9.]{1,255})(:([0-9]{1,5}))?"; size_t max_groups = 6; // The group indices of each field of interest in the regex. @@ -710,5 +719,6 @@ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) } } regfree(®ex_compiled); + #endif } #endif diff --git a/core/platform/lf_zephyr_support.c b/core/platform/lf_zephyr_support.c index 9dbf40c04..66f7d2a2e 100644 --- a/core/platform/lf_zephyr_support.c +++ b/core/platform/lf_zephyr_support.c @@ -348,7 +348,7 @@ int lf_notify_of_event() { #warning "Threaded support on Zephyr is still experimental." // FIXME: What is an appropriate stack size? -#define _LF_STACK_SIZE 1024 +#define _LF_STACK_SIZE 4096 // FIXME: What is an appropriate thread prio? #define _LF_THREAD_PRIORITY 5 @@ -363,7 +363,38 @@ int lf_notify_of_event() { #define USER_THREADS 0 #endif +#if defined(FEDERATED) && defined(FEDERATED_DECENTRALIZED) +#define RTI_SOCKET_LISTENER_THREAD 1 +#define FEDERATE_SOCKET_LISTENER_THREADS NUMBER_OF_FEDERATES +#define P2P_HANDLER_THREAD 1 + +#elif defined(FEDERATED) && defined(FEDERATED_CENTRALIZED) +#define RTI_SOCKET_LISTENER_THREAD 1 +#define FEDERATE_SOCKET_LISTENER_THREADS 0 +#define P2P_HANDLER_THREAD 0 + +#else +#define RTI_SOCKET_LISTENER_THREAD 0 +#define FEDERATE_SOCKET_LISTENER_THREADS 0 +#define P2P_HANDLER_THREAD 0 +#endif + +#if defined(FEDERATED) && defined(_LF_CLOCK_SYNC_ON) +#define CLOCK_SYNC_THREAD 1 +#else +#define CLOCK_SYNC_THREAD 0 +#endif + +#ifndef WORKERS_NEEDED_FOR_FEDERATE +#define WORKERS_NEEDED_FOR_FEDERATE 0 +#endif + #define NUMBER_OF_THREADS (NUMBER_OF_WORKERS \ + + WORKERS_NEEDED_FOR_FEDERATE \ + + RTI_SOCKET_LISTENER_THREAD \ + + FEDERATE_SOCKET_LISTENER_THREADS \ + + P2P_HANDLER_THREAD \ + + CLOCK_SYNC_THREAD \ + USER_THREADS) K_MUTEX_DEFINE(thread_mutex); diff --git a/include/core/federated/net_util.h b/include/core/federated/net_util.h index 666eac653..217440a24 100644 --- a/include/core/federated/net_util.h +++ b/include/core/federated/net_util.h @@ -40,6 +40,8 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef PLATFORM_ARDUINO #error To be implemented. No support for federation on Arduino yet. +#elif PLATFORM_ZEPHYR +#warning Federated support on Zephyr is still experimental. #else #include #include @@ -345,6 +347,7 @@ bool validate_host(const char* host); */ bool validate_user(const char* user); +#ifndef PLATFORM_ZEPHYR /** * Extract one match group from the rti_addr regex . @@ -359,6 +362,8 @@ bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, */ bool extract_match_groups(const char* rti_addr, char** rti_addr_strs, bool** rti_addr_flags, regmatch_t* group_array, int* gids, int* max_lens, int* min_lens, const char** err_msgs); +#endif + /** * Extract the host, port and user from rti_addr. */ diff --git a/include/core/platform/lf_zephyr_support.h b/include/core/platform/lf_zephyr_support.h index 0e1e0b6ea..c87355ca6 100644 --- a/include/core/platform/lf_zephyr_support.h +++ b/include/core/platform/lf_zephyr_support.h @@ -39,6 +39,8 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include //malloc, calloc, free, realloc #include +#include +#include #define NO_TTY #define _LF_TIMEOUT 1 From c5875af59d61e1fbe81c918b24ed2cccc02519fa Mon Sep 17 00:00:00 2001 From: Silje Susort Date: Sat, 3 Jun 2023 20:59:37 +0200 Subject: [PATCH 2/2] Remove macros for regex functions --- core/federated/net_util.c | 10 ---------- core/platform/lf_zephyr_support.c | 2 +- include/core/federated/net_util.h | 5 ++--- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/core/federated/net_util.c b/core/federated/net_util.c index 8a52c2346..91cdc5427 100644 --- a/core/federated/net_util.c +++ b/core/federated/net_util.c @@ -576,7 +576,6 @@ void encode_tag( * @return true if there is a match, false otherwise. */ bool match_regex(const char* str, char* regex) { - #ifndef PLATFORM_ZEPHYR regex_t regex_compiled; regmatch_t group; bool valid = false; @@ -592,9 +591,6 @@ bool match_regex(const char* str, char* regex) { } regfree(®ex_compiled); return valid; - #else - return true; - #endif } @@ -641,8 +637,6 @@ bool validate_user(const char* user) { return match_regex(user, username_regex); } -#ifndef PLATFORM_ZEPHYR - /** * Extract one match group from the rti_addr regex . * @return true if SUCCESS, else false. @@ -677,13 +671,10 @@ bool extract_match_groups(const char* rti_addr, char** rti_addr_strs, bool** rti return true; } -#endif - /** * Extract the host, port and user from rti_addr. */ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) { - #ifndef PLATFORM_ZEPHYR const char* regex_str = "(([a-zA-Z0-9_-]{1,254})@)?([a-zA-Z0-9.]{1,255})(:([0-9]{1,5}))?"; size_t max_groups = 6; // The group indices of each field of interest in the regex. @@ -719,6 +710,5 @@ void extract_rti_addr_info(const char* rti_addr, rti_addr_info_t* rti_addr_info) } } regfree(®ex_compiled); - #endif } #endif diff --git a/core/platform/lf_zephyr_support.c b/core/platform/lf_zephyr_support.c index 66f7d2a2e..6ad2dfdfc 100644 --- a/core/platform/lf_zephyr_support.c +++ b/core/platform/lf_zephyr_support.c @@ -49,7 +49,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Keep track of overflows to keep clocks monotonic static int64_t _lf_timer_epoch_duration_nsec; static int64_t _lf_timer_epoch_duration_usec; -static uint32_t _lf_timer_max_tics; +static uint32_t _lf_timer_max_ticks; static volatile int64_t _lf_timer_last_epoch_nsec = 0; static uint32_t _lf_timer_freq; diff --git a/include/core/federated/net_util.h b/include/core/federated/net_util.h index 217440a24..cd9862605 100644 --- a/include/core/federated/net_util.h +++ b/include/core/federated/net_util.h @@ -44,10 +44,10 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #warning Federated support on Zephyr is still experimental. #else #include -#include #endif #include +#include #include #include "../platform.h" @@ -347,7 +347,6 @@ bool validate_host(const char* host); */ bool validate_user(const char* user); -#ifndef PLATFORM_ZEPHYR /** * Extract one match group from the rti_addr regex . @@ -356,13 +355,13 @@ bool validate_user(const char* user); bool extract_match_group(const char* rti_addr, char* dest, regmatch_t group, int max_len, int min_len, const char* err_msg); + /** * Extract match groups from the rti_addr regex. * @return true if success, else false. */ bool extract_match_groups(const char* rti_addr, char** rti_addr_strs, bool** rti_addr_flags, regmatch_t* group_array, int* gids, int* max_lens, int* min_lens, const char** err_msgs); -#endif /** * Extract the host, port and user from rti_addr.