24 #ifndef GOBY_MIDDLEWARE_IO_MAVLINK_COMMON_H
25 #define GOBY_MIDDLEWARE_IO_MAVLINK_COMMON_H
33 #include <mavlink/v2.0/common/common.hpp>
67 auto msg_out_callback = [
this](std::shared_ptr<const mavlink::mavlink_message_t>
msg,
68 const std::string&
type) {
70 goby::glog <<
"writing msg [sysid: " <<
static_cast<int>(
msg->sysid)
71 <<
", compid: " <<
static_cast<int>(
msg->compid)
72 <<
"] of msgid: " <<
static_cast<int>(
msg->msgid) << std::endl;
73 auto io_msg = std::make_shared<goby::middleware::protobuf::IOData>();
76 io_msg->set_data(&data[0], data.size());
81 .template subscribe_type_regex<line_out_group, mavlink::mavlink_message_t>(
89 void try_parse(std::size_t bytes_transferred);
90 std::array<char, MAVLINK_MAX_PACKET_LEN>&
buffer() {
return buffer_; }
95 msg_ = std::make_shared<mavlink::mavlink_message_t>();
102 std::array<char, MAVLINK_MAX_PACKET_LEN> buffer_;
103 std::shared_ptr<mavlink::mavlink_message_t> msg_ =
104 std::make_shared<mavlink::mavlink_message_t>();
105 mavlink::mavlink_message_t msg_buffer_{};
106 mavlink::mavlink_status_t status_{}, status_buffer_{};
118 IOConfig>::try_parse(std::size_t bytes_transferred)
120 auto bytes_begin = buffer_.begin(), bytes_end = buffer_.begin() + bytes_transferred;
121 for (
auto c = bytes_begin; c != bytes_end; ++c)
125 auto res = mavlink::mavlink_frame_char_buffer(&msg_buffer_, &status_buffer_, *c,
126 msg_.get(), &status_);
129 case mavlink::MAVLINK_FRAMING_BAD_CRC:
130 if (status_.parse_state != mavlink::MAVLINK_PARSE_STATE_IDLE)
134 else if (mavlink::mavlink_get_msg_entry(msg_->msgid) ==
nullptr)
137 goby::glog <<
"BAD CRC decoding MAVLink msg, but "
138 "forwarding because we don't know this msgid"
150 case mavlink::MAVLINK_FRAMING_OK:
155 this->publish_in(msg_);
157 std::array<uint8_t, MAVLINK_MAX_PACKET_LEN> buffer;
158 auto length = mavlink::mavlink_msg_to_send_buffer(&buffer[0], msg_.get());
159 std::string bytes(buffer.begin(), buffer.begin() + length);
160 this->handle_read_success(length, bytes);
164 case mavlink::MAVLINK_FRAMING_INCOMPLETE:
break;
166 case mavlink::MAVLINK_FRAMING_BAD_SIGNATURE:
172 <<
" returned while decoding MAVLink msg"