Skip to content

Commit

Permalink
Sml update (#20474)
Browse files Browse the repository at this point in the history
* add decryption flags

* add gcm crypto flags
  • Loading branch information
gemu2015 authored Jan 12, 2024
1 parent fa6d18a commit 03cadd2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/lib_div/ams/DataParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

struct DataParserContext {
uint8_t type;
uint8_t flags;
uint16_t length;
time_t timestamp;
uint8_t system_title[8];
Expand Down
23 changes: 17 additions & 6 deletions lib/lib_div/ams/GcmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,39 @@ int8_t GCMParser::parse(uint8_t *d, DataParserContext &ctx) {
int len = 0;
int headersize = 2 + systemTitleLength;
ptr += systemTitleLength;
if(((*ptr) & 0xFF) == 0x81) {

if (ctx.flags & 1) {
len = *ptr;
ptr++;
headersize++;
} else {
if(((*ptr) & 0xFF) == 0x81) {
ptr++;
len = *ptr;
// 1-byte payload length
ptr++;
headersize += 2;
} else if(((*ptr) & 0xFF) == 0x82) {
} else if(((*ptr) & 0xFF) == 0x82) {
GCMSizeDef* h = (GCMSizeDef*) ptr;

// 2-byte payload length
len = (ntohs(h->format) & 0xFFFF);

ptr += 3;
headersize += 3;
} else if(((*ptr) & 0xFF) == 0x4f) {
// ???????? single frame did only decode with this compare
} else if(((*ptr) & 0xFF) == 0x4f) {
// ???????? single frame did only decode with this compare
ptr++;
headersize++;
} else if(((*ptr) & 0xFF) == 0x5e) {
// ???????? single frame did only decode with this compare
} else if(((*ptr) & 0xFF) == 0x5e) {
// ???????? single frame did only decode with this compare
ptr++;
headersize++;
} else {
len = *ptr;
ptr++;
headersize++;
}
}
if(len + headersize > ctx.length)
return DATA_PARSE_INCOMPLETE;
Expand Down
3 changes: 2 additions & 1 deletion lib/lib_div/ams/han_Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int16_t Han_Parser::serial_readBytes(uint8_t *buf, uint16_t size) {
return size;
}

bool Han_Parser::readHanPort(uint8_t **out, uint16_t *size) {
bool Han_Parser::readHanPort(uint8_t **out, uint16_t *size, uint8_t flags) {

if (!serial_available()) return false;

Expand All @@ -56,6 +56,7 @@ bool Han_Parser::readHanPort(uint8_t **out, uint16_t *size) {
}

DataParserContext ctx = {0};
ctx.flags = flags;
int pos = DATA_PARSE_INCOMPLETE;
// For each byte received, check if we have a complete frame we can handle
while (serial_available() && pos == DATA_PARSE_INCOMPLETE) {
Expand Down
2 changes: 1 addition & 1 deletion lib/lib_div/ams/han_Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Han_Parser
public:
Han_Parser(uint16_t (*)(uint8_t, uint8_t), uint8_t, uint8_t *, uint8_t *);
~Han_Parser(void);
bool readHanPort(uint8_t **out, uint16_t *size);
bool readHanPort(uint8_t **out, uint16_t *size, uint8_t flags);
int16_t unwrapData(uint8_t *buf, DataParserContext &context);
void printHanReadError(int16_t pos);
uint8_t encryptionKey[16];
Expand Down
14 changes: 12 additions & 2 deletions tasmota/tasmota_xsns_sensor/xsns_53_sml.ino
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ on esp8266 2 filter masks
9:
on esp32 1 filter
on esp8266 6 filters
A:
decryption flags (8 bits)
*/

//#define MODBUS_DEBUG
Expand Down Expand Up @@ -490,6 +494,7 @@ struct METER_DESC {

#ifdef USE_SML_DECRYPT
bool use_crypt = false;
uint8_t crypflags;
uint8_t last_iob;
uint8_t key[SML_CRYPT_SIZE];
Han_Parser *hp;
Expand Down Expand Up @@ -753,7 +758,7 @@ void dump2log(void) {
d_lastms = millis();
uint16_t logsiz;
uint8_t *payload;
if (mp->hp->readHanPort(&payload, &logsiz)) {
if (mp->hp->readHanPort(&payload, &logsiz, mp->crypflags)) {
if (logsiz > mp->sbsiz) {
logsiz = mp->sbsiz;
}
Expand Down Expand Up @@ -1408,7 +1413,7 @@ void sml_shift_in(uint32_t meters, uint32_t shard) {
mp->lastms = millis();
uint16_t len;
uint8_t *payload;
if (mp->hp->readHanPort(&payload, &len)) {
if (mp->hp->readHanPort(&payload, &len, mp->crypflags)) {
if (len > mp->sbsiz) {
len = mp->sbsiz;
}
Expand Down Expand Up @@ -2868,6 +2873,10 @@ struct METER_DESC *mp = &meter_desc[mnum];
}
break;
#endif // USE_SML_AUTHKEY
case 'A':
cp += 2;
mp->crypflags = strtol(cp, &cp, 10);
break;
#endif // USE_SML_DECRYPT
case '6':
cp += 2;
Expand Down Expand Up @@ -3633,6 +3642,7 @@ next_line:
#else
mp->hp = new Han_Parser(serial_dispatch, meters, mp->key, nullptr);
#endif
mp->crypflags = 0;
}
#endif
}
Expand Down

0 comments on commit 03cadd2

Please sign in to comment.