chore: Moved unpacking of NIL/NTL headers for consistent responsibilities

This commit is contained in:
2026-06-30 15:05:09 +01:00
parent 7aa5a91181
commit 55b3404674
4 changed files with 49 additions and 35 deletions

View File

@@ -5,6 +5,8 @@
#include <stddef.h> #include <stddef.h>
#include "FreeRTOS.h"
#include "incphub/incphub.h" #include "incphub/incphub.h"
#include "nil/nil.h" #include "nil/nil.h"
@@ -39,15 +41,17 @@ int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer);
/// @param buffer The message buffer (gets consumed) /// @param buffer The message buffer (gets consumed)
/// @param length The message length /// @param length The message length
/// @param dst_port The destination NTL port /// @param dst_port The destination NTL port
/// @param max_delay The maximum time to wait for a resources, in ticks
/// @return INCPHUB_OK on success, an error otherwise /// @return INCPHUB_OK on success, an error otherwise
int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port); int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port, TickType_t max_delay);
/// @brief Awaits the receival of a message /// @brief Awaits the receival of a message
/// @param cli Current client handle /// @param cli Current client handle
/// @param buffer_ptr Location to return the buffer /// @param buffer_ptr Location to return the buffer
/// @param length_ptr Location to return the length /// @param length_ptr Location to return the length
/// @param source_ptr Location to return the message sender /// @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 /// @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); 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);
#endif #endif

View File

@@ -59,7 +59,7 @@ int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer)
return release_msg_buffer(unwound); return release_msg_buffer(unwound);
} }
int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port) int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port, TickType_t max_delay)
{ {
if (cli == NULL) if (cli == NULL)
return INCPHUB_EINVAL; return INCPHUB_EINVAL;
@@ -79,10 +79,10 @@ int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_p
nil_build_header(&nil_packet->header, NIL_ADDR_LOCALHOST, INCPHUB_LOCAL_NIL_ADDR, length + sizeof(ntl_header_t)); nil_build_header(&nil_packet->header, NIL_ADDR_LOCALHOST, INCPHUB_LOCAL_NIL_ADDR, length + sizeof(ntl_header_t));
// Pass to ingest // Pass to ingest
return enqueue_message_ingest((uint8_t *)nil_packet, portMAX_DELAY); 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) 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)
{ {
if (cli == NULL) if (cli == NULL)
return INCPHUB_EINVAL; return INCPHUB_EINVAL;
@@ -96,5 +96,34 @@ int incphub_await_msg(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t *length_p
// cli requested for interface consistency only (for now) // cli requested for interface consistency only (for now)
(void)cli; (void)cli;
return await_message_dispatch(buffer_ptr, length_ptr, source_ptr); uint8_t *buffer;
int err = await_message_dispatch(&buffer, max_delay);
if (err != INCPHUB_OK)
{
return err;
}
// 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;
} }

View File

@@ -100,6 +100,8 @@ int get_msg_buffer(uint8_t **buffer_ptr, size_t len)
{ {
if (buffer_ptr == NULL) if (buffer_ptr == NULL)
return INCPHUB_EINVAL; return INCPHUB_EINVAL;
if (len > INCPHUB_MSG_BUFFER_SIZE)
return INCPHUB_ENOMEM;
// == Enter == // == Enter ==
if (!global_lock(portMAX_DELAY)) if (!global_lock(portMAX_DELAY))
@@ -234,42 +236,23 @@ int do_message_dispatch(incphub_cli_t *cli, uint8_t *buffer)
return INCPHUB_OK; return INCPHUB_OK;
} }
int await_message_dispatch(uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *source_ptr, TickType_t max_delay) int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay)
{ {
//NOTE: Does not need global lock //NOTE: Does not need global lock
if (buffer_ptr == NULL) if (buffer_ptr == NULL)
return INCPHUB_EINVAL; return INCPHUB_EINVAL;
if (length_ptr == NULL)
return INCPHUB_EINVAL;
if (source_ptr == NULL)
return INCPHUB_EINVAL;
// Get raw msg buffer // Get raw msg buffer
uint8_t *buffer; uint8_t *buffer;
xTaskNotifyWaitIndexed(INCPHUB_TASKINDEX_MAILBOX, 0, 0, (uint32_t*)&buffer, max_delay); if (xTaskNotifyWaitIndexed(INCPHUB_TASKINDEX_MAILBOX, 0, 0, (uint32_t*)&buffer, max_delay) == pdFALSE)
// Extract header information
nil_packet_t *nil = extract_nil_packet(buffer, INCPHUB_MSG_BUFFER_SIZE);
if (nil == NULL)
{ {
return INCPHUB_BAD_PACKET; // Nothing pending
} return INCPHUB_EAGAIN;
ntl_packet_t *ntl = extract_ntl_packet(nil);
if (nil == NULL)
{
return INCPHUB_BAD_PACKET;
} }
// Build source spec // Return buffer
incphub_addr_t source = { 0 }; *buffer_ptr = buffer;
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; return INCPHUB_OK;
} }
@@ -320,7 +303,7 @@ static int find_index_of_buffer(uint8_t *buffer)
{ {
for (int i = 0; i < INCPHUB_MSG_BUFFER_COUNT; ++i) for (int i = 0; i < INCPHUB_MSG_BUFFER_COUNT; ++i)
{ {
if (buffer = _msg_buffers[i]) if (buffer == _msg_buffers[i])
return i; return i;
} }

View File

@@ -77,12 +77,10 @@ int do_message_dispatch(incphub_cli_t *cli, uint8_t *buffer);
/// @brief Blocks until a message is received for dispatch, or until a given timeout /// @brief Blocks until a message is received for dispatch, or until a given timeout
/// @param buffer_ptr Location to return the message buffer /// @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 /// @param max_delay The maximum time to wait for a message, in ticks
/// @return INCPHUB_OK on success, an error otherwise /// @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 /// @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); int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay);
/// @brief Extracts a NIL packet from a buffer /// @brief Extracts a NIL packet from a buffer
/// @param buffer The byte buffer to read from /// @param buffer The byte buffer to read from