Home | History | Annotate | Download | only in src
      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 * \file  phHal4Nfc_Emulation.c
     18 * \brief Hal4 Emulation source.
     19 *
     20 * Project: NFC-FRI 1.1
     21 *
     22 * $Date: Wed May 26 18:03:59 2010 $
     23 * $Author: ing07385 $
     24 * $Revision: 1.35 $
     25 * $Aliases: NFC_FRI1.1_WK1023_R35_1 $
     26 *
     27 */
     28 
     29 /* ---------------------------Include files ------------------------------------*/
     30 #include <phHciNfc.h>
     31 #include <phHal4Nfc.h>
     32 #include <phHal4Nfc_Internal.h>
     33 #include <phOsalNfc.h>
     34 
     35 /* ------------------------------- Macros ------------------------------------*/
     36 
     37 /* Note : Macros required and used  only in this module to be declared here*/
     38 
     39 
     40 /* --------------------Structures and enumerations --------------------------*/
     41 
     42 
     43 /*Event Notification handler for emulation*/
     44 void phHal4Nfc_HandleEmulationEvent(
     45                                     phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
     46                                     void *pInfo
     47                                     )
     48 {
     49     phNfc_sNotificationInfo_t *psNotificationInfo = (phNfc_sNotificationInfo_t *)
     50                                                                             pInfo;
     51     phHal4Nfc_NotificationInfo_t uNotificationInfo = {NULL};
     52     /*Pass on Event notification info from Hci to Upper layer*/
     53     uNotificationInfo.psEventInfo = psNotificationInfo->info;
     54     if(NULL != Hal4Ctxt->sUpperLayerInfo.pEventNotification)
     55     {
     56         Hal4Ctxt->sUpperLayerInfo.pEventNotification(
     57             Hal4Ctxt->sUpperLayerInfo.EventNotificationCtxt,
     58             psNotificationInfo->type,
     59             uNotificationInfo,
     60             NFCSTATUS_SUCCESS
     61             );
     62     }
     63     else/*No Event notification handler registered*/
     64     {
     65         /*Use default handler to notify to the upper layer*/
     66         if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
     67         {
     68             Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
     69                 Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
     70                 psNotificationInfo->type,
     71                 uNotificationInfo,
     72                 NFCSTATUS_SUCCESS
     73                 );
     74         }
     75     }
     76     return;
     77 }
     78 
     79 /*  Switch mode from Virtual to Wired or Vice Versa for SMX.
     80 */
     81 NFCSTATUS phHal4Nfc_Switch_SMX_Mode(
     82                                     phHal_sHwReference_t      *psHwReference,
     83                                     phHal_eSmartMX_Mode_t      smx_mode,
     84                                     pphHal4Nfc_GenCallback_t   pSwitchModecb,
     85                                     void                      *pContext
     86                                     )
     87 {
     88     NFCSTATUS CfgStatus = NFCSTATUS_PENDING;
     89     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
     90     static phHal_sADD_Cfg_t sSmxCfg;
     91 
     92     /*NULL  checks*/
     93     if((NULL == psHwReference) || (NULL == pSwitchModecb))
     94     {
     95         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
     96         CfgStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
     97     }
     98     /*Check Initialised state*/
     99     else if((NULL == psHwReference->hal_context)
    100                         || (((phHal4Nfc_Hal4Ctxt_t *)
    101                                 psHwReference->hal_context)->Hal4CurrentState
    102                                                < eHal4StateOpenAndReady)
    103                         || (((phHal4Nfc_Hal4Ctxt_t *)
    104                                 psHwReference->hal_context)->Hal4NextState
    105                                                == eHal4StateClosed))
    106     {
    107         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    108         CfgStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
    109     }
    110     else
    111     {
    112         Hal4Ctxt = psHwReference->hal_context;
    113         /*Previous POLL Config has not completed or device is connected,
    114           do not allow poll*/
    115         if(Hal4Ctxt->Hal4NextState == eHal4StateConfiguring)
    116         {
    117             PHDBG_INFO("Hal4:Configuration in progress.Returning status Busy");
    118             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_BUSY);
    119         }
    120         else if(Hal4Ctxt->Hal4CurrentState >= eHal4StateOpenAndReady)
    121         {
    122             /**If config discovery has not been called prior to this ,allocate
    123                ADD Context here*/
    124             if (NULL == Hal4Ctxt->psADDCtxtInfo)
    125             {
    126                 Hal4Ctxt->psADDCtxtInfo= (pphHal4Nfc_ADDCtxtInfo_t)
    127                     phOsalNfc_GetMemory((uint32_t)
    128                                         (sizeof(phHal4Nfc_ADDCtxtInfo_t)));
    129                 if(NULL != Hal4Ctxt->psADDCtxtInfo)
    130                 {
    131                     (void)memset(Hal4Ctxt->psADDCtxtInfo,
    132                                     0,sizeof(phHal4Nfc_ADDCtxtInfo_t));
    133                 }
    134             }
    135             if(NULL == Hal4Ctxt->psADDCtxtInfo)
    136             {
    137                 phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
    138                 CfgStatus= PHNFCSTVAL(CID_NFC_HAL ,
    139                                         NFCSTATUS_INSUFFICIENT_RESOURCES);
    140             }
    141             else
    142             {
    143                 /* Register Upper layer context */
    144                 Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    145                 /* Switch request to Wired mode */
    146                 if(eSmartMx_Wired == smx_mode)
    147                 {
    148                     if(Hal4Ctxt->Hal4CurrentState
    149                                     == eHal4StateTargetConnected)
    150                     {
    151                         PHDBG_INFO("Hal4:In Connected state.Returning Busy");
    152                         CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_BUSY);
    153                     }
    154                     /*It is Mandatory to register a listener before switching
    155                       to wired mode*/
    156                     else if(NULL ==
    157                             Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification)
    158                     {
    159                         CfgStatus = PHNFCSTVAL(CID_NFC_HAL ,
    160                             NFCSTATUS_FAILED);
    161                     }
    162                     else
    163                     {
    164                         Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
    165                         Hal4Ctxt->psADDCtxtInfo->smx_discovery = TRUE;
    166                         sSmxCfg.PollDevInfo.PollCfgInfo.EnableIso14443A = TRUE;
    167                         /*Switch mode to wired*/
    168                         CfgStatus = phHciNfc_Switch_SmxMode (
    169                                                     Hal4Ctxt->psHciHandle,
    170                                                     psHwReference,
    171                                                     smx_mode,
    172                                                     &sSmxCfg
    173                                                     );
    174                     }
    175                 }
    176                 else
    177                 {
    178                     Hal4Ctxt->psADDCtxtInfo->smx_discovery = FALSE;
    179                     /*Switch mode to virtual or off*/
    180                     CfgStatus = phHciNfc_Switch_SmxMode (
    181                                         Hal4Ctxt->psHciHandle,
    182                                         psHwReference,
    183                                         smx_mode,
    184                                         &(Hal4Ctxt->psADDCtxtInfo->sADDCfg)
    185                                         );
    186                 }
    187 
    188                 /* Change the State of the HAL only if Switch mode Returns
    189                    Success*/
    190                 if ( NFCSTATUS_PENDING == CfgStatus )
    191                 {
    192                     Hal4Ctxt->Hal4NextState = eHal4StateConfiguring;
    193                     Hal4Ctxt->sUpperLayerInfo.pConfigCallback
    194                         = pSwitchModecb;
    195                 }
    196             }
    197         }
    198         else/*Return Status not initialised*/
    199         {
    200             phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    201             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
    202         }
    203     }
    204     return CfgStatus;
    205 }
    206 
    207 
    208 
    209 /*  Switch mode for Swp.*/
    210 NFCSTATUS phHal4Nfc_Switch_Swp_Mode(
    211                                     phHal_sHwReference_t      *psHwReference,
    212                                     phHal_eSWP_Mode_t          swp_mode,
    213                                     pphHal4Nfc_GenCallback_t   pSwitchModecb,
    214                                     void                      *pContext
    215                                     )
    216 {
    217     NFCSTATUS CfgStatus = NFCSTATUS_PENDING;
    218     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    219     /*NULL checks*/
    220     if(NULL == psHwReference
    221         || NULL == pSwitchModecb
    222         )
    223     {
    224         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    225         CfgStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
    226     }
    227     /*Check Initialised state*/
    228     else if((NULL == psHwReference->hal_context)
    229                         || (((phHal4Nfc_Hal4Ctxt_t *)
    230                                 psHwReference->hal_context)->Hal4CurrentState
    231                                                < eHal4StateOpenAndReady)
    232                         || (((phHal4Nfc_Hal4Ctxt_t *)
    233                                 psHwReference->hal_context)->Hal4NextState
    234                                                == eHal4StateClosed))
    235     {
    236         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    237         CfgStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
    238     }
    239     else
    240     {
    241         Hal4Ctxt = psHwReference->hal_context;
    242         /*Previous POLL CFG has not completed or device is connected,
    243           do not allow poll*/
    244         if(Hal4Ctxt->Hal4NextState == eHal4StateConfiguring)
    245         {
    246             PHDBG_INFO("Hal4:Configuration in progress.Returning status Busy");
    247             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_BUSY);
    248         }
    249         else if(Hal4Ctxt->Hal4CurrentState >= eHal4StateOpenAndReady)
    250         {
    251              /**If config discovery has not been called prior to this ,allocate
    252                ADD Context here*/
    253             if (NULL == Hal4Ctxt->psADDCtxtInfo)
    254             {
    255                 Hal4Ctxt->psADDCtxtInfo= (pphHal4Nfc_ADDCtxtInfo_t)
    256                     phOsalNfc_GetMemory((uint32_t)
    257                                         (sizeof(phHal4Nfc_ADDCtxtInfo_t)));
    258                 if(NULL != Hal4Ctxt->psADDCtxtInfo)
    259                 {
    260                     (void)memset(Hal4Ctxt->psADDCtxtInfo,
    261                                     0,sizeof(phHal4Nfc_ADDCtxtInfo_t));
    262                 }
    263             }
    264             if(NULL == Hal4Ctxt->psADDCtxtInfo)
    265             {
    266                 phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
    267                 CfgStatus= PHNFCSTVAL(CID_NFC_HAL ,
    268                                         NFCSTATUS_INSUFFICIENT_RESOURCES);
    269             }
    270             else
    271             {
    272                 /* Register Upper layer context */
    273                 Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
    274                 /*Switch mode to On or off*/
    275                 CfgStatus = phHciNfc_Switch_SwpMode(
    276                                     Hal4Ctxt->psHciHandle,
    277                                     psHwReference,
    278                                     swp_mode
    279                                     );
    280 
    281                 /* Change the State of the HAL only if Switch mode Returns
    282                    Success*/
    283                 if ( NFCSTATUS_PENDING == CfgStatus )
    284                 {
    285                     Hal4Ctxt->Hal4NextState = eHal4StateConfiguring;
    286                     Hal4Ctxt->sUpperLayerInfo.pConfigCallback
    287                         = pSwitchModecb;
    288                 }
    289             }
    290         }
    291         else/*Return Status not initialised*/
    292         {
    293             phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    294             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
    295         }
    296     }
    297     return CfgStatus;
    298 }
    299 
    300 #ifdef FULL_HAL4_EMULATION_ENABLE
    301 /*  Switch Emulation mode ON or OFF.*/
    302 NFCSTATUS phHal4Nfc_Host_Emulation_Mode(
    303                                         phHal_sHwReference_t      *psHwReference,
    304                                         phNfc_eModeType_t          eModeType,
    305                                         pphHal4Nfc_GenCallback_t   pEmulationModecb,
    306                                         void                      *pContext
    307                                         )
    308 {
    309     NFCSTATUS RetStatus = NFCSTATUS_PENDING;
    310     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
    311     /*NULL checks*/
    312     if(NULL == psHwReference
    313         || NULL == pEmulationModecb
    314         )
    315     {
    316         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    317         RetStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
    318     }
    319     /*Check Initialised state*/
    320     else if((NULL == psHwReference->hal_context)
    321                         || (((phHal4Nfc_Hal4Ctxt_t *)
    322                                 psHwReference->hal_context)->Hal4CurrentState
    323                                                < eHal4StateOpenAndReady)
    324                         || (((phHal4Nfc_Hal4Ctxt_t *)
    325                                 psHwReference->hal_context)->Hal4NextState
    326                                                == eHal4StateClosed))
    327     {
    328         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
    329         RetStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
    330     }
    331     else
    332     {
    333 
    334     }
    335     return NFCSTATUS_PENDING;
    336 }
    337 #endif /*FULL_HAL4_EMULATION_ENABLE*/
    338