diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index ecdb49e..446b5c0 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -31,7 +31,7 @@ add_executable(${EXECUTABLE} "") # Set compiler/linker options target_compile_definitions(${EXECUTABLE} PRIVATE ${COMPILER_DEFINES} - INCPHUB_LOCAL_NIL_ADDR=1 + LIBSHUNT_LOCAL_NIL_ADDR=1 ) target_compile_options(${EXECUTABLE} PRIVATE -Wall diff --git a/app/src/CMakeLists.txt b/app/src/CMakeLists.txt index 4fe6720..9170c13 100644 --- a/app/src/CMakeLists.txt +++ b/app/src/CMakeLists.txt @@ -1,5 +1,5 @@ add_subdirectory(tasks) -add_subdirectory(incphub) +add_subdirectory(libshunt) add_subdirectory(nil) add_subdirectory(ntl) diff --git a/app/src/incphub/README.md b/app/src/incphub/README.md deleted file mode 100644 index 650a117..0000000 --- a/app/src/incphub/README.md +++ /dev/null @@ -1,35 +0,0 @@ -# INCPHUB - -## FreeRTOS requirements - -- INCLUDE_xTaskGetCurrentTaskHandle set to 1 -- INCLUDE_vTaskSuspend set to 1 -- configUSE_TASK_NOTIFICATIONS set to 1 -- configTASK_NOTIFICATION_ARRAY_ENTRIES set to >= 2 (0 is for stream/message buffers, 1(configurable) is used by incphub) -- configUSE_MUTEXES set to 1 - -## Configuration - -Set as a macro in compile flags if wishing to override the defaults - -- INCPHUB_TASKINDEX_MAILBOX, defaults to 1 (avoid using 0) - -## Setup - -- incphub_task_preinit MUST be run before any calls to the client, likely even before the scheduler is started - -## IPC flow - -Ingest (accepting of messages into incphub) is done via queue (many to one) - -Dispatch (internal routing to local task) is done via direct-to-task notifications (see INCPHUB_TASKINDEX_MAILBOX) by passing the message buffer pointer - -## Buffer ownership - -Buffers are allocated with incphub_get_msg_buffer, at which point they belong to the requesting task. - -When passed to incphub_send_XXX functions, they become owned by incphub - -When dispatched internally, the receiving task becomes the owner and must invoke incphub_release_msg_buffer when done - -When routed externally, after calling the transmitter function, incphub releases the buffer diff --git a/app/src/incphub/include/incphub/incphub_task.h b/app/src/incphub/include/incphub/incphub_task.h deleted file mode 100644 index edc5d3e..0000000 --- a/app/src/incphub/include/incphub/incphub_task.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef INCPHUB_TASK_H_ -#define INCPHUB_TASK_H_ - -#include "FreeRTOS.h" -#include "task.h" -#include "FreeRTOSConfig.h" - -#define INCPHUB_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE -#define INCPHUB_TASK_PRIORITY (configMAX_PRIORITIES - 1) - -extern StackType_t _incphub_task_stack[INCPHUB_TASK_STACK_DEPTH]; -extern StaticTask_t _incphub_task; - -void incphub_task_preinit(); -void incphub_task_main(void *params); - -#endif diff --git a/app/src/incphub/src/incphub_config.h b/app/src/incphub/src/incphub_config.h deleted file mode 100644 index 361e661..0000000 --- a/app/src/incphub/src/incphub_config.h +++ /dev/null @@ -1,30 +0,0 @@ -// incphub_config.h - Default configurations for incphub - -#ifndef INCPHUB_CONFIG_H_ -#define INCPHUB_CONFIG_H_ - -#ifndef INCPHUB_TASKINDEX_MAILBOX -/// @brief Index of direct-to-task notification for mailbox to use -#define INCPHUB_TASKINDEX_MAILBOX 1 -#endif - -#ifndef INCPHUB_MAX_CLIENTS -/// @brief Number of client slots to allocate -#define INCPHUB_MAX_CLIENTS 8 -#endif - -#ifndef INCPHUB_MSG_BUFFER_SIZE -/// @brief Message buffer size in bytes. Must allow space for NIL and NTL headers -#define INCPHUB_MSG_BUFFER_SIZE 32 -#endif - -#ifndef INCPHUB_MSG_BUFFER_COUNT -/// @brief Number of message buffers to allocate -#define INCPHUB_MSG_BUFFER_COUNT 8 -#endif - -#ifndef INCPHUB_LOCAL_NIL_ADDR -#error "Local NIL address must be specified with INCPHUB_LOCAL_NIL_ADDR" -#endif - -#endif diff --git a/app/src/incphub/CMakeLists.txt b/app/src/libshunt/CMakeLists.txt similarity index 66% rename from app/src/incphub/CMakeLists.txt rename to app/src/libshunt/CMakeLists.txt index 0e39fbc..98de7af 100644 --- a/app/src/incphub/CMakeLists.txt +++ b/app/src/libshunt/CMakeLists.txt @@ -1,7 +1,7 @@ target_sources(${EXECUTABLE} PRIVATE - src/incphub_client.c - src/incphub_task.c - src/incphub_shared.c + src/shunt_client.c + src/shunt_task.c + src/shunt_shared.c ) target_include_directories(${EXECUTABLE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) diff --git a/app/src/libshunt/README.md b/app/src/libshunt/README.md new file mode 100644 index 0000000..cee3c50 --- /dev/null +++ b/app/src/libshunt/README.md @@ -0,0 +1,37 @@ +# LibShunt + +Shunts messages, like trains + +## FreeRTOS requirements + +- INCLUDE_xTaskGetCurrentTaskHandle set to 1 +- INCLUDE_vTaskSuspend set to 1 +- configUSE_TASK_NOTIFICATIONS set to 1 +- configTASK_NOTIFICATION_ARRAY_ENTRIES set to >= 2 (0 is for stream/message buffers, 1(configurable) is used by libshunt) +- configUSE_MUTEXES set to 1 + +## Configuration + +Set as a macro in compile flags if wishing to override the defaults + +- LIBSHUNT_TASKINDEX_MAILBOX, defaults to 1 (avoid using 0) + +## Setup + +- libshunt_task_preinit MUST be run before any calls to the client, likely even before the scheduler is started + +## IPC flow + +Ingest (accepting of messages into libshunt) is done via queue (many to one) + +Dispatch (internal routing to local task) is done via direct-to-task notifications (see LIBSHUNT_TASKINDEX_MAILBOX) by passing the message buffer pointer + +## Buffer ownership + +Buffers are allocated with shunt_get_msg_buffer, at which point they belong to the requesting task. + +When passed to libshunt_send_XXX functions, they become owned by libshunt + +When dispatched internally, the receiving task becomes the owner and must invoke shunt_release_msg_buffer when done + +When routed externally, after calling the transmitter function, libshunt releases the buffer diff --git a/app/src/incphub/include/incphub/incphub.h b/app/src/libshunt/include/libshunt/libshunt.h similarity index 51% rename from app/src/incphub/include/incphub/incphub.h rename to app/src/libshunt/include/libshunt/libshunt.h index 2a8e5eb..86a7cad 100644 --- a/app/src/incphub/include/incphub/incphub.h +++ b/app/src/libshunt/include/libshunt/libshunt.h @@ -1,31 +1,31 @@ -// incphub.h - Public definitions common to incphub and incphub client +// libshunt.h - Public definitions common to all libshunt components -#ifndef INCPHUB_H_ -#define INCPHUB_H_ +#ifndef LIBSHUNT_H_ +#define LIBSHUNT_H_ #include "ntl/ntl.h" #include "nil/nil.h" /// @brief Success -#define INCPHUB_OK (0) +#define LIBSHUNT_OK (0) /// @brief Unspecified error -#define INCPHUB_ERR (1) +#define LIBSHUNT_ERR (1) /// @brief Invalid argument -#define INCPHUB_EINVAL (2) +#define LIBSHUNT_EINVAL (2) /// @brief Resource or entry not found -#define INCPHUB_ENOTFOUND (3) +#define LIBSHUNT_ENOTFOUND (3) /// @brief Resource in use -#define INCPHUB_EBUSY (4) +#define LIBSHUNT_EBUSY (4) /// @brief Resource temporarily unavailable -#define INCPHUB_EAGAIN (5) +#define LIBSHUNT_EAGAIN (5) /// @brief Could not allocate memory -#define INCPHUB_ENOMEM (6) +#define LIBSHUNT_ENOMEM (6) /// @brief Packet failed integrity checks -#define INCPHUB_BAD_PACKET (7) +#define LIBSHUNT_BAD_PACKET (7) typedef struct { nil_addr_t addr; ntl_port_t port; -} incphub_addr_t; +} shunt_addr_t; #endif diff --git a/app/src/incphub/include/incphub/incphub_client.h b/app/src/libshunt/include/libshunt/shunt_client.h similarity index 51% rename from app/src/incphub/include/incphub/incphub_client.h rename to app/src/libshunt/include/libshunt/shunt_client.h index 75b682b..52f6dd9 100644 --- a/app/src/incphub/include/incphub/incphub_client.h +++ b/app/src/libshunt/include/libshunt/shunt_client.h @@ -1,40 +1,40 @@ -// incphub_client.h - Public interface of incphub client side +// shunt_client.h - Public interface of libshunt client side -#ifndef INCPHUB_CLIENT_H_ -#define INCPHUB_CLIENT_H_ +#ifndef LIBSHUNT_CLIENT_H_ +#define LIBSHUNT_CLIENT_H_ #include #include "FreeRTOS.h" -#include "incphub/incphub.h" +#include "libshunt/libshunt.h" #include "nil/nil.h" #include "ntl/ntl.h" -typedef struct incphub_cli_t incphub_cli_t; +typedef struct shunt_cli_t shunt_cli_t; -/// @brief Initializes an incphub client +/// @brief Initializes an libshunt client /// @param port The port number to assign to the caller -/// @return INCPHUB_OK on success, an error otherwise -int incphub_init_client(incphub_cli_t **cli, ntl_port_t port); +/// @return LIBSHUNT_OK on success, an error otherwise +int shunt_init_client(shunt_cli_t **cli, ntl_port_t port); /// @brief Attempts to get a free message buffer from the central storage space /// @param cli Current client handle /// @param buffer_ptr Location to return the buffer /// @param length The minimum size, in bytes, of the buffer get -/// @return INCPHUB_OK on success, an error otherwise -int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t length); +/// @return LIBSHUNT_OK on success, an error otherwise +int shunt_get_msg_buffer(shunt_cli_t *cli, uint8_t **buffer_ptr, size_t length); -/// @brief Releases a buffer received from incphub_await_msg +/// @brief Releases a buffer received from shunt_await_msg /// @param cli Current client handle /// @param buffer The buffer to release -/// @return INCPHUB_OK on success, an error otherwise -int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer); +/// @return LIBSHUNT_OK on success, an error otherwise +int shunt_release_msg_buffer(shunt_cli_t *cli, uint8_t *buffer); /// @brief Attempts to send a message /// @param cli Current client handle @@ -42,8 +42,8 @@ int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer); /// @param length The message length /// @param dst_addr The destination address /// @param max_delay The maximum time to wait for a resources, in ticks -/// @return INCPHUB_OK on success, an error otherwise -int incphub_send_msg(incphub_cli_t *cli, uint8_t *buffer, size_t length, incphub_addr_t dst, TickType_t max_delay); +/// @return LIBSHUNT_OK on success, an error otherwise +int shunt_send_msg(shunt_cli_t *cli, uint8_t *buffer, size_t length, shunt_addr_t dst, TickType_t max_delay); /// @brief Awaits the receival of a message /// @param cli Current client handle @@ -51,7 +51,7 @@ int incphub_send_msg(incphub_cli_t *cli, uint8_t *buffer, size_t length, incphub /// @param length_ptr Location to return the length /// @param source_ptr Location to return the message sender /// @param max_delay The maximum time to wait for a message, in ticks -/// @return INCPHUB_OK on success, an error otherwise -int incphub_await_msg(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *source_ptr, TickType_t max_delay); +/// @return LIBSHUNT_OK on success, an error otherwise +int shunt_await_msg(shunt_cli_t *cli, uint8_t **buffer_ptr, size_t *length_ptr, shunt_addr_t *source_ptr, TickType_t max_delay); #endif diff --git a/app/src/libshunt/include/libshunt/shunt_task.h b/app/src/libshunt/include/libshunt/shunt_task.h new file mode 100644 index 0000000..e99462b --- /dev/null +++ b/app/src/libshunt/include/libshunt/shunt_task.h @@ -0,0 +1,19 @@ +// shunt_task.h - Public interface to launch the libshunt FreeRTOS task + +#ifndef LIBSHUNT_TASK_H_ +#define LIBSHUNT_TASK_H_ + +#include "FreeRTOS.h" +#include "task.h" +#include "FreeRTOSConfig.h" + +#define LIBSHUNT_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE +#define LIBSHUNT_TASK_PRIORITY (configMAX_PRIORITIES - 1) + +extern StackType_t _libshunt_task_stack[LIBSHUNT_TASK_STACK_DEPTH]; +extern StaticTask_t _libshunt_task; + +void libshunt_task_preinit(); +void libshunt_task_main(void *params); + +#endif diff --git a/app/src/incphub/src/incphub_client.c b/app/src/libshunt/src/shunt_client.c similarity index 60% rename from app/src/incphub/src/incphub_client.c rename to app/src/libshunt/src/shunt_client.c index 54cf5e5..c7f266e 100644 --- a/app/src/incphub/src/incphub_client.c +++ b/app/src/libshunt/src/shunt_client.c @@ -1,10 +1,10 @@ #include -#include "incphub/incphub_client.h" -#include "incphub/incphub_task.h" -#include "incphub/incphub.h" -#include "incphub_config.h" -#include "incphub_shared.h" +#include "libshunt/shunt_client.h" +#include "libshunt/shunt_task.h" +#include "libshunt/libshunt.h" +#include "shunt_config.h" +#include "shunt_shared.h" #include "ntl/ntl.h" #include "nil/nil.h" @@ -13,24 +13,24 @@ -int incphub_init_client(incphub_cli_t **cli, ntl_port_t port) +int shunt_init_client(shunt_cli_t **cli, ntl_port_t port) { return register_client(cli, port, xTaskGetCurrentTaskHandle()); } -int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t length) +int shunt_get_msg_buffer(shunt_cli_t *cli, uint8_t **buffer_ptr, size_t length) { if (cli == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (buffer_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // Request extra space for NIL/NTL headers size_t extended_len = sizeof(nil_header_t) + sizeof(ntl_header_t) + length; uint8_t *buf; int status = get_msg_buffer(&buf, extended_len); - if (status != INCPHUB_OK) + if (status != LIBSHUNT_OK) return status; // Reserve space for NIL/NTL headers @@ -39,15 +39,15 @@ int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t leng // Return buffer *buffer_ptr = buf; - return INCPHUB_OK; + return LIBSHUNT_OK; } -int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer) +int shunt_release_msg_buffer(shunt_cli_t *cli, uint8_t *buffer) { if (cli == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (buffer == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // cli requested for interface consistency only (for now) (void)cli; @@ -59,14 +59,14 @@ int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer) return release_msg_buffer(unwound); } -int incphub_send_msg(incphub_cli_t *cli, uint8_t *buffer, size_t length, incphub_addr_t dst, TickType_t max_delay) +int shunt_send_msg(shunt_cli_t *cli, uint8_t *buffer, size_t length, shunt_addr_t dst, TickType_t max_delay) { if (cli == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (buffer == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (dst.port == NTL_PORT_EMPTY) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // Unwind buffer pointer ntl_packet_t *ntl_packet = (ntl_packet_t*)((uint8_t*)buffer - sizeof(ntl_header_t)); @@ -76,47 +76,47 @@ int incphub_send_msg(incphub_cli_t *cli, uint8_t *buffer, size_t length, incphub ntl_build_header(&ntl_packet->header, dst.port, cli->port, length); // Populate NIL header - nil_build_header(&nil_packet->header, dst.addr, INCPHUB_LOCAL_NIL_ADDR, length + sizeof(ntl_header_t)); + nil_build_header(&nil_packet->header, dst.addr, LIBSHUNT_LOCAL_NIL_ADDR, length + sizeof(ntl_header_t)); // Pass to ingest return enqueue_message_ingest((uint8_t *)nil_packet, max_delay); } -int incphub_await_msg(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *source_ptr, TickType_t max_delay) +int shunt_await_msg(shunt_cli_t *cli, uint8_t **buffer_ptr, size_t *length_ptr, shunt_addr_t *source_ptr, TickType_t max_delay) { if (cli == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (buffer_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (length_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (source_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // cli requested for interface consistency only (for now) (void)cli; uint8_t *buffer; int err = await_message_dispatch(&buffer, max_delay); - if (err != INCPHUB_OK) + if (err != LIBSHUNT_OK) { return err; } // Extract header information - nil_packet_t *nil = extract_nil_packet(buffer, INCPHUB_MSG_BUFFER_SIZE); + nil_packet_t *nil = extract_nil_packet(buffer, LIBSHUNT_MSG_BUFFER_SIZE); if (nil == NULL) { - return INCPHUB_BAD_PACKET; + return LIBSHUNT_BAD_PACKET; } ntl_packet_t *ntl = extract_ntl_packet(nil); if (nil == NULL) { - return INCPHUB_BAD_PACKET; + return LIBSHUNT_BAD_PACKET; } // Build source spec - incphub_addr_t source = { 0 }; + shunt_addr_t source = { 0 }; source.addr = nil->header.src_addr; source.port = ntl->header.src_port; @@ -125,5 +125,5 @@ int incphub_await_msg(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t *length_p *length_ptr = ntl->header.len; *source_ptr = source; - return INCPHUB_OK; + return LIBSHUNT_OK; } diff --git a/app/src/libshunt/src/shunt_config.h b/app/src/libshunt/src/shunt_config.h new file mode 100644 index 0000000..8f51444 --- /dev/null +++ b/app/src/libshunt/src/shunt_config.h @@ -0,0 +1,30 @@ +// shunt_config.h - Default configurations for libshunt + +#ifndef LIBSHUNT_CONFIG_H_ +#define LIBSHUNT_CONFIG_H_ + +#ifndef LIBSHUNT_TASKINDEX_MAILBOX +/// @brief Index of direct-to-task notification for mailbox to use +#define LIBSHUNT_TASKINDEX_MAILBOX 1 +#endif + +#ifndef LIBSHUNT_MAX_CLIENTS +/// @brief Number of client slots to allocate +#define LIBSHUNT_MAX_CLIENTS 8 +#endif + +#ifndef LIBSHUNT_MSG_BUFFER_SIZE +/// @brief Message buffer size in bytes. Must allow space for NIL and NTL headers +#define LIBSHUNT_MSG_BUFFER_SIZE 32 +#endif + +#ifndef LIBSHUNT_MSG_BUFFER_COUNT +/// @brief Number of message buffers to allocate +#define LIBSHUNT_MSG_BUFFER_COUNT 8 +#endif + +#ifndef LIBSHUNT_LOCAL_NIL_ADDR +#error "Local NIL address must be specified with LIBSHUNT_LOCAL_NIL_ADDR" +#endif + +#endif diff --git a/app/src/incphub/src/incphub_shared.c b/app/src/libshunt/src/shunt_shared.c similarity index 56% rename from app/src/incphub/src/incphub_shared.c rename to app/src/libshunt/src/shunt_shared.c index bbb03ff..c2a7a46 100644 --- a/app/src/incphub/src/incphub_shared.c +++ b/app/src/libshunt/src/shunt_shared.c @@ -1,4 +1,4 @@ -#include "incphub_shared.h" +#include "shunt_shared.h" #include #include @@ -8,15 +8,15 @@ #include "semphr.h" #include "task.h" -#include "incphub/incphub.h" -#include "incphub_config.h" +#include "libshunt/libshunt.h" +#include "shunt_config.h" #include "nil/nil.h" #if UINTPTR_MAX > UINT32_MAX -#error "Pointers of 32-bits or less are required for incphub mailbox to function correctly." +#error "Pointers of 32-bits or less are required for libshunt mailbox to function correctly." #endif @@ -24,16 +24,16 @@ // === Private variables === // Global lock -static SemaphoreHandle_t _incphub_lock; -static StaticSemaphore_t _incphub_lock_store; +static SemaphoreHandle_t _libshunt_lock; +static StaticSemaphore_t _libshunt_lock_store; // Clients -static incphub_cli_t _clients[INCPHUB_MAX_CLIENTS]; +static shunt_cli_t _clients[LIBSHUNT_MAX_CLIENTS]; // Message buffers -// INCPHUB_MSG_BUFFER_COUNT buffers of INCPHUB_MSG_BUFFER_SIZE bytes -static uint8_t _msg_buffers[INCPHUB_MSG_BUFFER_COUNT][INCPHUB_MSG_BUFFER_SIZE]; -static bool _msg_buffer_used[INCPHUB_MSG_BUFFER_COUNT]; +// LIBSHUNT_MSG_BUFFER_COUNT buffers of LIBSHUNT_MSG_BUFFER_SIZE bytes +static uint8_t _msg_buffers[LIBSHUNT_MSG_BUFFER_COUNT][LIBSHUNT_MSG_BUFFER_SIZE]; +static bool _msg_buffer_used[LIBSHUNT_MSG_BUFFER_COUNT]; @@ -41,9 +41,9 @@ static bool _msg_buffer_used[INCPHUB_MSG_BUFFER_COUNT]; static bool global_lock(TickType_t max_delay); static void global_unlock(); /// @remark Returns NULL if not found -static incphub_cli_t *find_empty_client(); +static shunt_cli_t *find_empty_client(); /// @remark Returns NULL if not found -static incphub_cli_t *find_client_with_port(ntl_port_t port); +static shunt_cli_t *find_client_with_port(ntl_port_t port); /// @remark Returns NULL if not found static uint8_t *find_unused_buffer(); /// @remark Returns -1 if not found @@ -54,35 +54,35 @@ static int find_index_of_buffer(uint8_t *buffer); // === Public functions === int internal_init() { - _incphub_lock = xSemaphoreCreateMutexStatic(&_incphub_lock_store); + _libshunt_lock = xSemaphoreCreateMutexStatic(&_libshunt_lock_store); memset(_clients, 0, sizeof(_clients)); memset(_msg_buffer_used, 0, sizeof(_msg_buffer_used)); - return INCPHUB_OK; + return LIBSHUNT_OK; } -int register_client(incphub_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task) +int register_client(shunt_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task) { if (port == NTL_PORT_EMPTY) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (cli_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // == Enter == if (!global_lock(portMAX_DELAY)) - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; // Prevent double binding of ports if (find_client_with_port(port) != NULL) { global_unlock(); - return INCPHUB_EBUSY; + return LIBSHUNT_EBUSY; } - incphub_cli_t *client = find_empty_client(); + shunt_cli_t *client = find_empty_client(); if (client == NULL) { global_unlock(); - return INCPHUB_EBUSY; + return LIBSHUNT_EBUSY; } // Populate client data @@ -93,43 +93,43 @@ int register_client(incphub_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task) *cli_ptr = client; global_unlock(); - return INCPHUB_OK; + return LIBSHUNT_OK; } int get_msg_buffer(uint8_t **buffer_ptr, size_t len) { if (buffer_ptr == NULL) - return INCPHUB_EINVAL; - if (len > INCPHUB_MSG_BUFFER_SIZE) - return INCPHUB_ENOMEM; + return LIBSHUNT_EINVAL; + if (len > LIBSHUNT_MSG_BUFFER_SIZE) + return LIBSHUNT_ENOMEM; // == Enter == if (!global_lock(portMAX_DELAY)) - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; //TODO: Allow buffer to be given by another layer (below NIL/NTL) uint8_t *buf = find_unused_buffer(); if (buf == NULL) { global_unlock(); - return INCPHUB_ENOMEM; + return LIBSHUNT_ENOMEM; } // Return buffer *buffer_ptr = buf; global_unlock(); - return INCPHUB_OK; + return LIBSHUNT_OK; } int release_msg_buffer(uint8_t *buffer) { if (buffer == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // == Enter == if (!global_lock(portMAX_DELAY)) - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; int index = find_index_of_buffer(buffer); @@ -137,47 +137,47 @@ int release_msg_buffer(uint8_t *buffer) if (index == -1) { global_unlock(); - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; } // Handle not-in-use buffers if (_msg_buffer_used[index] == false) { global_unlock(); - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; } // Mark buffer as free _msg_buffer_used[index] = false; global_unlock(); - return INCPHUB_OK; + return LIBSHUNT_OK; } -int get_client_with_port(ntl_port_t port, incphub_cli_t **client_ptr) +int get_client_with_port(ntl_port_t port, shunt_cli_t **client_ptr) { if (port == NTL_PORT_EMPTY) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (client_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // == Enter == if (!global_lock(portMAX_DELAY)) - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; // Map port to client - incphub_cli_t *client = find_client_with_port(port); + shunt_cli_t *client = find_client_with_port(port); if (client == NULL) { global_unlock(); - return INCPHUB_ENOTFOUND; + return LIBSHUNT_ENOTFOUND; } // Return client *client_ptr = client; global_unlock(); - return INCPHUB_OK; + return LIBSHUNT_OK; } int enqueue_message_ingest(uint8_t *buffer, TickType_t max_delay) @@ -185,15 +185,15 @@ int enqueue_message_ingest(uint8_t *buffer, TickType_t max_delay) //NOTE: Does not need global lock if (buffer == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // Try to queue message - if (xQueueSendToBack(incphub_ingest_queue, &buffer, max_delay) != pdPASS) + if (xQueueSendToBack(libshunt_ingest_queue, &buffer, max_delay) != pdPASS) { - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; } - return INCPHUB_OK; + return LIBSHUNT_OK; } int dequeue_message_ingest(uint8_t **buffer_ptr, TickType_t max_delay) @@ -201,39 +201,39 @@ int dequeue_message_ingest(uint8_t **buffer_ptr, TickType_t max_delay) //NOTE: Does not need global lock if (buffer_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; uint8_t *buffer; // Try to dequeue message - if (xQueueReceive(incphub_ingest_queue, &buffer, max_delay) == pdFALSE) + if (xQueueReceive(libshunt_ingest_queue, &buffer, max_delay) == pdFALSE) { - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; } // Return buffer *buffer_ptr = buffer; - return INCPHUB_OK; + return LIBSHUNT_OK; } -int do_message_dispatch(incphub_cli_t *cli, uint8_t *buffer) +int do_message_dispatch(shunt_cli_t *cli, uint8_t *buffer) { //NOTE: Does not need global lock if (cli == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; if (buffer == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // Try to notify - if (xTaskNotifyIndexed(cli->task, INCPHUB_TASKINDEX_MAILBOX, (uint32_t)buffer, eSetValueWithoutOverwrite) == pdFALSE) + if (xTaskNotifyIndexed(cli->task, LIBSHUNT_TASKINDEX_MAILBOX, (uint32_t)buffer, eSetValueWithoutOverwrite) == pdFALSE) { // Failed because previous message has not yet been processed - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; } - return INCPHUB_OK; + return LIBSHUNT_OK; } int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay) @@ -241,20 +241,20 @@ int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay) //NOTE: Does not need global lock if (buffer_ptr == NULL) - return INCPHUB_EINVAL; + return LIBSHUNT_EINVAL; // Get raw msg buffer uint8_t *buffer; - if (xTaskNotifyWaitIndexed(INCPHUB_TASKINDEX_MAILBOX, 0, 0, (uint32_t*)&buffer, max_delay) == pdFALSE) + if (xTaskNotifyWaitIndexed(LIBSHUNT_TASKINDEX_MAILBOX, 0, 0, (uint32_t*)&buffer, max_delay) == pdFALSE) { // Nothing pending - return INCPHUB_EAGAIN; + return LIBSHUNT_EAGAIN; } // Return buffer *buffer_ptr = buffer; - return INCPHUB_OK; + return LIBSHUNT_OK; } @@ -262,24 +262,24 @@ int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay) // === Private function definitions === static bool global_lock(TickType_t max_delay) { - return xSemaphoreTake(_incphub_lock, max_delay) == pdTRUE; + return xSemaphoreTake(_libshunt_lock, max_delay) == pdTRUE; } static void global_unlock() { - xSemaphoreGive(_incphub_lock); + xSemaphoreGive(_libshunt_lock); } -static incphub_cli_t *find_empty_client() +static shunt_cli_t *find_empty_client() { return find_client_with_port(NTL_PORT_EMPTY); } -static incphub_cli_t *find_client_with_port(ntl_port_t port) +static shunt_cli_t *find_client_with_port(ntl_port_t port) { - for (int i = 0; i < INCPHUB_MAX_CLIENTS; ++i) + for (int i = 0; i < LIBSHUNT_MAX_CLIENTS; ++i) { - incphub_cli_t *client = &_clients[i]; + shunt_cli_t *client = &_clients[i]; if (client->port == port) return client; @@ -290,7 +290,7 @@ static incphub_cli_t *find_client_with_port(ntl_port_t port) static uint8_t *find_unused_buffer() { - for (int i = 0; i < INCPHUB_MSG_BUFFER_COUNT; ++i) + for (int i = 0; i < LIBSHUNT_MSG_BUFFER_COUNT; ++i) { if (!_msg_buffer_used[i]) return _msg_buffers[i]; @@ -301,7 +301,7 @@ static uint8_t *find_unused_buffer() static int find_index_of_buffer(uint8_t *buffer) { - for (int i = 0; i < INCPHUB_MSG_BUFFER_COUNT; ++i) + for (int i = 0; i < LIBSHUNT_MSG_BUFFER_COUNT; ++i) { if (buffer == _msg_buffers[i]) return i; diff --git a/app/src/incphub/src/incphub_shared.h b/app/src/libshunt/src/shunt_shared.h similarity index 73% rename from app/src/incphub/src/incphub_shared.h rename to app/src/libshunt/src/shunt_shared.h index 004f160..febf0ed 100644 --- a/app/src/incphub/src/incphub_shared.h +++ b/app/src/libshunt/src/shunt_shared.h @@ -1,7 +1,7 @@ -// incphub_sharedl.h - Internal mechanisms shared by incphub and incphub_client +// shunt_sharedl.h - Internal mechanisms shared by libshunt components -#ifndef INCPHUB_SHARED_H_ -#define INCPHUB_SHARED_H_ +#ifndef LIBSHUNT_SHARED_H_ +#define LIBSHUNT_SHARED_H_ #include "FreeRTOS.h" #include "task.h" @@ -10,75 +10,75 @@ #include "nil/nil.h" #include "ntl/ntl.h" -#include "incphub/incphub.h" +#include "libshunt/libshunt.h" -struct incphub_cli_t { +struct shunt_cli_t { /// @remark If set to NTL_PORT_EMPTY denotes an empty client ntl_port_t port; TaskHandle_t task; }; // Queue (queue of buffers) -extern QueueHandle_t incphub_ingest_queue; +extern QueueHandle_t libshunt_ingest_queue; -#ifndef INCPHUB_CLIENT_H_ -typedef struct incphub_cli_t incphub_cli_t; +#ifndef LIBSHUNT_CLIENT_H_ +typedef struct shunt_cli_t shunt_cli_t; #endif -/// @brief Initializes the internal state of incphub -/// @return INCPHUB_OK on success, an error otherwise +/// @brief Initializes the internal state of libshunt +/// @return LIBSHUNT_OK on success, an error otherwise int internal_init(); -/// @brief Registers a task as an incphub client +/// @brief Registers a task as an libshunt client /// @param cli_ptr Location to return client /// @param port The port to register to /// @param task The task to register as the handler -/// @return INCPHUB_OK on success, an error otherwise -int register_client(incphub_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task); +/// @return LIBSHUNT_OK on success, an error otherwise +int register_client(shunt_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task); /// @brief Attempts to get a free message buffer from the central storage space /// @param buffer_ptr Location to return the buffer /// @param len The minimum size, in bytes, of the buffer get -/// @return INCPHUB_OK on success, an error otherwise +/// @return LIBSHUNT_OK on success, an error otherwise int get_msg_buffer(uint8_t **buffer_ptr, size_t len); /// @brief Releases a buffer previously acquired with get_msg_buffer /// @param buffer The buffer to release -/// @return INCPHUB_OK on success, an error otherwise +/// @return LIBSHUNT_OK on success, an error otherwise int release_msg_buffer(uint8_t *buffer); /// @brief Attempts to get the client assigned to the given port /// @param port The port to look for /// @param client_ptr Location to return the client -/// @return INCPHUB_OK on success, an error otherwise -int get_client_with_port(ntl_port_t port, incphub_cli_t **client_ptr); +/// @return LIBSHUNT_OK on success, an error otherwise +int get_client_with_port(ntl_port_t port, shunt_cli_t **client_ptr); -/// @brief Enqueues a message for ingest by the incphub task +/// @brief Enqueues a message for ingest by the libshunt task /// @param buffer The buffer to enqueue /// @param max_delay The maximum time to wait for resources, in ticks -/// @return INCPHUB_OK on success, an error otherwise +/// @return LIBSHUNT_OK on success, an error otherwise int enqueue_message_ingest(uint8_t *buffer, TickType_t max_delay); /// @brief Dequeues a message for ingest /// @param buffer_ptr Location to return the buffer /// @param max_delay The maximum time to wait for a message, in ticks -/// @return INCPHUB_OK on success, an error otherwise +/// @return LIBSHUNT_OK on success, an error otherwise int dequeue_message_ingest(uint8_t **buffer_ptr, TickType_t max_delay); /// @brief Sends a message for dispatch to the corresponding client /// @param cli The client to send the message to /// @param buffer The message buffer to send -/// @return INCPHUB_OK on success, an error otherwise -int do_message_dispatch(incphub_cli_t *cli, uint8_t *buffer); +/// @return LIBSHUNT_OK on success, an error otherwise +int do_message_dispatch(shunt_cli_t *cli, uint8_t *buffer); /// @brief Blocks until a message is received for dispatch, or until a given timeout /// @param buffer_ptr Location to return the message buffer /// @param max_delay The maximum time to wait for a message, in ticks -/// @return INCPHUB_OK on success, an error otherwise +/// @return LIBSHUNT_OK on success, an error otherwise /// @remark Buffers obatined from this function must be release with release_msg_buffer once no longer in use int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay); diff --git a/app/src/incphub/src/incphub_task.c b/app/src/libshunt/src/shunt_task.c similarity index 73% rename from app/src/incphub/src/incphub_task.c rename to app/src/libshunt/src/shunt_task.c index 2ce2ec7..9612f56 100644 --- a/app/src/incphub/src/incphub_task.c +++ b/app/src/libshunt/src/shunt_task.c @@ -1,13 +1,13 @@ -#include "incphub/incphub_task.h" +#include "libshunt/shunt_task.h" #include #include "FreeRTOS.h" #include "queue.h" -#include "incphub/incphub.h" -#include "incphub_config.h" -#include "incphub_shared.h" +#include "libshunt/libshunt.h" +#include "shunt_config.h" +#include "shunt_shared.h" #include "ntl/ntl.h" #include "nil/nil.h" @@ -15,15 +15,15 @@ // === Static task space === -StackType_t _incphub_task_stack[INCPHUB_TASK_STACK_DEPTH]; -StaticTask_t _incphub_task; +StackType_t _libshunt_task_stack[LIBSHUNT_TASK_STACK_DEPTH]; +StaticTask_t _libshunt_task; // === Public variables === -// Queue Packets awaiting to be accepted by incphub -QueueHandle_t incphub_ingest_queue; +// Queue Packets awaiting to be accepted by libshunt +QueueHandle_t libshunt_ingest_queue; @@ -42,14 +42,14 @@ static int handle_ntl_packet(nil_packet_t *nil, ntl_packet_t *ntl); // === Public functions === -void incphub_task_preinit() +void libshunt_task_preinit() { internal_init(); - incphub_ingest_queue = + libshunt_ingest_queue = xQueueCreateStatic(INGEST_QUEUE_LENGTH, INGEST_QUEUE_ITEM_SIZE, _ingest_queue_store, &_ingest_queue_static); } -void incphub_task_main(void *params) +void libshunt_task_main(void *params) { (void)params; int err; @@ -61,7 +61,7 @@ void incphub_task_main(void *params) err = dequeue_message_ingest(&buffer, portMAX_DELAY); // Get NIL packet - nil_packet_t *nil = extract_nil_packet(buffer, INCPHUB_MSG_BUFFER_SIZE); + nil_packet_t *nil = extract_nil_packet(buffer, LIBSHUNT_MSG_BUFFER_SIZE); if (nil == NULL) { //TODO: Error @@ -70,7 +70,7 @@ void incphub_task_main(void *params) // Process or route err = handle_nil_packet(nil); - if (err == INCPHUB_EAGAIN) + if (err == LIBSHUNT_EAGAIN) { // Failed but only temporarily // Could be target task not ready @@ -81,7 +81,7 @@ void incphub_task_main(void *params) // This avoids clogging the system due to a single unresponsive task continue; } - else if (err != INCPHUB_OK) + else if (err != LIBSHUNT_OK) { // Failed for some other reason // Do not retry @@ -95,21 +95,21 @@ void incphub_task_main(void *params) // === Private functions === static int handle_nil_packet(nil_packet_t *nil) { - if (nil->header.dst_addr == INCPHUB_LOCAL_NIL_ADDR + if (nil->header.dst_addr == LIBSHUNT_LOCAL_NIL_ADDR || nil->header.dst_addr == NIL_ADDR_LOCALHOST) { ntl_packet_t *ntl = extract_ntl_packet(nil); if (ntl == NULL) { //TODO: Error - return INCPHUB_BAD_PACKET; + return LIBSHUNT_BAD_PACKET; } return handle_ntl_packet(nil, ntl); } //TODO: Route to external interface - return INCPHUB_ERR; + return LIBSHUNT_ERR; } static int handle_ntl_packet(nil_packet_t *nil, ntl_packet_t *ntl) @@ -117,9 +117,9 @@ static int handle_ntl_packet(nil_packet_t *nil, ntl_packet_t *ntl) int err; ntl_port_t port = ntl_get_dst(&ntl->header); - incphub_cli_t *client; + shunt_cli_t *client; err = get_client_with_port(port, &client); - if (err != INCPHUB_OK) + if (err != LIBSHUNT_OK) { //TODO: Error return err; diff --git a/app/src/main.c b/app/src/main.c index 51bcb1e..ff3e690 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -6,18 +6,18 @@ #include "tasks/heartbeat_task.h" #include "tasks/sensor_task.h" #include "tasks/requester_task.h" -#include "incphub/incphub_task.h" +#include "libshunt/shunt_task.h" int my_main() { // Init - incphub_task_preinit(); + libshunt_task_preinit(); // Create tasks xTaskCreateStatic(heartbeat_task_main, "heartbeat", HEARTBEAT_TASK_STACK_DEPTH, NULL, HEARTBEAT_TASK_PRIORITY, _heartbeat_task_stack, &_heartbeat_task); xTaskCreateStatic(sensor_task_main, "sensor", SENSOR_TASK_STACK_DEPTH, NULL, SENSOR_TASK_PRIORITY, _sensor_task_stack, &_sensor_task); xTaskCreateStatic(requester_task_main, "requester", REQUESTER_TASK_STACK_DEPTH, NULL, REQUESTER_TASK_PRIORITY, _requester_task_stack, &_requester_task); - xTaskCreateStatic(incphub_task_main, "incphub", INCPHUB_TASK_STACK_DEPTH, NULL, INCPHUB_TASK_PRIORITY, _incphub_task_stack, &_incphub_task); + xTaskCreateStatic(libshunt_task_main, "shunt", LIBSHUNT_TASK_STACK_DEPTH, NULL, LIBSHUNT_TASK_PRIORITY, _libshunt_task_stack, &_libshunt_task); // Run vTaskStartScheduler(); diff --git a/app/src/tasks/requester_task.c b/app/src/tasks/requester_task.c index eb42270..f7560a0 100644 --- a/app/src/tasks/requester_task.c +++ b/app/src/tasks/requester_task.c @@ -7,7 +7,7 @@ #include "stm32f4xx_hal_uart.h" #include "usart.h" -#include "incphub/incphub_client.h" +#include "libshunt/shunt_client.h" @@ -27,11 +27,11 @@ void requester_task_main(void *params) uint8_t *buffer; size_t length; - incphub_addr_t source; + shunt_addr_t source; - // Init incphub client - incphub_cli_t *cli; - incphub_init_client(&cli, 70); + // Init libshunt client + shunt_cli_t *cli; + shunt_init_client(&cli, 70); while (1) { @@ -43,17 +43,17 @@ void requester_task_main(void *params) random_index %= RANDOM_COUNT; // Get buffer - incphub_get_msg_buffer(cli, &buffer, sizeof(uint32_t)); + shunt_get_msg_buffer(cli, &buffer, sizeof(uint32_t)); // Populate buffer *(uint32_t*)buffer = request; // Send to sendor task - incphub_addr_t destination = { .addr = NIL_ADDR_LOCALHOST, .port = 69 }; - incphub_send_msg(cli, buffer, sizeof(uint32_t), destination, portMAX_DELAY); + shunt_addr_t destination = { .addr = NIL_ADDR_LOCALHOST, .port = 69 }; + shunt_send_msg(cli, buffer, sizeof(uint32_t), destination, portMAX_DELAY); // Await reply (assumes no other task will send to this one) - incphub_await_msg(cli, &buffer, &length, &source, portMAX_DELAY); + shunt_await_msg(cli, &buffer, &length, &source, portMAX_DELAY); // Ignore messages of different size if (length != sizeof(uint32_t)) @@ -63,7 +63,7 @@ void requester_task_main(void *params) uint32_t num = *(uint32_t*)buffer; // Release buffer - incphub_release_msg_buffer(cli, buffer); + shunt_release_msg_buffer(cli, buffer); // Profit (send to UART to ensure it worked) char strbuf[32]; diff --git a/app/src/tasks/sensor_task.c b/app/src/tasks/sensor_task.c index 64296b1..1a6a5ff 100644 --- a/app/src/tasks/sensor_task.c +++ b/app/src/tasks/sensor_task.c @@ -4,7 +4,7 @@ #include "stm32f4xx_hal_gpio.h" #include "gpio.h" -#include "incphub/incphub_client.h" +#include "libshunt/shunt_client.h" @@ -19,16 +19,16 @@ void sensor_task_main(void *params) uint8_t *buffer; size_t length; - incphub_addr_t source; + shunt_addr_t source; - // Init incphub client - incphub_cli_t *cli; - incphub_init_client(&cli, 69); + // Init libshunt client + shunt_cli_t *cli; + shunt_init_client(&cli, 69); while (1) { // Get a message (assume it is a uint32_t) - incphub_await_msg(cli, &buffer, &length, &source, portMAX_DELAY); + shunt_await_msg(cli, &buffer, &length, &source, portMAX_DELAY); // Ignore messages of different size if (length != sizeof(uint32_t)) @@ -38,18 +38,18 @@ void sensor_task_main(void *params) uint32_t num = *(uint32_t*)buffer; // Release buffer once read - incphub_release_msg_buffer(cli, buffer); + shunt_release_msg_buffer(cli, buffer); // Process message num++; // Request buffer for reply - incphub_get_msg_buffer(cli, &buffer, sizeof(uint32_t)); + shunt_get_msg_buffer(cli, &buffer, sizeof(uint32_t)); // Prepare reply message *(uint32_t*)buffer = num; // Send reply - incphub_send_msg(cli, buffer, sizeof(uint32_t), source, portMAX_DELAY); + shunt_send_msg(cli, buffer, sizeof(uint32_t), source, portMAX_DELAY); } }