From d6eaa9f8d8a5549dc50a965cae0d60d498020e30 Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Wed, 6 Apr 2022 08:25:02 +0200
Subject: [PATCH] Begin implementing CRC check

---
 CLC-qthing/SiliconTorch/CyanBusBREAK.cpp |  2 +-
 CLC-qthing/SiliconTorch/CyanBusCRC.cpp   | 42 ++++++++++++++++++++++++
 CLC-qthing/SiliconTorch/CyanBusCRC.hpp   | 22 +++++++++++++
 CLC-qthing/SiliconTorch/CyanBusTOUT.cpp  | 25 ++++++++++----
 CLC-qthing/device_main.cpp               | 19 ++++++++++-
 5 files changed, 102 insertions(+), 8 deletions(-)
 create mode 100644 CLC-qthing/SiliconTorch/CyanBusCRC.cpp
 create mode 100644 CLC-qthing/SiliconTorch/CyanBusCRC.hpp

diff --git a/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp b/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp
index 0775ab8..f50c744 100644
--- a/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp
+++ b/CLC-qthing/SiliconTorch/CyanBusBREAK.cpp
@@ -1,4 +1,4 @@
-#include "CyanBus.hpp"
+#include "CyanBusBREAK.hpp"
 
 // C++ system level
 #include <cstring>  // memset
diff --git a/CLC-qthing/SiliconTorch/CyanBusCRC.cpp b/CLC-qthing/SiliconTorch/CyanBusCRC.cpp
new file mode 100644
index 0000000..84a388e
--- /dev/null
+++ b/CLC-qthing/SiliconTorch/CyanBusCRC.cpp
@@ -0,0 +1,42 @@
+#include "CyanBusCRC.hpp"
+
+// C++ system level
+#include <cinttypes>
+
+// ESP32 specific
+#include "esp_log.h"
+#include "esp32/rom/crc.h"
+
+// project specific
+// #include "LambdaTask.hpp"
+
+
+static const char* TAG = "CyanBus";
+
+namespace SiliconTorch {
+
+  namespace Impl {
+
+
+    // Checks CyanBus CRC
+    // Note: The last 4 bytes of buffer contain the CRC to check against and are included in the given length
+    bool checkCRC(uint8_t* buffer, uint32_t length) {
+
+      if (length < 4) {
+        ESP_LOGE(TAG, "CRC calculation failed: Input buffer too small! Got bytes[ %d ] but needs bytes[ >= 4 ]", length);
+        return false;
+      }
+
+      // uint32_t _init = 0x00000000;
+      uint32_t _init = 0xFFFFFFFF;
+      uint32_t calculatedCRC = crc32_be(_init, buffer, length - 4);
+      uint32_t packetCRC = reinterpret_cast<uint32_t*>(&buffer[length - 4])[0];
+
+      ESP_LOGW("CheckCRC", "packetCRC = 0x%X   calcCRC = 0x%X", packetCRC, calculatedCRC);
+
+      return packetCRC == calculatedCRC;
+    }
+
+
+  }
+}
diff --git a/CLC-qthing/SiliconTorch/CyanBusCRC.hpp b/CLC-qthing/SiliconTorch/CyanBusCRC.hpp
new file mode 100644
index 0000000..2b47a41
--- /dev/null
+++ b/CLC-qthing/SiliconTorch/CyanBusCRC.hpp
@@ -0,0 +1,22 @@
+// C++ system level
+#include <cinttypes>
+
+// ESP32 specific
+// #include "esp_log.h"
+
+// project specific
+// #include "LambdaTask.hpp"
+
+
+
+namespace SiliconTorch {
+
+  namespace Impl {
+
+    // Checks CyanBus CRC
+    // seems to work against Crc32Posix from python crccheck package
+    bool checkCRC(uint8_t* buffer, uint32_t length);
+
+  }
+
+}
diff --git a/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp b/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp
index 53adce2..17257d5 100644
--- a/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp
+++ b/CLC-qthing/SiliconTorch/CyanBusTOUT.cpp
@@ -1,4 +1,4 @@
-#include "CyanBus.hpp"
+#include "CyanBusTOUT.hpp"
 
 // C++ system level
 #include <cstring>  // memset
@@ -124,14 +124,14 @@ namespace SiliconTorch {
       uint8_t headerLength = std::strlen(HEADER);
       uint32_t headerByteNum = 0; */
 
-      auto Q2 = packetQ;
+      auto Q = packetQ;
 
       while (true) {
 
         uint32_t packetLEN = bufPTR - buffer;
 
         // what happens when we try to read 0 bytes???
-        auto bytes = uart_read_bytes(_ch, bufPTR, MTU - packetLEN, 5);  // 5 = ms Tout; TODO: make configurable and smaller
+        auto bytes = uart_read_bytes(_ch, bufPTR, MTU - packetLEN, 2);  // 5 = ms Tout; TODO: make configurable and smaller
 
         if (bytes > 0) {
 
@@ -139,10 +139,23 @@ namespace SiliconTorch {
 
         } else {
 
-          ESP_LOGI(TAG, "Received Tout ⏰ after reading %d bytes", bufPTR - buffer);
-          xQueueSendToBack(Q2, buffer, portMAX_DELAY);
-          bufPTR = buffer;
+          if (bufPTR - buffer > 0) {
+            ESP_LOGI(TAG, "Received Tout ⏰ after reading %d bytes", bufPTR - buffer);
+            // xQueueSendToBack(Q, buffer, portMAX_DELAY);
+
+
+
+            char strbuf[17];
+            strbuf[16] = 0x00;
+
+            std::memcpy(strbuf, buffer, 16);
+
+            ESP_LOGI(TAG, "PACKET DEBUG: Starts with [ %s ]", buffer);
+
+
 
+            bufPTR = buffer;
+          }
         }
       }
 
diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp
index 5b5a073..e98a28a 100644
--- a/CLC-qthing/device_main.cpp
+++ b/CLC-qthing/device_main.cpp
@@ -9,7 +9,13 @@
 
 #include "CyanLight.hpp"
 
+
+// ### LIBS FOR TESTING ###
+#include <cstdlib>
+#include "SiliconTorch/CyanBusCRC.hpp"
 #include "SiliconTorch/CyanBusTOUT.hpp"
+// ###     END LIBS     ###
+
 
 
 qthing::Config cfg;
@@ -21,13 +27,24 @@ void device_main() {
   // Needed for packet parsing, animation rendering and stuff
   qthing::power_managment_max_power();
 
+  uint8_t L = 9;
+  uint8_t* buffer = new uint8_t[L+1];
+  std::memcpy(buffer, "fxk8y", 5);
+  buffer[L-4] = 0x84;
+  buffer[L-3] = 0xF1;
+  buffer[L-2] = 0xEF;
+  buffer[L-1] = 0x44;
+  buffer[L] = 0x00;
+
+  ESP_LOGW("CheckCRC", "CRC of str[ %s ] is crc[ %s ]", buffer, SiliconTorch::Impl::checkCRC(buffer, L) ? "true" : "false");
+
+  return;
 
   SiliconTorch::CyanBusTOUT::CyanBus cyanBus(13, 14, 12, 15);  // Pinout of CyanStripe
 
   // TODO: ???
 
 
-
   cfg.apply();
 
   return;
-- 
GitLab