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>
158 if (!_nodeID)
return false;
162#if RF24MESH_CONN_CHECK_TYPE == RF24MESH_CONN_CHECK_PARENT
163 RF24NetworkHeader header;
164 header.to_node = network.parent();
165 header.type = NETWORK_PING;
167 if (network.write(header, 0, 0)) {
177 int16_t result = getAddress(_nodeID);
179 case -2:
return false;
181 case 0:
return false;
183 if ((uint16_t)result == mesh_address) {
195template<
class network_t,
class radio_t>
200 if (!nodeID)
return 0;
204#if !defined(MESH_NOMASTER)
206 for (uint8_t i = 0; i < addrListTop; i++) {
207 if (addrList[i].nodeID == nodeID) {
208 return addrList[i].address;
216 if (network.write(header, &nodeID,
sizeof(nodeID))) {
217 uint32_t timer = millis();
225 memcpy(&address, network.frame_buffer +
sizeof(RF24NetworkHeader),
sizeof(address));
233template<
class network_t,
class radio_t>
237 if (address == 0)
return 0;
240#if !defined(MESH_NOMASTER)
242 for (uint8_t i = 0; i < addrListTop; i++) {
243 if (addrList[i].address == address) {
244 return addrList[i].nodeID;
252 if (network.write(header, &address,
sizeof(address))) {
253 uint32_t timer = millis();
259 memcpy(&ID, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(ID));
267template<
class network_t,
class radio_t>
280template<
class network_t,
class radio_t>
283 radio.stopListening();
290template<
class network_t,
class radio_t>
296 if (network.write(header, 0, 0)) {
306template<
class network_t,
class radio_t>
309 for (uint8_t i = 0; i < addrListTop; ++i) {
310 if (addrList[i].address == address) {
311 addrList[i].address = 0;
321template<
class network_t,
class radio_t>
324 if (radio.available()) network.update();
326 uint8_t reqCounter = 0;
327 uint8_t totalReqs = 0;
331 uint32_t start = millis();
332 while (!requestAddress(reqCounter)) {
333 if (millis() - start > timeout)
break;
335 uint32_t timeoutInternal = 50 + ((totalReqs + 1) * (reqCounter + 1)) * 2;
336 uint32_t startInternal = millis();
337 while (millis() - startInternal < timeoutInternal) {
342 reqCounter = reqCounter % 4;
344 totalReqs = totalReqs % 10;
351template<
class network_t,
class radio_t>
357 network.multicast(header, 0, 0, level);
359 uint32_t timeout = millis() + 55;
360#define MESH_MAXPOLLS 4
362 uint8_t pollCount = 0;
365#if defined(RF24MESH_DEBUG)
366 bool goodSignal = radio.testRPD();
368 if (network.update() == NETWORK_POLL) {
369 uint16_t contact = 0;
370 memcpy(&contact, &network.frame_buffer[0],
sizeof(contact));
374 for (uint8_t i = 0; i < pollCount; ++i) {
375 if (contact == contactNode[i]) {
381 contactNode[pollCount] = contact;
383 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Poll %c -64dbm from 0%o \n"), (goodSignal ?
'>' :
'<'), contact));
391 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Polls from level %d: %d\n"), level, pollCount));
393 if (!pollCount)
return 0;
395 for (uint8_t i = 0; i < pollCount; i++) {
397 bool gotResponse = 0;
400 header.type = NETWORK_REQ_ADDRESS;
401 header.reserved = _nodeID;
402 header.to_node = contactNode[i];
405 network.write(header, 0, 0, contactNode[i]);
407 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Request address from: 0%o\n"), contactNode[i]));
409 timeout = millis() + 225;
411 while (millis() < timeout) {
412 if (network.update() == NETWORK_ADDR_RESPONSE) {
413 if (network.frame_buffer[7] == _nodeID) {
414 uint16_t newAddy = 0;
415 memcpy(&newAddy, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(newAddy));
416 uint16_t mask = 0xFFFF;
417 newAddy &= ~(mask << (3 * getLevel(contactNode[i])));
418 if (newAddy == contactNode[i]) {
431 uint16_t newAddress = 0;
432 memcpy(&newAddress, network.frame_buffer +
sizeof(RF24NetworkHeader),
sizeof(newAddress));
434 IF_RF24MESH_DEBUG(printf_P(PSTR(
"Set address: Current: 0%o New: 0%o\n"), mesh_address, newAddress));
435 mesh_address = newAddress;
437 radio.stopListening();
438 network.begin(mesh_address);
441 if (getNodeID(mesh_address) != _nodeID) {
442 if (getNodeID(mesh_address) != _nodeID) {
455template<
class network_t,
class radio_t>
462#if !defined(MESH_NOMASTER)
464template<
class network_t,
class radio_t>
467 setAddress(nodeID, address);
472template<
class network_t,
class radio_t>
476 for (uint8_t i = 0; i < addrListTop; i++) {
477 if (searchBy ==
false) {
478 if (addrList[i].nodeID == nodeID) {
479 addrList[i].address = address;
480 #if defined(__linux) && !defined(__ARDUINO_X86__)
487 if (addrList[i].address == address) {
489 addrList[i].nodeID = nodeID;
490 #if defined(__linux) && !defined(__ARDUINO_X86__)
501 addrList[addrListTop].address = address;
502 addrList[addrListTop++].nodeID = nodeID;
503 #if defined(__linux) && !defined(__ARDUINO_X86__)
510template<
class network_t,
class radio_t>
514 #if defined(__linux) && !defined(__ARDUINO_X86__)
515 std::ifstream infile(
"dhcplist.txt", std::ifstream::binary);
518 infile.seekg(0, infile.end);
519 int length = infile.tellg();
520 infile.seekg(0, infile.beg);
534template<
class network_t,
class radio_t>
537 #if defined(__linux) && !defined(__ARDUINO_X86__)
538 std::ofstream outfile(
"dhcplist.txt", std::ofstream::binary | std::ofstream::trunc);
540 for (
int i = 0; i < addrListTop; i++) {
549template<
class network_t,
class radio_t>
557 RF24NetworkHeader header;
558 memcpy(&header, network.frame_buffer,
sizeof(RF24NetworkHeader));
563 if (!header.reserved || header.type != NETWORK_REQ_ADDRESS) {
569 uint8_t shiftVal = 0;
570 bool extraChild =
false;
573 fwd_by = header.from_node;
593 newAddress = fwd_by | (i << shiftVal);
597 for (uint8_t i = 0; i < addrListTop; i++) {
599 if (addrList[i].address == newAddress && addrList[i].nodeID != header.reserved) {
606 header.type = NETWORK_ADDR_RESPONSE;
607 header.to_node = header.from_node;
610 setAddress(header.reserved, newAddress);
612 #if defined(SLOW_ADDR_POLL_RESPONSE)
613 delay(SLOW_ADDR_POLL_RESPONSE);
618 if (!network.write(header, &newAddress,
sizeof(newAddress))) {
619 network.write(header, &newAddress,
sizeof(newAddress));
624 network.write(header, &newAddress,
sizeof(newAddress), header.to_node);
640template<
class network_t,
class radio_t>
644 this->meshCallback = meshCallback;
651#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)