forked from maniacbug/RF24Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF24Network.cpp
436 lines (333 loc) · 11.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
/*
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"
#include "RF24.h"
#include "RF24Network.h"
uint16_t RF24NetworkHeader::next_id = 1;
uint64_t pipe_address( uint16_t node, uint8_t pipe );
bool is_valid_address( uint16_t node );
/******************************************************************/
RF24Network::RF24Network( RF24& _radio ): radio(_radio), next_frame(frame_queue)
{
}
/******************************************************************/
void RF24Network::begin(uint8_t _channel, uint16_t _node_address )
{
if (! is_valid_address(_node_address) )
return;
node_address = _node_address;
// Set up the radio the way we want it to look
radio.setChannel(_channel);
radio.setDataRate(RF24_1MBPS);
radio.setCRCLength(RF24_CRC_16);
// Setup our address helper cache
setup_address();
// Open up all listening pipes
int i = 6;
while (i--)
radio.openReadingPipe(i,pipe_address(_node_address,i));
radio.startListening();
// Spew debugging state about the radio
radio.printDetails();
}
/******************************************************************/
void RF24Network::update(void)
{
// if there is data ready
uint8_t pipe_num;
while ( radio.available(&pipe_num) )
{
// Dump the payloads until we've gotten everything
boolean done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( frame_buffer, sizeof(frame_buffer) );
// Read the beginning of the frame as the header
const RF24NetworkHeader& header = * reinterpret_cast<RF24NetworkHeader*>(frame_buffer);
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));
// Throw it away if it's not a valid address
if ( !is_valid_address(header.to_node) )
continue;
// Is this for us?
if ( header.to_node == node_address )
// Add it to the buffer of frames for us
enqueue();
else
// Relay it
write(header.to_node);
// NOT NEEDED anymore. Now all reading pipes are open to start.
#if 0
// If this was for us, from one of our children, but on our listening
// pipe, it could mean that we are not listening to them. If so, open up
// and listen to their talking pipe
if ( header.to_node == node_address && pipe_num == 0 && is_descendant(header.from_node) )
{
uint8_t pipe = pipe_to_descendant(header.from_node);
radio.openReadingPipe(pipe,pipe_address(node_address,pipe));
// Also need to open pipe 1 so the system can get the full 5-byte address of the pipe.
radio.openReadingPipe(1,pipe_address(node_address,1));
}
#endif
}
}
}
/******************************************************************/
bool RF24Network::enqueue(void)
{
bool result = false;
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Enqueue @%x "),millis(),next_frame-frame_queue));
// Copy the current frame into the frame queue
if ( next_frame <= frame_queue + sizeof(frame_queue) )
{
memcpy(next_frame,frame_buffer, frame_size );
next_frame += frame_size;
result = true;
IF_SERIAL_DEBUG(printf_P(PSTR("ok\n\r")));
}
else
{
IF_SERIAL_DEBUG(printf_P(PSTR("failed\n\r")));
}
return result;
}
/******************************************************************/
bool RF24Network::available(void)
{
// Are there frames on the queue for us?
return (next_frame > frame_queue);
}
/******************************************************************/
void RF24Network::peek(RF24NetworkHeader& header)
{
if ( available() )
{
// Copy the next available frame from the queue into the provided buffer
memcpy(&header,next_frame-frame_size,sizeof(RF24NetworkHeader));
}
}
/******************************************************************/
size_t RF24Network::read(RF24NetworkHeader& header,void* message, size_t maxlen)
{
size_t bufsize = 0;
if ( available() )
{
// Move the pointer back one in the queue
next_frame -= frame_size;
uint8_t* frame = next_frame;
// How much buffer size should we actually copy?
bufsize = min(maxlen,frame_size-sizeof(RF24NetworkHeader));
// Copy the next available frame from the queue into the provided buffer
memcpy(&header,frame,sizeof(RF24NetworkHeader));
memcpy(message,frame+sizeof(RF24NetworkHeader),bufsize);
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Received %s\n\r"),millis(),header.toString()));
}
return bufsize;
}
/******************************************************************/
bool RF24Network::write(RF24NetworkHeader& header,const void* message, size_t len)
{
// Fill out the header
header.from_node = node_address;
// Build the full frame to send
memcpy(frame_buffer,&header,sizeof(RF24NetworkHeader));
if (len)
memcpy(frame_buffer + sizeof(RF24NetworkHeader),message,min(frame_size-sizeof(RF24NetworkHeader),len));
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: NET Sending %s\n\r"),millis(),header.toString()));
if (len)
{
IF_SERIAL_DEBUG(const uint16_t* i = reinterpret_cast<const uint16_t*>(message);printf_P(PSTR("%lu: NET message %04x\n\r"),millis(),*i));
}
// If the user is trying to send it to himself
if ( header.to_node == node_address )
// Just queue it in the received queue
return enqueue();
else
// Otherwise send it out over the air
return write(header.to_node);
}
/******************************************************************/
bool RF24Network::write(uint16_t to_node)
{
bool ok = false;
// Throw it away if it's not a valid address
if ( !is_valid_address(to_node) )
return false;
// First, stop listening so we can talk.
radio.stopListening();
// Where do we send this? By default, to our parent
uint16_t send_node = parent_node;
// On which pipe
uint8_t send_pipe = parent_pipe;
// If the node is a direct child,
if ( is_direct_child(to_node) )
{
// Send directly
send_node = to_node;
// To its listening pipe
send_pipe = 0;
}
// If the node is a child of a child
// talk on our child's listening pipe,
// and let the direct child relay it.
else if ( is_descendant(to_node) )
{
send_node = direct_child_route_to(to_node);
send_pipe = 0;
}
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Sending to 0%o via 0%o on pipe %x\n\r"),millis(),to_node,send_node,send_pipe));
// First, stop listening so we can talk
radio.stopListening();
// Put the frame on the pipe
ok = write_to_pipe( send_node, send_pipe );
// NOT NEEDED anymore. Now all reading pipes are open to start.
#if 0
// If we are talking on our talking pipe, it's possible that no one is listening.
// If this fails, try sending it on our parent's listening pipe. That will wake
// it up, and next time it will listen to us.
if ( !ok && send_node == parent_node )
ok = write_to_pipe( parent_node, 0 );
#endif
// Now, continue listening
radio.startListening();
return ok;
}
/******************************************************************/
bool RF24Network::write_to_pipe( uint16_t node, uint8_t pipe )
{
bool ok = false;
uint64_t out_pipe = pipe_address( node, pipe );
// Open the correct pipe for writing.
radio.openWritingPipe(out_pipe);
// Retry a few times
short attempts = 5;
do
{
ok = radio.write( frame_buffer, frame_size );
}
while ( !ok && --attempts );
IF_SERIAL_DEBUG(printf_P(PSTR("%lu: MAC Sent on %lx %S\n\r"),millis(),(uint32_t)out_pipe,ok?PSTR("ok"):PSTR("failed")));
return ok;
}
/******************************************************************/
const char* RF24NetworkHeader::toString(void) const
{
static char buffer[45];
snprintf_P(buffer,sizeof(buffer),PSTR("id %04x from 0%o to 0%o type %c"),id,from_node,to_node,type);
return buffer;
}
/******************************************************************/
bool RF24Network::is_direct_child( uint16_t node )
{
bool result = false;
// A direct child of ours has the same low numbers as us, and only
// one higher number.
//
// e.g. node 0234 is a direct child of 034, and node 01234 is a
// descendant but not a direct child
// First, is it even a descendant?
if ( is_descendant(node) )
{
// Does it only have ONE more level than us?
uint16_t child_node_mask = ( ~ node_mask ) << 3;
result = ( node & child_node_mask ) == 0 ;
}
return result;
}
/******************************************************************/
bool RF24Network::is_descendant( uint16_t node )
{
return ( node & node_mask ) == node_address;
}
/******************************************************************/
void RF24Network::setup_address(void)
{
// First, establish the node_mask
uint16_t node_mask_check = 0xFFFF;
while ( node_address & node_mask_check )
node_mask_check <<= 3;
node_mask = ~ node_mask_check;
// parent mask is the next level down
uint16_t parent_mask = node_mask >> 3;
// parent node is the part IN the mask
parent_node = node_address & parent_mask;
// parent pipe is the part OUT of the mask
uint16_t i = node_address;
uint16_t m = parent_mask;
while (m)
{
i >>= 3;
m >>= 3;
}
parent_pipe = i;
#ifdef SERIAL_DEBUG
printf_P(PSTR("setup_address node=0%o mask=0%o parent=0%o pipe=0%o\n\r"),node_address,node_mask,parent_node,parent_pipe);
#endif
}
/******************************************************************/
uint16_t RF24Network::direct_child_route_to( uint16_t node )
{
// Presumes that this is in fact a child!!
uint16_t child_mask = ( node_mask << 3 ) | 0B111;
return node & child_mask ;
}
/******************************************************************/
uint8_t RF24Network::pipe_to_descendant( uint16_t node )
{
uint16_t i = node;
uint16_t m = node_mask;
while (m)
{
i >>= 3;
m >>= 3;
}
return i & 0B111;
}
/******************************************************************/
uint16_t RF24Network::get_parent_node()
{
return parent_node;
}
/******************************************************************/
bool is_valid_address( uint16_t node )
{
bool result = true;
while(node)
{
uint8_t digit = node & 0B111;
if (digit < 1 || digit > 5)
{
result = false;
printf_P(PSTR("*** WARNING *** Invalid address 0%o\n\r"),node);
break;
}
node >>= 3;
}
return result;
}
/******************************************************************/
uint64_t pipe_address( uint16_t node, uint8_t pipe )
{
static uint8_t pipe_segment[] = { 0x3c, 0x5a, 0x69, 0x96, 0xa5, 0xc3 };
uint64_t result;
uint8_t* out = reinterpret_cast<uint8_t*>(&result);
out[0] = pipe_segment[pipe];
uint8_t w;
short i = 4;
short shift = 12;
while(i--)
{
w = ( node >> shift ) & 0xF ;
w |= ~w << 4;
out[i+1] = w;
shift -= 4;
}
IF_SERIAL_DEBUG(uint32_t* top = reinterpret_cast<uint32_t*>(out+1);printf_P(PSTR("%lu: NET Pipe %i on node 0%o has address %lx%x\n\r"),millis(),pipe,node,*top,*out));
return result;
}
// vim:ai:cin:sts=2 sw=2 ft=cpp