V6MiLightUdpServer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. struct V6Session {
  20. V6Session(IPAddress ipAddr, uint16_t port, uint16_t sessionId)
  21. : ipAddr(ipAddr),
  22. port(port),
  23. sessionId(sessionId),
  24. next(NULL)
  25. { }
  26. IPAddress ipAddr;
  27. uint16_t port;
  28. uint16_t sessionId;
  29. V6Session* next;
  30. };
  31. class V6MiLightUdpServer : public MiLightUdpServer {
  32. public:
  33. V6MiLightUdpServer(MiLightClient*& client, uint16_t port, uint16_t deviceId)
  34. : MiLightUdpServer(client, port, deviceId),
  35. sessionId(0),
  36. numSessions(0),
  37. firstSession(NULL)
  38. { }
  39. ~V6MiLightUdpServer();
  40. // Should return size of the response packet
  41. virtual void handlePacket(uint8_t* packet, size_t packetSize);
  42. template <typename T>
  43. static T readInt(uint8_t* packet);
  44. template <typename T>
  45. static uint8_t* writeInt(const T& value, uint8_t* packet);
  46. protected:
  47. static uint8_t START_SESSION_COMMAND[];
  48. static uint8_t START_SESSION_RESPONSE[];
  49. static uint8_t COMMAND_HEADER[];
  50. static uint8_t COMMAND_RESPONSE[];
  51. static uint8_t SEARCH_COMMAND[];
  52. static uint8_t LOCAL_SEARCH_COMMAND[];
  53. V6Session* firstSession;
  54. size_t numSessions;
  55. uint16_t sessionId;
  56. uint16_t beginSession();
  57. void sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize);
  58. void handleStartSession();
  59. void handleCommand(
  60. uint16_t sessionId,
  61. uint8_t sequenceNum,
  62. uint8_t* cmd,
  63. uint8_t group,
  64. uint8_t checksum
  65. );
  66. bool handleV1BulbCommand(
  67. uint8_t group,
  68. uint32_t cmd,
  69. uint32_t cmdArg
  70. );
  71. bool handleV2BulbCommand(
  72. uint8_t group,
  73. uint32_t cmd,
  74. uint32_t cmdArg
  75. );
  76. };
  77. #endif