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

#include "config.h"

#include <glib.h>

#include "fu-dell-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_dell_dock_module_type_to_string:
 * @val: value, e.g. %FU_DELL_DOCK_MODULE_TYPE_W45_GENERIC
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
static const gchar *
fu_dell_dock_module_type_to_string(FuDellDockModuleType val)
{
    if (val == FU_DELL_DOCK_MODULE_TYPE_W45_TBT)
        return "w45-tbt";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W45_GENERIC)
        return "w45-generic";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W130_TBT)
        return "w130-tbt";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W130_DP)
        return "w130-dp";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W130_UNIVERSAL)
        return "w130-universal";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W240_TRIN)
        return "w240-trin";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W210_DUAL)
        return "w210-dual";
    if (val == FU_DELL_DOCK_MODULE_TYPE_W130_USB4)
        return "w130-usb4";
    return NULL;
}
/**
 * fu_struct_dell_dock_data_ref: (skip):
 **/
FuStructDellDockData *
fu_struct_dell_dock_data_ref(FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_dell_dock_data_unref: (skip):
 **/
void
fu_struct_dell_dock_data_unref(FuStructDellDockData *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructDellDockData refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_dell_dock_data_new_internal: (skip):
 **/
static FuStructDellDockData *
fu_struct_dell_dock_data_new_internal(void)
{
    FuStructDellDockData *st = g_new0(FuStructDellDockData, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_dell_dock_data_get_dock_configuration: (skip):
 **/
guint8
fu_struct_dell_dock_data_get_dock_configuration(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[0];
}
/**
 * fu_struct_dell_dock_data_get_dock_type: (skip):
 **/
guint8
fu_struct_dell_dock_data_get_dock_type(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[1];
}
/**
 * fu_struct_dell_dock_data_get_power_supply_wattage: (skip):
 **/
guint16
fu_struct_dell_dock_data_get_power_supply_wattage(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 2, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_module_type: (skip):
 **/
FuDellDockModuleType
fu_struct_dell_dock_data_get_module_type(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_board_id: (skip):
 **/
guint16
fu_struct_dell_dock_data_get_board_id(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 6, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_port0_dock_status: (skip):
 **/
guint16
fu_struct_dell_dock_data_get_port0_dock_status(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_port1_dock_status: (skip):
 **/
guint16
fu_struct_dell_dock_data_get_port1_dock_status(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_dock_firmware_pkg_ver: (skip):
 **/
guint32
fu_struct_dell_dock_data_get_dock_firmware_pkg_ver(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 12, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_module_serial: (skip):
 **/
guint64
fu_struct_dell_dock_data_get_module_serial(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint64(st->buf->data + 16, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_original_module_serial: (skip):
 **/
guint64
fu_struct_dell_dock_data_get_original_module_serial(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint64(st->buf->data + 24, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_dell_dock_data_get_service_tag: (skip):
 **/
gchar *
fu_struct_dell_dock_data_get_service_tag(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 32, 7, NULL);
}
/**
 * fu_struct_dell_dock_data_get_marketing_name: (skip):
 **/
gchar *
fu_struct_dell_dock_data_get_marketing_name(const FuStructDellDockData *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 39, 64, NULL);
}

/* setters */
/**
 * fu_struct_dell_dock_data_to_string: (skip):
 **/
static gchar *
fu_struct_dell_dock_data_to_string(const FuStructDellDockData *st)
{
    g_autoptr(GString) str = g_string_new("FuStructDellDockData:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  dock_configuration: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_dock_configuration(st));
    g_string_append_printf(str, "  dock_type: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_dock_type(st));
    g_string_append_printf(str, "  power_supply_wattage: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_power_supply_wattage(st));
    {
        const gchar *tmp = fu_dell_dock_module_type_to_string(fu_struct_dell_dock_data_get_module_type(st));
        if (tmp != NULL) {
            g_string_append_printf(str, "  module_type: 0x%x [%s]\n", (guint) fu_struct_dell_dock_data_get_module_type(st), tmp);
        } else {
            g_string_append_printf(str, "  module_type: 0x%x\n", (guint) fu_struct_dell_dock_data_get_module_type(st));
        }
    }
    g_string_append_printf(str, "  board_id: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_board_id(st));
    g_string_append_printf(str, "  port0_dock_status: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_port0_dock_status(st));
    g_string_append_printf(str, "  port1_dock_status: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_port1_dock_status(st));
    g_string_append_printf(str, "  dock_firmware_pkg_ver: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_dock_firmware_pkg_ver(st));
    g_string_append_printf(str, "  module_serial: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_module_serial(st));
    g_string_append_printf(str, "  original_module_serial: 0x%x\n",
                           (guint) fu_struct_dell_dock_data_get_original_module_serial(st));
    {
        g_autofree gchar *tmp = fu_struct_dell_dock_data_get_service_tag(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  service_tag: %s\n", tmp);
    }
    {
        g_autofree gchar *tmp = fu_struct_dell_dock_data_get_marketing_name(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  marketing_name: %s\n", tmp);
    }
    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_dell_dock_data_parse_internal(FuStructDellDockData *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_dell_dock_data_to_string(st);
        g_debug("%s", str);
    }
    return TRUE;
}

/**
 * fu_struct_dell_dock_data_parse: (skip):
 **/
static FuStructDellDockData *
fu_struct_dell_dock_data_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(FuStructDellDockData) st = fu_struct_dell_dock_data_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, 103, error)) {
        g_prefix_error_literal(error, "invalid struct FuStructDellDockData: ");
        return NULL;
    }
    st->buf = g_byte_array_new();
    g_byte_array_append(st->buf, buf + offset, 103);
    if (!fu_struct_dell_dock_data_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
/**
 * fu_struct_dell_dock_data_parse_bytes: (skip):
 **/
FuStructDellDockData *
fu_struct_dell_dock_data_parse_bytes(GBytes *blob, gsize offset, GError **error)
{
    gsize bufsz = 0;
    const guint8 *buf = fu_bytes_get_data_safe(blob, &bufsz, error);
    if (buf == NULL)
        return NULL;
    return fu_struct_dell_dock_data_parse(buf, bufsz, offset, error);
}
