Initialize Tizen 2.3
[adaptation/devices/nfc-plugin-nxp.git] / src / phHal4Nfc_Emulation.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 * \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                         sSmxCfg.PollDevInfo.PollCfgInfo.DisableCardEmulation = TRUE;
168                         /*Switch mode to wired*/
169                         CfgStatus = phHciNfc_Switch_SmxMode (
170                                                     Hal4Ctxt->psHciHandle,
171                                                     psHwReference,
172                                                     smx_mode,
173                                                     &sSmxCfg
174                                                     );
175                     }
176                 }
177                 else
178                 {
179                     Hal4Ctxt->psADDCtxtInfo->smx_discovery = FALSE;
180                     /*Switch mode to virtual or off*/
181                     CfgStatus = phHciNfc_Switch_SmxMode (
182                                         Hal4Ctxt->psHciHandle,
183                                         psHwReference,
184                                         smx_mode,
185                                         &(Hal4Ctxt->psADDCtxtInfo->sADDCfg)
186                                         );
187                 }
188
189                 /* Change the State of the HAL only if Switch mode Returns
190                    Success*/
191                 if ( NFCSTATUS_PENDING == CfgStatus )
192                 {
193                     Hal4Ctxt->Hal4NextState = eHal4StateConfiguring;
194                     Hal4Ctxt->sUpperLayerInfo.pConfigCallback
195                         = pSwitchModecb;
196                 }
197             }           
198         }
199         else/*Return Status not initialised*/
200         {
201             phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
202             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
203         }
204     }
205     return CfgStatus;
206 }
207
208
209
210 /*  Switch mode for Swp.*/
211 NFCSTATUS phHal4Nfc_Switch_Swp_Mode(                                    
212                                     phHal_sHwReference_t      *psHwReference,
213                                     phHal_eSWP_Mode_t          swp_mode,
214                                     pphHal4Nfc_GenCallback_t   pSwitchModecb,
215                                     void                      *pContext
216                                     )
217 {
218     NFCSTATUS CfgStatus = NFCSTATUS_PENDING;
219     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL; 
220     /*NULL checks*/
221     if(NULL == psHwReference  
222         || NULL == pSwitchModecb
223         )
224     {
225         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
226         CfgStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
227     }
228     /*Check Initialised state*/
229     else if((NULL == psHwReference->hal_context)
230                         || (((phHal4Nfc_Hal4Ctxt_t *)
231                                 psHwReference->hal_context)->Hal4CurrentState 
232                                                < eHal4StateOpenAndReady)
233                         || (((phHal4Nfc_Hal4Ctxt_t *)
234                                 psHwReference->hal_context)->Hal4NextState 
235                                                == eHal4StateClosed))
236     {
237         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
238         CfgStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
239     }
240     else
241     {
242         Hal4Ctxt = psHwReference->hal_context;
243         /*Previous POLL CFG has not completed or device is connected,
244           do not allow poll*/
245         if(Hal4Ctxt->Hal4NextState == eHal4StateConfiguring)
246         {
247             PHDBG_INFO("Hal4:Configuration in progress.Returning status Busy");
248             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_BUSY);
249         }
250         else if(Hal4Ctxt->Hal4CurrentState >= eHal4StateOpenAndReady)
251         {
252              /**If config discovery has not been called prior to this ,allocate 
253                ADD Context here*/
254             if (NULL == Hal4Ctxt->psADDCtxtInfo)
255             {
256                 Hal4Ctxt->psADDCtxtInfo= (pphHal4Nfc_ADDCtxtInfo_t)
257                     phOsalNfc_GetMemory((uint32_t)
258                                         (sizeof(phHal4Nfc_ADDCtxtInfo_t)));
259                 if(NULL != Hal4Ctxt->psADDCtxtInfo)
260                 {
261                     (void)memset(Hal4Ctxt->psADDCtxtInfo,
262                                     0,sizeof(phHal4Nfc_ADDCtxtInfo_t));
263                 }
264             }
265             if(NULL == Hal4Ctxt->psADDCtxtInfo)
266             {
267                 phOsalNfc_RaiseException(phOsalNfc_e_NoMemory,0);
268                 CfgStatus= PHNFCSTVAL(CID_NFC_HAL , 
269                                         NFCSTATUS_INSUFFICIENT_RESOURCES);
270             }
271             else
272             {   
273                 /* Register Upper layer context */
274                 Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext; 
275                 /*Switch mode to On or off*/
276                 CfgStatus = phHciNfc_Switch_SwpMode(
277                                     Hal4Ctxt->psHciHandle,
278                                     psHwReference,
279                                     swp_mode
280                                     );
281
282                 /* Change the State of the HAL only if Switch mode Returns
283                    Success*/
284                 if ( NFCSTATUS_PENDING == CfgStatus )
285                 {
286                     Hal4Ctxt->Hal4NextState = eHal4StateConfiguring;
287                     Hal4Ctxt->sUpperLayerInfo.pConfigCallback
288                         = pSwitchModecb;
289                 }
290             }           
291         }
292         else/*Return Status not initialised*/
293         {
294             phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
295             CfgStatus= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
296         }
297     }
298     return CfgStatus;
299 }
300
301 #ifdef FULL_HAL4_EMULATION_ENABLE
302 /*  Switch Emulation mode ON or OFF.*/
303 NFCSTATUS phHal4Nfc_Host_Emulation_Mode( 
304                                         phHal_sHwReference_t      *psHwReference,
305                                         phNfc_eModeType_t          eModeType,
306                                         pphHal4Nfc_GenCallback_t   pEmulationModecb,
307                                         void                      *pContext
308                                         )
309 {
310     NFCSTATUS RetStatus = NFCSTATUS_PENDING;
311     phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
312     /*NULL checks*/
313     if(NULL == psHwReference  
314         || NULL == pEmulationModecb
315         )
316     {
317         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
318         RetStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
319     }
320     /*Check Initialised state*/
321     else if((NULL == psHwReference->hal_context)
322                         || (((phHal4Nfc_Hal4Ctxt_t *)
323                                 psHwReference->hal_context)->Hal4CurrentState 
324                                                < eHal4StateOpenAndReady)
325                         || (((phHal4Nfc_Hal4Ctxt_t *)
326                                 psHwReference->hal_context)->Hal4NextState 
327                                                == eHal4StateClosed))
328     {
329         phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
330         RetStatus = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
331     }    
332     else
333     {
334
335     }
336     return NFCSTATUS_PENDING;
337 }
338 #endif /*FULL_HAL4_EMULATION_ENABLE*/