mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-24 19:41:57 +00:00
42 lines
1.1 KiB
C++
42 lines
1.1 KiB
C++
/**
|
|
* @copyright Utilitech AS 2023
|
|
* License: Fair Source
|
|
*
|
|
*/
|
|
|
|
#include "DsmrParser.h"
|
|
#include "crc.h"
|
|
#include "hexutils.h"
|
|
#include "lwip/def.h"
|
|
|
|
int8_t DSMRParser::parse(uint8_t *buf, DataParserContext &ctx, bool verified) {
|
|
uint16_t crcPos = 0;
|
|
bool reachedEnd = verified;
|
|
uint8_t lastByte = 0x00;
|
|
for(int pos = 0; pos < ctx.length; pos++) {
|
|
uint8_t b = *(buf+pos);
|
|
if(pos == 0 && b != '/') return DATA_PARSE_BOUNDRY_FLAG_MISSING;
|
|
if(pos > 0 && b == '!' && lastByte == '\n') crcPos = pos+1;
|
|
if(crcPos > 0 && b == '\n') reachedEnd = true;
|
|
lastByte = b;
|
|
}
|
|
if(!reachedEnd) return DATA_PARSE_INCOMPLETE;
|
|
buf[ctx.length+1] = '\0';
|
|
if(crcPos > 0) {
|
|
crc_calc = crc16(buf, crcPos);
|
|
crc = 0x0000;
|
|
fromHex((uint8_t*) &crc, String((char*) buf+crcPos), 2);
|
|
crc = ntohs(crc);
|
|
|
|
if(crc != crc_calc)
|
|
return DATA_PARSE_FOOTER_CHECKSUM_ERROR;
|
|
}
|
|
return DATA_PARSE_OK;
|
|
}
|
|
|
|
uint16_t DSMRParser::getCrc() {
|
|
return crc;
|
|
}
|
|
uint16_t DSMRParser::getCrcCalc() {
|
|
return crc_calc;
|
|
} |