forked from nRF24/RF24Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF24Network.cpp
1342 lines (1089 loc) · 43.2 KB
/
RF24Network.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
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
/*
Copyright (C) 2011 James Coliz, Jr. <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
#include "RF24Network_config.h"
#if defined (RF24_LINUX)
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <iostream>
#include <algorithm>
#include <RF24/RF24.h>
#include "RF24Network.h"
#else
#include "RF24.h"
#include "RF24Network.h"
#endif
#if defined (ENABLE_SLEEP_MODE) && !defined (RF24_LINUX) && !defined (__ARDUINO_X86__)
#include <avr/sleep.h>
#include <avr/power.h>
volatile byte sleep_cycles_remaining;
volatile bool wasInterrupted;
#endif
uint16_t RF24NetworkHeader::next_id = 1;
#if defined ENABLE_NETWORK_STATS
uint32_t RF24Network::nFails = 0;
uint32_t RF24Network::nOK = 0;
#endif
uint64_t pipe_address( uint16_t node, uint8_t pipe );
#if defined (RF24NetworkMulticast)
uint16_t levelToAddress( uint8_t level );
#endif
bool is_valid_address( uint16_t node );
/******************************************************************/
#if defined (RF24_LINUX)
#if !defined (DUAL_HEAD_RADIO)
RF24Network::RF24Network( RF24& _radio ): radio(_radio), frame_size(MAX_FRAME_SIZE)
#else
RF24Network::RF24Network( RF24& _radio, RF24& _radio1 ): radio(_radio), radio1(_radio1),frame_size(MAX_FRAME_SIZE)
#endif
{
}
#elif !defined (DUAL_HEAD_RADIO)
RF24Network::RF24Network( RF24& _radio ): radio(_radio), next_frame(frame_queue)
{
#if !defined ( DISABLE_FRAGMENTATION )
frag_queue.message_buffer=&frag_queue_message_buffer[0];
frag_ptr = &frag_queue;
#endif
}
#else
RF24Network::RF24Network( RF24& _radio, RF24& _radio1 ): radio(_radio), radio1(_radio1), next_frame(frame_queue)
{
#if !defined ( DISABLE_FRAGMENTATION )
frag_queue.message_buffer=&frag_queue_message_buffer[0];
frag_ptr = &frag_queue;
#endif
}
#endif
/******************************************************************/
void RF24Network::begin(uint8_t _channel, uint16_t _node_address )
{
if (! is_valid_address(_node_address) )
return;
node_address = _node_address;
if ( ! radio.isValid() ){
return;
}
// Set up the radio the way we want it to look
if(_channel != USE_CURRENT_CHANNEL){
radio.setChannel(_channel);
}
//radio.enableDynamicAck();
radio.setAutoAck(0,0);
#if defined (ENABLE_DYNAMIC_PAYLOADS)
radio.enableDynamicPayloads();
#endif
// Use different retry periods to reduce data collisions
uint8_t retryVar = (((node_address % 6)+1) *2) + 3;
radio.setRetries(retryVar, 5); // max about 85ms per attempt
txTimeout = 25;
routeTimeout = txTimeout*3; // Adjust for max delay per node within a single chain
#if defined (DUAL_HEAD_RADIO)
radio1.setChannel(_channel);
radio1.enableDynamicAck();
radio1.enableDynamicPayloads();
#endif
// Setup our address helper cache
setup_address();
// Open up all listening pipes
uint8_t i = 6;
while (i--){
radio.openReadingPipe(i,pipe_address(_node_address,i));
}
radio.startListening();
}
/******************************************************************/
#if defined ENABLE_NETWORK_STATS
void RF24Network::failures(uint32_t *_fails, uint32_t *_ok){
*_fails = nFails;
*_ok = nOK;
}
#endif
/******************************************************************/
uint8_t RF24Network::update(void)
{
// if there is data ready
uint8_t pipe_num;
uint8_t returnVal = 0;
// If bypass is enabled, continue although incoming user data may be dropped
// Allows system payloads to be read while user cache is full
// Incoming Hold prevents data from being read from the radio, preventing incoming payloads from being acked
#if !defined (RF24_LINUX)
if(!(networkFlags & FLAG_BYPASS_HOLDS)){
if( (networkFlags & FLAG_HOLD_INCOMING) || (next_frame-frame_queue) + 34 > MAIN_BUFFER_SIZE ){
if(!available()){
networkFlags &= ~FLAG_HOLD_INCOMING;
}else{
return 0;
}
}
}
#endif
while ( radio.isValid() && radio.available(&pipe_num) ){
#if defined (ENABLE_DYNAMIC_PAYLOADS)
if( (frame_size = radio.getDynamicPayloadSize() ) < sizeof(RF24NetworkHeader)){
delay(10);
continue;
}
#else
frame_size=32;
#endif
// Dump the payloads until we've gotten everything
// Fetch the payload, and see if this was the last one.
radio.read( frame_buffer, frame_size );
// Read the beginning of the frame as the header
RF24NetworkHeader *header = (RF24NetworkHeader*)(&frame_buffer);
#if defined (RF24_LINUX)
IF_SERIAL_DEBUG(printf_P("%u: MAC Received on %u %s\n\r",millis(),pipe_num,header->toString()));
if (frame_size) {
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf("%u: FRG Rcv frame size %i\n",millis(),frame_size););
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf("%u: FRG Rcv frame ",millis()); const char* charPtr = reinterpret_cast<const char*>(frame_buffer); for (uint16_t i = 0; i < frame_size; i++) { printf("%02X ", charPtr[i]); }; printf("\n\r"));
}
#else
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Received on %u %s\n\r"),millis(),pipe_num,header->toString()));
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(frame_buffer + sizeof(RF24NetworkHeader));printf_P(PSTR("%lu: NET message %04x\n\r"),millis(),*i));
#endif
// Throw it away if it's not a valid address
if ( !is_valid_address(header->to_node) ){
continue;
}
uint8_t returnVal = header->type;
// Is this for us?
if ( header->to_node == node_address ){
if(header->type == NETWORK_PING){
continue;
}
if(header->type == NETWORK_ADDR_RESPONSE ){
uint16_t requester = 04444;
if(requester != node_address){
header->to_node = requester;
write(header->to_node,USER_TX_TO_PHYSICAL_ADDRESS);
delay(10);
write(header->to_node,USER_TX_TO_PHYSICAL_ADDRESS);
//printf("Fwd add response to 0%o\n",requester);
continue;
}
}
if(header->type == NETWORK_REQ_ADDRESS && node_address){
//printf("Fwd add req to 0\n");
header->from_node = node_address;
header->to_node = 0;
write(header->to_node,TX_NORMAL);
continue;
}
if( (returnSysMsgs && header->type > 127) || header->type == NETWORK_ACK ){
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu MAC: System payload rcvd %d\n"),millis(),returnVal); );
//if( (header->type < 148 || header->type > 150) && header->type != NETWORK_MORE_FRAGMENTS_NACK && header->type != EXTERNAL_DATA_TYPE && header->type!= NETWORK_LAST_FRAGMENT){
if( header->type != NETWORK_FIRST_FRAGMENT && header->type != NETWORK_MORE_FRAGMENTS && header->type != NETWORK_MORE_FRAGMENTS_NACK && header->type != EXTERNAL_DATA_TYPE && header->type!= NETWORK_LAST_FRAGMENT){
return returnVal;
}
}
if( enqueue(header) == 2 ){ //External data received
#if defined (SERIAL_DEBUG_MINIMAL)
printf("ret ext\n");
#endif
return EXTERNAL_DATA_TYPE;
}
}else{
#if defined (RF24NetworkMulticast)
if( header->to_node == 0100){
if(header->type == NETWORK_POLL ){
if( !(networkFlags & FLAG_NO_POLL) && node_address != 04444 ){
header->to_node = header->from_node;
header->from_node = node_address;
delay(parent_pipe);
write(header->to_node,USER_TX_TO_PHYSICAL_ADDRESS);
}
continue;
}
uint8_t val = enqueue(header);
if(multicastRelay){
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%u MAC: FWD multicast frame from 0%o to level %u\n"),millis(),header->from_node,multicast_level+1); );
write(levelToAddress(multicast_level)<<3,4);
}
if( val == 2 ){ //External data received
//Serial.println("ret ext multicast");
return EXTERNAL_DATA_TYPE;
}
}else{
write(header->to_node,1); //Send it on, indicate it is a routed payload
}
#else
write(header->to_node,1); //Send it on, indicate it is a routed payload
#endif
}
}
return returnVal;
}
#if defined (RF24_LINUX)
/******************************************************************/
uint8_t RF24Network::enqueue(RF24NetworkHeader* header) {
uint8_t result = false;
RF24NetworkFrame frame = RF24NetworkFrame(*header,frame_buffer+sizeof(RF24NetworkHeader),frame_size-sizeof(RF24NetworkHeader));
bool isFragment = ( frame.header.type == NETWORK_FIRST_FRAGMENT || frame.header.type == NETWORK_MORE_FRAGMENTS || frame.header.type == NETWORK_LAST_FRAGMENT || frame.header.type == NETWORK_MORE_FRAGMENTS_NACK);
// This is sent to itself
if (frame.header.from_node == node_address) {
if (isFragment) {
printf("Cannot enqueue multi-payload frames to self\n");
result = false;
}else{
frame_queue.push(frame);
result = true;
}
}else
if (isFragment)
{
//The received frame contains the a fragmented payload
//Set the more fragments flag to indicate a fragmented frame
IF_SERIAL_DEBUG_FRAGMENTATION_L2(printf("%u: FRG Payload type %d of size %i Bytes with fragmentID '%i' received.\n\r",millis(),frame.header.type,frame.message_size,frame.header.reserved););
//Append payload
result = appendFragmentToFrame(frame);
//The header.reserved contains the actual header.type on the last fragment
if ( result && frame.header.type == NETWORK_LAST_FRAGMENT) {
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%u: FRG Last fragment received. \n",millis() ););
IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET Enqueue assembled frame @%x "),millis(),frame_queue.size()));
RF24NetworkFrame *f = &(frameFragmentsCache[ frame.header.from_node ] );
result=f->header.type == EXTERNAL_DATA_TYPE ? 2 : 1;
//Load external payloads into a separate queue on linux
if(result == 2){
external_queue.push( frameFragmentsCache[ frame.header.from_node ] );
}else{
frame_queue.push( frameFragmentsCache[ frame.header.from_node ] );
}
frameFragmentsCache.erase( frame.header.from_node );
}
}else{// if (frame.header.type <= MAX_USER_DEFINED_HEADER_TYPE) {
//This is not a fragmented payload but a whole frame.
IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET Enqueue @%x "),millis(),frame_queue.size()));
// Copy the current frame into the frame queue
result=frame.header.type == EXTERNAL_DATA_TYPE ? 2 : 1;
//Load external payloads into a separate queue on linux
if(result == 2){
external_queue.push( frame );
}else{
frame_queue.push( frame );
}
}/* else {
//Undefined/Unknown header.type received. Drop frame!
IF_SERIAL_DEBUG_MINIMAL( printf("%u: FRG Received unknown or system header type %d with fragment id %d\n",millis(),frame.header.type, frame.header.reserved); );
//The frame is not explicitly dropped, but the given object is ignored.
//FIXME: does this causes problems with memory management?
}*/
if (result) {
//IF_SERIAL_DEBUG(printf("ok\n\r"));
} else {
IF_SERIAL_DEBUG(printf("failed\n\r"));
}
return result;
}
/******************************************************************/
bool RF24Network::appendFragmentToFrame(RF24NetworkFrame frame) {
// This is the first of 2 or more fragments.
if (frame.header.type == NETWORK_FIRST_FRAGMENT){
if( frameFragmentsCache.count(frame.header.from_node) != 0 ){
RF24NetworkFrame *f = &(frameFragmentsCache[ frame.header.from_node ]);
//Already rcvd first frag
if (f->header.id == frame.header.id){
return false;
}
}
if(frame.header.reserved > (MAX_PAYLOAD_SIZE /24) + 1 ){
IF_SERIAL_DEBUG_FRAGMENTATION( printf("%u FRG Too many fragments in payload %u, dropping...",millis(),frame.header.reserved); );
// If there are more fragments than we can possibly handle, return
return false;
}
frameFragmentsCache[ frame.header.from_node ] = frame;
return true;
}else
if ( frame.header.type == NETWORK_MORE_FRAGMENTS || frame.header.type == NETWORK_MORE_FRAGMENTS_NACK ){
if( frameFragmentsCache.count(frame.header.from_node) < 1 ){
return false;
}
RF24NetworkFrame *f = &(frameFragmentsCache[ frame.header.from_node ]);
if( f->header.reserved - 1 == frame.header.reserved && f->header.id == frame.header.id){
// Cache the fragment
memcpy(f->message_buffer+f->message_size, frame.message_buffer, frame.message_size);
f->message_size += frame.message_size; //Increment message size
f->header = frame.header; //Update header
return true;
} else {
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%u: FRG Dropping fragment for frame with header id:%d, out of order fragment(s).\n",millis(),frame.header.id););
return false;
}
}else
if ( frame.header.type == NETWORK_LAST_FRAGMENT ){
//We have received the last fragment
if(frameFragmentsCache.count(frame.header.from_node) < 1){
return false;
}
//Create pointer to the cached frame
RF24NetworkFrame *f = &(frameFragmentsCache[ frame.header.from_node ]);
if( f->message_size + frame.message_size > MAX_PAYLOAD_SIZE){
IF_SERIAL_DEBUG_FRAGMENTATION( printf("%u FRG Frame of size %u plus enqueued frame of size %u exceeds max payload size \n",millis(),frame.message_size,f->message_size); );
return false;
}
//Error checking for missed fragments and payload size
if ( f->header.reserved-1 != 1 || f->header.id != frame.header.id) {
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%u: FRG Duplicate or out of sequence frame %d, expected %d. Cleared.\n",millis(),frame.header.reserved,f->header.reserved););
//frameFragmentsCache.erase( std::make_pair(frame.header.id,frame.header.from_node) );
return false;
}
//The user specified header.type is sent with the last fragment in the reserved field
frame.header.type = frame.header.reserved;
frame.header.reserved = 1;
//Append the received fragment to the cached frame
memcpy(f->message_buffer+f->message_size, frame.message_buffer, frame.message_size);
f->message_size += frame.message_size; //Increment message size
f->header = frame.header; //Update header
return true;
}
return false;
}
/******************************************************************/
/******************************************************************/
#else // Not defined RF24_Linux:
/******************************************************************/
/******************************************************************/
uint8_t RF24Network::enqueue(RF24NetworkHeader* header)
{
bool result = false;
uint16_t message_size = frame_size - sizeof(RF24NetworkHeader);
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Enqueue @%x "),millis(),next_frame-frame_queue));
#if !defined ( DISABLE_FRAGMENTATION )
bool isFragment = header->type == NETWORK_FIRST_FRAGMENT || header->type == NETWORK_MORE_FRAGMENTS || header->type == NETWORK_LAST_FRAGMENT || header->type == NETWORK_MORE_FRAGMENTS_NACK ;
if(isFragment){
if(header->type == NETWORK_FIRST_FRAGMENT){
// Drop frames exceeding max size and duplicates (MAX_PAYLOAD_SIZE needs to be divisible by 24)
if(header->reserved > (MAX_PAYLOAD_SIZE / max_frame_payload_size) ){
#if defined (SERIAL_DEBUG_FRAGMENTATION) || defined (SERIAL_DEBUG_MINIMAL)
printf_P(PSTR("Frag frame with %d frags exceeds MAX_PAYLOAD_SIZE or out of sequence\n"),header->reserved);
#endif
frag_queue.header.reserved = 0;
return false;
}else
if(frag_queue.header.id == header->id && frag_queue.header.from_node == header->from_node){
return true;
}
if( (header->reserved * 24) > (MAX_PAYLOAD_SIZE - (next_frame-frame_queue)) ){
networkFlags |= FLAG_HOLD_INCOMING;
radio.stopListening();
}
memcpy(&frag_queue,&frame_buffer,8);
memcpy(frag_queue.message_buffer,frame_buffer+sizeof(RF24NetworkHeader),message_size);
//IF_SERIAL_DEBUG_FRAGMENTATION( Serial.print(F("queue first, total frags ")); Serial.println(header->reserved); );
//Store the total size of the stored frame in message_size
frag_queue.message_size = message_size;
--frag_queue.header.reserved;
IF_SERIAL_DEBUG_FRAGMENTATION_L2( for(int i=0; i<frag_queue.message_size;i++){ Serial.println(frag_queue.message_buffer[i],HEX); } );
return true;
}else // NETWORK_MORE_FRAGMENTS
if(header->type == NETWORK_LAST_FRAGMENT || header->type == NETWORK_MORE_FRAGMENTS || header->type == NETWORK_MORE_FRAGMENTS_NACK){
if(frag_queue.message_size + message_size > MAX_PAYLOAD_SIZE){
#if defined (SERIAL_DEBUG_FRAGMENTATION) || defined (SERIAL_DEBUG_MINIMAL)
Serial.print(F("Drop frag ")); Serial.print(header->reserved);
Serial.println(F(" Size exceeds max"));
#endif
frag_queue.header.reserved=0;
return false;
}
if( frag_queue.header.reserved == 0 || (header->type != NETWORK_LAST_FRAGMENT && header->reserved != frag_queue.header.reserved ) || frag_queue.header.id != header->id ){
#if defined (SERIAL_DEBUG_FRAGMENTATION) || defined (SERIAL_DEBUG_MINIMAL)
Serial.print(F("Drop frag ")); Serial.print(header->reserved);
//Serial.print(F(" header id ")); Serial.print(header->id);
Serial.println(F(" Out of order "));
#endif
return false;
}
memcpy(frag_queue.message_buffer+frag_queue.message_size,frame_buffer+sizeof(RF24NetworkHeader),message_size);
frag_queue.message_size += message_size;
if(header->type != NETWORK_LAST_FRAGMENT){
--frag_queue.header.reserved;
return true;
}
frag_queue.header.reserved = 0;
frag_queue.header.type = header->reserved;
IF_SERIAL_DEBUG_FRAGMENTATION( printf_P(PSTR("fq 3: %d\n"),frag_queue.message_size); );
IF_SERIAL_DEBUG_FRAGMENTATION_L2(for(int i=0; i< frag_queue.message_size;i++){ Serial.println(frag_queue.message_buffer[i],HEX); } );
//Frame assembly complete, copy to main buffer if OK
if(frag_queue.header.type == EXTERNAL_DATA_TYPE){
return 2;
}
#if defined (DISABLE_USER_PAYLOADS)
return 0;
#endif
if(MAX_PAYLOAD_SIZE - (next_frame-frame_queue) >= frag_queue.message_size){
memcpy(next_frame,&frag_queue,10);
memcpy(next_frame+10,frag_queue.message_buffer,frag_queue.message_size);
next_frame += (10+frag_queue.message_size);
#if !defined(ARDUINO_ARCH_AVR)
if(uint8_t padding = (frag_queue.message_size+10)%4){
next_frame += 4 - padding;
}
#endif
IF_SERIAL_DEBUG_FRAGMENTATION( printf_P(PSTR("enq size %d\n"),frag_queue.message_size); );
return true;
}else{
radio.stopListening();
networkFlags |= FLAG_HOLD_INCOMING;
}
IF_SERIAL_DEBUG_FRAGMENTATION( printf_P(PSTR("Drop frag payload, queue full\n")); );
return false;
}//If more or last fragments
}else //else is not a fragment
#endif // End fragmentation enabled
// Copy the current frame into the frame queue
#if !defined( DISABLE_FRAGMENTATION )
if(header->type == EXTERNAL_DATA_TYPE){
memcpy(&frag_queue,&frame_buffer,8);
frag_queue.message_buffer = frame_buffer+sizeof(RF24NetworkHeader);
frag_queue.message_size = message_size;
return 2;
}
#endif
#if defined (DISABLE_USER_PAYLOADS)
return 0;
}
#else
if(message_size + (next_frame-frame_queue) <= MAIN_BUFFER_SIZE){
memcpy(next_frame,&frame_buffer,8);
memcpy(next_frame+8,&message_size,2);
memcpy(next_frame+10,frame_buffer+8,message_size);
//IF_SERIAL_DEBUG_FRAGMENTATION( for(int i=0; i<message_size;i++){ Serial.print(next_frame[i],HEX); Serial.print(" : "); } Serial.println(""); );
next_frame += (message_size + 10);
#if !defined(ARDUINO_ARCH_AVR)
if(uint8_t padding = (message_size+10)%4){
next_frame += 4 - padding;
}
#endif
//IF_SERIAL_DEBUG_FRAGMENTATION( Serial.print("Enq "); Serial.println(next_frame-frame_queue); );//printf_P(PSTR("enq %d\n"),next_frame-frame_queue); );
result = true;
}else{
result = false;
IF_SERIAL_DEBUG(printf_P(PSTR("NET **Drop Payload** Buffer Full")));
}
return result;
}
#endif //USER_PAYLOADS_ENABLED
#endif //End not defined RF24_Linux
/******************************************************************/
bool RF24Network::available(void)
{
#if defined (RF24_LINUX)
return (!frame_queue.empty());
#else
// Are there frames on the queue for us?
return (next_frame > frame_queue);
#endif
}
/******************************************************************/
uint16_t RF24Network::parent() const
{
if ( node_address == 0 )
return -1;
else
return parent_node;
}
/******************************************************************/
/*uint8_t RF24Network::peekData(){
return frame_queue[0];
}*/
uint16_t RF24Network::peek(RF24NetworkHeader& header)
{
if ( available() )
{
#if defined (RF24_LINUX)
RF24NetworkFrame frame = frame_queue.front();
memcpy(&header,&frame.header,sizeof(RF24NetworkHeader));
return frame.message_size;
#else
RF24NetworkFrame *frame = (RF24NetworkFrame*)(frame_queue);
memcpy(&header,&frame->header,sizeof(RF24NetworkHeader));
uint16_t msg_size;
memcpy(&msg_size,frame+8,2);
return msg_size;
#endif
}
return 0;
}
/******************************************************************/
uint16_t RF24Network::read(RF24NetworkHeader& header,void* message, uint16_t maxlen)
{
uint16_t bufsize = 0;
#if defined (RF24_LINUX)
if ( available() ) {
RF24NetworkFrame frame = frame_queue.front();
// How much buffer size should we actually copy?
bufsize = rf24_min(frame.message_size,maxlen);
memcpy(&header,&(frame.header),sizeof(RF24NetworkHeader));
memcpy(message,frame.message_buffer,bufsize);
IF_SERIAL_DEBUG(printf("%u: FRG message size %i\n",millis(),frame.message_size););
IF_SERIAL_DEBUG(printf("%u: FRG message ",millis()); const char* charPtr = reinterpret_cast<const char*>(message); for (uint16_t i = 0; i < bufsize; i++) { printf("%02X ", charPtr[i]); }; printf("\n\r"));
IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET read %s\n\r"),millis(),header.toString()));
frame_queue.pop();
}
#else
if ( available() )
{
memcpy(&header,frame_queue,8);
memcpy(&bufsize,frame_queue+8,2);
if (maxlen > 0)
{
maxlen = rf24_min(maxlen,bufsize);
memcpy(message,frame_queue+10,maxlen);
IF_SERIAL_DEBUG(printf("%lu: NET message size %d\n",millis(),bufsize););
IF_SERIAL_DEBUG( uint16_t len = maxlen; printf_P(PSTR("%lu: NET r message "),millis());const uint8_t* charPtr = reinterpret_cast<const uint8_t*>(message);while(len--){ printf("%02x ",charPtr[len]);} printf_P(PSTR("\n\r") ) );
}
memmove(frame_queue,frame_queue+bufsize+10,sizeof(frame_queue)- bufsize);
next_frame-=bufsize+10;
#if !defined(ARDUINO_ARCH_AVR)
if(uint8_t padding = (bufsize+10)%4){
next_frame -= 4 - padding;
}
#endif
//IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Received %s\n\r"),millis(),header.toString()));
}
#endif
return bufsize;
}
#if defined RF24NetworkMulticast
/******************************************************************/
bool RF24Network::multicast(RF24NetworkHeader& header,const void* message, uint16_t len, uint8_t level){
// Fill out the header
header.to_node = 0100;
header.from_node = node_address;
return write(header, message, len, levelToAddress(level));
}
#endif
/******************************************************************/
bool RF24Network::write(RF24NetworkHeader& header,const void* message, uint16_t len){
return write(header,message,len,070);
}
/******************************************************************/
bool RF24Network::write(RF24NetworkHeader& header,const void* message, uint16_t len, uint16_t writeDirect){
//Allows time for requests (RF24Mesh) to get through between failed writes on busy nodes
while(millis()-txTime < 25){ if(update() > 127){break;} }
delayMicroseconds(200);
#if defined (DISABLE_FRAGMENTATION)
frame_size = rf24_min(len+sizeof(RF24NetworkHeader),MAX_FRAME_SIZE);
return _write(header,message,rf24_min(len,max_frame_payload_size),writeDirect);
#else
if(len <= max_frame_payload_size){
//Normal Write (Un-Fragmented)
frame_size = len + sizeof(RF24NetworkHeader);
if(_write(header,message,len,writeDirect)){
return 1;
}
txTime = millis();
return 0;
}
//Check payload size
if (len > MAX_PAYLOAD_SIZE) {
IF_SERIAL_DEBUG(printf("%u: NET write message failed. Given 'len' %d is bigger than the MAX Payload size %i\n\r",millis(),len,MAX_PAYLOAD_SIZE););
return false;
}
//Divide the message payload into chunks of max_frame_payload_size
uint8_t fragment_id = (len % max_frame_payload_size != 0) + ((len ) / max_frame_payload_size); //the number of fragments to send = ceil(len/max_frame_payload_size)
uint8_t msgCount = 0;
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%lu: FRG Total message fragments %d\n\r",millis(),fragment_id););
if(header.to_node != 0100){
networkFlags |= FLAG_FAST_FRAG;
#if !defined (DUAL_HEAD_RADIO)
radio.stopListening();
#endif
}
uint8_t retriesPerFrag = 0;
uint8_t type = header.type;
bool ok = 0;
while (fragment_id > 0) {
//Copy and fill out the header
//RF24NetworkHeader fragmentHeader = header;
header.reserved = fragment_id;
if (fragment_id == 1) {
header.type = NETWORK_LAST_FRAGMENT; //Set the last fragment flag to indicate the last fragment
header.reserved = type; //The reserved field is used to transmit the header type
} else {
if (msgCount == 0) {
header.type = NETWORK_FIRST_FRAGMENT;
}else{
header.type = NETWORK_MORE_FRAGMENTS; //Set the more fragments flag to indicate a fragmented frame
}
}
uint16_t offset = msgCount*max_frame_payload_size;
uint16_t fragmentLen = rf24_min((uint16_t)(len-offset),max_frame_payload_size);
//Try to send the payload chunk with the copied header
frame_size = sizeof(RF24NetworkHeader)+fragmentLen;
ok = _write(header,((char *)message)+offset,fragmentLen,writeDirect);
if (!ok) {
delay(2);
++retriesPerFrag;
}else{
retriesPerFrag = 0;
fragment_id--;
msgCount++;
}
//if(writeDirect != 070){ delay(2); } //Delay 2ms between sending multicast payloads
if (!ok && retriesPerFrag >= 3) {
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%lu: FRG TX with fragmentID '%d' failed after %d fragments. Abort.\n\r",millis(),fragment_id,msgCount););
break;
}
//Message was successful sent
#if defined SERIAL_DEBUG_FRAGMENTATION_L2
printf("%lu: FRG message transmission with fragmentID '%d' sucessfull.\n\r",millis(),fragment_id);
#endif
}
header.type = type;
#if !defined (DUAL_HEAD_RADIO)
if(networkFlags & FLAG_FAST_FRAG){
ok = radio.txStandBy(txTimeout);
radio.startListening();
radio.setAutoAck(0,0);
}
networkFlags &= ~FLAG_FAST_FRAG;
if(!ok){
return false;
}
#endif
//int frag_delay = uint8_t(len/48);
//delay( rf24_min(len/48,20));
//Return true if all the chunks where sent successfully
IF_SERIAL_DEBUG_FRAGMENTATION(printf("%u: FRG total message fragments sent %i. \n",millis(),msgCount); );
if(fragment_id > 0){
txTime = millis();
return false;
}
return true;
#endif //Fragmentation enabled
}
/******************************************************************/
bool RF24Network::_write(RF24NetworkHeader& header,const void* message, uint16_t len, uint16_t writeDirect)
{
// Fill out the header
header.from_node = node_address;
// Build the full frame to send
memcpy(frame_buffer,&header,sizeof(RF24NetworkHeader));
#if defined (RF24_LINUX)
IF_SERIAL_DEBUG(printf_P(PSTR("%u: NET Sending %s\n\r"),millis(),header.toString()));
#else
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Sending %s\n\r"),millis(),header.toString()));
#endif
if (len){
#if defined (RF24_LINUX)
memcpy(frame_buffer + sizeof(RF24NetworkHeader),message,rf24_min(frame_size-sizeof(RF24NetworkHeader),len));
IF_SERIAL_DEBUG(printf("%u: FRG frame size %i\n",millis(),frame_size););
IF_SERIAL_DEBUG(printf("%u: FRG frame ",millis()); const char* charPtr = reinterpret_cast<const char*>(frame_buffer); for (uint16_t i = 0; i < frame_size; i++) { printf("%02X ", charPtr[i]); }; printf("\n\r"));
#else
memcpy(frame_buffer + sizeof(RF24NetworkHeader),message,len);
IF_SERIAL_DEBUG(uint16_t tmpLen = len;printf_P(PSTR("%lu: NET message "),millis());const uint8_t* charPtr = reinterpret_cast<const uint8_t*>(message);while(tmpLen--){ printf("%02x ",charPtr[tmpLen]);} printf_P(PSTR("\n\r") ) );
#endif
}
// If the user is trying to send it to himself
/*if ( header.to_node == node_address ){
#if defined (RF24_LINUX)
RF24NetworkFrame frame = RF24NetworkFrame(header,message,rf24_min(MAX_FRAME_SIZE-sizeof(RF24NetworkHeader),len));
#else
RF24NetworkFrame frame(header,len);
#endif
// Just queue it in the received queue
return enqueue(frame);
}*/
// Otherwise send it out over the air
if(writeDirect != 070){
uint8_t sendType = USER_TX_TO_LOGICAL_ADDRESS; // Payload is multicast to the first node, and routed normally to the next
if(header.to_node == 0100){
sendType = USER_TX_MULTICAST;
}
if(header.to_node == writeDirect){
sendType = USER_TX_TO_PHYSICAL_ADDRESS; // Payload is multicast to the first node, which is the recipient
}
return write(writeDirect,sendType);
}
return write(header.to_node,TX_NORMAL);
}
/******************************************************************/
bool RF24Network::write(uint16_t to_node, uint8_t directTo) // Direct To: 0 = First Payload, standard routing, 1=routed payload, 2=directRoute to host, 3=directRoute to Route
{
bool ok = false;
bool isAckType = false;
if(frame_buffer[6] > 64 && frame_buffer[6] < 192 ){ isAckType=true; }
/*if( ( (frame_buffer[7] % 2) && frame_buffer[6] == NETWORK_MORE_FRAGMENTS) ){
isAckType = 0;
}*/
// Throw it away if it's not a valid address
if ( !is_valid_address(to_node) )
return false;
//Load info into our conversion structure, and get the converted address info
logicalToPhysicalStruct conversion = { to_node,directTo,0};
logicalToPhysicalAddress(&conversion);
#if defined (RF24_LINUX)
IF_SERIAL_DEBUG(printf_P(PSTR("%u: MAC Sending to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe));
#else
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Sending to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe));
#endif
/**Write it*/
ok=write_to_pipe(conversion.send_node, conversion.send_pipe, conversion.multicast);
if(!ok){
#if defined (RF24_LINUX)
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%u: MAC Send fail to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe););
}
#else
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu: MAC Send fail to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe););
}
#endif
if( directTo == TX_ROUTED && ok && conversion.send_node == to_node && isAckType){
RF24NetworkHeader* header = (RF24NetworkHeader*)&frame_buffer;
header->type = NETWORK_ACK; // Set the payload type to NETWORK_ACK
header->to_node = header->from_node; // Change the 'to' address to the 'from' address
conversion.send_node = header->from_node;
conversion.send_pipe = TX_ROUTED;
conversion.multicast = 0;
logicalToPhysicalAddress(&conversion);
//Write the data using the resulting physical address
frame_size = sizeof(RF24NetworkHeader);
write_to_pipe(conversion.send_node, conversion.send_pipe, conversion.multicast);
//dynLen=0;
#if defined (RF24_LINUX)
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%u MAC: Route OK to 0%o ACK sent to 0%o\n"),millis(),to_node,header->from_node); );
#else
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu MAC: Route OK to 0%o ACK sent to 0%o\n"),millis(),to_node,header->from_node); );
#endif
}
if( ok && conversion.send_node != to_node && (directTo==0 || directTo==3) && isAckType){
#if !defined (DUAL_HEAD_RADIO)
// Now, continue listening
if(networkFlags & FLAG_FAST_FRAG){
radio.txStandBy(txTimeout);
networkFlags &= ~FLAG_FAST_FRAG;
radio.setAutoAck(0,0);
}
radio.startListening();
#endif
uint32_t reply_time = millis();
while( update() != NETWORK_ACK){
#if defined (RF24_LINUX)
delayMicroseconds(900);
#endif
if(millis() - reply_time > routeTimeout){
#if defined (RF24_LINUX)
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%u: MAC Network ACK fail from 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe); );
#else
IF_SERIAL_DEBUG_ROUTING( printf_P(PSTR("%lu: MAC Network ACK fail from 0%o via 0%o on pipe %x\n\r"),millis(),to_node,conversion.send_node,conversion.send_pipe); );
#endif
ok=false;
break;
}
}
}
if( !(networkFlags & FLAG_FAST_FRAG) ){
#if !defined (DUAL_HEAD_RADIO)
// Now, continue listening
radio.startListening();
#endif
}
#if defined ENABLE_NETWORK_STATS
if(ok == true){
++nOK;
}else{ ++nFails;
}
#endif
return ok;
}
/******************************************************************/
// Provided the to_node and directTo option, it will return the resulting node and pipe
bool RF24Network::logicalToPhysicalAddress(logicalToPhysicalStruct *conversionInfo){
//Create pointers so this makes sense.. kind of
//We take in the to_node(logical) now, at the end of the function, output the send_node(physical) address, etc.
//back to the original memory address that held the logical information.
uint16_t *to_node = &conversionInfo->send_node;
uint8_t *directTo = &conversionInfo->send_pipe;
bool *multicast = &conversionInfo->multicast;
// Where do we send this? By default, to our parent
uint16_t pre_conversion_send_node = parent_node;
// On which pipe
uint8_t pre_conversion_send_pipe = parent_pipe;
if(*directTo > TX_ROUTED ){
pre_conversion_send_node = *to_node;
*multicast = 1;
//if(*directTo == USER_TX_MULTICAST || *directTo == USER_TX_TO_PHYSICAL_ADDRESS){
pre_conversion_send_pipe=0;
//}
}
// If the node is a direct child,
else
if ( is_direct_child(*to_node) )
{
// Send directly