Skip to content

Commit 8a7fcce

Browse files
committed
check for primitive messages in message_arrived
1 parent 8bd1184 commit 8a7fcce

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

include/mqtt_client/MqttClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ class MqttClient : public nodelet::Nodelet,
192192
void mqtt2ros(mqtt::const_message_ptr mqtt_msg,
193193
const ros::WallTime& arrival_stamp = ros::WallTime::now());
194194

195+
void mqtt2primitive(mqtt::const_message_ptr mqtt_msg);
196+
195197
/**
196198
* @brief Callback for when the client has successfully connected to the
197199
* broker.

src/MqttClient.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ void MqttClient::connected(const std::string& cause) {
547547
// subscribe MQTT topics
548548
for (auto& mqtt2ros_p : mqtt2ros_) {
549549
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;
551553
client_->subscribe(mqtt_topic, mqtt2ros.mqtt.qos);
552554
NODELET_DEBUG("Subscribed MQTT topic '%s'", mqtt_topic.c_str());
553555
}
@@ -583,17 +585,26 @@ void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
583585

584586
std::string mqtt_topic = mqtt_msg->get_topic();
585587
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());
588588

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
589597
bool msg_contains_ros_msg_type =
590598
mqtt_topic.find(kRosMsgTypeMqttTopicPrefix) != std::string::npos;
591599
if (msg_contains_ros_msg_type) {
592600

593601
// 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());
594604
char* non_const_payload = const_cast<char*>(&payload[0]);
595605
uint8_t* msg_type_buffer = reinterpret_cast<uint8_t*>(non_const_payload);
596606

607+
// TODO: might crash if payload by chance does not contain ros msg type
597608
// deserialize ROS message type
598609
RosMsgType ros_msg_type;
599610
ros::serialization::IStream msg_type_stream(msg_type_buffer,

0 commit comments

Comments
 (0)