interface.cpp
Go to the documentation of this file.
1 
9 /*
10  * libmavconn
11  * Copyright 2013,2014,2015,2016 Vladimir Ermakov, All rights reserved.
12  *
13  * This file is part of the mavros package and subject to the license terms
14  * in the top-level LICENSE file of the mavros repository.
15  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
16  */
17 
18 #include <set>
19 #include <cassert>
20 
22 #include <mavconn/interface.h>
23 #include <mavconn/msgbuffer.h>
24 #include <mavconn/serial.h>
25 #include <mavconn/udp.h>
26 #include <mavconn/tcp.h>
27 
28 namespace mavconn {
29 #define PFX "mavconn: "
30 
31 using mavlink::mavlink_message_t;
32 using mavlink::mavlink_status_t;
33 
34 // static members
35 std::once_flag MAVConnInterface::init_flag;
36 std::unordered_map<mavlink::msgid_t, const mavlink::mavlink_msg_entry_t*> MAVConnInterface::message_entries {};
37 std::atomic<size_t> MAVConnInterface::conn_id_counter {0};
38 
39 
40 MAVConnInterface::MAVConnInterface(uint8_t system_id, uint8_t component_id) :
41  sys_id(system_id),
42  comp_id(component_id),
43  m_status {},
44  m_buffer {},
45  tx_total_bytes(0),
46  rx_total_bytes(0),
49  last_iostat(steady_clock::now())
50 {
51  conn_id = conn_id_counter.fetch_add(1);
52  std::call_once(init_flag, init_msg_entry);
53 }
54 
56 {
57  return m_status;
58 }
59 
61 {
62  std::lock_guard<std::recursive_mutex> lock(iostat_mutex);
63  IOStat stat;
64 
67 
68  auto d_tx = stat.tx_total_bytes - last_tx_total_bytes;
69  auto d_rx = stat.rx_total_bytes - last_rx_total_bytes;
72 
73  auto now = steady_clock::now();
74  auto dt = now - last_iostat;
75  last_iostat = now;
76 
77  float dt_s = std::chrono::duration_cast<std::chrono::seconds>(dt).count();
78 
79  stat.tx_speed = d_tx / dt_s;
80  stat.rx_speed = d_rx / dt_s;
81 
82  return stat;
83 }
84 
86 {
87  tx_total_bytes += bytes;
88 }
89 
91 {
92  rx_total_bytes += bytes;
93 }
94 
95 void MAVConnInterface::parse_buffer(const char *pfx, uint8_t *buf, const size_t bufsize, size_t bytes_received)
96 {
97  mavlink::mavlink_status_t status;
98  mavlink::mavlink_message_t message;
99 
100  assert(bufsize >= bytes_received);
101 
102  iostat_rx_add(bytes_received);
103  for (; bytes_received > 0; bytes_received--) {
104  auto c = *buf++;
105 
106  // based on mavlink_parse_char()
107  auto msg_received = static_cast<Framing>(mavlink::mavlink_frame_char_buffer(&m_buffer, &m_status, c, &message, &status));
108  if (msg_received == Framing::bad_crc || msg_received == Framing::bad_signature) {
109  mavlink::_mav_parse_error(&m_status);
110  m_status.msg_received = mavlink::MAVLINK_FRAMING_INCOMPLETE;
111  m_status.parse_state = mavlink::MAVLINK_PARSE_STATE_IDLE;
112  if (c == MAVLINK_STX) {
113  m_status.parse_state = mavlink::MAVLINK_PARSE_STATE_GOT_STX;
114  m_buffer.len = 0;
115  mavlink::mavlink_start_checksum(&m_buffer);
116  }
117  }
118 
119  if (msg_received != Framing::incomplete) {
120  log_recv(pfx, message, msg_received);
121 
123  message_received_cb(&message, msg_received);
124  }
125  }
126 }
127 
128 void MAVConnInterface::log_recv(const char *pfx, mavlink_message_t &msg, Framing framing)
129 {
130  const char *framing_str = (framing == Framing::ok) ? "OK" :
131  (framing == Framing::bad_crc) ? "!CRC" :
132  (framing == Framing::bad_signature) ? "!SIG" : "ERR";
133 
134  const char *proto_version_str = (msg.magic == MAVLINK_STX) ? "v2.0" : "v1.0";
135 
136  CONSOLE_BRIDGE_logDebug("%s%zu: recv: %s %4s Message-Id: %u [%u bytes] IDs: %u.%u Seq: %u",
137  pfx, conn_id,
138  proto_version_str,
139  framing_str,
140  msg.msgid, msg.len, msg.sysid, msg.compid, msg.seq);
141 }
142 
143 void MAVConnInterface::log_send(const char *pfx, const mavlink_message_t *msg)
144 {
145  const char *proto_version_str = (msg->magic == MAVLINK_STX) ? "v2.0" : "v1.0";
146 
147  CONSOLE_BRIDGE_logDebug("%s%zu: send: %s Message-Id: %u [%u bytes] IDs: %u.%u Seq: %u",
148  pfx, conn_id,
149  proto_version_str,
150  msg->msgid, msg->len, msg->sysid, msg->compid, msg->seq);
151 }
152 
153 void MAVConnInterface::log_send_obj(const char *pfx, const mavlink::Message &msg)
154 {
155  CONSOLE_BRIDGE_logDebug("%s%zu: send: %s", pfx, conn_id, msg.to_yaml().c_str());
156 }
157 
158 void MAVConnInterface::send_message_ignore_drop(const mavlink::mavlink_message_t *msg)
159 {
160  try {
161  send_message(msg);
162  }
163  catch (std::length_error &e) {
164  CONSOLE_BRIDGE_logError(PFX "%zu: DROPPED Message-Id %u [%u bytes] IDs: %u.%u Seq: %u: %s",
165  conn_id,
166  msg->msgid, msg->len, msg->sysid, msg->compid, msg->seq,
167  e.what());
168  }
169 }
170 
171 void MAVConnInterface::send_message_ignore_drop(const mavlink::Message &msg, uint8_t source_compid)
172 {
173  try {
174  send_message(msg, source_compid);
175  }
176  catch (std::length_error &e) {
177  CONSOLE_BRIDGE_logError(PFX "%zu: DROPPED Message %s: %s",
178  conn_id,
179  msg.get_name().c_str(),
180  e.what());
181  }
182 }
183 
185 {
186  if (pver == Protocol::V10)
188  else
190 }
191 
193 {
195  return Protocol::V10;
196  else
197  return Protocol::V20;
198 }
199 
203 static void url_parse_host(std::string host,
204  std::string &host_out, int &port_out,
205  const std::string def_host, const int def_port)
206 {
207  std::string port;
208 
209  auto sep_it = std::find(host.begin(), host.end(), ':');
210  if (sep_it == host.end()) {
211  // host
212  if (!host.empty()) {
213  host_out = host;
214  port_out = def_port;
215  }
216  else {
217  host_out = def_host;
218  port_out = def_port;
219  }
220  return;
221  }
222 
223  if (sep_it == host.begin()) {
224  // :port
225  host_out = def_host;
226  }
227  else {
228  // host:port
229  host_out.assign(host.begin(), sep_it);
230  }
231 
232  port.assign(sep_it + 1, host.end());
233  port_out = std::stoi(port);
234 }
235 
239 static void url_parse_query(std::string query, uint8_t &sysid, uint8_t &compid)
240 {
241  const std::string ids_end("ids=");
242  std::string sys, comp;
243 
244  if (query.empty())
245  return;
246 
247  auto ids_it = std::search(query.begin(), query.end(),
248  ids_end.begin(), ids_end.end());
249  if (ids_it == query.end()) {
250  CONSOLE_BRIDGE_logWarn(PFX "URL: unknown query arguments");
251  return;
252  }
253 
254  std::advance(ids_it, ids_end.length());
255  auto comma_it = std::find(ids_it, query.end(), ',');
256  if (comma_it == query.end()) {
257  CONSOLE_BRIDGE_logError(PFX "URL: no comma in ids= query");
258  return;
259  }
260 
261  sys.assign(ids_it, comma_it);
262  comp.assign(comma_it + 1, query.end());
263 
264  sysid = std::stoi(sys);
265  compid = std::stoi(comp);
266 
267  CONSOLE_BRIDGE_logDebug(PFX "URL: found system/component id = [%u, %u]", sysid, compid);
268 }
269 
271  std::string path, std::string query,
272  uint8_t system_id, uint8_t component_id, bool hwflow)
273 {
274  std::string file_path;
275  int baudrate;
276 
277  // /dev/ttyACM0:57600
279  url_parse_query(query, system_id, component_id);
280 
281  return std::make_shared<MAVConnSerial>(system_id, component_id,
282  file_path, baudrate, hwflow);
283 }
284 
286  std::string hosts, std::string query,
287  uint8_t system_id, uint8_t component_id, bool is_udpb, bool permanent_broadcast)
288 {
289  std::string bind_pair, remote_pair;
290  std::string bind_host, remote_host;
291  int bind_port, remote_port;
292 
293  auto sep_it = std::find(hosts.begin(), hosts.end(), '@');
294  if (sep_it == hosts.end()) {
295  CONSOLE_BRIDGE_logError(PFX "UDP URL should contain @!");
296  throw DeviceError("url", "UDP separator not found");
297  }
298 
299  bind_pair.assign(hosts.begin(), sep_it);
300  remote_pair.assign(sep_it + 1, hosts.end());
301 
302  // udp://0.0.0.0:14555@:14550
303  url_parse_host(bind_pair, bind_host, bind_port, "0.0.0.0", MAVConnUDP::DEFAULT_BIND_PORT);
304  url_parse_host(remote_pair, remote_host, remote_port, MAVConnUDP::DEFAULT_REMOTE_HOST, MAVConnUDP::DEFAULT_REMOTE_PORT);
305  url_parse_query(query, system_id, component_id);
306 
307  if (is_udpb)
309 
310  return std::make_shared<MAVConnUDP>(system_id, component_id,
311  bind_host, bind_port,
312  remote_host, remote_port);
313 }
314 
316  std::string host, std::string query,
317  uint8_t system_id, uint8_t component_id)
318 {
319  std::string server_host;
320  int server_port;
321 
322  // tcp://localhost:5760
323  url_parse_host(host, server_host, server_port, "localhost", 5760);
324  url_parse_query(query, system_id, component_id);
325 
326  return std::make_shared<MAVConnTCPClient>(system_id, component_id,
327  server_host, server_port);
328 }
329 
331  std::string host, std::string query,
332  uint8_t system_id, uint8_t component_id)
333 {
334  std::string bind_host;
335  int bind_port;
336 
337  // tcp-l://0.0.0.0:5760
338  url_parse_host(host, bind_host, bind_port, "0.0.0.0", 5760);
339  url_parse_query(query, system_id, component_id);
340 
341  return std::make_shared<MAVConnTCPServer>(system_id, component_id,
342  bind_host, bind_port);
343 }
344 
346  uint8_t system_id, uint8_t component_id)
347 {
348  /* Based on code found here:
349  * http://stackoverflow.com/questions/2616011/easy-way-to-parse-a-url-in-c-cross-platform
350  */
351 
352  const std::string proto_end("://");
353  std::string proto;
354  std::string host;
355  std::string path;
356  std::string query;
357 
358  auto proto_it = std::search(
359  url.begin(), url.end(),
360  proto_end.begin(), proto_end.end());
361  if (proto_it == url.end()) {
362  // looks like file path
363  CONSOLE_BRIDGE_logDebug(PFX "URL: %s: looks like file path", url.c_str());
364  return url_parse_serial(url, "", system_id, component_id, false);
365  }
366 
367  // copy protocol
368  proto.reserve(std::distance(url.begin(), proto_it));
369  std::transform(url.begin(), proto_it,
370  std::back_inserter(proto),
371  std::ref(tolower));
372 
373  // copy host
374  std::advance(proto_it, proto_end.length());
375  auto path_it = std::find(proto_it, url.end(), '/');
376  std::transform(proto_it, path_it,
377  std::back_inserter(host),
378  std::ref(tolower));
379 
380  // copy path, and query if exists
381  auto query_it = std::find(path_it, url.end(), '?');
382  path.assign(path_it, query_it);
383  if (query_it != url.end())
384  ++query_it;
385  query.assign(query_it, url.end());
386 
387  CONSOLE_BRIDGE_logDebug(PFX "URL: %s: proto: %s, host: %s, path: %s, query: %s",
388  url.c_str(), proto.c_str(), host.c_str(),
389  path.c_str(), query.c_str());
390 
391  if (proto == "udp")
392  return url_parse_udp(host, query, system_id, component_id, false, false);
393  else if (proto == "udp-b")
394  return url_parse_udp(host, query, system_id, component_id, true, false);
395  else if (proto == "udp-pb")
396  return url_parse_udp(host, query, system_id, component_id, true, true);
397  else if (proto == "tcp")
398  return url_parse_tcp_client(host, query, system_id, component_id);
399  else if (proto == "tcp-l")
400  return url_parse_tcp_server(host, query, system_id, component_id);
401  else if (proto == "serial")
402  return url_parse_serial(path, query, system_id, component_id, false);
403  else if (proto == "serial-hwfc")
404  return url_parse_serial(path, query, system_id, component_id, true);
405  else
406  throw DeviceError("url", "Unknown URL type");
407 }
408 } // namespace mavconn
virtual void send_message(const mavlink::mavlink_message_t *message)=0
Send message (mavlink_message_t)
virtual mavlink::mavlink_status_t get_status()
Definition: interface.cpp:55
float rx_speed
current receive speed [B/s]
Definition: interface.h:112
void log_send(const char *pfx, const mavlink::mavlink_message_t *msg)
Definition: interface.cpp:143
void parse_buffer(const char *pfx, uint8_t *buf, const size_t bufsize, size_t bytes_received)
Definition: interface.cpp:95
static void init_msg_entry()
static std::once_flag init_flag
init_msg_entry() once flag
Definition: interface.h:292
#define CONSOLE_BRIDGE_logWarn(fmt,...)
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:158
Common exception for communication error.
Definition: interface.h:64
static MAVConnInterface::Ptr url_parse_tcp_client(std::string host, std::string query, uint8_t system_id, uint8_t component_id)
Definition: interface.cpp:315
static constexpr auto DEFAULT_BIND_PORT
Definition: udp.h:35
#define CONSOLE_BRIDGE_logDebug(fmt,...)
void log_send_obj(const char *pfx, const mavlink::Message &msg)
Definition: interface.cpp:153
ReceivedCb message_received_cb
Message receive callback.
Definition: interface.h:193
std::chrono::time_point< steady_clock > last_iostat
Definition: interface.h:286
float tx_speed
current transfer speed [B/s]
Definition: interface.h:111
static MAVConnInterface::Ptr url_parse_serial(std::string path, std::string query, uint8_t system_id, uint8_t component_id, bool hwflow)
Definition: interface.cpp:270
MAVConn class interface.
static void url_parse_host(std::string host, std::string &host_out, int &port_out, const std::string def_host, const int def_port)
Definition: interface.cpp:203
void iostat_tx_add(size_t bytes)
Definition: interface.cpp:85
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:252
size_t conn_id
Channel number used for logging.
Definition: interface.h:255
#define CONSOLE_BRIDGE_logError(fmt,...)
static constexpr auto BROADCAST_REMOTE_HOST
Markers for broadcast modes. Not valid domain names.
Definition: udp.h:39
MAVConn message buffer class (internal)
MAVConn Serial link class.
static constexpr auto DEFAULT_DEVICE
Definition: serial.h:32
static constexpr auto DEFAULT_REMOTE_PORT
Definition: udp.h:37
static MAVConnInterface::Ptr url_parse_udp(std::string hosts, std::string query, uint8_t system_id, uint8_t component_id, bool is_udpb, bool permanent_broadcast)
Definition: interface.cpp:285
virtual IOStat get_iostat()
Definition: interface.cpp:60
MAVConnInterface(const MAVConnInterface &)=delete
mavlink::mavlink_message_t m_buffer
Definition: interface.h:281
std::atomic< size_t > rx_total_bytes
Definition: interface.h:283
size_t rx_total_bytes
total bytes received
Definition: interface.h:110
size_t tx_total_bytes
total bytes transferred
Definition: interface.h:109
MAVConn console-bridge compatibility header.
static constexpr auto PERMANENT_BROADCAST_REMOTE_HOST
Definition: udp.h:40
Protocol get_protocol_version()
Definition: interface.cpp:192
static constexpr auto DEFAULT_BAUDRATE
Definition: serial.h:33
mavlink::mavlink_status_t m_status
Definition: interface.h:280
static Ptr open_url(std::string url, uint8_t system_id=1, uint8_t component_id=MAV_COMP_ID_UDP_BRIDGE)
Construct connection from URL.
Definition: interface.cpp:345
static std::atomic< size_t > conn_id_counter
monotonic counter (increment only)
Definition: interface.h:289
void set_protocol_version(Protocol pver)
Definition: interface.cpp:184
#define PFX
Definition: interface.cpp:29
void iostat_rx_add(size_t bytes)
Definition: interface.cpp:90
Protocol
MAVLink protocol version.
Definition: interface.h:56
static void url_parse_query(std::string query, uint8_t &sysid, uint8_t &compid)
Definition: interface.cpp:239
static constexpr auto DEFAULT_REMOTE_HOST
Definition: udp.h:36
std::recursive_mutex iostat_mutex
Definition: interface.h:284
static MAVConnInterface::Ptr url_parse_tcp_server(std::string host, std::string query, uint8_t system_id, uint8_t component_id)
Definition: interface.cpp:330
void log_recv(const char *pfx, mavlink::mavlink_message_t &msg, Framing framing)
Definition: interface.cpp:128
std::atomic< size_t > tx_total_bytes
Definition: interface.h:283
Framing
Rx packer framing status. (same as mavlink::mavlink_framing_t)
Definition: interface.h:48
std::shared_ptr< MAVConnInterface > Ptr
Definition: interface.h:104


libmavconn
Author(s): Vladimir Ermakov
autogenerated on Mon Jul 8 2019 03:20:07