Remove build error in Tizen 3.0
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server_se.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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 #include <vconf.h>
18 #include <tapi_common.h>
19 #include <ITapiSim.h>
20
21 #include "net_nfc_typedef_internal.h"
22 #include "net_nfc_debug_internal.h"
23 #include "net_nfc_util_internal.h"
24 #include "net_nfc_util_gdbus_internal.h"
25 #include "net_nfc_gdbus.h"
26 #include "net_nfc_server_common.h"
27 #include "net_nfc_server_context.h"
28 #include "net_nfc_server_manager.h"
29 #include "net_nfc_server_controller.h"
30 #include "net_nfc_server_util.h"
31 #include "net_nfc_server_se.h"
32
33 enum
34 {
35         SE_UICC_UNAVAILABLE = -1,
36         SE_UICC_ON_PROGRESS = 0,
37         SE_UICC_READY = 1,
38 };
39
40 typedef struct _nfc_se_setting_t
41 {
42         bool busy;
43         uint8_t type;
44         uint8_t return_type;
45         uint8_t mode;
46 }
47 net_nfc_server_se_setting_t;
48
49
50 static NetNfcGDbusSecureElement *se_skeleton = NULL;
51 #if 0
52 static uint8_t gdbus_se_prev_type = SECURE_ELEMENT_TYPE_INVALID;
53 static uint8_t gdbus_se_prev_mode = SECURE_ELEMENT_OFF_MODE;
54 #endif
55 static net_nfc_server_se_setting_t gdbus_se_setting;
56
57 /* TODO : make a list for handles */
58 static TapiHandle *gdbus_uicc_handle;
59 static net_nfc_target_handle_s *gdbus_ese_handle;
60
61 static int gdbus_uicc_ready;
62
63 /* server_side */
64 typedef struct _ServerSeData ServerSeData;
65
66 struct _ServerSeData
67 {
68         data_s aid;
69         data_s param;
70         guint event;
71 };
72
73 typedef struct _SeSetCardEmul SeSetCardEmul;
74
75 struct _SeSetCardEmul
76 {
77         NetNfcGDbusSecureElement *object;
78         GDBusMethodInvocation *invocation;
79         gint mode;
80 };
81
82 typedef struct _SeDataSeType SeDataSeType;
83
84 struct _SeDataSeType
85 {
86         NetNfcGDbusSecureElement *object;
87         GDBusMethodInvocation *invocation;
88         gint se_type;
89 };
90
91
92 typedef struct _SeDataHandle SeDataHandle;
93
94 struct _SeDataHandle
95 {
96         NetNfcGDbusSecureElement *object;
97         GDBusMethodInvocation *invocation;
98         net_nfc_target_handle_s *handle;
99 };
100
101 typedef struct _SeDataApdu SeDataApdu;
102
103 struct _SeDataApdu
104 {
105         NetNfcGDbusSecureElement *object;
106         GDBusMethodInvocation *invocation;
107         net_nfc_target_handle_s *handle;
108         GVariant *data;
109 };
110
111 typedef struct _ChangeCardEmulMode ChangeCardEmulMode;
112
113 struct _ChangeCardEmulMode
114 {
115         NetNfcGDbusSecureElement *object;
116         GDBusMethodInvocation *invocation;
117         gint mode;
118 };
119
120 uint8_t net_nfc_server_se_get_se_type()
121 {
122         return gdbus_se_setting.type;
123 }
124
125 uint8_t net_nfc_server_se_get_return_se_mode()
126 {
127         return gdbus_se_setting.return_type;
128 }
129
130 uint8_t net_nfc_server_se_get_se_mode()
131 {
132         return gdbus_se_setting.mode;
133 }
134
135 static void net_nfc_server_se_set_se_type(uint8_t type)
136 {
137         gdbus_se_setting.return_type = gdbus_se_setting.type;
138         gdbus_se_setting.type = type;
139 }
140
141 static void net_nfc_server_se_set_se_mode(uint8_t mode)
142 {
143         gdbus_se_setting.mode = mode;
144 }
145
146
147 /* eSE functions */
148 static bool net_nfc_server_se_is_ese_handle(net_nfc_target_handle_s *handle)
149 {
150         return (gdbus_ese_handle != NULL && gdbus_ese_handle == handle);
151 }
152
153 static void net_nfc_server_se_set_current_ese_handle(
154                 net_nfc_target_handle_s *handle)
155 {
156         gdbus_ese_handle = handle;
157 }
158
159 static net_nfc_target_handle_s *net_nfc_server_se_open_ese()
160 {
161         bool ret;
162
163         if (NULL == gdbus_ese_handle)
164         {
165                 net_nfc_error_e result = NET_NFC_OK;
166                 net_nfc_target_handle_s *handle = NULL;
167
168                 ret = net_nfc_controller_secure_element_open(SECURE_ELEMENT_TYPE_ESE,
169                                         &handle, &result);
170                 if (true == ret)
171                 {
172                         net_nfc_server_se_set_current_ese_handle(handle);
173
174                         NFC_DBG("handle [%p]", handle);
175                 }
176                 else
177                 {
178                         NFC_ERR("net_nfc_controller_secure_element_open failed [%d]", result);
179                 }
180         }
181
182         return gdbus_ese_handle;
183 }
184
185 static net_nfc_error_e net_nfc_server_se_close_ese()
186 {
187         net_nfc_error_e result = NET_NFC_OK;
188
189         if (gdbus_ese_handle != NULL && net_nfc_server_gdbus_is_server_busy() == false)
190         {
191                 if (net_nfc_controller_secure_element_close(gdbus_ese_handle, &result) == false)
192                         net_nfc_controller_exception_handler();
193
194                 net_nfc_server_se_set_current_ese_handle(NULL);
195          }
196
197         return result;
198 }
199
200 static void _se_uicc_enable_card_emulation()
201 {
202         net_nfc_error_e result;
203
204         /*turn off ESE*/
205         net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
206                         SECURE_ELEMENT_OFF_MODE, &result);
207
208         /*turn on UICC*/
209         net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
210                         SECURE_ELEMENT_VIRTUAL_MODE, &result);
211
212         if (NET_NFC_OK == result)
213         {
214                 NFC_INFO("card emulation changed to SECURE_ELEMENT_TYPE_UICC");
215
216                 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
217                 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
218
219                 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_POLICY_UICC_ON) < 0)
220                         NFC_ERR("vconf_set_int failed");
221         }
222         else
223         {
224                 NFC_ERR("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
225         }
226 }
227
228 static void _se_uicc_prepare(void)
229 {
230         char **cpList;
231
232         cpList = tel_get_cp_name_list();
233         if (cpList != NULL)
234         {
235                 gdbus_uicc_handle = tel_init(cpList[0]);
236                 if (NULL == gdbus_uicc_handle)
237                         NFC_ERR("tel_init() failed");
238
239         }
240         else
241         {
242                 NFC_ERR("tel_get_cp_name_list() failed");
243         }
244 }
245
246 static void _se_uicc_status_noti_cb(TapiHandle *handle,
247                 const char *noti_id, void *data, void *user_data)
248 {
249         TelSimCardStatus_t *status = data;
250
251         NFC_DBG("_se_uicc_status_noti_cb");
252
253         switch (*status)
254         {
255         case TAPI_SIM_STATUS_SIM_INIT_COMPLETED :
256                 gdbus_uicc_ready = SE_UICC_READY;
257
258                 _se_uicc_prepare();
259
260                 if (gdbus_se_setting.busy == true &&
261                                 net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC)
262                 {
263                         gdbus_se_setting.busy = false;
264
265                         _se_uicc_enable_card_emulation();
266                 }
267                 break;
268
269         case TAPI_SIM_STATUS_CARD_REMOVED :
270                 NFC_DBG("TAPI_SIM_STATUS_CARD_REMOVED");
271                 gdbus_uicc_ready = SE_UICC_UNAVAILABLE;
272
273                 if (net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC)
274                 {
275                         net_nfc_error_e result;
276
277                         /*turn off UICC*/
278                         net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
279                                         SECURE_ELEMENT_OFF_MODE, &result);
280
281                         net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
282                         net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
283                 }
284                 break;
285
286         default:
287                 break;
288         }
289 }
290
291 static void _se_uicc_init(void)
292 {
293         _se_uicc_prepare();
294         tel_register_noti_event(gdbus_uicc_handle, TAPI_NOTI_SIM_STATUS,
295                         _se_uicc_status_noti_cb, NULL);
296 }
297
298 static void _se_uicc_deinit()
299 {
300         if (gdbus_uicc_handle != NULL)
301         {
302                 tel_deregister_noti_event(gdbus_uicc_handle, TAPI_NOTI_SIM_STATUS);
303
304                 tel_deinit(gdbus_uicc_handle);
305
306                 gdbus_uicc_handle = NULL;
307         }
308 }
309
310 static net_nfc_target_handle_s* _se_uicc_open()
311 {
312         net_nfc_target_handle_s *result = NULL;
313
314         if (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL)
315                 result = (net_nfc_target_handle_s *)gdbus_uicc_handle;
316
317         return result;
318 }
319
320 static bool _se_is_uicc_handle(net_nfc_target_handle_s *handle)
321 {
322         return (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL &&
323                         (TapiHandle *)handle == gdbus_uicc_handle);
324 }
325
326 static void _se_uicc_close(net_nfc_target_handle_s *handle)
327 {
328 }
329
330 /* SE Functions */
331 net_nfc_error_e net_nfc_server_se_disable_card_emulation()
332 {
333         net_nfc_error_e result;
334
335         net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
336         net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
337
338         /*turn off ESE*/
339         net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
340                         SECURE_ELEMENT_OFF_MODE, &result);
341
342         /*turn off UICC*/
343         net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
344                         SECURE_ELEMENT_OFF_MODE, &result);
345
346         return NET_NFC_OK;
347 }
348
349 net_nfc_error_e net_nfc_server_se_change_se(uint8_t type)
350 {
351         net_nfc_error_e result = NET_NFC_OK;
352
353         switch (type)
354         {
355         case SECURE_ELEMENT_TYPE_UICC :
356                 if (false == gdbus_se_setting.busy)
357                 {
358                         if (SE_UICC_READY == gdbus_uicc_ready)
359                         {
360                                 _se_uicc_enable_card_emulation();
361                         }
362                         else if (SE_UICC_ON_PROGRESS == gdbus_uicc_ready)
363                         {
364                                 NFC_INFO("waiting for uicc initializing complete...");
365
366                                 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
367                                 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
368
369                                 gdbus_se_setting.busy = true;
370                         }
371                         else
372                         {
373                                 result = NET_NFC_NOT_SUPPORTED;
374                         }
375                 }
376                 else
377                 {
378                         NFC_DBG("Previous request is processing.");
379
380                         result = NET_NFC_BUSY;
381                 }
382                 break;
383
384         case SECURE_ELEMENT_TYPE_ESE :
385                 /*turn off UICC*/
386                 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
387                                 SECURE_ELEMENT_OFF_MODE, &result);
388
389                 /*turn on ESE*/
390                 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
391                                 SECURE_ELEMENT_VIRTUAL_MODE, &result);
392
393                 if (NET_NFC_OK == result)
394                 {
395                         NFC_INFO("card emulation changed to SECURE_ELEMENT_TYPE_ESE");
396
397                         net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
398                         net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
399
400                         if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_POLICY_ESE_ON) != 0)
401                                 NFC_ERR("vconf_set_int failed");
402                 }
403                 else
404                 {
405                         NFC_ERR("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
406                 }
407                 break;
408
409         default:
410                 result = net_nfc_server_se_disable_card_emulation();
411                 if (NET_NFC_OK == result)
412                 {
413                         NFC_INFO("card emulation turned off");
414
415                         if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_POLICY_NONE) != 0)
416                                 NFC_ERR("vconf_set_int failed");
417                 }
418                 break;
419         }
420
421         return result;
422 }
423
424 static void se_close_secure_element_thread_func(gpointer user_data)
425 {
426         net_nfc_error_e result;
427         SeDataHandle *detail = user_data;
428
429         g_assert(detail != NULL);
430         g_assert(detail->object != NULL);
431         g_assert(detail->invocation != NULL);
432
433         if (_se_is_uicc_handle(detail->handle) == true)
434         {
435                 _se_uicc_close(detail->handle);
436
437                 result = NET_NFC_OK;
438         }
439         else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
440         {
441                 /* decrease client reference count */
442                 net_nfc_server_gdbus_decrease_se_count(
443                                 g_dbus_method_invocation_get_sender(detail->invocation));
444
445                 result = net_nfc_server_se_close_ese();
446         }
447         else
448         {
449                 result = NET_NFC_INVALID_HANDLE;
450         }
451 #if 0
452         if ((gdbus_se_prev_type != net_nfc_server_se_get_se_type()) ||
453                         (gdbus_se_prev_mode != net_nfc_server_se_get_se_mode()))
454         {
455                 /*return back se mode*/
456                 net_nfc_controller_set_secure_element_mode(gdbus_se_prev_type,
457                                 gdbus_se_prev_mode, &result);
458
459                 net_nfc_server_se_set_se_type(gdbus_se_prev_type);
460                 net_nfc_server_se_set_se_mode(gdbus_se_prev_mode);
461         }
462 #endif
463         net_nfc_gdbus_secure_element_complete_close_secure_element(
464                         detail->object, detail->invocation, result);
465
466         g_object_unref(detail->invocation);
467         g_object_unref(detail->object);
468
469         g_free(detail);
470
471         /* shutdown process if it doesn't need */
472         if (net_nfc_server_manager_get_active() == false &&
473                         net_nfc_server_gdbus_is_server_busy() == false)
474         {
475                 net_nfc_server_controller_deinit();
476         }
477 }
478
479 static gboolean se_handle_close_secure_element(
480                 NetNfcGDbusSecureElement *object,
481                 GDBusMethodInvocation *invocation,
482                 guint arg_handle,
483                 GVariant *smack_privilege)
484 {
485         bool ret;
486         gboolean result;
487         SeDataHandle *data;
488
489         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
490
491         /* check privilege and update client context */
492         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
493                                 "nfc-manager", "rw");
494         if (false == ret)
495         {
496                 NFC_ERR("permission denied, and finished request");
497
498                 return FALSE;
499         }
500
501         data = g_try_new0(SeDataHandle, 1);
502         if (NULL == data)
503         {
504                 NFC_ERR("Memory allocation failed");
505                 g_dbus_method_invocation_return_dbus_error(invocation,
506                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
507
508                 return FALSE;
509         }
510
511         data->object = g_object_ref(object);
512         data->invocation = g_object_ref(invocation);
513         data->handle = GUINT_TO_POINTER(arg_handle);
514
515         result = net_nfc_server_controller_async_queue_push(
516                         se_close_secure_element_thread_func, data);
517         if (FALSE == result)
518         {
519                 g_dbus_method_invocation_return_dbus_error(invocation,
520                                 "org.tizen.NetNfcService.Se.ThreadError",
521                                 "can not push to controller thread");
522
523                 g_object_unref(data->object);
524                 g_object_unref(data->invocation);
525
526                 g_free(data);
527         }
528
529         return result;
530 }
531
532 static void se_get_atr_thread_func(gpointer user_data)
533 {
534         GVariant *data;
535         data_s *atr = NULL;
536         SeDataHandle *detail = user_data;
537         net_nfc_error_e result = NET_NFC_OK;
538
539         g_assert(detail != NULL);
540         g_assert(detail->object != NULL);
541         g_assert(detail->invocation != NULL);
542
543         if (_se_is_uicc_handle(detail->handle) == true)
544         {
545                 result = NET_NFC_NOT_SUPPORTED;
546         }
547         else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
548         {
549                 net_nfc_controller_secure_element_get_atr(detail->handle, &atr, &result);
550         }
551         else
552         {
553                 NFC_ERR("invalid se handle");
554
555                 result = NET_NFC_INVALID_HANDLE;
556         }
557
558         data = net_nfc_util_gdbus_data_to_variant(atr);
559
560         net_nfc_gdbus_secure_element_complete_get_atr(detail->object, detail->invocation,
561                         result, data);
562
563         if (atr != NULL)
564         {
565                 net_nfc_util_free_data(atr);
566                 g_free(atr);
567         }
568
569         g_object_unref(detail->invocation);
570         g_object_unref(detail->object);
571
572         g_free(detail);
573 }
574
575 static gboolean se_handle_get_atr(
576                 NetNfcGDbusSecureElement *object,
577                 GDBusMethodInvocation *invocation,
578                 guint arg_handle,
579                 GVariant *smack_privilege)
580 {
581         bool ret;
582         gboolean result;
583         SeDataHandle *data;
584
585         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
586
587         /* check privilege and update client context */
588         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
589                                 "nfc-manager", "r");
590         if (false == ret)
591         {
592                 NFC_ERR("permission denied, and finished request");
593
594                 return FALSE;
595         }
596
597         data = g_try_new0(SeDataHandle, 1);
598         if (NULL == data)
599         {
600                 NFC_ERR("Memory allocation failed");
601                 g_dbus_method_invocation_return_dbus_error(invocation,
602                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
603
604                 return FALSE;
605         }
606
607         data->object = g_object_ref(object);
608         data->invocation = g_object_ref(invocation);
609         data->handle = GUINT_TO_POINTER(arg_handle);
610
611         result = net_nfc_server_controller_async_queue_push(se_get_atr_thread_func, data);
612         if (FALSE == result)
613         {
614                 g_dbus_method_invocation_return_dbus_error(invocation,
615                                 "org.tizen.NetNfcService.Se.ThreadError",
616                                 "can not push to controller thread");
617
618                 g_object_unref(data->object);
619                 g_object_unref(data->invocation);
620
621                 g_free(data);
622         }
623
624         return result;
625 }
626
627 static void se_open_secure_element_thread_func(gpointer user_data)
628 {
629         SeDataSeType *detail = user_data;
630         net_nfc_error_e result = NET_NFC_OK;
631         net_nfc_target_handle_s *handle = NULL;
632
633         g_assert(detail != NULL);
634         g_assert(detail->object != NULL);
635         g_assert(detail->invocation != NULL);
636
637 #if 0 /* opening SE doesn't affect card emulation */
638         gdbus_se_prev_type = net_nfc_server_se_get_se_type();
639         gdbus_se_prev_mode = net_nfc_server_se_get_se_mode();
640 #endif
641
642         if (detail->se_type == SECURE_ELEMENT_TYPE_UICC)
643         {
644 #if 0 /* opening SE doesn't affect card emulation */
645                 /*off ESE*/
646                 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
647                                 SECURE_ELEMENT_OFF_MODE, &result);
648
649                 /*Off UICC. UICC SHOULD not be detected by external reader when
650                   being communicated in internal process*/
651                 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
652                                 SECURE_ELEMENT_OFF_MODE, &result);
653
654                 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
655                 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
656 #endif
657                 handle = _se_uicc_open();
658                 if (handle != NULL)
659                 {
660                         result = NET_NFC_OK;
661                 }
662                 else
663                 {
664                         result = NET_NFC_INVALID_STATE;
665                         handle = NULL;
666                 }
667         }
668         else if (detail->se_type == SECURE_ELEMENT_TYPE_ESE)
669         {
670 #if 0 /* opening SE doesn't affect card emulation */
671                 /*off UICC*/
672                 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
673                                 SECURE_ELEMENT_OFF_MODE, &result);
674 #endif
675                 handle = net_nfc_server_se_open_ese();
676                 if (handle != NULL)
677                 {
678                         result = NET_NFC_OK;
679 #if 0 /* opening SE doesn't affect card emulation */
680                         net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
681                         net_nfc_server_se_set_se_mode(SECURE_ELEMENT_WIRED_MODE);
682
683                         net_nfc_server_se_set_current_ese_handle(handle);
684 #endif
685                         NFC_DBG("handle [%p]", handle);
686
687                         /* increase client reference count */
688                         net_nfc_server_gdbus_increase_se_count(
689                                         g_dbus_method_invocation_get_sender(detail->invocation));
690                 }
691                 else
692                 {
693                         result = NET_NFC_INVALID_STATE;
694                         handle = NULL;
695                 }
696         }
697         else
698         {
699                 result = NET_NFC_INVALID_PARAM;
700                 handle = NULL;
701         }
702
703         net_nfc_gdbus_secure_element_complete_open_secure_element(detail->object,
704                         detail->invocation, result, (guint)handle);
705
706         g_object_unref(detail->invocation);
707         g_object_unref(detail->object);
708
709         g_free(detail);
710 }
711
712 static gboolean se_handle_open_secure_element(
713                 NetNfcGDbusSecureElement *object,
714                 GDBusMethodInvocation *invocation,
715                 gint arg_type,
716                 GVariant *smack_privilege)
717 {
718         bool ret;
719         gboolean result;
720         SeDataSeType *data;
721
722         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
723
724         /* check privilege and update client context */
725         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
726                                 "nfc-manager", "rw");
727         if (false == ret)
728         {
729                 NFC_ERR("permission denied, and finished request");
730
731                 return FALSE;
732         }
733
734         data = g_try_new0(SeDataSeType, 1);
735         if (NULL == data)
736         {
737                 NFC_ERR("Memory allocation failed");
738                 g_dbus_method_invocation_return_dbus_error(invocation,
739                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
740
741                 return FALSE;
742         }
743
744         data->object = g_object_ref(object);
745         data->invocation = g_object_ref(invocation);
746         data->se_type= arg_type;
747
748         result = net_nfc_server_controller_async_queue_push(
749                         se_open_secure_element_thread_func, data);
750         if (FALSE == result)
751         {
752                 g_dbus_method_invocation_return_dbus_error(invocation,
753                                 "org.tizen.NetNfcService.Se.ThreadError",
754                                 "can not push to controller thread");
755
756                 g_object_unref(data->object);
757                 g_object_unref(data->invocation);
758
759                 g_free(data);
760         }
761
762         return result;
763 }
764
765 static void se_send_apdu_thread_func(gpointer user_data)
766 {
767         data_s *response = NULL;
768         GVariant *rspdata = NULL;
769         SeDataApdu *detail = user_data;
770         data_s apdu_data = { NULL, 0 };
771         net_nfc_error_e result = NET_NFC_OK;
772
773         g_assert(detail != NULL);
774         g_assert(detail->object != NULL);
775         g_assert(detail->invocation != NULL);
776
777         net_nfc_util_gdbus_variant_to_data_s(detail->data, &apdu_data);
778
779         if (_se_is_uicc_handle(detail->handle) == true)
780         {
781                 result = NET_NFC_NOT_SUPPORTED;
782         }
783         else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
784         {
785                 net_nfc_controller_secure_element_send_apdu(detail->handle,
786                                 &apdu_data, &response, &result);
787         }
788         else
789         {
790                 result = NET_NFC_INVALID_HANDLE;
791         }
792
793         rspdata = net_nfc_util_gdbus_data_to_variant(response);
794
795         net_nfc_gdbus_secure_element_complete_send_apdu(detail->object, detail->invocation,
796                         result, rspdata);
797
798         if (response != NULL)
799         {
800                 net_nfc_util_free_data(response);
801                 g_free(response);
802         }
803
804         net_nfc_util_free_data(&apdu_data);
805
806         g_variant_unref(detail->data);
807
808         g_object_unref(detail->invocation);
809         g_object_unref(detail->object);
810
811         g_free(detail);
812 }
813
814 static gboolean se_handle_send_apdu(
815                 NetNfcGDbusSecureElement *object,
816                 GDBusMethodInvocation *invocation,
817                 guint arg_handle,
818                 GVariant *apdudata,
819                 GVariant *smack_privilege)
820 {
821         bool ret;
822         gboolean result;
823         SeDataApdu *data;
824
825         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
826
827         /* check privilege and update client context */
828         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
829                                 "nfc-manager", "rw");
830         if (false == ret)
831         {
832                 NFC_ERR("permission denied, and finished request");
833
834                 return FALSE;
835         }
836
837         data = g_try_new0(SeDataApdu, 1);
838         if (NULL == data)
839         {
840                 NFC_ERR("Memory allocation failed");
841                 g_dbus_method_invocation_return_dbus_error(invocation,
842                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
843
844                 return FALSE;
845         }
846
847         data->object = g_object_ref(object);
848         data->invocation = g_object_ref(invocation);
849         data->handle = GUINT_TO_POINTER(arg_handle);
850         data->data = g_variant_ref(apdudata);
851
852         result = net_nfc_server_controller_async_queue_push(se_send_apdu_thread_func, data);
853         if (FALSE == result)
854         {
855                 g_dbus_method_invocation_return_dbus_error(invocation,
856                                 "org.tizen.NetNfcService.Se.ThreadError",
857                                 "can not push to controller thread");
858
859                 g_variant_unref(data->data);
860
861                 g_object_unref(data->object);
862                 g_object_unref(data->invocation);
863
864                 g_free(data);
865         }
866
867         return result;
868 }
869
870 static void _se_set_card_emulation_thread_func(gpointer user_data)
871 {
872         SeSetCardEmul *data = user_data;
873         net_nfc_error_e result = NET_NFC_OK;
874
875         g_assert(data != NULL);
876         g_assert(data->object != NULL);
877         g_assert(data->invocation != NULL);
878
879         if (data->mode == NET_NFC_CARD_EMELATION_ENABLE)
880         {
881                 net_nfc_controller_set_secure_element_mode(net_nfc_server_se_get_se_mode(),
882                                 SECURE_ELEMENT_VIRTUAL_MODE, &result);
883
884                 if (NET_NFC_OK == result)
885                 {
886                         NFC_DBG("changed to CARD EMULATION ON");
887
888                         net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
889                 }
890                 else
891                 {
892                         NFC_ERR("CARD EMULATION ON fail [%d]", result);
893                 }
894         }
895         else if (data->mode == NET_NFC_CARD_EMULATION_DISABLE)
896         {
897                 net_nfc_controller_set_secure_element_mode(net_nfc_server_se_get_se_mode(),
898                                 SECURE_ELEMENT_OFF_MODE, &result);
899                 if (NET_NFC_OK == result)
900                 {
901                         NFC_DBG("changed to CARD EMULATION OFF");
902
903                         net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
904                 }
905                 else
906                 {
907                         NFC_ERR("CARD EMULATION OFF fail [%d]", result);
908                 }
909         }
910         else
911         {
912                 result = NET_NFC_INVALID_PARAM;
913         }
914
915         net_nfc_gdbus_secure_element_complete_set_card_emulation(data->object,
916                         data->invocation, result);
917
918         g_object_unref(data->invocation);
919         g_object_unref(data->object);
920
921         g_free(data);
922 }
923
924 static gboolean _se_handle_set_card_emulation(
925                 NetNfcGDbusSecureElement *object,
926                 GDBusMethodInvocation *invocation,
927                 gint arg_mode,
928                 GVariant *smack_privilege)
929 {
930         bool ret;
931         gboolean result;
932         SeSetCardEmul *data;
933
934         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
935
936         /* check privilege and update client context */
937         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
938                                 "nfc-manager", "w");
939         if (false == ret)
940         {
941                 NFC_ERR("permission denied, and finished request");
942
943                 return FALSE;
944         }
945
946         data = g_try_new0(SeSetCardEmul, 1);
947         if (NULL == data)
948         {
949                 NFC_ERR("Memory allocation failed");
950                 g_dbus_method_invocation_return_dbus_error(invocation,
951                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
952
953                 return FALSE;
954         }
955
956         data->object = g_object_ref(object);
957         data->invocation = g_object_ref(invocation);
958         data->mode = arg_mode;
959
960         result = net_nfc_server_controller_async_queue_push(
961                         _se_set_card_emulation_thread_func, data);
962         if (FALSE == result)
963         {
964                 g_dbus_method_invocation_return_dbus_error(invocation,
965                                 "org.tizen.NetNfcService.Se.ThreadError",
966                                 "can not push to controller thread");
967
968                 g_object_unref(data->object);
969                 g_object_unref(data->invocation);
970
971                 g_free(data);
972         }
973
974         return result;
975 }
976
977 static void se_set_data_thread_func(gpointer user_data)
978 {
979         SeDataSeType *data = user_data;
980         gboolean isTypeChanged = FALSE;
981         net_nfc_error_e result = NET_NFC_OK;
982
983         g_assert(data != NULL);
984         g_assert(data->object != NULL);
985         g_assert(data->invocation != NULL);
986
987         if (data->se_type != net_nfc_server_se_get_se_type())
988         {
989                 result = net_nfc_server_se_change_se(data->se_type);
990                 isTypeChanged = TRUE;
991         }
992
993         net_nfc_gdbus_secure_element_complete_set(data->object, data->invocation, result);
994
995         if (isTypeChanged)
996                 net_nfc_gdbus_secure_element_emit_se_type_changed(data->object, data->se_type);
997
998         g_object_unref(data->invocation);
999         g_object_unref(data->object);
1000
1001         g_free(data);
1002 }
1003
1004 static gboolean se_handle_set(NetNfcGDbusSecureElement *object,
1005                 GDBusMethodInvocation *invocation, gint arg_type, GVariant *smack_privilege)
1006 {
1007         bool ret;
1008         gboolean result;
1009         SeDataSeType *data;
1010
1011         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1012
1013         /* check privilege and update client context */
1014         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1015                                 "nfc-manager", "w");
1016         if (false == ret)
1017         {
1018                 NFC_ERR("permission denied, and finished request");
1019
1020                 return FALSE;
1021         }
1022
1023         data = g_try_new0(SeDataSeType, 1);
1024         if (NULL == data)
1025         {
1026                 NFC_ERR("Memory allocation failed");
1027                 g_dbus_method_invocation_return_dbus_error(invocation,
1028                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
1029
1030                 return FALSE;
1031         }
1032
1033         data->object = g_object_ref(object);
1034         data->invocation = g_object_ref(invocation);
1035         data->se_type = arg_type;
1036
1037         result = net_nfc_server_controller_async_queue_push(se_set_data_thread_func, data);
1038
1039         if (FALSE == result)
1040         {
1041                 g_dbus_method_invocation_return_dbus_error(invocation,
1042                                 "org.tizen.NetNfcService.Se.ThreadError",
1043                                 "can not push to controller thread");
1044
1045                 g_object_unref(data->object);
1046                 g_object_unref(data->invocation);
1047
1048                 g_free(data);
1049         }
1050
1051         return result;
1052 }
1053
1054 static void se_get_data_thread_func(gpointer user_data)
1055 {
1056         SeDataSeType *data = user_data;
1057         net_nfc_error_e result = NET_NFC_OK;
1058
1059         g_assert(data != NULL);
1060         g_assert(data->object != NULL);
1061         g_assert(data->invocation != NULL);
1062
1063         net_nfc_gdbus_secure_element_complete_get(data->object, data->invocation,
1064                         net_nfc_server_se_get_se_type(), result);
1065
1066         g_object_unref(data->invocation);
1067         g_object_unref(data->object);
1068
1069         g_free(data);
1070 }
1071
1072 static gboolean se_handle_get(NetNfcGDbusSecureElement *object,
1073                 GDBusMethodInvocation *invocation, GVariant *smack_privilege)
1074 {
1075         bool ret;
1076         gboolean result;
1077         SeDataSeType *data;
1078
1079         NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1080
1081         /* check privilege and update client context */
1082         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1083                                 "nfc-manager", "r");
1084         if (false == ret)
1085         {
1086                 NFC_ERR("permission denied, and finished request");
1087
1088                 return FALSE;
1089         }
1090
1091         data = g_try_new0(SeDataSeType, 1);
1092         if (NULL == data)
1093         {
1094                 NFC_ERR("Memory allocation failed");
1095                 g_dbus_method_invocation_return_dbus_error(invocation,
1096                                 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
1097
1098                 return FALSE;
1099         }
1100
1101         data->object = g_object_ref(object);
1102         data->invocation = g_object_ref(invocation);
1103
1104         result = net_nfc_server_controller_async_queue_push(se_get_data_thread_func, data);
1105         if (FALSE == result)
1106         {
1107                 g_dbus_method_invocation_return_dbus_error(invocation,
1108                                 "org.tizen.NetNfcService.Se.ThreadError",
1109                                 "can not push to controller thread");
1110
1111                 g_object_unref(data->object);
1112                 g_object_unref(data->invocation);
1113
1114                 g_free(data);
1115         }
1116
1117         return result;
1118 }
1119
1120 static void _se_change_card_emulation_mode_thread_func(gpointer user_data)
1121 {
1122         bool isChanged = false;
1123         net_nfc_error_e result = NET_NFC_OK;
1124         uint8_t current_mode, return_mode;
1125         ChangeCardEmulMode *data = (ChangeCardEmulMode *)user_data;
1126
1127         g_assert(data != NULL);
1128         g_assert(data->object != NULL);
1129         g_assert(data->invocation != NULL);
1130
1131         current_mode = net_nfc_server_se_get_se_mode();
1132
1133         if(data->mode == SECURE_ELEMENT_ACTIVE_STATE
1134                         && current_mode == SECURE_ELEMENT_TYPE_INVALID)
1135         {
1136                 return_mode = net_nfc_server_se_get_return_se_mode();
1137                 result = net_nfc_server_se_change_se(return_mode);
1138                 isChanged = true;
1139         }
1140         else if(data->mode == SECURE_ELEMENT_INACTIVE_STATE
1141                         && current_mode != SECURE_ELEMENT_TYPE_INVALID)
1142         {
1143                 result = net_nfc_server_se_disable_card_emulation();
1144                 isChanged = true;
1145         }
1146
1147         net_nfc_gdbus_secure_element_complete_change_card_emulation_mode(data->object,
1148                         data->invocation, result);
1149
1150         if(isChanged)
1151         {
1152                 net_nfc_gdbus_secure_element_emit_card_emulation_mode_changed(data->object,
1153                         data->mode);
1154         }
1155
1156         g_object_unref(data->invocation);
1157         g_object_unref(data->object);
1158
1159         g_free(data);
1160 }
1161
1162
1163 static gboolean se_handle_change_card_emulation_mode(
1164                 NetNfcGDbusSecureElement *object,
1165                 GDBusMethodInvocation *invocation,
1166                 gint arg_mode,
1167                 GVariant *smack_privilege)
1168 {
1169         bool ret;
1170         gboolean result;
1171         SeSetCardEmul *data;
1172
1173         NFC_DBG(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1174
1175         /* check privilege and update client context */
1176         ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1177                                 "nfc-manager", "w");
1178         if (false == ret)
1179         {
1180                 NFC_ERR("permission denied, and finished request");
1181
1182                 return FALSE;
1183         }
1184
1185         data = g_try_new0(SeSetCardEmul, 1);
1186         if (NULL == data)
1187         {
1188                 NFC_ERR("Memory allocation failed");
1189                 g_dbus_method_invocation_return_dbus_error(invocation,
1190                                 "org.tizen.NetNfcService.AllocationError",
1191                                 "Can not allocate memory");
1192
1193                 return FALSE;
1194         }
1195
1196         data->object = g_object_ref(object);
1197         data->invocation = g_object_ref(invocation);
1198         data->mode = arg_mode;
1199
1200         result = net_nfc_server_controller_async_queue_push(
1201                         _se_change_card_emulation_mode_thread_func, data);
1202         if (FALSE == result)
1203         {
1204                 g_dbus_method_invocation_return_dbus_error(invocation,
1205                                 "org.tizen.NetNfcService.Se.ThreadError",
1206                                 "can not push to controller thread");
1207
1208                 g_object_unref(data->object);
1209                 g_object_unref(data->invocation);
1210
1211                 g_free(data);
1212         }
1213
1214         return result;
1215 }
1216
1217 gboolean net_nfc_server_se_init(GDBusConnection *connection)
1218 {
1219         gboolean result;
1220         GError *error = NULL;
1221
1222         if (se_skeleton)
1223                 g_object_unref(se_skeleton);
1224
1225         /* initialize UICC */
1226         _se_uicc_init();
1227
1228         se_skeleton = net_nfc_gdbus_secure_element_skeleton_new();
1229
1230         g_signal_connect(se_skeleton, "handle-set", G_CALLBACK(se_handle_set), NULL);
1231
1232         g_signal_connect(se_skeleton, "handle-get", G_CALLBACK(se_handle_get), NULL);
1233
1234         g_signal_connect(se_skeleton, "handle-set-card-emulation",
1235                         G_CALLBACK(_se_handle_set_card_emulation), NULL);
1236
1237         g_signal_connect(se_skeleton, "handle-open-secure-element",
1238                         G_CALLBACK(se_handle_open_secure_element), NULL);
1239
1240         g_signal_connect(se_skeleton, "handle-close-secure-element",
1241                         G_CALLBACK(se_handle_close_secure_element), NULL);
1242
1243         g_signal_connect(se_skeleton,   "handle-get-atr", G_CALLBACK(se_handle_get_atr), NULL);
1244
1245         g_signal_connect(se_skeleton, "handle-send-apdu",
1246                         G_CALLBACK(se_handle_send_apdu), NULL);
1247
1248         g_signal_connect(se_skeleton, "handle-change-card-emulation-mode",
1249                         G_CALLBACK(se_handle_change_card_emulation_mode), NULL);
1250
1251         result = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(se_skeleton),
1252                         connection, "/org/tizen/NetNfcService/SecureElement", &error);
1253         if (FALSE == result)
1254         {
1255                 NFC_ERR("can not skeleton_export %s", error->message);
1256
1257                 g_error_free(error);
1258
1259                 net_nfc_server_se_deinit();
1260         }
1261
1262         return result;
1263 }
1264
1265 void net_nfc_server_se_deinit(void)
1266 {
1267         if (se_skeleton)
1268         {
1269                 g_object_unref(se_skeleton);
1270                 se_skeleton = NULL;
1271
1272                 /* de-initialize UICC */
1273                 _se_uicc_deinit();
1274         }
1275 }
1276
1277 static void se_detected_thread_func(gpointer user_data)
1278 {
1279         uint32_t devType = 0;
1280         GVariant *data = NULL;
1281         net_nfc_error_e result = NET_NFC_OK;
1282         net_nfc_target_handle_s *handle = NULL;
1283
1284         g_assert(user_data != NULL);
1285
1286         if (NULL == se_skeleton)
1287         {
1288                 NFC_ERR("se skeleton is not initialized");
1289
1290                 g_variant_unref((GVariant *)user_data);
1291
1292                 return;
1293         }
1294
1295         g_variant_get((GVariant *)user_data, "uu@a(y)", (guint *)&handle, &devType, &data);
1296
1297         net_nfc_server_se_set_current_ese_handle(handle);
1298
1299         NFC_DBG("trying to connect to ESE = [0x%p]", handle);
1300
1301         if (!net_nfc_controller_connect(handle, &result))
1302                 NFC_DBG("connect failed = [%d]", result);
1303
1304         net_nfc_gdbus_secure_element_emit_ese_detected(se_skeleton, GPOINTER_TO_UINT(handle),
1305                         devType, data);
1306
1307         g_variant_unref((GVariant *)user_data);
1308 }
1309
1310 static void se_transcation_thread_func(gpointer user_data)
1311 {
1312         bool fg_dispatch;
1313         pid_t focus_app_pid;
1314         ServerSeData *detail = user_data;
1315
1316
1317         g_assert(user_data != NULL);
1318
1319         if (detail->event == NET_NFC_MESSAGE_SE_START_TRANSACTION)
1320         {
1321                 GVariant *aid = NULL;
1322                 GVariant *param = NULL;
1323                 uint8_t se_type;
1324
1325                 NFC_DBG("launch se app");
1326
1327                 aid = net_nfc_util_gdbus_data_to_variant(&(detail->aid));
1328                 param = net_nfc_util_gdbus_data_to_variant(&(detail->param));
1329                 se_type = net_nfc_server_se_get_se_type();
1330                 focus_app_pid = net_nfc_app_util_get_focus_app_pid();
1331                 fg_dispatch = net_nfc_app_util_check_launch_state();
1332
1333                 /* TODO : check access control */
1334                 net_nfc_gdbus_secure_element_emit_transaction_event (
1335                                 se_skeleton,
1336                                 se_type,
1337                                 aid,
1338                                 param,
1339                                 fg_dispatch,
1340                                 focus_app_pid);
1341
1342                 net_nfc_app_util_launch_se_transaction_app(
1343                                 se_type,
1344                                 detail->aid.buffer,
1345                                 detail->aid.length,
1346                                 detail->param.buffer,
1347                                 detail->param.length);
1348
1349                 NFC_DBG("launch se app end");
1350         }
1351
1352         net_nfc_util_free_data(&detail->param);
1353         net_nfc_util_free_data(&detail->aid);
1354
1355         g_free(detail);
1356 }
1357
1358 void net_nfc_server_se_detected(void *info)
1359 {
1360         gboolean ret;
1361         GVariant *data;
1362         GVariant *parameter;
1363         net_nfc_request_target_detected_t *se_target =
1364                 (net_nfc_request_target_detected_t *)info;
1365
1366         data = net_nfc_util_gdbus_buffer_to_variant(se_target->target_info_values.buffer,
1367                         se_target->target_info_values.length);
1368
1369         parameter = g_variant_new("uu@a(y)", GPOINTER_TO_UINT(se_target->handle),
1370                         se_target->devType, data);
1371         if (parameter != NULL)
1372         {
1373                 ret = net_nfc_server_controller_async_queue_push(se_detected_thread_func,
1374                                         parameter);
1375                 if (FALSE == ret)
1376                 {
1377                         NFC_ERR("can not push to controller thread");
1378
1379                         g_variant_unref(parameter);
1380                 }
1381         }
1382         else
1383         {
1384                 NFC_ERR("g_variant_new failed");
1385         }
1386
1387         /* FIXME : should be removed when plugins would be fixed*/
1388         _net_nfc_util_free_mem(info);
1389 }
1390
1391 void net_nfc_server_se_transaction_received(void *info)
1392 {
1393         gboolean ret;
1394         ServerSeData *detail;
1395         net_nfc_request_se_event_t *se_event = (net_nfc_request_se_event_t *)info;
1396
1397         detail = g_try_new0(ServerSeData, 1);
1398         if (detail != NULL)
1399         {
1400                 detail->event = se_event->request_type;
1401
1402                 if (se_event->aid.buffer != NULL && se_event->aid.length > 0)
1403                 {
1404                         if (net_nfc_util_alloc_data(&detail->aid, se_event->aid.length) == true)
1405                                 memcpy(detail->aid.buffer, se_event->aid.buffer, se_event->aid.length);
1406                 }
1407
1408                 if (se_event->param.buffer != NULL && se_event->param.length > 0)
1409                 {
1410                         if (net_nfc_util_alloc_data(&detail->param, se_event->param.length) == true)
1411                                 memcpy(detail->param.buffer, se_event->param.buffer, se_event->param.length);
1412                 }
1413
1414                 ret = net_nfc_server_controller_async_queue_push(
1415                                         se_transcation_thread_func, detail);
1416                 if (FALSE == ret)
1417                 {
1418                         NFC_ERR("can not push to controller thread");
1419
1420                         net_nfc_util_free_data(&detail->param);
1421                         net_nfc_util_free_data(&detail->aid);
1422
1423                         g_free(detail);
1424                 }
1425         }
1426         else
1427         {
1428                 NFC_ERR("g_new0 failed");
1429         }
1430
1431         /* FIXME : should be removed when plugins would be fixed*/
1432         net_nfc_util_free_data(&se_event->param);
1433         net_nfc_util_free_data(&se_event->aid);
1434
1435         _net_nfc_util_free_mem(info);
1436 }