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

#include "config.h"

#include <glib.h>

#include "fu-huddly-usb-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_struct_h_link_header_ref: (skip):
 **/
FuStructHLinkHeader *
fu_struct_h_link_header_ref(FuStructHLinkHeader *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_h_link_header_unref: (skip):
 **/
void
fu_struct_h_link_header_unref(FuStructHLinkHeader *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructHLinkHeader refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_h_link_header_new_internal: (skip):
 **/
static FuStructHLinkHeader *
fu_struct_h_link_header_new_internal(void)
{
    FuStructHLinkHeader *st = g_new0(FuStructHLinkHeader, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_h_link_header_get_req_id: (skip):
 **/
guint32
fu_struct_h_link_header_get_req_id(const FuStructHLinkHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_get_res_id: (skip):
 **/
guint32
fu_struct_h_link_header_get_res_id(const FuStructHLinkHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 4, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_get_flags: (skip):
 **/
guint16
fu_struct_h_link_header_get_flags(const FuStructHLinkHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 8, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_get_msg_name_size: (skip):
 **/
guint16
fu_struct_h_link_header_get_msg_name_size(const FuStructHLinkHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 10, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_get_payload_size: (skip):
 **/
guint32
fu_struct_h_link_header_get_payload_size(const FuStructHLinkHeader *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 12, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_h_link_header_set_req_id: (skip):
 **/
void
fu_struct_h_link_header_set_req_id(FuStructHLinkHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 0, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_set_res_id: (skip):
 **/
void
fu_struct_h_link_header_set_res_id(FuStructHLinkHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 4, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_set_flags: (skip):
 **/
void
fu_struct_h_link_header_set_flags(FuStructHLinkHeader *st, guint16 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->buf->data + 8, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_set_msg_name_size: (skip):
 **/
void
fu_struct_h_link_header_set_msg_name_size(FuStructHLinkHeader *st, guint16 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->buf->data + 10, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_set_payload_size: (skip):
 **/
void
fu_struct_h_link_header_set_payload_size(FuStructHLinkHeader *st, guint32 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint32(st->buf->data + 12, value, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_h_link_header_new: (skip):
 **/
FuStructHLinkHeader *
fu_struct_h_link_header_new(void)
{
    FuStructHLinkHeader *st = fu_struct_h_link_header_new_internal();
    st->buf = g_byte_array_sized_new(16);
    fu_byte_array_set_size(st->buf, 16, 0x0);
    return st;
}
/**
 * fu_struct_h_link_header_to_string: (skip):
 **/
static gchar *
fu_struct_h_link_header_to_string(const FuStructHLinkHeader *st)
{
    g_autoptr(GString) str = g_string_new("FuStructHLinkHeader:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  req_id: 0x%x\n",
                           (guint) fu_struct_h_link_header_get_req_id(st));
    g_string_append_printf(str, "  res_id: 0x%x\n",
                           (guint) fu_struct_h_link_header_get_res_id(st));
    g_string_append_printf(str, "  flags: 0x%x\n",
                           (guint) fu_struct_h_link_header_get_flags(st));
    g_string_append_printf(str, "  msg_name_size: 0x%x\n",
                           (guint) fu_struct_h_link_header_get_msg_name_size(st));
    g_string_append_printf(str, "  payload_size: 0x%x\n",
                           (guint) fu_struct_h_link_header_get_payload_size(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_h_link_header_parse_internal(FuStructHLinkHeader *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_h_link_header_to_string(st);
        g_debug("%s", str);
    }
    return TRUE;
}

/**
 * fu_struct_h_link_header_parse: (skip):
 **/
FuStructHLinkHeader *
fu_struct_h_link_header_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error)
{
    g_autoptr(FuStructHLinkHeader) st = fu_struct_h_link_header_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, 16, error)) {
        g_prefix_error_literal(error, "invalid struct FuStructHLinkHeader: ");
        return NULL;
    }
    st->buf = g_byte_array_new();
    g_byte_array_append(st->buf, buf + offset, 16);
    if (!fu_struct_h_link_header_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
