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

#include "config.h"

#include <glib.h>

#include "fu-ti-tps6598x-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_ti_tps6598x_sfwi_to_string:
 * @val: value, e.g. %FU_TI_TPS6598X_SFWI_FAIL_FLASH_ERROR_OR_BUSY
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_ti_tps6598x_sfwi_to_string(FuTiTps6598xSfwi val)
{
    if (val == FU_TI_TPS6598X_SFWI_SUCCESS)
        return "success";
    if (val == FU_TI_TPS6598X_SFWI_FAIL_FLASH_ERROR_OR_BUSY)
        return "fail-flash-error-or-busy";
    if (val == FU_TI_TPS6598X_SFWI_FAIL_FLASH_INVALID_ADDRESS)
        return "fail-flash-invalid-address";
    if (val == FU_TI_TPS6598X_SFWI_FAIL_LAST_BOOT_WAS_UART)
        return "fail-last-boot-was-uart";
    if (val == FU_TI_TPS6598X_SFWI_FAIL_SFWI_AFTER_COMPLETE)
        return "fail-sfwi-after-complete";
    if (val == FU_TI_TPS6598X_SFWI_FAIL_NO_VALID_FLASH_REGION)
        return "fail-no-valid-flash-region";
    if (val == FU_TI_TPS6598X_SFWI_FAIL_UNKNOWN_ERROR)
        return "fail-unknown-error";
    return NULL;
}

/**
 * fu_ti_tps6598x_sfwd_to_string:
 * @val: value, e.g. %FU_TI_TPS6598X_SFWD_FAIL_FLASH_ERASE_WRITE_ERROR
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_ti_tps6598x_sfwd_to_string(FuTiTps6598xSfwd val)
{
    if (val == FU_TI_TPS6598X_SFWD_SUCCESS)
        return "success";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_FLASH_ERASE_WRITE_ERROR)
        return "fail-flash-erase-write-error";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_SFWI_NOT_RUN_FIRST)
        return "fail-sfwi-not-run-first";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_TOO_MUCH_DATA)
        return "fail-too-much-data";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_ID_NOT_IN_HEADER)
        return "fail-id-not-in-header";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_BINARY_TOO_LARGE)
        return "fail-binary-too-large";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_DEVICE_ID_MISMATCH)
        return "fail-device-id-mismatch";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_FLASH_ERROR_READ_ONLY)
        return "fail-flash-error-read-only";
    if (val == FU_TI_TPS6598X_SFWD_FAIL_UNKNOWN_ERROR)
        return "fail-unknown-error";
    return NULL;
}

/**
 * fu_ti_tps6598x_sfws_to_string:
 * @val: value, e.g. %FU_TI_TPS6598X_SFWS_FAIL_FLASH_ERASE_WRITE_ERROR
 *
 * Converts an enumerated value to a string.
 *
 * Returns: identifier string
 **/
const gchar *
fu_ti_tps6598x_sfws_to_string(FuTiTps6598xSfws val)
{
    if (val == FU_TI_TPS6598X_SFWS_SUCCESS)
        return "success";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_FLASH_ERASE_WRITE_ERROR)
        return "fail-flash-erase-write-error";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_SFWD_NOT_RUN_OR_NO_KEY_EXISTS)
        return "fail-sfwd-not-run-or-no-key-exists";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_TOO_MUCH_DATA)
        return "fail-too-much-data";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_CRC_FAIL)
        return "fail-crc-fail";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_DID_CHECK_FAIL)
        return "fail-did-check-fail";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_VERSION_CHECK_FAIL)
        return "fail-version-check-fail";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_NO_HASH_MATCH_RULE_SATISFIED)
        return "fail-no-hash-match-rule-satisfied";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_ENGR_FW_UPDATE_ATTEMPT_WHILE_RUNNING_PROD)
        return "fail-engr-fw-update-attempt-while-running-prod";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_INCOMPATIBLE_ROM_VERSION)
        return "fail-incompatible-rom-version";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_CRC_BUSY)
        return "fail-crc-busy";
    if (val == FU_TI_TPS6598X_SFWS_FAIL_UNKNOWN_ERROR)
        return "fail-unknown-error";
    return NULL;
}

/**
 * fu_struct_ti_tps6598x_firmware_hdr_ref: (skip):
 **/
FuStructTiTps6598xFirmwareHdr *
fu_struct_ti_tps6598x_firmware_hdr_ref(FuStructTiTps6598xFirmwareHdr *st)
{
    g_return_val_if_fail(st != NULL, NULL);
    st->refcount++;
    return st;
}
/**
 * fu_struct_ti_tps6598x_firmware_hdr_unref: (skip):
 **/
void
fu_struct_ti_tps6598x_firmware_hdr_unref(FuStructTiTps6598xFirmwareHdr *st)
{
    g_return_if_fail(st != NULL);
    if (st->refcount == 0) {
        g_critical("FuStructTiTps6598xFirmwareHdr refcount already zero");
        return;
    }
    if (--st->refcount > 0)
        return;
    if (st->buf != NULL)
        g_byte_array_unref(st->buf);
    g_free(st);
}
/**
 * fu_struct_ti_tps6598x_firmware_hdr_new_internal: (skip):
 **/
static FuStructTiTps6598xFirmwareHdr *
fu_struct_ti_tps6598x_firmware_hdr_new_internal(void)
{
    FuStructTiTps6598xFirmwareHdr *st = g_new0(FuStructTiTps6598xFirmwareHdr, 1);
    st->refcount = 1;
    return st;
}

/* getters */
/**
 * fu_struct_ti_tps6598x_firmware_hdr_get_magic: (skip):
 **/
static guint32
fu_struct_ti_tps6598x_firmware_hdr_get_magic(const FuStructTiTps6598xFirmwareHdr *st)
{
    g_return_val_if_fail(st != NULL, 0x0);
    return fu_memread_uint32(st->buf->data + 0, G_LITTLE_ENDIAN);
}

/* setters */
static gboolean
fu_struct_ti_tps6598x_firmware_hdr_validate_internal(FuStructTiTps6598xFirmwareHdr *st, GError **error)
{
    g_return_val_if_fail(st != NULL, FALSE);
    if (fu_struct_ti_tps6598x_firmware_hdr_get_magic(st) != 0xACEF0001) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "constant FuStructTiTps6598xFirmwareHdr.magic was not valid, "
                    "expected 0x%x and got 0x%x",
                    (guint) 0xACEF0001,
                    (guint) fu_struct_ti_tps6598x_firmware_hdr_get_magic(st));
        return FALSE;
    }
    return TRUE;
}
/**
 * fu_struct_ti_tps6598x_firmware_hdr_validate_stream: (skip):
 **/
gboolean
fu_struct_ti_tps6598x_firmware_hdr_validate_stream(GInputStream *stream, gsize offset, GError **error)
{
    g_autoptr(FuStructTiTps6598xFirmwareHdr) st = fu_struct_ti_tps6598x_firmware_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, 4, NULL, error);
    if (st->buf == NULL) {
        g_prefix_error(error, "FuStructTiTps6598xFirmwareHdr failed read of 0x%x: ", (guint) 4);
        return FALSE;
    }
    if (st->buf->len != 4) {
        g_set_error(error,
                    FWUPD_ERROR,
                    FWUPD_ERROR_INVALID_DATA,
                    "FuStructTiTps6598xFirmwareHdr requested 0x%x and got 0x%x",
                    (guint) 4,
                    (guint) st->buf->len);
        return FALSE;
    }
    return fu_struct_ti_tps6598x_firmware_hdr_validate_internal(st, error);
}
