Bridge 2 to 1

#include <iostream>

// include ROS 1
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include "ros/ros.h"
#include "std_msgs/String.h"
#ifdef __clang__
# pragma clang diagnostic pop
#endif

// include ROS 2
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

ros::Publisher pub;

void chatterCallback(const std_msgs::msg::String::SharedPtr ros2_msg)
{
  // print heard message
  std::cout << "I heard: [" << ros2_msg->data << "]" << std::endl;
  // create ros2_msg variable
  std_msgs::String ros1_msg;
  // save ROS 1 into ROS 2 variable
  ros1_msg.data = ros2_msg->data;
  // print
  std::cout << "Passing along: [" << ros1_msg.data << "]" << std::endl;
  // publish ROS 2 message
  pub.publish(ros1_msg);
}

int main(int argc, char * argv[])
{
  // ROS 1 node and publisher
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
  pub = n.advertise<std_msgs::String>("chatter", 10);

  // ROS 2 node and subscriber
  rclcpp::init(argc, argv);
  auto node = rclcpp::node::Node::make_shared("listener");
  auto sub = node->create_subscription<std_msgs::msg::String>(
    "chatter", chatterCallback, rmw_qos_profile_sensor_data);

  rclcpp::spin(node);

  return 0;
}