diff --git a/app/src/incphub/include/incphub/incphub_client.h b/app/src/incphub/include/incphub/incphub_client.h index b3453c5..e27ce20 100644 --- a/app/src/incphub/include/incphub/incphub_client.h +++ b/app/src/incphub/include/incphub/incphub_client.h @@ -28,6 +28,12 @@ int incphub_init_client(incphub_cli_t **cli, ntl_port_t port); /// @return INCPHUB_OK on success, an error otherwise int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t length); +/// @brief Releases a buffer previously acquired with incphub_get_msg_buffer +/// @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); + /// @brief Attempts to send a message to the local interface /// @param cli Current client handle /// @param buffer The message buffer diff --git a/app/src/incphub/src/incphub_client.c b/app/src/incphub/src/incphub_client.c index c64826e..e135c97 100644 --- a/app/src/incphub/src/incphub_client.c +++ b/app/src/incphub/src/incphub_client.c @@ -42,6 +42,23 @@ int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t leng return INCPHUB_OK; } +int incphub_release_msg_buffer(incphub_cli_t *cli, uint8_t *buffer) +{ + if (cli == NULL) + return INCPHUB_EINVAL; + if (buffer == NULL) + return INCPHUB_EINVAL; + + // cli requested for interface consistency only (for now) + (void)cli; + + // Unwind buffer pointer + uint8_t *unwound = buffer - sizeof(nil_header_t) + sizeof(ntl_header_t); + + // Release correct buffer pointer + return release_msg_buffer(buffer); +} + int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port) { if (cli == NULL) diff --git a/app/src/incphub/src/incphub_shared.c b/app/src/incphub/src/incphub_shared.c index 3bc42d3..58ba719 100644 --- a/app/src/incphub/src/incphub_shared.c +++ b/app/src/incphub/src/incphub_shared.c @@ -38,6 +38,8 @@ static incphub_cli_t *find_empty_client(); static incphub_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 +static int find_index_of_buffer(uint8_t *buffer); @@ -111,6 +113,38 @@ int get_msg_buffer(uint8_t **buffer_ptr, size_t len) return INCPHUB_OK; } +int release_msg_buffer(uint8_t *buffer) +{ + if (buffer == NULL) + return INCPHUB_EINVAL; + + // == Enter == + if (!global_lock(portMAX_DELAY)) + return INCPHUB_EAGAIN; + + int index = find_index_of_buffer(buffer); + + // Handle invalid buffers + if (index == -1) + { + global_unlock(); + return INCPHUB_EINVAL; + } + + // Handle not-in-use buffers + if (_msg_buffer_used[index] == false) + { + global_unlock(); + return INCPHUB_EINVAL; + } + + // Mark buffer as free + _msg_buffer_used[index] = false; + + global_unlock(); + return INCPHUB_OK; +} + int get_client_with_port(ntl_port_t port, incphub_cli_t **client_ptr) { if (port == NTL_PORT_EMPTY) @@ -195,3 +229,14 @@ static uint8_t *find_unused_buffer() return NULL; } + +static int find_index_of_buffer(uint8_t *buffer) +{ + for (int i = 0; i < INCPHUB_MSG_BUFFER_COUNT; ++i) + { + if (buffer = _msg_buffers[i]) + return i; + } + + return -1; +} diff --git a/app/src/incphub/src/incphub_shared.h b/app/src/incphub/src/incphub_shared.h index 65c0eda..6b0a72a 100644 --- a/app/src/incphub/src/incphub_shared.h +++ b/app/src/incphub/src/incphub_shared.h @@ -46,6 +46,11 @@ int register_client(incphub_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task) /// @return INCPHUB_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 +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