-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
143 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "Mesh.hpp" | ||
|
||
Mesh::Mesh(): hosts(nullptr) {} | ||
|
||
Mesh::~Mesh() { | ||
if (this->hosts != nullptr) { | ||
delete this->hosts; | ||
} | ||
} | ||
|
||
void Mesh::addHost(Host h) { | ||
this->hosts = new List<Host>(h, this->hosts); | ||
} | ||
|
||
const List<Host>* Mesh::getHosts() { | ||
return this->hosts; | ||
} | ||
|
||
void Mesh::scan() { | ||
MDNS.queryService("ssm", "tcp"); | ||
|
||
uint16_t i = 0; | ||
while(true) { | ||
String h = MDNS.hostname(i); // FIXME Causes an error log on the serial. | ||
|
||
if(h == "") break; | ||
|
||
String ip = MDNS.IP(i).toString(); | ||
|
||
Host host = Host(h, ip, MDNS.port(i)); | ||
|
||
if(!contains(this->hosts, host)) { | ||
this->addHost(host); | ||
} | ||
|
||
i++; | ||
} | ||
} | ||
|
||
void Mesh::update() { | ||
#ifdef ESP8266 | ||
MDNS.update(); | ||
#endif | ||
} | ||
|
||
bool Mesh::begin(String hostname, String ip, uint16_t port) { | ||
// NOTE Add self to start the mesh. | ||
this->addHost(Host(hostname, ip, port)); | ||
|
||
bool ret = MDNS.begin(hostname.c_str()); | ||
MDNS.addService("http", "tcp", port); | ||
MDNS.addService("ssm", "tcp", port); | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef __MESH_HPP__ | ||
#define __MESH_HPP__ | ||
|
||
#include <Arduino.h> | ||
|
||
#ifdef ESP8266 | ||
#include <ESP8266mDNS.h> | ||
#endif | ||
|
||
#ifdef ESP32 | ||
#include <ESPmDNS.h> | ||
#endif | ||
|
||
#include "List.hpp" | ||
|
||
struct Host { | ||
String hostname; | ||
String ip; | ||
uint16_t port; | ||
|
||
Host(String h, String i, uint16_t p) | ||
: hostname(h), | ||
ip(i), | ||
port(p) | ||
{} | ||
|
||
bool operator==(const Host& other) const { | ||
return hostname == other.hostname && ip == other.ip && port == other.port; | ||
} | ||
}; | ||
|
||
class Mesh { | ||
private: | ||
List<Host> *hosts; // FIXME Could use Reading<any> here. | ||
|
||
public: | ||
|
||
Mesh(); | ||
~Mesh(); | ||
|
||
bool begin(String hostname, String ip, uint16_t port); | ||
|
||
void scan(); | ||
void update(); | ||
void addHost(Host h); | ||
|
||
const List<Host> *getHosts(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters