forked from nRF24/RF24Mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRF24Mesh.cpp
More file actions
601 lines (491 loc) · 16.9 KB
/
Copy pathRF24Mesh.cpp
File metadata and controls
601 lines (491 loc) · 16.9 KB
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
#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),radio_channel(MESH_DEFAULT_CHANNEL){}
/*****************************************************/
void RF24Mesh::begin(){
radio.begin();
if(getNodeID()){ //Not master node
mesh_address = MESH_DEFAULT_ADDRESS;
}else{
#if !defined (RF24_TINY)
addrList = (addrListStruct*)malloc(2 * sizeof(addrListStruct));
loadDHCP();
#endif
mesh_address = 0;
}
network.begin(radio_channel,mesh_address);
network.returnSysMsgs = 1;
if(getNodeID()){ //Not master node
renewAddress();
}
}
/*****************************************************/
uint8_t RF24Mesh::update(){
uint8_t type = network.update();
#if !defined (RF24_TINY)
if(type == NETWORK_REQ_ADDRESS){
doDHCP = 1;
}
if(type == MESH_ADDR_LOOKUP && !getNodeID()) {
uint8_t nodeID;
RF24NetworkHeader& header = *(RF24NetworkHeader*)network.frame_buffer;
header.to_node = header.from_node;
uint16_t returnAddr = getAddress(network.frame_buffer[sizeof(RF24NetworkHeader)]);
//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 && !getNodeID() ){
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;
}
}
}
#endif
return type;
}
bool RF24Mesh::write(uint16_t to_node, const void* data, uint8_t msg_type, size_t size ){
RF24NetworkHeader header(to_node,msg_type);
uint32_t writeTimer = millis();
while ( !network.write(header,data,size) ){
uint32_t delayTimer = millis();
while(millis() - delayTimer < 50){
network.update();
delay(1);
}
if(millis()-writeTimer > MESH_WRITE_TIMEOUT){
return 0;
}
}
return 1;
}
/*****************************************************/
bool RF24Mesh::write(const void* data, uint8_t msg_type, size_t size ){
return write(00,data,msg_type,size);
}
/*****************************************************/
void RF24Mesh::setChannel(uint8_t _channel){
radio_channel = _channel;
}
/*****************************************************/
bool RF24Mesh::checkConnection(){
RF24NetworkHeader header(00,NETWORK_PING);
uint8_t count = 3;
bool ok;
while(count--){
ok = network.write(header,0,0);
delay(153);
if(ok){break;}
}
return ok;
}
/*****************************************************/
uint16_t RF24Mesh::getAddress(uint8_t nodeID){
//#if defined (ARDUINO_SAM_DUE) || defined (__linux)
#if !defined RF24_TINY
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 0;
#endif
RF24NetworkHeader header( 00, MESH_ADDR_LOOKUP );
if(network.write(header,&nodeID,sizeof(nodeID)+1) ){
uint32_t timer=millis(), timeout = 500;
while(network.update() != MESH_ADDR_LOOKUP){
if(millis()-timer > timeout){ return 0; }
}
}
uint16_t address;
memcpy(&address,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(address));
return address;
}
uint8_t RF24Mesh::getNodeId(uint16_t address){
if(!getNodeID()){ //Master Node
for(uint8_t i=0; i<addrListTop; i++){
if(addrList[i].address == address){
return addrList[i].nodeID;
}
}
}
return 0;
}
/*****************************************************/
bool RF24Mesh::releaseAddress(){
RF24NetworkHeader header(00,MESH_ADDR_RELEASE);
return network.write(header,0,0);
}
/*****************************************************/
uint16_t RF24Mesh::renewAddress(){
static const uint16_t requestDelay = 150;
uint8_t reqCounter = 0;
network.begin(radio_channel,MESH_DEFAULT_ADDRESS);
mesh_address = MESH_DEFAULT_ADDRESS;
while(!requestAddress(reqCounter)){
delay(requestDelay+(mesh_address%(7)*8));
(++reqCounter) = reqCounter%4;
}
return mesh_address;
}
/*****************************************************/
bool RF24Mesh::requestAddress(uint8_t level){
RF24NetworkHeader header( 0100, NETWORK_POLL );
struct addrResponseStruct{
uint16_t requester;
uint16_t new_address;
}addrResponse;
//Find another radio, starting with level 0 multicast
network.multicast(header,0,0,level);
// Wait for a response
/*uint32_t timr = millis();
while( network.update() != NETWORK_POLL){
if(millis() - timr > 150UL){
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.print(F(" MSH: No poll response from level "));Serial.println(level);
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: No poll response from level %d\n", millis(), level);
#endif
return 0;
}
}*/
uint32_t timr = millis();
uint16_t *contactNode = 0;
bool timeout = 0;
while(!timeout){
bool goodSignal = radio.testRPD();
if(network.update() == NETWORK_POLL){
contactNode = (uint16_t*)(&network.frame_buffer);
if(goodSignal){
// This response was better than -64dBm
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.println(F(" MSH: Poll response > -64dbm "));
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: Poll response > -64dbm\n", millis() );
#endif
break;
}
}
if(millis() - timr > 25 ){
if(!contactNode){
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.print(F(" MSH: No poll response from level "));Serial.println(level);
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: No poll response from level %d\n", millis(), level);
#endif
return 0;
}else{
#if defined (MESH_DEBUG_SERIAL)
Serial.print( millis() ); Serial.println(F(" MSH: Poll response > -64dbm "));
#elif defined (MESH_DEBUG_PRINTF)
printf( "%u MSH: Poll response > -64dbm\n", millis() );
#endif
break;
}
}
}
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.print(F(" MSH: Got poll from level ")); Serial.println(level);
#elif defined MESH_DEBUG_PRINTF
printf("%u MSH: Got poll from level %d\n",millis(),level);
#endif
// Request an address via the contact node
header.type = NETWORK_REQ_ADDRESS;
header.reserved = getNodeID();
header.to_node = *contactNode;
// Do a direct write (no ack) to the contact node. Include the nodeId and address.
network.write(header,&mesh_address,sizeof(addrResponse),*contactNode);
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.println(F(" MSH: Request address "));
#elif defined MESH_DEBUG_PRINTF
printf("%u MSH: Request address this node: 0%o\n",millis(),mesh_address);
#endif
timr = millis();
while(network.update() != NETWORK_ADDR_RESPONSE){
if(millis() - timr > 50){
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.print(F(" MSH: No address response from level ")); Serial.println( level );
#elif defined MESH_DEBUG_PRINTF
printf("%u MSH: No address response from level %d\n",millis(),level );
#endif
return 0;
}
}
//Serial.print("response took");
//Serial.println(millis()-timr);
#ifdef MESH_DEBUG_SERIAL
uint8_t mask = 7; char addr[5] = " ", count=3; uint16_t newAddr;
#endif
uint8_t registerAddrCount = 0;
memcpy(&addrResponse,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(addrResponse));
if(!addrResponse.new_address || network.frame_buffer[7] != getNodeID() ){
#ifdef MESH_DEBUG_SERIAL
Serial.print(millis()); Serial.println(F(" MSH: Response discarded, wrong node"));
#elif defined MESH_DEBUG_PRINTF
printf("%u Response discarded, wrong node 0%o from node 0%o sending node 0%o\n",millis(),addrResponse.new_address,header.from_node,addrResponse.requester);
#endif
return 0;
}
#ifdef MESH_DEBUG_SERIAL
Serial.print(F("Set address: "));
newAddr = addrResponse.new_address;
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);
#elif defined (MESH_DEBUG_PRINTF)
printf("Set address 0%o rcvd 0%o\n",mesh_address,addrResponse.new_address);
#endif
mesh_address = addrResponse.new_address;
//radio.begin();
network.begin(radio_channel,mesh_address);
header.to_node = 00;
header.type = MESH_ADDR_CONFIRM;
//network.write(header,0,0);
//delay(55);
while( !network.write(header,0,0) ){
//delay(55);
//printf("Retry register address...\n");
if(registerAddrCount++ >= 6 ){ return 0; }
}
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; }
}
*/
/*****************************************************/
#if (defined (__AVR__) || defined(__ARDUINO_X86__)) && !defined (RF24_TINY)
uint8_t RF24Mesh::getNodeID(){
if(EEPROM.read(509) == 'R' && EEPROM.read(510) == 'F'){
return EEPROM.read(511);
}
return 0;
}
/*****************************************************/
void RF24Mesh::setNodeID(uint8_t nodeID){
if(millis() - lastSaveTime < MESH_MIN_SAVE_TIME && millis() >= MESH_MIN_SAVE_TIME){
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.println(F(" MSH: Can't change nodeID that fast"));
#endif
return;
}
lastSaveTime=millis();
if(EEPROM.read(509) != 'R' || EEPROM.read(510) != 'F' || EEPROM.read(511) != nodeID){
EEPROM.write(509,'R'); EEPROM.write(510,'F'); EEPROM.write(511, nodeID);
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.println(F(" MSH: Wrote data to EEPROM"));
#endif
}else{
#ifdef MESH_DEBUG_SERIAL
Serial.print( millis() ); Serial.println(F(" MSH: Data already stored in EEPROM"));
#endif
}
}
/*****************************************************/
#else
/*****************************************************/
uint8_t RF24Mesh::getNodeID(){
return _nodeID;
}
void RF24Mesh::setNodeID(uint8_t nodeID){
_nodeID = nodeID;
}
#endif
/*****************************************************/
void RF24Mesh::setStaticAddress(char nodeID, uint16_t address){
addrList[addrListTop].nodeID = nodeID;
addrList[addrListTop].address = address;
++addrListTop;
addrList = (addrListStruct*)realloc(addrList,(addrListTop) * sizeof(addrListStruct));
}
/*****************************************************/
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)
void RF24Mesh::DHCP(){
if(doDHCP){
doDHCP = 0;
}else{ return; }
RF24NetworkHeader header;
memcpy(&header,network.frame_buffer,sizeof(RF24NetworkHeader));
struct addrResponseStruct{
uint16_t requester;
uint16_t new_address;
} addrResponse;
// 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
//network.read(header,0,0);
return;
}
//network.read(header,&addrResponse,sizeof(addrResponse));
memcpy(&addrResponse,network.frame_buffer+sizeof(RF24NetworkHeader),sizeof(addrResponse));
// Get the address of the sender (initial, or intermediary)
uint16_t fwd_by = header.from_node;
uint8_t shiftVal = 0;
if(header.from_node == addrResponse.requester || header.from_node == MESH_DEFAULT_ADDRESS){ //Addresses 01-05
fwd_by = 0; // No forwarding address
}else{ //Addresses 01111-05555
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
}
#ifdef MESH_DEBUG_PRINTF
// printf("%u MSH: Rcv addr req from_id %d \n",millis(),from_id);
#endif
for(int i=MESH_MAX_CHILDREN; i> 0; i--){ // For each of the possible addresses (5 max)
bool found = 0;
addrResponse.new_address = fwd_by | (i << shiftVal);
if(!addrResponse.new_address ){ /*printf("dumped 0%o\n",addrResponse.new_address);*/ 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 == addrResponse.new_address && addrList[i].nodeID != from_id ) || addrResponse.new_address == 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 != addrResponse.requester){ //Is NOT node 01 to 05
delay(2);
if( network.write(header,&addrResponse,sizeof(addrResponse)) ){
//addrMap[from_id] = addrResponse.new_address;
}
}else{
delay(2);
network.write(header,&addrResponse,sizeof(addrResponse),header.to_node);
//addrMap[from_id] = addrResponse.new_address;
}
uint32_t timer=millis();
while(network.update() != MESH_ADDR_CONFIRM){
if(millis()-timer>900){
//printf("No addr confirmation from 0%o\n",header.to_node);
return;
}
}
//printf("Got addr confirmation from 0%o\n",header.to_node);
found = 0;
for(uint8_t i=0; i < addrListTop; i++){
if( addrList[i].nodeID == from_id ){
addrList[i].address = addrResponse.new_address;
found = 1;
#if defined (__linux) && !defined(__ARDUINO_X86__)
if(millis()-lastFileSave > 300){
lastFileSave = millis();
saveDHCP();
}
#endif
break;
}
}
if(!found){
addrList[addrListTop].nodeID = from_id;
addrList[addrListTop].address = addrResponse.new_address;
#if defined (__linux) && !defined(__ARDUINO_X86__)
if(millis()-lastFileSave > 300){
lastFileSave = millis();
saveDHCP();
}
#endif
++addrListTop;
addrList = (addrListStruct*)realloc(addrList,(addrListTop+1) * sizeof(addrListStruct));
}
#ifdef MESH_DEBUG_PRINTF
printf("Sent to 0%o phys: 0%o new: 0%o id: %d\n", header.to_node,addrResponse.requester,addrResponse.new_address,header.reserved);
#endif
break;
}else{
#if defined (MESH_DEBUG_PRINTF)
printf("not allocated\n");
#endif
}
}
//}else{
//break;
//}
//}
}
/*****************************************************/
#endif