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
45 net_nfc_server_se_setting_t;
48 static NetNfcGDbusSecureElement *se_skeleton = NULL;
50 static uint8_t gdbus_se_prev_type = SECURE_ELEMENT_TYPE_INVALID;
51 static uint8_t gdbus_se_prev_mode = SECURE_ELEMENT_OFF_MODE;
53 static net_nfc_server_se_setting_t gdbus_se_setting;
55 /* TODO : make a list for handles */
56 static TapiHandle *gdbus_uicc_handle;
57 static net_nfc_target_handle_s *gdbus_ese_handle;
59 static int gdbus_uicc_ready;
62 typedef struct _ServerSeData ServerSeData;
71 typedef struct _SeSetCardEmul SeSetCardEmul;
75 NetNfcGDbusSecureElement *object;
76 GDBusMethodInvocation *invocation;
80 typedef struct _SeDataSeType SeDataSeType;
84 NetNfcGDbusSecureElement *object;
85 GDBusMethodInvocation *invocation;
90 typedef struct _SeDataHandle SeDataHandle;
94 NetNfcGDbusSecureElement *object;
95 GDBusMethodInvocation *invocation;
96 net_nfc_target_handle_s *handle;
99 typedef struct _SeDataApdu SeDataApdu;
103 NetNfcGDbusSecureElement *object;
104 GDBusMethodInvocation *invocation;
105 net_nfc_target_handle_s *handle;
109 static void se_close_secure_element_thread_func(gpointer user_data);
111 static void se_get_atr_thread_func(gpointer user_data);
113 static void se_open_secure_element_thread_func(gpointer user_data);
115 static void se_send_apdu_thread_func(gpointer user_data);
117 static void se_set_data_thread_func(gpointer user_data);
119 static gboolean se_handle_close_secure_element(
120 NetNfcGDbusSecureElement *object,
121 GDBusMethodInvocation *invocation,
123 GVariant *smack_privilege);
125 static gboolean se_handle_get_atr(
126 NetNfcGDbusSecureElement *object,
127 GDBusMethodInvocation *invocation,
129 GVariant *smack_privilege);
132 static gboolean se_handle_open_secure_element(
133 NetNfcGDbusSecureElement *object,
134 GDBusMethodInvocation *invocation,
136 GVariant *smack_privilege);
139 static gboolean se_handle_send_apdu(
140 NetNfcGDbusSecureElement *object,
141 GDBusMethodInvocation *invocation,
144 GVariant *smack_privilege);
146 static gboolean se_handle_set(
147 NetNfcGDbusSecureElement *object,
148 GDBusMethodInvocation *invocation,
150 GVariant *smack_privilege);
153 uint8_t net_nfc_server_se_get_se_type()
155 return gdbus_se_setting.type;
158 uint8_t net_nfc_server_se_get_se_mode()
160 return gdbus_se_setting.mode;
163 static void net_nfc_server_se_set_se_type(uint8_t type)
165 gdbus_se_setting.type = type;
168 static void net_nfc_server_se_set_se_mode(uint8_t mode)
170 gdbus_se_setting.mode = mode;
175 static bool net_nfc_server_se_is_ese_handle(net_nfc_target_handle_s *handle)
177 return (gdbus_ese_handle != NULL && gdbus_ese_handle == handle);
180 static void net_nfc_server_se_set_current_ese_handle(
181 net_nfc_target_handle_s *handle)
183 gdbus_ese_handle = handle;
186 static net_nfc_target_handle_s *net_nfc_server_se_open_ese()
188 if (gdbus_ese_handle == NULL) {
189 net_nfc_error_e result = NET_NFC_OK;
190 net_nfc_target_handle_s *handle = NULL;
192 if (net_nfc_controller_secure_element_open(
193 SECURE_ELEMENT_TYPE_ESE,
194 &handle, &result) == true)
196 net_nfc_server_se_set_current_ese_handle(handle);
198 DEBUG_SERVER_MSG("handle [%p]", handle);
202 DEBUG_ERR_MSG("net_nfc_controller_secure_element_open failed [%d]",
207 return gdbus_ese_handle;
210 static net_nfc_error_e net_nfc_server_se_close_ese()
212 net_nfc_error_e result = NET_NFC_OK;
214 if (gdbus_ese_handle != NULL &&
215 net_nfc_server_gdbus_is_server_busy() == false) {
216 if (net_nfc_controller_secure_element_close(
220 net_nfc_controller_exception_handler();
222 net_nfc_server_se_set_current_ese_handle(NULL);
228 static void _se_uicc_enable_card_emulation()
230 net_nfc_error_e result;
233 net_nfc_controller_set_secure_element_mode(
234 SECURE_ELEMENT_TYPE_ESE,
235 SECURE_ELEMENT_OFF_MODE,
239 net_nfc_controller_set_secure_element_mode(
240 SECURE_ELEMENT_TYPE_UICC,
241 SECURE_ELEMENT_VIRTUAL_MODE,
243 if (result == NET_NFC_OK) {
244 INFO_MSG("card emulation changed to SECURE_ELEMENT_TYPE_UICC");
246 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
247 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
249 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE,
250 VCONFKEY_NFC_SE_TYPE_UICC) < 0) {
251 DEBUG_ERR_MSG("vconf_set_int failed");
254 DEBUG_ERR_MSG("net_nfc_controller_set_secure_element_mode failed, [%d]",
259 static void _se_uicc_prepare(void)
263 cpList = tel_get_cp_name_list();
264 if (cpList != NULL) {
265 gdbus_uicc_handle = tel_init(cpList[0]);
266 if (gdbus_uicc_handle != NULL) {
268 DEBUG_ERR_MSG("tel_init() failed");
271 DEBUG_ERR_MSG("tel_get_cp_name_list() failed");
275 static void _se_uicc_status_noti_cb(TapiHandle *handle,
280 TelSimCardStatus_t *status = (TelSimCardStatus_t *)data;
282 DEBUG_SERVER_MSG("_se_uicc_status_noti_cb");
285 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED :
286 gdbus_uicc_ready = SE_UICC_READY;
290 if (gdbus_se_setting.busy == true &&
291 net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC) {
293 gdbus_se_setting.busy = false;
295 _se_uicc_enable_card_emulation();
299 case TAPI_SIM_STATUS_CARD_REMOVED :
300 DEBUG_SERVER_MSG("TAPI_SIM_STATUS_CARD_REMOVED");
301 gdbus_uicc_ready = SE_UICC_UNAVAILABLE;
303 if (net_nfc_server_se_get_se_type() == SECURE_ELEMENT_TYPE_UICC) {
304 net_nfc_error_e result;
307 net_nfc_controller_set_secure_element_mode(
308 SECURE_ELEMENT_TYPE_UICC,
309 SECURE_ELEMENT_OFF_MODE,
312 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
313 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
322 static void _se_uicc_init(void)
325 tel_register_noti_event(gdbus_uicc_handle,
326 TAPI_NOTI_SIM_STATUS,
327 _se_uicc_status_noti_cb,
331 static void _se_uicc_deinit()
333 if (gdbus_uicc_handle != NULL) {
334 tel_deregister_noti_event(gdbus_uicc_handle,
335 TAPI_NOTI_SIM_STATUS);
337 tel_deinit(gdbus_uicc_handle);
339 gdbus_uicc_handle = NULL;
343 static net_nfc_target_handle_s* _se_uicc_open()
345 net_nfc_target_handle_s *result = NULL;
347 if (gdbus_uicc_ready == SE_UICC_READY && gdbus_uicc_handle != NULL) {
348 result = (net_nfc_target_handle_s *)gdbus_uicc_handle;
354 static bool _se_is_uicc_handle(net_nfc_target_handle_s *handle)
356 return (gdbus_uicc_ready == SE_UICC_READY &&
357 gdbus_uicc_handle != NULL &&
358 (TapiHandle *)handle == gdbus_uicc_handle);
361 static void _se_uicc_close(net_nfc_target_handle_s *handle)
366 net_nfc_error_e net_nfc_server_se_disable_card_emulation()
368 net_nfc_error_e result;
370 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_INVALID);
371 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
374 net_nfc_controller_set_secure_element_mode(
375 SECURE_ELEMENT_TYPE_ESE,
376 SECURE_ELEMENT_OFF_MODE,
380 net_nfc_controller_set_secure_element_mode(
381 SECURE_ELEMENT_TYPE_UICC,
382 SECURE_ELEMENT_OFF_MODE,
388 net_nfc_error_e net_nfc_server_se_change_se(uint8_t type)
390 net_nfc_error_e result = NET_NFC_OK;
393 case SECURE_ELEMENT_TYPE_UICC :
394 if (gdbus_se_setting.busy == false) {
395 if (gdbus_uicc_ready == SE_UICC_READY) {
396 _se_uicc_enable_card_emulation();
397 } else if (gdbus_uicc_ready == SE_UICC_ON_PROGRESS) {
398 INFO_MSG("waiting for uicc initializing complete...");
400 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
401 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
403 gdbus_se_setting.busy = true;
405 result = NET_NFC_NOT_SUPPORTED;
408 DEBUG_SERVER_MSG("Previous request is processing.");
410 result = NET_NFC_BUSY;
414 case SECURE_ELEMENT_TYPE_ESE :
416 net_nfc_controller_set_secure_element_mode(
417 SECURE_ELEMENT_TYPE_UICC,
418 SECURE_ELEMENT_OFF_MODE,
422 net_nfc_controller_set_secure_element_mode(
423 SECURE_ELEMENT_TYPE_ESE,
424 SECURE_ELEMENT_VIRTUAL_MODE,
427 if (result == NET_NFC_OK) {
428 INFO_MSG("card emulation changed to SECURE_ELEMENT_TYPE_ESE");
430 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
431 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_VIRTUAL_MODE);
433 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE,
434 VCONFKEY_NFC_SE_TYPE_ESE) != 0) {
435 DEBUG_ERR_MSG("vconf_set_int failed");
438 DEBUG_ERR_MSG("net_nfc_controller_set_secure_element_mode failed, [%d]", result);
443 result = net_nfc_server_se_disable_card_emulation();
444 if (result == NET_NFC_OK){
445 INFO_MSG("card emulation turned off");
447 if (vconf_set_int(VCONFKEY_NFC_SE_TYPE,
448 VCONFKEY_NFC_SE_TYPE_NONE) != 0) {
449 DEBUG_ERR_MSG("vconf_set_int failed");
458 static void se_close_secure_element_thread_func(gpointer user_data)
460 SeDataHandle *detail = (SeDataHandle *)user_data;
461 net_nfc_error_e result;
463 g_assert(detail != NULL);
464 g_assert(detail->object != NULL);
465 g_assert(detail->invocation != NULL);
467 if (_se_is_uicc_handle(detail->handle) == true)
469 _se_uicc_close(detail->handle);
473 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
475 /* decrease client reference count */
476 net_nfc_server_gdbus_decrease_se_count(
477 g_dbus_method_invocation_get_sender(
478 detail->invocation));
480 result = net_nfc_server_se_close_ese();
484 result = NET_NFC_INVALID_HANDLE;
487 if ((gdbus_se_prev_type != net_nfc_server_se_get_se_type()) ||
488 (gdbus_se_prev_mode != net_nfc_server_se_get_se_mode()))
490 /*return back se mode*/
491 net_nfc_controller_set_secure_element_mode(gdbus_se_prev_type,
492 gdbus_se_prev_mode, &result);
494 net_nfc_server_se_set_se_type(gdbus_se_prev_type);
495 net_nfc_server_se_set_se_mode(gdbus_se_prev_mode);
498 net_nfc_gdbus_secure_element_complete_close_secure_element(
499 detail->object, detail->invocation, result);
501 g_object_unref(detail->invocation);
502 g_object_unref(detail->object);
506 /* shutdown process if it doesn't need */
507 if (net_nfc_server_manager_get_active() == false &&
508 net_nfc_server_gdbus_is_server_busy() == false) {
509 net_nfc_server_controller_deinit();
513 static gboolean se_handle_close_secure_element(
514 NetNfcGDbusSecureElement *object,
515 GDBusMethodInvocation *invocation,
517 GVariant *smack_privilege)
522 INFO_MSG(">>> REQUEST from [%s]",
523 g_dbus_method_invocation_get_sender(invocation));
525 /* check privilege and update client context */
526 if (net_nfc_server_gdbus_check_privilege(invocation,
530 DEBUG_ERR_MSG("permission denied, and finished request");
535 data = g_try_new0(SeDataHandle, 1);
538 DEBUG_ERR_MSG("Memory allocation failed");
539 g_dbus_method_invocation_return_dbus_error(invocation,
540 "org.tizen.NetNfcService.AllocationError",
541 "Can not allocate memory");
546 data->object = g_object_ref(object);
547 data->invocation = g_object_ref(invocation);
548 data->handle = GUINT_TO_POINTER(arg_handle);
550 result = net_nfc_server_controller_async_queue_push(
551 se_close_secure_element_thread_func, data);
554 g_dbus_method_invocation_return_dbus_error(invocation,
555 "org.tizen.NetNfcService.Se.ThreadError",
556 "can not push to controller thread");
558 g_object_unref(data->object);
559 g_object_unref(data->invocation);
567 static void se_get_atr_thread_func(gpointer user_data)
569 SeDataHandle *detail = (SeDataHandle *)user_data;
570 net_nfc_error_e result = NET_NFC_OK;
574 g_assert(detail != NULL);
575 g_assert(detail->object != NULL);
576 g_assert(detail->invocation != NULL);
578 if (_se_is_uicc_handle(detail->handle) == true)
580 result = NET_NFC_NOT_SUPPORTED;
582 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
584 net_nfc_controller_secure_element_get_atr(detail->handle, &atr,
589 DEBUG_ERR_MSG("invalid se handle");
591 result = NET_NFC_INVALID_HANDLE;
594 data = net_nfc_util_gdbus_data_to_variant(atr);
596 net_nfc_gdbus_secure_element_complete_get_atr(
603 net_nfc_util_free_data(atr);
607 g_object_unref(detail->invocation);
608 g_object_unref(detail->object);
613 static gboolean se_handle_get_atr(
614 NetNfcGDbusSecureElement *object,
615 GDBusMethodInvocation *invocation,
617 GVariant *smack_privilege)
622 INFO_MSG(">>> REQUEST from [%s]",
623 g_dbus_method_invocation_get_sender(invocation));
625 /* check privilege and update client context */
626 if (net_nfc_server_gdbus_check_privilege(invocation,
630 DEBUG_ERR_MSG("permission denied, and finished request");
635 data = g_try_new0(SeDataHandle, 1);
638 DEBUG_ERR_MSG("Memory allocation failed");
639 g_dbus_method_invocation_return_dbus_error(invocation,
640 "org.tizen.NetNfcService.AllocationError",
641 "Can not allocate memory");
646 data->object = g_object_ref(object);
647 data->invocation = g_object_ref(invocation);
648 data->handle = GUINT_TO_POINTER(arg_handle);
650 result = net_nfc_server_controller_async_queue_push(
651 se_get_atr_thread_func, data);
654 g_dbus_method_invocation_return_dbus_error(invocation,
655 "org.tizen.NetNfcService.Se.ThreadError",
656 "can not push to controller thread");
658 g_object_unref(data->object);
659 g_object_unref(data->invocation);
667 static void se_open_secure_element_thread_func(gpointer user_data)
669 SeDataSeType *detail = (SeDataSeType *)user_data;
670 net_nfc_target_handle_s *handle = NULL;
671 net_nfc_error_e result = NET_NFC_OK;
673 g_assert(detail != NULL);
674 g_assert(detail->object != NULL);
675 g_assert(detail->invocation != NULL);
677 #if 0 /* opening SE doesn't affect card emulation */
678 gdbus_se_prev_type = net_nfc_server_se_get_se_type();
679 gdbus_se_prev_mode = net_nfc_server_se_get_se_mode();
682 if (detail->se_type == SECURE_ELEMENT_TYPE_UICC)
684 #if 0 /* opening SE doesn't affect card emulation */
686 net_nfc_controller_set_secure_element_mode(
687 SECURE_ELEMENT_TYPE_ESE,
688 SECURE_ELEMENT_OFF_MODE, &result);
690 /*Off UICC. UICC SHOULD not be detected by external reader when
691 being communicated in internal process*/
692 net_nfc_controller_set_secure_element_mode(SECURE_ELEMENT_TYPE_UICC,
693 SECURE_ELEMENT_OFF_MODE, &result);
695 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_UICC);
696 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
698 handle = _se_uicc_open();
705 result = NET_NFC_INVALID_STATE;
709 else if (detail->se_type == SECURE_ELEMENT_TYPE_ESE)
711 #if 0 /* opening SE doesn't affect card emulation */
713 net_nfc_controller_set_secure_element_mode(
714 SECURE_ELEMENT_TYPE_UICC,
715 SECURE_ELEMENT_OFF_MODE, &result);
717 handle = net_nfc_server_se_open_ese();
721 #if 0 /* opening SE doesn't affect card emulation */
722 net_nfc_server_se_set_se_type(SECURE_ELEMENT_TYPE_ESE);
723 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_WIRED_MODE);
725 net_nfc_server_se_set_current_ese_handle(handle);
727 DEBUG_SERVER_MSG("handle [%p]", handle);
729 /* increase client reference count */
730 net_nfc_server_gdbus_increase_se_count(
731 g_dbus_method_invocation_get_sender(
732 detail->invocation));
736 result = NET_NFC_INVALID_STATE;
742 result = NET_NFC_INVALID_PARAM;
746 net_nfc_gdbus_secure_element_complete_open_secure_element(
752 g_object_unref(detail->invocation);
753 g_object_unref(detail->object);
758 static gboolean se_handle_open_secure_element(
759 NetNfcGDbusSecureElement *object,
760 GDBusMethodInvocation *invocation,
762 GVariant *smack_privilege)
767 INFO_MSG(">>> REQUEST from [%s]",
768 g_dbus_method_invocation_get_sender(invocation));
770 /* check privilege and update client context */
771 if (net_nfc_server_gdbus_check_privilege(invocation,
775 DEBUG_ERR_MSG("permission denied, and finished request");
780 data = g_try_new0(SeDataSeType, 1);
783 DEBUG_ERR_MSG("Memory allocation failed");
784 g_dbus_method_invocation_return_dbus_error(invocation,
785 "org.tizen.NetNfcService.AllocationError",
786 "Can not allocate memory");
791 data->object = g_object_ref(object);
792 data->invocation = g_object_ref(invocation);
793 data->se_type= arg_type;
795 result = net_nfc_server_controller_async_queue_push(
796 se_open_secure_element_thread_func, data);
799 g_dbus_method_invocation_return_dbus_error(invocation,
800 "org.tizen.NetNfcService.Se.ThreadError",
801 "can not push to controller thread");
803 g_object_unref(data->object);
804 g_object_unref(data->invocation);
812 static void se_send_apdu_thread_func(gpointer user_data)
814 SeDataApdu *detail = (SeDataApdu *)user_data;
815 data_s apdu_data = { NULL, 0 };
816 data_s *response = NULL;
817 net_nfc_error_e result = NET_NFC_OK;
818 GVariant *rspdata = NULL;
820 g_assert(detail != NULL);
821 g_assert(detail->object != NULL);
822 g_assert(detail->invocation != NULL);
824 net_nfc_util_gdbus_variant_to_data_s(detail->data, &apdu_data);
826 if (_se_is_uicc_handle(detail->handle) == true)
828 result = NET_NFC_NOT_SUPPORTED;
830 else if (net_nfc_server_se_is_ese_handle(detail->handle) == true)
832 net_nfc_controller_secure_element_send_apdu(detail->handle,
833 &apdu_data, &response, &result);
837 result = NET_NFC_INVALID_HANDLE;
840 rspdata = net_nfc_util_gdbus_data_to_variant(response);
842 net_nfc_gdbus_secure_element_complete_send_apdu(
848 if (response != NULL)
850 net_nfc_util_free_data(response);
854 net_nfc_util_free_data(&apdu_data);
856 g_variant_unref(detail->data);
858 g_object_unref(detail->invocation);
859 g_object_unref(detail->object);
864 static gboolean se_handle_send_apdu(
865 NetNfcGDbusSecureElement *object,
866 GDBusMethodInvocation *invocation,
869 GVariant *smack_privilege)
874 INFO_MSG(">>> REQUEST from [%s]",
875 g_dbus_method_invocation_get_sender(invocation));
877 /* check privilege and update client context */
878 if (net_nfc_server_gdbus_check_privilege(invocation,
882 DEBUG_ERR_MSG("permission denied, and finished request");
887 data = g_try_new0(SeDataApdu, 1);
890 DEBUG_ERR_MSG("Memory allocation failed");
891 g_dbus_method_invocation_return_dbus_error(invocation,
892 "org.tizen.NetNfcService.AllocationError",
893 "Can not allocate memory");
898 data->object = g_object_ref(object);
899 data->invocation = g_object_ref(invocation);
900 data->handle = GUINT_TO_POINTER(arg_handle);
901 data->data = g_variant_ref(apdudata);
903 result = net_nfc_server_controller_async_queue_push(
904 se_send_apdu_thread_func, data);
907 g_dbus_method_invocation_return_dbus_error(invocation,
908 "org.tizen.NetNfcService.Se.ThreadError",
909 "can not push to controller thread");
911 g_variant_unref(data->data);
913 g_object_unref(data->object);
914 g_object_unref(data->invocation);
922 static void _se_set_card_emulation_thread_func(gpointer user_data)
924 SeSetCardEmul *data = (SeSetCardEmul *)user_data;
925 net_nfc_error_e result = NET_NFC_OK;
927 g_assert(data != NULL);
928 g_assert(data->object != NULL);
929 g_assert(data->invocation != NULL);
931 if (data->mode == NET_NFC_CARD_EMELATION_ENABLE)
933 net_nfc_controller_set_secure_element_mode(
934 net_nfc_server_se_get_se_mode(),
935 SECURE_ELEMENT_VIRTUAL_MODE,
937 if (result == NET_NFC_OK)
939 DEBUG_SERVER_MSG("changed to CARD EMULATION ON");
941 net_nfc_server_se_set_se_mode(
942 SECURE_ELEMENT_VIRTUAL_MODE);
946 DEBUG_ERR_MSG("CARD EMULATION ON fail [%d]", result);
949 else if (data->mode == NET_NFC_CARD_EMULATION_DISABLE)
951 net_nfc_controller_set_secure_element_mode(
952 net_nfc_server_se_get_se_mode(),
953 SECURE_ELEMENT_OFF_MODE,
955 if (result == NET_NFC_OK)
957 DEBUG_SERVER_MSG("changed to CARD EMULATION OFF");
959 net_nfc_server_se_set_se_mode(SECURE_ELEMENT_OFF_MODE);
963 DEBUG_ERR_MSG("CARD EMULATION OFF fail [%d]", result);
968 result = NET_NFC_INVALID_PARAM;
971 net_nfc_gdbus_secure_element_complete_set_card_emulation(data->object,
972 data->invocation, result);
974 g_object_unref(data->invocation);
975 g_object_unref(data->object);
980 static gboolean _se_handle_set_card_emulation(
981 NetNfcGDbusSecureElement *object,
982 GDBusMethodInvocation *invocation,
984 GVariant *smack_privilege)
989 INFO_MSG(">>> REQUEST from [%s]",
990 g_dbus_method_invocation_get_sender(invocation));
992 /* check privilege and update client context */
993 if (net_nfc_server_gdbus_check_privilege(invocation,
997 DEBUG_ERR_MSG("permission denied, and finished request");
1002 data = g_try_new0(SeSetCardEmul, 1);
1005 DEBUG_ERR_MSG("Memory allocation failed");
1006 g_dbus_method_invocation_return_dbus_error(invocation,
1007 "org.tizen.NetNfcService.AllocationError",
1008 "Can not allocate memory");
1013 data->object = g_object_ref(object);
1014 data->invocation = g_object_ref(invocation);
1015 data->mode = arg_mode;
1017 result = net_nfc_server_controller_async_queue_push(
1018 _se_set_card_emulation_thread_func, data);
1019 if (result == FALSE)
1021 g_dbus_method_invocation_return_dbus_error(invocation,
1022 "org.tizen.NetNfcService.Se.ThreadError",
1023 "can not push to controller thread");
1025 g_object_unref(data->object);
1026 g_object_unref(data->invocation);
1034 static void se_set_data_thread_func(gpointer user_data)
1036 SeDataSeType *data = (SeDataSeType *)user_data;
1037 gboolean isTypeChanged = FALSE;
1038 net_nfc_error_e result = NET_NFC_OK;
1040 g_assert(data != NULL);
1041 g_assert(data->object != NULL);
1042 g_assert(data->invocation != NULL);
1044 if (data->se_type != net_nfc_server_se_get_se_type())
1046 result = net_nfc_server_se_change_se(data->se_type);
1047 isTypeChanged = TRUE;
1050 net_nfc_gdbus_secure_element_complete_set(data->object,
1051 data->invocation, result);
1054 net_nfc_gdbus_secure_element_emit_se_type_changed(data->object,
1057 g_object_unref(data->invocation);
1058 g_object_unref(data->object);
1063 static gboolean se_handle_set(
1064 NetNfcGDbusSecureElement *object,
1065 GDBusMethodInvocation *invocation,
1067 GVariant *smack_privilege)
1072 INFO_MSG(">>> REQUEST from [%s]",
1073 g_dbus_method_invocation_get_sender(invocation));
1075 /* check privilege and update client context */
1076 if (net_nfc_server_gdbus_check_privilege(invocation,
1080 DEBUG_ERR_MSG("permission denied, and finished request");
1085 data = g_try_new0(SeDataSeType, 1);
1088 DEBUG_ERR_MSG("Memory allocation failed");
1089 g_dbus_method_invocation_return_dbus_error(invocation,
1090 "org.tizen.NetNfcService.AllocationError",
1091 "Can not allocate memory");
1096 data->object = g_object_ref(object);
1097 data->invocation = g_object_ref(invocation);
1098 data->se_type = arg_type;
1100 result = net_nfc_server_controller_async_queue_push(
1101 se_set_data_thread_func, data);
1102 if (result == FALSE)
1104 g_dbus_method_invocation_return_dbus_error(invocation,
1105 "org.tizen.NetNfcService.Se.ThreadError",
1106 "can not push to controller thread");
1108 g_object_unref(data->object);
1109 g_object_unref(data->invocation);
1117 static void se_get_data_thread_func(gpointer user_data)
1119 SeDataSeType *data = (SeDataSeType *)user_data;
1120 net_nfc_error_e result = NET_NFC_OK;
1122 g_assert(data != NULL);
1123 g_assert(data->object != NULL);
1124 g_assert(data->invocation != NULL);
1126 net_nfc_gdbus_secure_element_complete_get(data->object,
1128 net_nfc_server_se_get_se_type(),
1131 g_object_unref(data->invocation);
1132 g_object_unref(data->object);
1137 static gboolean se_handle_get(
1138 NetNfcGDbusSecureElement *object,
1139 GDBusMethodInvocation *invocation,
1140 GVariant *smack_privilege)
1145 INFO_MSG(">>> REQUEST from [%s]",
1146 g_dbus_method_invocation_get_sender(invocation));
1148 /* check privilege and update client context */
1149 if (net_nfc_server_gdbus_check_privilege(invocation,
1153 DEBUG_ERR_MSG("permission denied, and finished request");
1158 data = g_try_new0(SeDataSeType, 1);
1161 DEBUG_ERR_MSG("Memory allocation failed");
1162 g_dbus_method_invocation_return_dbus_error(invocation,
1163 "org.tizen.NetNfcService.AllocationError",
1164 "Can not allocate memory");
1169 data->object = g_object_ref(object);
1170 data->invocation = g_object_ref(invocation);
1172 result = net_nfc_server_controller_async_queue_push(
1173 se_get_data_thread_func, data);
1174 if (result == FALSE)
1176 g_dbus_method_invocation_return_dbus_error(invocation,
1177 "org.tizen.NetNfcService.Se.ThreadError",
1178 "can not push to controller thread");
1180 g_object_unref(data->object);
1181 g_object_unref(data->invocation);
1189 gboolean net_nfc_server_se_init(GDBusConnection *connection)
1191 GError *error = NULL;
1195 g_object_unref(se_skeleton);
1197 /* initialize UICC */
1200 se_skeleton = net_nfc_gdbus_secure_element_skeleton_new();
1202 g_signal_connect(se_skeleton,
1204 G_CALLBACK(se_handle_set),
1207 g_signal_connect(se_skeleton,
1209 G_CALLBACK(se_handle_get),
1212 g_signal_connect(se_skeleton,
1213 "handle-set-card-emulation",
1214 G_CALLBACK(_se_handle_set_card_emulation),
1217 g_signal_connect(se_skeleton,
1218 "handle-open-secure-element",
1219 G_CALLBACK(se_handle_open_secure_element),
1222 g_signal_connect(se_skeleton,
1223 "handle-close-secure-element",
1224 G_CALLBACK(se_handle_close_secure_element),
1227 g_signal_connect(se_skeleton,
1229 G_CALLBACK(se_handle_get_atr),
1232 g_signal_connect(se_skeleton,
1234 G_CALLBACK(se_handle_send_apdu),
1237 result = g_dbus_interface_skeleton_export(
1238 G_DBUS_INTERFACE_SKELETON(se_skeleton),
1240 "/org/tizen/NetNfcService/SecureElement",
1242 if (result == FALSE)
1244 DEBUG_ERR_MSG("can not skeleton_export %s", error->message);
1246 g_error_free(error);
1248 net_nfc_server_se_deinit();
1254 void net_nfc_server_se_deinit(void)
1258 g_object_unref(se_skeleton);
1261 /* de-initialize UICC */
1266 static void se_detected_thread_func(gpointer user_data)
1268 net_nfc_target_handle_s *handle = NULL;
1269 uint32_t devType = 0;
1270 GVariant *data = NULL;
1271 net_nfc_error_e result = NET_NFC_OK;
1273 g_assert(user_data != NULL);
1275 if (se_skeleton == NULL)
1277 DEBUG_ERR_MSG("se skeleton is not initialized");
1279 g_variant_unref((GVariant *)user_data);
1284 g_variant_get((GVariant *)user_data,
1290 net_nfc_server_se_set_current_ese_handle(handle);
1292 DEBUG_SERVER_MSG("trying to connect to ESE = [0x%p]", handle);
1294 if (!net_nfc_controller_connect(handle, &result))
1296 DEBUG_SERVER_MSG("connect failed = [%d]", result);
1299 net_nfc_gdbus_secure_element_emit_ese_detected(
1301 GPOINTER_TO_UINT(handle),
1305 g_variant_unref((GVariant *)user_data);
1308 static void se_transcation_thread_func(gpointer user_data)
1310 ServerSeData *detail = (ServerSeData *)user_data;
1312 g_assert(user_data != NULL);
1314 if (detail->event == NET_NFC_MESSAGE_SE_START_TRANSACTION)
1316 DEBUG_SERVER_MSG("launch se app");
1318 net_nfc_app_util_launch_se_transaction_app(
1321 detail->param.buffer,
1322 detail->param.length);
1324 DEBUG_SERVER_MSG("launch se app end");
1327 net_nfc_util_free_data(&detail->param);
1328 net_nfc_util_free_data(&detail->aid);
1333 void net_nfc_server_se_detected(void *info)
1335 net_nfc_request_target_detected_t *se_target =
1336 (net_nfc_request_target_detected_t *)info;
1337 GVariant *parameter;
1340 data = net_nfc_util_gdbus_buffer_to_variant(
1341 se_target->target_info_values.buffer,
1342 se_target->target_info_values.length);
1344 parameter = g_variant_new("uu@a(y)",
1345 GPOINTER_TO_UINT(se_target->handle),
1348 if (parameter != NULL) {
1349 if (net_nfc_server_controller_async_queue_push(
1350 se_detected_thread_func,
1351 parameter) == FALSE) {
1352 DEBUG_ERR_MSG("can not push to controller thread");
1354 g_variant_unref(parameter);
1357 DEBUG_ERR_MSG("g_variant_new failed");
1360 /* FIXME : should be removed when plugins would be fixed*/
1361 _net_nfc_util_free_mem(info);
1364 void net_nfc_server_se_transaction_received(void *info)
1366 net_nfc_request_se_event_t *se_event =
1367 (net_nfc_request_se_event_t *)info;
1368 ServerSeData *detail;
1370 detail = g_try_new0(ServerSeData, 1);
1371 if (detail != NULL) {
1372 detail->event = se_event->request_type;
1374 if (se_event->aid.buffer != NULL && se_event->aid.length > 0) {
1375 if (net_nfc_util_alloc_data(&detail->aid,
1376 se_event->aid.length) == true) {
1377 memcpy(detail->aid.buffer, se_event->aid.buffer,
1378 se_event->aid.length);
1382 if (se_event->param.buffer != NULL &&
1383 se_event->param.length > 0) {
1384 if (net_nfc_util_alloc_data(&detail->param,
1385 se_event->param.length) == true) {
1386 memcpy(detail->param.buffer,
1387 se_event->param.buffer,
1388 se_event->param.length);
1392 if (net_nfc_server_controller_async_queue_push(
1393 se_transcation_thread_func, detail) == FALSE) {
1394 DEBUG_ERR_MSG("can not push to controller thread");
1396 net_nfc_util_free_data(&detail->param);
1397 net_nfc_util_free_data(&detail->aid);
1402 DEBUG_ERR_MSG("g_new0 failed");
1405 /* FIXME : should be removed when plugins would be fixed*/
1406 net_nfc_util_free_data(&se_event->param);
1407 net_nfc_util_free_data(&se_event->aid);
1409 _net_nfc_util_free_mem(info);