Skip to content

Commit 048d77a

Browse files
minor reorg
1 parent 423ae3e commit 048d77a

File tree

2 files changed

+82
-57
lines changed

2 files changed

+82
-57
lines changed

libcanard/canard.c

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,22 @@ typedef unsigned char byte_t;
6767

6868
#define TREE_NULL (canard_tree_t){ NULL, { NULL, NULL }, 0 }
6969

70-
typedef enum transfer_kind_t
70+
typedef enum format_t
7171
{
72-
transfer_kind_message = 0,
73-
transfer_kind_response = 1,
74-
transfer_kind_request = 2,
75-
transfer_kind_v0_message = 3,
76-
transfer_kind_v0_response = 4,
77-
transfer_kind_v0_request = 5,
78-
} transfer_kind_t;
79-
80-
static bool transfer_kind_is_v0(const transfer_kind_t kind)
72+
format_1v1_message = 0,
73+
format_1v0_message = 1,
74+
format_1v0_response = 2,
75+
format_1v0_request = 3,
76+
// v0.1
77+
format_0v1_message = 4,
78+
format_0v1_response = 5,
79+
format_0v1_request = 6,
80+
} format_t;
81+
static_assert(CANARD_FORMAT_COUNT == format_0v1_request + 1, "");
82+
83+
static bool format_is_v0(const format_t kind)
8184
{
82-
return (kind == transfer_kind_v0_message) || //
83-
(kind == transfer_kind_v0_request) || //
84-
(kind == transfer_kind_v0_response);
85+
return (kind == format_0v1_message) || (kind == format_0v1_request) || (kind == format_0v1_response);
8586
}
8687

8788
#define DLC_BITS 4U
@@ -834,49 +835,6 @@ static void tx_eject_pending(canard_t* const self, const byte_t iface_index)
834835
}
835836
}
836837

837-
bool canard_new(canard_t* const self,
838-
const canard_vtable_t* const vtable,
839-
const canard_mem_set_t memory,
840-
const size_t tx_queue_capacity,
841-
const uint_least8_t node_id,
842-
const uint64_t prng_seed,
843-
const size_t filter_count,
844-
canard_filter_t* const filter_storage)
845-
{
846-
bool ok = (self != NULL) && (vtable != NULL) && (vtable->now != NULL) && (vtable->tx != NULL) &&
847-
(vtable->filter != NULL) && mem_valid(memory.tx_transfer) && mem_valid(memory.tx_frame) &&
848-
mem_valid(memory.rx_session) && mem_valid(memory.rx_payload) &&
849-
((filter_count == 0U) || (filter_storage != NULL)) && (node_id <= CANARD_NODE_ID_MAX);
850-
if (ok) {
851-
(void)memset(self, 0, sizeof(*self));
852-
self->node_id = (node_id <= CANARD_NODE_ID_MAX) ? node_id : CANARD_NODE_ID_ANONYMOUS;
853-
self->tx.fd = true;
854-
self->tx.queue_capacity = tx_queue_capacity;
855-
self->rx.filter_count = filter_count;
856-
self->rx.filters = filter_storage;
857-
self->mem = memory;
858-
self->prng_state = prng_seed;
859-
self->vtable = vtable;
860-
self->unicast_sub.index_port_id = TREE_NULL;
861-
}
862-
return ok;
863-
}
864-
865-
void canard_destroy(canard_t* const self)
866-
{
867-
CANARD_ASSERT(self != NULL);
868-
for (size_t i = 0; i < (sizeof(self->rx.subscriptions) / sizeof(self->rx.subscriptions[0])); i++) {
869-
CANARD_ASSERT(self->rx.subscriptions[i] == NULL);
870-
}
871-
CANARD_ASSERT(self->rx.list_session_by_animation.head == NULL);
872-
CANARD_ASSERT(self->rx.list_session_by_animation.tail == NULL);
873-
while (self->tx.agewise.head != NULL) {
874-
canard_txfer_t* const tr = LIST_HEAD(self->tx.agewise, canard_txfer_t, list_agewise);
875-
txfer_retire(self, tr);
876-
}
877-
(void)memset(self, 0, sizeof(*self));
878-
}
879-
880838
void canard_poll(canard_t* const self, const uint_least8_t tx_ready_iface_bitmap)
881839
{
882840
if (self != NULL) {
@@ -1083,6 +1041,70 @@ bool canard_0v1_respond(canard_t* const self,
10831041
self, deadline, priority, data_type_id, crc_seed, client_node_id, false, transfer_id, payload, context);
10841042
}
10851043

1044+
// --------------------------------------------- RX ---------------------------------------------
1045+
1046+
/*
1047+
typedef struct
1048+
{
1049+
canard_prio_t priority;
1050+
format_t format;
1051+
uint32_t port_id;
1052+
uint_least8_t dst;
1053+
uint_least8_t src;
1054+
uint_least8_t transfer_id;
1055+
bool start;
1056+
bool end;
1057+
bool toggle;
1058+
} frame_t;
1059+
*/
1060+
1061+
// TODO rx impl
1062+
1063+
// --------------------------------------------- MISC ---------------------------------------------
1064+
1065+
bool canard_new(canard_t* const self,
1066+
const canard_vtable_t* const vtable,
1067+
const canard_mem_set_t memory,
1068+
const size_t tx_queue_capacity,
1069+
const uint_least8_t node_id,
1070+
const uint64_t prng_seed,
1071+
const size_t filter_count,
1072+
canard_filter_t* const filter_storage)
1073+
{
1074+
bool ok = (self != NULL) && (vtable != NULL) && (vtable->now != NULL) && (vtable->tx != NULL) &&
1075+
(vtable->filter != NULL) && mem_valid(memory.tx_transfer) && mem_valid(memory.tx_frame) &&
1076+
mem_valid(memory.rx_session) && mem_valid(memory.rx_payload) &&
1077+
((filter_count == 0U) || (filter_storage != NULL)) && (node_id <= CANARD_NODE_ID_MAX);
1078+
if (ok) {
1079+
(void)memset(self, 0, sizeof(*self));
1080+
self->node_id = (node_id <= CANARD_NODE_ID_MAX) ? node_id : CANARD_NODE_ID_ANONYMOUS;
1081+
self->tx.fd = true;
1082+
self->tx.queue_capacity = tx_queue_capacity;
1083+
self->rx.filter_count = filter_count;
1084+
self->rx.filters = filter_storage;
1085+
self->mem = memory;
1086+
self->prng_state = prng_seed;
1087+
self->vtable = vtable;
1088+
self->unicast_sub.index_port_id = TREE_NULL;
1089+
}
1090+
return ok;
1091+
}
1092+
1093+
void canard_destroy(canard_t* const self)
1094+
{
1095+
CANARD_ASSERT(self != NULL);
1096+
for (size_t i = 0; i < (sizeof(self->rx.subscriptions) / sizeof(self->rx.subscriptions[0])); i++) {
1097+
CANARD_ASSERT(self->rx.subscriptions[i] == NULL);
1098+
}
1099+
CANARD_ASSERT(self->rx.list_session_by_animation.head == NULL);
1100+
CANARD_ASSERT(self->rx.list_session_by_animation.tail == NULL);
1101+
while (self->tx.agewise.head != NULL) {
1102+
canard_txfer_t* const tr = LIST_HEAD(self->tx.agewise, canard_txfer_t, list_agewise);
1103+
txfer_retire(self, tr);
1104+
}
1105+
(void)memset(self, 0, sizeof(*self));
1106+
}
1107+
10861108
uint16_t canard_0v1_crc_seed_from_data_type_signature(const uint64_t data_type_signature)
10871109
{
10881110
uint16_t crc = CRC_INITIAL;

libcanard/canard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ extern "C"
7979
#define CANARD_MTU_CAN_CLASSIC 8U
8080
#define CANARD_MTU_CAN_FD 64U
8181

82+
/// Exposed only for type completeness.
83+
#define CANARD_FORMAT_COUNT 7
84+
8285
typedef struct canard_t canard_t;
8386

8487
/// Monotonic time in microseconds; the current time is never negative.
@@ -304,7 +307,7 @@ struct canard_t
304307

305308
struct
306309
{
307-
canard_tree_t* subscriptions[6];
310+
canard_tree_t* subscriptions[CANARD_FORMAT_COUNT];
308311
canard_list_t list_session_by_animation; ///< Oldest at the tail.
309312

310313
size_t filter_count;

0 commit comments

Comments
 (0)