Skip to content

Commit 1182573

Browse files
committed
also use RosMsgType for transmitting ros2 type info
1 parent 5247422 commit 1182573

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

mqtt_client/src/MqttClient.ros2.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SOFTWARE.
3131
#include <vector>
3232

3333
#include <mqtt_client/MqttClient.ros2.hpp>
34+
#include <mqtt_client_interfaces/msg/ros_msg_type.hpp>
3435
#include <rcpputils/env.hpp>
3536
#include <std_msgs/msg/bool.hpp>
3637
#include <std_msgs/msg/char.hpp>
@@ -516,40 +517,51 @@ void MqttClient::ros2mqtt(
516517
const std::string& ros_topic) {
517518

518519
Ros2MqttInterface& ros2mqtt = ros2mqtt_[ros_topic];
519-
std::string& ros_msg_type = ros2mqtt.ros.msg_type;
520520
std::string mqtt_topic = ros2mqtt.mqtt.topic;
521521
std::vector<uint8_t> payload_buffer;
522522

523+
// gather information on ROS message type in special ROS message
524+
mqtt_client_interfaces::msg::RosMsgType ros_msg_type;
525+
ros_msg_type.name = ros2mqtt.ros.msg_type;
526+
523527
RCLCPP_DEBUG(get_logger(), "Received ROS message of type '%s' on topic '%s'",
524-
ros_msg_type.c_str(), ros_topic.c_str());
528+
ros_msg_type.name.c_str(), ros_topic.c_str());
525529

526530
if (ros2mqtt.primitive) { // publish as primitive (string) message
527531

528532
// resolve ROS messages to primitive strings if possible
529533
std::string payload;
530534
bool found_primitive =
531-
primitiveRosMessageToString(serialized_msg, ros_msg_type, payload);
535+
primitiveRosMessageToString(serialized_msg, ros_msg_type.name, payload);
532536
if (found_primitive) {
533537
payload_buffer = std::vector<uint8_t>(payload.begin(), payload.end());
534538
} else {
535539
RCLCPP_WARN(get_logger(),
536540
"Cannot send ROS message of type '%s' as primitive message, "
537541
"check supported primitive types",
538-
ros_msg_type.c_str());
542+
ros_msg_type.name.c_str());
539543
return;
540544
}
541545

542546
} else { // publish as complete ROS message incl. ROS message type
543547

548+
// serialize ROS message type
549+
rclcpp::SerializedMessage serialized_ros_msg_type;
550+
serializeRosMessage(ros_msg_type, serialized_ros_msg_type);
551+
uint32_t msg_type_length = serialized_ros_msg_type.size();
552+
std::vector<uint8_t> msg_type_buffer = std::vector<uint8_t>(
553+
serialized_ros_msg_type.get_rcl_serialized_message().buffer,
554+
serialized_ros_msg_type.get_rcl_serialized_message().buffer + msg_type_length);
555+
544556
// send ROS message type information to MQTT broker
545557
mqtt_topic = kRosMsgTypeMqttTopicPrefix + ros2mqtt.mqtt.topic;
546558
try {
547559
RCLCPP_DEBUG(get_logger(),
548560
"Sending ROS message type to MQTT broker on topic '%s' ...",
549561
mqtt_topic.c_str());
550562
mqtt::message_ptr mqtt_msg =
551-
mqtt::make_message(mqtt_topic, ros_msg_type.c_str(),
552-
ros_msg_type.size(), ros2mqtt.mqtt.qos, true);
563+
mqtt::make_message(mqtt_topic, msg_type_buffer.data(),
564+
msg_type_buffer.size(), ros2mqtt.mqtt.qos, true);
553565
client_->publish(mqtt_msg);
554566
} catch (const mqtt::exception& e) {
555567
RCLCPP_WARN(
@@ -605,7 +617,7 @@ void MqttClient::ros2mqtt(
605617
RCLCPP_DEBUG(
606618
get_logger(),
607619
"Sending ROS message of type '%s' to MQTT broker on topic '%s' ...",
608-
ros_msg_type.c_str(), mqtt_topic.c_str());
620+
ros_msg_type.name.c_str(), mqtt_topic.c_str());
609621
mqtt::message_ptr mqtt_msg = mqtt::make_message(
610622
mqtt_topic, payload_buffer.data(), payload_buffer.size(),
611623
ros2mqtt.mqtt.qos, ros2mqtt.mqtt.retained);
@@ -638,10 +650,6 @@ void MqttClient::mqtt2ros(mqtt::const_message_ptr mqtt_msg,
638650
// if stamped, compute latency
639651
if (stamped) {
640652

641-
// create ROS message buffer on top of MQTT message payload
642-
char* non_const_payload = const_cast<char*>(&payload[1]);
643-
uint8_t* stamp_buffer = reinterpret_cast<uint8_t*>(non_const_payload);
644-
645653
// copy stamp to generic message buffer
646654
rclcpp::SerializedMessage serialized_stamp(stamp_length_);
647655
std::memcpy(serialized_stamp.get_rcl_serialized_message().buffer,
@@ -860,7 +868,17 @@ void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
860868
mqtt_topic.find(kRosMsgTypeMqttTopicPrefix) != std::string::npos;
861869
if (msg_contains_ros_msg_type) {
862870

863-
std::string ros_msg_type = mqtt_msg->to_string();
871+
// copy message type to generic message buffer
872+
auto& payload = mqtt_msg->get_payload_ref();
873+
uint32_t payload_length = static_cast<uint32_t>(payload.size());
874+
rclcpp::SerializedMessage serialized_ros_msg_type(payload_length);
875+
std::memcpy(serialized_ros_msg_type.get_rcl_serialized_message().buffer,
876+
&(payload[0]), payload_length);
877+
serialized_ros_msg_type.get_rcl_serialized_message().buffer_length = payload_length;
878+
879+
// deserialize ROS message type
880+
mqtt_client_interfaces::msg::RosMsgType ros_msg_type;
881+
deserializeRosMessage(serialized_ros_msg_type, ros_msg_type);
864882

865883
// reconstruct corresponding MQTT data topic
866884
std::string mqtt_data_topic = mqtt_topic;
@@ -869,17 +887,17 @@ void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
869887
Mqtt2RosInterface& mqtt2ros = mqtt2ros_[mqtt_data_topic];
870888

871889
// if ROS message type has changed
872-
if (ros_msg_type != mqtt2ros.ros.msg_type) {
890+
if (ros_msg_type.name != mqtt2ros.ros.msg_type) {
873891

874-
mqtt2ros.ros.msg_type = ros_msg_type;
892+
mqtt2ros.ros.msg_type = ros_msg_type.name;
875893
RCLCPP_INFO(get_logger(),
876894
"ROS publisher message type on topic '%s' set to '%s'",
877-
mqtt2ros.ros.topic.c_str(), ros_msg_type.c_str());
895+
mqtt2ros.ros.topic.c_str(), ros_msg_type.name.c_str());
878896

879897
// recreate generic publisher
880898
try {
881899
mqtt2ros.ros.publisher = create_generic_publisher(
882-
mqtt2ros.ros.topic, ros_msg_type, mqtt2ros.ros.queue_size);
900+
mqtt2ros.ros.topic, ros_msg_type.name, mqtt2ros.ros.queue_size);
883901
} catch (rclcpp::exceptions::RCLError& e) {
884902
RCLCPP_ERROR(get_logger(), "Failed to create generic publisher: %s",
885903
e.what());

0 commit comments

Comments
 (0)