V6MiLightUdpServer.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef _V6_MILIGHT_UDP_SERVER
  10. #define _V6_MILIGHT_UDP_SERVER
  11. enum V2CommandIds {
  12. V2_COLOR = 0x01,
  13. V2_SATURATION = 0x02,
  14. V2_BRIGHTNESS = 0x03,
  15. V2_STATUS = 0x04,
  16. V2_KELVIN = 0x05
  17. };
  18. struct V6Session {
  19. V6Session(IPAddress ipAddr, uint16_t port, uint16_t sessionId)
  20. : ipAddr(ipAddr),
  21. port(port),
  22. sessionId(sessionId),
  23. next(NULL)
  24. { }
  25. IPAddress ipAddr;
  26. uint16_t port;
  27. uint16_t sessionId;
  28. V6Session* next;
  29. };
  30. class V6MiLightUdpServer : public MiLightUdpServer {
  31. public:
  32. V6MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
  33. : MiLightUdpServer(client, port, deviceId),
  34. sessionId(0),
  35. firstSession(NULL)
  36. { }
  37. ~V6MiLightUdpServer();
  38. // Should return size of the response packet
  39. virtual void handlePacket(uint8_t* packet, size_t packetSize);
  40. template <typename T>
  41. static T readInt(uint8_t* packet);
  42. template <typename T>
  43. static uint8_t* writeInt(const T& value, uint8_t* packet);
  44. protected:
  45. static uint8_t START_SESSION_COMMAND[];
  46. static uint8_t START_SESSION_RESPONSE[];
  47. static uint8_t COMMAND_HEADER[];
  48. static uint8_t COMMAND_RESPONSE[];
  49. V6Session* firstSession;
  50. uint16_t sessionId;
  51. uint16_t beginSession();
  52. void sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize);
  53. void handleStartSession();
  54. void handleCommand(
  55. uint16_t sessionId,
  56. uint8_t sequenceNum,
  57. uint8_t* cmd,
  58. uint8_t group,
  59. uint8_t checksum
  60. );
  61. bool handleV1BulbCommand(
  62. uint8_t group,
  63. uint32_t cmd,
  64. uint32_t cmdArg
  65. );
  66. bool handleV2BulbCommand(
  67. uint8_t group,
  68. uint32_t cmd,
  69. uint32_t cmdArg
  70. );
  71. };
  72. #endif