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_Target.c
20 * Project: NFC FRI 1.1
22 * $Date: Thu Oct 15 15:24:43 2009 $
25 * $Aliases: NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK944_SDK,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK949_SDK_INT,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK1003_SDK,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,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 **********************************
57 *************************** Static Function Declaration ***********************
60 /* Remote device receive callback */
61 STATIC void phLibNfc_RemoteDev_Receive_Cb(
63 phNfc_sData_t *rec_rsp_data,
67 /* Remote device Send callback */
68 STATIC void phLibNfc_RemoteDev_Send_Cb(
74 *************************** Function Definitions ******************************
78 * Interface used to receive data from initiator at target side during P2P
81 NFCSTATUS phLibNfc_RemoteDev_Receive(phLibNfc_Handle hRemoteDevice,
82 pphLibNfc_Receive_RspCb_t pReceiveRspCb,
86 NFCSTATUS RetVal = NFCSTATUS_FAILED;
87 /*Check Lib Nfc is initialized*/
88 if((NULL == gpphLibContext)||
89 (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
91 RetVal = NFCSTATUS_NOT_INITIALISED;
92 }/*Check application has sent valid parameters*/
93 else if (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateRelease)
95 RetVal = NFCSTATUS_DESELECTED;
97 else if((NULL == pReceiveRspCb)
99 || (0 == hRemoteDevice))
101 RetVal= NFCSTATUS_INVALID_PARAMETER;
103 else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
105 RetVal = NFCSTATUS_SHUTDOWN;
107 else if((TRUE == gpphLibContext->status.GenCb_pending_status)
108 ||(NULL!=gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb)
109 ||(phHal_eNfcIP1_Target==
110 ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType))
112 /*Previous callback is pending or if initiator uses this api */
113 RetVal = NFCSTATUS_REJECTED;
114 }/*check for Discovered initiator handle and handle sent by application */
115 else if(gpphLibContext->sNfcIp_Context.Rem_Initiator_Handle != hRemoteDevice)
117 RetVal= NFCSTATUS_INVALID_DEVICE;
119 #ifdef LLCP_TRANSACT_CHANGES
120 else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
121 && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
123 RetVal = NFCSTATUS_BUSY;
125 #endif /* #ifdef LLCP_TRANSACT_CHANGES */
128 if(eLibNfcHalStatePresenceChk ==
129 gpphLibContext->LibNfcState.next_state)
131 gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb = NULL;
132 RetVal = NFCSTATUS_PENDING;
136 /*Call below layer receive and register the callback with it*/
137 PHDBG_INFO("LibNfc:P2P Receive In Progress");
138 RetVal =phHal4Nfc_Receive(
139 gpphLibContext->psHwReference,
140 (phHal4Nfc_TransactInfo_t*)gpphLibContext->psTransInfo,
141 (pphLibNfc_Receive_RspCb_t)
142 phLibNfc_RemoteDev_Receive_Cb,
143 (void *)gpphLibContext
146 if(NFCSTATUS_PENDING == RetVal)
148 /*Update the Next state as Transaction*/
149 gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb= pReceiveRspCb;
150 gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx = pContext;
151 gpphLibContext->status.GenCb_pending_status=TRUE;
152 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
156 RetVal = NFCSTATUS_FAILED;
162 * Response callback for Remote Device Receive.
164 STATIC void phLibNfc_RemoteDev_Receive_Cb(
166 phNfc_sData_t *rec_rsp_data,
170 pphLibNfc_Receive_RspCb_t pClientCb=NULL;
172 phLibNfc_LibContext_t *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)context;
173 void *pUpperLayerContext=NULL;
175 /* Check for the context returned by below layer */
176 if(pLibNfc_Ctxt != gpphLibContext)
177 { /*wrong context returned*/
178 phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
182 pClientCb = gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb;
183 pUpperLayerContext = gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx;
185 gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb = NULL;
186 gpphLibContext->sNfcIp_Context.pClientNfcIpRxCntx = NULL;
187 gpphLibContext->status.GenCb_pending_status = FALSE;
188 if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
189 { /*shutdown called before completion of P2P receive allow
190 shutdown to happen */
191 phLibNfc_Pending_Shutdown();
192 status = NFCSTATUS_SHUTDOWN;
194 else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
196 status = NFCSTATUS_ABORTED;
200 if((NFCSTATUS_SUCCESS != status) &&
201 (PHNFCSTATUS(status) != NFCSTATUS_MORE_INFORMATION ) )
203 /*During p2p receive operation initiator was removed
204 from RF field of target*/
205 status = NFCSTATUS_DESELECTED;
209 status = NFCSTATUS_SUCCESS;
212 /* Update current state */
213 phLibNfc_UpdateCurState(status,gpphLibContext);
215 if (NULL != pClientCb)
217 /*Notify to upper layer status and No. of bytes
219 pClientCb(pUpperLayerContext, rec_rsp_data, status);
226 * Interface used to send data from target to initiator during P2P communication
229 phLibNfc_RemoteDev_Send(
230 phLibNfc_Handle hRemoteDevice,
231 phNfc_sData_t * pTransferData,
232 pphLibNfc_RspCb_t pSendRspCb,
236 NFCSTATUS RetVal = NFCSTATUS_FAILED;
237 /*Check Lib Nfc stack is initilized*/
238 if((NULL == gpphLibContext)||
239 (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
241 RetVal = NFCSTATUS_NOT_INITIALISED;
243 else if (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateRelease)
245 RetVal = NFCSTATUS_DESELECTED;
247 /*Check application has sent the valid parameters*/
248 else if((NULL == pTransferData)
249 || (NULL == pSendRspCb)
250 || (NULL == pTransferData->buffer)
251 || (0 == pTransferData->length)
252 || (NULL == pContext)
253 || (0 == hRemoteDevice))
255 RetVal= NFCSTATUS_INVALID_PARAMETER;
257 else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
259 RetVal = NFCSTATUS_SHUTDOWN;
261 else if((TRUE == gpphLibContext->status.GenCb_pending_status)
262 ||(NULL!=gpphLibContext->sNfcIp_Context.pClientNfcIpRxCb)
263 ||(phHal_eNfcIP1_Target==
264 ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType))
266 /*Previous callback is pending or local device is Initiator
268 RetVal = NFCSTATUS_REJECTED;
269 }/*Check for Discovered initiator handle and handle sent by application */
270 else if(gpphLibContext->sNfcIp_Context.Rem_Initiator_Handle != hRemoteDevice)
272 RetVal= NFCSTATUS_INVALID_DEVICE;
274 else if((NULL!=gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb))
276 RetVal =NFCSTATUS_BUSY ;
278 #ifdef LLCP_TRANSACT_CHANGES
279 else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)
280 && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))
282 RetVal= NFCSTATUS_BUSY;
284 #endif /* #ifdef LLCP_TRANSACT_CHANGES */
287 if(eLibNfcHalStatePresenceChk ==
288 gpphLibContext->LibNfcState.next_state)
290 gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb = NULL;
291 RetVal = NFCSTATUS_PENDING;
295 if(gpphLibContext->psTransInfo!=NULL)
297 (void)memset(gpphLibContext->psTransInfo,
299 sizeof(phLibNfc_sTransceiveInfo_t));
301 gpphLibContext->psTransInfo->addr =UNKNOWN_BLOCK_ADDRESS;
302 /*pointer to send data */
303 gpphLibContext->psTransInfo->sSendData.buffer =
304 pTransferData->buffer;
305 /*size of send data*/
306 gpphLibContext->psTransInfo->sSendData.length =
307 pTransferData->length;
309 /* Copy remote device type */
310 gpphLibContext->sNfcIp_Context.TransactInfoRole.remotePCDType =
311 ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType;
312 /*Call Hal4 Send API and register callback with it*/
313 PHDBG_INFO("LibNfc:P2P send In Progress");
314 RetVal= phHal4Nfc_Send(
315 gpphLibContext->psHwReference,
316 &(gpphLibContext->sNfcIp_Context.TransactInfoRole),
317 gpphLibContext->psTransInfo->sSendData,
319 phLibNfc_RemoteDev_Send_Cb,
320 (void *)gpphLibContext
324 if(NFCSTATUS_PENDING == RetVal)
326 /* Update next state to transaction */
327 gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb= pSendRspCb;
328 gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx = pContext;
329 gpphLibContext->status.GenCb_pending_status=TRUE;
330 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;
334 RetVal = NFCSTATUS_FAILED;
341 * Response callback for Remote Device Send.
343 STATIC void phLibNfc_RemoteDev_Send_Cb(
348 pphLibNfc_RspCb_t pClientCb=NULL;
349 phLibNfc_LibContext_t *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)Context;
350 void *pUpperLayerContext=NULL;
352 /* Check for the context returned by below layer */
353 if(pLibNfc_Ctxt != gpphLibContext)
354 { /*wrong context returned*/
355 phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
359 if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
360 { /*shutdown called before completion p2p send allow
361 shutdown to happen */
362 phLibNfc_Pending_Shutdown();
363 status = NFCSTATUS_SHUTDOWN;
365 else if(eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)
367 status = NFCSTATUS_ABORTED;
371 gpphLibContext->status.GenCb_pending_status = FALSE;
372 if((NFCSTATUS_SUCCESS != status) &&
373 (PHNFCSTATUS(status) != NFCSTATUS_MORE_INFORMATION ) )
375 /*During p2p send operation initator was not present in RF
377 status = NFCSTATUS_DESELECTED;
381 status = NFCSTATUS_SUCCESS;
384 /* Update current state */
385 phLibNfc_UpdateCurState(status,gpphLibContext);
387 pClientCb = gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb;
388 pUpperLayerContext = gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx;
390 gpphLibContext->sNfcIp_Context.pClientNfcIpTxCb = NULL;
391 gpphLibContext->sNfcIp_Context.pClientNfcIpTxCntx = NULL;
392 if (NULL != pClientCb)
394 /* Notify to upper layer status and No. of bytes
395 actually written or send to initiator */
396 pClientCb(pUpperLayerContext, status);