fix(uxrce_dds_client): Separate topic creation from request data (#28641)

Address review feedback on the split between entity creation and data
requests:

- Reuse data_reader_id() in dds_topics.h.em instead of duplicating the
  ID formula in the on_topic_update() switch cases.
- Drop the now-unused best_effort_in_stream_id parameter from
  SendTopicsSubs::init() and RcvTopicsPubs::init().
- Handle UXR_INVALID_REQUEST_ID in RcvTopicsPubs::request_data(): the
  reliable output stream has only STREAM_HISTORY slots, and a slot is
  freed only by an Agent ACKNACK, not by uxr_flash_output_streams().
  On overflow, wait for delivery to be confirmed (which drains
  acknowledged slots) and retry once before giving up.

Verified on hardware by shrinking the reliable stream's slot size to
force the overflow path: recovery completes in ~2.5ms per occurrence
and the session still connects. With the stock buffer size and the
current topic count (38 readers, ~760B of requests against 2016B of
capacity), the overflow path is not exercised in practice.

Assisted-by: Claude:claude-sonnet-5

Signed-off-by: danielbuleandra <daniel.buleandra@auterion.com>
This commit is contained in:
Daniel Buleandră authored and GitHub committed 2026-09-18 13:06:04 +02:00
1 parent e9e11c507c
commit c4e4ef98e9
3 files changed
+65 -18

No files matched your search

+49 -8
View File
@@ -34,6 +34,9 @@ import os
// 4ms ~= 250Hz.
#define UXRCE_DEFAULT_POLL_INTERVAL_MS 4
// Timeout for the Agent to acknowledge buffered data requests.
#define UXRCE_REQUEST_DATA_TIMEOUT_MS 1000
typedef bool (*UcdrSerializeMethod)(const void* data, ucdrBuffer& buf, int64_t time_offset);
static constexpr int max_topic_size = 512;
@@ -93,12 +96,12 @@ struct SendTopicsSubs {
uint32_t num_payload_sent{};
bool init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrStreamId best_effort_in_stream_id, uxrObjectId participant_id, const char *client_namespace);
bool init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrObjectId participant_id, const char *client_namespace);
void update(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId best_effort_stream_id, uxrObjectId participant_id, const char *client_namespace);
void reset();
};
bool SendTopicsSubs::init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrStreamId best_effort_in_stream_id, uxrObjectId participant_id, const char *client_namespace) {
bool SendTopicsSubs::init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrObjectId participant_id, const char *client_namespace) {
for (unsigned idx = 0; idx < sizeof(send_subscriptions)/sizeof(send_subscriptions[0]); ++idx) {
if (fds[idx].events == 0) {
fds[idx].fd = orb_subscribe_multi(send_subscriptions[idx].orb_meta, send_subscriptions[idx].orb_instance);
@@ -218,7 +221,8 @@ struct RcvTopicsPubs {
uint32_t num_payload_received{};
bool init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrStreamId best_effort_in_stream_id, uxrObjectId participant_id, const char *client_namespace);
bool init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrObjectId participant_id, const char *client_namespace);
bool request_data(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId best_effort_in_stream_id);
};
@[if subscriptions or subscriptions_multi]@
@@ -231,7 +235,7 @@ static void on_topic_update(uxrSession *session, uxrObjectId object_id, uint16_t
switch (object_id.id) {
@[ for idx, sub in enumerate(subscriptions)]@
case @(idx)+ (65535U / 32U) + 1: {
case data_reader_id(@(idx)): {
@(sub['simple_base_type'])_s data;
if (ucdr_deserialize_@(sub['simple_base_type'])(*ub, data, time_offset_us)) {
@@ -243,7 +247,7 @@ static void on_topic_update(uxrSession *session, uxrObjectId object_id, uint16_t
@[ end for]@
@[ for idx, sub in enumerate(subscriptions_multi)]@
case @(idx + len(subscriptions))+ (65535U / 32U) + 1: {
case data_reader_id(@(idx + len(subscriptions))): {
@(sub['simple_base_type'])_s data;
if (ucdr_deserialize_@(sub['simple_base_type'])(*ub, data, time_offset_us)) {
@@ -282,14 +286,14 @@ static void on_topic_update(uxrSession *session, uxrObjectId object_id, uint16_t
}
@[end if]@
bool RcvTopicsPubs::init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrStreamId best_effort_in_stream_id, uxrObjectId participant_id, const char *client_namespace)
bool RcvTopicsPubs::init(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId reliable_in_stream_id, uxrObjectId participant_id, const char *client_namespace)
{
@[ for idx, sub in enumerate(subscriptions)]@
{
uint16_t queue_depth = orb_get_queue_size(ORB_ID(@(sub['simple_base_type']))) * 2; // use a bit larger queue size than internal
uint32_t message_version = get_message_version<@(sub['simple_base_type'])_s>();
if (!create_data_reader(session, reliable_out_stream_id, best_effort_in_stream_id, participant_id, @(idx), client_namespace, "@(sub['topic'])", message_version, "@(sub['dds_type'])", queue_depth)) {
if (!create_data_reader(session, reliable_out_stream_id, participant_id, @(idx), client_namespace, "@(sub['topic'])", message_version, "@(sub['dds_type'])", queue_depth)) {
return false;
}
}
@@ -299,7 +303,7 @@ bool RcvTopicsPubs::init(uxrSession *session, uxrStreamId reliable_out_stream_id
uint16_t queue_depth = orb_get_queue_size(ORB_ID(@(sub['topic_simple']))) * @(sub.get('max_instances', 2)); // scale queue for multiple sources
uint32_t message_version = get_message_version<@(sub['simple_base_type'])_s>();
if (!create_data_reader(session, reliable_out_stream_id, best_effort_in_stream_id, participant_id, @(idx + len(subscriptions)), client_namespace, "@(sub['topic'])", message_version, "@(sub['dds_type'])", queue_depth)) {
if (!create_data_reader(session, reliable_out_stream_id, participant_id, @(idx + len(subscriptions)), client_namespace, "@(sub['topic'])", message_version, "@(sub['dds_type'])", queue_depth)) {
return false;
}
}
@@ -311,3 +315,40 @@ bool RcvTopicsPubs::init(uxrSession *session, uxrStreamId reliable_out_stream_id
return true;
}
bool RcvTopicsPubs::request_data(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId best_effort_in_stream_id)
{
@[ if subscriptions or subscriptions_multi]@
uxrDeliveryControl delivery_control{};
delivery_control.max_samples = UXR_MAX_SAMPLES_UNLIMITED;
// For each data reader: build its ID, then buffer a READ_DATA request for it
for (uint16_t index = 0; index < @(len(subscriptions) + len(subscriptions_multi)); ++index) {
uxrObjectId datareader_id = uxr_object_id(data_reader_id(index), UXR_DATAREADER_ID);
// Buffer is full: no slot for this request
if (uxr_buffer_request_data(session, reliable_out_stream_id, datareader_id, best_effort_in_stream_id,
&delivery_control) == UXR_INVALID_REQUEST_ID) {
// Flush and wait for the Agent's ACK: only that frees a history slot
if (!uxr_run_session_until_confirm_delivery(session, UXRCE_REQUEST_DATA_TIMEOUT_MS)) {
PX4_ERR("request data: delivery not confirmed at reader %u", index);
return false;
}
// Retry: buffer the same request now that a slot is free
if (uxr_buffer_request_data(session, reliable_out_stream_id, datareader_id, best_effort_in_stream_id,
&delivery_control) == UXR_INVALID_REQUEST_ID) {
PX4_ERR("request data failed for reader %u", index);
return false;
}
}
}
// Flush and wait for ack
if (!uxr_run_session_until_confirm_delivery(session, UXRCE_REQUEST_DATA_TIMEOUT_MS)) {
PX4_WARN("request data: delivery not confirmed for all readers");
}
@[ end if]@
return true;
}
+9 -8
View File
@@ -101,7 +101,14 @@ static bool create_data_writer(uxrSession *session, uxrStreamId reliable_out_str
return true;
}
static bool __attribute__((unused)) create_data_reader(uxrSession *session, uxrStreamId reliable_out_stream_id, uxrStreamId input_stream_id,
// Data reader IDs use the second half of the available ID space.
// Add 1 so that we get a nice hex starting number: 0x800 instead of 0x7ff.
static constexpr uint16_t data_reader_id(uint16_t index)
{
return index + (65535U / 32U) + 1;
}
static bool __attribute__((unused)) create_data_reader(uxrSession *session, uxrStreamId reliable_out_stream_id,
uxrObjectId participant_id, uint16_t index, const char *client_namespace, const char *topic,
uint32_t message_version,
const char *type_name, uint16_t queue_depth)
@@ -114,9 +121,7 @@ static bool __attribute__((unused)) create_data_reader(uxrSession *session, uxrS
return false;
}
// Use the second half of the available ID space.
// Add 1 so that we get a nice hex starting number: 0x800 instead of 0x7ff.
uint16_t id = index + (65535U / 32U) + 1;
uint16_t id = data_reader_id(index);
uxrObjectId topic_id = uxr_object_id(id, UXR_TOPIC_ID);
uint16_t topic_req = uxr_buffer_create_topic_bin(session, reliable_out_stream_id, topic_id, participant_id, topic_name,
@@ -151,9 +156,5 @@ static bool __attribute__((unused)) create_data_reader(uxrSession *session, uxrS
return false;
}
uxrDeliveryControl delivery_control{};
delivery_control.max_samples = UXR_MAX_SAMPLES_UNLIMITED;
uxr_buffer_request_data(session, reliable_out_stream_id, datareader_id, input_stream_id, &delivery_control);
return true;
}
@@ -367,12 +367,12 @@ bool UxrceddsClient::setupSession(uxrSession *session)
px4_usleep(10'000);
}
if (!_pubs->init(session, _reliable_out, reliable_in, best_effort_in, _participant_id, _client_namespace)) {
if (!_pubs->init(session, _reliable_out, reliable_in, _participant_id, _client_namespace)) {
PX4_ERR("pubs init failed");
return false;
}
if (!_subs->init(session, _reliable_out, reliable_in, best_effort_in, _participant_id, _client_namespace)) {
if (!_subs->init(session, _reliable_out, reliable_in, _participant_id, _client_namespace)) {
PX4_ERR("subs init failed");
return false;
}
@@ -386,6 +386,11 @@ bool UxrceddsClient::setupSession(uxrSession *session)
}
}
if (!_pubs->request_data(session, _reliable_out, best_effort_in)) {
PX4_ERR("pubs request data failed");
return false;
}
_connected = true;
return true;
}