V6MiLightUdpServer.cpp 5.8 KB

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