VSM C++ SDK
Vehicle Specific Modules SDK
android_serial_processor.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 
7 #ifndef ANDROID_SERIAL_PROCESSOR_H_
8 #define 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:
45 
47 
48  class Stream: public Io_stream {
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  private:
65  friend class Android_serial_processor;
66 
69  Mode mode;
71  int id = -1;
72 
74  virtual Operation_waiter
75  Write_impl(Io_buffer::Ptr buffer, Offset offset,
76  Write_handler completion_handler,
78 
80  virtual Operation_waiter
81  Read_impl(size_t max_to_read, size_t min_to_read, Offset offset,
82  Read_handler completion_handler,
84 
86  virtual Operation_waiter
87  Close_impl(Close_handler completion_handler,
89 
90  void
91  Complete_open(bool succeeded)
92  {
93  state = succeeded ? State::OPENED : State::CLOSED;
94  }
95 
96  void
97  Complete_close()
98  {
99  state = State::CLOSED;
100  id = -1;
101  }
102  };
103 
105  template <typename... Args>
106  static Ptr
107  Get_instance(Args &&... args)
108  {
109  return singleton.Get_instance(std::forward<Args>(args)...);
110  }
111 
116 
117  static std::list<std::string>
118  Enumerate_port_names();
119 
127  Open(const std::string &port_name, const Stream::Mode &mode = Stream::Mode(),
128  Open_handler completion_handler = Make_dummy_callback<void, Stream::Ref>(),
130 
131 private:
132  friend void Java_com_ugcs_vsm_Vsm_StreamReady(
133  JNIEnv *env, jobject _this, jint streamId);
134  friend void Java_com_ugcs_vsm_Vsm_StreamClosed(
135  JNIEnv *env, jobject _this, jint streamId);
136  friend void Java_com_ugcs_vsm_Vsm_StreamWriteComplete(
137  JNIEnv *env, jobject _this, jint streamId, jboolean succeeded);
138  friend void Java_com_ugcs_vsm_Vsm_StreamReadComplete(
139  JNIEnv *env, jobject _this, jint streamId, jobject buf);
140 
141  class Open_request: public Request {
142  DEFINE_COMMON_CLASS(Open_request, Request)
143  public:
144  using Request::Request;
145 
146  bool succeeded = false;
147  };
148 
149  class Write_request: public Request {
150  DEFINE_COMMON_CLASS(Write_request, Request)
151  public:
152  Stream::Ref stream;
153  Io_buffer::Ptr buf;
154  Io_result &result_ref;
155 
156  Write_request(Stream::Ref stream, Io_buffer::Ptr buf, Io_result &result_ref):
157  stream(stream), buf(buf), result_ref(result_ref)
158  {}
159 
160  void
161  Complete(Io_result result)
162  {
163  result_ref = result;
165  }
166  };
167 
168  class Read_request: public Request {
169  DEFINE_COMMON_CLASS(Read_request, Request)
170  public:
171  Stream::Ref stream;
172  size_t max_to_read, min_to_read;
173  Io_result &result_ref;
174  Io_buffer::Ptr &buf_ref;
175 
176  Read_request(Stream::Ref stream, size_t max_to_read, size_t min_to_read,
177  Io_buffer::Ptr &buf_ref, Io_result &result_ref):
178  stream(stream),
179  max_to_read(max_to_read), min_to_read(min_to_read),
180  buf_ref(buf_ref), result_ref(result_ref)
181  {}
182 
183  void
184  Complete(Io_result result)
185  {
186  result_ref = result;
188  }
189  };
190 
191  class Stream_entry {
192  DEFINE_COMMON_CLASS(Stream_entry)
193  public:
194  enum class State {
195  INITIAL,
196  FAILED,
198  OPEN_REQUESTED,
199  OPENED,
200  CLOSED
201  };
202 
203  Stream::Ref stream;
204  State state = State::INITIAL;
205  Open_request::Ptr open_req;
207  std::list<Write_request::Ptr> write_queue;
209  std::list<Read_request::Ptr> read_queue;
210 
211  Stream_entry(Android_serial_processor::Ptr processor,
212  const std::string &name, const Stream::Mode &mode,
213  Open_request::Ptr open_req);
214 
215  void
216  Complete_open(bool succeeded);
217 
218  void
219  Push_write(Write_request::Ptr req);
220 
221  void
222  Write_done(bool succeeded);
223 
224  void
225  Push_read(Read_request::Ptr req);
226 
227  void
228  Read_done(Io_buffer::Ptr data);
229 
230  void
231  Close();
232  };
233 
235  static Singleton<Android_serial_processor> singleton;
236  Request_worker::Ptr worker;
238  std::map<int, Stream_entry::Ptr> streams;
239 
240  virtual void
241  On_enable() override;
242 
243  virtual void
244  On_disable() override;
245 
246  void
247  Process_on_disable(Request::Ptr req);
248 
249  void
250  Handle_open(Stream_entry::Ptr stream);
251 
252  void
253  Handle_open_completion(Open_request::Ptr req, Stream_entry::Ptr stream,
254  Open_handler handler);
255 
256  void
257  Stream_ready(int stream_id);
258 
259  void
260  Stream_closed(int stream_id);
261 
262  Stream_entry::Ptr
263  Find_stream(int stream_id);
264 
265  void
266  Handle_stream_status(Request::Ptr req, int stream_id, bool succeeded);
267 
268  void
269  Close_stream_impl(int stream_id);
270 
271  void
272  Handle_stream_close(Request::Ptr req, Stream::Ref stream);
273 
274  void
275  Handle_write(Write_request::Ptr req);
276 
277  void
278  On_write_complete(int stream_id, bool succeeded);
279 
280  void
281  Process_write_complete(Request::Ptr req, int stream_id, bool succeeded);
282 
283  void
284  Process_read_complete(Request::Ptr req, int stream_id, Io_buffer::Ptr data);
285 
286  void
287  Handle_read(Read_request::Ptr req);
288 
289  void
290  On_read_complete(int stream_id, Io_buffer::Ptr buf);
291 };
292 
293 } /* namespace vsm */
294 } /* namespace ugcs */
295 
296 #endif /* ANDROID_SERIAL_PROCESSOR_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
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:115
int64_t Offset
Offset for read/write operations.
Definition: io_stream.h:62
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
STL namespace.
Io_result
Result of I/O operation.
Definition: io_stream.h:25
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:49
void Complete(Status status=Status::OK, Locker locker=Locker())
Complete the request processing.
Communication mode parameters for a serial port.
Definition: serial_processor.h:33
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
Abstract I/O stream interface.
Definition: io_stream.h:54
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:691
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: android_serial_processor.h:107
Generic I/O buffer.
Definition: io_buffer.h:33
Generic container for queued requests.
Definition: request_container.h:31
Definition: android_serial_processor.h:48
std::shared_ptr< Stream > Ptr
Pointer type.
Definition: android_serial_processor.h:49
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
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Stream has been or is closed.
Helper class for implementing singletons.
Definition: singleton.h:69
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