/*
 * internalComm.h
 *
 *  Created on: 2019年5月7日
 *      Author: foluswen
 */

#ifndef _INTERNAL_COMM_H_
#define _INTERNAL_COMM_H_

//------------------------------------------------------------------------------
#include <stdint.h>

//------------------------------------------------------------------------------
//address
#define ADDR_AUX                                (0x01)
#define ADDR_FAN                                (0x02)
#if defined DD360 || defined DD360Audi || defined DD360ComBox
#define ADDR_RELAY                              (0x09)
#else
#define ADDR_RELAY                              (0x03)
#endif //defined DD360 || defined DD360Audi || defined DD360ComBox
#define ADDR_AC_PLUG                            (0x05)
#define ADDR_LED                                (0x06)
#define ADDR_BROADCAST                          (0xFF)

//command
#define CMD_QUERY_FW_VER                        (0x01)
#define CMD_QUERY_HW_VER                        (0x02)
#define CMD_QUERY_PRESENT_IN_VOL                (0x03)
#define CMD_QUERY_PRESENT_OUT_VOL               (0x04)
#define CMD_QUERY_FAN_SPEED                     (0x05)
#define CMD_QUERY_TEMPERATURE                   (0x06)
#define CMD_QUERY_AUX_POWER_VOL                 (0x07)
#define CMD_QUERY_RELAY_OUTPUT                  (0x08)
#define CMD_QUERY_GFD_ADC                       (0x09)
#define CMD_QUERY_GPIO_IN                       (0x0a)
#define CMD_QUERY_MODEL_NAME                    (0x24)
#define CMD_QUERY_AC_OUTPUT_CURRENT             (0x27)
#define CMD_QUERY_AC_STATUS                     (0x28)
#define CMD_QUERY_AC_ALARM_CODE                 (0x29)
#define CMD_QUERY_AC_OUTPUT_ENERGY              (0x2C)

#define CMD_CONFIG_FAN_SPEED                    (0x81)
#define CMD_CONFIG_MODEL_NAME                   (0x83)
#define CMD_CONFIG_RELAY_OUTPUT                 (0x85)
#define CMD_CONFIG_GPIO_OUTPUT                  (0x86)
#define CMD_CONFIG_RTC_DATA                     (0x87)
#define CMD_CONFIG_AC_LED_STATUS                (0x88)
#define CMD_CONFIG_AC_DUTY                      (0x89)
#define CMD_CONFIG_LEGACY_REQ                   (0x8A)
#define CMD_CONFIG_GDF_VALUE                    (0x8B)
#define CMD_CONFIG_RESET_MCU                    (0x8C)
#define CMD_CONFIG_CSU_MODE                     (0x90)
#define CMD_CONFIG_LEN_COLOR                    (0x93)

#define CMD_UPDATE_START                        (0xe0)
#define CMD_UPDATE_ABORT                        (0xe1)
#define CMD_UPDATE_TRANSFER                     (0xe2)
#define CMD_UPDATE_FINISH                       (0xe3)

//------------------------------------------------------------------------------
typedef struct Verion {
    char Version_FW[9];
    char Version_HW[9];
} Ver;

typedef struct PRESENTINPUTVOLTAGE {
    uint8_t inputType;    // 0x00: Line to Line       0x01: Line to Neutral
    double L1N_L12;
    double L2N_L23;
    double L3N_L31;
} PresentInputVoltage;

typedef struct PRESENTOUTPUTVOLTAGE {
    double behindFuse_Voltage_C1;
    double behindRelay_Voltage_C1;
    double behindFuse_Voltage_C2;
    double behindRelay_Voltage_C2;
} PresentOutputVoltage;

typedef struct FANSPEED {
    uint16_t speed[8];
} FanSpeed;

typedef struct TEMPERATURE {
    uint8_t temperature[8];
} Temperature;

typedef struct AUXPOWER {
    uint8_t voltage[8];
} AuxPower;

typedef struct RELAY {
    union {
        uint8_t relay_status[8];
        struct {
            uint8_t AC_Contactor : 1;     //bit 0
            uint8_t CCS_Precharge : 1; //bit 1
            uint8_t : 1;              //bit 2 reserved
            uint8_t : 1;                  //bit 3 reserved
            uint8_t : 1;                  //bit 4 reserved
            uint8_t : 1;                  //bit 5 reserved
            uint8_t : 1;                  //bit 6 reserved
            uint8_t : 1;                  //bit 7 reserved

            uint8_t Gun1_N : 1;           //bit 0
            uint8_t Gun1_P : 1;           //bit 1
            uint8_t Gun1_Parallel_N : 1; //bit 2
            uint8_t Gun1_Parallel_P : 1;  //bit 3
            uint8_t : 1;                  //bit 4 reserved
            uint8_t : 1;                  //bit 5 reserved
            uint8_t : 1;                  //bit 6 reserved
            uint8_t : 1;                  //bit 7 reserved

            uint8_t Gun2_N : 1;           //bit 0
            uint8_t Gun2_P : 1;           //bit 1
            uint8_t : 1;                  //bit 2 reserved
            uint8_t : 1;                  //bit 3 reserved
            uint8_t : 1;                  //bit 4 reserved
            uint8_t : 1;                  //bit 5 reserved
            uint8_t : 1;                  //bit 6 reserved
            uint8_t : 1;                  //bit 7 reserved
        } bits;
    } relay_event;
} Relay;

typedef struct GFD {
    unsigned short Resister_conn1;
    unsigned short voltage_conn1;
    uint8_t result_conn1;
    uint8_t rb_step_1;

    unsigned short Resister_conn2;
    unsigned short voltage_conn2;
    uint8_t result_conn2;
    uint8_t rb_step_2;
} Gfd;

typedef struct Gfd_CONFIG {
    uint8_t index;
    uint8_t state;
} Gfd_config;

typedef struct GPIO_IN {
    uint8_t AC_Connector;         // bit 0
    uint8_t AC_MainBreaker;           // bit 1
    uint8_t SPD;                      // bit 2
    uint8_t Door_Open;                // bit 3
    uint8_t GFD[2];                   // bit 4,5
    uint8_t AC_Drop;                  // bit 6

    uint8_t Emergency_IO;         // bit 0

    uint8_t Button_Emergency_Press;   // bit 0
    uint8_t Button_On_Press;          // bit 1
    uint8_t Button_Off_Press;     // bit 2
    uint8_t Key_1_Press;              // bit 3
    uint8_t Key_2_Press;              // bit 4
    uint8_t Key_3_Press;              // bit 5
    uint8_t Key_4_Press;              // bit 6
} Gpio_in;

typedef struct GPIO_OUT {
    uint8_t AC_Connector;
    uint8_t Button_LED[2];
    uint8_t System_LED[4];
} Gpio_out;

typedef struct RTC {
    uint8_t RtcData[14];
} Rtc;

typedef struct LED_Color {
    uint8_t Connect_1_Red;
    uint8_t Connect_1_Green;
    uint8_t Connect_1_Blue;
    uint8_t Connect_2_Red;
    uint8_t Connect_2_Green;
    uint8_t Connect_2_Blue;
} Led_Color;

typedef struct AC_Status {
    uint8_t       CpStatus;
    unsigned short      CurLimit;
    short               PilotVol_P;
    short               PilotVol_N;
    uint8_t       LockStatus;
    uint8_t       RelayStatus;
    uint8_t       ShutterStatus;
    uint8_t       MeterStatus;
    uint8_t       PpStatus;
    uint8_t       MaxCurrent;
    uint8_t       RotateSwitchStatus;
} Ac_Status;

typedef struct AC_LED_Status {
    uint8_t       ActionMode;
    unsigned long       AcAlarmCode;
} Ac_Led_Status;

typedef struct AC_Alarm_Code {
    uint32_t        AcAlarmCode;
} Ac_Alarm_code;

typedef struct AC_Charging_Energy {
    uint32_t        Energy;
} Ac_Charging_energy;

typedef struct AC_Charging_Current {
    unsigned short      OuputCurrentL1;
    unsigned short      OuputCurrentL2;
    unsigned short      OuputCurrentL3;
} Ac_Charging_current;

int Query_FW_Ver(uint8_t fd, uint8_t targetAddr, Ver *Ret_Buf);
int Query_HW_Ver(uint8_t fd, uint8_t targetAddr, Ver *Ret_Buf);
int Query_Present_InputVoltage(uint8_t fd, uint8_t targetAddr, PresentInputVoltage *Ret_Buf);
int Query_Present_OutputVoltage(uint8_t fd, uint8_t targetAddr, PresentOutputVoltage *Ret_Buf);
int Query_Fan_Speed(uint8_t fd, uint8_t targetAddr, FanSpeed *Ret_Buf);
int Query_Temperature(uint8_t fd, uint8_t targetAddr, Temperature *Ret_Buf);
int Query_Aux_PowerVoltage(uint8_t fd, uint8_t targetAddr, AuxPower *Ret_Buf);
int Query_Relay_Output(uint8_t fd, uint8_t targetAddr, Relay *Ret_Buf);
int Query_Gfd_Adc(uint8_t fd, uint8_t targetAddr, Gfd *Ret_Buf);
int Query_Gpio_Input(uint8_t fd, uint8_t targetAddr, Gpio_in *Ret_Buf);
int Query_Model_Name(uint8_t fd, uint8_t targetAddr, uint8_t *modelname);
int Query_AC_Status(uint8_t fd, uint8_t targetAddr, Ac_Status *Ret_Buf);
int Query_AC_Alarm_Code(uint8_t fd, uint8_t targetAddr, Ac_Alarm_code *Ret_Buf);
int Query_Charging_Energy(uint8_t fd, uint8_t targetAddr, Ac_Charging_energy *Ret_Buf);
int Query_Charging_Current(uint8_t fd, uint8_t targetAddr, Ac_Charging_current *Ret_Buf);

int Config_Fan_Speed(uint8_t fd, uint8_t targetAddr, FanSpeed *Set_Buf);
int Config_Relay_Output(uint8_t fd, uint8_t targetAddr, Relay *Set_Buf);
int Config_Gpio_Output(uint8_t fd, uint8_t targetAddr, Gpio_out *Set_Buf);
int Config_Gfd_Value(uint8_t fd, uint8_t targetAddr, Gfd_config *Set_Buf);
int Config_Model_Name(uint8_t fd, uint8_t targetAddr, uint8_t *modelname);
int Config_Rtc_Data(uint8_t fd, uint8_t targetAddr, Rtc *Set_Buf);

int Config_LED_Status(uint8_t fd, uint8_t targetAddr, Ac_Led_Status *Ret_Buf);
int Config_Legacy_Req(uint8_t fd, uint8_t targetAddr, uint8_t _switch);
int Config_Ac_Duty(uint8_t fd, uint8_t targetAddr, uint8_t _value);
int Config_CSU_Mode(uint8_t fd, uint8_t targetAddr);
int Config_Reset_MCU(uint8_t fd, uint8_t targetAddr);
int Config_Led_Color(uint8_t fd, uint8_t targetAddr, Led_Color *Ret_Buf);

int Update_Start(uint8_t fd, uint8_t targetAddr, uint32_t crc32);
int Update_Abord(uint8_t fd, uint8_t targetAddr);
int Update_Transfer(uint8_t fd, uint8_t targetAddr, uint32_t startAddr, uint8_t *data, uint16_t length);
int Update_Finish(uint8_t fd, uint8_t targetAddr);

#endif /* _INTERNAL_COMM_H_ */