Skip to content
Snippets Groups Projects
Unverified Commit ad27a49f authored by Jake Drahos's avatar Jake Drahos
Browse files

Created getcontrol/respcontrol

Hopefully there are no copy-paste bugs
parent c963f8db
No related branches found
No related tags found
No related merge requests found
#ifndef _bitwise_h
#define _bitwise_h
#include <string.h>
#include <stdint.h>
/* Bit shifting for endianness of 16-bit numbers */
#define LSByte16(x) ((x) & 0xff)
#define MSByte16(x) (((x) >> 8) & 0xff)
......@@ -12,18 +15,44 @@
* to do this involves actually implementing
* IEEE 754 in software to do so
*/
#define FloatByte1(x) (((char *) &(x))[0])
#define FloatByte2(x) (((char *) &(x))[1])
#define FloatByte3(x) (((char *) &(x))[2])
#define FloatByte4(x) (((char *) &(x))[3])
/* This is so much UB it hurts to write */
#define BytesToFloat(f, b1, b2, b3, b4) \
do { \
((char *) &(f))[0] = (b1); \
((char *) &(f))[1] = (b2); \
((char *) &(f))[2] = (b3); \
((char *) &(f))[3] = (b4); \
} while (0);
static inline uint8_t FloatByte1(float f)
{
char arr[4];
memcpy(arr, &f, 4);
return arr[0];
}
static inline uint8_t FloatByte2(float f)
{
char arr[4];
memcpy(arr, &f, 4);
return arr[1];
}
static inline uint8_t FloatByte3(float f)
{
char arr[4];
memcpy(arr, &f, 4);
return arr[2];
}
static inline uint8_t FloatByte4(float f)
{
char arr[4];
memcpy(arr, &f, 4);
return arr[3];
}
static inline float BytesToFloat(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
{
char arr[4];
arr[0] = b1;
arr[1] = b2;
arr[2] = b3;
arr[3] = b4;
float f;
memcpy(&f, arr, 4);
return f;
}
#endif
#include "getcontrol.h"
#include "commands.h"
#include "bitwise.h"
#include <sys/types.h>
enum GetcontrolData {
CTRL_ID,
CTRLVAL_ID,
VAL_1,
VAL_2,
VAL_3,
VAL_4,
GC_DATA_SIZE
};
/* Creates data and metadata for a respcontrol packet
* Returns data size.
*/
ssize_t EncodeGetcontrol(
struct metadata * m, /* data_len and msg_type will be populated*/
uint8_t * data, /* Output buffer */
size_t data_size, /* Max buffer size */
const struct controller_message * cm) /* Message to encode */
{
m->msg_type = GETCONTROL_ID;
m->data_len = GC_DATA_SIZE;
if (data_size < GC_DATA_SIZE) {
return -1;
}
data[CTRL_ID] = cm->id;
data[CTRLVAL_ID] = cm->value_id;
data[VAL_1] = FloatByte1(cm->value);
data[VAL_2] = FloatByte2(cm->value);
data[VAL_3] = FloatByte3(cm->value);
data[VAL_4] = FloatByte4(cm->value);
return GC_DATA_SIZE;
}
/* Decode a metadata and data to populate a controller.
* Returns 0 on success, -1 on failure.
*/
int DecodeGetcontrol(
struct controller_message * cm, /* Decoded controller message */
const struct metadata * m, /* Metadata to aid in decoding */
const uint8_t * data) /* Data to decode */
{
if (m->data_len < GC_DATA_SIZE) {
return -1;
}
if (m->msg_type != GETCONTROL_ID) {
return -1;
}
cm->id = data[CTRL_ID];
cm->value_id = data[CTRLVAL_ID];
cm->value = BytesToFloat(data[VAL_1], data[VAL_2],
data[VAL_3], data[VAL_4]);
return 0;
}
#ifndef _getcontrol_h
#define _getcontrol_h
#include "packet.h"
#include "controller.h"
#include <sys/types.h>
/* Creates data and metadata for a getcontrol packet.
* Returns data size.
*/
ssize_t EncodeGetcontrol(
struct metadata *m, /* Out */
uint8_t *data, /* Out */
size_t data_size, /* Data buffer max size */
const struct controller_message *cm); /* Value is not used; only IDs */
/* Decode a metadata and data to populate a message
* Returns 0 on success, -1 on failure
*/
int DecodeGetcontrol(
struct controller_message *cm, /* Out. Value is undefined */
const struct metadata *m, /* In */
const uint8_t * data); /* In */
#endif
#include "respcontrol.h"
#include "commands.h"
#include "bitwise.h"
#include <sys/types.h>
enum RespcontrolData {
CTRL_ID,
CTRLVAL_ID,
VAL_1,
VAL_2,
VAL_3,
VAL_4,
RC_DATA_SIZE
};
/* Creates data and metadata for a respcontrol packet
* Returns data size.
*/
ssize_t EncodeRespcontrol(
struct metadata * m, /* data_len and msg_type will be populated*/
uint8_t * data, /* Output buffer */
size_t data_size, /* Max buffer size */
const struct controller_message * cm) /* Message to encode */
{
m->msg_type = RESPCONTROL_ID;
m->data_len = RC_DATA_SIZE;
if (data_size < RC_DATA_SIZE) {
return -1;
}
data[CTRL_ID] = cm->id;
data[CTRLVAL_ID] = cm->value_id;
data[VAL_1] = FloatByte1(cm->value);
data[VAL_2] = FloatByte2(cm->value);
data[VAL_3] = FloatByte3(cm->value);
data[VAL_4] = FloatByte4(cm->value);
return RC_DATA_SIZE;
}
/* Decode a metadata and data to populate a controller.
* Returns 0 on success, -1 on failure.
*/
int DecodeRespcontrol(
struct controller_message * cm, /* Decoded controller message */
const struct metadata * m, /* Metadata to aid in decoding */
const uint8_t * data) /* Data to decode */
{
if (m->data_len < RC_DATA_SIZE) {
return -1;
}
if (m->msg_type != RESPCONTROL_ID) {
return -1;
}
cm->id = data[CTRL_ID];
cm->value_id = data[CTRLVAL_ID];
cm->value = BytesToFloat(data[VAL_1], data[VAL_2],
data[VAL_3], data[VAL_4]);
return 0;
}
#ifndef _respcontrol_h
#define _respcontrol_h
#include "packet.h"
#include "controller.h"
#include <sys/types.h>
/* Creates data and metadata for a respcontrol packet.
* Returns data size.
*/
ssize_t EncodeRespcontrol(
struct metadata *m, /* Out */
uint8_t *data, /* Out */
size_t data_size, /* Data buffer max size */
const struct controller_message *cm); /* In */
/* Decode a metadata and data to populate a message
* Returns 0 on success, -1 on failure
*/
int DecodeRespcontrol(
struct controller_message *cm, /* Out */
const struct metadata *m, /* In */
const uint8_t * data); /* In */
#endif
......@@ -55,13 +55,11 @@ int DecodeSetcontrol(
return -1;
}
/* Would violate strict aliasing to work directly on the struct member */
float val;
BytesToFloat(val, data[VAL_1], data[VAL_2], data[VAL_3], data[VAL_4]);
cm->id = data[CTRL_ID];
cm->value_id = data[CTRLVAL_ID];
cm->value = val;
cm->value = BytesToFloat(data[VAL_1], data[VAL_2],
data[VAL_3], data[VAL_4]);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment