-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
serialqueue.hpp
265 lines (185 loc) · 7.91 KB
/
serialqueue.hpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright (c) 2013-2023 plan44.ch / Lukas Zeller, Zurich, Switzerland
//
// Author: Lukas Zeller <luz@plan44.ch>
//
// This file is part of p44utils.
//
// p44utils is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// p44utils is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with p44utils. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef __p44utils__serialqueue__
#define __p44utils__serialqueue__
#include "p44utils_main.hpp"
#include <time.h>
#include <list>
#include "operationqueue.hpp"
#include "serialcomm.hpp"
using namespace std;
namespace p44 {
/// acceptBytes() can return this for a queue with an accept buffer to reject
/// accepting bytes now because more are needed.
#define NOT_ENOUGH_BYTES -1
class SQError : public Error
{
public:
// Errors
typedef enum {
OK,
Transmit,
} ErrorCodes;
static const char *domain() { return "SerialQueue"; };
virtual const char *getErrorDomain() const { return SQError::domain(); };
SQError(ErrorCodes aError) : Error(ErrorCode(aError)) {};
};
class SerialOperation;
class SerialOperationQueue;
class SerialOperationSendAndReceive;
typedef boost::intrusive_ptr<SerialOperation> SerialOperationPtr;
/// SerialOperation transmitter
typedef boost::function<size_t (size_t aNumBytes, const uint8_t *aBytes)> SerialOperationTransmitter;
/// Serial operation
class SerialOperation : public Operation
{
typedef Operation inherited;
protected:
SerialOperationTransmitter mTransmitter;
public:
/// set transmitter
/// @param aTransmitter callback to be used to transmit data
void setTransmitter(SerialOperationTransmitter aTransmitter);
/// is called to deliver received bytes
/// @param aNumBytes number of bytes ready for accepting
/// @param aBytes pointer to bytes buffer
/// @return number of bytes operation could accept, 0 if operation does accept none,
/// NOT_ENOUGH_BYTES if operation would accept bytes, but not enough of them are ready.
/// Note that NOT_ENOUGH_BYTES may only be used when the SerialQueue has a
/// buffer for re-assembling messages (see SerialQueue::setAcceptBuffer())
virtual ssize_t acceptBytes(size_t aNumBytes, uint8_t *aBytes);
};
/// Send operation
class SerialOperationSend : public SerialOperation
{
typedef SerialOperation inherited;
size_t mDataSize;
size_t mAppendIndex;
uint8_t *mDataP;
public:
/// constructor
SerialOperationSend();
/// destructor
virtual ~SerialOperationSend();
/// set how much data to send
/// @param aDataSize size of data buffer to prepare
void setDataSize(size_t aDataSize);
/// clear all data
void clearData();
/// append data to buffer
/// @param aNumBytes number of bytes to add
/// @param aBytes bytes to append
void appendData(size_t aNumBytes, uint8_t *aBytes);
/// append single byte to buffer
/// @param aByte byte to append
void appendByte(uint8_t aByte);
/// will be called by queue when data can be sent
virtual bool initiate();
};
typedef boost::intrusive_ptr<SerialOperationSend> SerialOperationSendPtr;
/// receive operation
class SerialOperationReceive : public SerialOperation
{
typedef SerialOperation inherited;
size_t mExpectedBytes;
uint8_t *mDataP;
size_t mDataIndex;
public:
/// constructor
SerialOperationReceive();
/// destructor
virtual ~SerialOperationReceive();
/// set how much data to send
/// @param aExpectedBytes how many bytes we expect to receive
void setExpectedBytes(size_t aExpectedBytes);
/// clear all data
void clearData();
/// get size of data received
/// @return size of data accessible via getDataP()
size_t getDataSize() { return mDataIndex; };
/// get data buffer pointer
/// @return pointer to data
uint8_t *getDataP() { return mDataP; };
/// accepts bytes into buffer until expected number
virtual ssize_t acceptBytes(size_t aNumBytes, uint8_t *aBytes);
/// returns true when enough bytes were received
virtual bool hasCompleted();
/// abort
virtual void abortOperation(ErrorPtr aError);
};
typedef boost::intrusive_ptr<SerialOperationReceive> SerialOperationReceivePtr;
/// SerialOperation receiver
typedef boost::function<size_t (size_t aMaxBytes, uint8_t *aBytes)> SerialOperationReceiver;
/// SerialOperation extra bytes handler
typedef boost::function<ssize_t (size_t aNumBytes, const uint8_t *aBytes)> SerialOperationExtraBytesHandler;
/// Serial operation queue
class SerialOperationQueue : public OperationQueue
{
typedef OperationQueue inherited;
SerialOperationTransmitter mTransmitter;
SerialOperationReceiver mReceiver;
SerialOperationExtraBytesHandler mExtraBytesHandler;
size_t mAcceptBufferSize;
size_t mBufferedBytes;
uint8_t *mAcceptBufferP;
public:
/// the serial communication channel
SerialCommPtr mSerialComm;
/// create operation queue linked into specified Synchronous IO mainloop
SerialOperationQueue(MainLoop &aMainLoop = MainLoop::currentMainLoop());
/// destructor
virtual ~SerialOperationQueue();
/// set transmitter to be used for all operations (for sending data to hardware)
void setTransmitter(SerialOperationTransmitter aTransmitter);
/// set receiver (which actually gets data from hardware)
void setReceiver(SerialOperationReceiver aReceiver);
/// set extra byte handler (which gets "transmitted" extra bytes received from hardware and not accepted by queued operations)
void setExtraBytesHandler(SerialOperationExtraBytesHandler aExtraBytesHandler);
/// set an accept buffer
/// @param aBufferSize size of buffer that will hold received bytes until they can be processed.
/// setting a buffer size allows operations and acceptExtraBytes() to not accept bytes when there are to few bytes ready
void setAcceptBuffer(size_t aBufferSize);
/// queue a new operation
/// @param aOperation the serial IO operation to queue
void queueSerialOperation(SerialOperationPtr aOperation);
/// called to process extra bytes after all pending operations have processed their bytes
/// @param aNumBytes number of bytes ready to accept
/// @param aBytes bytes ready to accept
/// @return number of extra bytes that could be accepted, 0 if none, NOT_ENOUGH_BYTES if extra bytes would be accepted,
/// but not enough of them are ready. Note that NOT_ENOUGH_BYTES may only be used when the SerialQueue has a
/// buffer for re-assembling messages (see setAcceptBuffer())
/// @note if setExtraBytesHandler() has set a handler, this is called here. Otherwise, the baseclass' implementation
/// always returns 0 (does not accept any extra bytes by default)
virtual ssize_t acceptExtraBytes(size_t aNumBytes, uint8_t *aBytes);
/// standard transmitter
size_t standardTransmitter(size_t aNumBytes, const uint8_t *aBytes);
/// standard receiver
size_t standardReceiver(size_t aMaxBytes, uint8_t *aBytes);
private:
/// base class implementation: deliver bytes to the most recent waiting operation,
/// call acceptExtraBytes if bytes are left after all operations had chance to accept bytes.
virtual size_t acceptBytes(size_t aNumBytes, uint8_t *aBytes);
/// FdComm handler
void receiveHandler(ErrorPtr aError);
};
} // namespace p44
#endif /* defined(__p44utils__serialqueue__) */