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