Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 NXP Semiconductors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*!
     18 * \file  phFriNfcNdefMap.c
     19 * \brief NFC Ndef Mapping For Different Smart Cards.
     20 *
     21 * Project: NFC-FRI
     22 *
     23 * $Date: Mon Mar 29 17:32:37 2010 $
     24 * $Author: ing02260 $
     25 * $Revision: 1.37 $
     26 * $Aliases: NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $
     27 *
     28 */
     29 
     30 
     31 #include <phFriNfc_NdefMap.h>
     32 
     33 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
     34 #include <phFriNfc_MifareULMap.h>
     35 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
     36 
     37 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
     38 #include <phFriNfc_TopazMap.h>
     39 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
     40 
     41 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
     42 #include <phFriNfc_MifareStdMap.h>
     43 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
     44 
     45 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
     46 #include <phFriNfc_DesfireMap.h>
     47 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
     48 
     49 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
     50 #include <phFriNfc_FelicaMap.h>
     51 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
     52 
     53 #ifdef PHFRINFC_OVRHAL_MOCKUP
     54 #include <phFriNfc_MockupMap.h>
     55 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
     56 
     57 
     58 #include <phFriNfc_OvrHal.h>
     59 
     60 /*! \ingroup grp_file_attributes
     61 *  \name NDEF Mapping
     62 *
     63 * File: \ref phFriNfcNdefMap.c
     64 *
     65 */
     66 /*@{*/
     67 #define PHFRINFCNDEFMAP_FILEREVISION "$Revision: 1.37 $"
     68 #define PHFRINFCNDEFMAP_FILEALIASES  "$Aliases: NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $"
     69 /*@}*/
     70 
     71 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
     72 /* Desfire capability Container Reset Helper */
     73 static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t  *NdefMap);
     74 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
     75 
     76 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
     77 /* Felica Smart Tag Reset Helper */
     78 static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t  *NdefMap);
     79 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
     80 
     81 
     82 /* \note    This function has to be called at the beginning, after creating an
     83 *          instance of \ref phFriNfc_NdefMap_t . Use this function to reset
     84 *          the instance and/or switch to a different underlying device (
     85 *          different NFC device or device mode, or different Remote Device).
     86 */
     87 
     88 NFCSTATUS phFriNfc_NdefMap_Reset(   phFriNfc_NdefMap_t              *NdefMap,
     89                                  void                            *LowerDevice,
     90                                  phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
     91                                  phHal_sDevInputParam_t          *psDevInputParam,
     92                                  uint8_t                         *TrxBuffer,
     93                                  uint16_t                        TrxBufferSize,
     94                                  uint8_t                         *ReceiveBuffer,
     95                                  uint16_t                        *ReceiveLength,
     96                                  uint16_t                        *DataCount)
     97 {
     98     NFCSTATUS   status = NFCSTATUS_SUCCESS;
     99     uint8_t     index;
    100 
    101     if (    (ReceiveLength == NULL) || (NdefMap == NULL) || (psRemoteDevInfo == NULL) ||
    102         (TrxBuffer == NULL) || (TrxBufferSize == 0)  || (LowerDevice == NULL) ||
    103         (*ReceiveLength == 0) || (ReceiveBuffer == NULL) || (DataCount == NULL) ||
    104         (psDevInputParam == NULL) ||
    105         (*ReceiveLength < PH_FRINFC_NDEFMAP_MAX_SEND_RECV_BUF_SIZE ))
    106     {
    107         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    108     }
    109     else
    110     {
    111         /* Initialise the state to Init */
    112         NdefMap->State = PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
    113 
    114         for(index = 0;index<PH_FRINFC_NDEFMAP_CR;index++)
    115         {
    116             /* Initialise the NdefMap Completion Routine to Null */
    117             NdefMap->CompletionRoutine[index].CompletionRoutine = NULL;
    118             /* Initialise the NdefMap Completion Routine context to Null  */
    119             NdefMap->CompletionRoutine[index].Context = NULL;
    120         }
    121 
    122         /* Lower Device(Always Overlapped HAL Struct initialised in application
    123         is registred in NdefMap Lower Device) */
    124         NdefMap->LowerDevice = LowerDevice;
    125 
    126         /* Remote Device info received from Manual Device Discovery is registered here */
    127         NdefMap->psRemoteDevInfo = psRemoteDevInfo;
    128 
    129         /* Transfer Buffer registered */
    130         NdefMap->ApduBuffer = TrxBuffer;
    131 
    132         /* Set the MaxApduBufferSize */
    133         NdefMap->ApduBufferSize = TrxBufferSize;
    134 
    135         /* Set APDU Buffer Index */
    136         NdefMap->ApduBuffIndex = 0;
    137 
    138         /* Register Transfer Buffer Length */
    139         NdefMap->SendLength = 0;
    140 
    141         /* Register Receive Buffer */
    142         NdefMap->SendRecvBuf = ReceiveBuffer;
    143 
    144         /* Register Receive Buffer Length */
    145         NdefMap->SendRecvLength = ReceiveLength;
    146 
    147         /* Register Temporary Receive Buffer Length */
    148         NdefMap->TempReceiveLength = *ReceiveLength;
    149 
    150         /* Register Data Count variable and set it to zero */
    151         NdefMap->DataCount = DataCount;
    152         *NdefMap->DataCount = 0;
    153 
    154         /* Reset the PageOffset */
    155         NdefMap->Offset = 0;
    156 
    157         /* Reset the NumOfBytesRead*/
    158         NdefMap->NumOfBytesRead = 0;
    159 
    160         /* Reset the NumOfBytesWritten*/
    161         NdefMap->NumOfBytesWritten = 0;
    162 
    163         /* Reset the Card Type */
    164         NdefMap->CardType = 0;
    165 
    166         /* Reset the Memory Card Size*/
    167         NdefMap->CardMemSize = 0;
    168 
    169         /* Reset the Previous Operation*/
    170         NdefMap->PrevOperation = 0;
    171 
    172         /* Reset the Desfire Operation Flag*/
    173         NdefMap->DespOpFlag = 0;
    174 
    175         /* Reset MapCompletion Info*/
    176         NdefMap->MapCompletionInfo.CompletionRoutine = NULL;
    177         NdefMap->MapCompletionInfo.Context = NULL;
    178 
    179         /*  Reset the ReadingForWriteOperation flag. */
    180         NdefMap->ReadingForWriteOperation = 0;  /*  FALSE  */
    181 
    182 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
    183         /*Reset Desfire Cap Container elements*/
    184         phFriNfc_DesfCapCont_HReset(NdefMap);
    185 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
    186 
    187 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
    188         /*Reset Mifare Standard Container elements*/
    189         NdefMap->StdMifareContainer.DevInputParam = psDevInputParam;
    190         status = phFriNfc_MifareStdMap_H_Reset(NdefMap);
    191 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
    192 
    193 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
    194         /*Reset Felica Tag elements*/
    195         NdefMap->FelicaPollDetails.DevInputParam = psDevInputParam;
    196         phFriNfc_Felica_HReset(NdefMap);
    197 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
    198 
    199 #if !(defined(PH_FRINFC_MAP_TOPAZ_DISABLED ) || defined (PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED ))
    200 
    201         phFriNfc_TopazMap_H_Reset(NdefMap);
    202 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED || PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED  */
    203 
    204 
    205 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
    206         status = phFriNfc_MifareUL_H_Reset(NdefMap);
    207 #endif
    208 
    209 #ifdef PHFRINFC_OVRHAL_MOCKUP
    210         /*Reset Desfire Cap Container elements*/
    211         phFriNfc_Mockup_H_Reset(NdefMap);
    212 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
    213 
    214         /*
    215         *  Fix for PR - 0001256
    216         *  Date- 08-08-08
    217         */
    218         NdefMap->CardState = PH_NDEFMAP_CARD_STATE_INVALID;
    219     }
    220 
    221     return (status);
    222 }
    223 
    224 /*!
    225 * Registering the Completion Routine.
    226 *
    227 * This function requires the caller to set a Completion Routine
    228 * which serves as notifier for the upper component.
    229 * NOTE: Please refer the header file for more information.
    230 *
    231 */
    232 
    233 NFCSTATUS phFriNfc_NdefMap_SetCompletionRoutine(phFriNfc_NdefMap_t     *NdefMap,
    234                                                 uint8_t                 FunctionID,
    235                                                 pphFriNfc_Cr_t          CompletionRoutine,
    236                                                 void                   *CompletionRoutineContext)
    237 {
    238     NFCSTATUS   status = NFCSTATUS_SUCCESS;
    239 
    240     if ( ( NdefMap == NULL ) || (FunctionID >= PH_FRINFC_NDEFMAP_CR) ||
    241         ( CompletionRoutine == NULL) || (CompletionRoutineContext == NULL))
    242     {
    243         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    244     }
    245     else
    246     {
    247         /* Register the application callback with the NdefMap Completion Routine */
    248         NdefMap->CompletionRoutine[FunctionID].CompletionRoutine = CompletionRoutine;
    249 
    250         /* Register the application context with the NdefMap Completion Routine context */
    251         NdefMap->CompletionRoutine[FunctionID].Context = CompletionRoutineContext;
    252     }
    253 
    254     return status;
    255 }
    256 
    257 /*!
    258 * Initiates Reading of NDEF information from the Remote Device.
    259 *
    260 * Remote Peer device may be of type any card. Ex: desfire,felica,jewel
    261 * mifareUL,mifare 1K etc. The function initiates the reading of NDEF
    262 * information from a Remote Device.
    263 *
    264 * This is the main NdefMap read function call.Internally,depending upon
    265 * the CardType,respective mifare/desfire read functions are called.
    266 * In future this can be extended to support any types of card.
    267 *
    268 * It performs a reset of the state and triggers/starts the raed action (state machine).
    269 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once the action
    270 * has been triggered.
    271 *
    272 * NOTE: Please refer the header file for more information.
    273 *
    274 */
    275 
    276 NFCSTATUS phFriNfc_NdefMap_RdNdef(  phFriNfc_NdefMap_t  *NdefMap,
    277                                   uint8_t             *PacketData,
    278                                   uint32_t            *PacketDataLength,
    279                                   uint8_t             Offset)
    280 {
    281     NFCSTATUS   status = NFCSTATUS_PENDING;
    282 
    283 
    284     /* check for validity of input parameters*/
    285     if (( PacketData == NULL )
    286         || ( NdefMap == NULL )
    287         || ( PacketDataLength == NULL )
    288         || ( *PacketDataLength == 0 )
    289         || ( ( Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
    290         || (NdefMap->CompletionRoutine->CompletionRoutine == NULL)
    291         || (NdefMap->CompletionRoutine->Context == NULL )
    292         )
    293     {
    294         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    295     }
    296     else if (NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID)
    297     {
    298         /*  Card is in invalid state, cannot have any read/write
    299         operations*/
    300         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
    301             NFCSTATUS_INVALID_FORMAT);
    302     }
    303     else if(NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED)
    304     {
    305         /*  Can't read any data from the card:TLV length is zero*/
    306         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_EOF_NDEF_CONTAINER_REACHED);
    307 
    308         NdefMap->NumOfBytesRead = PacketDataLength;
    309         *NdefMap->NumOfBytesRead = 0;
    310 
    311 
    312     }
    313     else if ( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
    314     {
    315         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
    316     }
    317     else
    318     {
    319         /*  Check the offset given by the user
    320         If the offset is 1 (SEEK_BEGIN), reset everything and start
    321         reading from the first Page of the card.
    322         else if offset is 0 (PH_FRINFC_NDEFMAP_SEEK_CUR), continue reading
    323         No need to reset the parameters.  */
    324 
    325         if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
    326         {
    327             NdefMap->ApduBuffIndex = 0;
    328             *NdefMap->DataCount = 0;
    329         }
    330 
    331         if  ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD) &&
    332             (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 ))
    333         {
    334 
    335             /*  A READ operation cannot be done if the previuos operation was WRITE
    336             unless the offset is set to PH_FRINFC_NDEFMAP_SEEK_BEGIN Or
    337             Read Operation with Offset set to Continue & DataCount set to 0 */
    338             status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
    339         }
    340         else
    341         {
    342             switch ( NdefMap->CardType)
    343             {
    344 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
    345                 case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
    346                     /*  Mifare card selected. Call Mifare read */
    347                     status = phFriNfc_MifareUL_RdNdef ( NdefMap,
    348                         PacketData,
    349                         PacketDataLength,
    350                         Offset);
    351                     break;
    352 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
    353 
    354 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
    355                 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
    356                     /*  Desfire card selected. Call Desfire read */
    357                     status = phFriNfc_Desfire_RdNdef(   NdefMap,
    358                         PacketData,
    359                         PacketDataLength,
    360                         Offset);
    361                     break;
    362 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
    363 
    364 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
    365                 case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
    366                 case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
    367                     /*  Mifare card selected. Call Mifare read */
    368                     status = phFriNfc_MifareStdMap_RdNdef ( NdefMap,
    369                         PacketData,
    370                         PacketDataLength,
    371                         Offset);
    372                     break;
    373 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
    374 
    375 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
    376                 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
    377                     /*  Desfire card selected. Call Desfire Write */
    378                     status =  phFriNfc_Felica_RdNdef(  NdefMap,
    379                         PacketData,
    380                         PacketDataLength,
    381                         Offset);
    382                     break;
    383 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
    384 
    385 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
    386                 case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
    387                     /*  Topaz card selected. Call Topaz read */
    388                     status =  phFriNfc_TopazMap_RdNdef(  NdefMap,
    389                         PacketData,
    390                         PacketDataLength,
    391                         Offset);
    392                     break;
    393 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
    394                 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
    395                     /*  Topaz card selected. Call Topaz read */
    396                     status =  phFriNfc_TopazDynamicMap_RdNdef(  NdefMap,
    397                         PacketData,
    398                         PacketDataLength,
    399                         Offset);
    400                     break;
    401 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
    402 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
    403 
    404 #ifdef PHFRINFC_OVRHAL_MOCKUP
    405                 case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
    406                     /*  Mockup card selected. Call Mockup Write */
    407                     status =  phFriNfc_Mockup_RdNdef(  NdefMap,
    408                         PacketData,
    409                         PacketDataLength,
    410                         Offset);
    411                     break;
    412 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
    413 
    414                 default :
    415                     /*  Unknown card type. Return error */
    416                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
    417                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    418 
    419                     break;
    420             }
    421         }
    422     }
    423     return status;
    424 }
    425 
    426 /*!
    427 * Initiates Writing of NDEF information to the Remote Device.
    428 *
    429 * The function initiates the writing of NDEF information to a Remote Device
    430 *
    431 * Remote Peer device may be of type any card. Ex: desfire,felica,jewel
    432 * mifareUL,mifare 1K etc. The function initiates the reading of NDEF
    433 * information from a Remote Device.
    434 *
    435 * This is a main write api.Internally,depending upon the CardType,
    436 * respective mifare/desfire write apis are called.In future this can be
    437 * extended to support any types of card.
    438 *
    439 * It performs a reset of the state and starts the action (state machine).
    440 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once
    441 * the action has been triggered.
    442 *
    443 * NOTE: Please refer the header file for more information.
    444 *
    445 */
    446 
    447 
    448 NFCSTATUS phFriNfc_NdefMap_WrNdef(  phFriNfc_NdefMap_t  *NdefMap,
    449                                   uint8_t             *PacketData,
    450                                   uint32_t            *PacketDataLength,
    451                                   uint8_t             Offset)
    452 {
    453     NFCSTATUS   status = NFCSTATUS_PENDING;
    454     uint8_t     StatusChk=0;
    455 
    456      if (     (PacketData          == NULL)
    457         ||  ( NdefMap             == NULL )
    458         /* + Mantis 442 */
    459         || ( PacketDataLength    == NULL )
    460         /* - Mantis 442 */
    461         || ( *PacketDataLength   == 0 )
    462         || ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN ))
    463         || (NdefMap->CompletionRoutine->CompletionRoutine == NULL)
    464         || (NdefMap->CompletionRoutine->Context == NULL)
    465         )
    466     {
    467         /*  Invalid input parameter error   */
    468         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
    469     }
    470     else if (( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) &&
    471         (PH_FRINFC_NDEFMAP_TOPAZ_CARD != NdefMap->CardType) &&
    472         (PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD != NdefMap->CardType))
    473     {
    474         /*  Card is in invalid state, cannot have any read/write
    475         operations*/
    476         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
    477             NFCSTATUS_INVALID_FORMAT);
    478     }
    479 
    480     else if ( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY )
    481 
    482     {
    483         /*Can't write to the card :No Grants */
    484         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
    485             NFCSTATUS_NOT_ALLOWED);
    486 
    487         /* set the no. bytes written is zero*/
    488         NdefMap->WrNdefPacketLength = PacketDataLength;
    489         *NdefMap->WrNdefPacketLength = 0;
    490 
    491     }
    492     else
    493     {
    494         /*  Check the offset given by the user
    495         If the offset is 1 (SEEK_BEGIN), reset everything and start
    496         writing from the first Byte of the card.
    497         else if offset is 0 (PH_FRINFC_NDEFMAP_SEEK_CUR), continue writing
    498         No need to reset the parameters.  */
    499          if (( NdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) &&
    500             (PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD == NdefMap->CardType))
    501         {
    502             /* If Topaz Dynamic card CC bytes are not valid then also allow writing,
    503             If card is really good then writing will be done properly and reading can be performed,
    504             otherwise writing or reading will fail. so, setting card state to
    505             NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE */
    506             NdefMap->CardState = PH_NDEFMAP_CARD_STATE_READ_WRITE;
    507         }
    508 
    509         if ( Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN )
    510         {
    511             NdefMap->ApduBuffIndex = 0;
    512             *NdefMap->DataCount = 0;
    513         }
    514 
    515         if ( (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_UL_CARD) ||
    516             (NdefMap->CardType == PH_FRINFC_NDEFMAP_ISO14443_4A_CARD))
    517         {
    518             if (( (NdefMap->PrevOperation == PH_FRINFC_NDEFMAP_READ_OPE) && (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN )) ||
    519                 ( (Offset == PH_FRINFC_NDEFMAP_SEEK_CUR) && (*NdefMap->DataCount == 0 )))
    520             {
    521                 /*  A WRITE operation cannot be done if the previuos operation was READ
    522                 unless the offset is set to PH_FRINFC_NDEFMAP_SEEK_BEGIN OR
    523                 Write Operation with Offset set to Continue & DataCount set to 0  */
    524                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
    525                 StatusChk = 1;
    526             }
    527         }
    528         if(StatusChk != 1)
    529         {
    530             NdefMap->WrNdefPacketLength =   PacketDataLength;
    531             switch ( NdefMap->CardType)
    532             {
    533 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
    534                 case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
    535                     /*  Mifare card selected. Call Mifare Write */
    536                     status =  phFriNfc_MifareUL_WrNdef( NdefMap,
    537                         PacketData,
    538                         PacketDataLength,
    539                         Offset);
    540                     break;
    541 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
    542 
    543 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
    544                 case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
    545                     /*  Desfire card selected. Call Desfire Write */
    546                     status =  phFriNfc_Desfire_WrNdef(  NdefMap,
    547                         PacketData,
    548                         PacketDataLength,
    549                         Offset);
    550                     break;
    551 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
    552 
    553 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
    554                 case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
    555                 case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
    556                     /*  Mifare card selected. Call Mifare read */
    557                     status = phFriNfc_MifareStdMap_WrNdef ( NdefMap,
    558                         PacketData,
    559                         PacketDataLength,
    560                         Offset);
    561                     break;
    562 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
    563 
    564 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
    565                 case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
    566                     /*  Desfire card selected. Call Desfire Write */
    567                     status =  phFriNfc_Felica_WrNdef(  NdefMap,
    568                         PacketData,
    569                         PacketDataLength,
    570                         Offset);
    571                     break;
    572 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
    573 
    574 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
    575                 case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
    576                     /*  Topaz card selected. Call Topaz Write */
    577                     status =  phFriNfc_TopazMap_WrNdef(  NdefMap,
    578                         PacketData,
    579                         PacketDataLength,
    580                         Offset);
    581                     break;
    582 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
    583                 case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
    584                     /*  Topaz card selected. Call Topaz Write */
    585                     status =  phFriNfc_TopazDynamicMap_WrNdef(  NdefMap,
    586                         PacketData,
    587                         PacketDataLength,
    588                         Offset);
    589                     break;
    590 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
    591 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
    592 
    593 
    594 #ifdef PHFRINFC_OVRHAL_MOCKUP
    595                 case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
    596                     /*  Mockup card selected. Call Mockup Write */
    597                     status =  phFriNfc_Mockup_WrNdef(  NdefMap,
    598                         PacketData,
    599                         PacketDataLength,
    600                         Offset);
    601                     break;
    602 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
    603                 default :
    604                     /*  Unknown card type. Return error */
    605                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    606                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    607                     break;
    608             }
    609         }
    610     }
    611     return status;
    612 }
    613 
    614 /*!
    615 * Check whether a particular Remote Device is NDEF compliant.
    616 *
    617 * The function initiates the ndef compliancy check.
    618 *
    619 * This is a main check ndef api.Internally,depending upon the different
    620 * opmodes,respective mifare/desfire checkNdef apis are called.
    621 * In future this can be extended to check any types of card ndef
    622 * compliancy.
    623 *
    624 * It performs a reset of the state and starts the action (state machine).
    625 * A periodic call of the \ref phFriNfcNdefMap_Process has to be done once
    626 * the action has been triggered.
    627 *
    628 * NOTE: Please refer the header file for more information.
    629 *
    630 */
    631 
    632 NFCSTATUS phFriNfc_NdefMap_ChkNdef( phFriNfc_NdefMap_t     *NdefMap)
    633 {
    634     NFCSTATUS   status = NFCSTATUS_PENDING;
    635     uint8_t     sak;
    636 
    637 
    638     /*  Check for ndefmap context and relevant state. Else return error*/
    639     if ( NdefMap == NULL )
    640     {
    641         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    642     }
    643     else
    644     {
    645         if( (NdefMap->State !=  PH_FRINFC_NDEFMAP_STATE_RESET_INIT) ||
    646             (NdefMap->psRemoteDevInfo->SessionOpened != 0x01 ) )
    647             /*  Harsha: If SessionOpened is not 1, this means that connect has not happened */
    648         {
    649             status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_DEVICE_REQUEST);
    650         }
    651         else if ( (NdefMap->CompletionRoutine->CompletionRoutine == NULL) || (NdefMap->CompletionRoutine->Context == NULL ))
    652         {
    653             status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
    654         }
    655         else
    656         {
    657             /*
    658             * 1.Changed
    659             *   CardInfo106 Replace with the ReaderA_Info.
    660             */
    661 
    662             sak = NdefMap->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak;
    663 
    664             /*
    665             * 3. Changed
    666             *    Description: Opmode replace with RemDevType.
    667             */
    668 
    669 
    670             switch ( NdefMap->psRemoteDevInfo->RemDevType )
    671             {
    672             case phHal_eMifare_PICC:
    673             case phHal_eISO14443_3A_PICC:
    674                 /*  Remote device is Mifare card . Check for Mifare
    675                 NDEF compliancy */
    676                 if(0x00 == sak)
    677                 {
    678                     /*  The SAK/Sel_Res says the card is of the type
    679                     Mifare UL */
    680 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
    681                     status = phFriNfc_MifareUL_ChkNdef( NdefMap);
    682 #else   /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
    683                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    684                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    685 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
    686                 }
    687                 else if ((0x08 == (sak & 0x18)) ||
    688                         (0x18 == (sak & 0x18)))
    689                 {
    690                     /*  The SAK/Sel_Res says the card is of the type
    691                     Mifare Standard */
    692 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
    693                     status = phFriNfc_MifareStdMap_ChkNdef( NdefMap);
    694 #else   /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
    695                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    696                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    697 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
    698                 }
    699                 else
    700                 {
    701                     /*  Invalid Mifare UL card, as the remote device
    702                     info - opmode says its a Mifare UL card but,
    703                     The SAK/Sel_Res is wrong */
    704                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    705                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    706                 }
    707                 break;
    708             case phHal_eISO14443_B_PICC:
    709                 {
    710                     status = phFriNfc_Desfire_ChkNdef(NdefMap);
    711                 }
    712                 break;
    713             case  phHal_eISO14443_A_PICC :
    714                 /*  Remote device is Desfire card . Check for Desfire
    715                 NDEF compliancy */
    716                 if(0x20 == (sak & 0x20))
    717                 {
    718                     /*  The SAK/Sel_Res says the card is of the type
    719                     ISO14443_4A */
    720 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
    721                     status = phFriNfc_Desfire_ChkNdef(NdefMap);
    722 #else   /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
    723                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    724                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    725 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
    726                 }
    727                 else
    728                 {
    729                     /*  Invalid Desfire card, as the remote device
    730                     info - opmode says its a desfire card but,
    731                     The SAK/Sel_Res is wrong */
    732                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    733                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    734                 }
    735                 break;
    736 
    737             case phHal_eFelica_PICC:
    738 
    739                 /*Set the OpMode Type Flag*/
    740 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
    741 #ifndef PH_HAL4_ENABLE
    742                 NdefMap->OpModeType[0] = phHal_eOpModesFelica212;
    743                 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
    744 #endif /* #ifndef PH_HAL4_ENABLE */
    745                 status = phFriNfc_Felica_ChkNdef(NdefMap);
    746 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
    747                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    748                     NFCSTATUS_INVALID_REMOTE_DEVICE);
    749 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
    750 
    751                 break;
    752 
    753 #ifndef PH_HAL4_ENABLE
    754 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
    755             case phHal_eFelica424:
    756                 /*Set the OpMode Ttype Flag*/
    757                 NdefMap->OpModeType[0] = phHal_eOpModesFelica424;
    758                 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
    759                 status = phFriNfc_Felica_ChkNdef(NdefMap);
    760 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
    761                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    762                     NFCSTATUS_INVALID_REMOTE_DEVICE);
    763 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
    764                 break;
    765 #endif
    766 
    767             case phHal_eJewel_PICC :
    768                 /*  Remote device is Topaz card . Check for Topaz
    769                 NDEF compliancy */
    770 #ifdef PH_HAL4_ENABLE
    771 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
    772             /* Decide on the Header bytes to know the
    773                    Type of the Topaz card.Card could be Static or
    774                    Dynamic type. These are of type NFFC-NDEF Data Application*/
    775                 if ( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
    776                                 == PH_FRINFC_TOPAZ_HEADROM0_VAL)
    777                 {
    778 
    779                         status = phFriNfc_TopazMap_ChkNdef(NdefMap);
    780                 }
    781 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
    782                 else if( NdefMap->psRemoteDevInfo->RemoteDevInfo.Jewel_Info.HeaderRom0
    783                                 == PH_FRINFC_TOPAZ_DYNAMIC_HEADROM0_VAL)
    784                 {
    785 
    786                     status = phFriNfc_TopazDynamicMap_ChkNdef(NdefMap);
    787                 }
    788 #endif
    789                 else
    790                 {
    791 
    792                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    793                            NFCSTATUS_INVALID_REMOTE_DEVICE);
    794 
    795                 }
    796 #endif
    797 
    798 
    799 #else
    800                 if(0xC2 == sak)
    801                 {
    802                     /*  The SAK/Sel_Res says the card is of the type
    803                     ISO14443_4A */
    804 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
    805                     status = phFriNfc_TopazMap_ChkNdef(NdefMap);
    806 #else   /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
    807                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    808                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    809 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED*/
    810                 }
    811                 else
    812                 {
    813                     /*  Invalid Topaz card, as the remote device
    814                     info - opmode says its a desfire card but,
    815                     The SAK/Sel_Res is wrong */
    816                     status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    817                         NFCSTATUS_INVALID_REMOTE_DEVICE);
    818                 }
    819 #endif
    820                 break;
    821 
    822 #ifdef PHFRINFC_OVRHAL_MOCKUP
    823             case phHal_eOpModesMockup :
    824                 /*Set the OpMode Ttype Flag*/
    825                 NdefMap->OpModeType[0] = phHal_eOpModesMockup;
    826                 NdefMap->OpModeType[1] = phHal_eOpModesArrayTerminator;
    827                 status = phFriNfc_Mockup_ChkNdef(NdefMap);
    828                 break;
    829 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
    830 
    831             default :
    832                 /*  Remote device is not recognised.
    833                 Probably not NDEF compliant */
    834                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    835                     NFCSTATUS_INVALID_REMOTE_DEVICE);
    836                 break;
    837             }
    838         }
    839     }
    840     return status;
    841 }
    842 
    843 
    844 /*!
    845 * \brief Completion Routine, Processing function, needed to avoid long blocking.
    846 * \note The lower (Overlapped HAL) layer must register a pointer to this function as a Completion
    847 *       Routine in order to be able to notify the component that an I/O has finished and data are
    848 *       ready to be processed.
    849 * This is a main Ndef Map Process api.Internally,depending upon the different
    850 * CardTypes,respective mifare/desfire process functions are called.
    851 *
    852 */
    853 
    854 void phFriNfc_NdefMap_Process(  void        *Context,
    855                               NFCSTATUS   Status)
    856 {
    857 
    858     if ( Context != NULL )
    859     {
    860         phFriNfc_NdefMap_t  *NdefMap = (phFriNfc_NdefMap_t *)Context;
    861         /*
    862         * 4 Changed
    863         *   Description: Opmode replace with RevDevTyp.
    864         */
    865 
    866         switch ( NdefMap->psRemoteDevInfo->RemDevType )
    867         {
    868         case  phHal_eMifare_PICC :
    869         case phHal_eISO14443_3A_PICC:
    870 
    871             if((NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD) ||
    872                 (NdefMap->CardType == PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD))
    873             {
    874 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
    875                 /*  Remote device is Mifare Standard card */
    876                 phFriNfc_MifareStdMap_Process(NdefMap,Status);
    877 #else   /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
    878                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    879                     NFCSTATUS_INVALID_REMOTE_DEVICE);
    880 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED*/
    881             }
    882             else
    883             {
    884 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
    885                 /*  Remote device is Mifare UL card */
    886                 phFriNfc_MifareUL_Process(NdefMap,Status);
    887 #else   /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
    888                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    889                     NFCSTATUS_INVALID_REMOTE_DEVICE);
    890 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED*/
    891             }
    892             break;
    893 
    894         case phHal_eISO14443_A_PICC :
    895 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
    896             /*  Remote device is Desfire card */
    897             phFriNfc_Desfire_Process(NdefMap, Status);
    898 #else   /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
    899             Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    900                 NFCSTATUS_INVALID_REMOTE_DEVICE);
    901 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED*/
    902             break;
    903         case phHal_eISO14443_B_PICC:
    904             /*  Remote device is Desfire card */
    905             phFriNfc_Desfire_Process(NdefMap, Status);
    906             break;
    907 
    908         case phHal_eFelica_PICC :
    909 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
    910             /*  Remote device is Felica Smart card */
    911             phFriNfc_Felica_Process(NdefMap, Status);
    912 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
    913             Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    914                 NFCSTATUS_INVALID_REMOTE_DEVICE);
    915 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
    916             break;
    917 
    918         case phHal_eJewel_PICC:
    919 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
    920             if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_CARD )
    921             {
    922                 /*  Remote device is Topaz Smart card */
    923                 phFriNfc_TopazMap_Process(NdefMap, Status);
    924             }
    925 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
    926             else if ( NdefMap->CardType == PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD )
    927             {
    928                 /*  Remote device is Topaz Smart card */
    929                 phFriNfc_TopazDynamicMap_Process(NdefMap, Status);
    930             }
    931             else
    932             {
    933                 Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    934                 NFCSTATUS_INVALID_REMOTE_DEVICE);
    935 
    936             }
    937             break;
    938 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED*/
    939 #endif
    940 
    941 
    942 #ifdef PHFRINFC_OVRHAL_MOCKUP
    943         case phHal_eOpModesMockup:
    944             /*  Remote device is Desfire card */
    945             phFriNfc_Mockup_Process(NdefMap, Status);
    946             break;
    947 #endif  /* PHFRINFC_OVRHAL_MOCKUP*/
    948         default :
    949             /*  Remote device opmode not recognised.
    950             Probably not NDEF compliant */
    951             Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    952                 NFCSTATUS_INVALID_REMOTE_DEVICE);
    953             /* set the state back to the Reset_Init state*/
    954             NdefMap->State =  PH_FRINFC_NDEFMAP_STATE_RESET_INIT;
    955 
    956             /* set the completion routine*/
    957             NdefMap->CompletionRoutine[PH_FRINFC_NDEFMAP_CR_INVALID_OPE].
    958                 CompletionRoutine(NdefMap->CompletionRoutine->Context, Status);
    959             break;
    960         }
    961     }
    962     else
    963     {
    964         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
    965             NFCSTATUS_INVALID_PARAMETER);
    966         /* The control should not come here. As Context itself is NULL ,
    967         Can't call the CR*/
    968     }
    969 }
    970 
    971 #if 0
    972 
    973 NFCSTATUS phFriNfc_ChkAndParseTLV(phFriNfc_NdefMap_t    *NdefMap)
    974 {
    975     NFCSTATUS status = NFCSTATUS_PENDING;
    976 
    977     switch ( NdefMap->CardType )
    978     {
    979 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
    980             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
    981 
    982 
    983                 break;
    984 #endif /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
    985 
    986 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
    987             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
    988                 status = phFriNfc_Desf_ChkAndParseTLV(NdefMap);
    989                 return (status);
    990 
    991                 break;
    992 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
    993 
    994 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
    995             case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
    996             case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
    997 
    998                 break;
    999 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
   1000 
   1001 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
   1002             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
   1003                 ;
   1004                 break;
   1005 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
   1006 
   1007             default :
   1008                 /*  Unknown card type. Return error */
   1009                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
   1010                     NFCSTATUS_INVALID_REMOTE_DEVICE);
   1011 
   1012                 break;
   1013     }
   1014 
   1015     return ( status);
   1016 }
   1017 #endif
   1018 
   1019 
   1020 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
   1021 static void phFriNfc_DesfCapCont_HReset(phFriNfc_NdefMap_t *NdefMap)
   1022 {
   1023     /* Initialise/reset the desfire capability contatiner structure variables*/
   1024     NdefMap->DesfireCapContainer.DesfVersion = 0;
   1025     NdefMap->DesfireCapContainer.NdefMsgFid  = 0;
   1026     NdefMap->DesfireCapContainer.NdefFileSize = 0;
   1027     NdefMap->DesfireCapContainer.MaxCmdSize  = 0;
   1028     NdefMap->DesfireCapContainer.MaxRespSize = 0;
   1029     NdefMap->DesfireCapContainer.ReadAccess  = 0;
   1030     NdefMap->DesfireCapContainer.WriteAccess = 0;
   1031 }
   1032 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
   1033 
   1034 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
   1035 static void phFriNfc_Felica_HReset(phFriNfc_NdefMap_t *NdefMap)
   1036 {
   1037     uint8_t index=0;
   1038 
   1039     /* Initialise/reset the different felica structure variables*/
   1040     /* Reset all the felica Basic staruture variables*/
   1041     NdefMap->Felica.CurBlockNo = 0;
   1042 
   1043     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_BLOCK_SIZE;index++)
   1044     {
   1045         NdefMap->Felica.Rd_BytesToCopyBuff[index] = 0;
   1046         NdefMap->Felica.Wr_RemainedBytesBuff[index] = 0;
   1047     }
   1048     NdefMap->Felica.Rd_NoBytesToCopy = 0;
   1049     NdefMap->Felica.Wr_BytesRemained = 0;
   1050 
   1051 
   1052     /* Reset all the felica attribute information staruture variables*/
   1053     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_ATTR_NDEF_DATA_LEN;index++)
   1054     {
   1055         NdefMap->FelicaAttrInfo.LenBytes[index] = 0;
   1056     }
   1057 
   1058     NdefMap->FelicaAttrInfo.Nmaxb = 0;
   1059     NdefMap->FelicaAttrInfo.Nbr = 0;
   1060     NdefMap->FelicaAttrInfo.Nbw= 0;
   1061     NdefMap->FelicaAttrInfo.RdWrFlag = 0;
   1062     NdefMap->FelicaAttrInfo.WriteFlag = 0;
   1063     NdefMap->Felica.CurrBytesRead=0;
   1064 
   1065     /* Reset all the felica manufacture details staruture variables*/
   1066     for(index = 0;index<PH_FRINFC_NDEFMAP_FELICA_MANUF_ID_DATA_LEN;index++)
   1067     {
   1068         NdefMap->FelicaManufDetails.ManufID[index] = 0;
   1069         NdefMap->FelicaManufDetails.ManufParameter[index] = 0;
   1070     }
   1071     NdefMap->Felica.NoBlocksWritten=0;
   1072 }
   1073 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
   1074 
   1075 NFCSTATUS phFriNfc_NdefMap_EraseNdef(phFriNfc_NdefMap_t *NdefMap)
   1076 {
   1077     NFCSTATUS   status = NFCSTATUS_PENDING;
   1078 
   1079     static uint8_t     PktData[3] = PH_FRINFC_NDEFMAP_EMPTY_NDEF_MSG;
   1080     uint8_t     MemOffset = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
   1081     uint32_t    PacketDataLength = sizeof(PktData);
   1082 
   1083     if (NdefMap == NULL )
   1084     {
   1085         /*  Invalid input parameter error   */
   1086         status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
   1087     }
   1088     else
   1089     {
   1090         switch ( NdefMap->CardType)
   1091         {
   1092 #ifdef PHFRINFC_OVRHAL_MOCKUP
   1093             case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
   1094 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
   1095             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
   1096             case  PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
   1097             case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
   1098             case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
   1099 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
   1100             case  PH_FRINFC_NDEFMAP_TOPAZ_CARD :
   1101 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
   1102             case  PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
   1103 #endif
   1104 #endif
   1105                 /*  Mifare card selected. Call Mifare Write */
   1106                 status =  phFriNfc_NdefMap_WrNdef( NdefMap,
   1107                     PktData,
   1108                     &PacketDataLength,
   1109                     MemOffset);
   1110                 break;
   1111 
   1112             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
   1113 
   1114 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
   1115                 /*  Felica card selected. Call to write EMPTY NDEF Msg */
   1116                 status =  phFriNfc_Felica_EraseNdef( NdefMap );
   1117 #else   /* PH_FRINFC_MAP_FELICA_DISABLED*/
   1118                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
   1119                     NFCSTATUS_INVALID_REMOTE_DEVICE);
   1120 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED*/
   1121 
   1122                 break;
   1123             default :
   1124                 /*  Unknown card type. Return error */
   1125                 status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
   1126                     NFCSTATUS_INVALID_REMOTE_DEVICE);
   1127                 break;
   1128         }
   1129     }
   1130     return status;
   1131 }
   1132 /*  Harsha: Fix for the mantis entry 0000420: NDEF_MAP: Size of NDEF data:
   1133 no abstracted way for user to know how many bytes to read/write  */
   1134 
   1135 /*!
   1136 * \brief Helper API, exposed to the user to enable him to know the size
   1137 *        of the NDEF data that he can write in to the card.
   1138 */
   1139 NFCSTATUS phFriNfc_NdefMap_GetContainerSize(const phFriNfc_NdefMap_t *NdefMap,uint32_t *maxSize, uint32_t *actualSize)
   1140 {
   1141     NFCSTATUS   result = NFCSTATUS_SUCCESS;
   1142     uint8_t     sect_index = 0;
   1143     uint8_t     actual_sect_index = 0;
   1144     uint8_t     count_index = 0;
   1145 
   1146     if( (NdefMap == NULL) || (maxSize == NULL) || (actualSize == NULL))
   1147     {
   1148         /*  Invalid input parameter error   */
   1149         result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,NFCSTATUS_INVALID_PARAMETER);
   1150     }
   1151     else
   1152     {
   1153         /*  Which card ? */
   1154         switch(NdefMap->CardType)
   1155         {
   1156 #ifndef PH_FRINFC_MAP_MIFAREUL_DISABLED
   1157             case  PH_FRINFC_NDEFMAP_MIFARE_UL_CARD :
   1158                 /*  Mifare UL card */
   1159                 /*  The integration needs to ensure that the checkNdef
   1160                 function has been called before calling this function,
   1161                 otherwise NdefMap->CardMemSize will be 0 */
   1162                 *maxSize = NdefMap->MifareULContainer.RemainingSize;
   1163                 /* In Mifare UL card, the actual size is the length field
   1164                 value of the TLV */
   1165                 *actualSize = NdefMap->TLVStruct.ActualSize;
   1166                 break;
   1167 #endif  /* PH_FRINFC_MAP_MIFAREUL_DISABLED */
   1168 
   1169 #ifndef PH_FRINFC_MAP_DESFIRE_DISABLED
   1170             case PH_FRINFC_NDEFMAP_ISO14443_4A_CARD :
   1171                 /*  Desfire card */
   1172                 /*  The integration needs to ensure that the checkNdef
   1173                 function has been called before calling this function,
   1174                 otherwise NdefMap->DesfireCapContainer.NdefFileSize
   1175                 will be 0 */
   1176                 /* -2 bytes represents the size field bytes*/
   1177                 *maxSize = NdefMap->DesfireCapContainer.NdefFileSize - 2;
   1178                 /* In Desfire card, the actual size cant be calculated so
   1179                 the actual size is given as 0xFFFFFFFF */
   1180                 *actualSize = NdefMap->DesfireCapContainer.NdefDataLen;
   1181                 break;
   1182 #endif  /* PH_FRINFC_MAP_DESFIRE_DISABLED */
   1183 
   1184 #ifndef PH_FRINFC_MAP_TOPAZ_DISABLED
   1185             case PH_FRINFC_NDEFMAP_TOPAZ_CARD :
   1186                 /*  Topaz card */
   1187                 /*  The integration needs to ensure that the checkNdef
   1188                 function has been called before calling this function,
   1189                 otherwise NdefMap->CardMemSize will be 0 */
   1190                 *maxSize = NdefMap->TopazContainer.RemainingSize;
   1191                 /* In Topaz card, the actual size is the length field value of the
   1192                 TLV */
   1193                 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
   1194                 break;
   1195 #ifndef PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED
   1196             case PH_FRINFC_NDEFMAP_TOPAZ_DYNAMIC_CARD :
   1197                 /*  Topaz 512 card */
   1198                 /*  The integration needs to ensure that the checkNdef
   1199                 function has been called before calling this function,
   1200                 otherwise NdefMap->CardMemSize will be 0 */
   1201                 *maxSize = NdefMap->TopazContainer.NDEFRWSize;
   1202                 /* In Topaz card, the actual size is the length field value of the
   1203                 TLV */
   1204                 *actualSize = NdefMap->TopazContainer.ActualNDEFMsgSize;
   1205                 break;
   1206 
   1207 #endif  /* PH_FRINFC_MAP_TOPAZ_DISABLED */
   1208 #endif  /* PH_FRINFC_MAP_TOPAZ_DYNAMIC_DISABLED */
   1209 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
   1210             case  PH_FRINFC_NDEFMAP_MIFARE_STD_1K_CARD :
   1211             case  PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD :
   1212                 /*  Mifare std card */
   1213 
   1214                 /*  Max size is the number of NDEF compliant blocks in the card
   1215                 multiplied by 16 bytes */
   1216 #ifndef PH_HAL4_ENABLE
   1217 
   1218                 *maxSize = NdefMap->StdMifareContainer.remainingSize;
   1219 
   1220 #else /* #ifndef PH_HAL4_ENABLE */
   1221 
   1222                 while ((PH_FRINFC_MIFARESTD_NDEF_COMP ==
   1223                         NdefMap->StdMifareContainer.aid[count_index]) &&
   1224                         (count_index <
   1225                         PH_FRINFC_NDEFMAP_MIFARESTD_TOTALNO_BLK))
   1226                 {
   1227                     actual_sect_index++;
   1228                     count_index++;
   1229                 }
   1230                 /* Total number of sectors in 1k = 16 (0 to 15, MAD sector number = 0)
   1231                     Total number of sectors in 4k = 40 (0 to 39,
   1232                         MAD sector number = 0 and 16, After block number 31, each sector
   1233                         has 16 blocks)
   1234                     Every last block of the sector is the sector header, So the blocks
   1235                     that can be read or written in each sector is always
   1236                         (number of blocks in each sector - 1)
   1237                     No of blocks in the one sector till the sector number 0 to 31
   1238                         (total 32 sectors) =
   1239                         4 blocks, So blocks that can be read/write = (4 - 1 = 3 blocks)
   1240                     No of blocks in the one sector after the sector number 31 to 39 =
   1241                         16 blocks, So blocks that can be read/write = (16 - 1 = 15 blocks)
   1242                     Each block has 16 bytes
   1243                     To calculate number of bytes in the sector, depending on the number
   1244                     of blocks multiplied by 16
   1245                 */
   1246                 if (PH_FRINFC_NDEFMAP_MIFARE_STD_4K_CARD ==
   1247                     NdefMap->CardType)
   1248                 {
   1249                     if (actual_sect_index > 32)
   1250                     {
   1251                         sect_index = (actual_sect_index - 32);
   1252                         /* Here, 30 is used because block number 0 and 16 are MAD blocks
   1253                         which cannot be used for reading and writing data
   1254                         3 and 15 are blocks in each sector which can be read/write
   1255                         3 indicates the sector is in between (0 and 31)
   1256                         15 indicates the sector is in between (32 to 39)
   1257                         16 is always number of bytes in each block
   1258                         4 is used because each NDEF write has to write using the
   1259                             TLV format and T & L takes 4 bytes length and V is the
   1260                             input data
   1261                         */
   1262                         *maxSize = (((30 * (16 * 3)) + (sect_index * (16 * 15))) - 4);
   1263                     }
   1264                     else if (actual_sect_index <= 16)
   1265                     {
   1266                         *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
   1267                     }
   1268                     else
   1269                     {
   1270                         *maxSize = (((actual_sect_index - 2)  * (16 * 3)) - 4);
   1271                     }
   1272                 }
   1273                 else
   1274                 {
   1275                     /* Here, 16 is always number of bytes in each block
   1276                         3 indicates the sector is in between (0 and 31) */
   1277                     if (actual_sect_index > NdefMap->StdMifareContainer.SectorIndex)
   1278                     {
   1279                         actual_sect_index = NdefMap->StdMifareContainer.SectorIndex;
   1280                     }
   1281                     *maxSize = (((actual_sect_index - 1) * (16 * 3)) - 4);
   1282                 }
   1283 
   1284 #endif /* #ifndef PH_HAL4_ENABLE */
   1285                 *actualSize = NdefMap->TLVStruct.BytesRemainLinTLV;
   1286 
   1287                 break;
   1288 #endif  /* PH_FRINFC_MAP_MIFARESTD_DISABLED */
   1289 
   1290 #ifndef PH_FRINFC_MAP_FELICA_DISABLED
   1291             case PH_FRINFC_NDEFMAP_FELICA_SMART_CARD :
   1292                 /*  Felica card */
   1293 
   1294                 *maxSize = NdefMap->FelicaAttrInfo.Nmaxb * 0x10;
   1295 
   1296                 /* In Felica Card, actual size is calculated using the Length Bytes */
   1297                 *actualSize = NdefMap->FelicaAttrInfo.LenBytes[0];
   1298                 *actualSize = *actualSize << 16;
   1299                 *actualSize += NdefMap->FelicaAttrInfo.LenBytes[1];
   1300                 *actualSize = *actualSize << 8;
   1301                 *actualSize += NdefMap->FelicaAttrInfo.LenBytes[2];
   1302                 break;
   1303 #endif  /* PH_FRINFC_MAP_FELICA_DISABLED */
   1304 
   1305 #ifdef PHFRINFC_OVRHAL_MOCKUP
   1306             case PH_FRINFC_NDEFMAP_MOCKUP_CARD :
   1307                 *maxSize = 0xFFFFFFFF;
   1308                 /* In Desfire card, the actual size cant be calculated so
   1309                 the actual size is given as 0xFFFFFFFF */
   1310                 *actualSize = 0xFFFFFFFF;
   1311                 break;
   1312 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
   1313 
   1314             default :
   1315                 /*  Unknown card type. Return error */
   1316                 result = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,\
   1317                     NFCSTATUS_INVALID_REMOTE_DEVICE);
   1318                 break;
   1319         }
   1320     }
   1321     return(result);
   1322 }
   1323 
   1324 #ifdef PHFRINFC_OVRHAL_MOCKUP
   1325 NFCSTATUS phFriNfc_NdefMap_MockupCardSetter(phFriNfc_NdefMap_t *NdefMap,
   1326                                             uint8_t *NdefData,
   1327                                             uint32_t NdefActualSize,
   1328                                             uint32_t NdefMaxSize,
   1329                                             uint32_t CardSize)
   1330 {
   1331     NFCSTATUS Status = NFCSTATUS_SUCCESS;
   1332     // First check all parameters
   1333     if((NdefData != NULL) && (NdefMap != NULL))
   1334     {
   1335         // OK we can set
   1336         NdefMap->MochupContainer.NdefData       = NdefData;
   1337         NdefMap->MochupContainer.NdefActualSize = NdefActualSize;
   1338         NdefMap->MochupContainer.NdefMaxSize    = NdefMaxSize;
   1339         NdefMap->MochupContainer.CardSize       = CardSize;
   1340         NdefMap->MochupContainer.CurrentBlock   = 0;
   1341 
   1342     } else
   1343     {
   1344         Status = PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP, NFCSTATUS_INVALID_PARAMETER);
   1345     }
   1346     return Status;
   1347 }
   1348 
   1349 NFCSTATUS phFriNfc_NdefMap_MockupNDefModeEn(uint8_t  *pNdefCompliancy, uint8_t  *pCardType, uint8_t Enable)
   1350 {
   1351     *pNdefCompliancy = Enable;
   1352     *pCardType = PH_FRINFC_NDEFMAP_MOCKUP_CARD;
   1353     return NFCSTATUS_SUCCESS;
   1354 }
   1355 #endif  /* PHFRINFC_OVRHAL_MOCKUP */
   1356 
   1357 
   1358 
   1359 
   1360 
   1361 
   1362 
   1363 
   1364