Skip to content
Snippets Groups Projects
getparam.c 1.21 KiB
#include "getparam.h"
#include "commands.h"
#include "bitwise.h"

#include <sys/types.h>

enum GetparamData {
	CTRL_ID,
	CTRLVAL_ID,
	GP_DATA_SIZE
};

/* Creates data and metadata for a respcontrol packet
 * Returns data size.
 */
ssize_t EncodeGetparam(
        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 = GETPARAM_ID;
	m->data_len = GP_DATA_SIZE;

	if (data_size < GP_DATA_SIZE) {
		return -1;
	}

	data[CTRL_ID] = cm->id;
	data[CTRLVAL_ID] = cm->value_id;

	return GP_DATA_SIZE;
}

/* Decode a metadata and data to populate a controller.
 * Returns 0 on success, -1 on failure.
 */
int DecodeGetparam(
        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 < GP_DATA_SIZE) {
		return -1;
	}
	if (m->msg_type != GETPARAM_ID) {
		return -1;
	}

	cm->id = data[CTRL_ID];
	cm->value_id = data[CTRLVAL_ID];

	return 0;
}