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;
45#if !defined(MESH_NOMASTER)
46 if (!addrMemAllocated) {
47 addrMemAllocated =
true;
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));
87 network.write(*header, &returnAddr,
sizeof(returnAddr));
91 uint16_t* fromAddr = (uint16_t*)network.frame_buffer;
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;
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>
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)
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)
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>
268uint8_t ESBMesh<network_t, radio_t>::getLevel(uint16_t address)
280template<
class network_t,
class radio_t>
281void ESBMesh<network_t, radio_t>::beginDefault()
283 radio.stopListening();
290template<
class network_t,
class radio_t>
296 if (network.write(header, 0, 0)) {
306template<
class network_t,
class radio_t>
310 if (
addrList[i].address == address) {
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>
352bool ESBMesh<network_t, radio_t>::requestAddress(uint8_t level)
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>
472template<
class network_t,
class radio_t>
482 if (searchBy ==
false) {
485 #if defined(__linux) && !defined(__ARDUINO_X86__)
492 if (
addrList[i].address == address) {
495 #if defined(__linux) && !defined(__ARDUINO_X86__)
508 #if defined(__linux) && !defined(__ARDUINO_X86__)
515template<
class network_t,
class radio_t>
519 #if defined(__linux) && !defined(__ARDUINO_X86__)
520 std::ifstream infile(
"dhcplist.txt", std::ifstream::binary);
523 infile.seekg(0, infile.end);
524 int length = infile.tellg();
525 infile.seekg(0, infile.beg);
539template<
class network_t,
class radio_t>
542 #if defined(__linux) && !defined(__ARDUINO_X86__)
543 std::ofstream outfile(
"dhcplist.txt", std::ofstream::binary | std::ofstream::trunc);
554template<
class network_t,
class radio_t>
562 RF24NetworkHeader header;
563 memcpy(&header, network.frame_buffer,
sizeof(RF24NetworkHeader));
568 if (!header.reserved || header.type != NETWORK_REQ_ADDRESS) {
574 uint8_t shiftVal = 0;
575 bool extraChild =
false;
578 fwd_by = header.from_node;
598 newAddress = fwd_by | (i << shiftVal);
604 if (
addrList[i].address == newAddress &&
addrList[i].nodeID != header.reserved) {
611 header.type = NETWORK_ADDR_RESPONSE;
612 header.to_node = header.from_node;
617 #if defined(SLOW_ADDR_POLL_RESPONSE)
618 delay(SLOW_ADDR_POLL_RESPONSE);
623 if (!network.write(header, &newAddress,
sizeof(newAddress))) {
624 network.write(header, &newAddress,
sizeof(newAddress));
629 network.write(header, &newAddress,
sizeof(newAddress), header.to_node);
645template<
class network_t,
class radio_t>
649 this->meshCallback = meshCallback;
656#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))
uint8_t addrListTop
The number of entries in the addrListStruct of assigned addresses.
ESBMesh(radio_t &_radio, network_t &_network)
addrListStruct * addrList
A array of addrListStruct elements for assigned addresses.
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.