Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/common/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ typedef uint64_t absolute_time_t;
typedef unsigned int uint;
typedef struct
{
uint8_t _dummy;
// Data stored in the queue, i.e. a generic byte buffer.
uint8_t *data;

// The size of each element in the queue (in bytes).
unsigned int element_size;
Comment thread
megargayu marked this conversation as resolved.

// The capacity of the queue (in number of elements).
unsigned int element_count;

// The index of the head of the queue (where elements are removed from).
unsigned int head;

// The index of the tail of the queue (where elements are added to).
unsigned int tail;

// The current number of elements in the queue.
unsigned int level;
Comment thread
megargayu marked this conversation as resolved.
} queue_t;
#endif
8 changes: 8 additions & 0 deletions src/slate/slate.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ void free_slate(slate_t *slate)
{
// Free the filesys buffer if it was allocated - note free(NULL) is a no-op
// as per C specification, so this is safe even if allocation failed.
if (slate != NULL)
{
queue_free(&slate->payload_command_data);
queue_free(&slate->tx_queue);
queue_free(&slate->rx_queue);
queue_free(&slate->rpi_uart_queue);
Comment thread
megargayu marked this conversation as resolved.
}

free(slate->filesys_buffer);
slate->filesys_buffer = NULL;
}
Expand Down
8 changes: 4 additions & 4 deletions src/slate/slate.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ typedef struct samwise_slate
/*
* Command switch
*/
queue_t payload_command_data;
queue_t payload_command_data; // Initialized in command_task.c

uint8_t struct_buffer[MAX_DATASTRUCTURE_SIZE];

Expand All @@ -111,8 +111,8 @@ typedef struct samwise_slate
*/
rfm9x_t radio;
uint8_t radio_node;
queue_t tx_queue;
queue_t rx_queue;
queue_t tx_queue; // Initialized in radio_task.c
queue_t rx_queue; // Initialized in radio_task.c
uint32_t rx_bytes;
uint32_t rx_packets;
uint32_t rx_backpressure_drops;
Expand All @@ -123,7 +123,7 @@ typedef struct samwise_slate
/*
* RPi UART Communication
*/
queue_t rpi_uart_queue;
queue_t rpi_uart_queue; // Initialized in payload_uart.c
absolute_time_t rpi_uart_last_byte_receive_time;
int curr_command_seq_num;
bool is_payload_on;
Expand Down
57 changes: 51 additions & 6 deletions src/test_mocks/pico/util/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,80 @@

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "logger.h"
#include "typedefs.h"

// Mock queue functions that are no-ops in tests
static inline void queue_init(queue_t *q, unsigned int element_size,
unsigned int element_count)
{
q->element_size = element_size;
q->element_count = element_count;
q->head = 0;
q->tail = 0;
q->level = 0;
ASSERT(element_size > 0 && element_count > 0 &&
element_size * element_count < UINT32_MAX);
q->data = malloc(element_size * element_count);
ASSERT(q->data != NULL);
}
Comment thread
megargayu marked this conversation as resolved.

static inline bool queue_try_add(queue_t *q, void *data)
{
if (q->level >= q->element_count)
return false;

ASSERT(q->element_count > 0 && q->data != NULL);
memcpy(q->data + q->tail * q->element_size, data, q->element_size);
q->tail = (q->tail + 1) % q->element_count;
q->level++;
Comment thread
megargayu marked this conversation as resolved.
return true;
}

static inline bool queue_try_remove(queue_t *q, void *data)
{
return false;
if (q->level == 0)
return false;

ASSERT(q->element_count > 0 && q->data != NULL);
memcpy(data, q->data + q->head * q->element_size, q->element_size);
q->head = (q->head + 1) % q->element_count;
q->level--;
Comment thread
megargayu marked this conversation as resolved.
return true;
}

static inline bool queue_try_peek(queue_t *q, void *data)
{
return false;
if (q->level == 0)
Comment thread
megargayu marked this conversation as resolved.
return false;

ASSERT(q->element_count > 0 && q->data != NULL);
memcpy(data, q->data + q->head * q->element_size, q->element_size);
return true;
}

static inline bool queue_is_empty(queue_t *q)
{
return true;
return q->level == 0;
}

static inline bool queue_is_full(queue_t *q)
{
return false;
return q->level == q->element_count;
}

static inline unsigned int queue_get_level(queue_t *q)
{
return 0;
return q->level;
}

static inline void queue_free(queue_t *q)
{
free(q->data);
q->data = NULL;
q->level = 0;
q->head = 0;
q->tail = 0;
}
Loading