feat: Added message buffer releasing
This commit is contained in:
@@ -28,6 +28,12 @@ int incphub_init_client(incphub_cli_t **cli, ntl_port_t port);
|
|||||||
/// @return INCPHUB_OK on success, an error otherwise
|
/// @return INCPHUB_OK on success, an error otherwise
|
||||||
int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t length);
|
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
|
/// @brief Attempts to send a message to the local interface
|
||||||
/// @param cli Current client handle
|
/// @param cli Current client handle
|
||||||
/// @param buffer The message buffer
|
/// @param buffer The message buffer
|
||||||
|
|||||||
@@ -42,6 +42,23 @@ int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t leng
|
|||||||
return INCPHUB_OK;
|
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)
|
int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port)
|
||||||
{
|
{
|
||||||
if (cli == NULL)
|
if (cli == NULL)
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ static incphub_cli_t *find_empty_client();
|
|||||||
static incphub_cli_t *find_client_with_port(ntl_port_t port);
|
static incphub_cli_t *find_client_with_port(ntl_port_t port);
|
||||||
/// @remark Returns NULL if not found
|
/// @remark Returns NULL if not found
|
||||||
static uint8_t *find_unused_buffer();
|
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;
|
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)
|
int get_client_with_port(ntl_port_t port, incphub_cli_t **client_ptr)
|
||||||
{
|
{
|
||||||
if (port == NTL_PORT_EMPTY)
|
if (port == NTL_PORT_EMPTY)
|
||||||
@@ -195,3 +229,14 @@ static uint8_t *find_unused_buffer()
|
|||||||
|
|
||||||
return NULL;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
/// @return INCPHUB_OK on success, an error otherwise
|
||||||
int get_msg_buffer(uint8_t **buffer_ptr, size_t len);
|
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
|
/// @brief Attempts to get the client assigned to the given port
|
||||||
/// @param port The port to look for
|
/// @param port The port to look for
|
||||||
/// @param client_ptr Location to return the client
|
/// @param client_ptr Location to return the client
|
||||||
|
|||||||
Reference in New Issue
Block a user