Some changes after testing

This commit is contained in:
Gunnar Skjold
2021-11-30 08:01:20 +01:00
parent f425abb52d
commit ab101c8622
8 changed files with 106 additions and 41 deletions

View File

@@ -5,12 +5,14 @@
#include <Timezone.h>
enum AmsType {
AmsTypeAutodetect = 0x00,
AmsTypeAidon = 0x01,
AmsTypeKaifa = 0x02,
AmsTypeKamstrup = 0x03,
AmsTypeIskra = 0x08,
AmsTypeLandis = 0x09,
AmsTypeSagemcom = 0x0A,
AmsTypeCustom = 0x88,
AmsTypeUnknown = 0xFF
};

View File

@@ -689,6 +689,9 @@ void readHanPort() {
}
return;
}
for(int i = len; i<BUF_SIZE; i++) {
buf[i] = 0x00;
}
if(pos == HDLC_ENCRYPTION_CONFIG_MISSING) {
hc = new HDLCConfig();
memcpy(hc->encryption_key, meterConfig.encryptionKey, 16);

View File

@@ -271,9 +271,8 @@ IEC6205675::IEC6205675(const char* d, uint8_t useMeterType, CosemDateTime packag
AmsOctetTimestamp* amst = (AmsOctetTimestamp*) meterTs;
time_t ts = getTimestamp(amst->dt);
if(meterType == AmsTypeKamstrup || meterType == AmsTypeAidon) {
this->packageTimestamp = tz.toUTC(ts);
this->packageTimestamp = this->packageTimestamp > 0 ? tz.toUTC(this->packageTimestamp) : 0;
this->meterTimestamp = tz.toUTC(ts);
Serial.printf("\nKamstrup/Aidon time: %d\n", meterTimestamp);
} else {
meterTimestamp = ts;
}
@@ -457,7 +456,7 @@ time_t IEC6205675::getTimestamp(CosemDateTime timestamp) {
tm.Minute = timestamp.minute;
tm.Second = timestamp.second;
Serial.printf("\nY: %d, M: %d, D: %d, h: %d, m: %d, s: %d, deviation: 0x%2X, status: 0x%1X\n", tm.Year, tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second, timestamp.deviation, timestamp.status);
//Serial.printf("\nY: %d, M: %d, D: %d, h: %d, m: %d, s: %d, deviation: 0x%2X, status: 0x%1X\n", tm.Year, tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second, timestamp.deviation, timestamp.status);
time_t time = makeTime(tm);
int16_t deviation = ntohs(timestamp.deviation);

View File

@@ -23,17 +23,17 @@ int HDLC_validate(const uint8_t* d, int length, HDLCConfig* config, CosemDateTim
// Length field (11 lsb of format)
int len = (ntohs(h->format) & 0x7FF) + 2;
if(len > length)
return -4;
return HDLC_FRAME_INCOMPLETE;
HDLCFooter* f = (HDLCFooter*) (d + len - sizeof *f);
// First and last byte should be MBUS_HAN_TAG
if(h->flag != HDLC_FLAG || f->flag != HDLC_FLAG)
return -1;
return HDLC_BOUNDRY_FLAG_MISSING;
// Verify FCS
if(ntohs(f->fcs) != crc16_x25(d + 1, len - sizeof *f - 1))
return -2;
return HDLC_FCS_ERROR;
int headersize = 8;
int footersize = 3;
@@ -60,7 +60,7 @@ int HDLC_validate(const uint8_t* d, int length, HDLCConfig* config, CosemDateTim
// Verify HCS
if(ntohs(t3->hcs) != crc16_x25(d + 1, ptr-d))
return -3;
return HDLC_HCS_ERROR;
ptr += sizeof *t3;
}
@@ -138,7 +138,28 @@ int HDLC_validate(const uint8_t* d, int length, HDLCConfig* config, CosemDateTim
}
mbedtls_gcm_free(&m_ctx);
#endif
ptr += 36; // TODO: Come to this number in a proper way...
ptr += 23; // TODO: Come to this number in a proper way...
// ADPU timestamp
CosemData* dateTime = (CosemData*) ptr;
if(dateTime->base.type == CosemTypeOctetString) {
if(dateTime->base.length == 0x0C) {
memcpy(timestamp, ptr+1, dateTime->base.length);
}
ptr += 2 + dateTime->base.length;
} else if(dateTime->base.type == CosemTypeNull) {
timestamp = 0;
ptr++;
} else if(dateTime->base.type == CosemTypeDateTime) {
memcpy(timestamp, ptr, dateTime->base.length);
} else if(dateTime->base.type == 0x0C) { // Kamstrup bug...
memcpy(timestamp, ptr, 0x0C);
ptr += 13;
} else {
return -99;
}
return ptr-d;
}

View File

@@ -6,6 +6,8 @@
#define HDLC_FLAG 0x7E
#define HDLC_BOUNDRY_FLAG_MISSING -1
#define HDLC_FCS_ERROR -2
#define HDLC_HCS_ERROR -3
#define HDLC_FRAME_INCOMPLETE -4
#define HDLC_ENCRYPTION_CONFIG_MISSING -90

View File

@@ -349,6 +349,8 @@ void AmsWebServer::indexHtml() {
html.replace("{P}", String(meterState->getActiveImportPower()));
html.replace("{PO}", String(meterState->getActiveExportPower()));
html.replace("{Q}", String(meterState->getReactiveImportPower()));
html.replace("{QO}", String(meterState->getReactiveExportPower()));
html.replace("{de}", meterConfig->productionCapacity > 0 ? "" : "none");
html.replace("{dn}", meterConfig->productionCapacity > 0 ? "none" : "");
html.replace("{ti}", meterConfig->productionCapacity > 0 ? "Import" : "Use");