2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <tapi_common.h>
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"
35 SE_UICC_UNAVAILABLE = -1,
36 SE_UICC_ON_PROGRESS = 0,
40 typedef struct _nfc_se_setting_t
47 net_nfc_server_se_setting_t;
50 static NetNfcGDbusSecureElement *se_skeleton = NULL;
52 static uint8_t gdbus_se_prev_type = SECURE_ELEMENT_TYPE_INVALID;
53 static uint8_t gdbus_se_prev_mode = SECURE_ELEMENT_OFF_MODE;
55 static net_nfc_server_se_setting_t gdbus_se_setting;
57 /* TODO : make a list for handles */
58 static TapiHandle *gdbus_uicc_handle;
59 static net_nfc_target_handle_s *gdbus_ese_handle;
61 static int gdbus_uicc_ready;
64 typedef struct _ServerSeData ServerSeData;
73 typedef struct _SeSetCardEmul SeSetCardEmul;
77 NetNfcGDbusSecureElement *object;
78 GDBusMethodInvocation *invocation;
82 typedef struct _SeDataSeType SeDataSeType;
86 NetNfcGDbusSecureElement *object;
87 GDBusMethodInvocation *invocation;
92 typedef struct _SeDataHandle SeDataHandle;
96 NetNfcGDbusSecureElement *object;
97 GDBusMethodInvocation *invocation;
98 net_nfc_target_handle_s *handle;
101 typedef struct _SeDataApdu SeDataApdu;
105 NetNfcGDbusSecureElement *object;
106 GDBusMethodInvocation *invocation;
107 net_nfc_target_handle_s *handle;
111 typedef struct _ChangeCardEmulMode ChangeCardEmulMode;
113 struct _ChangeCardEmulMode
115 NetNfcGDbusSecureElement *object;
116 GDBusMethodInvocation *invocation;
120 uint8_t net_nfc_server_se_get_se_type()
122 return gdbus_se_setting.type;
125 uint8_t net_nfc_server_se_get_return_se_mode()
127 return gdbus_se_setting.return_type;
130 uint8_t net_nfc_server_se_get_se_mode()
132 return gdbus_se_setting.mode;
135 static void net_nfc_server_se_set_se_type(uint8_t type)
137 gdbus_se_setting.return_type = gdbus_se_setting.type;
138 gdbus_se_setting.type = type;
141 static void net_nfc_server_se_set_se_mode(uint8_t mode)
143 gdbus_se_setting.mode = mode;
148 static bool net_nfc_server_se_is_ese_handle(net_nfc_target_handle_s *handle)
150 return (gdbus_ese_handle != NULL && gdbus_ese_handle == handle);
153 static void net_nfc_server_se_set_current_ese_handle(
154 net_nfc_target_handle_s *handle)
156 gdbus_ese_handle = handle;
159 static net_nfc_target_handle_s *net_nfc_server_se_open_ese()
163 if (NULL == gdbus_ese_handle)
165 net_nfc_error_e result = NET_NFC_OK;
166 net_nfc_target_handle_s *handle = NULL;
168 ret = net_nfc_controller_secure_element_open(SECURE_ELEMENT_TYPE_ESE,
172 net_nfc_server_se_set_current_ese_handle(handle);
174 NFC_DBG("handle [%p]", handle);
178 NFC_ERR("net_nfc_controller_secure_element_open failed [%d]", result);
182 return gdbus_ese_handle;
185 static net_nfc_error_e net_nfc_server_se_close_ese()
187 net_nfc_error_e result = NET_NFC_OK;
189 if (gdbus_ese_handle != NULL && net_nfc_server_gdbus_is_server_busy() == false)
191 if (net_nfc_controller_secure_element_close(gdbus_ese_handle, &result) == false)
192 net_nfc_controller_exception_handler();
194 net_nfc_server_se_set_current_ese_handle(NULL);
200 static void _se_uicc_enable_card_emulation()
202 net_nfc_error_e result;
205 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
206 SECURE_ELEMENT_OFF_MODE, &result);
209 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
210 SECURE_ELEMENT_VIRTUAL_MODE, &result);
212 if (NET_NFC_OK == result)
214 NFC_INFO("card emulation changed to SECURE_ELEMENT_TYPE_UICC");
216 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
217 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
219 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_POLICY_UICC_ON) < 0)
220 NFC_ERR("vconf_set_int failed");
224 NFC_ERR("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
228 static void _se_uicc_prepare(void)
232 cpList = tel_get_cp_name_list();
235 gdbus_uicc_handle = tel_init(cpList[0]);
236 if (NULL == gdbus_uicc_handle)
237 NFC_ERR("tel_init() failed");
242 NFC_ERR("tel_get_cp_name_list() failed");
246 static void _se_uicc_status_noti_cb(TapiHandle *handle,
247 const char *noti_id, void *data, void *user_data)
249 TelSimCardStatus_t *status = data;
251 NFC_DBG("_se_uicc_status_noti_cb");
255 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED :
256 gdbus_uicc_ready = SE_UICC_READY;
260 if (gdbus_se_setting.busy == true &&
261 net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC)
263 gdbus_se_setting.busy = false;
265 _se_uicc_enable_card_emulation();
269 case TAPI_SIM_STATUS_CARD_REMOVED :
270 NFC_DBG("TAPI_SIM_STATUS_CARD_REMOVED");
271 gdbus_uicc_ready = SE_UICC_UNAVAILABLE;
273 if (net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC)
275 net_nfc_error_e result;
278 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
279 SECURE_ELEMENT_OFF_MODE, &result);
281 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
282 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
291 static void _se_uicc_init(void)
294 tel_register_noti_event(gdbus_uicc_handle, TAPI_NOTI_SIM_STATUS,
295 _se_uicc_status_noti_cb, NULL);
298 static void _se_uicc_deinit()
300 if (gdbus_uicc_handle != NULL)
302 tel_deregister_noti_event(gdbus_uicc_handle, TAPI_NOTI_SIM_STATUS);
304 tel_deinit(gdbus_uicc_handle);
306 gdbus_uicc_handle = NULL;
310 static net_nfc_target_handle_s* _se_uicc_open()
312 net_nfc_target_handle_s *result = NULL;
314 if (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL)
315 result = (net_nfc_target_handle_s *)gdbus_uicc_handle;
320 static bool _se_is_uicc_handle(net_nfc_target_handle_s *handle)
322 return (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL &&
323 (TapiHandle *)handle == gdbus_uicc_handle);
326 static void _se_uicc_close(net_nfc_target_handle_s *handle)
331 net_nfc_error_e net_nfc_server_se_disable_card_emulation()
333 net_nfc_error_e result;
335 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
336 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
339 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
340 SECURE_ELEMENT_OFF_MODE, &result);
343 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
344 SECURE_ELEMENT_OFF_MODE, &result);
349 net_nfc_error_e net_nfc_server_se_change_se(uint8_t type)
351 net_nfc_error_e result = NET_NFC_OK;
355 case SECURE_ELEMENT_TYPE_UICC :
356 if (false == gdbus_se_setting.busy)
358 if (SE_UICC_READY == gdbus_uicc_ready)
360 _se_uicc_enable_card_emulation();
362 else if (SE_UICC_ON_PROGRESS == gdbus_uicc_ready)
364 NFC_INFO("waiting for uicc initializing complete...");
366 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
367 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
369 gdbus_se_setting.busy = true;
373 result = NET_NFC_NOT_SUPPORTED;
378 NFC_DBG("Previous request is processing.");
380 result = NET_NFC_BUSY;
384 case SECURE_ELEMENT_TYPE_ESE :
386 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
387 SECURE_ELEMENT_OFF_MODE, &result);
390 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
391 SECURE_ELEMENT_VIRTUAL_MODE, &result);
393 if (NET_NFC_OK == result)
395 NFC_INFO("card emulation changed to SECURE_ELEMENT_TYPE_ESE");
397 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
398 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
400 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_POLICY_ESE_ON) != 0)
401 NFC_ERR("vconf_set_int failed");
405 NFC_ERR("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
410 result = net_nfc_server_se_disable_card_emulation();
411 if (NET_NFC_OK == result)
413 NFC_INFO("card emulation turned off");
415 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_POLICY_NONE) != 0)
416 NFC_ERR("vconf_set_int failed");
424 static void se_close_secure_element_thread_func(gpointer user_data)
426 net_nfc_error_e result;
427 SeDataHandle *detail = user_data;
429 g_assert(detail != NULL);
430 g_assert(detail->object != NULL);
431 g_assert(detail->invocation != NULL);
433 if (_se_is_uicc_handle(detail->handle) == true)
435 _se_uicc_close(detail->handle);
439 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
441 /* decrease client reference count */
442 net_nfc_server_gdbus_decrease_se_count(
443 g_dbus_method_invocation_get_sender(detail->invocation));
445 result = net_nfc_server_se_close_ese();
449 result = NET_NFC_INVALID_HANDLE;
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()))
455 /*return back se mode*/
456 net_nfc_controller_set_secure_element_mode(gdbus_se_prev_type,
457 gdbus_se_prev_mode, &result);
459 net_nfc_server_se_set_se_type(gdbus_se_prev_type);
460 net_nfc_server_se_set_se_mode(gdbus_se_prev_mode);
463 net_nfc_gdbus_secure_element_complete_close_secure_element(
464 detail->object, detail->invocation, result);
466 g_object_unref(detail->invocation);
467 g_object_unref(detail->object);
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)
475 net_nfc_server_controller_deinit();
479 static gboolean se_handle_close_secure_element(
480 NetNfcGDbusSecureElement *object,
481 GDBusMethodInvocation *invocation,
483 GVariant *smack_privilege)
489 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
491 /* check privilege and update client context */
492 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
493 "nfc-manager", "rw");
496 NFC_ERR("permission denied, and finished request");
501 data = g_try_new0(SeDataHandle, 1);
504 NFC_ERR("Memory allocation failed");
505 g_dbus_method_invocation_return_dbus_error(invocation,
506 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
511 data->object = g_object_ref(object);
512 data->invocation = g_object_ref(invocation);
513 data->handle = GUINT_TO_POINTER(arg_handle);
515 result = net_nfc_server_controller_async_queue_push(
516 se_close_secure_element_thread_func, data);
519 g_dbus_method_invocation_return_dbus_error(invocation,
520 "org.tizen.NetNfcService.Se.ThreadError",
521 "can not push to controller thread");
523 g_object_unref(data->object);
524 g_object_unref(data->invocation);
532 static void se_get_atr_thread_func(gpointer user_data)
536 SeDataHandle *detail = user_data;
537 net_nfc_error_e result = NET_NFC_OK;
539 g_assert(detail != NULL);
540 g_assert(detail->object != NULL);
541 g_assert(detail->invocation != NULL);
543 if (_se_is_uicc_handle(detail->handle) == true)
545 result = NET_NFC_NOT_SUPPORTED;
547 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
549 net_nfc_controller_secure_element_get_atr(detail->handle, &atr, &result);
553 NFC_ERR("invalid se handle");
555 result = NET_NFC_INVALID_HANDLE;
558 data = net_nfc_util_gdbus_data_to_variant(atr);
560 net_nfc_gdbus_secure_element_complete_get_atr(detail->object, detail->invocation,
565 net_nfc_util_free_data(atr);
569 g_object_unref(detail->invocation);
570 g_object_unref(detail->object);
575 static gboolean se_handle_get_atr(
576 NetNfcGDbusSecureElement *object,
577 GDBusMethodInvocation *invocation,
579 GVariant *smack_privilege)
585 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
587 /* check privilege and update client context */
588 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
592 NFC_ERR("permission denied, and finished request");
597 data = g_try_new0(SeDataHandle, 1);
600 NFC_ERR("Memory allocation failed");
601 g_dbus_method_invocation_return_dbus_error(invocation,
602 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
607 data->object = g_object_ref(object);
608 data->invocation = g_object_ref(invocation);
609 data->handle = GUINT_TO_POINTER(arg_handle);
611 result = net_nfc_server_controller_async_queue_push(se_get_atr_thread_func, data);
614 g_dbus_method_invocation_return_dbus_error(invocation,
615 "org.tizen.NetNfcService.Se.ThreadError",
616 "can not push to controller thread");
618 g_object_unref(data->object);
619 g_object_unref(data->invocation);
627 static void se_open_secure_element_thread_func(gpointer user_data)
629 SeDataSeType *detail = user_data;
630 net_nfc_error_e result = NET_NFC_OK;
631 net_nfc_target_handle_s *handle = NULL;
633 g_assert(detail != NULL);
634 g_assert(detail->object != NULL);
635 g_assert(detail->invocation != NULL);
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();
642 if (detail->se_type == SECURE_ELEMENT_TYPE_UICC)
644 #if 0 /* opening SE doesn't affect card emulation */
646 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
647 SECURE_ELEMENT_OFF_MODE, &result);
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);
654 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
655 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
657 handle = _se_uicc_open();
664 result = NET_NFC_INVALID_STATE;
668 else if (detail->se_type == SECURE_ELEMENT_TYPE_ESE)
670 #if 0 /* opening SE doesn't affect card emulation */
672 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
673 SECURE_ELEMENT_OFF_MODE, &result);
675 handle = net_nfc_server_se_open_ese();
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);
683 net_nfc_server_se_set_current_ese_handle(handle);
685 NFC_DBG("handle [%p]", handle);
687 /* increase client reference count */
688 net_nfc_server_gdbus_increase_se_count(
689 g_dbus_method_invocation_get_sender(detail->invocation));
693 result = NET_NFC_INVALID_STATE;
699 result = NET_NFC_INVALID_PARAM;
703 net_nfc_gdbus_secure_element_complete_open_secure_element(detail->object,
704 detail->invocation, result, (guint)handle);
706 g_object_unref(detail->invocation);
707 g_object_unref(detail->object);
712 static gboolean se_handle_open_secure_element(
713 NetNfcGDbusSecureElement *object,
714 GDBusMethodInvocation *invocation,
716 GVariant *smack_privilege)
722 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
724 /* check privilege and update client context */
725 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
726 "nfc-manager", "rw");
729 NFC_ERR("permission denied, and finished request");
734 data = g_try_new0(SeDataSeType, 1);
737 NFC_ERR("Memory allocation failed");
738 g_dbus_method_invocation_return_dbus_error(invocation,
739 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
744 data->object = g_object_ref(object);
745 data->invocation = g_object_ref(invocation);
746 data->se_type= arg_type;
748 result = net_nfc_server_controller_async_queue_push(
749 se_open_secure_element_thread_func, data);
752 g_dbus_method_invocation_return_dbus_error(invocation,
753 "org.tizen.NetNfcService.Se.ThreadError",
754 "can not push to controller thread");
756 g_object_unref(data->object);
757 g_object_unref(data->invocation);
765 static void se_send_apdu_thread_func(gpointer user_data)
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;
773 g_assert(detail != NULL);
774 g_assert(detail->object != NULL);
775 g_assert(detail->invocation != NULL);
777 net_nfc_util_gdbus_variant_to_data_s(detail->data, &apdu_data);
779 if (_se_is_uicc_handle(detail->handle) == true)
781 result = NET_NFC_NOT_SUPPORTED;
783 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
785 net_nfc_controller_secure_element_send_apdu(detail->handle,
786 &apdu_data, &response, &result);
790 result = NET_NFC_INVALID_HANDLE;
793 rspdata = net_nfc_util_gdbus_data_to_variant(response);
795 net_nfc_gdbus_secure_element_complete_send_apdu(detail->object, detail->invocation,
798 if (response != NULL)
800 net_nfc_util_free_data(response);
804 net_nfc_util_free_data(&apdu_data);
806 g_variant_unref(detail->data);
808 g_object_unref(detail->invocation);
809 g_object_unref(detail->object);
814 static gboolean se_handle_send_apdu(
815 NetNfcGDbusSecureElement *object,
816 GDBusMethodInvocation *invocation,
819 GVariant *smack_privilege)
825 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
827 /* check privilege and update client context */
828 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
829 "nfc-manager", "rw");
832 NFC_ERR("permission denied, and finished request");
837 data = g_try_new0(SeDataApdu, 1);
840 NFC_ERR("Memory allocation failed");
841 g_dbus_method_invocation_return_dbus_error(invocation,
842 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
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);
852 result = net_nfc_server_controller_async_queue_push(se_send_apdu_thread_func, data);
855 g_dbus_method_invocation_return_dbus_error(invocation,
856 "org.tizen.NetNfcService.Se.ThreadError",
857 "can not push to controller thread");
859 g_variant_unref(data->data);
861 g_object_unref(data->object);
862 g_object_unref(data->invocation);
870 static void _se_set_card_emulation_thread_func(gpointer user_data)
872 SeSetCardEmul *data = user_data;
873 net_nfc_error_e result = NET_NFC_OK;
875 g_assert(data != NULL);
876 g_assert(data->object != NULL);
877 g_assert(data->invocation != NULL);
879 if (data->mode == NET_NFC_CARD_EMELATION_ENABLE)
881 net_nfc_controller_set_secure_element_mode(net_nfc_server_se_get_se_mode(),
882 SECURE_ELEMENT_VIRTUAL_MODE, &result);
884 if (NET_NFC_OK == result)
886 NFC_DBG("changed to CARD EMULATION ON");
888 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
892 NFC_ERR("CARD EMULATION ON fail [%d]", result);
895 else if (data->mode == NET_NFC_CARD_EMULATION_DISABLE)
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)
901 NFC_DBG("changed to CARD EMULATION OFF");
903 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
907 NFC_ERR("CARD EMULATION OFF fail [%d]", result);
912 result = NET_NFC_INVALID_PARAM;
915 net_nfc_gdbus_secure_element_complete_set_card_emulation(data->object,
916 data->invocation, result);
918 g_object_unref(data->invocation);
919 g_object_unref(data->object);
924 static gboolean _se_handle_set_card_emulation(
925 NetNfcGDbusSecureElement *object,
926 GDBusMethodInvocation *invocation,
928 GVariant *smack_privilege)
934 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
936 /* check privilege and update client context */
937 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
941 NFC_ERR("permission denied, and finished request");
946 data = g_try_new0(SeSetCardEmul, 1);
949 NFC_ERR("Memory allocation failed");
950 g_dbus_method_invocation_return_dbus_error(invocation,
951 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
956 data->object = g_object_ref(object);
957 data->invocation = g_object_ref(invocation);
958 data->mode = arg_mode;
960 result = net_nfc_server_controller_async_queue_push(
961 _se_set_card_emulation_thread_func, data);
964 g_dbus_method_invocation_return_dbus_error(invocation,
965 "org.tizen.NetNfcService.Se.ThreadError",
966 "can not push to controller thread");
968 g_object_unref(data->object);
969 g_object_unref(data->invocation);
977 static void se_set_data_thread_func(gpointer user_data)
979 SeDataSeType *data = user_data;
980 gboolean isTypeChanged = FALSE;
981 net_nfc_error_e result = NET_NFC_OK;
983 g_assert(data != NULL);
984 g_assert(data->object != NULL);
985 g_assert(data->invocation != NULL);
987 if (data->se_type != net_nfc_server_se_get_se_type())
989 result = net_nfc_server_se_change_se(data->se_type);
990 isTypeChanged = TRUE;
993 net_nfc_gdbus_secure_element_complete_set(data->object, data->invocation, result);
996 net_nfc_gdbus_secure_element_emit_se_type_changed(data->object, data->se_type);
998 g_object_unref(data->invocation);
999 g_object_unref(data->object);
1004 static gboolean se_handle_set(NetNfcGDbusSecureElement *object,
1005 GDBusMethodInvocation *invocation, gint arg_type, GVariant *smack_privilege)
1011 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1013 /* check privilege and update client context */
1014 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1015 "nfc-manager", "w");
1018 NFC_ERR("permission denied, and finished request");
1023 data = g_try_new0(SeDataSeType, 1);
1026 NFC_ERR("Memory allocation failed");
1027 g_dbus_method_invocation_return_dbus_error(invocation,
1028 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
1033 data->object = g_object_ref(object);
1034 data->invocation = g_object_ref(invocation);
1035 data->se_type = arg_type;
1037 result = net_nfc_server_controller_async_queue_push(se_set_data_thread_func, data);
1039 if (FALSE == result)
1041 g_dbus_method_invocation_return_dbus_error(invocation,
1042 "org.tizen.NetNfcService.Se.ThreadError",
1043 "can not push to controller thread");
1045 g_object_unref(data->object);
1046 g_object_unref(data->invocation);
1054 static void se_get_data_thread_func(gpointer user_data)
1056 SeDataSeType *data = user_data;
1057 net_nfc_error_e result = NET_NFC_OK;
1059 g_assert(data != NULL);
1060 g_assert(data->object != NULL);
1061 g_assert(data->invocation != NULL);
1063 net_nfc_gdbus_secure_element_complete_get(data->object, data->invocation,
1064 net_nfc_server_se_get_se_type(), result);
1066 g_object_unref(data->invocation);
1067 g_object_unref(data->object);
1072 static gboolean se_handle_get(NetNfcGDbusSecureElement *object,
1073 GDBusMethodInvocation *invocation, GVariant *smack_privilege)
1079 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1081 /* check privilege and update client context */
1082 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1083 "nfc-manager", "r");
1086 NFC_ERR("permission denied, and finished request");
1091 data = g_try_new0(SeDataSeType, 1);
1094 NFC_ERR("Memory allocation failed");
1095 g_dbus_method_invocation_return_dbus_error(invocation,
1096 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
1101 data->object = g_object_ref(object);
1102 data->invocation = g_object_ref(invocation);
1104 result = net_nfc_server_controller_async_queue_push(se_get_data_thread_func, data);
1105 if (FALSE == result)
1107 g_dbus_method_invocation_return_dbus_error(invocation,
1108 "org.tizen.NetNfcService.Se.ThreadError",
1109 "can not push to controller thread");
1111 g_object_unref(data->object);
1112 g_object_unref(data->invocation);
1120 static void _se_change_card_emulation_mode_thread_func(gpointer user_data)
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;
1127 g_assert(data != NULL);
1128 g_assert(data->object != NULL);
1129 g_assert(data->invocation != NULL);
1131 current_mode = net_nfc_server_se_get_se_mode();
1133 if(data->mode == SECURE_ELEMENT_ACTIVE_STATE
1134 && current_mode == SECURE_ELEMENT_TYPE_INVALID)
1136 return_mode = net_nfc_server_se_get_return_se_mode();
1137 result = net_nfc_server_se_change_se(return_mode);
1140 else if(data->mode == SECURE_ELEMENT_INACTIVE_STATE
1141 && current_mode != SECURE_ELEMENT_TYPE_INVALID)
1143 result = net_nfc_server_se_disable_card_emulation();
1147 net_nfc_gdbus_secure_element_complete_change_card_emulation_mode(data->object,
1148 data->invocation, result);
1152 net_nfc_gdbus_secure_element_emit_card_emulation_mode_changed(data->object,
1156 g_object_unref(data->invocation);
1157 g_object_unref(data->object);
1163 static gboolean se_handle_change_card_emulation_mode(
1164 NetNfcGDbusSecureElement *object,
1165 GDBusMethodInvocation *invocation,
1167 GVariant *smack_privilege)
1171 SeSetCardEmul *data;
1173 NFC_DBG(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1175 /* check privilege and update client context */
1176 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1177 "nfc-manager", "w");
1180 NFC_ERR("permission denied, and finished request");
1185 data = g_try_new0(SeSetCardEmul, 1);
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");
1196 data->object = g_object_ref(object);
1197 data->invocation = g_object_ref(invocation);
1198 data->mode = arg_mode;
1200 result = net_nfc_server_controller_async_queue_push(
1201 _se_change_card_emulation_mode_thread_func, data);
1202 if (FALSE == result)
1204 g_dbus_method_invocation_return_dbus_error(invocation,
1205 "org.tizen.NetNfcService.Se.ThreadError",
1206 "can not push to controller thread");
1208 g_object_unref(data->object);
1209 g_object_unref(data->invocation);
1217 gboolean net_nfc_server_se_init(GDBusConnection *connection)
1220 GError *error = NULL;
1223 g_object_unref(se_skeleton);
1225 /* initialize UICC */
1228 se_skeleton = net_nfc_gdbus_secure_element_skeleton_new();
1230 g_signal_connect(se_skeleton, "handle-set", G_CALLBACK(se_handle_set), NULL);
1232 g_signal_connect(se_skeleton, "handle-get", G_CALLBACK(se_handle_get), NULL);
1234 g_signal_connect(se_skeleton, "handle-set-card-emulation",
1235 G_CALLBACK(_se_handle_set_card_emulation), NULL);
1237 g_signal_connect(se_skeleton, "handle-open-secure-element",
1238 G_CALLBACK(se_handle_open_secure_element), NULL);
1240 g_signal_connect(se_skeleton, "handle-close-secure-element",
1241 G_CALLBACK(se_handle_close_secure_element), NULL);
1243 g_signal_connect(se_skeleton, "handle-get-atr", G_CALLBACK(se_handle_get_atr), NULL);
1245 g_signal_connect(se_skeleton, "handle-send-apdu",
1246 G_CALLBACK(se_handle_send_apdu), NULL);
1248 g_signal_connect(se_skeleton, "handle-change-card-emulation-mode",
1249 G_CALLBACK(se_handle_change_card_emulation_mode), NULL);
1251 result = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(se_skeleton),
1252 connection, "/org/tizen/NetNfcService/SecureElement", &error);
1253 if (FALSE == result)
1255 NFC_ERR("can not skeleton_export %s", error->message);
1257 g_error_free(error);
1259 net_nfc_server_se_deinit();
1265 void net_nfc_server_se_deinit(void)
1269 g_object_unref(se_skeleton);
1272 /* de-initialize UICC */
1277 static void se_detected_thread_func(gpointer user_data)
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;
1284 g_assert(user_data != NULL);
1286 if (NULL == se_skeleton)
1288 NFC_ERR("se skeleton is not initialized");
1290 g_variant_unref((GVariant *)user_data);
1295 g_variant_get((GVariant *)user_data, "uu@a(y)", (guint *)&handle, &devType, &data);
1297 net_nfc_server_se_set_current_ese_handle(handle);
1299 NFC_DBG("trying to connect to ESE = [0x%p]", handle);
1301 if (!net_nfc_controller_connect(handle, &result))
1302 NFC_DBG("connect failed = [%d]", result);
1304 net_nfc_gdbus_secure_element_emit_ese_detected(se_skeleton, GPOINTER_TO_UINT(handle),
1307 g_variant_unref((GVariant *)user_data);
1310 static void se_transcation_thread_func(gpointer user_data)
1313 pid_t focus_app_pid;
1314 ServerSeData *detail = user_data;
1317 g_assert(user_data != NULL);
1319 if (detail->event == NET_NFC_MESSAGE_SE_START_TRANSACTION)
1321 GVariant *aid = NULL;
1322 GVariant *param = NULL;
1325 NFC_DBG("launch se app");
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();
1333 /* TODO : check access control */
1334 net_nfc_gdbus_secure_element_emit_transaction_event (
1342 net_nfc_app_util_launch_se_transaction_app(
1346 detail->param.buffer,
1347 detail->param.length);
1349 NFC_DBG("launch se app end");
1352 net_nfc_util_free_data(&detail->param);
1353 net_nfc_util_free_data(&detail->aid);
1358 void net_nfc_server_se_detected(void *info)
1362 GVariant *parameter;
1363 net_nfc_request_target_detected_t *se_target =
1364 (net_nfc_request_target_detected_t *)info;
1366 data = net_nfc_util_gdbus_buffer_to_variant(se_target->target_info_values.buffer,
1367 se_target->target_info_values.length);
1369 parameter = g_variant_new("uu@a(y)", GPOINTER_TO_UINT(se_target->handle),
1370 se_target->devType, data);
1371 if (parameter != NULL)
1373 ret = net_nfc_server_controller_async_queue_push(se_detected_thread_func,
1377 NFC_ERR("can not push to controller thread");
1379 g_variant_unref(parameter);
1384 NFC_ERR("g_variant_new failed");
1387 /* FIXME : should be removed when plugins would be fixed*/
1388 _net_nfc_util_free_mem(info);
1391 void net_nfc_server_se_transaction_received(void *info)
1394 ServerSeData *detail;
1395 net_nfc_request_se_event_t *se_event = (net_nfc_request_se_event_t *)info;
1397 detail = g_try_new0(ServerSeData, 1);
1400 detail->event = se_event->request_type;
1402 if (se_event->aid.buffer != NULL && se_event->aid.length > 0)
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);
1408 if (se_event->param.buffer != NULL && se_event->param.length > 0)
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);
1414 ret = net_nfc_server_controller_async_queue_push(
1415 se_transcation_thread_func, detail);
1418 NFC_ERR("can not push to controller thread");
1420 net_nfc_util_free_data(&detail->param);
1421 net_nfc_util_free_data(&detail->aid);
1428 NFC_ERR("g_new0 failed");
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);
1435 _net_nfc_util_free_mem(info);