
#include "xBeeAppEscapingMode.h"

//需要转义的数据
#define ESCAPING_DATA1 0x7E
#define ESCAPING_DATA2 0x7D
#define ESCAPING_DATA3 0x11
#define ESCAPING_DATA4 0x13

#define XBEE_APP2_ESCAP_LENGTH 150//数组长度
unsigned char xA2EsArray[XBEE_APP2_ESCAP_LENGTH];//保存转义完成后的数据

#define XBEE_APP2_MAKEFRAME_LENGTH 150//makeFrame数组长度
unsigned char xA2MakeFrame[XBEE_APP2_MAKEFRAME_LENGTH];//makeFrame保存转义完成后的数据


#define XBEE_APP2_UNMAKEFRAME_LENGTH 150//解析makeFrame数组长度
unsigned char xA2UnMakeFrame[XBEE_APP2_UNMAKEFRAME_LENGTH];//保存解析makeFrame完成后的数据

#define XBEE_APP2_REMOTE_LENGTH 150//远程命令
unsigned char xA2RemoteCommandFrame[XBEE_APP2_REMOTE_LENGTH];//远程命令数组

fRameFORMAT_t userSendFrame_t;//发送时候帧格式初始化
fRAME_0x90_RECEIVE_FORMAT_t comeFromWhom0x90Frame_t;//接收0x90时候帧格式
fRAME_0x91_RECEIVE_FORMAT_t comeFromWhom0x91Frame_t;//接收0x91时候帧格式
fRAME_0x17_REMOTE_FORMAT_t atRemoteCommand0x17Frame_t;//远程命令帧


void xBeeFrameFormatInit()//发送时候的帧结构初始化
{
    userSendFrame_t.frameHead = 0x7E;//帧头
    userSendFrame_t.frameLen = 0;//数据长度Number of bytes between length and checksum fields.
    userSendFrame_t.frameType = 0x10;//帧类型，Transmit Request
    userSendFrame_t.frameID = 0x01;
    userSendFrame_t.des64DeviceAdd[0] = 0x00;//器件ID 高
    userSendFrame_t.des64DeviceAdd[1] = 0x00;//器件ID  |
    userSendFrame_t.des64DeviceAdd[2] = 0x00;//器件ID  |
    userSendFrame_t.des64DeviceAdd[3] = 0x00;//器件ID  |
    userSendFrame_t.des64DeviceAdd[4] = 0x00;//器件ID  |
    userSendFrame_t.des64DeviceAdd[5] = 0x00;//器件ID  |
    userSendFrame_t.des64DeviceAdd[6] = 0xFF;//器件ID  |
    userSendFrame_t.des64DeviceAdd[7] = 0xFF;//器件ID 低
    userSendFrame_t.des16NetAdd = 0xFFFE ;//网络ID
    userSendFrame_t.broadcastRadius = 0x00;//广播半径
}

/*******************************************************************************
函数名：xBeeApp2EscapingMode
功  能：处理数据中需要转义后发送的数据
        data bytes which need escaping: 0x7E, 0x7D, 0x11, 0x13
参  数：buf,原始数据数组的首地址，len数组长度
返回值：处理后的数组地址
编  写：
时  间：2016-01-20
*******************************************************************************/
void xBeeApp2EscapingMode(unsigned char* buf)//返回一个处理完成后的数组的首地址
{
    unsigned char l = 0,xLen = 1;

    xA2EsArray[xLen++] = buf[1];

    for(l = 2; l <= buf[0]; l++)
    {
        if(buf[l] == ESCAPING_DATA1 || buf[l] == ESCAPING_DATA2 || buf[l] == ESCAPING_DATA3 || buf[l] == ESCAPING_DATA4)
        {
            xA2EsArray[xLen++] = 0x7D;
            xA2EsArray[xLen++] = buf[l]^0x20;//XOR
        }
        else
            xA2EsArray[xLen++] = buf[l];
    }
    xA2EsArray[0] = xLen - 1;//数据长度

    for(l = 0; l <= xLen; l++)
        buf[l] = xA2EsArray[l];//数据长度
}

/*******************************************************************************
函数名：xBeeApp2MakeFrame
功  能：

参  数:
返回值：
编  写：
时  间：2016-01-20
*******************************************************************************/
unsigned char* xBeeApp2MakeFrame(fRameFORMAT_t *sendFrameFormat,unsigned char* buf)//返回一个处理完成后的数组的首地址
{
    unsigned char len = buf[0];
    unsigned char l;
    unsigned char xLen = 1;//处理后的数组长度,放在数组的第一位
    unsigned char sumCheck = 0;//校验和

    sendFrameFormat->frameLen = len + 14;//总的数据长度
    xA2MakeFrame[xLen++] = sendFrameFormat->frameHead;//帧头0x7E
    xA2MakeFrame[xLen++] = sendFrameFormat->frameLen >> 8;//数据长度高八位
    xA2MakeFrame[xLen++] = sendFrameFormat->frameLen;//数据长度低八位
    xA2MakeFrame[xLen++] = sendFrameFormat->frameType;//帧类型，Transmit Request
    sumCheck += xA2MakeFrame[xLen - 1];
    xA2MakeFrame[xLen++] = sendFrameFormat->frameID;//
    sumCheck += xA2MakeFrame[xLen - 1];
    for(l = 0; l < 8; l++) //目标器件64位地址
    {
        xA2MakeFrame[xLen++] = sendFrameFormat->des64DeviceAdd[l];
        sumCheck += xA2MakeFrame[xLen - 1];
    }
    xA2MakeFrame[xLen++] = sendFrameFormat->des16NetAdd >> 8;//16位网络ID 高八位
    sumCheck += xA2MakeFrame[xLen - 1];
    xA2MakeFrame[xLen++] = sendFrameFormat->des16NetAdd;//16位网络ID 低八位
    sumCheck += xA2MakeFrame[xLen - 1];
    xA2MakeFrame[xLen++] = sendFrameFormat->broadcastRadius;//广播半径
    sumCheck += xA2MakeFrame[xLen - 1];
    xA2MakeFrame[xLen++] = sendFrameFormat->transmitOptions;//选项
    sumCheck += xA2MakeFrame[xLen - 1];
    //RFData
    for(l = 1; l < len + 1; l++)
    {
        xA2MakeFrame[xLen++] = buf[l];
        sumCheck += xA2MakeFrame[xLen - 1];
    }

    xA2MakeFrame[xLen++] = 0xFF - sumCheck;
    xA2MakeFrame[0] = xLen - 1;
    xBeeApp2EscapingMode(xA2MakeFrame);

    return xA2MakeFrame;//返回最终数组
}


/*******************************************************************************
*******************************************************************************/

/*******************************************************************************
*******************************************************************************/

/*******************************************************************************
*******************************************************************************/

/*******************************************************************************

函数名：xBeeApp2UnEscaping0x90Mode
功  能：接收指示，接收到0x91类型帧时的解析

参  数：buf,原始数据数组的首地址
返回值：处理后的数组地址
编  写：
时  间：2016-01-20
*******************************************************************************/
//if(buf[0] == 7E && buf[3] == 0x90);//
unsigned char* xBeeApp2UnEscaping0x90Mode(unsigned char* buf)//返回一个处理完成后的数组的首地址
{
    unsigned char l = 0;
    unsigned int frameDatLen;//帧中的数据长度字段
    unsigned int datLen,datLength;//帧中包含的数据长度
    //重新定义了一个与发送端相同的结构体，用来保存发送者的帧信息 comeFromWhoFrame_t
    comeFromWhom0x90Frame_t.frameHead = buf[0];//帧头
    comeFromWhom0x90Frame_t.frameLen = buf[1];//高八位
    comeFromWhom0x90Frame_t.frameLen <<= 8;
    comeFromWhom0x90Frame_t.frameLen |= buf[2];//低八位
    comeFromWhom0x90Frame_t.frameType = buf[3];//帧类型，Transmit Request,应该是0x90
    comeFromWhom0x90Frame_t.frameID = buf[4];//协议中，frameID是64位地址的最高位，所以最高位也是buf[4]
    comeFromWhom0x90Frame_t.source64DeviceAdd[0] = buf[5];//器件ID 高
    comeFromWhom0x90Frame_t.source64DeviceAdd[1]  = buf[6];//器件ID  |
    comeFromWhom0x90Frame_t.source64DeviceAdd[2]  = buf[7];//器件ID  |
    comeFromWhom0x90Frame_t.source64DeviceAdd[3]  = buf[8];//器件ID  |
    comeFromWhom0x90Frame_t.source64DeviceAdd[4]  = buf[9];//器件ID  |
    comeFromWhom0x90Frame_t.source64DeviceAdd[5]  = buf[10];//器件ID  |
    comeFromWhom0x90Frame_t.source64DeviceAdd[6]  = buf[11];//器件ID |
    comeFromWhom0x90Frame_t.source64DeviceAdd[7]  = buf[12];//器件ID 低
    comeFromWhom0x90Frame_t.source16NetAdd = buf[13] ;//网络ID
    comeFromWhom0x90Frame_t.source16NetAdd <<= 8 ;//网络ID
    comeFromWhom0x90Frame_t.source16NetAdd |= buf[14] ;//网络ID
    comeFromWhom0x90Frame_t.receiveOptions = buf[15];//接收选项

    frameDatLen = comeFromWhom0x90Frame_t.frameLen;//帧中标识的数据长度
    datLen = frameDatLen - 12;//负载数据长度
    datLength = 0;//真实数据长度
    for(l = 1; l < datLen + 1; l++) //取出帧中的负载数据
    {
        if(buf[l + 15] == 0x7D)
        {
            l++;//指向下一个
            xA2UnMakeFrame[datLength+1] = buf[l + 15] ^ 0x20;
            datLen++;
        }
        else
            xA2UnMakeFrame[datLength+1] = buf[l + 15];

        datLength++;//真实数据长度
    }
    xA2UnMakeFrame[0] = datLength;
    return xA2UnMakeFrame;//返回处理完成后的数组
}

/*******************************************************************************

函数名：xBeeApp2UnEscaping0x91Mode
功  能：接收指示，接收到0x91类型帧时的解析

参  数：buf,原始数据数组的首地址
返回值：处理后的数组地址
编  写：
时  间：2016-01-20
*******************************************************************************/
//if(buf[0] == 7E && buf[3] == 0x91);//
unsigned char* xBeeApp2UnEscaping0x91Mode(unsigned char* buf)//返回一个处理完成后的数组的首地址
{
    unsigned char l = 0;
    unsigned int frameDatLen;//帧中的数据长度字段
    unsigned int datLen,datLength;//帧中包含的数据长度
    //重新定义了一个与发送端相同的结构体，用来保存发送者的帧信息 comeFromWhoFrame_t
    comeFromWhom0x91Frame_t.frameHead = buf[0];//帧头
    comeFromWhom0x91Frame_t.frameLen = buf[1];//高八位
    comeFromWhom0x91Frame_t.frameLen <<= 8;
    comeFromWhom0x91Frame_t.frameLen |= buf[2];//低八位
    comeFromWhom0x91Frame_t.frameType = buf[3];//帧类型，Transmit Request,应该是0x90
    comeFromWhom0x91Frame_t.source64DeviceAdd[0] = buf[4];//器件ID 高
    comeFromWhom0x91Frame_t.source64DeviceAdd[1]  = buf[5];//器件ID  |
    comeFromWhom0x91Frame_t.source64DeviceAdd[2]  = buf[6];//器件ID  |
    comeFromWhom0x91Frame_t.source64DeviceAdd[3]  = buf[7];//器件ID  |
    comeFromWhom0x91Frame_t.source64DeviceAdd[4]  = buf[8];//器件ID  |
    comeFromWhom0x91Frame_t.source64DeviceAdd[5]  = buf[9];//器件ID  |
    comeFromWhom0x91Frame_t.source64DeviceAdd[6]  = buf[10];//器件ID |
    comeFromWhom0x91Frame_t.source64DeviceAdd[7]  = buf[11];//器件ID 低
    comeFromWhom0x91Frame_t.source16NetAdd = buf[12] ;//网络ID
    comeFromWhom0x91Frame_t.source16NetAdd <<= 8 ;//网络ID
    comeFromWhom0x91Frame_t.source16NetAdd |= buf[13] ;//网络ID
    comeFromWhom0x91Frame_t.sourceEndpoint = buf[14] ;//Source endpoint.
    comeFromWhom0x91Frame_t.destinationEndpoint = buf[15] ;//Destination Endpoint

    comeFromWhom0x91Frame_t.clusterID = buf[16] ;//Cluster ID
    comeFromWhom0x91Frame_t.clusterID <<= 8 ;//
    comeFromWhom0x91Frame_t.clusterID |= buf[17] ;//

    comeFromWhom0x91Frame_t.proFileID = buf[18] ;//proFileID
    comeFromWhom0x91Frame_t.proFileID <<= 8;//
    comeFromWhom0x91Frame_t.proFileID = buf[19] ;//
    comeFromWhom0x91Frame_t.receiveOptions = buf[20];//接收选项

    frameDatLen = comeFromWhom0x91Frame_t.frameLen;//帧中标识的数据长度
    datLen = frameDatLen - 18;//负载数据长度
    datLength = 0;//真实数据长度
    for(l = 1; l < datLen + 1; l++) //取出帧中的负载数据
    {
        if(buf[l + 22] == 0x7D)
        {
            l++;//指向下一个
            xA2UnMakeFrame[datLength+1] = buf[l + 22] ^ 0x20;
            datLen++;
        }
        else
            xA2UnMakeFrame[datLength+1] = buf[l + 22];

        datLength++;//真实数据长度
    }
    xA2UnMakeFrame[0] = datLength;
    return xA2UnMakeFrame;//返回处理完成后的数组
}

/*******************************************************************************
函数名：xBeeApp2RemoteFrame
功  能：

参  数:
返回值：
编  写：
时  间：2016-01-20
*******************************************************************************/
unsigned char* xBeeApp2RemoteFrame(void)////远程命令付值，返回赋值后的数组
{
    unsigned char sumCheck = 0;
    unsigned char xLen = 1;
    atRemoteCommand0x17Frame_t.frameHead = 0x7e;//帧头
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameHead;

    atRemoteCommand0x17Frame_t.frameLen = 0x0010;//
    xA2RemoteCommandFrame[xLen++] = (atRemoteCommand0x17Frame_t.frameLen >> 8);
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameLen;

    atRemoteCommand0x17Frame_t.frameType = 0x17;//帧类型
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameType;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.frameID = 0x01;//帧ID
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.frameID;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[0] = 0;//器件ID 高
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[0];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[1]  = 0;//器件ID  |
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[1];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[2]  = 0;//器件ID  |
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[2];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[3]  = 0;//器件ID  |
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[3];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[4]  = 0;//器件ID  |
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[4];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[5]  = 0;//器件ID  |
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[5];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[6]  = 0xFF;//器件ID |
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[6];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des64DeviceAdd[7]  = 0xFF;//器件ID 低
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des64DeviceAdd[7];
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.des16NetAdd = 0xFFFE ;//网络ID
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des16NetAdd >> 8;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.des16NetAdd;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];

    atRemoteCommand0x17Frame_t.remoteCommandOptions = 0x02 ;//命令选项
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.remoteCommandOptions;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];

    atRemoteCommand0x17Frame_t.ATCommand = 0x4248 ;//命令
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.ATCommand >> 8;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.ATCommand;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    atRemoteCommand0x17Frame_t.commandParameter = 0x01 ;//命令参数
    xA2RemoteCommandFrame[xLen++] = atRemoteCommand0x17Frame_t.commandParameter;
    sumCheck += xA2RemoteCommandFrame[xLen - 1];
    sumCheck = 0xFF - sumCheck;
    xA2RemoteCommandFrame[xLen++] = sumCheck;
    xA2RemoteCommandFrame[0] = xLen - 1;

    return xA2RemoteCommandFrame;
}
