Newly Optimized RF24Network Layer  v1.0.7
2014-2015 - Optimized RF24 Network Layer for NRF24L01 radios
RF24Network.h
1 /*
2  Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7  */
8 
9 #ifndef __RF24NETWORK_H__
10 #define __RF24NETWORK_H__
11 
18 #include <stddef.h>
19 #include <stdint.h>
20 #include "RF24Network_config.h"
21 
22 #if (defined (__linux) || defined (linux)) && !defined (__ARDUINO_X86__)
23  #include <stdint.h>
24  #include <stdio.h>
25  #include <time.h>
26  #include <string.h>
27  #include <sys/time.h>
28  #include <stddef.h>
29  #include <assert.h>
30  #include <map>
31  #include <utility> // std::pair
32  #include <queue>
33 
34 #endif
35 
36 
41 /* Header types range */
42 #define MIN_USER_DEFINED_HEADER_TYPE 0
43 #define MAX_USER_DEFINED_HEADER_TYPE 127
44 
49 // ACK Response Types
74 #define NETWORK_ADDR_RESPONSE 128
75 //#define NETWORK_ADDR_CONFIRM 129
76 
81 #define NETWORK_PING 130
82 
100 #define EXTERNAL_DATA_TYPE 131
101 
105 #define NETWORK_FIRST_FRAGMENT 148
106 
110 #define NETWORK_MORE_FRAGMENTS 149
111 
116 #define NETWORK_LAST_FRAGMENT 150
117 //#define NETWORK_LAST_FRAGMENT 201
118 
119 // NO ACK Response Types
120 //#define NETWORK_ACK_REQUEST 192
121 
133 #define NETWORK_ACK 193
134 
142 #define NETWORK_POLL 194
143 
150 #define NETWORK_REQ_ADDRESS 195
151 //#define NETWORK_ADDR_LOOKUP 196
152 //#define NETWORK_ADDR_RELEASE 197
155 #define NETWORK_MORE_FRAGMENTS_NACK 200
156 
157 
159 #define TX_NORMAL 0
160 #define TX_ROUTED 1
161 #define USER_TX_TO_PHYSICAL_ADDRESS 2 //no network ACK
162 #define USER_TX_TO_LOGICAL_ADDRESS 3 // network ACK
163 #define USER_TX_MULTICAST 4
164 
165 #define MAX_FRAME_SIZE 32 //Size of individual radio frames
166 #define FRAME_HEADER_SIZE 10 //Size of RF24Network frames - data
167 
168 #define USE_CURRENT_CHANNEL 255 // Use current radio channel when setting up the network
169 
172  #define FLAG_HOLD_INCOMING 1
173 
180  #define FLAG_BYPASS_HOLDS 2
181 
182  #define FLAG_FAST_FRAG 4
183 
184  #define FLAG_NO_POLL 8
185 
186 class RF24;
187 
196 {
197  uint16_t from_node;
198  uint16_t to_node;
199  uint16_t id;
206  unsigned char type;
212  unsigned char reserved;
214  static uint16_t next_id;
223 
247  RF24NetworkHeader(uint16_t _to, unsigned char _type = 0): to_node(_to), id(next_id++), type(_type) {}
257  const char* toString(void) const;
258 };
259 
260 
275 {
277  uint16_t message_size;
283  #if defined (RF24_LINUX)
284  uint8_t message_buffer[MAX_PAYLOAD_SIZE]; //< Array to store the message
285  #else
286  uint8_t *message_buffer; //< Pointer to the buffer storing the actual message
287  #endif
288 
293  //RF24NetworkFrame() {}
294 
315 #if defined (RF24_LINUX)
316  RF24NetworkFrame(RF24NetworkHeader& _header, const void* _message = NULL, uint16_t _len = 0) :
317  header(_header), message_size(_len) {
318  if (_message && _len) {
319  memcpy(message_buffer,_message,_len);
320  }
321  }
322 #else
323  RF24NetworkFrame(RF24NetworkHeader &_header, uint16_t _message_size):
324  header(_header), message_size(_message_size){
325  }
326 #endif
327 
328 
338  const char* toString(void) const;
339 
340 };
341 
342 
343 
352 {
353 
362 public:
370  RF24Network( RF24& _radio );
371 
396  inline void begin(uint16_t _node_address){
397  begin(USE_CURRENT_CHANNEL,_node_address);
398  }
399 
410  uint8_t update(void);
411 
417  bool available(void);
418 
429  uint16_t peek(RF24NetworkHeader& header);
430 
451  uint16_t read(RF24NetworkHeader& header, void* message, uint16_t maxlen);
452 
472  bool write(RF24NetworkHeader& header,const void* message, uint16_t len);
473 
495  RF24Network( RF24& _radio, RF24& _radio1);
496 
511  void multicastLevel(uint8_t level);
512 
521 
530  void setup_watchdog(uint8_t prescalar);
531 
542  uint32_t txTimeout;
551  uint16_t routeTimeout;
572  void failures(uint32_t *_fails, uint32_t *_ok);
573 
574  #if defined (RF24NetworkMulticast)
575 
590  bool multicast(RF24NetworkHeader& header,const void* message, uint16_t len, uint8_t level);
591 
592 
593  #endif
594 
600  bool write(RF24NetworkHeader& header,const void* message, uint16_t len, uint16_t writeDirect);
601 
625  bool sleepNode( unsigned int cycles, int interruptPin );
626 
627 
633  uint16_t parent() const;
634 
638  uint16_t addressOfPipe( uint16_t node,uint8_t pipeNo );
639 
644  bool is_valid_address( uint16_t node );
645 
675  void begin(uint8_t _channel, uint16_t _node_address );
676 
687  uint8_t frame_buffer[MAX_FRAME_SIZE];
688 
704  #if defined (RF24_LINUX)
705  std::queue<RF24NetworkFrame> external_queue;
706  #endif
707 
708  #if !defined ( DISABLE_FRAGMENTATION ) && !defined (RF24_LINUX)
709 
728  #endif
729 
748 
763  uint8_t networkFlags;
764 
765  private:
766 
767  uint32_t txTime;
768 
769  bool write(uint16_t, uint8_t directTo);
770  bool write_to_pipe( uint16_t node, uint8_t pipe, bool multicast );
771  uint8_t enqueue(RF24NetworkHeader *header);
772 
773  bool is_direct_child( uint16_t node );
774  bool is_descendant( uint16_t node );
775 
776  uint16_t direct_child_route_to( uint16_t node );
777  //uint8_t pipe_to_descendant( uint16_t node );
778  void setup_address(void);
779  bool _write(RF24NetworkHeader& header,const void* message, uint16_t len, uint16_t writeDirect);
780 
781  struct logicalToPhysicalStruct{
782  uint16_t send_node;
783  uint8_t send_pipe;
784  bool multicast;
785  };
786 
787  bool logicalToPhysicalAddress(logicalToPhysicalStruct *conversionInfo);
788 
789 
790  RF24& radio;
791 #if defined (DUAL_HEAD_RADIO)
792  RF24& radio1;
793 #endif
794 #if defined (RF24NetworkMulticast)
795  uint8_t multicast_level;
796 #endif
797  uint16_t node_address;
798  //const static int frame_size = 32; /**< How large is each frame over the air */
799  uint8_t frame_size;
800  const static unsigned int max_frame_payload_size = MAX_FRAME_SIZE-sizeof(RF24NetworkHeader);
801 
802  #if defined (RF24_LINUX)
803  std::queue<RF24NetworkFrame> frame_queue;
804  std::map< uint16_t, RF24NetworkFrame> frameFragmentsCache;
805  bool appendFragmentToFrame(RF24NetworkFrame frame);
806 
807  #else
808  #if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
809  #if !defined (NUM_USER_PAYLOADS)
810  #define NUM_USER_PAYLOADS 3
811  #endif
812  #endif
813  #if !defined (NUM_USER_PAYLOADS)
814  #define NUM_USER_PAYLOADS 5
815  #endif
816 
817  #if defined (DISABLE_USER_PAYLOADS)
818  uint8_t frame_queue[1];
819  #else
820  uint8_t frame_queue[MAIN_BUFFER_SIZE];
821  #endif
822 
823  uint8_t* next_frame;
825  #if !defined ( DISABLE_FRAGMENTATION )
826  RF24NetworkFrame frag_queue;
827  uint8_t frag_queue_message_buffer[MAX_PAYLOAD_SIZE]; //frame size + 1
828  #endif
829 
830  #endif
831 
832  //uint8_t frag_queue[MAX_PAYLOAD_SIZE + 11];
833  //RF24NetworkFrame frag_queue;
834 
835  uint16_t parent_node;
836  uint8_t parent_pipe;
837  uint16_t node_mask;
839  #if defined ENABLE_NETWORK_STATS
840  static uint32_t nFails;
841  static uint32_t nOK;
842  #endif
843 
844 public:
845 
846 
847 
848 };
849 
1298 #endif // __RF24NETWORK_H__
1299 
RF24NetworkHeader()
Default constructor.
Definition: RF24Network.h:222
bool returnSysMsgs
Variable to determine whether update() will return after the radio buffers have been emptied (DEFAULT...
Definition: RF24Network.h:747
RF24NetworkFrame()
Default constructor.
Definition: RF24Network.h:295
RF24NetworkFrame * frag_ptr
Linux Data with a header type of EXTERNAL_DATA_TYPE will be loaded into a separate queue...
Definition: RF24Network.h:727
uint8_t * message_buffer
On Arduino, the message buffer is just a pointer, and can be pointed to any memory location...
Definition: RF24Network.h:286
void multicastLevel(uint8_t level)
By default, multicast addresses are divided into levels.
Definition: RF24Network.cpp:1219
void begin(uint16_t _node_address)
Bring up the network using the current radio frequency/channel.
Definition: RF24Network.h:396
uint16_t to_node
Logical address where the message is going.
Definition: RF24Network.h:198
bool available(void)
Test whether there is a message available for this node.
Definition: RF24Network.cpp:581
RF24NetworkFrame(RF24NetworkHeader &_header, uint16_t _message_size)
Constructor - create a network frame with data Frames are constructed and handled differently on Ardu...
Definition: RF24Network.h:323
bool multicast(RF24NetworkHeader &header, const void *message, uint16_t len, uint8_t level)
Send a multicast message to multiple nodes at once Allows messages to be rapidly broadcast through th...
Definition: RF24Network.cpp:683
uint8_t update(void)
Main layer loop.
Definition: RF24Network.cpp:136
RF24Network(RF24 &_radio)
Construct the network.
Definition: RF24Network.cpp:58
uint8_t frame_buffer[MAX_FRAME_SIZE]
The raw system frame buffer of received data.
Definition: RF24Network.h:687
uint16_t routeTimeout
This only affects payloads that are routed by one or more nodes.
Definition: RF24Network.h:551
bool sleepNode(unsigned int cycles, int interruptPin)
Sleep this node - For AVR devices only.
uint16_t message_size
The size in bytes of the payload length.
Definition: RF24Network.h:277
RF24NetworkHeader header
Header which is sent with each message.
Definition: RF24Network.h:276
const char * toString(void) const
Create debugging string.
const char * toString(void) const
Create debugging string.
Definition: RF24Network.cpp:1071
void setup_watchdog(uint8_t prescalar)
Set up the watchdog timer for sleep mode using the number 0 through 10 to represent the following tim...
static uint16_t next_id
The message ID of the next message to be sent (unused)
Definition: RF24Network.h:214
uint16_t addressOfPipe(uint16_t node, uint8_t pipeNo)
Provided a node address and a pipe number, will return the RF24Network address of that child pipe for...
Definition: RF24Network.cpp:1151
RF24NetworkHeader(uint16_t _to, unsigned char _type=0)
Send constructor.
Definition: RF24Network.h:247
uint16_t peek(RF24NetworkHeader &header)
Read the next available header.
Definition: RF24Network.cpp:607
uint32_t txTimeout
Network timeout value.
Definition: RF24Network.h:542
2014-2015 - Optimized Network Layer for RF24 Radios
Definition: RF24Network.h:351
bool is_valid_address(uint16_t node)
Definition: RF24Network.cpp:1194
uint16_t id
Sequential message ID, incremented every time a new frame is constructed.
Definition: RF24Network.h:199
unsigned char type
Message Types: User message types 1 through 64 will NOT be acknowledged by the network, while message types 65 through 127 will receive a network ACK.
Definition: RF24Network.h:206
Header which is sent with each message.
Definition: RF24Network.h:195
bool multicastRelay
Enabling this will allow this node to automatically forward received multicast frames to the next hig...
Definition: RF24Network.h:520
unsigned char reserved
During fragmentation, it carries the fragment_id, and on the last fragment it carries the header_type...
Definition: RF24Network.h:212
uint16_t parent() const
This node's parent address.
Definition: RF24Network.cpp:593
uint16_t read(RF24NetworkHeader &header, void *message, uint16_t maxlen)
Read a message.
Definition: RF24Network.cpp:628
uint16_t from_node
Logical address where the message was generated.
Definition: RF24Network.h:197
Frame structure for internal message handling, and for use by external applications.
Definition: RF24Network.h:274
void failures(uint32_t *_fails, uint32_t *_ok)
Return the number of failures and successes for all transmitted payloads, routed or sent directly...
uint8_t networkFlags
Network Flags allow control of data flow.
Definition: RF24Network.h:763
bool write(RF24NetworkHeader &header, const void *message, uint16_t len)
Send a message.
Definition: RF24Network.cpp:692