#ifndef H2_TYPEDEF_MATLAB_H
#define H2_TYPEDEF_MATLAB_H

#include <stdint.h>

/*
 * MATLAB R2016a compatible typedef header.
 *
 * The original h2_typedef.h uses C++11 fixed-underlying-type enums such as:
 *     enum trigger_source : uint8_t
 *
 * MATLAB R2016a loadlibrary often cannot parse that syntax. This header keeps
 * the same structure names, field order, and enum storage size by replacing
 * those enums with uint8_t typedefs plus numeric constants.
 */

/* ===========================
 * API status codes
 * =========================== */

#define STATUS_NOERROR 0

#define STATUS_ERROR_BUSOPENFAILED -1
#define STATUS_ERROR_DISCONNECT -8
#define STATUS_ERROR_TXLEVEL_FILEMISSING -29
#define STATUS_ERROR_IQCAL_FILEMISSING -43

#define STATUS_WARNING_BUSTIMEOUT 10
#define STATUS_WARNING_TXLEVEL_FILEDEFAULT 42
#define STATUS_WARNING_IQCAL_FILEDEFAULT 44
#define STATUS_WARNING_PARAMOUTRANGE 98

/* ===========================
 * Global configuration
 * =========================== */

typedef uint8_t state;
#define STATE_OFF 0
#define STATE_ON 1

typedef uint8_t fan_mode;
#define FAN_ON 0
#define FAN_OFF 1
#define FAN_AUTO 2

/* ===========================
 * Device info
 * =========================== */

typedef struct
{
    uint16_t model;
    uint64_t uid_l64;
    uint32_t uid_h32;

    uint16_t hardware_version;

    uint32_t mfw_version;
    uint32_t ffw_version;
    uint32_t pmu_version;
    uint32_t agu_version;
    uint32_t bus_version;
    uint32_t eio_version;

    uint32_t bus_bandwidth;
} device_info;

/* ===========================
 * Supply info
 * =========================== */

typedef struct
{
    float rf_voltage;
    float rf_current;
    float usb_voltage;
    float usb_current;
} supply_info;

/* ===========================
 * ETH interface type
 * =========================== */

typedef uint8_t eth_interface_type;
#define ETH_HIGHSPEED 0x00
#define ETH_STANDARD 0x01

/* ===========================
 * ETH startup configuration
 * =========================== */

typedef struct
{
    eth_interface_type eth_interface;

    uint8_t eth_is_ipv6;
    uint8_t eth_ip_address[16];
    uint16_t eth_remote_port;
    int32_t eth_read_timeout;
} eth_setting;

/* ===========================
 * GNSS configuration
 * =========================== */

typedef struct
{
    uint8_t antenna;

    uint8_t pps_en;
    float pps_x;
    float pps_delay;
} gnss_setting;

/* ===========================
 * GNSS info
 * =========================== */

typedef struct
{
    state locked;

    uint8_t sat_nums;

    uint8_t snr_max;
    uint8_t snr_avg;
    uint8_t snr_min;

    float latitude;
    float longitude;
    float altitude;
    uint64_t ns_sinceepoch;
} gnss_info;

/* ===========================
 * Reference clock
 * =========================== */

typedef uint8_t referenceclock_source;
#define REFCLKSOURCE_INTERNAL 0
#define REFCLKSOURCE_EXTERNAL 1

/* ===========================
 * LO mode
 * =========================== */

typedef uint8_t lomode;
#define LOMODE_AUTO 0
#define LOMODE_HIGHSPEED 1
#define LOMODE_LOWSPUR 2
#define LOMODE_LOWPHASENOISE 3

/* ===========================
 * Channel definitions
 * =========================== */

#define MAXCHANNELS 8
#define ALLCHANNEL -1

typedef uint8_t channel_type;
#define CHANNELTYPE_RX 0
#define CHANNELTYPE_TX 1
#define CHANNELTYPE_VX 2

typedef struct
{
    void **device;
    uint16_t num;
    channel_type type;
} channel;

/* ===========================
 * Trigger definitions
 * =========================== */

typedef uint8_t trigger_action;
#define TRIGGER_ACTION_HOP 0
#define TRIGGER_ACTION_SWEEP 1

typedef uint8_t trigger_edge;
#define TRIGGER_EDGE_RISING 0
#define TRIGGER_EDGE_FALLING 1

typedef uint8_t trigger_source;
#define TRIGGER_SOURCE_BUS 0
#define TRIGGER_SOURCE_EXTERNAL 1
#define TRIGGER_SOURCE_XPPS 2
#define TRIGGER_SOURCE_LEVEL 3
#define TRIGGER_SOURCE_FREQMASK 4

typedef struct
{
    trigger_source source;
    trigger_edge edge;
    trigger_action action;
    int32_t count;
} tx_trigger;

typedef struct
{
    state enable;
    trigger_action action;
    trigger_edge edge;
} trigger_out;

#endif /* H2_TYPEDEF_MATLAB_H */
