/*
 * This file is auto-generated, do not modify!
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <glib.h>

#include "fu-usi-dock-struct.h"
#include "fu-byte-array.h"
#include "fu-bytes.h"
#include "fu-mem-private.h"
#include "fu-string.h"

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

/**
 * fu_usi_dock_spi_state_to_string:
 * @val: value, e.g. %FU_USI_DOCK_SPI_STATE_SWITCH_SUCCESS
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_usi_dock_spi_state_to_string(FuUsiDockSpiState val)
{
    if (val == FU_USI_DOCK_SPI_STATE_NONE)
        return "none";
    if (val == FU_USI_DOCK_SPI_STATE_SWITCH_SUCCESS)
        return "switch-success";
    if (val == FU_USI_DOCK_SPI_STATE_SWITCH_FAIL)
        return "switch-fail";
    if (val == FU_USI_DOCK_SPI_STATE_CMD_SUCCESS)
        return "cmd-success";
    if (val == FU_USI_DOCK_SPI_STATE_CMD_FAIL)
        return "cmd-fail";
    if (val == FU_USI_DOCK_SPI_STATE_RW_SUCCESS)
        return "rw-success";
    if (val == FU_USI_DOCK_SPI_STATE_RW_FAIL)
        return "rw-fail";
    if (val == FU_USI_DOCK_SPI_STATE_READY)
        return "ready";
    if (val == FU_USI_DOCK_SPI_STATE_BUSY)
        return "busy";
    if (val == FU_USI_DOCK_SPI_STATE_TIMEOUT)
        return "timeout";
    if (val == FU_USI_DOCK_SPI_STATE_FLASH_FOUND)
        return "flash-found";
    if (val == FU_USI_DOCK_SPI_STATE_FLASH_NOT_FOUND)
        return "flash-not-found";
    return NULL;
}

/**
 * fu_usi_dock_firmware_idx_to_string:
 * @val: value, e.g. %FU_USI_DOCK_FIRMWARE_IDX_DMC_PD
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_usi_dock_firmware_idx_to_string(FuUsiDockFirmwareIdx val)
{
    if (val == FU_USI_DOCK_FIRMWARE_IDX_NONE)
        return "none";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_DMC_PD)
        return "dmc-pd";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_DP)
        return "dp";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_TBT4)
        return "tbt4";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_USB3)
        return "usb3";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_USB2)
        return "usb2";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_AUDIO)
        return "audio";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_I225)
        return "i225";
    if (val == FU_USI_DOCK_FIRMWARE_IDX_MCU)
        return "mcu";
    return NULL;
}

/**
 * fu_usi_dock_tag2_to_string:
 * @val: value, e.g. %FU_USI_DOCK_TAG2_ISP
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
static const gchar *
fu_usi_dock_tag2_to_string(FuUsiDockTag2 val)
{
    if (val == FU_USI_DOCK_TAG2_ISP_BOOT)
        return "isp-boot";
    if (val == FU_USI_DOCK_TAG2_ISP)
        return "isp";
    if (val == FU_USI_DOCK_TAG2_CMD_MCU)
        return "cmd-mcu";
    if (val == FU_USI_DOCK_TAG2_CMD_SPI)
        return "cmd-spi";
    if (val == FU_USI_DOCK_TAG2_CMD_I2C)
        return "cmd-i2c";
    if (val == FU_USI_DOCK_TAG2_MASS_DATA_MCU)
        return "mass-data-mcu";
    if (val == FU_USI_DOCK_TAG2_MASS_DATA_SPI)
        return "mass-data-spi";
    if (val == FU_USI_DOCK_TAG2_MASS_DATA_I2C)
        return "mass-data-i2c";
    return NULL;
}

/**
 * fu_usi_dock_mcu_cmd_to_string:
 * @val: value, e.g. %FU_USI_DOCK_MCU_CMD_MCU_STATUS
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
static const gchar *
fu_usi_dock_mcu_cmd_to_string(FuUsiDockMcuCmd val)
{
    if (val == FU_USI_DOCK_MCU_CMD_MCU_NONE)
        return "mcu-none";
    if (val == FU_USI_DOCK_MCU_CMD_MCU_STATUS)
        return "mcu-status";
    if (val == FU_USI_DOCK_MCU_CMD_MCU_JUMP2BOOT)
        return "mcu-jump2boot";
    if (val == FU_USI_DOCK_MCU_CMD_READ_MCU_VERSIONPAGE)
        return "read-mcu-versionpage";
    if (val == FU_USI_DOCK_MCU_CMD_SET_I225_PWR)
        return "set-i225-pwr";
    if (val == FU_USI_DOCK_MCU_CMD_DOCK_RESET)
        return "dock-reset";
    if (val == FU_USI_DOCK_MCU_CMD_VERSION_WRITEBACK)
        return "version-writeback";
    if (val == FU_USI_DOCK_MCU_CMD_SET_CHIP_TYPE)
        return "set-chip-type";
    if (val == FU_USI_DOCK_MCU_CMD_FW_INITIAL)
        return "fw-initial";
    if (val == FU_USI_DOCK_MCU_CMD_FW_UPDATE)
        return "fw-update";
    if (val == FU_USI_DOCK_MCU_CMD_FW_TARGET_CHECKSUM)
        return "fw-target-checksum";
    if (val == FU_USI_DOCK_MCU_CMD_FW_ISP_END)
        return "fw-isp-end";
    if (val == FU_USI_DOCK_MCU_CMD_SET_PHASE2_DISCONNECT)
        return "set-phase2-disconnect";
    if (val == FU_USI_DOCK_MCU_CMD_SET_PHASE2_DELAY)
        return "set-phase2-delay";
    if (val == FU_USI_DOCK_MCU_CMD_SET_PHASE2_TIMEOUT)
        return "set-phase2-timeout";
    if (val == FU_USI_DOCK_MCU_CMD_GET_PHASE2_PARAMETER)
        return "get-phase2-parameter";
    if (val == FU_USI_DOCK_MCU_CMD_ALL)
        return "all";
    return NULL;
}

/**
 * fu_struct_usi_dock_hid_req_ref: (skip):
 **/
FuStructUsiDockHidReq *
fu_struct_usi_dock_hid_req_ref(FuStructUsiDockHidReq *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_usi_dock_hid_req_unref: (skip):
 **/
void
fu_struct_usi_dock_hid_req_unref(FuStructUsiDockHidReq *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructUsiDockHidReq refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_usi_dock_hid_req_new_internal: (skip):
 **/
static FuStructUsiDockHidReq *
fu_struct_usi_dock_hid_req_new_internal(void)
{
    FuStructUsiDockHidReq *st = g_new0(FuStructUsiDockHidReq, 1);
    st->refcount = 1;
    return st;
}

/* getters */

/* setters */
/**
 * fu_struct_usi_dock_hid_req_set_id: (skip):
 **/
static void
fu_struct_usi_dock_hid_req_set_id(FuStructUsiDockHidReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[0] = value;
}
/**
 * fu_struct_usi_dock_hid_req_set_length: (skip):
 **/
void
fu_struct_usi_dock_hid_req_set_length(FuStructUsiDockHidReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[1] = value;
}
/**
 * fu_struct_usi_dock_hid_req_set_buf: (skip):
 **/
gboolean
fu_struct_usi_dock_hid_req_set_buf(FuStructUsiDockHidReq *st, const guint8 *buf, gsize bufsz, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    g_return_val_if_fail(buf != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    return fu_memcpy_safe(st->buf->data, st->buf->len, 2, buf, bufsz, 0x0, bufsz, error);
}
/**
 * fu_struct_usi_dock_hid_req_set_tag3: (skip):
 **/
void
fu_struct_usi_dock_hid_req_set_tag3(FuStructUsiDockHidReq *st, FuUsiDockTag2 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[63] = value;
}
/**
 * fu_struct_usi_dock_hid_req_new: (skip):
 **/
FuStructUsiDockHidReq *
fu_struct_usi_dock_hid_req_new(void)
{
    FuStructUsiDockHidReq *st = fu_struct_usi_dock_hid_req_new_internal();
    st->buf = g_byte_array_sized_new(64);
    fu_byte_array_set_size(st->buf, 64, 0x0);
    fu_struct_usi_dock_hid_req_set_id(st, 2);
    return st;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_ref: (skip):
 **/
FuStructUsiDockMcuCmdReq *
fu_struct_usi_dock_mcu_cmd_req_ref(FuStructUsiDockMcuCmdReq *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_unref: (skip):
 **/
void
fu_struct_usi_dock_mcu_cmd_req_unref(FuStructUsiDockMcuCmdReq *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructUsiDockMcuCmdReq refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_new_internal: (skip):
 **/
static FuStructUsiDockMcuCmdReq *
fu_struct_usi_dock_mcu_cmd_req_new_internal(void)
{
    FuStructUsiDockMcuCmdReq *st = g_new0(FuStructUsiDockMcuCmdReq, 1);
    st->refcount = 1;
    return st;
}

/* getters */

/* setters */
/**
 * fu_struct_usi_dock_mcu_cmd_req_set_id: (skip):
 **/
static void
fu_struct_usi_dock_mcu_cmd_req_set_id(FuStructUsiDockMcuCmdReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[0] = value;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_set_length: (skip):
 **/
void
fu_struct_usi_dock_mcu_cmd_req_set_length(FuStructUsiDockMcuCmdReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[1] = value;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_set_tag1: (skip):
 **/
static void
fu_struct_usi_dock_mcu_cmd_req_set_tag1(FuStructUsiDockMcuCmdReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[2] = value;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_set_tag2: (skip):
 **/
static void
fu_struct_usi_dock_mcu_cmd_req_set_tag2(FuStructUsiDockMcuCmdReq *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[3] = value;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_set_buf: (skip):
 **/
gboolean
fu_struct_usi_dock_mcu_cmd_req_set_buf(FuStructUsiDockMcuCmdReq *st, const guint8 *buf, gsize bufsz, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    g_return_val_if_fail(buf != NULL, FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    return fu_memcpy_safe(st->buf->data, st->buf->len, 4, buf, bufsz, 0x0, bufsz, error);
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_set_tag3: (skip):
 **/
void
fu_struct_usi_dock_mcu_cmd_req_set_tag3(FuStructUsiDockMcuCmdReq *st, FuUsiDockTag2 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[63] = value;
}
/**
 * fu_struct_usi_dock_mcu_cmd_req_new: (skip):
 **/
FuStructUsiDockMcuCmdReq *
fu_struct_usi_dock_mcu_cmd_req_new(void)
{
    FuStructUsiDockMcuCmdReq *st = fu_struct_usi_dock_mcu_cmd_req_new_internal();
    st->buf = g_byte_array_sized_new(64);
    fu_byte_array_set_size(st->buf, 64, 0x0);
    fu_struct_usi_dock_mcu_cmd_req_set_id(st, 2);
    fu_struct_usi_dock_mcu_cmd_req_set_tag1(st, 0xFE);
    fu_struct_usi_dock_mcu_cmd_req_set_tag2(st, 0xFF);
    return st;
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_ref: (skip):
 **/
FuStructUsiDockMcuCmdRes *
fu_struct_usi_dock_mcu_cmd_res_ref(FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_unref: (skip):
 **/
void
fu_struct_usi_dock_mcu_cmd_res_unref(FuStructUsiDockMcuCmdRes *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructUsiDockMcuCmdRes refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_new_internal: (skip):
 **/
static FuStructUsiDockMcuCmdRes *
fu_struct_usi_dock_mcu_cmd_res_new_internal(void)
{
    FuStructUsiDockMcuCmdRes *st = g_new0(FuStructUsiDockMcuCmdRes, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_id: (skip):
 **/
static guint8
fu_struct_usi_dock_mcu_cmd_res_get_id(const FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[0];
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_cmd1: (skip):
 **/
FuUsiDockMcuCmd
fu_struct_usi_dock_mcu_cmd_res_get_cmd1(const FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[1];
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_tag1: (skip):
 **/
static guint8
fu_struct_usi_dock_mcu_cmd_res_get_tag1(const FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[2];
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_tag2: (skip):
 **/
static guint8
fu_struct_usi_dock_mcu_cmd_res_get_tag2(const FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[3];
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_cmd2: (skip):
 **/
FuUsiDockMcuCmd
fu_struct_usi_dock_mcu_cmd_res_get_cmd2(const FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[4];
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_buf: (skip):
 **/
const guint8 *
fu_struct_usi_dock_mcu_cmd_res_get_buf(const FuStructUsiDockMcuCmdRes *st, gsize *bufsz)
{
    g_return_val_if_fail(st != NULL, NULL);
    if (bufsz != NULL)
        *bufsz = 58;
    return st->buf->data + 5;
}
/**
 * fu_struct_usi_dock_mcu_cmd_res_get_tag3: (skip):
 **/
FuUsiDockTag2
fu_struct_usi_dock_mcu_cmd_res_get_tag3(const FuStructUsiDockMcuCmdRes *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[63];
}

/* setters */
/**
 * fu_struct_usi_dock_mcu_cmd_res_to_string: (skip):
 **/
static gchar *
fu_struct_usi_dock_mcu_cmd_res_to_string(const FuStructUsiDockMcuCmdRes *st)
{
    g_autoptr(GString) str = g_string_new("FuStructUsiDockMcuCmdRes:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  id: 0x%x\n",
                           (guint) fu_struct_usi_dock_mcu_cmd_res_get_id(st));
    {
        const gchar *tmp = fu_usi_dock_mcu_cmd_to_string(fu_struct_usi_dock_mcu_cmd_res_get_cmd1(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  cmd1: 0x%x [%s]\n", (guint) fu_struct_usi_dock_mcu_cmd_res_get_cmd1(st), tmp);
        } else {
            g_string_append_printf(str, "  cmd1: 0x%x\n", (guint) fu_struct_usi_dock_mcu_cmd_res_get_cmd1(st));
        }
    }
    g_string_append_printf(str, "  tag1: 0x%x\n",
                           (guint) fu_struct_usi_dock_mcu_cmd_res_get_tag1(st));
    g_string_append_printf(str, "  tag2: 0x%x\n",
                           (guint) fu_struct_usi_dock_mcu_cmd_res_get_tag2(st));
    {
        const gchar *tmp = fu_usi_dock_mcu_cmd_to_string(fu_struct_usi_dock_mcu_cmd_res_get_cmd2(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  cmd2: 0x%x [%s]\n", (guint) fu_struct_usi_dock_mcu_cmd_res_get_cmd2(st), tmp);
        } else {
            g_string_append_printf(str, "  cmd2: 0x%x\n", (guint) fu_struct_usi_dock_mcu_cmd_res_get_cmd2(st));
        }
    }
    {
        gsize bufsz = 0;
        const guint8 *buf = fu_struct_usi_dock_mcu_cmd_res_get_buf(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, "  buf: 0x%s\n", tmp->str);
    }
    {
        const gchar *tmp = fu_usi_dock_tag2_to_string(fu_struct_usi_dock_mcu_cmd_res_get_tag3(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  tag3: 0x%x [%s]\n", (guint) fu_struct_usi_dock_mcu_cmd_res_get_tag3(st), tmp);
        } else {
            g_string_append_printf(str, "  tag3: 0x%x\n", (guint) fu_struct_usi_dock_mcu_cmd_res_get_tag3(st));
        }
    }
    if (str->len > 0)
        g_string_set_size(str, str->len - 1);
    return g_string_free(g_steal_pointer(&str), FALSE);
}
static gboolean
fu_struct_usi_dock_mcu_cmd_res_validate_internal(FuStructUsiDockMcuCmdRes *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_usi_dock_mcu_cmd_res_get_id(st) != 2) {
        g_set_error_literal(error,
                            FWUPD_ERROR,
                            FWUPD_ERROR_INVALID_DATA,
                            "constant FuStructUsiDockMcuCmdRes.id was not valid");
        return FALSE;
    }
    if (fu_struct_usi_dock_mcu_cmd_res_get_tag1(st) != 0xFE) {
        g_set_error_literal(error,
                            FWUPD_ERROR,
                            FWUPD_ERROR_INVALID_DATA,
                            "constant FuStructUsiDockMcuCmdRes.tag1 was not valid");
        return FALSE;
    }
    if (fu_struct_usi_dock_mcu_cmd_res_get_tag2(st) != 0xFF) {
        g_set_error_literal(error,
                            FWUPD_ERROR,
                            FWUPD_ERROR_INVALID_DATA,
                            "constant FuStructUsiDockMcuCmdRes.tag2 was not valid");
        return FALSE;
    }
    return TRUE;
}
static gboolean
fu_struct_usi_dock_mcu_cmd_res_parse_internal(FuStructUsiDockMcuCmdRes *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_usi_dock_mcu_cmd_res_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_usi_dock_mcu_cmd_res_validate_internal(st, error))
        return FALSE;
    return TRUE;
}

/**
 * fu_struct_usi_dock_mcu_cmd_res_parse: (skip):
 **/
FuStructUsiDockMcuCmdRes *
fu_struct_usi_dock_mcu_cmd_res_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(FuStructUsiDockMcuCmdRes) st = fu_struct_usi_dock_mcu_cmd_res_new_internal();
    g_return_val_if_fail(buf != NULL, NULL);
    g_return_val_if_fail(error == NULL || *error == NULL, NULL);
    if (!fu_memchk_read(bufsz, offset, 64, error)) {
        g_prefix_error_literal(error, "invalid struct FuStructUsiDockMcuCmdRes: ");
        return NULL;
    }
    st->buf = g_byte_array_new();
    g_byte_array_append(st->buf, buf + offset, 64);
    if (!fu_struct_usi_dock_mcu_cmd_res_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
/**
 * fu_struct_usi_dock_isp_version_ref: (skip):
 **/
FuStructUsiDockIspVersion *
fu_struct_usi_dock_isp_version_ref(FuStructUsiDockIspVersion *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_usi_dock_isp_version_unref: (skip):
 **/
void
fu_struct_usi_dock_isp_version_unref(FuStructUsiDockIspVersion *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructUsiDockIspVersion refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}

/* getters */

/* setters */
