#include <stdio.h>      /*標準輸入輸出定義*/
#include <stdlib.h>     /*標準函數庫定義*/
#include <stdint.h>
#include <string.h>

#include <linux/can.h>
#include <linux/can/raw.h>

#include <unistd.h>     /*Unix 標準函數定義*/
#include <fcntl.h>      /*檔控制定義*/
#include <termios.h>    /*PPSIX 終端控制定義*/
#include <errno.h>      /*錯誤號定義*/

#include "Ev_Comm.h"
#include "Module_EvComm.h"
#include "../Config.h"
//------------------------------------------------------------------------------
int PackageIdCmd(int cmd)
{
    return cmd | 0x80000000;
}

void SendCmdToEvboard(int cmd, uint8_t *data, uint8_t dataLen)
{
    DcCommonInfo* ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
    int fd = GetCanFd();
    struct can_frame frame;
    int i;
    int len = 0;
    char _info[1024];
    frame.can_id = cmd;
    frame.can_dlc = dataLen;
    memcpy(frame.data, data, sizeof(frame.data));

    write(fd, &frame, sizeof(struct can_frame));

    if (ShmDcCommonData->showCanPackage) {
        len += sprintf(&_info[len], "CAN Dispenser => EV Tx:\t[0x%X] ", cmd);
        for (i = 0; i < dataLen; i++) {
            len += sprintf(&_info[len], "%X ", data[i]);
        }
        len += sprintf(&_info[len], "\n");
        printf("%s",_info);
    }
}

//------------------------------------------------------------------------------
void SetTargetAddr(uint8_t *target_number, uint8_t index)
{
    int id = PackageIdCmd(CMD_ADDR_ASSIGNMENT + index);
    //printf("intCmd = %x \n", cmd & CAN_EFF_MASK);
    //cmd = cmd & CAN_EFF_MASK;

    uint8_t data[8];

    data[0] = *target_number;
    data[1] = *(target_number + 1);
    data[2] = *(target_number + 2);
    data[3] = *(target_number + 3);
    data[4] = index;

    SendCmdToEvboard(id, data, 5);
}

void GetFirmwareVersion(uint8_t gunIndex, uint8_t toId)
{
    int id = PackageIdCmd(CMD_GET_FW_VER + toId);
    uint8_t data[8];

    SendCmdToEvboard(id, data, 0);
}

void GetHardwareVersion(uint8_t gunIndex, uint8_t toId)
{
    int id = PackageIdCmd(CMD_GET_HW_VER + toId);
    uint8_t data[8];

    SendCmdToEvboard(id, data, 0);
}

void SyncRtcInfo(uint8_t gunIndex, uint8_t toId, int epoch)
{
    int id = PackageIdCmd(CMD_SYNC_RTC + toId);
    uint8_t data[8];

    data[0] = epoch & 0xff;
    data[1] = (epoch >> 8) & 0xff;
    data[2] = (epoch >> 16) & 0xff;
    data[3] = (epoch >> 24) & 0xff;

    SendCmdToEvboard(id, data, 4);
}

void SetChargingPermission(uint8_t gunIndex, uint8_t permissionStatus, uint16_t aOutputPw, uint16_t aOutputCur, uint16_t aOutputVol, uint8_t toId)
{
    int id = PackageIdCmd(CMD_CHARGING_PERMISSION + toId);
    uint8_t data[8];

    data[0] = permissionStatus;
    data[1] = aOutputPw & 0xff;
    data[2] = (aOutputPw >> 8) & 0xff;
    data[3] = aOutputCur & 0xff;
    data[4] = (aOutputCur >> 8) & 0xff;
    data[5] = aOutputVol & 0xff;
    data[6] = (aOutputVol >> 8) & 0xff;
    data[7] = 0xf0;

    SendCmdToEvboard(id, data, sizeof(data));
}

void SetPresentOutputPower(uint16_t outputVol_b1, uint16_t outputCur_b1, uint16_t outputVol_b2, uint16_t outputCur_b2)
{
    int id = PackageIdCmd(CMD_PRESENT_OUTPUT_POWER);
    uint8_t data[8];

    data[0] = outputVol_b1 & 0xff;
    data[1] = (outputVol_b1 >> 8) & 0xff;
    data[2] = outputCur_b1 & 0xff;
    data[3] = (outputCur_b1 >> 8) & 0xff;
    data[4] = outputVol_b2 & 0xff;
    data[5] = (outputVol_b2 >> 8) & 0xff;
    data[6] = outputCur_b2 & 0xff;
    data[7] = (outputCur_b2 >> 8) & 0xff;

    SendCmdToEvboard(id, data, 8);
}

void SetPresentOutputCapacity(uint16_t aOutputPw_b1, uint16_t aOutputCur_b1, uint16_t aOutputPw_b2, uint16_t aOutputCur_b2)
{
    int id = PackageIdCmd(CMD_PRESENT_OUTPUT_CAPABILITIES);
    uint8_t data[8];

    data[0] = aOutputPw_b1 & 0xff;
    data[1] = (aOutputPw_b1 >> 8) & 0xff;
    data[2] = aOutputCur_b1 & 0xff;
    data[3] = (aOutputCur_b1 >> 8) & 0xff;
    data[4] = aOutputPw_b2 & 0xff;
    data[5] = (aOutputPw_b2 >> 8) & 0xff;
    data[6] = aOutputCur_b2 & 0xff;
    data[7] = (aOutputCur_b2 >> 8) & 0xff;

    SendCmdToEvboard(id, data, 8);
}

void GetOutputReq(uint8_t gunIndex, uint8_t toId)
{
    int id = PackageIdCmd(CMD_GET_OUTPUT_REQ + toId);
    uint8_t data[8];

    SendCmdToEvboard(id, data, 0);
}

void GetEvBatteryInfo(uint8_t gunIndex, uint8_t toId)
{
    int id = PackageIdCmd(CMD_GET_BATTERY_INFO + toId);
    uint8_t data[8];

    SendCmdToEvboard(id, data, 0);
}

void EvseStopChargingEvent(uint8_t stopResult, uint8_t *stopReason, uint8_t toId)
{
    int id = PackageIdCmd(CMD_EVSE_STOP_CHARGING + toId);
    uint8_t data[8];

    data[0] = stopResult;
    data[1] = *stopReason;
    data[2] = *(stopReason + 1);
    data[3] = *(stopReason + 2);
    data[4] = *(stopReason + 3);
    data[5] = *(stopReason + 4);
    data[6] = *(stopReason + 5);

    SendCmdToEvboard(id, data, 7);
}

void GetMiscellaneousInfo(uint8_t gunIndex, uint8_t relayStatus, float power, float voltage, uint8_t toId)
{
    int id = PackageIdCmd(CMD_GET_MISC_INFO + toId);
    uint8_t data[8];

    int _power = power * 10;

    data[0] = relayStatus;
    data[1] = (int)_power & 0xff;
    data[2] = ((int)_power >> 8) & 0xff;
    data[3] = (int)voltage & 0xff;
    data[4] = ((int)voltage >> 8) & 0xff;

    SendCmdToEvboard(id, data, 5);
}

void SetIsolationStatus(uint8_t gunIndex, uint8_t result, uint8_t toId)
{
    int id = PackageIdCmd(CMD_ISOLATION_STATUS + toId);
    uint8_t data[8];

    data[0] = result;
    SendCmdToEvboard(id, data, 1);
}

void SetEvsePrechargeInfo(uint8_t gunIndex, uint8_t result, uint8_t toId)
{
    int id = PackageIdCmd(CMD_EVSE_PRECHARGE + toId);
    uint8_t data[8];

    data[0] = result;
    SendCmdToEvboard(id, data, 1);
}

void GetEVCCIDReq(uint8_t gunIndex, uint8_t toId)
{
    int id = PackageIdCmd(CMD_EVCCID_REQ + toId);
    uint8_t data[8];

    SendCmdToEvboard(id, data, 0);
}