@@ -547,7 +547,9 @@ void MqttClient::connected(const std::string& cause) {
547
547
// subscribe MQTT topics
548
548
for (auto & mqtt2ros_p : mqtt2ros_) {
549
549
Mqtt2RosInterface& mqtt2ros = mqtt2ros_p.second ;
550
- std::string mqtt_topic = kRosMsgTypeMqttTopicPrefix + mqtt2ros_p.first ;
550
+ std::string mqtt_topic = mqtt2ros_p.first ;
551
+ if (!mqtt2ros.primitive ) // subscribe topics for ROS message types first
552
+ mqtt_topic = kRosMsgTypeMqttTopicPrefix + mqtt_topic;
551
553
client_->subscribe (mqtt_topic, mqtt2ros.mqtt .qos );
552
554
NODELET_DEBUG (" Subscribed MQTT topic '%s'" , mqtt_topic.c_str ());
553
555
}
@@ -583,17 +585,26 @@ void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
583
585
584
586
std::string mqtt_topic = mqtt_msg->get_topic ();
585
587
NODELET_DEBUG (" Received MQTT message on topic '%s'" , mqtt_topic.c_str ());
586
- auto & payload = mqtt_msg->get_payload_ref ();
587
- uint32_t payload_length = static_cast <uint32_t >(payload.size ());
588
588
589
+ // publish directly if primitive
590
+ if (mqtt2ros_.count (mqtt_topic) > 0 ) {
591
+ Mqtt2RosInterface& mqtt2ros = mqtt2ros_[mqtt_topic];
592
+ if (mqtt2ros.primitive ) mqtt2primitive (mqtt_msg);
593
+ return ;
594
+ }
595
+
596
+ // else first check for ROS message type
589
597
bool msg_contains_ros_msg_type =
590
598
mqtt_topic.find (kRosMsgTypeMqttTopicPrefix ) != std::string::npos;
591
599
if (msg_contains_ros_msg_type) {
592
600
593
601
// create ROS message buffer on top of MQTT message payload
602
+ auto & payload = mqtt_msg->get_payload_ref ();
603
+ uint32_t payload_length = static_cast <uint32_t >(payload.size ());
594
604
char * non_const_payload = const_cast <char *>(&payload[0 ]);
595
605
uint8_t * msg_type_buffer = reinterpret_cast <uint8_t *>(non_const_payload);
596
606
607
+ // TODO: might crash if payload by chance does not contain ros msg type
597
608
// deserialize ROS message type
598
609
RosMsgType ros_msg_type;
599
610
ros::serialization::IStream msg_type_stream (msg_type_buffer,
0 commit comments