/*
 * internalComm.c
 *
 *  Created on: 2019年5月7日
 *      Author: foluswen
 */
#include <stdio.h>      /*標準輸入輸出定義*/
#include <stdlib.h>     /*標準函數庫定義*/
#include <stdint.h>
#include <string.h>

#include <unistd.h>
#include <termios.h>

#include "internalComm.h"
#include "../Config.h"

//------------------------------------------------------------------------------
int tranceiveRelDelayTime(int fd, uint8_t *cmd, uint8_t cmd_len, uint8_t *rx, uint16_t _delay)
{
    int len;
    //sleep(2); //required to make flush work, for some reason
    tcflush(fd, TCIOFLUSH);

    if (write(fd, cmd, cmd_len) >= cmd_len) {
        usleep(_delay * 1000);
        len = read(fd, rx, 512);
    } else {
#ifdef SystemLogMessage
        DEBUG_ERROR("Serial command %s response fail.\n", cmd);
#endif
    }

    return len;
}

int tranceive(int fd, uint8_t *cmd, uint8_t cmd_len, uint8_t *rx)
{
    int len;
    //sleep(2); //required to make flush work, for some reason
    tcflush(fd, TCIOFLUSH);

    if (write(fd, cmd, cmd_len) >= cmd_len) {
        usleep(15000);
        len = read(fd, rx, 512);
    } else {
#ifdef SystemLogMessage
        DEBUG_ERROR("Serial command %s response fail.\n", cmd);
#endif
    }

    return len;
}

//------------------------------------------------------------------------------
//===== Query =====
//------------------------------------------------------------------------------
int Query_FW_Ver(uint8_t fd, uint8_t targetAddr, Ver *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FW_VER, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

//      for (int i = 0; i < 7; i++)
//          printf("tx = %x \n", tx[i]);
//      for (int i = 0; i < len; i++)
//          printf("rx = %x \n", rx[i]);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            memcpy(Ret_Buf->Version_FW, (char *)rx + 6, (rx[4] | rx[5] << 8));
            *(Ret_Buf->Version_FW + 8) = 0x00;
            result = PASS;
        }
    }

    return result;
}

int Query_HW_Ver(uint8_t fd, uint8_t targetAddr, Ver *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_HW_VER, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            memcpy(Ret_Buf->Version_HW, (char *)rx + 6, (rx[4] | rx[5] << 8));
            *(Ret_Buf->Version_HW + 8) = 0x00;
            result = PASS;
        }
    }

    return result;
}

int Query_Present_InputVoltage(uint8_t fd, uint8_t targetAddr, PresentInputVoltage *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_IN_VOL, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 13) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }
        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                chksum != 0) {
            Ret_Buf->inputType = rx[6];
            Ret_Buf->L1N_L12 = (rx[7] | (rx[8] << 8)) / 10.0;
            Ret_Buf->L2N_L23 = (rx[9] | (rx[10] << 8)) / 10.0;
            Ret_Buf->L3N_L31 = (rx[11] | (rx[12] << 8)) / 10.0;

            if (Ret_Buf->L1N_L12 >= 320 ||
                    Ret_Buf->L2N_L23 >= 320 ||
                    Ret_Buf->L3N_L31 >= 320) {
                result = FAIL;
            } else {
                result = PASS;
            }
        }
    }

    return result;
}

int Query_Present_OutputVoltage(uint8_t fd, uint8_t targetAddr, PresentOutputVoltage *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_PRESENT_OUT_VOL, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {

            Ret_Buf->behindFuse_Voltage_C1 = (rx[6] | (rx[7] << 8));
            Ret_Buf->behindRelay_Voltage_C1 = (rx[8] | (rx[9] << 8));
            if ((rx[4] | rx[5] << 8) > 4) {
                Ret_Buf->behindFuse_Voltage_C2 = (rx[10] | (rx[11] << 8));
                Ret_Buf->behindRelay_Voltage_C2 = (rx[12] | (rx[13] << 8));
            }
            result = PASS;
        }
    }

    return result;
}

int Query_Fan_Speed(uint8_t fd, uint8_t targetAddr, FanSpeed *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_FAN_SPEED, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            for (int idx = 0; idx < 4; idx++) {
                Ret_Buf->speed[idx] = (rx[6 + (2 * idx)] | (rx[6 + (2 * idx) + 1] << 8));
            }

            result = PASS;
        }
    }

    return result;
}

int Query_Temperature(uint8_t fd, uint8_t targetAddr, Temperature *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_TEMPERATURE, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            for (int idx = 0; idx < 4; idx++) {
                Ret_Buf->temperature[idx] = rx[6 + idx] - 60;
            }

            result = PASS;
        }
    }


    return result;
}

int Query_Aux_PowerVoltage(uint8_t fd, uint8_t targetAddr, AuxPower *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AUX_POWER_VOL, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
                Ret_Buf->voltage[idx] = rx[6 + idx];
            }

            result = PASS;
        }
    }

    return result;
}

int Query_Relay_Output(uint8_t fd, uint8_t targetAddr, Relay *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_RELAY_OUTPUT, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

//  for (int i = 0; i < 7; i++)
//      printf("tx = %x \n", tx[i]);
//  for (int i = 0; i < len; i++)
//      printf("rx = %x \n", rx[i]);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->relay_event.bits.AC_Contactor = (rx[6] >> 0) & 0x01;
            Ret_Buf->relay_event.bits.CCS_Precharge = (rx[6] >> 1) & 0x01;

            Ret_Buf->relay_event.bits.Gun1_N = (rx[7] >> 0) & 0x01;
            Ret_Buf->relay_event.bits.Gun1_P = (rx[7] >> 1) & 0x01;
            Ret_Buf->relay_event.bits.Gun1_Parallel_N = (rx[7] >> 2) & 0x01;
            Ret_Buf->relay_event.bits.Gun1_Parallel_P = (rx[7] >> 3) & 0x01;

            Ret_Buf->relay_event.bits.Gun2_N = (rx[8] >> 0) & 0x01;
            Ret_Buf->relay_event.bits.Gun2_P = (rx[8] >> 1) & 0x01;
            result = PASS;
        }
    }

    return result;
}

int Query_Gfd_Adc(uint8_t fd, uint8_t targetAddr, Gfd *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GFD_ADC, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

//  for(int i = 0; i < 7; i++)
//      printf ("tx = %d \n", tx[i]);
    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            //printf("Query_Gfd_Adc fail \n");
            return result;
        }

//      for(int i = 0; i < len; i++)
//          printf ("rx = %d \n", rx[i]);

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->Resister_conn1 = (rx[6] | (rx[7] << 8));
            Ret_Buf->voltage_conn1 = (rx[8] | (rx[9] << 8));
            Ret_Buf->result_conn1 = rx[10];
            Ret_Buf->rb_step_1 = rx[11];

            Ret_Buf->Resister_conn2 = (rx[12] | (rx[13] << 8));
            Ret_Buf->voltage_conn2 = (rx[14] | (rx[15] << 8));
            Ret_Buf->result_conn2 = rx[16];
            Ret_Buf->rb_step_2 = rx[17];

            result = PASS;
        }
    }

    return result;
}

int Query_Gpio_Input(uint8_t fd, uint8_t targetAddr, Gpio_in *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_GPIO_IN, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->AC_Connector       = (rx[6] >> 0) & 0x01;
            Ret_Buf->AC_MainBreaker     = (rx[6] >> 1) & 0x01;
            Ret_Buf->SPD                = (rx[6] >> 2) & 0x01;
            Ret_Buf->Door_Open          = ((rx[6] >> 3) & 0x01);
            Ret_Buf->GFD[0]             = (rx[6] >> 4) & 0x01;
            Ret_Buf->GFD[1]             = (rx[6] >> 5) & 0x01;
            Ret_Buf->AC_Drop            = (rx[6] >> 6) & 0x01;

            Ret_Buf->Emergency_IO       = (rx[7] >> 0) & 0x01;

            Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
            Ret_Buf->Button_On_Press    = (rx[8] >> 1) & 0x01;
            Ret_Buf->Button_Off_Press   = (rx[8] >> 2) & 0x01;
            Ret_Buf->Key_1_Press        = (rx[8] >> 3) & 0x01;
            Ret_Buf->Key_2_Press        = (rx[8] >> 4) & 0x01;
            Ret_Buf->Key_3_Press        = (rx[8] >> 5) & 0x01;
            Ret_Buf->Key_4_Press        = (rx[8] >> 6) & 0x01;

            result = PASS;
        }
    }

    return result;
}

int Query_Model_Name(uint8_t fd, uint8_t targetAddr, uint8_t *modelname)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_MODEL_NAME, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            strncpy((char *)modelname, (char *)(rx + 6), (rx[4] | rx[5] << 8));
            result = PASS;
        }
    }

    return result;
}

int Query_Charging_Current(uint8_t fd, uint8_t targetAddr, Ac_Charging_current *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_OUTPUT_CURRENT, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->OuputCurrentL1 = rx[6] + (rx[7] << 8);
            Ret_Buf->OuputCurrentL2 = rx[8] + (rx[9] << 8);
            Ret_Buf->OuputCurrentL3 = rx[10] + (rx[11] << 8);
            result = PASS;
        }
    }

    return result;
}

int Query_AC_Status(uint8_t fd, uint8_t targetAddr, Ac_Status *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_STATUS, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->CpStatus = rx[6];
            Ret_Buf->CurLimit = (rx[7] | (rx[8] << 8));
            Ret_Buf->PilotVol_P = (rx[9] | (rx[10] << 8));
            Ret_Buf->PilotVol_N = (rx[11] | (rx[12] << 8));
            Ret_Buf->LockStatus = rx[13];
            Ret_Buf->RelayStatus = rx[14];
            Ret_Buf->ShutterStatus = rx[15];
            Ret_Buf->MeterStatus = rx[16];
            Ret_Buf->PpStatus = rx[17];
            Ret_Buf->MaxCurrent = rx[18];
            Ret_Buf->RotateSwitchStatus = rx[19];
//
//          Ret_Buf->AC_Connector       = (rx[6] >> 0) & 0x01;
//          Ret_Buf->AC_MainBreaker     = (rx[6] >> 1) & 0x01;
//          Ret_Buf->SPD                = (rx[6] >> 2) & 0x01;
//          Ret_Buf->Door_Open          = (rx[6] >> 3) & 0x01;
//          Ret_Buf->GFD[0]             = (rx[6] >> 4) & 0x01;
//          Ret_Buf->GFD[1]             = (rx[6] >> 5) & 0x01;
//          Ret_Buf->AC_Drop            = (rx[6] >> 6) & 0x01;
//
//          Ret_Buf->Emergency_IO       = (rx[7] >> 0) & 0x01;
//
//          Ret_Buf->Button_Emergency_Press = (rx[8] >> 0) & 0x01;
//          Ret_Buf->Button_On_Press    = (rx[8] >> 1) & 0x01;
//          Ret_Buf->Button_Off_Press   = (rx[8] >> 2) & 0x01;
//          Ret_Buf->Key_1_Press        = (rx[8] >> 3) & 0x01;
//          Ret_Buf->Key_2_Press        = (rx[8] >> 4) & 0x01;
//          Ret_Buf->Key_3_Press        = (rx[8] >> 5) & 0x01;
//          Ret_Buf->Key_4_Press        = (rx[8] >> 6) & 0x01;

            result = PASS;
        }
    }

    return result;
}

int Query_AC_Alarm_Code(uint8_t fd, uint8_t targetAddr, Ac_Alarm_code *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_ALARM_CODE, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->AcAlarmCode = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
            result = PASS;
        }
    }

    return result;
}

int Query_Charging_Energy(uint8_t fd, uint8_t targetAddr, Ac_Charging_energy *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_QUERY_AC_OUTPUT_ENERGY, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            Ret_Buf->Energy = rx[6] + (rx[7] << 8) + (rx[8] << 16) + (rx[9] << 24);
            result = PASS;
        }
    }

    return result;
}

//------------------------------------------------------------------------------
//===== Configure =====
//------------------------------------------------------------------------------
int Config_Fan_Speed(uint8_t fd, uint8_t targetAddr, FanSpeed *Set_Buf)
{
    int result = FAIL;
    uint8_t tx[15] = {0xaa, 0x00, targetAddr, CMD_CONFIG_FAN_SPEED, 0x08, 0x00, Set_Buf->speed[0] & 0xff, (Set_Buf->speed[0] >> 8) & 0xff, Set_Buf->speed[1] & 0xff, (Set_Buf->speed[1] >> 8) & 0xff, Set_Buf->speed[2] & 0xff, (Set_Buf->speed[2] >> 8) & 0xff, Set_Buf->speed[3] & 0xff, (Set_Buf->speed[3] >> 8) & 0xff, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[14] = chksum;

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Model_Name(uint8_t fd, uint8_t targetAddr, uint8_t *modelname)
{
    int result = FAIL;
    uint8_t tx[21] = {0xaa, 0x00, targetAddr, CMD_CONFIG_MODEL_NAME, 0x0E, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                     };
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    memcpy(tx + 6, modelname, 14);

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[20] = chksum;

//  for(int i = 0; i < 21; i++)
//              printf ("tx = %x \n", tx[i]);
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
//  for(int i = 0; i < len; i++)
//                  printf ("rx = %x \n", rx[i]);
    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Relay_Output(uint8_t fd, uint8_t targetAddr, Relay *Set_Buf)
{
    int result = FAIL;
    uint8_t tx[10] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RELAY_OUTPUT, 0x03, 0x00, Set_Buf->relay_event.relay_status[0], Set_Buf->relay_event.relay_status[1], Set_Buf->relay_event.relay_status[2]};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[9] = chksum;

//  for (int i = 0; i < 10; i++)
//      printf("set relay cmd : tx = %x \n", tx[i]);

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

//      for (int i = 0; i < len; i++)
//          printf("set relay cmd : rx = %x \n", rx[i]);

        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Gpio_Output(uint8_t fd, uint8_t targetAddr, Gpio_out *Set_Buf)
{
    int result = FAIL;
    uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GPIO_OUTPUT, 0x01, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    tx[6] |= (Set_Buf->AC_Connector ? 0x01 : 0x00);

    for (int idx = 0; idx < 2; idx++) {
        tx[6] |= (Set_Buf->Button_LED[idx] ? 0x01 : 0x00) << (1 + idx);
    }

    for (int idx = 0; idx < 4; idx++) {
        tx[6] |= (Set_Buf->System_LED[idx] ? 0x01 : 0x00) << (3 + idx);
    }

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[14] = chksum;

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            result = PASS;
        }
    }

    return result;
}

int Config_Rtc_Data(uint8_t fd, uint8_t targetAddr, Rtc *Set_Buf)
{
    int result = FAIL;
    uint8_t tx[21] = { 0xaa, 0x00, targetAddr, CMD_CONFIG_RTC_DATA, 0x0E, 0x00, Set_Buf->RtcData[0], Set_Buf->RtcData[1],
                       Set_Buf->RtcData[2], Set_Buf->RtcData[3], Set_Buf->RtcData[4], Set_Buf->RtcData[5], Set_Buf->RtcData[6], Set_Buf->RtcData[7],
                       Set_Buf->RtcData[8], Set_Buf->RtcData[9], Set_Buf->RtcData[10], Set_Buf->RtcData[11], Set_Buf->RtcData[12], Set_Buf->RtcData[13]
                     };
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[20] = chksum;

    if (tranceive(fd, tx, sizeof(tx), rx) > 0) {
        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_LED_Status(uint8_t fd, uint8_t targetAddr, Ac_Led_Status *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[12] = {0xaa, 0x00, targetAddr, CMD_CONFIG_AC_LED_STATUS, 0x05, 0x00, Ret_Buf->ActionMode, (Ret_Buf->AcAlarmCode >> 0) & 0xFF,
                      (Ret_Buf->AcAlarmCode >> 8) & 0xFF, (Ret_Buf->AcAlarmCode >> 16) & 0xFF, (Ret_Buf->AcAlarmCode >> 24) & 0xFF
                     };
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[11] = chksum;

    if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Ac_Duty(uint8_t fd, uint8_t targetAddr, uint8_t _value)
{
    int result = FAIL;
    uint8_t tx[8] = {0xaa, 0x00, targetAddr, CMD_CONFIG_AC_DUTY, 0x01, 0x00, _value};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[7] = chksum;

    if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Legacy_Req(uint8_t fd, uint8_t targetAddr, uint8_t _switch)
{
    int result = FAIL;
    uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_LEGACY_REQ, 0x02, 0x00, _switch, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[8] = chksum;

    if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Reset_MCU(uint8_t fd, uint8_t targetAddr)
{
    int result = FAIL;
    uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_RESET_MCU, 0x02, 0x00, 0x01, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[7] = chksum;

    if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Gfd_Value(uint8_t fd, uint8_t targetAddr, Gfd_config *Set_Buf)
{
    int result = FAIL;
    uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_GDF_VALUE, 0x02, 0x00, 0x00, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    tx[6] = Set_Buf->index;
    tx[7] = Set_Buf->state;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[8] = chksum;

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3])) {
            result = PASS;
        }
    }

    return result;
}

int Config_CSU_Mode(uint8_t fd, uint8_t targetAddr)
{
    int result = FAIL;
    uint8_t tx[9] = {0xaa, 0x00, targetAddr, CMD_CONFIG_CSU_MODE, 0x02, 0x00, 0x01, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[7] = chksum;

    if (tranceiveRelDelayTime(fd, tx, sizeof(tx), rx, 100) > 0) {
        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                rx[6] == PASS) {
            result = PASS;
        }
    }

    return result;
}

int Config_Led_Color(uint8_t fd, uint8_t targetAddr, Led_Color *Ret_Buf)
{
    int result = FAIL;
    uint8_t tx[13] = {0xaa,
                      0x00,
                      targetAddr,
                      CMD_CONFIG_LEN_COLOR,
                      0x06,
                      0x00,
                      Ret_Buf->Connect_1_Red,
                      Ret_Buf->Connect_1_Green,
                      Ret_Buf->Connect_1_Blue,
                      Ret_Buf->Connect_2_Red,
                      Ret_Buf->Connect_2_Green,
                      Ret_Buf->Connect_2_Blue
                     };
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[12] = chksum;

//  for(int i = 0; i < 13; i++)
//          printf ("tx = %x \n", tx[i]);

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

//  for(int i = 0; i < len; i++)
//      printf ("rx = %x \n", rx[i]);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                (rx[6] == PASS)) {
            result = PASS;
        }
    }

    return result;
}

//------------------------------------------------------------------------------
//===== Update =====
//------------------------------------------------------------------------------
int Update_Start(uint8_t fd, uint8_t targetAddr, uint32_t crc32)
{
    int result = FAIL;
    uint8_t tx[11] = {0xaa, 0x00, targetAddr, CMD_UPDATE_START, 0x04, 0x00, (crc32 >> 0) & 0xff, (crc32 >> 8) & 0xff, (crc32 >> 16) & 0xff, (crc32 >> 24) & 0xff, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[10] = chksum;

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);
    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        chksum = 0x00;
        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                (rx[6] == 0x00)) {
            result = PASS;
        }
    }

    return result;
}

int Update_Abord(uint8_t fd, uint8_t targetAddr)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_ABORT, 0x04, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                (rx[6] == 0x00)) {
            result = PASS;
        }
    }

    return result;
}

int Update_Transfer(uint8_t fd, uint8_t targetAddr, uint32_t startAddr, uint8_t *data, uint16_t length)
{
    int result = FAIL;
    uint8_t tx[11 + length];
    uint8_t rx[512];
    uint8_t chksum = 0x00;

    tx[0] = 0xaa;
    tx[1] = 0x00;
    tx[2] = targetAddr;
    tx[3] = CMD_UPDATE_TRANSFER;
    tx[4] = (4 + length) & 0xff;
    tx[5] = ((4 + length) >> 8) & 0xff;
    tx[6] = (startAddr >> 0) & 0xff;
    tx[7] = (startAddr >> 8) & 0xff;
    tx[8] = (startAddr >> 16) & 0xff;
    tx[9] = (startAddr >> 24) & 0xff;
    memcpy(tx + 10, data, length);

    for (int idx = 0; idx < (tx[4] | tx[5] << 8); idx++) {
        chksum ^= tx[6 + idx];
    }
    tx[sizeof(tx) - 1] = chksum;

    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                (rx[6] == 0x00)) {
            result = PASS;
        }
    }

    return result;
}

int Update_Finish(uint8_t fd, uint8_t targetAddr)
{
    int result = FAIL;
    uint8_t tx[7] = {0xaa, 0x00, targetAddr, CMD_UPDATE_FINISH, 0x04, 0x00, 0x00};
    uint8_t rx[512];
    uint8_t chksum = 0x00;
    uint8_t len = tranceive(fd, tx, sizeof(tx), rx);

    if (len > 6) {
        if (len < 6 + (rx[4] | rx[5] << 8)) {
            return result;
        }

        for (int idx = 0; idx < (rx[4] | rx[5] << 8); idx++) {
            chksum ^= rx[6 + idx];
        }

        if ((chksum == rx[6 + (rx[4] | rx[5] << 8)]) &&
                (rx[2] == tx[1]) &&
                (rx[1] == tx[2]) &&
                (rx[3] == tx[3]) &&
                (rx[6] == 0x00)) {
            result = PASS;
        }
    }

    return result;
}