V6MiLightUdpServer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <V6MiLightUdpServer.h>
  2. #include <ESP8266WiFi.h>
  3. #include <Arduino.h>
  4. uint8_t V6MiLightUdpServer::START_SESSION_COMMAND[] = {
  5. 0x20, 0x00, 0x00, 0x00, 0x16, 0x02, 0x62, 0x3A, 0xD5, 0xED, 0xA3, 0x01, 0xAE,
  6. 0x08, 0x2D, 0x46, 0x61, 0x41, 0xA7, 0xF6, 0xDC, 0xAF
  7. };
  8. uint8_t V6MiLightUdpServer::START_SESSION_RESPONSE[] = {
  9. 0x28, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02,
  10. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // should be replaced with hw addr
  11. 0x69, 0xF0, 0x3C, 0x23, 0x00, 0x01,
  12. 0xFF, 0xFF, // should be replaced with a session ID
  13. 0x00
  14. };
  15. uint8_t V6MiLightUdpServer::COMMAND_HEADER[] = {
  16. 0x80, 0x00, 0x00, 0x00
  17. };
  18. uint8_t V6MiLightUdpServer::HEARTBEAT_HEADER[] = {
  19. 0xD0, 0x00, 0x00, 0x00, 0x02
  20. };
  21. uint8_t V6MiLightUdpServer::COMMAND_RESPONSE[] = {
  22. 0x88, 0x00, 0x00, 0x00, 0x03, 0x00, 0xFF, 0x00
  23. };
  24. template<typename T, size_t sz>
  25. size_t size(T(&)[sz]) {
  26. return sz;
  27. }
  28. V6MiLightUdpServer::~V6MiLightUdpServer() {
  29. V6Session* cur = firstSession;
  30. while (cur != NULL) {
  31. V6Session* next = cur->next;
  32. delete cur;
  33. cur = next;
  34. }
  35. }
  36. template <typename T>
  37. T V6MiLightUdpServer::readInt(uint8_t* packet) {
  38. size_t numBytes = sizeof(T);
  39. T value = 0;
  40. for (size_t i = 0; i < numBytes; i++) {
  41. value |= packet[i] << (8 * (numBytes - i - 1));
  42. }
  43. return value;
  44. }
  45. template <typename T>
  46. uint8_t* V6MiLightUdpServer::writeInt(const T& value, uint8_t* packet) {
  47. size_t numBytes = sizeof(T);
  48. for (size_t i = 0; i < numBytes; i++) {
  49. packet[i] = (value >> (8 * (numBytes - i - 1))) & 0xFF;
  50. }
  51. return packet + numBytes;
  52. }
  53. uint16_t V6MiLightUdpServer::beginSession() {
  54. const uint16_t id = sessionId++;
  55. V6Session* session = new V6Session(socket.remoteIP(), socket.remotePort(), id);
  56. session->next = firstSession;
  57. firstSession = session;
  58. if (numSessions >= V6_MAX_SESSIONS) {
  59. V6Session* cur = firstSession;
  60. for (size_t i = 1; i < V6_MAX_SESSIONS; i++) {
  61. cur = cur->next;
  62. }
  63. delete cur->next;
  64. cur->next = NULL;
  65. } else {
  66. numSessions++;
  67. }
  68. return id;
  69. }
  70. void V6MiLightUdpServer::handleStartSession() {
  71. size_t len = size(START_SESSION_RESPONSE);
  72. uint8_t response[len];
  73. uint16_t sessionId = beginSession();
  74. memcpy(response, START_SESSION_RESPONSE, len);
  75. WiFi.macAddress(response + 7);
  76. response[19] = sessionId >> 8;
  77. response[20] = sessionId & 0xFF;
  78. sendResponse(sessionId, response, len);
  79. }
  80. void V6MiLightUdpServer::sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize) {
  81. V6Session* session = firstSession;
  82. while (session != NULL) {
  83. if (session->sessionId == sessionId) {
  84. break;
  85. }
  86. session = session->next;
  87. }
  88. if (session == NULL || session->sessionId != sessionId) {
  89. Serial.print("Received request with untracked session ID: ");
  90. Serial.println(sessionId);
  91. return;
  92. }
  93. #ifdef MILIGHT_UDP_DEBUG
  94. printf("Sending response to %s:%d\n", session->ipAddr.toString().c_str(), session->port);
  95. #endif
  96. socket.beginPacket(session->ipAddr, session->port);
  97. socket.write(responseBuffer, responseSize);
  98. socket.endPacket();
  99. }
  100. bool V6MiLightUdpServer::handleV1BulbCommand(uint8_t group, uint32_t _cmd, uint32_t _arg) {
  101. }
  102. bool V6MiLightUdpServer::handleV2BulbCommand(uint8_t group, uint32_t _cmd, uint32_t _arg) {
  103. const uint8_t cmd = _cmd & 0xFF;
  104. const uint8_t arg = _arg >> 24;
  105. client->prepare(MilightRgbCctConfig, deviceId, group);
  106. switch (cmd) {
  107. case V2_STATUS:
  108. if (arg == 0x01) {
  109. client->updateStatus(ON);
  110. } else if (arg == 0x02) {
  111. client->updateStatus(OFF);
  112. } else if (arg == 0x05) {
  113. client->updateBrightness(0);
  114. }
  115. break;
  116. case V2_COLOR:
  117. client->updateColorRaw(arg);
  118. break;
  119. case V2_KELVIN:
  120. client->updateTemperature(arg);
  121. break;
  122. case V2_BRIGHTNESS:
  123. client->updateBrightness(arg);
  124. break;
  125. case V2_SATURATION:
  126. client->updateSaturation(arg);
  127. break;
  128. default:
  129. return false;
  130. }
  131. return true;
  132. }
  133. void V6MiLightUdpServer::handleCommand(
  134. uint16_t sessionId,
  135. uint8_t sequenceNum,
  136. uint8_t* cmd,
  137. uint8_t group,
  138. uint8_t checksum
  139. ) {
  140. uint8_t cmdType = readInt<uint8_t>(cmd);
  141. uint32_t cmdHeader = readInt<uint32_t>(cmd+1);
  142. uint32_t cmdArg = readInt<uint32_t>(cmd+5);
  143. #ifdef MILIGHT_UDP_DEBUG
  144. printf("Command type: %02X, command: %08X, arg: %08X\n", cmdType, cmdHeader, cmdArg);
  145. #endif
  146. bool handled = false;
  147. if ((cmdHeader & 0x0800) == 0x0800) {
  148. handled = handleV2BulbCommand(group, cmdHeader, cmdArg);
  149. } else if ((cmdHeader & 0x0700) == 0x0700) {
  150. handled = handleV1BulbCommand(group, cmdHeader, cmdArg);
  151. }
  152. if (handled) {
  153. size_t len = size(COMMAND_RESPONSE);
  154. memcpy(responseBuffer, COMMAND_RESPONSE, len);
  155. responseBuffer[6] = sequenceNum;
  156. sendResponse(sessionId, responseBuffer, len);
  157. return;
  158. }
  159. #ifdef MILIGHT_UDP_DEBUG
  160. printf("V6MiLightUdpServer - Unhandled command: ");
  161. for (size_t i = 0; i < V6_COMMAND_LEN; i++) {
  162. printf("%02X ", cmd[i]);
  163. }
  164. printf("\n");
  165. #endif
  166. }
  167. void V6MiLightUdpServer::handleHeartbeat(uint16_t sessionId) {
  168. char header[] = { 0xD8, 0x00, 0x00, 0x00, 0x07 };
  169. memcpy(responseBuffer, header, size(header));
  170. WiFi.macAddress(responseBuffer+5);
  171. responseBuffer[11] = 0;
  172. sendResponse(sessionId, responseBuffer, 12);
  173. }
  174. void V6MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize) {
  175. printf("Packet size: %d\n", packetSize);
  176. if (packetSize >= size(START_SESSION_COMMAND) && memcmp(START_SESSION_COMMAND, packet, size(START_SESSION_COMMAND)) == 0) {
  177. handleStartSession();
  178. } else if (packetSize >= size(HEARTBEAT_HEADER) && memcmp(HEARTBEAT_HEADER, packet, size(HEARTBEAT_HEADER)) == 0) {
  179. uint16_t sessionId = readInt<uint16_t>(packet+5);
  180. handleHeartbeat(sessionId);
  181. } else if (packetSize == 22 && memcmp(COMMAND_HEADER, packet, size(COMMAND_HEADER)) == 0) {
  182. uint16_t sessionId = readInt<uint16_t>(packet+5);
  183. uint8_t sequenceNum = packet[8];
  184. uint8_t* cmd = packet+10;
  185. uint8_t group = packet[19];
  186. uint8_t checksum = packet[21];
  187. #ifdef MILIGHT_UDP_DEBUG
  188. printf("session: %04X, sequence: %d, group: %d, checksum: %d\n", sessionId, sequenceNum, group, checksum);
  189. #endif
  190. handleCommand(sessionId, sequenceNum, cmd, group, checksum);
  191. } else {
  192. Serial.println("Unhandled V6 packet");
  193. }
  194. }