diff --git a/app/src/incphub/include/incphub/incphub_client.h b/app/src/incphub/include/incphub/incphub_client.h index a6f25a3..5d6dc9b 100644 --- a/app/src/incphub/include/incphub/incphub_client.h +++ b/app/src/incphub/include/incphub/incphub_client.h @@ -5,6 +5,8 @@ #include +#include "FreeRTOS.h" + #include "incphub/incphub.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 length The message length /// @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 -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 /// @param cli Current client handle /// @param buffer_ptr Location to return the buffer /// @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); +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 diff --git a/app/src/incphub/src/incphub_client.c b/app/src/incphub/src/incphub_client.c index 375e9e8..1af0b3c 100644 --- a/app/src/incphub/src/incphub_client.c +++ b/app/src/incphub/src/incphub_client.c @@ -59,7 +59,7 @@ int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer) 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) 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)); // 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) 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) (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; } diff --git a/app/src/incphub/src/incphub_shared.c b/app/src/incphub/src/incphub_shared.c index d178880..27eb98b 100644 --- a/app/src/incphub/src/incphub_shared.c +++ b/app/src/incphub/src/incphub_shared.c @@ -100,6 +100,8 @@ 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; // == Enter == if (!global_lock(portMAX_DELAY)) @@ -234,42 +236,23 @@ 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, TickType_t max_delay) +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; - 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) + if (xTaskNotifyWaitIndexed(INCPHUB_TASKINDEX_MAILBOX, 0, 0, (uint32_t*)&buffer, max_delay) == pdFALSE) { - return INCPHUB_BAD_PACKET; - } - ntl_packet_t *ntl = extract_ntl_packet(nil); - if (nil == NULL) - { - return INCPHUB_BAD_PACKET; + // Nothing pending + return INCPHUB_EAGAIN; } - // 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 buffer + *buffer_ptr = buffer; 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) { - if (buffer = _msg_buffers[i]) + if (buffer == _msg_buffers[i]) return i; } diff --git a/app/src/incphub/src/incphub_shared.h b/app/src/incphub/src/incphub_shared.h index 6f83567..004f160 100644 --- a/app/src/incphub/src/incphub_shared.h +++ b/app/src/incphub/src/incphub_shared.h @@ -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 /// @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); +int await_message_dispatch(uint8_t **buffer_ptr, TickType_t max_delay); /// @brief Extracts a NIL packet from a buffer /// @param buffer The byte buffer to read from