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

#include "config.h"

#include <glib.h>

#include "fu-nordic-hid-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_nordic_hid_dfu_fwinfo_ref: (skip):
 **/
FuStructNordicHidDfuFwinfo *
fu_struct_nordic_hid_dfu_fwinfo_ref(FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_unref: (skip):
 **/
void
fu_struct_nordic_hid_dfu_fwinfo_unref(FuStructNordicHidDfuFwinfo *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructNordicHidDfuFwinfo refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_new_internal: (skip):
 **/
static FuStructNordicHidDfuFwinfo *
fu_struct_nordic_hid_dfu_fwinfo_new_internal(void)
{
    FuStructNordicHidDfuFwinfo *st = g_new0(FuStructNordicHidDfuFwinfo, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_nordic_hid_dfu_fwinfo_get_flash_area_id: (skip):
 **/
guint8
fu_struct_nordic_hid_dfu_fwinfo_get_flash_area_id(const FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[0];
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_get_flashed_image_len: (skip):
 **/
guint32
fu_struct_nordic_hid_dfu_fwinfo_get_flashed_image_len(const FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 1, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_get_ver_major: (skip):
 **/
guint8
fu_struct_nordic_hid_dfu_fwinfo_get_ver_major(const FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[5];
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_get_ver_minor: (skip):
 **/
guint8
fu_struct_nordic_hid_dfu_fwinfo_get_ver_minor(const FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[6];
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_get_ver_rev: (skip):
 **/
guint16
fu_struct_nordic_hid_dfu_fwinfo_get_ver_rev(const FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint16(st->buf->data + 7, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_nordic_hid_dfu_fwinfo_get_ver_build_nr: (skip):
 **/
guint32
fu_struct_nordic_hid_dfu_fwinfo_get_ver_build_nr(const FuStructNordicHidDfuFwinfo *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 9, G_LITTLE_ENDIAN);
}

/* setters */
/**
 * fu_struct_nordic_hid_dfu_fwinfo_to_string: (skip):
 **/
static gchar *
fu_struct_nordic_hid_dfu_fwinfo_to_string(const FuStructNordicHidDfuFwinfo *st)
{
    g_autoptr(GString) str = g_string_new("FuStructNordicHidDfuFwinfo:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  flash_area_id: 0x%x\n",
                           (guint) fu_struct_nordic_hid_dfu_fwinfo_get_flash_area_id(st));
    g_string_append_printf(str, "  flashed_image_len: 0x%x\n",
                           (guint) fu_struct_nordic_hid_dfu_fwinfo_get_flashed_image_len(st));
    g_string_append_printf(str, "  ver_major: 0x%x\n",
                           (guint) fu_struct_nordic_hid_dfu_fwinfo_get_ver_major(st));
    g_string_append_printf(str, "  ver_minor: 0x%x\n",
                           (guint) fu_struct_nordic_hid_dfu_fwinfo_get_ver_minor(st));
    g_string_append_printf(str, "  ver_rev: 0x%x\n",
                           (guint) fu_struct_nordic_hid_dfu_fwinfo_get_ver_rev(st));
    g_string_append_printf(str, "  ver_build_nr: 0x%x\n",
                           (guint) fu_struct_nordic_hid_dfu_fwinfo_get_ver_build_nr(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_nordic_hid_dfu_fwinfo_validate_internal(FuStructNordicHidDfuFwinfo *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    return TRUE;
}
static gboolean
fu_struct_nordic_hid_dfu_fwinfo_parse_internal(FuStructNordicHidDfuFwinfo *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_nordic_hid_dfu_fwinfo_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_nordic_hid_dfu_fwinfo_validate_internal(st, error))
        return FALSE;
    return TRUE;
}

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