9#if defined(__linux) && !defined(__ARDUINO_X86__)
13template<
class network_t,
class radio_t>
18#if !defined(MESH_NOMASTER)
19 addrMemAllocated =
false;
25template<
class network_t,
class radio_t>
30 radio.stopListening();
35 radio.setChannel(channel);
36 radio.setDataRate(data_rate);
37 network.returnSysMsgs =
true;
39 if (getNodeID() > 0) {
45#if !defined(MESH_NOMASTER)
46 if (!addrMemAllocated) {
47 addrMemAllocated =
true;
54 network.begin(mesh_address);
62template<
class network_t,
class radio_t>
65 uint8_t type = network.update();
68#if !defined(MESH_NOMASTER)
69 if (type == NETWORK_REQ_ADDRESS) {
75 RF24NetworkHeader* header = (RF24NetworkHeader*)(&network.frame_buffer);
76 header->to_node = header->from_node;
78 int16_t returnAddr = 0;
80 returnAddr = getAddress(network.frame_buffer[
sizeof(RF24NetworkHeader)]);
81 network.write(*header, &returnAddr,
sizeof(returnAddr));
85 memcpy(&addr, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(addr));
86 returnAddr = getNodeID(addr);
87 network.write(*header, &returnAddr,
sizeof(returnAddr));
91 uint16_t* fromAddr = (uint16_t*)network.frame_buffer;
92 releaseAddress(*fromAddr);
102template<
class network_t,
class radio_t>
107 RF24NetworkHeader header(to_node, msg_type);
108 return network.write(header, data, size);
113template<
class network_t,
class radio_t>
120 uint32_t retryDelay = 5;
123 while ((toNode = getAddress(nodeID)) < 0) {
124 if (millis() > lookupTimeout || toNode == -2) {
131 return write(toNode, data, msg_type, size);
136template<
class network_t,
class radio_t>
139 radio.stopListening();
140 radio.setChannel(_channel);
141 radio.startListening();
146template<
class network_t,
class radio_t>
149 network.networkFlags = allow ? network.networkFlags & ~FLAG_NO_POLL : network.networkFlags | FLAG_NO_POLL;
154template<
class network_t,
class radio_t>
160 int16_t result = getAddress(_nodeID);
162 case -2:
return false;
164 case 0:
return false;
166 if ((uint16_t)result == mesh_address) {
177template<
class network_t,
class radio_t>
182 if (!nodeID)
return 0;
186#if !defined(MESH_NOMASTER)
188 for (uint8_t i = 0; i < addrListTop; i++) {
189 if (addrList[i].nodeID == nodeID) {
190 return addrList[i].address;
198 if (network.write(header, &nodeID,
sizeof(nodeID))) {
199 uint32_t timer = millis();
207 memcpy(&address, network.frame_buffer +
sizeof(RF24NetworkHeader),
sizeof(address));
215template<
class network_t,
class radio_t>
219 if (address == 0)
return 0;
222#if !defined(MESH_NOMASTER)
224 for (uint8_t i = 0; i < addrListTop; i++) {
225 if (addrList[i].address == address) {
226 return addrList[i].nodeID;
234 if (network.write(header, &address,
sizeof(address))) {
235 uint32_t timer = millis();
241 memcpy(&ID, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(ID));
249template<
class network_t,
class radio_t>
262template<
class network_t,
class radio_t>
265 radio.stopListening();
272template<
class network_t,
class radio_t>
278 if (network.write(header, 0, 0)) {
288template<
class network_t,
class radio_t>
291 for (uint8_t i = 0; i < addrListTop; ++i) {
292 if (addrList[i].address == address) {
293 addrList[i].address = 0;
303template<
class network_t,
class radio_t>
306 if (radio.available()) network.update();
308 uint8_t reqCounter = 0;
309 uint8_t totalReqs = 0;
313 uint32_t start = millis();
314 while (!requestAddress(reqCounter)) {
315 if (millis() - start > timeout)
break;
317 uint32_t timeoutInternal = 50 + ((totalReqs + 1) * (reqCounter + 1)) * 2;
318 uint32_t startInternal = millis();
319 while (millis() - startInternal < timeoutInternal) {
324 reqCounter = reqCounter % 4;
326 totalReqs = totalReqs % 10;
333template<
class network_t,
class radio_t>
339 network.multicast(header, 0, 0, level);
341 uint32_t timeout = millis() + 55;
342#define MESH_MAXPOLLS 4
344 uint8_t pollCount = 0;
347#if defined(RF24MESH_DEBUG)
348 bool goodSignal = radio.testRPD();
350 if (network.update() == NETWORK_POLL) {
351 uint16_t contact = 0;
352 memcpy(&contact, &network.frame_buffer[0],
sizeof(contact));
356 for (uint8_t i = 0; i < pollCount; ++i) {
357 if (contact == contactNode[i]) {
363 contactNode[pollCount] = contact;
365 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Poll %c -64dbm from 0%o \n"), (goodSignal ?
'>' :
'<'), contact));
373 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Polls from level %d: %d\n"), level, pollCount));
375 if (!pollCount)
return 0;
377 for (uint8_t i = 0; i < pollCount; i++) {
379 bool gotResponse = 0;
382 header.type = NETWORK_REQ_ADDRESS;
383 header.reserved = _nodeID;
384 header.to_node = contactNode[i];
387 network.write(header, 0, 0, contactNode[i]);
389 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Request address from: 0%o\n"), contactNode[i]));
391 timeout = millis() + 225;
393 while (millis() < timeout) {
394 if (network.update() == NETWORK_ADDR_RESPONSE) {
395 if (network.frame_buffer[7] == _nodeID) {
396 uint16_t newAddy = 0;
397 memcpy(&newAddy, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(newAddy));
398 uint16_t mask = 0xFFFF;
399 newAddy &= ~(mask << (3 * getLevel(contactNode[i])));
400 if (newAddy == contactNode[i]) {
413 uint16_t newAddress = 0;
414 memcpy(&newAddress, network.frame_buffer +
sizeof(RF24NetworkHeader),
sizeof(newAddress));
416 IF_RF24MESH_DEBUG(printf_P(PSTR(
"Set address: Current: 0%o New: 0%o\n"), mesh_address, newAddress));
417 mesh_address = newAddress;
419 radio.stopListening();
420 network.begin(mesh_address);
423 if (getNodeID(mesh_address) != _nodeID) {
424 if (getNodeID(mesh_address) != _nodeID) {
437template<
class network_t,
class radio_t>
444#if !defined(MESH_NOMASTER)
446template<
class network_t,
class radio_t>
449 setAddress(nodeID, address);
454template<
class network_t,
class radio_t>
458 for (uint8_t i = 0; i < addrListTop; i++) {
459 if (searchBy ==
false) {
460 if (addrList[i].nodeID == nodeID) {
461 addrList[i].address = address;
462 #if defined(__linux) && !defined(__ARDUINO_X86__)
469 if (addrList[i].address == address) {
471 addrList[i].nodeID = nodeID;
472 #if defined(__linux) && !defined(__ARDUINO_X86__)
483 addrList[addrListTop].address = address;
484 addrList[addrListTop++].nodeID = nodeID;
485 #if defined(__linux) && !defined(__ARDUINO_X86__)
492template<
class network_t,
class radio_t>
496 #if defined(__linux) && !defined(__ARDUINO_X86__)
497 std::ifstream infile(
"dhcplist.txt", std::ifstream::binary);
500 infile.seekg(0, infile.end);
501 int length = infile.tellg();
502 infile.seekg(0, infile.beg);
516template<
class network_t,
class radio_t>
519 #if defined(__linux) && !defined(__ARDUINO_X86__)
520 std::ofstream outfile(
"dhcplist.txt", std::ofstream::binary | std::ofstream::trunc);
522 for (
int i = 0; i < addrListTop; i++) {
531template<
class network_t,
class radio_t>
539 RF24NetworkHeader header;
540 memcpy(&header, network.frame_buffer,
sizeof(RF24NetworkHeader));
545 if (!header.reserved || header.type != NETWORK_REQ_ADDRESS) {
551 uint8_t shiftVal = 0;
552 bool extraChild =
false;
555 fwd_by = header.from_node;
575 newAddress = fwd_by | (i << shiftVal);
579 for (uint8_t i = 0; i < addrListTop; i++) {
581 if (addrList[i].address == newAddress && addrList[i].nodeID != header.reserved) {
588 header.type = NETWORK_ADDR_RESPONSE;
589 header.to_node = header.from_node;
592 setAddress(header.reserved, newAddress);
594 #if defined(SLOW_ADDR_POLL_RESPONSE)
595 delay(SLOW_ADDR_POLL_RESPONSE);
600 if (!network.write(header, &newAddress,
sizeof(newAddress))) {
601 network.write(header, &newAddress,
sizeof(newAddress));
606 network.write(header, &newAddress,
sizeof(newAddress), header.to_node);
622template<
class network_t,
class radio_t>
626 this->meshCallback = meshCallback;
633#if defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840)
#define MESH_ADDR_RELEASE
#define MESH_MULTICAST_ADDRESS
#define IF_RF24MESH_DEBUG(x)
#define MESH_MAX_CHILDREN
Set 1 to 4 (Default: 4) Restricts the maximum children per node.
#define MESH_LOOKUP_TIMEOUT
How long to wait in ms for a response during individual address lookups.
#define IF_RF24MESH_DEBUG_MINIMAL(x)
#define MESH_DEFAULT_ADDRESS
#define MESH_CONNECTION_CHECK_ATTEMPTS
Number of attempts to verify a connection.
#define MESH_MEM_ALLOC_SIZE
master node memory allocation
#define MESH_WRITE_TIMEOUT
How long RF24Mesh::write() retries address lookups before timing out. Allows multiple attempts.
int16_t getNodeID(uint16_t address=MESH_BLANK_ID)
void setStaticAddress(uint8_t nodeID, uint16_t address)
uint16_t renewAddress(uint32_t timeout=MESH_RENEWAL_TIMEOUT)
Reconnect to the mesh and renew the current RF24Network address.
void setChild(bool allow)
bool begin(uint8_t channel=MESH_DEFAULT_CHANNEL, rf24_datarate_e data_rate=RF24_1MBPS, uint32_t timeout=MESH_RENEWAL_TIMEOUT)
void setCallback(void(*meshCallback)(void))
ESBMesh(radio_t &_radio, network_t &_network)
void setNodeID(uint8_t nodeID)
bool write(const void *data, uint8_t msg_type, size_t size, uint8_t nodeID=0)
void setChannel(uint8_t _channel)
void setAddress(uint8_t nodeID, uint16_t address, bool searchBy=false)
int16_t getAddress(uint8_t nodeID)
Convert a nodeID into an RF24Network address.
A struct for storing a nodeID and an address in a single element of the ESBMesh::addrList array.
uint8_t nodeID
The nodeID of an network node (child)
uint16_t address
The logical address of an network node (child)