/* auto-generated, do not modify */
#include "config.h"

#include "fu-logitech-rallysystem-struct.h"
#include "fu-byte-array.h"
#include "fu-mem-private.h"
#include "fu-string.h"

#ifdef G_LOG_DOMAIN
  #undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "FuStruct"



static const gchar *
fu_usb_image_state_to_string(FuUsbImageState val)
{
    if (val == FU_USB_IMAGE_STATE_NEW)
        return "new";
    if (val == FU_USB_IMAGE_STATE_VALID)
        return "valid";
    if (val == FU_USB_IMAGE_STATE_INVALID)
        return "invalid";
    return NULL;
}
/* getters */
/**
 * fu_struct_audio_serial_number_get_mac_address: (skip):
 **/
const guint8 *
fu_struct_audio_serial_number_get_mac_address(const GByteArray *st, gsize *bufsz)
{
    g_return_val_if_fail(st != NULL, NULL);
    if (bufsz != NULL)
        *bufsz = 6;
    return st->data + 0;
}
/**
 * fu_struct_audio_serial_number_get_pid: (skip):
 **/
guint16
fu_struct_audio_serial_number_get_pid(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 6, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_audio_serial_number_get_year: (skip):
 **/
guint16
fu_struct_audio_serial_number_get_year(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 8, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_audio_serial_number_get_month: (skip):
 **/
guint8
fu_struct_audio_serial_number_get_month(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->data[10];
}
/**
 * fu_struct_audio_serial_number_get_day: (skip):
 **/
guint8
fu_struct_audio_serial_number_get_day(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->data[11];
}

/* setters */
/**
 * fu_struct_audio_serial_number_to_string: (skip):
 **/
static gchar *
fu_struct_audio_serial_number_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("AudioSerialNumber:\n");
    g_return_val_if_fail(st != NULL, NULL);
    {
        gsize bufsz = 0;
        const guint8 *buf = fu_struct_audio_serial_number_get_mac_address(st, &bufsz);
        g_autoptr(GString) tmp = g_string_new(NULL);
        for (gsize i = 0; i < bufsz; i++)
            g_string_append_printf(tmp, "%02X", buf[i]);
        g_string_append_printf(str, "  mac_address: 0x%s\n", tmp->str);
    }
    g_string_append_printf(str, "  pid: 0x%x\n",
                           (guint) fu_struct_audio_serial_number_get_pid(st));
    g_string_append_printf(str, "  year: 0x%x\n",
                           (guint) fu_struct_audio_serial_number_get_year(st));
    g_string_append_printf(str, "  month: 0x%x\n",
                           (guint) fu_struct_audio_serial_number_get_month(st));
    g_string_append_printf(str, "  day: 0x%x\n",
                           (guint) fu_struct_audio_serial_number_get_day(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_audio_serial_number_parse: (skip):
 **/
GByteArray *
fu_struct_audio_serial_number_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(GByteArray) st = g_byte_array_new();
    g_autofree gchar *str = NULL;
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 12, error)) {
        g_prefix_error(error, "invalid struct AudioSerialNumber: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 12);
    str = fu_struct_audio_serial_number_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
/* getters */

/* setters */
/**
 * fu_struct_usb_init_request_set_id: (skip):
 **/
static void
fu_struct_usb_init_request_set_id(GByteArray *st, FuUsbCmdId value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_init_request_set_status: (skip):
 **/
static void
fu_struct_usb_init_request_set_status(GByteArray *st, FuUsbCmdStatus value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->data + 2, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_init_request_set_len: (skip):
 **/
static void
fu_struct_usb_init_request_set_len(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_init_request_new: (skip):
 **/
GByteArray *
fu_struct_usb_init_request_new(void)
{
    GByteArray *st = g_byte_array_sized_new(8);
    fu_byte_array_set_size(st, 8, 0x0);
    fu_struct_usb_init_request_set_id(st, FU_USB_CMD_ID_INIT);
    fu_struct_usb_init_request_set_status(st, FU_USB_CMD_STATUS_INIT_REQ);
    fu_struct_usb_init_request_set_len(st, 0);
    return st;
}
/* getters */
/**
 * fu_struct_usb_init_response_get_id: (skip):
 **/
static FuUsbCmdId
fu_struct_usb_init_response_get_id(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_init_response_get_status: (skip):
 **/
static FuUsbCmdStatus
fu_struct_usb_init_response_get_status(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 2, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_init_response_get_len: (skip):
 **/
static guint32
fu_struct_usb_init_response_get_len(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 4, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_usb_init_response_to_string: (skip):
 **/
static gchar *
fu_struct_usb_init_response_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("UsbInitResponse:\n");
    g_return_val_if_fail(st != NULL, NULL);
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_usb_init_response_parse: (skip):
 **/
GByteArray *
fu_struct_usb_init_response_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(GByteArray) st = g_byte_array_new();
    g_autofree gchar *str = NULL;
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 8, error)) {
        g_prefix_error(error, "invalid struct UsbInitResponse: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 8);
    if (fu_struct_usb_init_response_get_id(st) != FU_USB_CMD_ID_INIT) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant UsbInitResponse.id was not valid, expected FU_USB_CMD_ID_INIT");
        return NULL;
    }
    if (fu_struct_usb_init_response_get_status(st) != FU_USB_CMD_STATUS_INIT_REQ_ACK) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant UsbInitResponse.status was not valid, expected FU_USB_CMD_STATUS_INIT_REQ_ACK");
        return NULL;
    }
    if (fu_struct_usb_init_response_get_len(st) != 0) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant UsbInitResponse.len was not valid, expected 0");
        return NULL;
    }
    str = fu_struct_usb_init_response_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
/* getters */

/* setters */
/**
 * fu_struct_usb_firmware_download_request_set_id: (skip):
 **/
static void
fu_struct_usb_firmware_download_request_set_id(GByteArray *st, FuUsbCmdId value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_firmware_download_request_set_status: (skip):
 **/
static void
fu_struct_usb_firmware_download_request_set_status(GByteArray *st, FuUsbCmdStatus value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->data + 2, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_firmware_download_request_set_len: (skip):
 **/
void
fu_struct_usb_firmware_download_request_set_len(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_firmware_download_request_set_fw_version: (skip):
 **/
gboolean
fu_struct_usb_firmware_download_request_set_fw_version(GByteArray *st, const gchar *value, GError **error)
{
    gsize len;
    g_return_val_if_fail(st != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    if (value == NULL) {
        memset(st->data + 8, 0x0, 16);
        return TRUE;
    }
    len = strlen(value);
    if (len > 16) {
        g_set_error(error,
                    G_IO_ERROR,
                    G_IO_ERROR_INVALID_DATA,
                    "string '%s' (0x%x bytes) does not fit in UsbFirmwareDownloadRequest.fw_version (0x%x bytes)",
                    value, (guint) len, (guint) 16);
        return FALSE;
    }
    return fu_memcpy_safe(st->data, st->len, 8, (const guint8 *)value, len, 0x0, len, error);
}
/**
 * fu_struct_usb_firmware_download_request_new: (skip):
 **/
GByteArray *
fu_struct_usb_firmware_download_request_new(void)
{
    GByteArray *st = g_byte_array_sized_new(24);
    fu_byte_array_set_size(st, 24, 0x0);
    fu_struct_usb_firmware_download_request_set_id(st, FU_USB_CMD_ID_FIRMWARE_DOWNLOAD);
    fu_struct_usb_firmware_download_request_set_status(st, FU_USB_CMD_STATUS_REQ);
    return st;
}
/* getters */
/**
 * fu_struct_usb_firmware_download_response_get_id: (skip):
 **/
static FuUsbCmdId
fu_struct_usb_firmware_download_response_get_id(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_firmware_download_response_get_status: (skip):
 **/
static FuUsbCmdStatus
fu_struct_usb_firmware_download_response_get_status(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->data + 2, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_firmware_download_response_get_len: (skip):
 **/
guint32
fu_struct_usb_firmware_download_response_get_len(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 4, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_usb_firmware_download_response_to_string: (skip):
 **/
static gchar *
fu_struct_usb_firmware_download_response_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("UsbFirmwareDownloadResponse:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  len: 0x%x\n",
                           (guint) fu_struct_usb_firmware_download_response_get_len(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_usb_firmware_download_response_parse: (skip):
 **/
GByteArray *
fu_struct_usb_firmware_download_response_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(GByteArray) st = g_byte_array_new();
    g_autofree gchar *str = NULL;
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 8, error)) {
        g_prefix_error(error, "invalid struct UsbFirmwareDownloadResponse: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 8);
    if (fu_struct_usb_firmware_download_response_get_id(st) != FU_USB_CMD_ID_FIRMWARE_DOWNLOAD) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant UsbFirmwareDownloadResponse.id was not valid, expected FU_USB_CMD_ID_FIRMWARE_DOWNLOAD");
        return NULL;
    }
    if (fu_struct_usb_firmware_download_response_get_status(st) != FU_USB_CMD_STATUS_OK) {
        g_set_error_literal(error,
                            G_IO_ERROR,
                            G_IO_ERROR_INVALID_DATA,
                            "constant UsbFirmwareDownloadResponse.status was not valid, expected FU_USB_CMD_STATUS_OK");
        return NULL;
    }
    str = fu_struct_usb_firmware_download_response_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
/* getters */

/* setters */
/**
 * fu_struct_usb_read_version_request_set_id: (skip):
 **/
static void
fu_struct_usb_read_version_request_set_id(GByteArray *st, FuUsbCmdId value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_read_version_request_set_status: (skip):
 **/
static void
fu_struct_usb_read_version_request_set_status(GByteArray *st, FuUsbCmdStatus value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->data + 2, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_read_version_request_set_len: (skip):
 **/
static void
fu_struct_usb_read_version_request_set_len(GByteArray *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_usb_read_version_request_new: (skip):
 **/
GByteArray *
fu_struct_usb_read_version_request_new(void)
{
    GByteArray *st = g_byte_array_sized_new(8);
    fu_byte_array_set_size(st, 8, 0x0);
    fu_struct_usb_read_version_request_set_id(st, FU_USB_CMD_ID_READ_VERSION);
    fu_struct_usb_read_version_request_set_status(st, FU_USB_CMD_STATUS_REQ);
    fu_struct_usb_read_version_request_set_len(st, 0);
    return st;
}
/* getters */
/**
 * fu_struct_usb_read_version_response_get_fw_version: (skip):
 **/
gchar *
fu_struct_usb_read_version_response_get_fw_version(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->data, st->len, 0, 16, NULL);
}
/**
 * fu_struct_usb_read_version_response_get_img_state: (skip):
 **/
FuUsbImageState
fu_struct_usb_read_version_response_get_img_state(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 16, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_usb_read_version_response_to_string: (skip):
 **/
static gchar *
fu_struct_usb_read_version_response_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("UsbReadVersionResponse:\n");
    g_return_val_if_fail(st != NULL, NULL);
    {
        g_autofree gchar *tmp = fu_struct_usb_read_version_response_get_fw_version(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  fw_version: %s\n", tmp);
    }
    {
        const gchar *tmp = fu_usb_image_state_to_string(fu_struct_usb_read_version_response_get_img_state(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  img_state: 0x%x [%s]\n", (guint) fu_struct_usb_read_version_response_get_img_state(st), tmp);
        } else {
            g_string_append_printf(str, "  img_state: 0x%x\n", (guint) fu_struct_usb_read_version_response_get_img_state(st));
        }
    }
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_usb_read_version_response_parse: (skip):
 **/
GByteArray *
fu_struct_usb_read_version_response_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(GByteArray) st = g_byte_array_new();
    g_autofree gchar *str = NULL;
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 20, error)) {
        g_prefix_error(error, "invalid struct UsbReadVersionResponse: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 20);
    str = fu_struct_usb_read_version_response_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
/* getters */
/**
 * fu_struct_usb_progress_response_get_completed: (skip):
 **/
guint32
fu_struct_usb_progress_response_get_completed(const GByteArray *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->data + 0, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_usb_progress_response_to_string: (skip):
 **/
static gchar *
fu_struct_usb_progress_response_to_string(const GByteArray *st)
{
    g_autoptr(GString) str = g_string_new("UsbProgressResponse:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  completed: 0x%x\n",
                           (guint) fu_struct_usb_progress_response_get_completed(st));
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
/**
 * fu_struct_usb_progress_response_parse: (skip):
 **/
GByteArray *
fu_struct_usb_progress_response_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(GByteArray) st = g_byte_array_new();
    g_autofree gchar *str = NULL;
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 4, error)) {
        g_prefix_error(error, "invalid struct UsbProgressResponse: ");
        return NULL;
    }
    g_byte_array_append(st, buf + offset, 4);
    str = fu_struct_usb_progress_response_to_string(st);
    g_debug("%s", str);
    return g_steal_pointer(&st);
}
