V6MiLightUdpServer.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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::handleRgbBulbCommand(uint8_t group, uint32_t _cmd, uint32_t _arg) {
  101. const uint8_t cmd = _cmd & 0xFF;
  102. const uint8_t arg = _arg >> 24;
  103. client->prepare(MilightRgbConfig, deviceId, 0);
  104. if (cmd == V2_RGB_COMMAND_PREFIX) {
  105. switch (arg) {
  106. case V2_RGB_ON:
  107. client->updateStatus(ON);
  108. break;
  109. case V2_RGB_OFF:
  110. client->updateStatus(OFF);
  111. break;
  112. case V2_RGB_BRIGHTNESS_DOWN:
  113. client->decreaseBrightness();
  114. break;
  115. case V2_RGB_BRIGHTNESS_UP:
  116. client->increaseBrightness();
  117. break;
  118. case V2_RGB_MODE_DOWN:
  119. client->previousMode();
  120. break;
  121. case V2_RGB_MODE_UP:
  122. client->nextMode();
  123. break;
  124. case V2_RGB_SPEED_DOWN:
  125. client->modeSpeedDown();
  126. break;
  127. case V2_RGB_SPEED_UP:
  128. client->modeSpeedUp();
  129. break;
  130. }
  131. } else if (cmd == V2_RGB_COLOR_PREFIX) {
  132. client->updateColorRaw(arg);
  133. }
  134. }
  135. bool V6MiLightUdpServer::handleV2BulbCommand(uint8_t group, uint32_t _cmd, uint32_t _arg) {
  136. const uint8_t cmd = _cmd & 0xFF;
  137. const uint8_t arg = _arg >> 24;
  138. client->prepare(MilightRgbCctConfig, deviceId, group);
  139. switch (cmd) {
  140. case V2_STATUS:
  141. if (arg == 0x01) {
  142. client->updateStatus(ON);
  143. } else if (arg == 0x02) {
  144. client->updateStatus(OFF);
  145. } else if (arg == 0x05) {
  146. client->updateBrightness(0);
  147. }
  148. break;
  149. case V2_COLOR:
  150. client->updateColorRaw(arg);
  151. break;
  152. case V2_KELVIN:
  153. client->updateTemperature(arg);
  154. break;
  155. case V2_BRIGHTNESS:
  156. client->updateBrightness(arg);
  157. break;
  158. case V2_SATURATION:
  159. client->updateSaturation(100 - arg);
  160. break;
  161. default:
  162. return false;
  163. }
  164. return true;
  165. }
  166. void V6MiLightUdpServer::handleCommand(
  167. uint16_t sessionId,
  168. uint8_t sequenceNum,
  169. uint8_t* cmd,
  170. uint8_t group,
  171. uint8_t checksum
  172. ) {
  173. uint8_t cmdType = readInt<uint8_t>(cmd);
  174. uint32_t cmdHeader = readInt<uint32_t>(cmd+1);
  175. uint32_t cmdArg = readInt<uint32_t>(cmd+5);
  176. #ifdef MILIGHT_UDP_DEBUG
  177. printf("Command type: %02X, command: %08X, arg: %08X\n", cmdType, cmdHeader, cmdArg);
  178. #endif
  179. bool handled = false;
  180. if ((cmdHeader & 0x0800) == 0x0800) {
  181. handled = handleV2BulbCommand(group, cmdHeader, cmdArg);
  182. } else if ((cmdHeader & 0x0500) == 0x0500) {
  183. handled = handleRgbBulbCommand(group, cmdHeader, cmdArg);
  184. }
  185. if (handled) {
  186. size_t len = size(COMMAND_RESPONSE);
  187. memcpy(responseBuffer, COMMAND_RESPONSE, len);
  188. responseBuffer[6] = sequenceNum;
  189. sendResponse(sessionId, responseBuffer, len);
  190. return;
  191. }
  192. #ifdef MILIGHT_UDP_DEBUG
  193. printf("V6MiLightUdpServer - Unhandled command: ");
  194. for (size_t i = 0; i < V6_COMMAND_LEN; i++) {
  195. printf("%02X ", cmd[i]);
  196. }
  197. printf("\n");
  198. #endif
  199. }
  200. void V6MiLightUdpServer::handleHeartbeat(uint16_t sessionId) {
  201. char header[] = { 0xD8, 0x00, 0x00, 0x00, 0x07 };
  202. memcpy(responseBuffer, header, size(header));
  203. WiFi.macAddress(responseBuffer+5);
  204. responseBuffer[11] = 0;
  205. sendResponse(sessionId, responseBuffer, 12);
  206. }
  207. void V6MiLightUdpServer::handlePacket(uint8_t* packet, size_t packetSize) {
  208. #ifdef MILIGHT_UDP_DEBUG
  209. printf("Packet size: %d\n", packetSize);
  210. #endif
  211. if (packetSize >= size(START_SESSION_COMMAND) && memcmp(START_SESSION_COMMAND, packet, size(START_SESSION_COMMAND)) == 0) {
  212. handleStartSession();
  213. } else if (packetSize >= size(HEARTBEAT_HEADER) && memcmp(HEARTBEAT_HEADER, packet, size(HEARTBEAT_HEADER)) == 0) {
  214. uint16_t sessionId = readInt<uint16_t>(packet+5);
  215. handleHeartbeat(sessionId);
  216. } else if (packetSize == 22 && memcmp(COMMAND_HEADER, packet, size(COMMAND_HEADER)) == 0) {
  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. }