Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/bot_speak_frame_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
void botSpeak_serialize(void* sourceBuffer, uint8_t numberElements, uint8_t elementSize, uint8_t* destinationBuffer, uint8_t* destinationLength) {

uint8_t* source_pointer = (uint8_t*)sourceBuffer;
int index;

// Use memcpy for direct copy since endianness does not change
memcpy(destinationBuffer, source_pointer, numberElements * elementSize);
Expand All @@ -17,7 +16,6 @@ void botSpeak_serialize(void* sourceBuffer, uint8_t numberElements, uint8_t elem
void botSpeak_deserialize(void* destinationBuffer, uint8_t *numberElements, uint8_t elementSize, uint8_t* sourceBuffer, uint8_t sourceLength) {

uint8_t* destination_pointer = (uint8_t*)destinationBuffer;
int index;

// Use memcpy for direct copy since endianness does not change
memcpy(destination_pointer, sourceBuffer, sourceLength);
Expand Down Expand Up @@ -69,15 +67,9 @@ int botSpeak_unpackFrame(DataFrame_TypeDef* destinationFrame, uint8_t* sourceBuf
// Extract frame ID
memcpy(&destinationFrame->frameID, &sourceBuffer[6], sizeof(destinationFrame->frameID));

// Allocate memory for data if data length is greater than zero
// Extract data
if (destinationFrame->dataLength > 0) {
destinationFrame->data = (uint8_t*)malloc(destinationFrame->dataLength);
if (!destinationFrame->data) {
return -ENOMEM; // Memory allocation failed
}
memcpy(destinationFrame->data, &sourceBuffer[10], destinationFrame->dataLength);
} else {
destinationFrame->data = NULL;
}

return 0;
Expand Down
8 changes: 1 addition & 7 deletions src/bot_speak_frame_ops_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,9 @@ int botSpeak_unpackFrame(DataFrame_TypeDef* destinationFrame, uint8_t* sourceBuf
// Extract frame ID
destinationFrame->frameID = (sourceBuffer[6] << 24) | (sourceBuffer[7] << 16) | (sourceBuffer[8] << 8) | (sourceBuffer[9]);

// Allocate memory for data if data length is greater than zero
// Extract data
if (destinationFrame->dataLength > 0) {
destinationFrame->data = (uint8_t*)malloc(destinationFrame->dataLength);
if (!destinationFrame->data) {
return -ENOMEM; // Memory allocation failed
}
memcpy(destinationFrame->data, &sourceBuffer[10], destinationFrame->dataLength);
} else {
destinationFrame->data = NULL;
}

return 0;
Expand Down
4 changes: 4 additions & 0 deletions test/test_frame_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ int testRequestFrameUnpacking(DataFrame_TypeDef* expectedFrame, uint8_t* sourceB
int testResponseFrameUnpacking(DataFrame_TypeDef* expectedFrame, uint8_t* sourceBuffer, uint8_t sourceLength) {
int status;
DataFrame_TypeDef actualFrame;
uint8_t dataBuffer[TEST_BUFFER_SIZE]; // Temporary buffer to hold data
actualFrame.data = dataBuffer; // Point the data pointer to the temporary buffer

// Attempt to unpack the frame from the source buffer, and store the result in `actualFrame`.
printf("\n\nTesting response frame unpacking...\n");
Expand Down Expand Up @@ -376,6 +378,8 @@ int testMotorCurrentResponseFrameUnpacking() {

// Unpack the response frame from the byte-array
DataFrame_TypeDef actualResponseFrame;
uint8_t actualDataBuffer[SERIALIZED_MOTOR_CURRENT_BYTES]; // Temporary buffer to hold data
actualResponseFrame.data = actualDataBuffer; // Point the data pointer to the temporary buffer
status = botSpeak_unpackFrame(&actualResponseFrame, responseBuffer, sizeof(responseBuffer));

// Run the unpacking test and check the status
Expand Down
4 changes: 4 additions & 0 deletions test/test_frame_ops_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ int testRequestFrameUnpacking(DataFrame_TypeDef* expectedFrame, uint8_t* sourceB
int testResponseFrameUnpacking(DataFrame_TypeDef* expectedFrame, uint8_t* sourceBuffer, uint8_t sourceLength) {
int status;
DataFrame_TypeDef actualFrame;
uint8_t dataBuffer[TEST_BUFFER_SIZE]; // Temporary buffer to hold data
actualFrame.data = dataBuffer; // Point the data pointer to the temporary buffer

// Attempt to unpack the frame from the source buffer, and store the result in `actualFrame`.
printf("\n\nTesting response frame unpacking...\n");
Expand Down Expand Up @@ -376,6 +378,8 @@ int testMotorCurrentResponseFrameUnpacking() {

// Unpack the response frame from the byte-array
DataFrame_TypeDef actualResponseFrame;
uint8_t actualDataBuffer[TEST_BUFFER_SIZE]; // Temporary buffer to hold data
actualResponseFrame.data = actualDataBuffer; // Point the data pointer to the temporary buffer
status = botSpeak_unpackFrame(&actualResponseFrame, responseBuffer, sizeof(responseBuffer));

// Run the unpacking test and check the status
Expand Down