interface.h
Go to the documentation of this file.
1 
13 /*
14  * libmavconn
15  * Copyright 2013,2014,2015,2016 Vladimir Ermakov, All rights reserved.
16  *
17  * This file is part of the mavros package and subject to the license terms
18  * in the top-level LICENSE file of the mavros repository.
19  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
20  */
21 
22 #pragma once
23 
24 #include <boost/system/system_error.hpp>
25 
26 #include <deque>
27 #include <mutex>
28 #include <vector>
29 #include <atomic>
30 #include <chrono>
31 #include <thread>
32 #include <memory>
33 #include <sstream>
34 #include <cassert>
35 #include <stdexcept>
36 #include <unordered_map>
37 #include <mavconn/mavlink_dialect.h>
38 
39 
40 namespace mavconn {
42 using lock_guard = std::lock_guard<std::recursive_mutex>;
43 
45 static constexpr auto MAV_COMP_ID_UDP_BRIDGE = 240;
46 
48 enum class Framing : uint8_t {
49  incomplete = mavlink::MAVLINK_FRAMING_INCOMPLETE,
50  ok = mavlink::MAVLINK_FRAMING_OK,
51  bad_crc = mavlink::MAVLINK_FRAMING_BAD_CRC,
52  bad_signature = mavlink::MAVLINK_FRAMING_BAD_SIGNATURE,
53 };
54 
56 enum class Protocol : uint8_t {
57  V10 = 1,
58  V20 = 2
59 };
60 
64 class DeviceError : public std::runtime_error {
65 public:
69  template <typename T>
70  DeviceError(const char *module, T msg) :
71  std::runtime_error(make_message(module, msg))
72  { }
73 
74  template <typename T>
75  static std::string make_message(const char *module, T msg) {
76  std::ostringstream ss;
77  ss << "DeviceError:" << module << ":" << msg_to_string(msg);
78  return ss.str();
79  }
80 
81  static std::string msg_to_string(const char *description) {
82  return description;
83  }
84 
85  static std::string msg_to_string(int errnum) {
86  return ::strerror(errnum);
87  }
88 
89  static std::string msg_to_string(boost::system::system_error &err) {
90  return err.what();
91  }
92 };
93 
98 private:
99  MAVConnInterface(const MAVConnInterface&) = delete;
100 
101 public:
102  using ReceivedCb = std::function<void (const mavlink::mavlink_message_t *message, const Framing framing)>;
103  using ClosedCb = std::function<void (void)>;
104  using Ptr = std::shared_ptr<MAVConnInterface>;
105  using ConstPtr = std::shared_ptr<MAVConnInterface const>;
106  using WeakPtr = std::weak_ptr<MAVConnInterface>;
107 
108  struct IOStat {
109  size_t tx_total_bytes;
110  size_t rx_total_bytes;
111  float tx_speed;
112  float rx_speed;
113  };
114 
119  MAVConnInterface(uint8_t system_id = 1, uint8_t component_id = MAV_COMP_ID_UDP_BRIDGE);
120 
124  virtual void connect(
125  const ReceivedCb &cb_handle_message,
126  const ClosedCb &cb_handle_closed_port = ClosedCb()) = 0;
127 
131  virtual void close() = 0;
132 
143  virtual void send_message(const mavlink::mavlink_message_t *message) = 0;
144 
154  virtual void send_message(const mavlink::Message &message) {
155  send_message(message, this->comp_id);
156  }
157 
169  virtual void send_message(const mavlink::Message &message, const uint8_t src_compid) = 0;
170 
175  virtual void send_bytes(const uint8_t *bytes, size_t length) = 0;
176 
180  void send_message_ignore_drop(const mavlink::mavlink_message_t *message);
181 
187  void send_message_ignore_drop(const mavlink::Message &message) {
188  send_message_ignore_drop(message, this->comp_id);
189  }
190 
197  void send_message_ignore_drop(const mavlink::Message &message, const uint8_t src_compid);
198 
203 
204  virtual mavlink::mavlink_status_t get_status();
205  virtual IOStat get_iostat();
206  virtual bool is_open() = 0;
207 
208  inline uint8_t get_system_id() {
209  return sys_id;
210  }
211  inline void set_system_id(uint8_t sysid) {
212  sys_id = sysid;
213  }
214  inline uint8_t get_component_id() {
215  return comp_id;
216  }
217  inline void set_component_id(uint8_t compid) {
218  comp_id = compid;
219  }
220 
224  void set_protocol_version(Protocol pver);
226 
244  static Ptr open_url(
245  std::string url,
246  uint8_t system_id = 1,
247  uint8_t component_id = MAV_COMP_ID_UDP_BRIDGE,
248  const ReceivedCb &cb_handle_message = ReceivedCb(),
249  const ClosedCb &cb_handle_closed_port = ClosedCb());
250 
254  static Ptr open_url_no_connect(
255  std::string url,
256  uint8_t system_id = 1,
257  uint8_t component_id = MAV_COMP_ID_UDP_BRIDGE);
258 
259  static std::vector<std::string> get_known_dialects();
260 
261 protected:
262  uint8_t sys_id;
263  uint8_t comp_id;
264 
266  static constexpr size_t MAX_PACKET_SIZE = MAVLINK_MAX_PACKET_LEN + 16;
268  static constexpr size_t MAX_TXQ_SIZE = 1000;
269 
271  static std::unordered_map<mavlink::msgid_t, const mavlink::mavlink_msg_entry_t*> message_entries;
272 
274  size_t conn_id;
275 
276  inline mavlink::mavlink_status_t *get_status_p() {
277  return &m_parse_status;
278  }
279 
280  inline mavlink::mavlink_message_t *get_buffer_p() {
281  return &m_buffer;
282  }
283 
287  void parse_buffer(const char *pfx, uint8_t *buf, const size_t bufsize, size_t bytes_received);
288 
289  void iostat_tx_add(size_t bytes);
290  void iostat_rx_add(size_t bytes);
291 
292  void log_recv(const char *pfx, mavlink::mavlink_message_t &msg, Framing framing);
293  void log_send(const char *pfx, const mavlink::mavlink_message_t *msg);
294  void log_send_obj(const char *pfx, const mavlink::Message &msg);
295 
296 private:
297  friend const mavlink::mavlink_msg_entry_t* mavlink::mavlink_get_msg_entry(uint32_t msgid);
298 
299  mavlink::mavlink_status_t m_parse_status;
300  mavlink::mavlink_message_t m_buffer;
301  mavlink::mavlink_status_t m_mavlink_status;
302 
303  std::atomic<size_t> tx_total_bytes, rx_total_bytes;
304  std::recursive_mutex iostat_mutex;
306  std::chrono::time_point<steady_clock> last_iostat;
307 
309  static std::atomic<size_t> conn_id_counter;
310 
312  static std::once_flag init_flag;
313 
319  static void init_msg_entry();
320 };
321 } // namespace mavconn
mavconn::MAVConnInterface::close
virtual void close()=0
Close connection.
mavconn::MAVConnInterface::message_received_cb
ReceivedCb message_received_cb
Message receive callback.
Definition: interface.h:200
mavconn::DeviceError::DeviceError
DeviceError(const char *module, T msg)
Definition: interface.h:70
mavconn::MAVConnInterface::MAX_TXQ_SIZE
static constexpr size_t MAX_TXQ_SIZE
Maximum count of transmission buffers.
Definition: interface.h:268
mavconn::MAVConnInterface::set_protocol_version
void set_protocol_version(Protocol pver)
Definition: interface.cpp:173
mavconn::MAVConnInterface::iostat_tx_add
void iostat_tx_add(size_t bytes)
Definition: interface.cpp:86
mavconn::MAVConnInterface::conn_id
size_t conn_id
Channel number used for logging.
Definition: interface.h:274
mavconn::MAVConnInterface::is_open
virtual bool is_open()=0
mavconn::MAVConnInterface::set_component_id
void set_component_id(uint8_t compid)
Definition: interface.h:217
mavconn::MAVConnInterface::get_iostat
virtual IOStat get_iostat()
Definition: interface.cpp:61
mavconn::MAVConnInterface::send_message
virtual void send_message(const mavlink::Message &message)
Send message (child of mavlink::Message)
Definition: interface.h:154
mavconn::DeviceError::msg_to_string
static std::string msg_to_string(int errnum)
Definition: interface.h:85
mavconn::MAVConnInterface::IOStat::rx_speed
float rx_speed
current receive speed [B/s]
Definition: interface.h:112
mavconn::MAVConnInterface::m_parse_status
mavlink::mavlink_status_t m_parse_status
Definition: interface.h:299
mavconn::MAVConnInterface::tx_total_bytes
std::atomic< size_t > tx_total_bytes
Definition: interface.h:303
mavconn::DeviceError
Common exception for communication error.
Definition: interface.h:64
mavconn::lock_guard
std::lock_guard< std::recursive_mutex > lock_guard
Definition: interface.h:42
mavconn::MAVConnInterface::last_iostat
std::chrono::time_point< steady_clock > last_iostat
Definition: interface.h:306
mavconn::MAVConnInterface::log_recv
void log_recv(const char *pfx, mavlink::mavlink_message_t &msg, Framing framing)
Definition: interface.cpp:117
mavconn::MAVConnInterface::iostat_rx_add
void iostat_rx_add(size_t bytes)
Definition: interface.cpp:91
mavconn::MAVConnInterface::get_component_id
uint8_t get_component_id()
Definition: interface.h:214
mavconn::MAVConnInterface::ConstPtr
std::shared_ptr< MAVConnInterface const > ConstPtr
Definition: interface.h:105
mavconn::MAVConnInterface::IOStat::tx_total_bytes
size_t tx_total_bytes
total bytes transferred
Definition: interface.h:109
mavconn::Protocol::V10
@ V10
MAVLink v1.0.
mavconn::MAVConnInterface::init_msg_entry
static void init_msg_entry()
mavconn::MAVConnInterface::MAVConnInterface
MAVConnInterface(const MAVConnInterface &)=delete
mavconn::MAVConnInterface::open_url_no_connect
static Ptr open_url_no_connect(std::string url, uint8_t system_id=1, uint8_t component_id=MAV_COMP_ID_UDP_BRIDGE)
version of open_url() which do not perform connect()
Definition: interface.cpp:339
mavconn::MAVConnInterface::last_rx_total_bytes
size_t last_rx_total_bytes
Definition: interface.h:305
mavconn::DeviceError::msg_to_string
static std::string msg_to_string(boost::system::system_error &err)
Definition: interface.h:89
mavconn::Framing::ok
@ ok
mavconn::MAVConnInterface::WeakPtr
std::weak_ptr< MAVConnInterface > WeakPtr
Definition: interface.h:106
mavconn::MAVConnInterface::log_send
void log_send(const char *pfx, const mavlink::mavlink_message_t *msg)
Definition: interface.cpp:132
mavconn::Protocol
Protocol
MAVLink protocol version.
Definition: interface.h:56
mavconn::MAVConnInterface::IOStat::rx_total_bytes
size_t rx_total_bytes
total bytes received
Definition: interface.h:110
mavconn::MAVConnInterface::open_url
static Ptr open_url(std::string url, uint8_t system_id=1, uint8_t component_id=MAV_COMP_ID_UDP_BRIDGE, const ReceivedCb &cb_handle_message=ReceivedCb(), const ClosedCb &cb_handle_closed_port=ClosedCb())
Construct connection from URL.
Definition: interface.cpp:409
mavconn::MAVConnInterface::set_system_id
void set_system_id(uint8_t sysid)
Definition: interface.h:211
mavconn::MAVConnInterface
Generic mavlink interface.
Definition: interface.h:97
mavconn::steady_clock
std::chrono::steady_clock steady_clock
Definition: interface.h:41
mavconn::MAVConnInterface::get_status
virtual mavlink::mavlink_status_t get_status()
Definition: interface.cpp:56
mavconn::MAVConnInterface::get_known_dialects
static std::vector< std::string > get_known_dialects()
mavconn::MAVConnInterface::init_flag
static std::once_flag init_flag
init_msg_entry() once flag
Definition: interface.h:312
mavconn::DeviceError::msg_to_string
static std::string msg_to_string(const char *description)
Definition: interface.h:81
mavconn::MAVConnInterface::last_tx_total_bytes
size_t last_tx_total_bytes
Definition: interface.h:305
mavconn::Framing
Framing
Rx packer framing status. (same as mavlink::mavlink_framing_t)
Definition: interface.h:48
mavconn::MAVConnInterface::conn_id_counter
static std::atomic< size_t > conn_id_counter
monotonic counter (increment only)
Definition: interface.h:309
mavconn::MAVConnInterface::parse_buffer
void parse_buffer(const char *pfx, uint8_t *buf, const size_t bufsize, size_t bytes_received)
Definition: interface.cpp:96
mavconn::MAV_COMP_ID_UDP_BRIDGE
static constexpr auto MAV_COMP_ID_UDP_BRIDGE
Same as mavlink::common::MAV_COMPONENT::COMP_ID_UDP_BRIDGE.
Definition: interface.h:45
mavconn::MAVConnInterface::Ptr
std::shared_ptr< MAVConnInterface > Ptr
Definition: interface.h:104
mavconn::MAVConnInterface::sys_id
uint8_t sys_id
Connection System Id.
Definition: interface.h:262
mavconn::MAVConnInterface::comp_id
uint8_t comp_id
Connection Component Id.
Definition: interface.h:263
mavconn::MAVConnInterface::send_message_ignore_drop
void send_message_ignore_drop(const mavlink::mavlink_message_t *message)
Send message and ignore possible drop due to Tx queue limit.
Definition: interface.cpp:147
mavconn::Framing::incomplete
@ incomplete
mavconn::MAVConnInterface::iostat_mutex
std::recursive_mutex iostat_mutex
Definition: interface.h:304
mavconn::MAVConnInterface::log_send_obj
void log_send_obj(const char *pfx, const mavlink::Message &msg)
Definition: interface.cpp:142
mavconn::MAVConnInterface::get_status_p
mavlink::mavlink_status_t * get_status_p()
Definition: interface.h:276
mavconn::MAVConnInterface::IOStat
Definition: interface.h:108
mavconn::MAVConnInterface::connect
virtual void connect(const ReceivedCb &cb_handle_message, const ClosedCb &cb_handle_closed_port=ClosedCb())=0
Establish connection, automatically called by open_url()
mavconn::MAVConnInterface::send_message_ignore_drop
void send_message_ignore_drop(const mavlink::Message &message)
Send message and ignore possible drop due to Tx queue limit.
Definition: interface.h:187
mavconn::MAVConnInterface::MAX_PACKET_SIZE
static constexpr size_t MAX_PACKET_SIZE
Maximum mavlink packet size + some extra bytes for padding.
Definition: interface.h:266
mavconn::MAVConnInterface::ClosedCb
std::function< void(void)> ClosedCb
Definition: interface.h:103
mavconn::MAVConnInterface::IOStat::tx_speed
float tx_speed
current transfer speed [B/s]
Definition: interface.h:111
mavconn::MAVConnInterface::message_entries
static std::unordered_map< mavlink::msgid_t, const mavlink::mavlink_msg_entry_t * > message_entries
This map merge all dialect mavlink_msg_entry_t structs. Needed for packet parser.
Definition: interface.h:271
mavconn::MAVConnInterface::get_system_id
uint8_t get_system_id()
Definition: interface.h:208
mavconn::MAVConnInterface::port_closed_cb
ClosedCb port_closed_cb
Port closed notification callback.
Definition: interface.h:202
mavconn
Definition: interface.h:40
mavconn::MAVConnInterface::m_buffer
mavlink::mavlink_message_t m_buffer
Definition: interface.h:300
mavconn::MAVConnInterface::send_message
virtual void send_message(const mavlink::mavlink_message_t *message)=0
Send message (mavlink_message_t)
mavconn::Protocol::V20
@ V20
MAVLink v2.0.
mavconn::MAVConnInterface::get_buffer_p
mavlink::mavlink_message_t * get_buffer_p()
Definition: interface.h:280
mavconn::MAVConnInterface::m_mavlink_status
mavlink::mavlink_status_t m_mavlink_status
Definition: interface.h:301
mavconn::MAVConnInterface::rx_total_bytes
std::atomic< size_t > rx_total_bytes
Definition: interface.h:303
mavconn::Framing::bad_signature
@ bad_signature
mavconn::Framing::bad_crc
@ bad_crc
mavconn::DeviceError::make_message
static std::string make_message(const char *module, T msg)
Definition: interface.h:75
mavconn::MAVConnInterface::get_protocol_version
Protocol get_protocol_version()
Definition: interface.cpp:186
mavconn::MAVConnInterface::send_bytes
virtual void send_bytes(const uint8_t *bytes, size_t length)=0
Send raw bytes (for some quirks)
mavconn::MAVConnInterface::ReceivedCb
std::function< void(const mavlink::mavlink_message_t *message, const Framing framing)> ReceivedCb
Definition: interface.h:102


libmavconn
Author(s): Vladimir Ermakov
autogenerated on Tue May 6 2025 02:34:01