VSM C++ SDK
Vehicle Specific Modules SDK
mavlink_stream.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
8 #ifndef _MAVLINK_STREAM_H_
9 #define _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 template<typename Mavlink_kind>
26 class Mavlink_stream: public std::enable_shared_from_this<Mavlink_stream<Mavlink_kind>>
27 {
29 
30 public:
31 
33  typedef Mavlink_decoder<Mavlink_kind> Decoder;
34 
36  Mavlink_stream(Io_stream::Ref stream) :
37  stream(stream), decoder()
38  {
39  }
40 
42  Mavlink_stream(const Mavlink_stream&) = delete;
43 
47  {
48  return stream;
49  }
50 
52  Decoder&
54  {
55  return decoder;
56  }
57 
61  {
62  return demuxer;
63  }
64 
68  void
70  {
71  auto binder = [](Io_buffer::Ptr buffer, mavlink::MESSAGE_ID_TYPE message_id,
72  Mavlink_demuxer::System_id system_id, uint8_t component_id, uint32_t request_id,
73  Mavlink_stream::Ptr mav_stream)
74  {
75  mav_stream->demuxer.Demux(buffer, message_id, system_id, component_id, request_id);
76  };
77 
78  decoder.Register_handler(
79  Decoder::Make_decoder_handler(
80  binder, Shared_from_this()));
81  }
82 
87  void
89  const mavlink::Payload_base& payload,
90  typename Mavlink_kind::System_id system_id,
91  uint8_t component_id,
92  const std::chrono::milliseconds& timeout,
93  Operation_waiter::Timeout_handler timeout_handler,
94  const Request_completion_context::Ptr& completion_ctx)
95  {
96  ASSERT(completion_ctx->Get_type() != Request_completion_context::Type::TEMPORAL);
97 
98  Io_buffer::Ptr buffer = encoder.Encode<Mavlink_kind>(payload, system_id, component_id);
99 
100  Operation_waiter waiter = stream->Write(
101  buffer,
102  Make_dummy_callback<void, Io_result>(),
103  completion_ctx);
104  waiter.Timeout(timeout, timeout_handler, true, completion_ctx);
105 
106  write_ops.emplace(std::move(waiter));
107  Cleanup_write_ops();
108  }
109 
114  void
116  const mavlink::Payload_base& payload,
117  typename Mavlink_kind::System_id system_id,
118  uint8_t component_id,
119  uint32_t request_id,
120  const std::chrono::milliseconds& timeout,
121  Operation_waiter::Timeout_handler timeout_handler,
122  const Request_completion_context::Ptr& completion_ctx)
123  {
124  ASSERT(completion_ctx->Get_type() != Request_completion_context::Type::TEMPORAL);
125 
126  Io_buffer::Ptr buffer = encoder.Encode<Mavlink_kind>(payload, system_id, component_id, request_id);
127 
128  Operation_waiter waiter = stream->Write(
129  buffer,
130  Make_dummy_callback<void, Io_result>(),
131  completion_ctx);
132  waiter.Timeout(timeout, timeout_handler, true, completion_ctx);
133 
134  write_ops.emplace(std::move(waiter));
135  Cleanup_write_ops();
136  }
137 
142  void
144  {
145  decoder.Disable();
146  demuxer.Disable();
147  stream = nullptr;
148  while (!write_ops.empty()) {
149  write_ops.front().Abort();
150  write_ops.pop();
151  }
152  }
153 
154 private:
155 
157  Io_stream::Ref stream;
158 
160  Decoder decoder;
161 
163  Mavlink_demuxer demuxer;
164 
166  Mavlink_encoder encoder;
167 
169  void
170  Cleanup_write_ops()
171  {
172  while (!write_ops.empty()) {
173  if (write_ops.front().Is_done()) {
174  write_ops.pop();
175  continue;
176  }
177  /* Operations are completed sequentially. */
178  break;
179  }
180  }
181 
186  std::queue<Operation_waiter> write_ops;
187 };
188 
189 } /* namespace vsm */
190 } /* namespace ugcs */
191 
192 #endif /* _MAVLINK_STREAM_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
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:54
Helper class for proxying callback invocation.
Definition: callback.h:691
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