V6MiLightUdpServer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. static uint8_t HEARTBEAT_HEADER[];
  54. V6Session* firstSession;
  55. size_t numSessions;
  56. uint16_t sessionId;
  57. uint16_t beginSession();
  58. void sendResponse(uint16_t sessionId, uint8_t* responseBuffer, size_t responseSize);
  59. void handleStartSession();
  60. void handleHeartbeat(uint16_t sessionId);
  61. void handleCommand(
  62. uint16_t sessionId,
  63. uint8_t sequenceNum,
  64. uint8_t* cmd,
  65. uint8_t group,
  66. uint8_t checksum
  67. );
  68. bool handleV1BulbCommand(
  69. uint8_t group,
  70. uint32_t cmd,
  71. uint32_t cmdArg
  72. );
  73. bool handleV2BulbCommand(
  74. uint8_t group,
  75. uint32_t cmd,
  76. uint32_t cmdArg
  77. );
  78. };
  79. #endif