forked from gioblu/PJON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPJON.h
1083 lines (898 loc) · 33.4 KB
/
PJON.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-O//\ __ __
|-gfo\ |__| | | | |\ | ™
|!y°o:\ | __| |__| | \| v8.2
|y"s§+`\ multi-master, multi-media communications bus system framework
/so+:-..`\ Copyright 2010-2017 by Giovanni Blu Mitolo [email protected]
|+/:ngr-*.`\
|5/:%&-a3f.:;\
\+//u/+g%{osv,,\
\=+&/osw+olds.\\
\:/+-.-°-:+oss\
| | \oy\\
> <
______-| |-___________________________________________________________________
PJON™ is a self-funded, no-profit open-source project created and maintained by
Giovanni Blu Mitolo with the support of the internet community if you want
to see the PJON project growing with a faster pace, consider a donation
at the following link: https://www.paypal.me/PJON
For the PJON™ Protocol specification see the specification directory.
PJON™ Standard compliant tools:
- ModuleInterface - Easy config and value sync between IOT modules
https://github.com/fredilarsen/ModuleInterface
- Command line PJON wrapper over unnamed pipes by Zbigniew Zasieczny
https://github.com/Girgitt/PJON-piper
- PJON-python - PJON running on Python by Zbigniew Zasieczny
https://github.com/Girgitt/PJON-python
- Logic analyzer by Andrew Grande (obsolete)
https://github.com/aperepel/saleae-pjon-protocol-analyzer
Credits to contributors:
- Fred Larsen. Systems engineering, header driven communication, debugging
- Zbigniew Zasieczny. WINX86 interface
- 4ib3r github user. Memory optimization configurable strategies inclusion
- budaics github user. ATtiny85 16MHz external clock testing and wiki page
- Pantovich github user. Update returning number of packets to be delivered
- Adrian Sławiński. Fix to enable SimpleModbusMasterV2 compatibility
- SticilFace github user. Teensy porting
- Esben Soeltoft. Arduino Zero porting
- Alex Grishin. ESP8266 porting
- Andrew Grande. Testing, support, bugfix
- Mauro Zancarlin. Systems engineering, testing, bugfix
- Michael Teeww. Callback based reception, debugging
- PaoloP74 github user. Library conversion to 1.x Arduino IDE
Bug reports:
- bryant1410 github user. Debug readme format
- pacproduct github user. Added missing mode configuration PJON_SIMPLEX example
- elusive-code github user. PJONMaster reset bug
- Franketto arduino forum user. PJON ThroughSerial over RS485 delay issue
- Zbigniew Zasieczny. Header reference inconsistency report
- DanRoad reddit user. can_start ThroughSerial bugfix
- Remo Kallio. Packet index 0 bugfix
- Emanuele Iannone. Forcing PJON_SIMPLEX in OverSamplingSimplex
- Christian Pointner. Fixed compiler warnings
- Andrew Grande. ESP8266 example watchdog error bug fix
- Fabian Gärtner. receive function and big packets bugfix
- Mauro Mombelli. Code cleanup
- Shachar Limor. Blink example pinMode bugfix
______________________________________________________________________________
Copyright 2010-2017 by Giovanni Blu Mitolo [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "interfaces/PJON_Interfaces.h"
#include "PJONDefines.h"
#include "strategies/PJON_Strategies.h"
template<typename Strategy>
class PJON {
public:
/* Abstract data link layer class */
Strategy strategy;
uint16_t config = PJON_TX_INFO_BIT | PJON_ACK_REQ_BIT;
uint8_t bus_id[4] = {0, 0, 0, 0};
const uint8_t localhost[4] = {0, 0, 0, 0};
/* Data buffers */
uint8_t data[PJON_PACKET_MAX_LENGTH];
PJON_Packet_Info last_packet_info;
PJON_Packet packets[PJON_MAX_PACKETS];
#if(PJON_INCLUDE_ASYNC_ACK)
PJON_Packet_Record recent_packet_ids[PJON_MAX_RECENT_PACKET_IDS];
#endif
/* PJON bus default initialization:
State: Local (bus_id: 0.0.0.0)
Acknowledge: true (Acknowledge is requested)
device id: PJON_NOT_ASSIGNED (255)
Mode: PJON_HALF_DUPLEX
Sender info: true (Sender info are included in the packet)
Strategy: SoftwareBitBang */
PJON() : strategy(Strategy()) {
_device_id = PJON_NOT_ASSIGNED;
set_default();
};
/* PJON initialization passing device id:
PJON bus(1); */
PJON(uint8_t device_id) : strategy(Strategy()) {
_device_id = device_id;
set_default();
};
/* PJON initialization passing bus and device id:
uint8_t my_bus = {1, 1, 1, 1};
PJON bus(my_bys, 1); */
PJON(const uint8_t *b_id, uint8_t device_id) : strategy(Strategy()) {
copy_bus_id(bus_id, b_id);
_device_id = device_id;
set_default();
};
/* Begin function to be called in setup: */
void begin() {
PJON_RANDOM_SEED(PJON_ANALOG_READ(_random_seed) + _device_id);
strategy.begin(_device_id);
#if(PJON_INCLUDE_ASYNC_ACK)
_packet_id_seed = PJON_RANDOM(65535) + _device_id;
#endif
};
/* Compose packet in PJON format: */
uint16_t compose_packet(
const uint8_t id,
const uint8_t *b_id,
char *destination,
const char *source,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED,
uint16_t p_id = 0
) {
if(header == PJON_NOT_ASSIGNED) header = config;
if(header > 255) header |= PJON_EXT_HEAD_BIT;
if(length > 255) header |= PJON_EXT_LEN_BIT;
if(id == PJON_BROADCAST)
header &= ~(PJON_ACK_REQ_BIT | PJON_ACK_MODE_BIT);
uint16_t new_length = length + packet_overhead(header);
bool extended_header = header & PJON_EXT_HEAD_BIT;
bool extended_length = header & PJON_EXT_LEN_BIT;
#if(PJON_INCLUDE_ASYNC_ACK)
bool async_ack =
(header & PJON_ACK_MODE_BIT) && (header & PJON_TX_INFO_BIT);
if(!p_id && async_ack) p_id = new_packet_id();
#endif
if(new_length > 15 && !(header & PJON_CRC_BIT)) {
header |= PJON_CRC_BIT;
new_length = (uint16_t)(length + packet_overhead(header));
}
if(new_length > 255 && !extended_length) {
header |= PJON_EXT_LEN_BIT;
new_length = (uint16_t)(length + packet_overhead(header));
}
if(new_length >= PJON_PACKET_MAX_LENGTH) {
_error(PJON_CONTENT_TOO_LONG, new_length);
return 0;
}
destination[0] = id;
if(extended_header) {
destination[1] = (uint16_t)header;
destination[2] = (uint16_t)header >> 8;
} else destination[1] = header;
if(extended_length) {
destination[2 + extended_header] = new_length >> 8;
destination[3 + extended_header] = new_length & 0xFF;
} else destination[2 + extended_header] = new_length;
if(header & PJON_MODE_BIT) {
copy_bus_id(
(uint8_t*) &destination[3 + extended_header + extended_length],
b_id
);
if(header & PJON_TX_INFO_BIT) {
copy_bus_id(
(uint8_t*) &destination[7 + extended_header + extended_length],
bus_id
);
destination[11 + extended_header + extended_length] = _device_id;
#if(PJON_INCLUDE_ASYNC_ACK)
if(async_ack)
memcpy(
destination + 12 + extended_header + extended_length,
&p_id,
2
);
#endif
}
} else if(header & PJON_TX_INFO_BIT) {
destination[3 + extended_header + extended_length] = _device_id;
#if(PJON_INCLUDE_ASYNC_ACK)
if(async_ack)
memcpy(
destination + 4 + extended_header + extended_length,
&p_id,
2
);
#endif
}
memcpy(
destination + (new_length - length - (header & PJON_CRC_BIT ? 4 : 1)),
source,
length
);
if(header & PJON_CRC_BIT) {
uint32_t computed_crc =
PJON_crc32::compute((uint8_t *)destination, new_length - 4);
destination[new_length - 4] = (uint32_t)(computed_crc) >> 24;
destination[new_length - 3] = (uint32_t)(computed_crc) >> 16;
destination[new_length - 2] = (uint32_t)(computed_crc) >> 8;
destination[new_length - 1] = (uint32_t)(computed_crc);
} else destination[new_length - 1] =
PJON_crc8::compute((uint8_t *)destination, new_length - 1);
return new_length;
};
/* Get the device id, returning a single byte: */
uint8_t device_id() const {
return _device_id;
};
/* Add a packet to the send list, delivered by the next update() call: */
uint16_t dispatch(
uint8_t id,
const uint8_t *b_id,
const char *packet,
uint16_t length,
uint32_t timing,
uint16_t header = PJON_NOT_ASSIGNED,
uint16_t p_id = 0,
uint16_t p_index = PJON_FAIL
) {
bool req_index = (p_index != PJON_FAIL);
for(uint16_t i = ((req_index) ? p_index : 0); i < PJON_MAX_PACKETS; i++)
if(packets[i].state == 0 || req_index) {
if(!(length = compose_packet(
id, b_id, packets[i].content, packet, length, header, p_id
))) return PJON_FAIL;
packets[i].length = length;
packets[i].state = PJON_TO_BE_SENT;
packets[i].registration = PJON_MICROS();
packets[i].timing = timing;
return i;
}
_error(PJON_PACKETS_BUFFER_FULL, PJON_MAX_PACKETS);
return PJON_FAIL;
};
/* Check if a packet id is already dispatched in buffer: */
bool dispatched(PJON_Packet_Info info) {
PJON_Packet_Info actual_info;
for(uint16_t i = 0; i < PJON_MAX_PACKETS; i++) {
parse((uint8_t *)packets[i].content, actual_info);
if(
packets[i].state && packets[i].state != PJON_ACK &&
(actual_info.header & PJON_ACK_MODE_BIT) &&
(actual_info.header & PJON_TX_INFO_BIT)
)
if(
actual_info.id == info.id &&
info.sender_id == actual_info.receiver_id &&
(
!(info.header & PJON_MODE_BIT) ? true :
bus_id_equality(
info.receiver_bus_id,
actual_info.receiver_bus_id
)
)
) return true;
}
return false;
};
/* Get count of the packets for a device_id:
Don't pass any parameter to count all packets
Pass a device id to count all it's related packets */
uint16_t get_packets_count(uint8_t device_id = PJON_NOT_ASSIGNED) const {
uint16_t packets_count = 0;
for(uint16_t i = 0; i < PJON_MAX_PACKETS; i++) {
if(packets[i].state == 0) continue;
if(
device_id == PJON_NOT_ASSIGNED ||
packets[i].content[0] == device_id
) packets_count++;
}
return packets_count;
};
/* Generate a new packet id: */
uint16_t new_packet_id() {
_packet_id_seed += 1;
if(!_packet_id_seed) _packet_id_seed = 1;
return _packet_id_seed;
};
/* Calculate the packet's overhead: */
uint8_t packet_overhead(uint16_t header = PJON_NOT_ASSIGNED) const {
header = (header == PJON_NOT_ASSIGNED) ? config : header;
return (
(
(header & PJON_MODE_BIT) ?
(header & PJON_TX_INFO_BIT ? 10 : 5) :
(header & PJON_TX_INFO_BIT ? 2 : 1)
) + (header & PJON_EXT_LEN_BIT ? 2 : 1)
+ (header & PJON_EXT_HEAD_BIT ? 2 : 1)
+ (header & PJON_CRC_BIT ? 4 : 1)
+ (header & PJON_ACK_MODE_BIT ? 2 : 0)
);
};
/* Fill in a PJON_Packet_Info struct by parsing a packet: */
void parse(const uint8_t *packet, PJON_Packet_Info &packet_info) const {
packet_info.receiver_id = packet[0];
bool extended_header = packet[1] & PJON_EXT_HEAD_BIT;
bool extended_length = packet[1] & PJON_EXT_LEN_BIT;
packet_info.header =
(extended_header) ? packet[2] << 8 | packet[1] : packet[1];
uint8_t offset = extended_header + extended_length;
if((packet_info.header & PJON_MODE_BIT) != 0) {
copy_bus_id(packet_info.receiver_bus_id, packet + 3 + offset);
if((packet_info.header & PJON_TX_INFO_BIT) != 0) {
copy_bus_id(packet_info.sender_bus_id, packet + 7 + offset);
packet_info.sender_id = packet[11 + offset];
#if(PJON_INCLUDE_ASYNC_ACK)
if(packet_info.header & PJON_ACK_MODE_BIT)
packet_info.id =
(packet[13 + offset] << 8) | (packet[12 + offset] & 0xFF);
#endif
}
} else if((packet_info.header & PJON_TX_INFO_BIT) != 0) {
packet_info.sender_id = packet[3 + offset];
#if(PJON_INCLUDE_ASYNC_ACK)
if(packet_info.header & PJON_ACK_MODE_BIT)
packet_info.id =
(packet[5 + offset] << 8) | (packet[4 + offset] & 0xFF);
#endif
}
};
uint16_t receive() {
uint16_t length = PJON_PACKET_MAX_LENGTH;
uint16_t batch_length = 0;
bool computed_crc = 0;
bool extended_header = false;
bool extended_length = false;
for(uint16_t i = 0; i < length; i++) {
if(!batch_length) {
batch_length = strategy.receive_string(data + i, length - i);
if(batch_length == PJON_FAIL || batch_length == 0)
return PJON_FAIL;
}
batch_length--;
if(i == 0)
if(data[i] != _device_id && data[i] != PJON_BROADCAST && !_router)
return PJON_BUSY;
if(i == 1) {
if(
(
((data[1] & PJON_MODE_BIT) != (config & PJON_MODE_BIT)) &&
!_router
) || (
data[0] == PJON_BROADCAST &&
((data[1] & PJON_ACK_MODE_BIT) || (data[1] & PJON_ACK_REQ_BIT))
) || (
((data[1] & PJON_ACK_MODE_BIT) && !(data[1] & PJON_TX_INFO_BIT))
) || (
((data[1] & PJON_EXT_LEN_BIT) && !(data[1] & PJON_CRC_BIT))
)
) return PJON_BUSY;
extended_length = data[i] & PJON_EXT_LEN_BIT;
extended_header = data[i] & PJON_EXT_HEAD_BIT;
}
if((i == (2 + extended_header)) && !extended_length) {
length = data[i];
if(length < 5 || length > PJON_PACKET_MAX_LENGTH) return PJON_FAIL;
}
if((i == (3 + extended_header)) && extended_length) {
length = (data[i - 1] << 8) | (data[i] & 0xFF);
if(length < 5 || length > PJON_PACKET_MAX_LENGTH) return PJON_FAIL;
}
if((config & PJON_MODE_BIT) && (data[1] & PJON_MODE_BIT) && !_router)
if((i > (2 + extended_header + extended_length)))
if((i < (7 + extended_header + extended_length)))
if(bus_id[i - 3 - extended_header - extended_length] != data[i])
return PJON_BUSY;
}
if(data[1] & PJON_CRC_BIT)
computed_crc = PJON_crc32::compare(
PJON_crc32::compute(data, length - 4), data + (length - 4)
);
else computed_crc = !PJON_crc8::compute(data, length);
if(data[1] & PJON_ACK_REQ_BIT && data[0] != PJON_BROADCAST)
if(_mode != PJON_SIMPLEX && !_router)
if(
!(config & PJON_MODE_BIT) ||
(
(config & PJON_MODE_BIT) &&
(data[1] & PJON_MODE_BIT) &&
bus_id_equality(
data + 3 + extended_length + extended_header,
bus_id
)
) && computed_crc
) strategy.send_response(PJON_ACK);
if(!computed_crc) return PJON_NAK;
parse(data, last_packet_info);
#if(PJON_INCLUDE_ASYNC_ACK)
/* If a packet requesting asynchronous acknowledgement is received
send the acknowledgement packet back to the packet's transmitter */
if((data[1] & PJON_ACK_MODE_BIT) && (data[1] & PJON_TX_INFO_BIT)) {
if(_auto_delete && length == packet_overhead(data[1]))
if(handle_asynchronous_acknowledgment(last_packet_info))
return PJON_ACK;
if(length > packet_overhead(data[1])) {
if(!dispatched(last_packet_info)) {
dispatch(
last_packet_info.sender_id,
(uint8_t *)last_packet_info.sender_bus_id,
NULL,
0,
0,
config | PJON_ACK_MODE_BIT | PJON_TX_INFO_BIT,
last_packet_info.id
);
update();
}
if(known_packet_id(last_packet_info))
return PJON_ACK;
}
}
#endif
_receiver(
data + (packet_overhead(data[1]) - (data[1] & PJON_CRC_BIT ? 4 : 1)),
length - packet_overhead(data[1]),
last_packet_info
);
return PJON_ACK;
};
/* Try to receive a packet repeatedly with a maximum duration: */
uint16_t receive(uint32_t duration) {
uint16_t response = PJON_FAIL;
uint32_t time = PJON_MICROS();
while((uint32_t)(PJON_MICROS() - time) <= duration) {
response = receive();
if(response == PJON_ACK)
return PJON_ACK;
}
return response;
};
/* Remove a packet from the send list: */
void remove(uint16_t index) {
packets[index].attempts = 0;
packets[index].length = 0;
packets[index].registration = 0;
packets[index].state = 0;
};
/* Remove a packet from the packet's buffer passing its id as reference: */
bool handle_asynchronous_acknowledgment(PJON_Packet_Info packet_info) {
PJON_Packet_Info actual_info;
for(uint16_t i = 0; i < PJON_MAX_PACKETS; i++) {
parse((uint8_t *)packets[i].content, actual_info);
if(actual_info.id == packet_info.id)
if(actual_info.receiver_id == packet_info.sender_id && (
(!(actual_info.header & PJON_MODE_BIT) &&
!(packet_info.header & PJON_MODE_BIT)) ? true :
bus_id_equality(
actual_info.receiver_bus_id,
packet_info.sender_bus_id
)
)) {
if(packets[i].timing) {
uint8_t offset = packet_overhead(actual_info.header);
uint8_t crc_offset =
((actual_info.header & PJON_CRC_BIT) ? 4 : 1);
dispatch(
actual_info.receiver_id,
(uint8_t *)actual_info.receiver_bus_id,
packets[i].content + (offset - crc_offset),
packets[i].length - offset,
packets[i].timing,
actual_info.header,
new_packet_id(),
i
);
packets[i].attempts = 0;
return true;
}
remove(i);
return true;
}
}
return false;
};
/* Remove all packets from the list:
Don't pass any parameter to delete all packets
Pass a device id to delete all it's related packets */
void remove_all_packets(uint8_t device_id = 0) {
for(uint16_t i = 0; i < PJON_MAX_PACKETS; i++) {
if(packets[i].state == 0) continue;
if(!device_id || packets[i].content[0] == device_id) remove(i);
}
};
/* Send a packet to the sender of the last packet received.
This function is typically called from with the receive
callback function to deliver a response to a request. */
uint16_t reply(
const char *packet,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
if(last_packet_info.sender_id != PJON_BROADCAST)
return dispatch(
last_packet_info.sender_id,
last_packet_info.sender_bus_id,
packet,
length,
0,
header
);
return false;
};
/* Insert a packet in the send list: */
uint16_t send(
uint8_t id,
const char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
return dispatch(id, bus_id, string, length, 0, header);
};
uint16_t send(
uint8_t id,
const uint8_t *b_id,
const char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
return dispatch(id, b_id, string, length, 0, header);
};
/* Send a packet and configure its sender info: */
uint16_t send_from_id(
uint8_t sender_id,
const uint8_t *sender_bus_id,
uint8_t id,
const uint8_t *b_id,
const char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
uint8_t original_device_id = _device_id;
uint8_t original_bus_id[4];
copy_bus_id(original_bus_id, bus_id);
set_id(sender_id);
copy_bus_id(bus_id, sender_bus_id);
uint16_t result = dispatch(id, b_id, string, length, 0, header);
copy_bus_id(bus_id, original_bus_id);
set_id(original_device_id);
return result;
};
/* IMPORTANT: send_repeatedly timing maximum
is 4293014170 microseconds or 71.55 minutes */
uint16_t send_repeatedly(
uint8_t id,
const char *string,
uint16_t length,
uint32_t timing,
uint16_t header = PJON_NOT_ASSIGNED
) {
return dispatch(id, bus_id, string, length, timing, header);
};
/* IMPORTANT: send_repeatedly timing maximum
is 4293014170 microseconds or 71.55 minutes */
uint16_t send_repeatedly(
uint8_t id,
const uint8_t *b_id,
const char *string,
uint16_t length,
uint32_t timing,
uint16_t header = PJON_NOT_ASSIGNED
) {
return dispatch(id, b_id, string, length, timing, header);
};
/* Send an already composed packet: */
uint16_t send_packet(const char *string, uint16_t length) {
if(!string) return PJON_FAIL;
if(_mode != PJON_SIMPLEX && !strategy.can_start()) return PJON_BUSY;
strategy.send_string((uint8_t *)string, length);
if(
string[0] == PJON_BROADCAST ||
!(string[1] & PJON_ACK_REQ_BIT) ||
_mode == PJON_SIMPLEX
) return PJON_ACK;
uint16_t response = strategy.receive_response();
if(
response == PJON_ACK ||
response == PJON_FAIL
) return response;
else return PJON_BUSY;
};
/* Compose and send a packet passing its info as parameters: */
uint16_t send_packet(
uint8_t id,
char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
if(!(length =
compose_packet(id, bus_id, (char *)data, string, length, header)))
return PJON_FAIL;
return send_packet((char *)data, length);
};
uint16_t send_packet(
uint8_t id,
const uint8_t *b_id,
char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
if(!(length =
compose_packet(id, b_id, (char *)data, string, length, header)))
return PJON_FAIL;
return send_packet((char *)data, length);
};
/* Send a packet without using the send list. It is called
send_packet_blocking because it tries to transmit a packet multiple
times within an internal cycle until the packet is delivered, or timing
limit is reached. */
uint16_t send_packet_blocking(
uint8_t id,
const uint8_t *b_id,
const char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED,
uint32_t timeout = 3000000
) {
if(!(length = compose_packet(
id,
b_id,
(char *)data,
string,
length,
header
))) return PJON_FAIL;
uint16_t state = PJON_FAIL;
uint32_t attempts = 0;
uint32_t time = PJON_MICROS(), start = time;
while(
(state != PJON_ACK) && (attempts <= strategy.get_max_attempts()) &&
(uint32_t)(PJON_MICROS() - start) <= timeout
) {
state = send_packet((char*)data, length);
if(state == PJON_ACK) return state;
attempts++;
if(state != PJON_FAIL) strategy.handle_collision();
while((uint32_t)(PJON_MICROS() - time) < strategy.back_off(attempts));
time = PJON_MICROS();
}
return state;
};
uint16_t send_packet_blocking(
uint8_t id,
const char *string,
uint16_t length,
uint16_t header = PJON_NOT_ASSIGNED
) {
return send_packet_blocking(id, bus_id, string, length, header);
};
/* In router mode, the receiver function can ack for selected receiver
device ids for which the route is known */
void send_synchronous_acknowledge() {
strategy.send_response(PJON_ACK);
};
/* Set the config bit state: */
void set_config_bit(bool new_state, uint16_t bit) {
if(new_state) config |= bit;
else config &= ~bit;
};
/* Configure synchronous acknowledge presence:
TRUE: Send synchronous acknowledge when a packet is correctly received
FALSE: Avoid acknowledge transmission */
void set_synchronous_acknowledge(bool state) {
set_config_bit(state, PJON_ACK_REQ_BIT);
};
/* Configure asynchronous acknowledge presence:
TRUE: Send back asynchronous acknowledge packet
FALSE: Avoid acknowledge packet transmission */
void set_asynchronous_acknowledge(bool state) {
set_config_bit(state, PJON_ACK_MODE_BIT);
};
/* Configure CRC selected for packet checking:
TRUE: CRC32
FALSE: CRC8 */
void set_crc_32(bool state) {
set_config_bit(state, PJON_CRC_BIT);
};
/* Set communication mode: */
void set_communication_mode(uint8_t mode) {
_mode = mode;
};
/* Set bus state default configuration: */
void set_default() {
_mode = PJON_HALF_DUPLEX;
if(!bus_id_equality(bus_id, localhost)) set_shared_network(true);
set_error(PJON_dummy_error_handler);
set_receiver(PJON_dummy_receiver_handler);
for(uint16_t i = 0; i < PJON_MAX_PACKETS; i++) {
packets[i].state = 0;
packets[i].timing = 0;
packets[i].attempts = 0;
}
};
/* Pass as a parameter a void function you previously defined in your code.
This will be called when an error in communication occurs
void error_handler(uint8_t code, uint8_t data) {
Serial.print(code);
Serial.print(" ");
Serial.println(data);
};
bus.set_error(error_handler); */
void set_error(PJON_Error e) {
_error = e;
};
/* Set the device id, passing a single byte (watch out to id collision): */
void set_id(uint8_t id) {
_device_id = id;
};
/* Configure sender's information inclusion in the packet.
TRUE: +1 byte (device id) local, +5 bytes (bus id + device id) shared
FALSE: No inclusion -1 byte overhead in local, -5 in shared
If you don't need the sender info disable the inclusion to reduce
overhead and higher communication speed. */
void include_sender_info(bool state) {
set_config_bit(state, PJON_TX_INFO_BIT);
};
/* Configure the bus network behaviour.
TRUE: Enable devices part of other bus ids (shared medium).
FALSE: Isolate from external/third-party communication. */
void set_shared_network(bool state) {
set_config_bit(state, PJON_MODE_BIT);
};
/* Set if delivered or undeliverable packets are auto deleted:
TRUE: Automatic deletion
FALSE: No packet deletion from buffer, (deletion from buffer by user) */
void set_packet_auto_deletion(bool state) {
_auto_delete = state;
};
/* Set the analog pin used as a seed for random generator: */
void set_random_seed(uint8_t random_seed) {
_random_seed = random_seed;
};
/* Pass as a parameter a void function you previously defined in your
code. This will be called when a correct message will be received.
Inside there you can code how to react when data is received.
void receiver_function(
uint8_t *payload,
uint16_t length,
const PJON_Packet_Info &packet_info
) {
for(int i = 0; i < length; i++)
Serial.print((char)payload[i]);
Serial.print(" ");
Serial.println(length);
};
bus.set_receiver(receiver_function); */
void set_receiver(PJON_Receiver r) {
_receiver = r;
};
/* Configure if device will act as a router:
FALSE: device receives messages only for its bus and device id
TRUE: The receiver function is always called if data is received */
void set_router(bool state) {
_router = state;
};
/* Update the state of the send list:
Check if there are packets to be sent or to be erased if correctly
delivered. Returns the actual number of packets to be sent. */
uint16_t update() {
uint16_t packets_count = 0;
for(uint16_t i = 0; i < PJON_MAX_PACKETS; i++) {
if(packets[i].state == 0) continue;
packets_count++;
#if(PJON_ORDERED_SENDING)
if(!first_packet_to_be_sent(i)) continue;
#endif
bool async_ack = (packets[i].content[1] & PJON_ACK_MODE_BIT) &&
(packets[i].content[1] & PJON_TX_INFO_BIT);
bool sync_ack = (packets[i].content[1] & PJON_ACK_REQ_BIT);
if(
(uint32_t)(PJON_MICROS() - packets[i].registration) >
(uint32_t)(
packets[i].timing +
strategy.back_off(packets[i].attempts)
)
) {
if(!(sync_ack && async_ack && packets[i].state == PJON_ACK))
packets[i].state = // Avoid resending sync-acked async ack packets
send_packet(packets[i].content, packets[i].length);
} else continue;
packets[i].attempts++;
if(packets[i].state == PJON_ACK) {
if(!packets[i].timing) {
if(
_auto_delete && (
(packets[i].length == packet_overhead(packets[i].content[1])
&& async_ack ) || !(packets[i].content[1] & PJON_ACK_MODE_BIT))
) {
remove(i);
packets_count--;
}
} else {
if(!async_ack) {
packets[i].attempts = 0;
packets[i].registration = PJON_MICROS();
packets[i].state = PJON_TO_BE_SENT;
}
}
if(!async_ack) continue;
}
if(packets[i].state != PJON_FAIL && packets[i].state != PJON_ACK)
strategy.handle_collision();
if(packets[i].attempts > strategy.get_max_attempts()) {
_error(PJON_CONNECTION_LOST, i);
if(!packets[i].timing) {
if(_auto_delete) {
remove(i);
packets_count--;
}
} else {
packets[i].attempts = 0;
packets[i].registration = PJON_MICROS();
packets[i].state = PJON_TO_BE_SENT;
}
}
}
return packets_count;
};
/* Check if the packet index passed is the first to be sent: */
bool first_packet_to_be_sent(uint8_t index) {
PJON_Packet_Info actual_info;
PJON_Packet_Info tested_info;
parse((uint8_t *)packets[index].content, actual_info);