mirror of
https://github.com/halleysfifthinc/AVCLAN-Mockingboard.git
synced 2026-08-07 01:13:18 +00:00
Refactor AVCLAN_parseframe call signature to avoid dynamic allocs
This commit is contained in:
+34
-22
@@ -1173,14 +1173,21 @@ void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len) {
|
uint8_t AVCLAN_parseframe(const uint8_t *bytes, uint8_t len,
|
||||||
if (len < sizeof(AVCLAN_frame_t))
|
AVCLAN_frame_t *frame) {
|
||||||
return NULL;
|
struct errtype {
|
||||||
|
enum : uint8_t {
|
||||||
|
TOO_SHORT = 0x01,
|
||||||
|
MISMATCH_LENGTH,
|
||||||
|
} errno;
|
||||||
|
uint8_t val;
|
||||||
|
} err = {0};
|
||||||
|
|
||||||
AVCLAN_frame_t *frame = malloc(sizeof(AVCLAN_frame_t) + 1);
|
if (len < sizeof(AVCLAN_frame_t)) {
|
||||||
|
err.erno = TOO_SHORT;
|
||||||
if (!frame)
|
goto handle_err;
|
||||||
return NULL;
|
}
|
||||||
|
uint8_t *last = bytes + len;
|
||||||
|
|
||||||
frame->broadcast = *bytes++;
|
frame->broadcast = *bytes++;
|
||||||
frame->controller_addr = *(uint16_t *)bytes++;
|
frame->controller_addr = *(uint16_t *)bytes++;
|
||||||
@@ -1190,24 +1197,29 @@ AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len) {
|
|||||||
frame->control = *bytes++;
|
frame->control = *bytes++;
|
||||||
frame->length = *bytes++;
|
frame->length = *bytes++;
|
||||||
|
|
||||||
if (frame->length <= (len - 8)) {
|
if ((bytes + frame->length) <= last) {
|
||||||
free(frame);
|
memcpy(frame->data, bytes, frame->length);
|
||||||
return NULL;
|
|
||||||
} else {
|
} else {
|
||||||
AVCLAN_frame_t *framedata =
|
err.errno = MISMATCH_LENGTH;
|
||||||
realloc(frame, sizeof(AVCLAN_frame_t) + frame->length);
|
goto handle_err;
|
||||||
if (!framedata) {
|
|
||||||
free(frame);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
frame = framedata;
|
|
||||||
frame->data = (uint8_t *)frame + sizeof(AVCLAN_frame_t);
|
|
||||||
for (uint8_t i = 0; i < frame->length; i++) {
|
|
||||||
frame->data[i] = *bytes++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return frame;
|
if (0) {
|
||||||
|
handle_err:;
|
||||||
|
RS232_Print("ERR(parse): ");
|
||||||
|
switch (err.errno) {
|
||||||
|
case TOO_SHORT:
|
||||||
|
RS232_Print("not enough bytes too fill AVCLAN frame");
|
||||||
|
break;
|
||||||
|
case MISMATCH_LENGTH:
|
||||||
|
RS232_Print("frame->length is longer than remaining data");
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
RS232_Print("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return err.errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AVCLAN_updateCDStatus() {
|
void AVCLAN_updateCDStatus() {
|
||||||
|
|||||||
+2
-1
@@ -205,7 +205,8 @@ inline uint8_t AVCLAN_responseNeeded() { return (answerReq != 0) || !qEmpty(); }
|
|||||||
uint8_t AVCLAN_respond();
|
uint8_t AVCLAN_respond();
|
||||||
|
|
||||||
void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary);
|
void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary);
|
||||||
AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len);
|
uint8_t AVCLAN_parseframe(const uint8_t *bytes, uint8_t len,
|
||||||
|
AVCLAN_frame_t *frame);
|
||||||
|
|
||||||
#ifdef SOFTWARE_DEBUG
|
#ifdef SOFTWARE_DEBUG
|
||||||
void AVCLan_Measure();
|
void AVCLan_Measure();
|
||||||
|
|||||||
+6
-4
@@ -178,14 +178,16 @@ int main() {
|
|||||||
} // else (readSeq || readBinary); fall through to default
|
} // else (readSeq || readBinary); fall through to default
|
||||||
case '\n':
|
case '\n':
|
||||||
if (readSeq && readBinary && data_tmp[s_len] == 0x17) {
|
if (readSeq && readBinary && data_tmp[s_len] == 0x17) {
|
||||||
s_len--;
|
{
|
||||||
AVCLAN_frame_t *frame = AVCLAN_parseframe(data_tmp, s_len);
|
uint8_t tmp[MAXMSGLEN];
|
||||||
if (frame) {
|
AVCLAN_frame_t frame = {.data = tmp};
|
||||||
|
err = AVCLAN_parseframe(data_tmp, --s_len, &frame);
|
||||||
|
if (!err) {
|
||||||
AVCLAN_sendframe(frame);
|
AVCLAN_sendframe(frame);
|
||||||
free(frame);
|
|
||||||
readSeq = 0;
|
readSeq = 0;
|
||||||
readBinary = 0;
|
readBinary = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
} // else (readSeq || readBinary || most recent char != 0x17);
|
} // else (readSeq || readBinary || most recent char != 0x17);
|
||||||
// fall through to default
|
// fall through to default
|
||||||
|
|||||||
Reference in New Issue
Block a user