Skip to content

Commit 4c13edb

Browse files
udpard_tx_push_p2p
1 parent 9e21daa commit 4c13edb

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

libudpard/udpard.c

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,11 @@ static uint32_t tx_push(udpard_tx_t* const tx,
890890
(void)cavl2_find_or_insert(
891891
&tx->index_deadline, &tr->deadline, tx_cavl_compare_deadline, &tr->index_deadline, cavl2_trivial_factory);
892892
// Add to the transfer index for incoming ack management.
893-
const tx_transfer_key_t key = { .topic_hash = tr->topic_hash, .transfer_id = tr->transfer_id };
894-
(void)cavl2_find_or_insert(
893+
const tx_transfer_key_t key = { .topic_hash = tr->topic_hash, .transfer_id = tr->transfer_id };
894+
const udpard_tree_t* const tree_transfer = cavl2_find_or_insert(
895895
&tx->index_transfer, &key, tx_cavl_compare_transfer, &tr->index_transfer, cavl2_trivial_factory);
896+
UDPARD_ASSERT(tree_transfer == &tr->index_transfer); // ensure no duplicates; checked at the API level
897+
(void)tree_transfer;
896898
// Add to the agewise list to allow instant sacrifice when needed; oldest at the tail.
897899
enlist_head(&tx->agewise, &tr->agewise);
898900

@@ -1039,23 +1041,38 @@ uint32_t udpard_tx_push(udpard_tx_t* const self,
10391041
// Before attempting to enqueue a new transfer, we need to update the transmission scheduler.
10401042
// It may release some items from the tx queue, and it may also promote some staged transfers to the queue.
10411043
udpard_tx_poll(self, now, UDPARD_IFACE_MASK_ALL);
1042-
const meta_t meta = {
1043-
.priority = priority,
1044-
.flag_ack = feedback != NULL,
1045-
.transfer_payload_size = (uint32_t)payload.size,
1046-
.transfer_id = transfer_id,
1047-
.sender_uid = self->local_uid,
1048-
.topic_hash = topic_hash,
1049-
};
1050-
out = tx_push(self, //
1051-
now,
1052-
deadline,
1053-
meta,
1054-
remote_ep,
1055-
payload,
1056-
feedback,
1057-
user_transfer_reference,
1058-
NULL);
1044+
const meta_t meta = { .priority = priority,
1045+
.flag_ack = feedback != NULL,
1046+
.transfer_payload_size = (uint32_t)payload.size,
1047+
.transfer_id = transfer_id,
1048+
.sender_uid = self->local_uid,
1049+
.topic_hash = topic_hash };
1050+
out = tx_push(self, now, deadline, meta, remote_ep, payload, feedback, user_transfer_reference, NULL);
1051+
}
1052+
return out;
1053+
}
1054+
1055+
uint32_t udpard_tx_push_p2p(udpard_tx_t* const self,
1056+
const udpard_us_t now,
1057+
const udpard_us_t deadline,
1058+
const udpard_prio_t priority,
1059+
const udpard_remote_t remote,
1060+
const udpard_bytes_t payload,
1061+
void (*const feedback)(udpard_tx_t*, udpard_tx_feedback_t),
1062+
void* const user_transfer_reference)
1063+
{
1064+
uint32_t out = 0;
1065+
if (self != NULL) {
1066+
out = udpard_tx_push(self,
1067+
now,
1068+
deadline,
1069+
priority,
1070+
remote.uid,
1071+
remote.endpoints,
1072+
self->p2p_transfer_id++,
1073+
payload,
1074+
feedback,
1075+
user_transfer_reference);
10591076
}
10601077
return out;
10611078
}

libudpard/udpard.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ struct udpard_tx_t
329329
/// The globally unique identifier of the local node. Must not change after initialization.
330330
uint64_t local_uid;
331331

332-
/// A random-initialized transfer-ID counter for all outgoing P2P transfers.
332+
/// A random-initialized transfer-ID counter for all outgoing P2P transfers. Must not be changed by the application.
333333
uint64_t p2p_transfer_id;
334334

335335
/// The maximum number of Cyphal transfer payload bytes per UDP datagram.
@@ -392,10 +392,6 @@ bool udpard_tx_new(udpard_tx_t* const self,
392392
/// transmission queue at the appropriate position. The transfer payload will be copied into the transmission queue
393393
/// so that the lifetime of the datagrams is not related to the lifetime of the input payload buffer.
394394
///
395-
/// The topic hash is not defined for P2P transfers since there are no topics involved; in P2P, this parameter
396-
/// is used to pass the destination node's UID instead. Setting it incorrectly will cause the destination node
397-
/// to reject the transfer as misaddressed.
398-
///
399395
/// The transfer_id parameter is used to populate the transfer_id field of the generated Cyphal/UDP frames.
400396
/// The caller shall increment the transfer-ID counter after each successful invocation of this function
401397
/// per redundant interface; the same transfer published over redundant interfaces shall have the same transfer-ID.
@@ -435,13 +431,24 @@ uint32_t udpard_tx_push(udpard_tx_t* const self,
435431
const udpard_us_t now,
436432
const udpard_us_t deadline,
437433
const udpard_prio_t priority,
438-
const uint64_t topic_hash, // For P2P transfers, this is the destination's UID.
434+
const uint64_t topic_hash,
439435
const udpard_udpip_ep_t remote_ep[UDPARD_IFACE_COUNT_MAX], // May be invalid for some ifaces.
440436
const uint64_t transfer_id,
441437
const udpard_bytes_t payload,
442438
void (*const feedback)(udpard_tx_t*, udpard_tx_feedback_t), // NULL if best-effort.
443439
void* const user_transfer_reference);
444440

441+
/// Specialization for P2P transfers. The semantics are identical to udpard_tx_push().
442+
/// The transfer-ID will be provided by the library based on the udpard_tx_t::p2p_transfer_id counter.
443+
uint32_t udpard_tx_push_p2p(udpard_tx_t* const self,
444+
const udpard_us_t now,
445+
const udpard_us_t deadline,
446+
const udpard_prio_t priority,
447+
const udpard_remote_t remote, // Endpoints may be invalid for some ifaces.
448+
const udpard_bytes_t payload,
449+
void (*const feedback)(udpard_tx_t*, udpard_tx_feedback_t), // NULL if best-effort.
450+
void* const user_transfer_reference);
451+
445452
/// This should be invoked whenever the socket/NIC of this queue becomes ready to accept new datagrams for transmission.
446453
/// It is fine to also invoke it periodically unconditionally to drive the transmission process.
447454
/// Internally, the function will query the scheduler for the next frame to be transmitted and will attempt

0 commit comments

Comments
 (0)