From 84f37631c29474057c4efc3ad6a1ef534da7817c Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 8 Jun 2025 15:46:47 -0500 Subject: [PATCH] Add ByteBuffer APIs for reading/writing multiple bytes --- src/ByteBuffer.c | 35 +++++++++++++++++++++++++---------- src/ByteBuffer.h | 3 +++ src/ControlStream.c | 8 ++------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/ByteBuffer.c b/src/ByteBuffer.c index 2c5f5f1..aa1b0b8 100644 --- a/src/ByteBuffer.c +++ b/src/ByteBuffer.c @@ -47,18 +47,28 @@ bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset) { return true; } -// Get a byte from the byte buffer -bool BbGet8(PBYTE_BUFFER buff, uint8_t* c) { - if (buff->position + sizeof(*c) > buff->length) { +// Rewind the byte buffer back to the starting position +void BbRewindBuffer(PBYTE_BUFFER buff) { + buff->position = 0; +} + +// Get a variable number of bytes from the byte buffer (all or nothing though) +bool BbGetBytes(PBYTE_BUFFER buff, uint8_t* data, int length) { + if (buff->position + length > buff->length) { return false; } - memcpy(c, &buff->buffer[buff->position], sizeof(*c)); - buff->position += sizeof(*c); + memcpy(data, &buff->buffer[buff->position], length); + buff->position += length; return true; } +// Get a byte from the byte buffer +bool BbGet8(PBYTE_BUFFER buff, uint8_t* c) { + return BbGetBytes(buff, c, sizeof(*c)); +} + // Get a short from the byte buffer bool BbGet16(PBYTE_BUFFER buff, uint16_t* s) { if (buff->position + sizeof(*s) > buff->length) { @@ -143,14 +153,19 @@ bool BbPut16(PBYTE_BUFFER buff, uint16_t s) { return true; } -// Put a byte into the buffer -bool BbPut8(PBYTE_BUFFER buff, uint8_t c) { - if (buff->position + sizeof(c) > buff->length) { +// Put a variable number of bytes into the byte buffer (all or nothing though) +bool BbPutBytes(PBYTE_BUFFER buff, const uint8_t* data, int length) { + if (buff->position + length > buff->length) { return false; } - memcpy(&buff->buffer[buff->position], &c, sizeof(c)); - buff->position += sizeof(c); + memcpy(&buff->buffer[buff->position], data, length); + buff->position += length; return true; } + +// Put a byte into the buffer +bool BbPut8(PBYTE_BUFFER buff, uint8_t c) { + return BbPutBytes(buff, &c, sizeof(c)); +} diff --git a/src/ByteBuffer.h b/src/ByteBuffer.h index 7f7ad52..96bc4b2 100644 --- a/src/ByteBuffer.h +++ b/src/ByteBuffer.h @@ -14,12 +14,15 @@ typedef struct _BYTE_BUFFER { void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int length, int byteOrder); bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset); +void BbRewindBuffer(PBYTE_BUFFER buff); +bool BbGetBytes(PBYTE_BUFFER buff, uint8_t* data, int length); bool BbGet8(PBYTE_BUFFER buff, uint8_t* c); bool BbGet16(PBYTE_BUFFER buff, uint16_t* s); bool BbGet32(PBYTE_BUFFER buff, uint32_t* i); bool BbGet64(PBYTE_BUFFER buff, uint64_t* l); +bool BbPutBytes(PBYTE_BUFFER buff, const uint8_t* data, int length); bool BbPut8(PBYTE_BUFFER buff, uint8_t c); bool BbPut16(PBYTE_BUFFER buff, uint16_t s); bool BbPut32(PBYTE_BUFFER buff, uint32_t i); diff --git a/src/ControlStream.c b/src/ControlStream.c index 8f17dbd..4280963 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -1059,12 +1059,8 @@ static void queueAsyncCallback(PNVCTL_ENET_PACKET_HEADER_V1 ctlHdr, int packetLe BbGet8(&bb, &queuedCb->data.dsAdaptiveTrigger.typeLeft); BbGet8(&bb, &queuedCb->data.dsAdaptiveTrigger.typeRight); - for(int i = 0; i < DS_EFFECT_PAYLOAD_SIZE; i++) { - BbGet8(&bb, &queuedCb->data.dsAdaptiveTrigger.left[i]); - } - for(int i = 0; i < DS_EFFECT_PAYLOAD_SIZE; i++) { - BbGet8(&bb, &queuedCb->data.dsAdaptiveTrigger.right[i]); - } + BbGetBytes(&bb, queuedCb->data.dsAdaptiveTrigger.left, DS_EFFECT_PAYLOAD_SIZE); + BbGetBytes(&bb, queuedCb->data.dsAdaptiveTrigger.right, DS_EFFECT_PAYLOAD_SIZE); queuedCb->typeIndex = IDX_DS_ADAPTIVE_TRIGGERS; } else {