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

#include "config.h"

#include <glib.h>

#include "fu-algoltek-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_algoltek_product_identity_ref: (skip):
 **/
FuStructAlgoltekProductIdentity *
fu_struct_algoltek_product_identity_ref(FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_algoltek_product_identity_unref: (skip):
 **/
void
fu_struct_algoltek_product_identity_unref(FuStructAlgoltekProductIdentity *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructAlgoltekProductIdentity refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_algoltek_product_identity_new_internal: (skip):
 **/
static FuStructAlgoltekProductIdentity *
fu_struct_algoltek_product_identity_new_internal(void)
{
    FuStructAlgoltekProductIdentity *st = g_new0(FuStructAlgoltekProductIdentity, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_algoltek_product_identity_get_header_len: (skip):
 **/
guint8
fu_struct_algoltek_product_identity_get_header_len(const FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[0];
}
/**
 * fu_struct_algoltek_product_identity_get_header: (skip):
 **/
static guint64
fu_struct_algoltek_product_identity_get_header(const FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint64(st->buf->data + 1, G_LITTLE_ENDIAN);
}
/**
 * fu_struct_algoltek_product_identity_get_product_name_len: (skip):
 **/
guint8
fu_struct_algoltek_product_identity_get_product_name_len(const FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[9];
}
/**
 * fu_struct_algoltek_product_identity_get_product_name: (skip):
 **/
gchar *
fu_struct_algoltek_product_identity_get_product_name(const FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 10, 16, NULL);
}
/**
 * fu_struct_algoltek_product_identity_get_version_len: (skip):
 **/
guint8
fu_struct_algoltek_product_identity_get_version_len(const FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return st->buf->data[26];
}
/**
 * fu_struct_algoltek_product_identity_get_version: (skip):
 **/
gchar *
fu_struct_algoltek_product_identity_get_version(const FuStructAlgoltekProductIdentity *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    return fu_memstrsafe(st->buf->data, st->buf->len, 27, 48, NULL);
}

/* setters */
/**
 * fu_struct_algoltek_product_identity_to_string: (skip):
 **/
static gchar *
fu_struct_algoltek_product_identity_to_string(const FuStructAlgoltekProductIdentity *st)
{
    g_autoptr(GString) str = g_string_new("FuStructAlgoltekProductIdentity:\n");
    g_return_val_if_fail(st != NULL, NULL);
    g_string_append_printf(str, "  header_len: 0x%x\n",
                           (guint) fu_struct_algoltek_product_identity_get_header_len(st));
    g_string_append_printf(str, "  header: 0x%x\n",
                           (guint) fu_struct_algoltek_product_identity_get_header(st));
    g_string_append_printf(str, "  product_name_len: 0x%x\n",
                           (guint) fu_struct_algoltek_product_identity_get_product_name_len(st));
    {
        g_autofree gchar *tmp = fu_struct_algoltek_product_identity_get_product_name(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  product_name: %s\n", tmp);
    }
    g_string_append_printf(str, "  version_len: 0x%x\n",
                           (guint) fu_struct_algoltek_product_identity_get_version_len(st));
    {
        g_autofree gchar *tmp = fu_struct_algoltek_product_identity_get_version(st);
        if (tmp != NULL)
            g_string_append_printf(str, "  version: %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_algoltek_product_identity_validate_internal(FuStructAlgoltekProductIdentity *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_algoltek_product_identity_get_header(st) != 0x4B45544C4F474C41) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructAlgoltekProductIdentity.header was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0x4B45544C4F474C41,
                    (guint) fu_struct_algoltek_product_identity_get_header(st));
        return FALSE;
    }
    return TRUE;
}
/**
 * fu_struct_algoltek_product_identity_validate_stream: (skip):
 **/
gboolean
fu_struct_algoltek_product_identity_validate_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructAlgoltekProductIdentity) st = fu_struct_algoltek_product_identity_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, 75, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructAlgoltekProductIdentity failed read of 0x%x: ", (guint) 75);
        return FALSE;
    }
    if (st->buf->len != 75) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructAlgoltekProductIdentity requested 0x%x and got 0x%x",
                    (guint) 75,
                    (guint) st->buf->len);
        return FALSE;
    }
    return fu_struct_algoltek_product_identity_validate_internal(st, error);
}
static gboolean
fu_struct_algoltek_product_identity_parse_internal(FuStructAlgoltekProductIdentity *st, GError **error)
{
    if (g_log_get_debug_enabled()) {
        g_autofree gchar *str = fu_struct_algoltek_product_identity_to_string(st);
        g_debug("%s", str);
    }
    if (!fu_struct_algoltek_product_identity_validate_internal(st, error))
        return FALSE;
    return TRUE;
}
/**
 * fu_struct_algoltek_product_identity_parse_stream: (skip):
 **/
FuStructAlgoltekProductIdentity *
fu_struct_algoltek_product_identity_parse_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructAlgoltekProductIdentity) st = fu_struct_algoltek_product_identity_new_internal();
    st->buf = fu_input_stream_read_byte_array(stream, offset, 75, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructAlgoltekProductIdentity failed read of 0x%x: ", (guint) 75);
        return NULL;
    }
    if (st->buf->len != 75) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructAlgoltekProductIdentity requested 0x%x and got 0x%x",
                    (guint) 75,
                    (guint) st->buf->len);
        return NULL;
    }
    if (!fu_struct_algoltek_product_identity_parse_internal(st, error))
        return NULL;
    return g_steal_pointer(&st);
}
/**
 * fu_struct_algoltek_cmd_address_pkt_ref: (skip):
 **/
FuStructAlgoltekCmdAddressPkt *
fu_struct_algoltek_cmd_address_pkt_ref(FuStructAlgoltekCmdAddressPkt *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_algoltek_cmd_address_pkt_unref: (skip):
 **/
void
fu_struct_algoltek_cmd_address_pkt_unref(FuStructAlgoltekCmdAddressPkt *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructAlgoltekCmdAddressPkt refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_algoltek_cmd_address_pkt_new_internal: (skip):
 **/
static FuStructAlgoltekCmdAddressPkt *
fu_struct_algoltek_cmd_address_pkt_new_internal(void)
{
    FuStructAlgoltekCmdAddressPkt *st = g_new0(FuStructAlgoltekCmdAddressPkt, 1);
    st->refcount = 1;
    return st;
}

/* getters */

/* setters */
/**
 * fu_struct_algoltek_cmd_address_pkt_set_len: (skip):
 **/
void
fu_struct_algoltek_cmd_address_pkt_set_len(FuStructAlgoltekCmdAddressPkt *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[0] = value;
}
/**
 * fu_struct_algoltek_cmd_address_pkt_set_cmd: (skip):
 **/
void
fu_struct_algoltek_cmd_address_pkt_set_cmd(FuStructAlgoltekCmdAddressPkt *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[1] = value;
}
/**
 * fu_struct_algoltek_cmd_address_pkt_set_address: (skip):
 **/
void
fu_struct_algoltek_cmd_address_pkt_set_address(FuStructAlgoltekCmdAddressPkt *st, guint16 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->buf->data + 2, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_algoltek_cmd_address_pkt_set_value: (skip):
 **/
void
fu_struct_algoltek_cmd_address_pkt_set_value(FuStructAlgoltekCmdAddressPkt *st, guint16 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->buf->data + 4, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_algoltek_cmd_address_pkt_set_checksum: (skip):
 **/
void
fu_struct_algoltek_cmd_address_pkt_set_checksum(FuStructAlgoltekCmdAddressPkt *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[10] = value;
}
/**
 * fu_struct_algoltek_cmd_address_pkt_new: (skip):
 **/
FuStructAlgoltekCmdAddressPkt *
fu_struct_algoltek_cmd_address_pkt_new(void)
{
    FuStructAlgoltekCmdAddressPkt *st = fu_struct_algoltek_cmd_address_pkt_new_internal();
    st->buf = g_byte_array_sized_new(11);
    fu_byte_array_set_size(st->buf, 11, 0x0);
    return st;
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_ref: (skip):
 **/
FuStructAlgoltekCmdTransferPkt *
fu_struct_algoltek_cmd_transfer_pkt_ref(FuStructAlgoltekCmdTransferPkt *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_unref: (skip):
 **/
void
fu_struct_algoltek_cmd_transfer_pkt_unref(FuStructAlgoltekCmdTransferPkt *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructAlgoltekCmdTransferPkt refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_new_internal: (skip):
 **/
static FuStructAlgoltekCmdTransferPkt *
fu_struct_algoltek_cmd_transfer_pkt_new_internal(void)
{
    FuStructAlgoltekCmdTransferPkt *st = g_new0(FuStructAlgoltekCmdTransferPkt, 1);
    st->refcount = 1;
    return st;
}

/* getters */

/* setters */
/**
 * fu_struct_algoltek_cmd_transfer_pkt_set_len: (skip):
 **/
void
fu_struct_algoltek_cmd_transfer_pkt_set_len(FuStructAlgoltekCmdTransferPkt *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[0] = value;
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_set_cmd: (skip):
 **/
void
fu_struct_algoltek_cmd_transfer_pkt_set_cmd(FuStructAlgoltekCmdTransferPkt *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[1] = value;
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_set_address: (skip):
 **/
void
fu_struct_algoltek_cmd_transfer_pkt_set_address(FuStructAlgoltekCmdTransferPkt *st, guint16 value)
{
    g_return_if_fail(st != NULL);
    fu_memwrite_uint16(st->buf->data + 2, value, G_BIG_ENDIAN);
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_set_data: (skip):
 **/
gboolean
fu_struct_algoltek_cmd_transfer_pkt_set_data(FuStructAlgoltekCmdTransferPkt *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_algoltek_cmd_transfer_pkt_set_checksum: (skip):
 **/
void
fu_struct_algoltek_cmd_transfer_pkt_set_checksum(FuStructAlgoltekCmdTransferPkt *st, guint8 value)
{
    g_return_if_fail(st != NULL);
    st->buf->data[65] = value;
}
/**
 * fu_struct_algoltek_cmd_transfer_pkt_new: (skip):
 **/
FuStructAlgoltekCmdTransferPkt *
fu_struct_algoltek_cmd_transfer_pkt_new(void)
{
    FuStructAlgoltekCmdTransferPkt *st = fu_struct_algoltek_cmd_transfer_pkt_new_internal();
    st->buf = g_byte_array_sized_new(66);
    fu_byte_array_set_size(st->buf, 66, 0x0);
    return st;
}
