|
|
@ -1,5 +1,7 @@
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include "includes/serialization.h"
|
|
|
|
#include "includes/serialization.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Takes in float values, casts them to uint16_t and creates a Serial_Data struct */
|
|
|
|
/* Takes in float values, casts them to uint16_t and creates a Serial_Data struct */
|
|
|
@ -13,20 +15,37 @@ Serial_Data Serial_create_data(float pad_x, float pad_y, float ball_x, float bal
|
|
|
|
return data;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Serializes a 'Data' struct into a byte array */
|
|
|
|
/* Serializes a 'Data' struct into a byte array, converted to network-byte order */
|
|
|
|
uint8_t* Serial_serialize(Serial_Data data) {
|
|
|
|
uint8_t* Serial_serialize(Serial_Data data) {
|
|
|
|
/* Create a pointer that can fit the entire struct */
|
|
|
|
/* Create a pointer that can fit the entire struct */
|
|
|
|
uint8_t* serialized = malloc(sizeof(Serial_Data));
|
|
|
|
uint8_t* serialized = malloc(4 * sizeof(uint16_t) + 1);
|
|
|
|
/* Store the data into the pointer, by using an incremented memory address for each successive store */
|
|
|
|
uint8_t* pad_x_ptr;
|
|
|
|
*serialized = data.pad_x;
|
|
|
|
uint8_t* pad_y_ptr;
|
|
|
|
*(serialized + sizeof(uint16_t)) = data.pad_y;
|
|
|
|
uint8_t* ball_x_ptr;
|
|
|
|
*(serialized + 2 * sizeof(uint16_t)) = data.ball_x;
|
|
|
|
uint8_t* ball_y_ptr;
|
|
|
|
*(serialized + 3 * sizeof(uint16_t)) = data.ball_y;
|
|
|
|
memset(serialized, 0, 4 * sizeof(uint16_t) + 1); // Zero out the memory
|
|
|
|
|
|
|
|
pad_x_ptr = serialized;
|
|
|
|
|
|
|
|
pad_y_ptr = pad_x_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
ball_x_ptr = pad_y_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
ball_y_ptr = ball_x_ptr + sizeof(uint16_t);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*((uint16_t *)pad_x_ptr) = data.pad_x;
|
|
|
|
|
|
|
|
*((uint16_t *)pad_x_ptr) = htons(*((uint16_t *)pad_x_ptr));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*((uint16_t *)pad_y_ptr) = data.pad_y;
|
|
|
|
|
|
|
|
*((uint16_t *)pad_y_ptr) = htons(*((uint16_t *)pad_y_ptr));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*((uint16_t *)ball_x_ptr) = data.ball_x;
|
|
|
|
|
|
|
|
*((uint16_t *)ball_x_ptr) = htons(*((uint16_t *)ball_x_ptr));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*((uint16_t *)ball_y_ptr) = data.ball_y;
|
|
|
|
|
|
|
|
*((uint16_t *)ball_y_ptr) = htons(*((uint16_t *)ball_y_ptr));
|
|
|
|
|
|
|
|
*(ball_y_ptr + sizeof(uint16_t)) = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
return serialized;
|
|
|
|
return serialized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Deserialize a byte array into a 'Data' struct */
|
|
|
|
/* Deserialize a byte array into a 'Data' struct, converted to host byte order */
|
|
|
|
Serial_Data Serial_deserialize(uint8_t* serialized) {
|
|
|
|
Serial_Data Serial_deserialize(uint8_t* serialized) {
|
|
|
|
Serial_Data deserialized;
|
|
|
|
Serial_Data deserialized;
|
|
|
|
/* Use successive chunks of memory address to create pointers to the data */
|
|
|
|
/* Use successive chunks of memory address to create pointers to the data */
|
|
|
@ -37,10 +56,17 @@ Serial_Data Serial_deserialize(uint8_t* serialized) {
|
|
|
|
|
|
|
|
|
|
|
|
/* Dereference (and cast) the pointers, and store them into the struct */
|
|
|
|
/* Dereference (and cast) the pointers, and store them into the struct */
|
|
|
|
deserialized.pad_x = *((uint16_t *)pad_x_ptr);
|
|
|
|
deserialized.pad_x = *((uint16_t *)pad_x_ptr);
|
|
|
|
|
|
|
|
deserialized.pad_x = ntohs(deserialized.pad_x);
|
|
|
|
|
|
|
|
|
|
|
|
deserialized.pad_y = *((uint16_t *)pad_y_ptr);
|
|
|
|
deserialized.pad_y = *((uint16_t *)pad_y_ptr);
|
|
|
|
|
|
|
|
deserialized.pad_y = ntohs(deserialized.pad_y);
|
|
|
|
|
|
|
|
|
|
|
|
deserialized.ball_x = *((uint16_t *)ball_x_ptr);
|
|
|
|
deserialized.ball_x = *((uint16_t *)ball_x_ptr);
|
|
|
|
|
|
|
|
deserialized.ball_x = ntohs(deserialized.ball_x);
|
|
|
|
|
|
|
|
|
|
|
|
deserialized.ball_y = *((uint16_t *)ball_y_ptr);
|
|
|
|
deserialized.ball_y = *((uint16_t *)ball_y_ptr);
|
|
|
|
|
|
|
|
deserialized.ball_y = ntohs(deserialized.ball_y);
|
|
|
|
|
|
|
|
|
|
|
|
return deserialized;
|
|
|
|
return deserialized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|