-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from sidoh/48899_discovery
Add support for 48899 discovery
- Loading branch information
Showing
8 changed files
with
146 additions
and
15 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,85 @@ | ||
#include <MiLightDiscoveryServer.h> | ||
#include <Size.h> | ||
#include <ESP8266WiFi.h> | ||
|
||
const char V3_SEARCH_STRING[] = "Link_Wi-Fi"; | ||
const char V6_SEARCH_STRING[] = "HF-A11ASSISTHREAD"; | ||
|
||
MiLightDiscoveryServer::MiLightDiscoveryServer(Settings& settings) | ||
: settings(settings) | ||
{ } | ||
|
||
MiLightDiscoveryServer::MiLightDiscoveryServer(MiLightDiscoveryServer& other) | ||
: settings(other.settings) | ||
{ } | ||
|
||
MiLightDiscoveryServer& MiLightDiscoveryServer::operator=(MiLightDiscoveryServer other) { | ||
this->settings = other.settings; | ||
this->socket = other.socket; | ||
} | ||
|
||
MiLightDiscoveryServer::~MiLightDiscoveryServer() { | ||
socket.stop(); | ||
} | ||
|
||
void MiLightDiscoveryServer::begin() { | ||
socket.begin(settings.discoveryPort); | ||
} | ||
|
||
void MiLightDiscoveryServer::handleClient() { | ||
size_t packetSize = socket.parsePacket(); | ||
|
||
if (packetSize) { | ||
char buffer[size(V6_SEARCH_STRING) + 1]; | ||
socket.read(buffer, packetSize); | ||
buffer[packetSize] = 0; | ||
|
||
#ifdef MILIGHT_UDP_DEBUG | ||
printf("Got discovery packet: %s\n", buffer); | ||
#endif | ||
|
||
if (strcmp(buffer, V3_SEARCH_STRING) == 0) { | ||
handleDiscovery(5); | ||
} else if (strcmp(buffer, V6_SEARCH_STRING) == 0) { | ||
handleDiscovery(6); | ||
} | ||
} | ||
} | ||
|
||
void MiLightDiscoveryServer::handleDiscovery(uint8_t version) { | ||
#ifdef MILIGHT_UDP_DEBUG | ||
printf("Handling discovery for version: %u, %d configs to consider\n", version, settings.numGatewayConfigs); | ||
#endif | ||
|
||
char buffer[40]; | ||
|
||
for (size_t i = 0; i < settings.numGatewayConfigs; i++) { | ||
GatewayConfig* config = settings.gatewayConfigs[i]; | ||
|
||
if (config->protocolVersion != version) { | ||
continue; | ||
} | ||
|
||
IPAddress addr = WiFi.localIP(); | ||
char* ptr = buffer; | ||
ptr += sprintf_P( | ||
buffer, | ||
PSTR("%d.%d.%d.%d,00000000%02X%02X"), | ||
addr[0], addr[1], addr[2], addr[3], | ||
(config->deviceId >> 8), (config->deviceId & 0xFF) | ||
); | ||
|
||
if (config->protocolVersion == 5) { | ||
sendResponse(buffer); | ||
} else { | ||
sprintf_P(ptr, PSTR(",HF-LPB100")); | ||
sendResponse(buffer); | ||
} | ||
} | ||
} | ||
|
||
void MiLightDiscoveryServer::sendResponse(char* buffer) { | ||
socket.beginPacket(socket.remoteIP(), socket.remotePort()); | ||
socket.write(buffer); | ||
socket.endPacket(); | ||
} |
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,25 @@ | ||
#include <WiFiUdp.h> | ||
#include <Settings.h> | ||
|
||
#ifndef MILIGHT_DISCOVERY_SERVER_H | ||
#define MILIGHT_DISCOVERY_SERVER_H | ||
|
||
class MiLightDiscoveryServer { | ||
public: | ||
MiLightDiscoveryServer(Settings& settings); | ||
MiLightDiscoveryServer(MiLightDiscoveryServer&); | ||
MiLightDiscoveryServer& operator=(MiLightDiscoveryServer other); | ||
~MiLightDiscoveryServer(); | ||
|
||
void begin(); | ||
void handleClient(); | ||
|
||
private: | ||
Settings& settings; | ||
WiFiUDP socket; | ||
|
||
void handleDiscovery(uint8_t version); | ||
void sendResponse(char* buffer); | ||
}; | ||
|
||
#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