MCPcopy Create free account
hub / github.com/UtilitechAS/amsreader-firmware / parse

Method parse

lib/AmsDecoder/src/DsmrParser.cpp:13–85  ·  view source on GitHub ↗

verified indicates that this data was encapsulated in something else, so we know this has the correct size etc

Source from the content-addressed store, hash-verified

11
12// verified indicates that this data was encapsulated in something else, so we know this has the correct size etc
13int8_t DSMRParser::parse(uint8_t *buf, DataParserContext &ctx, bool verified, Print* debugger) {
14 uint16_t lenBefore = ctx.length;
15 uint16_t crcPos = 0;
16 bool reachedEnd = verified;
17 uint8_t lastByte = 0x00;
18 for(uint16_t pos = 0; pos < ctx.length; pos++) {
19 uint8_t b = *(buf+pos);
20 if(pos == 0 && b != '/') return DATA_PARSE_BOUNDARY_FLAG_MISSING;
21 if(pos > 0 && b == '!') crcPos = pos+1;
22 if(crcPos > 0 && b == 0x0A && lastByte == 0x0D) {
23 reachedEnd = true;
24 ctx.length = pos;
25 break;
26 }
27 lastByte = b;
28 }
29 if(!reachedEnd) return DATA_PARSE_INCOMPLETE;
30 buf[ctx.length+1] = '\0';
31
32 // If we expect data to be encrypted and it was not previously verified, decrypt content
33 if(gcmParser != NULL && !verified) {
34 uint8_t* ptr = (uint8_t*) buf;
35 while(*ptr != 0x0D && *ptr != 0x0A) ptr++;
36 while(*ptr == 0x0D || *ptr == 0x0A) ptr++;
37 uint16_t pos = ptr-buf;
38 DataParserContext gcmCtx = {
39 DATA_TAG_GCM,
40 crcPos - pos - 1,
41 ctx.timestamp
42 };
43 if(debugger != NULL) {
44 debugger->printf_P(PSTR("DSMR wants to decrypt at position %lu, length: %d, payload:\n"), pos, gcmCtx.length);
45 debugPrint(ptr, 0, gcmCtx.length, debugger);
46 }
47 int8_t gcmRet = gcmParser->parse(ptr, gcmCtx, false);
48 if(gcmRet < 0) {
49 if(debugger != NULL) {
50 debugger->printf_P(PSTR(" - Failed! (%d)\n"), gcmRet);
51 }
52 return gcmRet;
53 } else {
54 if(debugger != NULL) {
55 debugger->printf_P(PSTR(" - Success! (%d)\n"), gcmRet);
56 }
57 ptr += gcmRet;
58 for(uint16_t i = 0; i < gcmCtx.length; i++) {
59 buf[pos++] = ptr[i];
60 }
61 ptr = buf + crcPos - 1;
62 crcPos = pos + 1;
63 while(*ptr != '\0') {
64 ctx.length = pos;
65 buf[pos++] = *(ptr++);
66 }
67 while(pos < lenBefore) {
68 buf[pos++] = '\0';
69 }
70 }

Callers

nothing calls this directly

Calls 3

debugPrintFunction · 0.85
crc16Function · 0.85
fromHexFunction · 0.85

Tested by

no test coverage detected