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 phLibNfc_ndef_raw.c
     19 
     20  * Project: NFC FRI 1.1
     21  *
     22  * $Date: Thu Apr 22 13:59:50 2010 $
     23  * $Author: ing07385 $
     24  * $Revision: 1.70 $
     25  * $Aliases: NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $
     26  *
     27  */
     28 
     29 /*
     30 ************************* Header Files ****************************************
     31 */
     32 
     33 #include <phLibNfcStatus.h>
     34 #include <phLibNfc.h>
     35 #include <phHal4Nfc.h>
     36 #include <phOsalNfc.h>
     37 #include <phLibNfc_Internal.h>
     38 #include <phLibNfc_ndef_raw.h>
     39 #include <phLibNfc_initiator.h>
     40 #include <phLibNfc_discovery.h>
     41 #include <phFriNfc_NdefReg.h>
     42 
     43 /*
     44 *************************** Macro's  ****************************************
     45 */
     46 
     47 #ifndef STATIC_DISABLE
     48 #define STATIC static
     49 #else
     50 //#undef STATIC
     51 #define STATIC
     52 #endif
     53 
     54 #define     TOPAZ_NDEF_BITMASK             0x10U
     55 #define     TOPAZ_LEN_BITMASK              0x02U
     56 #define     TOPAZ_DYNAMIC_LEN               460U
     57 #define     TOPAZ_STATIC_CARD_LEN           128U
     58 #define     MIFARE_STD_BLOCK_SIZE          0x10U
     59 /*
     60 *************************** Global Variables **********************************
     61 */
     62 phLibNfc_Ndef_Info_t NdefInfo;
     63 phFriNfc_NdefRecord_t *pNdefRecord=NULL;
     64 /*
     65 *************************** Static Function Declaration ***********************
     66 */
     67 
     68 /* Response callback for Check Ndef */
     69 STATIC
     70 void phLibNfc_Ndef_CheckNdef_Cb(void *pContext, NFCSTATUS status);
     71 
     72 /* Response callback for Ndef Write */
     73 STATIC
     74 void phLibNfc_Ndef_Write_Cb(void* Context,NFCSTATUS status);
     75 
     76 /* Response callback for Ndef Read*/
     77 STATIC
     78 void phLibNfc_Ndef_Read_Cb(void* Context,NFCSTATUS status);
     79 
     80 /* Response callback forNdef Format*/
     81 STATIC
     82 void phLibNfc_Ndef_format_Cb(void *Context,NFCSTATUS status);
     83 
     84 /* Response callback for Search Ndef Content */
     85 STATIC
     86 void phLibNfc_Ndef_SrchNdefCnt_Cb(void *context, NFCSTATUS status);
     87 
     88 /* Response callback for Ndef Record Type Discovery */
     89 STATIC
     90 void phLibNfc_Ndef_Rtd_Cb( void *CallBackParam);
     91 
     92 /* Response callback for Check Ndef timer callback */
     93 STATIC void CheckNdef_timer_cb(uint32_t timer_id, void *pContext);
     94 
     95 /*Callback for Presence check call from Chk Ndef*/
     96 STATIC void phLibNfc_Ndef_ChkNdef_Pchk_Cb(void   *pContext,
     97                                 NFCSTATUS  status
     98                                 );
     99 /*
    100 *************************** Function Definitions ******************************
    101 */
    102 
    103 /**
    104 * This function reads an NDEF message from  already connected tag.
    105 * the NDEF message  is read starting after the position of the
    106 * last read operation of the same tag during current session.
    107 */
    108 
    109 NFCSTATUS phLibNfc_Ndef_Read( phLibNfc_Handle                   hRemoteDevice,
    110                             phNfc_sData_t                      *psRd,
    111                             phLibNfc_Ndef_EOffset_t             Offset,
    112                             pphLibNfc_RspCb_t                   pNdefRead_RspCb,
    113                             void*                               pContext
    114                             )
    115 {
    116     NFCSTATUS RetVal = NFCSTATUS_FAILED;
    117     if((NULL == gpphLibContext)||
    118         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
    119     {
    120         RetVal = NFCSTATUS_NOT_INITIALISED;
    121     }
    122     else if((NULL == psRd) || (NULL == pNdefRead_RspCb)
    123         || (NULL == psRd->buffer)
    124         || (0 == psRd->length)
    125         || (NULL == pContext)
    126         || (0 == hRemoteDevice))
    127     {
    128         RetVal= NFCSTATUS_INVALID_PARAMETER;
    129     }
    130     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
    131     {
    132         RetVal = NFCSTATUS_SHUTDOWN;
    133     }
    134     else if(0 == gpphLibContext->Connected_handle)
    135     {   /*presently no target or tag is connected*/
    136         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
    137     }
    138     else if(hRemoteDevice != gpphLibContext->Connected_handle)
    139     {   /*This handle of the device sent by application is not connected */
    140         RetVal=NFCSTATUS_INVALID_HANDLE;
    141     }
    142     else if((TRUE == gpphLibContext->status.GenCb_pending_status)
    143             ||(NULL!=gpphLibContext->CBInfo.pClientRdNdefCb)
    144             ||(CHK_NDEF_NOT_DONE == gpphLibContext->ndef_cntx.is_ndef))
    145     {
    146         /*Previous callback is pending*/
    147         RetVal = NFCSTATUS_REJECTED;
    148     }
    149     else if(gpphLibContext->ndef_cntx.is_ndef == FALSE)
    150     {
    151         /*no Ndef Support in tag*/
    152          RetVal = NFCSTATUS_NON_NDEF_COMPLIANT;
    153     }
    154     else if((gpphLibContext->ndef_cntx.is_ndef == TRUE)
    155         &&(0 == gpphLibContext->ndef_cntx.NdefActualSize))
    156     {
    157         /*Card is empty- So Returning length as zero*/
    158         psRd->length = 0;
    159         RetVal = NFCSTATUS_SUCCESS;
    160     }
    161     else
    162     {
    163         gpphLibContext->psRemoteDevList->psRemoteDevInfo->SessionOpened = SESSION_OPEN;
    164         gpphLibContext->ndef_cntx.eLast_Call = NdefRd;
    165         if((((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType ==
    166             phHal_eMifare_PICC) && (((phHal_sRemoteDevInformation_t*)
    167             hRemoteDevice)->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
    168             ((NULL == gpphLibContext->psBufferedAuth)
    169             ||(phHal_eMifareAuthentA == gpphLibContext->psBufferedAuth->cmd.MfCmd))
    170             )
    171         {
    172             if(NULL != gpphLibContext->psBufferedAuth)
    173             {
    174                 if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
    175                 {
    176                     phOsalNfc_FreeMemory(
    177                         gpphLibContext->psBufferedAuth->sRecvData.buffer);
    178                 }
    179                 if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
    180                 {
    181                     phOsalNfc_FreeMemory(
    182                         gpphLibContext->psBufferedAuth->sSendData.buffer);
    183                 }
    184                 phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
    185             }
    186             gpphLibContext->psBufferedAuth
    187                 =(phLibNfc_sTransceiveInfo_t *)
    188                 phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
    189             gpphLibContext->psBufferedAuth->addr =
    190              (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
    191              ->StdMifareContainer.currentBlock;
    192             gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
    193             gpphLibContext->psBufferedAuth->sSendData.length
    194                 = 0;
    195             gpphLibContext->psBufferedAuth->sRecvData.length
    196                 = MIFARE_STD_BLOCK_SIZE;
    197             gpphLibContext->psBufferedAuth->sRecvData.buffer
    198                 = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
    199             gpphLibContext->psBufferedAuth->sSendData.buffer
    200              = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
    201         }
    202         if(eLibNfcHalStatePresenceChk !=
    203                 gpphLibContext->LibNfcState.next_state)
    204         {
    205             uint8_t     cr_index = 0;
    206             gpphLibContext->ndef_cntx.psUpperNdefMsg = psRd;
    207             for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
    208             {
    209                 RetVal= phFriNfc_NdefMap_SetCompletionRoutine(
    210                                     gpphLibContext->ndef_cntx.psNdefMap,
    211                                     cr_index,
    212                                     phLibNfc_Ndef_Read_Cb,
    213                                     (void *)gpphLibContext);
    214 
    215             }
    216             gpphLibContext->ndef_cntx.NdefContinueRead =(uint8_t) ((phLibNfc_Ndef_EBegin==Offset) ?
    217                                                     PH_FRINFC_NDEFMAP_SEEK_BEGIN :
    218                                                     PH_FRINFC_NDEFMAP_SEEK_CUR);
    219             /* call below layer Ndef Read*/
    220             RetVal = phFriNfc_NdefMap_RdNdef(gpphLibContext->ndef_cntx.psNdefMap,
    221                             gpphLibContext->ndef_cntx.psUpperNdefMsg->buffer,
    222                             (uint32_t*)&gpphLibContext->ndef_cntx.psUpperNdefMsg->length,
    223                             gpphLibContext->ndef_cntx.NdefContinueRead);
    224 
    225             RetVal = PHNFCSTATUS(RetVal);
    226             if(NFCSTATUS_INSUFFICIENT_STORAGE == RetVal)
    227             {
    228                 gpphLibContext->ndef_cntx.psUpperNdefMsg->length = 0;
    229                 RetVal = NFCSTATUS_SUCCESS;
    230             }
    231         }
    232         else
    233         {
    234              gpphLibContext->CBInfo.pClientRdNdefCb= NULL;
    235              RetVal = NFCSTATUS_PENDING;
    236         }
    237         if(NFCSTATUS_PENDING == RetVal)
    238         {
    239             gpphLibContext->CBInfo.pClientRdNdefCb = pNdefRead_RspCb;
    240             gpphLibContext->CBInfo.pClientRdNdefCntx = pContext;
    241             gpphLibContext->status.GenCb_pending_status=TRUE;
    242 			gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
    243 
    244         }
    245         else if (NFCSTATUS_SUCCESS == RetVal)
    246         {
    247             RetVal= NFCSTATUS_SUCCESS;
    248         }
    249         else
    250         {
    251             /*Ndef read failed*/
    252             RetVal = NFCSTATUS_FAILED;
    253         }
    254     }
    255     return RetVal;
    256 }
    257 /* Response callback for phLibNfc_Ndef_Read */
    258 STATIC
    259 void phLibNfc_Ndef_Read_Cb(void* Context,NFCSTATUS status)
    260 {
    261     NFCSTATUS               RetStatus = NFCSTATUS_SUCCESS;
    262     pphLibNfc_RspCb_t       pClientCb=NULL;
    263     phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
    264     void                    *pUpperLayerContext=NULL;
    265     phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
    266 
    267     if(pLibNfc_Ctxt != gpphLibContext)
    268     {
    269         /*wrong context returned*/
    270         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    271     }
    272     else
    273     {
    274         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
    275         {   /*shutdown called before completion of Ndef read allow
    276               shutdown to happen */
    277             phLibNfc_Pending_Shutdown();
    278             RetStatus = NFCSTATUS_SHUTDOWN;
    279         }
    280         else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
    281         {
    282             RetStatus = NFCSTATUS_ABORTED;
    283         }
    284         else
    285         {
    286             gpphLibContext->status.GenCb_pending_status = FALSE;
    287             if (gpphLibContext->psBufferedAuth != NULL && gpphLibContext->ndef_cntx.psNdefMap != NULL) {
    288 		    gpphLibContext->psBufferedAuth->addr = (uint8_t)
    289 		    gpphLibContext->ndef_cntx.psNdefMap->StdMifareContainer.currentBlock;
    290             }
    291 
    292             if(NFCSTATUS_FAILED == status )
    293             {
    294                 /*During Ndef read operation tag was not present in RF
    295                 field of reader*/
    296                 RetStatus = NFCSTATUS_FAILED;
    297                 gpphLibContext->LastTrancvSuccess = FALSE;
    298                 gpphLibContext->ndef_cntx.is_ndef = FALSE;
    299                 ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
    300                                     gpphLibContext->Connected_handle;
    301                 if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
    302                     (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
    303                 {
    304 
    305                     /* card type is mifare 1k/4k, then reconnect */
    306                     RetStatus = phHal4Nfc_Connect(gpphLibContext->psHwReference,
    307                                 ps_rem_dev_info,
    308                                 (pphHal4Nfc_ConnectCallback_t)
    309                                 phLibNfc_Reconnect_Mifare_Cb,
    310                                 (void *)gpphLibContext);
    311                 }
    312 
    313             }
    314             else if(status == NFCSTATUS_SUCCESS)
    315             {
    316                 gpphLibContext->LastTrancvSuccess = TRUE;
    317                 RetStatus = NFCSTATUS_SUCCESS;
    318             }
    319 		    else
    320 		    {
    321                 gpphLibContext->LastTrancvSuccess = FALSE;
    322 				RetStatus = NFCSTATUS_FAILED;
    323 			}
    324         }
    325         /*update the current state as connected*/
    326         phLibNfc_UpdateCurState(status,gpphLibContext);
    327 
    328         pClientCb = gpphLibContext->CBInfo.pClientRdNdefCb;
    329         pUpperLayerContext = gpphLibContext->CBInfo.pClientRdNdefCntx;
    330 
    331         gpphLibContext->CBInfo.pClientRdNdefCb = NULL;
    332         gpphLibContext->CBInfo.pClientRdNdefCntx = NULL;
    333         if(NFCSTATUS_PENDING != RetStatus)
    334         {
    335             if (NULL != pClientCb)
    336             {
    337                 /*Notify to upper layer status and read bytes*/
    338                 pClientCb(pUpperLayerContext,RetStatus);
    339             }
    340         }
    341     }
    342     return;
    343 }
    344 
    345 /**
    346 * Write NDEF to a tag.
    347 *
    348 * This function allows the user to write a NDEF data to already connected NFC
    349 * tag.Function writes   a complete NDEF message to a tag. If a NDEF message
    350 * already exists in the tag, it will be overwritten. When the transaction is
    351 * complete,a notification callback is notified.
    352 */
    353 NFCSTATUS phLibNfc_Ndef_Write(
    354                             phLibNfc_Handle          hRemoteDevice,
    355                             phNfc_sData_t           *psWr,
    356                             pphLibNfc_RspCb_t        pNdefWrite_RspCb,
    357                             void*                    pContext
    358                             )
    359 {
    360     NFCSTATUS RetVal = NFCSTATUS_FAILED;
    361     uint8_t             NdefWriteType=0xFF;
    362     /*LibNfc is initilized or not */
    363     if((NULL == gpphLibContext)||
    364         (gpphLibContext->LibNfcState.cur_state
    365                             == eLibNfcHalStateShutdown))
    366     {
    367         RetVal = NFCSTATUS_NOT_INITIALISED;
    368     }/*Check for application has sent the valid parameters*/
    369     else if((NULL == psWr) || (NULL == pNdefWrite_RspCb)
    370         || (NULL == psWr->buffer)
    371         || (NULL == pContext)
    372         || (0 ==hRemoteDevice))
    373     {
    374         RetVal= NFCSTATUS_INVALID_PARAMETER;
    375     }
    376     else if(gpphLibContext->LibNfcState.next_state
    377                             == eLibNfcHalStateShutdown)
    378     {   /* Lib Nfc Shutdown*/
    379         RetVal= NFCSTATUS_SHUTDOWN;
    380     }
    381     else if(0 == gpphLibContext->Connected_handle)
    382     {
    383         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
    384     }
    385     else if(hRemoteDevice != gpphLibContext->Connected_handle)
    386     {
    387         RetVal=NFCSTATUS_INVALID_HANDLE;
    388     }
    389     else if((TRUE == gpphLibContext->status.GenCb_pending_status)||
    390            (gpphLibContext->ndef_cntx.is_ndef == CHK_NDEF_NOT_DONE))
    391     {
    392          /* Previous callback is pending or Tag is not NDEF tag*/
    393         RetVal = NFCSTATUS_REJECTED;
    394         PHDBG_INFO("LIbNfc:Previous Callback is Pending");
    395     }
    396     else if(FALSE == gpphLibContext->ndef_cntx.is_ndef)
    397     {
    398         RetVal = NFCSTATUS_NON_NDEF_COMPLIANT;
    399     }
    400     else if(psWr->length > gpphLibContext->ndef_cntx.NdefLength)
    401     {
    402         RetVal = NFCSTATUS_NOT_ENOUGH_MEMORY;
    403     }
    404     else
    405     {
    406         uint8_t         cr_index = 0;
    407         gpphLibContext->ndef_cntx.psUpperNdefMsg = psWr;
    408         gpphLibContext->ndef_cntx.AppWrLength= psWr->length;
    409         gpphLibContext->ndef_cntx.eLast_Call = NdefWr;
    410         gpphLibContext->psRemoteDevList->psRemoteDevInfo->SessionOpened
    411             = SESSION_OPEN;
    412         if((((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType ==
    413                phHal_eMifare_PICC) && (((phHal_sRemoteDevInformation_t*)
    414                hRemoteDevice)->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
    415                ((NULL == gpphLibContext->psBufferedAuth)
    416                 ||(phHal_eMifareAuthentA ==
    417                    gpphLibContext->psBufferedAuth->cmd.MfCmd))
    418                )
    419         {
    420             if(NULL != gpphLibContext->psBufferedAuth)
    421             {
    422                 if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
    423                 {
    424                     phOsalNfc_FreeMemory(
    425                         gpphLibContext->psBufferedAuth->sRecvData.buffer);
    426                 }
    427                 if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
    428                 {
    429                     phOsalNfc_FreeMemory(
    430                         gpphLibContext->psBufferedAuth->sSendData.buffer);
    431                 }
    432                 phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
    433             }
    434             gpphLibContext->psBufferedAuth
    435                 =(phLibNfc_sTransceiveInfo_t *)
    436                 phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
    437             gpphLibContext->psBufferedAuth->addr =
    438              (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
    439              ->StdMifareContainer.currentBlock;
    440             gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
    441             gpphLibContext->psBufferedAuth->sSendData.length
    442                 = 0;
    443             gpphLibContext->psBufferedAuth->sRecvData.length
    444                 = MIFARE_STD_BLOCK_SIZE;
    445             gpphLibContext->psBufferedAuth->sRecvData.buffer
    446                 = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
    447              gpphLibContext->psBufferedAuth->sSendData.buffer
    448                 = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
    449         }
    450         if(eLibNfcHalStatePresenceChk ==
    451                 gpphLibContext->LibNfcState.next_state)
    452         {
    453             gpphLibContext->CBInfo.pClientWrNdefCb = NULL;
    454             RetVal = NFCSTATUS_PENDING;
    455         }
    456         else
    457         {
    458             for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
    459             {
    460                 /* Registering the Completion Routine.*/
    461                 RetVal= phFriNfc_NdefMap_SetCompletionRoutine(
    462                                     gpphLibContext->ndef_cntx.psNdefMap,
    463                                     cr_index,
    464                                     phLibNfc_Ndef_Write_Cb,
    465                                     (void *)gpphLibContext);
    466 
    467             }
    468             if(0 == psWr->length)
    469             {
    470                  /* Length of bytes to be written Zero- Erase the Tag  */
    471                 RetVal = phFriNfc_NdefMap_EraseNdef(gpphLibContext->ndef_cntx.psNdefMap);
    472             }
    473             else
    474             {
    475                 /*Write from beginning or current location*/
    476                 NdefWriteType = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
    477                 /*Call FRI Ndef Write*/
    478                 RetVal=phFriNfc_NdefMap_WrNdef(gpphLibContext->ndef_cntx.psNdefMap,
    479                             gpphLibContext->ndef_cntx.psUpperNdefMsg->buffer,
    480                             (uint32_t*)&gpphLibContext->ndef_cntx.psUpperNdefMsg->length,
    481                             NdefWriteType);
    482             }
    483             if(NFCSTATUS_PENDING == RetVal)
    484             {
    485                 gpphLibContext->CBInfo.pClientWrNdefCb = pNdefWrite_RspCb;
    486                 gpphLibContext->CBInfo.pClientWrNdefCntx = pContext;
    487                 gpphLibContext->status.GenCb_pending_status=TRUE;
    488                 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
    489             }
    490             else
    491             {
    492                 RetVal = NFCSTATUS_FAILED;
    493             }
    494         }
    495     }
    496     return RetVal;
    497 }
    498 
    499 /* Response callback for phLibNfc_Ndef_Write */
    500 STATIC
    501 void phLibNfc_Ndef_Write_Cb(void* Context,NFCSTATUS status)
    502 {
    503 
    504     pphLibNfc_RspCb_t       pClientCb=NULL;
    505     phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
    506     void                    *pUpperLayerContext=NULL;
    507     phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
    508 
    509     if(pLibNfc_Ctxt != gpphLibContext)
    510     {   /*wrong context returned*/
    511         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    512     }
    513     else
    514     {
    515         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
    516         {   /*shutdown called before completion of Ndef write allow
    517               shutdown to happen */
    518             phLibNfc_Pending_Shutdown();
    519             status = NFCSTATUS_SHUTDOWN;
    520         }
    521         else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
    522         {
    523             status = NFCSTATUS_ABORTED;
    524         }
    525         else
    526         {
    527             gpphLibContext->status.GenCb_pending_status = FALSE;
    528             if (gpphLibContext->psBufferedAuth != NULL && gpphLibContext->ndef_cntx.psNdefMap != NULL) {
    529                 gpphLibContext->psBufferedAuth->addr = (uint8_t)
    530                     gpphLibContext->ndef_cntx.psNdefMap->TLVStruct.NdefTLVBlock;
    531             }
    532             if(status == NFCSTATUS_FAILED )
    533             {
    534 				status = NFCSTATUS_FAILED;
    535                 gpphLibContext->LastTrancvSuccess = FALSE;
    536                 /*During Ndef write operation tag was not present in RF
    537                 field of reader*/
    538                 ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
    539                                     gpphLibContext->Connected_handle;
    540                if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
    541                     (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
    542                 {
    543 
    544 
    545                     /* card type is mifare 1k/4k, then reconnect */
    546                     status = phHal4Nfc_Connect(gpphLibContext->psHwReference,
    547                                 ps_rem_dev_info,
    548                                 (pphHal4Nfc_ConnectCallback_t)
    549                                 phLibNfc_Reconnect_Mifare_Cb,
    550                                 (void *)gpphLibContext);
    551                 }
    552             }
    553             else if( status== NFCSTATUS_SUCCESS)
    554             {
    555                 gpphLibContext->LastTrancvSuccess = TRUE;
    556                 status = NFCSTATUS_SUCCESS;
    557                 if(gpphLibContext->ndef_cntx.AppWrLength >
    558                                  gpphLibContext->ndef_cntx.NdefLength)
    559                 {
    560                     status = NFCSTATUS_NOT_ENOUGH_MEMORY;
    561                 }
    562             }
    563 			else
    564 			{
    565                 gpphLibContext->LastTrancvSuccess = FALSE;
    566 				status = NFCSTATUS_FAILED;;
    567 			}
    568         }
    569         phLibNfc_UpdateCurState(status,gpphLibContext);
    570 
    571         pClientCb = gpphLibContext->CBInfo.pClientWrNdefCb;
    572         pUpperLayerContext = gpphLibContext->CBInfo.pClientWrNdefCntx;
    573 
    574         gpphLibContext->CBInfo.pClientWrNdefCb = NULL;
    575         gpphLibContext->CBInfo.pClientWrNdefCntx = NULL;
    576         if(NFCSTATUS_PENDING !=status)
    577         {
    578             if (NULL != pClientCb)
    579             {
    580                 /*Notify to upper layer status and No. of bytes
    581                 actually written */
    582                 pClientCb(pUpperLayerContext, status);
    583             }
    584         }
    585     }
    586     return;
    587 }
    588 
    589 
    590 /**
    591 * Initialize structures needed for the Ndef
    592 * related operation such as Check Ndef, read, write
    593 * and Ndef foramt.only once allocation
    594 */
    595 void phLibNfc_Ndef_Init(void)
    596 {
    597     if(gpphLibContext->psTransInfo==NULL)
    598     {
    599         /*Allocate memory for Transceiveinformation Structure*/
    600         gpphLibContext->psTransInfo = (phLibNfc_sTransceiveInfo_t *)
    601             phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
    602     }
    603     if(gpphLibContext->psTransInfo==NULL)
    604     {
    605         /*exception: Not enough memory*/
    606         phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
    607 
    608     }
    609     if(NULL == gpphLibContext->ndef_cntx.psNdefMap)
    610     {
    611         /*Allocate memory for NDEF Mapping Component Context Structure*/
    612         gpphLibContext->ndef_cntx.psNdefMap = (phFriNfc_NdefMap_t *)
    613                     phOsalNfc_GetMemory(sizeof(phFriNfc_NdefMap_t));
    614     }
    615     if(NULL != gpphLibContext->ndef_cntx.psNdefMap)
    616     {
    617         /*Allocation successful*/
    618         (void)memset(gpphLibContext->ndef_cntx.psNdefMap,0,sizeof(phFriNfc_NdefMap_t));
    619         gpphLibContext->ndef_cntx.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
    620         gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf =
    621                 (uint8_t*) phOsalNfc_GetMemory(gpphLibContext->
    622                 ndef_cntx.NdefSendRecvLen);
    623 
    624         if(NULL != gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf)
    625         {
    626             (void)memset(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
    627                 0,
    628                 gpphLibContext->ndef_cntx.NdefSendRecvLen);
    629 
    630             gpphLibContext->psOverHalCtxt =(phFriNfc_OvrHal_t *)
    631                 phOsalNfc_GetMemory(sizeof(phFriNfc_OvrHal_t));
    632         }
    633     }
    634     if(NULL == gpphLibContext->psOverHalCtxt)
    635     {   /*exception: Not enough memory*/
    636         phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
    637     }
    638     else
    639     {
    640 
    641         (void)memset(gpphLibContext->psOverHalCtxt,0,
    642             sizeof(phFriNfc_OvrHal_t));
    643 
    644         /* Initialize the Overlapped hal structure*/
    645         gpphLibContext->psOverHalCtxt->psHwReference =
    646              gpphLibContext->psHwReference;
    647         if(NULL == gpphLibContext->psDevInputParam )
    648         {
    649             gpphLibContext->psDevInputParam = (phHal_sDevInputParam_t *)
    650                 phOsalNfc_GetMemory(sizeof(phHal_sDevInputParam_t));
    651         }
    652         gpphLibContext->ndef_cntx.is_ndef = CHK_NDEF_NOT_DONE;
    653     }
    654     if(NULL == gpphLibContext->ndef_cntx.ndef_fmt)
    655     {
    656         /*Allocate memory for Ndef format structure*/
    657         gpphLibContext->ndef_cntx.ndef_fmt = (phFriNfc_sNdefSmtCrdFmt_t *)
    658                 phOsalNfc_GetMemory(sizeof(phFriNfc_sNdefSmtCrdFmt_t));
    659     }
    660     if(NULL != gpphLibContext->ndef_cntx.ndef_fmt)
    661     {
    662         (void)memset(gpphLibContext->ndef_cntx.ndef_fmt,
    663                         0,
    664                         sizeof(phFriNfc_sNdefSmtCrdFmt_t));
    665     }
    666     else
    667     {
    668         /*exception: Not enough memory*/
    669         phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
    670     }
    671     return;
    672 }
    673 /**
    674 * Free the allocated memory used for Ndef operations
    675 */
    676 void phLibNfc_Ndef_DeInit(void)
    677 {
    678     /* If only allocated then only free the memory*/
    679     if(gpphLibContext->ndef_cntx.psNdefMap !=NULL)
    680     {
    681         if(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf !=NULL)
    682         {
    683             phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf);
    684             gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf=NULL;
    685         }
    686         phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap);
    687         gpphLibContext->ndef_cntx.psNdefMap =NULL;
    688     }
    689 
    690     if(NULL != gpphLibContext->ndef_cntx.ndef_fmt)
    691     {
    692         phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.ndef_fmt);
    693         gpphLibContext->ndef_cntx.ndef_fmt = NULL;
    694     }
    695 
    696     if(gpphLibContext->psOverHalCtxt !=NULL)
    697     {
    698         phOsalNfc_FreeMemory(gpphLibContext->psOverHalCtxt);
    699         gpphLibContext->psOverHalCtxt =NULL;
    700     }
    701     if(gpphLibContext->psDevInputParam !=NULL)
    702     {
    703         phOsalNfc_FreeMemory(gpphLibContext->psDevInputParam);
    704         gpphLibContext->psDevInputParam = NULL;
    705     }
    706     if(gpphLibContext->psTransInfo!=NULL)
    707     {
    708         phOsalNfc_FreeMemory(gpphLibContext->psTransInfo);
    709         gpphLibContext->psTransInfo= NULL;
    710     }
    711 }
    712 
    713 
    714 /**
    715 * This function allows  the user to check whether a particular Remote Device
    716 * is NDEF compliant or not
    717 */
    718 NFCSTATUS phLibNfc_Ndef_CheckNdef(phLibNfc_Handle       hRemoteDevice,
    719                         pphLibNfc_ChkNdefRspCb_t        pCheckNdef_RspCb,
    720                         void*                           pContext)
    721 {
    722     NFCSTATUS RetVal = NFCSTATUS_FAILED;
    723 
    724 
    725     if((NULL == gpphLibContext)||
    726         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
    727     {
    728         /*Lib Nfc not initialized*/
    729         RetVal = NFCSTATUS_NOT_INITIALISED;
    730     }
    731     else if((NULL == pCheckNdef_RspCb)||
    732         (NULL==pContext)||
    733         (hRemoteDevice == 0))
    734     {
    735         /*parameter sent by upper layer are not valid */
    736         RetVal= NFCSTATUS_INVALID_PARAMETER;
    737     }
    738     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
    739     {
    740         RetVal = NFCSTATUS_SHUTDOWN;
    741     }
    742     else if(0 == gpphLibContext->Connected_handle)
    743     {
    744         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
    745     }
    746     else if(hRemoteDevice != gpphLibContext->Connected_handle)
    747     {
    748         RetVal=NFCSTATUS_INVALID_HANDLE;
    749     }
    750     else
    751     {
    752         uint8_t     cr_index = 0;
    753         static uint16_t     data_cnt = 0;
    754         /* Allocate memory for the ndef related structure */
    755         gpphLibContext->ndef_cntx.NdefSendRecvLen=300;
    756         gpphLibContext->ndef_cntx.eLast_Call = ChkNdef;
    757 
    758         /* Resets the component instance */
    759         RetVal = phFriNfc_NdefMap_Reset( gpphLibContext->ndef_cntx.psNdefMap,
    760                             gpphLibContext->psOverHalCtxt,
    761                             (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice,
    762                             gpphLibContext->psDevInputParam,
    763                             gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
    764                             gpphLibContext->ndef_cntx.NdefSendRecvLen,
    765                             gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
    766                             &(gpphLibContext->ndef_cntx.NdefSendRecvLen),
    767                             &(data_cnt));
    768 
    769 
    770         for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
    771         {
    772             /* Register the callback for the check ndef */
    773             RetVal = phFriNfc_NdefMap_SetCompletionRoutine(
    774                                 gpphLibContext->ndef_cntx.psNdefMap,
    775                                 cr_index,
    776                                 phLibNfc_Ndef_CheckNdef_Cb,
    777                                 (void *)gpphLibContext);
    778         }
    779         /*call below layer check Ndef function*/
    780         RetVal = phFriNfc_NdefMap_ChkNdef(gpphLibContext->ndef_cntx.psNdefMap);
    781         RetVal =PHNFCSTATUS(RetVal);
    782 
    783         if(RetVal== NFCSTATUS_PENDING)
    784         {
    785             RetVal = NFCSTATUS_PENDING;
    786         }
    787         else if((RetVal == NFCSTATUS_FAILED) || (RetVal ==(PHNFCSTVAL(CID_FRI_NFC_NDEF_MAP,
    788                     NFCSTATUS_INVALID_REMOTE_DEVICE))))
    789         {
    790 			      RetVal= NFCSTATUS_FAILED;
    791         }
    792         else
    793         {
    794             if((0x00 == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id)||
    795               (PH_OSALNFC_INVALID_TIMER_ID == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id))
    796 	          {
    797 		            gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id =
    798 			          phOsalNfc_Timer_Create();
    799 	          }
    800 	          if((0x00 == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id)||
    801               (PH_OSALNFC_INVALID_TIMER_ID == gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id))
    802 	          {
    803 		            RetVal = NFCSTATUS_FAILED;
    804 	          }
    805 	          else
    806 	          {
    807 	              phOsalNfc_Timer_Start(gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id,
    808 			          CHK_NDEF_TIMER_TIMEOUT,CheckNdef_timer_cb,NULL);
    809                 RetVal = NFCSTATUS_PENDING;
    810 	          }
    811         }
    812         if(RetVal== NFCSTATUS_PENDING)
    813         {
    814             gpphLibContext->CBInfo.pClientCkNdefCb = pCheckNdef_RspCb;
    815             gpphLibContext->CBInfo.pClientCkNdefCntx = pContext;
    816             gpphLibContext->status.GenCb_pending_status=TRUE;
    817             gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
    818         }
    819 
    820     }
    821     return RetVal;
    822 }
    823 
    824 /* Response callback for phLibNfc_Ndef_CheckNdef */
    825 STATIC
    826 void phLibNfc_Ndef_CheckNdef_Cb(void *pContext,NFCSTATUS status)
    827 {
    828     phLibNfc_ChkNdef_Info_t    Ndef_Info;
    829     NFCSTATUS RetStatus = NFCSTATUS_SUCCESS;
    830     pphLibNfc_ChkNdefRspCb_t       pClientCb=NULL;
    831     phLibNfc_LibContext_t           *pLibNfc_Ctxt =
    832                                     (phLibNfc_LibContext_t *)pContext;
    833     void                    *pUpperLayerContext=NULL;
    834     phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
    835 
    836     Ndef_Info.ActualNdefMsgLength = 0;
    837     Ndef_Info.MaxNdefMsgLength = 0;
    838     if(pLibNfc_Ctxt != gpphLibContext)
    839     {    /*wrong context returned from below layer*/
    840         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    841     }
    842     else
    843     {
    844         ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
    845                                     gpphLibContext->Connected_handle;
    846         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
    847         {   /*shutdown called before completion of check Ndef, allow
    848               shutdown to happen */
    849             phLibNfc_Pending_Shutdown();
    850             RetStatus = NFCSTATUS_SHUTDOWN;
    851         }
    852         else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
    853         {
    854             RetStatus = NFCSTATUS_ABORTED;
    855         }
    856         else
    857         {
    858             if(status == NFCSTATUS_SUCCESS)
    859             {
    860                 /*Tag is Ndef tag*/
    861                 gpphLibContext->ndef_cntx.is_ndef = TRUE;
    862                 (void)phFriNfc_NdefMap_GetContainerSize(
    863                                 pLibNfc_Ctxt->ndef_cntx.psNdefMap,
    864                                 &(pLibNfc_Ctxt->ndef_cntx.NdefLength),
    865                                 &(pLibNfc_Ctxt->ndef_cntx.NdefActualSize));
    866                 /*Get the data size support by particular ndef card */
    867                 Ndef_Info.ActualNdefMsgLength = pLibNfc_Ctxt->ndef_cntx.NdefActualSize;
    868                 Ndef_Info.MaxNdefMsgLength = pLibNfc_Ctxt->ndef_cntx.NdefLength;
    869                 gpphLibContext->LastTrancvSuccess = TRUE;
    870                 RetStatus =NFCSTATUS_SUCCESS;
    871             }
    872             else if (PHNFCSTATUS(status) != NFCSTATUS_MORE_INFORMATION )
    873             {
    874                 /*Ndef check Failed.Issue a PresenceChk to ascertain if tag is
    875                   still in the field*/
    876                 RetStatus = phHal4Nfc_PresenceCheck(
    877                                     gpphLibContext->psHwReference,
    878                                     phLibNfc_Ndef_ChkNdef_Pchk_Cb,
    879                                     (void *)gpphLibContext
    880                                     );
    881             }
    882             else
    883             {
    884 				RetStatus = NFCSTATUS_FAILED;
    885                 gpphLibContext->LastTrancvSuccess = FALSE;
    886                 gpphLibContext->ndef_cntx.is_ndef = FALSE;
    887 
    888                 if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
    889                     (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
    890                 {
    891 
    892                     /* card type is mifare 1k/4k, then reconnect */
    893                     RetStatus = phHal4Nfc_Connect(gpphLibContext->psHwReference,
    894                                 ps_rem_dev_info,
    895                                 (pphHal4Nfc_ConnectCallback_t)
    896                                 phLibNfc_Reconnect_Mifare_Cb,
    897                                 (void *)gpphLibContext);
    898                 }
    899                 else
    900                 {
    901                    if((phHal_eJewel_PICC == ps_rem_dev_info->RemDevType)
    902                        &&(TOPAZ_NDEF_BITMASK &
    903                           ps_rem_dev_info->RemoteDevInfo.Jewel_Info.HeaderRom0))
    904                     {
    905                         gpphLibContext->ndef_cntx.is_ndef = TRUE;
    906                         RetStatus = phFriNfc_NdefMap_GetContainerSize(
    907                                         pLibNfc_Ctxt->ndef_cntx.psNdefMap,
    908                                         &(pLibNfc_Ctxt->ndef_cntx.NdefLength),
    909                                         &(pLibNfc_Ctxt->ndef_cntx.NdefActualSize));
    910                         /*Get the data size support by particular ndef card */
    911                         Ndef_Info.ActualNdefMsgLength =
    912                             pLibNfc_Ctxt->ndef_cntx.NdefActualSize;
    913                         Ndef_Info.MaxNdefMsgLength
    914                             = pLibNfc_Ctxt->ndef_cntx.NdefLength
    915                             = (TOPAZ_LEN_BITMASK &
    916                             ps_rem_dev_info->RemoteDevInfo.Jewel_Info.HeaderRom0?
    917                             TOPAZ_DYNAMIC_LEN:TOPAZ_STATIC_CARD_LEN);
    918                         RetStatus = NFCSTATUS_SUCCESS;
    919                     }
    920                 }
    921             }
    922             gpphLibContext->LibNfcState.cur_state=eLibNfcHalStateConnect;
    923         }
    924         gpphLibContext->status.GenCb_pending_status = FALSE;
    925         /* Update the current state */
    926         phLibNfc_UpdateCurState(RetStatus,gpphLibContext);
    927         if(NFCSTATUS_PENDING != RetStatus)
    928         {
    929             if(((ps_rem_dev_info->RemDevType == phHal_eMifare_PICC)
    930                 && (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
    931                ((NULL == gpphLibContext->psBufferedAuth)
    932                 ||(phHal_eMifareAuthentA == gpphLibContext->psBufferedAuth->cmd.MfCmd)))
    933                )
    934             {
    935                 if(NULL != gpphLibContext->psBufferedAuth)
    936                 {
    937                     if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
    938                     {
    939                         phOsalNfc_FreeMemory(
    940                             gpphLibContext->psBufferedAuth->sRecvData.buffer);
    941                     }
    942                     if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
    943                     {
    944                         phOsalNfc_FreeMemory(
    945                             gpphLibContext->psBufferedAuth->sSendData.buffer);
    946                     }
    947                     phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
    948                 }
    949                 gpphLibContext->psBufferedAuth
    950                     =(phLibNfc_sTransceiveInfo_t *)
    951                     phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
    952                 gpphLibContext->psBufferedAuth->addr =
    953                 (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
    954                 ->StdMifareContainer.currentBlock;
    955                 gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
    956                 gpphLibContext->psBufferedAuth->sSendData.length
    957                     = 0;
    958                 gpphLibContext->psBufferedAuth->sRecvData.length
    959                     = MIFARE_STD_BLOCK_SIZE;
    960                 gpphLibContext->psBufferedAuth->sRecvData.buffer
    961                     = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
    962                 gpphLibContext->psBufferedAuth->sSendData.buffer
    963                     = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
    964             }
    965             pClientCb = gpphLibContext->CBInfo.pClientCkNdefCb;
    966             pUpperLayerContext = gpphLibContext->CBInfo.pClientCkNdefCntx;
    967             gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
    968             gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
    969             if(NULL != pClientCb)
    970             {
    971                 /* call the upper check ndef callback */
    972                 pClientCb(pUpperLayerContext,Ndef_Info,RetStatus);
    973             }
    974         }
    975     }
    976     return;
    977 }
    978 
    979 /*Callback for Presence check call from Chk Ndef*/
    980 STATIC void phLibNfc_Ndef_ChkNdef_Pchk_Cb(void   *pContext,
    981                                 NFCSTATUS  status
    982                                 )
    983 {
    984     phLibNfc_ChkNdef_Info_t    Ndef_Info = {0,0};
    985     NFCSTATUS RetStatus = NFCSTATUS_SUCCESS;
    986     pphLibNfc_ChkNdefRspCb_t       pClientCb=NULL;
    987     phLibNfc_LibContext_t           *pLibNfc_Ctxt =
    988                                     (phLibNfc_LibContext_t *)pContext;
    989     void                    *pUpperLayerContext=NULL;
    990     phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
    991     if(NFCSTATUS_SUCCESS == status)
    992     {
    993         RetStatus = NFCSTATUS_FAILED;
    994         gpphLibContext->ndef_cntx.is_ndef = FALSE;
    995     }
    996     else
    997     {
    998         RetStatus = NFCSTATUS_TARGET_LOST;
    999     }
   1000     if(pLibNfc_Ctxt != gpphLibContext)
   1001     {    /*wrong context returned from below layer*/
   1002         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
   1003     }
   1004     else
   1005     {
   1006         ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
   1007                                     gpphLibContext->Connected_handle;
   1008         if(((ps_rem_dev_info->RemDevType == phHal_eMifare_PICC)
   1009             && (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
   1010             ((NULL == gpphLibContext->psBufferedAuth)
   1011             ||(phHal_eMifareAuthentA == gpphLibContext->psBufferedAuth->cmd.MfCmd)))
   1012             )
   1013         {
   1014             if(NULL != gpphLibContext->psBufferedAuth)
   1015             {
   1016                 if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
   1017                 {
   1018                     phOsalNfc_FreeMemory(
   1019                         gpphLibContext->psBufferedAuth->sRecvData.buffer);
   1020                 }
   1021                 if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
   1022                 {
   1023                     phOsalNfc_FreeMemory(
   1024                         gpphLibContext->psBufferedAuth->sSendData.buffer);
   1025                 }
   1026                 phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
   1027             }
   1028             gpphLibContext->psBufferedAuth
   1029                 =(phLibNfc_sTransceiveInfo_t *)
   1030                 phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
   1031             gpphLibContext->psBufferedAuth->addr =
   1032             (uint8_t)gpphLibContext->ndef_cntx.psNdefMap
   1033             ->StdMifareContainer.currentBlock;
   1034             gpphLibContext->psBufferedAuth->cmd.MfCmd = phHal_eMifareRead16;
   1035             gpphLibContext->psBufferedAuth->sSendData.length
   1036                 = 0;
   1037             gpphLibContext->psBufferedAuth->sRecvData.length
   1038                 = MIFARE_STD_BLOCK_SIZE;
   1039             gpphLibContext->psBufferedAuth->sRecvData.buffer
   1040                 = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
   1041             gpphLibContext->psBufferedAuth->sSendData.buffer
   1042                 = (uint8_t *)phOsalNfc_GetMemory(MIFARE_STD_BLOCK_SIZE);
   1043         }
   1044         pClientCb = gpphLibContext->CBInfo.pClientCkNdefCb;
   1045         pUpperLayerContext = gpphLibContext->CBInfo.pClientCkNdefCntx;
   1046         gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
   1047         gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
   1048         if(NULL != pClientCb)
   1049         {
   1050             /* call the upper check ndef callback */
   1051             pClientCb(pUpperLayerContext,Ndef_Info,RetStatus);
   1052         }
   1053     }
   1054     return;
   1055 }
   1056 /* Check Ndef Timer Callback*/
   1057 STATIC void CheckNdef_timer_cb(uint32_t timer_id, void *pContext)
   1058 {
   1059 	PHNFC_UNUSED_VARIABLE(pContext);
   1060    phOsalNfc_Timer_Stop(timer_id);
   1061 	phOsalNfc_Timer_Delete(gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id);
   1062 	gpphLibContext->ndef_cntx.Chk_Ndef_Timer_Id = 0x00;
   1063 	phLibNfc_Ndef_CheckNdef_Cb((void *)gpphLibContext,NFCSTATUS_MORE_INFORMATION);
   1064 }
   1065 
   1066 void phLibNfc_Reconnect_Mifare_Cb (
   1067                     void                            *pContext,
   1068                     phHal_sRemoteDevInformation_t   *psRemoteDevInfo,
   1069                     NFCSTATUS                       status)
   1070 {
   1071     phLibNfc_ChkNdef_Info_t     Ndef_Info;
   1072     phLibNfc_LibContext_t       *pLibNfc_Ctxt =
   1073                                 (phLibNfc_LibContext_t *)pContext;
   1074     void                        *pUpperLayerContext = NULL;
   1075     switch(gpphLibContext->ndef_cntx.eLast_Call)
   1076     {
   1077         case ChkNdef:
   1078         {
   1079             pphLibNfc_ChkNdefRspCb_t    pClientCb=NULL;
   1080             pClientCb = pLibNfc_Ctxt->CBInfo.pClientCkNdefCb;
   1081             pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientCkNdefCntx;
   1082             pLibNfc_Ctxt->CBInfo.pClientCkNdefCb = NULL;
   1083             pLibNfc_Ctxt->CBInfo.pClientCkNdefCntx = NULL;
   1084             if (NULL != pClientCb)
   1085             {
   1086                 status = (NFCSTATUS_SUCCESS == status?
   1087                         NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
   1088                 Ndef_Info.ActualNdefMsgLength = 0;
   1089                 Ndef_Info.MaxNdefMsgLength = 0;
   1090                 /* call the upper check ndef callback */
   1091                 pClientCb(pUpperLayerContext,Ndef_Info,status);
   1092             }
   1093         }
   1094         break;
   1095         case NdefRd:
   1096         {
   1097             pphLibNfc_RspCb_t       pClientCb = pLibNfc_Ctxt->CBInfo.pClientRdNdefCb;
   1098             pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientRdNdefCntx;
   1099             pLibNfc_Ctxt->CBInfo.pClientRdNdefCb = NULL;
   1100             pLibNfc_Ctxt->CBInfo.pClientRdNdefCntx = NULL;
   1101             if (NULL != pClientCb)
   1102             {
   1103                 status = (NFCSTATUS_SUCCESS == status?
   1104                         NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
   1105                 /* call the upper ndef read callback */
   1106                 pClientCb(pUpperLayerContext,status);
   1107             }
   1108         }
   1109         break;
   1110         case NdefWr:
   1111         {
   1112             pphLibNfc_RspCb_t       pClientCb =  pLibNfc_Ctxt->CBInfo.pClientWrNdefCb;
   1113             pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientWrNdefCntx;
   1114             pLibNfc_Ctxt->CBInfo.pClientWrNdefCb = NULL;
   1115             pLibNfc_Ctxt->CBInfo.pClientWrNdefCntx = NULL;
   1116             if (NULL != pClientCb)
   1117             {
   1118                 status = (NFCSTATUS_SUCCESS == status?
   1119                         NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
   1120                 /* call the upper ndef write callback */
   1121                 pClientCb(pUpperLayerContext,status);
   1122             }
   1123         }
   1124         break;
   1125         case NdefFmt:
   1126         {
   1127             pphLibNfc_RspCb_t       pClientCb =
   1128                            pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCb;
   1129             pUpperLayerContext= pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCntx;
   1130             pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCb = NULL;
   1131             pLibNfc_Ctxt->ndef_cntx.pClientNdefFmtCntx = NULL;
   1132             if (NULL != pClientCb)
   1133             {
   1134                 status = (NFCSTATUS_SUCCESS == status?
   1135                         NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
   1136                 /* call the upper ndef format callback */
   1137                 pClientCb(pUpperLayerContext,status);
   1138             }
   1139         }
   1140         break;
   1141         case RawTrans:
   1142         {
   1143             phNfc_sData_t trans_resp;
   1144             pphLibNfc_TransceiveCallback_t pClientCb =
   1145                            pLibNfc_Ctxt->CBInfo.pClientTransceiveCb;
   1146             trans_resp.length = 0;
   1147             pUpperLayerContext = pLibNfc_Ctxt->CBInfo.pClientTranseCntx;
   1148             pLibNfc_Ctxt->CBInfo.pClientTranseCntx= NULL;
   1149             pLibNfc_Ctxt->CBInfo.pClientTransceiveCb= NULL;
   1150             if (NULL != pClientCb)
   1151             {
   1152                 status = (NFCSTATUS_SUCCESS == status?
   1153                         NFCSTATUS_FAILED:NFCSTATUS_TARGET_LOST);
   1154                 /* call the upper transceive callback */
   1155                 pClientCb(pUpperLayerContext,
   1156                         (uint32_t)psRemoteDevInfo,
   1157                         & trans_resp,
   1158                         status);
   1159             }
   1160         }
   1161         break;
   1162         default:
   1163         {
   1164         }
   1165         break;
   1166     }
   1167 
   1168 }
   1169 /**
   1170 * Target format to make it NDEF compliant
   1171 */
   1172 NFCSTATUS phLibNfc_RemoteDev_FormatNdef(phLibNfc_Handle         hRemoteDevice,
   1173                                         phNfc_sData_t*          pScrtKey,
   1174                                         pphLibNfc_RspCb_t       pNdefformat_RspCb,
   1175                                         void*                   pContext
   1176                                         )
   1177 {
   1178     NFCSTATUS RetVal = NFCSTATUS_FAILED;
   1179 
   1180     static uint8_t       mif_std_key[6] ={0},
   1181                          Index = 0;
   1182     if((NULL == gpphLibContext)
   1183         ||(gpphLibContext->LibNfcState.cur_state
   1184                             == eLibNfcHalStateShutdown))
   1185     {
   1186         /*Lib Nfc not initialized*/
   1187         RetVal = NFCSTATUS_NOT_INITIALISED;
   1188     }
   1189     else if((NULL == pContext)
   1190         || (NULL == pNdefformat_RspCb)
   1191         ||(NULL == pScrtKey)
   1192         ||(0 == hRemoteDevice))
   1193     {
   1194         RetVal= NFCSTATUS_INVALID_PARAMETER;
   1195     }
   1196     else if(gpphLibContext->LibNfcState.next_state
   1197                             == eLibNfcHalStateShutdown)
   1198     {
   1199         RetVal= NFCSTATUS_SHUTDOWN;
   1200     }
   1201     else if(0 == gpphLibContext->Connected_handle)
   1202     {
   1203         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
   1204     }
   1205     else if(hRemoteDevice != gpphLibContext->Connected_handle)
   1206     {
   1207         RetVal=NFCSTATUS_INVALID_HANDLE;
   1208     }
   1209     else if((TRUE == gpphLibContext->status.GenCb_pending_status)||
   1210         (NULL != gpphLibContext->ndef_cntx.pClientNdefFmtCb)
   1211         ||(gpphLibContext->ndef_cntx.is_ndef == TRUE))
   1212     {
   1213         /*Previous Callback is Pending*/
   1214         RetVal = NFCSTATUS_REJECTED;
   1215         PHDBG_INFO("LIbNfc:Previous Callback is Pending");
   1216     }
   1217     else
   1218     {
   1219         uint8_t   fun_id;
   1220         gpphLibContext->ndef_cntx.eLast_Call = NdefFmt;
   1221         gpphLibContext->ndef_cntx.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN;
   1222 
   1223         /* Call ndef format reset, this will initialize the ndef
   1224         format structure, and appropriate values are filled */
   1225         RetVal = phFriNfc_NdefSmtCrd_Reset(gpphLibContext->ndef_cntx.ndef_fmt,
   1226                             gpphLibContext->psOverHalCtxt,
   1227                             (phHal_sRemoteDevInformation_t*)hRemoteDevice,
   1228                             gpphLibContext->psDevInputParam,
   1229                             gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf,
   1230                             &(gpphLibContext->ndef_cntx.NdefSendRecvLen));
   1231         for(fun_id = 0; fun_id < PH_FRINFC_SMTCRDFMT_CR; fun_id++)
   1232         {
   1233             /* Register for all the callbacks */
   1234             RetVal = phFriNfc_NdefSmtCrd_SetCR(gpphLibContext->ndef_cntx.ndef_fmt,
   1235                         fun_id,
   1236                         phLibNfc_Ndef_format_Cb,
   1237                         gpphLibContext);
   1238         }
   1239         /* mif_std_key is required to format the mifare 1k/4k card */
   1240         for (Index =0 ;Index < (pScrtKey->length); Index++ )
   1241         {
   1242             mif_std_key[Index] = *(pScrtKey->buffer++);
   1243         }
   1244         /* Start smart card formatting function   */
   1245         RetVal = phFriNfc_NdefSmtCrd_Format(gpphLibContext->ndef_cntx.ndef_fmt,
   1246                                         mif_std_key);
   1247 		RetVal = PHNFCSTATUS(RetVal);
   1248         if(RetVal== NFCSTATUS_PENDING)
   1249         {
   1250             gpphLibContext->ndef_cntx.pClientNdefFmtCb = pNdefformat_RspCb;
   1251             gpphLibContext->ndef_cntx.pClientNdefFmtCntx = pContext;
   1252             gpphLibContext->status.GenCb_pending_status=TRUE;
   1253             gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
   1254         }
   1255         else
   1256         {
   1257             RetVal = NFCSTATUS_FAILED;
   1258         }
   1259     }
   1260     return RetVal;
   1261 }
   1262 
   1263 /**
   1264 * Response callback for NDEF format.
   1265 */
   1266 STATIC
   1267 void phLibNfc_Ndef_format_Cb(void *Context,NFCSTATUS  status)
   1268 {
   1269     NFCSTATUS RetStatus = NFCSTATUS_SUCCESS;
   1270     pphLibNfc_RspCb_t       pClientCb=NULL;
   1271     phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
   1272     void                    *pUpperLayerContext=NULL;
   1273     phHal_sRemoteDevInformation_t   *ps_rem_dev_info = NULL;
   1274     if(pLibNfc_Ctxt != gpphLibContext)
   1275     {   /*wrong context returned*/
   1276         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
   1277     }
   1278     else
   1279     {
   1280         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
   1281         {
   1282             /*shutdown is pending so issue shutdown*/
   1283             phLibNfc_Pending_Shutdown();
   1284             RetStatus = NFCSTATUS_SHUTDOWN;
   1285         }
   1286         else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
   1287         {
   1288             RetStatus = NFCSTATUS_ABORTED;
   1289         }
   1290         else
   1291         {
   1292             gpphLibContext->status.GenCb_pending_status = FALSE;
   1293             if(NFCSTATUS_SUCCESS == status)
   1294             {
   1295                 RetStatus = NFCSTATUS_SUCCESS;
   1296             }
   1297             else if(PHNFCSTATUS(status)==NFCSTATUS_FAILED)
   1298             {
   1299                 RetStatus = NFCSTATUS_FAILED;
   1300                 ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
   1301                                     gpphLibContext->Connected_handle;
   1302                 if ((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType) &&
   1303                     (0x08 == (ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak & 0x08)))
   1304                 {
   1305 
   1306                     /* card type is mifare 1k/4k, then reconnect */
   1307                     RetStatus = phHal4Nfc_Connect(gpphLibContext->psHwReference,
   1308                                 (phHal_sRemoteDevInformation_t *)
   1309                                 gpphLibContext->Connected_handle,
   1310                                 (pphHal4Nfc_ConnectCallback_t)
   1311                                 phLibNfc_Reconnect_Mifare_Cb,
   1312                                 (void *)gpphLibContext);
   1313                 }
   1314             }
   1315 			else
   1316             {
   1317                 /*Target was removed during transaction*/
   1318                 RetStatus = NFCSTATUS_FAILED;
   1319             }
   1320             gpphLibContext->LibNfcState.cur_state =eLibNfcHalStateConnect;
   1321         }
   1322         phLibNfc_UpdateCurState(status,gpphLibContext);
   1323 
   1324         pClientCb = gpphLibContext->ndef_cntx.pClientNdefFmtCb;
   1325         pUpperLayerContext= gpphLibContext->ndef_cntx.pClientNdefFmtCntx;
   1326         gpphLibContext->ndef_cntx.pClientNdefFmtCb = NULL;
   1327         gpphLibContext->ndef_cntx.pClientNdefFmtCntx = NULL;
   1328         if(NFCSTATUS_PENDING != RetStatus)
   1329         {
   1330             if (NULL != pClientCb)
   1331             {
   1332                 /* Call the tag format upper layer callback */
   1333                 pClientCb(pUpperLayerContext,RetStatus);
   1334             }
   1335         }
   1336     }
   1337     return;
   1338 }
   1339 
   1340 STATIC
   1341 void phLibNfc_Ndef_SrchNdefCnt_Cb(void *context, NFCSTATUS status)
   1342 {
   1343     static NFCSTATUS RegPrSt=FALSE;
   1344     uint8_t RegStatus=0;
   1345     NFCSTATUS RetVal = NFCSTATUS_SUCCESS ;
   1346     uint32_t Index=0;
   1347 
   1348 
   1349 	  PHNFC_UNUSED_VARIABLE(context);
   1350     if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
   1351     {   /*shutdown called before completion of Ndef read allow
   1352               shutdown to happen */
   1353         phLibNfc_Pending_Shutdown();
   1354         RetVal = NFCSTATUS_SHUTDOWN;
   1355     }
   1356     else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
   1357     {
   1358         RetVal = NFCSTATUS_ABORTED;
   1359     }
   1360     else if(NFCSTATUS_SUCCESS != status)
   1361     {
   1362         RetVal = status;
   1363     }
   1364     else
   1365     {
   1366      /* This conditional branch is for QMORE fix */
   1367     }
   1368     gpphLibContext->status.GenCb_pending_status = FALSE;
   1369 
   1370     phLibNfc_UpdateCurState(status,gpphLibContext);
   1371     /* Read is not success send failed to upperlayer Call back*/
   1372     if( RetVal!= NFCSTATUS_SUCCESS )
   1373     {
   1374         if((RetVal!=NFCSTATUS_SHUTDOWN)&& (RetVal!=NFCSTATUS_ABORTED))
   1375         {
   1376             RetVal= NFCSTATUS_FAILED;
   1377         }
   1378         gpphLibContext->CBInfo.pClientNdefNtfRespCb(
   1379                             gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
   1380                             NULL,
   1381                             gpphLibContext->Connected_handle,
   1382                             RetVal);
   1383         gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
   1384         gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
   1385         return;
   1386     }
   1387 
   1388     /*Get the Number of records ( If Raw record parameter is null then API gives number of Records*/
   1389     RetVal = phFriNfc_NdefRecord_GetRecords(
   1390                             gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
   1391                             gpphLibContext->phLib_NdefRecCntx.ndef_message.length,
   1392                             NULL,
   1393                             gpphLibContext->phLib_NdefRecCntx.IsChunked,
   1394                             &(gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords));
   1395 
   1396     NdefInfo.pNdefMessage = gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer;
   1397     NdefInfo.NdefMessageLengthActual = gpphLibContext->ndef_cntx.NdefActualSize;
   1398     NdefInfo.NdefMessageLengthMaximum = gpphLibContext->ndef_cntx.NdefLength;
   1399     NdefInfo.NdefRecordCount =0;
   1400 
   1401     /*Allocate memory to hold the records Read*/
   1402     NdefInfo.pNdefRecord = phOsalNfc_GetMemory
   1403         (sizeof(phFriNfc_NdefRecord_t)* gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords );
   1404     if(NULL==NdefInfo.pNdefRecord)
   1405     {
   1406         gpphLibContext->CBInfo.pClientNdefNtfRespCb(
   1407                             gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
   1408                             NULL,
   1409                             gpphLibContext->Connected_handle,
   1410                             NFCSTATUS_FAILED);
   1411         gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
   1412         gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
   1413         return;
   1414     }
   1415 
   1416     pNdefRecord=NdefInfo.pNdefRecord;
   1417     /*If phLibNfc_Ndef_SearchNdefContent Reg type is NULL return all the Records*/
   1418     if(gpphLibContext->ndef_cntx.pNdef_NtfSrch_Type==NULL)
   1419     {
   1420         RetVal = phFriNfc_NdefRecord_GetRecords(
   1421                         gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
   1422                         gpphLibContext->phLib_NdefRecCntx.ndef_message.length,
   1423                         gpphLibContext->phLib_NdefRecCntx.RawRecords,
   1424                         gpphLibContext->phLib_NdefRecCntx.IsChunked,
   1425                         &(gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords));
   1426 
   1427         for (Index = 0; Index < gpphLibContext->phLib_NdefRecCntx.NumberOfRawRecords; Index++)
   1428         {
   1429             RetVal = phFriNfc_NdefRecord_Parse(
   1430                         pNdefRecord,
   1431                         gpphLibContext->phLib_NdefRecCntx.RawRecords[Index]);
   1432             pNdefRecord++;
   1433             NdefInfo.NdefRecordCount++;
   1434         }
   1435     }
   1436     else
   1437     {
   1438 
   1439         /* Look for registerd TNF */
   1440         RetVal = phFriNfc_NdefReg_DispatchPacket(
   1441                     &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
   1442                     gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
   1443                     (uint16_t)gpphLibContext->phLib_NdefRecCntx.ndef_message.length);
   1444         if(NFCSTATUS_SUCCESS != RetVal)
   1445         {
   1446             /*phFriNfc_NdefReg_DispatchPacket is failed call upper layer*/
   1447             gpphLibContext->CBInfo.pClientNdefNtfRespCb(gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
   1448                                                     NULL,gpphLibContext->Connected_handle,NFCSTATUS_FAILED);
   1449             gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
   1450             gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
   1451             return;
   1452         }
   1453 
   1454         while(1 != RegStatus)
   1455         {
   1456             /* Process the NDEF records, If match FOUND we will get Call back*/
   1457             RegStatus = phFriNfc_NdefReg_Process(   &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
   1458                                                 &RegPrSt);
   1459             if(RegPrSt == TRUE)
   1460             {
   1461                 /*  Processing Done */
   1462                 break;
   1463             }
   1464             /*If match found the CbParam will be updated by lower layer, copy the record info*/
   1465             for(Index=0;Index<gpphLibContext->phLib_NdefRecCntx.CbParam.Count;Index++)
   1466             {
   1467                 pNdefRecord->Tnf  = gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Tnf;
   1468                 pNdefRecord->TypeLength  =
   1469                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].TypeLength;
   1470                 pNdefRecord->PayloadLength  =
   1471                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].PayloadLength;
   1472                 pNdefRecord->IdLength  =
   1473                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].IdLength;
   1474                 pNdefRecord->Flags =
   1475                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Flags;
   1476 
   1477                 pNdefRecord->Id = phOsalNfc_GetMemory(pNdefRecord->IdLength);
   1478                 pNdefRecord->Type = phOsalNfc_GetMemory(pNdefRecord->TypeLength);
   1479                 pNdefRecord->PayloadData = phOsalNfc_GetMemory(pNdefRecord->PayloadLength);
   1480 
   1481                 (void)memcpy(pNdefRecord->Id,
   1482                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Id,
   1483                     pNdefRecord->IdLength);
   1484                 (void)memcpy(pNdefRecord->PayloadData,
   1485                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].PayloadData,
   1486                     pNdefRecord->PayloadLength);
   1487                 (void)memcpy(pNdefRecord->Type,
   1488                     gpphLibContext->phLib_NdefRecCntx.CbParam.Records[Index].Type,
   1489                     pNdefRecord->TypeLength);
   1490 
   1491                 pNdefRecord++;
   1492                 NdefInfo.NdefRecordCount++;
   1493             }
   1494         }
   1495     }
   1496     /* If no record found call upper layer with failed status*/
   1497     if(pNdefRecord == NdefInfo.pNdefRecord)
   1498     {
   1499         NdefInfo.NdefRecordCount =0;
   1500         gpphLibContext->CBInfo.pClientNdefNtfRespCb(
   1501                     gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
   1502                     &NdefInfo,gpphLibContext->Connected_handle,
   1503                     NFCSTATUS_SUCCESS);
   1504 
   1505     }
   1506     else
   1507     {
   1508         /*Call upperlayer Call back with match records*/
   1509 
   1510         gpphLibContext->CBInfo.pClientNdefNtfRespCb(
   1511                     gpphLibContext->CBInfo.pClientNdefNtfRespCntx,
   1512                     &NdefInfo,gpphLibContext->Connected_handle,
   1513                     NFCSTATUS_SUCCESS);
   1514         /*Remove entry from FRI*/
   1515         RetVal = phFriNfc_NdefReg_RmCb(
   1516                     &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
   1517                     gpphLibContext->phLib_NdefRecCntx.NdefCb );
   1518         /*Free the memory*/
   1519         if(gpphLibContext->ndef_cntx.pNdef_NtfSrch_Type!=NULL)
   1520         {
   1521             pNdefRecord=NdefInfo.pNdefRecord;
   1522             for(Index=0;Index<gpphLibContext->phLib_NdefRecCntx.CbParam.Count;Index++)
   1523             {
   1524                 phOsalNfc_FreeMemory(pNdefRecord->Id);
   1525                 phOsalNfc_FreeMemory(pNdefRecord->PayloadData);
   1526                 phOsalNfc_FreeMemory(pNdefRecord->Type);
   1527                 pNdefRecord++;
   1528             }
   1529         }
   1530     }
   1531 
   1532     gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
   1533     gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
   1534 
   1535 }
   1536 
   1537 STATIC
   1538 void phLibNfc_Ndef_Rtd_Cb( void *CallBackParam)
   1539 {
   1540     /*There will be single call back given to all match
   1541       It's processed in phLibNfc_Ndef_SrchNdefCnt_Cb*/
   1542     PHNFC_UNUSED_VARIABLE(CallBackParam);
   1543 }
   1544 
   1545 NFCSTATUS phLibNfc_Ndef_SearchNdefContent(
   1546                                 phLibNfc_Handle                 hRemoteDevice,
   1547                                 phLibNfc_Ndef_SrchType_t*       psSrchTypeList,
   1548                                 uint8_t                         uNoSrchRecords,
   1549                                 pphLibNfc_Ndef_Search_RspCb_t   pNdefNtfRspCb,
   1550                                 void *                          pContext
   1551                                 )
   1552 {
   1553 
   1554      NFCSTATUS  RetVal =NFCSTATUS_SUCCESS;
   1555      uint32_t Index=0;
   1556      uint8_t     cr_index = 0;
   1557 
   1558 
   1559       if((NULL == gpphLibContext) ||
   1560         (gpphLibContext->LibNfcState.cur_state
   1561                             == eLibNfcHalStateShutdown))
   1562       {
   1563          RetVal = NFCSTATUS_NOT_INITIALISED;
   1564       }
   1565      /* Check the state for DeInit is called or not,if yes return NFCSTATUS_SHUTDOWN*/
   1566       else if(gpphLibContext->LibNfcState.next_state
   1567                             == eLibNfcHalStateShutdown)
   1568      {
   1569         RetVal= NFCSTATUS_SHUTDOWN;
   1570      }
   1571      else if( (NULL == pNdefNtfRspCb) ||
   1572         (NULL == pContext ) ||
   1573         (0 == hRemoteDevice))
   1574      {
   1575         RetVal= NFCSTATUS_INVALID_PARAMETER;
   1576      }
   1577      else if( (NULL != psSrchTypeList) && (0==uNoSrchRecords))
   1578      {
   1579         RetVal= NFCSTATUS_INVALID_PARAMETER;
   1580      }
   1581      else if(0 == gpphLibContext->Connected_handle)
   1582      {   /*presently no target or tag is connected*/
   1583         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
   1584      }
   1585      else if(hRemoteDevice != gpphLibContext->Connected_handle)
   1586      {   /*This handle of the device sent by application is not connected */
   1587         RetVal=NFCSTATUS_INVALID_HANDLE;
   1588      }
   1589      else if((TRUE == gpphLibContext->status.GenCb_pending_status)
   1590         ||(NULL!=gpphLibContext->CBInfo.pClientNdefNtfRespCb))
   1591      {
   1592         /*Previous callback is pending*/
   1593         RetVal = NFCSTATUS_REJECTED;
   1594      }
   1595      else
   1596      {
   1597         gpphLibContext->ndef_cntx.pNdef_NtfSrch_Type = psSrchTypeList;
   1598 
   1599         if(psSrchTypeList!=NULL)
   1600         {
   1601             /*Maximum records supported*/
   1602             gpphLibContext->phLib_NdefRecCntx.NumberOfRecords = 255;
   1603             /*Reset the FRI component to add the Reg type*/
   1604             RetVal = phFriNfc_NdefReg_Reset(
   1605                             &(gpphLibContext->phLib_NdefRecCntx.NdefReg),
   1606                             gpphLibContext->phLib_NdefRecCntx.NdefTypes_array,
   1607                             &(gpphLibContext->phLib_NdefRecCntx.RecordsExtracted),
   1608                             &(gpphLibContext->phLib_NdefRecCntx.CbParam),
   1609                             gpphLibContext->phLib_NdefRecCntx.ChunkedRecordsarray,
   1610                             gpphLibContext->phLib_NdefRecCntx.NumberOfRecords);
   1611 
   1612             gpphLibContext->phLib_NdefRecCntx.NdefCb = phOsalNfc_GetMemory(sizeof(phFriNfc_NdefReg_Cb_t));
   1613             if(gpphLibContext->phLib_NdefRecCntx.NdefCb==NULL)
   1614             {
   1615                 /*exception: Not enough memory*/
   1616                 phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,1);
   1617             }
   1618             gpphLibContext->phLib_NdefRecCntx.NdefCb->NdefCallback = phLibNfc_Ndef_Rtd_Cb;
   1619             /*Copy the TNF types to search in global structure*/
   1620             gpphLibContext->phLib_NdefRecCntx.NdefCb->NumberOfRTDs = uNoSrchRecords;
   1621             for(Index=0;Index<uNoSrchRecords;Index++)
   1622             {
   1623                 gpphLibContext->phLib_NdefRecCntx.NdefCb->NdefType[Index] = psSrchTypeList->Type;
   1624                 gpphLibContext->phLib_NdefRecCntx.NdefCb->Tnf[Index] = psSrchTypeList->Tnf ;
   1625                 gpphLibContext->phLib_NdefRecCntx.NdefCb->NdeftypeLength[Index] = psSrchTypeList->TypeLength;
   1626                 psSrchTypeList++;
   1627             }
   1628             /* Add the TNF type to FRI component*/
   1629 
   1630             RetVal = phFriNfc_NdefReg_AddCb(&(gpphLibContext->phLib_NdefRecCntx.NdefReg),
   1631                                                 gpphLibContext->phLib_NdefRecCntx.NdefCb );
   1632 
   1633         }
   1634         gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer =
   1635             phOsalNfc_GetMemory(gpphLibContext->ndef_cntx.NdefActualSize);
   1636         gpphLibContext->phLib_NdefRecCntx.ndef_message.length =
   1637             gpphLibContext->ndef_cntx.NdefActualSize;
   1638         /*Set Complete routine for NDEF Read*/
   1639         for (cr_index = 0; cr_index < PH_FRINFC_NDEFMAP_CR; cr_index++)
   1640         {
   1641             RetVal= phFriNfc_NdefMap_SetCompletionRoutine(
   1642                                 gpphLibContext->ndef_cntx.psNdefMap,
   1643                                 cr_index,
   1644                                 phLibNfc_Ndef_SrchNdefCnt_Cb,
   1645                                 (void *)gpphLibContext);
   1646 
   1647         }
   1648         gpphLibContext->ndef_cntx.NdefContinueRead = PH_FRINFC_NDEFMAP_SEEK_BEGIN;
   1649         /* call below layer Ndef Read*/
   1650         RetVal = phFriNfc_NdefMap_RdNdef(gpphLibContext->ndef_cntx.psNdefMap,
   1651                         gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer,
   1652                         (uint32_t*)&gpphLibContext->phLib_NdefRecCntx.ndef_message.length,
   1653                         PH_FRINFC_NDEFMAP_SEEK_BEGIN);
   1654 
   1655         if(NFCSTATUS_PENDING == RetVal)
   1656         {
   1657             gpphLibContext->CBInfo.pClientNdefNtfRespCb = pNdefNtfRspCb;
   1658             gpphLibContext->CBInfo.pClientNdefNtfRespCntx = pContext;
   1659             gpphLibContext->status.GenCb_pending_status=TRUE;
   1660             gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
   1661         }
   1662         else if (NFCSTATUS_SUCCESS == RetVal)
   1663         {
   1664             RetVal= NFCSTATUS_SUCCESS;
   1665         }
   1666         else
   1667         {
   1668             /*Ndef read failed*/
   1669             RetVal = NFCSTATUS_FAILED;
   1670         }
   1671     }
   1672     return RetVal;
   1673 
   1674 }
   1675 
   1676