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.c
     19 
     20  * Project: NFC FRI / HALDL
     21  *
     22  * $Date: Tue Jun  1 14:53:48 2010 $
     23  * $Author: ing07385 $
     24  * $Revision: 1.89 $
     25  * $Aliases: NFC_FRI1.1_WK1024_SDK $
     26  *
     27  */
     28 
     29 
     30 /*
     31 ************************* Header Files ****************************************
     32 */
     33 
     34 #include <phLibNfc.h>
     35 #include <phDal4Nfc.h>
     36 #include <phHal4Nfc.h>
     37 #include <phOsalNfc.h>
     38 #include <phLibNfc_Internal.h>
     39 #include <phLibNfc_ndef_raw.h>
     40 #include <phLibNfc_initiator.h>
     41 #include <phLibNfc_discovery.h>
     42 #include <phNfcStatus.h>
     43 #include <cutils/log.h>
     44 /*
     45 *************************** Macro's  ******************************************
     46 */
     47 
     48 extern int dlopen_firmware();
     49 
     50 #ifndef STATIC_DISABLE
     51 #define STATIC static
     52 #else
     53 #define STATIC
     54 #endif
     55 
     56 
     57 /*
     58 *************************** Global Variables **********************************
     59 */
     60 
     61 
     62 pphLibNfc_LibContext_t gpphLibContext=NULL;
     63 
     64 /*
     65 *************************** Static Function Declaration ***********************
     66 */
     67 
     68 /* Init callback */
     69 STATIC void phLibNfc_InitCb(void *pContext,NFCSTATUS status);
     70 
     71 /* Shutdown callback */
     72 STATIC void phLibNfc_ShutdownCb(void *pContext,NFCSTATUS status);
     73 
     74 /**Default notification handler registered with lower layer immediately after
     75    successful initialization*/
     76 STATIC void phLibNfc_DefaultHandler(
     77                                 void                        *context,
     78                                 phHal_eNotificationType_t    type,
     79                                 phHal4Nfc_NotificationInfo_t info,
     80                                 NFCSTATUS                    status
     81                                 );
     82 /*
     83 *************************** Function Definitions ******************************
     84 */
     85 
     86 NFCSTATUS phLibNfc_Mgt_ConfigureDriver (pphLibNfc_sConfig_t     psConfig,
     87                                         void **                 ppDriverHandle)
     88 {
     89     if(NULL != gpphLibContext)
     90     {
     91         return NFCSTATUS_ALREADY_INITIALISED;
     92     }
     93 
     94     return phDal4Nfc_Config(psConfig, ppDriverHandle);
     95 }
     96 
     97 NFCSTATUS phLibNfc_Mgt_UnConfigureDriver (void *                 pDriverHandle)
     98 {
     99     if(NULL != gpphLibContext)
    100     {
    101         return NFCSTATUS_ALREADY_INITIALISED;
    102     }
    103 
    104    return phDal4Nfc_ConfigRelease(pDriverHandle);
    105 }
    106 
    107 NFCSTATUS phLibNfc_HW_Reset ()
    108 {
    109     NFCSTATUS Status = NFCSTATUS_SUCCESS;
    110 
    111     Status = phDal4Nfc_Reset(1);
    112     Status = phDal4Nfc_Reset(0);
    113     Status = phDal4Nfc_Reset(1);
    114 
    115     return Status;
    116 }
    117 
    118 NFCSTATUS phLibNfc_Download_Mode ()
    119 {
    120    return phDal4Nfc_Download();
    121 }
    122 
    123 int phLibNfc_Load_Firmware_Image ()
    124 {
    125     int status;
    126     status = dlopen_firmware();
    127     return status;
    128 }
    129 
    130 
    131 extern uint8_t nxp_nfc_isoxchg_timeout;
    132 NFCSTATUS phLibNfc_SetIsoXchgTimeout(uint8_t timeout) {
    133     nxp_nfc_isoxchg_timeout = timeout;
    134     return NFCSTATUS_SUCCESS;
    135 }
    136 
    137 extern uint32_t nxp_nfc_hci_response_timeout;
    138 NFCSTATUS phLibNfc_SetHciTimeout(uint32_t timeout_in_ms) {
    139     nxp_nfc_hci_response_timeout = timeout_in_ms;
    140     return NFCSTATUS_SUCCESS;
    141 }
    142 
    143 /**
    144 *    Initialize the phLibNfc interface.
    145 */
    146 
    147 NFCSTATUS phLibNfc_Mgt_Initialize(void                *pDriverHandle,
    148                                  pphLibNfc_RspCb_t    pInitCb,
    149                                  void                 *pContext)
    150 {
    151      NFCSTATUS Status = NFCSTATUS_SUCCESS;
    152      if((NULL == pDriverHandle)||(NULL == pInitCb))
    153      {
    154         Status = NFCSTATUS_INVALID_PARAMETER;
    155      }
    156      else if(NULL == gpphLibContext)
    157      {
    158         /* Initialize the Lib context */
    159         gpphLibContext=(pphLibNfc_LibContext_t)phOsalNfc_GetMemory(
    160                                         (uint32_t)sizeof(phLibNfc_LibContext_t));
    161         if(NULL == gpphLibContext)
    162         {
    163             Status=NFCSTATUS_INSUFFICIENT_RESOURCES;
    164         }
    165         else
    166         {
    167             (void)memset((void *)gpphLibContext,0,(
    168                                     (uint32_t)sizeof(phLibNfc_LibContext_t)));
    169 
    170             /* Store the Callback and context in LibContext structure*/
    171             gpphLibContext->CBInfo.pClientInitCb=pInitCb;
    172             gpphLibContext->CBInfo.pClientInitCntx=pContext;
    173             /* Initialize the HwReferece structure */
    174             gpphLibContext->psHwReference=(phHal_sHwReference_t *)
    175                                     phOsalNfc_GetMemory((uint32_t)sizeof(phHal_sHwReference_t));
    176             (void)memset((void *)gpphLibContext->psHwReference,0,
    177                                         ((uint32_t)sizeof(phHal_sHwReference_t)));
    178             /* Allocate the Memory for the Transceive info */
    179             if( gpphLibContext->psHwReference!=NULL)
    180             {
    181                 gpphLibContext->psHwReference->p_board_driver = pDriverHandle;
    182                 Status = phLibNfc_UpdateNextState(gpphLibContext,
    183                                             eLibNfcHalStateInitandIdle);
    184                 if(Status==NFCSTATUS_SUCCESS)
    185                 {
    186                     Status=phHal4Nfc_Open(
    187                                     gpphLibContext->psHwReference,
    188                                     eInitDefault,
    189                                     phLibNfc_InitCb,
    190                                     (void *)gpphLibContext);
    191                 }
    192             }
    193             else
    194             {
    195                 Status = NFCSTATUS_INSUFFICIENT_RESOURCES;
    196             }
    197             phLibNfc_Ndef_Init();
    198         }
    199     }
    200     else if(gpphLibContext->LibNfcState.next_state==eLibNfcHalStateShutdown)
    201     {
    202         Status = NFCSTATUS_SHUTDOWN;
    203     }
    204     else
    205     {
    206         Status=NFCSTATUS_ALREADY_INITIALISED;
    207     }
    208    return Status;
    209 }
    210 
    211 /*
    212  * This function called by the HAL4 when the initialization seq is completed.
    213  */
    214 STATIC void phLibNfc_InitCb(void *pContext,NFCSTATUS status)
    215 {
    216     pphLibNfc_LibContext_t   pLibContext=NULL;
    217     pphLibNfc_RspCb_t          pClientCb=NULL;
    218     void                        *pUpperLayerContext=NULL;
    219 
    220 
    221     /* Initialize the local variable */
    222     pLibContext  = (pphLibNfc_LibContext_t)pContext;
    223 
    224     pClientCb =pLibContext->CBInfo.pClientInitCb;
    225     pUpperLayerContext=pLibContext->CBInfo.pClientInitCntx;
    226     if(status == NFCSTATUS_SUCCESS)
    227     {
    228         /* Get the Lib context */
    229         pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;
    230         gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeOff;
    231         if(pLibContext->psHwReference->uicc_connected==TRUE)
    232         {
    233             /* populate state of the secured element */
    234             gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeDefault;
    235             sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_CurrentState=phLibNfc_SE_Active;
    236             pLibContext->sSeContext.uUiccActivate=TRUE;
    237         }
    238         if(pLibContext->psHwReference->smx_connected==TRUE)
    239         {
    240             /* populate state of the secured element */
    241             gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeDefault;
    242             sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_CurrentState=phLibNfc_SE_Inactive;
    243             pLibContext->sSeContext.uSmxActivate =FALSE;
    244         }
    245 
    246         phLibNfc_UpdateCurState(status,pLibContext);
    247         (void)phHal4Nfc_RegisterNotification(
    248                                 pLibContext->psHwReference,
    249                                 eRegisterDefault,
    250                                 phLibNfc_DefaultHandler,
    251                                 (void*)pLibContext
    252                                 );
    253         /* call the upper layer register function */
    254         (*pClientCb)(pUpperLayerContext,status);
    255 
    256     }
    257     else
    258     {
    259         /*Change the status code failed*/
    260         status = NFCSTATUS_FAILED;
    261         /* Get the Lib context */
    262         pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;
    263 
    264         phLibNfc_UpdateCurState(status,pLibContext);
    265 
    266 
    267 
    268         /* Allocate the Memory for the Transceive info */
    269         if(pLibContext->psHwReference!= NULL)
    270         {
    271             phOsalNfc_FreeMemory(pLibContext->psHwReference);
    272             pLibContext->psHwReference = NULL;
    273         }
    274         (*pClientCb)(pUpperLayerContext, status);
    275 
    276         phOsalNfc_FreeMemory(pLibContext);
    277         pLibContext= NULL;
    278         gpphLibContext = NULL;
    279 
    280     }
    281     return;
    282 }
    283 
    284 /**Default notification handler registered with lower layer immediately after
    285    successful initialization*/
    286 STATIC void phLibNfc_DefaultHandler(
    287                                 void                        *context,
    288                                 phHal_eNotificationType_t    type,
    289                                 phHal4Nfc_NotificationInfo_t info,
    290                                 NFCSTATUS                    status
    291                                 )
    292 {
    293     if(context != (void *)gpphLibContext)
    294     {
    295         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    296     }
    297     else
    298     {
    299         info = info;
    300         if((NFC_EVENT_NOTIFICATION == type) &&
    301             (NFCSTATUS_BOARD_COMMUNICATION_ERROR == status))
    302         {
    303             phLibNfc_UpdateCurState(NFCSTATUS_FAILED,gpphLibContext);
    304             phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1);
    305         }
    306     }
    307     return;
    308 }
    309 /**
    310 * De-Initialize the LIB NFC.
    311 */
    312 NFCSTATUS phLibNfc_Mgt_DeInitialize(void *                      pDriverHandle,
    313                                    pphLibNfc_RspCb_t            pDeInitCb,
    314                                    void*                        pContext
    315                                    )
    316 {
    317     NFCSTATUS Status = NFCSTATUS_SUCCESS;
    318     pphLibNfc_LibContext_t pLibContext = gpphLibContext;
    319     if(NULL==pDriverHandle)
    320     {
    321         /*Check for valid parameters */
    322         Status = NFCSTATUS_INVALID_PARAMETER;
    323     }
    324     else if((pLibContext==NULL)
    325         || (pLibContext->LibNfcState.cur_state
    326             == eLibNfcHalStateShutdown))
    327     {   /*Lib Nfc not initlized*/
    328         Status = NFCSTATUS_NOT_INITIALISED;
    329     }
    330     else
    331     {
    332         if(pDeInitCb==NULL)
    333         {
    334             phHal4Nfc_Hal4Reset(pLibContext->psHwReference,(void *)pLibContext);
    335             if(pLibContext->psHwReference!=NULL)
    336             {
    337                 phOsalNfc_FreeMemory(pLibContext->psHwReference);
    338                 pLibContext->psHwReference = NULL;
    339             }
    340             /*Free the memory allocated during NDEF read,write
    341               and NDEF formatting*/
    342             phLibNfc_Ndef_DeInit();
    343             phOsalNfc_FreeMemory(pLibContext);
    344             gpphLibContext=NULL;
    345             pLibContext= NULL;
    346         }
    347         else
    348         {
    349             if (NULL!= pLibContext->CBInfo.pClientShutdownCb)
    350             {
    351                 /* Previous callback pending */
    352                 Status = NFCSTATUS_BUSY;
    353             }
    354           Status = NFCSTATUS_PENDING;
    355           if(TRUE != pLibContext->status.GenCb_pending_status)
    356           {
    357               Status = phHal4Nfc_Close(pLibContext->psHwReference,
    358                                   phLibNfc_ShutdownCb,
    359                                   (void *)pLibContext);
    360           }
    361           if(Status== NFCSTATUS_PENDING)
    362           {
    363               pLibContext->CBInfo.pClientShutdownCb = pDeInitCb;
    364               pLibContext->CBInfo.pClientShtdwnCntx = pContext;
    365               pLibContext->status.GenCb_pending_status=TRUE;
    366               pLibContext->LibNfcState.next_state= eLibNfcHalStateShutdown;
    367           }
    368           else
    369           {
    370               Status =NFCSTATUS_FAILED;
    371           }
    372         }
    373     }
    374     return Status;
    375 }
    376 /* shutdown callback -
    377   Free the allocated memory here */
    378 STATIC void phLibNfc_ShutdownCb(void *pContext,NFCSTATUS status)
    379 {
    380     pphLibNfc_RspCb_t           pClientCb=NULL;
    381     void                        *pUpperLayerContext=NULL;
    382     pphLibNfc_LibContext_t      pLibContext=NULL;
    383 
    384     PHNFC_UNUSED_VARIABLE(pContext);
    385     /* Get the Lib context */
    386     pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;
    387 
    388     if(pLibContext == NULL)
    389     {
    390         status = NFCSTATUS_FAILED;
    391     }
    392     else
    393     {
    394         /* Initialize the local variable */
    395         pClientCb =pLibContext->CBInfo.pClientShutdownCb;
    396         pUpperLayerContext=pLibContext->CBInfo.pClientShtdwnCntx;
    397         if(status == NFCSTATUS_SUCCESS)
    398         {
    399             pLibContext->LibNfcState.cur_state = eLibNfcHalStateShutdown;
    400             phLibNfc_UpdateCurState(status,pLibContext);
    401 
    402             pLibContext->status.GenCb_pending_status=FALSE;
    403 
    404             /* Allocate the Memory for the Transceive info */
    405             if(pClientCb!=NULL)
    406             {
    407                 (*pClientCb)(pUpperLayerContext, status);
    408             }
    409             if(pLibContext->psHwReference!=NULL)
    410             {
    411                 phOsalNfc_FreeMemory(pLibContext->psHwReference);
    412                 pLibContext->psHwReference = NULL;
    413             }
    414             if(NULL != gpphLibContext->psBufferedAuth)
    415             {
    416                 if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
    417                 {
    418                     phOsalNfc_FreeMemory(
    419                         gpphLibContext->psBufferedAuth->sRecvData.buffer);
    420                 }
    421                 if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
    422                 {
    423                     phOsalNfc_FreeMemory(
    424                         gpphLibContext->psBufferedAuth->sSendData.buffer);
    425                 }
    426                 phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
    427                 gpphLibContext->psBufferedAuth = NULL;
    428             }
    429             /*Free the memory allocated during NDEF read,write
    430               and NDEF formatting*/
    431             phLibNfc_Ndef_DeInit();
    432                 phOsalNfc_FreeMemory(pLibContext);
    433                 gpphLibContext=NULL;
    434                 pLibContext= NULL;
    435 
    436         }
    437         else
    438         {
    439             /* shutdown sequence failed by HAL 4 */
    440             status= NFCSTATUS_FAILED;
    441             pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;
    442             phLibNfc_UpdateCurState(status,pLibContext);
    443             pLibContext->status.GenCb_pending_status=FALSE;
    444             if(pClientCb!=NULL)
    445             {
    446                 (*pClientCb)(pUpperLayerContext,status);
    447             }
    448         }
    449     }
    450 }
    451 /**
    452 *    Pending shutdown call.
    453 */
    454 
    455 
    456 void phLibNfc_Pending_Shutdown(void)
    457 {
    458     NFCSTATUS RetStatus = NFCSTATUS_SUCCESS ;
    459     gpphLibContext->status.GenCb_pending_status = FALSE;
    460     RetStatus = phHal4Nfc_Close(
    461                         gpphLibContext->psHwReference,
    462                         phLibNfc_ShutdownCb,
    463                         (void *)gpphLibContext);
    464     PHNFC_UNUSED_VARIABLE(RetStatus);
    465     return;
    466 }
    467 
    468 
    469 /**
    470 * Reset the LIB NFC.
    471 */
    472 NFCSTATUS phLibNfc_Mgt_Reset(void  *pContext)
    473 {
    474     NFCSTATUS Status = NFCSTATUS_SUCCESS;
    475     phLibNfc_LibContext_t   *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)pContext;
    476 
    477     if((pLibNfc_Ctxt == NULL)
    478         || (gpphLibContext->LibNfcState.cur_state
    479             == eLibNfcHalStateShutdown))
    480     {   /*Lib Nfc not initlized*/
    481         Status = NFCSTATUS_NOT_INITIALISED;
    482     }
    483     else if(NULL == pContext)
    484     {
    485         Status = NFCSTATUS_INVALID_PARAMETER;
    486     }
    487     /* Check for valid state,If De initialize is called then
    488     return NFCSTATUS_SHUTDOWN */
    489     else if(gpphLibContext->LibNfcState.next_state
    490                             == eLibNfcHalStateShutdown)
    491     {
    492         Status = NFCSTATUS_SHUTDOWN;
    493     }
    494     else
    495     {
    496         /*Reset all callback status*/
    497         (void) memset(&(gpphLibContext->RegNtfType),0,
    498                         sizeof(phLibNfc_Registry_Info_t));
    499         (void) memset(&(gpphLibContext->sADDconfig),0,
    500                         sizeof(phLibNfc_sADD_Cfg_t));
    501         (void) memset(&(gpphLibContext->ndef_cntx),0,
    502                         sizeof(phLibNfc_NdefInfo_t));
    503         (void) memset(&(gpphLibContext->sNfcIp_Context),0,
    504                         sizeof(phLibNfc_NfcIpInfo_t));
    505         (void) memset(&(gpphLibContext->sCardEmulCfg),0,
    506                         sizeof(phHal_sEmulationCfg_t));
    507         (void) memset(&(gpphLibContext->Discov_handle),0,
    508                         MAX_REMOTE_DEVICES);
    509 
    510         /*Free memory allocated for NDEF records*/
    511         if(NULL != gpphLibContext->psBufferedAuth)
    512         {
    513             if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
    514             {
    515                 phOsalNfc_FreeMemory(
    516                     gpphLibContext->psBufferedAuth->sRecvData.buffer);
    517                 gpphLibContext->psBufferedAuth->sRecvData.buffer = NULL;
    518             }
    519             if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
    520             {
    521                 phOsalNfc_FreeMemory(
    522                     gpphLibContext->psBufferedAuth->sSendData.buffer);
    523                 gpphLibContext->psBufferedAuth->sSendData.buffer = NULL;
    524             }
    525             phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
    526             gpphLibContext->psBufferedAuth = NULL;
    527         }
    528         if(NULL != gpphLibContext->psTransInfo)
    529         {
    530             phOsalNfc_FreeMemory(gpphLibContext->psTransInfo);
    531             gpphLibContext->psTransInfo = NULL;
    532         }
    533         if(NULL != gpphLibContext->ndef_cntx.psNdefMap)
    534         {
    535             if(NULL != gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf)
    536             {
    537                 phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf);
    538                 gpphLibContext->ndef_cntx.psNdefMap->SendRecvBuf = NULL;
    539             }
    540             phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.psNdefMap);
    541             gpphLibContext->ndef_cntx.psNdefMap = NULL;
    542         }
    543         if(NULL != gpphLibContext->psOverHalCtxt)
    544         {
    545             phOsalNfc_FreeMemory(gpphLibContext->psOverHalCtxt);
    546             gpphLibContext->psTransInfo = NULL;
    547         }
    548         if(NULL != gpphLibContext->psDevInputParam)
    549         {
    550             phOsalNfc_FreeMemory(gpphLibContext->psDevInputParam);
    551             gpphLibContext->psDevInputParam = NULL;
    552         }
    553         if(NULL != gpphLibContext->ndef_cntx.ndef_fmt)
    554         {
    555             phOsalNfc_FreeMemory(gpphLibContext->ndef_cntx.ndef_fmt);
    556             gpphLibContext->ndef_cntx.ndef_fmt = NULL;
    557         }
    558         if(NULL != pNdefRecord)
    559         {
    560             if(NULL != pNdefRecord->Id)
    561             {
    562                 phOsalNfc_FreeMemory(pNdefRecord->Id);
    563                 pNdefRecord->Id = NULL;
    564             }
    565             if(NULL != pNdefRecord->Type)
    566             {
    567                 phOsalNfc_FreeMemory(pNdefRecord->Type);
    568                 pNdefRecord->Type = NULL;
    569             }
    570             if(NULL != pNdefRecord->PayloadData)
    571             {
    572                 phOsalNfc_FreeMemory(pNdefRecord->PayloadData);
    573                 pNdefRecord->PayloadData = NULL;
    574             }
    575         }
    576         if(NULL != NdefInfo.pNdefRecord)
    577         {
    578             phOsalNfc_FreeMemory(NdefInfo.pNdefRecord);
    579             NdefInfo.pNdefRecord = NULL;
    580         }
    581         if(NULL != gpphLibContext->phLib_NdefRecCntx.NdefCb)
    582         {
    583             phOsalNfc_FreeMemory(gpphLibContext->phLib_NdefRecCntx.NdefCb);
    584             gpphLibContext->phLib_NdefRecCntx.NdefCb = NULL;
    585         }
    586         if(NULL != gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer)
    587         {
    588             phOsalNfc_FreeMemory(gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer);
    589             gpphLibContext->phLib_NdefRecCntx.ndef_message.buffer = NULL;
    590         }
    591         /* No device is connected */
    592         gpphLibContext->Connected_handle = 0x00;
    593         gpphLibContext->Prev_Connected_handle = 0x00;
    594         gpphLibContext->ReleaseType = NFC_INVALID_RELEASE_TYPE;
    595         gpphLibContext->eLibNfcCfgMode = NFC_DISCOVERY_STOP;
    596         /*Lib Nfc Stack is initilized and in idle state*/
    597         gpphLibContext->LibNfcState.cur_state = eLibNfcHalStateInitandIdle;
    598 
    599         /* Reset all callback status */
    600         gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
    601         gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
    602         gpphLibContext->CBInfo.pClientConCntx = NULL;
    603         gpphLibContext->CBInfo.pClientConnectCb = NULL;
    604         gpphLibContext->CBInfo.pClientDConCntx = NULL;
    605         gpphLibContext->CBInfo.pClientDisCfgCntx = NULL;
    606         gpphLibContext->CBInfo.pClientDisConfigCb = NULL;
    607         gpphLibContext->CBInfo.pClientInitCb = NULL;
    608         gpphLibContext->CBInfo.pClientInitCntx = gpphLibContext;
    609         gpphLibContext->CBInfo.pClientNdefNtfRespCb = NULL;
    610         gpphLibContext->CBInfo.pClientNdefNtfRespCntx = NULL;
    611         gpphLibContext->CBInfo.pClientNtfRegRespCB = NULL;
    612         gpphLibContext->CBInfo.pClientNtfRegRespCntx = NULL;
    613         gpphLibContext->CBInfo.pClientPresChkCb = NULL;
    614         gpphLibContext->CBInfo.pClientPresChkCntx = NULL;
    615         gpphLibContext->CBInfo.pClientRdNdefCb = NULL;
    616         gpphLibContext->CBInfo.pClientRdNdefCntx = NULL;
    617         gpphLibContext->CBInfo.pClientShtdwnCntx = NULL;
    618         gpphLibContext->CBInfo.pClientShutdownCb = NULL;
    619         gpphLibContext->CBInfo.pClientTransceiveCb = NULL;
    620         gpphLibContext->CBInfo.pClientTranseCntx = NULL;
    621         gpphLibContext->CBInfo.pClientWrNdefCb = NULL;
    622         gpphLibContext->CBInfo.pClientWrNdefCntx = NULL;
    623         gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb = NULL;
    624         gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx = NULL;
    625         gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb = NULL;
    626         gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx = NULL;
    627         gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb = NULL;
    628         gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx = NULL;
    629         gpphLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb = NULL;
    630         gpphLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt = NULL;
    631         gpphLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCb = NULL;
    632         gpphLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCtxt = NULL;
    633         /*No callback is pending*/
    634         gpphLibContext->status.GenCb_pending_status = FALSE;
    635 
    636     }
    637     return Status;
    638 }
    639 /**
    640 *    LibNfc state machine next state update.
    641 */
    642 
    643 NFCSTATUS
    644 phLibNfc_UpdateNextState(
    645                          pphLibNfc_LibContext_t   pLibContext,
    646                          phLibNfc_State_t        next_state
    647                          )
    648 {
    649     NFCSTATUS       status = NFCSTATUS_INVALID_STATE;
    650     switch(pLibContext->LibNfcState.cur_state)
    651     {
    652     case eLibNfcHalStateShutdown:
    653         {
    654             switch(next_state)
    655             {
    656             case eLibNfcHalStateShutdown:
    657             case eLibNfcHalStateInitandIdle:
    658                 status = NFCSTATUS_SUCCESS;
    659                 break;
    660             default:
    661                 break;
    662             }
    663         }
    664         break;
    665     case eLibNfcHalStateConfigReady:
    666         {
    667             switch(next_state)
    668             {
    669             case eLibNfcHalStateShutdown:
    670             case eLibNfcHalStateConfigReady:
    671             case eLibNfcHalStateInitandIdle:
    672             case eLibNfcHalStateConnect:
    673                 status = NFCSTATUS_SUCCESS;
    674                 break;
    675             default:
    676                 break;
    677             }
    678         }
    679         break;
    680     case eLibNfcHalStateConnect:
    681         {
    682             switch(next_state)
    683             {
    684             case eLibNfcHalStateShutdown:
    685             case eLibNfcHalStateRelease:
    686             case eLibNfcHalStateTransaction:
    687             case eLibNfcHalStatePresenceChk:
    688                 status = NFCSTATUS_SUCCESS;
    689                 break;
    690             default:
    691                 break;
    692             }
    693         }
    694         break;
    695     case eLibNfcHalStatePresenceChk:
    696         {
    697             switch(next_state)
    698             {
    699             case eLibNfcHalStateShutdown:
    700             case eLibNfcHalStateConfigReady:
    701             case eLibNfcHalStateRelease:
    702             case eLibNfcHalStateTransaction:
    703             case eLibNfcHalStatePresenceChk:
    704                 status = NFCSTATUS_SUCCESS;
    705                 break;
    706             default:
    707                 break;
    708             }
    709         }
    710         break;
    711     case eLibNfcHalStateInitandIdle:
    712         {
    713             switch(next_state)
    714             {
    715             case eLibNfcHalStateShutdown:
    716             case eLibNfcHalStateConfigReady:
    717                 status = NFCSTATUS_SUCCESS;
    718                 break;
    719             default:
    720                 break;
    721             }
    722         }
    723         break;
    724     default:
    725         break;
    726     }
    727     pLibContext->LibNfcState.next_state =
    728         (uint8_t)((NFCSTATUS_SUCCESS == status)?next_state:pLibContext->LibNfcState.next_state);
    729 
    730     return status;
    731 }
    732 
    733 /**
    734 *    LibNfc state machine current state update.
    735 */
    736 
    737 void
    738 phLibNfc_UpdateCurState(
    739                         NFCSTATUS      status,
    740                         pphLibNfc_LibContext_t psLibContext
    741                         )
    742 {
    743     switch(psLibContext->LibNfcState.next_state)
    744     {
    745     case eLibNfcHalStateTransaction:
    746         psLibContext->LibNfcState.cur_state = (uint8_t)eLibNfcHalStateConnect;
    747         break;
    748     case eLibNfcHalStateRelease:
    749         psLibContext->LibNfcState.cur_state
    750             = (uint8_t)(psLibContext->status.DiscEnbl_status == TRUE?
    751               eLibNfcHalStateInitandIdle:eLibNfcHalStateConfigReady);
    752         break;
    753     case eLibNfcHalStateInvalid:
    754         break;
    755     default:
    756         psLibContext->LibNfcState.cur_state
    757             = (uint8_t)((NFCSTATUS_SUCCESS == status)?
    758             psLibContext->LibNfcState.next_state:
    759         psLibContext->LibNfcState.cur_state);
    760     }
    761     psLibContext->LibNfcState.next_state = (uint8_t)eLibNfcHalStateInvalid;
    762     return;
    763 }
    764 /* Interface to stack capabilities */
    765 
    766 NFCSTATUS phLibNfc_Mgt_GetstackCapabilities(
    767                     phLibNfc_StackCapabilities_t *phLibNfc_StackCapabilities,
    768                     void                         *pContext)
    769 {
    770     NFCSTATUS RetVal = NFCSTATUS_FAILED;
    771     /*Check Lib Nfc stack is initilized*/
    772     if((NULL == gpphLibContext)||
    773         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
    774     {
    775         RetVal = NFCSTATUS_NOT_INITIALISED;
    776     }
    777     /*Check application has sent the valid parameters*/
    778     else if((NULL == phLibNfc_StackCapabilities)
    779         || (NULL == pContext))
    780     {
    781         RetVal= NFCSTATUS_INVALID_PARAMETER;
    782     }
    783     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
    784     {
    785         RetVal = NFCSTATUS_SHUTDOWN;
    786     }
    787     else if(TRUE == gpphLibContext->status.GenCb_pending_status)
    788     {
    789         /*Previous operation is pending  */
    790         RetVal = NFCSTATUS_BUSY;
    791     }
    792     else
    793     {
    794         /* Tag Format Capabilities*/
    795         phLibNfc_StackCapabilities->psFormatCapabilities.Desfire = TRUE;
    796         phLibNfc_StackCapabilities->psFormatCapabilities.MifareStd = TRUE;
    797         phLibNfc_StackCapabilities->psFormatCapabilities.MifareUL = TRUE;
    798         phLibNfc_StackCapabilities->psFormatCapabilities.FeliCa = FALSE;
    799         phLibNfc_StackCapabilities->psFormatCapabilities.Jewel = FALSE;
    800         phLibNfc_StackCapabilities->psFormatCapabilities.ISO14443_4A = FALSE;
    801         phLibNfc_StackCapabilities->psFormatCapabilities.ISO14443_4B = FALSE;
    802         phLibNfc_StackCapabilities->psFormatCapabilities.MifareULC = TRUE;
    803          phLibNfc_StackCapabilities->psFormatCapabilities.ISO15693 = FALSE;
    804 
    805         /* Tag Mapping Capabilities */
    806         phLibNfc_StackCapabilities->psMappingCapabilities.FeliCa = TRUE;
    807         phLibNfc_StackCapabilities->psMappingCapabilities.Desfire = TRUE;
    808         phLibNfc_StackCapabilities->psMappingCapabilities.ISO14443_4A = TRUE;
    809         phLibNfc_StackCapabilities->psMappingCapabilities.ISO14443_4B = TRUE;
    810         phLibNfc_StackCapabilities->psMappingCapabilities.MifareStd = TRUE;
    811         phLibNfc_StackCapabilities->psMappingCapabilities.MifareUL = TRUE;
    812         phLibNfc_StackCapabilities->psMappingCapabilities.MifareULC = TRUE;
    813         phLibNfc_StackCapabilities->psMappingCapabilities.Jewel = TRUE;
    814         phLibNfc_StackCapabilities->psMappingCapabilities.ISO15693 = FALSE;
    815 
    816         /*Call Hal4 Get Dev Capabilities to get info about protocols supported
    817           by Lib Nfc*/
    818         PHDBG_INFO("LibNfc:Get Stack capabilities ");
    819         RetVal= phHal4Nfc_GetDeviceCapabilities(
    820                         gpphLibContext->psHwReference,
    821                         &(phLibNfc_StackCapabilities->psDevCapabilities),
    822                         (void *)gpphLibContext);
    823 
    824         LIB_NFC_VERSION_SET(phLibNfc_StackCapabilities->psDevCapabilities.hal_version,
    825                             PH_HAL4NFC_VERSION,
    826                             PH_HAL4NFC_REVISION,
    827                             PH_HAL4NFC_PATCH,
    828                             PH_HAL4NFC_BUILD);
    829 
    830         phLibNfc_StackCapabilities->psDevCapabilities.fw_version=
    831             gpphLibContext->psHwReference->device_info.fw_version;
    832         phLibNfc_StackCapabilities->psDevCapabilities.hci_version=
    833             gpphLibContext->psHwReference->device_info.hci_version;
    834         phLibNfc_StackCapabilities->psDevCapabilities.hw_version=
    835             gpphLibContext->psHwReference->device_info.hw_version;
    836         phLibNfc_StackCapabilities->psDevCapabilities.model_id=
    837             gpphLibContext->psHwReference->device_info.model_id;
    838         (void)memcpy(phLibNfc_StackCapabilities->psDevCapabilities.full_version,
    839             gpphLibContext->psHwReference->device_info.full_version,NXP_FULL_VERSION_LEN);
    840 
    841         /* Check the firmware version */
    842         phLibNfc_StackCapabilities->psDevCapabilities.firmware_update_info = memcmp(phLibNfc_StackCapabilities->psDevCapabilities.full_version, nxp_nfc_full_version,
    843                    NXP_FULL_VERSION_LEN);
    844 
    845         if(NFCSTATUS_SUCCESS != RetVal)
    846         {
    847             RetVal = NFCSTATUS_FAILED;
    848         }
    849     }
    850     return RetVal;
    851 }
    852 
    853 
    854 
    855 
    856 
    857 
    858 NFCSTATUS phLibNfc_Mgt_ConfigureTestMode(void   *pDriverHandle,
    859                                  pphLibNfc_RspCb_t   pTestModeCb,
    860                                  phLibNfc_Cfg_Testmode_t eTstmode,
    861                                  void                *pContext)
    862 {
    863      NFCSTATUS Status = NFCSTATUS_SUCCESS;
    864      phHal4Nfc_InitType_t eInitType=eInitDefault;
    865 
    866      if((NULL == pDriverHandle)||(NULL == pTestModeCb))
    867      {
    868         Status = NFCSTATUS_INVALID_PARAMETER;
    869      }
    870      else if((NULL != gpphLibContext) && \
    871          (gpphLibContext->LibNfcState.next_state==eLibNfcHalStateShutdown))
    872      {
    873         Status = NFCSTATUS_SHUTDOWN;
    874      }
    875      else if( (eTstmode == phLibNfc_TstMode_On) && (NULL != gpphLibContext))
    876      {
    877         Status=NFCSTATUS_ALREADY_INITIALISED;
    878      }
    879      else if( (eTstmode == phLibNfc_TstMode_Off) && (NULL == gpphLibContext))
    880      {
    881         Status = NFCSTATUS_NOT_INITIALISED;
    882      }
    883      else if( (eTstmode == phLibNfc_TstMode_Off) && (NULL != gpphLibContext))
    884      {
    885         if (NULL!= gpphLibContext->CBInfo.pClientShutdownCb)
    886         {   /* Previous callback pending */
    887             Status = NFCSTATUS_BUSY;
    888         }
    889         else
    890         {
    891             Status = NFCSTATUS_PENDING;
    892             if(TRUE != gpphLibContext->status.GenCb_pending_status)
    893             {
    894                 Status = phHal4Nfc_Close(gpphLibContext->psHwReference,
    895                                     phLibNfc_ShutdownCb,
    896                                     (void *)gpphLibContext);
    897             }
    898             if(Status== NFCSTATUS_PENDING)
    899             {
    900                 gpphLibContext->CBInfo.pClientShutdownCb = pTestModeCb;
    901                 gpphLibContext->CBInfo.pClientShtdwnCntx = pContext;
    902                 gpphLibContext->status.GenCb_pending_status=TRUE;
    903                 gpphLibContext->LibNfcState.next_state= eLibNfcHalStateShutdown;
    904             }
    905             else
    906             {
    907                 Status =NFCSTATUS_FAILED;
    908             }
    909         }
    910      }
    911      else
    912      {
    913             /* Initialize the Lib context */
    914         gpphLibContext=(pphLibNfc_LibContext_t)phOsalNfc_GetMemory(
    915                                         (uint32_t)sizeof(phLibNfc_LibContext_t));
    916         if(NULL == gpphLibContext)
    917         {
    918             Status=NFCSTATUS_INSUFFICIENT_RESOURCES;
    919         }
    920         else
    921         {
    922             (void)memset((void *)gpphLibContext,0,(
    923                                     (uint32_t)sizeof(phLibNfc_LibContext_t)));
    924 
    925             /* Store the Callback and context in LibContext structure*/
    926             gpphLibContext->CBInfo.pClientInitCb=pTestModeCb;
    927             gpphLibContext->CBInfo.pClientInitCntx=pContext;
    928             /* Initialize the HwReferece structure */
    929             gpphLibContext->psHwReference=(phHal_sHwReference_t *)
    930                                     phOsalNfc_GetMemory((uint32_t)sizeof(phHal_sHwReference_t));
    931             (void)memset((void *)gpphLibContext->psHwReference,0,
    932                                         ((uint32_t)sizeof(phHal_sHwReference_t)));
    933             /* Allocate the Memory for the Transceive info */
    934             if( gpphLibContext->psHwReference!=NULL)
    935             {
    936                 gpphLibContext->psHwReference->p_board_driver = pDriverHandle;
    937                 Status = phLibNfc_UpdateNextState(gpphLibContext,
    938                                             eLibNfcHalStateInitandIdle);
    939                 if(Status==NFCSTATUS_SUCCESS)
    940                 {
    941                     if(eTstmode == phLibNfc_TstMode_On)
    942                         eInitType = eInitTestModeOn;
    943                     if(eTstmode == phLibNfc_TstMode_Off)
    944                         eInitType = eInitDefault;
    945                     Status=phHal4Nfc_Open(
    946                                     gpphLibContext->psHwReference,
    947                                     eInitType,
    948                                     phLibNfc_InitCb,
    949                                     (void *)gpphLibContext);
    950                 }
    951             }
    952             else
    953             {
    954                 Status = NFCSTATUS_INSUFFICIENT_RESOURCES;
    955             }
    956             phLibNfc_Ndef_Init();
    957         }
    958     }
    959 
    960    return Status;
    961 }
    962 
    963