VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
android_serial_processor.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 
7 #ifndef _UGCS_VSM_ANDROID_SERIAL_PROCESSOR_H_
8 #define _UGCS_VSM_ANDROID_SERIAL_PROCESSOR_H_
9 
10 #ifndef ANDROID
11 #error "This file should be included only for Android target"
12 #endif
13 
15 #include <ugcs/vsm/java.h>
16 
17 namespace ugcs {
18 namespace vsm {
19 
20 extern "C" {
21 
22 JNIEXPORT void JNICALL
23 Java_com_ugcs_vsm_Vsm_StreamReady(JNIEnv *env, jobject _this, jint streamId);
24 
25 JNIEXPORT void JNICALL
26 Java_com_ugcs_vsm_Vsm_StreamClosed(JNIEnv *env, jobject _this, jint streamId);
27 
28 JNIEXPORT void JNICALL
29 Java_com_ugcs_vsm_Vsm_StreamWriteComplete(JNIEnv *env, jobject _this, jint streamId,
30  jboolean succeeded);
31 
32 JNIEXPORT void JNICALL
33 Java_com_ugcs_vsm_Vsm_StreamReadComplete(JNIEnv *env, jobject _this,
34  jint streamId, jobject buf);
35 
36 }
37 
43 
44 public:
46 
47  class Stream: public Io_stream {
49 
50  public:
53 
54  using Mode = ugcs::vsm::Serial_processor::Stream::Mode;
55 
56  Stream(Android_serial_processor::Weak_ptr processor,
57  const std::string &name, const Mode &mode);
58 
59  const Mode &
60  Get_mode() const
61  {
62  return mode;
63  }
64 
65  private:
66  friend class Android_serial_processor;
67 
70  Mode mode;
72  int id = -1;
73 
75  virtual Operation_waiter
76  Write_impl(Io_buffer::Ptr buffer, Offset offset,
77  Write_handler completion_handler,
79 
81  virtual Operation_waiter
82  Read_impl(size_t max_to_read, size_t min_to_read, Offset offset,
83  Read_handler completion_handler,
85 
87  virtual Operation_waiter
88  Close_impl(Close_handler completion_handler,
90 
91  void
92  Complete_open(bool succeeded)
93  {
94  state = succeeded ? State::OPENED : State::CLOSED;
95  }
96 
97  void
98  Complete_close()
99  {
100  state = State::CLOSED;
101  id = -1;
102  }
103  };
104 
106  template <typename... Args>
107  static Ptr
108  Get_instance(Args &&... args)
109  {
110  return singleton.Get_instance(std::forward<Args>(args)...);
111  }
112 
117 
118  static std::list<std::string>
119  Enumerate_port_names();
120 
128  Open(const std::string &port_name, const Stream::Mode &mode = Stream::Mode(),
129  Open_handler completion_handler = Make_dummy_callback<void, Stream::Ref>(),
131 
132 private:
133  friend void Java_com_ugcs_vsm_Vsm_StreamReady(
134  JNIEnv *env, jobject _this, jint streamId);
135  friend void Java_com_ugcs_vsm_Vsm_StreamClosed(
136  JNIEnv *env, jobject _this, jint streamId);
137  friend void Java_com_ugcs_vsm_Vsm_StreamWriteComplete(
138  JNIEnv *env, jobject _this, jint streamId, jboolean succeeded);
139  friend void Java_com_ugcs_vsm_Vsm_StreamReadComplete(
140  JNIEnv *env, jobject _this, jint streamId, jobject buf);
141 
142  class Open_request: public Request {
143  DEFINE_COMMON_CLASS(Open_request, Request)
144  public:
145  using Request::Request;
146 
147  bool succeeded = false;
148  };
149 
150  class Write_request: public Request {
151  DEFINE_COMMON_CLASS(Write_request, Request)
152  public:
153  Stream::Ref stream;
154  Io_buffer::Ptr buf;
155  Io_result &result_ref;
156 
157  Write_request(Stream::Ref stream, Io_buffer::Ptr buf, Io_result &result_ref):
158  stream(stream), buf(buf), result_ref(result_ref)
159  {}
160 
161  void
162  Complete(Io_result result)
163  {
164  result_ref = result;
166  }
167  };
168 
169  class Read_request: public Request {
170  DEFINE_COMMON_CLASS(Read_request, Request)
171 
172  public:
173  Stream::Ref stream;
174  size_t max_to_read, min_to_read;
175  Io_result &result_ref;
176  Io_buffer::Ptr &buf_ref;
177 
178  Read_request(Stream::Ref stream, size_t max_to_read, size_t min_to_read,
179  Io_buffer::Ptr &buf_ref, Io_result &result_ref):
180  stream(stream),
181  max_to_read(max_to_read), min_to_read(min_to_read),
182  buf_ref(buf_ref), result_ref(result_ref)
183  {}
184 
185  void
186  Complete(Io_result result)
187  {
188  result_ref = result;
190  }
191  };
192 
193  class Stream_entry {
194  DEFINE_COMMON_CLASS(Stream_entry)
195 
196  public:
197  enum class State {
198  INITIAL,
199  FAILED,
201  OPEN_REQUESTED,
202  OPENED,
203  CLOSED
204  };
205 
206  Stream::Ref stream;
207  State state = State::INITIAL;
208  Open_request::Ptr open_req;
210  std::list<Write_request::Ptr> write_queue;
212  std::list<Read_request::Ptr> read_queue;
213 
214  Stream_entry(Android_serial_processor::Ptr processor,
215  const std::string &name, const Stream::Mode &mode,
216  Open_request::Ptr open_req);
217 
218  void
219  Complete_open(bool succeeded);
220 
221  void
222  Push_write(Write_request::Ptr req);
223 
224  void
225  Write_done(bool succeeded);
226 
227  void
228  Push_read(Read_request::Ptr req);
229 
230  void
231  Read_done(Io_buffer::Ptr data);
232 
233  void
234  Close();
235  };
236 
238  static Singleton<Android_serial_processor> singleton;
239  Request_worker::Ptr worker;
241  std::map<int, Stream_entry::Ptr> streams;
242 
243  virtual void
244  On_enable() override;
245 
246  virtual void
247  On_disable() override;
248 
249  void
250  Process_on_disable(Request::Ptr req);
251 
252  void
253  Handle_open(Stream_entry::Ptr stream);
254 
255  void
256  Handle_open_completion(Open_request::Ptr req, Stream_entry::Ptr stream,
257  Open_handler handler);
258 
259  void
260  Stream_ready(int stream_id);
261 
262  void
263  Stream_closed(int stream_id);
264 
265  Stream_entry::Ptr
266  Find_stream(int stream_id);
267 
268  void
269  Handle_stream_status(Request::Ptr req, int stream_id, bool succeeded);
270 
271  void
272  Close_stream_impl(int stream_id);
273 
274  void
275  Handle_stream_close(Request::Ptr req, Stream::Ref stream);
276 
277  void
278  Handle_write(Write_request::Ptr req);
279 
280  void
281  On_write_complete(int stream_id, bool succeeded);
282 
283  void
284  Process_write_complete(Request::Ptr req, int stream_id, bool succeeded);
285 
286  void
287  Process_read_complete(Request::Ptr req, int stream_id, Io_buffer::Ptr data);
288 
289  void
290  Handle_read(Read_request::Ptr req);
291 
292  void
293  On_read_complete(int stream_id, Io_buffer::Ptr buf);
294 };
295 
296 } /* namespace vsm */
297 } /* namespace ugcs */
298 
299 #endif /* _UGCS_VSM_ANDROID_SERIAL_PROCESSOR_H_ */
Generic request for implementing inter-threads communications and asynchronous operations.
Definition: request_container.h:37
Callback_proxy< void, Io_stream::Ref > Open_handler
Default prototype for open operation completion handler.
Definition: android_serial_processor.h:116
Serial port I/O processor.
Operation_waiter Open(const std::string &port_name, const Stream::Mode &mode=Stream::Mode(), Open_handler completion_handler=Make_dummy_callback< void, Stream::Ref >(), Request_completion_context::Ptr comp_ctx=Request_temp_completion_context::Create())
Open serial device stream.
static Ptr Create(Args &&...args)
Create an instance.
Definition: request_temp_completion_context.h:19
Io_result
Result of I/O operation.
Definition: io_stream.h:37
std::shared_ptr< Android_serial_processor > Ptr
Pointer type.
Definition: android_serial_processor.h:42
std::weak_ptr< Stream > Weak_ptr
Pointer type.
Definition: android_serial_processor.h:48
void Complete(Status status=Status::OK, Locker locker=Locker())
Complete the request processing.
Communication mode parameters for a serial port.
Definition: serial_processor.h:34
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Abstract I/O stream interface.
Definition: io_stream.h:66
std::shared_ptr< Request_worker > Ptr
Pointer type.
Definition: request_worker.h:25
std::weak_ptr< Android_serial_processor > Weak_ptr
Pointer type.
Definition: android_serial_processor.h:42
Request execution context.
Definition: request_context.h:24
Helper class for proxying callback invocation.
Definition: callback.h:694
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: android_serial_processor.h:108
Generic I/O buffer.
Definition: io_buffer.h:33
std::shared_ptr< Read_request > Ptr
Shared pointer to read request.
Definition: io_request.h:147
Generic container for queued requests.
Definition: request_container.h:30
Definition: android_serial_processor.h:47
std::shared_ptr< Stream > Ptr
Pointer type.
Definition: android_serial_processor.h:48
std::shared_ptr< Write_request > Ptr
Shared pointer to write request.
Definition: io_request.h:111
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
Request_container::Request Request
Request type for convenient usage.
Definition: request_container.h:691
Reference_guard< Ptr > Ref
Reference type.
Definition: android_serial_processor.h:52
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Stream has been or is closed.
Serial ports I/O processor.
Definition: serial_processor.h:21
Class for synchronizing with request execution.
Definition: operation_waiter.h:24
Working with serial ports on Android platform.
Definition: android_serial_processor.h:41