From edfd70c3cc700af8977f15d4a5cbd66d6fd7a362 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 26 Feb 2024 21:38:01 -0500 Subject: [PATCH] Created a simple data serialization library, to serialize and deserialize the data to be sent --- includes/serialization.h | 31 +++++++++++++++++++++++++++ serialization.c | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 includes/serialization.h create mode 100644 serialization.c diff --git a/includes/serialization.h b/includes/serialization.h new file mode 100644 index 0000000..870a0f4 --- /dev/null +++ b/includes/serialization.h @@ -0,0 +1,31 @@ +#ifndef _SERIALIZATION_H +#define _SERIALIZATION_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Struct used to hold the data that will be sent between sockets */ +typedef struct { + uint16_t pad_x; + uint16_t pad_y; + uint16_t ball_x; + uint16_t ball_y; +} Serial_Data; + +/* Create a Serial_Data struct from float values */ +Serial_Data Serial_create_data(float pad_x, float pad_y, float ball_x, float ball_y); + +/* Serialize a struct into a byte array, that can be sent through a socket */ +uint8_t* Serial_serialize(Serial_Data data); + +/* Deserialize a byte array into a struct, and return the struct */ +Serial_Data Serial_deserialize(uint8_t* serialized); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/serialization.c b/serialization.c new file mode 100644 index 0000000..6783f57 --- /dev/null +++ b/serialization.c @@ -0,0 +1,46 @@ +#include +#include +#include "includes/serialization.h" + +/* Takes in float values, casts them to uint16_t and creates a Serial_Data struct */ +Serial_Data Serial_create_data(float pad_x, float pad_y, float ball_x, float ball_y) { + Serial_Data data; + data.pad_x = (uint16_t)pad_x; + data.pad_y = (uint16_t)pad_y; + data.ball_x = (uint16_t)ball_x; + data.ball_y = (uint16_t)ball_y; + + return data; +} + +/* Serializes a 'Data' struct into a byte array */ +uint8_t* Serial_serialize(Serial_Data data) { + /* Create a pointer that can fit the entire struct */ + uint8_t* serialized = malloc(sizeof(Serial_Data)); + /* Store the data into the pointer, by using an incremented memory address for each successive store */ + *serialized = data.pad_x; + *(serialized + sizeof(uint16_t)) = data.pad_y; + *(serialized + 2 * sizeof(uint16_t)) = data.ball_x; + *(serialized + 3 * sizeof(uint16_t)) = data.ball_y; + + return serialized; +} + +/* Deserialize a byte array into a 'Data' struct */ +Serial_Data Serial_deserialize(uint8_t* serialized) { + Serial_Data deserialized; + /* Use successive chunks of memory address to create pointers to the data */ + uint8_t* pad_x_ptr = serialized; + uint8_t* pad_y_ptr = serialized + sizeof(uint16_t); + uint8_t* ball_x_ptr = pad_y_ptr + sizeof(uint16_t); + uint8_t* ball_y_ptr = ball_x_ptr + sizeof(uint16_t); + + /* Dereference (and cast) the pointers, and store them into the struct */ + deserialized.pad_x = *((uint16_t *)pad_x_ptr); + deserialized.pad_y = *((uint16_t *)pad_y_ptr); + deserialized.ball_x = *((uint16_t *)ball_x_ptr); + deserialized.ball_y = *((uint16_t *)ball_y_ptr); + + return deserialized; +} +