V6MiLightUdpServer.cpp 5.3 KB

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