Optimized RF24Network Layer v2.0.0
2024 - Optimized RF24 Network Layer for NRF24L01 & NRF52x radios
Loading...
Searching...
No Matches
RF24Network.h
Go to the documentation of this file.
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//ATXMega
35#elif defined(XMEGA_D3)
36 #include "../../rf24lib/rf24lib/RF24.h"
37#endif
38
39/* Header types range */
40#define MIN_USER_DEFINED_HEADER_TYPE 0
41#define MAX_USER_DEFINED_HEADER_TYPE 127
42
43// ACK Response Types
72#define NETWORK_ADDR_RESPONSE 128
73
79#define NETWORK_PING 130
80
103#define EXTERNAL_DATA_TYPE 131
104
108#define NETWORK_FIRST_FRAGMENT 148
109
113#define NETWORK_MORE_FRAGMENTS 149
114
119#define NETWORK_LAST_FRAGMENT 150
120//#define NETWORK_LAST_FRAGMENT 201
121
122// NO ACK Response Types
123//#define NETWORK_ACK_REQUEST 192
124
161#define NETWORK_ACK 193
162
170#define NETWORK_POLL 194
171
178#define NETWORK_REQ_ADDRESS 195
179
180// The following 2 atrifacts now exist in RF24Mesh as their significance was specific to RF24Mesh.
181//#define NETWORK_ADDR_LOOKUP 196
182//#define NETWORK_ADDR_RELEASE 197
185/* This isn't actually used anywhere. */
186#define NETWORK_MORE_FRAGMENTS_NACK 200
187
188/* Internal defines for handling written payloads */
189#define TX_NORMAL 0
190#define TX_ROUTED 1
191#define USER_TX_TO_PHYSICAL_ADDRESS 2 // no network ACK
192#define USER_TX_TO_LOGICAL_ADDRESS 3 // network ACK
193#define USER_TX_MULTICAST 4
194
195#define MAX_FRAME_SIZE 32 // Size of individual radio frames
196#define FRAME_HEADER_SIZE 10 // Size of RF24Network frames - data
197
202#define USE_CURRENT_CHANNEL 255
203
208#define FLAG_FAST_FRAG 4
214#define FLAG_NO_POLL 8
215
216class RF24;
217#if defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52833)
218class nrf_to_nrf;
219#endif
220
229{
231 uint16_t from_node;
232
234 uint16_t to_node;
235
237 uint16_t id;
238
246 unsigned char type;
247
254 unsigned char reserved;
255
257 static uint16_t next_id;
258
265
287 RF24NetworkHeader(uint16_t _to, unsigned char _type = 0) : to_node(_to), id(next_id++), type(_type) {}
288
298 const char* toString(void) const;
299};
300
312{
315
317 uint16_t message_size;
318
323#if defined(RF24_LINUX)
324 uint8_t message_buffer[MAX_PAYLOAD_SIZE]; // Array to store the message
325#else
326 uint8_t* message_buffer; // Pointer to the buffer storing the actual message
327#endif
328
335
346#if defined(RF24_LINUX) || defined(DOXYGEN_FORCED)
347 RF24NetworkFrame(RF24NetworkHeader& _header, const void* _message = NULL, uint16_t _len = 0) : header(_header), message_size(_len)
348 {
349 if (_message && _len) {
350 memcpy(message_buffer, _message, _len);
351 }
352 }
353#endif
354#if defined(DOXYGEN_FORCED) || !defined(RF24_LINUX)
365 RF24NetworkFrame(RF24NetworkHeader& _header, uint16_t _message_size) : header(_header), message_size(_message_size)
366 {
367 }
368#endif
369};
370
382template<class radio_t = RF24>
384{
385
393public:
411 ESBNetwork(radio_t& _radio);
412
433 inline void begin(uint16_t _node_address)
434 {
435 begin(USE_CURRENT_CHANNEL, _node_address);
436 }
437
446 uint8_t update(void);
447
453 bool available(void);
454
465 uint16_t peek(RF24NetworkHeader& header);
466
480 void peek(RF24NetworkHeader& header, void* message, uint16_t maxlen = MAX_PAYLOAD_SIZE);
481
504 uint16_t read(RF24NetworkHeader& header, void* message, uint16_t maxlen = MAX_PAYLOAD_SIZE);
505
525 bool write(RF24NetworkHeader& header, const void* message, uint16_t len);
526
555 void multicastLevel(uint8_t level);
556
565
574 void setup_watchdog(uint8_t prescalar);
575
585 uint32_t txTimeout;
586
595 uint16_t routeTimeout;
596
605#if defined(ENABLE_NETWORK_STATS) || defined(DOXYGEN_FORCED)
606
616 void failures(uint32_t* _fails, uint32_t* _ok);
617
618#endif // defined (ENABLE_NETWORK_STATS)
619#if defined(RF24NetworkMulticast)
620
637 bool multicast(RF24NetworkHeader& header, const void* message, uint16_t len, uint8_t level = 7);
638
639#endif
640
646 bool write(RF24NetworkHeader& header, const void* message, uint16_t len, uint16_t writeDirect);
647
680 bool sleepNode(unsigned int cycles, int interruptPin, uint8_t INTERRUPT_MODE = 0); //added interrupt mode support (default 0=LOW)
681
687 uint16_t parent() const;
688
692 uint16_t addressOfPipe(uint16_t node, uint8_t pipeNo);
693
703 bool is_valid_address(uint16_t node);
704
734 void begin(uint8_t _channel, uint16_t _node_address);
735
754
773#if defined(RF24_LINUX) || defined(DOXYGEN_FORCED)
774 std::queue<RF24NetworkFrame> external_queue;
775#endif
776
777#if (!defined(DISABLE_FRAGMENTATION) && !defined(RF24_LINUX)) || defined(DOXYGEN_FORCED)
797#endif
798
816
832
833protected:
834#if defined(RF24NetworkMulticast)
840#endif
847 uint16_t node_address;
848
849private:
867 bool write(uint16_t to_node, uint8_t sendType);
868
876 bool write_to_pipe(uint16_t node, uint8_t pipe, bool multicast);
877
886 uint8_t enqueue(RF24NetworkHeader* header);
887
888 /*
889 * Called from begin(), this sets up the radio to act accordingly per the
890 * logical `_node_address` parameter passed to `begin()`.
891 *
892 * Based on the value of the private member `node_address`, the resulting confiuration affects
893 * private members `node_mask`, `parent_node`, `parent_pipe`, and `_multicast_level`.
894 */
895 void setup_address(void);
896
897 /*
898 * This (non-overloaded) function copies the outgoing frame into the `frame_buffer` and detirmines
899 * the initial values passed into `logicalToPhysicalAddress()` (based on the value passed
900 * to the `writeDirect` parameter). This is always called from either of the overloaded public
901 * `write()` functions.
902 */
903 bool _write(RF24NetworkHeader& header, const void* message, uint16_t len, uint16_t writeDirect);
904
905 struct logicalToPhysicalStruct
906 {
908 uint16_t send_node;
910 uint8_t send_pipe;
912 bool multicast;
913 };
914
915 /*
916 * Translates an outgoing frame's header information into the current node's
917 * required information (`logicalToPhysicalStruct`) for making the transmission.
918 *
919 * This returns void because the translated results are stored in the
920 * `logicalToPhysicalStruct` passed by reference.
921 */
922 void logicalToPhysicalAddress(logicalToPhysicalStruct* conversionInfo);
923
924 /********* only called from `logicalToPhysicalAddress()` ***************/
925
926 /* Returns true if the given logical address (`node` parameter) is a direct child of the current node; otherwise returns false. */
927 bool is_direct_child(uint16_t node);
928 /* Returns true if the given logical address (`node` parameter) is a descendent of the current node; otherwise returns false. */
929 bool is_descendant(uint16_t node);
930 /* Returns a logical address for the first child en route to a child node */
931 uint16_t direct_child_route_to(uint16_t node);
932
933 /***********************************************************************/
934
935 radio_t& radio;
937 uint8_t frame_size; /* The outgoing frame's total size including the header info. Ranges [8, MAX_PAYLOAD_SIZE] */
938 const static unsigned int max_frame_payload_size = MAX_FRAME_SIZE - sizeof(RF24NetworkHeader); /* always 24 bytes to compensate for the frame's header */
939
940#if defined(RF24_LINUX)
941 std::queue<RF24NetworkFrame> frame_queue;
942 std::map<uint16_t, RF24NetworkFrame> frameFragmentsCache;
943 bool appendFragmentToFrame(RF24NetworkFrame frame);
944#else // Not Linux:
945
946 #if defined(DISABLE_USER_PAYLOADS)
947 uint8_t frame_queue[1];
948 #else
949 uint8_t frame_queue[MAIN_BUFFER_SIZE];
950 #endif
951
952 uint8_t* next_frame;
954 #if !defined(DISABLE_FRAGMENTATION)
955 RF24NetworkFrame frag_queue; /* a cache for re-assembling incoming message fragments */
956 uint8_t frag_queue_message_buffer[MAX_PAYLOAD_SIZE]; //frame size + 1
957 #endif
958
959#endif // Linux/Not Linux
960
961 uint16_t parent_node;
962 uint8_t parent_pipe;
963 uint16_t node_mask;
965 /* Given the Logical node address & a pipe number, this returns the Physical address assigned to the radio's pipes. */
966 uint64_t pipe_address(uint16_t node, uint8_t pipe);
967
968#if defined ENABLE_NETWORK_STATS
969 uint32_t nFails;
970 uint32_t nOK;
971#endif
972
973#if defined(RF24NetworkMulticast)
974 /* translates network level number (0-3) to a Logical address (used for TX multicasting) */
975 uint16_t levelToAddress(uint8_t level);
976#endif
977
979};
980
993#if defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52833)
994typedef ESBNetwork<nrf_to_nrf> RF52Network;
995#endif
996
1066#endif // __RF24NETWORK_H__
ESBNetwork< RF24 > RF24Network
Definition RF24Network.h:992
#define USE_CURRENT_CHANNEL
Definition RF24Network.h:202
#define MAX_FRAME_SIZE
Definition RF24Network.h:195
#define MAX_PAYLOAD_SIZE
Maximum size of fragmented network frames and fragmentation cache.
Definition RF24Network_config.h:61
#define MAIN_BUFFER_SIZE
The allocated size of the incoming frame buffer.
Definition RF24Network_config.h:72
Definition RF24Network.h:384
ESBNetwork(radio_t &_radio)
Definition RF24Network.cpp:60
std::queue< RF24NetworkFrame > external_queue
Definition RF24Network.h:774
bool sleepNode(unsigned int cycles, int interruptPin, uint8_t INTERRUPT_MODE=0)
bool returnSysMsgs
Definition RF24Network.h:815
void multicastLevel(uint8_t level)
Definition RF24Network.cpp:1154
uint16_t node_address
Definition RF24Network.h:847
bool multicast(RF24NetworkHeader &header, const void *message, uint16_t len, uint8_t level=7)
Definition RF24Network.cpp:670
bool available(void)
Definition RF24Network.cpp:544
void begin(uint16_t _node_address)
Definition RF24Network.h:433
uint16_t routeTimeout
Timeout for routed payloads.
Definition RF24Network.h:595
void setup_watchdog(uint8_t prescalar)
void failures(uint32_t *_fails, uint32_t *_ok)
uint8_t update(void)
Definition RF24Network.cpp:127
uint32_t txTimeout
Network timeout value.
Definition RF24Network.h:585
uint8_t networkFlags
Definition RF24Network.h:831
RF24NetworkFrame * frag_ptr
Definition RF24Network.h:796
uint16_t peek(RF24NetworkHeader &header)
Definition RF24Network.cpp:568
uint8_t _multicast_level
Definition RF24Network.h:839
uint16_t addressOfPipe(uint16_t node, uint8_t pipeNo)
Definition RF24Network.cpp:1094
bool is_valid_address(uint16_t node)
Definition RF24Network.cpp:1122
uint8_t frame_buffer[MAX_FRAME_SIZE]
The raw system frame buffer.
Definition RF24Network.h:753
uint16_t read(RF24NetworkHeader &header, void *message, uint16_t maxlen=MAX_PAYLOAD_SIZE)
Definition RF24Network.cpp:614
bool multicastRelay
Definition RF24Network.h:564
uint16_t parent() const
Definition RF24Network.cpp:557
bool write(RF24NetworkHeader &header, const void *message, uint16_t len)
Definition RF24Network.cpp:682
Definition RF24Network.h:312
RF24NetworkFrame(RF24NetworkHeader &_header, uint16_t _message_size)
Definition RF24Network.h:365
RF24NetworkFrame(RF24NetworkHeader &_header, const void *_message=NULL, uint16_t _len=0)
Definition RF24Network.h:347
uint8_t * message_buffer
Definition RF24Network.h:326
RF24NetworkHeader header
Definition RF24Network.h:314
uint16_t message_size
Definition RF24Network.h:317
RF24NetworkFrame()
Definition RF24Network.h:334
Definition RF24Network.h:229
const char * toString(void) const
Definition RF24Network.cpp:1014
unsigned char reserved
Definition RF24Network.h:254
RF24NetworkHeader(uint16_t _to, unsigned char _type=0)
Definition RF24Network.h:287
RF24NetworkHeader()
Definition RF24Network.h:264
unsigned char type
Definition RF24Network.h:246
uint16_t id
Definition RF24Network.h:237
uint16_t to_node
Definition RF24Network.h:234
static uint16_t next_id
Definition RF24Network.h:257
uint16_t from_node
Definition RF24Network.h:231