forked from nRF24/RF24Mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF24Mesh.cpp
645 lines (532 loc) · 18.7 KB
/
RF24Mesh.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
#include "RF24Mesh.h"
#include "RF24Mesh_config.h"
#if defined (__linux) && !defined(__ARDUINO_X86__)
#include <fstream>
#endif
RF24Mesh::RF24Mesh( RF24& _radio,RF24Network& _network ): radio(_radio),network(_network){}
/*****************************************************/
bool RF24Mesh::begin(uint8_t channel, rf24_datarate_e data_rate, uint32_t timeout){
//delay(1); // Found problems w/SPIDEV & ncurses. Without this, getch() returns a stream of garbage
radio.begin();
radio_channel = channel;
radio.setChannel(radio_channel);
radio.setDataRate(data_rate);
network.returnSysMsgs = 1;
if(getNodeID()){ //Not master node
mesh_address = MESH_DEFAULT_ADDRESS;
if(!renewAddress(timeout)){
return 0;
}
}else{
#if !defined (RF24_TINY) && !defined(MESH_NOMASTER)
addrList = (addrListStruct*)malloc(2 * sizeof(addrListStruct));
addrListTop = 0;
loadDHCP();
#endif
mesh_address = 0;
network.begin(mesh_address);
}
return 1;
}
/*****************************************************/
uint8_t RF24Mesh::update(){
uint8_t type = network.update();
if(mesh_address == MESH_DEFAULT_ADDRESS){ return type; }
#if !defined (RF24_TINY) && !defined(MESH_NOMASTER)
if(type == NETWORK_REQ_ADDRESS){
doDHCP = 1;
}
if(!getNodeID()){
if( (type == MESH_ADDR_LOOKUP || type == MESH_ID_LOOKUP)) {
RF24NetworkHeader& header = *(RF24NetworkHeader*)network.frame_buffer;
header.to_node = header.from_node;
if(type==MESH_ADDR_LOOKUP){
int16_t returnAddr = getAddress(network.frame_buffer[sizeof(RF24NetworkHeader)]);
network.write(header,&returnAddr,sizeof(returnAddr));
}else{
int16_t returnAddr = getNodeID(network.frame_buffer[sizeof(RF24NetworkHeader)]);
network.write(header,&returnAddr,sizeof(returnAddr));
}
//printf("Returning lookup 0%o to 0%o \n",returnAddr,header.to_node);
//network.write(header,&returnAddr,sizeof(returnAddr));
}else
if(type == MESH_ADDR_RELEASE ){
uint16_t *fromAddr = (uint16_t*)network.frame_buffer;
for(uint8_t i=0; i<addrListTop; i++){
if(addrList[i].address == *fromAddr){
addrList[i].address = 0;
}
}
}
#if !defined (ARDUINO_ARCH_AVR)
else
if(type == MESH_ADDR_CONFIRM ){
RF24NetworkHeader& header = *(RF24NetworkHeader*)network.frame_buffer;
if(header.from_node == lastAddress){
setAddress(lastID,lastAddress);
}
}
#endif
}
#endif
return type;
}
bool RF24Mesh::write(uint16_t to_node, const void* data, uint8_t msg_type, size_t size ){
if(mesh_address == MESH_DEFAULT_ADDRESS){ return 0; }
RF24NetworkHeader header(to_node,msg_type);
return network.write(header,data,size);
}
/*****************************************************/
bool RF24Mesh::write(const void* data, uint8_t msg_type, size_t size, uint8_t nodeID){
if(mesh_address == MESH_DEFAULT_ADDRESS){ return 0; }
int16_t toNode = 0;
int32_t lookupTimeout = millis()+ MESH_LOOKUP_TIMEOUT;
uint32_t retryDelay = 50;
if(nodeID){
while( (toNode=getAddress(nodeID)) < 0 ){
if(millis() > lookupTimeout || toNode == -2){
return 0;
}
retryDelay+=50;
delay(retryDelay);
}
}
return write(toNode,data,msg_type,size);
}
/*****************************************************/
void RF24Mesh::setChannel(uint8_t _channel){
radio_channel = _channel;
radio.setChannel(radio_channel);
radio.startListening();
}
/*****************************************************/
void RF24Mesh::setChild(bool allow){
//Prevent old versions of RF24Network from throwing an error
//Note to remove this ""if defined"" after a few releases from 1.0.1
#if defined FLAG_NO_POLL
network.networkFlags = allow ? network.networkFlags & ~FLAG_NO_POLL : network.networkFlags | FLAG_NO_POLL;
#endif
}
/*****************************************************/
bool RF24Mesh::checkConnection(){
uint8_t count = 3;
bool ok = 0;
while(count-- && mesh_address != MESH_DEFAULT_ADDRESS){
update();
if(radio.rxFifoFull() || (network.networkFlags & 1)){
return 1;
}
RF24NetworkHeader header(00,NETWORK_PING);
ok = network.write(header,0,0);
if(ok){break;}
delay(103);
}
if(!ok){ radio.stopListening(); }
return ok;
}
/*****************************************************/
int16_t RF24Mesh::getAddress(uint8_t nodeID){
//#if defined (ARDUINO_SAM_DUE) || defined (__linux)
#if !defined RF24_TINY && !defined(MESH_NOMASTER)
if(!getNodeID()){ //Master Node
uint16_t address = 0;
for(uint8_t i=0; i<addrListTop; i++){
if(addrList[i].nodeID == nodeID){
address = addrList[i].address;
return address;
}
}
return -1;
}
#endif
if(mesh_address == MESH_DEFAULT_ADDRESS){ return -1; }
if(!nodeID){return 0;}
RF24NetworkHeader header( 00, MESH_ADDR_LOOKUP );
if(network.write(header,&nodeID,sizeof(nodeID)+1) ){
uint32_t timer=millis(), timeout = 150;
while(network.update() != MESH_ADDR_LOOKUP){
if(millis()-timer > timeout){ return -1; }
}
}else{
return -1;
}
int16_t address = 0;
memcpy(&address,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(address));
return address >= 0 ? address: -2;
}
int16_t RF24Mesh::getNodeID(uint16_t address){
if(address == MESH_BLANK_ID){
return _nodeID;
}else
if(address == 0){
return 0;
}
if(!mesh_address){ //Master Node
for(uint8_t i=0; i<addrListTop; i++){
if(addrList[i].address == address){
return addrList[i].nodeID;
}
}
}else{
if(mesh_address == MESH_DEFAULT_ADDRESS){ return -1; }
RF24NetworkHeader header( 00, MESH_ID_LOOKUP );
if(network.write(header,&address,sizeof(address)) ){
uint32_t timer=millis(), timeout = 500;
while(network.update() != MESH_ID_LOOKUP){
if(millis()-timer > timeout){ return -1; }
}
int16_t ID;
memcpy(&ID,&network.frame_buffer[sizeof(RF24NetworkHeader)],sizeof(ID));
return ID;
}
}
return -1;
}
/*****************************************************/
bool RF24Mesh::releaseAddress(){
if(mesh_address == MESH_DEFAULT_ADDRESS){ return 0; }
RF24NetworkHeader header(00,MESH_ADDR_RELEASE);
if(network.write(header,0,0)){
network.begin(MESH_DEFAULT_ADDRESS);
mesh_address=MESH_DEFAULT_ADDRESS;
return 1;
}
return 0;
}
/*****************************************************/
uint16_t RF24Mesh::renewAddress(uint32_t timeout){
if(radio.available()){ return 0; }
uint8_t reqCounter = 0;
uint8_t totalReqs = 0;
radio.stopListening();
network.networkFlags |= 2;
delay(10);
network.begin(MESH_DEFAULT_ADDRESS);
mesh_address = MESH_DEFAULT_ADDRESS;
uint32_t start = millis();
while(!requestAddress(reqCounter)){
if(millis()-start > timeout){ return 0; }
delay(50 + ( (totalReqs+1)*(reqCounter+1)) * 2);
(++reqCounter) = reqCounter%4;
(++totalReqs) = totalReqs%10;
}
network.networkFlags &= ~2;
return mesh_address;
}
/*****************************************************/
bool RF24Mesh::requestAddress(uint8_t level){
RF24NetworkHeader header( 0100, NETWORK_POLL );
//Find another radio, starting with level 0 multicast
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.println(F(" MSH: Poll "));
#endif
network.multicast(header,0,0,level);
uint32_t timr = millis();
#define MESH_MAXPOLLS 4
uint16_t contactNode[MESH_MAXPOLLS];
uint8_t pollCount=0;
while(1){
#if defined (MESH_DEBUG_SERIAL) || defined (MESH_DEBUG_PRINTF)
bool goodSignal = radio.testRPD();
#endif
if(network.update() == NETWORK_POLL){
memcpy(&contactNode[pollCount],&network.frame_buffer[0],sizeof(uint16_t));
++pollCount;
#if defined (MESH_DEBUG_SERIAL) || defined (MESH_DEBUG_PRINTF)
if(goodSignal){
// This response was better than -64dBm
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.println(F(" MSH: Poll > -64dbm "));
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: Poll > -64dbm\n", millis() );
#endif
}else{
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.println(F(" MSH: Poll < -64dbm "));
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: Poll < -64dbm\n", millis() );
#endif
}
#endif
}
if(millis() - timr > 55 || pollCount >= MESH_MAXPOLLS ){
if(!pollCount){
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.print(F(" MSH: No poll from level "));Serial.println(level);
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: No poll from level %d\n", millis(), level);
#endif
return 0;
}else{
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.println(F(" MSH: Poll OK "));
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: Poll OK\n", millis() );
#endif
break;
}
}
}
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.print(F(" MSH: Got poll from level ")); Serial.print(level);
Serial.print(F(" count "));Serial.println(pollCount);
#elif defined MESH_DEBUG_PRINTF
printf("%u MSH: Got poll from level %d count %d\n",millis(),level,pollCount);
#endif
uint8_t type=0;
for(uint8_t i=0; i<pollCount; i++){
// Request an address via the contact node
header.type = NETWORK_REQ_ADDRESS;
header.reserved = getNodeID();
header.to_node = contactNode[i];
// Do a direct write (no ack) to the contact node. Include the nodeId and address.
network.write(header,0,0,contactNode[i]);
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.print(F(" MSH: Req addr from ")); Serial.println(contactNode[i],OCT);
#elif defined MESH_DEBUG_PRINTF
printf("%u MSH: Request address from: 0%o\n",millis(),contactNode[i]);
#endif
timr = millis();
while(millis()-timr<225){
if( (type = network.update()) == NETWORK_ADDR_RESPONSE){
i=pollCount;
break;
}
}
delay(5);
}
if(type != NETWORK_ADDR_RESPONSE){
return 0;
}
//Serial.print("response took");
//Serial.println(millis()-timr);
#ifdef MESH_DEBUG_SERIAL
uint8_t mask = 7; char addrs[5] = " ", count=3; uint16_t newAddr;
#endif
uint8_t registerAddrCount = 0;
uint16_t newAddress=0;
//memcpy(&addrResponse,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(addrResponse));
memcpy(&newAddress,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(newAddress));
if(!newAddress || network.frame_buffer[7] != getNodeID() ){
#ifdef MESH_DEBUG_SERIAL
Serial.print(millis()); Serial.print(F(" MSH: Attempt Failed ")); Serial.println(network.frame_buffer[7]);
Serial.print("My NodeID ");Serial.println(getNodeID());
#elif defined MESH_DEBUG_PRINTF
printf("%u Response discarded, wrong node 0%o from node 0%o sending node 0%o id %d\n",millis(),newAddress,header.from_node,MESH_DEFAULT_ADDRESS,network.frame_buffer[7]);
#endif
return 0;
}
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() );Serial.print(F(" Set address: "));
newAddr = newAddress;
while(newAddr){
addrs[count] = (newAddr & mask)+48; //get the individual Octal numbers, specified in chunks of 3 bits, convert to ASCII by adding 48
newAddr >>= 3;
count--;
}
Serial.println(addrs);
#elif defined (MESH_DEBUG_PRINTF)
printf("Set address 0%o rcvd 0%o\n",mesh_address,newAddress);
#endif
mesh_address = newAddress;
radio.stopListening();
delay(10);
network.begin(mesh_address);
header.to_node = 00;
header.type = MESH_ADDR_CONFIRM;
while( !network.write(header,0,0) ){
if(registerAddrCount++ >= 6 ){
network.begin(MESH_DEFAULT_ADDRESS);
mesh_address = MESH_DEFAULT_ADDRESS;
return 0;
}
delay(3);
}
return 1;
}
/*****************************************************/
/*
bool RF24Mesh::waitForAvailable(uint32_t timeout){
unsigned long timer = millis();
while(millis()-timer < timeout){
network.update();
if(network.available()){ return 1; }
}
if(network.available()){ return 1; }
else{ return 0; }
}
*/
/*****************************************************/
void RF24Mesh::setNodeID(uint8_t nodeID){
_nodeID = nodeID;
}
/*****************************************************/
void RF24Mesh::setStaticAddress(uint8_t nodeID, uint16_t address){
setAddress(nodeID,address);
}
/*****************************************************/
void RF24Mesh::setAddress(uint8_t nodeID, uint16_t address){
uint8_t position = addrListTop;
for(uint8_t i=0; i<addrListTop; i++){
if( addrList[i].nodeID == nodeID){
position = i;
break;
}
}
addrList[position].nodeID = nodeID;
addrList[position].address = address;
if(position == addrListTop){
++addrListTop;
addrList = (addrListStruct*)realloc(addrList,(addrListTop + 1) * sizeof(addrListStruct));
}
#if defined (__linux) && !defined(__ARDUINO_X86__)
//if(millis()-lastFileSave > 300){
// lastFileSave = millis();
saveDHCP();
//}
#endif
}
/*****************************************************/
void RF24Mesh::loadDHCP(){
#if defined (__linux) && !defined(__ARDUINO_X86__)
std::ifstream infile ("dhcplist.txt",std::ifstream::binary);
if(!infile){ return; }
addrList[addrListTop].nodeID = 255;
addrList[addrListTop].address = 01114;
infile.seekg(0,infile.end);
int length = infile.tellg();
infile.seekg(0,infile.beg);
addrList = (addrListStruct*)realloc(addrList,length + sizeof(addrListStruct));
addrListTop = length/sizeof(addrListStruct);
for(int i=0; i<addrListTop; i++){
infile.read( (char*)&addrList[i],sizeof(addrListStruct));
}
infile.close();
#endif
}
/*****************************************************/
void RF24Mesh::saveDHCP(){
#if defined (__linux) && !defined(__ARDUINO_X86__)
std::ofstream outfile ("dhcplist.txt",std::ofstream::binary | std::ofstream::trunc);
//printf("writingToFile %d 0%o size %d\n",addrList[0].nodeID,addrList[0].address,sizeof(addrListStruct));
for(int i=0; i< addrListTop; i++){
outfile.write( (char*)&addrList[i],sizeof(addrListStruct));
}
outfile.close();
/*addrListStruct aList;
std::ifstream infile ("dhcplist.txt",std::ifstream::binary);
infile.seekg(0,infile.end);
int length = infile.tellg();
infile.seekg(0,infile.beg);
//addrList = (addrListStruct*)malloc(length);
//infile.read( (char*)&addrList,length);
infile.read( (char*)&aList,sizeof(addrListStruct));
//addrListTop = length/sizeof(addrListStruct);
//for(int i=0; i< addrListTop; i++){
printf("ID: %d ADDR: 0%o \n",aList.nodeID,aList.address);
//}
infile.close();*/
#endif
}
/*****************************************************/
#if !defined (RF24_TINY) && !defined(MESH_NOMASTER)
void RF24Mesh::DHCP(){
if(doDHCP){
doDHCP = 0;
}else{ return; }
RF24NetworkHeader header;
memcpy(&header,network.frame_buffer,sizeof(RF24NetworkHeader));
uint16_t newAddress;
// Get the unique id of the requester
uint8_t from_id = header.reserved;
if(!from_id){
#ifdef MESH_DEBUG_PRINTF
printf("MSH: Invalid id 0 rcvd\n");
#endif
return;
}
uint16_t fwd_by = 0;
uint8_t shiftVal = 0;
bool extraChild = 0;
if( header.from_node != MESH_DEFAULT_ADDRESS){
fwd_by = header.from_node;
uint16_t m = fwd_by;
uint8_t count = 0;
while(m){ //Octal addresses convert nicely to binary in threes. Address 03 = B011 Address 033 = B011011
m >>= 3; //Find out how many digits are in the octal address
count++;
}
shiftVal = count*3; //Now we know how many bits to shift when adding a child node 1-5 (B001 to B101) to any address
}else{
//If request is coming from level 1, add an extra child to the master
extraChild = 1;
}
#ifdef MESH_DEBUG_PRINTF
// printf("%u MSH: Rcv addr req from_id %d \n",millis(),from_id);
#endif
for(int i=MESH_MAX_CHILDREN+extraChild; i> 0; i--){ // For each of the possible addresses (5 max)
bool found = 0;
newAddress = fwd_by | (i << shiftVal);
if(!newAddress ){ /*printf("dumped 0%o\n",newAddress);*/ continue; }
for(uint8_t i=0; i < addrListTop; i++){
#if defined (MESH_DEBUG_MINIMAL)
#if !defined (__linux) && !defined ARDUINO_SAM_DUE || defined TEENSY || defined(__ARDUINO_X86__)
Serial.print("ID: ");Serial.print(addrList[i].nodeID,DEC);Serial.print(" ADDR: ");
uint16_t newAddr = addrList[i].address;
char addr[5] = " ", count=3, mask=7;
while(newAddr){
addr[count] = (newAddr & mask)+48; //get the individual Octal numbers, specified in chunks of 3 bits, convert to ASCII by adding 48
newAddr >>= 3;
count--;
}
Serial.println(addr);
#else
printf("ID: %d ADDR: 0%o\n", addrList[i].nodeID,addrList[i].address);
#endif
#endif
if( (addrList[i].address == newAddress && addrList[i].nodeID != from_id ) || newAddress == MESH_DEFAULT_ADDRESS){
found = 1;
break;
}
}
if(!found){
header.type = NETWORK_ADDR_RESPONSE;
header.to_node = header.from_node;
//This is a routed request to 00
if(header.from_node != MESH_DEFAULT_ADDRESS){ //Is NOT node 01 to 05
delay(2);
if( network.write(header,&newAddress,sizeof(newAddress)) ){
//addrMap[from_id] = newAddress; //????
}else{
network.write(header,&newAddress,sizeof(newAddress));
}
}else{
delay(2);
network.write(header,&newAddress,sizeof(newAddress),header.to_node);
//addrMap[from_id] = newAddress;
}
uint32_t timer=millis();
lastAddress = newAddress;
lastID = from_id;
while(network.update() != MESH_ADDR_CONFIRM){
if(millis()-timer > network.routeTimeout){
return;
}
}
setAddress(from_id,newAddress);
#ifdef MESH_DEBUG_PRINTF
printf("Sent to 0%o phys: 0%o new: 0%o id: %d\n", header.to_node,MESH_DEFAULT_ADDRESS,newAddress,header.reserved);
#endif
break;
}else{
#if defined (MESH_DEBUG_PRINTF)
printf("not allocated\n");
#endif
}
}
//}else{
//break;
//}
}
/*****************************************************/
#endif