RW_NDEF_T3T.c 4.12 KB
Newer Older
Gus Grubba's avatar
Gus Grubba committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/*
*         Copyright (c), NXP Semiconductors Caen / France
*
*                     (C)NXP Semiconductors
*       All rights are reserved. Reproduction in whole or in part is
*      prohibited without the written consent of the copyright owner.
*  NXP reserves the right to make changes without notice at any time.
* NXP makes no warranty, expressed, implied or statutory, including but
* not limited to any implied warranty of merchantability or fitness for any
*particular purpose, or that the use will not infringe any third party patent,
* copyright or trademark. NXP must not be liable for any loss or damage
*                          arising from its use.
*/

#ifdef RW_SUPPORT
#ifndef NO_NDEF_SUPPORT
#include <TML/inc/tool.h>
#include <NfcLibrary/NdefLibrary/inc/RW_NDEF.h>

#define T3T_MAGIC_NUMBER    0xE1
#define T3T_NDEF_TLV        0x03

unsigned char T3T_Check[] = {0x10,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0B,0x00,0x1,0x80,0x00};

typedef enum
{
    Initial,
    Getting_AttributeInfo,
    Reading_CardContent
} RW_NDEF_T3T_state_t;

typedef struct
{
    unsigned char IDm[8];
    unsigned char BlkNb;
    unsigned short Ptr;
    unsigned short Size;
    unsigned char *p;
} RW_NDEF_T3T_Ndef_t;

static RW_NDEF_T3T_state_t eRW_NDEF_T3T_State = Initial;
static RW_NDEF_T3T_Ndef_t RW_NDEF_T3T_Ndef;

void RW_NDEF_T3T_Reset(void)
{
    eRW_NDEF_T3T_State = Initial;
    RW_NDEF_T3T_Ndef.p = NdefBuffer;
}

void RW_NDEF_T3T_SetIDm(unsigned char *pIDm)
{
    memcpy(RW_NDEF_T3T_Ndef.IDm, pIDm, sizeof(RW_NDEF_T3T_Ndef.IDm));
    memcpy(&T3T_Check[2], pIDm, sizeof(RW_NDEF_T3T_Ndef.IDm));
}

void RW_NDEF_T3T_Read_Next(unsigned char *pRsp, unsigned short Rsp_size, unsigned char *pCmd, unsigned short *pCmd_size)
{
    /* By default no further command to be sent */
    *pCmd_size = 0;

    switch(eRW_NDEF_T3T_State)
    {
    case Initial:
        /* Get AttributeInfo */
        memcpy (pCmd, T3T_Check, sizeof(T3T_Check));
        *pCmd_size = sizeof(T3T_Check);
        eRW_NDEF_T3T_State = Getting_AttributeInfo;
        break;

    case Getting_AttributeInfo:
        /* Is Check success ?*/
        if ((pRsp[Rsp_size-1] == 0x00) && (pRsp[1] == 0x07) && (pRsp[10] == 0x00) && (pRsp[11] == 0x00))
        {
            /* Fill File structure */
            RW_NDEF_T3T_Ndef.Size = (pRsp[24] << 16) + (pRsp[25] << 16) + pRsp[26];

            /* If provisioned buffer is not large enough, notify the application and stop reading */
            if (RW_NDEF_T3T_Ndef.Size > RW_MAX_NDEF_FILE_SIZE)
            {
                if(pRW_NDEF_PullCb != NULL) pRW_NDEF_PullCb(NULL, 0);
                break;
            }

            RW_NDEF_T3T_Ndef.Ptr = 0;
            RW_NDEF_T3T_Ndef.BlkNb = 1;

            /* Read first NDEF block */
            memcpy (pCmd, T3T_Check, sizeof(T3T_Check));
            pCmd[15] = 0x01;
            *pCmd_size = sizeof(T3T_Check);
            eRW_NDEF_T3T_State = Reading_CardContent;
        }
        break;

    case Reading_CardContent:
        /* Is Check success ?*/
        if ((pRsp[Rsp_size-1] == 0x00) && (pRsp[1] == 0x07) && (pRsp[10] == 0x00) && (pRsp[11] == 0x00))
        {
            /* Is NDEF message read completed ?*/
            if ((RW_NDEF_T3T_Ndef.Size - RW_NDEF_T3T_Ndef.Ptr) <= 16)
            {
                memcpy(&RW_NDEF_T3T_Ndef.p[RW_NDEF_T3T_Ndef.Ptr], &pRsp[13], (RW_NDEF_T3T_Ndef.Size - RW_NDEF_T3T_Ndef.Ptr));
                /* Notify application of the NDEF reception */
                if(pRW_NDEF_PullCb != NULL) pRW_NDEF_PullCb(RW_NDEF_T3T_Ndef.p, RW_NDEF_T3T_Ndef.Size);
            }
            else
            {
                memcpy(&RW_NDEF_T3T_Ndef.p[RW_NDEF_T3T_Ndef.Ptr], &pRsp[13], 16);
                RW_NDEF_T3T_Ndef.Ptr += 16;
                RW_NDEF_T3T_Ndef.BlkNb++;

                /* Read next NDEF block */
                memcpy (pCmd, T3T_Check, sizeof(T3T_Check));
                pCmd[15] = RW_NDEF_T3T_Ndef.BlkNb;
                *pCmd_size = sizeof(T3T_Check);
            }
        }
        break;

    default:
        break;
    }
}
#endif
#endif