瀏覽代碼

Bufix checksum in CCT packet. (#182)

* Bugfix checksum in CCT packet.

Add checksum of the 7 packet bytes (including packet length = 7).
Without it, several CCT bulbs cannot be linked and do not respond
on commands. The CctPacketFormatter.cpp protocol is now identical
to the FUT006 remote CCT controller.
Erriez 8 年之前
父節點
當前提交
3b9c3cbd62
共有 2 個文件被更改,包括 31 次插入1 次删除
  1. 30 1
      lib/MiLight/CctPacketFormatter.cpp
  2. 1 0
      lib/MiLight/CctPacketFormatter.h

+ 30 - 1
lib/MiLight/CctPacketFormatter.cpp

@@ -9,13 +9,42 @@ bool CctPacketFormatter::canHandle(const uint8_t *packet, const size_t len) {
 void CctPacketFormatter::initializePacket(uint8_t* packet) {
   size_t packetPtr = 0;
 
+  // Byte 0: Packet length = 7 bytes
+
+  // Byte 1: CCT protocol
   packet[packetPtr++] = CCT_PROTOCOL_ID;
+
+  // Byte 2 and 3: Device ID
   packet[packetPtr++] = deviceId >> 8;
   packet[packetPtr++] = deviceId & 0xFF;
+
+  // Byte 4: Zone
   packet[packetPtr++] = groupId;
+
+  // Byte 5: Bulb command, filled in later
   packet[packetPtr++] = 0;
-  packet[packetPtr++] = sequenceNum;
+
+  // Byte 6: Packet sequence number 0..255
   packet[packetPtr++] = sequenceNum++;
+
+  // Byte 7: Checksum over previous bytes, including packet length = 7
+  // The checksum will be calculated when setting the command field
+  packet[packetPtr++] = 0;
+
+  // Byte 8: CRC LSB
+  // Byte 9: CRC MSB
+}
+
+void CctPacketFormatter::finalizePacket(uint8_t* packet) {
+  uint8_t checksum;
+
+  // Calculate checksum over packet length .. sequenceNum
+  checksum = 7; // Packet length is not part of packet
+  for (uint8_t i = 0; i < 6; i++) {
+    checksum += currentPacket[i];
+  }
+  // Store the checksum in the sixth byte
+  currentPacket[6] = checksum;
 }
 
 void CctPacketFormatter::updateBrightness(uint8_t value) {

+ 1 - 0
lib/MiLight/CctPacketFormatter.h

@@ -45,6 +45,7 @@ public:
 
   virtual void format(uint8_t const* packet, char* buffer);
   virtual void initializePacket(uint8_t* packet);
+  virtual void finalizePacket(uint8_t* packet);
   virtual BulbId parsePacket(const uint8_t* packet, JsonObject& result, GroupStateStore* stateStore);
 
   static uint8_t getCctStatusButton(uint8_t groupId, MiLightStatus status);