From 9ff6cf355fc59e440039778f50bd36f1701229a0 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Mon, 25 Dec 2023 18:36:35 +0100 Subject: [PATCH] Fixing bugs --- lib/PriceService/src/PriceService.cpp | 11 ++++++----- src/PassiveMeterCommunicator.cpp | 20 +++++++++++--------- src/PassiveMeterCommunicator.h | 6 ++---- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/PriceService/src/PriceService.cpp b/lib/PriceService/src/PriceService.cpp index 76d9195e..59520b35 100644 --- a/lib/PriceService/src/PriceService.cpp +++ b/lib/PriceService/src/PriceService.cpp @@ -526,16 +526,17 @@ std::vector& PriceService::getPriceConfig() { } void PriceService::setPriceConfig(uint8_t index, PriceConfig &priceConfig) { - if(index < this->priceConfig.capacity()) + if(this->priceConfig.capacity() != index+1) this->priceConfig.resize(index+1); - this->priceConfig[index] = priceConfig; + if(this->priceConfig.size() > index) + this->priceConfig[index] = priceConfig; + else + this->priceConfig.push_back(priceConfig); } bool PriceService::save() { if(!LittleFS.begin()) { - if(debugger->isActive(RemoteDebug::ERROR)) { - debugger->printf_P(PSTR("(PriceService) Unable to load LittleFS\n")); - } + if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(PriceService) Unable to load LittleFS\n")); return false; } diff --git a/src/PassiveMeterCommunicator.cpp b/src/PassiveMeterCommunicator.cpp index e26f880f..babb7253 100644 --- a/src/PassiveMeterCommunicator.cpp +++ b/src/PassiveMeterCommunicator.cpp @@ -53,6 +53,8 @@ void PassiveMeterCommunicator::configure(MeterConfig& meterConfig, Timezone* tz) } bool PassiveMeterCommunicator::loop() { + if(hanBufferSize == 0) return false; + unsigned long start, end; if(!hanSerial->available()) { return false; @@ -60,7 +62,7 @@ bool PassiveMeterCommunicator::loop() { // Before reading, empty serial buffer to increase chance of getting first byte of a data transfer if(!serialInit) { - hanSerial->readBytes(hanBuffer, BUF_SIZE_HAN); + hanSerial->readBytes(hanBuffer, hanBufferSize); serialInit = true; return false; } @@ -76,8 +78,8 @@ bool PassiveMeterCommunicator::loop() { start = millis(); while(hanSerial->available() && pos == DATA_PARSE_INCOMPLETE) { // If buffer was overflowed, reset - if(len >= BUF_SIZE_HAN) { - hanSerial->readBytes(hanBuffer, BUF_SIZE_HAN); + if(len >= hanBufferSize) { + hanSerial->readBytes(hanBuffer, hanBufferSize); len = 0; if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Buffer overflow, resetting\n")); return false; @@ -124,7 +126,7 @@ bool PassiveMeterCommunicator::loop() { } else if(pos == DATA_PARSE_UNKNOWN_DATA) { if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Unknown data received\n")); lastError = pos; - len = len + hanSerial->readBytes(hanBuffer+len, BUF_SIZE_HAN-len); + len = len + hanSerial->readBytes(hanBuffer+len, hanBufferSize-len); if(debugger->isActive(RemoteDebug::VERBOSE)) { debugger->printf_P(PSTR(" payload:\n")); debugPrint(hanBuffer, 0, len); @@ -138,7 +140,7 @@ bool PassiveMeterCommunicator::loop() { } else if(pos < 0) { lastError = pos; printHanReadError(pos); - len += hanSerial->readBytes(hanBuffer+len, BUF_SIZE_HAN-len); + len += hanSerial->readBytes(hanBuffer+len, hanBufferSize-len); if(pt != NULL) { pt->publishBytes(hanBuffer+pos, len); } @@ -150,7 +152,7 @@ bool PassiveMeterCommunicator::loop() { if(ctx.type == 0) { if (debugger->isActive(RemoteDebug::WARNING)) debugger->printf_P(PSTR("Ended up with context type %d, return code %d and length: %lu/%lu\n"), ctx.type, pos, ctx.length, len); lastError = pos; - len = len + hanSerial->readBytes(hanBuffer+len, BUF_SIZE_HAN-len); + len = len + hanSerial->readBytes(hanBuffer+len, hanBufferSize-len); if(debugger->isActive(RemoteDebug::VERBOSE)) { debugger->printf_P(PSTR(" payload:\n")); debugPrint(hanBuffer, 0, len); @@ -160,7 +162,7 @@ bool PassiveMeterCommunicator::loop() { } // Data is valid, clear the rest of the buffer to avoid tainted parsing - for(int i = pos+ctx.length; i BUF_SIZE_HAN) { + if(ctx.length > hanBufferSize) { debugger->printf_P(PSTR("Invalid context length\n")); dataAvailable = false; return NULL; @@ -282,7 +284,7 @@ void PassiveMeterCommunicator::getCurrentConfig(MeterConfig& meterConfig) { int16_t PassiveMeterCommunicator::unwrapData(uint8_t *buf, DataParserContext &context) { int16_t ret = 0; bool doRet = false; - uint16_t end = BUF_SIZE_HAN; + uint16_t end = hanBufferSize; uint8_t tag = (*buf); uint8_t lastTag = DATA_TAG_NONE; while(tag != DATA_TAG_NONE) { diff --git a/src/PassiveMeterCommunicator.h b/src/PassiveMeterCommunicator.h index d1cd0feb..630d76f8 100644 --- a/src/PassiveMeterCommunicator.h +++ b/src/PassiveMeterCommunicator.h @@ -15,8 +15,6 @@ #include "Timezone.h" #include "PassthroughMqttHandler.h" -#define BUF_SIZE_HAN (1280) - class PassiveMeterCommunicator : public MeterCommunicator { public: PassiveMeterCommunicator(RemoteDebug* debugger); @@ -39,8 +37,8 @@ protected: PassthroughMqttHandler* pt = NULL; - uint8_t *hanBuffer; - uint16_t hanBufferSize = 512; + uint8_t *hanBuffer = NULL; + uint16_t hanBufferSize = 0; Stream *hanSerial; SoftwareSerial *swSerial = NULL; HardwareSerial *hwSerial = NULL;