Support HDLC segmentation

This commit is contained in:
Gunnar Skjold 2024-03-02 08:33:19 +01:00
parent 7813d3ea08
commit 9caec71e1c
2 changed files with 38 additions and 1 deletions

View File

@ -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

View File

@ -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;
}