2 * Copyright (C) 2010 NXP Semiconductors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * \file phLibNfc_discovery.c
20 * Project: NFC FRI 1.1
22 * $Date: Mon Mar 1 19:02:41 2010 $
25 * $Aliases: NFC_FRI1.1_WK1008_SDK,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1007_SDK,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 $
30 ************************* Header Files ****************************************
33 #include <phLibNfcStatus.h>
35 #include <phHal4Nfc.h>
36 #include <phOsalNfc.h>
37 #include <phLibNfc_Internal.h>
38 #include <phLibNfc_ndef_raw.h>
39 #include <phLibNfc_initiator.h>
40 #include <phLibNfc_discovery.h>
43 *************************** Macro's ****************************************
46 #ifndef STATIC_DISABLE
53 *************************** Global Variables **********************************
59 *************************** Static Function Declaration ***********************
63 /*Remote device Presence check callback*/
64 STATIC void phLibNfc_RemoteDev_CheckPresence_Cb(void *context,
67 /**Used for presence chk incase of mifare std tags*/
68 STATIC void phLibNfc_ChkPresence_Trcv_Cb(
70 phHal_sRemoteDevInformation_t *psRemoteDevInfo,
71 phNfc_sData_t *response,
76 *************************** Function Definitions ******************************
78 void phLibNfc_config_discovery_cb(void *context,
82 if((phLibNfc_LibContext_t *)context == gpphLibContext)
83 { /*check for same context*/
85 if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
87 /*If shutdown called in between allow shutdown to happen*/
88 phLibNfc_Pending_Shutdown();
89 status = NFCSTATUS_SHUTDOWN;
93 gpphLibContext->status.GenCb_pending_status = FALSE;
94 gpphLibContext->status.DiscEnbl_status = FALSE;
95 phLibNfc_UpdateCurState(status,gpphLibContext);
97 if(gpphLibContext->status.Discovery_pending_status == TRUE)
99 NFCSTATUS RetStatus = NFCSTATUS_FAILED;
100 /* Application has called discovery before receiving this callback,
101 so NO notification to the upper layer, instead lower layer
102 discovery is called */
103 gpphLibContext->status.Discovery_pending_status = FALSE;
104 RetStatus = phHal4Nfc_ConfigureDiscovery(
105 gpphLibContext->psHwReference,
106 gpphLibContext->eLibNfcCfgMode,
107 &gpphLibContext->sADDconfig,
109 phLibNfc_config_discovery_cb,
110 (void *)gpphLibContext);
111 if (NFCSTATUS_PENDING == RetStatus)
113 (void)phLibNfc_UpdateNextState(gpphLibContext,
114 eLibNfcHalStateConfigReady);
115 gpphLibContext->status.GenCb_pending_status = TRUE;
116 gpphLibContext->status.DiscEnbl_status = TRUE;
120 status = NFCSTATUS_FAILED;
123 #endif /* #ifdef RESTART_CFG */
125 } /*End of if-context check*/
127 { /*exception: wrong context pointer returned*/
128 phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
129 status = NFCSTATUS_FAILED;
131 if(gpphLibContext->CBInfo.pClientDisConfigCb!=NULL)
133 gpphLibContext->CBInfo.pClientDisConfigCb(gpphLibContext->CBInfo.pClientDisCfgCntx,status);
134 gpphLibContext->CBInfo.pClientDisConfigCb=NULL;
139 * Configure Discovery Modes.
140 * This function is used to configure ,start and stop the discovery wheel.
142 NFCSTATUS phLibNfc_Mgt_ConfigureDiscovery (
143 phLibNfc_eDiscoveryConfigMode_t DiscoveryMode,
144 phLibNfc_sADD_Cfg_t sADDSetup,
145 pphLibNfc_RspCb_t pConfigDiscovery_RspCb,
149 NFCSTATUS RetVal = NFCSTATUS_FAILED;
150 phHal_sADD_Cfg_t *psADDConfig;
151 psADDConfig = (phHal_sADD_Cfg_t *)&(sADDSetup);
154 if((NULL == gpphLibContext) ||
155 (gpphLibContext->LibNfcState.cur_state
156 == eLibNfcHalStateShutdown))
158 /*Lib Nfc not initialized*/
159 RetVal = NFCSTATUS_NOT_INITIALISED;
161 /* Check for Valid parameters*/
162 else if((NULL == pContext) || (NULL == pConfigDiscovery_RspCb))
164 RetVal= NFCSTATUS_INVALID_PARAMETER;
166 else if(gpphLibContext->LibNfcState.next_state
167 == eLibNfcHalStateShutdown)
169 RetVal= NFCSTATUS_SHUTDOWN;
173 gpphLibContext->eLibNfcCfgMode =DiscoveryMode;
174 gpphLibContext->sADDconfig = sADDSetup;
175 if(gpphLibContext->status.DiscEnbl_status != TRUE)
178 /* call lower layer config API for the discovery
179 configuration sent by the application */
180 RetVal = phHal4Nfc_ConfigureDiscovery ( gpphLibContext->psHwReference,
184 phLibNfc_config_discovery_cb,
185 (void*)gpphLibContext);
186 if(PHNFCSTATUS(RetVal) == NFCSTATUS_PENDING)
188 gpphLibContext->status.DiscEnbl_status = TRUE;
189 /* Copy discovery callback and its context */
190 gpphLibContext->CBInfo.pClientDisConfigCb = pConfigDiscovery_RspCb;
191 gpphLibContext->CBInfo.pClientDisCfgCntx = pContext;
192 gpphLibContext->status.GenCb_pending_status = TRUE;
193 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConfigReady;
197 RetVal=NFCSTATUS_FAILED;
203 RetVal=NFCSTATUS_BUSY;
210 * Check for target presence.
211 * Checks given target is present in RF filed or not
213 NFCSTATUS phLibNfc_RemoteDev_CheckPresence( phLibNfc_Handle hTargetDev,
214 pphLibNfc_RspCb_t pPresenceChk_RspCb,
218 NFCSTATUS RetVal = NFCSTATUS_FAILED;
219 phHal_sRemoteDevInformation_t *ps_rem_dev_info = NULL;
220 /* Check for valid sate */
221 if((NULL == gpphLibContext) ||
222 (gpphLibContext->LibNfcState.cur_state
223 == eLibNfcHalStateShutdown))
225 RetVal = NFCSTATUS_NOT_INITIALISED;
227 /* Check for valid parameters*/
228 else if((NULL == pRspCbCtx) || (NULL == pPresenceChk_RspCb)
229 || (hTargetDev == 0) )
231 RetVal= NFCSTATUS_INVALID_PARAMETER;
233 /* Check for DeInit call*/
234 else if(gpphLibContext->LibNfcState.next_state
235 == eLibNfcHalStateShutdown)
237 RetVal = NFCSTATUS_SHUTDOWN;
239 /* Check target is connected or not */
240 else if( gpphLibContext->Connected_handle == 0)
242 RetVal = NFCSTATUS_TARGET_NOT_CONNECTED;
244 /* Check given handle is valid or not*/
245 else if(hTargetDev != gpphLibContext->Connected_handle)
247 RetVal = NFCSTATUS_INVALID_HANDLE;
249 #ifdef LLCP_TRANSACT_CHANGES
250 else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
251 && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
253 RetVal= NFCSTATUS_BUSY;
255 #endif /* #ifdef LLCP_TRANSACT_CHANGES */
258 ps_rem_dev_info = (phHal_sRemoteDevInformation_t *)
259 gpphLibContext->Connected_handle;
260 if((phHal_eMifare_PICC == ps_rem_dev_info->RemDevType)
261 &&(0 != ps_rem_dev_info->RemoteDevInfo.Iso14443A_Info.Sak)
262 &&(TRUE == gpphLibContext->LastTrancvSuccess))
265 RetVal = phHal4Nfc_Transceive(
266 gpphLibContext->psHwReference,
267 gpphLibContext->psBufferedAuth,
268 (phHal_sRemoteDevInformation_t *)
269 gpphLibContext->Connected_handle,
270 (pphHal4Nfc_TransceiveCallback_t )
271 phLibNfc_ChkPresence_Trcv_Cb,
272 (void *)gpphLibContext
278 /* Call lower layer PresenceCheck function */
279 RetVal = phHal4Nfc_PresenceCheck(gpphLibContext->psHwReference,
280 phLibNfc_RemoteDev_CheckPresence_Cb,
281 (void *)gpphLibContext);
283 if( NFCSTATUS_PENDING == PHNFCSTATUS(RetVal))
285 gpphLibContext->CBInfo.pClientPresChkCb = pPresenceChk_RspCb;
286 gpphLibContext->CBInfo.pClientPresChkCntx = pRspCbCtx;
287 /* Mark General callback pending status as TRUE*/
288 gpphLibContext->status.GenCb_pending_status = TRUE;
290 /* Update the state machine*/
291 gpphLibContext->LibNfcState.next_state = eLibNfcHalStatePresenceChk;
293 else /* If return value is internal error(other than pending ) return NFCSTATUS_FAILED*/
295 RetVal = NFCSTATUS_FAILED;
302 * Response Callback for Remote device Presence Check.
305 void phLibNfc_RemoteDev_CheckPresence_Cb(void *context,
308 void *pUpperLayerContext=NULL;
309 pphLibNfc_RspCb_t pClientCb=NULL;
311 /*check valid context is returned or not*/
312 if((phLibNfc_LibContext_t *)context != gpphLibContext)
314 /*exception: wrong context pointer returned*/
315 phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
317 /* Mark general callback pending status as FALSE*/
318 gpphLibContext->status.GenCb_pending_status = FALSE;
319 pClientCb =gpphLibContext->CBInfo.pClientPresChkCb ;
320 pUpperLayerContext = gpphLibContext->CBInfo.pClientPresChkCntx;
321 gpphLibContext->CBInfo.pClientPresChkCntx = NULL;
322 gpphLibContext->CBInfo.pClientPresChkCb =NULL;
323 /* Check DeInit call is called, if yes call pending
324 shutdown and return NFCSTATUS_SHUTDOWN */
325 if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
327 phLibNfc_Pending_Shutdown();
328 status = NFCSTATUS_SHUTDOWN;
332 if (status != NFCSTATUS_SUCCESS)
334 /*If status is other than SUCCESS (Internal error) return
335 NFCSTATUS_TARGET_LOST */
336 status= NFCSTATUS_TARGET_LOST;
340 status = NFCSTATUS_SUCCESS;
343 /* Update the current state */
344 phLibNfc_UpdateCurState(status,gpphLibContext);
345 if(NULL != pClientCb)
347 /* call the upper layer callback */
348 pClientCb(pUpperLayerContext,status);
353 /**Used for presence chk incase of mifare std tags*/
354 STATIC void phLibNfc_ChkPresence_Trcv_Cb(
356 phHal_sRemoteDevInformation_t *psRemoteDevInfo,
357 phNfc_sData_t *response,
361 PHNFC_UNUSED_VARIABLE(psRemoteDevInfo);
362 PHNFC_UNUSED_VARIABLE(response);
363 phLibNfc_RemoteDev_CheckPresence_Cb(context,status);