RW_NDEF_T1T.c 6.18 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
/*
*         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 T1T_MAGIC_NUMBER    0xE1
#define T1T_NDEF_TLV        0x03

const unsigned char T1T_RID[] = {0x78,0x00,0x00,0x00,0x00,0x00,0x00};
const unsigned char T1T_RALL[] = {0x00,0x00,0x00};
const unsigned char T1T_READ8[] = {0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

typedef enum
{
    Initial,
    Getting_ID,
    Reading_CardContent,
    Reading_NDEF
} RW_NDEF_T1T_state_t;

typedef struct
{
    unsigned char HR0;
    unsigned char HR1;
    unsigned char UID[4];
    unsigned char BlkNb;
    unsigned short MessagePtr;
    unsigned short MessageSize;
    unsigned char *pMessage;
} RW_NDEF_T1T_Ndef_t;

static RW_NDEF_T1T_state_t eRW_NDEF_T1T_State = Initial;
static RW_NDEF_T1T_Ndef_t RW_NDEF_T1T_Ndef;

void RW_NDEF_T1T_Reset(void)
{
    eRW_NDEF_T1T_State = Initial;
    RW_NDEF_T1T_Ndef.pMessage = NdefBuffer;
}

void RW_NDEF_T1T_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_T1T_State)
    {
    case Initial:
        /* Send T1T_RID */
        memcpy (pCmd, T1T_RID, sizeof(T1T_RID));
        *pCmd_size = 7;
        eRW_NDEF_T1T_State = Getting_ID;
        break;

    case Getting_ID:
        /* Is CC Read and Is Ndef ?*/
        if ((Rsp_size == 7) && (pRsp[Rsp_size-1] == 0x00))
        {
            /* Fill File structure */
            RW_NDEF_T1T_Ndef.HR0 = pRsp[0];
            RW_NDEF_T1T_Ndef.HR1 = pRsp[1];
            memcpy (RW_NDEF_T1T_Ndef.UID, &pRsp[2], sizeof(RW_NDEF_T1T_Ndef.UID));
            
            /* Read full card content */
            memcpy (pCmd, T1T_RALL, sizeof(T1T_RALL));
            memcpy (&pCmd[3], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID));
            *pCmd_size = sizeof(T1T_RALL) + sizeof(RW_NDEF_T1T_Ndef.UID);
            eRW_NDEF_T1T_State = Reading_CardContent;
        }
        break;

    case Reading_CardContent:
        /* Is Read success ?*/
        if ((Rsp_size == 123) && (pRsp[Rsp_size-1] == 0x00))
        {
            /* Check CC */ 
            if (pRsp[10] == T1T_MAGIC_NUMBER)
            {
                unsigned char Tmp = 14;
                unsigned char data_size;

                /* If not NDEF Type skip TLV */
                while (pRsp[Tmp] != T1T_NDEF_TLV)
                {
                    Tmp += 2 + pRsp[Tmp+1];
                    if (Tmp > Rsp_size) return;
                }

                RW_NDEF_T1T_Ndef.MessageSize = pRsp[Tmp+1];
                data_size = (Rsp_size - 1) - 16 - Tmp - 2;

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

                /* Is NDEF read already completed ? */
                if(RW_NDEF_T1T_Ndef.MessageSize <= data_size)
                {
                    memcpy(RW_NDEF_T1T_Ndef.pMessage, &pRsp[Tmp+2], RW_NDEF_T1T_Ndef.MessageSize);

                    /* Notify application of the NDEF reception */
                    if(pRW_NDEF_PullCb != NULL) pRW_NDEF_PullCb(RW_NDEF_T1T_Ndef.pMessage, RW_NDEF_T1T_Ndef.MessageSize);
                }
                else
                {
                    RW_NDEF_T1T_Ndef.MessagePtr = data_size;
                    memcpy (RW_NDEF_T1T_Ndef.pMessage, &pRsp[Tmp+2], RW_NDEF_T1T_Ndef.MessagePtr);
                    RW_NDEF_T1T_Ndef.BlkNb = 0x10;

                    /* Read NDEF content */
                    memcpy (pCmd, T1T_READ8, sizeof(T1T_READ8));
                    pCmd[1] = RW_NDEF_T1T_Ndef.BlkNb;
                    memcpy (&pCmd[10], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID));
                    *pCmd_size = sizeof(T1T_READ8) + sizeof(RW_NDEF_T1T_Ndef.UID);

                    eRW_NDEF_T1T_State = Reading_NDEF;
                }
            }
        }
        break;

        case Reading_NDEF:
            /* Is Read success ?*/
            if ((Rsp_size == 10) && (pRsp[Rsp_size-1] == 0x00))
            {
                /* Is NDEF read already completed ? */
                if ((RW_NDEF_T1T_Ndef.MessageSize - RW_NDEF_T1T_Ndef.MessagePtr) < 8)
                {
                    memcpy (&RW_NDEF_T1T_Ndef.pMessage[RW_NDEF_T1T_Ndef.MessagePtr], &pRsp[1], RW_NDEF_T1T_Ndef.MessageSize - RW_NDEF_T1T_Ndef.MessagePtr);

                    /* Notify application of the NDEF reception */
                    if(pRW_NDEF_PullCb != NULL) pRW_NDEF_PullCb(RW_NDEF_T1T_Ndef.pMessage, RW_NDEF_T1T_Ndef.MessageSize);
                }
                else
                {
                    memcpy (&RW_NDEF_T1T_Ndef.pMessage[RW_NDEF_T1T_Ndef.MessagePtr], &pRsp[1], 8);
                    RW_NDEF_T1T_Ndef.MessagePtr += 8;
                    RW_NDEF_T1T_Ndef.BlkNb++;

                    /* Read NDEF content */
                    memcpy (pCmd, T1T_READ8, sizeof(T1T_READ8));
                    pCmd[1] = RW_NDEF_T1T_Ndef.BlkNb;
                    memcpy (&pCmd[10], RW_NDEF_T1T_Ndef.UID, sizeof(RW_NDEF_T1T_Ndef.UID));
                    *pCmd_size = sizeof(T1T_READ8) + sizeof(RW_NDEF_T1T_Ndef.UID);
                }
            }
        break;

    default:
        break;
    }
}
#endif
#endif