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