libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
SerialReaderWriter.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef SERIALREADERWRITER_H_
32 #define SERIALREADERWRITER_H_
33 
34 #include "../Exception.h"
35 #include "../Managers/FileDescriptorManager.h"
36 #include "../IEvents.h"
37 
38 #include <thread>
39 #include <atomic>
40 
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <termios.h>
44 #include <signal.h>
45 
46 namespace BaseLib {
48  public:
49  SerialReaderWriterException(std::string message) : Exception(message) {}
50 };
51 
52 class SerialReaderWriter : public IEventsEx {
53  public:
54  enum class CharacterSize : tcflag_t {
55  Five = CS5,
56  Six = CS6,
57  Seven = CS7,
58  Eight = CS8
59  };
60 
61  // {{{ Event handling
63  public:
64  virtual void lineReceived(const std::string &data) = 0;
65  };
66  // }}}
67 
79  SerialReaderWriter(BaseLib::SharedObjects *baseLib, std::string device, int32_t baudrate, int32_t flags, bool createLockFile, int32_t readThreadPriority, bool writeOnly = false);
80 
84  virtual ~SerialReaderWriter();
85 
86  bool isOpen() { return _fileDescriptor && _fileDescriptor->descriptor != -1; }
87  std::shared_ptr<FileDescriptor> fileDescriptor() { return _fileDescriptor; }
88 
98  void openDevice(bool parity, bool oddParity, bool events = true, CharacterSize characterSize = CharacterSize::Eight, bool twoStopBits = false);
99 
103  void closeDevice();
104 
112  int32_t readLine(std::string &data, uint32_t timeout = 500000, char splitChar = '\n');
113 
120  int32_t readChar(char &data, uint32_t timeout = 500000);
121 
126  void writeLine(std::string &data);
127 
132  void writeData(const std::vector<char> &data);
133 
138  void writeData(const std::vector<uint8_t> &data);
139 
144  void writeChar(char data);
145  protected:
146  BaseLib::SharedObjects *_bl = nullptr;
147  std::shared_ptr<FileDescriptor> _fileDescriptor;
148  std::string _device;
149  bool _writeOnly = false;
150  struct termios _termios;
151  int32_t _baudrate = 0;
152  int32_t _flags = 0;
153  int32_t _readThreadPriority = 0;
154  int32_t _handles = 0;
155 
156  std::atomic_bool _stopReadThread;
157  std::mutex _readThreadMutex;
158  std::thread _readThread;
159  std::mutex _sendMutex;
161  std::thread _openDeviceThread;
162 
163  void readThread(bool parity, bool oddParity, CharacterSize characterSize, bool twoStopBits);
164 };
165 
166 }
167 #endif
std::thread _openDeviceThread
Definition: SerialReaderWriter.h:161
std::atomic_bool _stopReadThread
Definition: SerialReaderWriter.h:156
std::mutex _sendMutex
Definition: SerialReaderWriter.h:159
This is the base library main class.
Definition: BaseLib.h:95
SerialReaderWriterException(std::string message)
Definition: SerialReaderWriter.h:49
Definition: BaseLib.cpp:34
CharacterSize
Definition: SerialReaderWriter.h:54
std::shared_ptr< FileDescriptor > _fileDescriptor
Definition: SerialReaderWriter.h:147
bool isOpen()
Definition: SerialReaderWriter.h:86
Definition: SerialReaderWriter.h:52
std::mutex _readThreadMutex
Definition: SerialReaderWriter.h:157
Definition: SerialReaderWriter.h:47
Definition: IEvents.h:87
std::thread _readThread
Definition: SerialReaderWriter.h:158
Base class for all exceptions defined in Homegear.
Definition: Exception.h:41
std::mutex _openDeviceThreadMutex
Definition: SerialReaderWriter.h:160
std::shared_ptr< FileDescriptor > fileDescriptor()
Definition: SerialReaderWriter.h:87
Definition: IEvents.h:66
std::string _device
Definition: SerialReaderWriter.h:148