Debian Codegraph @

Network/Serial.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <mutex>
24 #include <string>
25 #include <termios.h>
26 #include <unistd.h> //close & write;
27
28 #include "Logger/Logger.h"
29
30 namespace Network
31 {
32 class Serial
33 {
34 public:
35 Serial() = delete;
36 Serial(const Serial&) = delete;
37 Serial& operator=(const Serial&) = delete;
38
39 inline Serial(Logger::Logger* logger,
40 const std::string& tty,
41 const unsigned int dataSpeed, // from termio (ex. B9600)
42 const unsigned char dataBits,
43 const char parity,
44 const unsigned char stopBits,
45 const bool hardwareFlowControl = false)
46 : logger(logger),
47 tty(tty),
48 dataSpeed(dataSpeed),
49 dataBits(dataBits),
50 parity(parity),
51 stopBits(stopBits),
52 hardwareFlowControl(hardwareFlowControl),
53 fileHandle(-1)
54 {
55 Init();
56 }
57
58 ~Serial()
59 {
60 Close();
61 }
62
63 inline void ReInit()
64 {
65 Close();
66 Init();
67 }
68
69 inline bool IsConnected() const
70 {
71 return fileHandle != -1;
72 }
73
74 inline void ClearBuffers()
75 {
76 tcflush(fileHandle, TCIOFLUSH);
77 }
78
79 inline ssize_t Send(const std::string& data)
80 {
81 return Send(reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
82 }
83
84 inline ssize_t Send(const unsigned char data)
85 {
86 return Send(&data, 1);
87 }
88
89 inline ssize_t Send(const unsigned char* data, const size_t size)
90 {
91 if (!IsConnected())
92 {
93 return 0;
94 }
95 std::lock_guard<std::mutex> Guard(fileHandleMutex);
96 return write(fileHandle, data, size);
97 }
98
99 bool Receive(std::string& data, const size_t maxData = 1024, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000);
100 ssize_t Receive(unsigned char* data, const size_t maxData, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000);
101 bool ReceiveExact(std::string& data, const size_t length, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000);
102 ssize_t ReceiveExact(unsigned char* data, const size_t length, const unsigned int timeoutS = 0, const unsigned int timeoutUS = 100000);
103
104 private:
105 void Init();
106 void Close();
107
108 Logger::Logger* logger;
109 const std::string tty;
110 const unsigned int dataSpeed;
111 const unsigned char dataBits;
112 const char parity;
113 const unsigned char stopBits;
114 const bool hardwareFlowControl;
115 int fileHandle;
116 mutable std::mutex fileHandleMutex;
117 };
118 }