#ifndef _DO_COMM_H_
#define _DO_COMM_H_

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

//------------------------------------------------------------------------------
//--- common define ---
//------------------------------------------------------------------------------
#define CMP_ETH_IP_HEAD                         "192.168.100"

#define DoIPAddress                             "192.168.100.1"
#define DoTcpPort                               36000

#define IMAGE_FILE_PATH                         "../mnt"
#define TFTP_PULL_CMD                           "tftp"
#define SIGTERM_MSG                             "SegmentFault.~~~~\n"

#define MAX_REGISTER_NUM                        30

#define CHECK_NETWORK_FAIL_COUNT                10//10
#define CONNECT_SERVER_FAIL_COUNT               3//5
#define WARNING_CODE_SIZE                       6

#define MAX_VOLTAGE                             10000
#define MAX_CURRENCY                            5000
#define MAX_POWER                               3600

#define DISPENSER_INIT_SUCC                     0x01
#define DISPENER_INIT_FAIL                      0x02
#define DISPENSER_SOCKET_RECONN                 0x03

#define VERSION_BUF_SIZE                        32

#define PRICES_UNIT                             0.01

#define LOOP_RETRY_TIME                         1000 //1 second

//------------------------------------------------------------------------------
//--- dispenser ID ---
//------------------------------------------------------------------------------
#define ID_RESERVER                             0x00
#define ID_OF_DISPENSER                         0x01
#define ID_REGISTER                             0xFF

//------------------------------------------------------------------------------
//--- dispenser operation ---
//------------------------------------------------------------------------------
#define OP_READ_DATA                            0x01
#define OP_WRITE_DATA                           0x02
#define OP_WAIT_RESPONSE                        0x03

//------------------------------------------------------------------------------
//--- connector status ---
//------------------------------------------------------------------------------
#define CONN_ST_IDLE                            0x00
#define CONN_ST_PREPARING                       0x01
#define CONN_ST_CHARGING                        0x02
#define CONN_ST_TERMINATING                     0x03
#define CONN_ST_ALARM                           0x04

//------------------------------------------------------------------------------
//--- dispenser register ---
//------------------------------------------------------------------------------
#define REG_MODEL_NAME                          0x01
#define REG_CONNECTOR_ID                        0x02
#define REG_POWER_CABINET_STATUS                0x03
#define REG_DISPENSER_STATUS                    0x04
#define REG_CHARGING_CAP                        0x05
#define REG_CHARGING_TARGET                     0x06
#define REG_SOFTWARE_UPDATE                     0x07
#define REG_PLUG_IN_STATE                       0x08
#define REG_CONNECTOR_STATE                     0x09
#define REG_USER_ID                             0x0A
#define REG_CHARGING_PERMISSION                 0x0B
#define REG_MISC_CONTROL                        0X0C
#define REG_REPORT_CSU_VERSION                  0X0D
#define REG_REPORT_OTHER_VERSION                0X0E
#define REG_PRESENT_CHARGING_INFO               0X0F
#define REG_QRCODE_URL_INFO                     0X10
#define REG_WAIT_PLUG_IT_STATE                  0x11
#define REG_Ground_Fault_Detection              0x12
#define REG_CABINET_DCM_VERSION                 0x13
#define REG_CABINET_OTHER_VERSION               0x14
#define REG_TOTAL_PSU_QUANTITY                  0x15
#define REG_PSU_VERSION                         0x16
#define REG_RESERVATION_IDTAG                   0x17
#define REG_DISPENSER_REQUEST                   0x18
#define REG_REMOTE_START_NO_ID                  0x19
#define REG_REFUND_AMOUNT                       0x1A
#define REG_PREPAYMENT_INFO                     0x1B
#define REG_PAYMENT_FAIL_REASON                 0x1C
#define REG_CONNECTOR_QR_CODE                   0x1D
#define REG_STATION_INFO                        0x1E
#define REG_DEDUCT_INFO                         0x1F

//------------------------------------------------------------------------------
//--- dispenser result ---
//------------------------------------------------------------------------------
#define COMMAND_RESULT_OK                       0x01
#define COMMAND_RESULT_NG                       0x02 //wait state

//------------------------------------------------------------------------------
//--- power cabinet misc status code ---
//------------------------------------------------------------------------------
#define MISC_ST_MISC_CMD                        "B40001"

#define MISC_ST_VERSION                         "B40999"

//------------------------------------------------------------------------------
//--- power cabinet misc command ---
//------------------------------------------------------------------------------
#define MISC_CMD_CONNECOTOR_TIMEOUT             (0X0001)
#define MISC_CMD_OPERATIVE_STATE                (0X0002)
#define MISC_CMD_DEFAULT_PRICES                 (0x0003)
#define MISC_CMD_DEFAULT_CURRENCY               (0x0004)
#define MISC_CMD_ACCOUNT_BALANCE                (0x0005)

#define MISC_CMD_BACKEND_STATUS                 (0x0006)
#define MISC_CMD_ETHERNET_STATUS                (0x0007)
#define MISC_CMD_WIFI_STATUS                    (0x0008)
#define MISC_CMD_4G_STATUS                      (0x0009)
#define MISC_CMD_BILLING_INFO                   (0x000A)
#define MISC_CMD_WEB_STOP_CHARGING              (0x000B)
#define MISC_CMD_AUTH_DISABLE					(0x000C)
#define MISC_CMD_EVCCID_ENABLE					(0x000D)

#define MISC_CMD_HARDWARE_REBOOT                (0x0101)
#define MISC_CMD_SOFTWARE_RESTART               (0x0102)
#define MISC_CMD_REMOTE_START_CHARGING          (0x0103)
#define MISC_CMD_REMOTE_STOP_CHARGING           (0x0104)
#define MISC_CMD_REMOTE_UNLOCK                  (0x0105)
#define MISC_CMD_RESERVATION                    (0x0106)
#define MISC_CMD_CHANGE_LCM_PAGE                (0x0107)
#define MISC_CMD_QR_CODE_REQ                    (0x0108)
#define MISC_CMD_STATION_INFO_REQ               (0x0109)
#define MISC_CMD_FINAL_COST_REQ                 (0x010A)
#define MISC_CMD_LINE_STATUS_REQ                (0x010B)

#define ST_UPDATE_FIRMWARE                      (0x01)
#define ST_NO_UPDATE_FIRMWARE                   (0x02)

#define LCM_PAGE_REMOTE_START_NO_ID             (0x0001)
//#define LCM_PAGE_JOIN_LINE                      (0x0002)
//#define LCM_PAGE_PAYMENT_SWITCH                 (0x0003)

//------------------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)

typedef struct StDeductInfoVar {
    unsigned char DeductResult;                     // 0: Fail, 1: Pass
    unsigned char IsDonateInvoice;                  // 0: Do not donate, 1: Donate
    int TransactionId;								// transaction id
    int Amount;                                     // unit = 0.01 dollar
    char ApprovalNumber[12];						// approval number
} DeductInfoVar;

/*
typedef union
{
    unsigned int Value;
    struct
    {
        unsigned int JoinLine:1;                    // 0: no effect,                1: JoinLine page is trigger
        unsigned int PaymentSwitch:1;               // 0: no effect,                1: PaymentSwitch page is trigger
        unsigned int res:30;
    }bits;
} ChangePageReq;
*/

typedef union
{
    unsigned int Value;
    struct
    {
        unsigned int ReservationReq:1;              // 0: no effect,                1: need to request Reservation
        unsigned int RemoteStartNoID:1;             // 0: no effect,                1: need to request RemoteStartNoID
        unsigned int StationInfoReq:1;              // 0: no effect,                1: need to request StationInfo
        unsigned int FinalCostReq:1;                // 0: no effect,                1: need to request FinalCost
        unsigned int res:28;
    }bits;
} MoreInfoReq;

typedef struct StStationVar {
    int StationID;
    char StationName[64];
    int WeatherID;
    float Temperature;
} StationVar;

typedef struct StConnectorState {
    uint8_t State;
    uint8_t WarningCode[6];
    uint8_t ConnectorTemp;
    uint8_t ChillerTemp;
    uint8_t PlugIn;
    uint8_t Reserved[2];
} ConnectorState;

typedef struct StConnectorIDTable {
    uint8_t DcConnID1;
    uint8_t DcConnID2;
    uint8_t AcConnID;
    uint8_t Reserved;
} ConnectorIDTable;

typedef struct StDoCommGlobalVar {
    uint8_t SeqNum;
    uint8_t DisConnCount;
    uint8_t ConnectorID[3]; //keep from power cabinet
    uint8_t MiscCmd;
    uint8_t Reserved[2];
} DoCommGblData;

typedef struct StCommnadHead {
    uint8_t SeqNum;     //sequence number 0 ~ 255
    uint8_t ID;         //0: Reserved, 0x01 ~ 0xFE: connector ID, 0xFF: exists in register 1 and register 2
    uint8_t OP;         //0x01: read, 0x02: write, 0x03: response from power cabinet
    uint8_t DataLen;    //raw data Length
} CmdHead;

typedef struct StCommandData {
    uint8_t Register;   /*  0x01: dispenser model name (maximum 32 bytes) and the value of dispenser switch
                            0x02: The ID to connector equipped at dispenser
                            0x03: Power cabinet status
                            0x04: Dispenser status
                            0x05: The charging capacity of each connector
                            0x06: The required voltage and current during charging
                            0x07: The indicator of software update to dispenser
                            0x08: Indicate whether the connector is plug-in or not
                            0x09: Indicate the connector state
                            0x0A: The user ID can be RFID card number, EVCCID, etc.
                            0x0B: Charging permission from power cabinet to dispenser
                        */
    uint8_t Data[250];
} CmdData;

typedef struct StResultData {
    uint8_t Register;   /*  0x01: dispenser model name (maximum 32 bytes) and the value of dispenser switch
                            0x02: The ID to connector equipped at dispenser
                            0x03: Power cabinet status
                            0x04: Dispenser status
                            0x05: The charging capacity of each connector
                            0x06: The required voltage and current during charging
                            0x07: The indicator of software update to dispenser
                            0x08: Indicate whether the connector is plug-in or not
                            0x09: Indicate the connector state
                            0x0A: The user ID can be RFID card number, EVCCID, etc.
                            0x0B: Charging permission from power cabinet to dispenser
                        */
    uint8_t Result;
    uint8_t Data[249];
} ResultData;

typedef struct StCsuCmdPkt {
    CmdHead Head;
    CmdData Data;
} CsuCmdPkt;

typedef struct StCsuResultPkt {
    CmdHead Head;
    ResultData Data;
} CsuResultPkt;

typedef struct StMiscCommand {
    uint16_t CMD;
    uint8_t Value[4];
} MiscCommand;

typedef struct StPresentChargingInfo {
    uint16_t PresentChargingVoltage;   //0~6553.5 volt
    uint16_t PresentChargingCurrent;   //0~6553.5 amp
    int RemainChargingDuration;     // second
    uint8_t EvBatterySoc;               // 0~100%
} PreChargingInfo;

typedef struct StGroundFaultDetection { //Ground Fault Detection
    uint8_t Status;
} GroundFaultDetection;

typedef struct StSoftwareUpdInfo {
    uint8_t UpdateState;         //1:update , 2: not update
    uint8_t ImgName[248];
} SoftwareUpdInfo;

typedef struct StCapabilityInfo {
    uint16_t OutputVoltage;
    uint16_t OutputCurrent;
    uint16_t OutputPower;
    uint8_t Reserved[2];
} CapabilityInfo;

typedef struct StAccountInfo {
    uint8_t Currency;
    int UserPrices;
    int TotalCost;
    int Balance;
    int Discount;
    int Transaction;
    uint8_t Reserved[3];
} AccountInfo;

#pragma pack(pop)

#endif /* _DO_COMM_H_ */