Skip to content

Commit d43d242

Browse files
fixes & tests
1 parent 474253e commit d43d242

File tree

4 files changed

+348
-800
lines changed

4 files changed

+348
-800
lines changed

libcanard/canard.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ typedef struct tx_frame_t
420420
{
421421
struct tx_frame_t* next;
422422
size_t refcount : (sizeof(size_t) * CHAR_BIT) - DLC_BITS; ///< 268+ million ought to be enough for anybody
423-
size_t dlc : DLC_BITS; ///< use canard_len_to_dlc[] and canard_dlc_to_len[]
423+
size_t dlc : DLC_BITS; ///< use canard_len_to_dlc[] and canard_dlc_to_len[]
424424
byte_t data[];
425425
} tx_frame_t;
426426
static_assert((sizeof(void*) > 4) || ((sizeof(tx_frame_t) + CANARD_MTU_CAN_CLASSIC) <= 24),
@@ -499,8 +499,8 @@ struct canard_txfer_t
499499
/// Constant transfer properties supplied by the client.
500500
canard_us_t deadline;
501501
uint64_t topic_hash;
502-
uint64_t can_id_msb : CAN_ID_MSb_BITS;
503-
uint64_t transfer_id : CANARD_TRANSFER_ID_BIT_LENGTH;
502+
uint64_t can_id_msb : CAN_ID_MSb_BITS;
503+
uint64_t transfer_id : CANARD_TRANSFER_ID_BIT_LENGTH;
504504
uint64_t is_1v1_message : 1; ///< Needs delayed subject-ID resolution.
505505
uint64_t fd : 1;
506506

@@ -620,22 +620,19 @@ static void tx_stage_reliable_if(canard_t* const self, canard_txfer_t* const tr)
620620
const canard_us_t timeout = tx_ack_timeout(self->ack_baseline_timeout, txfer_prio(tr), epoch);
621621
canard_us_t new_staged_until = txfer_staged_until(tr);
622622
if (new_staged_until == tr->deadline) {
623-
CANARD_ASSERT(!cavl2_is_inserted(self->tx.reliable, &tr->index_reliable));
623+
CANARD_ASSERT(!cavl2_is_inserted(self->tx.staged, &tr->index_staged));
624624
new_staged_until = self->vtable->now(self); // this is the first attempt
625625
} else {
626-
CANARD_ASSERT(cavl2_is_inserted(self->tx.reliable, &tr->index_reliable));
627-
cavl2_remove(&self->tx.reliable, &tr->index_reliable);
626+
CANARD_ASSERT(cavl2_is_inserted(self->tx.staged, &tr->index_staged));
627+
cavl2_remove(&self->tx.staged, &tr->index_staged);
628628
}
629629
new_staged_until += timeout;
630630
if ((tr->deadline - timeout) >= new_staged_until) {
631631
const canard_us_t delta = min_i64(tr->deadline - new_staged_until, (canard_us_t)STAGED_UNTIL_DELTA_MAX);
632632
CANARD_ASSERT(delta > 0);
633633
tr->staged_until_delta = ((uint64_t)delta) & STAGED_UNTIL_DELTA_MAX;
634-
const canard_tree_t* const tree = cavl2_find_or_insert(&self->tx.reliable,
635-
&new_staged_until,
636-
tx_cavl_compare_staged_until,
637-
&tr->index_staged,
638-
cavl2_trivial_factory);
634+
const canard_tree_t* const tree = cavl2_find_or_insert(
635+
&self->tx.staged, &new_staged_until, tx_cavl_compare_staged_until, &tr->index_staged, cavl2_trivial_factory);
639636
CANARD_ASSERT(tree == &tr->index_staged);
640637
(void)tree;
641638
}

tests/src/test_api_tx.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,28 @@ static void test_canard_0v1_publish_requires_node_id(void)
5151
TEST_ASSERT_FALSE(canard_0v1_publish(&self, 0, 1, canard_prio_nominal, 1, 0xFFFF, 0, payload));
5252
}
5353

54+
// Provide a subject-ID that is out of range.
55+
static uint32_t bad_subject_id(canard_t* const self, const canard_user_context_t context)
56+
{
57+
(void)self;
58+
(void)context;
59+
return CANARD_SUBJECT_ID_MAX + 1U;
60+
}
61+
62+
// Reject unpinned publish when subject-ID resolution fails.
63+
static void test_canard_publish_subject_id_out_of_range(void)
64+
{
65+
canard_t self = {};
66+
canard_vtable_t vtable = {};
67+
vtable.tx_subject_id = bad_subject_id;
68+
self.vtable = &vtable;
69+
70+
const canard_bytes_chain_t payload = { .bytes = { .size = 0, .data = NULL }, .next = NULL };
71+
const uint64_t topic = (uint64_t)CANARD_SUBJECT_ID_MAX_1v0 + 1U;
72+
TEST_ASSERT_FALSE(
73+
canard_publish(&self, 0, 1, canard_prio_nominal, topic, 0, payload, CANARD_USER_CONTEXT_NULL, false));
74+
}
75+
5476
extern "C" void setUp() {}
5577
extern "C" void tearDown() {}
5678

@@ -62,6 +84,7 @@ int main()
6284
RUN_TEST(test_canard_publish_validation);
6385
RUN_TEST(test_canard_publish_oom);
6486
RUN_TEST(test_canard_0v1_publish_requires_node_id);
87+
RUN_TEST(test_canard_publish_subject_id_out_of_range);
6588

6689
return UNITY_END();
6790
}

0 commit comments

Comments
 (0)