From 151a5bddd76b7172957aa30f44a5977056022b76 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Tue, 19 Apr 2022 09:38:24 +0200 Subject: [PATCH] =?UTF-8?q?Preparing=20new=20approch=20to=20data=20parsing?= =?UTF-8?q?/reading=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CLC-qthing/SiliconTorch/CyanBus.cpp | 91 ++++++++++++++++------------- CLC-qthing/SiliconTorch/CyanBus.hpp | 2 +- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/CLC-qthing/SiliconTorch/CyanBus.cpp b/CLC-qthing/SiliconTorch/CyanBus.cpp index b970222..33db651 100644 --- a/CLC-qthing/SiliconTorch/CyanBus.cpp +++ b/CLC-qthing/SiliconTorch/CyanBus.cpp @@ -1,7 +1,7 @@ #include "CyanBus.hpp" // C++ system level -#include <cstring> // memset +#include <cstring> // memset, strncmp #include <cstdlib> // TODO: is this for memcpy? #include <cinttypes> #include <functional> @@ -39,7 +39,7 @@ namespace SiliconTorch { namespace CyanBus { // CyanBus envelope header - const uint8_t* const HEADER = (const uint8_t*)"fxCyan"; + const char* const HEADER = "fxCyan"; CyanBus::CyanBus(uint8_t tx, uint8_t rx, uint8_t de, uint8_t re, uint32_t baudRate, uint8_t uartChannel) : tx(tx), rx(rx), de(de), re(re), uartChannel(uartChannel) { @@ -113,10 +113,13 @@ namespace SiliconTorch { uart_port_t _ch = (uart_port_t)uartChannel; - uint32_t bufLEN = MTU; + const uint32_t bufLEN = MTU + 256; // MTU + maxEventSize(120) + slack uint8_t* buffer = new uint8_t[bufLEN]; // our packet buffer uint8_t* bufPTR = buffer; // points inside our buffer; used for envelope parsing… - // uint16_t payloadLEN = 0; + + uint16_t payloadLEN = 0; + const uint8_t crcLEN = 4; + const uint8_t headerLEN = std::strlen(HEADER); std::memset(buffer, 0x00, bufLEN); @@ -124,7 +127,6 @@ namespace SiliconTorch { /* enum FSM { SearchHeader, ReadLength, ReadPayload, CheckCRC }; FSM fsm = SearchHeader; - uint8_t headerLength = std::strlen(HEADER); uint32_t headerByteNum = 0; */ uart_event_t event; @@ -149,39 +151,49 @@ namespace SiliconTorch { // received plain data case UART_DATA: { - uint32_t bytesRead = bufPTR - buffer; + if ((bufPTR - buffer) + event.size > bufLEN) { + ESP_LOGE(TAG, "Buffer#[ %d ] Overflow!", bufLEN); - if (bytesRead + event.size > bufLEN) { - ESP_LOGE(TAG, "Buffer#[ %d ] Overflow: Got %d bytes without a break", bufLEN, bytesRead + event.size); + bufPTR = buffer; + std::memset(buffer, 0x00, bufLEN); // TODO: needed…? + uart_flush_input(_ch); + xQueueReset(Q); + // fsm = SearchHeader; + payloadLEN = 0; + } - if (event.size < bufLEN) { // read into buffer and just start over - uart_read_bytes(_ch, buffer, event.size, portMAX_DELAY); - bufPTR = buffer; + bufPTR += uart_read_bytes(_ch, bufPTR, event.size, portMAX_DELAY); - } else { // kill it with 🔥 + if (payloadLEN > 0 && (bufPTR - buffer) > headerLEN + payloadLEN + crcLEN) { // packet complete! + /* char strbuf[65]; + strbuf[64] = 0x00; - ESP_LOGE(TAG, "// kill it with 🔥"); - uart_flush_input(_ch); - bufPTR = buffer; + std::memcpy(strbuf, buffer, 64); */ + + ESP_LOGW(TAG, "PACKET DEBUG: Starts with header[ %s ] and contains data[ %s ]", buffer, buffer + headerLEN + 2); - } - } else { - - /*int32_t _bytes = 0; - do { - _bytes = uart_read_bytes(_ch, bufPTR, 16, 1); - ESP_LOGW(TAG, "_bytes#[ %d ]", _bytes); - - if (_bytes < 0) { - ESP_LOGE(TAG, "!! _bytes < 0 !!"); - break; - } - bufPTR += _bytes; - } while (_bytes > 0);*/ - bufPTR += uart_read_bytes(_ch, bufPTR, event.size, portMAX_DELAY); + // TODO: send to queue! + // xQueueSendToBack(Q2, buffer, portMAX_DELAY); + + } else if (payloadLEN == 0 && bufPTR - buffer >= headerLEN + 2) { // got enough for header/length parsing + + if (strncmp(reinterpret_cast<const char*>(buffer), HEADER, headerLEN) == 0) { + payloadLEN = (buffer[headerLEN] << 8) | buffer[headerLEN + 1]; + ESP_LOGW(TAG, "Found header: payload#[ %d ]", payloadLEN); + + if (payloadLEN > MTU) { + ESP_LOGE(TAG, "payload#[ %d ] exceeds MTU[ %d ]", payloadLEN, MTU); + bufPTR = buffer; + std::memset(buffer, 0x00, bufLEN); // TODO: needed…? + uart_flush_input(_ch); + xQueueReset(Q); + // fsm = SearchHeader; + payloadLEN = 0; + } + } } break; @@ -191,20 +203,12 @@ namespace SiliconTorch { case UART_BREAK: { ESP_LOGI(TAG, "Received BREAK 🛑 after reading %d bytes", bufPTR - buffer); - - char strbuf[65]; - strbuf[64] = 0x00; - - std::memcpy(strbuf, buffer, 64); - - ESP_LOGW(TAG, "PACKET DEBUG: Starts with [ %s ]", strbuf); - - - // xQueueSendToBack(Q2, buffer, portMAX_DELAY); bufPTR = buffer; std::memset(buffer, 0x00, bufLEN); // TODO: needed…? uart_flush_input(_ch); xQueueReset(Q); + // fsm = SearchHeader; + payloadLEN = 0; break; } @@ -227,9 +231,12 @@ namespace SiliconTorch { ESP_LOGE(TAG, "Unexpected event[ %s ]!", eventName); - uart_flush_input(_ch); bufPTR = buffer; - std::memset(buffer, 0x00, bufLEN); + std::memset(buffer, 0x00, bufLEN); // TODO: needed…? + uart_flush_input(_ch); + xQueueReset(Q); + // fsm = SearchHeader; + payloadLEN = 0; } } diff --git a/CLC-qthing/SiliconTorch/CyanBus.hpp b/CLC-qthing/SiliconTorch/CyanBus.hpp index b10cc65..47f267b 100644 --- a/CLC-qthing/SiliconTorch/CyanBus.hpp +++ b/CLC-qthing/SiliconTorch/CyanBus.hpp @@ -22,7 +22,7 @@ namespace SiliconTorch { constexpr uint32_t MTU = 4096; // CyanBus envelope header - extern const uint8_t* const HEADER; + extern const char* const HEADER; class CyanBus { -- GitLab