#include "getcontrol.h" #include "commands.h" #include "bitwise.h" #include <sys/types.h> enum GetcontrolData { CTRL_ID, CTRLVAL_ID, 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; 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]; return 0; }