feat: Draft architecture for incp hub
This commit is contained in:
10
app/src/CMakeLists.txt
Normal file
10
app/src/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
add_subdirectory(tasks)
|
||||
add_subdirectory(incphub)
|
||||
add_subdirectory(nil)
|
||||
add_subdirectory(ntl)
|
||||
|
||||
target_sources(${EXECUTABLE} PRIVATE
|
||||
main.c
|
||||
)
|
||||
|
||||
target_include_directories(${EXECUTABLE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
11
app/src/incp_fake/messages.h
Normal file
11
app/src/incp_fake/messages.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef INCP_FAKE_MESSAGES_H_
|
||||
#define INCP_FAKE_MESSAGES_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
void *content;
|
||||
size_t len;
|
||||
} incp_any_msg_t;
|
||||
|
||||
#endif
|
||||
7
app/src/incphub/CMakeLists.txt
Normal file
7
app/src/incphub/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
target_sources(${EXECUTABLE} PRIVATE
|
||||
src/incphub_client.c
|
||||
src/incphub_task.c
|
||||
src/incphub_shared.c
|
||||
)
|
||||
|
||||
target_include_directories(${EXECUTABLE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
19
app/src/incphub/README.md
Normal file
19
app/src/incphub/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# INCPHUB
|
||||
|
||||
## FreeRTOS requirements
|
||||
|
||||
- INCLUDE_xTaskGetCurrentTaskHandle set to 1
|
||||
- INCLUDE_vTaskSuspend set to 1
|
||||
- configUSE_TASK_NOTIFICATIONS set to 1
|
||||
- configTASK_NOTIFICATION_ARRAY_ENTRIES set to >= 2 (0 is for stream/message buffers, 1(configurable) is used by incphub)
|
||||
- configUSE_MUTEXES set to 1
|
||||
|
||||
## Configuration
|
||||
|
||||
Set as a macro in compile flags if wishing to override the defaults
|
||||
|
||||
- INCPHUB_TASKINDEX_MAILBOX, defaults to 1 (avoid using 0)
|
||||
|
||||
## Setup
|
||||
|
||||
- incphub_task_preinit MUST be run before any calls to the client, likely even before the scheduler is started
|
||||
22
app/src/incphub/include/incphub/incphub.h
Normal file
22
app/src/incphub/include/incphub/incphub.h
Normal file
@@ -0,0 +1,22 @@
|
||||
// incphub.h - Public definitions common to incphub and incphub client
|
||||
|
||||
#ifndef INCPHUB_H_
|
||||
#define INCPHUB_H_
|
||||
|
||||
#include "ntl/ntl.h"
|
||||
#include "nil/nil.h"
|
||||
|
||||
#define INCPHUB_OK (0)
|
||||
#define INCPHUB_ERR (1)
|
||||
#define INCPHUB_EINVAL (2)
|
||||
#define INCPHUB_ENOTFOUND (3)
|
||||
#define INCPHUB_EBUSY (4)
|
||||
#define INCPHUB_EAGAIN (5)
|
||||
#define INCPHUB_ENOMEM (6)
|
||||
|
||||
typedef struct {
|
||||
nil_addr_t addr;
|
||||
ntl_port_t port;
|
||||
} incphub_addr_t;
|
||||
|
||||
#endif
|
||||
47
app/src/incphub/include/incphub/incphub_client.h
Normal file
47
app/src/incphub/include/incphub/incphub_client.h
Normal file
@@ -0,0 +1,47 @@
|
||||
// incphub_client.h - Public interface of incphub client side
|
||||
|
||||
#ifndef INCPHUB_CLIENT_H_
|
||||
#define INCPHUB_CLIENT_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "incphub/incphub.h"
|
||||
|
||||
#include "nil/nil.h"
|
||||
#include "ntl/ntl.h"
|
||||
|
||||
|
||||
|
||||
typedef struct incphub_cli_t incphub_cli_t;
|
||||
|
||||
|
||||
|
||||
/// @brief Initializes an incphub client
|
||||
/// @param port The port number to assign to the caller
|
||||
/// @return INCPHUB_OK on success, an error otherwise
|
||||
int incphub_init_client(incphub_cli_t **cli, ntl_port_t port);
|
||||
|
||||
/// @brief Attempts to get a free message buffer from the central storage space
|
||||
/// @param cli Current client handle
|
||||
/// @param buffer_ptr Location to return the buffer
|
||||
/// @param length The minimum size, in bytes, of the buffer get
|
||||
/// @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 Attempts to send a message to the local interface
|
||||
/// @param cli Current client handle
|
||||
/// @param buffer The message buffer
|
||||
/// @param length The message length
|
||||
/// @param dst_port The destination NTL port
|
||||
/// @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);
|
||||
|
||||
/// @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 sender_ptr Location to return the message length
|
||||
/// @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 *sender_ptr);
|
||||
|
||||
#endif
|
||||
25
app/src/incphub/include/incphub/incphub_task.h
Normal file
25
app/src/incphub/include/incphub/incphub_task.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef INCPHUB_TASK_H_
|
||||
#define INCPHUB_TASK_H_
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
|
||||
#define INCPHUB_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
|
||||
#define INCPHUB_TASK_PRIORITY (configMAX_PRIORITIES - 1)
|
||||
|
||||
extern StackType_t _incphub_task_stack[INCPHUB_TASK_STACK_DEPTH];
|
||||
extern StaticTask_t _incphub_task;
|
||||
|
||||
void incphub_task_preinit();
|
||||
void incphub_task_main(void *params);
|
||||
|
||||
|
||||
|
||||
// === For IncpHub client ===
|
||||
|
||||
#include "queue.h"
|
||||
|
||||
extern QueueHandle_t incphub_ingest_queue;
|
||||
|
||||
#endif
|
||||
74
app/src/incphub/src/incphub_client.c
Normal file
74
app/src/incphub/src/incphub_client.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "incphub/incphub_client.h"
|
||||
#include "incphub/incphub_task.h"
|
||||
#include "incphub/incphub.h"
|
||||
#include "incphub_config.h"
|
||||
#include "incphub_shared.h"
|
||||
|
||||
#include "ntl/ntl.h"
|
||||
#include "nil/nil.h"
|
||||
|
||||
#include "message_buffer.h"
|
||||
|
||||
|
||||
|
||||
int incphub_init_client(incphub_cli_t **cli, ntl_port_t port)
|
||||
{
|
||||
return register_client(cli, port, xTaskGetCurrentTaskHandle());
|
||||
}
|
||||
|
||||
int incphub_get_msg_buffer(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t length)
|
||||
{
|
||||
if (cli == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
if (buffer_ptr == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
// Request extra space for NTL header
|
||||
size_t extended_len = sizeof(nil_header_t) + sizeof(ntl_header_t) + length;
|
||||
uint8_t *buf;
|
||||
|
||||
int status = get_msg_buffer(&buf, extended_len);
|
||||
if (status != INCPHUB_OK)
|
||||
return status;
|
||||
|
||||
// Return buffer
|
||||
*buffer_ptr = buf;
|
||||
|
||||
return INCPHUB_OK;
|
||||
}
|
||||
|
||||
int incphub_send_local(incphub_cli_t *cli, uint8_t *buffer, size_t length, ntl_port_t dst_port)
|
||||
{
|
||||
if (cli == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
if (buffer == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
if (dst_port == NTL_PORT_EMPTY)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
// Unwind pointer
|
||||
ntl_packet_t *ntl_packet = (ntl_packet_t*)(buffer - sizeof(ntl_header_t));
|
||||
nil_packet_t *nil_packet = (nil_packet_t*)(ntl_packet - sizeof(ntl_header_t));
|
||||
size_t extended_len = sizeof(nil_header_t) + sizeof(ntl_header_t) + length;
|
||||
|
||||
// Populate NTL header
|
||||
ntl_build_header(&ntl_packet->header, dst_port, cli->port, length);
|
||||
|
||||
// Populate NIL header
|
||||
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, extended_len);
|
||||
}
|
||||
|
||||
int incphub_await_msg(incphub_cli_t *cli, uint8_t **buffer_ptr, size_t *length_ptr, incphub_addr_t *sender_ptr)
|
||||
{
|
||||
(void)cli;
|
||||
(void)buffer_ptr;
|
||||
(void)length_ptr;
|
||||
(void)sender_ptr;
|
||||
//TODO: Implement
|
||||
return INCPHUB_ERR;
|
||||
}
|
||||
30
app/src/incphub/src/incphub_config.h
Normal file
30
app/src/incphub/src/incphub_config.h
Normal file
@@ -0,0 +1,30 @@
|
||||
// incphub_config.h - Default configurations for incphub
|
||||
|
||||
#ifndef INCPHUB_CONFIG_H_
|
||||
#define INCPHUB_CONFIG_H_
|
||||
|
||||
#ifndef INCPHUB_TASKINDEX_MAILBOX
|
||||
/// @brief Index of direct-to-task notification for mailbox to use
|
||||
#define INCPHUB_TASKINDEX_MAILBOX 1
|
||||
#endif
|
||||
|
||||
#ifndef INCPHUB_MAX_CLIENTS
|
||||
/// @brief Number of client slots to allocate
|
||||
#define INCPHUB_MAX_CLIENTS 16
|
||||
#endif
|
||||
|
||||
#ifndef INCPHUB_MSG_BUFFER_SIZE
|
||||
/// @brief Message buffer size in bytes. Must allow space for NIL and NTL headers
|
||||
#define INCPHUB_MSG_BUFFER_SIZE 32
|
||||
#endif
|
||||
|
||||
#ifndef INCPHUB_MSG_BUFFER_COUNT
|
||||
/// @brief Number of message buffers to allocate
|
||||
#define INCPHUB_MSG_BUFFER_COUNT 8
|
||||
#endif
|
||||
|
||||
#ifndef INCPHUB_LOCAL_NIL_ADDR
|
||||
#error "Local NIL address must be specified with INCPHUB_LOCAL_NIL_ADDR"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
201
app/src/incphub/src/incphub_shared.c
Normal file
201
app/src/incphub/src/incphub_shared.c
Normal file
@@ -0,0 +1,201 @@
|
||||
#include "incphub_shared.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
#include "task.h"
|
||||
|
||||
#include "incphub/incphub.h"
|
||||
#include "incphub_config.h"
|
||||
|
||||
#include "nil/nil.h"
|
||||
|
||||
|
||||
// === Private variables ===
|
||||
|
||||
// Global lock
|
||||
static SemaphoreHandle_t _incphub_lock;
|
||||
static StaticSemaphore_t _incphub_lock_store;
|
||||
|
||||
// Clients
|
||||
static incphub_cli_t _clients[INCPHUB_MAX_CLIENTS];
|
||||
|
||||
// Message buffers
|
||||
// INCPHUB_MSG_BUFFER_COUNT buffers of INCPHUB_MSG_BUFFER_SIZE bytes
|
||||
static uint8_t _msg_buffers[INCPHUB_MSG_BUFFER_COUNT][INCPHUB_MSG_BUFFER_SIZE];
|
||||
static bool _msg_buffer_used[INCPHUB_MSG_BUFFER_COUNT];
|
||||
|
||||
|
||||
|
||||
// === Private function declarations ===
|
||||
static bool public_enter(TickType_t maxDelay);
|
||||
static void public_exit();
|
||||
static incphub_cli_t *find_empty_client();
|
||||
static incphub_cli_t *find_client_with_port(ntl_port_t port);
|
||||
static uint8_t *find_unused_buffer();
|
||||
|
||||
|
||||
|
||||
// === Public functions ===
|
||||
int internal_init()
|
||||
{
|
||||
_incphub_lock = xSemaphoreCreateMutexStatic(&_incphub_lock_store);
|
||||
memset(_clients, 0, sizeof(_clients));
|
||||
memset(_msg_buffer_used, 0, sizeof(_msg_buffer_used));
|
||||
return INCPHUB_OK;
|
||||
}
|
||||
|
||||
int register_client(incphub_cli_t **cli_ptr, ntl_port_t port, TaskHandle_t task)
|
||||
{
|
||||
if (port == NTL_PORT_EMPTY)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
if (cli_ptr == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
// == Enter ==
|
||||
if (!public_enter(portMAX_DELAY))
|
||||
return INCPHUB_EAGAIN;
|
||||
|
||||
// Prevent double binding of ports
|
||||
if (find_client_with_port(port) != NULL)
|
||||
{
|
||||
public_exit();
|
||||
return INCPHUB_EBUSY;
|
||||
}
|
||||
|
||||
incphub_cli_t *client = find_empty_client();
|
||||
if (client == NULL)
|
||||
{
|
||||
public_exit();
|
||||
return INCPHUB_EBUSY;
|
||||
}
|
||||
|
||||
// Populate client data
|
||||
client->port = port;
|
||||
client->task = task;
|
||||
|
||||
// Save to passed pointer
|
||||
*cli_ptr = client;
|
||||
|
||||
public_exit();
|
||||
return INCPHUB_OK;
|
||||
}
|
||||
|
||||
int get_msg_buffer(uint8_t **buffer_ptr, size_t len)
|
||||
{
|
||||
if (buffer_ptr == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
// Ensure space for NIL header and payload
|
||||
if ((sizeof(nil_header_t) + len) > INCPHUB_MSG_BUFFER_SIZE)
|
||||
return INCPHUB_ENOMEM;
|
||||
|
||||
// == Enter ==
|
||||
if (!public_enter(portMAX_DELAY))
|
||||
return INCPHUB_EAGAIN;
|
||||
|
||||
//TODO: Allow buffer to be given by another layer (below NIL)
|
||||
uint8_t *buf = find_unused_buffer();
|
||||
if (buf == NULL)
|
||||
{
|
||||
public_exit();
|
||||
return INCPHUB_ENOMEM;
|
||||
}
|
||||
|
||||
// Reserve space for NIL header
|
||||
buf += sizeof(nil_header_t);
|
||||
|
||||
// Return buffer
|
||||
*buffer_ptr = buf;
|
||||
|
||||
public_exit();
|
||||
return INCPHUB_OK;
|
||||
}
|
||||
|
||||
int get_client_with_port(ntl_port_t port, incphub_cli_t **client_ptr)
|
||||
{
|
||||
if (port == NTL_PORT_EMPTY)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
if (client_ptr == NULL)
|
||||
return INCPHUB_EINVAL;
|
||||
|
||||
// == Enter ==
|
||||
if (!public_enter(portMAX_DELAY))
|
||||
return INCPHUB_EAGAIN;
|
||||
|
||||
// Map port to client
|
||||
incphub_cli_t *client = find_client_with_port(port);
|
||||
if (client == NULL)
|
||||
{
|
||||
public_exit();
|
||||
return INCPHUB_ENOTFOUND;
|
||||
}
|
||||
|
||||
// Return client
|
||||
*client_ptr = client;
|
||||
|
||||
public_exit();
|
||||
return INCPHUB_OK;
|
||||
}
|
||||
|
||||
int enqueue_message_ingest(uint8_t *buffer, size_t length)
|
||||
{
|
||||
(void)buffer;
|
||||
(void)length;
|
||||
//TODO: Implement
|
||||
return INCPHUB_ERR;
|
||||
}
|
||||
|
||||
int dequeue_message_ingest(uint8_t **buffer_ptr, size_t *length_ptr)
|
||||
{
|
||||
(void)buffer_ptr;
|
||||
(void)length_ptr;
|
||||
//TODO: Implement
|
||||
return INCPHUB_ERR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// === Private function definitions ===
|
||||
static bool public_enter(TickType_t maxDelay)
|
||||
{
|
||||
return xSemaphoreTake(_incphub_lock, maxDelay) == pdTRUE;
|
||||
}
|
||||
|
||||
static void public_exit()
|
||||
{
|
||||
xSemaphoreGive(_incphub_lock);
|
||||
}
|
||||
|
||||
static incphub_cli_t *find_empty_client()
|
||||
{
|
||||
return find_client_with_port(NTL_PORT_EMPTY);
|
||||
}
|
||||
|
||||
static incphub_cli_t *find_client_with_port(ntl_port_t port)
|
||||
{
|
||||
for (int i = 0; i < INCPHUB_MAX_CLIENTS; ++i)
|
||||
{
|
||||
incphub_cli_t *client = &_clients[i];
|
||||
|
||||
if (client->port == port)
|
||||
return client;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static uint8_t *find_unused_buffer()
|
||||
{
|
||||
for (int i = 0; i < INCPHUB_MSG_BUFFER_COUNT; ++i)
|
||||
{
|
||||
if (!_msg_buffer_used[i])
|
||||
return _msg_buffers[i];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
61
app/src/incphub/src/incphub_shared.h
Normal file
61
app/src/incphub/src/incphub_shared.h
Normal file
@@ -0,0 +1,61 @@
|
||||
// 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 "nil/nil.h"
|
||||
#include "ntl/ntl.h"
|
||||
|
||||
|
||||
|
||||
struct incphub_cli_t {
|
||||
/// @remark If set to NTL_PORT_EMPTY denotes an empty client
|
||||
ntl_port_t port;
|
||||
TaskHandle_t task;
|
||||
};
|
||||
|
||||
#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 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 length The message length
|
||||
/// @return INCPHUB_OK on success, an error otherwise
|
||||
int enqueue_message_ingest(uint8_t *buffer, size_t length);
|
||||
|
||||
/// @brief Dequeues a message for ingest
|
||||
/// @param buffer_ptr Location to return the buffer
|
||||
/// @param length_ptr Location to store the message length
|
||||
/// @return INCPHUB_OK on success, an error otherwise
|
||||
int dequeue_message_ingest(uint8_t **buffer_ptr, size_t *length_ptr);
|
||||
|
||||
#endif
|
||||
138
app/src/incphub/src/incphub_task.c
Normal file
138
app/src/incphub/src/incphub_task.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include "incphub/incphub_task.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "queue.h"
|
||||
|
||||
#include "incphub/incphub.h"
|
||||
#include "incphub_config.h"
|
||||
#include "incphub_shared.h"
|
||||
|
||||
#include "ntl/ntl.h"
|
||||
#include "nil/nil.h"
|
||||
|
||||
|
||||
|
||||
// === Static task space ===
|
||||
StackType_t _incphub_task_stack[INCPHUB_TASK_STACK_DEPTH];
|
||||
StaticTask_t _incphub_task;
|
||||
|
||||
|
||||
|
||||
// === Public variables ===
|
||||
|
||||
// Queue<nil_packet_t*> Packets awaiting to be accepted by incphub
|
||||
QueueHandle_t incphub_ingest_queue;
|
||||
|
||||
|
||||
|
||||
// === Private variables ===
|
||||
#define INGEST_QUEUE_LENGTH (16)
|
||||
#define INGEST_QUEUE_ITEM_SIZE (sizeof(ntl_packet_t *))
|
||||
static uint8_t _ingest_queue_store[INGEST_QUEUE_LENGTH * INGEST_QUEUE_ITEM_SIZE];
|
||||
static StaticQueue_t _ingest_queue_static;
|
||||
|
||||
|
||||
|
||||
// === Private functions ===
|
||||
static nil_packet_t *extract_nil_packet(uint8_t *buffer, size_t length);
|
||||
static ntl_packet_t *extract_ntl_packet(nil_packet_t *nil);
|
||||
static void handle_nil_packet(nil_packet_t *nil);
|
||||
static void handle_ntl_packet(ntl_packet_t *ntl);
|
||||
|
||||
|
||||
|
||||
// === Public functions ===
|
||||
void incphub_task_preinit()
|
||||
{
|
||||
internal_init();
|
||||
incphub_ingest_queue =
|
||||
xQueueCreateStatic(INGEST_QUEUE_LENGTH, INGEST_QUEUE_ITEM_SIZE, _ingest_queue_store, &_ingest_queue_static);
|
||||
}
|
||||
|
||||
void incphub_task_main(void *params)
|
||||
{
|
||||
(void)params;
|
||||
|
||||
while (1)
|
||||
{
|
||||
uint8_t *buffer;
|
||||
size_t length;
|
||||
dequeue_message_ingest(&buffer, &length);
|
||||
|
||||
nil_packet_t *nil = extract_nil_packet(buffer, length);
|
||||
if (nil == NULL)
|
||||
{
|
||||
//TODO: Error
|
||||
continue;
|
||||
}
|
||||
|
||||
handle_nil_packet(nil);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// === Private functions ===
|
||||
static nil_packet_t *extract_nil_packet(uint8_t *buffer, size_t length)
|
||||
{
|
||||
if (length < sizeof(nil_packet_t))
|
||||
{
|
||||
//TODO: Error
|
||||
return NULL;
|
||||
}
|
||||
|
||||
nil_packet_t *nil = (nil_packet_t *)buffer;
|
||||
if (length < sizeof(nil_packet_t) + nil->header.len )
|
||||
{
|
||||
//TODO: Error
|
||||
return NULL;;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
static ntl_packet_t *extract_ntl_packet(nil_packet_t *nil)
|
||||
{
|
||||
if (nil->header.len < sizeof(ntl_packet_t))
|
||||
{
|
||||
//TODO: Error
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ntl_packet_t *ntl = (ntl_packet_t *)nil->payload;
|
||||
|
||||
if (nil->header.len < sizeof(ntl_packet_t) + ntl->header.len)
|
||||
{
|
||||
//TODO: Error
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ntl;
|
||||
}
|
||||
|
||||
static void handle_nil_packet(nil_packet_t *nil)
|
||||
{
|
||||
if (nil->header.dst_addr == INCPHUB_LOCAL_NIL_ADDR)
|
||||
{
|
||||
ntl_packet_t *ntl = extract_ntl_packet(nil);
|
||||
if (ntl == NULL)
|
||||
{
|
||||
//TODO: Error
|
||||
return;
|
||||
}
|
||||
|
||||
handle_ntl_packet(ntl);
|
||||
return;
|
||||
}
|
||||
|
||||
//TODO: Route to external interface
|
||||
return;
|
||||
}
|
||||
|
||||
static void handle_ntl_packet(ntl_packet_t *ntl)
|
||||
{
|
||||
(void)ntl;
|
||||
//TODO: Implement
|
||||
}
|
||||
0
app/src/incphub/src/incphub_task_internal.h
Normal file
0
app/src/incphub/src/incphub_task_internal.h
Normal file
22
app/src/main.c
Normal file
22
app/src/main.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "main.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
#include "tasks/heartbeat_task.h"
|
||||
#include "incphub/incphub_task.h"
|
||||
|
||||
int my_main()
|
||||
{
|
||||
// Init
|
||||
incphub_task_preinit();
|
||||
|
||||
// Create tasks
|
||||
xTaskCreateStatic(heartbeat_task_main, "heartbeat", HEARTBEAT_TASK_STACK_DEPTH, NULL, HEARTBEAT_TASK_PRIORITY, _heartbeat_task_stack, &_heartbeat_task);
|
||||
xTaskCreateStatic(incphub_task_main, "incphub", INCPHUB_TASK_STACK_DEPTH, NULL, INCPHUB_TASK_PRIORITY, _incphub_task_stack, &_incphub_task);
|
||||
|
||||
// Run
|
||||
vTaskStartScheduler();
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
app/src/nil/CMakeLists.txt
Normal file
1
app/src/nil/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_include_directories(${EXECUTABLE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
3
app/src/nil/README.md
Normal file
3
app/src/nil/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# NIL
|
||||
|
||||
Nanosatlab Internet Layer (from IP model)
|
||||
87
app/src/nil/include/nil/nil.h
Normal file
87
app/src/nil/include/nil/nil.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#ifndef NIL_H_
|
||||
#define NIL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
// === Type Aliases ===
|
||||
|
||||
typedef uint8_t nil_addr_t;
|
||||
typedef uint32_t ntl_chk_t;
|
||||
|
||||
|
||||
|
||||
// === Special Port Values ===
|
||||
|
||||
#define NIL_ADDR_LOCALHOST ((nil_addr_t)0)
|
||||
|
||||
|
||||
|
||||
// === Special Check Values ===
|
||||
|
||||
/// @brief Denotes no check value to validate
|
||||
#define NIL_CHK_EMPTY ((ntl_chk_t)0)
|
||||
|
||||
|
||||
|
||||
// === Struct Definitions ===
|
||||
|
||||
/// @brief Packed header format
|
||||
typedef struct __attribute__((packed, aligned(4))) {
|
||||
nil_addr_t dst_addr;
|
||||
nil_addr_t src_addr;
|
||||
uint16_t len;
|
||||
ntl_chk_t chk;
|
||||
} nil_header_t;
|
||||
|
||||
/// @brief An NIL packet (packed) with header and payload
|
||||
typedef struct __attribute__((packed, aligned(4))) {
|
||||
nil_header_t header;
|
||||
uint8_t payload[];
|
||||
} nil_packet_t;
|
||||
|
||||
|
||||
|
||||
// === Convenience Functions ===
|
||||
|
||||
/// @brief Convert uint16 value from host to network byte order
|
||||
/// @param value The value in host order
|
||||
/// @return The value in network order
|
||||
static inline uint16_t nil_htons(uint16_t value)
|
||||
{
|
||||
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
|
||||
defined(__BIG_ENDIAN__) || \
|
||||
defined(__ARMEB__) || \
|
||||
defined(__THUMBEB__) || \
|
||||
defined(__AARCH64EB__) || \
|
||||
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
|
||||
return ((value << 8) & 0xFF) | ((value & 0xFF) << 8);
|
||||
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
|
||||
defined(__LITTLE_ENDIAN__) || \
|
||||
defined(__ARMEL__) || \
|
||||
defined(__THUMBEL__) || \
|
||||
defined(__AARCH64EL__) || \
|
||||
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
|
||||
return value;
|
||||
#else
|
||||
#error "Unable to detect byte endianness of platform."
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// @brief Populates an NIL header from the provided fields
|
||||
/// @param hdr The header to pupulate
|
||||
/// @param dst The destination address
|
||||
/// @param src The source address
|
||||
/// @param len The length of the NIL payload in bytes
|
||||
static inline void nil_build_header(volatile nil_header_t *hdr, nil_addr_t dst, nil_addr_t src, uint16_t len)
|
||||
{
|
||||
hdr->dst_addr = dst;
|
||||
hdr->src_addr = src;
|
||||
hdr->len = nil_htons(len);
|
||||
//REVIEW: Disabled for now, add a flag to set this to an actual check?
|
||||
hdr->chk = NIL_CHK_EMPTY;
|
||||
}
|
||||
|
||||
#endif
|
||||
1
app/src/ntl/CMakeLists.txt
Normal file
1
app/src/ntl/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_include_directories(${EXECUTABLE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||
7
app/src/ntl/README.md
Normal file
7
app/src/ntl/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# NTL
|
||||
|
||||
Nanosatlab Transport Layer (from IP model)
|
||||
|
||||
## Byte ordering
|
||||
|
||||
Multi-byte fields are sent in little-endian order. (Natural order for x86 and arm)
|
||||
96
app/src/ntl/include/ntl/ntl.h
Normal file
96
app/src/ntl/include/ntl/ntl.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#ifndef NTL_H_
|
||||
#define NTL_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
// === Type Aliases ===
|
||||
|
||||
/// @brief An NTL port number
|
||||
typedef uint8_t ntl_port_t;
|
||||
/// @brief An NTL check value
|
||||
typedef uint32_t ntl_chk_t;
|
||||
|
||||
|
||||
|
||||
// === Special Port Values ===
|
||||
|
||||
/// @brief Denotes an invalid port number
|
||||
#define NTL_PORT_EMPTY ((ntl_port_t)0)
|
||||
/// @brief The highest valid port number
|
||||
#define NTL_MAX_PORT (~(ntl_port_t)0)
|
||||
|
||||
|
||||
|
||||
// === Special Check Values ===
|
||||
|
||||
/// @brief Denotes no check value to validate
|
||||
#define NTL_CHK_EMPTY ((ntl_chk_t)0)
|
||||
|
||||
|
||||
|
||||
// === Struct Definitions ===
|
||||
|
||||
/// @brief Packed header format
|
||||
typedef struct __attribute__((packed, aligned(4))) {
|
||||
ntl_port_t dst_port;
|
||||
ntl_port_t src_port;
|
||||
uint16_t len;
|
||||
ntl_chk_t chk;
|
||||
} ntl_header_t;
|
||||
|
||||
/// @brief An NTL packet (packed) with header and payload
|
||||
typedef struct __attribute__((packed, aligned(4))) {
|
||||
ntl_header_t header;
|
||||
uint8_t payload[];
|
||||
} ntl_packet_t;
|
||||
|
||||
|
||||
|
||||
// === Convenience Functions ===
|
||||
|
||||
/// @brief Convert uint16 value from host to network byte order
|
||||
/// @param value The value in host order
|
||||
/// @return The value in network order
|
||||
static inline uint16_t ntl_htons(uint16_t value)
|
||||
{
|
||||
#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
|
||||
defined(__BIG_ENDIAN__) || \
|
||||
defined(__ARMEB__) || \
|
||||
defined(__THUMBEB__) || \
|
||||
defined(__AARCH64EB__) || \
|
||||
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
|
||||
return ((value << 8) & 0xFF) | ((value & 0xFF) << 8);
|
||||
#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
|
||||
defined(__LITTLE_ENDIAN__) || \
|
||||
defined(__ARMEL__) || \
|
||||
defined(__THUMBEL__) || \
|
||||
defined(__AARCH64EL__) || \
|
||||
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
|
||||
return value;
|
||||
#else
|
||||
#error "Unable to detect byte endianness of platform."
|
||||
#endif
|
||||
}
|
||||
|
||||
/// @brief Populates an NTL header from the provided fields
|
||||
/// @param hdr The header to pupulate
|
||||
/// @param dst The destination port
|
||||
/// @param src The source port
|
||||
/// @param len The length of the NTL payload in bytes
|
||||
static inline void ntl_build_header(volatile ntl_header_t *hdr, ntl_port_t dst, ntl_port_t src, uint16_t len)
|
||||
{
|
||||
hdr->dst_port = dst;
|
||||
hdr->src_port = src;
|
||||
hdr->len = ntl_htons(len);
|
||||
//REVIEW: Disabled for now, add a flag to set this to an actual check?
|
||||
hdr->chk = NTL_CHK_EMPTY;
|
||||
}
|
||||
|
||||
static inline ntl_port_t ntl_get_dst(const ntl_header_t *hdr) { return hdr->dst_port; }
|
||||
static inline ntl_port_t ntl_get_src(const ntl_header_t *hdr) { return hdr->src_port; }
|
||||
static inline ntl_port_t ntl_get_len(const ntl_header_t *hdr) { return ntl_htons(hdr->len); }
|
||||
static inline ntl_port_t ntl_get_chk(const ntl_header_t *hdr) { return hdr->chk; }
|
||||
|
||||
#endif
|
||||
4
app/src/tasks/CMakeLists.txt
Normal file
4
app/src/tasks/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
target_sources(${EXECUTABLE} PRIVATE
|
||||
tasks.c
|
||||
heartbeat_task.c
|
||||
)
|
||||
27
app/src/tasks/heartbeat_task.c
Normal file
27
app/src/tasks/heartbeat_task.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "heartbeat_task.h"
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_hal_gpio.h"
|
||||
#include "gpio.h"
|
||||
|
||||
|
||||
|
||||
StackType_t _heartbeat_task_stack[HEARTBEAT_TASK_STACK_DEPTH];
|
||||
StaticTask_t _heartbeat_task;
|
||||
|
||||
|
||||
|
||||
void heartbeat_task_main(void *params)
|
||||
{
|
||||
(void)params;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();
|
||||
|
||||
while (1)
|
||||
{
|
||||
xTaskDelayUntil(&last_wake, 298);
|
||||
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
|
||||
xTaskDelayUntil(&last_wake, 2);
|
||||
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
}
|
||||
16
app/src/tasks/heartbeat_task.h
Normal file
16
app/src/tasks/heartbeat_task.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _HEARTBEAT_TASK_H_
|
||||
#define _HEARTBEAT_TASK_H_
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
|
||||
#define HEARTBEAT_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
|
||||
#define HEARTBEAT_TASK_PRIORITY (configMAX_PRIORITIES - 1)
|
||||
|
||||
extern StackType_t _heartbeat_task_stack[HEARTBEAT_TASK_STACK_DEPTH];
|
||||
extern StaticTask_t _heartbeat_task;
|
||||
|
||||
void heartbeat_task_main(void *params);
|
||||
|
||||
#endif
|
||||
21
app/src/tasks/tasks.c
Normal file
21
app/src/tasks/tasks.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
static StaticTask_t xIdleTaskTCB;
|
||||
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
|
||||
|
||||
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
|
||||
StackType_t **ppxIdleTaskStackBuffer,
|
||||
size_t *pulIdleTaskStackSize)
|
||||
{
|
||||
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
|
||||
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
|
||||
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
|
||||
}
|
||||
|
||||
void vApplicationStackOverflowHook(TaskHandle_t pxTask, char *pcTaskName)
|
||||
{
|
||||
(void)pxTask;
|
||||
(void)pcTaskName;
|
||||
while (1); //TODO: Better handling (external feedback?)
|
||||
}
|
||||
Reference in New Issue
Block a user