#include "M4255_NFC.h"

int ccu_NFCReader_Init(M4255_t *dev, const char *devname)
{
    if (dev)
    {
        com_ttySx_init(&dev->tty, devname, 9600, 8, 'N', 1);
        ccu_NFCReader_set_Beep(dev, 1);
        ccu_NFCReader_set_ReadOnce(dev, M4255_READ_ONLYONCE);
        ccu_NFCReader_set_WorkMode(dev, M4255_CMD_MODE);
        return 0;
    }
    return -1;
}

void ccu_NFCReader_Exit(M4255_t *dev)
{
    if (dev)
    {
        com_ttySx_exit(&dev->tty);
    }
}

static uint8_t ccu_NFC_CheckSum(uint8_t *data, int len)
{
    uint8_t sum = 0;
    int i;
    for (i=0; i<len-1; i++)
    {
        sum ^= data[i];
    }
    return ~sum;
}

int ccu_NFC_read_CardID(M4255_t *dev, uint8_t *card_type, uint8_t* card_id)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 8;
    dev->txbuff[idx++] = 0xA1;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = 0x01;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 12, 1000);
        if (rxbyte == 12)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[11] && dev->txbuff[4] == 0x00)
            {
                card_type[0] = dev->rxbuff[5];
                card_type[1] = dev->rxbuff[6];
                card_id[0] = dev->rxbuff[7];
                card_id[1] = dev->rxbuff[8];
                card_id[2] = dev->rxbuff[9];
                card_id[3] = dev->rxbuff[10];
                return 0;
            }
        }
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
        }
        return -2;
    }
    return -1;
}

int ccu_NFC_read_DataBlock(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint8_t* buff)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 8;
    if (useKeyB) dev->txbuff[idx++] = 0x5C; else dev->txbuff[idx++] = 0xA3;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = block_id;
    dev->txbuff[idx++] = 0x01;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 0x16, 1000);
        if (rxbyte == 0x16)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[0x15] && dev->txbuff[4] == 0x00)
            {
                int i;
                for (i=0; i<16; i++)
                {
                    buff[i++] = dev->rxbuff[5+i];
                }
                return 0;
            }
        }
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[0x07] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
        }
        return -2;
    }
    return -1;
}

int ccu_NFC_fill_DataBlock(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint8_t* data)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 0x17;
    if (useKeyB) dev->txbuff[idx++] = 0x5B; else dev->txbuff[idx++] = 0xA4;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = block_id;
    dev->txbuff[idx++] = 0x01;
    int i;
    for (i=0; i<16; i++)
    {
        dev->txbuff[idx++] = data[i];
    }
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

static int ccu_NFCReader_fill_PIN(M4255_t *dev, uint8_t* data)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_READER_SET;
    dev->txbuff[idx++] = 0x15;
    dev->txbuff[idx++] = 0xC3;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    int i;
    for (i=0; i<16; i++)
    {
        dev->txbuff[idx++] = data[i];
    }
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

static int ccu_NFCReader_fill_ORG(M4255_t *dev, uint8_t* data)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_READER_SET;
    dev->txbuff[idx++] = 0x0B;
    dev->txbuff[idx++] = 0xC4;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    int i;
    for (i=0; i<6; i++)
    {
        dev->txbuff[idx++] = data[i];
    }
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

int ccu_NFC_fill_PINBlock(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint8_t* keyA, uint8_t *keyB)
{
    uint8_t orgpin[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    if (ccu_NFCReader_fill_ORG(dev, orgpin) == 0)
    {
        int i;
        uint8_t data[16];
        for (i=0; i<6; i++)
        {
            data[i] = keyA[i];
            data[10+i] = keyB[i];
        }
        data[6] = 0xFF;
        data[7] = 0x07;
        data[8] = 0x80;
        data[9] = 0x69;
        if (ccu_NFCReader_fill_PIN(dev, data) == 0)
        {
            int idx = 0;
            dev->txbuff[idx++] = M4255_CARD_CMD;
            dev->txbuff[idx++] = 0x08;
            if (useKeyB) dev->txbuff[idx++] = 0x5A; else dev->txbuff[idx++] = 0xA5;
            dev->txbuff[idx++] = M4255_RS485ADDR;
            dev->txbuff[idx++] = block_id;
            dev->txbuff[idx++] = 0x01;
            dev->txbuff[idx++] = 0x00;
            dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
            if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
            {
                int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
                if (rxbyte == 8)
                {
                    if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
                    {
                        return 0;
                    }
                    if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
                    {
                        return -3;
                    }
                    return -4;
                }
                return -2;
            }
            return -1;
        }
        return -6;
    }
    return -5;
}

int ccu_NFCReader_Init_PrepaidIC(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint32_t value)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 0x0B;
    if (useKeyB) dev->txbuff[idx++] = 0x59; else dev->txbuff[idx++] = 0xA6;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = block_id;
    dev->txbuff[idx++] = 0x01;
    dev->txbuff[idx++] = (value>>0)&0xFF;
    dev->txbuff[idx++] = (value>>8)&0xFF;
    dev->txbuff[idx++] = (value>>16)&0xFF;
    dev->txbuff[idx++] = (value>>24)&0xFF;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 0xA, 1000);
        if (rxbyte == 0xA)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[9] && dev->txbuff[4] == 0x00)
            {
                return 0;
            }
        }
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x03)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
        }
        return -2;
    }
    return -1;
}

int ccu_NFC_sub_PrepaidIC(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint32_t value)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 0x0B;
    if (useKeyB) dev->txbuff[idx++] = 0x58; else dev->txbuff[idx++] = 0xA7;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = block_id;
    dev->txbuff[idx++] = 0x01;
    dev->txbuff[idx++] = (value>>0)&0xFF;
    dev->txbuff[idx++] = (value>>8)&0xFF;
    dev->txbuff[idx++] = (value>>16)&0xFF;
    dev->txbuff[idx++] = (value>>24)&0xFF;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 0xA, 1000);
        if (rxbyte == 0xA)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[9] && dev->txbuff[4] == 0x00)
            {
                return 0;
            }
        }
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x03)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
        }
        return -2;
    }
    return -1;
}

int ccu_NFC_add_PrepaidIC(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint32_t value)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 0x0B;
    if (useKeyB) dev->txbuff[idx++] = 0x57; else dev->txbuff[idx++] = 0xA8;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = block_id;
    dev->txbuff[idx++] = 0x01;
    dev->txbuff[idx++] = (value>>0)&0xFF;
    dev->txbuff[idx++] = (value>>8)&0xFF;
    dev->txbuff[idx++] = (value>>16)&0xFF;
    dev->txbuff[idx++] = (value>>24)&0xFF;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 0xA, 1000);
        if (rxbyte == 0xA)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[9] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
        }
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x03)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
        }
        return -2;
    }
    return -1;
}

int ccu_NFC_read_PrepaidIC(M4255_t *dev, uint8_t useKeyB, uint8_t block_id, uint32_t *value)
{
    value[0] = 0;
    int idx = 0;
    dev->txbuff[idx++] = M4255_CARD_CMD;
    dev->txbuff[idx++] = 0x08;
    if (useKeyB) dev->txbuff[idx++] = 0x57; else dev->txbuff[idx++] = 0xA8;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = block_id;
    dev->txbuff[idx++] = 0x01;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 0xA, 1000);
        if (rxbyte == 0xA)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[9] && dev->rxbuff[4] == 0x00)
            {
                value[0] = dev->rxbuff[5]|(dev->rxbuff[6]<<8)|(dev->rxbuff[7]<<16)|(dev->rxbuff[8]<<24);
                return 0;
            }
        }
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
        }
        return -2;
    }
    return -1;
}

int ccu_NFCReader_set_Beep(M4255_t *dev, uint8_t on)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_READER_SET;
    dev->txbuff[idx++] = 0x08;
    dev->txbuff[idx++] = 0xC2;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = on;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

int ccu_NFCReader_force_Beep(M4255_t *dev, uint8_t on)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_OTHER_CMD;
    dev->txbuff[idx++] = 0x08;
    if (on==1)
        dev->txbuff[idx++] = 0xD1;
    else
        dev->txbuff[idx++] = 0xD2;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

int ccu_NFCReader_set_WorkMode(M4255_t *dev, uint8_t mode)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_READER_SET;
    dev->txbuff[idx++] = 0x08;
    dev->txbuff[idx++] = 0xC1;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = mode;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

int ccu_NFCReader_set_ReadOnce(M4255_t *dev, uint8_t type)
{
    int idx = 0;
    dev->txbuff[idx++] = M4255_READER_SET;
    dev->txbuff[idx++] = 0x08;
    dev->txbuff[idx++] = 0xC8;
    dev->txbuff[idx++] = M4255_RS485ADDR;
    dev->txbuff[idx++] = type;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = 0x00;
    dev->txbuff[idx++] = ccu_NFC_CheckSum(dev->txbuff, dev->txbuff[1]);
    if (com_ttySx_send(&dev->tty, dev->txbuff, dev->txbuff[1]) == dev->txbuff[1])
    {
        int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 8, 1000);
        if (rxbyte == 8)
        {
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x00)
            {
                return 0;
            }
            if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[7] && dev->rxbuff[4] == 0x01)
            {
                return -3;
            }
            return -4;
        }
        return -2;
    }
    return -1;
}

/*
****************************************************************************************************************************************
**      NFCA == Philips MIFARE A == ISO14443 A
**      NFCB == Philips MIFARE B == ISO14443 B
**      NFCF == Sony Felica TM
**      NULL == free Card，即无卡
**      UPAY == 银联标志的银行卡，ISO7816定义的接触式IC卡，有类似SIM卡的铜片，6脚和8脚两种
**      PAXJ == 百富的POS机
****************************************************************************************************************************************
*/

int ccu_NFCReader_wait_CardID(M4255_t *dev, int timeout, char* card_id)
{
    int rxbyte = com_ttySx_recv_with_timeout(&dev->tty, dev->rxbuff, 0x1C, timeout);
    if (rxbyte == 0xC)
    {
        if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[0xB] && dev->rxbuff[4] == 0x00)
        {
            sprintf(card_id, "NFCA%02X%02X%02X%02X%02X%02X", dev->rxbuff[5], dev->rxbuff[6], dev->rxbuff[7], dev->rxbuff[8], dev->rxbuff[9], dev->rxbuff[10]);
            return 0;
        }
    }
    if (rxbyte == 0x1C)
    {
        if (ccu_NFC_CheckSum(dev->rxbuff, dev->rxbuff[1]) == dev->rxbuff[0x1B] && dev->rxbuff[4] == 0x00)
        {
            sprintf(card_id, "NFCA%02X%02X%02X%02X%02X%02X", dev->rxbuff[5], dev->rxbuff[6], dev->rxbuff[7], dev->rxbuff[8], dev->rxbuff[9], dev->rxbuff[10]);
            return 0;
        }
    }
    return -1;
}
