#ifndef __XH_CCU_H__
#define __XH_CCU_H__
#include "tc_system.h"
#include "com_ttySx.h"
#include "modbus_RTU.h"

typedef struct ast_xh
{
    pthread_mutex_t mutex;
    int quit;
    
    com_ttySx_t tty;

    uint8_t txbuff[128];
    int txbytes;
    uint8_t rxbuff[128];
    int rxbytes;

    uint8_t temp[6];
    uint8_t addr;
    uint8_t emgstate;

    uint8_t GUNAState;
    uint8_t GUNBState;
    uint8_t LowVoltMode[2];

    uint8_t GUNA_ELockState;
    uint8_t GUNB_ELockState;
    uint8_t GUNA_AuxPowerState;
    uint8_t GUNB_AuxPowerState;

    uint8_t GUNA_ELockCtrl;
    uint8_t GUNB_ELockCtrl;
    uint8_t GUNA_AuxPowerCtrl;
    uint8_t GUNB_AuxPowerCtrl;

    uint8_t stat1[2];
    uint8_t stat2[2];
    uint16_t power_allocated[2];
    float currI[2];
    float currV[2];
    
    uint8_t more_cmd;
    uint8_t need_send_ctrl_cmd;
    uint8_t ast_stage[2];
    uint8_t ast_type[2];
    uint8_t ast_check[2];
}ast_xh_t;

typedef enum ast_state
{
   AST_IDLE = 0,
   AST_FAULT = 1,
   AST_PLUGIN = 2,
   AST_PLUGIN_FAULT = 3,
   AST_STARTING = 4,
   AST_INSULATION = 5,
   AST_PRECHARGE = 6,
   AST_CHARGING = 7,
   AST_END = 8,
}ast_state_t;

// typedef enum ast_state
// {
//     AST_IDLE = 0,
//     AST_INSULATION= 1,
//     AST_PRECHARGE = 2,
//     AST_CHARGING = 3,
//     AST_END = 4
// }ast_state_t;

int ccu_AST_GUNA_ELock(ast_xh_t* dev, int lock);
int ccu_AST_GUNB_ELock(ast_xh_t* dev, int lock);

int ccu_AST_GUNA_AuxPower(ast_xh_t* dev, int close);
int ccu_AST_GUNB_AuxPower(ast_xh_t* dev, int close);

int ccu_AST_init(ast_xh_t* dev, const char* devname);
void* ccu_AST_loop(void* argv);
void ccu_AST_exit(ast_xh_t* dev);

#endif//__XH_CCU_H__
