9#if defined(__linux) && !defined(__ARDUINO_X86__)
13template<
class network_t,
class radio_t>
18#if !defined(MESH_NOMASTER)
26template<
class network_t,
class radio_t>
31 radio.stopListening();
36 radio.setChannel(channel);
37 radio.setDataRate(data_rate);
38 network.returnSysMsgs =
true;
55template<
class network_t,
class radio_t>
58 uint8_t type = network.update();
61#if !defined(MESH_NOMASTER)
62 if (type == NETWORK_REQ_ADDRESS) {
68 RF24NetworkHeader* header = (RF24NetworkHeader*)(&network.frame_buffer);
69 header->to_node = header->from_node;
71 int16_t returnAddr = 0;
73 returnAddr =
getAddress(network.frame_buffer[
sizeof(RF24NetworkHeader)]);
74 network.write(*header, &returnAddr,
sizeof(returnAddr));
78 memcpy(&addr, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(addr));
80 network.write(*header, &returnAddr,
sizeof(returnAddr));
84 uint16_t* fromAddr = (uint16_t*)network.frame_buffer;
95template<
class network_t,
class radio_t>
100 RF24NetworkHeader header(to_node, msg_type);
101 return network.write(header, data, size);
106template<
class network_t,
class radio_t>
113 uint32_t retryDelay = 5;
117 if (millis() > lookupTimeout || toNode == -2) {
124 return write(toNode, data, msg_type, size);
129template<
class network_t,
class radio_t>
132 radio.stopListening();
133 radio.setChannel(_channel);
134 radio.startListening();
139template<
class network_t,
class radio_t>
142 network.networkFlags = allow ? network.networkFlags & ~FLAG_NO_POLL : network.networkFlags | FLAG_NO_POLL;
147template<
class network_t,
class radio_t>
155#if RF24MESH_CONN_CHECK_TYPE == RF24MESH_CONN_CHECK_PARENT
156 RF24NetworkHeader header;
157 header.to_node = network.parent();
158 header.type = NETWORK_PING;
160 if (network.write(header, 0, 0)) {
172 case -2:
return false;
174 case 0:
return false;
188template<
class network_t,
class radio_t>
193 if (!nodeID)
return 0;
197#if !defined(MESH_NOMASTER)
209 if (network.write(header, &nodeID,
sizeof(nodeID))) {
210 uint32_t timer = millis();
218 memcpy(&address, network.frame_buffer +
sizeof(RF24NetworkHeader),
sizeof(address));
226template<
class network_t,
class radio_t>
230 if (address == 0)
return 0;
233#if !defined(MESH_NOMASTER)
236 if (
addrList[i].address == address) {
245 if (network.write(header, &address,
sizeof(address))) {
246 uint32_t timer = millis();
252 memcpy(&ID, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(ID));
260template<
class network_t,
class radio_t>
261uint8_t ESBMesh<network_t, radio_t>::getLevel(uint16_t address)
273template<
class network_t,
class radio_t>
274void ESBMesh<network_t, radio_t>::beginDefault()
276 radio.stopListening();
283template<
class network_t,
class radio_t>
289 if (network.write(header, 0, 0)) {
299template<
class network_t,
class radio_t>
303 if (
addrList[i].address == address) {
314template<
class network_t,
class radio_t>
317 if (radio.available()) network.update();
319 uint8_t reqCounter = 0;
320 uint8_t totalReqs = 0;
324 uint32_t start = millis();
325 while (!requestAddress(reqCounter)) {
326 if (millis() - start > timeout)
break;
328 uint32_t timeoutInternal = 50 + ((totalReqs + 1) * (reqCounter + 1)) * 2;
329 uint32_t startInternal = millis();
330 while (millis() - startInternal < timeoutInternal) {
335 reqCounter = reqCounter % 4;
337 totalReqs = totalReqs % 10;
344template<
class network_t,
class radio_t>
345bool ESBMesh<network_t, radio_t>::requestAddress(uint8_t level)
350 network.multicast(header, 0, 0, level);
352 uint32_t timeout = millis() + 55;
353#define MESH_MAXPOLLS 4
355 uint8_t pollCount = 0;
358#if defined(RF24MESH_DEBUG)
359 bool goodSignal = radio.testRPD();
361 if (network.update() == NETWORK_POLL) {
362 uint16_t contact = 0;
363 memcpy(&contact, &network.frame_buffer[0],
sizeof(contact));
367 for (uint8_t i = 0; i < pollCount; ++i) {
368 if (contact == contactNode[i]) {
374 contactNode[pollCount] = contact;
376 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Poll %c -64dbm from 0%o \n"), (goodSignal ?
'>' :
'<'), contact));
384 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Polls from level %d: %d\n"), level, pollCount));
386 if (!pollCount)
return 0;
388 for (uint8_t i = 0; i < pollCount; i++) {
390 bool gotResponse = 0;
393 header.type = NETWORK_REQ_ADDRESS;
394 header.reserved = _nodeID;
395 header.to_node = contactNode[i];
398 network.write(header, 0, 0, contactNode[i]);
400 IF_RF24MESH_DEBUG(printf_P(PSTR(
"MSH Request address from: 0%o\n"), contactNode[i]));
402 timeout = millis() + 225;
404 while (millis() < timeout) {
405 if (network.update() == NETWORK_ADDR_RESPONSE) {
406 if (network.frame_buffer[7] == _nodeID) {
407 uint16_t newAddy = 0;
408 memcpy(&newAddy, &network.frame_buffer[
sizeof(RF24NetworkHeader)],
sizeof(newAddy));
409 uint16_t mask = 0xFFFF;
410 newAddy &= ~(mask << (3 * getLevel(contactNode[i])));
411 if (newAddy == contactNode[i]) {
424 uint16_t newAddress = 0;
425 memcpy(&newAddress, network.frame_buffer +
sizeof(RF24NetworkHeader),
sizeof(newAddress));
427 IF_RF24MESH_DEBUG(printf_P(PSTR(
"Set address: Current: 0%o New: 0%o\n"), mesh_address, newAddress));
428 mesh_address = newAddress;
430 radio.stopListening();
431 network.begin(mesh_address);
434 if (getNodeID(mesh_address) != _nodeID) {
435 if (getNodeID(mesh_address) != _nodeID) {
448template<
class network_t,
class radio_t>
452#if !defined(MESH_NOMASTER)
453 if (!nodeID &&
addrList ==
nullptr) {
461#if !defined(MESH_NOMASTER)
463template<
class network_t,
class radio_t>
471template<
class network_t,
class radio_t>
481 if (searchBy ==
false) {
484 #if defined(__linux) && !defined(__ARDUINO_X86__)
491 if (
addrList[i].address == address) {
494 #if defined(__linux) && !defined(__ARDUINO_X86__)
507 #if defined(__linux) && !defined(__ARDUINO_X86__)
514template<
class network_t,
class radio_t>
518 #if defined(__linux) && !defined(__ARDUINO_X86__)
519 std::ifstream infile(
"dhcplist.txt", std::ifstream::binary);
522 infile.seekg(0, infile.end);
523 int length = infile.tellg();
524 infile.seekg(0, infile.beg);
538template<
class network_t,
class radio_t>
541 #if defined(__linux) && !defined(__ARDUINO_X86__)
542 std::ofstream outfile(
"dhcplist.txt", std::ofstream::binary | std::ofstream::trunc);
553template<
class network_t,
class radio_t>
561 RF24NetworkHeader header;
562 memcpy(&header, network.frame_buffer,
sizeof(RF24NetworkHeader));
567 if (!header.reserved || header.type != NETWORK_REQ_ADDRESS) {
573 uint8_t shiftVal = 0;
574 bool extraChild =
false;
577 fwd_by = header.from_node;
597 newAddress = fwd_by | (i << shiftVal);
603 if (
addrList[i].address == newAddress &&
addrList[i].nodeID != header.reserved) {
610 header.type = NETWORK_ADDR_RESPONSE;
611 header.to_node = header.from_node;
616 #if defined(SLOW_ADDR_POLL_RESPONSE)
617 delay(SLOW_ADDR_POLL_RESPONSE);
622 if (!network.write(header, &newAddress,
sizeof(newAddress))) {
623 network.write(header, &newAddress,
sizeof(newAddress));
628 network.write(header, &newAddress,
sizeof(newAddress), header.to_node);
644template<
class network_t,
class radio_t>
648 this->meshCallback = meshCallback;
655#if defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_NRF54L15)
#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.