forked from hideakitai/Packetizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packetizer.h
514 lines (429 loc) · 16.2 KB
/
Packetizer.h
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#pragma once
#ifndef HT_SERIAL_PACKETIZER
#define HT_SERIAL_PACKETIZER
#ifdef ARDUINO
#include <Arduino.h>
#endif
#if defined(ARDUINO)\
|| defined(OF_VERSION_MAJOR)
#define PACKETIZER_ENABLE_STREAM
#endif
#include "Packetizer/util/ArxTypeTraits/ArxTypeTraits.h"
#include "Packetizer/util/ArxContainer/ArxContainer.h"
#include "Packetizer/util/ArxSmartPtr/ArxSmartPtr.h"
#include "Packetizer/Types.h"
#include "Packetizer/Encoding.h"
namespace ht {
namespace serial {
namespace packetizer {
template <typename Encoding>
class Encoder
{
EncoderBaseRef encoder {encoder_t<Encoding>::create()};
public:
size_t encode(const uint8_t index, const uint8_t* src, const size_t size, bool b_crc)
{
return encoder->encode(index, src, size, b_crc);
}
size_t encode(const uint8_t index, const uint8_t* src, const size_t size)
{
return encoder->encode(index, src, size);
}
size_t encode(const uint8_t* src, const size_t size, bool b_crc)
{
return encoder->encode(src, size, b_crc);
}
size_t encode(const uint8_t* src, const size_t size)
{
return encoder->encode(src, size);
}
const uint8_t* data() const { return encoder->data(); };
size_t size() const { return encoder->size(); };
const Packet& packet() const { return encoder->packet(); }
void verifying(bool b) { encoder->verifying(b); }
bool verifying() const { return encoder->verifying(); }
};
template <typename Encoding = DefaultEncoding>
class Decoder
{
DecoderBaseRef decoder {decoder_t<Encoding>::create()};
PacketQueue packets;
CallbackType cb_no_index;
CallbackAlwaysType cb_always;
CallbackMap callbacks;
public:
void subscribe(const CallbackType& func)
{
cb_no_index = func;
}
void subscribe(const CallbackAlwaysType& func)
{
cb_always = func;
}
void subscribe(const uint8_t index, const CallbackType& func)
{
callbacks.emplace(make_pair(index, func));
}
void unsubscribe()
{
if (indexing()) cb_always = nullptr;
else cb_no_index = nullptr;
}
void unsubscribe(uint8_t index)
{
callbacks.erase(index);
}
void feed(const uint8_t* const data, const size_t size, bool b_exec_cb = true)
{
for (size_t i = 0; i < size; ++i)
{
decoder->feed(data[i], packets);
if (PACKETIZER_MAX_PACKET_QUEUE_SIZE != 0)
if (available() > PACKETIZER_MAX_PACKET_QUEUE_SIZE)
pop();
if (available() && b_exec_cb) callback();
}
}
void callback()
{
if (indexing())
{
if (!cb_always && callbacks.empty()) return;
while(available())
{
if (cb_always) cb_always(index(), data(), size());
auto it = callbacks.find(index());
if (it != callbacks.end()) it->second(data(), size());
pop();
}
}
else
{
if (!cb_no_index) return;
while(available())
{
cb_no_index(data(), size());
pop();
}
}
}
void reset() { packets.clear(); decoder->reset(); }
bool parsing() const { return decoder->parsing(); }
size_t available() const { return packets.size(); }
void pop() { pop_front(); }
void pop_front() { packets.pop_front(); }
void pop_back() { packets.pop_back(); }
const Packet& packet() const { return packets.front(); }
uint8_t index() const { return packets.front().index; }
size_t size() const { return packet().data.size(); }
const uint8_t* data() const { return packet().data.data(); }
uint8_t data(const uint8_t i) const { return packet().data[i]; }
const Packet& packet_latest() const { return packets.back(); }
uint8_t index_latest() const { return packets.back().index; }
size_t size_latest() const { return packet_latest().data.size(); }
const uint8_t* data_latest() const { return packet_latest().data.data(); }
uint8_t data_latest(const uint8_t i) const { return packet_latest().data[i]; }
uint32_t errors() const { return decoder->errors(); }
void options(bool b_index, bool b_crc) { indexing(b_index); verifying(b_crc); }
void indexing(bool b) { decoder->indexing(b); }
void verifying(bool b) { decoder->verifying(b); }
bool indexing() const { return decoder->indexing(); }
bool verifying() const { return decoder->verifying(); }
};
template <typename Encoding>
using EncoderRef = std::shared_ptr<Encoder<Encoding>>;
template <typename Encoding>
using DecoderRef = std::shared_ptr<Decoder<Encoding>>;
template <typename Encoding>
class EncodeManager
{
EncodeManager() : encoder(std::make_shared<Encoder<Encoding>>()) {}
EncodeManager(const EncodeManager&) = delete;
EncodeManager& operator=(const EncodeManager&) = delete;
EncoderRef<Encoding> encoder;
public:
static EncodeManager<Encoding>& getInstance()
{
static EncodeManager<Encoding> m;
return m;
}
EncoderRef<Encoding> getEncoder()
{
return encoder;
}
};
template <typename Encoding = DefaultEncoding>
inline const Packet& encode(const uint8_t index, const uint8_t* data, const size_t size, const bool b_crc)
{
auto e = EncodeManager<Encoding>::getInstance().getEncoder();
e->verifying(b_crc);
e->encode(index, data, size);
return e->packet();
}
template <typename Encoding = DefaultEncoding>
inline const Packet& encode(const uint8_t index, const uint8_t* data, const size_t size)
{
auto e = EncodeManager<Encoding>::getInstance().getEncoder();
e->encode(index, data, size);
return e->packet();
}
template <typename Encoding = DefaultEncoding>
inline const Packet& encode(const uint8_t* data, const size_t size, const bool b_crc)
{
auto e = EncodeManager<Encoding>::getInstance().getEncoder();
e->verifying(b_crc);
e->encode(data, size);
return e->packet();
}
template <typename Encoding = DefaultEncoding>
inline const Packet& encode(const uint8_t* data, const size_t size)
{
auto e = EncodeManager<Encoding>::getInstance().getEncoder();
e->encode(data, size);
return e->packet();
}
template <typename Encoding = DefaultEncoding>
inline void encode_option(const bool b_crc)
{
auto e = EncodeManager<Encoding>::getInstance().getEncoder();
e->verifying(b_crc);
}
#ifdef PACKETIZER_ENABLE_STREAM
template <typename Encoding = DefaultEncoding>
inline void send(StreamType& stream, const uint8_t index, const uint8_t* data, const size_t size, const bool b_crc)
{
const auto& packet = encode<Encoding>(index, data, size, b_crc);
PACKETIZER_STREAM_WRITE(stream, packet.data.data(), packet.data.size());
}
template <typename Encoding = DefaultEncoding>
inline void send(StreamType& stream, const uint8_t index, const uint8_t* data, const size_t size)
{
const auto& packet = encode<Encoding>(index, data, size);
PACKETIZER_STREAM_WRITE(stream, packet.data.data(), packet.data.size());
}
template <typename Encoding = DefaultEncoding>
inline void send(StreamType& stream, const uint8_t* data, const size_t size, const bool b_crc)
{
const auto& packet = encode<Encoding>(data, size, b_crc);
PACKETIZER_STREAM_WRITE(stream, packet.data.data(), packet.data.size());
}
template <typename Encoding = DefaultEncoding>
inline void send(StreamType& stream, const uint8_t* data, const size_t size)
{
const auto& packet = encode<Encoding>(data, size);
PACKETIZER_STREAM_WRITE(stream, packet.data.data(), packet.data.size());
}
#endif // PACKETIZER_ENABLE_STREAM
template <typename Encoding = DefaultEncoding>
class DecodeManager
{
DecodeManager() {}
DecodeManager(const DecodeManager&) = delete;
DecodeManager& operator=(const DecodeManager&) = delete;
DecoderRef<Encoding> decoder;
public:
static DecodeManager& getInstance()
{
static DecodeManager m;
return m;
}
DecoderRef<Encoding> getDecoderRef()
{
if (!decoder) decoder = std::make_shared<Decoder<Encoding>>();
return decoder;
}
#ifdef PACKETIZER_ENABLE_STREAM
private:
DecoderMap<Encoding> decoders;
public:
DecoderRef<Encoding> getDecoderRef(const StreamType& stream)
{
StreamType* s = (StreamType*)&stream;
if (decoders.find(s) == decoders.end())
decoders.insert(make_pair(s, std::make_shared<Decoder<Encoding>>()));
return decoders[s];
}
void options(const bool b_index, const bool b_crc)
{
decoder->indexing(b_index);
decoder->verifying(b_crc);
for (auto& d : decoders)
{
d.second->indexing(b_index);
d.second->verifying(b_crc);
}
}
void parse(const bool b_exec_cb = true)
{
for (auto& d : decoders)
{
while (d.first->available() > 0)
{
const int size = d.first->available();
uint8_t* data = new uint8_t[size];
d.first->readBytes((char*)data, size);
d.second->feed(data, size, b_exec_cb);
delete[] data;
}
}
}
void reset()
{
decoder->reset();
for (auto& d : decoders)
d.second->reset();
}
#endif // PACKETIZER_ENABLE_STREAM
};
template <typename Encoding = DefaultEncoding>
inline const Packet& decode(const uint8_t* data, const size_t size)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->reset();
decoder->indexing(PACKETIZER_DEFAULT_INDEX_SETTING);
decoder->verifying(PACKETIZER_DEFAULT_CRC_SETTING);
decoder->feed(data, size);
return decoder->packet();
}
template <typename Encoding = DefaultEncoding>
inline const Packet& decode(const uint8_t* data, const size_t size, const bool b_crc)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->reset();
decoder->indexing(PACKETIZER_DEFAULT_INDEX_SETTING);
decoder->verifying(b_crc);
decoder->feed(data, size);
return decoder->packet();
}
template <typename Encoding = DefaultEncoding>
inline const Packet& decode(const uint8_t* data, const size_t size, const bool b_index, const bool b_crc)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->reset();
decoder->indexing(b_index);
decoder->verifying(b_crc);
decoder->feed(data, size);
return decoder->packet();
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> decode_option(const bool b_index, const bool b_crc)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->indexing(b_index);
decoder->verifying(b_crc);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> subscribe(const CallbackType& func)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->subscribe(func);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> subscribe(const CallbackAlwaysType& func)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->subscribe(func);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> subscribe(const uint8_t index, const CallbackType& func)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->subscribe(index, func);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> unsubscribe()
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->unsubscribe();
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> unsubscribe(const uint8_t index)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->unsubscribe(index);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> feed(const uint8_t* data, const size_t size, bool b_exec_cb = true)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->feed(data, size, b_exec_cb);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> reset()
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef();
decoder->reset();
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> getDecoderRef()
{
return DecodeManager<Encoding>::getInstance().getDecoderRef();
}
#ifdef PACKETIZER_ENABLE_STREAM
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> options(const StreamType& stream, const bool b_index, const bool b_crc)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
decoder->indexing(b_index);
decoder->verifying(b_crc);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> subscribe(const StreamType& stream, const CallbackType& func)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
decoder->subscribe(func);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> subscribe(const StreamType& stream, const CallbackAlwaysType& func)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
decoder->subscribe(func);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> subscribe(const StreamType& stream, const uint8_t index, const CallbackType& func)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
decoder->subscribe(index, func);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> unsubscribe(const StreamType& stream)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
decoder->unsubscribe();
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> unsubscribe(const StreamType& stream, const uint8_t index)
{
auto decoder = DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
decoder->unsubscribe(index);
return decoder;
}
template <typename Encoding = DefaultEncoding>
inline void parse(bool b_exec_cb = true)
{
DecodeManager<Encoding>::getInstance().parse(b_exec_cb);
}
template <typename Encoding = DefaultEncoding>
inline DecoderRef<Encoding> getDecoderRef(const StreamType& stream)
{
return DecodeManager<Encoding>::getInstance().getDecoderRef(stream);
}
#endif // PACKETIZER_ENABLE_STREAM
} // packetizer
} // serial
} // ht
namespace Packetizer = ht::serial::packetizer;
#endif // HT_SERIAL_PACKETIZER