forked from wirenboard/wb-mqtt-serial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_descriptor_port.cpp
243 lines (204 loc) · 6.15 KB
/
file_descriptor_port.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "file_descriptor_port.h"
#include "serial_exc.h"
#include "binary_semaphore.h"
#include <unistd.h>
#include <iomanip>
#include <iostream>
#include <sys/select.h>
#include <sys/ioctl.h>
using namespace std;
namespace {
const chrono::milliseconds DefaultFrameTimeout(15);
const chrono::milliseconds NoiseTimeout(10);
}
TFileDescriptorPort::TFileDescriptorPort(const PPortSettings & settings)
: Fd(-1)
, DebugEnabled(false)
, Settings(settings)
{}
TFileDescriptorPort::~TFileDescriptorPort()
{
if (IsOpen()) {
close(Fd);
}
}
void TFileDescriptorPort::Close()
{
CheckPortOpen();
close(Fd);
Fd = -1;
}
bool TFileDescriptorPort::IsOpen() const
{
return Fd >= 0;
}
void TFileDescriptorPort::CheckPortOpen() const
{
if (!IsOpen()) {
throw TSerialDeviceException("port not open");
}
}
void TFileDescriptorPort::WriteBytes(const uint8_t * buf, int count) {
if (write(Fd, buf, count) < count) {
throw TSerialDeviceException("serial write failed");
}
if (Debug()) {
// TBD: move this to libwbmqtt (HexDump?)
ios::fmtflags f(cerr.flags());
cerr << "Write:" << hex << setfill('0');
for (int i = 0; i < count; ++i)
cerr << " " << setw(2) << int(buf[i]);
cerr << endl;
cerr.flags(f);
}
}
bool TFileDescriptorPort::Select(const chrono::microseconds& us)
{
fd_set rfds;
struct timeval tv, *tvp = 0;
#if 0
// too verbose
if (Dbg)
cerr << "Select on " << Settings.Device << ": " << ms.count() << " us" << endl;
#endif
FD_ZERO(&rfds);
FD_SET(Fd, &rfds);
if (us.count() > 0) {
tv.tv_sec = us.count() / 1000000;
tv.tv_usec = us.count();
tvp = &tv;
}
int r = select(Fd + 1, &rfds, NULL, NULL, tvp);
if (r < 0) {
throw TSerialDeviceException("select() failed");
}
return r > 0;
}
void TFileDescriptorPort::OnReadyEmptyFd()
{}
uint8_t TFileDescriptorPort::ReadByte()
{
CheckPortOpen();
if (!Select(Settings->ResponseTimeout)) {
throw TSerialDeviceTransientErrorException("timeout");
}
uint8_t b;
if (read(Fd, &b, 1) < 1) {
throw TSerialDeviceException("read() failed");
}
if (Debug()) {
ios::fmtflags f(cerr.flags());
cerr << "Read: " << hex << setw(2) << setfill('0') << int(b) << endl;
cerr.flags(f);
}
return b;
}
// Reading becomes unstable when using timeout less than default because of bufferization
int TFileDescriptorPort::ReadFrame(uint8_t * buf, int size,
const chrono::microseconds& timeout,
TFrameCompletePred frame_complete)
{
CheckPortOpen();
int nread = 0;
while (nread < size) {
if (frame_complete && frame_complete(buf, nread)) {
// XXX A hack.
// The problem is that if we don't pause here and the
// serial client switches to another device after
// processing this frame, that device may miss the frame
// boundary and consider the last response (from this
// device) and the query (from the master) to be single
// frame. On the other hand, we don't want to use
// device-specific frame timeout here as it can be quite
// long. The proper solution would be perhaps ensuring
// that there's a pause of at least
// DeviceConfig->FrameTimeoutMs before polling each
// device.
usleep(DefaultFrameTimeout.count());
break;
}
if (!Select(!nread ? Settings->ResponseTimeout :
timeout.count() < 0 ? DefaultFrameTimeout :
timeout))
break; // end of the frame
// We don't want to use non-blocking IO in general
// (e.g. we want blocking writes), but we don't want
// read() call below to block because actual frame
// size is not known at this point. So we must
// know how many bytes are available
int nb;
if (ioctl(Fd, FIONREAD, &nb) < 0) {
throw TSerialDeviceException("FIONREAD ioctl() failed");
}
if (!nb) {
OnReadyEmptyFd();
continue;
}
if (nb > size - nread) {
nb = size - nread;
}
int n = read(Fd, buf + nread, nb);
if (n < 0) {
throw TSerialDeviceException("read() failed");
}
if (n < nb) { // may happen only due to a kernel/driver bug
throw TSerialDeviceException("short read()");
}
nread += nb;
}
if (!nread) {
throw TSerialDeviceTransientErrorException("request timed out");
}
if (Debug()) {
// TBD: move this to libwbmqtt (HexDump?)
ios::fmtflags f(cerr.flags());
cerr << "ReadFrame:" << hex << uppercase << setfill('0');
for (int i = 0; i < nread; ++i) {
cerr << " " << setw(2) << int(buf[i]);
}
cerr << endl;
cerr.flags(f);
}
return nread;
}
void TFileDescriptorPort::SkipNoise()
{
uint8_t b;
while (Select(NoiseTimeout)) {
if (read(Fd, &b, 1) < 1) {
throw TSerialDeviceException("read() failed");
}
if (Debug()) {
ios::fmtflags f(cerr.flags());
cerr << "read noise: " << hex << setfill('0') << setw(2) << int(b) << endl;
cerr.flags(f);
}
}
}
void TFileDescriptorPort::Sleep(const chrono::microseconds & us)
{
usleep(us.count());
}
bool TFileDescriptorPort::Wait(const PBinarySemaphore & semaphore, const TTimePoint & until)
{
if (DebugEnabled) {
cerr << chrono::duration_cast<chrono::milliseconds>(
chrono::steady_clock::now().time_since_epoch()).count() <<
": Wait until " <<
chrono::duration_cast<chrono::milliseconds>(until.time_since_epoch()).count() <<
endl;
}
return semaphore->Wait(until);
}
void TFileDescriptorPort::SetDebug(bool debug)
{
DebugEnabled = debug;
}
bool TFileDescriptorPort::Debug() const
{
return DebugEnabled;
}
TTimePoint TFileDescriptorPort::CurrentTime() const
{
return chrono::steady_clock::now();
}