#include "uart.hpp"
#include <zlog.h>

extern zlog_category_t *zct;
extern zlog_category_t *zbt;

void Uart::UpdateZigbeeInfoCtrl() {
    char command[5] = {0xab, 0xbc, 0xcd, 0xd1, 0xaa};
    WriteToUart(command, 5);
    zlog_info(zct, "UpdateZigbeeInfoCtrl ");
}

void Uart::modify_distaddr_info(unsigned short id, char *zigbee, unsigned char *distAddr) {
    char command[6] = {0xDE, 0xDF, 0xEF, 0xDC, ((char *)&id)[1], ((char *)&id)[0]};
    WriteToUart(command, 6);
    mssleep(10000);
    char command1[6] = {0xDE, 0xDF, 0xEF, 0xD2, distAddr[0], distAddr[1]};
    WriteToUart(command1, 6);
}

void Uart::getZigbeeSignal(uint16_t ushortAdd) {
    mssleep(10000);
    char command[6] = {0x00};
    command[0] = 0xDE;
    command[1] = 0xDF;
    command[2] = 0xEF;
    command[3] = 0xDA;
    command[4] = UINT16_HIGH(ushortAdd);
    command[5] = UINT16_LOW(ushortAdd);
    WriteToUart(command, 6);
}

void Uart::modify_DistAddr(unsigned char *distAddr) {
    char command[6] = {0x00};
    memset(command, 0x00, sizeof(command));
    command[0] = 0xDE;
    command[1] = 0xDF;
    command[2] = 0xEF;
    command[3] = 0xD2;
    command[4] = distAddr[0];
    command[5] = distAddr[1];
    WriteToUart(command, 6);
}
void Uart::ModifyDistAddr(uint16_t distAddr){
    char command[6] = {0x00};
    memset(command, 0x00, sizeof(command));
    command[0] = 0xDE;
    command[1] = 0xDF;
    command[2] = 0xEF;
    command[3] = 0xD2;
    command[4] = (distAddr >> 8) & 0xFF;
    command[5] = distAddr & 0xFF;
    WriteToUart(command, 6);
}

void Uart::modify_LocalAddr(unsigned short id) {
    char command[6] = {0x00};
    command[0] = 0xDE;
    command[1] = 0xDF;
    command[2] = 0xEF;
    command[3] = 0xDC;
    command[4] = ((char *)&id)[1];
    command[5] = ((char *)&id)[0];
    WriteToUart(command, 6);
}

void Uart::modify_Localchannel(unsigned char pad) {
    char command[6] = {0x00};
    command[0] = 0xDE;
    command[1] = 0xDF;
    command[2] = 0xEF;
    command[3] = 0xD1;
    command[4] = pad & 0xff;
    WriteToUart(command, 5);
}

void Uart::modify_LocalPanID(unsigned short padID) {
    char command[6] = {0x00};
    command[0] = 0xDE;
    command[1] = 0xDF;
    command[2] = 0xEF;
    command[3] = 0xDB;
    command[4] = ((char *)&padID)[1];
    command[5] = ((char *)&padID)[0];
    WriteToUart(command, 6);
}

void Uart::modify_info(unsigned short id, char *zigbee) {
    int i, con;
    char command[100];
    char tmp = 0;
    command[0] = 0xab;
    command[1] = 0xbc;
    command[2] = 0xcd;
    command[3] = 0xd6;
    command[4] = ((char *)&id)[0];
    command[5] = ((char *)&id)[1];
    command[6] = 0;
    if (zigbee != NULL) {
        con = 71;
        memcpy(&(command[6]), zigbee + 4, 65);
    } else {
        con = 6;
    }
    for (i = 0; i < con; i++) {
        tmp += command[i];
    }
    command[i] = 0xaa;
    WriteToUart(command, i + 1);
    sleep(1);
}

void Uart::zigbee_reset(unsigned short pad, unsigned short type) {
    char command[10], tmp = 0;
    int i = 0;
    command[0] = 0xab;
    command[1] = 0xbc;
    command[2] = 0xcd;
    command[3] = 0xd9;
    command[4] = ((char *)&pad)[1];
    command[5] = ((char *)&pad)[0];
    command[6] = ((char *)&type)[1];
    command[7] = ((char *)&type)[0];
    for (i = 0; i < 8; i++) {
        tmp += command[i];
    }
    command[8] = tmp;
    WriteToUart(command, 9);
}

void Uart::WriteChanl2Zigbee(unsigned char pad) {
    zlog_info(zct, "WriteChanl2Zigbee : %d\n", pad);
    unsigned char pad1 = pad;
    unsigned short tmp;
    tmp = GlobalConfig::Zigbee_G.MyAddr;
    GlobalConfig::Zigbee_G.Chan = pad1;
    modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);

    gpio_set(GlobalConfig::GPIO_G.zigReset, 0);
    mssleep(10000);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
}

void Uart::WritePanId2Zigbee(unsigned short pad) {
    zlog_info(zct, "WritePanId2Zigbee : %d", pad);
    unsigned short pad1 = pad, tmp;
    tmp = GlobalConfig::Zigbee_G.MyAddr;
    swap((char *)&pad1);
    GlobalConfig::Zigbee_G.PanID = pad1;
    modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);

    gpio_set(GlobalConfig::GPIO_G.zigReset, 0);
    mssleep(10000);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
}
void Uart::WriteSpeed2Zigbee() {
    GlobalConfig::Zigbee_G.Serial_Rate = 0x07;
    GlobalConfig::Zigbee_G.Serial_DataB = 0x08;
    GlobalConfig::Zigbee_G.Serial_StopB = 0x01;
    unsigned short tmp;
    tmp = GlobalConfig::Zigbee_G.MyAddr;
    modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 0);
    mssleep(10000);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
}

void Uart::WriteTranTimeout2Zigbee(unsigned char Time) {
    zlog_info(zct, "WriteTranTimeout2Zigbee : %d", Time);

    unsigned short tmp = GlobalConfig::Zigbee_G.MyAddr;
    GlobalConfig::Zigbee_G.PowerLevel = 0x03;
    GlobalConfig::Zigbee_G.RetryNum = 0x0A;
    GlobalConfig::Zigbee_G.TranTimeout = Time;
    modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);

    gpio_set(GlobalConfig::GPIO_G.zigReset, 0);
    mssleep(10000);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
}

void Uart::WriteShortAddr2Zigbee(unsigned short pad) {
    zlog_info(zct, "WriteShortAddr2Zigbee : %4x", (unsigned short)pad);
    unsigned short pad1 = pad, tmp;
    tmp = GlobalConfig::Zigbee_G.MyAddr;

    GlobalConfig::Zigbee_G.MyAddr = pad1;
    modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);

    gpio_set(GlobalConfig::GPIO_G.zigReset, 0);
    mssleep(10000);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
    sleep(1);
}

void Uart::WriteShortAddr_DistAddr2Zigbee(unsigned short pad, unsigned char *pDestShortAddr) {
    zlog_info(zct, "WriteShortAddr2Zigbee : %4x\n", (unsigned short)pad);
    unsigned short pad1 = pad, tmp;
    tmp = GlobalConfig::Zigbee_G.MyAddr;
    swap((char *)&pad1);
    swap((char *)&tmp);
    char tmpDest[8] = {0x00};
    sprintf(tmpDest, "%02x%02x", pDestShortAddr[0], pDestShortAddr[1]);
    memcpy(&GlobalConfig::Zigbee_G.DstAddr, pDestShortAddr, 2);
    GlobalConfig::Zigbee_G.MyAddr = pad1;

    zlog_info(zct, "DstAddr = %x\n", GlobalConfig::Zigbee_G.DstAddr);
    GlobalConfig::ZigbeeInfo_G.MyAddr = "9999";
    modify_info(tmp, (char *)&GlobalConfig::Zigbee_G);

    gpio_set(GlobalConfig::GPIO_G.zigReset, 0);
    mssleep(10000);
    gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
}
