VSM C++ SDK
Vehicle Specific Modules SDK
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 
11 #ifndef SERIAL_PROCESSOR_H_
12 #define SERIAL_PROCESSOR_H_
13 
15 #include <ugcs/vsm/log.h>
16 
17 namespace ugcs {
18 namespace vsm {
19 
23 public:
24 
26  class Stream: public File_processor::Stream {
28  public:
31 
33  class Mode {
34  public:
35  Mode() {}
36 
42  Mode &
43  Baud(int baud)
44  {
45  this->baud = baud;
46  return *this;
47  }
48 
53  Mode &
54  Char_size(int size)
55  {
56  char_size = size;
57  return *this;
58  }
59 
64  Mode &
65  Stop_bit(bool enable)
66  {
67  stop_bit = enable;
68  return *this;
69  }
70 
76  Mode &
77  Parity_check(bool enable)
78  {
79  parity_check = enable;
80  return *this;
81  }
82 
88  Mode &
89  Parity(bool is_odd)
90  {
91  parity = is_odd;
92  return *this;
93  }
94 
102  Mode &
103  Read_timeout(std::chrono::milliseconds timeout)
104  {
105  if (timeout.count() < 0) {
106  VSM_EXCEPTION(Invalid_param_exception, "Negative timeout specified.");
107  }
108  read_timeout = timeout;
109  return *this;
110  }
111 
112  int
113  Get_baud() const
114  {
115  return baud;
116  }
117 
118  int
119  Get_char_size() const
120  {
121  return char_size;
122  }
123 
124  bool
125  Get_stop_bit() const
126  {
127  return stop_bit;
128  }
129 
130  bool
131  Get_parity_check() const
132  {
133  return parity_check;
134  }
135 
136  bool
137  Get_parity() const
138  {
139  return parity;
140  }
141 
142  protected:
143 
145  // @{
146  int baud = 1800;
147  int char_size = 8;
148  bool stop_bit = false;
149  bool parity_check = false;
150  bool parity = false;
151  std::chrono::milliseconds read_timeout = std::chrono::milliseconds(100);
152  // @}
153  };
154 
161  Stream(Serial_processor::Ptr processor,
162  const std::string& port_name,
163  const Stream::Mode &mode,
164  Native_handle::Unique_ptr&& native_handle);
165 
166  private:
168  Serial_processor::Ptr processor;
170  Mode mode;
171  };
172 
174  template <typename... Args>
175  static Ptr
176  Get_instance(Args &&... args)
177  {
178  return singleton.Get_instance(std::forward<Args>(args)...);
179  }
180 
182 
201  Open(const std::string &port_name, const Stream::Mode &mode = Stream::Mode());
202 
209  static std::list<std::string>
211 
212  // Used in mac and linux.
213  static const uint8_t MAX_VMIN;
214 
215 private:
216 
218  static Singleton<Serial_processor> singleton;
219 
222  Open_native_handle(const std::string &port_name, const Stream::Mode &mode);
223 };
224 
225 } /* namespace vsm */
226 } /* namespace ugcs */
227 
228 #endif /* SERIAL_PROCESSOR_H_ */
Mode & Stop_bit(bool enable)
Set number of stop bits.
Definition: serial_processor.h:65
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Mode & Baud(int baud)
Set baud rate.
Definition: serial_processor.h:43
Exception class with one parameter.
Definition: exception.h:88
Mode & Parity(bool is_odd)
Set parity mode.
Definition: serial_processor.h:89
Stream::Ref Open(const std::string &port_name, const Stream::Mode &mode=Stream::Mode())
Open serial port for communication.
Mode & Parity_check(bool enable)
Set parity check/generation mode.
Definition: serial_processor.h:77
Mode & Char_size(int size)
Set character size.
Definition: serial_processor.h:54
Communication mode parameters for a serial port.
Definition: serial_processor.h:33
Abstract I/O stream interface.
Definition: io_stream.h:54
static std::list< std::string > Enumerate_port_names()
Platform-specific enumeration of available serial ports.
std::unique_ptr< Native_handle > Unique_ptr
Unique pointer type.
Definition: file_processor.h:112
Generic container for queued requests.
Definition: request_container.h:31
std::shared_ptr< Serial_processor > Ptr
Pointer type.
Definition: serial_processor.h:22
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: serial_processor.h:176
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Stream which represents opened serial port.
Definition: serial_processor.h:26
Logging functionality for VSM.
Helper class for implementing singletons.
Definition: singleton.h:69
std::shared_ptr< Stream > Ptr
Pointer type.
Definition: file_processor.h:52
#define VSM_EXCEPTION(__exc_class, __msg,...)
Throw VSM exception instance.
Definition: exception.h:170
Mode & Read_timeout(std::chrono::milliseconds timeout)
Set read timeout.
Definition: serial_processor.h:103
Processor for working with filesystem I/O.
Definition: file_processor.h:32
Processor for handling file I/O.
Serial ports I/O processor.
Definition: serial_processor.h:21