Skip to content

[22872] Configure ROS 2 Easy Mode via C++ and XML #5689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions include/fastdds/dds/core/policy/QosPolicies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2664,6 +2664,7 @@ class WireProtocolConfigQos : public QosPolicy
FASTDDS_EXPORTED_API WireProtocolConfigQos()
: QosPolicy(false)
, participant_id(-1)
, easy_mode_("")
{
}

Expand All @@ -2683,6 +2684,7 @@ class WireProtocolConfigQos : public QosPolicy
(this->default_multicast_locator_list == b.default_multicast_locator_list) &&
(this->default_external_unicast_locators == b.default_external_unicast_locators) &&
(this->ignore_non_matching_locators == b.ignore_non_matching_locators) &&
(this->easy_mode_ == b.easy_mode()) &&
QosPolicy::operator ==(b);
}

Expand Down Expand Up @@ -2725,6 +2727,41 @@ class WireProtocolConfigQos : public QosPolicy
* Whether locators that don't match with the announced locators should be kept.
*/
bool ignore_non_matching_locators = false;

/**
* Setter for ROS 2 Easy Mode IP
*/
void easy_mode(
std::string ip)
{
// Check if the input is empty
if (!ip.empty())
{
// Check if the input is a valid IP
if (!rtps::IPLocator::isIPv4(ip))
{
EPROSIMA_LOG_ERROR(
WIREPROTOCOLQOS, "Invalid IP address format for ROS 2 Easy Mode. It must be an IPv4 address.");

return;
}
}

easy_mode_ = ip;
}

/**
* Getter for ROS 2 Easy Mode IP
*/
const std::string& easy_mode() const
{
return easy_mode_;
}

private:

//! ROS 2 Easy Mode IP
std::string easy_mode_;
};

//! Qos Policy to configure the transport layer
Expand Down
4 changes: 4 additions & 0 deletions include/fastdds/rtps/attributes/RTPSParticipantAttributes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ class RTPSParticipantAttributes
(this->port == b.port) &&
(this->userData == b.userData) &&
(this->participantID == b.participantID) &&
(this->easy_mode_ip == b.easy_mode_ip) &&
(this->useBuiltinTransports == b.useBuiltinTransports) &&
(this->properties == b.properties) &&
(this->prefix == b.prefix) &&
Expand Down Expand Up @@ -527,6 +528,9 @@ class RTPSParticipantAttributes
//! Participant ID
int32_t participantID = -1;

//! IP of the Host where master Server is located (EASY_MODE context)
std::string easy_mode_ip = "";

//! User defined transports to use alongside or in place of builtins.
std::vector<std::shared_ptr<fastdds::rtps::TransportDescriptorInterface>> userTransports;

Expand Down
2 changes: 2 additions & 0 deletions resources/xsd/fastdds_profiles.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
├ builtin [0~1],
├ port [0~1],
├ participantID [int32],
├ easy_mode_ip [string],
├ userTransports [0~1],
| └ transport_id [1~*] [string],
├ useBuiltinTransports [bool],
Expand Down Expand Up @@ -172,6 +173,7 @@
<xs:element name="builtin" type="builtinAttributesType" minOccurs="0" maxOccurs="1"/>
<xs:element name="port" type="portType" minOccurs="0" maxOccurs="1"/>
<xs:element name="participantID" type="int32" minOccurs="0" maxOccurs="1"/>
<xs:element name="easy_mode_ip" type="string" minOccurs="0" maxOccurs="1"/>
<xs:element name="userTransports" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/fastdds/utils/QosConverters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void set_qos_from_attributes(
qos.wire_protocol().default_multicast_locator_list = attr.defaultMulticastLocatorList;
qos.wire_protocol().default_external_unicast_locators = attr.default_external_unicast_locators;
qos.wire_protocol().ignore_non_matching_locators = attr.ignore_non_matching_locators;
qos.wire_protocol().easy_mode(attr.easy_mode_ip);
qos.transport().user_transports = attr.userTransports;
qos.transport().use_builtin_transports = attr.useBuiltinTransports;
qos.transport().send_socket_buffer_size = attr.sendSocketBufferSize;
Expand Down Expand Up @@ -198,6 +199,7 @@ void set_attributes_from_qos(
attr.setName(qos.name());
attr.prefix = qos.wire_protocol().prefix;
attr.participantID = qos.wire_protocol().participant_id;
attr.easy_mode_ip = qos.wire_protocol().easy_mode();
attr.builtin = qos.wire_protocol().builtin;
attr.port = qos.wire_protocol().port;
attr.defaultUnicastLocatorList = qos.wire_protocol().default_unicast_locator_list;
Expand Down
14 changes: 14 additions & 0 deletions src/cpp/rtps/attributes/ServerAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ const std::string& ros_easy_mode_env()
{
static std::string ip_value;
SystemInfo::get_env(ROS2_EASY_MODE_URI, ip_value);

if (!ip_value.empty())
{
// Check that the value is a valid IPv4 address
if (!IPLocator::isIPv4(ip_value))
{
EPROSIMA_LOG_ERROR(
SERVERATTRIBUTES,
"Invalid format: Easy Mode IP must be a valid IPv4 address. "
"Ignoring " << ROS2_EASY_MODE_URI << " value.");

ip_value = "";
}
}
return ip_value;
}

Expand Down
3 changes: 2 additions & 1 deletion src/cpp/rtps/attributes/ServerAttributes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ const std::string& ros_discovery_server_env();

/**
* Get the value of environment variable ROS2_EASY_MODE_URI
* @return The value of environment variable ROS2_EASY_MODE_URI. Empty string if the variable is not defined.
* @return The value of environment variable ROS2_EASY_MODE_URI.
* Empty string if the variable is not defined or does not have a valid IPv4 format.
*/
const std::string& ros_easy_mode_env();

Expand Down
19 changes: 19 additions & 0 deletions src/cpp/xmlparser/XMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2246,6 +2246,7 @@ XMLP_ret XMLParser::fillDataNode(
<xs:element name="port" type="portType" minOccurs="0"/>
<xs:element name="userData" type="octetVectorType" minOccurs="0"/>
<xs:element name="participantID" type="int32Type" minOccurs="0"/>
<xs:element name="easy_mode_ip" type="stringType" minOccurs="0"/>
<xs:element name="flow_controller_descriptors" type="flowControllerDescriptorsType" minOccurs="0"/>
<xs:element name="userTransports" type="stringListType" minOccurs="0"/>
<xs:element name="useBuiltinTransports" type="boolType" minOccurs="0"/>
Expand Down Expand Up @@ -2446,6 +2447,24 @@ XMLP_ret XMLParser::fillDataNode(
return XMLP_ret::XML_ERROR;
}
}
else if (strcmp(name, EASY_MODE_IP) == 0)
{
// easy_mode_ip - stringType
std::string str_aux;
if (XMLP_ret::XML_OK != getXMLString(p_aux0, &str_aux, ident))
{
return XMLP_ret::XML_ERROR;
}

// Check that the string is a valid IPv4 address
if (!fastdds::rtps::IPLocator::isIPv4(str_aux))
{
EPROSIMA_LOG_ERROR(XMLPARSER, "'easy_mode_ip' is not a valid IPv4 address.");
return XMLP_ret::XML_ERROR;
}

participant_node.get()->rtps.easy_mode_ip = str_aux;
}
else if (strcmp(name, FLOW_CONTROLLER_DESCRIPTOR_LIST) == 0)
{
// flow_controller_descriptors
Expand Down
1 change: 1 addition & 0 deletions src/cpp/xmlparser/XMLParserCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ const char* LOGICAL_PORT = "logical_port";
const char* PHYSICAL_PORT = "physical_port";
const char* USER_DATA = "userData";
const char* PART_ID = "participantID";
const char* EASY_MODE_IP = "easy_mode_ip";
const char* FLOW_CONTROLLER_DESCRIPTOR_LIST = "flow_controller_descriptor_list";
const char* USER_TRANS = "userTransports";
const char* USE_BUILTIN_TRANS = "useBuiltinTransports";
Expand Down
1 change: 1 addition & 0 deletions src/cpp/xmlparser/XMLParserCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ extern const char* LOGICAL_PORT;
extern const char* PHYSICAL_PORT;
extern const char* USER_DATA;
extern const char* PART_ID;
extern const char* EASY_MODE_IP;
extern const char* IP4_TO_SEND;
extern const char* IP6_TO_SEND;
extern const char* FLOW_CONTROLLER_DESCRIPTOR_LIST;
Expand Down
76 changes: 76 additions & 0 deletions test/blackbox/common/DDSBlackboxTestsDSEasyMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,79 @@ TEST(DSEasyMode, easy_discovery_mode_env_inconsistent_ip)
stop_background_servers();
#endif // _WIN32
}

/**
* Check that the environment variable ROS2_EASY_MODE is ignored if
* it does not have a valid IPv4 format.
*/
TEST(DSEasyMode, easy_discovery_mode_env_invalid)
{
#ifndef _WIN32 // The feature is not supported on Windows yet

PubSubWriter<HelloWorldPubSubType> writer(TEST_TOPIC_NAME);
PubSubReader<HelloWorldPubSubType> reader(TEST_TOPIC_NAME);

// Set ROS2_EASY_MODE to an invalid string value
set_easy_discovery_mode_env("Foo");

std::atomic<bool> writer_background_ds_discovered(false);
std::atomic<bool> reader_background_ds_discovered(false);

writer.set_on_discovery_function(
[&writer_background_ds_discovered](
const eprosima::fastdds::rtps::ParticipantBuiltinTopicData& data,
eprosima::fastdds::rtps::ParticipantDiscoveryStatus)
{
if (data.participant_name == "DiscoveryServerAuto")
{
writer_background_ds_discovered.store(true);
}
return true;
});
writer.init();

reader.set_on_discovery_function(
[&reader_background_ds_discovered](const eprosima::fastdds::rtps::ParticipantBuiltinTopicData& data,
eprosima::fastdds::rtps::ParticipantDiscoveryStatus)
{
if (data.participant_name == "DiscoveryServerAuto")
{
reader_background_ds_discovered.store(true);
}
return true;
});
reader.init();

ASSERT_TRUE(writer.isInitialized());
ASSERT_TRUE(reader.isInitialized());

// Wait for endpoint discovery first
writer.wait_discovery();
reader.wait_discovery();

// Check that no Background DS was discovered,
// only the other reader or writer
ASSERT_GE(writer.get_participants_matched(), 1u);
ASSERT_GE(reader.get_participants_matched(), 1u);
ASSERT_FALSE(writer_background_ds_discovered.load());
ASSERT_FALSE(reader_background_ds_discovered.load());
#endif // _WIN32
}

/**
* Check that easy mode configuration is ignored when setting it
* with code using WireProtocolConfigQos.
*
* Note: This test only checks that the configuration is ignored.
* Probably it should be extended similarly to easy_discovery_mode_env_invalid
* when Configuring Easy Mode via c++ and XML is implemented.
*/
TEST(DSEasyMode, wire_protocol_qos_params_invalid)
{
eprosima::fastdds::dds::WireProtocolConfigQos wire_protocol_qos;

// Try to set easy mode IP using an invalid IP address
wire_protocol_qos.easy_mode("Foo");

ASSERT_TRUE(wire_protocol_qos.easy_mode().empty());
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ class RTPSParticipantAttributes
(this->port == b.port) &&
(this->userData == b.userData) &&
(this->participantID == b.participantID) &&
(this->easy_mode_ip == b.easy_mode_ip) &&
(this->useBuiltinTransports == b.useBuiltinTransports) &&
(this->properties == b.properties) &&
(this->prefix == b.prefix) &&
Expand Down Expand Up @@ -553,6 +554,9 @@ class RTPSParticipantAttributes
//! Participant ID
int32_t participantID = -1;

//! IP of the Host where master Server is located (EASY_MODE context)
std::string easy_mode_ip = "";

//! User defined transports to use alongside or in place of builtins.
std::vector<std::shared_ptr<fastdds::rtps::TransportDescriptorInterface>> userTransports;

Expand Down
13 changes: 13 additions & 0 deletions test/unittest/xmlparser/XMLParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ TEST_F(XMLParserTests, Data)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -638,6 +639,7 @@ TEST_F(XMLParserTests, DataDeprecated)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -733,6 +735,7 @@ TEST_F(XMLParserTests, DataBuffer)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -828,6 +831,7 @@ TEST_F(XMLParserTests, DataBufferDeprecated)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -1809,6 +1813,7 @@ TEST_F(XMLParserTests, parseLogConfig)
* 2. Check missing DomainId value in tag
* 3. Check bad values for all attributes
* 4. Check a non existant attribute tag
* 5. Check a non valid Easy Mode IP
*/
TEST_F(XMLParserTests, fillDataNodeParticipantNegativeClauses)
{
Expand Down Expand Up @@ -1862,6 +1867,7 @@ TEST_F(XMLParserTests, fillDataNodeParticipantNegativeClauses)
"<builtin><bad_element></bad_element></builtin>",
"<port><bad_element></bad_element></port>",
"<participantID><bad_element></bad_element></participantID>",
"<easy_mode_ip><bad_element></bad_element></easy_mode_ip>",
"<flow_controller_descriptor_list><bad_element></bad_element></flow_controller_descriptor_list>",
"<userTransports><bad_element></bad_element></userTransports>",
"<useBuiltinTransports><bad_element></bad_element></useBuiltinTransports>",
Expand All @@ -1880,6 +1886,13 @@ TEST_F(XMLParserTests, fillDataNodeParticipantNegativeClauses)
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParserTest::fillDataNode_wrapper(titleElement, *participant_node));

}

// Check invalid easy_mode_ip value (not a valid IPv4 address)
std::string invalid_easy_mode_ip = "<easy_mode_ip>Foo</easy_mode_ip>";
snprintf(xml, xml_len, xml_p, invalid_easy_mode_ip.c_str());
ASSERT_EQ(tinyxml2::XMLError::XML_SUCCESS, xml_doc.Parse(xml));
titleElement = xml_doc.RootElement();
EXPECT_EQ(XMLP_ret::XML_ERROR, XMLParserTest::fillDataNode_wrapper(titleElement, *participant_node));
}
}

Expand Down
9 changes: 7 additions & 2 deletions test/unittest/xmlparser/XMLProfileParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class XMLProfileParserTests : public XMLProfileParserBasicTests, public testing:

std::string xml_filename_ = "test_xml_profile.xml";

const std::pair<std::string, std::string> c_environment_values_[168]
const std::pair<std::string, std::string> c_environment_values_[169]
{
{"XML_PROFILES_ENV_VAR_1", "123"},
{"XML_PROFILES_ENV_VAR_2", "4"},
Expand Down Expand Up @@ -305,7 +305,8 @@ class XMLProfileParserTests : public XMLProfileParserBasicTests, public testing:
{"XML_PROFILES_ENV_VAR_165", "2048"},
{"XML_PROFILES_ENV_VAR_166", "45"},
{"XML_PROFILES_ENV_VAR_167", "test_flow_controller"},
{"XML_PROFILES_ENV_VAR_168", "251"}
{"XML_PROFILES_ENV_VAR_168", "251"},
{"XML_PROFILES_ENV_VAR_169", "127.0.0.1"}
};

};
Expand Down Expand Up @@ -568,6 +569,7 @@ TEST_P(XMLProfileParserTests, XMLParserParticipant)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -669,6 +671,7 @@ TEST_F(XMLProfileParserBasicTests, XMLParserParticipantDeprecated)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -753,6 +756,7 @@ TEST_P(XMLProfileParserTests, XMLParserDefaultParticipantProfile)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down Expand Up @@ -837,6 +841,7 @@ TEST_F(XMLProfileParserBasicTests, XMLParserDefaultParticipantProfileDeprecated)
EXPECT_EQ(port.offsetd3, 456);
EXPECT_EQ(port.offsetd4, 251);
EXPECT_EQ(rtps_atts.participantID, 9898);
EXPECT_EQ(rtps_atts.easy_mode_ip, "127.0.0.1");
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->max_bytes_per_period, 2048);
EXPECT_EQ(rtps_atts.flow_controllers.at(0)->period_ms, 45u);
EXPECT_EQ(rtps_atts.useBuiltinTransports, true);
Expand Down
Loading