|
|
@@ -1,6 +1,17 @@
|
|
|
#include <V6MiLightUdpServer.h>
|
|
|
#include <ESP8266WiFi.h>
|
|
|
#include <Arduino.h>
|
|
|
+#include <Size.h>
|
|
|
+#include <V6CommandHandler.h>
|
|
|
+
|
|
|
+#define MATCHES_PACKET(packet1) ( \
|
|
|
+ matchesPacket(packet1, size(packet1), packet, packetSize) \
|
|
|
+)
|
|
|
+
|
|
|
+V6CommandDemuxer V6MiLightUdpServer::COMMAND_DEMUXER = V6CommandDemuxer(
|
|
|
+ V6CommandHandler::ALL_HANDLERS,
|
|
|
+ V6CommandHandler::NUM_HANDLERS
|
|
|
+);
|
|
|
|
|
|
uint8_t V6MiLightUdpServer::START_SESSION_COMMAND[] = {
|
|
|
0x20, 0x00, 0x00, 0x00, 0x16, 0x02, 0x62, 0x3A, 0xD5, 0xED, 0xA3, 0x01, 0xAE,
|
|
|
@@ -23,14 +34,45 @@ uint8_t V6MiLightUdpServer::HEARTBEAT_HEADER[] = {
|
|
|
0xD0, 0x00, 0x00, 0x00, 0x02
|
|
|
};
|
|
|
|
|
|
+uint8_t V6MiLightUdpServer::HEARTBEAT_HEADER2[] = {
|
|
|
+ 0x30, 0x00, 0x00, 0x00, 0x03
|
|
|
+};
|
|
|
+
|
|
|
uint8_t V6MiLightUdpServer::COMMAND_RESPONSE[] = {
|
|
|
0x88, 0x00, 0x00, 0x00, 0x03, 0x00, 0xFF, 0x00
|
|
|
};
|
|
|
|
|
|
-template<typename T, size_t sz>
|
|
|
-size_t size(T(&)[sz]) {
|
|
|
- return sz;
|
|
|
-}
|
|
|
+uint8_t V6MiLightUdpServer::SEARCH_COMMAND[] = {
|
|
|
+ 0x10, 0x00, 0x00, 0x00
|
|
|
+ //, 0x24, 0x02
|
|
|
+ //, 0xAE, 0x65, 0x02, 0x39, 0x38, 0x35, 0x62
|
|
|
+};
|
|
|
+
|
|
|
+uint8_t V6MiLightUdpServer::SEARCH_RESPONSE[] = {
|
|
|
+ 0x18, 0x00, 0x00, 0x00, 0x40, 0x02,
|
|
|
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // mac address
|
|
|
+ 0x00, 0x20, 0x39, 0x38, 0x35, 0x62,
|
|
|
+ 0x31, 0x35, 0x37, 0x62, 0x66, 0x36,
|
|
|
+ 0x66, 0x63, 0x34, 0x33, 0x33, 0x36,
|
|
|
+ 0x38, 0x61, 0x36, 0x33, 0x34, 0x36,
|
|
|
+ 0x37, 0x65, 0x61, 0x33, 0x62, 0x31,
|
|
|
+ 0x39, 0x64, 0x30, 0x64, 0x01, 0x00,
|
|
|
+ 0x01,
|
|
|
+ 0x17, 0x63, // this is 5987 in hex. specifying a different value seems to
|
|
|
+ // cause client to connect on a different port for some commands
|
|
|
+ 0x00, 0xFF,
|
|
|
+ 0x00, 0x00, 0x05, 0x00, 0x09, 0x78,
|
|
|
+ 0x6C, 0x69, 0x6E, 0x6B, 0x5F, 0x64,
|
|
|
+ 0x65, 0x76, 0x07, 0x5B, 0xCD, 0x15
|
|
|
+};
|
|
|
+
|
|
|
+uint8_t V6MiLightUdpServer::OPEN_COMMAND_RESPONSE[] = {
|
|
|
+ 0x80, 0x00, 0x00, 0x00, 0x15,
|
|
|
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // mac address
|
|
|
+ 0x05, 0x02, 0x00, 0x34, 0x00, 0x00,
|
|
|
+ 0x00, 0x00 ,0x00 ,0x00, 0x00, 0x00,
|
|
|
+ 0x00, 0x00, 0x34
|
|
|
+};
|
|
|
|
|
|
V6MiLightUdpServer::~V6MiLightUdpServer() {
|
|
|
V6Session* cur = firstSession;
|
|
|
@@ -88,6 +130,17 @@ uint16_t V6MiLightUdpServer::beginSession() {
|
|
|
return id;
|
|
|
}
|
|
|
|
|
|
+void V6MiLightUdpServer::handleSearch() {
|
|
|
+ const size_t packetLen = size(SEARCH_RESPONSE);
|
|
|
+ uint8_t response[packetLen];
|
|
|
+ memcpy(response, SEARCH_RESPONSE, packetLen);
|
|
|
+ WiFi.macAddress(response + 6);
|
|
|
+
|
|
|
+ socket.beginPacket(socket.remoteIP(), socket.remotePort());
|
|
|
+ socket.write(response, packetLen);
|
|
|
+ socket.endPacket();
|
|
|
+}
|
|
|
+
|
|
|
void V6MiLightUdpServer::handleStartSession() {
|
|
|
size_t len = size(START_SESSION_RESPONSE);
|
|
|
uint8_t response[len];
|
|
|
@@ -101,7 +154,7 @@ void V6MiLightUdpServer::handleStartSession() {
|
|
|
sendResponse(sessionId, response, len);
|
|
|
}
|
|
|
|
|
|
-void V6MiLightUdpServer::sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize) {
|
|
|
+bool V6MiLightUdpServer::sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize) {
|
|
|
V6Session* session = firstSession;
|
|
|
|
|
|
while (session != NULL) {
|
|
|
@@ -114,60 +167,27 @@ void V6MiLightUdpServer::sendResponse(uint16_t sessionId, uint8_t* responseBuffe
|
|
|
if (session == NULL || session->sessionId != sessionId) {
|
|
|
Serial.print("Received request with untracked session ID: ");
|
|
|
Serial.println(sessionId);
|
|
|
- return;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
#ifdef MILIGHT_UDP_DEBUG
|
|
|
- printf("Sending response to %s:%d\n", session->ipAddr.toString().c_str(), session->port);
|
|
|
+ printf_P("Sending response to %s:%d\n", session->ipAddr.toString().c_str(), session->port);
|
|
|
#endif
|
|
|
|
|
|
socket.beginPacket(session->ipAddr, session->port);
|
|
|
socket.write(responseBuffer, responseSize);
|
|
|
socket.endPacket();
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
-bool V6MiLightUdpServer::handleV1BulbCommand(uint8_t group, uint32_t _cmd, uint32_t _arg) {
|
|
|
- // Makes more sense to use V5 protocol for now.
|
|
|
-}
|
|
|
-
|
|
|
-bool V6MiLightUdpServer::handleV2BulbCommand(uint8_t group, uint32_t _cmd, uint32_t _arg) {
|
|
|
- const uint8_t cmd = _cmd & 0xFF;
|
|
|
- const uint8_t arg = _arg >> 24;
|
|
|
-
|
|
|
- client->prepare(MilightRgbCctConfig, deviceId, group);
|
|
|
-
|
|
|
- switch (cmd) {
|
|
|
- case V2_STATUS:
|
|
|
- if (arg == 0x01) {
|
|
|
- client->updateStatus(ON);
|
|
|
- } else if (arg == 0x02) {
|
|
|
- client->updateStatus(OFF);
|
|
|
- } else if (arg == 0x05) {
|
|
|
- client->updateBrightness(0);
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case V2_COLOR:
|
|
|
- client->updateColorRaw(arg);
|
|
|
- break;
|
|
|
-
|
|
|
- case V2_KELVIN:
|
|
|
- client->updateTemperature(arg);
|
|
|
- break;
|
|
|
-
|
|
|
- case V2_BRIGHTNESS:
|
|
|
- client->updateBrightness(arg);
|
|
|
- break;
|
|
|
-
|
|
|
- case V2_SATURATION:
|
|
|
- client->updateSaturation(100 - arg);
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- return false;
|
|
|
- }
|
|
|
+bool V6MiLightUdpServer::handleOpenCommand(uint16_t sessionId) {
|
|
|
+ size_t len = size(OPEN_COMMAND_RESPONSE);
|
|
|
+ uint8_t response[len];
|
|
|
+ memcpy(response, OPEN_COMMAND_RESPONSE, len);
|
|
|
+ WiFi.macAddress(response + 5);
|
|
|
|
|
|
- return true;
|
|
|
+ return sendResponse(sessionId, response, len);
|
|
|
}
|
|
|
|
|
|
void V6MiLightUdpServer::handleCommand(
|
|
|
@@ -183,15 +203,22 @@ void V6MiLightUdpServer::handleCommand(
|
|
|
uint32_t cmdArg = readInt<uint32_t>(cmd+5);
|
|
|
|
|
|
#ifdef MILIGHT_UDP_DEBUG
|
|
|
- printf("Command type: %02X, command: %08X, arg: %08X\n", cmdType, cmdHeader, cmdArg);
|
|
|
+ printf_P("Command cmdType: %02X, cmdHeader: %08X, cmdArg: %08X\n", cmdType, cmdHeader, cmdArg);
|
|
|
#endif
|
|
|
|
|
|
bool handled = false;
|
|
|
|
|
|
- if ((cmdHeader & 0x0800) == 0x0800) {
|
|
|
- handled = handleV2BulbCommand(group, cmdHeader, cmdArg);
|
|
|
- } else if ((cmdHeader & 0x0700) == 0x0700) {
|
|
|
- handled = handleV1BulbCommand(group, cmdHeader, cmdArg);
|
|
|
+ if (cmdHeader == 0) {
|
|
|
+ handled = handleOpenCommand(sessionId);
|
|
|
+ } else {
|
|
|
+ handled = COMMAND_DEMUXER.handleCommand(
|
|
|
+ client,
|
|
|
+ deviceId,
|
|
|
+ group,
|
|
|
+ cmdType,
|
|
|
+ cmdHeader,
|
|
|
+ cmdArg
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
if (handled) {
|
|
|
@@ -205,11 +232,11 @@ void V6MiLightUdpServer::handleCommand(
|
|
|
}
|
|
|
|
|
|
#ifdef MILIGHT_UDP_DEBUG
|
|
|
- printf("V6MiLightUdpServer - Unhandled command: ");
|
|
|
+ printf_P("V6MiLightUdpServer - Unhandled command: ");
|
|
|
for (size_t i = 0; i < V6_COMMAND_LEN; i++) {
|
|
|
- printf("%02X ", cmd[i]);
|
|
|
+ printf_P("%02X ", cmd[i]);
|
|
|
}
|
|
|
- printf("\n");
|
|
|
+ printf_P("\n");
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
@@ -221,18 +248,24 @@ void V6MiLightUdpServer::handleHeartbeat(uint16_t sessionId) {
|
|
|
|
|
|
sendResponse(sessionId, responseBuffer, 12);
|
|
|
}
|
|
|
+
|
|
|
+bool V6MiLightUdpServer::matchesPacket(uint8_t* packet1, size_t packet1Len, uint8_t* packet2, size_t packet2Len) {
|
|
|
+ return packet2Len >= packet1Len && memcmp(packet1, packet2, packet1Len) == 0;
|
|
|
+}
|
|
|
|
|
|
void V6MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize) {
|
|
|
#ifdef MILIGHT_UDP_DEBUG
|
|
|
- printf("Packet size: %d\n", packetSize);
|
|
|
+ printf_P("Packet size: %d\n", packetSize);
|
|
|
#endif
|
|
|
|
|
|
- if (packetSize >= size(START_SESSION_COMMAND) && memcmp(START_SESSION_COMMAND, packet, size(START_SESSION_COMMAND)) == 0) {
|
|
|
+ if (MATCHES_PACKET(START_SESSION_COMMAND)) {
|
|
|
handleStartSession();
|
|
|
- } else if (packetSize >= size(HEARTBEAT_HEADER) && memcmp(HEARTBEAT_HEADER, packet, size(HEARTBEAT_HEADER)) == 0) {
|
|
|
+ } else if (MATCHES_PACKET(HEARTBEAT_HEADER) || MATCHES_PACKET(HEARTBEAT_HEADER2)) {
|
|
|
uint16_t sessionId = readInt<uint16_t>(packet+5);
|
|
|
handleHeartbeat(sessionId);
|
|
|
- } else if (packetSize == 22 && memcmp(COMMAND_HEADER, packet, size(COMMAND_HEADER)) == 0) {
|
|
|
+ } else if (MATCHES_PACKET(SEARCH_COMMAND)) {
|
|
|
+ handleSearch();
|
|
|
+ } else if (packetSize == 22 && MATCHES_PACKET(COMMAND_HEADER)) {
|
|
|
uint16_t sessionId = readInt<uint16_t>(packet+5);
|
|
|
uint8_t sequenceNum = packet[8];
|
|
|
uint8_t* cmd = packet+10;
|
|
|
@@ -240,11 +273,11 @@ void V6MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize) {
|
|
|
uint8_t checksum = packet[21];
|
|
|
|
|
|
#ifdef MILIGHT_UDP_DEBUG
|
|
|
- printf("session: %04X, sequence: %d, group: %d, checksum: %d\n", sessionId, sequenceNum, group, checksum);
|
|
|
+ printf_P("session: %04X, sequence: %d, group: %d, checksum: %d\n", sessionId, sequenceNum, group, checksum);
|
|
|
#endif
|
|
|
|
|
|
handleCommand(sessionId, sequenceNum, cmd, group, checksum);
|
|
|
} else {
|
|
|
- Serial.println("Unhandled V6 packet");
|
|
|
+ Serial.println(F("Unhandled V6 packet"));
|
|
|
}
|
|
|
}
|