V6MiLightUdpServer.cpp 5.5 KB

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