From 333382ae746f4b49bf205bbe3a38cc3e7cd49044 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 30 Nov 2020 22:03:24 -0600 Subject: [PATCH] Use stdint types for ByteBuffer --- src/ByteBuffer.c | 34 +++++++++++++++++----------------- src/ByteBuffer.h | 16 ++++++++-------- src/ControlStream.c | 34 +++++++++++++++++----------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/ByteBuffer.c b/src/ByteBuffer.c index e3dc6a9..a1516c3 100644 --- a/src/ByteBuffer.c +++ b/src/ByteBuffer.c @@ -8,7 +8,7 @@ void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int le } // Get the long long in the correct byte order -static long long byteSwapLongLong(PBYTE_BUFFER buff, long long l) { +static uint64_t byteSwap64(PBYTE_BUFFER buff, uint64_t l) { if (buff->byteOrder == BYTE_ORDER_BIG) { return HTONLL(l); } @@ -18,7 +18,7 @@ static long long byteSwapLongLong(PBYTE_BUFFER buff, long long l) { } // Get the int in the correct byte order -static int byteSwapInt(PBYTE_BUFFER buff, int i) { +static uint32_t byteSwap32(PBYTE_BUFFER buff, uint32_t i) { if (buff->byteOrder == BYTE_ORDER_BIG) { return htonl(i); } @@ -28,7 +28,7 @@ static int byteSwapInt(PBYTE_BUFFER buff, int i) { } // Get the short in the correct byte order -static int byteSwapShort(PBYTE_BUFFER buff, short s) { +static uint16_t byteSwap16(PBYTE_BUFFER buff, uint16_t s) { if (buff->byteOrder == BYTE_ORDER_BIG) { return htons(s); } @@ -48,7 +48,7 @@ bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset) { } // Get a byte from the byte buffer -bool BbGet(PBYTE_BUFFER buff, char* c) { +bool BbGet8(PBYTE_BUFFER buff, uint8_t* c) { if (buff->position + sizeof(*c) > buff->length) { return false; } @@ -60,7 +60,7 @@ bool BbGet(PBYTE_BUFFER buff, char* c) { } // Get a short from the byte buffer -bool BbGetShort(PBYTE_BUFFER buff, short* s) { +bool BbGet16(PBYTE_BUFFER buff, uint16_t* s) { if (buff->position + sizeof(*s) > buff->length) { return false; } @@ -68,13 +68,13 @@ bool BbGetShort(PBYTE_BUFFER buff, short* s) { memcpy(s, &buff->buffer[buff->position], sizeof(*s)); buff->position += sizeof(*s); - *s = byteSwapShort(buff, *s); + *s = byteSwap16(buff, *s); return true; } // Get an int from the byte buffer -bool BbGetInt(PBYTE_BUFFER buff, int* i) { +bool BbGet32(PBYTE_BUFFER buff, uint32_t* i) { if (buff->position + sizeof(*i) > buff->length) { return false; } @@ -82,13 +82,13 @@ bool BbGetInt(PBYTE_BUFFER buff, int* i) { memcpy(i, &buff->buffer[buff->position], sizeof(*i)); buff->position += sizeof(*i); - *i = byteSwapInt(buff, *i); + *i = byteSwap32(buff, *i); return true; } // Get a long from the byte buffer -bool BbGetLong(PBYTE_BUFFER buff, long long* l) { +bool BbGet64(PBYTE_BUFFER buff, uint64_t* l) { if (buff->position + sizeof(*l) > buff->length) { return false; } @@ -96,18 +96,18 @@ bool BbGetLong(PBYTE_BUFFER buff, long long* l) { memcpy(l, &buff->buffer[buff->position], sizeof(*l)); buff->position += sizeof(*l); - *l = byteSwapLongLong(buff, *l); + *l = byteSwap64(buff, *l); return true; } // Put an int into the byte buffer -bool BbPutInt(PBYTE_BUFFER buff, int i) { +bool BbPut32(PBYTE_BUFFER buff, uint32_t i) { if (buff->position + sizeof(i) > buff->length) { return false; } - i = byteSwapInt(buff, i); + i = byteSwap32(buff, i); memcpy(&buff->buffer[buff->position], &i, sizeof(i)); buff->position += sizeof(i); @@ -116,12 +116,12 @@ bool BbPutInt(PBYTE_BUFFER buff, int i) { } // Put a long into the byte buffer -bool BbPutLong(PBYTE_BUFFER buff, long long l) { +bool BbPut64(PBYTE_BUFFER buff, uint64_t l) { if (buff->position + sizeof(l) > buff->length) { return false; } - l = byteSwapLongLong(buff, l); + l = byteSwap64(buff, l); memcpy(&buff->buffer[buff->position], &l, sizeof(l)); buff->position += sizeof(l); @@ -130,12 +130,12 @@ bool BbPutLong(PBYTE_BUFFER buff, long long l) { } // Put a short into the byte buffer -bool BbPutShort(PBYTE_BUFFER buff, short s) { +bool BbPut16(PBYTE_BUFFER buff, uint16_t s) { if (buff->position + sizeof(s) > buff->length) { return false; } - s = byteSwapShort(buff, s); + s = byteSwap16(buff, s); memcpy(&buff->buffer[buff->position], &s, sizeof(s)); buff->position += sizeof(s); @@ -144,7 +144,7 @@ bool BbPutShort(PBYTE_BUFFER buff, short s) { } // Put a byte into the buffer -bool BbPut(PBYTE_BUFFER buff, char c) { +bool BbPut8(PBYTE_BUFFER buff, uint8_t c) { if (buff->position + sizeof(c) > buff->length) { return false; } diff --git a/src/ByteBuffer.h b/src/ByteBuffer.h index 1d874af..c416f46 100644 --- a/src/ByteBuffer.h +++ b/src/ByteBuffer.h @@ -27,12 +27,12 @@ typedef struct _BYTE_BUFFER { void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int length, int byteOrder); bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset); -bool BbGet(PBYTE_BUFFER buff, char* c); -bool BbGetShort(PBYTE_BUFFER buff, short* s); -bool BbGetInt(PBYTE_BUFFER buff, int* i); -bool BbGetLong(PBYTE_BUFFER buff, long long* l); +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 BbPutInt(PBYTE_BUFFER buff, int i); -bool BbPutShort(PBYTE_BUFFER buff, short s); -bool BbPut(PBYTE_BUFFER buff, char c); -bool BbPutLong(PBYTE_BUFFER buff, long long l); +bool BbPut8(PBYTE_BUFFER buff, uint8_t c); +bool BbPut16(PBYTE_BUFFER buff, uint16_t s); +bool BbPut32(PBYTE_BUFFER buff, uint32_t i); +bool BbPut64(PBYTE_BUFFER buff, uint64_t l); diff --git a/src/ControlStream.c b/src/ControlStream.c index 142b863..63581b0 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -544,13 +544,13 @@ static void controlReceiveThreadFunc(void* context) { BbInitializeWrappedBuffer(&bb, (char*)event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE); BbAdvanceBuffer(&bb, 4); - unsigned short controllerNumber; - unsigned short lowFreqRumble; - unsigned short highFreqRumble; + uint16_t controllerNumber; + uint16_t lowFreqRumble; + uint16_t highFreqRumble; - BbGetShort(&bb, (short*)&controllerNumber); - BbGetShort(&bb, (short*)&lowFreqRumble); - BbGetShort(&bb, (short*)&highFreqRumble); + BbGet16(&bb, &controllerNumber); + BbGet16(&bb, &lowFreqRumble); + BbGet16(&bb, &highFreqRumble); ListenerCallbacks.rumble(controllerNumber, lowFreqRumble, highFreqRumble); } @@ -559,10 +559,10 @@ static void controlReceiveThreadFunc(void* context) { BbInitializeWrappedBuffer(&bb, (char*)event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE); - unsigned short terminationReason; + uint16_t terminationReason; int terminationErrorCode; - BbGetShort(&bb, (short*)&terminationReason); + BbGet16(&bb, &terminationReason); Limelog("Server notified termination reason: 0x%04x\n", terminationReason); @@ -603,8 +603,8 @@ static void lossStatsThreadFunc(void* context) { char periodicPingPayload[8]; BbInitializeWrappedBuffer(&byteBuffer, periodicPingPayload, 0, sizeof(periodicPingPayload), BYTE_ORDER_LITTLE); - BbPutShort(&byteBuffer, 4); // Length of payload - BbPutInt(&byteBuffer, 0); // Timestamp? + BbPut16(&byteBuffer, 4); // Length of payload + BbPut32(&byteBuffer, 0); // Timestamp? while (!PltIsThreadInterrupted(&lossStatsThread)) { // Send the message (and don't expect a response) @@ -631,13 +631,13 @@ static void lossStatsThreadFunc(void* context) { while (!PltIsThreadInterrupted(&lossStatsThread)) { // Construct the payload BbInitializeWrappedBuffer(&byteBuffer, lossStatsPayload, 0, payloadLengths[IDX_LOSS_STATS], BYTE_ORDER_LITTLE); - BbPutInt(&byteBuffer, lossCountSinceLastReport); - BbPutInt(&byteBuffer, LOSS_REPORT_INTERVAL_MS); - BbPutInt(&byteBuffer, 1000); - BbPutLong(&byteBuffer, lastGoodFrame); - BbPutInt(&byteBuffer, 0); - BbPutInt(&byteBuffer, 0); - BbPutInt(&byteBuffer, 0x14); + BbPut32(&byteBuffer, lossCountSinceLastReport); + BbPut32(&byteBuffer, LOSS_REPORT_INTERVAL_MS); + BbPut32(&byteBuffer, 1000); + BbPut64(&byteBuffer, lastGoodFrame); + BbPut32(&byteBuffer, 0); + BbPut32(&byteBuffer, 0); + BbPut32(&byteBuffer, 0x14); // Send the message (and don't expect a response) if (!sendMessageAndForget(packetTypes[IDX_LOSS_STATS],