mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-19 09:38:11 +00:00
Support HDLC segmentation
This commit is contained in:
parent
7813d3ea08
commit
9caec71e1c
@ -30,6 +30,11 @@ typedef struct HDLC3CtrlHcs {
|
||||
class HDLCParser {
|
||||
public:
|
||||
int8_t parse(uint8_t *buf, DataParserContext &ctx);
|
||||
|
||||
private:
|
||||
uint8_t lastSequenceNumber = 0;
|
||||
uint16_t pos = 0;
|
||||
uint8_t *buf = NULL;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@ -59,7 +59,39 @@ int8_t HDLCParser::parse(uint8_t *d, DataParserContext &ctx) {
|
||||
if(ctx.length > 1) {
|
||||
ctx.length -= 3;
|
||||
}
|
||||
return ptr-d;
|
||||
|
||||
// Payload incomplete
|
||||
if((h->format & 0x08) == 0x08) {
|
||||
if(lastSequenceNumber == 0) {
|
||||
if(buf == NULL) buf = (uint8_t *)malloc((size_t)1024);
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
if(buf == NULL) return DATA_PARSE_FAIL;
|
||||
|
||||
uint8_t* ptr = (uint8_t*) &h[1];
|
||||
memcpy(buf + pos, ptr, ctx.length);
|
||||
pos += ctx.length;
|
||||
|
||||
lastSequenceNumber++;
|
||||
return DATA_PARSE_INTERMEDIATE_SEGMENT;
|
||||
} else if(lastSequenceNumber > 0) {
|
||||
lastSequenceNumber = 0;
|
||||
if(buf == NULL) return DATA_PARSE_FAIL;
|
||||
|
||||
uint8_t* ptr = (uint8_t*) &h[1];
|
||||
memcpy(buf + pos, ptr, ctx.length);
|
||||
pos += ctx.length;
|
||||
|
||||
memcpy((uint8_t *) d, buf, pos);
|
||||
free(buf);
|
||||
buf = NULL;
|
||||
ctx.length = pos;
|
||||
pos = 0;
|
||||
return DATA_PARSE_OK;
|
||||
} else {
|
||||
return ptr-d;
|
||||
}
|
||||
}
|
||||
return DATA_PARSE_UNKNOWN_DATA;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user