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

#include "config.h"

#include <glib.h>

#include "fu-parade-usbhub-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_parade_usbhub_chip_to_string:
 * @val: value, e.g. %FU_PARADE_USBHUB_CHIP_PS5512
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_parade_usbhub_chip_to_string(FuParadeUsbhubChip val)
{
    if (val == FU_PARADE_USBHUB_CHIP_PS5512)
        return "ps5512";
    if (val == FU_PARADE_USBHUB_CHIP_PS188)
        return "ps188";
    return NULL;
}
/**
 * fu_parade_usbhub_chip_from_string:
 * @val: (nullable): a string, e.g. `ps5512`
 *
 * Converts a string to an enumerated value.
 *
 * Returns: enumerated value
 **/
FuParadeUsbhubChip
fu_parade_usbhub_chip_from_string(const gchar *val)
{
    if (g_strcmp0(val, "ps5512") == 0)
        return FU_PARADE_USBHUB_CHIP_PS5512;
    if (g_strcmp0(val, "ps188") == 0)
        return FU_PARADE_USBHUB_CHIP_PS188;
    return FU_PARADE_USBHUB_CHIP_UNKNOWN;
}
/**
 * fu_struct_parade_usbhub_hdr_ref: (skip):
 **/
FuStructParadeUsbhubHdr *
fu_struct_parade_usbhub_hdr_ref(FuStructParadeUsbhubHdr *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_parade_usbhub_hdr_unref: (skip):
 **/
void
fu_struct_parade_usbhub_hdr_unref(FuStructParadeUsbhubHdr *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructParadeUsbhubHdr refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_parade_usbhub_hdr_new_internal: (skip):
 **/
static FuStructParadeUsbhubHdr *
fu_struct_parade_usbhub_hdr_new_internal(void)
{
    FuStructParadeUsbhubHdr *st = g_new0(FuStructParadeUsbhubHdr, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_parade_usbhub_hdr_get_signature: (skip):
 **/
static guint16
fu_struct_parade_usbhub_hdr_get_signature(const FuStructParadeUsbhubHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 0, G_BIG_ENDIAN);
}

/* setters */
static gboolean
fu_struct_parade_usbhub_hdr_validate_internal(FuStructParadeUsbhubHdr *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_parade_usbhub_hdr_get_signature(st) != 0x55AA) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructParadeUsbhubHdr.signature was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0x55AA,
                    (guint) fu_struct_parade_usbhub_hdr_get_signature(st));
        return FALSE;
    }
    return TRUE;
}
/**
 * fu_struct_parade_usbhub_hdr_validate_stream: (skip):
 **/
gboolean
fu_struct_parade_usbhub_hdr_validate_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructParadeUsbhubHdr) st = fu_struct_parade_usbhub_hdr_new_internal();
    g_return_val_if_fail(G_IS_INPUT_STREAM(stream), FALSE);
    g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
    st->buf = fu_input_stream_read_byte_array(stream, offset, 2, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructParadeUsbhubHdr failed read of 0x%x: ", (guint) 2);
        return FALSE;
    }
    if (st->buf->len != 2) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructParadeUsbhubHdr requested 0x%x and got 0x%x",
                    (guint) 2,
                    (guint) st->buf->len);
        return FALSE;
    }
    return fu_struct_parade_usbhub_hdr_validate_internal(st, error);
}
