Skip to content

Commit 5a1823d

Browse files
misc
1 parent 5f03f25 commit 5a1823d

File tree

4 files changed

+42
-67
lines changed

4 files changed

+42
-67
lines changed

libcanard/canard.c

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,6 @@ uint_least8_t canard_pending_ifaces(const canard_t* const self)
899899
return out;
900900
}
901901

902-
// Common Cyphal v1.0 service TX path for both request and response.
903902
static bool tx_1v0_service(canard_t* const self,
904903
const canard_us_t deadline,
905904
const canard_prio_t priority,
@@ -908,7 +907,19 @@ static bool tx_1v0_service(canard_t* const self,
908907
const bool request_not_response,
909908
const uint_least8_t transfer_id,
910909
const canard_bytes_chain_t payload,
911-
const canard_user_context_t context);
910+
const canard_user_context_t context)
911+
{
912+
bool ok = (self != NULL) && (priority < CANARD_PRIO_COUNT) && bytes_chain_valid(payload) &&
913+
(service_id <= CANARD_SERVICE_ID_MAX) && (destination_node_id <= CANARD_NODE_ID_MAX);
914+
if (ok) {
915+
const uint32_t can_id = (((uint32_t)priority) << PRIO_SHIFT) | (UINT32_C(1) << 25U) |
916+
(request_not_response ? (UINT32_C(1) << 24U) : 0U) | (((uint32_t)service_id) << 14U) |
917+
(((uint32_t)destination_node_id) << 7U);
918+
canard_txfer_t* const tr = txfer_new(self, deadline, transfer_id, can_id, self->tx.fd, context);
919+
ok = (tr != NULL) && tx_push(self, tr, false, CANARD_IFACE_BITMAP_ALL, payload, CRC_INITIAL);
920+
}
921+
return ok;
922+
}
912923

913924
bool canard_publish(canard_t* const self,
914925
const canard_us_t deadline,
@@ -938,23 +949,17 @@ bool canard_unicast(canard_t* const self,
938949
const canard_bytes_chain_t payload,
939950
const canard_user_context_t context)
940951
{
941-
// Keep transfer-ID map behavior: increment only if basic argument validation succeeds.
942-
bool ok = (self != NULL) && (priority < CANARD_PRIO_COUNT) && bytes_chain_valid(payload) &&
943-
(destination_node_id <= CANARD_NODE_ID_MAX);
944-
if (ok) {
945-
const byte_t tr_id = (byte_t)self->unicast_transfer_id[destination_node_id]++;
946-
// Per Cyphal v1.1, unicast is a service request with fixed service-ID 511.
947-
ok = tx_1v0_service(self,
948-
deadline,
949-
priority,
950-
(uint16_t)CANARD_SERVICE_ID_UNICAST,
951-
destination_node_id,
952-
true,
953-
tr_id,
954-
payload,
955-
context);
956-
}
957-
return ok;
952+
const bool ok = (self != NULL) && (priority < CANARD_PRIO_COUNT) && bytes_chain_valid(payload) &&
953+
(destination_node_id <= CANARD_NODE_ID_MAX);
954+
return ok && tx_1v0_service(self,
955+
deadline,
956+
priority,
957+
CANARD_SERVICE_ID_UNICAST,
958+
destination_node_id,
959+
true,
960+
self->unicast_transfer_id[destination_node_id]++,
961+
payload,
962+
context);
958963
}
959964

960965
bool canard_1v0_publish(canard_t* const self,
@@ -979,29 +984,6 @@ bool canard_1v0_publish(canard_t* const self,
979984
return ok;
980985
}
981986

982-
static bool tx_1v0_service(canard_t* const self,
983-
const canard_us_t deadline,
984-
const canard_prio_t priority,
985-
const uint16_t service_id,
986-
const uint_least8_t destination_node_id,
987-
const bool request_not_response,
988-
const uint_least8_t transfer_id,
989-
const canard_bytes_chain_t payload,
990-
const canard_user_context_t context)
991-
{
992-
bool ok = (self != NULL) && (priority < CANARD_PRIO_COUNT) && bytes_chain_valid(payload) &&
993-
(service_id <= CANARD_SERVICE_ID_MAX) && (destination_node_id <= CANARD_NODE_ID_MAX);
994-
if (ok) {
995-
const uint32_t can_id = (((uint32_t)priority) << PRIO_SHIFT) | //
996-
(UINT32_C(1) << 25U) | // service
997-
(request_not_response ? (UINT32_C(1) << 24U) : 0U) | (((uint32_t)service_id) << 14U) |
998-
(((uint32_t)destination_node_id) << 7U);
999-
canard_txfer_t* const tr = txfer_new(self, deadline, transfer_id, can_id, self->tx.fd, context);
1000-
ok = (tr != NULL) && tx_push(self, tr, false, CANARD_IFACE_BITMAP_ALL, payload, CRC_INITIAL);
1001-
}
1002-
return ok;
1003-
}
1004-
1005987
bool canard_1v0_request(canard_t* const self,
1006988
const canard_us_t deadline,
1007989
const canard_prio_t priority,
@@ -1063,9 +1045,9 @@ static bool tx_0v1_service(canard_t* const self,
10631045
bool ok = (self != NULL) && (priority < CANARD_PRIO_COUNT) && bytes_chain_valid(payload) && (self->node_id != 0U) &&
10641046
(destination_node_id > 0U) && (destination_node_id <= CANARD_NODE_ID_MAX);
10651047
if (ok) {
1066-
const uint32_t can_id = (((((uint32_t)priority) << 2U) | UINT32_C(3)) << 24U) | //
1048+
const uint32_t can_id = (((((uint32_t)priority) << 2U) | UINT32_C(3)) << 24U) |
10671049
(((uint32_t)data_type_id) << 16U) | (request_not_response ? (UINT32_C(1) << 15U) : 0U) |
1068-
(((uint32_t)destination_node_id) << 8U) | (UINT32_C(1) << 7U); // service
1050+
(((uint32_t)destination_node_id) << 8U) | (UINT32_C(1) << 7U);
10691051
canard_txfer_t* const tr = txfer_new(self, deadline, transfer_id, can_id, false, context);
10701052
ok = (tr != NULL) && tx_push(self, tr, true, CANARD_IFACE_BITMAP_ALL, payload, crc_seed);
10711053
}

libcanard/canard.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern "C"
6565
/// Cyphal v1.1 does not support anonymous messages so this value is never used there.
6666
#define CANARD_NODE_ID_ANONYMOUS 0xFFU
6767

68-
/// Cyphal/CAN v1.1 uses a dedicated service-ID for unicast messages.
68+
/// Cyphal/CAN v1.1 uses a dedicated service-ID for unicast messages with the request-not-response bit set.
6969
#define CANARD_SERVICE_ID_UNICAST 511U
7070

7171
/// This is the recommended transfer-ID timeout value given in the Cyphal Specification. The application may choose
@@ -236,12 +236,12 @@ typedef struct canard_vtable_t
236236

237237
/// A new unicast message is received.
238238
/// The handler takes ownership of the payload; it must free it after use using the corresponding memory resource.
239-
void (*on_p2p)(canard_t*,
240-
canard_us_t timestamp,
241-
canard_prio_t priority,
242-
uint_least8_t source_node_id,
243-
uint_least8_t transfer_id,
244-
canard_bytes_mut_t payload);
239+
void (*on_unicast)(canard_t*,
240+
canard_us_t timestamp,
241+
canard_prio_t priority,
242+
uint_least8_t source_node_id,
243+
uint_least8_t transfer_id,
244+
canard_bytes_mut_t payload);
245245

246246
/// Submit one CAN frame for transmission via the specified interface.
247247
/// If the data is empty (size==0), the data pointer may be NULL.

tests/src/test_api_tx.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ static bool mock_tx(canard_t* const,
3434
static bool mock_filter(canard_t* const, const size_t, const canard_filter_t*) { return true; }
3535

3636
// Shared vtable and memory resources used by canard_new() tests.
37-
static const canard_vtable_t kTestVtable = {
38-
.now = mock_now,
39-
.on_p2p = nullptr,
40-
.tx = mock_tx,
41-
.filter = mock_filter,
42-
};
37+
static const canard_vtable_t kTestVtable = { .now = mock_now,
38+
.on_unicast = nullptr,
39+
.tx = mock_tx,
40+
.filter = mock_filter };
4341

4442
static const canard_mem_vtable_t kStdMemVtable = {
4543
.free = std_free_mem,
@@ -96,10 +94,10 @@ static bool capture_tx(canard_t* const self,
9694
}
9795

9896
static const canard_vtable_t kCaptureVtable = {
99-
.now = capture_now,
100-
.on_p2p = nullptr,
101-
.tx = capture_tx,
102-
.filter = mock_filter,
97+
.now = capture_now,
98+
.on_unicast = nullptr,
99+
.tx = capture_tx,
100+
.filter = mock_filter,
103101
};
104102

105103
static canard_mem_set_t make_std_memory()

tests/src/test_intrusive_tx.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ static canard_us_t mock_now(canard_t* const self)
1919
}
2020

2121
// Minimal vtable used by tests.
22-
static const canard_vtable_t test_vtable = {
23-
.now = mock_now,
24-
.on_p2p = NULL,
25-
.tx = NULL,
26-
.filter = NULL,
27-
};
22+
static const canard_vtable_t test_vtable = { .now = mock_now, .on_unicast = NULL, .tx = NULL, .filter = NULL };
2823

2924
// Build a minimal instance with instrumented allocators.
3025
static void init_canard(canard_t* const self,

0 commit comments

Comments
 (0)