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_initiator.c
     19 
     20  * Project: NFC FRI 1.1
     21  *
     22  * $Date: Fri Apr 23 14:34:08 2010 $
     23  * $Author: ing07385 $
     24  * $Revision: 1.53 $
     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_SE.h>
     39 #include <phLibNfc_ndef_raw.h>
     40 #include <phLibNfc_initiator.h>
     41 #include <phLibNfc_discovery.h>
     42 
     43 
     44 /*
     45 *************************** Macro's  ****************************************
     46 */
     47 
     48 #ifndef STATIC_DISABLE
     49 #define STATIC static
     50 #else
     51 #define STATIC
     52 #endif
     53 
     54 /*
     55 *************************** Global Variables **********************************
     56 */
     57 
     58 #define PN544_IO_TIMEOUT_RESPONSE 0x89
     59 
     60 /*
     61 *************************** Static Function Declaration ***********************
     62 */
     63 
     64 /* Target discvovery notification callback */
     65 STATIC void phLibNfc_NotificationRegister_Resp_Cb (
     66                                 void                             *context,
     67                                 phHal_eNotificationType_t        type,
     68                                 phHal4Nfc_NotificationInfo_t     info,
     69                                 NFCSTATUS                        status
     70                                 );
     71 
     72 /*Remote device connect response callback*/
     73 STATIC void phLibNfc_RemoteDev_Connect_Cb(
     74                            void        *pContext,
     75                            phHal_sRemoteDevInformation_t *pRmtdev_info,
     76                            NFCSTATUS    status
     77                            );
     78 
     79 #ifdef RECONNECT_SUPPORT
     80 STATIC
     81 void
     82 phLibNfc_config_discovery_con_failure_cb (
     83     void                *context,
     84     NFCSTATUS           status);
     85 #endif /* #ifdef RECONNECT_SUPPORT */
     86 
     87 /*Remote device disconnect response callback*/
     88 STATIC void phLibNfc_RemoteDev_Disconnect_cb(
     89                                 void                          *context,
     90                                 phHal_sRemoteDevInformation_t *reg_handle,
     91                                 NFCSTATUS                      status
     92                                 );
     93 /*Remote device Transceive response callback*/
     94 STATIC void phLibNfc_RemoteDev_Transceive_Cb(void *context,
     95                                 phHal_sRemoteDevInformation_t *pRmtdev_info,
     96                                 phNfc_sData_t *response,
     97                                 NFCSTATUS status
     98                                 );
     99 /*Set P2P config paramater response callback*/
    100 STATIC void phLibNfc_Mgt_SetP2P_ConfigParams_Cb(
    101                                 void                             *context,
    102                                 NFCSTATUS                        status
    103                                 );
    104 
    105 
    106 /*
    107 *************************** Function Definitions ******************************
    108 */
    109 
    110 /**
    111 * Response to target discovery.
    112 */
    113 STATIC
    114 void phLibNfc_NotificationRegister_Resp_Cb (
    115                                 void                            *context,
    116                                 phHal_eNotificationType_t       type,
    117                                 phHal4Nfc_NotificationInfo_t    info,
    118                                 NFCSTATUS                       status
    119                                 )
    120 {
    121     NFCSTATUS RetVal = NFCSTATUS_SUCCESS,
    122               Status = NFCSTATUS_SUCCESS;
    123     uint16_t DeviceIndx, DeviceIndx1;
    124     uint8_t sak_byte=0;
    125     uint8_t tag_disc_flg = 0;
    126     phLibNfc_NtfRegister_RspCb_t pClientCb=NULL;
    127     pClientCb =gpphLibContext->CBInfo.pClientNtfRegRespCB;
    128 	PHNFC_UNUSED_VARIABLE(context);
    129 
    130 
    131     if(( type != NFC_DISCOVERY_NOTIFICATION )
    132         &&(PHNFCSTATUS(status)!=NFCSTATUS_DESELECTED))
    133     {
    134         Status = NFCSTATUS_FAILED;
    135     }
    136 	else
    137 	{
    138 		DeviceIndx=0;DeviceIndx1=0;
    139 		while(DeviceIndx < info.psDiscoveryInfo->NumberOfDevices)
    140 		{
    141 			switch(info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx]->RemDevType)
    142 			{
    143 				case  phHal_eMifare_PICC:
    144 				{
    145 					/*Mifare Tag discovered*/
    146 					sak_byte =  info.psDiscoveryInfo->
    147 								ppRemoteDevInfo[DeviceIndx]->RemoteDevInfo.Iso14443A_Info.Sak;
    148 					if((TRUE == gpphLibContext->RegNtfType.MifareUL)&& (sak_byte==0x00))
    149 					{
    150 						/*Copy the tag related info*/
    151 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    152 							info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    153 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    154 							(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    155 						gpphLibContext->Discov_handle[DeviceIndx1] =
    156 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    157 						DeviceIndx1++;
    158 						tag_disc_flg++;
    159 					}
    160 
    161 					if((TRUE == gpphLibContext->RegNtfType.MifareStd)&&
    162 						(((sak_byte & 0x18)==0x08)||((sak_byte & 0x18)==0x18)))
    163 					{
    164 						/*Copy the tag related info*/
    165 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    166 							info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    167 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    168 							(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    169 						gpphLibContext->Discov_handle[DeviceIndx1]=
    170 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    171 						DeviceIndx1++;
    172 						tag_disc_flg++;
    173 					}
    174 
    175 				}break;
    176 				case  phHal_eISO14443_A_PICC:
    177 				{
    178 					/*ISO 14443-A type tag discovered*/
    179 					if(TRUE == gpphLibContext->RegNtfType.ISO14443_4A)
    180 					{
    181 						/*Copy the ISO type A tag info*/
    182 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    183 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    184 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    185 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    186 						gpphLibContext->Discov_handle[DeviceIndx1] =
    187 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    188 						DeviceIndx1++;
    189 						tag_disc_flg++;
    190 					}
    191 				}break;
    192 				case  phHal_eISO14443_3A_PICC:
    193 				{
    194 					/*ISO 14443-A type tag discovered*/
    195 					if(TRUE == gpphLibContext->RegNtfType.MifareUL)
    196 					{
    197 						/*Copy the ISO type A tag info*/
    198 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    199 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    200 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    201 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    202 						gpphLibContext->Discov_handle[DeviceIndx1] =
    203 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    204 						DeviceIndx1++;
    205 						tag_disc_flg++;
    206 					}
    207 				}break;
    208 				case  phHal_eISO14443_B_PICC:
    209 				{
    210 					/*ISO 14443-B type tag Discovered */
    211 					if(TRUE == gpphLibContext->RegNtfType.ISO14443_4B)
    212 					{
    213 						/*Copy the Type B tag info */
    214 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    215 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    216 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    217 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    218 						gpphLibContext->Discov_handle[DeviceIndx1] =
    219 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    220 						DeviceIndx1++;
    221 						tag_disc_flg++;
    222 					}
    223 				}break;
    224 				case  phHal_eFelica_PICC:
    225 				{
    226 					/*Felica Type Tag Discovered */
    227 					if(TRUE == gpphLibContext->RegNtfType.Felica)
    228 					{
    229 						/*Copy the Felica tag info */
    230 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    231 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    232 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    233 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    234 						gpphLibContext->Discov_handle[DeviceIndx1] =
    235 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    236 						DeviceIndx1++;
    237 						tag_disc_flg++;
    238 					}
    239 				}break;
    240 				case  phHal_eJewel_PICC:
    241 				{
    242 					/*Jewel Type Tag Discovered */
    243 					if(TRUE == gpphLibContext->RegNtfType.Jewel)
    244 					{
    245 						/*Copy the Felica tag info */
    246 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    247 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    248 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    249 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    250 						gpphLibContext->Discov_handle[DeviceIndx1] =
    251 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    252 						DeviceIndx1++;
    253 						tag_disc_flg++;
    254 					}
    255 				}
    256 				break;
    257 				case  phHal_eISO15693_PICC:
    258 				{
    259 					/*Jewel Type Tag Discovered */
    260 					if(TRUE == gpphLibContext->RegNtfType.ISO15693)
    261 					{
    262 						/*Copy the Felica tag info */
    263 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    264 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    265 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    266 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    267 						gpphLibContext->Discov_handle[DeviceIndx1] =
    268 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    269 						DeviceIndx1++;
    270 						tag_disc_flg++;
    271 					}
    272 				}
    273 				break;
    274 				case  phHal_eNfcIP1_Target:
    275 				{
    276 					if(TRUE == gpphLibContext->RegNtfType.NFC)
    277 					{
    278 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    279 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    280 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    281 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;
    282 						gpphLibContext->Discov_handle[DeviceIndx1] =
    283 							gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    284 						DeviceIndx1++;
    285 						tag_disc_flg++;
    286 					}
    287 				}
    288 				break;
    289 				case  phHal_eNfcIP1_Initiator:
    290 				{
    291 					if(TRUE == gpphLibContext->RegNtfType.NFC)
    292 					{
    293 						gpphLibContext->LibNfcState.cur_state=eLibNfcHalStateConnect;
    294 						gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=
    295 								info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];
    296 						gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =
    297 								(uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo;
    298 						gpphLibContext->sNfcIp_Context.Rem_Initiator_Handle=
    299 								gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;
    300 						DeviceIndx1++;
    301 						tag_disc_flg++;
    302 					}
    303 				}
    304 				break;
    305 				default :
    306 				{
    307 					break;
    308 				}
    309 			}
    310 			DeviceIndx++;
    311 		}
    312 	}
    313 
    314     if((tag_disc_flg >0 )&&(status != NFCSTATUS_FAILED))
    315     {
    316         gpphLibContext->dev_cnt = tag_disc_flg;
    317         /* Check for if the discovered tags are multiple or
    318          Multiple protocol tag */
    319         if((gpphLibContext->dev_cnt > 1)&&(
    320             (status ==NFCSTATUS_MULTIPLE_PROTOCOLS) ||
    321             (status ==NFCSTATUS_MULTIPLE_TAGS)) )
    322         {
    323             status = status;
    324         }
    325         else
    326         {
    327             status =NFCSTATUS_SUCCESS;
    328         }
    329         /*Notify to upper layer the no of tag discovered and
    330           the protocol */
    331         if (NULL != pClientCb)
    332         {
    333             pClientCb(
    334 					(void*)gpphLibContext->CBInfo.pClientNtfRegRespCntx,
    335                     gpphLibContext->psRemoteDevList,
    336                     gpphLibContext->dev_cnt,
    337 					status
    338                     );
    339         }
    340 
    341     }
    342     else if(PHNFCSTATUS(status)==NFCSTATUS_DESELECTED)
    343     {
    344         info.psDiscoveryInfo->NumberOfDevices = 0;
    345         if (NULL != pClientCb)
    346         {
    347             gpphLibContext->LibNfcState.cur_state=eLibNfcHalStateRelease;
    348             pClientCb((void*)gpphLibContext->CBInfo.pClientNtfRegRespCntx,
    349                     NULL,
    350                     0,
    351                     status);
    352         }
    353 
    354     }
    355     else /*Reconfigure the discovery wheel*/
    356     {
    357         RetVal = phHal4Nfc_ConfigureDiscovery ( gpphLibContext->psHwReference,
    358                                             NFC_DISCOVERY_RESUME,
    359                                             &(gpphLibContext->sADDconfig),
    360                                             phLibNfc_config_discovery_cb,
    361                                             gpphLibContext);
    362 
    363         if((RetVal!=NFCSTATUS_SUCCESS) &&(RetVal!=NFCSTATUS_PENDING))
    364         {
    365             Status = NFCSTATUS_FAILED;
    366         }
    367 
    368     }
    369     if(Status == NFCSTATUS_FAILED)
    370     {
    371         if (NULL != pClientCb)
    372         {
    373             pClientCb(gpphLibContext->CBInfo.pClientNtfRegRespCntx,
    374                 NULL,
    375                 0,
    376                 Status);
    377         }
    378     }
    379     return;
    380 }
    381 
    382 /**
    383 * This interface registers notification handler for target discovery.
    384 */
    385 NFCSTATUS
    386 phLibNfc_RemoteDev_NtfRegister(
    387                         phLibNfc_Registry_Info_t*       pRegistryInfo,
    388                         phLibNfc_NtfRegister_RspCb_t    pNotificationHandler,
    389                         void                            *pContext
    390                         )
    391 {
    392     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;
    393 
    394 
    395     /*Check for valid parameters*/
    396     if((NULL == pNotificationHandler)
    397         || (NULL == pContext)
    398         ||(NULL== pRegistryInfo))
    399     {
    400         RetVal= NFCSTATUS_INVALID_PARAMETER;
    401     }
    402     else if((NULL == gpphLibContext) ||
    403         (gpphLibContext->LibNfcState.cur_state
    404                             == eLibNfcHalStateShutdown))
    405     {
    406         RetVal = NFCSTATUS_NOT_INITIALISED;
    407     }
    408     else if(gpphLibContext->LibNfcState.next_state
    409                             == eLibNfcHalStateShutdown)
    410     {
    411         /*Next state is shutdown*/
    412         RetVal= NFCSTATUS_SHUTDOWN;
    413     }
    414     else
    415     {
    416 
    417         PHDBG_INFO("LibNfc:Registering Notification Handler");
    418 
    419 
    420         (void) memcpy(&(gpphLibContext->RegNtfType),pRegistryInfo,
    421                         sizeof(phLibNfc_Registry_Info_t));
    422         /* Register Discovery Notification Handler*/
    423 
    424 		/*Register for NFCIP1 target type*/
    425 		RetVal = phHal4Nfc_RegisterNotification(
    426                                 gpphLibContext->psHwReference,
    427                                 eRegisterP2PDiscovery,
    428                                 phLibNfc_NotificationRegister_Resp_Cb,
    429                                 (void*)gpphLibContext
    430                                 );
    431         /*Register for Tag discovery*/
    432 		RetVal = phHal4Nfc_RegisterNotification(
    433                             gpphLibContext->psHwReference,
    434                             eRegisterTagDiscovery,
    435                             phLibNfc_NotificationRegister_Resp_Cb,
    436                             (void*)gpphLibContext
    437                             );
    438         gpphLibContext->CBInfo.pClientNtfRegRespCB = pNotificationHandler;
    439         gpphLibContext->CBInfo.pClientNtfRegRespCntx = pContext;
    440         /*Register notification handler with below layer*/
    441 
    442     }
    443     return RetVal;
    444 }
    445 /**
    446 * This interface unregisters notification handler for target discovery.
    447 */
    448 NFCSTATUS phLibNfc_RemoteDev_NtfUnregister(void)
    449 {
    450     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;
    451     if((NULL == gpphLibContext) ||
    452        (gpphLibContext->LibNfcState.cur_state
    453                             == eLibNfcHalStateShutdown))
    454     {
    455         /*Lib Nfc not Initialized*/
    456         RetVal = NFCSTATUS_NOT_INITIALISED;
    457     }
    458     else if(gpphLibContext->LibNfcState.next_state
    459                             == eLibNfcHalStateShutdown)
    460     {
    461         /*Lib Nfc Shutdown*/
    462         RetVal= NFCSTATUS_SHUTDOWN;
    463     }
    464     else
    465     {
    466         /*Unregister notification handler with lower layer */
    467         RetVal = phHal4Nfc_UnregisterNotification(
    468                                     gpphLibContext->psHwReference,
    469                                     eRegisterP2PDiscovery,
    470                                     gpphLibContext);
    471 
    472 		RetVal = phHal4Nfc_UnregisterNotification(
    473                                     gpphLibContext->psHwReference,
    474                                     eRegisterTagDiscovery,
    475                                     gpphLibContext);
    476 
    477         gpphLibContext->CBInfo.pClientNtfRegRespCB = NULL;
    478         gpphLibContext->CBInfo.pClientNtfRegRespCntx =NULL;
    479         PHDBG_INFO("LibNfc:Unregister Notification Handler");
    480     }
    481     return RetVal;
    482 }
    483 
    484 #ifdef RECONNECT_SUPPORT
    485 
    486 NFCSTATUS
    487 phLibNfc_RemoteDev_ReConnect (
    488     phLibNfc_Handle                 hRemoteDevice,
    489     pphLibNfc_ConnectCallback_t     pNotifyReConnect_RspCb,
    490     void                            *pContext)
    491 {
    492 
    493     NFCSTATUS                           ret_val = NFCSTATUS_FAILED;
    494     phLibNfc_sRemoteDevInformation_t    *psRemoteDevInfo = NULL;
    495 
    496     if ((NULL == gpphLibContext)
    497       || (eLibNfcHalStateShutdown ==
    498         gpphLibContext->LibNfcState.cur_state))
    499     {
    500          ret_val = NFCSTATUS_NOT_INITIALISED;
    501     }
    502     else if ((NULL == pContext)
    503         || (NULL == pNotifyReConnect_RspCb)
    504         || (NULL == (void *)hRemoteDevice))
    505     {
    506         /* Check valid parameters */
    507         ret_val = NFCSTATUS_INVALID_PARAMETER;
    508     }
    509     /* Check valid lib nfc State */
    510     else if (gpphLibContext->LibNfcState.next_state
    511              == eLibNfcHalStateShutdown)
    512     {
    513         ret_val = NFCSTATUS_SHUTDOWN;
    514     }
    515     else if (0 == gpphLibContext->Connected_handle)
    516     {
    517         ret_val = NFCSTATUS_TARGET_NOT_CONNECTED;
    518     }
    519     else if ((gpphLibContext->Discov_handle[0] != hRemoteDevice)
    520 		&& (gpphLibContext->Discov_handle[1] != hRemoteDevice)
    521 		&& (gpphLibContext->Discov_handle[2] != hRemoteDevice)
    522 		&& (gpphLibContext->Discov_handle[3] != hRemoteDevice)
    523 		&& (gpphLibContext->Discov_handle[4] != hRemoteDevice)
    524 		&& (gpphLibContext->Discov_handle[5] != hRemoteDevice)
    525 		&& (gpphLibContext->Discov_handle[6] != hRemoteDevice)
    526 		&& (gpphLibContext->Discov_handle[7] != hRemoteDevice)
    527 		&& (gpphLibContext->Discov_handle[8] != hRemoteDevice)
    528 		&& (gpphLibContext->Discov_handle[9] != hRemoteDevice))
    529     {
    530         ret_val = NFCSTATUS_INVALID_HANDLE;
    531     }
    532     else
    533     {
    534         psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t *)hRemoteDevice;
    535 
    536         /* Call the HAL connect*/
    537         ret_val = phHal4Nfc_Connect (gpphLibContext->psHwReference,
    538                                psRemoteDevInfo,
    539                                phLibNfc_RemoteDev_Connect_Cb,
    540                                (void *)gpphLibContext);
    541 
    542         if (NFCSTATUS_PENDING == ret_val)
    543         {
    544             /* If HAL Connect is pending update the LibNFC state machine
    545                 and store the CB pointer and Context,
    546                 mark the General CB pending status is TRUE */
    547             gpphLibContext->CBInfo.pClientConnectCb = pNotifyReConnect_RspCb;
    548             gpphLibContext->CBInfo.pClientConCntx = pContext;
    549             gpphLibContext->status.GenCb_pending_status = TRUE;
    550 			gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;
    551 
    552             gpphLibContext->Prev_Connected_handle = gpphLibContext->Connected_handle;
    553 
    554 			gpphLibContext->Connected_handle = hRemoteDevice;
    555          }
    556          else if (NFCSTATUS_INVALID_REMOTE_DEVICE == PHNFCSTATUS(ret_val))
    557          {
    558            /* The Handle given for connect is invalid*/
    559             ret_val = NFCSTATUS_TARGET_NOT_CONNECTED;
    560          }
    561          else
    562          {
    563             /* Lower layer returns internal error code return NFCSTATUS_FAILED*/
    564             ret_val = NFCSTATUS_FAILED;
    565          }
    566     }
    567 
    568     return ret_val;
    569 }
    570 #endif /* #ifdef RECONNECT_SUPPORT */
    571 
    572 
    573 /**
    574 * Connect to a single Remote Device
    575 */
    576 NFCSTATUS phLibNfc_RemoteDev_Connect(
    577                     phLibNfc_Handle             hRemoteDevice,
    578                     pphLibNfc_ConnectCallback_t pNotifyConnect_RspCb,
    579                     void                        *pContext
    580                     )
    581 {
    582 
    583     NFCSTATUS RetVal = NFCSTATUS_FAILED;
    584     phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo;
    585 
    586     if((NULL == gpphLibContext) ||
    587       (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
    588     {
    589          RetVal = NFCSTATUS_NOT_INITIALISED;
    590     }/* Check valid parameters*/
    591     else if((NULL == pContext)
    592         || (NULL == pNotifyConnect_RspCb)
    593         || (NULL == (void*)hRemoteDevice))
    594     {
    595        RetVal= NFCSTATUS_INVALID_PARAMETER;
    596     }
    597     /* Check valid lib nfc State*/
    598     else if(gpphLibContext->LibNfcState.next_state
    599                             == eLibNfcHalStateShutdown)
    600     {
    601         RetVal= NFCSTATUS_SHUTDOWN;
    602     }
    603     else if((gpphLibContext->Discov_handle[0] != hRemoteDevice)&&
    604 		(gpphLibContext->Discov_handle[1] != hRemoteDevice)&&
    605 		(gpphLibContext->Discov_handle[2] != hRemoteDevice)&&
    606 		(gpphLibContext->Discov_handle[3] != hRemoteDevice)&&
    607 		(gpphLibContext->Discov_handle[4] != hRemoteDevice)&&
    608 		(gpphLibContext->Discov_handle[5] != hRemoteDevice)&&
    609 		(gpphLibContext->Discov_handle[6] != hRemoteDevice)&&
    610 		(gpphLibContext->Discov_handle[7] != hRemoteDevice)&&
    611 		(gpphLibContext->Discov_handle[8] != hRemoteDevice)&&
    612 		(gpphLibContext->Discov_handle[9] != hRemoteDevice))
    613     {
    614         RetVal= NFCSTATUS_INVALID_HANDLE;
    615     }
    616     else if ((hRemoteDevice != gpphLibContext->Connected_handle)
    617         && (0 != gpphLibContext->Connected_handle))
    618     {
    619         RetVal = NFCSTATUS_FAILED;
    620     }
    621     else
    622     {
    623         psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice;
    624 
    625         /* Call the HAL connect*/
    626         RetVal = phHal4Nfc_Connect(gpphLibContext->psHwReference,
    627                                psRemoteDevInfo,
    628                                phLibNfc_RemoteDev_Connect_Cb,
    629                                (void* )gpphLibContext);
    630         if(RetVal== NFCSTATUS_PENDING)
    631         {
    632             /* If HAL Connect is pending update the LibNFC state machine
    633                 and store the CB pointer and Context,
    634                 mark the General CB pending status is TRUE*/
    635             gpphLibContext->CBInfo.pClientConnectCb = pNotifyConnect_RspCb;
    636             gpphLibContext->CBInfo.pClientConCntx = pContext;
    637             gpphLibContext->status.GenCb_pending_status=TRUE;
    638 			gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;
    639             gpphLibContext->Prev_Connected_handle = gpphLibContext->Connected_handle;
    640 			gpphLibContext->Connected_handle = hRemoteDevice;
    641          }
    642          else if(PHNFCSTATUS(RetVal) == NFCSTATUS_INVALID_REMOTE_DEVICE)
    643          {
    644            /* The Handle given for connect is invalid*/
    645             RetVal= NFCSTATUS_TARGET_NOT_CONNECTED;
    646          }
    647          else
    648          {
    649             /* Lower layer returns internal error code return NFCSTATUS_FAILED*/
    650             RetVal = NFCSTATUS_FAILED;
    651          }
    652     }
    653     return RetVal;
    654 }
    655 
    656 #ifdef RECONNECT_SUPPORT
    657 STATIC
    658 void
    659 phLibNfc_config_discovery_con_failure_cb (
    660     void                *context,
    661     NFCSTATUS           status)
    662 {
    663     if((phLibNfc_LibContext_t *)context == gpphLibContext)
    664     {   /*check for same context*/
    665         pphLibNfc_ConnectCallback_t    ps_client_con_cb =
    666                                     gpphLibContext->CBInfo.pClientConnectCb;
    667 
    668         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
    669         {
    670             /*If shutdown called in between allow shutdown to happen*/
    671             phLibNfc_Pending_Shutdown();
    672             status = NFCSTATUS_SHUTDOWN;
    673         }
    674         else
    675         {
    676             gpphLibContext->status.GenCb_pending_status = FALSE;
    677             gpphLibContext->status.DiscEnbl_status = FALSE;
    678             status = NFCSTATUS_TARGET_LOST;
    679 
    680             phLibNfc_UpdateCurState (status,gpphLibContext);
    681 #ifdef RESTART_CFG
    682             if(gpphLibContext->status.Discovery_pending_status == TRUE)
    683             {
    684                 NFCSTATUS RetStatus = NFCSTATUS_FAILED;
    685                 /* Application has called discovery before receiving this callback,
    686                 so NO notification to the upper layer, instead lower layer
    687                 discovery is called */
    688                 gpphLibContext->status.Discovery_pending_status = FALSE;
    689                 RetStatus =  phHal4Nfc_ConfigureDiscovery(
    690                         gpphLibContext->psHwReference,
    691                         gpphLibContext->eLibNfcCfgMode,
    692                         &gpphLibContext->sADDconfig,
    693                         (pphLibNfc_RspCb_t)
    694                         phLibNfc_config_discovery_cb,
    695                         (void *)gpphLibContext);
    696                 if (NFCSTATUS_PENDING == RetStatus)
    697                 {
    698                     (void)phLibNfc_UpdateNextState(gpphLibContext,
    699                                             eLibNfcHalStateConfigReady);
    700                     gpphLibContext->status.GenCb_pending_status = TRUE;
    701                     gpphLibContext->status.DiscEnbl_status = TRUE;
    702                 }
    703             }
    704 
    705 #endif /* #ifdef RESTART_CFG */
    706         }
    707 
    708         if (NULL != ps_client_con_cb)
    709         {
    710             gpphLibContext->CBInfo.pClientConnectCb = NULL;
    711             /* Call the upper layer callback*/
    712             ps_client_con_cb (gpphLibContext->CBInfo.pClientConCntx,
    713                             0, NULL, status);
    714         }
    715     } /*End of if-context check*/
    716     else
    717     {   /*exception: wrong context pointer returned*/
    718         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    719         status = NFCSTATUS_FAILED;
    720     }
    721 
    722 
    723 }
    724 #endif /* #ifdef RECONNECT_SUPPORT */
    725 /**
    726 * Response callback for remote device connect
    727 */
    728 STATIC void phLibNfc_RemoteDev_Connect_Cb(
    729                            void        *pContext,
    730                            phHal_sRemoteDevInformation_t *pRmtdev_info,
    731                            NFCSTATUS    status
    732                            )
    733 {
    734     NFCSTATUS             Connect_status = NFCSTATUS_SUCCESS;
    735     /*Check valid lib nfc context is returned from lower layer*/
    736     if((phLibNfc_LibContext_t *)pContext == gpphLibContext)
    737     {
    738         gpphLibContext->LastTrancvSuccess = FALSE;
    739 
    740         /* Mark General Callback pending status as false*/
    741         gpphLibContext->status.GenCb_pending_status = FALSE;
    742 
    743         /* Check the shutdown is called during the lower layer Connect in process,
    744            If yes call shutdown call and return NFCSTATUS_SHUTDOWN */
    745         if((eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state))
    746         {
    747             phLibNfc_Pending_Shutdown();
    748             Connect_status = NFCSTATUS_SHUTDOWN;
    749 
    750         }
    751         else if(PHNFCSTATUS(status)==NFCSTATUS_SUCCESS)
    752         {
    753             /* Copy the Remote device address as connected handle*/
    754             gpphLibContext->Connected_handle =(uint32_t) pRmtdev_info;
    755             /* Update the state to connected and return status as SUCCESS*/
    756             gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;
    757             Connect_status = NFCSTATUS_SUCCESS;
    758         }
    759         else
    760         {  /* if(PHNFCSTATUS(status)==NFCSTATUS_INVALID_REMOTE_DEVICE) */
    761             /* If remote device is invalid return as TARGET LOST to upper layer*/
    762             /* If error code is other than SUCCESS return NFCSTATUS_TARGET_LOST */
    763             Connect_status = NFCSTATUS_TARGET_LOST;
    764             gpphLibContext->Connected_handle = gpphLibContext->Prev_Connected_handle ;
    765         }
    766         gpphLibContext->ndef_cntx.is_ndef = CHK_NDEF_NOT_DONE;
    767         /* Update the Current Sate*/
    768         phLibNfc_UpdateCurState(Connect_status,(phLibNfc_LibContext_t *)pContext);
    769         /* Call the upper layer callback*/
    770         gpphLibContext->CBInfo.pClientConnectCb(
    771                     gpphLibContext->CBInfo.pClientConCntx,
    772                     (uint32_t)pRmtdev_info,
    773                     (phLibNfc_sRemoteDevInformation_t*)pRmtdev_info,
    774                     Connect_status);
    775     }
    776     else
    777     {   /*exception: wrong context pointer returned*/
    778         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    779     }
    780     return;
    781 }
    782 
    783 /**
    784 * Allows to disconnect from already connected target.
    785 */
    786 NFCSTATUS phLibNfc_RemoteDev_Disconnect( phLibNfc_Handle                 hRemoteDevice,
    787                                         phLibNfc_eReleaseType_t          ReleaseType,
    788                                         pphLibNfc_DisconnectCallback_t   pDscntCallback,
    789                                         void*                            pContext
    790                                         )
    791 {
    792     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;
    793     phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo=NULL;
    794 
    795     /*Check for valid parameter*/
    796     if((NULL == gpphLibContext) ||
    797         (gpphLibContext->LibNfcState.cur_state
    798                             == eLibNfcHalStateShutdown))
    799     {
    800         RetVal = NFCSTATUS_NOT_INITIALISED;
    801     }
    802     else if((NULL == pContext) ||
    803         (NULL == pDscntCallback)||(hRemoteDevice == 0))
    804     {
    805         RetVal= NFCSTATUS_INVALID_PARAMETER;
    806     }
    807     /* Check for valid state,If De initialize is called then
    808     return NFCSTATUS_SHUTDOWN */
    809     else if(gpphLibContext->LibNfcState.next_state
    810                             == eLibNfcHalStateShutdown)
    811     {
    812         RetVal= NFCSTATUS_SHUTDOWN;
    813     }
    814     else if(gpphLibContext->Connected_handle==0)
    815     {
    816         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
    817     }
    818     /* The given handle is not the connected handle return NFCSTATUS_INVALID_HANDLE*/
    819     else if(hRemoteDevice != gpphLibContext->Connected_handle )
    820     {
    821         RetVal=NFCSTATUS_INVALID_HANDLE;
    822     }
    823     else
    824     {
    825         if((eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
    826             ||((gpphLibContext->sSeContext.eActivatedMode == phLibNfc_SE_ActModeWired)&&
    827                     (ReleaseType != NFC_SMARTMX_RELEASE))
    828                     ||((gpphLibContext->sSeContext.eActivatedMode != phLibNfc_SE_ActModeWired)&&
    829                             (ReleaseType == NFC_SMARTMX_RELEASE)))
    830         {   /* Previous disconnect callback is pending */
    831             RetVal = NFCSTATUS_REJECTED;
    832         }
    833 #ifndef LLCP_CHANGES
    834         else if(eLibNfcHalStateTransaction == gpphLibContext->LibNfcState.next_state)
    835         {   /* Previous  Transaction is Pending*/
    836             RetVal = NFCSTATUS_BUSY;
    837             PHDBG_INFO("LibNfc:Transaction is Pending");
    838         }
    839 #endif /* #ifdef LLCP_CHANGES */
    840         else
    841         {
    842             gpphLibContext->ReleaseType = ReleaseType;
    843             psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice;
    844             RetVal = phHal4Nfc_Disconnect(gpphLibContext->psHwReference,
    845                                 (phHal_sRemoteDevInformation_t*)psRemoteDevInfo,
    846                                 gpphLibContext->ReleaseType,
    847                                 (pphHal4Nfc_DiscntCallback_t)
    848                                 phLibNfc_RemoteDev_Disconnect_cb,
    849                                 (void *)gpphLibContext);
    850             if( NFCSTATUS_PENDING == PHNFCSTATUS(RetVal))
    851             {
    852                 /*Copy the upper layer Callback pointer and context*/
    853                 gpphLibContext->CBInfo.pClientDisConnectCb = pDscntCallback;
    854                 gpphLibContext->CBInfo.pClientDConCntx = pContext;
    855                 /* Mark general callback pending status as TRUE and update the state*/
    856                 gpphLibContext->status.GenCb_pending_status=TRUE;
    857 				gpphLibContext->LibNfcState.next_state = eLibNfcHalStateRelease;
    858 
    859             }
    860             else
    861             {
    862                 /*If lower layer returns other than pending
    863                 (internal error codes) return NFCSTATUS_FAILED */
    864                 RetVal = NFCSTATUS_FAILED;
    865             }
    866         }
    867     }
    868     return RetVal;
    869 }
    870 /**
    871 * Response callback for Remote device Disconnect.
    872 */
    873 STATIC void phLibNfc_RemoteDev_Disconnect_cb(
    874                                 void                          *context,
    875                                 phHal_sRemoteDevInformation_t *reg_handle,
    876                                 NFCSTATUS                      status
    877                                 )
    878 {
    879     NFCSTATUS             DisCnct_status = NFCSTATUS_SUCCESS;
    880     pphLibNfc_DisconnectCallback_t pUpper_NtfCb = NULL;
    881         void  *pUpper_Context = NULL;
    882 
    883     /* Copy the upper layer Callback and context*/
    884     pUpper_NtfCb = gpphLibContext->CBInfo.pClientDisConnectCb;
    885     pUpper_Context = gpphLibContext->CBInfo.pClientDConCntx;
    886 
    887     /* Check valid context is returned or not */
    888     if((phLibNfc_LibContext_t *)context != gpphLibContext)
    889     {
    890         /*exception: wrong context pointer returned*/
    891         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
    892     }
    893     else
    894     {
    895         /* Mark the General callback pending status FALSE   */
    896         gpphLibContext->status.GenCb_pending_status = FALSE;
    897         gpphLibContext->CBInfo.pClientDisConnectCb = NULL;
    898         gpphLibContext->CBInfo.pClientDConCntx = NULL;
    899 
    900         gpphLibContext->ndef_cntx.is_ndef = CHK_NDEF_NOT_DONE;
    901         gpphLibContext->LastTrancvSuccess = FALSE;
    902         /*Reset Connected handle */
    903         gpphLibContext->Connected_handle=0x0000;
    904         /*Reset previous Connected handle */
    905         gpphLibContext->Prev_Connected_handle = 0x0000;
    906 
    907         if(gpphLibContext->sSeContext.eActivatedMode == phLibNfc_SE_ActModeWired)
    908         {
    909           gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeDefault;
    910         }
    911         if(NULL != gpphLibContext->psBufferedAuth)
    912         {
    913             if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
    914             {
    915                 phOsalNfc_FreeMemory(
    916                     gpphLibContext->psBufferedAuth->sRecvData.buffer);
    917             }
    918             if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
    919             {
    920                 phOsalNfc_FreeMemory(
    921                     gpphLibContext->psBufferedAuth->sSendData.buffer);
    922             }
    923             phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
    924             gpphLibContext->psBufferedAuth = NULL;
    925         }
    926     }
    927     /* Check DeInit is called or not */
    928     if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
    929     {
    930         /*call shutdown and return  status as NFCSTATUS_SHUTDOWN */
    931         phLibNfc_Pending_Shutdown();
    932         DisCnct_status = NFCSTATUS_SHUTDOWN;
    933     }
    934     else if(NFCSTATUS_SUCCESS == status)
    935     {
    936         DisCnct_status = NFCSTATUS_SUCCESS;
    937 		gpphLibContext->LibNfcState.next_state = eLibNfcHalStateRelease;
    938     }
    939     else
    940     {
    941         DisCnct_status = NFCSTATUS_FAILED;
    942         phLibNfc_UpdateCurState(DisCnct_status,(phLibNfc_LibContext_t *)context);
    943     }
    944     /* Call the upper layer Callback */
    945     (*pUpper_NtfCb)(pUpper_Context,
    946                     (uint32_t)reg_handle,
    947                     DisCnct_status);
    948     return;
    949 }
    950 
    951 /**
    952 * This interface allows to perform Read/write operation on remote device.
    953 */
    954 NFCSTATUS
    955 phLibNfc_RemoteDev_Transceive(phLibNfc_Handle                   hRemoteDevice,
    956                               phLibNfc_sTransceiveInfo_t*       psTransceiveInfo,
    957                               pphLibNfc_TransceiveCallback_t    pTransceive_RspCb,
    958                               void*                             pContext
    959                               )
    960 {
    961     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;
    962 
    963     /*Check for valid parameter */
    964 
    965     if((NULL == gpphLibContext) ||
    966         (gpphLibContext->LibNfcState.cur_state
    967                             == eLibNfcHalStateShutdown))
    968     {
    969         RetVal = NFCSTATUS_NOT_INITIALISED;
    970     }
    971     else if((NULL == psTransceiveInfo)
    972         || (NULL == pTransceive_RspCb)
    973         || (NULL == (void *)hRemoteDevice)
    974         || (NULL == psTransceiveInfo->sRecvData.buffer)
    975         || (NULL == psTransceiveInfo->sSendData.buffer)
    976         || (NULL == pContext))
    977     {
    978         RetVal= NFCSTATUS_INVALID_PARAMETER;
    979     }
    980     /* Check the state for DeInit is called or not,if yes return NFCSTATUS_SHUTDOWN*/
    981     else if(gpphLibContext->LibNfcState.next_state
    982                             == eLibNfcHalStateShutdown)
    983     {
    984         RetVal= NFCSTATUS_SHUTDOWN;
    985     }/* If there is no handle connected return NFCSTATUS_TARGET_NOT_CONNECTED*/
    986     else if(gpphLibContext->Connected_handle==0)
    987     {
    988         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;
    989     }/* If the given handle is not the connected handle return NFCSTATUS_INVALID_HANDLE */
    990 	else if(gpphLibContext->Connected_handle!= hRemoteDevice )
    991     {
    992         RetVal=NFCSTATUS_INVALID_HANDLE;
    993     } /*If the transceive is called before finishing the previous transceive function
    994       return NFCSTATUS_REJECTED  */
    995     else if((eLibNfcHalStateTransaction ==
    996         gpphLibContext->LibNfcState.next_state)
    997         ||(phHal_eNfcIP1_Initiator==
    998         ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType))
    999     {
   1000         RetVal = NFCSTATUS_REJECTED;
   1001     }
   1002 #ifdef LLCP_TRANSACT_CHANGES
   1003     else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
   1004             && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
   1005     {
   1006         RetVal= NFCSTATUS_BUSY;
   1007     }
   1008 #endif /* #ifdef LLCP_TRANSACT_CHANGES */
   1009     else
   1010     {
   1011         gpphLibContext->ndef_cntx.eLast_Call = RawTrans;
   1012         (void)memcpy((void *)(gpphLibContext->psTransInfo),
   1013                     (void *)psTransceiveInfo,
   1014                     sizeof(phLibNfc_sTransceiveInfo_t));
   1015         /* Check the given Mifare command is supported or not ,
   1016                             If not return NFCSTATUS_COMMAND_NOT_SUPPORTED */
   1017         if( (((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType ==
   1018                phHal_eMifare_PICC)&&
   1019             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRaw ) &&
   1020             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareAuthentA ) &&
   1021             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareAuthentB ) &&
   1022             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRead16 ) &&
   1023             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRead ) &&
   1024             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareWrite16 ) &&
   1025             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareWrite4 ) &&
   1026             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareDec ) &&
   1027             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareTransfer ) &&
   1028             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRestore ) &&
   1029             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareReadSector ) &&
   1030             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareWriteSector ))
   1031         {
   1032             RetVal = NFCSTATUS_COMMAND_NOT_SUPPORTED;
   1033         }
   1034         if(eLibNfcHalStatePresenceChk !=
   1035                      gpphLibContext->LibNfcState.next_state)
   1036         {
   1037             PHDBG_INFO("LibNfc:Transceive In Progress");
   1038             if((((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType ==
   1039                phHal_eMifare_PICC) && (((phHal_sRemoteDevInformation_t*)
   1040                hRemoteDevice)->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&
   1041                (phHal_eMifareAuthentA == gpphLibContext->psTransInfo->cmd.MfCmd))
   1042             {
   1043                 if(NULL != gpphLibContext->psBufferedAuth)
   1044                 {
   1045                     if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)
   1046                     {
   1047                         phOsalNfc_FreeMemory(
   1048                             gpphLibContext->psBufferedAuth->sRecvData.buffer);
   1049                     }
   1050                     if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)
   1051                     {
   1052                         phOsalNfc_FreeMemory(
   1053                             gpphLibContext->psBufferedAuth->sSendData.buffer);
   1054                     }
   1055                     phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);
   1056                 }
   1057                 gpphLibContext->psBufferedAuth
   1058                     =(phLibNfc_sTransceiveInfo_t *)
   1059                     phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));
   1060                 gpphLibContext->psBufferedAuth->addr = psTransceiveInfo->addr;
   1061                 gpphLibContext->psBufferedAuth->cmd = psTransceiveInfo->cmd;
   1062                 gpphLibContext->psBufferedAuth->sSendData.length
   1063                     = psTransceiveInfo->sSendData.length;
   1064                 gpphLibContext->psBufferedAuth->sRecvData.length
   1065                     = psTransceiveInfo->sRecvData.length;
   1066                 gpphLibContext->psBufferedAuth->sSendData.buffer
   1067                   = (uint8_t *)
   1068                       phOsalNfc_GetMemory(
   1069                       gpphLibContext->psTransInfo->sSendData.length);
   1070 
   1071                 (void)memcpy((void *)
   1072                         (gpphLibContext->psBufferedAuth->sSendData.buffer),
   1073                         (void *)psTransceiveInfo->sSendData.buffer,
   1074                         psTransceiveInfo->sSendData.length);
   1075 
   1076                 gpphLibContext->psBufferedAuth->sRecvData.buffer
   1077                   = (uint8_t *)
   1078                       phOsalNfc_GetMemory(
   1079                         gpphLibContext->psTransInfo->sRecvData.length);
   1080             }
   1081             /*Call the lower layer Transceive function */
   1082             RetVal = phHal4Nfc_Transceive( gpphLibContext->psHwReference,
   1083                                         (phHal_sTransceiveInfo_t*)gpphLibContext->psTransInfo,
   1084                                         (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice,
   1085                                         (pphHal4Nfc_TransceiveCallback_t)
   1086                                         phLibNfc_RemoteDev_Transceive_Cb,
   1087                                         (void* )gpphLibContext);
   1088             if(PHNFCSTATUS(RetVal) == NFCSTATUS_PENDING)
   1089             {
   1090                 /* Copy the upper layer callback pointer and context */
   1091                 gpphLibContext->CBInfo.pClientTransceiveCb = pTransceive_RspCb;
   1092                 gpphLibContext->CBInfo.pClientTranseCntx = pContext;
   1093                 /* Mark the General callback pending status is TRUE */
   1094                 gpphLibContext->status.GenCb_pending_status = TRUE;
   1095                 /*Transceive is in Progress-Used in Release API*/
   1096 
   1097                 /*Update the state machine*/
   1098                 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
   1099             }
   1100         }
   1101         else
   1102         {
   1103             gpphLibContext->status.GenCb_pending_status = FALSE;
   1104             RetVal = NFCSTATUS_FAILED;
   1105         }
   1106     }
   1107     return RetVal;
   1108 }
   1109 /**
   1110 * Response for Remote device transceive.
   1111 */
   1112 STATIC
   1113 void phLibNfc_RemoteDev_Transceive_Cb(void *context,
   1114                                     phHal_sRemoteDevInformation_t *pRmtdev_info,
   1115                                     phNfc_sData_t *response,
   1116                                     NFCSTATUS status
   1117                                     )
   1118 {
   1119     NFCSTATUS             trans_status = NFCSTATUS_SUCCESS;
   1120     phNfc_sData_t         *trans_resp= NULL;
   1121     void                  *pUpper_Context = NULL;
   1122     pphLibNfc_TransceiveCallback_t pUpper_TagNtfCb =
   1123                             gpphLibContext->CBInfo.pClientTransceiveCb;
   1124 
   1125     /*Check valid context is returned or not */
   1126     if((phLibNfc_LibContext_t *)context == gpphLibContext)
   1127     {
   1128         trans_resp = &gpphLibContext->psTransInfo->sRecvData;
   1129 
   1130         pUpper_Context = gpphLibContext->CBInfo.pClientTranseCntx;
   1131         gpphLibContext->status.GenCb_pending_status = FALSE;
   1132 
   1133         /*If DeInit is called during the transceive,
   1134            call the shutdown and return NFCSTATUS_SHUTDOWN*/
   1135         if(gpphLibContext->LibNfcState.next_state
   1136                             == eLibNfcHalStateShutdown)
   1137         {
   1138             phLibNfc_Pending_Shutdown();
   1139             trans_status = NFCSTATUS_SHUTDOWN;
   1140         }
   1141          /* If Disconnect is called return NFCSTATUS_ABORTED */
   1142         else if(eLibNfcHalStateRelease ==
   1143                 gpphLibContext->LibNfcState.next_state)
   1144         {
   1145             trans_status = NFCSTATUS_ABORTED;
   1146         }
   1147         /* If the received lower layer status is not SUCCESS return NFCSTATUS_FAILED */
   1148         else if( NFCSTATUS_SUCCESS == status)
   1149         {
   1150             trans_status = NFCSTATUS_SUCCESS;
   1151         }
   1152         else if((PHNFCSTATUS(status) != NFCSTATUS_SUCCESS) &&
   1153                 (phHal_eMifare_PICC == pRmtdev_info->RemDevType) &&
   1154                 (0x00 != pRmtdev_info->RemoteDevInfo.Iso14443A_Info.Sak))
   1155         {
   1156             gpphLibContext->LastTrancvSuccess = FALSE;
   1157             trans_status = NFCSTATUS_FAILED;
   1158             /* card type is mifare 1k/4k, then reconnect */
   1159             trans_status = phHal4Nfc_Connect(gpphLibContext->psHwReference,
   1160                         pRmtdev_info,
   1161                         (pphHal4Nfc_ConnectCallback_t)
   1162                         phLibNfc_Reconnect_Mifare_Cb,
   1163                         (void *)gpphLibContext);
   1164         }
   1165         else if ((PHNFCSTATUS(status) == PN544_IO_TIMEOUT_RESPONSE) ||
   1166                  (PHNFCSTATUS(status) == NFCSTATUS_RF_TIMEOUT))
   1167         {
   1168             // 0x89, 0x09 HCI response values from PN544 indicate timeout
   1169             trans_status = NFCSTATUS_TARGET_LOST;
   1170         }
   1171         else
   1172         {
   1173             // PN544 did get some reply from tag, just not valid
   1174             trans_status = NFCSTATUS_FAILED;
   1175         }
   1176         /*Update the state machine */
   1177         phLibNfc_UpdateCurState(status,gpphLibContext);
   1178         gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;
   1179         if(NFCSTATUS_PENDING != trans_status)
   1180         {
   1181             /* Tranceive over */
   1182             PHDBG_INFO("LibNfc:TXRX Callback-Update the Transceive responce");
   1183             if (NULL != pUpper_TagNtfCb)
   1184             {
   1185                 if(trans_status == NFCSTATUS_SUCCESS)
   1186                 {
   1187                     gpphLibContext->LastTrancvSuccess = TRUE;
   1188                     pUpper_Context = gpphLibContext->CBInfo.pClientTranseCntx;
   1189                     trans_resp->buffer = response->buffer;
   1190                     trans_resp->length = response->length;
   1191                             /* Notify the upper layer */
   1192                     PHDBG_INFO("LibNfc:Transceive Complete");
   1193                     /* Notify the Transceive Completion to upper layer */
   1194                     gpphLibContext->CBInfo.pClientTransceiveCb(pUpper_Context,
   1195                                 (uint32_t)pRmtdev_info,
   1196                                 trans_resp,
   1197                                 trans_status);
   1198                 }
   1199                 else
   1200                 {
   1201                     gpphLibContext->LastTrancvSuccess = FALSE;
   1202                     pUpper_Context = gpphLibContext->CBInfo.pClientTranseCntx;
   1203                     trans_resp->length = 0;
   1204                             /* Notify the upper layer */
   1205                     PHDBG_INFO("LibNfc:Transceive Complete");
   1206                     /* Notify the Transceive Completion to upper layer */
   1207                     gpphLibContext->CBInfo.pClientTransceiveCb(pUpper_Context,
   1208                                 (uint32_t)pRmtdev_info,
   1209                                 trans_resp,
   1210                                 trans_status);
   1211                 }
   1212             }
   1213        }
   1214 
   1215     }
   1216     else
   1217     {    /*exception: wrong context pointer returned*/
   1218         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
   1219     }
   1220 
   1221     return;
   1222 }
   1223 /**
   1224 * Interface to configure P2P configurations.
   1225 */
   1226 NFCSTATUS
   1227 phLibNfc_Mgt_SetP2P_ConfigParams(phLibNfc_sNfcIPCfg_t*		pConfigInfo,
   1228                                 pphLibNfc_RspCb_t			pConfigRspCb,
   1229                                 void*						pContext
   1230                                 )
   1231 {
   1232     NFCSTATUS RetVal = NFCSTATUS_FAILED;
   1233     /* LibNfc Initialized or not */
   1234     if((NULL == gpphLibContext)||
   1235         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
   1236     {
   1237         RetVal = NFCSTATUS_NOT_INITIALISED;
   1238     }/* Check for valid parameters */
   1239     else if((NULL == pConfigInfo) || (NULL == pConfigRspCb)
   1240         || (NULL == pContext))
   1241     {
   1242         RetVal= NFCSTATUS_INVALID_PARAMETER;
   1243     }
   1244     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
   1245     {
   1246         RetVal = NFCSTATUS_SHUTDOWN;
   1247     }
   1248     else if(TRUE == gpphLibContext->status.GenCb_pending_status)
   1249     { /*Previous callback is pending */
   1250         RetVal = NFCSTATUS_BUSY;
   1251     }
   1252     else
   1253     {
   1254         if(eLibNfcHalStatePresenceChk !=
   1255                 gpphLibContext->LibNfcState.next_state)
   1256         {
   1257             phHal_uConfig_t uConfig;
   1258             /* copy General bytes of Max length = 48 bytes */
   1259             (void)memcpy((void *)&(uConfig.nfcIPConfig.generalBytes),
   1260                     (void *)pConfigInfo->generalBytes,
   1261                     pConfigInfo->generalBytesLength);
   1262             /* also copy the General Bytes length*/
   1263             uConfig.nfcIPConfig.generalBytesLength = pConfigInfo->generalBytesLength;
   1264 
   1265             RetVal = phHal4Nfc_ConfigParameters(
   1266                                 gpphLibContext->psHwReference,
   1267                                 NFC_P2P_CONFIG,
   1268                                 &uConfig,
   1269                                 phLibNfc_Mgt_SetP2P_ConfigParams_Cb,
   1270                                 (void *)gpphLibContext
   1271                                 );
   1272         }
   1273         else
   1274         {
   1275              gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb= NULL;
   1276              RetVal = NFCSTATUS_PENDING;
   1277         }
   1278         if(NFCSTATUS_PENDING == RetVal)
   1279         {
   1280             /* save the context and callback for later use */
   1281             gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb = pConfigRspCb;
   1282             gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx = pContext;
   1283             gpphLibContext->status.GenCb_pending_status=TRUE;
   1284             /* Next state is configured */
   1285             gpphLibContext->LibNfcState.next_state =eLibNfcHalStateConfigReady;
   1286         }
   1287         else
   1288         {
   1289             RetVal = NFCSTATUS_FAILED;
   1290         }
   1291     }
   1292     return RetVal;
   1293 }
   1294 /**
   1295 * Response callback for P2P configurations.
   1296 */
   1297 STATIC void phLibNfc_Mgt_SetP2P_ConfigParams_Cb(void     *context,
   1298                                         NFCSTATUS status)
   1299 {
   1300         pphLibNfc_RspCb_t       pClientCb=NULL;
   1301     void                    *pUpperLayerContext=NULL;
   1302      /* Check for the context returned by below layer */
   1303     if((phLibNfc_LibContext_t *)context != gpphLibContext)
   1304     {   /*wrong context returned*/
   1305         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
   1306     }
   1307     else
   1308     {
   1309         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
   1310         {   /*shutdown called before completion of this api allow
   1311             shutdown to happen */
   1312             phLibNfc_Pending_Shutdown();
   1313             status = NFCSTATUS_SHUTDOWN;
   1314         }
   1315         else
   1316         {
   1317             gpphLibContext->status.GenCb_pending_status = FALSE;
   1318             if(NFCSTATUS_SUCCESS != status)
   1319             {
   1320                 status = NFCSTATUS_FAILED;
   1321             }
   1322             else
   1323             {
   1324                 status = NFCSTATUS_SUCCESS;
   1325             }
   1326         }
   1327         /*update the current state */
   1328         phLibNfc_UpdateCurState(status,gpphLibContext);
   1329 
   1330         pClientCb = gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb;
   1331         pUpperLayerContext = gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx;
   1332 
   1333         gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb = NULL;
   1334         gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx = NULL;
   1335         if (NULL != pClientCb)
   1336         {
   1337             /* Notify to upper layer status of configure operation */
   1338             pClientCb(pUpperLayerContext, status);
   1339         }
   1340     }
   1341     return;
   1342 }
   1343 
   1344 
   1345 
   1346 
   1347 
   1348 
   1349 
   1350 
   1351 
   1352 
   1353