#ifndef H2_TYPEDEF_H
#define H2_TYPEDEF_H

#include <stdint.h>

/*
 * h2_typedef.h
 *
 * Definitions of basic types, enumerations, and return codes exposed to users for H2 series devices.
 *
 * This document only contains data structures and constants with API stability commitment:
 *  - Can be directly included by user programs
 *  - Does not contain any definitions related to specific implementations
 *  - Does not contain any definitions related to specific platforms
 */

/* ===========================
 * API Return Code Definitions
 * =========================== */

#define STATUS_NOERROR                     0    // No Error

/* Exception Error: Device cannot continue to operate */
#define STATUS_ERROR_BUSOPENFAILED         -1  // Bus open error
#define STATUS_ERROR_DISCONNECT            -8  // Bus communication error
#define STATUS_ERROR_TXLEVEL_FILEMISSING   -29 // The RF calibration file is missing
#define STATUS_ERROR_IQCAL_FILEMISSING     -43 // The IQ calibration file is missing
#define STATUS_ERROR_OPTIONS_MISSING       -47 // Required option is not available.

/* Exception Warning: Does not affect device's continued operation */
#define STATUS_WARNING_BUSTIMEOUT          10  // Data is not obtained within the specified time
#define STATUS_WARNING_TXLEVEL_FILEDEFAULT 42  // Use default RF calibration file
#define STATUS_WARNING_IQCAL_FILEDEFAULT   44  // Use default IQ calibration file
#define STATUS_WARNING_PARAMOUTRANGE       98  // Parameter out of bounds, abnormal call
#define STATUS_WARNING_UNLEVEL             99  // Transmit power is too low, but still within limits

#define STATUS_WARNING_SYSCLK_UNLOCKED	15 // System clock unlocked
#define	STATUS_WARNING_ADDACLK_UNLOCKED	16 // ADC clock unlocked
#define	STATUS_WARNING_LOREF_UNLOCKED	17 // RX IF local oscillator unlocked
#define STATUS_WARNING_RFLO_UNLOCKED	18 // RX RF local oscillator unlocked


/* ===========================
 * Global Configuration
 * =========================== */

enum state : uint8_t
{
    STATE_OFF = 0,
    STATE_ON = 1
};

/* Fan Mode */
enum fan_mode : uint8_t 
{
    FAN_ON,     // Fan Always On
    FAN_OFF,    // Fan Always Off
    FAN_AUTO    // Automatically adjust fan status based on temperature
};

/* ===========================
 * Device Information
 * =========================== */

typedef struct
{
    uint16_t model;            // Device Model
    uint64_t uid_l64;          // Low 64-bit Device Serial Number
    uint32_t uid_h32;          // High 32-bit Device Serial Number

    uint16_t hardware_version;  // Hardware Version

    uint32_t mfw_version;       // MCU Firmware Version
    uint32_t ffw_version;       // FPGA Firmware Version
    uint32_t pmu_version;       // PMU Firmware Version
    uint32_t agu_version;       // AGU Firmware Version
    uint32_t bus_version;       // Bus Peripheral Firmware Version
    uint32_t eio_version;       // EIO Firmware Version
    
    uint32_t bus_bandwidth;     // Bus Bandwidth, unit: MB/s
} device_info;

/* ===========================
 * Power Supply Information
 * =========================== */
typedef struct
{
    float rf_voltage; // Power Port Voltage (V)
    float rf_current; // Power Port Current (A)
    float usb_voltage; // USB Port Voltage (V)
    float usb_current; // USB Port Current (A)
} supply_info;

/* ===========================
 * eth Interface Type
 * =========================== */

enum eth_interface_type : uint8_t
{
    ETH_HIGHSPEED = 0x00,          // High-Speed Ethernet Bus Interface
    ETH_STANDARD = 0x01,           // Standard Ethernet Bus Interface
};

/* ===========================
 * eth Boot Profile
 * =========================== */

typedef struct
{
    enum eth_interface_type eth_interface; // Physical Interface Type

    uint8_t  eth_is_ipv6;              // ETH Protocol Type (0: IPv4, 1: IPv6)
    uint8_t  eth_ip_address[16];       // ETH Interface IP Address
    uint16_t eth_remote_port;          // ETH Interface Remote Port
    int32_t  eth_read_timeout;         // ETH Interface Read Timeout (ms)
} eth_setting;

/* ===========================
 * GNSS Configuration
 * =========================== */

/* GNSS Configuration */
typedef struct 
{
	uint8_t antenna;	// Antenna Selection, 0 for internal antenna, 1 for external antenna

	uint8_t pps_en;	    // Whether to enable PPS
	float   pps_x;      // PPS Frequency, unit: Hz
	float   pps_delay;  // PPS Delay, unit: s
}gnss_setting;

/* ===========================
 * GNSS Information
 * =========================== */

typedef struct
{
    state locked;       // GNSS Lock Status, 0-UnLocked, 1-Locked

    uint8_t sat_nums;  // Current GNSS Positioning Satellite Count

    uint8_t snr_max;   // Current GNSS Positioning Satellite SNR(Max)
    uint8_t snr_avg;   // Current GNSS Positioning Satellite SNR(Avg)
    uint8_t snr_min;   // Current GNSS Positioning Satellite SNR(Min)

    float latitude;    // Latitude, positive for North, negative for South
    float longitude;   // Longitude, positive for East, negative for West
    float altitude;    // Altitude, m
    uint64_t ns_sinceepoch; // System Timestamp (ns)
} gnss_info;

/* ===========================
 * Reference Clock
 * =========================== */

enum referenceclock_source : uint8_t 
{
    REFCLKSOURCE_INTERNAL, 
    REFCLKSOURCE_EXTERNAL
};

/* ===========================
 * LO Mode
 * =========================== */

enum lomode : uint8_t 
{
    LOMODE_AUTO, 
    LOMODE_HIGHSPEED,
    LOMODE_LOWSPUR, 
    LOMODE_LOWPHASENOISE
};

/* ===========================
 * Channel Related Definitions
 * =========================== */

/* Maximum Number of Channels */
#define MAXCHANNELS   8

 /* Select All Channels */
#define ALLCHANNEL    -1

/* Channel Type */
enum channel_type : uint8_t
{
    CHANNELTYPE_RX,   // Receive Channel
    CHANNELTYPE_TX,   // Transmit Channel
    CHANNELTYPE_VX    // Vector Network Analyzer Channel
};

/* Channel Information */
typedef struct
{
    void** device;        // Channel's Device Handle
    uint16_t num;         // Channel Number
    enum channel_type type; // Channel Type
} channel;

/* ===========================
 * Trigger Related Definitions
 * =========================== */

/* Trigger Action */
enum trigger_action : uint8_t
{
    TRIGGER_ACTION_HOP,    // Execute One Frequency Hop
    TRIGGER_ACTION_SWEEP   // Execute One Complete Sweep
};

/* TriggerEdge */
enum trigger_edge : uint8_t
{
    TRIGGER_EDGE_RISING,   // RisingEdge Trigger
    TRIGGER_EDGE_FALLING   // FallingEdge Trigger
};

/* TriggerSource */
enum trigger_source : uint8_t
{
    TRIGGER_SOURCE_BUS,      // Bus Trigger
    TRIGGER_SOURCE_EXTERNAL, // External Trigger
    TRIGGER_SOURCE_XPPS,     // GNSS1PPS Trigger
    TRIGGER_SOURCE_LEVEL,    // Level Trigger (Rx only)
    TRIGGER_SOURCE_FREQMASK  // SpectrumMask Trigger (Rx only)
};

/* Transmit Channel Trigger Configuration */
typedef struct
{
    enum trigger_source source; // TriggerSource
    enum trigger_edge   edge;   // TriggerEdge
    enum trigger_action action; // Trigger Action
    int32_t             count;  // Trigger Response Count (-1 for infinite)
} tx_trigger;

/* Channel Trigger Output Configuration */
typedef struct
{
    state enable;
    trigger_action action;
    trigger_edge edge;
} trigger_out;

#endif // H2_TYPEDEF_H