Skip to content
Snippets Groups Projects
Commit d6eaa9f8 authored by fxk8y's avatar fxk8y :spider:
Browse files

Begin implementing CRC check

parent 7910c788
No related branches found
No related tags found
No related merge requests found
#include "CyanBus.hpp"
#include "CyanBusBREAK.hpp"
// C++ system level
#include <cstring> // memset
......
#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;
}
}
}
// 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);
}
}
#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;
}
}
}
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment