code update
[adaptation/devices/nfc-plugin-nxp.git] / src / phLibNfc_SE.c
1 /*\r
2  * Copyright (C) 2010 NXP Semiconductors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /*!\r
18  * \file phLibNfc_SE.c\r
19  \r
20  * Project: NFC FRI / HALDL\r
21  *\r
22  * $Date: Thu Apr 22 13:59:50 2010 $ \r
23  * $Author: ing07385 $\r
24  * $Revision: 1.65 $\r
25  * $Aliases: NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $\r
26  *\r
27  */\r
28 \r
29 /*\r
30 ************************* Header Files ***************************************\r
31 */\r
32 \r
33 #include <phNfcStatus.h>\r
34 #include <phLibNfc.h>\r
35 #include <phHal4Nfc.h>\r
36 #include <phOsalNfc.h>\r
37 #include <phLibNfc_Internal.h>\r
38 #include <phLibNfc_SE.h>\r
39 #include <phLibNfc_ndef_raw.h>\r
40 #include <phLibNfc_initiator.h>\r
41 #include <phLibNfc_discovery.h>\r
42 \r
43 /*\r
44 *************************** Macro's  ****************************************\r
45 */\r
46 \r
47 #ifndef STATIC_DISABLE\r
48 #define STATIC static\r
49 #else\r
50 #define STATIC\r
51 #endif\r
52 \r
53 /*\r
54 *************************** Global Variables **********************************\r
55 */\r
56 \r
57 /*This Structure  contains the Secure Element information*/\r
58 phLibNfc_SE_List_t sSecuredElementInfo[PHLIBNFC_MAXNO_OF_SE];\r
59 \r
60 /*\r
61 *************************** Static Function Declaration ***********************\r
62 */\r
63 \r
64 /* Response callback for SE Set Mode*/\r
65 STATIC \r
66 void phLibNfc_SE_SetMode_cb(void  *context, NFCSTATUS status);\r
67 \r
68 \r
69 /* SE register listner response notification */\r
70 STATIC \r
71 void phLibNfc_SeNotification(void                     *context,\r
72                         phHal_eNotificationType_t     type,\r
73                         phHal4Nfc_NotificationInfo_t  info,\r
74                         NFCSTATUS                     status\r
75                         );\r
76 /*\r
77 *************************** Function Definitions ******************************\r
78 */\r
79 \r
80 /**\r
81 * Registers notification handler to handle secure element specific events\r
82 */\r
83 NFCSTATUS phLibNfc_SE_NtfRegister   (\r
84                             pphLibNfc_SE_NotificationCb_t  pSE_NotificationCb,\r
85                             void                            *pContext\r
86                             )\r
87 {\r
88      NFCSTATUS         Status = NFCSTATUS_SUCCESS;\r
89      pphLibNfc_LibContext_t pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;\r
90 \r
91      if((NULL == gpphLibContext) || \r
92          (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))\r
93      {\r
94          Status = NFCSTATUS_NOT_INITIALISED;\r
95      }\r
96      else if((pSE_NotificationCb == NULL)\r
97          ||(NULL == pContext))\r
98      {\r
99          /*parameters sent by upper layer are not valid*/\r
100          Status = NFCSTATUS_INVALID_PARAMETER;\r
101      }     \r
102      else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)\r
103      {\r
104          Status = NFCSTATUS_SHUTDOWN;\r
105      }\r
106      else \r
107      {\r
108          /*Register SE notification with lower layer.\r
109          Any activity on Smx or UICC will be notified */\r
110          Status = phHal4Nfc_RegisterNotification(\r
111                                             pLibContext->psHwReference,\r
112                                             eRegisterSecureElement,\r
113                                             phLibNfc_SeNotification,\r
114                                             (void*)pLibContext); \r
115         if(Status == NFCSTATUS_SUCCESS)\r
116         {\r
117             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb = pSE_NotificationCb;\r
118             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt=pContext;\r
119         }\r
120         else\r
121         {\r
122             /* Registration failed */\r
123             Status = NFCSTATUS_FAILED;\r
124         }           \r
125      }\r
126      return Status;\r
127 }\r
128 /**\r
129 * SE Notification events are notified with this callback\r
130 */\r
131 STATIC void phLibNfc_SeNotification(void  *context,                                 \r
132                                     phHal_eNotificationType_t    type,\r
133                                     phHal4Nfc_NotificationInfo_t  info,\r
134                                     NFCSTATUS                   status)\r
135 {\r
136     pphLibNfc_LibContext_t pLibContext=(pphLibNfc_LibContext_t)context;\r
137     phHal_sEventInfo_t  *pEvtInfo = NULL;     \r
138     phLibNfc_uSeEvtInfo_t Se_Trans_Info={{{0,0},{0,0}}};\r
139     phLibNfc_SE_List_t  *pSeInfo=NULL;  \r
140     \r
141     if(pLibContext != gpphLibContext)\r
142     {\r
143         /*wrong context returned*/\r
144         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
145     }\r
146     else\r
147     {\r
148         if((status == NFCSTATUS_SUCCESS) && (type == NFC_EVENT_NOTIFICATION))\r
149         {\r
150             pEvtInfo = info.psEventInfo;\r
151             status = NFCSTATUS_SUCCESS;\r
152             if((pEvtInfo->eventSource == phHal_ePICC_DevType )\r
153                 && (pEvtInfo->eventHost == phHal_eHostController) )\r
154             {\r
155                 sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_Type = phLibNfc_SE_Type_SmartMX;\r
156                 /* Smartx Mx is Activated */\r
157                 pSeInfo = &sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX];\r
158             }\r
159             if(pEvtInfo->eventHost == phHal_eUICCHost)\r
160             { \r
161                 /* UICC is Activate */\r
162                 sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_Type = phLibNfc_SE_Type_UICC;\r
163                 pSeInfo = &sSecuredElementInfo[LIBNFC_SE_UICC_INDEX];           \r
164             }\r
165             else\r
166             {\r
167                 /*presently Smx event source is not supported */\r
168             }\r
169             if(pSeInfo!=NULL)\r
170             {\r
171                 switch(pEvtInfo->eventType)\r
172                 {\r
173                     case NFC_EVT_TRANSACTION:\r
174                     {\r
175                         if((pEvtInfo->eventInfo.aid.length != 0) && ((pEvtInfo->eventInfo.aid.length <= 16)))  // PLG\r
176                         {\r
177                             /*copy the Application id on which transaction happened*/                        \r
178                                                         Se_Trans_Info.UiccEvtInfo.aid.buffer =pEvtInfo->eventInfo.aid.buffer;\r
179                                                         Se_Trans_Info.UiccEvtInfo.aid.length =pEvtInfo->eventInfo.aid.length;\r
180                         }\r
181                                                 else\r
182                                                 {\r
183                                                         // PLG patch\r
184                             Se_Trans_Info.UiccEvtInfo.aid.buffer = NULL;\r
185                                                         Se_Trans_Info.UiccEvtInfo.aid.length = 0;\r
186                                                 }\r
187                                                 if((pEvtInfo->eventHost == phHal_eUICCHost)\r
188                            && (info.psEventInfo->eventInfo.uicc_info.param.length\r
189                                 != 0))\r
190                         {\r
191                             /*copy the parameters info on which transaction happened*/                       \r
192                                                         Se_Trans_Info.UiccEvtInfo.param.buffer =\r
193                                                                                 info.psEventInfo->eventInfo.uicc_info.param.buffer;\r
194                                                         Se_Trans_Info.UiccEvtInfo.param.length =\r
195                                                                                 info.psEventInfo->eventInfo.uicc_info.param.length;\r
196                         }\r
197                             /*Notify to upper layer that transaction had happened on the\r
198                             one of the application stored in UICC or Smx*/\r
199                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
200                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
201                             phLibNfc_eSE_EvtStartTransaction,\r
202                             pSeInfo->hSecureElement,\r
203                             &Se_Trans_Info,\r
204                             status);\r
205                         break;\r
206                     }\r
207 \r
208                     case NFC_EVT_APDU_RECEIVED:\r
209                     {\r
210                         if ((pEvtInfo->eventInfo.aid.length != 0) && ((pEvtInfo->eventInfo.aid.length <= 16)))\r
211                         {\r
212                             /* Copy received APDU to aid buffer. */\r
213                             Se_Trans_Info.UiccEvtInfo.aid.buffer = pEvtInfo->eventInfo.aid.buffer;\r
214                             Se_Trans_Info.UiccEvtInfo.aid.length = pEvtInfo->eventInfo.aid.length;\r
215                         }\r
216 \r
217                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
218                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
219                             phLibNfc_eSE_EvtApduReceived,\r
220                             pSeInfo->hSecureElement,\r
221                             &Se_Trans_Info,\r
222                             status);\r
223                         break;\r
224                     }\r
225 \r
226                     case NFC_EVT_MIFARE_ACCESS:\r
227                     {\r
228                         /* copy the Block MIFARE accessed */\r
229                         Se_Trans_Info.UiccEvtInfo.aid.buffer = pEvtInfo->eventInfo.aid.buffer;\r
230                         Se_Trans_Info.UiccEvtInfo.aid.length = pEvtInfo->eventInfo.aid.length;\r
231 \r
232                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
233                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
234                             phLibNfc_eSE_EvtMifareAccess,\r
235                             pSeInfo->hSecureElement,\r
236                             &Se_Trans_Info,\r
237                             status);\r
238                         break;\r
239                     }\r
240 \r
241                     case NFC_EVT_EMV_CARD_REMOVAL:\r
242                     {\r
243                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
244                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
245                             phLibNfc_eSE_EvtCardRemoval,\r
246                             pSeInfo->hSecureElement,\r
247                             &Se_Trans_Info,\r
248                             status);\r
249                         break;\r
250                     }\r
251 \r
252                     case NFC_EVT_END_OF_TRANSACTION:\r
253                     {\r
254                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
255                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
256                             phLibNfc_eSE_EvtEndTransaction,\r
257                             pSeInfo->hSecureElement,\r
258                             &Se_Trans_Info,\r
259                             status);\r
260                         break;\r
261                     }\r
262                     case NFC_EVT_CONNECTIVITY:\r
263                     {\r
264                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
265                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
266                             phLibNfc_eSE_EvtConnectivity,\r
267                             pSeInfo->hSecureElement,\r
268                             &Se_Trans_Info,\r
269                             status);\r
270                         break;\r
271                     }\r
272                     case NFC_EVT_START_OF_TRANSACTION:\r
273                     {\r
274                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
275                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
276                             phLibNfc_eSE_EvtTypeTransaction,\r
277                             pSeInfo->hSecureElement,\r
278                             &Se_Trans_Info,\r
279                             status);\r
280                         break;\r
281                     }\r
282                     case NFC_EVT_FIELD_ON:\r
283                     {\r
284                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
285                             pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
286                             phLibNfc_eSE_EvtFieldOn,\r
287                             pSeInfo->hSecureElement,\r
288                             &Se_Trans_Info,\r
289                             status);\r
290                         break;\r
291                     }\r
292                     case NFC_EVT_FIELD_OFF:\r
293                     {\r
294                         (*pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb)(\r
295                              pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt,\r
296                              phLibNfc_eSE_EvtFieldOff,\r
297                              pSeInfo->hSecureElement,\r
298                              &Se_Trans_Info,\r
299                              status);\r
300                         break;\r
301                     }\r
302                     default:\r
303                     {\r
304                         break;\r
305                     }\r
306                 }\r
307             }\r
308             else\r
309             {\r
310 \r
311             }           \r
312          }        \r
313     }\r
314   return;\r
315 }\r
316 \r
317 /**\r
318  * Unregister the Secured Element Notification.\r
319  */\r
320 NFCSTATUS phLibNfc_SE_NtfUnregister(void)\r
321 {\r
322     NFCSTATUS Status = NFCSTATUS_SUCCESS;\r
323     pphLibNfc_LibContext_t pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;\r
324      \r
325     if((NULL == gpphLibContext) || \r
326         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))\r
327     {\r
328         /*Lib Nfc is not initialized*/\r
329         Status = NFCSTATUS_NOT_INITIALISED;\r
330     }\r
331     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)\r
332     {\r
333         Status = NFCSTATUS_SHUTDOWN;\r
334     }\r
335     else \r
336     {\r
337         /*Unregister SE event notification with lower layer.\r
338         even some transaction happens on UICC or Smx will not \r
339         be notified afterworlds */\r
340         Status = phHal4Nfc_UnregisterNotification(\r
341                                                 pLibContext->psHwReference,\r
342                                                 eRegisterSecureElement,\r
343                                                 pLibContext);   \r
344         if(Status != NFCSTATUS_SUCCESS)\r
345         {\r
346             /*Unregister failed*/\r
347             Status=NFCSTATUS_FAILED;\r
348         }\r
349         pLibContext->sSeContext.sSeCallabackInfo.pSeListenerNtfCb=NULL;\r
350         pLibContext->sSeContext.sSeCallabackInfo.pSeListenerCtxt=NULL;\r
351     }\r
352     return Status;\r
353 }\r
354 \r
355 /**\r
356 * Get list of available Secure Elements\r
357 */\r
358 NFCSTATUS phLibNfc_SE_GetSecureElementList(\r
359                         phLibNfc_SE_List_t*     pSE_List,\r
360                         uint8_t*                uSE_count\r
361                         )\r
362 {        \r
363     NFCSTATUS Status = NFCSTATUS_SUCCESS;  \r
364         uint8_t    uNo_Of_SE = 0;\r
365 \r
366     if((NULL == gpphLibContext) || \r
367         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))\r
368     {\r
369         Status = NFCSTATUS_NOT_INITIALISED;\r
370     }     \r
371     else if((NULL ==pSE_List) || (NULL ==uSE_count))\r
372     {   \r
373         Status = NFCSTATUS_INVALID_PARAMETER;\r
374     }\r
375     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)\r
376     {\r
377         Status = NFCSTATUS_SHUTDOWN;\r
378     }\r
379     else\r
380     {\r
381         /*Check for which type of Secure Element is available*/\r
382         if(gpphLibContext->psHwReference->uicc_connected==TRUE)\r
383         {\r
384             /* Populate the UICC type */\r
385             sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_Type = phLibNfc_SE_Type_UICC;\r
386                         \r
387             /* Populate the UICC handle */\r
388             sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement =(phLibNfc_Handle)\r
389                             (LIBNFC_SE_UICC_INDEX + LIBNFC_SE_BASE_HANDLE); \r
390 \r
391 #ifdef NXP_HAL_ENABLE_SMX\r
392 \r
393             pSE_List[LIBNFC_SE_UICC_INDEX].eSE_Type = \r
394                                 sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_Type;\r
395             pSE_List[LIBNFC_SE_UICC_INDEX].hSecureElement = (phLibNfc_Handle)\r
396                             (LIBNFC_SE_UICC_INDEX + LIBNFC_SE_BASE_HANDLE);\r
397             pSE_List[LIBNFC_SE_UICC_INDEX].eSE_CurrentState = \r
398                                 sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_CurrentState; \r
399 #else             \r
400                         pSE_List->eSE_Type = \r
401                                 sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_Type;\r
402             pSE_List->hSecureElement = (phLibNfc_Handle)\r
403                             (LIBNFC_SE_UICC_INDEX + LIBNFC_SE_BASE_HANDLE);\r
404             pSE_List->eSE_CurrentState = \r
405                                 sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_CurrentState; \r
406 #endif\r
407             /* update the No of SE retrieved */\r
408                         uNo_Of_SE ++;\r
409                         \r
410         }\r
411         if (gpphLibContext->psHwReference->smx_connected ==TRUE)\r
412         {\r
413             /* if the Smx is also connected to the PN544 */ \r
414             /* Populate the SMX type */\r
415             sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_Type = phLibNfc_SE_Type_SmartMX;\r
416                         \r
417             /* Populate the SMX handle */\r
418             sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement =(phLibNfc_Handle)\r
419                             (LIBNFC_SE_SMARTMX_INDEX + LIBNFC_SE_BASE_HANDLE); \r
420             pSE_List[LIBNFC_SE_SMARTMX_INDEX].eSE_Type = \r
421                                 sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_Type;\r
422             pSE_List[LIBNFC_SE_SMARTMX_INDEX].hSecureElement = (phLibNfc_Handle)\r
423                             (LIBNFC_SE_SMARTMX_INDEX + LIBNFC_SE_BASE_HANDLE);\r
424             pSE_List[LIBNFC_SE_SMARTMX_INDEX].eSE_CurrentState = \r
425                                 sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_CurrentState; \r
426             \r
427             /* update the No of SE retrieved */\r
428                         uNo_Of_SE ++;\r
429                          \r
430         }\r
431                 *uSE_count = uNo_Of_SE;\r
432     }\r
433     return Status;\r
434 }\r
435 \r
436 /**\r
437 * Sets secure element mode.\r
438 * This  function configures SE to specific mode based on activation mode type\r
439 */\r
440 \r
441 NFCSTATUS phLibNfc_SE_SetMode ( phLibNfc_Handle             hSE_Handle, \r
442                                phLibNfc_eSE_ActivationMode  eActivation_mode,\r
443                                pphLibNfc_SE_SetModeRspCb_t  pSE_SetMode_Rsp_cb,\r
444                                void *                       pContext\r
445                                )\r
446 {\r
447     NFCSTATUS Status = NFCSTATUS_SUCCESS;\r
448     phHal_eEmulationType_t  eEmulationType = NFC_SMARTMX_EMULATION;\r
449     pphLibNfc_LibContext_t pLibContext=(pphLibNfc_LibContext_t)gpphLibContext;\r
450     \r
451     if((NULL == gpphLibContext) || \r
452         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))\r
453     {\r
454         Status = NFCSTATUS_NOT_INITIALISED;\r
455     }\r
456         else if((pSE_SetMode_Rsp_cb ==NULL)\r
457         ||(NULL == pContext)||(NULL==(void *)hSE_Handle))\r
458     {\r
459         Status=NFCSTATUS_INVALID_PARAMETER;\r
460     }\r
461         else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)\r
462     {\r
463         Status = NFCSTATUS_SHUTDOWN;\r
464     }            \r
465     else if((pLibContext->status.GenCb_pending_status == TRUE)\r
466           ||(NULL!=pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCb))\r
467     {\r
468         /*previous callback is pending still*/\r
469         Status =NFCSTATUS_REJECTED;\r
470     }\r
471     else \r
472     {\r
473         switch(eActivation_mode)\r
474         {\r
475             case phLibNfc_SE_ActModeVirtual: \r
476             {\r
477                 if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement)\r
478                 {\r
479                     eEmulationType = NFC_UICC_EMULATION;  \r
480                     /*Enable the UICC -External reader can see it*/\r
481                     pLibContext->sCardEmulCfg.config.uiccEmuCfg.enableUicc = TRUE;                     \r
482                 }\r
483                 else if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement)\r
484                 {\r
485                     eEmulationType = NFC_SMARTMX_EMULATION;  \r
486                     /*Enable the SMX -External reader can see it*/\r
487                     pLibContext->sCardEmulCfg.config.smartMxCfg.enableEmulation = TRUE;                    \r
488                 }\r
489                 else\r
490                 {\r
491                     Status=NFCSTATUS_INVALID_HANDLE;\r
492                 }\r
493                 if(Status==NFCSTATUS_SUCCESS)\r
494                 {\r
495                     if(pLibContext->sSeContext.eActivatedMode != phLibNfc_SE_ActModeWired)\r
496                     {\r
497                         pLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeVirtual;\r
498                     }\r
499                     pLibContext->sCardEmulCfg.emuType = eEmulationType;\r
500                     Status = phHal4Nfc_ConfigParameters(\r
501                                             pLibContext->psHwReference,\r
502                                             NFC_EMULATION_CONFIG,           \r
503                                             (phHal_uConfig_t*)&pLibContext->sCardEmulCfg,\r
504                                             phLibNfc_SE_SetMode_cb,\r
505                                             pLibContext);\r
506                 }\r
507             }\r
508             break;\r
509             case phLibNfc_SE_ActModeVirtualVolatile:\r
510             {\r
511                 if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement)\r
512                 {\r
513                     eEmulationType = NFC_SMARTMX_EMULATION;\r
514                     /*Enable the SMX -External reader can see it*/\r
515                     pLibContext->sCardEmulCfg.config.smartMxCfg.enableEmulation = TRUE;\r
516                     pLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeVirtualVolatile;\r
517 \r
518                     Status = phHal4Nfc_Switch_SMX_Mode(\r
519                                         pLibContext->psHwReference,\r
520                                         eSmartMx_Virtual,\r
521                                         phLibNfc_SE_SetMode_cb,\r
522                                         pLibContext\r
523                                         );\r
524                 }\r
525                 else if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement)\r
526                 {\r
527                     eEmulationType = NFC_UICC_EMULATION;\r
528                     /*Enable the UICC -External reader can see it*/\r
529                     pLibContext->sCardEmulCfg.config.uiccEmuCfg.enableUicc = TRUE;\r
530                     pLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeVirtualVolatile;\r
531 \r
532                     Status = phHal4Nfc_Switch_Swp_Mode(\r
533                                         pLibContext->psHwReference,\r
534                                         eSWP_Switch_On,\r
535                                         phLibNfc_SE_SetMode_cb,\r
536                                         pLibContext\r
537                                         );\r
538                 }\r
539                 else\r
540                 {\r
541                     Status = NFCSTATUS_INVALID_HANDLE;\r
542                 }\r
543             }\r
544             break;\r
545             case phLibNfc_SE_ActModeDefault:\r
546             {\r
547                 if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement)\r
548                 {\r
549                     Status = phHal4Nfc_Switch_SMX_Mode(\r
550                                         pLibContext->psHwReference,\r
551                                         eSmartMx_Default,\r
552                                         phLibNfc_SE_SetMode_cb,\r
553                                         pLibContext\r
554                                         );\r
555                 }\r
556                 else if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement)\r
557                 {\r
558                     Status = phHal4Nfc_Switch_Swp_Mode(\r
559                                         pLibContext->psHwReference,\r
560                                         eSWP_Switch_Default,\r
561                                         phLibNfc_SE_SetMode_cb,\r
562                                         pLibContext\r
563                                         );\r
564                 }\r
565                 else\r
566                 {\r
567                     Status = NFCSTATUS_INVALID_HANDLE;\r
568                 }\r
569             }\r
570             break;\r
571 \r
572             case phLibNfc_SE_ActModeWired:\r
573             {\r
574                 if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement)\r
575                 {\r
576                     if(pLibContext->CBInfo.pClientNtfRegRespCB!=NULL)\r
577                     {   \r
578                         /*Disable the SMX -External reader can't see it anymore*/\r
579                         pLibContext->sCardEmulCfg.config.smartMxCfg.enableEmulation = FALSE; \r
580                         pLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeWired;\r
581 \r
582                         Status = phHal4Nfc_Switch_SMX_Mode(                                                  \r
583                                             pLibContext->psHwReference,\r
584                                             eSmartMx_Wired,\r
585                                             phLibNfc_SE_SetMode_cb,\r
586                                             pLibContext\r
587                                             );     \r
588                     }\r
589                 }\r
590                 else if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement)\r
591                 {\r
592                     /*This mode is not applicable to UICC*/\r
593                     Status = NFCSTATUS_REJECTED;                    \r
594                 }\r
595                 else\r
596                 {\r
597                     Status = NFCSTATUS_INVALID_HANDLE;     \r
598                 }  \r
599             }  \r
600             break;\r
601         \r
602             case phLibNfc_SE_ActModeOff:\r
603             {\r
604                 /*UICC emulation deactivate*/\r
605                 if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement)\r
606                 {\r
607                     eEmulationType = NFC_UICC_EMULATION;\r
608                     /*Disable the UICC -External reader can't see it anymore*/\r
609                     pLibContext->sCardEmulCfg.config.uiccEmuCfg.enableUicc = FALSE;     \r
610                     \r
611                 }\r
612                 else if(hSE_Handle == sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement)\r
613                 {\r
614                     eEmulationType = NFC_SMARTMX_EMULATION;  \r
615                     /*Disable the SMX -External reader can't see it anymore*/\r
616                     pLibContext->sCardEmulCfg.config.smartMxCfg.enableEmulation=FALSE; \r
617                     \r
618                 }\r
619                 else\r
620                 {\r
621                     Status = NFCSTATUS_INVALID_HANDLE; \r
622                 }           \r
623                 if(Status==NFCSTATUS_SUCCESS)\r
624                 {\r
625                     pLibContext->sCardEmulCfg.emuType = eEmulationType;\r
626 \r
627                     if(pLibContext->sSeContext.eActivatedMode != phLibNfc_SE_ActModeWired)\r
628                     {\r
629                          pLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeOff;\r
630                     }\r
631 \r
632                     Status = phHal4Nfc_ConfigParameters(pLibContext->psHwReference,\r
633                                                             NFC_EMULATION_CONFIG,           \r
634                                                             (phHal_uConfig_t*)&pLibContext->sCardEmulCfg,\r
635                                                             phLibNfc_SE_SetMode_cb,\r
636                                                             pLibContext);\r
637                 }\r
638             }  \r
639             break;\r
640             default:\r
641                 Status=NFCSTATUS_INVALID_PARAMETER;\r
642                 break;\r
643 \r
644         }/*End of eActivation_mode switch */       \r
645         if(Status==NFCSTATUS_PENDING)\r
646         {\r
647             pLibContext->sSeContext.hSetemp=hSE_Handle;\r
648             pLibContext->status.GenCb_pending_status = TRUE;\r
649             pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCb = pSE_SetMode_Rsp_cb;\r
650             pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCtxt=pContext;                       \r
651         }\r
652         else if(Status == NFCSTATUS_INVALID_HANDLE)\r
653         {\r
654             Status= Status;\r
655         }\r
656         else\r
657         {\r
658             Status = NFCSTATUS_FAILED;\r
659         }\r
660     }\r
661     return Status;\r
662 }\r
663 /**\r
664 * Callback for Se Set mode \r
665 */\r
666 STATIC void phLibNfc_SE_SetMode_cb(void  *context, NFCSTATUS status)\r
667 {\r
668     pphLibNfc_LibContext_t pLibContext=(pphLibNfc_LibContext_t)context;\r
669     pphLibNfc_SE_SetModeRspCb_t  pUpperLayerCb=NULL;\r
670     void                         *pUpperContext=NULL;\r
671     phLibNfc_Handle              hSeHandle=0;\r
672     uint8_t                      TempState=FALSE;  \r
673 \r
674     if(pLibContext != gpphLibContext)\r
675     {\r
676         /*wrong context returned*/\r
677         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
678     }\r
679     else\r
680     {\r
681         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)\r
682         {\r
683             /*If shutdown is called in between allow shutdown to happen*/\r
684             phLibNfc_Pending_Shutdown();\r
685             status = NFCSTATUS_SHUTDOWN;\r
686         }\r
687         else\r
688         {\r
689             if(status == NFCSTATUS_SUCCESS)\r
690             {\r
691                 hSeHandle = pLibContext->sSeContext.hSetemp;       \r
692 \r
693                 if(hSeHandle == sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].hSecureElement)\r
694                 {\r
695                     if(TRUE==pLibContext->sCardEmulCfg.config.uiccEmuCfg.enableUicc)\r
696                     {\r
697                         /*If  Activation mode was virtual allow external reader to see it*/\r
698                         pLibContext->sSeContext.uUiccActivate = TRUE;\r
699                         sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_CurrentState = phLibNfc_SE_Active;\r
700                     }\r
701                     else\r
702                     {\r
703                         /*If  Activation mode was wired don't allow external reader to see it*/\r
704                         pLibContext->sSeContext.uUiccActivate = FALSE;\r
705                         sSecuredElementInfo[LIBNFC_SE_UICC_INDEX].eSE_CurrentState =\r
706                                                                     phLibNfc_SE_Inactive;\r
707                     }\r
708                     status = NFCSTATUS_SUCCESS;                    \r
709                     TempState = pLibContext->sSeContext.uUiccActivate;\r
710                 }\r
711                 else if (hSeHandle==sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].hSecureElement)\r
712                 {\r
713                     if(TRUE==pLibContext->sCardEmulCfg.config.smartMxCfg.enableEmulation)\r
714                     {\r
715                         /*If  Activation mode was virtual allow external reader to see it*/\r
716                         pLibContext->sSeContext.uSmxActivate = TRUE;\r
717                         sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_CurrentState = \r
718                                                                         phLibNfc_SE_Active; \r
719                     }\r
720                     else\r
721                     {\r
722                         /*If  Activation mode was wired don't allow external reader to see it*/\r
723                         pLibContext->sSeContext.uSmxActivate = FALSE;                        \r
724                         sSecuredElementInfo[LIBNFC_SE_SMARTMX_INDEX].eSE_CurrentState=\r
725                                                                         phLibNfc_SE_Inactive; \r
726                     }\r
727                     status = NFCSTATUS_SUCCESS;                    \r
728                     TempState = pLibContext->sSeContext.uSmxActivate;\r
729                 }\r
730                 else\r
731                 {\r
732                     status = NFCSTATUS_FAILED;\r
733                 }\r
734             }\r
735             else\r
736             {\r
737                 status = NFCSTATUS_FAILED;\r
738             }\r
739             pLibContext->status.GenCb_pending_status = FALSE; \r
740         }\r
741         \r
742     }\r
743     pUpperLayerCb = pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCb;\r
744     pUpperContext = pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCtxt;  \r
745     pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCb = NULL;\r
746     pLibContext->sSeContext.sSeCallabackInfo.pSEsetModeCtxt = NULL;\r
747         PHNFC_UNUSED_VARIABLE(TempState);\r
748     /* Call the upper layer cb */\r
749     if(pUpperLayerCb!= NULL )\r
750     {\r
751         (*pUpperLayerCb)(pUpperContext,                        \r
752                         hSeHandle,\r
753                                                 status);\r
754     }\r
755     return;\r
756 }\r
757 \r
758 \r
759 \r