2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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.
17 #include <tapi_common.h>
20 #include "net_nfc_typedef_internal.h"
21 #include "net_nfc_debug_internal.h"
22 #include "net_nfc_util_internal.h"
23 #include "net_nfc_util_gdbus_internal.h"
24 #include "net_nfc_gdbus.h"
25 #include "net_nfc_server_common.h"
26 #include "net_nfc_server_context.h"
27 #include "net_nfc_server_manager.h"
28 #include "net_nfc_server_controller.h"
29 #include "net_nfc_server_util.h"
30 #include "net_nfc_server_se.h"
34 SE_UICC_UNAVAILABLE = -1,
35 SE_UICC_ON_PROGRESS = 0,
39 typedef struct _nfc_se_setting_t
46 net_nfc_server_se_setting_t;
49 static NetNfcGDbusSecureElement *se_skeleton = NULL;
51 static uint8_t gdbus_se_prev_type = SECURE_ELEMENT_TYPE_INVALID;
52 static uint8_t gdbus_se_prev_mode = SECURE_ELEMENT_OFF_MODE;
54 static net_nfc_server_se_setting_t gdbus_se_setting;
56 /* TODO : make a list for handles */
57 static TapiHandle *gdbus_uicc_handle;
58 static net_nfc_target_handle_s *gdbus_ese_handle;
60 static int gdbus_uicc_ready;
63 typedef struct _ServerSeData ServerSeData;
72 typedef struct _SeSetCardEmul SeSetCardEmul;
76 NetNfcGDbusSecureElement *object;
77 GDBusMethodInvocation *invocation;
81 typedef struct _SeDataSeType SeDataSeType;
85 NetNfcGDbusSecureElement *object;
86 GDBusMethodInvocation *invocation;
91 typedef struct _SeDataHandle SeDataHandle;
95 NetNfcGDbusSecureElement *object;
96 GDBusMethodInvocation *invocation;
97 net_nfc_target_handle_s *handle;
100 typedef struct _SeDataApdu SeDataApdu;
104 NetNfcGDbusSecureElement *object;
105 GDBusMethodInvocation *invocation;
106 net_nfc_target_handle_s *handle;
110 typedef struct _ChangeCardEmulMode ChangeCardEmulMode;
112 struct _ChangeCardEmulMode
114 NetNfcGDbusSecureElement *object;
115 GDBusMethodInvocation *invocation;
119 uint8_t net_nfc_server_se_get_se_type()
121 return gdbus_se_setting.type;
124 uint8_t net_nfc_server_se_get_return_se_mode()
126 return gdbus_se_setting.return_type;
129 uint8_t net_nfc_server_se_get_se_mode()
131 return gdbus_se_setting.mode;
134 static void net_nfc_server_se_set_se_type(uint8_t type)
136 gdbus_se_setting.return_type = gdbus_se_setting.type;
137 gdbus_se_setting.type = type;
140 static void net_nfc_server_se_set_se_mode(uint8_t mode)
142 gdbus_se_setting.mode = mode;
147 static bool net_nfc_server_se_is_ese_handle(net_nfc_target_handle_s *handle)
149 return (gdbus_ese_handle != NULL && gdbus_ese_handle == handle);
152 static void net_nfc_server_se_set_current_ese_handle(
153 net_nfc_target_handle_s *handle)
155 gdbus_ese_handle = handle;
158 static net_nfc_target_handle_s *net_nfc_server_se_open_ese()
162 if (NULL == gdbus_ese_handle)
164 net_nfc_error_e result = NET_NFC_OK;
165 net_nfc_target_handle_s *handle = NULL;
167 ret = net_nfc_controller_secure_element_open(SECURE_ELEMENT_TYPE_ESE,
171 net_nfc_server_se_set_current_ese_handle(handle);
173 NFC_DBG("handle [%p]", handle);
177 NFC_ERR("net_nfc_controller_secure_element_open failed [%d]", result);
181 return gdbus_ese_handle;
184 static net_nfc_error_e net_nfc_server_se_close_ese()
186 net_nfc_error_e result = NET_NFC_OK;
188 if (gdbus_ese_handle != NULL && net_nfc_server_gdbus_is_server_busy() == false)
190 if (net_nfc_controller_secure_element_close(gdbus_ese_handle, &result) == false)
191 net_nfc_controller_exception_handler();
193 net_nfc_server_se_set_current_ese_handle(NULL);
199 static void _se_uicc_enable_card_emulation()
201 net_nfc_error_e result;
204 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
205 SECURE_ELEMENT_OFF_MODE, &result);
208 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
209 SECURE_ELEMENT_VIRTUAL_MODE, &result);
211 if (NET_NFC_OK == result)
213 NFC_INFO("card emulation changed to SECURE_ELEMENT_TYPE_UICC");
215 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
216 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
218 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_TYPE_UICC) < 0)
219 NFC_ERR("vconf_set_int failed");
223 NFC_ERR("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
227 static void _se_uicc_prepare(void)
231 cpList = tel_get_cp_name_list();
234 gdbus_uicc_handle = tel_init(cpList[0]);
235 if (NULL == gdbus_uicc_handle)
236 NFC_ERR("tel_init() failed");
241 NFC_ERR("tel_get_cp_name_list() failed");
245 static void _se_uicc_status_noti_cb(TapiHandle *handle,
246 const char *noti_id, void *data, void *user_data)
248 TelSimCardStatus_t *status = data;
250 NFC_DBG("_se_uicc_status_noti_cb");
254 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED :
255 gdbus_uicc_ready = SE_UICC_READY;
259 if (gdbus_se_setting.busy == true &&
260 net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC)
262 gdbus_se_setting.busy = false;
264 _se_uicc_enable_card_emulation();
268 case TAPI_SIM_STATUS_CARD_REMOVED :
269 NFC_DBG("TAPI_SIM_STATUS_CARD_REMOVED");
270 gdbus_uicc_ready = SE_UICC_UNAVAILABLE;
272 if (net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC)
274 net_nfc_error_e result;
277 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
278 SECURE_ELEMENT_OFF_MODE, &result);
280 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
281 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
290 static void _se_uicc_init(void)
293 tel_register_noti_event(gdbus_uicc_handle, TAPI_NOTI_SIM_STATUS,
294 _se_uicc_status_noti_cb, NULL);
297 static void _se_uicc_deinit()
299 if (gdbus_uicc_handle != NULL)
301 tel_deregister_noti_event(gdbus_uicc_handle, TAPI_NOTI_SIM_STATUS);
303 tel_deinit(gdbus_uicc_handle);
305 gdbus_uicc_handle = NULL;
309 static net_nfc_target_handle_s* _se_uicc_open()
311 net_nfc_target_handle_s *result = NULL;
313 if (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL)
314 result = (net_nfc_target_handle_s *)gdbus_uicc_handle;
319 static bool _se_is_uicc_handle(net_nfc_target_handle_s *handle)
321 return (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL &&
322 (TapiHandle *)handle == gdbus_uicc_handle);
325 static void _se_uicc_close(net_nfc_target_handle_s *handle)
330 net_nfc_error_e net_nfc_server_se_disable_card_emulation()
332 net_nfc_error_e result;
334 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
335 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
338 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
339 SECURE_ELEMENT_OFF_MODE, &result);
342 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
343 SECURE_ELEMENT_OFF_MODE, &result);
348 net_nfc_error_e net_nfc_server_se_change_se(uint8_t type)
350 net_nfc_error_e result = NET_NFC_OK;
354 case SECURE_ELEMENT_TYPE_UICC :
355 if (false == gdbus_se_setting.busy)
357 if (SE_UICC_READY == gdbus_uicc_ready)
359 _se_uicc_enable_card_emulation();
361 else if (SE_UICC_ON_PROGRESS == gdbus_uicc_ready)
363 NFC_INFO("waiting for uicc initializing complete...");
365 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
366 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
368 gdbus_se_setting.busy = true;
372 result = NET_NFC_NOT_SUPPORTED;
377 NFC_DBG("Previous request is processing.");
379 result = NET_NFC_BUSY;
383 case SECURE_ELEMENT_TYPE_ESE :
385 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
386 SECURE_ELEMENT_OFF_MODE, &result);
389 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
390 SECURE_ELEMENT_VIRTUAL_MODE, &result);
392 if (NET_NFC_OK == result)
394 NFC_INFO("card emulation changed to SECURE_ELEMENT_TYPE_ESE");
396 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
397 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
399 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_TYPE_ESE) != 0)
400 NFC_ERR("vconf_set_int failed");
404 NFC_ERR("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
409 result = net_nfc_server_se_disable_card_emulation();
410 if (NET_NFC_OK == result)
412 NFC_INFO("card emulation turned off");
414 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE, VCONFKEY_NFC_SE_TYPE_NONE) != 0)
415 NFC_ERR("vconf_set_int failed");
423 static void se_close_secure_element_thread_func(gpointer user_data)
425 net_nfc_error_e result;
426 SeDataHandle *detail = user_data;
428 g_assert(detail != NULL);
429 g_assert(detail->object != NULL);
430 g_assert(detail->invocation != NULL);
432 if (_se_is_uicc_handle(detail->handle) == true)
434 _se_uicc_close(detail->handle);
438 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
440 /* decrease client reference count */
441 net_nfc_server_gdbus_decrease_se_count(
442 g_dbus_method_invocation_get_sender(detail->invocation));
444 result = net_nfc_server_se_close_ese();
448 result = NET_NFC_INVALID_HANDLE;
451 if ((gdbus_se_prev_type != net_nfc_server_se_get_se_type()) ||
452 (gdbus_se_prev_mode != net_nfc_server_se_get_se_mode()))
454 /*return back se mode*/
455 net_nfc_controller_set_secure_element_mode(gdbus_se_prev_type,
456 gdbus_se_prev_mode, &result);
458 net_nfc_server_se_set_se_type(gdbus_se_prev_type);
459 net_nfc_server_se_set_se_mode(gdbus_se_prev_mode);
462 net_nfc_gdbus_secure_element_complete_close_secure_element(
463 detail->object, detail->invocation, result);
465 g_object_unref(detail->invocation);
466 g_object_unref(detail->object);
470 /* shutdown process if it doesn't need */
471 if (net_nfc_server_manager_get_active() == false &&
472 net_nfc_server_gdbus_is_server_busy() == false)
474 net_nfc_server_controller_deinit();
478 static gboolean se_handle_close_secure_element(
479 NetNfcGDbusSecureElement *object,
480 GDBusMethodInvocation *invocation,
482 GVariant *smack_privilege)
488 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
490 /* check privilege and update client context */
491 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
492 "nfc-manager", "rw");
495 NFC_ERR("permission denied, and finished request");
500 data = g_try_new0(SeDataHandle, 1);
503 NFC_ERR("Memory allocation failed");
504 g_dbus_method_invocation_return_dbus_error(invocation,
505 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
510 data->object = g_object_ref(object);
511 data->invocation = g_object_ref(invocation);
512 data->handle = GUINT_TO_POINTER(arg_handle);
514 result = net_nfc_server_controller_async_queue_push(
515 se_close_secure_element_thread_func, data);
518 g_dbus_method_invocation_return_dbus_error(invocation,
519 "org.tizen.NetNfcService.Se.ThreadError",
520 "can not push to controller thread");
522 g_object_unref(data->object);
523 g_object_unref(data->invocation);
531 static void se_get_atr_thread_func(gpointer user_data)
535 SeDataHandle *detail = user_data;
536 net_nfc_error_e result = NET_NFC_OK;
538 g_assert(detail != NULL);
539 g_assert(detail->object != NULL);
540 g_assert(detail->invocation != NULL);
542 if (_se_is_uicc_handle(detail->handle) == true)
544 result = NET_NFC_NOT_SUPPORTED;
546 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
548 net_nfc_controller_secure_element_get_atr(detail->handle, &atr, &result);
552 NFC_ERR("invalid se handle");
554 result = NET_NFC_INVALID_HANDLE;
557 data = net_nfc_util_gdbus_data_to_variant(atr);
559 net_nfc_gdbus_secure_element_complete_get_atr(detail->object, detail->invocation,
564 net_nfc_util_free_data(atr);
568 g_object_unref(detail->invocation);
569 g_object_unref(detail->object);
574 static gboolean se_handle_get_atr(
575 NetNfcGDbusSecureElement *object,
576 GDBusMethodInvocation *invocation,
578 GVariant *smack_privilege)
584 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
586 /* check privilege and update client context */
587 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
591 NFC_ERR("permission denied, and finished request");
596 data = g_try_new0(SeDataHandle, 1);
599 NFC_ERR("Memory allocation failed");
600 g_dbus_method_invocation_return_dbus_error(invocation,
601 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
606 data->object = g_object_ref(object);
607 data->invocation = g_object_ref(invocation);
608 data->handle = GUINT_TO_POINTER(arg_handle);
610 result = net_nfc_server_controller_async_queue_push(se_get_atr_thread_func, data);
613 g_dbus_method_invocation_return_dbus_error(invocation,
614 "org.tizen.NetNfcService.Se.ThreadError",
615 "can not push to controller thread");
617 g_object_unref(data->object);
618 g_object_unref(data->invocation);
626 static void se_open_secure_element_thread_func(gpointer user_data)
628 SeDataSeType *detail = user_data;
629 net_nfc_error_e result = NET_NFC_OK;
630 net_nfc_target_handle_s *handle = NULL;
632 g_assert(detail != NULL);
633 g_assert(detail->object != NULL);
634 g_assert(detail->invocation != NULL);
636 #if 0 /* opening SE doesn't affect card emulation */
637 gdbus_se_prev_type = net_nfc_server_se_get_se_type();
638 gdbus_se_prev_mode = net_nfc_server_se_get_se_mode();
641 if (detail->se_type == SECURE_ELEMENT_TYPE_UICC)
643 #if 0 /* opening SE doesn't affect card emulation */
645 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_ESE,
646 SECURE_ELEMENT_OFF_MODE, &result);
648 /*Off UICC. UICC SHOULD not be detected by external reader when
649 being communicated in internal process*/
650 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
651 SECURE_ELEMENT_OFF_MODE, &result);
653 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
654 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
656 handle = _se_uicc_open();
663 result = NET_NFC_INVALID_STATE;
667 else if (detail->se_type == SECURE_ELEMENT_TYPE_ESE)
669 #if 0 /* opening SE doesn't affect card emulation */
671 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
672 SECURE_ELEMENT_OFF_MODE, &result);
674 handle = net_nfc_server_se_open_ese();
678 #if 0 /* opening SE doesn't affect card emulation */
679 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
680 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_WIRED_MODE);
682 net_nfc_server_se_set_current_ese_handle(handle);
684 NFC_DBG("handle [%p]", handle);
686 /* increase client reference count */
687 net_nfc_server_gdbus_increase_se_count(
688 g_dbus_method_invocation_get_sender(detail->invocation));
692 result = NET_NFC_INVALID_STATE;
698 result = NET_NFC_INVALID_PARAM;
702 net_nfc_gdbus_secure_element_complete_open_secure_element(detail->object,
703 detail->invocation, result, (guint)handle);
705 g_object_unref(detail->invocation);
706 g_object_unref(detail->object);
711 static gboolean se_handle_open_secure_element(
712 NetNfcGDbusSecureElement *object,
713 GDBusMethodInvocation *invocation,
715 GVariant *smack_privilege)
721 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
723 /* check privilege and update client context */
724 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
725 "nfc-manager", "rw");
728 NFC_ERR("permission denied, and finished request");
733 data = g_try_new0(SeDataSeType, 1);
736 NFC_ERR("Memory allocation failed");
737 g_dbus_method_invocation_return_dbus_error(invocation,
738 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
743 data->object = g_object_ref(object);
744 data->invocation = g_object_ref(invocation);
745 data->se_type= arg_type;
747 result = net_nfc_server_controller_async_queue_push(
748 se_open_secure_element_thread_func, data);
751 g_dbus_method_invocation_return_dbus_error(invocation,
752 "org.tizen.NetNfcService.Se.ThreadError",
753 "can not push to controller thread");
755 g_object_unref(data->object);
756 g_object_unref(data->invocation);
764 static void se_send_apdu_thread_func(gpointer user_data)
766 data_s *response = NULL;
767 GVariant *rspdata = NULL;
768 SeDataApdu *detail = user_data;
769 data_s apdu_data = { NULL, 0 };
770 net_nfc_error_e result = NET_NFC_OK;
772 g_assert(detail != NULL);
773 g_assert(detail->object != NULL);
774 g_assert(detail->invocation != NULL);
776 net_nfc_util_gdbus_variant_to_data_s(detail->data, &apdu_data);
778 if (_se_is_uicc_handle(detail->handle) == true)
780 result = NET_NFC_NOT_SUPPORTED;
782 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
784 net_nfc_controller_secure_element_send_apdu(detail->handle,
785 &apdu_data, &response, &result);
789 result = NET_NFC_INVALID_HANDLE;
792 rspdata = net_nfc_util_gdbus_data_to_variant(response);
794 net_nfc_gdbus_secure_element_complete_send_apdu(detail->object, detail->invocation,
797 if (response != NULL)
799 net_nfc_util_free_data(response);
803 net_nfc_util_free_data(&apdu_data);
805 g_variant_unref(detail->data);
807 g_object_unref(detail->invocation);
808 g_object_unref(detail->object);
813 static gboolean se_handle_send_apdu(
814 NetNfcGDbusSecureElement *object,
815 GDBusMethodInvocation *invocation,
818 GVariant *smack_privilege)
824 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
826 /* check privilege and update client context */
827 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
828 "nfc-manager", "rw");
831 NFC_ERR("permission denied, and finished request");
836 data = g_try_new0(SeDataApdu, 1);
839 NFC_ERR("Memory allocation failed");
840 g_dbus_method_invocation_return_dbus_error(invocation,
841 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
846 data->object = g_object_ref(object);
847 data->invocation = g_object_ref(invocation);
848 data->handle = GUINT_TO_POINTER(arg_handle);
849 data->data = g_variant_ref(apdudata);
851 result = net_nfc_server_controller_async_queue_push(se_send_apdu_thread_func, data);
854 g_dbus_method_invocation_return_dbus_error(invocation,
855 "org.tizen.NetNfcService.Se.ThreadError",
856 "can not push to controller thread");
858 g_variant_unref(data->data);
860 g_object_unref(data->object);
861 g_object_unref(data->invocation);
869 static void _se_set_card_emulation_thread_func(gpointer user_data)
871 SeSetCardEmul *data = user_data;
872 net_nfc_error_e result = NET_NFC_OK;
874 g_assert(data != NULL);
875 g_assert(data->object != NULL);
876 g_assert(data->invocation != NULL);
878 if (data->mode == NET_NFC_CARD_EMELATION_ENABLE)
880 net_nfc_controller_set_secure_element_mode(net_nfc_server_se_get_se_mode(),
881 SECURE_ELEMENT_VIRTUAL_MODE, &result);
883 if (NET_NFC_OK == result)
885 NFC_DBG("changed to CARD EMULATION ON");
887 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
891 NFC_ERR("CARD EMULATION ON fail [%d]", result);
894 else if (data->mode == NET_NFC_CARD_EMULATION_DISABLE)
896 net_nfc_controller_set_secure_element_mode(net_nfc_server_se_get_se_mode(),
897 SECURE_ELEMENT_OFF_MODE, &result);
898 if (NET_NFC_OK == result)
900 NFC_DBG("changed to CARD EMULATION OFF");
902 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
906 NFC_ERR("CARD EMULATION OFF fail [%d]", result);
911 result = NET_NFC_INVALID_PARAM;
914 net_nfc_gdbus_secure_element_complete_set_card_emulation(data->object,
915 data->invocation, result);
917 g_object_unref(data->invocation);
918 g_object_unref(data->object);
923 static gboolean _se_handle_set_card_emulation(
924 NetNfcGDbusSecureElement *object,
925 GDBusMethodInvocation *invocation,
927 GVariant *smack_privilege)
933 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
935 /* check privilege and update client context */
936 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
940 NFC_ERR("permission denied, and finished request");
945 data = g_try_new0(SeSetCardEmul, 1);
948 NFC_ERR("Memory allocation failed");
949 g_dbus_method_invocation_return_dbus_error(invocation,
950 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
955 data->object = g_object_ref(object);
956 data->invocation = g_object_ref(invocation);
957 data->mode = arg_mode;
959 result = net_nfc_server_controller_async_queue_push(
960 _se_set_card_emulation_thread_func, data);
963 g_dbus_method_invocation_return_dbus_error(invocation,
964 "org.tizen.NetNfcService.Se.ThreadError",
965 "can not push to controller thread");
967 g_object_unref(data->object);
968 g_object_unref(data->invocation);
976 static void se_set_data_thread_func(gpointer user_data)
978 SeDataSeType *data = user_data;
979 gboolean isTypeChanged = FALSE;
980 net_nfc_error_e result = NET_NFC_OK;
982 g_assert(data != NULL);
983 g_assert(data->object != NULL);
984 g_assert(data->invocation != NULL);
986 if (data->se_type != net_nfc_server_se_get_se_type())
988 result = net_nfc_server_se_change_se(data->se_type);
989 isTypeChanged = TRUE;
992 net_nfc_gdbus_secure_element_complete_set(data->object, data->invocation, result);
995 net_nfc_gdbus_secure_element_emit_se_type_changed(data->object, data->se_type);
997 g_object_unref(data->invocation);
998 g_object_unref(data->object);
1003 static gboolean se_handle_set(NetNfcGDbusSecureElement *object,
1004 GDBusMethodInvocation *invocation, gint arg_type, GVariant *smack_privilege)
1010 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1012 /* check privilege and update client context */
1013 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1014 "nfc-manager", "w");
1017 NFC_ERR("permission denied, and finished request");
1022 data = g_try_new0(SeDataSeType, 1);
1025 NFC_ERR("Memory allocation failed");
1026 g_dbus_method_invocation_return_dbus_error(invocation,
1027 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
1032 data->object = g_object_ref(object);
1033 data->invocation = g_object_ref(invocation);
1034 data->se_type = arg_type;
1036 result = net_nfc_server_controller_async_queue_push(se_set_data_thread_func, data);
1038 if (FALSE == result)
1040 g_dbus_method_invocation_return_dbus_error(invocation,
1041 "org.tizen.NetNfcService.Se.ThreadError",
1042 "can not push to controller thread");
1044 g_object_unref(data->object);
1045 g_object_unref(data->invocation);
1053 static void se_get_data_thread_func(gpointer user_data)
1055 SeDataSeType *data = user_data;
1056 net_nfc_error_e result = NET_NFC_OK;
1058 g_assert(data != NULL);
1059 g_assert(data->object != NULL);
1060 g_assert(data->invocation != NULL);
1062 net_nfc_gdbus_secure_element_complete_get(data->object, data->invocation,
1063 net_nfc_server_se_get_se_type(), result);
1065 g_object_unref(data->invocation);
1066 g_object_unref(data->object);
1071 static gboolean se_handle_get(NetNfcGDbusSecureElement *object,
1072 GDBusMethodInvocation *invocation, GVariant *smack_privilege)
1078 NFC_INFO(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1080 /* check privilege and update client context */
1081 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1082 "nfc-manager", "r");
1085 NFC_ERR("permission denied, and finished request");
1090 data = g_try_new0(SeDataSeType, 1);
1093 NFC_ERR("Memory allocation failed");
1094 g_dbus_method_invocation_return_dbus_error(invocation,
1095 "org.tizen.NetNfcService.AllocationError", "Can not allocate memory");
1100 data->object = g_object_ref(object);
1101 data->invocation = g_object_ref(invocation);
1103 result = net_nfc_server_controller_async_queue_push(se_get_data_thread_func, data);
1104 if (FALSE == result)
1106 g_dbus_method_invocation_return_dbus_error(invocation,
1107 "org.tizen.NetNfcService.Se.ThreadError",
1108 "can not push to controller thread");
1110 g_object_unref(data->object);
1111 g_object_unref(data->invocation);
1119 static void _se_change_card_emulation_mode_thread_func(gpointer user_data)
1121 bool isChanged = false;
1122 net_nfc_error_e result = NET_NFC_OK;
1123 uint8_t current_mode, return_mode;
1124 ChangeCardEmulMode *data = (ChangeCardEmulMode *)user_data;
1126 g_assert(data != NULL);
1127 g_assert(data->object != NULL);
1128 g_assert(data->invocation != NULL);
1130 current_mode = net_nfc_server_se_get_se_mode();
1132 if(data->mode == SECURE_ELEMENT_ACTIVE_STATE
1133 && current_mode == SECURE_ELEMENT_TYPE_INVALID)
1135 return_mode = net_nfc_server_se_get_return_se_mode();
1136 result = net_nfc_server_se_change_se(return_mode);
1139 else if(data->mode == SECURE_ELEMENT_INACTIVE_STATE
1140 && current_mode != SECURE_ELEMENT_TYPE_INVALID)
1142 result = net_nfc_server_se_disable_card_emulation();
1146 net_nfc_gdbus_secure_element_complete_change_card_emulation_mode(data->object,
1147 data->invocation, result);
1151 net_nfc_gdbus_secure_element_emit_card_emulation_mode_changed(data->object,
1155 g_object_unref(data->invocation);
1156 g_object_unref(data->object);
1162 static gboolean se_handle_change_card_emulation_mode(
1163 NetNfcGDbusSecureElement *object,
1164 GDBusMethodInvocation *invocation,
1166 GVariant *smack_privilege)
1170 SeSetCardEmul *data;
1172 NFC_DBG(">>> REQUEST from [%s]", g_dbus_method_invocation_get_sender(invocation));
1174 /* check privilege and update client context */
1175 ret = net_nfc_server_gdbus_check_privilege(invocation, smack_privilege,
1176 "nfc-manager", "w");
1179 NFC_ERR("permission denied, and finished request");
1184 data = g_try_new0(ChangeCardEmulMode, 1);
1187 NFC_ERR("Memory allocation failed");
1188 g_dbus_method_invocation_return_dbus_error(invocation,
1189 "org.tizen.NetNfcService.AllocationError",
1190 "Can not allocate memory");
1195 data->object = g_object_ref(object);
1196 data->invocation = g_object_ref(invocation);
1197 data->mode = arg_mode;
1199 result = net_nfc_server_controller_async_queue_push(
1200 _se_change_card_emulation_mode_thread_func, data);
1201 if (FALSE == result)
1203 g_dbus_method_invocation_return_dbus_error(invocation,
1204 "org.tizen.NetNfcService.Se.ThreadError",
1205 "can not push to controller thread");
1207 g_object_unref(data->object);
1208 g_object_unref(data->invocation);
1216 gboolean net_nfc_server_se_init(GDBusConnection *connection)
1219 GError *error = NULL;
1222 g_object_unref(se_skeleton);
1224 /* initialize UICC */
1227 se_skeleton = net_nfc_gdbus_secure_element_skeleton_new();
1229 g_signal_connect(se_skeleton, "handle-set", G_CALLBACK(se_handle_set), NULL);
1231 g_signal_connect(se_skeleton, "handle-get", G_CALLBACK(se_handle_get), NULL);
1233 g_signal_connect(se_skeleton, "handle-set-card-emulation",
1234 G_CALLBACK(_se_handle_set_card_emulation), NULL);
1236 g_signal_connect(se_skeleton, "handle-open-secure-element",
1237 G_CALLBACK(se_handle_open_secure_element), NULL);
1239 g_signal_connect(se_skeleton, "handle-close-secure-element",
1240 G_CALLBACK(se_handle_close_secure_element), NULL);
1242 g_signal_connect(se_skeleton, "handle-get-atr", G_CALLBACK(se_handle_get_atr), NULL);
1244 g_signal_connect(se_skeleton, "handle-send-apdu",
1245 G_CALLBACK(se_handle_send_apdu), NULL);
1247 g_signal_connect(se_skeleton, "handle-change-card-emulation-mode",
1248 G_CALLBACK(se_handle_change_card_emulation_mode), NULL);
1250 result = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(se_skeleton),
1251 connection, "/org/tizen/NetNfcService/SecureElement", &error);
1252 if (FALSE == result)
1254 NFC_ERR("can not skeleton_export %s", error->message);
1256 g_error_free(error);
1258 net_nfc_server_se_deinit();
1264 void net_nfc_server_se_deinit(void)
1268 g_object_unref(se_skeleton);
1271 /* de-initialize UICC */
1276 static void se_detected_thread_func(gpointer user_data)
1278 uint32_t devType = 0;
1279 GVariant *data = NULL;
1280 net_nfc_error_e result = NET_NFC_OK;
1281 net_nfc_target_handle_s *handle = NULL;
1283 g_assert(user_data != NULL);
1285 if (NULL == se_skeleton)
1287 NFC_ERR("se skeleton is not initialized");
1289 g_variant_unref((GVariant *)user_data);
1294 g_variant_get((GVariant *)user_data, "uu@a(y)", (guint *)&handle, &devType, &data);
1296 net_nfc_server_se_set_current_ese_handle(handle);
1298 NFC_DBG("trying to connect to ESE = [0x%p]", handle);
1300 if (!net_nfc_controller_connect(handle, &result))
1301 NFC_DBG("connect failed = [%d]", result);
1303 net_nfc_gdbus_secure_element_emit_ese_detected(se_skeleton, GPOINTER_TO_UINT(handle),
1306 g_variant_unref((GVariant *)user_data);
1309 static void se_transcation_thread_func(gpointer user_data)
1312 pid_t focus_app_pid;
1313 ServerSeData *detail = user_data;
1316 g_assert(user_data != NULL);
1318 if (detail->event == NET_NFC_MESSAGE_SE_START_TRANSACTION)
1320 GVariant *aid = NULL;
1321 GVariant *param = NULL;
1324 NFC_DBG("launch se app");
1326 aid = net_nfc_util_gdbus_data_to_variant(&(detail->aid));
1327 param = net_nfc_util_gdbus_data_to_variant(&(detail->param));
1328 se_type = net_nfc_server_se_get_se_type();
1329 focus_app_pid = net_nfc_app_util_get_focus_app_pid();
1330 fg_dispatch = net_nfc_app_util_check_launch_state();
1332 /* TODO : check access control */
1333 net_nfc_gdbus_secure_element_emit_transaction_event (
1341 net_nfc_app_util_launch_se_transaction_app(
1345 detail->param.buffer,
1346 detail->param.length);
1348 NFC_DBG("launch se app end");
1351 net_nfc_util_free_data(&detail->param);
1352 net_nfc_util_free_data(&detail->aid);
1357 void net_nfc_server_se_detected(void *info)
1361 GVariant *parameter;
1362 net_nfc_request_target_detected_t *se_target =
1363 (net_nfc_request_target_detected_t *)info;
1365 data = net_nfc_util_gdbus_buffer_to_variant(se_target->target_info_values.buffer,
1366 se_target->target_info_values.length);
1368 parameter = g_variant_new("uu@a(y)", GPOINTER_TO_UINT(se_target->handle),
1369 se_target->devType, data);
1370 if (parameter != NULL)
1372 ret = net_nfc_server_controller_async_queue_push(se_detected_thread_func,
1376 NFC_ERR("can not push to controller thread");
1378 g_variant_unref(parameter);
1383 NFC_ERR("g_variant_new failed");
1386 /* FIXME : should be removed when plugins would be fixed*/
1387 _net_nfc_util_free_mem(info);
1390 void net_nfc_server_se_transaction_received(void *info)
1393 ServerSeData *detail;
1394 net_nfc_request_se_event_t *se_event = (net_nfc_request_se_event_t *)info;
1396 detail = g_try_new0(ServerSeData, 1);
1399 detail->event = se_event->request_type;
1401 if (se_event->aid.buffer != NULL && se_event->aid.length > 0)
1403 if (net_nfc_util_alloc_data(&detail->aid, se_event->aid.length) == true)
1404 memcpy(detail->aid.buffer, se_event->aid.buffer, se_event->aid.length);
1407 if (se_event->param.buffer != NULL && se_event->param.length > 0)
1409 if (net_nfc_util_alloc_data(&detail->param, se_event->param.length) == true)
1410 memcpy(detail->param.buffer, se_event->param.buffer, se_event->param.length);
1413 ret = net_nfc_server_controller_async_queue_push(
1414 se_transcation_thread_func, detail);
1417 NFC_ERR("can not push to controller thread");
1419 net_nfc_util_free_data(&detail->param);
1420 net_nfc_util_free_data(&detail->aid);
1427 NFC_ERR("g_new0 failed");
1430 /* FIXME : should be removed when plugins would be fixed*/
1431 net_nfc_util_free_data(&se_event->param);
1432 net_nfc_util_free_data(&se_event->aid);
1434 _net_nfc_util_free_mem(info);