VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mavlink_stream.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_STREAM_H_
9 #define _UGCS_VSM_MAVLINK_STREAM_H_
10 
11 #include <ugcs/vsm/io_stream.h>
15 #include <tuple>
16 #include <queue>
17 
18 namespace ugcs {
19 namespace vsm {
20 
25 class Mavlink_stream: public std::enable_shared_from_this<Mavlink_stream>
26 {
28 
29 public:
32 
34  Mavlink_stream(Io_stream::Ref stream) :
35  stream(stream), decoder()
36  {
37  }
38 
40  Mavlink_stream(const Mavlink_stream&) = delete;
41 
45  {
46  return stream;
47  }
48 
50  Decoder&
52  {
53  return decoder;
54  }
55 
59  {
60  return demuxer;
61  }
62 
66  void
68  {
69  auto binder = [](Io_buffer::Ptr buffer, mavlink::MESSAGE_ID_TYPE message_id,
70  Mavlink_demuxer::System_id system_id, uint8_t component_id, uint32_t request_id,
71  Mavlink_stream::Ptr mav_stream)
72  {
73  mav_stream->demuxer.Demux(buffer, message_id, system_id, component_id, request_id);
74  };
75 
76  decoder.Register_handler(
77  Decoder::Make_decoder_handler(
78  binder, Shared_from_this()));
79  }
80 
82  void
83  Set_mavlink_v2(bool enable = true)
84  {
85  send_mavlink2 = enable;
86  }
87 
89  bool
91  {
92  return send_mavlink2;
93  }
94 
99  void
101  const mavlink::Payload_base& payload,
102  uint8_t system_id,
103  uint8_t component_id,
104  const std::chrono::milliseconds& timeout,
105  Operation_waiter::Timeout_handler timeout_handler,
106  const Request_completion_context::Ptr& completion_ctx)
107  {
108  Send_message(
109  payload,
110  system_id,
111  component_id,
112  timeout,
113  timeout_handler,
114  completion_ctx,
115  send_mavlink2);
116  }
117 
122  void
124  const mavlink::Payload_base& payload,
125  uint8_t system_id,
126  uint8_t component_id,
127  const std::chrono::milliseconds& timeout,
128  Operation_waiter::Timeout_handler timeout_handler,
129  const Request_completion_context::Ptr& completion_ctx,
130  bool mav2)
131  {
132  ASSERT(completion_ctx->Get_type() != Request_completion_context::Type::TEMPORAL);
133 
134  Io_buffer::Ptr buffer;
135  if (mav2) {
136  buffer = encoder.Encode_v2(payload, system_id, component_id);
137  } else {
138  buffer = encoder.Encode_v1(payload, system_id, component_id);
139  }
140 
141  Operation_waiter waiter = stream->Write(
142  buffer,
143  Make_dummy_callback<void, Io_result>(),
144  completion_ctx);
145  waiter.Timeout(timeout, timeout_handler, true, completion_ctx);
146 
147  write_ops.emplace(std::move(waiter));
148  Cleanup_write_ops();
149  }
150 
155  void
157  {
158  decoder.Disable();
159  demuxer.Disable();
160  stream = nullptr;
161  while (!write_ops.empty()) {
162  write_ops.front().Abort();
163  write_ops.pop();
164  }
165  }
166 
167 private:
169  Io_stream::Ref stream;
170 
172  Decoder decoder;
173 
175  Mavlink_demuxer demuxer;
176 
178  Mavlink_encoder encoder;
179 
181  void
182  Cleanup_write_ops()
183  {
184  while (!write_ops.empty()) {
185  if (write_ops.front().Is_done()) {
186  write_ops.pop();
187  continue;
188  }
189  /* Operations are completed sequentially. */
190  break;
191  }
192  }
193 
198  std::queue<Operation_waiter> write_ops;
199 
201  bool send_mavlink2 = false;
202 };
203 
204 } /* namespace vsm */
205 } /* namespace ugcs */
206 
207 #endif /* _UGCS_VSM_MAVLINK_STREAM_H_ */
void Timeout(std::chrono::milliseconds timeout, Timeout_handler timeout_handler=Timeout_handler(), bool cancel_operation=true, Request_completion_context::Ptr ctx=nullptr)
Schedule timeout for the operation.
Abstract stream for I/O operations.
#define ASSERT(x)
No action in release.
Definition: debug.h:68
Abstract I/O stream interface.
Definition: io_stream.h:66
Helper class for proxying callback invocation.
Definition: callback.h:694
Temporal context used for a separate request synchronization.
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
Class for synchronizing with request execution.
Definition: operation_waiter.h:24