VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mavlink_encoder.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
8 #ifndef _UGCS_VSM_MAVLINK_ENCODER_H_
9 #define _UGCS_VSM_MAVLINK_ENCODER_H_
10 
11 #include <ugcs/vsm/io_buffer.h>
12 #include <ugcs/vsm/mavlink.h>
13 
14 namespace ugcs {
15 namespace vsm {
16 
21 public:
30  uint8_t system_id, uint8_t component_id)
31  {
32  /* Reserve space for the whole packet. */
33  std::vector<uint8_t> data(mavlink::MAVLINK_1_HEADER_LEN + payload.Get_size_v1() +
34  sizeof(uint16_t));
35 
36  /* Fill the header. */
37  data[0] = mavlink::START_SIGN;
38  data[1] = payload.Get_size_v1();
39  data[2] = seq++;
40  data[3] = system_id;
41  data[4] = component_id;
42  ASSERT(payload.Get_id() < 256);
43  data[5] = static_cast<uint8_t>(payload.Get_id());
44 
45  /* If this affects performance, then Payload class should be redesigned
46  * to provide space for header and checksum in-place.
47  */
48  memcpy(&data[mavlink::MAVLINK_1_HEADER_LEN], payload.Get_buffer()->Get_data(),
49  payload.Get_size_v1());
50 
51  /* Don't include start sign. */
52  mavlink::Checksum sum(&data[1], mavlink::MAVLINK_1_HEADER_LEN + payload.Get_size_v1() - 1);
53  uint16_t sum_val = sum.Accumulate(payload.Get_extra_byte());
54  mavlink::Uint16 *wire_sum =
55  reinterpret_cast<mavlink::Uint16*>(&data[mavlink::MAVLINK_1_HEADER_LEN + payload.Get_size_v1()]);
56  *wire_sum = sum_val;
57 
58  return Io_buffer::Create(std::move(data));
59  }
68  uint8_t system_id, uint8_t component_id)
69  {
70  /* Reserve space for the whole packet. */
71  std::vector<uint8_t> data(mavlink::MAVLINK_2_HEADER_LEN + payload.Get_size_v2() +
72  sizeof(uint16_t));
73 
74  /* Fill the header. */
75  data[0] = mavlink::START_SIGN2;
76  data[2] = 0; // incompat_flags
77  data[3] = 0; // compat_flags
78  data[4] = seq++;
79  data[5] = system_id;
80  data[6] = component_id;
81  data[7] = static_cast<uint8_t>(payload.Get_id() >> 0);
82  data[8] = static_cast<uint8_t>(payload.Get_id() >> 8);
83  data[9] = static_cast<uint8_t>(payload.Get_id() >> 16);
84 
85  memcpy(&data[mavlink::MAVLINK_2_HEADER_LEN], payload.Get_buffer()->Get_data(), payload.Get_size_v2());
86 
87  auto packet_len = payload.Get_size_v2();
88 
89  // trim trailing zeroes.
90  for (; packet_len > 1 && data[mavlink::MAVLINK_2_HEADER_LEN + packet_len - 1] == 0; packet_len--) {
91  }
92 
93  data[1] = packet_len;
94 
95  /* Don't include start sign and any extension fields*/
96  mavlink::Checksum sum(&data[1], mavlink::MAVLINK_2_HEADER_LEN + packet_len - 1);
97 
98  uint16_t sum_val = sum.Accumulate(payload.Get_extra_byte());
99  mavlink::Uint16 *wire_sum =
100  reinterpret_cast<mavlink::Uint16*>(&data[mavlink::MAVLINK_2_HEADER_LEN + packet_len]);
101  *wire_sum = sum_val;
102 
103  data.resize(mavlink::MAVLINK_2_HEADER_LEN + packet_len + 2);
104 
105  return Io_buffer::Create(std::move(data));
106  }
107 
108 private:
110  uint8_t seq = 0;
111 };
112 
113 } /* namespace vsm */
114 } /* namespace ugcs */
115 
116 #endif /* _UGCS_VSM_MAVLINK_ENCODER_H_ */
static Ptr Create(Args &&...args)
Create an instance.
Definition: io_buffer.h:34
#define ASSERT(x)
No action in release.
Definition: debug.h:68
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
Io_buffer class implementation.