LibLogicalAccess  2.5.0
An Open Source RFID Library
Loading...
Searching...
No Matches
serialport.hpp
Go to the documentation of this file.
1
8#ifndef SERIALPORT_HPP
9#define SERIALPORT_HPP
10
11#include <iostream>
12#include <string>
13#include <mutex>
14#include <thread>
15
16#include <boost/asio.hpp>
17#include <boost/utility.hpp>
18#include <boost/date_time/posix_time/posix_time.hpp>
19#include <boost/circular_buffer.hpp>
20#include <boost/thread/shared_mutex.hpp>
21#include <condition_variable>
22
25
26namespace logicalaccess
27{
39class LLA_CORE_API SerialPort
40{
41 public:
47 SerialPort();
48
53 explicit SerialPort(const std::string &dev);
54
58 SerialPort(const SerialPort &other) = delete; // non construction-copyable
59 SerialPort &operator=(const SerialPort &) = delete; // non copyable
60
66 virtual ~SerialPort()
67 {
68 close();
69 }
70
86 void open();
87
96 void reopen();
97
105 void close();
106
107 bool isOpen() const;
108
113 const std::string &deviceName() const
114 {
115 return m_dev;
116 }
117
131 size_t read(ByteVector &buf);
132
142 size_t write(const ByteVector &buf);
143
144 void setBaudrate(unsigned int rate);
145 unsigned int getBaudrate();
146
147 void setFlowControl(const boost::asio::serial_port_base::flow_control::type &type);
148 boost::asio::serial_port_base::flow_control::type getFlowControl();
149
150 void setParity(const boost::asio::serial_port_base::parity::type &parity);
151 boost::asio::serial_port_base::parity::type getParity();
152
153 void setStopBits(const boost::asio::serial_port_base::stop_bits::type &stop_bits);
154 boost::asio::serial_port_base::stop_bits::type getStopBits();
155
156 void setCharacterSize(unsigned int character_size);
157 unsigned int getCharacterSize();
158
159 void setCircularBufferParser(CircularBufferParser *circular_buffer_parser)
160 {
161 m_circular_buffer_parser.reset(circular_buffer_parser);
162 }
163 std::shared_ptr<CircularBufferParser> getCircularBufferParser() const
164 {
165 return m_circular_buffer_parser;
166 }
167
168 boost::circular_buffer<unsigned char> &getCircularReadBuffer()
169 {
170 return m_circular_read_buffer;
171 }
172
179 template <typename T>
180 void waitMoreData(const std::chrono::steady_clock::time_point &until, T &&callback)
181 {
182 std::unique_lock<std::mutex> ul(cond_var_mutex_);
183 cond_var_.wait_until(ul, until, [&]() { return data_flag_; });
184 if (data_flag_)
185 {
186 callback();
187 }
188 }
189
193 template <typename T>
194 void lockedExecute(T &&callback)
195 {
196 std::unique_lock<std::mutex> ul(cond_var_mutex_);
197 callback();
198 }
199
204 void dataConsumed();
205
206 private:
207 void do_read(const boost::system::error_code &e, size_t bytes_transferred);
208
209 void do_close(const boost::system::error_code &error);
210
211 void do_write(const ByteVector &buf);
212 void write_start();
213 void write_complete(const boost::system::error_code &error,
214 const size_t bytes_transferred);
215
219 std::string m_dev;
220
221 boost::asio::io_service m_io;
222
223 boost::asio::serial_port m_serial_port;
224
225 boost::circular_buffer<unsigned char> m_circular_read_buffer;
226
228
230
231 std::shared_ptr<std::thread> m_thread_reader;
232
233 std::shared_ptr<CircularBufferParser> m_circular_buffer_parser;
234
239 std::condition_variable cond_var_;
241 std::mutex cond_var_mutex_;
242};
243}
244
245#endif /* SERIALPORT_HPP */
A CircularBufferParser class.
Definition: circularbufferparser.hpp:18
A class that represents a serial (COM) port.
Definition: serialport.hpp:40
ByteVector m_read_buffer
Definition: serialport.hpp:227
boost::asio::io_service m_io
Definition: serialport.hpp:221
void waitMoreData(const std::chrono::steady_clock::time_point &until, T &&callback)
Definition: serialport.hpp:180
SerialPort(const SerialPort &other)=delete
Remove copy.
virtual ~SerialPort()
Destructor.
Definition: serialport.hpp:66
void lockedExecute(T &&callback)
Definition: serialport.hpp:194
std::string m_dev
The internal device name.
Definition: serialport.hpp:219
std::shared_ptr< CircularBufferParser > m_circular_buffer_parser
Definition: serialport.hpp:233
ByteVector m_write_buffer
Definition: serialport.hpp:229
std::shared_ptr< std::thread > m_thread_reader
Definition: serialport.hpp:231
void setCircularBufferParser(CircularBufferParser *circular_buffer_parser)
Definition: serialport.hpp:159
std::mutex cond_var_mutex_
Definition: serialport.hpp:241
std::condition_variable cond_var_
Definition: serialport.hpp:239
boost::asio::serial_port m_serial_port
Definition: serialport.hpp:223
std::shared_ptr< CircularBufferParser > getCircularBufferParser() const
Definition: serialport.hpp:163
boost::circular_buffer< unsigned char > & getCircularReadBuffer()
Definition: serialport.hpp:168
const std::string & deviceName() const
Get the device name.
Definition: serialport.hpp:113
boost::circular_buffer< unsigned char > m_circular_read_buffer
Definition: serialport.hpp:225
bool data_flag_
Definition: serialport.hpp:240
SerialPort & operator=(const SerialPort &)=delete
std::vector< uint8_t > ByteVector
Definition: lla_fwd.hpp:80
Definition: asn1.hpp:9
Reader unit.