V6MiLightUdpServer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // This protocol is documented here:
  2. // http://www.limitlessled.com/dev/
  3. #include <Arduino.h>
  4. #include <MiLightClient.h>
  5. #include <WiFiUdp.h>
  6. #include <MiLightUdpServer.h>
  7. #include <Vector.h>
  8. #define V6_COMMAND_LEN 8
  9. #define V6_MAX_SESSIONS 10
  10. #ifndef _V6_MILIGHT_UDP_SERVER
  11. #define _V6_MILIGHT_UDP_SERVER
  12. enum V2CommandIds {
  13. V2_COLOR = 0x01,
  14. V2_SATURATION = 0x02,
  15. V2_BRIGHTNESS = 0x03,
  16. V2_STATUS = 0x04,
  17. V2_KELVIN = 0x05
  18. };
  19. enum RgbCommandIds {
  20. V2_RGB_COMMAND_PREFIX = 0x02,
  21. V2_RGB_COLOR_PREFIX = 0x01,
  22. V2_RGB_BRIGHTNESS_DOWN = 0x01,
  23. V2_RGB_BRIGHTNESS_UP = 0x02,
  24. V2_RGB_SPEED_DOWN = 0x03,
  25. V2_RGB_SPEED_UP = 0x04,
  26. V2_RGB_MODE_DOWN = 0x05,
  27. V2_RGB_MODE_UP = 0x06,
  28. V2_RGB_ON = 0x09,
  29. V2_RGB_OFF = 0x0A
  30. };
  31. struct V6Session {
  32. V6Session(IPAddress ipAddr, uint16_t port, uint16_t sessionId)
  33. : ipAddr(ipAddr),
  34. port(port),
  35. sessionId(sessionId),
  36. next(NULL)
  37. { }
  38. IPAddress ipAddr;
  39. uint16_t port;
  40. uint16_t sessionId;
  41. V6Session* next;
  42. };
  43. class V6MiLightUdpServer : public MiLightUdpServer {
  44. public:
  45. V6MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
  46. : MiLightUdpServer(client, port, deviceId),
  47. sessionId(0),
  48. numSessions(0),
  49. firstSession(NULL)
  50. { }
  51. ~V6MiLightUdpServer();
  52. // Should return size of the response packet
  53. virtual void handlePacket(uint8_t* packet, size_t packetSize);
  54. template <typename T>
  55. static T readInt(uint8_t* packet);
  56. template <typename T>
  57. static uint8_t* writeInt(const T& value, uint8_t* packet);
  58. protected:
  59. static uint8_t START_SESSION_COMMAND[] PROGMEM;
  60. static uint8_t START_SESSION_RESPONSE[] PROGMEM;
  61. static uint8_t COMMAND_HEADER[] PROGMEM;
  62. static uint8_t COMMAND_RESPONSE[] PROGMEM;
  63. static uint8_t LOCAL_SEARCH_COMMAND[] PROGMEM;
  64. static uint8_t HEARTBEAT_HEADER[] PROGMEM;
  65. static uint8_t HEARTBEAT_HEADER2[] PROGMEM;
  66. static uint8_t SEARCH_COMMAND[] PROGMEM;
  67. static uint8_t SEARCH_RESPONSE[] PROGMEM;
  68. static uint8_t OPEN_COMMAND_RESPONSE[] PROGMEM;
  69. V6Session* firstSession;
  70. size_t numSessions;
  71. uint16_t sessionId;
  72. uint16_t beginSession();
  73. bool sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize);
  74. bool matchesPacket(uint8_t* packet1, size_t packet1Len, uint8_t* packet2, size_t packet2Len);
  75. void handleSearch();
  76. void handleStartSession();
  77. bool handleOpenCommand(uint16_t sessionId);
  78. void handleHeartbeat(uint16_t sessionId);
  79. void handleCommand(
  80. uint16_t sessionId,
  81. uint8_t sequenceNum,
  82. uint8_t* cmd,
  83. uint8_t group,
  84. uint8_t checksum
  85. );
  86. bool handleRgbBulbCommand(
  87. uint8_t group,
  88. uint32_t cmd,
  89. uint32_t cmdArg
  90. );
  91. bool handleV2BulbCommand(
  92. uint8_t group,
  93. uint32_t cmd,
  94. uint32_t cmdArg
  95. );
  96. };
  97. #endif