Skip to content

Commit

Permalink
[ESPEasy-Now] Improve p2p NodeStruct + extend for ESPEasy-Now
Browse files Browse the repository at this point in the history
Also added the webserver port in PR letscontrolit#3053
  • Loading branch information
TD-er committed Sep 25, 2020
1 parent 0b9c50c commit bc73607
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 111 deletions.
13 changes: 0 additions & 13 deletions src/Misc.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@
\*********************************************************************************************/


String getNodeTypeDisplayString(byte nodeType) {
switch (nodeType)
{
case NODE_TYPE_ID_ESP_EASY_STD: return F("ESP Easy");
case NODE_TYPE_ID_ESP_EASYM_STD: return F("ESP Easy Mega");
case NODE_TYPE_ID_ESP_EASY32_STD: return F("ESP Easy 32");
case NODE_TYPE_ID_RPI_EASY_STD: return F("RPI Easy");
case NODE_TYPE_ID_ARDUINO_EASY_STD: return F("Arduino Easy");
case NODE_TYPE_ID_NANO_EASY_STD: return F("Nano Easy");
}
return "";
}


#ifdef USES_MQTT
String getMQTT_state() {
Expand Down
68 changes: 20 additions & 48 deletions src/Networking.ino
Original file line number Diff line number Diff line change
Expand Up @@ -219,51 +219,25 @@ void checkUDP()
if (len < 13) {
break;
}
byte unit = packetBuffer[12];
#ifndef BUILD_NO_DEBUG
byte mac[6];
byte ip[4];

for (byte x = 0; x < 6; x++) {
mac[x] = packetBuffer[x + 2];
}

for (byte x = 0; x < 4; x++) {
ip[x] = packetBuffer[x + 8];
}
#endif // ifndef BUILD_NO_DEBUG
Nodes[unit].age = 0; // Create a new element when not present
NodesMap::iterator it = Nodes.find(unit);

if (it != Nodes.end()) {
for (byte x = 0; x < 4; x++) {
it->second.ip[x] = packetBuffer[x + 8];
}
it->second.age = 0; // reset 'age counter'

if (len >= 41) // extended packet size
{
it->second.build = makeWord(packetBuffer[14], packetBuffer[13]);
char tmpNodeName[26] = { 0 };
memcpy(&tmpNodeName[0], reinterpret_cast<byte *>(&packetBuffer[15]), 25);
tmpNodeName[25] = 0;
it->second.nodeName = tmpNodeName;
it->second.nodeName.trim();
it->second.nodeType = packetBuffer[40];
it->second.webgui_portnumber = 80;
if (len >= 43 && it->second.build >= 20107) {
it->second.webgui_portnumber = makeWord(packetBuffer[42],packetBuffer[41]);
}
}
int copy_length = sizeof(NodeStruct);
if (copy_length > len) {
copy_length = len;
}
NodeStruct received;
memcpy(&received, &packetBuffer[2], copy_length);
received.age = 0; // Data just got in, so age = 0
Nodes[received.unit] = received; // Create a new element when not present

#ifndef BUILD_NO_DEBUG

if (loglevelActiveFor(LOG_LEVEL_DEBUG_MORE)) {
char macaddress[20];
formatMAC(mac, macaddress);
char log[80] = { 0 };
sprintf_P(log, PSTR("UDP : %s,%s,%u"), macaddress, formatIP(ip).c_str(), unit);
String log;
log.reserve(64);
log = F("UDP : ");
log += formatMAC(received.mac);
log += ',';
log += formatIP(received.ip);
log += ',';
log += received.unit;
addLog(LOG_LEVEL_DEBUG_MORE, log);
}
#endif // ifndef BUILD_NO_DEBUG
Expand Down Expand Up @@ -402,21 +376,18 @@ void sendSysInfoUDP(byte repeats)
return;
}

// TODO: make a nice struct of it and clean up
// 1 byte 'binary token 255'
// 1 byte id '1'
// 6 byte mac
// 4 byte ip
// 1 byte unit
// 2 byte build
// 25 char name
// 1 byte node type id
// NodeStruct object (packed data struct)

// send my info to the world...
#ifndef BUILD_NO_DEBUG
addLog(LOG_LEVEL_DEBUG_MORE, F("UDP : Send Sysinfo message"));
#endif // ifndef BUILD_NO_DEBUG

NodeStruct thisNode;
thisNode.setLocalData();

for (byte counter = 0; counter < repeats; counter++)
{
uint8_t mac[] = { 0, 0, 0, 0, 0, 0 };
Expand All @@ -425,6 +396,7 @@ void sendSysInfoUDP(byte repeats)
byte data[80] = {0};
data[0] = 255;
data[1] = 1;
memcpy(&data[2], &thisNode, sizeof(NodeStruct));

for (byte x = 0; x < 6; x++) {
data[x + 2] = macread[x];
Expand Down
4 changes: 2 additions & 2 deletions src/WebServer_CustomPage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ boolean handle_custom(String path) {
TXBuffer.startStream();
sendHeadandTail(F("TmplDsh"), _HEAD);
addHtml(F("<meta http-equiv=\"refresh\" content=\"0; URL=http://"));
addHtml(it->second.ip.toString());
addHtml(it->second.IP().toString());
addHtml(F("/dashboard.esp\">"));
sendHeadandTail(F("TmplDsh"), _TAIL);
TXBuffer.endStream();
Expand All @@ -65,7 +65,7 @@ boolean handle_custom(String path) {
String name = String(it->first) + F(" - ");

if (it->first != Settings.Unit) {
name += it->second.nodeName;
name += it->second.getNodeName();
}
else {
name += Settings.Name;
Expand Down
4 changes: 2 additions & 2 deletions src/WebServer_DevicesPage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ void handle_devicess_ShowAllTasksTable(byte page)

if (it != Nodes.end()) {
addHtml(F(" - "));
addHtml(it->second.nodeName);
addHtml(it->second.getNodeName());
} else {
addHtml(F(" - Not Seen recently"));
}
Expand Down Expand Up @@ -898,7 +898,7 @@ void handle_devices_TaskSettingsPage(taskIndex_t taskIndex, byte page)
NodesMap::iterator it = Nodes.find(remoteUnit);

if (it != Nodes.end()) {
addUnit(it->second.nodeName);
addUnit(it->second.getNodeName());
} else {
addUnit(F("Unknown Unit Name"));
}
Expand Down
14 changes: 5 additions & 9 deletions src/WebServer_JSON.ino
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,16 @@ void handle_json()
addHtml("{");
stream_next_json_object_value(F("nr"), String(it->first));
stream_next_json_object_value(F("name"),
(it->first != Settings.Unit) ? it->second.nodeName : Settings.Name);
(it->first != Settings.Unit) ? it->second.getNodeName() : Settings.Name);

if (it->second.build) {
stream_next_json_object_value(F("build"), String(it->second.build));
}

if (it->second.nodeType) {
String platform = getNodeTypeDisplayString(it->second.nodeType);

if (platform.length() > 0) {
stream_next_json_object_value(F("platform"), platform);
}
stream_next_json_object_value(F("platform"), it->second.getNodeTypeDisplayString());
}
stream_next_json_object_value(F("ip"), it->second.ip.toString());
stream_next_json_object_value(F("ip"), it->second.IP().toString());
stream_last_json_object_value(F("age"), String(it->second.age));
} // if node info exists
} // for loop
Expand Down Expand Up @@ -410,10 +406,10 @@ void handle_nodes_list_json() {
}

json_number(F("first"), String(it->first));
json_prop(F("name"), isThisUnit ? Settings.Name : it->second.nodeName);
json_prop(F("name"), isThisUnit ? Settings.Name : it->second.getNodeName());

if (it->second.build) { json_prop(F("build"), String(it->second.build)); }
json_prop(F("type"), getNodeTypeDisplayString(it->second.nodeType));
json_prop(F("type"), it->second.getNodeTypeDisplayString());
json_prop(F("ip"), it->second.ip.toString());
json_number(F("age"), String(it->second.age));
json_close();
Expand Down
6 changes: 3 additions & 3 deletions src/WebServer_RootPage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ void handle_root() {
addHtml(Settings.Name);
}
else {
addHtml(it->second.nodeName);
addHtml(it->second.getNodeName());
}
html_TD();

if (it->second.build) {
addHtml(String(it->second.build));
}
html_TD();
addHtml(getNodeTypeDisplayString(it->second.nodeType));
addHtml(it->second.getNodeTypeDisplayString());
html_TD();
html_add_wide_button_prefix();
{
Expand All @@ -202,7 +202,7 @@ void handle_root() {
html += String(port);
}
html += "'>";
html += it->second.ip.toString();
html += it->second.IP().toString();
html += "</a>";
addHtml(html);
}
Expand Down
19 changes: 19 additions & 0 deletions src/src/DataStructs/ESPEasy_Now_peerInfo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ESPEasy_Now_peerInfo.h"

#include "../../ESPEasy_fdwdecl.h"


static uint64_t mac_to_key(const uint8_t *mac)
{
Expand Down Expand Up @@ -37,3 +39,20 @@ bool ESPEasy_Now_peerInfo::getPeer(const uint8_t *mac,

return true;
}

String ESPEasy_Now_peerInfo::formatPeerInfo(const uint8_t *mac) const
{
uint64_t key = mac_to_key(mac);
auto it = peer_map.find(key);
String res;

res = formatMAC(mac);

if (it == peer_map.end()) { return res; }

res.reserve(128);
res += F(" \"");
res += it->second.nodeName;
res += '"';
return res;
}
16 changes: 10 additions & 6 deletions src/src/DataStructs/ESPEasy_Now_peerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <Arduino.h>

#include <map>

#include <IPAddress.h>

struct ESPEasy_Now_peerInfo_meta {
String nodeName;
IPAddress ip;
byte unit;
uint8_t channel = 0;
uint8_t distance = 0;
bool encrypt = false;
Expand All @@ -17,13 +19,15 @@ typedef std::map<uint64_t, ESPEasy_Now_peerInfo_meta> peerInfoMap_t;


struct ESPEasy_Now_peerInfo {
void addPeer(const uint8_t *mac,
const ESPEasy_Now_peerInfo_meta& meta);
void addPeer(const uint8_t *mac,
const ESPEasy_Now_peerInfo_meta& meta);

bool hasPeer(const uint8_t *mac) const;

bool hasPeer(const uint8_t *mac) const;
bool getPeer(const uint8_t *mac,
ESPEasy_Now_peerInfo_meta& meta) const;

bool getPeer(const uint8_t *mac,
ESPEasy_Now_peerInfo_meta& meta) const;
String formatPeerInfo(const uint8_t *mac) const;

private:

Expand Down
50 changes: 50 additions & 0 deletions src/src/DataStructs/NodeStruct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "NodeStruct.h"

#include "../Globals/Settings.h"
#include "../../ESPEasy-Globals.h"

void NodeStruct::setLocalData() {
WiFi.macAddress(mac);
WiFi.softAPmacAddress(ap_mac);
{
IPAddress localIP = WiFi.localIP();
for (byte i = 0; i < 4; ++i) {
ip[i] = localIP[i];
}
}

unit = Settings.Unit;
build = Settings.Build;
memcpy(nodeName, Settings.Name, 25);
nodeType = NODE_TYPE_ID;

// webserverPort = Settings.WebserverPort; // PR #3053
}


String NodeStruct::getNodeTypeDisplayString() const {
switch (nodeType)
{
case NODE_TYPE_ID_ESP_EASY_STD: return F("ESP Easy");
case NODE_TYPE_ID_ESP_EASYM_STD: return F("ESP Easy Mega");
case NODE_TYPE_ID_ESP_EASY32_STD: return F("ESP Easy 32");
case NODE_TYPE_ID_RPI_EASY_STD: return F("RPI Easy");
case NODE_TYPE_ID_ARDUINO_EASY_STD: return F("Arduino Easy");
case NODE_TYPE_ID_NANO_EASY_STD: return F("Nano Easy");
}
return "";
}

String NodeStruct::getNodeName() const {
String res;
size_t length = strnlen(reinterpret_cast<const char*>(nodeName), sizeof(nodeName));
res.reserve(length);
for (size_t i = 0; i < length; ++i) {
res += static_cast<char>(nodeName[i]);
}
return res;
}

IPAddress NodeStruct::IP() const {
return IPAddress(ip[0], ip[1], ip[2], ip[3]);
}
Loading

0 comments on commit bc73607

Please sign in to comment.