From bfcd91e61a9382759955953afb8d0bb521fbef04 Mon Sep 17 00:00:00 2001 From: Diogo Diniz Date: Tue, 30 Jun 2026 14:46:30 +0100 Subject: [PATCH] feat: Implemented await_message_dispatch --- app/src/incphub/src/incphub_config.h | 2 +- app/src/incphub/src/incphub_shared.c | 41 +++++++++++++++--- app/src/incphub/src/incphub_shared.h | 63 ++++++++++++++++++++++++++-- app/src/incphub/src/incphub_task.c | 42 ------------------- 4 files changed, 95 insertions(+), 53 deletions(-) diff --git a/app/src/incphub/src/incphub_config.h b/app/src/incphub/src/incphub_config.h index f92ea6a..361e661 100644 --- a/app/src/incphub/src/incphub_config.h +++ b/app/src/incphub/src/incphub_config.h @@ -10,7 +10,7 @@ #ifndef INCPHUB_MAX_CLIENTS /// @brief Number of client slots to allocate -#define INCPHUB_MAX_CLIENTS 16 +#define INCPHUB_MAX_CLIENTS 8 #endif #ifndef INCPHUB_MSG_BUFFER_SIZE diff --git a/app/src/incphub/src/incphub_shared.c b/app/src/incphub/src/incphub_shared.c index c32ff86..d178880 100644 --- a/app/src/incphub/src/incphub_shared.c +++ b/app/src/incphub/src/incphub_shared.c @@ -234,15 +234,44 @@ int do_message_dispatch(incphub_cli_t *cli, uint8_t *buffer) return INCPHUB_OK; } -int await_message_dispatch(uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *source_ptr) +int await_message_dispatch(uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *source_ptr, TickType_t max_delay) { //NOTE: Does not need global lock - (void)buffer_ptr; - (void)length_ptr; - (void)source_ptr; - //TODO: Implement - return INCPHUB_ERR; + if (buffer_ptr == NULL) + return INCPHUB_EINVAL; + if (length_ptr == NULL) + return INCPHUB_EINVAL; + if (source_ptr == NULL) + return INCPHUB_EINVAL; + + // Get raw msg buffer + uint8_t *buffer; + xTaskNotifyWaitIndexed(INCPHUB_TASKINDEX_MAILBOX, 0, 0, (uint32_t*)&buffer, max_delay); + + // Extract header information + nil_packet_t *nil = extract_nil_packet(buffer, INCPHUB_MSG_BUFFER_SIZE); + if (nil == NULL) + { + return INCPHUB_BAD_PACKET; + } + ntl_packet_t *ntl = extract_ntl_packet(nil); + if (nil == NULL) + { + return INCPHUB_BAD_PACKET; + } + + // Build source spec + incphub_addr_t source = { 0 }; + source.addr = nil->header.src_addr; + source.port = ntl->header.src_port; + + // Return values + *buffer_ptr = ntl->payload; + *length_ptr = ntl->header.len; + *source_ptr = source; + + return INCPHUB_OK; } diff --git a/app/src/incphub/src/incphub_shared.h b/app/src/incphub/src/incphub_shared.h index e33007b..6f83567 100644 --- a/app/src/incphub/src/incphub_shared.h +++ b/app/src/incphub/src/incphub_shared.h @@ -59,18 +59,73 @@ int get_client_with_port(ntl_port_t port, incphub_cli_t **client_ptr); /// @brief Enqueues a message for ingest by the incphub task /// @param buffer The buffer to enqueue -/// @param max_delay The maximum time to wait for resources +/// @param max_delay The maximum time to wait for resources, in ticks /// @return INCPHUB_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 resources +/// @param max_delay The maximum time to wait for a message, in ticks /// @return INCPHUB_OK on success, an error otherwise -int dequeue_message_ingest(uint8_t **buffer_ptr, TickType_t maxDelay); +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); -int await_message_dispatch(uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *source_ptr); +/// @brief Blocks until a message is received for dispatch, or until a given timeout +/// @param buffer_ptr Location to return the message buffer +/// @param length_ptr Location to return the message length +/// @param source_ptr Location to return the message source +/// @param max_delay The maximum time to wait for a message, in ticks +/// @return INCPHUB_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, size_t *length_ptr, incphub_addr_t *source_ptr, TickType_t max_delay); + +/// @brief Extracts a NIL packet from a buffer +/// @param buffer The byte buffer to read from +/// @param length The maximum length to read from +/// @return Pointer to the NIL packet in the buffer or NULL if the packet is invalid +static inline nil_packet_t *extract_nil_packet(uint8_t *buffer, size_t length) +{ + // Ensure message is big enough to hold a packet + if (length < sizeof(nil_packet_t)) + { + return NULL; + } + + nil_packet_t *nil = (nil_packet_t *)buffer; + // Ensure length field is not going to cause buffer overflow + if (length < sizeof(nil_packet_t) + nil->header.len ) + { + return NULL; + } + + return nil; +} + +/// @brief Extracts an NTL packet from a buffer +/// @param buffer The byte buffer to read from +/// @param length The maximum length to read from +/// @return Pointer to the NTL packet in the buffer or NULL if the packet is invalid +static inline ntl_packet_t *extract_ntl_packet(nil_packet_t *nil) +{ + // Ensure message is big enough to hold a packet + if (nil->header.len < sizeof(ntl_packet_t)) + { + return NULL; + } + + ntl_packet_t *ntl = (ntl_packet_t *)nil->payload; + // Ensure length field is not going to cause buffer overflow + if (nil->header.len < sizeof(ntl_packet_t) + ntl->header.len) + { + return NULL; + } + + return ntl; +} #endif diff --git a/app/src/incphub/src/incphub_task.c b/app/src/incphub/src/incphub_task.c index ff60852..f7f832a 100644 --- a/app/src/incphub/src/incphub_task.c +++ b/app/src/incphub/src/incphub_task.c @@ -38,8 +38,6 @@ static StaticQueue_t _ingest_queue_static; // === Private functions === static nil_packet_t *extract_nil_packet(uint8_t *buffer, size_t length); static ntl_packet_t *extract_ntl_packet(nil_packet_t *nil); -static int handle_nil_packet(nil_packet_t *nil); -static int handle_ntl_packet(nil_packet_t *nil, ntl_packet_t *ntl); @@ -98,46 +96,6 @@ void incphub_task_main(void *params) // === Private functions === -static nil_packet_t *extract_nil_packet(uint8_t *buffer, size_t length) -{ - // Ensure message is big enough to hold a packet - if (length < sizeof(nil_packet_t)) - { - //TODO: Error - return NULL; - } - - nil_packet_t *nil = (nil_packet_t *)buffer; - // Ensure length field is not going to cause buffer overflow - if (length < sizeof(nil_packet_t) + nil->header.len ) - { - //TODO: Error - return NULL; - } - - return nil; -} - -static ntl_packet_t *extract_ntl_packet(nil_packet_t *nil) -{ - // Ensure message is big enough to hold a packet - if (nil->header.len < sizeof(ntl_packet_t)) - { - //TODO: Error - return NULL; - } - - ntl_packet_t *ntl = (ntl_packet_t *)nil->payload; - // Ensure length field is not going to cause buffer overflow - if (nil->header.len < sizeof(ntl_packet_t) + ntl->header.len) - { - //TODO: Error - return NULL; - } - - return ntl; -} - static int handle_nil_packet(nil_packet_t *nil) { if (nil->header.dst_addr == INCPHUB_LOCAL_NIL_ADDR)