130 lines
4.3 KiB
C
130 lines
4.3 KiB
C
// incphub_sharedl.h - Internal mechanisms shared by incphub and incphub_client
|
|
|
|
#ifndef INCPHUB_SHARED_H_
|
|
#define INCPHUB_SHARED_H_
|
|
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
#include "queue.h"
|
|
|
|
#include "nil/nil.h"
|
|
#include "ntl/ntl.h"
|
|
|
|
#include "incphub/incphub.h"
|
|
|
|
|
|
|
|
struct incphub_cli_t {
|
|
/// @remark If set to NTL_PORT_EMPTY denotes an empty client
|
|
ntl_port_t port;
|
|
TaskHandle_t task;
|
|
};
|
|
|
|
// Queue<uint8_t*> (queue of buffers)
|
|
extern QueueHandle_t incphub_ingest_queue;
|
|
|
|
#ifndef INCPHUB_CLIENT_H_
|
|
typedef struct incphub_cli_t incphub_cli_t;
|
|
#endif
|
|
|
|
|
|
|
|
/// @brief Initializes the internal state of incphub
|
|
/// @return INCPHUB_OK on success, an error otherwise
|
|
int internal_init();
|
|
|
|
/// @brief Registers a task as an incphub client
|
|
/// @param cli_ptr Location to return client
|
|
/// @param port The port to register to
|
|
/// @param task The task to register as the handler
|
|
/// @return INCPHUB_OK on success, an error otherwise
|
|
int register_client(incphub_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task);
|
|
|
|
/// @brief Attempts to get a free message buffer from the central storage space
|
|
/// @param buffer_ptr Location to return the buffer
|
|
/// @param len The minimum size, in bytes, of the buffer get
|
|
/// @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
|
|
/// @return INCPHUB_OK on success, an error otherwise
|
|
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, 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 a message, in ticks
|
|
/// @return INCPHUB_OK on success, an error otherwise
|
|
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);
|
|
|
|
/// @brief Blocks until a message is received for dispatch, or until a given timeout
|
|
/// @param buffer_ptr Location to return the message buffer
|
|
/// @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, 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
|