-
Notifications
You must be signed in to change notification settings - Fork 8
/
TransmissionProtocolUartBridge.h
639 lines (569 loc) · 18.8 KB
/
TransmissionProtocolUartBridge.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* The MIT License (MIT)
*
* Copyright (C) 2018 Copyright Gabriel Nikol
*/
#ifndef ARDUINO_MQTTSN_CLIENT_TRANSMISSIONPROTOCOLUARTBRDIGE_H
#define ARDUINO_MQTTSN_CLIENT_TRANSMISSIONPROTOCOLUARTBRDIGE_H
#include "MqttSnMessageHandler.h"
#include "System.h"
#include <stdint.h>
#include "global_defines.h"
#include "mqttsn_messages.h"
#include <Stream.h>
#if defined(ESP8266)
#include <cerrno>
#include <climits>
#endif
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_YUN) || defined(ARDUINO_AVR_MEGA2560)
#ifndef LONG_MIN
#define LONG_MIN -2147483647
#endif // LONG_MIN
#ifndef LONG_MAX
#define LONG_MAX 2147483647
#endif // LONG_MAX
#endif
enum TransmissionProtocolUartBridgeStatus {
STARTING,
IDLE,
SEND,
RECEIVE,
CONFIGURATION,
PARSE_FAILURE,
ERROR
};
enum SEND_STATUS {
SEND_NONE,
AWAIT_ADDRESS,
AWAIT_DATA,
SENDING
};
enum RECEIVE_STATUS {
RECEIVE_NONE,
SEND_ADDRESS,
SEND_DATA
};
enum CONFIGURATION_STATUS {
CONFIGURATION_NONE = 0,
CONFIGURATION_STATUS = 1,
CONFIGURATION_OWN_ADDRESS = 2,
CONFIGURATION_BROADCAST_ADDRESS = 3,
CONFIGURATION_MAXIMUM_MESSAGE_LENGTH = 4,
CONFIGURATION_SERIAL_BUFFER_SIZE = 5
};
// #define PARSERDATADEBUG
// #define PARSERADDRESSDEBUG
#define SERIAL_BUFFER_SIZE 200
#define RECEIVE_BUFFER_SIZE 64
#define SEND_BUFFER_SIZE 64
template<class TransmissionProtocolUartBridge_SocketInterface>
class TransmissionProtocolUartBridge {
private:
// SocketInterface
TransmissionProtocolUartBridge_SocketInterface &socketInterface;
// friend TransmissionProtocolUartBridge_SocketInterface;
// Status
TransmissionProtocolUartBridgeStatus status = STARTING;
RECEIVE_STATUS receive_status = RECEIVE_NONE;
SEND_STATUS send_status = SEND_NONE;
uint8_t configuration_status = CONFIGURATION_NONE;
// StreamBuffer
bool lineReady = false;
char serialBuffer[SERIAL_BUFFER_SIZE];
uint16_t serialBufferCounter = 0;
// SendBuffer
device_address destination_address;
uint8_t data[SEND_BUFFER_SIZE];
uint16_t data_length = 0;
// ReceiveBuffer
device_address receive_address;
uint8_t receive_buffer[RECEIVE_BUFFER_SIZE];
uint16_t receive_buffer_length = 0;
Stream *stream;
public:
explicit TransmissionProtocolUartBridge(Stream *stream,
TransmissionProtocolUartBridge_SocketInterface &socketInterface) :
stream(stream), socketInterface(socketInterface) {}
bool begin() {
memset(serialBuffer, 0x0, sizeof(serialBuffer));
socketInterface.setTransmissionProtocolUartBridge(this);
if (!socketInterface.begin()) {
status = ERROR;
return false;
}
status = IDLE;
return true;
}
/*
* Receive from Stream
*/
void putChar(char c) {
serialBuffer[serialBufferCounter++] = c;
if (c == '\n') {
lineReady = true;
} else if (serialBufferCounter == SERIAL_BUFFER_SIZE) {
status = PARSE_FAILURE;
}
}
void receiveData(device_address *address, uint8_t *bytes, uint16_t bytes_length) {
reset_received_buffer();
memcpy(&receive_address, address, sizeof(device_address));
receive_buffer_length = bytes_length;
memcpy(receive_buffer, bytes, bytes_length);
/*
if (bytes[0] != bytes_length) {
return;
}
if (receive_buffer_length == 0 && sizeof(receive_buffer) > bytes[0]) {
// buffer empty
// received bytes less or equal to receive_buffer size
reset_received_buffer();
memcpy(&receive_address, address, sizeof(device_address));
receive_buffer_length = bytes[0];
memcpy(receive_buffer, bytes, receive_buffer_length);
}
*/
}
bool print_received_address() {
stream->print(F("ADDRESS"));
for (uint16_t i = 0; i < sizeof(device_address); i++) {
stream->print(F(" "));
stream->print(receive_address.bytes[i], DEC);
}
stream->print(F("\n"));
return true;
}
bool print_received_data() {
stream->print(F("DATA"));
for (uint16_t i = 0; i < receive_buffer_length; i++) {
stream->print(F(" "));
stream->print(receive_buffer[i], DEC);
}
stream->print(F("\n"));
return true;
}
void reset_received_buffer() {
memset(&receive_address, 0x0, sizeof(device_address));
memset(receive_buffer, 0x0, sizeof(receive_buffer));
receive_buffer_length = 0;
}
void notify_socket_disconnected() {
status == ERROR;
}
void resetChip() {
#if defined(ESP8266)
stream->print(F("OK RESET\n"));
delay(500);
ESP.restart();
#elif defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_YUN)
stream->print(F("OK RESET\n"));
delay(500);
asm volatile (" jmp 0");
#else
#warning "resetChip() is not properly implemented. This means we cannot reset the TransmissionProtocolUartBridge."
stream->print(F("ERROR RESET_NOT_SUPPORTED\n"));
#endif
}
bool loop() {
if (!socketInterface.loop()) {
return false;
}
if (lineReady) {
if (isReset(serialBuffer)) {
resetSerialBuffer();
resetChip();
}
}
if (status == STARTING) {
// begin should be already called
stream->print(F("ERROR NOT_STARTED\n"));
return false;
}
if (status == IDLE && lineReady) {
// parse
if (isSend(serialBuffer)) {
status = SEND;
send_status = SEND_NONE;
resetSerialBuffer();
} else if (isReceived(serialBuffer)) {
status = RECEIVE;
receive_status = RECEIVE_NONE;
resetSerialBuffer();
} else if (isConfiguration(serialBuffer)) {
status = CONFIGURATION;
configuration_status = CONFIGURATION_NONE;
resetSerialBuffer();
} else if (isReset(serialBuffer)) {
resetSerialBuffer();
resetChip();
} else {
status = PARSE_FAILURE;
}
}
if (status == SEND) {
if (send_status == SEND_NONE) {
stream->print(F("OK AWAIT_ADDRESS\n"));
resetSendBuffer();
send_status = AWAIT_ADDRESS;
} else if (send_status == AWAIT_ADDRESS && lineReady) {
if (parseAddress(serialBuffer)) {
stream->print(F("OK AWAIT_DATA\n"));
resetSerialBuffer();
send_status = AWAIT_DATA;
} else {
status = PARSE_FAILURE;
}
} else if (send_status == AWAIT_DATA && lineReady) {
if (parseData(serialBuffer)) {
stream->print(F("OK SENDING\n"));
resetSerialBuffer();
send_status = SENDING;
} else {
status = PARSE_FAILURE;
}
} else if (send_status == SENDING) {
if (sendDataToAddress()) {
stream->print(F("OK IDLE\n"));
resetSendBuffer();
send_status = SEND_NONE;
status = IDLE;
} else {
status = ERROR;
}
}
}
if (status == RECEIVE) {
if (receive_status == RECEIVE_NONE) {
stream->print(F("OK SEND_ADDRESS\n"));
receive_status = SEND_ADDRESS;
} else if (receive_status == SEND_ADDRESS) {
if (send_receive_address()) {
stream->print(F("OK SEND_DATA\n"));
receive_status = SEND_DATA;
} else {
status = ERROR;
}
} else if (receive_status == SEND_DATA) {
if (send_receive_data()) {
stream->print(F("OK IDLE\n"));
resetReceiveBuffer();
receive_status = RECEIVE_NONE;
status = IDLE;
} else {
status = ERROR;
}
}
}
if (status == CONFIGURATION) {
if (configuration_status == CONFIGURATION_NONE) {
stream->print(F("OK SEND_STATUS\n"));
configuration_status = CONFIGURATION_STATUS;
} else if (configuration_status == CONFIGURATION_STATUS) {
if (printStatus()) {
stream->print(F("OK SEND_OWN_ADDRESS\n"));
configuration_status = CONFIGURATION_OWN_ADDRESS;
} else {
status = ERROR;
}
} else if (configuration_status == CONFIGURATION_OWN_ADDRESS) {
if (printOwnAddress()) {
stream->print(F("OK SEND_BROADCAST_ADDRESS\n"));
configuration_status = CONFIGURATION_BROADCAST_ADDRESS;
} else {
status = ERROR;
}
} else if (configuration_status == CONFIGURATION_BROADCAST_ADDRESS) {
if (printBroadcastAddress()) {
stream->print(F("OK SEND_MAXIMUM_MESSAGE_LENGTH\n"));
configuration_status = CONFIGURATION_MAXIMUM_MESSAGE_LENGTH;
} else {
status = ERROR;
}
} else if (configuration_status == CONFIGURATION_MAXIMUM_MESSAGE_LENGTH) {
if (printMaximumMessageLength()) {
stream->print(F("OK SEND_SERIAL_BUFFER_SIZE\n"));
configuration_status = CONFIGURATION_SERIAL_BUFFER_SIZE;
} else {
status = ERROR;
}
} else if (configuration_status == CONFIGURATION_SERIAL_BUFFER_SIZE) {
if (printSerialBufferSize()) {
stream->print(F("OK IDLE\n"));
configuration_status = CONFIGURATION_NONE;
status = IDLE;
} else {
status = ERROR;
}
}
}
if (status == PARSE_FAILURE) {
stream->print(F("FAILURE PARSE_FAILURE\n"));
resetSerialBuffer();
receive_status = RECEIVE_NONE;
send_status = SEND_NONE;
status = IDLE;
}
/*
if (status == FAILURE) {
stream->print(F("FAILURE\n"));
resetSerialBuffer();
receive_status = RECEIVE_NONE;
send_status = SEND_NONE;
status = STARTING;
}
*/
if (status == ERROR) {
stream->print(F("ERROR\n"));
resetSerialBuffer();
resetChip();
}
}
bool printStatus() {
stream->print(F("STATUS"));
stream->print(F(" "));
if (status == STARTING) {
stream->print(F("STARTING"));
} else if (status == IDLE) {
stream->print(F("IDLE"));
} else if (status == SEND) {
stream->print(F("SEND"));
} else if (status == CONFIGURATION) {
stream->print(F("CONFIGURATION"));
} else if (status == PARSE_FAILURE) {
stream->print(F("PARSE_FAILURE"));
} else if (status == ERROR) {
stream->print(F("ERROR"));
} else {
stream->print(F("\n"));
return false;
}
stream->print(F("\n"));
return true;
}
bool printOwnAddress() {
stream->print(F("OWN_ADDRESS"));
device_address *address = socketInterface.getAddress();
if (address != nullptr) {
for (uint16_t i = 0; i < sizeof(device_address); i++) {
stream->print(F(" "));
stream->print(address->bytes[i], DEC);
}
} else {
stream->print(F(" "));
stream->print(F("N/A"));
}
stream->print(F("\n"));
return true;
}
bool printBroadcastAddress() {
stream->print(F("BROADCAST_ADDRESS"));
device_address *address = socketInterface.getBroadcastAddress();
if (address != nullptr) {
for (uint16_t i = 0; i < sizeof(device_address); i++) {
stream->print(F(" "));
stream->print(address->bytes[i], DEC);
}
} else {
stream->print(F(" "));
stream->print(F("N/A"));
}
stream->print(F("\n"));
return true;
}
bool printMaximumMessageLength() {
stream->print(F("MAXIMUM_MESSAGE_LENGTH"));
stream->print(F(" "));
uint8_t maximumMessageLength = socketInterface.getMaximumMessageLength();
stream->print(maximumMessageLength, DEC);
stream->print(F("\n"));
return true;
}
bool printSerialBufferSize() {
stream->print(F("SERIAL_BUFFER_SIZE"));
stream->print(F(" "));
stream->print(SERIAL_BUFFER_SIZE, DEC);
stream->print(F("\n"));
return true;
}
void printSerialBuffer() {
if (lineReady) {
for (uint16_t i = 0; i < serialBufferCounter; i++) {
stream->print(serialBuffer[i]);
}
} else {
stream->print(F("lineNotReady"));
}
}
bool isReset(char *buffer) {
/*
char *token = strsep(&buffer, " ");
if (token == NULL) {
return false;
}
*/
return strncmp(buffer, "RESET", strlen("RESET")) == 0;
//return memcmp(token, "RESET", strlen("RESET")) == 0;
}
void resetReceiveBuffer() {
reset_received_buffer();
}
bool send_error() {
stream->print(F("ERROR\n"));
return true;
}
bool send_receive_data() {
return print_received_data();
}
bool send_receive_address() {
return print_received_address();
}
void resetSerialBuffer() {
memset(serialBuffer, 0x0, sizeof(serialBuffer));
serialBufferCounter = 0;
lineReady = false;
}
void resetSendBuffer() {
memset(&destination_address, 0x0, sizeof(device_address));
memset(data, 0x0, sizeof(data));
data_length = 0;
}
bool sendDataToAddress() {
return socketInterface.send(&destination_address, (uint8_t * ) & data, data_length);
}
bool parseData(char *buffer) {
#if defined(PARSERDATADEBUG)
stream->print(F("parseData\n"));
#endif
char *token = strsep(&buffer, " ");
if (token == NULL) {
#if defined(PARSERDATADEBUG)
stream->print(F("token NULL\n"));
#endif
return false;
}
if (memcmp(token, "DATA", strlen("DATA")) != 0) {
#if defined(PARSERDATADEBUG)
stream->print(F("does not start with DATA\n"));
#endif
return false;
}
memset(data, 0x0, sizeof(data));
data_length = 0;
while ((token = strsep(&buffer, " ")) != NULL) {
long int number = 0;
if (!parseLong(token, &number)) {
#if defined(PARSERDATADEBUG)
stream->print(F("failure parsing parseLong\n"));
#endif
return false;
}
if (number > UINT8_MAX || number < 0) {
#if defined(PARSERDATADEBUG)
stream->print(F("number out of range\n"));
#endif
return false;
}
#if defined(PARSERDATADEBUG)
stream->print(number, DEC);
stream->print(F("\n"));
#endif
data[data_length++] = (uint8_t) number;
}
return true;
}
bool parseAddress(char *buffer) {
#if defined(PARSERADDRESSDEBUG)
stream->print(F("parseAddress\n"));
#endif
char *token = strsep(&buffer, " ");
if (token == NULL) {
#if defined(PARSERADDRESSDEBUG)
stream->print(F("token NULL\n"));
#endif
return false;
}
if (memcmp(token, "ADDRESS", strlen("ADDRESS")) != 0) {
#if defined(PARSERADDRESSDEBUG)
stream->print(F("does not start with ADDRESS\n"));
#endif
return false;
}
memset(&destination_address, 0x0, sizeof(device_address));
uint16_t destination_address_length = 0;
while ((token = strsep(&buffer, " ")) != NULL) {
long int number = 0;
if (!parseLong(token, &number)) {
#if defined(PARSERADDRESSDEBUG)
stream->print(F("failure parsing parseLong\n"));
#endif
return false;
}
#if defined(PARSERADDRESSDEBUG)
stream->print(number, DEC);
stream->print(F("\n"));
#endif
if (number > UINT8_MAX || number < 0) {
#if defined(PARSERADDRESSDEBUG)
stream->print(F("number out of bounds: "));
stream->print(number, DEC);
stream->print(F("\n"));
#endif
return false;
}
if (destination_address_length + 1 > sizeof(device_address)) {
#if defined(PARSERADDRESSDEBUG)
stream->print(F("address size too long"));
#endif
return false;
}
destination_address.bytes[destination_address_length++] = (uint8_t) number;
}
#if defined(PARSERADDRESSDEBUG)
if (destination_address_length != sizeof(device_address)) {
stream->print(F("destination_address_length unequal device_address length"));
}
#endif
return destination_address_length == sizeof(device_address);
}
bool isReceived(char *buffer) {
char *token = strsep(&buffer, " ");
if (token == NULL) {
return false;
}
return memcmp(token, "RECEIVE", strlen("RECEIVE")) == 0;
}
bool isSend(char *buffer) {
char *token = strsep(&buffer, " ");
if (token == NULL) {
return false;
}
return memcmp(token, "SEND", strlen("SEND")) == 0;
}
bool isConfiguration(char *buffer) {
char *token = strsep(&buffer, " ");
if (token == NULL) {
return false;
}
return memcmp(token, "CONFIGURATION", strlen("CONFIGURATION")) == 0;
}
bool parseLong(const char *str, long *val) {
char *temp;
bool rc = true;
#if defined(ESP8266)
errno = 0;
#endif
*val = strtol(str, &temp, 0);
if (temp == str || (*temp != '\0' && *temp != '\n' && *temp != '\r') ||
#if defined(ESP8266)
((*val == LONG_MIN || *val == LONG_MAX) && errno == ERANGE))
#else
// see: strtol() function return values
((*val == LONG_MIN || *val == LONG_MAX) && true))
#endif
{
rc = false;
}
return rc;
}
};
#endif //ARDUINO_MQTTSN_CLIENT_TRANSMISSIONPROTOCOLUARTBRDIGE_H