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