Skip to content

Commit

Permalink
[ESPEasy-Now] Add node discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
TD-er committed Oct 7, 2020
1 parent f640fab commit 12965b4
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 59 deletions.
5 changes: 5 additions & 0 deletions src/WebServer_ConfigPage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ void handle_config() {
String id = F("peer");
id += String(peer);
addFormMACBox(label, id, SecuritySettings.EspEasyNowPeerMAC[peer]);
ESPEasy_Now_peerInfo_meta meta;
if (ESPEasy_now_handler.getPeerInfo(SecuritySettings.EspEasyNowPeerMAC[peer], meta))
{
addFormNote(meta.nodeName);
}
}
#endif

Expand Down
24 changes: 20 additions & 4 deletions src/src/DataStructs/ESPEasy_Now_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void ESPEasy_Now_packet::setHeader(ESPEasy_now_hdr header)
memcpy(&_buf[0], &header, sizeof(ESPEasy_now_hdr));
}

size_t ESPEasy_Now_packet::addString(const String& string, size_t payload_pos)
size_t ESPEasy_Now_packet::addString(const String& string, size_t& payload_pos)
{
const size_t payload_size = getPayloadSize();

Expand All @@ -79,27 +79,43 @@ size_t ESPEasy_Now_packet::addString(const String& string, size_t payload_pos)
// If the null-termination does not fit, no other string can be added anyway.
size_t buf_pos = payload_pos + sizeof(ESPEasy_now_hdr);
memcpy(&_buf[buf_pos], reinterpret_cast<const uint8_t *>(string.c_str()), bytesToWrite);
payload_pos += bytesToWrite;
return bytesToWrite;
}

String ESPEasy_Now_packet::getString(size_t payload_pos) const
void ESPEasy_Now_packet::setMac(uint8_t mac[6])
{
memcpy(_mac, mac, 6);
}

void ESPEasy_Now_packet::setBroadcast()
{
for (byte i = 0; i < 6; ++i)
{
_mac[i] = 0xFF;
}
}

String ESPEasy_Now_packet::getString(size_t& payload_pos) const
{
String res;
const size_t size = getSize();
size_t buf_pos = payload_pos + sizeof(ESPEasy_now_hdr);

while (buf_pos < size && _buf[buf_pos] == 0) {
++buf_pos;
++payload_pos;
}

if (buf_pos >= size) { return res; }

const size_t maxlen = size - buf_pos;
size_t strlength = strnlen(reinterpret_cast<const char *>(&_buf[buf_pos]), maxlen);
res.reserve(strlength);
const size_t max_buf_pos = buf_pos + strlength;

for (size_t i = 0; i < strlength; ++i) {
res += static_cast<char>(_buf[buf_pos + i]);
for ( ; buf_pos < max_buf_pos; ++buf_pos, ++payload_pos) {
res += static_cast<char>(_buf[buf_pos]);
}
return res;
}
Expand Down
12 changes: 9 additions & 3 deletions src/src/DataStructs/ESPEasy_Now_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ class ESPEasy_Now_packet {

void setHeader(ESPEasy_now_hdr header);

void setMac(uint8_t mac[6]);

void setBroadcast();

// Add a string to the packet, starting at payload position payload_pos
size_t addString(const String& string,
size_t payload_pos = 0);
// Return the number of bytes added (can be 1 more than the given string)
size_t addString(const String& string,
size_t & payload_pos);

// Return a string starting from position pos in the buffer.
String getString(size_t pos = 0) const;
// payload_pos will contain the new position to start for a next string
String getString(size_t& payload_pos) const;

const uint8_t * operator[](size_t idx) const {
return &_buf[idx];
Expand Down
40 changes: 39 additions & 1 deletion src/src/DataStructs/ESPEasy_Now_peerInfo.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
#include "ESPEasy_Now_peerInfo.h"
#include "ESPEasy_Now_peerInfo.h"


static uint64_t mac_to_key(const uint8_t *mac)
{
uint64_t key = 0;

for (byte i = 0; i < 6; ++i) {
key = key << 8;
key += mac[i];
}
return key;
}

void ESPEasy_Now_peerInfo::addPeer(const uint8_t *mac, const ESPEasy_Now_peerInfo_meta& meta)
{
uint64_t key = mac_to_key(mac);

peer_map[key] = meta;
}

bool ESPEasy_Now_peerInfo::hasPeer(const uint8_t *mac) const
{
uint64_t key = mac_to_key(mac);

return peer_map.find(key) != peer_map.end();
}

bool ESPEasy_Now_peerInfo::getPeer(const uint8_t *mac,
ESPEasy_Now_peerInfo_meta& meta) const
{
uint64_t key = mac_to_key(mac);
auto it = peer_map.find(key);

if (it == peer_map.end()) { return false; }
meta = it->second;

return true;
}
22 changes: 16 additions & 6 deletions src/src/DataStructs/ESPEasy_Now_peerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@

#include <map>


struct ESPEasy_Now_peerInfo_meta {
String nodeName;
uint8_t channel = 0;
uint8_t channel = 0;
uint8_t distance = 0;
bool encrypt = false;
bool encrypt = false;
};

typedef std::map<uint64_t, ESPEasy_Now_peerInfo_meta> peerInfoMap_t;


struct ESPEasy_Now_peerInfo {
ESPEasy_Now_peerInfo_meta meta;
uint8_t mac[6] = { 0 };
};
void addPeer(const uint8_t *mac,
const ESPEasy_Now_peerInfo_meta& meta);

bool hasPeer(const uint8_t *mac) const;

typedef std::map<uint8_t[6], ESPEasy_Now_peerInfo_meta> peerInfoMap_t;
bool getPeer(const uint8_t *mac,
ESPEasy_Now_peerInfo_meta& meta) const;

private:

peerInfoMap_t peer_map;
};


#endif // DATASTRUCTS_ESPEASY_NOW_PEERINFOR_H
Loading

0 comments on commit 12965b4

Please sign in to comment.