msgbuffer.h
Go to the documentation of this file.
1 
9 /*
10  * libmavconn
11  * Copyright 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 #pragma once
19 
20 #include <cassert>
21 #include <mavconn/mavlink_dialect.h>
22 
23 namespace mavconn {
27 struct MsgBuffer {
29  static constexpr ssize_t MAX_SIZE = MAVLINK_MAX_PACKET_LEN + 16;
30  uint8_t data[MAX_SIZE];
31  ssize_t len;
32  ssize_t pos;
33 
35  pos(0),
36  len(0)
37  { }
38 
42  explicit MsgBuffer(const mavlink::mavlink_message_t *msg) :
43  pos(0)
44  {
45  len = mavlink::mavlink_msg_to_send_buffer(data, msg);
46  // paranoic check, it must be less than MAVLINK_MAX_PACKET_LEN
47  assert(len < MAX_SIZE);
48  }
49 
53  MsgBuffer(const mavlink::Message &obj, mavlink::mavlink_status_t *status, uint8_t sysid, uint8_t compid) :
54  pos(0)
55  {
56  mavlink::mavlink_message_t msg;
57  mavlink::MsgMap map(msg);
58 
59  auto mi = obj.get_message_info();
60 
61  obj.serialize(map);
62  mavlink::mavlink_finalize_message_buffer(&msg, sysid, compid, status, mi.min_length, mi.length, mi.crc_extra);
63 
64  len = mavlink::mavlink_msg_to_send_buffer(data, &msg);
65  // paranoic check, it must be less than MAVLINK_MAX_PACKET_LEN
66  assert(len < MAX_SIZE);
67  }
68 
73  MsgBuffer(const uint8_t *bytes, ssize_t nbytes) :
74  pos(0),
75  len(nbytes)
76  {
77  assert(0 < nbytes && nbytes < MAX_SIZE);
78  memcpy(data, bytes, nbytes);
79  }
80 
81  virtual ~MsgBuffer() {
82  pos = 0;
83  len = 0;
84  }
85 
86  uint8_t *dpos() {
87  return data + pos;
88  }
89 
90  ssize_t nbytes() {
91  return len - pos;
92  }
93 };
94 } // namespace mavconn
uint8_t * dpos()
Definition: msgbuffer.h:86
uint8_t data[MAX_SIZE]
Definition: msgbuffer.h:30
MsgBuffer(const mavlink::Message &obj, mavlink::mavlink_status_t *status, uint8_t sysid, uint8_t compid)
Buffer constructor for mavlink::Message derived object.
Definition: msgbuffer.h:53
MsgBuffer(const mavlink::mavlink_message_t *msg)
Buffer constructor from mavlink_message_t.
Definition: msgbuffer.h:42
MsgBuffer(const uint8_t *bytes, ssize_t nbytes)
Buffer constructor for send_bytes()
Definition: msgbuffer.h:73
virtual ~MsgBuffer()
Definition: msgbuffer.h:81
Message buffer for internal use in libmavconn.
Definition: msgbuffer.h:27
ssize_t nbytes()
Definition: msgbuffer.h:90
static constexpr ssize_t MAX_SIZE
Maximum buffer size with padding for CRC bytes (280 + padding)
Definition: msgbuffer.h:29


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