VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
11 #ifndef _UGCS_VSM_SERIAL_PROCESSOR_H_
12 #define _UGCS_VSM_SERIAL_PROCESSOR_H_
13 
15 #include <ugcs/vsm/log.h>
16 
17 namespace ugcs {
18 namespace vsm {
19 
23 
24 public:
26  class Stream: public File_processor::Stream {
28 
29  public:
32 
34  class Mode {
35  public:
36  Mode() {}
37 
43  Mode &
44  Baud(int baud)
45  {
46  this->baud = baud;
47  return *this;
48  }
49 
54  Mode &
55  Char_size(int size)
56  {
57  char_size = size;
58  return *this;
59  }
60 
65  Mode &
66  Stop_bit(bool enable)
67  {
68  stop_bit = enable;
69  return *this;
70  }
71 
77  Mode &
78  Parity_check(bool enable)
79  {
80  parity_check = enable;
81  return *this;
82  }
83 
89  Mode &
90  Parity(bool is_odd)
91  {
92  parity = is_odd;
93  return *this;
94  }
95 
103  Mode &
104  Read_timeout(std::chrono::milliseconds timeout)
105  {
106  if (timeout.count() < 0) {
107  VSM_EXCEPTION(Invalid_param_exception, "Negative timeout specified.");
108  }
109  read_timeout = timeout;
110  return *this;
111  }
112 
113  int
114  Get_baud() const
115  {
116  return baud;
117  }
118 
119  int
120  Get_char_size() const
121  {
122  return char_size;
123  }
124 
125  bool
126  Get_stop_bit() const
127  {
128  return stop_bit;
129  }
130 
131  bool
132  Get_parity_check() const
133  {
134  return parity_check;
135  }
136 
137  bool
138  Get_parity() const
139  {
140  return parity;
141  }
142 
143  protected:
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:
217  static Singleton<Serial_processor> singleton;
218 
221  Open_native_handle(const std::string &port_name, const Stream::Mode &mode);
222 };
223 
224 } /* namespace vsm */
225 } /* namespace ugcs */
226 
227 #endif /* _UGCS_VSM_SERIAL_PROCESSOR_H_ */
Mode & Stop_bit(bool enable)
Set number of stop bits.
Definition: serial_processor.h:66
Mode & Baud(int baud)
Set baud rate.
Definition: serial_processor.h:44
Mode & Parity(bool is_odd)
Set parity mode.
Definition: serial_processor.h:90
Reference_guard< Ptr > Ref
Reference type.
Definition: serial_processor.h:31
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:78
Mode & Char_size(int size)
Set character size.
Definition: serial_processor.h:55
Communication mode parameters for a serial port.
Definition: serial_processor.h:34
Abstract I/O stream interface.
Definition: io_stream.h:66
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:30
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
std::shared_ptr< File_processor > Ptr
Pointer type.
Definition: file_processor.h:32
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:168
Mode & Read_timeout(std::chrono::milliseconds timeout)
Set read timeout.
Definition: serial_processor.h:104
Processor for working with filesystem I/O.
Definition: file_processor.h:31
Processor for handling file I/O.
Serial ports I/O processor.
Definition: serial_processor.h:21