V6MiLightUdpServer.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <V6MiLightUdpServer.h>
  2. #include <ESP8266WiFi.h>
  3. #include <Arduino.h>
  4. #include <Size.h>
  5. #include <V6CommandHandler.h>
  6. #define MATCHES_PACKET(packet1) ( \
  7. matchesPacket(packet1, size(packet1), packet, packetSize) \
  8. )
  9. V6CommandDemuxer* V6MiLightUdpServer::COMMAND_DEMUXER = new V6CommandDemuxer(
  10. V6CommandHandler::ALL_HANDLERS,
  11. V6CommandHandler::NUM_HANDLERS
  12. );
  13. uint8_t V6MiLightUdpServer::START_SESSION_COMMAND[] = {
  14. 0x20, 0x00, 0x00, 0x00, 0x16, 0x02, 0x62, 0x3A, 0xD5, 0xED, 0xA3, 0x01, 0xAE,
  15. 0x08, 0x2D, 0x46, 0x61, 0x41, 0xA7, 0xF6, 0xDC, 0xAF
  16. };
  17. uint8_t V6MiLightUdpServer::START_SESSION_RESPONSE[] = {
  18. 0x28, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02,
  19. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // should be replaced with hw addr
  20. 0x69, 0xF0, 0x3C, 0x23, 0x00, 0x01,
  21. 0xFF, 0xFF, // should be replaced with a session ID
  22. 0x00
  23. };
  24. uint8_t V6MiLightUdpServer::COMMAND_HEADER[] = {
  25. 0x80, 0x00, 0x00, 0x00
  26. };
  27. uint8_t V6MiLightUdpServer::HEARTBEAT_HEADER[] = {
  28. 0xD0, 0x00, 0x00, 0x00, 0x02
  29. };
  30. uint8_t V6MiLightUdpServer::HEARTBEAT_HEADER2[] = {
  31. 0x30, 0x00, 0x00, 0x00, 0x03
  32. };
  33. uint8_t V6MiLightUdpServer::COMMAND_RESPONSE[] = {
  34. 0x88, 0x00, 0x00, 0x00, 0x03, 0x00, 0xFF, 0x00
  35. };
  36. uint8_t V6MiLightUdpServer::SEARCH_COMMAND[] = {
  37. 0x10, 0x00, 0x00, 0x00
  38. //, 0x24, 0x02
  39. //, 0xAE, 0x65, 0x02, 0x39, 0x38, 0x35, 0x62
  40. };
  41. uint8_t V6MiLightUdpServer::SEARCH_RESPONSE[] = {
  42. 0x18, 0x00, 0x00, 0x00, 0x40, 0x02,
  43. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // mac address
  44. 0x00, 0x20, 0x39, 0x38, 0x35, 0x62,
  45. 0x31, 0x35, 0x37, 0x62, 0x66, 0x36,
  46. 0x66, 0x63, 0x34, 0x33, 0x33, 0x36,
  47. 0x38, 0x61, 0x36, 0x33, 0x34, 0x36,
  48. 0x37, 0x65, 0x61, 0x33, 0x62, 0x31,
  49. 0x39, 0x64, 0x30, 0x64, 0x01, 0x00,
  50. 0x01,
  51. 0x17, 0x63, // this is 5987 in hex. specifying a different value seems to
  52. // cause client to connect on a different port for some commands
  53. 0x00, 0xFF,
  54. 0x00, 0x00, 0x05, 0x00, 0x09, 0x78,
  55. 0x6C, 0x69, 0x6E, 0x6B, 0x5F, 0x64,
  56. 0x65, 0x76, 0x07, 0x5B, 0xCD, 0x15
  57. };
  58. uint8_t V6MiLightUdpServer::OPEN_COMMAND_RESPONSE[] = {
  59. 0x80, 0x00, 0x00, 0x00, 0x15,
  60. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // mac address
  61. 0x05, 0x02, 0x00, 0x34, 0x00, 0x00,
  62. 0x00, 0x00 ,0x00 ,0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x34
  64. };
  65. V6MiLightUdpServer::~V6MiLightUdpServer() {
  66. V6Session* cur = firstSession;
  67. while (cur != NULL) {
  68. V6Session* next = cur->next;
  69. delete cur;
  70. cur = next;
  71. }
  72. }
  73. template <typename T>
  74. T V6MiLightUdpServer::readInt(uint8_t* packet) {
  75. size_t numBytes = sizeof(T);
  76. T value = 0;
  77. for (size_t i = 0; i < numBytes; i++) {
  78. value |= packet[i] << (8 * (numBytes - i - 1));
  79. }
  80. return value;
  81. }
  82. template <typename T>
  83. uint8_t* V6MiLightUdpServer::writeInt(const T& value, uint8_t* packet) {
  84. size_t numBytes = sizeof(T);
  85. for (size_t i = 0; i < numBytes; i++) {
  86. packet[i] = (value >> (8 * (numBytes - i - 1))) & 0xFF;
  87. }
  88. return packet + numBytes;
  89. }
  90. uint16_t V6MiLightUdpServer::beginSession() {
  91. const uint16_t id = sessionId++;
  92. V6Session* session = new V6Session(socket.remoteIP(), socket.remotePort(), id);
  93. session->next = firstSession;
  94. firstSession = session;
  95. if (numSessions >= V6_MAX_SESSIONS) {
  96. V6Session* cur = firstSession;
  97. for (size_t i = 1; i < V6_MAX_SESSIONS; i++) {
  98. cur = cur->next;
  99. }
  100. delete cur->next;
  101. cur->next = NULL;
  102. } else {
  103. numSessions++;
  104. }
  105. return id;
  106. }
  107. void V6MiLightUdpServer::handleSearch() {
  108. const size_t packetLen = size(SEARCH_RESPONSE);
  109. uint8_t response[packetLen];
  110. memcpy(response, SEARCH_RESPONSE, packetLen);
  111. WiFi.macAddress(response + 6);
  112. socket.beginPacket(socket.remoteIP(), socket.remotePort());
  113. socket.write(response, packetLen);
  114. socket.endPacket();
  115. }
  116. void V6MiLightUdpServer::handleStartSession() {
  117. size_t len = size(START_SESSION_RESPONSE);
  118. uint8_t response[len];
  119. uint16_t sessionId = beginSession();
  120. memcpy(response, START_SESSION_RESPONSE, len);
  121. WiFi.macAddress(response + 7);
  122. response[19] = sessionId >> 8;
  123. response[20] = sessionId & 0xFF;
  124. sendResponse(sessionId, response, len);
  125. }
  126. bool V6MiLightUdpServer::sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize) {
  127. V6Session* session = firstSession;
  128. while (session != NULL) {
  129. if (session->sessionId == sessionId) {
  130. break;
  131. }
  132. session = session->next;
  133. }
  134. if (session == NULL || session->sessionId != sessionId) {
  135. Serial.print("Received request with untracked session ID: ");
  136. Serial.println(sessionId);
  137. return false;
  138. }
  139. #ifdef MILIGHT_UDP_DEBUG
  140. printf("Sending response to %s:%d\n", session->ipAddr.toString().c_str(), session->port);
  141. #endif
  142. socket.beginPacket(session->ipAddr, session->port);
  143. socket.write(responseBuffer, responseSize);
  144. socket.endPacket();
  145. return true;
  146. }
  147. bool V6MiLightUdpServer::handleOpenCommand(uint16_t sessionId) {
  148. size_t len = size(OPEN_COMMAND_RESPONSE);
  149. uint8_t response[len];
  150. memcpy(response, OPEN_COMMAND_RESPONSE, len);
  151. WiFi.macAddress(response + 5);
  152. return sendResponse(sessionId, response, len);
  153. }
  154. void V6MiLightUdpServer::handleCommand(
  155. uint16_t sessionId,
  156. uint8_t sequenceNum,
  157. uint8_t* cmd,
  158. uint8_t group,
  159. uint8_t checksum
  160. ) {
  161. uint8_t cmdType = readInt<uint8_t>(cmd);
  162. uint32_t cmdHeader = readInt<uint32_t>(cmd+1);
  163. uint32_t cmdArg = readInt<uint32_t>(cmd+5);
  164. #ifdef MILIGHT_UDP_DEBUG
  165. printf("Command cmdType: %02X, cmdHeader: %08X, cmdArg: %08X\n", cmdType, cmdHeader, cmdArg);
  166. #endif
  167. bool handled = false;
  168. if (cmdHeader == 0) {
  169. handled = handleOpenCommand(sessionId);
  170. } else {
  171. handled = COMMAND_DEMUXER->handleCommand(
  172. client,
  173. deviceId,
  174. group,
  175. cmdType,
  176. cmdHeader,
  177. cmdArg
  178. );
  179. }
  180. if (handled) {
  181. size_t len = size(COMMAND_RESPONSE);
  182. memcpy(responseBuffer, COMMAND_RESPONSE, len);
  183. responseBuffer[6] = sequenceNum;
  184. sendResponse(sessionId, responseBuffer, len);
  185. return;
  186. }
  187. #ifdef MILIGHT_UDP_DEBUG
  188. printf("V6MiLightUdpServer - Unhandled command: ");
  189. for (size_t i = 0; i < V6_COMMAND_LEN; i++) {
  190. printf("%02X ", cmd[i]);
  191. }
  192. printf("\n");
  193. #endif
  194. }
  195. void V6MiLightUdpServer::handleHeartbeat(uint16_t sessionId) {
  196. char header[] = { 0xD8, 0x00, 0x00, 0x00, 0x07 };
  197. memcpy(responseBuffer, header, size(header));
  198. WiFi.macAddress(responseBuffer+5);
  199. responseBuffer[11] = 0;
  200. sendResponse(sessionId, responseBuffer, 12);
  201. }
  202. bool V6MiLightUdpServer::matchesPacket(uint8_t* packet1, size_t packet1Len, uint8_t* packet2, size_t packet2Len) {
  203. return packet2Len >= packet1Len && memcmp(packet1, packet2, packet1Len) == 0;
  204. }
  205. void V6MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize) {
  206. #ifdef MILIGHT_UDP_DEBUG
  207. printf("Packet size: %d\n", packetSize);
  208. #endif
  209. if (MATCHES_PACKET(START_SESSION_COMMAND)) {
  210. handleStartSession();
  211. } else if (MATCHES_PACKET(HEARTBEAT_HEADER) || MATCHES_PACKET(HEARTBEAT_HEADER2)) {
  212. uint16_t sessionId = readInt<uint16_t>(packet+5);
  213. handleHeartbeat(sessionId);
  214. } else if (MATCHES_PACKET(SEARCH_COMMAND)) {
  215. handleSearch();
  216. } else if (packetSize == 22 && MATCHES_PACKET(COMMAND_HEADER)) {
  217. uint16_t sessionId = readInt<uint16_t>(packet+5);
  218. uint8_t sequenceNum = packet[8];
  219. uint8_t* cmd = packet+10;
  220. uint8_t group = packet[19];
  221. uint8_t checksum = packet[21];
  222. #ifdef MILIGHT_UDP_DEBUG
  223. printf("session: %04X, sequence: %d, group: %d, checksum: %d\n", sessionId, sequenceNum, group, checksum);
  224. #endif
  225. handleCommand(sessionId, sequenceNum, cmd, group, checksum);
  226. } else {
  227. Serial.println("Unhandled V6 packet");
  228. }
  229. }