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