From 38cba4e8da01fc0e64294fc261b3f5625a3bbf5a Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Mon, 6 Dec 2021 19:33:40 +0100 Subject: [PATCH] Various fixes --- src/AmsDataStorage.cpp | 30 ++++++++++++++++++++++-------- src/AmsToMqttBridge.ino | 17 ++++++++++------- src/ams/hdlc.cpp | 19 ++++++++++--------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/AmsDataStorage.cpp b/src/AmsDataStorage.cpp index 2b235f4e..6b21f4ee 100644 --- a/src/AmsDataStorage.cpp +++ b/src/AmsDataStorage.cpp @@ -33,6 +33,14 @@ bool AmsDataStorage::update(AmsData* data) { return false; } + tmElements_t tm, last; + breakTime(now, tm); + breakTime(day.lastMeterReadTime, last); + for(int i = last.Hour; i < tm.Hour; i++) { + debugger->printf("(AmsDataStorage) Clearing hour: %d\n", i); + setHour(i, 0); + } + // Update day plot if(day.activeImport == 0 || now - day.lastMeterReadTime > 86400) { day.activeImport = data->getActiveImportCounter() * 1000; @@ -42,12 +50,11 @@ bool AmsDataStorage::update(AmsData* data) { setHour(i, 0); } } else if(now - day.lastMeterReadTime < 4000) { - tmElements_t tm; breakTime(now - 3600, tm); int16_t val = (((data->getActiveImportCounter() * 1000) - day.activeImport) - ((data->getActiveExportCounter() * 1000) - day.activeExport)); setHour(tm.Hour, val); - if(debugger->isActive(RemoteDebug::DEBUG)) { + if(debugger->isActive(RemoteDebug::INFO)) { debugger->printf("(AmsDataStorage) Usage for hour %d: %d\n", tm.Hour, val); } @@ -69,7 +76,7 @@ bool AmsDataStorage::update(AmsData* data) { float val = ((ipm * minutes) - (epm * minutes)); setHour(tm.Hour-1, val); - if(debugger->isActive(RemoteDebug::DEBUG)) { + if(debugger->isActive(RemoteDebug::INFO)) { debugger->printf("(AmsDataStorage) Estimated usage for hour %d: %d\n", tm.Hour, val); } @@ -80,27 +87,34 @@ bool AmsDataStorage::update(AmsData* data) { } // Update month plot - tmElements_t tm; if(tz != NULL) { time_t local = tz->toLocal(now); breakTime(local, tm); } else { breakTime(now, tm); } + breakTime(month.lastMeterReadTime, last); + for(int i = last.Day; i < tm.Day; i++) { + debugger->printf("(AmsDataStorage) Clearing day: %d\n", i); + setDay(i, 0); + } if(tm.Hour == 0 && now-month.lastMeterReadTime > 86300) { if(month.activeImport == 0 || now - month.lastMeterReadTime > 2678400) { month.activeImport = data->getActiveImportCounter() * 1000; month.activeExport = data->getActiveExportCounter() * 1000; month.lastMeterReadTime = now; + if(debugger->isActive(RemoteDebug::WARNING)) { + debugger->printf("(AmsDataStorage) Too long since last update, clearing data\n"); + } for(int i = 0; i<31; i++) { setDay(i, 0); } } else if(now - month.lastMeterReadTime < 87000) { int32_t val = (month.activeImport == 0 ? 0 : ((data->getActiveImportCounter() * 1000) - month.activeImport) - ((data->getActiveExportCounter() * 1000) - month.activeExport)); - if(debugger->isActive(RemoteDebug::DEBUG)) { + //if(debugger->isActive(RemoteDebug::INFO)) { debugger->printf("(AmsDataStorage) Usage for day %d: %d\n", tm.Day, val); - } + //} time_t yesterday = now - 3600; breakTime(yesterday, tm); @@ -124,9 +138,9 @@ bool AmsDataStorage::update(AmsData* data) { float val = ((iph * hours) - (eph * hours)); setDay(tm.Day-1, val); - if(debugger->isActive(RemoteDebug::DEBUG)) { + //if(debugger->isActive(RemoteDebug::INFO)) { debugger->printf("(AmsDataStorage) Estimated usage for day %d: %d\n", tm.Day, val); - } + //} month.activeImport += iph * hours; month.activeExport += eph * hours; diff --git a/src/AmsToMqttBridge.ino b/src/AmsToMqttBridge.ino index 5ff69cfb..fe25b5f0 100644 --- a/src/AmsToMqttBridge.ino +++ b/src/AmsToMqttBridge.ino @@ -68,6 +68,7 @@ AmsMqttHandler* mqttHandler = NULL; Stream *hanSerial; SoftwareSerial *swSerial = NULL; +HDLCConfig* hc = NULL; GpioConfig gpioConfig; MeterConfig meterConfig; @@ -482,6 +483,8 @@ void loop() { config.getMeterConfig(meterConfig); setupHanPort(gpioConfig.hanPin, meterConfig.baud, meterConfig.parity, meterConfig.invert); config.ackMeterChanged(); + delete hc; + hc = NULL; } delay(1); // Needed for auto modem sleep @@ -656,7 +659,6 @@ void swapWifiMode() { int len = 0; uint8_t buf[BUF_SIZE]; -HDLCConfig* hc = NULL; int currentMeterType = -1; void readHanPort() { if(!hanSerial->available()) return; @@ -681,13 +683,14 @@ void readHanPort() { delay(1); } if(len > 0) { + if(len >= BUF_SIZE) { + hanSerial->readBytes(buf, BUF_SIZE); + len = 0; + debugI("Buffer overflow, resetting"); + return; + } int pos = HDLC_validate((uint8_t *) buf, len, hc, ×tamp); if(pos == HDLC_FRAME_INCOMPLETE) { - if(len >= BUF_SIZE) { - hanSerial->readBytes(buf, BUF_SIZE); - len = 0; - debugI("Buffer overflow, resetting"); - } return; } for(int i = len; iadditional_authenticated_data, 0, 17); debugD("Authentication tag:"); - debugPrint(hc->authentication_tag, 0, 8); + debugPrint(hc->authentication_tag, 0, 12); } } len = 0; diff --git a/src/ams/hdlc.cpp b/src/ams/hdlc.cpp index 836eae51..b7e2e316 100644 --- a/src/ams/hdlc.cpp +++ b/src/ams/hdlc.cpp @@ -134,18 +134,19 @@ int HDLC_validate(const uint8_t* d, int length, HDLCConfig* config, CosemDateTim // 1-byte payload length ptr++; } else if(((*ptr) & 0xFF) == 0x82) { - HDLCHeader* h = (HDLCHeader*) d; + HDLCHeader* h = (HDLCHeader*) ptr; - // Length field (11 lsb of format) + // Length field len = (ntohs(h->format) & 0xFFFF); ptr += 3; headersize += 3; } - len += preheadersize; - if(len > length) + if(len + preheadersize > length) return HDLC_FRAME_INCOMPLETE; + //Serial.printf("\nL: %d : %d, %d : %d\n", length, len, preheadersize, headersize); + // TODO: FCS memcpy(config->additional_authenticated_data, ptr, 1); @@ -167,7 +168,7 @@ int HDLC_validate(const uint8_t* d, int length, HDLCConfig* config, CosemDateTim aadlen = 17; footersize += authkeylen; memcpy(config->additional_authenticated_data + 1, config->authentication_key, 16); - memcpy(config->authentication_tag, d + len - footersize, authkeylen); + memcpy(config->authentication_tag, ptr + len - footersize - 2, authkeylen); } #if defined(ESP8266) @@ -180,13 +181,13 @@ int HDLC_validate(const uint8_t* d, int length, HDLCConfig* config, CosemDateTim br_gcm_aad_inject(&gcmCtx, config->additional_authenticated_data, aadlen); } br_gcm_flip(&gcmCtx); - br_gcm_run(&gcmCtx, 0, (void*) ptr, (len - headersize - footersize)); - if(authkeylen > 0 && br_gcm_check_tag_trunc(&gcmCtx, config->authentication_tag, authkeylen) != 1) { + br_gcm_run(&gcmCtx, 0, (void*) (ptr), len - footersize - 2); + if(authkeylen > 0 && br_gcm_check_tag_trunc(&gcmCtx, config->authentication_tag, authkeylen) != 1) { return -91; } #elif defined(ESP32) - uint8_t cipher_text[len - headersize - footersize]; - memcpy(cipher_text, ptr, len - headersize - footersize); + uint8_t cipher_text[len - authkeylen - footersize - 2]; + memcpy(cipher_text, ptr, len - authkeylen - footersize - 2); mbedtls_gcm_context m_ctx; mbedtls_gcm_init(&m_ctx);