NFC version up 0.1.102 -> 0.1.143 14/59714/1 accepted/tizen/ivi/20160218.084014 accepted/tizen/mobile/20160218.055427 accepted/tizen/tv/20160218.055452 accepted/tizen/wearable/20160218.055523 submit/tizen/20160218.012429 submit/tizen_common/20160218.142243
authorJihoon Jung <jh8801.jung@samsung.com>
Wed, 17 Feb 2016 22:35:35 +0000 (07:35 +0900)
committerJihoon Jung <jh8801.jung@samsung.com>
Wed, 17 Feb 2016 23:10:57 +0000 (08:10 +0900)
Signed-off-by: Jihoon Jung <jh8801.jung@samsung.com>
Change-Id: I0a5713fd18c5cc044dc99ee236e3b36bee39244e

71 files changed:
CMakeLists.txt
packaging/nfc-manager.service
packaging/nfc-manager.socket [new file with mode: 0755]
packaging/nfc-manager.spec
src/clientlib/CMakeLists.txt
src/clientlib/include/net_nfc_client_hce.h
src/clientlib/include/net_nfc_client_hce_ipc.h [new file with mode: 0755]
src/clientlib/include/net_nfc_client_se.h
src/clientlib/include/net_nfc_client_test.h
src/clientlib/net_nfc_client_hce.c
src/clientlib/net_nfc_client_hce_ipc.c [new file with mode: 0755]
src/clientlib/net_nfc_client_se.c
src/clientlib/net_nfc_client_tag.c
src/clientlib/net_nfc_client_tag_felica.c
src/clientlib/net_nfc_client_tag_mifare.c
src/clientlib/net_nfc_client_test.c
src/commonlib/CMakeLists.txt
src/commonlib/include/net_nfc_debug_internal.h
src/commonlib/include/net_nfc_oem_controller.h
src/commonlib/include/net_nfc_typedef.h
src/commonlib/include/net_nfc_typedef_internal.h
src/commonlib/include/net_nfc_util_hce.h
src/commonlib/include/net_nfc_util_internal.h
src/commonlib/net_nfc.xml
src/commonlib/net_nfc_util.c
src/commonlib/net_nfc_util_handover.c
src/commonlib/net_nfc_util_hce.c
src/commonlib/net_nfc_util_ndef_message.c
src/commonlib/net_nfc_util_openssl.c
src/commonlib/net_nfc_util_sign_record.c
src/manager/CMakeLists.txt
src/manager/addons/net_nfc_addon_hce.c
src/manager/addons/net_nfc_addon_hce_ndef.c
src/manager/addons/net_nfc_addon_hce_ppse.c
src/manager/addons/net_nfc_addon_hce_tmoney.c
src/manager/include/net_nfc_app_util_internal.h
src/manager/include/net_nfc_controller_internal.h
src/manager/include/net_nfc_server_common.h
src/manager/include/net_nfc_server_context_internal.h
src/manager/include/net_nfc_server_handover_internal.h
src/manager/include/net_nfc_server_hce.h
src/manager/include/net_nfc_server_hce_ipc.h [new file with mode: 0755]
src/manager/include/net_nfc_server_route_table.h
src/manager/include/net_nfc_server_se.h
src/manager/include/net_nfc_server_vconf.h
src/manager/include/net_nfc_service_access_control.h [new file with mode: 0755]
src/manager/net_nfc_app_util.c
src/manager/net_nfc_controller.c
src/manager/net_nfc_manager.c
src/manager/net_nfc_manager_util.c
src/manager/net_nfc_server.c
src/manager/net_nfc_server_common.c
src/manager/net_nfc_server_context.c
src/manager/net_nfc_server_handover_wfd.c [new file with mode: 0755]
src/manager/net_nfc_server_handover_wps.c
src/manager/net_nfc_server_hce.c
src/manager/net_nfc_server_hce_ipc.c [new file with mode: 0755]
src/manager/net_nfc_server_manager.c
src/manager/net_nfc_server_process_handover.c
src/manager/net_nfc_server_process_npp.c
src/manager/net_nfc_server_process_snep.c
src/manager/net_nfc_server_route_table.c
src/manager/net_nfc_server_se.c
src/manager/net_nfc_server_tag.c
src/manager/net_nfc_server_test.c
src/manager/net_nfc_server_vconf.c
src/manager/org.tizen.NetNfcService.service
src/manager/private/net_nfc_service_access_control_private.c [new file with mode: 0644]
src/plugin/CMakeLists.txt [new file with mode: 0644]
src/plugin/pkgmgr_plugin_cardemulation.c [new file with mode: 0644]
src/plugin/pkgmgr_plugin_useese.c [new file with mode: 0644]

index 62115ff..ff270b7 100755 (executable)
@@ -11,3 +11,4 @@ ADD_DEFINITIONS("-D_GNU_SOURCE")
 ADD_SUBDIRECTORY(src/commonlib)
 ADD_SUBDIRECTORY(src/clientlib)
 ADD_SUBDIRECTORY(src/manager)
+ADD_SUBDIRECTORY(src/plugin)
\ No newline at end of file
index 62f8637..6971b8d 100755 (executable)
@@ -1,14 +1,16 @@
 [Unit]
 Description=NFC manager
-After=system-server.service
+After=system-server.service nfc-manager.socket
 
 [Service]
 User=system
 Group=system
-Type=simple
-ExecStart=/usr/bin/nfc-manager-daemon --daemon
+Type=dbus
+BusName=org.tizen.NetNfcService
+ExecStart=/usr/bin/nfc-manager-daemon
 KillSignal=SIGKILL
 MemoryLimit=30M
+Sockets=nfc-manager.socket
 
 [Install]
 WantedBy=multi-user.target
diff --git a/packaging/nfc-manager.socket b/packaging/nfc-manager.socket
new file mode 100755 (executable)
index 0000000..c3f0250
--- /dev/null
@@ -0,0 +1,7 @@
+[Socket]
+ListenStream=/tmp/.nfc-hce.sock
+SocketMode=0777
+SmackLabelIPIn=nfc-manager::card_emul
+SmackLabelIPOut=@
+
+Service=nfc-manager.service
index 8fcf7c6..6a66de9 100755 (executable)
@@ -1,11 +1,12 @@
 Name:       nfc-manager
 Summary:    NFC framework manager
-Version:    0.1.102
+Version:    0.1.143
 Release:    0
 Group:      Network & Connectivity/NFC
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
 Source1:    nfc-manager.service
+Source2:    nfc-manager.socket
 BuildRequires: cmake
 BuildRequires: pkgconfig(aul)
 BuildRequires: pkgconfig(glib-2.0)
@@ -25,6 +26,7 @@ BuildRequires: pkgconfig(mm-keysound)
 BuildRequires: pkgconfig(syspopup-caller)
 BuildRequires: pkgconfig(notification)
 BuildRequires: pkgconfig(capi-network-wifi)
+BuildRequires: pkgconfig(capi-network-wifi-direct)
 BuildRequires: pkgconfig(capi-system-info)
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(pkgmgr-info)
@@ -33,6 +35,8 @@ BuildRequires: pkgconfig(libcurl)
 BuildRequires: pkgconfig(cynara-client)
 BuildRequires: pkgconfig(cynara-creds-gdbus)
 BuildRequires: pkgconfig(cynara-session)
+BuildRequires: pkgconfig(capi-system-device)
+BuildRequires: pkgconfig(libsystemd-daemon)
 BuildRequires: python
 BuildRequires: python-xml
 
@@ -106,6 +110,7 @@ LDFLAGS="$LDFLAGS" cmake . \
 
 mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants
 cp -af %{SOURCE1} %{buildroot}%{_libdir}/systemd/system/
+cp -af %{SOURCE2} %{buildroot}%{_libdir}/systemd/system/
 
 mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants
 ln -s ../%{name}.service %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants/%{name}.service
@@ -164,6 +169,7 @@ systemctl daemon-reload
 %defattr(-,root,root,-)
 %{_bindir}/nfc-manager-daemon
 %{_libdir}/systemd/system/%{name}.service
+%{_libdir}/systemd/system/%{name}.socket
 %{_libdir}/systemd/system/multi-user.target.wants/%{name}.service
 %{_datadir}/dbus-1/system-services/org.tizen.NetNfcService.service
 %{_datadir}/license/%{name}
@@ -189,6 +195,8 @@ systemctl daemon-reload
 %defattr(-,root,root,-)
 %{_libdir}/libnfc-common-lib.so
 %{_libdir}/libnfc-common-lib.so.*
+/usr/etc/package-manager/parserlib/metadata/libcardemulation_plugin.so
+/usr/etc/package-manager/parserlib/metadata/libuseese_plugin.so
 %{_datadir}/license/nfc-common-lib
 %{_datadir}/nfc-manager-daemon/sounds/Operation_sdk.wav
 
index d5b5693..7d64ea9 100755 (executable)
@@ -18,7 +18,7 @@ IF("${CMAKE_BUILD_TYPE}" STREQUAL "")
 ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "")
 
 INCLUDE(FindPkgConfig)
-pkg_check_modules(clientlib_pkges REQUIRED dlog vconf)
+pkg_check_modules(clientlib_pkges REQUIRED dlog vconf gthread-2.0)
 
 FOREACH(flag ${clientlib_pkges_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index da96271..d4b8d38 100755 (executable)
@@ -45,6 +45,9 @@ net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
 net_nfc_error_e net_nfc_client_hce_init(void);
 void net_nfc_client_hce_deinit(void);
 
+void net_nfc_client_hce_process_received_event(int event,
+       net_nfc_target_handle_h handle, data_h data);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/clientlib/include/net_nfc_client_hce_ipc.h b/src/clientlib/include/net_nfc_client_hce_ipc.h
new file mode 100755 (executable)
index 0000000..cc5ca6d
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NET_NFC_CLIENT_HCE_IPC_H__
+#define __NET_NFC_CLIENT_HCE_IPC_H__
+
+#include "net_nfc_typedef.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* internal */
+net_nfc_error_e net_nfc_client_hce_ipc_init();
+void net_nfc_client_hce_ipc_deinit();
+bool net_nfc_client_hce_ipc_is_initialized();
+
+bool net_nfc_server_hce_ipc_send_to_server(int type,
+       net_nfc_target_handle_s *handle, data_s *data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //__NET_NFC_CLIENT_HCE_IPC_H__
index c5cfad9..6e01c5d 100755 (executable)
@@ -76,7 +76,7 @@ typedef bool (*net_nfc_client_se_registered_aid_cb)(net_nfc_se_type_e se_type,
        const char *aid, bool readonly, void *user_data);
 
 typedef bool (*net_nfc_client_se_registered_handler_cb)(const char *package,
-       void *user_data);
+       int count, void *user_data);
 
 /************* Secure Element API's*************/
 
@@ -214,6 +214,15 @@ net_nfc_error_e net_nfc_client_se_foreach_registered_handlers_sync(
        net_nfc_client_se_registered_handler_cb callback,
        void *user_data);
 
+net_nfc_error_e net_nfc_client_se_get_handler_storage_info_sync(
+       net_nfc_card_emulation_category_t category, int *used, int *max);
+
+net_nfc_error_e net_nfc_client_se_get_conflict_handlers_sync(
+       const char *package, net_nfc_card_emulation_category_t category,
+       const char *aid, char ***handlers);
+
+net_nfc_error_e net_nfc_client_se_set_preferred_handler_sync(bool state);
+
 //net_nfc_error_e net_nfc_client_hce_get_route_table_sync(data_h arg_aid);
 
 
index 2fa8bc4..418f8bd 100755 (executable)
@@ -87,6 +87,9 @@ net_nfc_error_e net_nfc_client_test_init(void);
 
 void net_nfc_client_test_deinit(void);
 
+net_nfc_error_e net_nfc_client_test_set_listen_tech_mask_sync(uint32_t tech);
+
+
 #ifdef __cplusplus
 }
 #endif
index 0d21368..a95b969 100755 (executable)
@@ -25,6 +25,7 @@
 #include "net_nfc_client.h"
 #include "net_nfc_client_util_internal.h"
 #include "net_nfc_client_manager.h"
+#include "net_nfc_client_hce_ipc.h"
 #include "net_nfc_client_hce.h"
 
 
@@ -56,7 +57,7 @@ static void __load_package_name()
 static void hce_event_received(GObject *source_object, guint arg_handle,
        guint arg_event, GVariant *arg_apdu, gchar *arg_package)
 {
-       INFO_MSG(">>> SIGNAL arrived hce_apdu_receive");
+       INFO_MSG(">>> SIGNAL arrived");
 
        if (hce_handler.hce_event_cb != NULL) {
 
@@ -72,6 +73,18 @@ static void hce_event_received(GObject *source_object, guint arg_handle,
        }
 }
 
+void net_nfc_client_hce_process_received_event(int event,
+       net_nfc_target_handle_h handle, data_h data)
+{
+       INFO_MSG(">>> SIGNAL arrived");
+
+       if (hce_handler.hce_event_cb != NULL) {
+               hce_handler.hce_event_cb(handle,
+                       (net_nfc_hce_event_t)event, data,
+                       hce_handler.hce_data);
+       }
+}
+
 
 
 NET_NFC_EXPORT_API
@@ -81,7 +94,9 @@ net_nfc_error_e net_nfc_client_hce_set_event_received_cb(
        net_nfc_error_e result = NET_NFC_OK;
        GError *error = NULL;
 
-       DEBUG_CLIENT_MSG("net_nfc_client_hce_set_event_received_cb set");
+       if (callback == NULL) {
+               return net_nfc_client_hce_unset_event_received_cb();
+       }
 
        if (hce_proxy == NULL)
        {
@@ -98,6 +113,15 @@ net_nfc_error_e net_nfc_client_hce_set_event_received_cb(
                &result, NULL, &error) == true) {
                hce_handler.hce_event_cb = callback;
                hce_handler.hce_data = user_data;
+
+               if (net_nfc_client_hce_ipc_is_initialized() == false) {
+                       result = net_nfc_client_hce_ipc_init();
+                       if (result != NET_NFC_OK) {
+                               DEBUG_ERR_MSG("net_nfc_client_hce_ipc_init failed");
+
+                               result = NET_NFC_IPC_FAIL;
+                       }
+               }
        } else {
                DEBUG_ERR_MSG("net_nfc_gdbus_hce_call_start_hce_handler_sync failed: %s", error->message);
                g_error_free(error);
@@ -116,6 +140,7 @@ net_nfc_error_e net_nfc_client_hce_unset_event_received_cb(void)
 
        if (hce_proxy == NULL) {
                DEBUG_ERR_MSG("not initialized!!!");
+
                return NET_NFC_NOT_INITIALIZED;
        }
 
@@ -123,6 +148,8 @@ net_nfc_error_e net_nfc_client_hce_unset_event_received_cb(void)
                &result, NULL, &error) == true) {
                hce_handler.hce_event_cb = NULL;
                hce_handler.hce_data = NULL;
+
+               net_nfc_client_hce_ipc_deinit();
        } else {
                DEBUG_ERR_MSG("net_nfc_gdbus_hce_call_stop_hce_handler_sync failed: %s", error->message);
                g_error_free(error);
@@ -132,7 +159,7 @@ net_nfc_error_e net_nfc_client_hce_unset_event_received_cb(void)
 
        return result;
 }
-
+#if 0
 NET_NFC_EXPORT_API
 net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
                                net_nfc_target_handle_h handle,
@@ -179,7 +206,44 @@ net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
 
        return result;
 }
+#else
+NET_NFC_EXPORT_API
+net_nfc_error_e net_nfc_client_hce_response_apdu_sync(
+                               net_nfc_target_handle_h handle,
+                               data_h resp_apdu_data)
+{
+       net_nfc_error_e result;
 
+       DEBUG_CLIENT_MSG(">>> net_nfc_client_hce_response_apdu_sync!!");
+
+       if (hce_proxy == NULL) {
+               result = net_nfc_client_hce_init();
+               if (result != NET_NFC_OK) {
+                       DEBUG_ERR_MSG("net_nfc_client_hce_init failed, [%d]", result);
+
+                       return result;
+               }
+       }
+
+       if (net_nfc_client_hce_ipc_is_initialized() == false) {
+               if (net_nfc_client_hce_ipc_init() == false) {
+                       DEBUG_ERR_MSG("net_nfc_client_hce_ipc_init failed");
+
+                       return NET_NFC_NOT_INITIALIZED;
+               }
+       }
+
+       if (net_nfc_server_hce_ipc_send_to_server(0, handle, resp_apdu_data) == true) {
+               result = NET_NFC_OK;
+       } else {
+               DEBUG_ERR_MSG("net_nfc_server_hce_ipc_send_to_server failed");
+
+               result = NET_NFC_IPC_FAIL;
+       }
+
+       return result;
+}
+#endif
 net_nfc_error_e net_nfc_client_hce_init(void)
 {
        GError *error = NULL;
@@ -223,6 +287,8 @@ void net_nfc_client_hce_deinit(void)
 {
        if (hce_proxy != NULL)
        {
+               net_nfc_client_hce_ipc_deinit();
+
                g_object_unref(hce_proxy);
                hce_proxy = NULL;
        }
diff --git a/src/clientlib/net_nfc_client_hce_ipc.c b/src/clientlib/net_nfc_client_hce_ipc.c
new file mode 100755 (executable)
index 0000000..5850a6f
--- /dev/null
@@ -0,0 +1,364 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/epoll.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <stdarg.h>
+#include <signal.h>
+#include <glib-object.h>
+
+#include "net_nfc_typedef_internal.h"
+#include "net_nfc_debug_internal.h"
+#include "net_nfc_util_defines.h"
+#include "net_nfc_util_internal.h"
+#include "net_nfc_util_hce.h"
+#include "net_nfc_client_hce.h"
+#include "net_nfc_client_hce_ipc.h"
+
+/* static variable */
+static pthread_mutex_t g_client_ipc_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t cb_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static int hce_client_socket = -1;
+static GIOChannel *hce_client_channel = NULL;
+static guint hce_client_src_id = 0;
+
+/* static function */
+
+/////////////////////
+static void __set_non_block_socket(int socket)
+{
+       int flags;
+
+       flags = fcntl(socket, F_GETFL);
+       flags |= O_NONBLOCK;
+
+       if (fcntl(socket, F_SETFL, flags) < 0) {
+               DEBUG_ERR_MSG("fcntl, executing nonblock error");
+       }
+}
+
+static bool __receive_data_from_server(int socket, data_s *data)
+{
+       bool result;
+       ssize_t ret;
+       uint32_t len;
+
+       /* first, receive length */
+       ret = recv(socket, (void *)&len, sizeof(len), 0);
+       if (ret != sizeof(len)) {
+               DEBUG_ERR_MSG("recv failed, socket [%d], result [%d]", socket, ret);
+
+               return false;
+       }
+
+       if (len > 1024) {
+               DEBUG_ERR_MSG("too large message, socket [%d], len [%d]", socket, len);
+
+               return false;
+       }
+
+       result = net_nfc_util_init_data(data, len);
+       if (result == true) {
+               ssize_t offset = 0;
+
+               /* second, receive buffer */
+               do {
+                       ret = recv(socket, data->buffer + offset, data->length - offset, 0);
+                       if (ret == -1) {
+                               break;
+                       }
+
+                       offset += ret;
+               } while (offset < len);
+
+               if (offset != len) {
+                       DEBUG_ERR_MSG("recv failed, socket [%d], offset [%d], len [%d]", socket, offset, len);
+
+                       net_nfc_util_clear_data(data);
+
+                       result = false;
+               } else {
+                       DEBUG_CLIENT_MSG("recv success, length [%d]", offset);
+               }
+       } else {
+               DEBUG_ERR_MSG("net_nfc_util_init_data failed");
+       }
+
+       return result;
+}
+
+static bool __process_server_message()
+{
+       bool result;
+       data_s data;
+
+       if (__receive_data_from_server(hce_client_socket, &data) == true) {
+               net_nfc_hce_data_t *header;
+               data_s temp;
+
+               header = (net_nfc_hce_data_t *)data.buffer;
+
+               temp.buffer = header->data;
+               temp.length = data.length - sizeof(net_nfc_hce_data_t);
+
+               net_nfc_client_hce_process_received_event(header->type,
+                       (net_nfc_target_handle_h)header->handle, (data_h)&temp);
+
+               result = true;
+       } else {
+               DEBUG_ERR_MSG("__receive_data_from_client failed");
+
+               result = false;
+       }
+
+       return result;
+}
+
+static bool __send_data_to_server(int socket, data_s *data)
+{
+       ssize_t ret;
+
+       ret = send(socket, data->buffer, data->length, 0);
+       if (ret == -1) {
+               DEBUG_ERR_MSG("send failed, socket [%d]", socket);
+
+               return false;
+       }
+
+       return true;
+}
+
+bool net_nfc_server_hce_ipc_send_to_server(int type,
+       net_nfc_target_handle_s *handle, data_s *data)
+{
+       bool ret;
+       data_s temp;
+       uint32_t len = sizeof(net_nfc_hce_data_t);
+
+       if (data != NULL && data->length > 0) {
+               len += data->length;
+       }
+
+       ret = net_nfc_util_init_data(&temp, len + sizeof(len));
+       if (ret == true) {
+               net_nfc_hce_data_t *header;
+
+               *(uint32_t *)(temp.buffer) = len;
+               header = (net_nfc_hce_data_t *)(temp.buffer + sizeof(len));
+
+               header->type = type;
+               header->handle = (int)handle;
+
+               if (data != NULL && data->length > 0) {
+                       memcpy(header->data, data->buffer, data->length);
+               }
+
+               ret = __send_data_to_server(hce_client_socket, &temp);
+
+               net_nfc_util_clear_data(&temp);
+       } else {
+               DEBUG_ERR_MSG("net_nfc_util_init_data failed");
+       }
+
+       return ret;
+}
+
+/******************************************************************************/
+
+inline void net_nfc_client_ipc_lock()
+{
+       pthread_mutex_lock(&g_client_ipc_mutex);
+}
+
+inline void net_nfc_client_ipc_unlock()
+{
+       pthread_mutex_unlock(&g_client_ipc_mutex);
+}
+
+bool net_nfc_client_hce_ipc_is_initialized()
+{
+       return (hce_client_socket != -1 && hce_client_channel != NULL);
+}
+
+static net_nfc_error_e _finalize_client_socket()
+{
+       net_nfc_error_e result = NET_NFC_OK;
+
+       net_nfc_client_ipc_lock();
+
+       if (hce_client_src_id > 0) {
+               g_source_remove(hce_client_src_id);
+               hce_client_src_id = 0;
+       }
+
+       if (hce_client_channel != NULL) {
+               g_io_channel_unref(hce_client_channel);
+               hce_client_channel = NULL;
+       }
+
+       if (hce_client_socket != -1) {
+               shutdown(hce_client_socket, SHUT_WR);
+               close(hce_client_socket);
+               hce_client_socket = -1;
+
+               INFO_MSG("client socket closed");
+       }
+
+       net_nfc_client_ipc_unlock();
+
+       return result;
+}
+
+static gboolean __on_io_event_cb(GIOChannel *channel, GIOCondition condition,
+       gpointer data)
+{
+       if ((G_IO_ERR & condition) || (G_IO_HUP & condition)) {
+               DEBUG_CLIENT_MSG("client socket is closed");
+
+               /* clean up client context */
+               net_nfc_client_hce_ipc_deinit();
+
+               return FALSE;
+       } else if (G_IO_NVAL & condition) {
+               DEBUG_CLIENT_MSG("INVALID socket");
+
+               return FALSE;
+       } else if (G_IO_IN & condition) {
+               if(channel != hce_client_channel) {
+                       DEBUG_CLIENT_MSG("unknown channel");
+
+                       return FALSE;
+               }
+
+               DEBUG_CLIENT_MSG("message from server to client socket");
+
+               if (__process_server_message() == false) {
+                       DEBUG_ERR_MSG("__process_server_message failed");
+
+                       net_nfc_client_hce_ipc_deinit();
+
+                       return FALSE;
+               }
+       } else {
+               DEBUG_CLIENT_MSG("IO ERROR. socket is closed ");
+
+               /* clean up client context */
+               net_nfc_client_hce_ipc_deinit();
+
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+net_nfc_error_e net_nfc_client_hce_ipc_init()
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       struct sockaddr_un saddrun_rv;
+       socklen_t len_saddr = 0;
+       pthread_mutexattr_t attr;
+
+       if (net_nfc_client_hce_ipc_is_initialized() == true)
+       {
+               DEBUG_CLIENT_MSG("client is already initialized");
+
+               return NET_NFC_ALREADY_INITIALIZED;
+       }
+
+       pthread_mutexattr_init(&attr);
+       pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
+       pthread_mutex_init(&cb_lock, &attr);
+
+       memset(&saddrun_rv, 0, sizeof(struct sockaddr_un));
+
+       net_nfc_client_ipc_lock();
+
+       hce_client_socket = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (hce_client_socket == -1)
+       {
+               DEBUG_ERR_MSG("get socket is failed");
+
+               result = NET_NFC_IPC_FAIL;
+               goto ERROR;
+       }
+
+       __set_non_block_socket(hce_client_socket);
+
+       saddrun_rv.sun_family = AF_UNIX;
+       strncpy(saddrun_rv.sun_path, NET_NFC_HCE_SERVER_DOMAIN, sizeof(saddrun_rv.sun_path) - 1);
+
+       len_saddr = sizeof(saddrun_rv.sun_family) + strlen(NET_NFC_HCE_SERVER_DOMAIN);
+
+       if (connect(hce_client_socket, (struct sockaddr *)&saddrun_rv, len_saddr) < 0) {
+               DEBUG_ERR_MSG("error is occured");
+               result = NET_NFC_IPC_FAIL;
+
+               goto ERROR;
+       }
+
+       GIOCondition condition = (GIOCondition)(G_IO_ERR | G_IO_HUP | G_IO_IN);
+
+       hce_client_channel = g_io_channel_unix_new(hce_client_socket);
+       if (hce_client_channel == NULL) {
+               DEBUG_ERR_MSG(" g_io_channel_unix_new is failed ");
+               result = NET_NFC_IPC_FAIL;
+
+               goto ERROR;
+       }
+
+       hce_client_src_id = g_io_add_watch(hce_client_channel, condition,
+               __on_io_event_cb, NULL);
+       if (hce_client_src_id < 1) {
+               DEBUG_ERR_MSG(" g_io_add_watch is failed ");
+               result = NET_NFC_IPC_FAIL;
+
+               goto ERROR;
+       }
+
+       DEBUG_CLIENT_MSG("client socket is ready");
+
+       net_nfc_client_ipc_unlock();
+
+       return NET_NFC_OK;
+
+ERROR :
+       DEBUG_ERR_MSG("error while initializing client ipc");
+
+       net_nfc_client_ipc_unlock();
+
+       _finalize_client_socket();
+
+       return result;
+}
+
+void net_nfc_client_hce_ipc_deinit()
+{
+       if (net_nfc_client_hce_ipc_is_initialized() == true) {
+               _finalize_client_socket();
+       }
+}
index 95561cc..4b52261 100755 (executable)
@@ -178,10 +178,10 @@ static void se_transaction_event(GObject *source_object,
        INFO_MSG(">>> SIGNAL arrived");
 
        if (fg_dispatch == true && focus_app_pid != getpgid(mypid)) {
-               DEBUG_MSG("skip transaction event, fg_dispatch [%d], focus_app_pid [%d]", fg_dispatch, focus_app_pid);
+               SECURE_MSG("skip transaction event, fg_dispatch [%d], focus_app_pid [%d]", fg_dispatch, focus_app_pid);
                return;
        }
-
+#ifdef CHECK_NFC_ACCESS_FOR_ESE
        if (net_nfc_gdbus_secure_element_call_check_transaction_permission_sync(
                NET_NFC_GDBUS_SECURE_ELEMENT(source_object),
                arg_aid,
@@ -197,7 +197,7 @@ static void se_transaction_event(GObject *source_object,
                DEBUG_ERR_MSG("not allowed process [%d]", result);
                return;
        }
-
+#endif
        switch (arg_se_type)
        {
        case NET_NFC_SE_TYPE_UICC :
@@ -554,7 +554,7 @@ net_nfc_error_e net_nfc_client_se_get_secure_element_type_sync(
                        NULL,
                        &error) == true) {
 
-               DEBUG_CLIENT_MSG("type [%d]", type);
+               SECURE_MSG("type [%d]", type);
                *se_type = type;
        } else {
                DEBUG_ERR_MSG("get secure element failed: %s", error->message);
@@ -1574,11 +1574,12 @@ net_nfc_error_e net_nfc_client_se_foreach_registered_handlers_sync(
        if (result == NET_NFC_OK) {
                GVariantIter iter;
                const gchar *handler;
+               int count;
 
                g_variant_iter_init(&iter, handlers);
 
-               while (g_variant_iter_loop(&iter, "(s)", &handler) == true) {
-                       callback(handler, user_data);
+               while (g_variant_iter_loop(&iter, "(is)", &count, &handler) == true) {
+                       callback(handler, count, user_data);
                }
        }
 
@@ -1728,6 +1729,130 @@ net_nfc_error_e net_nfc_client_se_remove_package_aids_sync(
        return result;
 }
 
+NET_NFC_EXPORT_API
+net_nfc_error_e net_nfc_client_se_set_preferred_handler_sync(bool state)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       GError *error = NULL;
+
+       if (se_proxy == NULL) {
+               result = net_nfc_client_se_init();
+               if (result != NET_NFC_OK) {
+                       DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
+
+                       return NET_NFC_NOT_INITIALIZED;
+               }
+       }
+
+       if (net_nfc_gdbus_secure_element_call_set_preferred_handler_sync(
+               se_proxy,
+               state,
+               &result,
+               NULL, &error) == FALSE) {
+               DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_set_preferred_handler_sync failed : %s", error->message);
+               result = NET_NFC_IPC_FAIL;
+
+               g_error_free(error);
+       }
+
+       return result;
+}
+
+NET_NFC_EXPORT_API
+net_nfc_error_e net_nfc_client_se_get_handler_storage_info_sync(
+       net_nfc_card_emulation_category_t category, int *used, int *max)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       GError *error = NULL;
+
+       if (se_proxy == NULL) {
+               result = net_nfc_client_se_init();
+               if (result != NET_NFC_OK) {
+                       DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
+
+                       return NET_NFC_NOT_INITIALIZED;
+               }
+       }
+
+       if (net_nfc_gdbus_secure_element_call_get_handler_storage_info_sync(
+               se_proxy,
+               category,
+               &result,
+               used,
+               max,
+               NULL, &error) == FALSE) {
+               DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_handler_storage_info_sync failed : %s", error->message);
+               result = NET_NFC_IPC_FAIL;
+
+               g_error_free(error);
+       }
+
+       return result;
+}
+
+NET_NFC_EXPORT_API
+net_nfc_error_e net_nfc_client_se_get_conflict_handlers_sync(
+       const char *package, net_nfc_card_emulation_category_t category,
+       const char *aid, char ***handlers)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       GError *error = NULL;
+       GVariant *packages = NULL;
+
+       if (se_proxy == NULL) {
+               result = net_nfc_client_se_init();
+               if (result != NET_NFC_OK) {
+                       DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
+
+                       return NET_NFC_NOT_INITIALIZED;
+               }
+       }
+
+       if (net_nfc_gdbus_secure_element_call_get_conflict_handlers_sync(
+               se_proxy,
+               package,
+               category,
+               aid,
+               &result,
+               &packages,
+               NULL, &error) == true) {
+               if (result == NET_NFC_DATA_CONFLICTED) {
+                       GVariantIter iter;
+                       gchar **pkgs;
+                       size_t len;
+
+                       g_variant_iter_init(&iter, packages);
+                       len = g_variant_iter_n_children(&iter);
+
+                       SECURE_MSG("conflict count [%d]", len);
+
+                       if (len > 0) {
+                               size_t i;
+                               gchar *temp;
+
+                               pkgs = g_new0(gchar *, len + 1);
+
+                               for (i = 0; i < len; i++) {
+                                       if (g_variant_iter_next(&iter, "(s)", &temp) == true) {
+                                               SECURE_MSG("conflict package [%s]", temp);
+                                               pkgs[i] = g_strdup(temp);
+                                       } else {
+                                               DEBUG_ERR_MSG("g_variant_iter_next failed");
+                                       }
+                               }
+
+                               *handlers = pkgs;
+                       }
+               }
+       } else {
+               DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_conflict_handlers_sync failed : %s", error->message);
+               result = NET_NFC_IPC_FAIL;
+
+               g_error_free(error);
+       }
+
+       return result;
+}
 
 net_nfc_error_e net_nfc_client_se_init(void)
 {
index d191a5b..6aceef1 100755 (executable)
@@ -153,7 +153,7 @@ static void tag_get_info_list(guint8 *buffer,
                str = g_new0(gchar, length + 1);
                memcpy(str, pos, length);
 
-               DEBUG_CLIENT_MSG("key = [%s]", str);
+               SECURE_MSG("key = [%s]", str);
 
                pos += length;
 
@@ -404,7 +404,7 @@ static void tag_tag_discovered(NetNfcGDbusTag *object,
        client_target_info = NULL;
 
        if (tag_check_filter(arg_dev_type) == FALSE) {
-               INFO_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
+               DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
 
                return;
        }
@@ -444,7 +444,7 @@ static void tag_tag_detached(NetNfcGDbusTag *object,
                        callback(tag_detached_func_data.user_data);
                }
        } else {
-               INFO_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
+               DEBUG_CLIENT_MSG("The detected target is filtered out, type [%d]", arg_dev_type);
        }
 
        net_nfc_release_tag_info((net_nfc_target_info_h)client_target_info);
@@ -644,7 +644,7 @@ net_nfc_error_e net_nfc_client_tag_get_current_tag_info_sync(
 
                                        result = NET_NFC_OK;
                                } else {
-                                       INFO_MSG("The detected target is filtered out");
+                                       DEBUG_CLIENT_MSG("The detected target is filtered out");
 
                                        result = NET_NFC_NOT_CONNECTED;
                                }
index 38fc02d..9d0226a 100755 (executable)
@@ -121,7 +121,7 @@ net_nfc_error_e net_nfc_client_felica_request_service(
 
        if (target_info->devType != NET_NFC_FELICA_PICC)
        {
-               DEBUG_CLIENT_MSG("only Jewel tag is available");
+               DEBUG_CLIENT_MSG("only felica tag is available");
                return NET_NFC_NOT_ALLOWED_OPERATION;
        }
 
@@ -212,7 +212,7 @@ net_nfc_error_e net_nfc_client_felica_request_response(
 
        if (target_info->devType != NET_NFC_FELICA_PICC)
        {
-               DEBUG_CLIENT_MSG("only Jewel tag is available");
+               DEBUG_CLIENT_MSG("only felica tag is available");
                return NET_NFC_NOT_ALLOWED_OPERATION;
        }
 
@@ -278,7 +278,7 @@ net_nfc_error_e net_nfc_client_felica_read_without_encryption(
 
        if (target_info->devType != NET_NFC_FELICA_PICC)
        {
-               DEBUG_CLIENT_MSG("only Jewel tag is available");
+               DEBUG_CLIENT_MSG("only felica tag is available");
                return NET_NFC_NOT_ALLOWED_OPERATION;
        }
 
@@ -386,7 +386,7 @@ net_nfc_error_e net_nfc_client_felica_write_without_encryption(
 
        if (target_info->devType != NET_NFC_FELICA_PICC)
        {
-               DEBUG_CLIENT_MSG("only Jewel tag is available");
+               DEBUG_CLIENT_MSG("only felica tag is available");
                return NET_NFC_NOT_ALLOWED_OPERATION;
        }
 
@@ -490,7 +490,7 @@ net_nfc_error_e net_nfc_client_felica_request_system_code(
 
        if (target_info->devType != NET_NFC_FELICA_PICC)
        {
-               DEBUG_CLIENT_MSG("only Jewel tag is available");
+               DEBUG_CLIENT_MSG("only felica tag is available");
                return NET_NFC_NOT_ALLOWED_OPERATION;
        }
 
index 5c1b115..c98e39e 100755 (executable)
@@ -418,7 +418,7 @@ net_nfc_error_e net_nfc_client_mifare_write_block(
        if (target_info->devType < NET_NFC_MIFARE_MINI_PICC ||
                        target_info->devType > NET_NFC_MIFARE_ULTRA_PICC)
        {
-               DEBUG_CLIENT_MSG("not a MIFARE TAG = [%d] \n",
+               DEBUG_CLIENT_MSG("not a MIFARE TAG = [%d] ",
                                        target_info->devType);
                return NET_NFC_NOT_SUPPORTED;
        }
@@ -579,7 +579,7 @@ net_nfc_error_e net_nfc_client_mifare_write_page(
        if (target_info->devType < NET_NFC_MIFARE_MINI_PICC ||
                        target_info->devType > NET_NFC_MIFARE_ULTRA_PICC)
        {
-               DEBUG_CLIENT_MSG("not a MIFARE TAG = [%d] \n",
+               DEBUG_CLIENT_MSG("not a MIFARE TAG = [%d] ",
                                target_info->devType);
                return NET_NFC_NOT_SUPPORTED;
        }
@@ -794,7 +794,7 @@ net_nfc_error_e net_nfc_client_mifare_decrement(net_nfc_target_handle_h handle,
                if (target_info->devType < NET_NFC_MIFARE_MINI_PICC ||
                                target_info->devType > NET_NFC_MIFARE_4K_PICC)
                {
-                       DEBUG_CLIENT_MSG("not a MIFARE Classic TAG = [%d] \n",
+                       DEBUG_CLIENT_MSG("not a MIFARE Classic TAG = [%d] ",
                                        target_info->devType);
                        return NET_NFC_NOT_SUPPORTED;
                }
index 0409b7d..b37e429 100755 (executable)
@@ -170,7 +170,7 @@ static void test_call_set_ee_data_callback(GObject *source_object,
                                res,
                                &error) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not finish set_ee_data: %s\n",
+               DEBUG_ERR_MSG("Can not finish set_ee_data: %s",
                        error->message);
                out_result = NET_NFC_IPC_FAIL;
 
@@ -654,6 +654,45 @@ net_nfc_error_e net_nfc_client_test_set_se_tech_type_sync(
        return out_result;
 }
 
+NET_NFC_EXPORT_API
+net_nfc_error_e net_nfc_client_test_set_listen_tech_mask_sync(uint32_t tech)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       GError *error = NULL;
+
+       DEBUG_CLIENT_MSG("net_nfc_client_test_set_listen_tech_mask_sync start");
+
+       if (test_proxy == NULL)
+       {
+               if(net_nfc_client_test_init() != NET_NFC_OK)
+               {
+                       DEBUG_ERR_MSG("test_proxy fail");
+                       return NET_NFC_NOT_INITIALIZED;
+               }
+       }
+
+       /* prevent executing daemon when nfc is off */
+       if (net_nfc_client_manager_is_activated() == false) {
+               return NET_NFC_NOT_ACTIVATED;
+       }
+
+       if (net_nfc_gdbus_test_call_set_listen_tech_mask_sync(test_proxy,
+                                       tech,
+                                       &result,
+                                       NULL,
+                                       &error) == FALSE)
+       {
+               DEBUG_ERR_MSG("can not call listen tech mask: %s",
+                               error->message);
+               result = NET_NFC_IPC_FAIL;
+
+               g_error_free(error);
+       }
+
+       return result;
+}
+
+
 net_nfc_error_e net_nfc_client_test_init(void)
 {
        GError *error = NULL;
index d47f555..38f736d 100755 (executable)
@@ -24,7 +24,7 @@ ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "")
 
 INCLUDE(FindPkgConfig)
 pkg_check_modules(commonlib_pkges REQUIRED
-               glib-2.0 gio-2.0 gio-unix-2.0 dlog bluetooth-api openssl aul pkgmgr-info)
+               glib-2.0 gio-2.0 gio-unix-2.0 dlog bluetooth-api openssl aul pkgmgr-info libsystemd-daemon)
 
 FOREACH(flag ${commonlib_pkges_CFLAGS})
        SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
index 8a7f25f..61f8603 100755 (executable)
@@ -211,6 +211,24 @@ const char *net_nfc_get_log_tag();
                }\
        } while(0)
 
+#define SECURE_MSG(format, args...) \
+       do {\
+               SECURE_LOGD(format, ##args);\
+               if (nfc_log_file) \
+               { \
+                       struct tm *local_tm; \
+                       char timeBuf[50]; \
+                       time_t rawtime;   time (&rawtime); \
+                       local_tm = localtime(&rawtime); \
+                       if(local_tm != NULL) { \
+                               strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", local_tm); \
+                               fprintf(nfc_log_file, "\n%s",timeBuf); \
+                               fprintf(nfc_log_file, "[W][%s:%d] "format"",__func__, __LINE__,  ##args); \
+                               fflush(nfc_log_file);\
+                       } \
+               }\
+       } while(0)
+
 #define PROFILING(str) \
        do{ \
                struct timeval mytime;\
index 7b897f1..fda0b8c 100755 (executable)
@@ -104,6 +104,10 @@ typedef bool (*net_nfc_oem_controller_secure_element_set_route_entry)
 typedef bool (*net_nfc_oem_controller_secure_element_clear_routing_entry)
        (net_nfc_se_entry_type_e type, net_nfc_error_e *result);
 
+typedef bool (*net_nfc_oem_controller_secure_element_set_listen_tech_mask)
+       (net_nfc_se_tech_protocol_type_e screen_state, net_nfc_error_e *result);
+
+
 typedef bool (*net_nfc_oem_controller_set_screen_state)
        (net_nfc_screen_state_type_e screen_state, net_nfc_error_e *result);
 
@@ -180,6 +184,8 @@ typedef struct _net_nfc_oem_interface_s
 
        net_nfc_oem_controller_secure_element_set_route_entry set_routing_entry;
        net_nfc_oem_controller_secure_element_clear_routing_entry clear_routing_entry;
+       net_nfc_oem_controller_secure_element_set_listen_tech_mask set_listen_tech_mask;
+
        net_nfc_oem_controller_set_screen_state set_screen_state;
 } net_nfc_oem_interface_s;
 
index 5938d3d..661ec4d 100755 (executable)
@@ -122,6 +122,8 @@ typedef enum
 /*968*/NET_NFC_INVALID_PARAM, /**< removal is requested but requested data is not registered */
 /*967*/NET_NFC_PERMISSION_DENIED, /**< privilege check is failed */
 /*966*/NET_NFC_NOT_ACTIVATED, /**< Application tries to request without activation */
+/*965*/NET_NFC_DATA_CONFLICTED, /**< Data is conflicted with another one */
+
 /*499*/NET_NFC_NDEF_TYPE_LENGTH_IS_NOT_OK = -499, /**< Illegal ndef record type length */
 /*498*/NET_NFC_NDEF_PAYLOAD_LENGTH_IS_NOT_OK, /**< Illegal ndef record payload length */
 /*497*/NET_NFC_NDEF_ID_LENGTH_IS_NOT_OK, /**< Illegal ndef record id length */
@@ -540,6 +542,7 @@ typedef enum
        NET_NFC_SE_INVALID_ENTRY = 0x00, /**< Indicates SE type is Invalid */
        NET_NFC_SE_TECH_ENTRY = 0x01, /*Technology*/
        NET_NFC_SE_PROTOCOL_ENTRY = 0x02, /*Protocol*/
+       NET_NFC_SE_AID_ENTRY = 0x04, /*Using it clear aid table*/
 } net_nfc_se_entry_type_e;
 
 typedef enum
index ecbd93f..af6b322 100755 (executable)
@@ -465,7 +465,7 @@ net_nfc_llcp_param_t;
 
 typedef enum
 {
-       NET_NFC_INVALID = 0x00,
+       NET_NFC_SCREEN_INVALID = 0x00,
        NET_NFC_SCREEN_OFF = 0x01,
        NET_NFC_SCREEN_ON_LOCK = 0x02,
        NET_NFC_SCREEN_ON_UNLOCK = 0x03,
index b1f7c81..507b232 100644 (file)
@@ -19,6 +19,8 @@
 
 #include "net_nfc_typedef_internal.h"
 
+#define NET_NFC_HCE_SERVER_DOMAIN              "/tmp/.nfc-hce.sock"
+
 #define NET_NFC_HCE_INS_SELECT                 (uint8_t)0xA4
 #define NET_NFC_HCE_INS_READ_BINARY            (uint8_t)0xB0
 #define NET_NFC_HCE_INS_UPDATE_BINARY          (uint8_t)0xD6
@@ -69,4 +71,13 @@ net_nfc_error_e net_nfc_util_hce_extract_parameter(data_s *apdu,
 net_nfc_error_e net_nfc_util_hce_generate_apdu(net_nfc_apdu_data_t *apdu_data,
        data_s **apdu);
 
+typedef struct _net_nfc_hce_data_t
+{
+       uint32_t type;
+       uint32_t handle;
+       uint8_t data[0];
+}
+net_nfc_hce_data_t;
+
+
 #endif //__NET_NFC_UTIL_HCE_H__
index 06f5f94..f7f9712 100755 (executable)
@@ -96,6 +96,6 @@ bool net_nfc_util_get_pkgid_by_pid(pid_t pid, char *pkgid, size_t len);
 
 bool net_nfc_util_hex_string_to_binary(const char *str, data_s *result);
 bool net_nfc_util_binary_to_hex_string(data_s *data, char *out_buf, uint32_t max_len);
-
+int net_nfc_util_get_fd_from_systemd(void);
 
 #endif //__NET_NFC_UTIL_INTERNAL_H__
index c6ad09f..1bea639 100755 (executable)
       <arg type="i" name="result" direction="out" />
     </method>
 
+        <!--
+      SetPreferredHandler
+    -->
+    <method name="SetPreferredHandler">
+         <arg type="b" name="state" direction="in" />
+      <arg type="i" name="result" direction="out" />
+    </method>
+
     <!--
       CheckTransactionPermission
     -->
     <method name="GetRegisteredHandlers">
       <arg type="u" name="category" direction="in" />
       <arg type="i" name="result" direction="out" />
+      <arg type="a(is)" name="packages" direction="out" />
+    </method>
+
+    <!--
+      GetHandlerStorageInfo
+    -->
+    <method name="GetHandlerStorageInfo">
+      <arg type="u" name="category" direction="in" />
+      <arg type="i" name="result" direction="out" />
+      <arg type="i" name="used" direction="out" />
+      <arg type="i" name="maximum" direction="out" />
+    </method>
+
+    <!--
+      GetConflictHandlers
+    -->
+    <method name="GetConflictHandlers">
+      <arg type="s" name="package" direction="in" />
+      <arg type="u" name="category" direction="in" />
+      <arg type="s" name="aid" direction="in" />
+      <arg type="i" name="result" direction="out" />
       <arg type="a(s)" name="packages" direction="out" />
     </method>
 
       <arg type="u" name="tech" direction="in" />
       <arg type="i" name="result" direction="out" />
     </method>
+
+   <!--
+      SetListenTechMask
+    -->
+    <method name="SetListenTechMask">
+      <arg type="u" name="tech" direction="in" />
+      <arg type="i" name="result" direction="out" />
+    </method>
   </interface>
 
   <interface name="org.tizen.NetNfcService.Snep">
index c185c1e..a627262 100644 (file)
@@ -20,6 +20,7 @@
 #include <pthread.h>
 #include <fcntl.h>
 #include <glib.h>
+#include <systemd/sd-daemon.h>
 
 // platform header
 #include "aul.h"
@@ -331,7 +332,7 @@ net_nfc_conn_handover_carrier_state_e net_nfc_util_get_cps(net_nfc_conn_handover
        {
                int wifi_state = 0;
 
-               vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
+               (void)vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
 
                switch (wifi_state)
                {
@@ -753,3 +754,16 @@ bool net_nfc_util_binary_to_hex_string(data_s *data, char *out_buf, uint32_t max
 
        return true;
 }
+
+int net_nfc_util_get_fd_from_systemd()
+{
+       int n = sd_listen_fds(0);
+       int fd;
+
+       for(fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START+n; ++fd) {
+               if (0 < sd_is_socket_unix(fd, SOCK_STREAM, 1, "/tmp/.nfc-hce.sock", 0)) {
+                       return fd;
+               }
+       }
+       return -1;
+}
index 9bf08f6..b0192e5 100755 (executable)
@@ -435,6 +435,10 @@ net_nfc_error_e net_nfc_util_create_ndef_record_with_carrier_config(
                result = net_nfc_util_handover_wps_create_record_from_config(record, config);
                break;
 
+       case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P :
+               result = net_nfc_util_handover_wfd_create_record_from_config(record, config);
+               break;
+
        default :
                result = NET_NFC_NOT_SUPPORTED;
                break;
@@ -457,6 +461,8 @@ net_nfc_error_e net_nfc_util_create_carrier_config_from_config_record(
                result = net_nfc_util_handover_bt_create_config_from_record(config, record);
        } else if (net_nfc_util_handover_wps_check_carrier_record(record)) {
                result = net_nfc_util_handover_wps_create_config_from_record(config, record);
+       } else if (net_nfc_util_handover_wfd_check_carrier_record(record)) {
+               result = net_nfc_util_handover_wfd_create_config_from_record(config, record);
        } else {
                result = NET_NFC_NOT_SUPPORTED;
        }
@@ -1271,6 +1277,10 @@ static net_nfc_conn_handover_carrier_type_e _get_carrier_type_from_record(
        {
                result = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
        }
+       else if (net_nfc_util_handover_wfd_check_carrier_record(record))
+       {
+               result = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
+       }
        else
        {
                result = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
index f8c9a26..64a01db 100755 (executable)
@@ -91,7 +91,7 @@ net_nfc_error_e net_nfc_util_hce_extract_parameter(data_s *apdu,
        apdu_data->le = NET_NFC_HCE_INVALID_VALUE;
        apdu_data->data = NULL;
 
-       DEBUG_SERVER_MSG("[%02X][%02X][%02X][%02X]", header->cla, header->ins, header->p1, header->p2);
+       SECURE_MSG("[%02X][%02X][%02X][%02X]", header->cla, header->ins, header->p1, header->p2);
 
        if (apdu->length > l) {
                if (apdu->length == l + 1) {
index 61c5889..fca35fd 100755 (executable)
@@ -541,33 +541,33 @@ void net_nfc_util_print_ndef_message(ndef_message_s *msg)
        }
 
        //                123456789012345678901234567890123456789012345678901234567890
-       DEBUG_MSG("========== NDEF Message ====================================\n");
-       DEBUG_MSG("Total NDEF Records count: %d\n", msg->recordCount);
+       DEBUG_MSG("========== NDEF Message ====================================");
+       DEBUG_MSG("Total NDEF Records count: %d", msg->recordCount);
        current = msg->records;
        for (idx = 0; idx < msg->recordCount; idx++)
        {
                if (current == NULL)
                {
                        DEBUG_ERR_MSG("Message Record is NULL!! unexpected error");
-                       DEBUG_MSG("============================================================\n");
+                       DEBUG_MSG("============================================================");
                        return;
                }
-               DEBUG_MSG("---------- Record -----------------------------------------\n");
-               DEBUG_MSG("MB:%d ME:%d CF:%d SR:%d IL:%d TNF:0x%02X\n",
+               DEBUG_MSG("---------- Record -----------------------------------------");
+               DEBUG_MSG("MB:%d ME:%d CF:%d SR:%d IL:%d TNF:0x%02X",
                        current->MB, current->ME, current->CF, current->SR, current->IL, current->TNF);
-               DEBUG_MSG("TypeLength:%d  PayloadLength:%d  IDLength:%d\n",
+               DEBUG_MSG("TypeLength:%d  PayloadLength:%d  IDLength:%d",
                        current->type_s.length, current->payload_s.length, current->id_s.length);
                if (current->type_s.buffer != NULL)
                {
                        memcpy(buffer, current->type_s.buffer, current->type_s.length);
                        buffer[current->type_s.length] = '\0';
-                       DEBUG_MSG("Type: %s\n", buffer);
+                       DEBUG_MSG("Type: %s", buffer);
                }
                if (current->id_s.buffer != NULL)
                {
                        memcpy(buffer, current->id_s.buffer, current->id_s.length);
                        buffer[current->id_s.length] = '\0';
-                       SECURE_LOGD("ID: %s\n", buffer);
+                       SECURE_LOGD("ID: %s", buffer);
                }
                if (current->payload_s.buffer != NULL)
                {
@@ -575,15 +575,15 @@ void net_nfc_util_print_ndef_message(ndef_message_s *msg)
                        for (idx2 = 0; idx2 < current->payload_s.length; idx2++)
                        {
                                if (idx2 % 16 == 0)
-                                       DEBUG_MSG("\n\t");
+                                       DEBUG_MSG("\t");
                                DEBUG_MSG("%02X ", current->payload_s.buffer[idx2]);
                        }
-                       DEBUG_MSG("\n");
+                       DEBUG_MSG("");
                }
                current = current->next;
        }
        //                123456789012345678901234567890123456789012345678901234567890
-       DEBUG_MSG("============================================================\n");
+       DEBUG_MSG("============================================================");
 
 }
 
index a346927..2f9c336 100755 (executable)
@@ -280,7 +280,7 @@ EVP_PKEY *_load_key(const char *file, int format, const char *pass, ENGINE *e)
 
        if (file == NULL)
        {
-               DEBUG_ERR_MSG("no keyfile specified\n");
+               DEBUG_ERR_MSG("no keyfile specified");
                return pkey;
        }
 
index 97a5b0c..67e7703 100644 (file)
@@ -337,7 +337,7 @@ net_nfc_error_e net_nfc_util_sign_records(ndef_message_s *msg, int begin_index,
        /* get target data */
        _get_records_data_buffer(begin_record, end_record->next, &data_buffer, &data_len);
 
-       DEBUG_MSG_PRINT_BUFFER(data_buffer, data_len);
+//     DEBUG_MSG_PRINT_BUFFER(data_buffer, data_len);
 
        net_nfc_util_openssl_sign_buffer(NET_NFC_SIGN_TYPE_PKCS_1, data_buffer, data_len, cert_file, password, signature, &sign_len);
 
index faea782..900ccf5 100755 (executable)
@@ -17,12 +17,12 @@ IF("${CMAKE_BUILD_TYPE}" STREQUAL "")
 ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "")
 
 # for package file
-SET(dependents "aul glib-2.0 gio-unix-2.0 vconf dlog tapi appsvc libcurl bluetooth-api capi-network-bluetooth openssl deviced feedback capi-media-wav-player mm-keysound syspopup-caller notification capi-network-wifi capi-system-info sqlite3  cynara-client cynara-creds-gdbus cynara-session")
+SET(dependents "aul glib-2.0 gio-unix-2.0 vconf dlog tapi appsvc libcurl bluetooth-api capi-network-bluetooth openssl deviced feedback capi-media-wav-player mm-keysound syspopup-caller notification capi-network-wifi capi-system-info sqlite3 capi-network-wifi-direct capi-system-device cynara-client cynara-creds-gdbus cynara-session")
 
-#IF (TIZEN_TELEPHONY_ENABLED)
-       #MESSAGE("-DENABLE_TELEPHONY")
-       #ADD_DEFINITIONS(-DENABLE_TELEPHONY)
-#ENDIF (TIZEN_TELEPHONY_ENABLED)
+IF (TIZEN_TELEPHONY_ENABLED)
+       MESSAGE("-DENABLE_TELEPHONY")
+       ADD_DEFINITIONS(-DENABLE_TELEPHONY)
+ENDIF (TIZEN_TELEPHONY_ENABLED)
 
 INCLUDE(FindPkgConfig)
 pkg_check_modules(manager_pkges REQUIRED ${dependents})
@@ -53,7 +53,7 @@ IF("${ARCH}" MATCHES "^arm.*")
 ENDIF()
 
 # for addons
-ADD_DEFINITIONS("-DADDON_HCE_NDEF")
+#ADD_DEFINITIONS("-DADDON_HCE_NDEF")
 #ADD_DEFINITIONS("-DADDON_HCE_PPSE")
 #ADD_DEFINITIONS("-DADDON_HCE_TMONEY")
 
index 4bf2b68..a5a9deb 100755 (executable)
@@ -100,16 +100,16 @@ static void __hce_listener(net_nfc_target_handle_s *handle, int event,
 {
        switch (event) {
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
                __process_command(handle, data);
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
                break;
 
        default :
index 1de6c95..60a00ea 100755 (executable)
@@ -150,7 +150,7 @@ static void __process_command(net_nfc_target_handle_s *handle, data_s *cmd)
                if (apdu->p1 == NET_NFC_HCE_P1_SELECT_BY_NAME) {
                        if (memcmp(apdu->data, ndef_aid,
                                MIN(sizeof(ndef_aid), apdu->lc)) == 0) {
-                               DEBUG_SERVER_MSG("select ndef applet");
+                               DEBUG_ADDON_MSG("select ndef applet");
 
                                /* bt on */
                                /* fill bt address */
@@ -177,14 +177,14 @@ static void __process_command(net_nfc_target_handle_s *handle, data_s *cmd)
 
                        if (memcmp(apdu->data, cc_fid,
                                MIN(sizeof(cc_fid), apdu->lc)) == 0) {
-                               DEBUG_SERVER_MSG("select capability container");
+                               DEBUG_ADDON_MSG("select capability container");
 
                                selected_fid = cc_fid;
 
                                __send_response(handle, NET_NFC_HCE_SW_SUCCESS, NULL, 0);
                        } else if (memcmp(apdu->data, ndef_fid,
                                MIN(sizeof(ndef_fid), apdu->lc)) == 0) {
-                               DEBUG_SERVER_MSG("select ndef");
+                               DEBUG_ADDON_MSG("select ndef");
 
                                selected_fid = ndef_fid;
 
@@ -255,18 +255,18 @@ static void __nfc_addon_hce_ndef_listener(net_nfc_target_handle_s *handle,
 {
        switch (event) {
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
                selected_fid = NULL;
                selected_aid = NULL;
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
                __process_command(handle, data);
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
                selected_fid = NULL;
                selected_aid = NULL;
                break;
index a0bbc06..7a7b22f 100755 (executable)
@@ -164,7 +164,7 @@ static void __process_command(net_nfc_target_handle_s *handle, data_s *cmd)
                        uint8_t temp_label[] = { 'T', 'E', 'S', 'T' };
                        data_s label = { temp_label, sizeof(temp_label) };
 
-                       DEBUG_SERVER_MSG("select ppse applet");
+                       DEBUG_ADDON_MSG("select ppse applet");
 
                        len = __fill_fci(buffer, sizeof(buffer), &aid, &label, 1);
 
@@ -202,7 +202,7 @@ static void __process_command(net_nfc_target_handle_s *handle, data_s *cmd)
                        goto END;
                }
 
-               DEBUG_SERVER_MSG("ppse loopback");
+               DEBUG_ADDON_MSG("ppse loopback");
 
                if (apdu->le == 0) {
                        apdu->le = 255;
@@ -227,17 +227,17 @@ static void __nfc_addon_hce_ppse_listener(net_nfc_target_handle_s *handle,
 {
        switch (event) {
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
                selected = false;
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
                __process_command(handle, data);
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
                selected = false;
                break;
 
index 1b39b7d..2b01cc3 100644 (file)
@@ -100,7 +100,7 @@ static void __process_command(net_nfc_target_handle_s *handle, data_s *cmd)
                }
 
                if (memcmp(apdu->data, tmoney_aid, MIN(sizeof(tmoney_aid), apdu->lc)) == 0) {
-                       DEBUG_SERVER_MSG("select tmoney applet");
+                       DEBUG_ADDON_MSG("select tmoney applet");
 
                        selected = true;
 
@@ -128,7 +128,7 @@ static void __process_command(net_nfc_target_handle_s *handle, data_s *cmd)
                        goto END;
                }
 
-               DEBUG_SERVER_MSG("tmoney read");
+               DEBUG_ADDON_MSG("tmoney read");
 
                __send_response(handle, NET_NFC_HCE_SW_SUCCESS,
                        tmoney_uid,
@@ -149,17 +149,17 @@ static void __nfc_addon_hce_tmoney_listener(net_nfc_target_handle_s *handle,
 {
        switch (event) {
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED");
                selected = false;
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA");
                __process_command(handle, data);
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED :
-               INFO_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
+               DEBUG_ADDON_MSG("NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED");
                selected = false;
                break;
 
index ce2a67f..abe8562 100755 (executable)
@@ -51,6 +51,7 @@ void net_nfc_app_util_clean_storage(char* src_path);
 bool net_nfc_app_util_is_dir(const char* path_name);
 int net_nfc_app_util_appsvc_launch(const char *operation, const char *uri, const char *mime, const char *data);
 int net_nfc_app_util_launch_se_transaction_app(net_nfc_se_type_e se_type, uint8_t *aid, uint32_t aid_len, uint8_t *param, uint32_t param_len);
+int net_nfc_app_util_launch_se_off_host_apdu_service_app(net_nfc_se_type_e se_type, uint8_t *aid, uint32_t aid_len, uint8_t *param, uint32_t param_len);
 int net_nfc_app_util_encode_base64(uint8_t *buffer, uint32_t buf_len, char *result, uint32_t max_result);
 int net_nfc_app_util_decode_base64(const char *buffer, uint32_t buf_len, uint8_t *result, uint32_t *res_len);
 bool net_nfc_app_util_check_launch_state();
index 64a3a9f..5cf46c2 100755 (executable)
@@ -120,9 +120,12 @@ bool net_nfc_controller_secure_element_get_aid_table_size(int *AIDTableSize, net
 bool net_nfc_controller_secure_element_set_route_entry
        (net_nfc_se_entry_type_e type, net_nfc_se_tech_protocol_type_e value, net_nfc_se_type_e route, int power, net_nfc_error_e *result);
 
-bool net_nfc_controller_secure_element_set_clear_routing_entry
+bool net_nfc_controller_secure_element_clear_routing_entry
        (net_nfc_se_entry_type_e type, net_nfc_error_e *result);
 
+bool net_nfc_controller_secure_element_set_listen_tech_mask
+       (net_nfc_se_tech_protocol_type_e screen_state, net_nfc_error_e *result);
+
 bool net_nfc_controller_set_screen_state
        (net_nfc_screen_state_type_e screen_state, net_nfc_error_e *result);
 
index a2183fd..0a66c71 100755 (executable)
@@ -50,6 +50,9 @@ void net_nfc_server_restart_polling_loop(void);
 
 void net_nfc_server_force_polling_loop(void);
 
+void net_nfc_server_quit_nfc_manager_loop(void);
+
+
 void net_nfc_server_set_state(guint32 state);
 
 void net_nfc_server_unset_state(guint32 state);
@@ -59,4 +62,7 @@ guint32 net_nfc_server_get_state(void);
 void net_nfc_server_controller_run_dispatch_loop();
 void net_nfc_server_controller_quit_dispatch_loop();
 
+void net_nfc_server_controller_init_sync();
+
+
 #endif //__NET_NFC_SERVER_COMMON_H__
index 3f5f2d1..a11ffde 100755 (executable)
@@ -64,6 +64,9 @@ void net_nfc_server_gdbus_cleanup_client_context(const char *id);
 net_nfc_client_context_info_t *net_nfc_server_gdbus_get_client_context(
        const char *id);
 
+net_nfc_client_context_info_t *net_nfc_server_gdbus_get_client_context_by_pid(
+       pid_t pid);
+
 size_t net_nfc_server_gdbus_get_client_count();
 
 void net_nfc_server_gdbus_for_each_client_context(
@@ -93,6 +96,7 @@ bool net_nfc_app_util_check_transaction_fg_dispatch();
 void net_nfc_server_gdbus_increase_se_count(const char *id);
 void net_nfc_server_gdbus_decrease_se_count(const char *id);
 
+bool net_nfc_server_gdbus_is_server_busy_no_lock();
 bool net_nfc_server_gdbus_is_server_busy();
 
 net_nfc_error_e net_nfc_server_gdbus_set_transaction_fg_dispatch(
index 26aa244..96998b4 100755 (executable)
@@ -69,4 +69,11 @@ net_nfc_error_e net_nfc_server_handover_wps_do_connect(
        net_nfc_server_handover_process_carrier_cb cb,
        void *user_param);
 
+/* wifi-direct */
+net_nfc_error_e net_nfc_server_handover_wfd_do_pairing(
+       net_nfc_ch_carrier_s *carrier,
+       net_nfc_server_handover_process_carrier_cb cb,
+       void *user_param);
+
+
 #endif //__NET_NFC_SERVER_HANDOVER_INTERNAL_H__
index 80bccd1..765e801 100755 (executable)
@@ -46,4 +46,7 @@ void net_nfc_server_hce_deinit(void);
 
 void net_nfc_server_hce_apdu_received(void *info);
 
+void net_nfc_server_hce_handle_send_apdu_response(
+       net_nfc_target_handle_s *handle, data_s *response);
+
 #endif //__NET_NFC_SERVER_SE_H__
diff --git a/src/manager/include/net_nfc_server_hce_ipc.h b/src/manager/include/net_nfc_server_hce_ipc.h
new file mode 100755 (executable)
index 0000000..596f721
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NET_NFC_SERVER_HCE_IPC_H__
+#define __NET_NFC_SERVER_HCE_IPC_H__
+
+#include <gio/gio.h>
+
+#include "net_nfc_typedef_internal.h"
+
+
+/******************************************************************************/
+/* internal */
+bool net_nfc_server_hce_ipc_init();
+
+void net_nfc_server_hce_ipc_deinit();
+
+
+bool net_nfc_server_hce_send_to_client(const char *id, int type,
+       net_nfc_target_handle_s *handle, data_s *data);
+
+bool net_nfc_server_hce_send_to_all_client(int type,
+       net_nfc_target_handle_s *handle, data_s *data);
+
+#endif //__NET_NFC_SERVER_HCE_IPC_H__
index a14e5be..7ddc95e 100755 (executable)
@@ -18,7 +18,6 @@
 #define __NET_NFC_SERVER_ROUTE_TABLE_H__
 
 #include <gio/gio.h>
-#include <vconf.h>
 
 #include "net_nfc_typedef_internal.h"
 
@@ -57,6 +56,9 @@ void net_nfc_server_route_table_deinit();
 void net_nfc_server_route_table_iterate_handler(
        net_nfc_server_route_table_handler_iter_cb cb, void *user_data);
 
+void net_nfc_server_route_table_iterate_handler_activated_last(
+       net_nfc_server_route_table_handler_iter_cb cb, void *user_data);
+
 route_table_handler_t *net_nfc_server_route_table_find_handler(
        const char *package);
 
@@ -66,6 +68,9 @@ net_nfc_error_e net_nfc_server_route_table_add_handler(const char *id,
 net_nfc_error_e net_nfc_server_route_table_del_handler(const char *id,
        const char *package, bool force);
 
+net_nfc_error_e net_nfc_server_route_table_update_handler_id(
+       const char *package, const char *id);
+
 net_nfc_error_e net_nfc_server_route_table_set_handler_activation(
        const char *package, net_nfc_card_emulation_category_t category);
 
@@ -101,7 +106,10 @@ net_nfc_error_e net_nfc_server_route_table_del_aid(const char *id, const char *p
 net_nfc_error_e net_nfc_server_route_table_del_aids(const char *id, const char *package,
        bool force);
 
-void net_nfc_server_route_table_iterate_aid(const char *package,
+void net_nfc_server_route_table_iterate_aids(
+       net_nfc_server_route_table_aid_iter_cb cb, void *user_data);
+
+void net_nfc_server_route_table_iterate_handler_aids(const char *package,
        net_nfc_server_route_table_aid_iter_cb cb, void *user_data);
 
 aid_info_t *net_nfc_server_route_table_find_aid_by_id(const char *package,
@@ -115,7 +123,7 @@ net_nfc_error_e net_nfc_server_route_table_add_aid_by_id(const char *id,
 net_nfc_error_e net_nfc_server_route_table_del_aid_by_id(const char *id,
        const char *aid, bool force);
 
-void net_nfc_server_route_table_iterate_aid_by_id(const char *id,
+void net_nfc_server_route_table_iterate_aids_by_id(const char *id,
        net_nfc_server_route_table_aid_iter_cb cb, void *user_data);
 
 
@@ -133,6 +141,22 @@ net_nfc_error_e net_nfc_server_route_table_delete_aids_from_db(
 void net_nfc_server_route_table_update_category_handler(const char *package,
        net_nfc_card_emulation_category_t category);
 
-net_nfc_error_e net_nfc_server_route_table_do_update(void);
+net_nfc_error_e net_nfc_server_route_table_do_update(bool routing);
+
+
+net_nfc_error_e net_nfc_server_route_table_get_storage_info(
+       net_nfc_card_emulation_category_t category, int *used, int *max);
+
+route_table_handler_t *net_nfc_server_route_table_get_preferred_handler();
+
+void net_nfc_server_route_table_set_preferred_handler(route_table_handler_t *handler);
+
+net_nfc_error_e net_nfc_server_route_table_update_preferred_handler();
+
+bool net_nfc_server_route_table_is_allowed_preferred_handler(route_table_handler_t *data);
+
+void net_nfc_server_route_table_unset_preferred_handler_by_id(const char* id);
+
+void net_nfc_server_route_table_preferred_handler_dump();
 
 #endif //__NET_NFC_SERVER_ROUTE_TABLE_H__
index febe8a6..733dd6b 100644 (file)
@@ -60,5 +60,18 @@ void net_nfc_server_se_transaction_received(void *info);
 
 void net_nfc_server_se_rf_field_on(void *info);
 
+void net_nfc_server_se_rf_field_off(void *info);
+
+void net_nfc_server_se_connected(void *info);
+
+bool net_nfc_server_se_notify_lcd_state_changed(net_nfc_screen_state_type_e state);
+
+void net_nfc_server_se_convert_to_binary(uint8_t *orig, size_t len,
+       uint8_t **dest, size_t *destLen);
+
+void net_nfc_server_se_create_deactivate_apdu_command(uint8_t *orig, uint8_t **dest, size_t *destLen);
+
+void net_nfc_server_se_deactivate_card(void);
+
 
 #endif //__NET_NFC_SERVER_SE_H__
index 0962f78..c6b07e4 100755 (executable)
@@ -24,4 +24,6 @@ bool net_nfc_check_csc_vconf(void);
 
 bool net_nfc_check_start_polling_vconf(void);
 
+void net_nfc_server_vconf_set_screen_on_flag(bool flag);
+
 #endif //__NET_NFC_SERVER_VCONF_H__
diff --git a/src/manager/include/net_nfc_service_access_control.h b/src/manager/include/net_nfc_service_access_control.h
new file mode 100755 (executable)
index 0000000..51d36d9
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NET_NFC_MANAGER_ACCESS_CONTROL_H__
+#define __NET_NFC_MANAGER_ACCESS_CONTROL_H__
+
+typedef enum
+{
+       NET_NFC_ACCESS_CONTROL_PLATFORM = 0x01,
+       NET_NFC_ACCESS_CONTROL_UICC = 0x02,
+       NET_NFC_ACCESS_CONTROL_ESE = 0x04,
+} net_nfc_access_control_type_e;
+
+bool net_nfc_service_check_access_control(pid_t pid,
+       net_nfc_access_control_type_e type);
+
+bool net_nfc_service_access_control_is_authorized_nfc_access(uint8_t se_type,
+       const char *package, const uint8_t *aid, const uint32_t len);
+
+bool net_nfc_service_access_control_is_authorized_nfc_access_by_pid(
+       uint8_t se_type, pid_t pid, const uint8_t *aid, const uint32_t len);
+
+#endif //__NET_NFC_MANAGER_ACCESS_CONTROL_H__
index fe02082..0e9d95f 100755 (executable)
@@ -79,7 +79,7 @@ static void _process_carrier_record_cb(net_nfc_error_e result,
 
        if (result == NET_NFC_OK)
        {
-               INFO_MSG("process carrier record success");
+               DEBUG_SERVER_MSG("process carrier record success");
        }
        else
        {
@@ -155,6 +155,11 @@ static net_nfc_error_e __process_ch_message(net_nfc_ch_message_s *message,
 
                                net_nfc_server_handover_emit_started_signal(NULL, temp);
 
+                               /*Implement to connct with wifi direct*/
+                               net_nfc_server_handover_wfd_do_pairing(
+                                       carrier,
+                                       _process_carrier_record_cb,
+                                       temp);
                        }
                }
        }
@@ -817,7 +822,7 @@ void net_nfc_app_util_aul_launch_app(char* package_name, bundle* kb)
        }
        else
        {
-               DEBUG_SERVER_MSG("success to launch [%s]", package_name);
+               SECURE_MSG("success to launch [%s]", package_name);
        }
 }
 
@@ -833,25 +838,25 @@ int net_nfc_app_util_appsvc_launch(const char *operation, const char *uri, const
 
        if (operation != NULL && strlen(operation) > 0)
        {
-               DEBUG_SERVER_MSG("operation : %s", operation);
+               SECURE_MSG("operation : %s", operation);
                appsvc_set_operation(bd, operation);
        }
 
        if (uri != NULL && strlen(uri) > 0)
        {
-               DEBUG_SERVER_MSG("uri : %s", uri);
+               SECURE_MSG("uri : %s", uri);
                appsvc_set_uri(bd, uri);
        }
 
        if (mime != NULL && strlen(mime) > 0)
        {
-               DEBUG_SERVER_MSG("mime : %s", mime);
+               SECURE_MSG("mime : %s", mime);
                appsvc_set_mime(bd, mime);
        }
 
        if (data != NULL && strlen(data) > 0)
        {
-               DEBUG_SERVER_MSG("data : %s", data);
+               SECURE_MSG("data : %s", data);
                appsvc_add_data(bd, "data", data);
        }
 
@@ -940,7 +945,61 @@ int net_nfc_app_util_launch_se_transaction_app(net_nfc_se_type_e se_type, uint8_
                                break;
                }
 
-               DEBUG_SERVER_MSG("aid_string : %s", aid_string);
+               SECURE_MSG("aid_string : %s", aid_string);
+               appsvc_set_uri(bd, aid_string);
+       }
+
+       if (param != NULL && param_len > 0)
+       {
+               char param_string[1024] = { 0, };
+               data_s temp = { param, param_len };
+
+               net_nfc_util_binary_to_hex_string(&temp, param_string, sizeof(param_string));
+
+               SECURE_MSG("param_string : %s", param_string);
+               appsvc_add_data(bd, "data", param_string);
+       }
+
+       appsvc_run_service(bd, 0, NULL, NULL);
+
+       bundle_free(bd);
+
+       return 0;
+}
+
+int net_nfc_app_util_launch_se_off_host_apdu_service_app(net_nfc_se_type_e se_type, uint8_t *aid, uint32_t aid_len, uint8_t *param, uint32_t param_len)
+{
+       bundle *bd = NULL;
+
+       /* launch */
+       bd = bundle_create();
+
+       appsvc_set_operation(bd, "http://tizen.org/appcontrol/operation/nfc/card_emulation/off_host_apdu_service");
+
+       /* convert aid to aid string */
+       if (aid != NULL && aid_len > 0)
+       {
+               char temp_string[1024] = { 0, };
+               char aid_string[1024] = { 0, };
+               data_s temp = { aid, aid_len };
+
+               net_nfc_util_binary_to_hex_string(&temp, temp_string, sizeof(temp_string));
+
+               switch(se_type)
+               {
+                       case NET_NFC_SE_TYPE_UICC:
+                               snprintf(aid_string, sizeof(aid_string), "nfc://secure/SIM1/aid/%s", temp_string);
+                               break;
+
+                       case NET_NFC_SE_TYPE_ESE:
+                               snprintf(aid_string, sizeof(aid_string), "nfc://secure/eSE/aid/%s", temp_string);
+                               break;
+                       default:
+                               snprintf(aid_string, sizeof(aid_string), "nfc://secure/aid/%s", temp_string);
+                               break;
+               }
+
+               SECURE_MSG("aid_string : %s", aid_string);
                appsvc_set_uri(bd, aid_string);
        }
 
@@ -951,7 +1010,7 @@ int net_nfc_app_util_launch_se_transaction_app(net_nfc_se_type_e se_type, uint8_
 
                net_nfc_util_binary_to_hex_string(&temp, param_string, sizeof(param_string));
 
-               DEBUG_SERVER_MSG("param_string : %s", param_string);
+               SECURE_MSG("param_string : %s", param_string);
                appsvc_add_data(bd, "data", param_string);
        }
 
index d44a0dc..db52926 100755 (executable)
@@ -24,6 +24,7 @@
 #include <errno.h>
 
 #include <dd-display.h>/*for pm lock*/
+#include <device/power.h>
 
 #include "net_nfc_oem_controller.h"
 #include "net_nfc_controller_internal.h"
@@ -49,7 +50,7 @@ static void *net_nfc_controller_load_file(const char *dir_path,
        bool (*onload)(net_nfc_oem_interface_s *interfaces);
 
        snprintf(path, PATH_MAX, "%s/%s", dir_path, filename);
-       DEBUG_SERVER_MSG("path : %s", path);
+       SECURE_MSG("path : %s", path);
 
        if (stat(path, &st) == -1) {
                DEBUG_ERR_MSG("stat failed : file not found");
@@ -109,7 +110,7 @@ void *net_nfc_controller_onload()
        dirp = opendir(NFC_MANAGER_MODULEDIR);
        if (dirp == NULL)
        {
-               DEBUG_ERR_MSG("Can not open directory %s",
+               SECURE_MSG("Can not open directory %s",
                                NFC_MANAGER_MODULEDIR);
                return NULL;
        }
@@ -150,13 +151,13 @@ void *net_nfc_controller_onload()
 
        if (handle)
        {
-               DEBUG_SERVER_MSG("loaded default plugin : %s",
+               SECURE_MSG("loaded default plugin : %s",
                                NET_NFC_DEFAULT_PLUGIN);
                return handle;
        }
        else
        {
-               DEBUG_ERR_MSG("can not load default plugin : %s",
+               SECURE_MSG("can not load default plugin : %s",
                                NET_NFC_DEFAULT_PLUGIN);
                return NULL;
        }
@@ -331,16 +332,21 @@ bool net_nfc_controller_set_secure_element_mode(net_nfc_secure_element_type_e el
 
 bool net_nfc_controller_secure_element_open(net_nfc_secure_element_type_e element_type, net_nfc_target_handle_s **handle, net_nfc_error_e *result)
 {
-       int ret_val = 0;
-
-       ret_val = display_lock_state(LCD_NORMAL, GOTO_STATE_NOW, 0);
-       if (ret_val < 0) {
-               DEBUG_ERR_MSG("display_lock_state failed, [%d]", ret_val);
-       }
-
        if (g_interface.secure_element_open != NULL)
        {
-               return g_interface.secure_element_open(element_type, handle, result);
+               bool ret;
+
+               ret = g_interface.secure_element_open(element_type, handle, result);
+               if (ret == true) {
+                       int ret_val;
+
+                       ret_val = device_power_request_lock(POWER_LOCK_CPU, 300000);
+                       if (ret_val < 0) {
+                               DEBUG_ERR_MSG("device_power_request_lock failed, [%d]", ret_val);
+                       }
+               }
+
+               return ret;
        }
        else
        {
@@ -380,11 +386,11 @@ bool net_nfc_controller_secure_element_send_apdu(net_nfc_target_handle_s *handle
 
 bool net_nfc_controller_secure_element_close(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
 {
-       int ret_val = 0;
+       int ret_val;
 
-       ret_val = display_unlock_state(LCD_NORMAL, PM_RESET_TIMER);
+       ret_val = device_power_release_lock(POWER_LOCK_CPU);
        if (ret_val < 0) {
-               DEBUG_ERR_MSG("display_unlock_state failed, [%d]", ret_val);
+               DEBUG_ERR_MSG("device_power_release_lock failed, [%d]", ret_val);
        }
 
        if (g_interface.secure_element_close != NULL)
@@ -415,15 +421,21 @@ bool net_nfc_controller_check_target_presence(net_nfc_target_handle_s *handle, n
 
 bool net_nfc_controller_connect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
 {
-       int ret_val = 0;
+       if (g_interface.connect != NULL)
+       {
+               bool ret;
 
-       ret_val = display_lock_state(LCD_NORMAL, GOTO_STATE_NOW, 0);
+               ret = g_interface.connect(handle, result);
+               if (ret == true) {
+                       int ret_val = 0;
 
-       DEBUG_SERVER_MSG("net_nfc_controller_connect display_lock_state [%d]!!", ret_val);
+                       ret_val = device_power_request_lock(POWER_LOCK_CPU, 20000);
+                       if (ret_val < 0) {
+                               DEBUG_ERR_MSG("device_power_request_lock failed, [%d]", ret_val);
+                       }
+               }
 
-       if (g_interface.connect != NULL)
-       {
-               return g_interface.connect(handle, result);
+               return ret;
        }
        else
        {
@@ -435,11 +447,12 @@ bool net_nfc_controller_connect(net_nfc_target_handle_s *handle, net_nfc_error_e
 
 bool net_nfc_controller_disconnect(net_nfc_target_handle_s *handle, net_nfc_error_e *result)
 {
-       int ret_val = 0;
+       int ret_val;
 
-       ret_val = display_unlock_state(LCD_NORMAL, PM_RESET_TIMER);
-
-       DEBUG_ERR_MSG("net_nfc_controller_disconnect display_lock_state [%d]!!", ret_val);
+       ret_val = device_power_release_lock(POWER_LOCK_CPU);
+       if (ret_val < 0) {
+               DEBUG_ERR_MSG("device_power_release_lock failed, [%d]", ret_val);
+       }
 
        if (g_interface.disconnect != NULL)
        {
@@ -844,13 +857,8 @@ void net_nfc_controller_llcp_connected_cb(net_nfc_llcp_socket_t socket,
 
 bool net_nfc_controller_llcp_connect_by_url(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t *service_access_name, net_nfc_error_e *result, net_nfc_service_llcp_cb cb, void *user_param)
 {
-       int ret_val = 0;
        bool ret;
 
-       ret_val = display_lock_state(LCD_NORMAL, GOTO_STATE_NOW, 0);
-
-       DEBUG_SERVER_MSG("net_nfc_controller_llcp_connect_by_url display_lock_state [%d]!!", ret_val);
-
        if (g_interface.connect_llcp_by_url != NULL)
        {
                net_nfc_llcp_param_t *param = NULL;
@@ -867,9 +875,17 @@ bool net_nfc_controller_llcp_connect_by_url(net_nfc_target_handle_s *handle, net
                param->user_param = user_param;
 
                ret = g_interface.connect_llcp_by_url(handle, socket, service_access_name, result, param);
-               if (ret != true) {
+               if (ret == true) {
+                       int ret_val;
+
+                       ret_val = device_power_request_lock(POWER_LOCK_CPU, 20000);
+                       if (ret_val < 0) {
+                               DEBUG_ERR_MSG("device_power_request_lock failed, [%d]", ret_val);
+                       }
+               } else {
                        _remove_socket_info(socket);
                }
+
                return ret;
        }
        else
@@ -882,14 +898,9 @@ bool net_nfc_controller_llcp_connect_by_url(net_nfc_target_handle_s *handle, net
 
 bool net_nfc_controller_llcp_connect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, uint8_t service_access_point, net_nfc_error_e *result, net_nfc_service_llcp_cb cb, void *user_param)
 {
-       int ret_val = 0;
-
-       ret_val = display_lock_state(LCD_NORMAL, GOTO_STATE_NOW, 0);
-
-       DEBUG_SERVER_MSG("net_nfc_controller_llcp_connect display_lock_state [%d]!!", ret_val);
-
        if (g_interface.connect_llcp != NULL)
        {
+               bool ret;
                net_nfc_llcp_param_t *param = NULL;
 
                _net_nfc_util_alloc_mem(param, sizeof(*param));
@@ -903,7 +914,17 @@ bool net_nfc_controller_llcp_connect(net_nfc_target_handle_s *handle, net_nfc_ll
                param->cb = cb;
                param->user_param = user_param;
 
-               return g_interface.connect_llcp(handle, socket, service_access_point, result, param);
+               ret = g_interface.connect_llcp(handle, socket, service_access_point, result, param);
+               if (ret == true) {
+                       int ret_val;
+
+                       ret_val = device_power_request_lock(POWER_LOCK_CPU, 20000);
+                       if (ret_val < 0) {
+                               DEBUG_ERR_MSG("device_power_request_lock failed, [%d]", ret_val);
+                       }
+               }
+
+               return ret;
        }
        else
        {
@@ -930,11 +951,12 @@ void net_nfc_controller_llcp_disconnected_cb(net_nfc_llcp_socket_t socket,
 
 bool net_nfc_controller_llcp_disconnect(net_nfc_target_handle_s *handle, net_nfc_llcp_socket_t socket, net_nfc_error_e *result, net_nfc_service_llcp_cb cb, void *user_param)
 {
-       int ret_val = 0;
-
-       ret_val = display_unlock_state(LCD_NORMAL, PM_RESET_TIMER);
+       int ret_val;
 
-       DEBUG_SERVER_MSG("net_nfc_controller_llcp_disconnect display_unlock_state [%d]!!", ret_val);
+       ret_val = device_power_release_lock(POWER_LOCK_CPU);
+       if (ret_val < 0) {
+               DEBUG_ERR_MSG("device_power_release_lock failed, [%d]", ret_val);
+       }
 
        if (g_interface.disconnect_llcp != NULL)
        {
@@ -1393,7 +1415,7 @@ bool net_nfc_controller_secure_element_set_route_entry
        }
 }
 
-bool net_nfc_controller_secure_element_set_clear_routing_entry
+bool net_nfc_controller_secure_element_clear_routing_entry
        (net_nfc_se_entry_type_e type, net_nfc_error_e *result)
 {
        if (g_interface.clear_routing_entry != NULL)
@@ -1407,6 +1429,21 @@ bool net_nfc_controller_secure_element_set_clear_routing_entry
        }
 }
 
+bool net_nfc_controller_secure_element_set_listen_tech_mask(net_nfc_se_tech_protocol_type_e value, net_nfc_error_e *result)
+{
+       if (g_interface.set_listen_tech_mask!= NULL)
+       {
+               return g_interface.set_listen_tech_mask(value , result);
+       }
+       else
+       {
+               *result = NET_NFC_UNKNOWN_ERROR;
+               DEBUG_SERVER_MSG("interface is null");
+               return false;
+       }
+}
+
+
 bool net_nfc_controller_set_screen_state(net_nfc_screen_state_type_e screen_state, net_nfc_error_e *result)
 {
        if (g_interface.set_screen_state!= NULL)
index 3bc4388..461c115 100755 (executable)
@@ -53,7 +53,8 @@ static bool net_nfc_check_mode_conditions();
 
 void net_nfc_manager_quit()
 {
-       DEBUG_MSG("net_nfc_manager_quit kill the nfc-manager daemon!!");
+       DEBUG_SERVER_MSG("net_nfc_manager_quit kill the nfc-manager daemon!!");
+
        if (loop != NULL) {
                g_main_loop_quit(loop);
        }
@@ -65,15 +66,19 @@ static void on_bus_acquired(GDBusConnection *connection,
 {
        gint state;
 
-       DEBUG_MSG("bus path : %s", path);
+       DEBUG_SERVER_MSG("bus path : %s", path);
 
        net_nfc_server_gdbus_init();
 
+       #if 0
        net_nfc_server_controller_init();
+       #else
+       net_nfc_server_controller_init_sync();
+       #endif
 
        if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
        {
-               DEBUG_MSG("VCONFKEY_NFC_STATE is not exist");
+               DEBUG_SERVER_MSG("VCONFKEY_NFC_STATE is not exist");
                net_nfc_manager_quit();
 
                return;
@@ -88,7 +93,7 @@ static void on_bus_acquired(GDBusConnection *connection,
 #ifndef ESE_ALWAYS_ON
        else if (use_daemon == TRUE)
        {
-               DEBUG_ERR_MSG("net_nfc_server_controller_deinit");
+               DEBUG_SERVER_MSG("exit process...");
                net_nfc_server_controller_deinit();
        }
 #endif
@@ -98,14 +103,14 @@ static void on_name_acquired(GDBusConnection *connection,
                        const gchar *name,
                        gpointer user_data)
 {
-       DEBUG_SERVER_MSG("name : %s", name);
+       SECURE_MSG("name : %s", name);
 }
 
 static void on_name_lost(GDBusConnection *connnection,
                        const gchar *name,
                        gpointer user_data)
 {
-       DEBUG_SERVER_MSG("name : %s", name);
+       SECURE_MSG("name : %s", name);
 
        net_nfc_manager_quit();
 }
@@ -120,6 +125,7 @@ int main(int argc, char *argv[])
        GOptionContext *option_context;
        GError *error = NULL;
        bool check_csc = 0;
+       int result;
 
        if (!g_thread_supported())
        {
@@ -206,7 +212,10 @@ EXIT :
                g_bus_unown_name(id);
        }
 
+       usleep(1000 * 10); // 10ms
+
        net_nfc_controller_unload(handle);
+       DEBUG_SERVER_MSG("END net_nfc_controller_unload");
 
        net_nfc_manager_fini_log();
 
index 3a5ab05..03680f2 100755 (executable)
@@ -48,31 +48,31 @@ void net_nfc_manager_util_play_sound(net_nfc_sound_type_e sound_type)
 
        if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &bSoundOn) != 0)
        {
-               DEBUG_MSG("vconf_get_bool failed for Sound");
+               DEBUG_SERVER_MSG("vconf_get_bool failed for Sound");
                return;
        }
 
        if (vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &bVibrationOn) != 0)
        {
-               DEBUG_MSG("vconf_get_bool failed for Vibration");
+               DEBUG_SERVER_MSG("vconf_get_bool failed for Vibration");
                return;
        }
 
        if ((sound_type > NET_NFC_TASK_ERROR) || (sound_type < NET_NFC_TASK_START))
        {
-               DEBUG_MSG("Invalid Sound Type");
+               DEBUG_SERVER_MSG("Invalid Sound Type");
                return;
        }
 
        if (bVibrationOn)
        {
-               DEBUG_MSG("Play Vibration");
+               DEBUG_SERVER_MSG("Play Vibration");
 
                if (FEEDBACK_ERROR_NONE == feedback_initialize())
                {
                        if (FEEDBACK_ERROR_NONE ==  feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_SIP))
                        {
-                               DEBUG_MSG("feedback_play_type success");
+                               DEBUG_SERVER_MSG("feedback_play_type success");
                        }
 
                        feedback_deinitialize();
@@ -83,7 +83,7 @@ void net_nfc_manager_util_play_sound(net_nfc_sound_type_e sound_type)
        {
                char *sound_path = NULL;
 
-               DEBUG_MSG("Play Sound");
+               DEBUG_SERVER_MSG("Play Sound");
 
                switch (sound_type)
                {
index 266b071..2d250ef 100755 (executable)
@@ -240,6 +240,7 @@ void net_nfc_server_gdbus_deinit(void)
        net_nfc_server_se_deinit();
        net_nfc_server_snep_deinit();
        net_nfc_server_system_handler_deinit();
+       net_nfc_server_hce_deinit();
 
        net_nfc_server_controller_thread_deinit();
 
index 42d4041..c20fce5 100755 (executable)
@@ -162,7 +162,7 @@ static void controller_target_detected_cb(void *info,
 
        g_assert(info != NULL);
 
-       INFO_MSG("check devType = [%d]", req->devType );
+       DEBUG_SERVER_MSG("check devType = [%d]", req->devType );
 
        if (req->request_type == NET_NFC_MESSAGE_SERVICE_RESTART_POLLING_LOOP)
        {
@@ -212,7 +212,7 @@ static void controller_se_transaction_cb(void *info,
        req->user_param = (uint32_t)user_context;
 
 
-       INFO_MSG("SE Transaction = [%d]", req->request_type );
+       DEBUG_SERVER_MSG("SE Transaction = [%d]", req->request_type );
 
        switch(req->request_type)
        {
@@ -228,7 +228,17 @@ static void controller_se_transaction_cb(void *info,
                net_nfc_server_se_rf_field_on(req);
                break;
 
+       case NET_NFC_MESSAGE_SE_CONNECTIVITY :
+               net_nfc_server_se_connected(req);
+               break;
+
+       case NET_NFC_MESSAGE_SE_FIELD_OFF :
+               net_nfc_server_se_rf_field_off(req);
+               break;
+
        default :
+               /* FIXME : should be removed when plugins would be fixed*/
+               _net_nfc_util_free_mem(info);
                break;
        }
 }
@@ -363,17 +373,24 @@ static void controller_init_thread_func(gpointer user_data)
 {
        net_nfc_error_e result;
 
-       if (net_nfc_controller_init(&result) == false)
-       {
-               DEBUG_ERR_MSG("net_nfc_controller_init failed, [%d]", result);
+       net_nfc_controller_is_ready(&result);
 
-               /* ADD TEMPORARY ABORT FOR DEBUG */
-               abort();
-//             net_nfc_manager_quit();
-               return;
+       if(result != NET_NFC_OK) {
+
+               DEBUG_ERR_MSG("net_nfc_controller_is_ready[%d]", result);
+
+               if (net_nfc_controller_init(&result) == false)
+               {
+                       DEBUG_ERR_MSG("net_nfc_controller_init failed, [%d]", result);
+
+                       /* ADD TEMPORARY ABORT FOR DEBUG */
+                       abort();
+       //              net_nfc_manager_quit();
+                       return;
+               }
        }
 
-       INFO_MSG("net_nfc_controller_init success, [%d]", result);
+       DEBUG_SERVER_MSG("net_nfc_controller_init success, [%d]", result);
 
        if (net_nfc_controller_register_listener(controller_target_detected_cb,
                                                controller_se_transaction_cb,
@@ -388,7 +405,7 @@ static void controller_init_thread_func(gpointer user_data)
                abort();
        }
 
-       INFO_MSG("net_nfc_contorller_register_listener success");
+       DEBUG_SERVER_MSG("net_nfc_contorller_register_listener success");
 
        result = net_nfc_server_llcp_set_config(NULL);
        if (result != NET_NFC_OK)
@@ -400,7 +417,7 @@ static void controller_init_thread_func(gpointer user_data)
                abort();
        }
 
-       INFO_MSG("net_nfc_server_llcp_set_config success");
+       DEBUG_SERVER_MSG("net_nfc_server_llcp_set_config success");
 }
 
 #ifndef ESE_ALWAYS_ON
@@ -417,12 +434,11 @@ static void controller_deinit_thread_func(gpointer user_data)
                return;
        }
 
-       INFO_MSG("net_nfc_controller_deinit success");
+       DEBUG_SERVER_MSG("net_nfc_controller_deinit success");
 
        net_nfc_manager_quit();
 }
 #endif
-
 static void restart_polling_loop_thread_func(gpointer user_data)
 {
        gint state = 0;
@@ -506,6 +522,24 @@ static void force_polling_loop_thread_func(gpointer user_data)
        return;
 }
 
+static void quit_nfc_manager_loop_thread_func(gpointer user_data)
+{
+       gint state;
+
+       if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
+       {
+               DEBUG_SERVER_MSG("VCONFKEY_NFC_STATE is not exist");
+
+       }
+       else
+       {
+               if (state != VCONFKEY_NFC_STATE_ON)
+                       net_nfc_manager_quit();
+               else
+                       DEBUG_SERVER_MSG("NFC is ON!! No kill daemon!!");
+       }
+}
+
 gboolean net_nfc_server_controller_thread_init(void)
 {
        GError *error = NULL;
@@ -666,7 +700,7 @@ void net_nfc_server_controller_run_dispatch_loop()
                return;
        }
 
-       WARN_MSG("START DISPATCH LOOP");
+       DEBUG_SERVER_MSG("START DISPATCH LOOP");
 
        controller_dispath_running = TRUE;
        while (controller_is_running && controller_dispath_running)
@@ -675,7 +709,7 @@ void net_nfc_server_controller_run_dispatch_loop()
 
                func_data = g_async_queue_try_pop(controller_async_queue);
                if (func_data != NULL) {
-                       WARN_MSG("DISPATCHED!!!");
+                       DEBUG_SERVER_MSG("DISPATCHED!!!");
                        if (func_data->func)
                                func_data->func(func_data->data);
 
@@ -685,7 +719,7 @@ void net_nfc_server_controller_run_dispatch_loop()
                }
        }
 
-       WARN_MSG("STOP DISPATCH LOOP");
+       DEBUG_SERVER_MSG("STOP DISPATCH LOOP");
 }
 
 void net_nfc_server_controller_quit_dispatch_loop()
@@ -713,6 +747,16 @@ void net_nfc_server_force_polling_loop(void)
        }
 }
 
+void net_nfc_server_quit_nfc_manager_loop(void)
+{
+       if (net_nfc_server_controller_async_queue_push_force(
+                                       quit_nfc_manager_loop_thread_func,
+                                       NULL) == FALSE)
+       {
+               DEBUG_ERR_MSG("Failed to push onto the queue");
+       }
+}
+
 void net_nfc_server_set_state(guint32 state)
 {
        if (state == NET_NFC_SERVER_IDLE)
@@ -730,3 +774,48 @@ guint32 net_nfc_server_get_state(void)
 {
        return server_state;
 }
+
+void net_nfc_server_controller_init_sync(void)
+{
+       net_nfc_error_e result;
+
+       if (net_nfc_controller_init(&result) == false)
+       {
+               DEBUG_ERR_MSG("net_nfc_controller_init failed, [%d]", result);
+
+               /* ADD TEMPORARY ABORT FOR DEBUG */
+               abort();
+//             net_nfc_manager_quit();
+               return;
+       }
+
+       DEBUG_SERVER_MSG("net_nfc_controller_init success, [%d]", result);
+
+       if (net_nfc_controller_register_listener(controller_target_detected_cb,
+                                               controller_se_transaction_cb,
+                                               controller_llcp_event_cb,
+                                               controller_hce_apdu_cb,
+                                               &result) == false)
+       {
+               DEBUG_ERR_MSG("net_nfc_contorller_register_listener failed [%d]",
+                               result);
+
+               /* ADD TEMPORARY ABORT FOR DEBUG */
+               abort();
+       }
+
+       DEBUG_SERVER_MSG("net_nfc_contorller_register_listener success");
+
+       result = net_nfc_server_llcp_set_config(NULL);
+       if (result != NET_NFC_OK)
+       {
+               DEBUG_ERR_MSG("net_nfc_server_llcp_set config failed, [%d]",
+                       result);
+
+               /* ADD TEMPORARY ABORT FOR DEBUG */
+               abort();
+       }
+
+       DEBUG_SERVER_MSG("net_nfc_server_llcp_set_config success");
+}
+
index 2a42b04..ecf32e4 100755 (executable)
@@ -27,6 +27,8 @@
 #include "net_nfc_util_gdbus_internal.h"
 #include "net_nfc_server.h"
 #include "net_nfc_server_context_internal.h"
+#include "net_nfc_server_common.h"
+#include "net_nfc_server_route_table.h"
 
 #ifdef USE_CYNARA
 #include "cynara-client.h"
@@ -51,7 +53,7 @@ static void _on_client_detached(gpointer data, gpointer user_data)
 {
        net_nfc_server_gdbus_on_client_detached_cb cb = data;
 
-       DEBUG_MSG("invoke releasing callbacks");
+       DEBUG_SERVER_MSG("invoke releasing callbacks");
 
        if (cb != NULL) {
                cb((net_nfc_client_context_info_t *)user_data);
@@ -193,7 +195,7 @@ bool net_nfc_server_gdbus_check_privilege(GDBusMethodInvocation *invocation, net
 
        const char *id = g_dbus_method_invocation_get_sender(invocation);
 
-       INFO_MSG("check the id of the gdbus sender = [%s]", id);
+       DEBUG_SERVER_MSG("check the id of the gdbus sender = [%s]",id);
 
        net_nfc_server_gdbus_add_client_context(id,
                        NET_NFC_CLIENT_ACTIVE_STATE);
@@ -247,6 +249,31 @@ net_nfc_client_context_info_t *net_nfc_server_gdbus_get_client_context(
        return result;
 }
 
+net_nfc_client_context_info_t *net_nfc_server_gdbus_get_client_context_by_pid(
+       pid_t pid)
+{
+       net_nfc_client_context_info_t *result = NULL;
+       char *key;
+       net_nfc_client_context_info_t *value;
+       GHashTableIter iter;
+
+       pthread_mutex_lock(&context_lock);
+
+       g_hash_table_iter_init(&iter, client_contexts);
+
+       while (g_hash_table_iter_next(&iter, (gpointer *)&key,
+               (gpointer *)&value) == true) {
+               if (value->pid == pid) {
+                       result = value;
+                       break;
+               }
+       }
+
+       pthread_mutex_unlock(&context_lock);
+
+       return result;
+}
+
 void net_nfc_server_gdbus_add_client_context(const char *id,
        client_state_e state)
 {
@@ -286,7 +313,7 @@ void net_nfc_server_gdbus_add_client_context(const char *id,
        }
        else
        {
-               INFO_MSG("we already have this client in our context!!");
+               DEBUG_SERVER_MSG("we already have this client in our context!!");
        }
 
        pthread_mutex_unlock(&context_lock);
@@ -309,12 +336,14 @@ void net_nfc_server_gdbus_cleanup_client_context(const char *id)
                DEBUG_SERVER_MSG("current client count = [%d]",
                        net_nfc_server_gdbus_get_client_count_no_lock());
 
-//             /* TODO : exit when no client */
-//             if (net_nfc_server_gdbus_get_client_count_no_lock() == 0)
-//             {
-//                     /* terminate service */
-//                     net_nfc_manager_quit();
-//             }
+               net_nfc_server_route_table_unset_preferred_handler_by_id(id);
+
+               /* TODO : exit when no client */
+               if (net_nfc_server_gdbus_get_client_count_no_lock() == 0)
+               {
+                       DEBUG_SERVER_MSG("put the nfc-manager into queue");
+                       net_nfc_server_quit_nfc_manager_loop();
+               }
        }
 
        pthread_mutex_unlock(&context_lock);
@@ -554,12 +583,10 @@ void net_nfc_server_gdbus_decrease_se_count(const char *id)
        pthread_mutex_unlock(&context_lock);
 }
 
-bool net_nfc_server_gdbus_is_server_busy()
+bool net_nfc_server_gdbus_is_server_busy_no_lock()
 {
        bool result = false;
 
-       pthread_mutex_lock(&context_lock);
-
        if (g_hash_table_size(client_contexts) > 0) {
                GHashTableIter iter;
                char *id;
@@ -575,6 +602,17 @@ bool net_nfc_server_gdbus_is_server_busy()
                }
        }
 
+       return result;
+}
+
+bool net_nfc_server_gdbus_is_server_busy()
+{
+       bool result;
+
+       pthread_mutex_lock(&context_lock);
+
+       result = net_nfc_server_gdbus_is_server_busy_no_lock();
+
        pthread_mutex_unlock(&context_lock);
 
        return result;
diff --git a/src/manager/net_nfc_server_handover_wfd.c b/src/manager/net_nfc_server_handover_wfd.c
new file mode 100755 (executable)
index 0000000..1fdb8c9
--- /dev/null
@@ -0,0 +1,277 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*for wifi-direct*/
+#include <wifi-direct.h>
+
+#include "net_nfc_debug_internal.h"
+#include "net_nfc_util_internal.h"
+#include "net_nfc_util_ndef_message.h"
+#include "net_nfc_util_ndef_record.h"
+#include "net_nfc_util_handover.h"
+#include "net_nfc_server_handover_internal.h"
+#include "net_nfc_server_llcp.h"
+
+/*For Wifi-direct*/
+#define Size_of_MAC_Address 18
+
+typedef struct _net_nfc_handover_wfd_process_context_t
+{
+       bool already_on;
+       net_nfc_error_e result;
+       net_nfc_server_handover_process_carrier_cb cb;
+       net_nfc_carrier_config_s *config;
+       void *user_param;
+       char *mac_address;
+}
+net_nfc_handover_wfd_process_context_t;
+
+typedef struct _device_type_t
+{
+       uint16_t cat_id;
+       uint32_t oui;
+       uint16_t sub_cat_id;
+}
+__attribute__((packed)) device_type_t;
+
+typedef struct _device_info_attr_t
+{
+       uint8_t address[6];
+       uint16_t config_methods;
+       device_type_t primary_device;
+       uint16_t secondary_devices_num;
+       device_type_t secondary_devices[0];
+}
+__attribute__((packed)) device_info_attr_t;
+
+static void set_mac_address(char *data, uint8_t *origin_data)
+{
+       int i, len = 0;
+
+       for (i = 0; i < 6; i++)
+       {
+               len += sprintf(data + len, "%02x", origin_data[i]);
+
+               //DEBUG_SERVER_MSG("check temp [%s]", data);
+
+               if (i != 5)
+                       len += sprintf(data + len, ":");
+       }
+
+       SECURE_MSG("check mac_address [%s]", data);
+}
+
+static bool _cb_discovered_peers(wifi_direct_discovered_peer_info_s *peer,
+       void *user_data)
+{
+       net_nfc_handover_wfd_process_context_t *context =
+               (net_nfc_handover_wfd_process_context_t *)user_data;
+       bool result = true;
+
+       if (peer->mac_address != NULL && context->mac_address != NULL)
+       {
+               SECURE_MSG("mac_address [%s]", context->mac_address);
+               SECURE_MSG("peer->mac_address [%s]", peer->mac_address);
+
+               /* Check mac address */
+               if (strncasecmp(peer->mac_address, context->mac_address,
+                       strlen(context->mac_address)) == 0)
+               {
+                       DEBUG_SERVER_MSG("wifi_direct_connect");
+                       wifi_direct_connect(context->mac_address);
+
+                       context->cb(context->result,
+                               NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P,
+                               NULL, context->user_param);
+
+                       g_free(context->mac_address);
+
+                       if (context->config != NULL)
+                       {
+                               net_nfc_util_free_carrier_config(context->config);
+                       }
+
+                       _net_nfc_util_free_mem(context);
+
+                       result = false; /* break iterate */
+               }
+               else
+               {
+                       DEBUG_SERVER_MSG("I have to find other wifi direct");
+               }
+       }
+
+       return result;
+}
+
+static void _cb_activation(int error_code,
+       wifi_direct_device_state_e device_state, void *user_data)
+{
+       int result;
+
+       DEBUG_SERVER_MSG("Call _cb_activation [%p]", user_data);
+
+       switch (device_state)
+       {
+       case WIFI_DIRECT_DEVICE_STATE_ACTIVATED :
+               {
+                       DEBUG_SERVER_MSG("event -WIFI_DIRECT_DEVICE_STATE_ACTIVATED");
+
+                       result = wifi_direct_start_discovery_specific_channel(
+                               FALSE, 0, WIFI_DIRECT_DISCOVERY_FULL_SCAN);
+                       if (WIFI_DIRECT_ERROR_NOT_PERMITTED == result)
+                       {
+                               DEBUG_SERVER_MSG("WIFI_DIRECT_ERROR_NOT_PERMITTED");
+
+                               result = wifi_direct_foreach_discovered_peers(
+                                       _cb_discovered_peers, (void*)user_data);
+                       }
+               }
+               break;
+
+       case WIFI_DIRECT_DEVICE_STATE_DEACTIVATED :
+               {
+                       DEBUG_SERVER_MSG("event - WIFI_DIRECT_DEVICE_STATE_DEACTIVATED");
+               }
+               break;
+
+       default:
+               break;
+       }
+}
+
+static void _cb_discovery(int error_code,
+       wifi_direct_discovery_state_e discovery_state, void *user_data)
+{
+       int result;
+
+       DEBUG_SERVER_MSG("Call _cb_discovery");
+
+       switch (discovery_state)
+       {
+       case WIFI_DIRECT_DISCOVERY_STARTED :
+               DEBUG_SERVER_MSG("event - WIFI_DIRECT_DISCOVERY_STARTED");
+               break;
+
+       case WIFI_DIRECT_DISCOVERY_FOUND :
+               {
+                       DEBUG_SERVER_MSG("event - WIFI_DIRECT_DISCOVERY_FOUND");
+
+                       result = wifi_direct_foreach_discovered_peers(
+                               _cb_discovered_peers, (void*)user_data);
+
+                       DEBUG_SERVER_MSG("wifi_direct_foreach_discovered_peers() result=[0x%x]", result);
+               }
+               break;
+
+       default:
+               break;
+       }
+}
+
+static int handover_wfd_init(net_nfc_handover_wfd_process_context_t *user_data)
+{
+       int result;
+
+       DEBUG_SERVER_MSG("handover_wfd_init");
+
+       result = wifi_direct_initialize();
+       if (WIFI_DIRECT_ERROR_ALREADY_INITIALIZED == result)
+       {
+               DEBUG_SERVER_MSG("WIFI_DIRECT_ERROR_ALREADY_INITIALIZED!!");
+               return NET_NFC_OK;
+       }
+
+       if (WIFI_DIRECT_ERROR_NONE != result)
+       {
+               DEBUG_SERVER_MSG("wifi_direct_initialize failed [0x%x]!!", result);
+               return NET_NFC_UNKNOWN_ERROR;
+       }
+
+       /* set callback for active*/
+       wifi_direct_set_device_state_changed_cb(_cb_activation, user_data);
+
+       wifi_direct_set_discovery_state_changed_cb(_cb_discovery, user_data);
+
+       /* wifi-direct initialize */
+       result = wifi_direct_activate();
+
+       DEBUG_SERVER_MSG("wifi_direct_activate");
+
+       /*we need to check wifi state??*/
+       //wifi_direct_set_connection_state_changed_cb(_cb_connection, ad);
+       return NET_NFC_OK;
+}
+
+net_nfc_error_e _net_nfc_server_handover_wifi_direct_pairing(
+       net_nfc_handover_wfd_process_context_t *context)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       uint16_t len = 0;
+       uint8_t *data = NULL;
+
+       result = net_nfc_util_get_carrier_config_property(context->config,
+               NET_NFC_WIFI_P2P_ATTRIBUTE_DEVICE_INFO, &len, &data);
+       if (result == NET_NFC_OK) {
+               device_info_attr_t *info = (device_info_attr_t *)data;
+
+               context->mac_address = (char *)malloc(Size_of_MAC_Address * sizeof(char));
+               set_mac_address(context->mac_address, info->address);
+
+               DEBUG_SERVER_MSG("mac address= [%s]", context->mac_address);
+
+               handover_wfd_init(context);
+       } else {
+               DEBUG_ERR_MSG("net_nfc_util_get_carrier_config_property failed, [%d]", result);
+       }
+
+       return result;
+}
+
+net_nfc_error_e net_nfc_server_handover_wfd_do_pairing(
+       net_nfc_ch_carrier_s *carrier,
+       net_nfc_server_handover_process_carrier_cb cb,
+       void *user_param)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+       net_nfc_handover_wfd_process_context_t *context = NULL;
+
+       DEBUG_SERVER_MSG("Call this function for connecting wifi-direct");
+
+       _net_nfc_util_alloc_mem(context, sizeof(*context));
+       if (context != NULL)
+       {
+               context->cb = cb;
+               context->user_param = user_param;
+
+               result = net_nfc_util_create_carrier_config_from_config_record(
+                       &context->config, carrier->carrier_record);
+               if (result == NET_NFC_OK) {
+                       g_idle_add((GSourceFunc)_net_nfc_server_handover_wifi_direct_pairing,
+                               (gpointer)context);
+               } else {
+                       DEBUG_ERR_MSG("net_nfc_util_create_carrier_config_from_config_record failed, [%d]", result);
+
+                       _net_nfc_util_free_mem(context);
+               }
+       }
+       else
+       {
+               result = NET_NFC_ALLOC_FAIL;
+       }
+
+       return result;
+}
index e8597e8..9fe2596 100755 (executable)
@@ -230,10 +230,10 @@ static bool _wifi_found_ap_cb(wifi_ap_h ap, void *user_data)
 
        ret = wifi_ap_get_essid(ap, &ssid);
        if (ret == WIFI_ERROR_NONE && ssid != NULL) {
-               DEBUG_MSG("ssid [%s]", ssid);
+               SECURE_MSG("ssid [%s]", ssid);
 
                if (g_strcmp0(ssid, (char *)context[0]) == 0) {
-                       DEBUG_MSG("found!! ssid [%s]", ssid);
+                       SECURE_MSG("found!! ssid [%s]", ssid);
 
                        context[1] = ap;
 
@@ -259,7 +259,7 @@ static wifi_ap_h _wifi_search_ap(const char *ssid)
        context[0] = (gpointer)ssid;
        context[1] = NULL;
 
-       wifi_foreach_found_aps(_wifi_found_ap_cb, &context);
+       (void)wifi_foreach_found_aps(_wifi_found_ap_cb, &context);
 
        return (wifi_ap_h)context[1];
 }
@@ -322,7 +322,7 @@ static void _wps_finish_do_connect(int result, wps_process_context_t *context)
 
 static void _wifi_connected_cb(wifi_error_e result, void *user_data)
 {
-       DEBUG_MSG("wifi_connect result [%d]", result);
+       DEBUG_SERVER_MSG("wifi_connect result [%d]", result);
 
        _wps_finish_do_connect(result, user_data);
 }
@@ -332,12 +332,12 @@ static int __connect(wifi_ap_h ap, wps_process_context_t *context)
        int result;
        char *net_key;
 
-       wifi_ap_set_security_type(ap, _wps_get_security_type(context->config));
-       wifi_ap_set_encryption_type(ap, _wps_get_encryption_type(context->config));
+       (void)wifi_ap_set_security_type(ap, _wps_get_security_type(context->config));
+       (void)wifi_ap_set_encryption_type(ap, _wps_get_encryption_type(context->config));
 
        net_key = _wps_get_string_property(context->config,
                NET_NFC_WIFI_ATTRIBUTE_NET_KEY);
-       wifi_ap_set_passphrase(ap, net_key);
+       (void)wifi_ap_set_passphrase(ap, net_key);
        g_free(net_key);
 
        result = wifi_connect(ap, _wifi_connected_cb, context);
@@ -356,7 +356,7 @@ static void _connect(wps_process_context_t *context)
        if (ssid != NULL) {
                ap = _wifi_search_ap(ssid);
                if (ap == NULL) {
-                       DEBUG_MSG("no ap found");
+                       DEBUG_SERVER_MSG("no ap found");
 
                        result = wifi_ap_create(ssid, &ap);
                        if (result == WIFI_ERROR_NONE) {
@@ -374,7 +374,7 @@ static void _connect(wps_process_context_t *context)
                                _wps_finish_do_connect(result, context);
                        }
                } else if (_wifi_is_connected(ap) == false) {
-                       DEBUG_MSG("found ap, but not connected");
+                       DEBUG_SERVER_MSG("found ap, but not connected");
 
                        result = __connect(ap, context);
                        if (result != WIFI_ERROR_NONE) {
@@ -399,7 +399,7 @@ static void _connect(wps_process_context_t *context)
 /* activation */
 static void _wifi_scan_finished_cb(wifi_error_e result, void *user_data)
 {
-       DEBUG_MSG("_wifi_scan_finished_cb");
+       DEBUG_SERVER_MSG("_wifi_scan_finished_cb");
 
        if (result == WIFI_ERROR_NONE) {
                _connect(user_data);
@@ -412,7 +412,7 @@ static void _wifi_scan_finished_cb(wifi_error_e result, void *user_data)
 
 static void _wifi_activated_cb(wifi_error_e result, void *user_data)
 {
-       DEBUG_MSG("_wifi_activated_cb");
+       DEBUG_SERVER_MSG("_wifi_activated_cb");
 
        if (result == WIFI_ERROR_NONE) {
                int ret;
@@ -439,7 +439,7 @@ static int _wifi_activate(wps_process_context_t *context)
                result = wifi_is_activated(&activated);
                if (result == WIFI_ERROR_NONE) {
                        if (activated == false) {
-                               DEBUG_MSG("wifi is off, try to activate");
+                               DEBUG_SERVER_MSG("wifi is off, try to activate");
                                /* activate */
                                result = wifi_activate(_wifi_activated_cb, context);
                                if (result != WIFI_ERROR_NONE) {
index a6db767..e05278d 100755 (executable)
 #include "net_nfc_server_manager.h"
 #include "net_nfc_server_route_table.h"
 #include "net_nfc_app_util_internal.h"
-#include "net_nfc_server_hce.h"
-
-/*TODO : */
 #include "net_nfc_app_util_internal.h"
+#include "net_nfc_server_hce_ipc.h"
+#include "net_nfc_server_hce.h"
+#include "appsvc.h"
 
 #define OPERATION_APDU_RECEIVED                "http://tizen.org/appcontrol/operation/nfc/card_emulation/apdu_received"
 #define OPERATION_TRANSACTION_RECEIVED "http://tizen.org/appcontrol/operation/nfc/card_emulation/transaction_received"
@@ -138,7 +138,7 @@ static net_nfc_error_e _routing_table_add(const char *package, const char *id,
 
        if (_routing_table_find_aid(package) == NULL) {
                hce_listener_t *data;
-               DEBUG_SERVER_MSG("new hce package, [%s]", package);
+               SECURE_MSG("new hce package, [%s]", package);
 
                data = g_new0(hce_listener_t, 1);
 
@@ -169,7 +169,7 @@ static void _routing_table_del(const char *package)
 
        data = _routing_table_find_aid(package);
        if (data != NULL) {
-               DEBUG_SERVER_MSG("remove hce package, [%s]", package);
+               SECURE_MSG("remove hce package, [%s]", package);
 
                if (data->destroy_cb != NULL) {
                        data->destroy_cb(data->user_data);
@@ -204,7 +204,7 @@ static bool _del_by_id_cb(hce_listener_t *data, void *user_data)
 
        if (data->id == NULL) {
                if (id == NULL) {
-                       DEBUG_MSG("remove context for nfc-manager");
+                       DEBUG_SERVER_MSG("remove context for nfc-manager");
 
                        result = false;
                } else {
@@ -212,7 +212,7 @@ static bool _del_by_id_cb(hce_listener_t *data, void *user_data)
                }
        } else {
                if (id != NULL && g_ascii_strcasecmp(data->id, id) == 0) {
-                       DEBUG_MSG("deleting package [%s:%s]", data->id, data->package);
+                       SECURE_MSG("deleting package [%s:%s]", data->id, data->package);
 
                        _routing_table_del(data->package);
 
@@ -244,7 +244,7 @@ net_nfc_error_e net_nfc_server_hce_start_hce_handler(const char *package,
        result = _routing_table_add(package, id, listener,
                destroy_cb, user_data);
        if (result == NET_NFC_OK) {
-               result = net_nfc_server_route_table_add_handler(id, package);
+               result = net_nfc_server_route_table_update_handler_id(package, id);
        }
 
        return result;
@@ -258,7 +258,7 @@ net_nfc_error_e net_nfc_server_hce_stop_hce_handler(const char *package)
 
        _routing_table_del(package);
 
-       net_nfc_server_route_table_del_handler(NULL, package, false);
+       net_nfc_server_route_table_update_handler_id(package, NULL);
 
        return NET_NFC_OK;
 }
@@ -296,7 +296,7 @@ net_nfc_error_e net_nfc_server_hce_send_apdu_response(
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-
+#if 0
 static void _emit_event_received_signal(GDBusConnection *connection,
        const char *id, int event,
        net_nfc_target_handle_h handle,
@@ -340,7 +340,7 @@ static void _hce_default_listener_cb(net_nfc_target_handle_s *handle,
                if (context->id != NULL) {
                        /* app is running */
                        _emit_event_received_signal(context->connection,
-                               context->id, event, handle, data);
+                               context->id, (int)NET_NFC_HCE_EVENT_APDU_RECEIVED, handle, data);
 
                } else {
                        /* launch app */
@@ -349,13 +349,65 @@ static void _hce_default_listener_cb(net_nfc_target_handle_s *handle,
                break;
 
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED :
+               DEBUG_SERVER_MSG("HCE ACTIVATE");
+
+               if (context->id != NULL) {
+                       /* app is running */
+                       _emit_event_received_signal(context->connection,
+                               context->id, (int)NET_NFC_HCE_EVENT_ACTIVATED, handle, data);
+               }
+               break;
        case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED :
-               DEBUG_SERVER_MSG("HCE %s", event == NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED ? "ACTIVATE" : "DEACTIVATE");
+               DEBUG_SERVER_MSG("HCE DEACTIVATE");
 
                if (context->id != NULL) {
                        /* app is running */
                        _emit_event_received_signal(context->connection,
-                               context->id, event, handle, data);
+                               context->id, (int)NET_NFC_HCE_EVENT_DEACTIVATED, handle, data);
+               }
+               break;
+
+       default :
+               break;
+       }
+}
+#else
+static void _hce_default_listener_cb(net_nfc_target_handle_s *handle,
+       int event, data_s *data, void *user_data)
+{
+       hce_client_context_s *context = (hce_client_context_s *)user_data;
+
+       if (context == NULL) {
+               return;
+       }
+
+       switch (event) {
+       case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA :
+               if (context->id != NULL) {
+                       /* app is running */
+                       net_nfc_server_hce_send_to_client(context->id,
+                               NET_NFC_HCE_EVENT_APDU_RECEIVED, handle, data);
+
+               } else {
+                       /* launch app */
+                       DEBUG_SERVER_MSG("launch apdu app!!");
+               }
+               break;
+
+       case NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED :
+               DEBUG_SERVER_MSG("HCE ACTIVATE");
+
+               if (context->id != NULL) {
+                       /* app is running */
+                       net_nfc_server_hce_send_to_all_client(NET_NFC_HCE_EVENT_ACTIVATED, handle, data);
+               }
+               break;
+       case NET_NFC_MESSAGE_ROUTING_HOST_EMU_DEACTIVATED :
+               DEBUG_SERVER_MSG("HCE DEACTIVATE");
+
+               if (context->id != NULL) {
+                       /* app is running */
+                       net_nfc_server_hce_send_to_all_client(NET_NFC_HCE_EVENT_DEACTIVATED, handle, data);
                }
                break;
 
@@ -363,6 +415,7 @@ static void _hce_default_listener_cb(net_nfc_target_handle_s *handle,
                break;
        }
 }
+#endif
 
 static void _hce_user_data_destroy_cb(void *user_data)
 {
@@ -390,7 +443,7 @@ static void hce_start_hce_handler_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> hce_start_hce_handler_thread_func!!");
+       DEBUG_SERVER_MSG(">>> hce_start_hce_handler_thread_func!!");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -493,7 +546,7 @@ static void hce_stop_hce_handler_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> hce_stop_hce_handler_thread_func!!");
+       DEBUG_SERVER_MSG(">>> hce_stop_hce_handler_thread_func!!");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -584,7 +637,7 @@ static void hce_response_apdu_thread_func(gpointer user_data)
        g_assert(detail->object != NULL);
        g_assert(detail->invocation != NULL);
 
-       INFO_MSG(">>> hce_response_apdu_thread_func!!");
+       DEBUG_SERVER_MSG(">>> hce_response_apdu_thread_func!!");
 
        net_nfc_util_gdbus_variant_to_data_s(detail->data, &apdu_data);
 
@@ -670,6 +723,62 @@ ERROR :
 }
 
 
+static void __hce_handle_send_apdu_response_thread_func(gpointer user_data)
+{
+       HceDataApdu *detail = (HceDataApdu *)user_data;
+       data_s *response;
+
+       g_assert(detail != NULL);
+       g_assert(detail->data != NULL);
+
+       DEBUG_SERVER_MSG(">>> __hce_handle_send_apdu_response_thread_func!!");
+
+       response = (data_s *)detail->data;
+
+       net_nfc_server_hce_send_apdu_response(detail->handle,
+               response);
+
+       net_nfc_util_free_data(response);
+
+       g_free(detail);
+}
+
+
+void net_nfc_server_hce_handle_send_apdu_response(
+       net_nfc_target_handle_s *handle, data_s *response)
+{
+       HceDataApdu *data;
+
+       data = g_try_new0(HceDataApdu, 1);
+       if (data == NULL) {
+               DEBUG_ERR_MSG("Memory allocation failed");
+
+               goto ERROR;
+       }
+
+       data->handle = (net_nfc_target_handle_s *)handle;
+       data->data = (GVariant *)net_nfc_util_duplicate_data(response); /* caution : invalid type casting */
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               __hce_handle_send_apdu_response_thread_func, data) == FALSE) {
+               /* return error if queue was blocked */
+               DEBUG_SERVER_MSG("controller is processing important message..");
+
+               goto ERROR;
+       }
+
+       return;
+
+ERROR :
+       if (data != NULL) {
+               net_nfc_util_free_data((data_s *)data->data); /* caution : invalid type casting */
+
+               g_free(data);
+       }
+}
+
+/******************************************************************************/
+
 typedef struct _apdu_header_t
 {
        uint8_t cla;
@@ -690,7 +799,7 @@ static bool __extract_parameter(apdu_header_t *apdu, size_t len, uint16_t *lc,
        *le = -1;
        *data = NULL;
 
-       DEBUG_SERVER_MSG("[%02X][%02X][%02X][%02X]", apdu->cla, apdu->ins, apdu->p1, apdu->p2);
+       SECURE_MSG("[%02X][%02X][%02X][%02X]", apdu->cla, apdu->ins, apdu->p1, apdu->p2);
 
        if (len > l) {
                if (len == l + 1) {
@@ -777,7 +886,7 @@ static bool __pre_process_apdu(net_nfc_target_handle_s *handle, data_s *cmd)
                                                g_free(selected_aid);
                                                selected_aid = g_strdup(aid);
                                        } else {
-                                               DEBUG_ERR_MSG("file not found, [%s]", aid);
+                                               DEBUG_ERR_MSG("file not found");
 
                                                /* send response */
                                                uint8_t temp[] = { 0x6A, 0x82 };
@@ -827,8 +936,11 @@ static void hce_apdu_thread_func(gpointer user_data)
 
        g_assert(data != NULL);
 
+       //recover session
+       net_nfc_server_route_table_update_preferred_handler();
+
        if (data->event == NET_NFC_MESSAGE_ROUTING_HOST_EMU_DATA) {
-               INFO_MSG("[HCE] Command arrived, handle [0x%x], len [%d]", (int)data->handle, data->apdu.length);
+               SECURE_MSG("[HCE] Command arrived, handle [0x%x], len [%d]", (int)data->handle, data->apdu.length);
 
                if (__pre_process_apdu(data->handle,
                        &data->apdu) == false) {
@@ -841,8 +953,32 @@ static void hce_apdu_thread_func(gpointer user_data)
                                        hce_listener_t *listener;
 
                                        listener = _routing_table_find_aid(handler->package);
-                                       if(listener != NULL)
-                                       {
+
+                                       if (!listener) {
+                                               apdu_header_t *apdu = (apdu_header_t *)data->apdu.buffer;
+                                               uint16_t lc, le;
+                                               uint8_t *aid_data;
+
+                                               if (__extract_parameter(apdu, data->apdu.length, &lc, &le, &aid_data) == true) {
+                                                       if (apdu->ins == NET_NFC_HCE_INS_SELECT && apdu->p1 == NET_NFC_HCE_P1_SELECT_BY_NAME && lc > 2) {
+                                                               bundle *bd;
+                                                               char aid[1024];
+                                                               int ret;
+                                                               data_s temp_aid = { aid_data, lc };
+
+                                                               bd = bundle_create();
+                                                               net_nfc_util_binary_to_hex_string(&temp_aid, aid, sizeof(aid));
+                                                               appsvc_set_operation(bd, "http://tizen.org/appcontrol/operation/nfc/card_emulation/host_apdu_service");
+                                                               appsvc_add_data(bd, "data", aid);
+                                                               ret = aul_launch_app(handler->package, bd);
+                                                               if (ret < 0) {
+                                                                       DEBUG_ERR_MSG("aul_launch_app failed, [%d]", ret);
+                                                               }
+
+                                                               bundle_free(bd);
+                                                       }
+                                               }
+                                       } else {
                                                listener->listener(data->handle,
                                                        data->event, &data->apdu,
                                                        listener->user_data);
@@ -867,7 +1003,7 @@ static void hce_apdu_thread_func(gpointer user_data)
                        DEBUG_SERVER_MSG("pre-processed data");
                }
        } else {
-               INFO_MSG("[HCE] %s!!!!, handle [0x%x]", data->event == NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED ? "Activated" : "Deactivated", (int)data->handle);
+               SECURE_MSG("[HCE] %s!!!!, handle [0x%x]", data->event == NET_NFC_MESSAGE_ROUTING_HOST_EMU_ACTIVATED ? "Activated" : "Deactivated", (int)data->handle);
 
                _routing_table_iterate(_route_table_iter_cb, data);
 
@@ -975,12 +1111,13 @@ gboolean net_nfc_server_hce_init(GDBusConnection *connection)
                connection,
                "/org/tizen/NetNfcService/Hce",
                &error);
-
        if (result == TRUE) {
                /*TODO : Make the Routing Table for AID!*/
                /*TODO : Do i have to make other file for routing table?????*/
                _routing_table_init();
 
+               net_nfc_server_hce_ipc_init();
+
                net_nfc_server_gdbus_register_on_client_detached_cb(
                        _hce_on_client_detached_cb);
        } else {
@@ -998,6 +1135,8 @@ void net_nfc_server_hce_deinit(void)
 {
        if (hce_skeleton)
        {
+               net_nfc_server_hce_ipc_deinit();
+
                net_nfc_server_gdbus_unregister_on_client_detached_cb(
                        _hce_on_client_detached_cb);
 
diff --git a/src/manager/net_nfc_server_hce_ipc.c b/src/manager/net_nfc_server_hce_ipc.c
new file mode 100755 (executable)
index 0000000..cca6a1f
--- /dev/null
@@ -0,0 +1,583 @@
+/*
+  * Copyright 2012  Samsung Electronics Co., Ltd
+  *
+  * Licensed under the Flora License, Version 1.0 (the "License");
+  * you may not use this file except in compliance with the License.
+  * You may obtain a copy of the License at
+
+  *     http://www.tizenopensource.org/license
+  *
+  * Unless required by applicable law or agreed to in writing, software
+  * distributed under the License is distributed on an "AS IS" BASIS,
+  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  * See the License for the specific language governing permissions and
+  * limitations under the License.
+  */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <glib-object.h>
+#include <unistd.h>
+#include <pthread.h>
+
+#include "net_nfc_typedef_internal.h"
+#include "net_nfc_debug_internal.h"
+#include "net_nfc_util_defines.h"
+#include "net_nfc_util_internal.h"
+#include "net_nfc_util_hce.h"
+#include "net_nfc_server_context_internal.h"
+#include "net_nfc_server_hce.h"
+#include "net_nfc_server_hce_ipc.h"
+
+/* define */
+typedef struct _net_nfc_hce_client_t
+{
+       int socket;
+       GIOChannel *channel;
+       guint source_id;
+       pid_t pid;
+
+       char *id;
+}
+net_nfc_hce_client_t;
+
+#define NET_NFC_MANAGER_OBJECT "nfc-manager"
+#define NET_NFC_CARD_EMUL_LABEL        "nfc-manager::card_emul"
+
+/////////////////////////////
+
+/* static variable */
+static int hce_server_socket = -1;
+static GIOChannel *hce_server_channel;
+static guint hce_server_src_id;
+
+static GHashTable *hce_clients;
+
+/////////////////
+
+/*define static function*/
+static void __set_non_block_socket(int socket);
+static pid_t __get_pid_by_socket(int socket);
+static bool __do_client_connect_request();
+
+////////////////////////////////////////////////////////////////////////////////
+
+static void __set_non_block_socket(int socket)
+{
+       int flags;
+
+       flags = fcntl(socket, F_GETFL);
+       flags |= O_NONBLOCK;
+
+       if (fcntl(socket, F_SETFL, flags) < 0) {
+               DEBUG_ERR_MSG("fcntl, executing nonblock error");
+       }
+}
+
+static pid_t __get_pid_by_socket(int socket)
+{
+       struct ucred uc;
+       socklen_t uc_len = sizeof(uc);
+       pid_t pid;
+
+       if (getsockopt(socket, SOL_SOCKET, SO_PEERCRED, &uc, &uc_len) == 0) {
+               pid = uc.pid;
+       } else {
+               pid = -1;
+       }
+
+       return pid;
+}
+
+static void __on_client_value_destroy(gpointer data)
+{
+       net_nfc_hce_client_t *client = (net_nfc_hce_client_t *)data;
+
+       if (data != NULL) {
+               if (client->id != NULL) {
+                       g_free(client->id);
+               }
+
+               if (client->source_id > 0) {
+                       g_source_remove(client->source_id);
+               }
+
+               if (client->channel != NULL) {
+                       g_io_channel_unref(client->channel);
+               }
+
+               if (client->socket != -1) {
+                       shutdown(client->socket, SHUT_RDWR);
+                       close(client->socket);
+               }
+
+               g_free(data);
+       }
+
+       DEBUG_SERVER_MSG("client removed, [%d]", g_hash_table_size(hce_clients));
+}
+
+static void __hce_client_init()
+{
+       if (hce_clients == NULL) {
+               hce_clients = g_hash_table_new_full(g_direct_hash,
+                       g_direct_equal, NULL, __on_client_value_destroy);
+       }
+}
+
+static void __hce_client_add(int socket, GIOChannel *channel,
+       guint source_id)
+{
+       net_nfc_hce_client_t *client;
+
+       client = g_try_new0(net_nfc_hce_client_t, 1);
+       if (client != NULL) {
+
+               client->pid = __get_pid_by_socket(socket);
+               if (client->pid != -1) {
+                       net_nfc_client_context_info_t *context;
+
+                       context = net_nfc_server_gdbus_get_client_context_by_pid(
+                               client->pid);
+                       if (context != NULL) {
+                               client->id = g_strdup(context->id);
+                       } else {
+                               DEBUG_ERR_MSG("net_nfc_server_gdbus_get_client_context_by_pid failed");
+                       }
+               } else {
+                       DEBUG_ERR_MSG("__get_pid_by_socket failed");
+               }
+
+               client->socket = socket;
+               client->channel = channel;
+               client->source_id = source_id;
+
+               g_hash_table_insert(hce_clients, (gpointer)channel, client);
+
+               DEBUG_SERVER_MSG("client added, [%d]", g_hash_table_size(hce_clients));
+       } else {
+               DEBUG_ERR_MSG("alloc failed");
+       }
+}
+
+static void __hce_client_del(GIOChannel *channel)
+{
+       g_hash_table_remove(hce_clients, (gconstpointer)channel);
+}
+
+static void __hce_client_clear()
+{
+       g_hash_table_unref(hce_clients);
+}
+
+static net_nfc_hce_client_t *__hce_client_find(GIOChannel *channel)
+{
+       return (net_nfc_hce_client_t *)g_hash_table_lookup(hce_clients,
+               (gconstpointer)channel);
+}
+
+static net_nfc_hce_client_t *__hce_client_find_by_id(const char *id)
+{
+       net_nfc_hce_client_t *result = NULL;
+       char *key;
+       net_nfc_hce_client_t *value;
+       GHashTableIter iter;
+
+       g_hash_table_iter_init(&iter, hce_clients);
+
+       while (g_hash_table_iter_next(&iter, (gpointer *)&key,
+               (gpointer *)&value) == true) {
+               if (value->id != NULL && g_strcmp0(value->id, id) == 0) {
+                       result = value;
+                       break;
+               }
+       }
+
+       return result;
+}
+
+/******************************************************************************/
+
+static bool __receive_data_from_client(int socket, data_s *data)
+{
+       bool result;
+       ssize_t ret;
+       uint32_t len;
+
+       /* first, receive length */
+       ret = recv(socket, (void *)&len, sizeof(len), 0);
+       if (ret != sizeof(len)) {
+               DEBUG_ERR_MSG("recv failed, socket [%d], result [%d]", socket, ret);
+
+               return false;
+       }
+
+       if (len > 1024) {
+               DEBUG_ERR_MSG("too large message, socket [%d], len [%d]", socket, len);
+
+               return false;
+       }
+
+       result = net_nfc_util_init_data(data, len);
+       if (result == true) {
+               ssize_t offset = 0;
+
+               /* second, receive buffer */
+               do {
+                       ret = recv(socket, data->buffer + offset, data->length - offset, 0);
+                       if (ret == -1) {
+                               break;
+                       }
+
+                       offset += ret;
+               } while (offset < len);
+
+               if (offset != len) {
+                       DEBUG_ERR_MSG("recv failed, socket [%d]", socket);
+
+                       net_nfc_util_clear_data(data);
+
+                       result = false;
+               } else {
+                       DEBUG_SERVER_MSG("recv success, length [%d]", offset);
+               }
+       } else {
+               DEBUG_ERR_MSG("net_nfc_util_init_data failed");
+       }
+
+       return result;
+}
+
+static bool __process_client_message(GIOChannel *channel)
+{
+       bool result;
+       data_s data;
+       net_nfc_hce_client_t *client;
+
+       client = __hce_client_find(channel);
+       if (client != NULL) {
+               if (__receive_data_from_client(client->socket, &data) == true) {
+                       net_nfc_hce_data_t *header;
+                       data_s temp;
+
+                       header = (net_nfc_hce_data_t *)data.buffer;
+
+                       temp.buffer = header->data;
+                       temp.length = data.length - sizeof(net_nfc_hce_data_t);
+
+                       net_nfc_server_hce_handle_send_apdu_response(
+                               (net_nfc_target_handle_s *)header->handle, &temp);
+
+                       result = true;
+               } else {
+                       DEBUG_ERR_MSG("__receive_data_from_client failed");
+
+                       result = false;
+               }
+       } else {
+               DEBUG_ERR_MSG("client not found");
+
+               result = false;
+       }
+
+       return result;
+}
+
+static gboolean __on_client_io_event_cb(GIOChannel *channel,
+       GIOCondition condition, gpointer data)
+{
+       if ((G_IO_ERR & condition) || (G_IO_HUP & condition)) {
+               DEBUG_SERVER_MSG("client socket is closed");
+
+               __hce_client_del(channel);
+
+               return FALSE;
+       } else if (G_IO_NVAL & condition) {
+               DEBUG_SERVER_MSG("INVALID socket");
+
+               return FALSE;
+       } else if (G_IO_IN & condition) {
+               if (__process_client_message(channel) == false) {
+                       DEBUG_SERVER_MSG("__process_client_message failed");
+
+                       __hce_client_del(channel);
+
+                       return FALSE;
+               }
+       }
+
+       return TRUE;
+}
+
+static bool __do_client_connect_request()
+{
+       socklen_t addrlen = 0;
+       int socket = -1;
+       GIOChannel *channel = NULL;
+       guint source_id = 0;
+       GIOCondition condition = (GIOCondition)(G_IO_ERR | G_IO_HUP | G_IO_IN);
+
+       socket = accept(hce_server_socket, NULL, &addrlen);
+       if (socket < 0) {
+               DEBUG_ERR_MSG("accept failed, [%d]", errno);
+
+               return false;
+       }
+
+       channel = g_io_channel_unix_new(socket);
+       if (channel == NULL) {
+               DEBUG_ERR_MSG("create new g io channel is failed");
+
+               goto ERROR;
+       }
+
+       source_id = g_io_add_watch(channel, condition,
+               __on_client_io_event_cb, NULL);
+       if (source_id == 0) {
+               DEBUG_ERR_MSG("add io callback is failed");
+
+               goto ERROR;
+       }
+
+       __hce_client_add(socket, channel, source_id);
+
+       INFO_MSG("client is accepted successfully, [%d]", socket);
+
+       return true;
+
+ERROR :
+       if (channel != NULL) {
+               g_io_channel_unref(channel);
+       }
+
+       if (socket != -1) {
+               shutdown(socket, SHUT_RDWR);
+               close(socket);
+       }
+
+       return false;
+}
+
+static gboolean __on_io_event_cb(GIOChannel *channel, GIOCondition condition,
+       gpointer data)
+{
+       if ((G_IO_ERR & condition) || (G_IO_HUP & condition)) {
+               DEBUG_SERVER_MSG("server socket is closed");
+
+               net_nfc_server_hce_ipc_deinit();
+
+               return FALSE;
+       } else if (G_IO_NVAL & condition) {
+               DEBUG_SERVER_MSG("INVALID socket");
+
+               return FALSE;
+       } else if (G_IO_IN & condition) {
+               __do_client_connect_request();
+       }
+
+       return TRUE;
+}
+
+bool net_nfc_server_hce_ipc_init()
+{
+       struct sockaddr_un saddrun_rv;
+       int ret;
+
+       /* initialize server context */
+       __hce_client_init();
+
+       hce_server_socket = -1;
+       hce_server_channel = (GIOChannel *)NULL;
+       hce_server_src_id = 0;
+
+       ///////////////////////////////
+
+       (void)remove(NET_NFC_HCE_SERVER_DOMAIN);
+
+       memset(&saddrun_rv, 0, sizeof(struct sockaddr_un));
+
+       hce_server_socket = net_nfc_util_get_fd_from_systemd();
+       if (hce_server_socket == -1) {
+               DEBUG_ERR_MSG("create socket failed, [%d]", errno);
+
+               return false;
+       }
+
+       __set_non_block_socket(hce_server_socket);
+
+       if (listen(hce_server_socket, SOMAXCONN) < 0) {
+               DEBUG_ERR_MSG("listen is failed");
+
+               goto ERROR;
+       }
+
+       GIOCondition condition = (GIOCondition)(G_IO_ERR | G_IO_HUP | G_IO_IN);
+
+       hce_server_channel = g_io_channel_unix_new(hce_server_socket);
+       if (hce_server_channel == NULL) {
+               DEBUG_ERR_MSG("g_io_channel_unix_new is failed");
+
+               goto ERROR;
+       }
+
+       hce_server_src_id = g_io_add_watch(hce_server_channel,
+               condition, __on_io_event_cb, NULL);
+       if (hce_server_src_id < 1) {
+               DEBUG_ERR_MSG("g_io_add_watch is failed");
+
+               goto ERROR;
+       }
+
+       DEBUG_SERVER_MSG("hce ipc is initialized");
+
+       return true;
+
+ERROR :
+       if (hce_server_src_id > 0) {
+               g_source_remove(hce_server_src_id);
+               hce_server_src_id = 0;
+       }
+
+       if (hce_server_channel != NULL) {
+               g_io_channel_unref(hce_server_channel);
+               hce_server_channel = NULL;
+       }
+
+       if (hce_server_socket != -1) {
+               shutdown(hce_server_socket, SHUT_RDWR);
+               close(hce_server_socket);
+
+               hce_server_socket = -1;
+       }
+
+       return false;
+}
+
+void net_nfc_server_hce_ipc_deinit()
+{
+       __hce_client_clear();
+
+       if (hce_server_src_id > 0) {
+               g_source_remove(hce_server_src_id);
+               hce_server_src_id = 0;
+       }
+
+       if (hce_server_channel != NULL) {
+               g_io_channel_unref(hce_server_channel);
+               hce_server_channel = NULL;
+       }
+
+       if (hce_server_socket != -1) {
+               shutdown(hce_server_socket, SHUT_RDWR);
+               close(hce_server_socket);
+               hce_server_socket = -1;
+       }
+}
+
+static bool __send_data_to_client(int socket, data_s *data)
+{
+       ssize_t ret;
+
+       ret = send(socket, data->buffer, data->length, 0);
+       if (ret == -1) {
+               DEBUG_ERR_MSG("send failed, socket [%d]", socket);
+
+               return false;
+       }
+
+       return true;
+}
+
+bool net_nfc_server_hce_send_to_client(const char *id, int type,
+       net_nfc_target_handle_s *handle, data_s *data)
+{
+       bool ret;
+       net_nfc_hce_client_t *client;
+       data_s temp;
+       uint32_t len = sizeof(net_nfc_hce_data_t);
+
+       client = __hce_client_find_by_id(id);
+       if (client == NULL) {
+               DEBUG_ERR_MSG("__hce_client_find_by_id failed");
+
+               return false;
+       }
+
+       if (data != NULL && data->length > 0) {
+               len += data->length;
+       }
+
+       ret = net_nfc_util_init_data(&temp, len + sizeof(len)); /* length + data */
+       if (ret == true) {
+               net_nfc_hce_data_t *header;
+
+               *(uint32_t *)(temp.buffer) = len;
+               header = (net_nfc_hce_data_t *)(temp.buffer + sizeof(len));
+
+               header->type = type;
+               header->handle = (int)handle;
+
+               if (data != NULL && data->length > 0) {
+                       memcpy(header->data, data->buffer, data->length);
+               }
+
+               ret = __send_data_to_client(client->socket, &temp);
+
+               net_nfc_util_clear_data(&temp);
+       } else {
+               DEBUG_ERR_MSG("net_nfc_util_init_data failed");
+       }
+
+       return ret;
+}
+
+bool net_nfc_server_hce_send_to_all_client(int type,
+       net_nfc_target_handle_s *handle, data_s *data)
+{
+       bool ret;
+       data_s temp;
+       uint32_t len = sizeof(net_nfc_hce_data_t);
+
+       if (data != NULL && data->length > 0) {
+               len += data->length;
+       }
+
+       ret = net_nfc_util_init_data(&temp, len + sizeof(len)); /* length + data */
+       if (ret == true) {
+               GHashTableIter iter;
+               gpointer key;
+               net_nfc_hce_client_t *client;
+               net_nfc_hce_data_t *header;
+
+               *(uint32_t *)(temp.buffer) = len;
+               header = (net_nfc_hce_data_t *)(temp.buffer + sizeof(len));
+
+               header->type = type;
+               header->handle = (int)handle;
+
+               if (data != NULL && data->length > 0) {
+                       memcpy(header->data, data->buffer, data->length);
+               }
+
+               g_hash_table_iter_init(&iter, hce_clients);
+
+               while (g_hash_table_iter_next(&iter, (gpointer *)&key,
+                       (gpointer *)&client) == true) {
+                       ret = __send_data_to_client(client->socket, &temp);
+               }
+
+               net_nfc_util_clear_data(&temp);
+       } else {
+               DEBUG_ERR_MSG("net_nfc_util_init_data failed");
+       }
+
+       return ret;
+}
index 85bbd8a..7745d1d 100755 (executable)
@@ -83,9 +83,6 @@ static net_nfc_error_e manager_active(void)
        /* keep_SE_select_value */
        result = net_nfc_server_se_apply_se_current_policy();
 
-       /* load aids from database */
-       net_nfc_server_route_table_load_db();
-
        /* register default snep server */
        net_nfc_server_snep_default_server_register();
 
@@ -99,7 +96,7 @@ static net_nfc_error_e manager_active(void)
        net_nfc_addons_init();
 
        /* update route table */
-       net_nfc_server_route_table_do_update();
+       net_nfc_server_route_table_do_update(true);
 
        /* current comsume issue for card only model */
        net_nfc_controller_set_screen_state(NET_NFC_SCREEN_OFF , &result);
@@ -208,17 +205,17 @@ static void manager_handle_active_thread_func(gpointer user_data)
                data->invocation,
                result);
 
-       g_object_unref(data->invocation);
-       g_object_unref(data->manager);
-
-       g_free(data);
-
        /* shutdown process if it doesn't need */
        if (result == NET_NFC_OK && data->is_active == false &&
                net_nfc_server_gdbus_is_server_busy() == false) {
                DEBUG_ERR_MSG("net_nfc_server_controller_deinit");
                net_nfc_server_controller_deinit();
        }
+
+       g_object_unref(data->invocation);
+       g_object_unref(data->manager);
+
+       g_free(data);
 }
 
 
@@ -257,6 +254,21 @@ static gboolean manager_handle_set_active(NetNfcGDbusManager *manager,
        data->invocation = g_object_ref(invocation);
        data->is_active = arg_is_active;
 
+
+       if (data->is_active == true){
+               net_nfc_error_e check_result;
+               INFO_MSG("Daemon alive, But check the nfc device state.");
+
+               net_nfc_controller_is_ready(&check_result);
+
+               if (check_result != NET_NFC_OK) {
+                       INFO_MSG("nfc is not active. so call net_nfc_server_controller_init");
+                       net_nfc_server_controller_init();
+               }
+               else
+                       INFO_MSG("nfc is already active!!!!!!");
+       }
+
        if (net_nfc_server_controller_async_queue_push_and_block(
                manager_handle_active_thread_func, data) == FALSE)
        {
@@ -338,6 +350,21 @@ static void manager_active_thread_func(gpointer user_data)
                DEBUG_ERR_MSG("activation change failed, [%d]", result);
        }
 
+       /* shutdown process if it doesn't need */
+       if (result == NET_NFC_OK && data->is_active == false) {
+               DEBUG_ERR_MSG("net_nfc_server_controller_deinit");
+
+               if (net_nfc_controller_deinit() == false)
+               {
+                       DEBUG_ERR_MSG("net_nfc_controller_deinit failed");
+
+                       /* ADD TEMPORARY ABORT FOR DEBUG */
+                       abort();
+                       return;
+               }
+
+       }
+
        g_free(data);
 }
 
@@ -390,6 +417,7 @@ void net_nfc_server_manager_deinit(void)
 void net_nfc_server_manager_set_active(gboolean is_active)
 {
        ManagerActivationData *data;
+       net_nfc_error_e result;
 
        if (manager_skeleton == NULL)
        {
@@ -411,6 +439,21 @@ void net_nfc_server_manager_set_active(gboolean is_active)
        data->manager = g_object_ref(manager_skeleton);
        data->is_active = is_active;
 
+
+       if (data->is_active == true){
+               INFO_MSG("Daemon alive, But check the nfc device state.");
+
+               net_nfc_controller_is_ready(&result);
+
+               if (result != NET_NFC_OK) {
+                       INFO_MSG("nfc is not active. so call net_nfc_server_controller_init");
+                       net_nfc_server_controller_init();
+               }
+               else
+                       INFO_MSG("nfc is already active!!!!!!");
+       }
+
+
        if (net_nfc_server_controller_async_queue_push(
                                        manager_active_thread_func,
                                        data) == FALSE)
index 57dc3de..13fa756 100755 (executable)
@@ -276,6 +276,30 @@ static void _wps_process_carrier_cb(net_nfc_error_e result,
        _net_nfc_util_free_mem(context);
 }
 
+static void _wfd_process_carrier_cb(net_nfc_error_e result,
+       net_nfc_conn_handover_carrier_type_e type,
+       data_s *data,
+       void *user_param)
+{
+       _process_config_context_t *context =
+               (_process_config_context_t *)user_param;
+
+       if (result == NET_NFC_OK)
+       {
+               if (context->cb != NULL)
+               {
+                       context->cb(result, type, data, context->user_param);
+               }
+       }
+       else
+       {
+               DEBUG_ERR_MSG("_wfd_process_carrier_cb failed [%d]",
+                       result);
+       }
+
+       _net_nfc_util_free_mem(context);
+}
+
 static net_nfc_error_e _get_carrier_record_by_priority_order(
        net_nfc_ch_message_s *request,
        net_nfc_ch_carrier_s **carrier)
@@ -649,32 +673,32 @@ static int _iterate_create_carrier_configs(_create_config_context_t *context)
        switch (context->current_type)
        {
        case NET_NFC_CONN_HANDOVER_CARRIER_BT :
-               DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
+               DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
                net_nfc_server_handover_bt_get_carrier(
                        _bt_get_carrier_record_cb,
                        context);
                break;
 
        case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS :
-               DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS]");
+               DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS]");
                g_idle_add((GSourceFunc)_iterate_carrier_configs_step,
                        (gpointer)context);
                break;
 
        case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P :
-               DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P]");
+               DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P]");
                g_idle_add((GSourceFunc)_iterate_carrier_configs_step,
                        (gpointer)context);
                break;
 
        case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
-               DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
+               DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
                g_idle_add((GSourceFunc)_iterate_carrier_configs_step,
                        (gpointer)context);
                break;
 
        default :
-               DEBUG_MSG("[unknown : %d]", context->current_type);
+               DEBUG_SERVER_MSG("[unknown : %d]", context->current_type);
                g_idle_add((GSourceFunc)_iterate_carrier_configs_step,
                        (gpointer)context);
                break;
@@ -908,7 +932,7 @@ static net_nfc_error_e _process_requester_carrier(net_nfc_ch_carrier_s *carrier,
                switch (type)
                {
                case NET_NFC_CONN_HANDOVER_CARRIER_BT :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
                        net_nfc_server_handover_bt_prepare_pairing(
                                carrier,
                                _bt_process_carrier_cb,
@@ -916,21 +940,21 @@ static net_nfc_error_e _process_requester_carrier(net_nfc_ch_carrier_s *carrier,
                        break;
 
                case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS]");
                        _net_nfc_util_free_mem(context);
                        break;
 
                case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P]");
                        _net_nfc_util_free_mem(context);
                        break;
 
                case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
                        break;
 
                default :
-                       DEBUG_MSG("[unknown]");
+                       DEBUG_SERVER_MSG("[unknown]");
                        _net_nfc_util_free_mem(context);
                        break;
                }
@@ -971,7 +995,7 @@ static net_nfc_error_e _process_selector_carrier(net_nfc_ch_carrier_s *carrier,
                switch (type)
                {
                case NET_NFC_CONN_HANDOVER_CARRIER_BT :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_BT]");
                        net_nfc_server_handover_bt_do_pairing(
                                carrier,
                                _bt_process_carrier_cb,
@@ -979,20 +1003,28 @@ static net_nfc_error_e _process_selector_carrier(net_nfc_ch_carrier_s *carrier,
                        break;
 
                case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS]");
                        net_nfc_server_handover_wps_do_connect(
                                carrier,
                                _wps_process_carrier_cb,
                                context);
                        break;
 
+               case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P :
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P]");
+                       net_nfc_server_handover_wfd_do_pairing(
+                               carrier,
+                               _wfd_process_carrier_cb,
+                               context);
+                       break;
+
                case NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN :
-                       DEBUG_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
+                       DEBUG_SERVER_MSG("[NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN]");
                        _net_nfc_util_free_mem(context);
                        break;
 
                default :
-                       DEBUG_MSG("[unknown]");
+                       DEBUG_SERVER_MSG("[unknown]");
                        _net_nfc_util_free_mem(context);
                        break;
                }
@@ -2191,7 +2223,7 @@ static void _client_create_carrier_configs_cb(net_nfc_error_e result,
                result = _convert_ndef_message_to_data(message, &context->data);
                if (result == NET_NFC_OK)
                {
-                       DEBUG_MSG_PRINT_BUFFER(context->data.buffer, context->data.length);
+//                     DEBUG_MSG_PRINT_BUFFER(context->data.buffer, context->data.length);
 
                        context->state = NET_NFC_LLCP_STEP_02;
                }
index ffac872..296a2d6 100755 (executable)
@@ -179,7 +179,7 @@ static void __npp_server_received_cb(net_nfc_error_e result,
        npp_data->data_offset += length;
 
        if (npp_data->data_offset == npp_data->data.length) {
-               INFO_MSG("receive message complete, length [%d]", npp_data->data.length);
+               DEBUG_SERVER_MSG("receive message complete, length [%d]", npp_data->data.length);
 
                /* launch */
                if (npp_data->callback) {
@@ -191,7 +191,7 @@ static void __npp_server_received_cb(net_nfc_error_e result,
 //                     /* start next entry */
 //             }
        } else {
-               INFO_MSG("fragmented message, read [%d], expected [%d]", npp_data->data_offset, npp_data->data.length);
+               DEBUG_SERVER_MSG("fragmented message, read [%d], expected [%d]", npp_data->data_offset, npp_data->data.length);
 
                /* receive next fragment */
                result = net_nfc_server_llcp_simple_receive(npp_data->handle,
@@ -418,7 +418,7 @@ static void npp_client_send_cb(net_nfc_error_e result,
                return;
 
        if (npp_data->data_offset < npp_data->data.length) {
-               INFO_MSG("fragmented message, sent [%d], total [%d]", npp_data->data_offset, npp_data->data.length);
+               DEBUG_SERVER_MSG("fragmented message, sent [%d], total [%d]", npp_data->data_offset, npp_data->data.length);
 
                npp_client_process(npp_data);
        } else {
index c28d18e..795a958 100755 (executable)
@@ -1821,14 +1821,14 @@ net_nfc_error_e net_nfc_server_snep_client_request(
                return NET_NFC_ALLOC_FAIL;
        }
 
-       INFO_MSG("enqueued jobs [%d]", g_queue_get_length(&context->queue));
+       DEBUG_SERVER_MSG("enqueued jobs [%d]", g_queue_get_length(&context->queue));
 
        /* if client is idle, starts sending request */
        if (context->state == NET_NFC_LLCP_IDLE)
        {
                _net_nfc_server_snep_client_do_job(context);
        } else {
-               INFO_MSG("client is working. this job will be enqueued");
+               DEBUG_SERVER_MSG("client is working. this job will be enqueued");
        }
 
        return result;
index d6dfb8f..d02b270 100755 (executable)
 #include "net_nfc_util_internal.h"
 #include "net_nfc_controller_internal.h"
 #include "net_nfc_server.h"
+#include "net_nfc_server_manager.h"
 #include "net_nfc_server_se.h"
 #include "net_nfc_server_vconf.h"
 #include "net_nfc_server_route_table.h"
+#include "net_nfc_app_util_internal.h"
 
 #define PRINT_TABLE
+#define AID_MAX                50
 
 /* route table database */
 #define NFC_ROUTE_TABLE_DB_FILE "/opt/usr/data/nfc-manager-daemon/.route_table.db"
@@ -38,15 +41,22 @@ typedef void (*_iterate_db_cb)(const char *package, net_nfc_se_type_e se_type,
 
 static sqlite3 *db;
 static sqlite3_stmt *current_stmt;
+static int count = 0;
 
 static net_nfc_error_e __route_table_add_aid(const char *id,
        const char *package, net_nfc_se_type_e se_type,
        net_nfc_card_emulation_category_t category,
-       const char *aid, bool unlock, int power, bool manifest);
+       const char *aid, bool unlock, int power, bool manifest, bool routing);
 
 static net_nfc_error_e __route_table_del_aid(const char *id,
        const char *package, const char *aid, bool force);
 
+static net_nfc_error_e __insert_into_db(const char *package, net_nfc_se_type_e se_type,
+       net_nfc_card_emulation_category_t category, const char *aid,
+       bool unlock, int power, bool manifest);
+
+static void __set_payment_handler(const char *package);
+
 static bool __is_table_existing(const char *table)
 {
        bool result;
@@ -353,8 +363,13 @@ static net_nfc_error_e __delete_aids_from_db(const char *package)
 #endif
 ////////////////////////////////////////////////////////////////////////////////
 
+static char *preferred_payment;
 static char *activated_payment;
+
+static char *preferred_other;
 static GPtrArray *activated_other;
+static int maximum_aids_count = AID_MAX;
+static int used_aids_count[NET_NFC_CARD_EMULATION_CATEGORY_MAX + 1];
 
 static void __update_payment_handler(const char *package)
 {
@@ -367,6 +382,12 @@ static void __update_payment_handler(const char *package)
        }
 }
 
+static bool __is_preferred_payment_handler(const char *package)
+{
+       return (preferred_payment != NULL &&
+               g_strcmp0(package, preferred_payment) == 0);
+}
+
 static bool __is_payment_handler(const char *package)
 {
        return (activated_payment != NULL &&
@@ -416,7 +437,13 @@ void net_nfc_server_route_table_update_other_handler(const char *packages)
 {
        __update_other_handler(packages);
 
-       net_nfc_server_route_table_do_update();
+       net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+}
+
+static bool __is_preferred_other_handler(const char *package)
+{
+       return (preferred_other != NULL &&
+               g_strcmp0(package, preferred_other) == 0);
 }
 
 static bool __is_other_handler(const char *package)
@@ -494,6 +521,19 @@ static void __append_other_handler(const char *package)
        }
 }
 
+static bool __is_preferred_handler(net_nfc_card_emulation_category_t category,
+       const char *package)
+{
+       switch (category) {
+       case NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT :
+               return __is_preferred_payment_handler(package);
+       case NET_NFC_CARD_EMULATION_CATEGORY_OTHER :
+               return __is_preferred_other_handler(package);
+       default :
+               return false;
+       }
+}
+
 static bool __is_activated_handler(net_nfc_card_emulation_category_t category,
        const char *package)
 {
@@ -560,12 +600,12 @@ void net_nfc_server_route_table_update_category_handler(const char *package,
        switch (category) {
        case NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT :
                __update_payment_handler(package);
-               net_nfc_server_route_table_do_update();
+               net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
                break;
 
        case NET_NFC_CARD_EMULATION_CATEGORY_OTHER :
                __update_other_handler(package);
-               net_nfc_server_route_table_do_update();
+               net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
                break;
 
        default :
@@ -577,6 +617,7 @@ void net_nfc_server_route_table_update_category_handler(const char *package,
 ////////////////////////////////////////////////////////////////////////////////
 
 /*Routing Table base on AID*/
+route_table_handler_t *preferred_handler;
 static GHashTable *routing_handlers;
 
 static bool __get_package_name(const char *id, char *package, size_t len)
@@ -646,9 +687,9 @@ static void __on_iterate_db_aid_cb(const char *package,
 {
        net_nfc_server_route_table_add_handler(NULL, package);
 
-       DEBUG_SERVER_MSG("package [%s], se_type [%d], category [%d], manifest [%d], aid [%s]", package, se_type, category, manifest, aid);
+       SECURE_MSG("package [%s], se_type [%d], category [%d], manifest [%d], aid [%s]", package, se_type, category, manifest, aid);
 
-       __route_table_add_aid(NULL, package, se_type, category, aid, unlock, power, manifest);
+       __route_table_add_aid(NULL, package, se_type, category, aid, unlock, power, manifest, false);
 }
 
 void net_nfc_server_route_table_init()
@@ -672,6 +713,12 @@ void net_nfc_server_route_table_init()
 
                routing_handlers = g_hash_table_new_full(g_str_hash,
                        g_str_equal, __on_key_destroy, __on_value_destroy);
+
+               /* load aids from database */
+               net_nfc_server_route_table_load_db();
+
+               /* update route table without routing */
+               net_nfc_server_route_table_do_update(false);
        }
 }
 
@@ -710,7 +757,7 @@ net_nfc_error_e net_nfc_server_route_table_add_handler(const char *id,
        if (data == NULL) {
                int i;
 
-               DEBUG_SERVER_MSG("new package, [%s]", package);
+               SECURE_MSG("new package, [%s]", package);
 
                data = g_new0(route_table_handler_t, 1);
 
@@ -736,7 +783,7 @@ net_nfc_error_e net_nfc_server_route_table_add_handler(const char *id,
                result = NET_NFC_OK;
        } else {
                if (id != NULL && data->id == NULL) {
-                       DEBUG_SERVER_MSG("update client id, [%s]", id);
+                       SECURE_MSG("update client id, [%s]", id);
                        data->id = g_strdup(id);
                }
 
@@ -753,16 +800,20 @@ static net_nfc_error_e __do_routing_aid(aid_info_t *info, bool commit)
        if (info->is_routed == false) {
                data_s temp = { 0, };
 
-               INFO_MSG("route aid, aid [%s]", info->aid);
+               SECURE_MSG("route aid, aid [%s]", info->aid);
 
                if (net_nfc_util_aid_is_prefix(info->aid) == true) {
-                       DEBUG_SERVER_MSG("prefix...");
+                       SECURE_MSG("prefix...");
                }
 
                if (net_nfc_util_hex_string_to_binary(info->aid, &temp) == true) {
                        net_nfc_controller_secure_element_route_aid(&temp, info->se_type, info->power, &result);
                        if (result == NET_NFC_OK) {
                                info->is_routed = true;
+                               if (info->power == 0x21 || info->power == 0x31) {
+                                       count++;
+                                       net_nfc_server_vconf_set_screen_on_flag(true);
+                               }
 
                                if (commit == true) {
                                        net_nfc_controller_secure_element_commit_routing(&result);
@@ -778,7 +829,7 @@ static net_nfc_error_e __do_routing_aid(aid_info_t *info, bool commit)
                        result = NET_NFC_ALLOC_FAIL;
                }
        } else {
-               DEBUG_ERR_MSG("already routed, aid [%s]", info->aid);
+               SECURE_MSG("already routed, aid [%s]", info->aid);
 
                result = NET_NFC_ALREADY_REGISTERED;
        }
@@ -793,16 +844,23 @@ static net_nfc_error_e __do_unrouting_aid(aid_info_t *info, bool commit)
        if (info->is_routed == true) {
                data_s temp = { 0, };
 
-               INFO_MSG("unroute aid, aid [%s]", info->aid);
+               SECURE_MSG("unroute aid, aid [%s]", info->aid);
 
                if (net_nfc_util_aid_is_prefix(info->aid) == true) {
-                       DEBUG_SERVER_MSG("prefix...");
+                       SECURE_MSG("prefix...");
                }
 
                if (net_nfc_util_hex_string_to_binary(info->aid, &temp) == true) {
                        net_nfc_controller_secure_element_unroute_aid(&temp, &result);
                        if (result == NET_NFC_OK) {
                                info->is_routed = false;
+                               if (info->power == 0x21 || info->power == 0x31) {
+                                       count--;
+                                       if (count <= 0) {
+                                               net_nfc_server_vconf_set_screen_on_flag(false);
+                                               net_nfc_controller_set_screen_state(NET_NFC_SCREEN_OFF , &result);
+                                       }
+                               }
 
                                if (commit == true) {
                                        net_nfc_controller_secure_element_commit_routing(&result);
@@ -818,7 +876,7 @@ static net_nfc_error_e __do_unrouting_aid(aid_info_t *info, bool commit)
                        result = NET_NFC_ALLOC_FAIL;
                }
        } else {
-               DEBUG_ERR_MSG("didn't routed, aid [%s]", info->aid);
+               SECURE_MSG("didn't routed, aid [%s]", info->aid);
 
                result = NET_NFC_NOT_REGISTERED;
        }
@@ -910,7 +968,7 @@ net_nfc_error_e net_nfc_server_route_table_del_handler(const char *id,
                        if (data->aids[category]->len == 0 &&
                                data->activated[category] == true) {
                        /* deactivate for each category */
-                               DEBUG_SERVER_MSG("deactivate handler, [%d][%s]", category, package);
+                               SECURE_MSG("deactivate handler, [%d][%s]", category, package);
 
                                /* TODO */
                                __deactivate_handler(package, category);
@@ -918,13 +976,13 @@ net_nfc_error_e net_nfc_server_route_table_del_handler(const char *id,
                }
 
                if (data->aids[0]->len == 0) {
-                       DEBUG_SERVER_MSG("deleting package, [%s]", package);
+                       SECURE_MSG("deleting package, [%s]", package);
 
                        g_hash_table_remove(routing_handlers, package);
 
                        result = NET_NFC_OK;
                } else {
-                       DEBUG_ERR_MSG("remain some aids, [%d]", data->aids[0]->len);
+                       SECURE_MSG("remain some aids, [%d]", data->aids[0]->len);
 
                        result = NET_NFC_OPERATION_FAIL;
                }
@@ -937,6 +995,140 @@ net_nfc_error_e net_nfc_server_route_table_del_handler(const char *id,
        return result;
 }
 #endif
+net_nfc_error_e net_nfc_server_route_table_update_handler_id(
+       const char *package, const char *id)
+{
+       net_nfc_error_e result;
+
+       if (id != NULL && strlen(id) > 0) {
+               result = net_nfc_server_route_table_add_handler(id, package);
+       } else {
+               route_table_handler_t *data;
+
+               data = net_nfc_server_route_table_find_handler(package);
+               if (data != NULL) {
+                       if (data->id != NULL) {
+                               SECURE_MSG("remove client id, [%s]", id);
+                               g_free(data->id);
+                               data->id = NULL;
+                       }
+
+               }
+
+               result = NET_NFC_OK;
+       }
+
+       return result;
+}
+
+route_table_handler_t *net_nfc_server_route_table_get_preferred_handler()
+{
+       return preferred_handler;
+}
+
+void net_nfc_server_route_table_unset_preferred_handler_by_id(const char* id)
+{
+       route_table_handler_t *handler;
+
+       handler = net_nfc_server_route_table_find_handler_by_id(id);
+       if (handler != NULL) {
+               if (__is_preferred_handler(NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT, handler->package) ||
+                        __is_preferred_handler(NET_NFC_CARD_EMULATION_CATEGORY_OTHER, handler->package)) {
+                       DEBUG_SERVER_MSG("[Preferred] Unset Preferred handler by id");
+                       net_nfc_server_route_table_set_preferred_handler(NULL);
+                       net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+               }
+       }
+}
+
+void net_nfc_server_route_table_set_preferred_handler(route_table_handler_t *handler)
+{
+       int payment_count, other_count;
+
+       preferred_handler = handler;
+
+       if (handler != NULL) {
+               payment_count = handler->aids[NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT]->len;
+               if (payment_count > 0) {
+                       preferred_payment = handler->package;
+                       DEBUG_SERVER_MSG("[Preferred] Set Preferred payment handler");
+               }
+
+               other_count = handler->aids[NET_NFC_CARD_EMULATION_CATEGORY_OTHER]->len;
+               if (other_count > 0) {
+                       preferred_other = handler->package;
+                       DEBUG_SERVER_MSG("[Preferred] Set Preferred other handler");
+               }
+       } else {
+               preferred_payment = NULL;
+               preferred_other = NULL;
+       }
+}
+
+net_nfc_error_e net_nfc_server_route_table_update_preferred_handler()
+{
+       char foreground[1024] = {0,};
+
+       if (preferred_handler != NULL && preferred_handler->package) {
+               bool ret;
+               pid_t pid;
+
+               //check match prefered and foreground app
+               pid = net_nfc_app_util_get_focus_app_pid();
+               ret = net_nfc_util_get_pkgid_by_pid(pid, foreground, sizeof(foreground));
+
+               if (strcmp(foreground, preferred_handler->package) != 0) {
+                       DEBUG_SERVER_MSG("[Preferred] Not match!!! foreground : %s, preferred : %s",
+                               foreground, preferred_handler->package);
+
+                       net_nfc_server_route_table_set_preferred_handler(NULL);
+                       net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+               }
+       }
+
+#ifdef PRINT_TABLE
+       net_nfc_server_route_table_preferred_handler_dump();
+#endif
+}
+
+bool net_nfc_server_route_table_is_allowed_preferred_handler(route_table_handler_t *handler)
+{
+       int payment_count;
+
+       //platform implentation
+       //check if package has payment aid and check if handler is activated handler.
+       payment_count = handler->aids[NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT]->len;
+       if (payment_count != 0) {
+               if (__is_activated_handler(NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT, handler->package) == true) {
+                       DEBUG_SERVER_MSG("[Preferred] %s is already payment activated handler", handler->package);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+#ifdef PRINT_TABLE
+void net_nfc_server_route_table_preferred_handler_dump()
+{
+       char foreground[1024] = {0,};
+       pid_t pid;
+
+       //check match prefered and foreground app
+       pid = net_nfc_app_util_get_focus_app_pid();
+       net_nfc_util_get_pkgid_by_pid(pid, foreground, sizeof(foreground));
+
+       DEBUG_SERVER_MSG("------------------------------");
+       DEBUG_SERVER_MSG("Foreground package : %s", foreground);
+       if (preferred_handler != NULL) {
+               DEBUG_SERVER_MSG("Preferred default : %s", preferred_handler->package);
+               DEBUG_SERVER_MSG("PAYMENT activate state : %d", preferred_handler->activated[1]);
+               DEBUG_SERVER_MSG("OTHER activate state : %d", preferred_handler->activated[2]);
+       }
+       DEBUG_SERVER_MSG("------------------------------");
+}
+#endif
+
 void net_nfc_server_route_table_iterate_handler(
        net_nfc_server_route_table_handler_iter_cb cb, void *user_data)
 {
@@ -955,6 +1147,46 @@ void net_nfc_server_route_table_iterate_handler(
                }
        }
 }
+
+void net_nfc_server_route_table_iterate_handler_activated_last(
+       net_nfc_server_route_table_handler_iter_cb cb, void *user_data)
+{
+       GHashTableIter iter;
+       gpointer key;
+       route_table_handler_t *data;
+
+       if (routing_handlers == NULL)
+               return;
+
+       g_hash_table_iter_init(&iter, routing_handlers);
+
+       while (g_hash_table_iter_next(&iter, &key, (gpointer)&data)) {
+               if (!__is_preferred_payment_handler((const char *)key)
+                       && !__is_preferred_other_handler((const char *)key)
+                       && !__is_payment_handler((const char *)key)
+                       && !__is_other_handler((const char *)key)) {
+                       if (cb((const char *)key, data, user_data) == false) {
+                               break;
+                       }
+               }
+       }
+
+       g_hash_table_iter_init(&iter, routing_handlers);
+
+       while (g_hash_table_iter_next(&iter, &key, (gpointer)&data)) {
+               if (__is_preferred_payment_handler((const char *)key) == false
+                       && __is_preferred_other_handler((const char *)key) == false
+                       && (__is_payment_handler((const char *)key) || __is_other_handler((const char *)key))) {
+                       if (cb((const char *)key, data, user_data) == false) {
+                               break;
+                       }
+               }
+       }
+
+       if (preferred_handler != NULL) {
+               cb((const char *)preferred_handler->package, preferred_handler, user_data);
+       }
+}
 #ifdef PRINT_TABLE
 static const char *__get_se_name(net_nfc_se_type_e se_type)
 {
@@ -976,13 +1208,16 @@ static bool _display_route_table_cb(const char *package,
        route_table_handler_t *handler, void *user_data)
 {
        int i;
-
-       DEBUG_SERVER_MSG(" + PACKAGE [%s|%s]", handler->package, handler->id);
+       if (preferred_handler == handler) {
+               SECURE_LOGD(" + Preferred PACKAGE [%s|%s]", handler->package, handler->id);
+       } else {
+               SECURE_LOGD(" + PACKAGE [%s|%s]", handler->package, handler->id);
+       }
 
        for (i = 0; i < handler->aids[0]->len; i++) {
                aid_info_t *info = (aid_info_t *)handler->aids[0]->pdata[i];
 
-               DEBUG_SERVER_MSG(" +-- AID [%s], SE [%s], CATEGORY [%s%s], MANIFEST [%s]%s",
+               SECURE_LOGD(" +-- AID [%s], SE [%s], CATEGORY [%s%s], MANIFEST [%s]%s",
                        info->aid,
                        __get_se_name(info->se_type),
                        info->category == NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT ? "payment" : "other",
@@ -996,9 +1231,9 @@ static bool _display_route_table_cb(const char *package,
 
 static void __display_route_table()
 {
-       DEBUG_SERVER_MSG(" +------------------------------------------------+");
+       SECURE_LOGD(" +------------------------------------------------+");
        net_nfc_server_route_table_iterate_handler(_display_route_table_cb, NULL);
-       DEBUG_SERVER_MSG(" +------------------------------------------------+");
+       SECURE_LOGD(" +------------------------------------------------+");
 }
 #endif
 static bool __activation_iter_cb(const char *package,
@@ -1037,6 +1272,8 @@ static bool __activation_iter_cb(const char *package,
                        /* FIXME : need commit?? check and return */
                }
 
+               /* in others category, it is changed just exact handler state */
+               /* FIXME : when 'others setting' is applied, conflict case should be handled */
                /* stop iterating */
                if (category == NET_NFC_CARD_EMULATION_CATEGORY_OTHER) {
                        ret = false;
@@ -1044,6 +1281,8 @@ static bool __activation_iter_cb(const char *package,
        } else {
                if (category == NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT &&
                        handler->activated[category] == true) {
+                       /* deactivate another handlers in payment category */
+
                        handler->activated[category] = false;
 
                        for (i = 0; i < handler->aids[category]->len; i++) {
@@ -1081,17 +1320,17 @@ net_nfc_error_e net_nfc_server_route_table_set_handler_activation(
 #endif
 
        if (ret == 1) {
-               INFO_MSG("activated category [%d], package [%s]", category, package);
+               SECURE_MSG("activated category [%d], package [%s]", category, package);
 
                net_nfc_controller_secure_element_commit_routing(&result);
 
                result = NET_NFC_OK;
        } else if (ret == 0) {
-               DEBUG_ERR_MSG("package not found : [%s]", package);
+               SECURE_MSG("package not found : [%s]", package);
 
                result = NET_NFC_NO_DATA_FOUND;
        } else {
-               DEBUG_ERR_MSG("wrong result : [%s][%d]", package, ret);
+               SECURE_MSG("wrong result : [%s][%d]", package, ret);
 
                result = NET_NFC_OPERATION_FAIL;
        }
@@ -1195,12 +1434,11 @@ static bool __find_handler_iter_cb(const char *package,
        aid = net_nfc_server_route_table_find_aid(package,
                (const char *)params[0]);
        if (aid != NULL) {
-               if (aid->category != NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT ||
-                       handler->activated[aid->category] == true) {
+               if (handler->activated[aid->category] == true) {
                        params[1] = handler;
                        result = false;
                } else {
-                       DEBUG_SERVER_MSG("not activated payment aid, [%s]", aid->aid);
+                       SECURE_MSG("not activated payment aid, [%s]", aid->aid);
                }
        }
 
@@ -1244,20 +1482,59 @@ aid_info_t *net_nfc_server_route_table_find_first_matched_aid(const char *aid)
        return NULL;
 }
 
+static bool __check_conflict_aid_cb(
+       const char *package, route_table_handler_t *handler,
+       aid_info_t *aid, void *user_data)
+{
+       bool result;
+       gpointer *params = (gpointer *)user_data;
+
+       if (g_strcmp0(package, (const char *)params[0]) == 0) {
+               /* skip same package */
+               return true;
+       }
+
+       if (handler->activated[aid->category] == true &&
+               g_strcmp0(aid->aid, (const char *)params[1]) == 0) {
+               net_nfc_error_e *res = params[3];
+
+               SECURE_MSG("conflict!!! [%s|%s] vs [%s|%s]", handler->package, aid->aid, (const char *)params[0], (const char *)params[1]);
+
+               *res = NET_NFC_DATA_CONFLICTED;
+               result = false;
+       } else {
+               result = true;
+       }
+
+       return result;
+}
+
 static net_nfc_error_e __route_table_add_aid(const char *id,
        const char *package, net_nfc_se_type_e se_type,
        net_nfc_card_emulation_category_t category,
-       const char *aid, bool unlock, int power, bool manifest)
+       const char *aid, bool unlock, int power, bool manifest, bool routing)
 {
-       net_nfc_error_e result;
+       net_nfc_error_e result = NET_NFC_OK;
        route_table_handler_t *data;
 
+       /* check conflict */
+       if (category == NET_NFC_CARD_EMULATION_CATEGORY_OTHER) {
+               gpointer params[4];
+
+               params[0] = (gpointer)package;
+               params[1] = (gpointer)aid;
+               params[2] = (gpointer)category;
+               params[3] = &result;
+
+               net_nfc_server_route_table_iterate_aids(__check_conflict_aid_cb, params);
+       }
+
        data = net_nfc_server_route_table_find_handler(package);
        if (data != NULL) {
                if (net_nfc_server_route_table_find_aid(package, aid) == NULL) {
                        aid_info_t *info;
 
-                       DEBUG_SERVER_MSG("new aid, package [%s], se_type [%d], category [%d], aid [%s], ", package, se_type, category, aid);
+                       SECURE_MSG("new aid, package [%s], se_type [%d], category [%d], aid [%s], ", package, se_type, category, aid);
 
                        info = g_new0(aid_info_t, 1);
 
@@ -1265,29 +1542,39 @@ static net_nfc_error_e __route_table_add_aid(const char *id,
                        info->se_type = se_type;
                        info->category = category;
                        info->unlock = unlock;
-                       info->power = power;
+                       info->power = 0x39;/*Temp code for Screen off Transaction*/
                        info->manifest = manifest;
 
                        g_ptr_array_add(data->aids[0], info);
                        g_ptr_array_add(data->aids[category], info);
 
-                       if (se_type != net_nfc_server_se_get_se_type()) {
-                               if (data->activated[category] == true) {
-                                       INFO_MSG("routing... package [%s], aid [%s], ", package, aid);
+                       if (result == NET_NFC_OK) {
+                               if (routing == true) {
+                                       if (se_type != net_nfc_server_se_get_se_type()) {
+                                               if (data->activated[category] == true) {
+                                                       SECURE_MSG("routing... package [%s], aid [%s], ", package, aid);
 
-                                       result = __do_routing_aid(info, true);
-                               } else {
-                                       INFO_MSG("not activated handler, aid [%s]", aid);
+                                                       result = __do_routing_aid(info, true);
+                                               } else {
+                                                       SECURE_MSG("not activated handler, aid [%s]", aid);
 
-                                       result = NET_NFC_OK;
+                                                       result = NET_NFC_OK;
+                                               }
+                                       } else {
+                                               SECURE_MSG("route to default SE... skip, aid [%s]", aid);
+
+                                               result = NET_NFC_OK;
+                                       }
                                }
-                       } else {
-                               INFO_MSG("route to default SE... skip, aid [%s]", aid);
 
-                               result = NET_NFC_OK;
+                               if (data->aids[0]->len > AID_MAX) {
+                                       SECURE_MSG("no space. cannot add aid, [%d]", result);
+
+                                       result = NET_NFC_INSUFFICIENT_STORAGE;
+                               }
                        }
                } else {
-                       DEBUG_ERR_MSG("already exist, aid [%s]", aid);
+                       SECURE_MSG("already exist, aid [%s]", aid);
 
                        result = NET_NFC_ALREADY_REGISTERED;
                }
@@ -1317,7 +1604,7 @@ static net_nfc_error_e __route_table_del_aid(const char *id,
 
                        if (g_ascii_strcasecmp(info->aid, aid) == 0) {
                                if (force == true || info->manifest == false) {
-                                       INFO_MSG("remove aid, package [%s], aid [%s]", package, aid);
+                                       SECURE_MSG("remove aid, package [%s], aid [%s]", package, aid);
 
                                        if (info->is_routed == true) {
                                                __do_unrouting_aid(info, true);
@@ -1328,7 +1615,7 @@ static net_nfc_error_e __route_table_del_aid(const char *id,
 
                                        result = NET_NFC_OK;
                                } else {
-                                       DEBUG_SERVER_MSG("cannot remove aid because it stored in manifest, aid [%s]", info->aid);
+                                       SECURE_MSG("cannot remove aid because it stored in manifest, aid [%s]", info->aid);
 
                                        result = NET_NFC_OPERATION_FAIL;
                                }
@@ -1351,7 +1638,7 @@ net_nfc_error_e net_nfc_server_route_table_add_aid(const char *id,
        result = net_nfc_server_route_table_add_handler(id, package);
        if (result == NET_NFC_OK) {
                result = __route_table_add_aid(id,
-                       package, se_type, category, aid, true, 1, false);
+                       package, se_type, category, aid, true, 1, false, true);
                if (result == NET_NFC_OK) {
                        result = __insert_into_db(package, se_type, category, aid, true, 1, false);
                        if (result != NET_NFC_OK) {
@@ -1362,6 +1649,10 @@ net_nfc_error_e net_nfc_server_route_table_add_aid(const char *id,
                                        DEBUG_ERR_MSG("__route_table_del_aid failed, [%d]", result);
                                }
                        }
+               } else if (result == NET_NFC_INSUFFICIENT_STORAGE) {
+                       DEBUG_ERR_MSG("no space. cannot add aid, [%d]", result);
+
+                       __route_table_del_aid(id, package, aid, false);
                } else {
                        DEBUG_ERR_MSG("net_nfc_server_route_table_add_aid failed, [%d]", result);
                }
@@ -1414,7 +1705,7 @@ net_nfc_error_e net_nfc_server_route_table_del_aids(const char *id,
                        aid_info_t *info = (aid_info_t *)data->aids[0]->pdata[i];
 
                        if (force == true || info->manifest == false) {
-                               DEBUG_SERVER_MSG("remove aid, package [%s], aid [%s]", package, info->aid);
+                               SECURE_MSG("remove aid, package [%s], aid [%s]", package, info->aid);
 
                                if (info->is_routed == true &&
                                        __do_unrouting_aid(info, false) == NET_NFC_OK) {
@@ -1429,11 +1720,11 @@ net_nfc_error_e net_nfc_server_route_table_del_aids(const char *id,
                                g_ptr_array_remove(data->aids[info->category], info);
                                g_ptr_array_remove_index(data->aids[0], i);
                        } else {
-                               DEBUG_SERVER_MSG("cannot remove aid because it stored in manifest, aid [%s]", info->aid);
+                               SECURE_MSG("cannot remove aid because it stored in manifest, aid [%s]", info->aid);
                        }
                }
        } else {
-               DEBUG_SERVER_MSG("not found, package [%s]", package);
+               SECURE_MSG("not found, package [%s]", package);
        }
 
        if (need_commit == true) {
@@ -1446,7 +1737,7 @@ net_nfc_error_e net_nfc_server_route_table_del_aids(const char *id,
        return result;
 }
 
-void net_nfc_server_route_table_iterate_aid(const char *package,
+void net_nfc_server_route_table_iterate_aids(
        net_nfc_server_route_table_aid_iter_cb cb, void *user_data)
 {
        GHashTableIter iter;
@@ -1464,7 +1755,32 @@ void net_nfc_server_route_table_iterate_aid(const char *package,
                for (i = 0; i < data->aids[0]->len; i++) {
                        info = (aid_info_t *)data->aids[0]->pdata[i];
 
-                       cb((const char *)key, data, info, user_data);
+                       if (cb((const char *)key, data, info, user_data) == false) {
+                               break;
+                       }
+               }
+       }
+}
+
+void net_nfc_server_route_table_iterate_handler_aids(const char *package,
+       net_nfc_server_route_table_aid_iter_cb cb, void *user_data)
+{
+       route_table_handler_t *data;
+       int i;
+       aid_info_t *info;
+
+       if (routing_handlers == NULL)
+               return;
+
+       data = (route_table_handler_t *)g_hash_table_lookup(routing_handlers,
+               package);
+       if (data != NULL) {
+               for (i = 0; i < data->aids[0]->len; i++) {
+                       info = (aid_info_t *)data->aids[0]->pdata[i];
+
+                       if (cb(package, data, info, user_data) == false) {
+                               break;
+                       }
                }
        }
 }
@@ -1518,17 +1834,16 @@ net_nfc_error_e net_nfc_server_route_table_del_aid_by_id(const char *id,
 }
 
 
-void net_nfc_server_route_table_iterate_aid_by_id(const char *id,
+void net_nfc_server_route_table_iterate_aids_by_id(const char *id,
        net_nfc_server_route_table_aid_iter_cb cb, void *user_data)
 {
        char package[1024];
 
        if (__get_package_name(id, package, sizeof(package)) == true) {
-               net_nfc_server_route_table_iterate_aid(package, cb, user_data);
+               net_nfc_server_route_table_iterate_handler_aids(package, cb, user_data);
        }
 }
 
-
 net_nfc_error_e net_nfc_server_route_table_insert_aid_into_db(
        const char *package, net_nfc_se_type_e se_type,
        net_nfc_card_emulation_category_t category,
@@ -1543,14 +1858,14 @@ net_nfc_error_e net_nfc_server_route_table_insert_aid_into_db(
                if (result == NET_NFC_OK) {
                        result = __route_table_add_aid(NULL,
                                package, se_type, category, aid,
-                               unlock, power, true);
+                               unlock, power, true, net_nfc_server_manager_get_active());
                        if (result == NET_NFC_OK) {
                                if (category == NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT) {
                                        char *handler;
 
                                        handler = vconf_get_str(VCONFKEY_NFC_PAYMENT_HANDLERS);
                                        if (handler == NULL || strlen(handler) == 0) {
-                                               DEBUG_SERVER_MSG("set to default payment handler, [%s]", package);
+                                               SECURE_MSG("set to default payment handler, [%s]", package);
 
                                                __activate_handler(package, category);
                                        }
@@ -1563,6 +1878,26 @@ net_nfc_error_e net_nfc_server_route_table_insert_aid_into_db(
                                                __activate_handler(package, category);
                                        }
                                }
+                       } else if (result == NET_NFC_INSUFFICIENT_STORAGE) {
+                               net_nfc_card_emulation_category_t temp;
+
+                               DEBUG_ERR_MSG("no space. deactive handler, [%d]", result);
+
+                               for (temp = NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT;
+                                       temp < NET_NFC_CARD_EMULATION_CATEGORY_MAX;
+                                       temp++) {
+                                       __deactivate_handler(package, temp);
+                               }
+                       } else if (result == NET_NFC_DATA_CONFLICTED) {
+                               net_nfc_card_emulation_category_t temp;
+
+                               DEBUG_ERR_MSG("conflict occured. deactive handler, [%d]", result);
+
+                               for (temp = NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT;
+                                       temp < NET_NFC_CARD_EMULATION_CATEGORY_MAX;
+                                       temp++) {
+                                       __deactivate_handler(package, temp);
+                               }
                        } else {
                                DEBUG_ERR_MSG("net_nfc_server_route_table_add_aid failed, [%d]", result);
                        }
@@ -1608,38 +1943,49 @@ static bool __update_table_iter_cb(const char *package,
        net_nfc_se_type_e se_type;
        net_nfc_card_emulation_category_t category;
        int *ret;
+       bool routing;
 
        se_type = (net_nfc_se_type_e)params[0];
        ret = (int *)params[1];
+       routing = (bool)params[2];
 
        for (category = NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT;
                category < NET_NFC_CARD_EMULATION_CATEGORY_MAX;
                category++) {
-               if (__is_activated_handler(category, package) == true) {
-                       for (i = 0; i < handler->aids[category]->len; i++) {
-                               info = (aid_info_t *)handler->aids[category]->pdata[i];
-
-                               if (info->se_type == se_type) {
-                                       /* unroute aid when se type is same with default */
-                                       if (info->is_routed == true) {
-                                               __do_unrouting_aid(info, false);
+               if (__is_preferred_handler(category, package) == true ||
+                       (preferred_handler == NULL && __is_activated_handler(category, package) == true)) {
+                       /* update used aid count */
+                       used_aids_count[category] += handler->aids[category]->len;
+                       used_aids_count[NET_NFC_CARD_EMULATION_CATEGORY_MAX] += handler->aids[category]->len;
+
+                       if (routing == true) {
+                               for (i = 0; i < handler->aids[category]->len; i++) {
+                                       info = (aid_info_t *)handler->aids[category]->pdata[i];
+
+                                       if (info->se_type == se_type) {
+                                               /* unroute aid when se type is same with default */
+                                               if (info->is_routed == true) {
+                                                       __do_unrouting_aid(info, false);
+                                               }
+
+                                               continue;
                                        }
 
-                                       continue;
-                               }
-
-                               if (info->is_routed == false) {
-                                       __do_routing_aid(info, false);
+                                       if (info->is_routed == false) {
+                                               __do_routing_aid(info, false);
+                                       }
                                }
                        }
 
                        handler->activated[category] = true;
                } else {
-                       for (i = 0; i < handler->aids[category]->len; i++) {
-                               info = (aid_info_t *)handler->aids[category]->pdata[i];
+                       if (routing == true) {
+                               for (i = 0; i < handler->aids[category]->len; i++) {
+                                       info = (aid_info_t *)handler->aids[category]->pdata[i];
 
-                               if (info->is_routed == true) {
-                                       __do_unrouting_aid(info, false);
+                                       if (info->is_routed == true) {
+                                               __do_unrouting_aid(info, false);
+                                       }
                                }
                        }
 
@@ -1652,12 +1998,13 @@ static bool __update_table_iter_cb(const char *package,
        return true;
 }
 
-net_nfc_error_e net_nfc_server_route_table_do_update(void)
+net_nfc_error_e net_nfc_server_route_table_do_update(bool routing)
 {
        net_nfc_error_e result = NET_NFC_OK;
        gpointer params[3];
        net_nfc_se_type_e se_type;
        int ret = 0;
+       net_nfc_card_emulation_category_t category;
 
 //     if (net_nfc_controller_secure_element_clear_aid_table(&result) == false) {
 //             DEBUG_ERR_MSG("net_nfc_controller_secure_element_clear_aid_table failed, [%d]", result);
@@ -1667,12 +2014,19 @@ net_nfc_error_e net_nfc_server_route_table_do_update(void)
 
        params[0] = (gpointer)se_type;
        params[1] = (gpointer)&ret;
+       params[2] = (gpointer)routing;
 
-       net_nfc_server_route_table_iterate_handler(__update_table_iter_cb,
+       for (category = NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT;
+               category <= NET_NFC_CARD_EMULATION_CATEGORY_MAX;
+               category++) {
+               used_aids_count[category] = 0;
+       }
+
+       net_nfc_server_route_table_iterate_handler_activated_last(__update_table_iter_cb,
                (void *)params);
 
        if (ret == 0) {
-               INFO_MSG("routing table update complete");
+               INFO_MSG("routing table update complete, [%d/%d]", used_aids_count[NET_NFC_CARD_EMULATION_CATEGORY_MAX], maximum_aids_count);
 
                result = NET_NFC_OK;
        } else if (ret > 1) {
@@ -1685,7 +2039,9 @@ net_nfc_error_e net_nfc_server_route_table_do_update(void)
                result = NET_NFC_OPERATION_FAIL;
        }
 
-       net_nfc_controller_secure_element_commit_routing(&result);
+       if (routing == true) {
+               net_nfc_controller_secure_element_commit_routing(&result);
+       }
 
 #ifdef PRINT_TABLE
        __display_route_table();
@@ -1693,3 +2049,16 @@ net_nfc_error_e net_nfc_server_route_table_do_update(void)
 
        return result;
 }
+
+net_nfc_error_e net_nfc_server_route_table_get_storage_info(
+       net_nfc_card_emulation_category_t category, int *used, int *max)
+{
+       if (used == NULL || max == NULL) {
+               return NET_NFC_NULL_PARAMETER;
+       }
+
+       *used = used_aids_count[category];
+       *max = maximum_aids_count;
+
+       return NET_NFC_OK;
+}
index a8be305..bbb3683 100755 (executable)
@@ -30,6 +30,7 @@
 #include "net_nfc_server_manager.h"
 #include "net_nfc_server_se.h"
 #include "net_nfc_app_util_internal.h"
+#include "net_nfc_service_access_control.h"
 #include "net_nfc_server_route_table.h"
 
 enum
@@ -47,6 +48,26 @@ static net_nfc_target_handle_s *gdbus_ese_handle;
 
 static int gdbus_uicc_ready;
 
+static unsigned char char_to_num[] =
+{
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+        0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
+       -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+};
+
 /* server_side */
 typedef struct _ServerSeData ServerSeData;
 
@@ -103,6 +124,15 @@ struct _SeDataApdu
        GVariant *data;
 };
 
+typedef struct _SeSetPreferred SeSetPreferred;
+
+struct _SeSetPreferred
+{
+       NetNfcGDbusSecureElement *object;
+       GDBusMethodInvocation *invocation;
+       gboolean state;
+};
+
 typedef struct _ChangeCardEmulMode ChangeCardEmulMode;
 
 struct _ChangeCardEmulMode
@@ -192,6 +222,8 @@ static net_nfc_card_emulation_mode_t __se_get_se_state(
 static net_nfc_se_type_e __se_get_se_type(
        net_nfc_secure_element_policy_e policy);
 
+static void __stop_lcd_on_timer();
+
 
 static net_nfc_secure_element_policy_e current_policy;
 
@@ -256,7 +288,7 @@ net_nfc_card_emulation_mode_t net_nfc_server_se_get_se_state()
 
        state = __se_get_se_state(se_policy);
 
-       DEBUG_SERVER_MSG("se state [%s]", state ? "activated" : "deactivated");
+       SECURE_MSG("se state [%s]", state ? "activated" : "deactivated");
 
        return state;
 }
@@ -394,11 +426,12 @@ net_nfc_se_type_e net_nfc_server_se_get_se_type()
 {
        net_nfc_se_type_e se_type;
        int wallet_mode = 0;
-/*
+
        if (vconf_get_int(VCONFKEY_NFC_WALLET_MODE, &wallet_mode) != 0) {
                DEBUG_ERR_MSG("vconf_get_int failed");
        }
 
+       /*TODO : Modify the wallet mode*/
        if (wallet_mode != NET_NFC_WALLET_MODE_UICC) {
                int se_policy = SECURE_ELEMENT_POLICY_INVALID;
 
@@ -411,8 +444,8 @@ net_nfc_se_type_e net_nfc_server_se_get_se_type()
                se_type = NET_NFC_SE_TYPE_UICC;
        }
 
-       DEBUG_SERVER_MSG("active se [%s]", __se_get_se_name(se_type));
-*/
+       SECURE_MSG("active se [%s]", __se_get_se_name(se_type));
+
        return se_type;
 }
 
@@ -421,7 +454,7 @@ net_nfc_error_e net_nfc_server_se_set_se_type(net_nfc_se_type_e type)
        net_nfc_error_e result;
        int se_policy = SECURE_ELEMENT_POLICY_INVALID;
        int wallet_mode = 0;
-/*
+
        if (vconf_get_int(VCONFKEY_NFC_WALLET_MODE, &wallet_mode) != 0) {
                DEBUG_ERR_MSG("vconf_get_int failed");
 
@@ -483,11 +516,11 @@ net_nfc_error_e net_nfc_server_se_set_se_type(net_nfc_se_type_e type)
                        result = NET_NFC_OPERATION_FAIL;
                }
        } else {
-               DEBUG_ERR_MSG("activated already, [%s]", __se_get_se_name(type));
+               SECURE_MSG("activated already, [%s]", __se_get_se_name(type));
 
                result = NET_NFC_INVALID_STATE;
        }
-*/
+
        return result;
 }
 
@@ -495,7 +528,7 @@ static net_nfc_secure_element_policy_e __se_get_se_policy()
 {
        int se_policy = SECURE_ELEMENT_POLICY_INVALID;
        int wallet_mode = 0;
-/*
+
        if (vconf_get_int(VCONFKEY_NFC_WALLET_MODE, &wallet_mode) != 0) {
                DEBUG_ERR_MSG("vconf_get_int failed");
        }
@@ -528,7 +561,7 @@ static net_nfc_secure_element_policy_e __se_get_se_policy()
                        se_policy = SECURE_ELEMENT_POLICY_HCE_OFF;
                }
        }
-*/
+
 
        return se_policy;
 }
@@ -621,7 +654,7 @@ static net_nfc_target_handle_s *net_nfc_server_se_open_ese()
                {
                        net_nfc_server_se_set_current_ese_handle(handle);
 
-                       DEBUG_SERVER_MSG("handle [%p]", handle);
+                       SECURE_MSG("handle [%p]", handle);
                }
                else
                {
@@ -934,9 +967,9 @@ net_nfc_error_e net_nfc_server_se_apply_se_policy(
 {
        net_nfc_error_e result = NET_NFC_OK;
 
-       net_nfc_controller_secure_element_set_clear_routing_entry(NET_NFC_SE_TECH_ENTRY, &result);
+       net_nfc_controller_secure_element_clear_routing_entry(NET_NFC_SE_TECH_ENTRY, &result);
 
-       net_nfc_controller_secure_element_set_clear_routing_entry(NET_NFC_SE_PROTOCOL_ENTRY, &result);
+       net_nfc_controller_secure_element_clear_routing_entry(NET_NFC_SE_PROTOCOL_ENTRY, &result);
 
        switch (policy)
        {
@@ -1089,7 +1122,7 @@ net_nfc_error_e net_nfc_server_se_apply_se_current_policy()
 
        policy = __se_get_se_policy();
 
-       DEBUG_SERVER_MSG("current se policy [%d]", policy);
+       SECURE_MSG("current se policy [%d]", policy);
 
        return net_nfc_server_se_apply_se_policy(policy);
 }
@@ -1104,7 +1137,7 @@ net_nfc_error_e net_nfc_server_se_change_wallet_mode(
 
        new_policy = __se_get_se_policy();
 
-       DEBUG_SERVER_MSG("current policy [%d], new policy [%d]", current_policy, new_policy);
+       SECURE_MSG("current policy [%d], new policy [%d]", current_policy, new_policy);
 
        if (__se_get_se_type(current_policy) != __se_get_se_type(new_policy)) {
                is_type_changed = true;
@@ -1361,6 +1394,26 @@ static void se_open_secure_element_thread_func(gpointer user_data)
        g_assert(detail->object != NULL);
        g_assert(detail->invocation != NULL);
 
+#ifdef ACCESS_CONTROL
+       pid_t pid;
+       const char *name;
+       bool ret = false;
+
+       name = g_dbus_method_invocation_get_sender(detail->invocation);
+       pid = net_nfc_server_gdbus_get_pid(name);
+
+       ret = net_nfc_service_check_access_control(pid,
+               NET_NFC_ACCESS_CONTROL_PLATFORM);
+
+       if (ret == false)
+       {
+               DEBUG_ERR_MSG("access control denied");
+               result = NET_NFC_SECURITY_FAIL;
+
+               goto END;
+       }
+#endif
+
        if (detail->se_type == NET_NFC_SE_TYPE_UICC)
        {
                handle = (net_nfc_target_handle_s *)_se_uicc_open();
@@ -1381,7 +1434,7 @@ static void se_open_secure_element_thread_func(gpointer user_data)
                {
                        result = NET_NFC_OK;
 
-                       DEBUG_SERVER_MSG("handle [%p]", handle);
+                       SECURE_MSG("handle [%p]", handle);
 
                        /* increase client reference count */
                        net_nfc_server_gdbus_increase_se_count(
@@ -1399,7 +1452,9 @@ static void se_open_secure_element_thread_func(gpointer user_data)
                result = NET_NFC_INVALID_PARAM;
                handle = NULL;
        }
-
+#ifdef ACCESS_CONTROL
+END :
+#endif
        net_nfc_gdbus_secure_element_complete_open_secure_element(
                detail->object,
                detail->invocation,
@@ -1603,8 +1658,44 @@ static void se_set_data_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
+#ifdef ACCESS_CONTROL
+       pid_t pid;
+       const char *name;
+       bool ret = true;
+
+       name = g_dbus_method_invocation_get_sender(data->invocation);
+       pid = net_nfc_server_gdbus_get_pid(name);
+
+       if (data->se_type == NET_NFC_SE_TYPE_UICC)
+       {
+               ret = net_nfc_service_check_access_control(pid,
+                       NET_NFC_ACCESS_CONTROL_PLATFORM |
+                       NET_NFC_ACCESS_CONTROL_UICC);
+       }
+       else if (data->se_type == NET_NFC_SE_TYPE_ESE)
+       {
+               ret = net_nfc_service_check_access_control(pid,
+                       NET_NFC_ACCESS_CONTROL_PLATFORM |
+                       NET_NFC_ACCESS_CONTROL_ESE);
+       }
+       else if (data->se_type == NET_NFC_SE_TYPE_HCE)
+       {
+               /* TODO */
+       }
+
+       if (ret == false)
+       {
+               DEBUG_ERR_MSG("access control denied");
+               result = NET_NFC_SECURITY_FAIL;
+
+               goto END;
+       }
+#endif
        result = net_nfc_server_se_set_se_type(data->se_type);
 
+#ifdef ACCESS_CONTROL
+END :
+#endif
        if (result == NET_NFC_OK)
                isTypeChanged = TRUE;
 
@@ -1766,8 +1857,47 @@ static void _se_change_card_emulation_mode_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
+#ifdef ACCESS_CONTROL
+       pid_t pid;
+       net_nfc_se_type_e se_type;
+       const char *name;
+       bool ret = true;
+
+       name = g_dbus_method_invocation_get_sender(data->invocation);
+       pid = net_nfc_server_gdbus_get_pid(name);
+
+       se_type = net_nfc_server_se_get_se_type();
+
+       if (se_type == NET_NFC_SE_TYPE_UICC)
+       {
+               ret = net_nfc_service_check_access_control(pid,
+                       NET_NFC_ACCESS_CONTROL_PLATFORM |
+                       NET_NFC_ACCESS_CONTROL_UICC);
+       }
+       else if (se_type == NET_NFC_SE_TYPE_ESE)
+       {
+               ret = net_nfc_service_check_access_control(pid,
+                       NET_NFC_ACCESS_CONTROL_PLATFORM |
+                       NET_NFC_ACCESS_CONTROL_ESE);
+       }
+       else if (se_type == NET_NFC_SE_TYPE_HCE)
+       {
+               /* TODO */
+       }
+
+       if (ret == false)
+       {
+               DEBUG_ERR_MSG("access control denied");
+               result = NET_NFC_SECURITY_FAIL;
+
+               goto END;
+       }
+#endif
        result = net_nfc_server_se_set_se_state(data->mode);
 
+#ifdef ACCESS_CONTROL
+END :
+#endif
        net_nfc_gdbus_secure_element_complete_set_card_emulation(
                data->object, data->invocation, result);
 
@@ -1878,10 +2008,47 @@ static void se_enable_transaction_fg_dispatch(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
+#ifdef ACCESS_CONTROL
+       pid_t pid;
+       const char *name;
+       bool ret = false;
+
+       name = g_dbus_method_invocation_get_sender(data->invocation);
+       pid = net_nfc_server_gdbus_get_pid(name);
+
+       net_nfc_se_type_e se_type = net_nfc_server_se_get_se_type();
+
+       if (se_type == NET_NFC_SE_TYPE_UICC)
+       {
+               ret = net_nfc_service_check_access_control(pid,
+                       NET_NFC_ACCESS_CONTROL_PLATFORM |
+                       NET_NFC_ACCESS_CONTROL_UICC);
+       }
+       else if (se_type == NET_NFC_SE_TYPE_ESE)
+       {
+               ret = net_nfc_service_check_access_control(pid,
+                       NET_NFC_ACCESS_CONTROL_PLATFORM |
+                       NET_NFC_ACCESS_CONTROL_ESE);
+       }
+       else if (se_type == NET_NFC_SE_TYPE_HCE)
+       {
+               /* TODO */
+       }
+
+       if (ret == false)
+       {
+               DEBUG_ERR_MSG("access control denied");
+               result = NET_NFC_SECURITY_FAIL;
+
+               goto END;
+       }
+#endif
        result = net_nfc_server_gdbus_set_transaction_fg_dispatch(
                g_dbus_method_invocation_get_sender(data->invocation),
                data->FgDispatch);
-
+#ifdef ACCESS_CONTROL
+END :
+#endif
        net_nfc_gdbus_secure_element_complete_set_transaction_fg_dispatch (
                data->object,
                data->invocation,
@@ -2096,11 +2263,33 @@ static void se_set_default_route_thread_func(gpointer user_data)
        g_assert(detail->object != NULL);
        g_assert(detail->invocation != NULL);
 
-       INFO_MSG(">>> Call se_set_default_route_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_set_default_route_thread_func");
+
+#ifdef ACCESS_CONTROL
+       pid_t pid;
+       const char *name;
+       bool ret = false;
+
+       name = g_dbus_method_invocation_get_sender(detail->invocation);
+       pid = net_nfc_server_gdbus_get_pid(name);
+
+       ret = net_nfc_service_check_access_control(pid,
+               NET_NFC_ACCESS_CONTROL_PLATFORM);
+       if (ret == false)
+       {
+               DEBUG_ERR_MSG("access control denied");
+               result = NET_NFC_SECURITY_FAIL;
+
+               goto END;
+       }
+#endif
 
        net_nfc_controller_secure_element_set_default_route(detail->switch_on,
                detail->switch_off, detail->battery_off, &result);
 
+#ifdef ACCESS_CONTROL
+END :
+#endif
        net_nfc_gdbus_secure_element_complete_set_default_route(
                detail->object,
                detail->invocation,
@@ -2189,7 +2378,7 @@ static void se_is_activated_aid_handler_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_is_activated_aid_handler_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_is_activated_aid_handler_thread_func");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -2302,7 +2491,7 @@ static void se_is_activated_category_handler_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_is_activated_category_handler_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_is_activated_category_handler_thread_func");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -2391,8 +2580,8 @@ static bool __get_aids_iter_cb(const char *package,
 {
        gpointer* params = (gpointer *)user_data;
 
-       if (aid->category == (net_nfc_card_emulation_category_t)params[0]) {
-               g_variant_builder_add((GVariantBuilder *)params[1],
+       if (aid->category == (net_nfc_card_emulation_category_t)params[0] && aid->se_type == (net_nfc_se_type_e)params[1]) {
+               g_variant_builder_add((GVariantBuilder *)params[2],
                        "(sb)", aid->aid, (gboolean)aid->manifest);
        }
 
@@ -2406,22 +2595,23 @@ static void se_get_registered_aids_thread_func(gpointer user_data)
        const char *id;
        GVariantBuilder builder;
        GVariant *aids;
-       gpointer params[2];
+       gpointer params[3];
 
        g_assert(data != NULL);
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_get_registered_aids_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_get_registered_aids_thread_func");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
        g_variant_builder_init(&builder, G_VARIANT_TYPE("a(sb)"));
 
        params[0] = (gpointer)data->category;
-       params[1] = &builder;
+       params[1] = (gpointer)data->se_type;
+       params[2] = &builder;
 
-       net_nfc_server_route_table_iterate_aid_by_id(id, __get_aids_iter_cb,
+       net_nfc_server_route_table_iterate_aids_by_id(id, __get_aids_iter_cb,
                params);
 
        aids = g_variant_builder_end(&builder);
@@ -2513,7 +2703,9 @@ static void se_register_aid_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_register_aid_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_register_aid_thread_func");
+
+       //Fix here : get preferred state and restrict
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -2617,7 +2809,7 @@ static void se_unregister_aid_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_unregister_aid_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_unregister_aid_thread_func");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -2717,7 +2909,7 @@ static void se_unregister_aids_thread_func(gpointer user_data)
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_unregister_aids_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_unregister_aids_thread_func");
 
        id = g_dbus_method_invocation_get_sender(data->invocation);
 
@@ -2808,7 +3000,7 @@ static void se_add_route_aid_thread_func(gpointer user_data)
        g_assert(detail->object != NULL);
        g_assert(detail->invocation != NULL);
 
-       INFO_MSG(">>> Call se_add_route_aid_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_add_route_aid_thread_func");
 
        result = net_nfc_server_route_table_insert_aid_into_db(detail->package,
                detail->se_type, detail->category, detail->aid,
@@ -2842,7 +3034,7 @@ static gboolean se_handle_add_route_aid(
 
        INFO_MSG(">>> REQUEST from [%s]",
                g_dbus_method_invocation_get_sender(invocation));
-#if 0
+
        /* check privilege and update client context */
        if (net_nfc_server_gdbus_check_privilege(invocation, NET_NFC_PRIVILEGE_NFC_CARD_EMUL) == false) {
                DEBUG_ERR_MSG("permission denied, and finished request");
@@ -2850,7 +3042,7 @@ static gboolean se_handle_add_route_aid(
 
                goto ERROR;
        }
-#endif
+
        data = g_try_new0(SeDataAid, 1);
        if (data == NULL)
        {
@@ -2869,7 +3061,7 @@ static gboolean se_handle_add_route_aid(
        data->unlock = unlock;
        data->power = power;
 
-       INFO_MSG(">>> hce_add_aid_thread_func");
+       DEBUG_SERVER_MSG(">>> hce_add_aid_thread_func");
 
        if (net_nfc_server_controller_async_queue_push_force(
                se_add_route_aid_thread_func, data) == FALSE)
@@ -2909,7 +3101,7 @@ static void se_remove_route_aid_thread_func(gpointer user_data)
        g_assert(detail->object != NULL);
        g_assert(detail->invocation != NULL);
 
-       INFO_MSG(">>> Call se_remove_route_aid_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_remove_route_aid_thread_func");
 
        result = net_nfc_server_route_table_delete_aid_from_db(detail->package,
                detail->aid);
@@ -2939,7 +3131,7 @@ static gboolean se_handle_remove_route_aid(
 
        INFO_MSG(">>> REQUEST from [%s]",
                g_dbus_method_invocation_get_sender(invocation));
-#if 0
+
        /* check privilege and update client context */
        if (net_nfc_server_gdbus_check_privilege(invocation, NET_NFC_PRIVILEGE_NFC_CARD_EMUL) == false) {
                DEBUG_ERR_MSG("permission denied, and finished request");
@@ -2947,7 +3139,7 @@ static gboolean se_handle_remove_route_aid(
 
                goto ERROR;
        }
-#endif
+
        data = g_try_new0(SeDataAid, 1);
        if (data == NULL)
        {
@@ -3000,7 +3192,7 @@ static void se_remove_package_aids_thread_func(gpointer user_data)
        g_assert(detail->object != NULL);
        g_assert(detail->invocation != NULL);
 
-       INFO_MSG(">>> Call se_remove_package_aids_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_remove_package_aids_thread_func");
 
        result = net_nfc_server_route_table_delete_aids_from_db(detail->package);
        if (result == NET_NFC_OK) {
@@ -3023,7 +3215,6 @@ static gboolean se_handle_remove_package_aids(
        NetNfcGDbusSecureElement *object,
        GDBusMethodInvocation *invocation,
        const gchar *package,
-       const gchar *aid,
        GVariant *smack_privilege)
 {
        SeDataAid *data = NULL;
@@ -3031,7 +3222,7 @@ static gboolean se_handle_remove_package_aids(
 
        INFO_MSG(">>> REQUEST from [%s]",
                g_dbus_method_invocation_get_sender(invocation));
-#if 0
+
        /* check privilege and update client context */
        if (net_nfc_server_gdbus_check_privilege(invocation, NET_NFC_PRIVILEGE_NFC_CARD_EMUL) == false) {
                DEBUG_ERR_MSG("permission denied, and finished request");
@@ -3039,7 +3230,7 @@ static gboolean se_handle_remove_package_aids(
 
                goto ERROR;
        }
-#endif
+
        data = g_try_new0(SeDataAid, 1);
        if (data == NULL)
        {
@@ -3084,13 +3275,12 @@ ERROR :
 static bool __check_category_iter_cb(const char *package,
        route_table_handler_t *handler, aid_info_t *aid, void *user_data)
 {
-       gpointerparams = (gpointer *)user_data;
+       gpointer *params = (gpointer *)user_data;
 
        if (aid->category == (net_nfc_card_emulation_category_t)params[0]) {
-               g_variant_builder_add((GVariantBuilder *)params[1],
-                       "(s)", package);
+               int *count = (int *)params[2];
 
-               return false;
+               (*count)++;
        }
 
        return true;
@@ -3099,13 +3289,23 @@ static bool __check_category_iter_cb(const char *package,
 static bool __get_handlers_iter_cb(const char *package,
        route_table_handler_t *handler, void *user_data)
 {
+       gpointer *params = (gpointer *)user_data;
+       int count = 0;
+
        /* skip current package */
        if (g_strcmp0(package, "nfc-manager") == 0) {
                return true;
        }
 
-       net_nfc_server_route_table_iterate_aid(package,
-               __check_category_iter_cb, user_data);
+       params[2] = &count;
+
+       net_nfc_server_route_table_iterate_handler_aids(package,
+               __check_category_iter_cb, params);
+
+       if (count > 0) {
+               g_variant_builder_add((GVariantBuilder *)params[1],
+                       "(is)", count, package);
+       }
 
        return true;
 }
@@ -3116,15 +3316,15 @@ static void se_get_registered_handlers_thread_func(gpointer user_data)
        net_nfc_error_e result = NET_NFC_OK;
        GVariantBuilder builder;
        GVariant *handlers;
-       gpointer params[2];
+       gpointer params[3];
 
        g_assert(data != NULL);
        g_assert(data->object != NULL);
        g_assert(data->invocation != NULL);
 
-       INFO_MSG(">>> Call se_get_registered_aids_thread_func");
+       DEBUG_SERVER_MSG(">>> Call se_get_registered_aids_thread_func");
 
-       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(s)"));
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(is)"));
 
 #ifdef ACCESS_CONTROL
        pid_t pid;
@@ -3167,7 +3367,6 @@ END :
 static gboolean se_handle_get_registered_handlers(
        NetNfcGDbusSecureElement *object,
        GDBusMethodInvocation *invocation,
-       guint se_type,
        guint category,
        GVariant *smack_privilege)
 {
@@ -3221,7 +3420,7 @@ ERROR :
        GVariantBuilder builder;
        GVariant *packages;
 
-       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(s)"));
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(is)"));
        packages = g_variant_builder_end(&builder);
 
        net_nfc_gdbus_secure_element_complete_get_registered_handlers(
@@ -3230,69 +3429,462 @@ ERROR :
        return TRUE;
 }
 
-gboolean net_nfc_server_se_init(GDBusConnection *connection)
+static void se_get_handler_storage_info_thread_func(gpointer user_data)
 {
-       GError *error = NULL;
-       gboolean result;
-
-       if (se_skeleton)
-               g_object_unref(se_skeleton);
+       SeDataAid *data = (SeDataAid *)user_data;
+       net_nfc_error_e result = NET_NFC_OK;
+       int used = -1;
+       int max = -1;
 
-       net_nfc_server_route_table_init();
+       g_assert(data != NULL);
+       g_assert(data->object != NULL);
+       g_assert(data->invocation != NULL);
 
-#ifdef ENABLE_TELEPHONY
-       /* initialize UICC */
-       _se_uicc_init();
-#endif
-       se_skeleton = net_nfc_gdbus_secure_element_skeleton_new();
+       DEBUG_SERVER_MSG(">>> Call se_get_handler_storage_info_thread_func");
 
-       g_signal_connect(se_skeleton,
-               "handle-set",
-               G_CALLBACK(se_handle_set),
-               NULL);
+#ifdef ACCESS_CONTROL
+       pid_t pid;
+       const char *name;
+       bool ret = false;
 
-       g_signal_connect(se_skeleton,
-               "handle-get",
-               G_CALLBACK(se_handle_get),
-               NULL);
+       name = g_dbus_method_invocation_get_sender(data->invocation);
+       pid = net_nfc_server_gdbus_get_pid(name);
 
-       g_signal_connect(se_skeleton,
-               "handle-set-card-emulation",
-               G_CALLBACK(se_handle_change_card_emulation_mode),
-               NULL);
+       ret = net_nfc_service_check_access_control(pid,
+               NET_NFC_ACCESS_CONTROL_PLATFORM);
+       if (ret == false)
+       {
+               DEBUG_ERR_MSG("access control denied");
+               result = NET_NFC_SECURITY_FAIL;
 
-       g_signal_connect(se_skeleton,
-               "handle-get-card-emulation",
-               G_CALLBACK(se_handle_get_card_emulation_mode),
-               NULL);
+               goto END;
+       }
+#endif
+       net_nfc_server_route_table_get_storage_info(data->category, &used, &max);
 
-       g_signal_connect(se_skeleton,
-               "handle-open-secure-element",
-               G_CALLBACK(se_handle_open_secure_element),
-               NULL);
+#ifdef ACCESS_CONTROL
+END :
+#endif
+       net_nfc_gdbus_secure_element_complete_get_handler_storage_info(
+               data->object, data->invocation, result, used, max);
 
-       g_signal_connect(se_skeleton,
-               "handle-close-secure-element",
-               G_CALLBACK(se_handle_close_secure_element),
-               NULL);
+       g_object_unref(data->invocation);
+       g_object_unref(data->object);
 
-       g_signal_connect(se_skeleton,
-               "handle-get-atr",
-               G_CALLBACK(se_handle_get_atr),
-               NULL);
+       g_free(data);
+}
 
-       g_signal_connect(se_skeleton,
-               "handle-send-apdu",
-               G_CALLBACK(se_handle_send_apdu),
-               NULL);
+static gboolean se_handle_get_handler_storage_info(
+       NetNfcGDbusSecureElement *object,
+       GDBusMethodInvocation *invocation,
+       guint category,
+       GVariant *smack_privilege)
+{
+       SeDataAid *data = NULL;
+       gint result;
 
-       g_signal_connect(se_skeleton,
-               "handle-set-transaction-fg-dispatch",
-               G_CALLBACK(se_handle_set_transaction_fg_dispatch),
-               NULL);
+       INFO_MSG(">>> REQUEST from [%s]",
+               g_dbus_method_invocation_get_sender(invocation));
 
-       g_signal_connect(se_skeleton,
-               "handle-check-transaction-permission",
+       /* check privilege and update client context */
+       if (net_nfc_server_gdbus_check_privilege(invocation, NET_NFC_PRIVILEGE_NFC_CARD_EMUL) == false) {
+               DEBUG_ERR_MSG("permission denied, and finished request");
+               result = NET_NFC_PERMISSION_DENIED;
+
+               goto ERROR;
+       }
+
+       data = g_try_new0(SeDataAid, 1);
+       if (data == NULL)
+       {
+               DEBUG_ERR_MSG("Memory allocation failed");
+               result = NET_NFC_ALLOC_FAIL;
+
+               goto ERROR;
+       }
+
+       data->object = g_object_ref(object);
+       data->invocation = g_object_ref(invocation);
+       data->category = category;
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               se_get_handler_storage_info_thread_func, data) == FALSE)
+       {
+               /* return error if queue was blocked */
+               DEBUG_SERVER_MSG("controller is processing important message..");
+               result = NET_NFC_BUSY;
+
+               goto ERROR;
+       }
+
+       return TRUE;
+
+ERROR :
+       if (data != NULL) {
+               g_object_unref(data->invocation);
+               g_object_unref(data->object);
+
+               g_free(data);
+       }
+
+       net_nfc_gdbus_secure_element_complete_get_handler_storage_info(
+               object, invocation, result, -1, -1);
+
+       return TRUE;
+}
+
+static bool __iterate_conflict_handlers(
+       const char *package, route_table_handler_t *handler, void *user_data)
+{
+       gpointer *params = (gpointer *)user_data;
+       net_nfc_card_emulation_category_t category;
+       size_t i;
+       aid_info_t *aid;
+
+       if (g_strcmp0(package, (const char *)params[0]) == 0) {
+               return true;
+       }
+
+       category = (net_nfc_card_emulation_category_t)params[1];
+
+       for (i = 0; i < handler->aids[category]->len; i++) {
+               aid = (aid_info_t *)(handler->aids[category]->pdata[i]);
+
+               if (g_strcmp0(aid->aid, (const char *)params[2]) == 0) {
+                       /* conflicted */
+
+                       SECURE_MSG("conflict with [%s]", package);
+
+                       *(net_nfc_error_e *)(params[3]) = NET_NFC_DATA_CONFLICTED;
+
+                       g_variant_builder_add((GVariantBuilder *)params[4], "(s)", package);
+                       break;
+               }
+       }
+
+       return true;
+}
+
+static void se_get_conflict_handlers_thread_func(gpointer user_data)
+{
+       SeDataAid *data = (SeDataAid *)user_data;
+       net_nfc_error_e result = NET_NFC_OK;
+       GVariantBuilder builder;
+
+       g_assert(data != NULL);
+       g_assert(data->object != NULL);
+       g_assert(data->invocation != NULL);
+
+       DEBUG_SERVER_MSG(">>> Call se_get_conflict_handlers_thread_func");
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(s)"));
+
+       gpointer params[5];
+
+       params[0] = (gpointer)data->package;
+       params[1] = (gpointer)data->category;
+       params[2] = (gpointer)data->aid;
+       params[3] = &result;
+       params[4] = &builder;
+
+       net_nfc_server_route_table_iterate_handler(__iterate_conflict_handlers,
+               params);
+
+       net_nfc_gdbus_secure_element_complete_get_conflict_handlers(
+               data->object, data->invocation, result,
+               g_variant_builder_end(&builder));
+
+       g_free(data->package);
+       g_free(data->aid);
+       g_object_unref(data->invocation);
+       g_object_unref(data->object);
+
+       g_free(data);
+}
+
+static gboolean se_handle_get_conflict_handlers(
+       NetNfcGDbusSecureElement *object,
+       GDBusMethodInvocation *invocation,
+       gchar *package,
+       guint category,
+       gchar *aid,
+       GVariant *smack_privilege)
+{
+       SeDataAid *data = NULL;
+       gint result;
+
+       INFO_MSG(">>> REQUEST from [%s]",
+               g_dbus_method_invocation_get_sender(invocation));
+#if 0
+       /* check privilege and update client context */
+       if (net_nfc_server_gdbus_check_privilege(invocation,
+               smack_privilege,
+               "nfc-manager",
+               "r") == false) {
+               DEBUG_ERR_MSG("permission denied, and finished request");
+               result = NET_NFC_PERMISSION_DENIED;
+
+               goto ERROR;
+       }
+#endif
+       data = g_try_new0(SeDataAid, 1);
+       if (data == NULL)
+       {
+               DEBUG_ERR_MSG("Memory allocation failed");
+               result = NET_NFC_ALLOC_FAIL;
+
+               goto ERROR;
+       }
+
+       data->object = g_object_ref(object);
+       data->invocation = g_object_ref(invocation);
+       data->package = g_strdup(package);
+       data->category = category;
+       data->aid = g_strdup(aid);
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               se_get_conflict_handlers_thread_func, data) == FALSE)
+       {
+               /* return error if queue was blocked */
+               DEBUG_SERVER_MSG("controller is processing important message..");
+               result = NET_NFC_BUSY;
+
+               goto ERROR;
+       }
+
+       return TRUE;
+
+ERROR :
+       if (data != NULL) {
+               g_free(data->package);
+               g_free(data->aid);
+               g_object_unref(data->invocation);
+               g_object_unref(data->object);
+
+               g_free(data);
+       }
+
+       GVariantBuilder builder;
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(s)"));
+
+       net_nfc_gdbus_secure_element_complete_get_conflict_handlers(
+               object, invocation, result, g_variant_builder_end(&builder));
+
+       return TRUE;
+}
+
+static net_nfc_error_e __se_close_ese_no_lock()
+{
+       net_nfc_error_e result = NET_NFC_OK;
+
+       if (gdbus_ese_handle != NULL &&
+               net_nfc_server_gdbus_is_server_busy_no_lock() == false) {
+               if (net_nfc_controller_secure_element_close(
+                       gdbus_ese_handle,
+                       &result) == false)
+               {
+                       net_nfc_controller_exception_handler();
+               }
+               net_nfc_server_se_set_current_ese_handle(NULL);
+       }
+
+       return result;
+}
+
+
+static void __llcp_on_client_detached_cb(net_nfc_client_context_info_t *client)
+{
+       if (client->ref_se > 0) {
+               DEBUG_SERVER_MSG("close opened secure element");
+
+               client->ref_se = 0;
+
+               __se_close_ese_no_lock();
+       }
+}
+
+static void se_set_preferred_handler_thread_func(gpointer user_data)
+{
+       SeSetPreferred *data = (SeSetPreferred *)user_data;
+       route_table_handler_t *handler;
+       net_nfc_error_e result = NET_NFC_OK;
+       bool ret;
+       pid_t pid;
+       const char *id;
+       char foreground[1024] = {0,};
+
+       g_assert(data != NULL);
+       g_assert(data->object != NULL);
+       g_assert(data->invocation != NULL);
+
+       DEBUG_SERVER_MSG(">>> Call se_set_preferred_handler_thread_func %d", data->state);
+
+       id = g_dbus_method_invocation_get_sender(data->invocation);
+
+       if (data->state == true) {
+               //check if handler is exist
+               handler = net_nfc_server_route_table_find_handler_by_id(id);
+               if (handler != NULL) {
+                       ret = net_nfc_server_route_table_is_allowed_preferred_handler(handler);
+
+                       //check if this handler is allowed about preferred operation.
+                       if (ret == true) {
+                               DEBUG_SERVER_MSG("[Preferred] Start Preferred process");
+
+                               net_nfc_server_route_table_set_preferred_handler(handler);
+                               net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+
+                               DEBUG_SERVER_MSG("[Preferred] End Preferred process");
+                       } else {
+                               DEBUG_SERVER_MSG("[Preferred] Error ! Not allowed operation");
+                               result = NET_NFC_NOT_ALLOWED_OPERATION;
+                       }
+               } else {
+                       DEBUG_SERVER_MSG("[Preferred] Error ! No handler found");
+                       result = NET_NFC_NO_DATA_FOUND;
+               }
+       } else {
+               net_nfc_server_route_table_set_preferred_handler(NULL);
+               net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+       }
+
+       net_nfc_server_route_table_preferred_handler_dump();
+
+       net_nfc_gdbus_secure_element_complete_set_preferred_handler(
+               data->object, data->invocation, result);
+
+       g_object_unref(data->invocation);
+       g_object_unref(data->object);
+
+       g_free(data);
+}
+
+static gboolean se_handle_set_preferred_handler(
+       NetNfcGDbusSecureElement *object,
+       GDBusMethodInvocation *invocation,
+       bool state,
+       GVariant *smack_privilege)
+{
+       SeSetPreferred *data = NULL;
+       gint result;
+
+       INFO_MSG(">>> REQUEST from [%s]",
+               g_dbus_method_invocation_get_sender(invocation));
+#if 0
+       /* check privilege and update client context */
+       if (net_nfc_server_gdbus_check_privilege(invocation,
+               smack_privilege,
+               "nfc-manager",
+               "r") == false) {
+               DEBUG_ERR_MSG("permission denied, and finished request");
+               result = NET_NFC_PERMISSION_DENIED;
+
+               goto ERROR;
+       }
+#endif
+       data = g_try_new0(SeDataAid, 1);
+       if (data == NULL)
+       {
+               DEBUG_ERR_MSG("Memory allocation failed");
+               result = NET_NFC_ALLOC_FAIL;
+
+               goto ERROR;
+       }
+
+       data->object = g_object_ref(object);
+       data->invocation = g_object_ref(invocation);
+       data->state = state;
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               se_set_preferred_handler_thread_func, data) == FALSE)
+       {
+               /* return error if queue was blocked */
+               DEBUG_SERVER_MSG("controller is processing important message..");
+               result = NET_NFC_BUSY;
+
+               goto ERROR;
+       }
+
+       return TRUE;
+
+ERROR :
+       if (data != NULL) {
+               g_object_unref(data->invocation);
+               g_object_unref(data->object);
+
+               g_free(data);
+       }
+
+       net_nfc_gdbus_secure_element_complete_set_preferred_handler(
+               object, invocation, result);
+
+       return TRUE;
+}
+
+gboolean net_nfc_server_se_init(GDBusConnection *connection)
+{
+       GError *error = NULL;
+       gboolean result;
+
+       if (se_skeleton)
+               g_object_unref(se_skeleton);
+
+       net_nfc_server_route_table_init();
+
+#ifdef ENABLE_TELEPHONY
+       /* initialize UICC */
+       //_se_uicc_init();
+#endif
+       se_skeleton = net_nfc_gdbus_secure_element_skeleton_new();
+
+       g_signal_connect(se_skeleton,
+               "handle-set",
+               G_CALLBACK(se_handle_set),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-get",
+               G_CALLBACK(se_handle_get),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-set-card-emulation",
+               G_CALLBACK(se_handle_change_card_emulation_mode),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-get-card-emulation",
+               G_CALLBACK(se_handle_get_card_emulation_mode),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-open-secure-element",
+               G_CALLBACK(se_handle_open_secure_element),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-close-secure-element",
+               G_CALLBACK(se_handle_close_secure_element),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-get-atr",
+               G_CALLBACK(se_handle_get_atr),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-send-apdu",
+               G_CALLBACK(se_handle_send_apdu),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-set-transaction-fg-dispatch",
+               G_CALLBACK(se_handle_set_transaction_fg_dispatch),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-check-transaction-permission",
                G_CALLBACK(se_handle_check_transaction_permission),
                NULL);
 
@@ -3351,13 +3943,30 @@ gboolean net_nfc_server_se_init(GDBusConnection *connection)
                G_CALLBACK(se_handle_get_registered_handlers),
                NULL);
 
+       g_signal_connect(se_skeleton,
+               "handle-get-handler-storage-info",
+               G_CALLBACK(se_handle_get_handler_storage_info),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-get-conflict-handlers",
+               G_CALLBACK(se_handle_get_conflict_handlers),
+               NULL);
+
+       g_signal_connect(se_skeleton,
+               "handle-set-preferred-handler",
+               G_CALLBACK(se_handle_set_preferred_handler),
+               NULL);
+
        result = g_dbus_interface_skeleton_export(
                G_DBUS_INTERFACE_SKELETON(se_skeleton),
                connection,
                "/org/tizen/NetNfcService/SecureElement",
                &error);
-       if (result == FALSE)
-       {
+       if (result == true) {
+               net_nfc_server_gdbus_register_on_client_detached_cb(
+                       __llcp_on_client_detached_cb);
+       } else {
                DEBUG_ERR_MSG("can not skeleton_export %s", error->message);
 
                g_error_free(error);
@@ -3370,6 +3979,11 @@ gboolean net_nfc_server_se_init(GDBusConnection *connection)
 
 void net_nfc_server_se_deinit(void)
 {
+       net_nfc_server_gdbus_unregister_on_client_detached_cb(
+               __llcp_on_client_detached_cb);
+
+       __stop_lcd_on_timer();
+
        if (se_skeleton)
        {
                g_object_unref(se_skeleton);
@@ -3377,7 +3991,7 @@ void net_nfc_server_se_deinit(void)
 
 #ifdef ENABLE_TELEPHONY
                /* de-initialize UICC */
-               _se_uicc_deinit();
+               //_se_uicc_deinit();
 #endif
        }
 }
@@ -3408,7 +4022,7 @@ static void se_detected_thread_func(gpointer user_data)
 
        net_nfc_server_se_set_current_ese_handle(handle);
 
-       DEBUG_SERVER_MSG("trying to connect to ESE = [0x%p]", handle);
+       SECURE_MSG("trying to connect to ESE = [0x%p]", handle);
 
        if (!net_nfc_controller_connect(handle, &result))
        {
@@ -3443,7 +4057,7 @@ static void se_transcation_thread_func(gpointer user_data)
                focus_app_pid = net_nfc_app_util_get_focus_app_pid();
                fg_dispatch = net_nfc_app_util_check_transaction_fg_dispatch();
 
-               DEBUG_SERVER_MSG("TRANSACTION event fg dispatch [%d]", fg_dispatch);
+               SECURE_MSG("TRANSACTION event fg dispatch [%d]", fg_dispatch);
 
                /* TODO : check access control */
                net_nfc_gdbus_secure_element_emit_transaction_event(
@@ -3461,6 +4075,13 @@ static void se_transcation_thread_func(gpointer user_data)
                                detail->aid.length,
                                detail->param.buffer,
                                detail->param.length);
+
+                       net_nfc_app_util_launch_se_off_host_apdu_service_app(
+                               se_type,
+                               detail->aid.buffer,
+                               detail->aid.length,
+                               detail->param.buffer,
+                               detail->param.length);
                }
 
                DEBUG_SERVER_MSG("launch se app end");
@@ -3474,8 +4095,6 @@ static void se_transcation_thread_func(gpointer user_data)
 
 static void se_rf_field_thread_func(gpointer user_data)
 {
-       DEBUG_SERVER_MSG("check se_skeleton = [0x%p]", se_skeleton);
-
        if (se_skeleton == NULL)
        {
                DEBUG_ERR_MSG("se skeleton is not initialized");
@@ -3578,3 +4197,427 @@ void net_nfc_server_se_rf_field_on(void *info)
        /* FIXME : should be removed when plugins would be fixed*/
        _net_nfc_util_free_mem(info);
 }
+
+static guint __timer_handle;
+static gint __set_guard_time;
+static net_nfc_screen_state_type_e __lcd_state;
+static pthread_mutex_t __state_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void ____delayed_change_screen_state_thread_func(gpointer user_data)
+{
+       net_nfc_error_e result = NET_NFC_OK;
+
+       DEBUG_SERVER_MSG(">>>");
+
+       if (net_nfc_controller_set_screen_state(
+               (net_nfc_screen_state_type_e)user_data, &result) == true)
+       {
+               DEBUG_SERVER_MSG("Set Screen State [%d]", (int)user_data);
+       }
+}
+
+static gboolean __delayed_change_screen_state(gpointer user_data)
+{
+       pthread_mutex_lock(&__state_mutex);
+
+       DEBUG_SERVER_MSG("timer callback");
+
+       if (__timer_handle > 0) {
+               if (__lcd_state != NET_NFC_SCREEN_INVALID) {
+                       DEBUG_SERVER_MSG("update screen state");
+
+                       if (net_nfc_server_controller_async_queue_push_force(
+                               ____delayed_change_screen_state_thread_func,
+                               (void *)__lcd_state) == FALSE) {
+                               DEBUG_ERR_MSG("can not push to controller thread");
+                       }
+               } else {
+                       DEBUG_SERVER_MSG("no update");
+               }
+
+               __lcd_state = NET_NFC_SCREEN_INVALID;
+               __timer_handle = 0;
+       }
+
+       pthread_mutex_unlock(&__state_mutex);
+
+       return FALSE;
+}
+
+static void __stop_lcd_on_timer()
+{
+       pthread_mutex_lock(&__state_mutex);
+
+       DEBUG_SERVER_MSG("timer stopped");
+
+       if (__timer_handle > 0) {
+               g_source_remove(__timer_handle);
+               __timer_handle = 0;
+       }
+
+       pthread_mutex_unlock(&__state_mutex);
+}
+
+static void __start_lcd_on_timer()
+{
+       __stop_lcd_on_timer();
+
+       pthread_mutex_lock(&__state_mutex);
+
+       DEBUG_SERVER_MSG("start timer");
+
+       __timer_handle = g_timeout_add(1000, __delayed_change_screen_state, NULL);
+
+       pthread_mutex_unlock(&__state_mutex);
+}
+
+bool net_nfc_server_se_notify_lcd_state_changed(net_nfc_screen_state_type_e state)
+{
+       bool result;
+
+       pthread_mutex_lock(&__state_mutex);
+
+       if (__timer_handle > 0 || __set_guard_time > 0) {
+               /* when screen unlocked */
+               if (state == NET_NFC_SCREEN_ON_UNLOCK) {
+                       INFO_MSG("set delayed unlock state");
+
+                       __lcd_state = state;
+
+                       result = true;
+               } else {
+                       __lcd_state = NET_NFC_SCREEN_INVALID;
+
+                       result = false;
+               }
+       } else {
+               __lcd_state = NET_NFC_SCREEN_INVALID;
+
+               result = false;
+       }
+
+       pthread_mutex_unlock(&__state_mutex);
+
+       return result;
+}
+
+#if 0
+static int __display_state;
+int __get_display_state()
+{
+       int result;
+       int pm_state;
+
+       result = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
+       if (result == 0) {
+               switch (pm_state) {
+               case VCONFKEY_PM_STATE_NORMAL :
+                       result = LCD_NORMAL;
+                       break;
+
+               case VCONFKEY_PM_STATE_LCDDIM :
+                       result = LCD_DIM;
+                       break;
+
+               case VCONFKEY_PM_STATE_LCDOFF :
+               case VCONFKEY_PM_STATE_SLEEP :
+                       result = LCD_OFF;
+                       break;
+               }
+       } else {
+               DEBUG_ERR_MSG("VCONFKEY_PM_STATE get failed, [%d]", result);
+
+               result = 0;
+       }
+
+       return result;
+}
+#endif
+
+static void se_connected_thread_func(gpointer user_data)
+{
+       __set_guard_time++;
+#if 0
+       if (__set_guard_time == 1) {
+               int ret;
+               __display_state = __get_display_state();
+
+               /* lock state */
+               ret = display_lock_state(__display_state, STAY_CUR_STATE, 0);
+               if (ret < 0) {
+                       DEBUG_ERR_MSG("display_lock_state failed, [%d]", ret);
+               }
+       }
+#endif
+}
+
+void net_nfc_server_se_connected(void *info)
+{
+       DEBUG_SERVER_MSG("net_nfc_server_se_connected");
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               se_connected_thread_func,
+               NULL) == FALSE) {
+               DEBUG_ERR_MSG("can not push to controller thread");
+       }
+
+       /* FIXME : should be removed when plugins would be fixed*/
+       _net_nfc_util_free_mem(info);
+}
+
+static void se_rf_field_off_thread_func(gpointer user_data)
+{
+       if (__set_guard_time > 0) {
+               __set_guard_time = 0;
+#if 1
+               __start_lcd_on_timer();
+#else
+               int ret;
+
+               /* lock timeout */
+               ret = display_lock_state(__display_state, STAY_CUR_STATE, 1000);
+               if (ret < 0) {
+                       DEBUG_ERR_MSG("display_lock_state failed, [%d]", ret);
+               }
+#endif
+       }
+}
+
+void net_nfc_server_se_rf_field_off(void *info)
+{
+       DEBUG_SERVER_MSG("net_nfc_server_se_rf_field_off");
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               se_rf_field_off_thread_func,
+               NULL) == FALSE) {
+               DEBUG_ERR_MSG("can not push to controller thread");
+       }
+
+       /* FIXME : should be removed when plugins would be fixed*/
+       _net_nfc_util_free_mem(info);
+}
+
+void net_nfc_server_se_convert_to_binary(uint8_t *orig, size_t len,
+       uint8_t **dest, size_t *destLen)
+{
+       size_t i = 0;
+
+       if (len > 1) {
+               *destLen = len / 2;
+               *dest = g_new0(uint8_t, *destLen);
+
+               for (i = 0; i < *destLen; i++) {
+                       (*dest)[i] = char_to_num[orig[i << 1]] << 4;
+                       (*dest)[i] |= char_to_num[orig[(i << 1) + 1]] & 0x0F;
+               }
+       } else {
+               *destLen = 1;
+               *dest = g_new0(uint8_t, 1);
+               (*dest)[0] = *orig;
+       }
+}
+
+void net_nfc_server_se_create_deactivate_apdu_command(uint8_t *orig, uint8_t **dest, size_t *destLen)
+{
+}
+
+#define CSA_USEESE_PATH "/csa/useese"
+#define CSA_USEESE_FILE_PATH "/csa/useese/aid.dat"
+#define FIRST_BOOT_USEESE_FILE_PATH  "/opt/usr/share/nfc_debug/is_first_boot.dat"
+
+static void net_nfc_server_se_deactivated_card_thread_func(gpointer user_data)
+{
+       net_nfc_target_handle_s *handle = NULL;
+       uint8_t cmd_select_scrs[] = {0x00, 0xA4, 0x04, 0x00, 0x09, 0xA0, 0x00, 0x00, 0x01, 0x51, 0x43, 0x52, 0x53, 0x00};
+       uint8_t *cmd = NULL;
+       data_s *command = NULL;
+       data_s *response = NULL;
+       net_nfc_error_e result = NET_NFC_OK;
+       int i, j, k = 0;
+       FILE *fp = NULL;
+       int count = 0;
+       char buf[1024 + 1];
+       size_t size = 0;
+       char *token = NULL;
+       static GList *list;
+       int token_counter = 0;
+
+       DEBUG_SERVER_MSG("net_nfc_server_se_deactivate_card");
+
+#if 0
+       fp = fopen(FIRST_BOOT_USEESE_FILE_PATH, "r");
+       if (!fp) {
+               fp = fopen(FIRST_BOOT_USEESE_FILE_PATH, "w+");
+               if (!fp) {
+                       DEBUG_ERR_MSG("error");
+                                       printf( "is_first_boot.daopen error = %dn", errno);
+               }
+               fclose(fp);
+       }
+       else {
+               fclose(fp);
+       }
+
+#endif
+       result = access( FIRST_BOOT_USEESE_FILE_PATH, 0 );
+
+
+       if( result == 0 )
+       {
+               DEBUG_SERVER_MSG("first boot NO");
+               return;
+       }
+       else if( result == -1 )
+       {
+               DEBUG_SERVER_MSG("first boot YES");
+               fp = fopen(FIRST_BOOT_USEESE_FILE_PATH, "w+");
+               if (fp)
+               {
+                       fclose(fp);
+               }
+               else
+               {
+                       DEBUG_ERR_MSG("error");
+                       DEBUG_ERR_MSG( "is_first_boot.daopen error = %dn", errno);
+               }
+       }
+
+
+       fp = fopen(CSA_USEESE_FILE_PATH, "r");
+       if (!fp) {
+               DEBUG_SERVER_MSG("fp is null");
+               DEBUG_ERR_MSG( "aid.dat open error = %dn", errno);
+               return;
+       }
+       fseek(fp, 0, SEEK_SET);
+       count = fread(buf, 1, 1024, fp);
+       DEBUG_SERVER_MSG("count : %d", count);
+       DEBUG_SERVER_MSG("fp : %s", buf);
+
+       if (count > 0 && count <= 1024) {
+               while (count > 0 && buf[count - 1] == '\n')
+                       count--;
+               buf[count] = '\0';
+       } else {
+               buf[0] = '\0';
+       }
+       fclose(fp);
+
+       if (count > 0) {
+               token = strtok(buf, ";");
+               while(token) {
+                       SECURE_MSG("Token[%d] : %s", token_counter, token);
+                       list = g_list_append(list, strdup(token));
+                       token_counter++;
+                       token = strtok(NULL, ";");
+               }
+       }
+
+       handle = net_nfc_server_se_open_ese();
+       DEBUG_SERVER_MSG("start sleep");
+       sleep(1);
+       DEBUG_SERVER_MSG("end sleep");
+       if (net_nfc_server_se_is_ese_handle(handle) == true)
+       {
+               command = net_nfc_util_create_data(14);
+               memcpy(command->buffer, cmd_select_scrs, command->length);
+               (void)net_nfc_controller_secure_element_send_apdu(
+                               handle, command, &response, &result);
+               SECURE_MSG("net_nfc_controller_secure_element_send_apdu[scrs] : %d", result);
+
+               if (response != NULL) {
+                       for (k=0; k<response->length;k++) {
+                               SECURE_MSG("%02X", (int)response->buffer[k]);
+                       }
+               }
+
+               if (result == NET_NFC_OK /*&& response->buffer*/) {
+                       net_nfc_util_clear_data(command);
+                       net_nfc_util_clear_data(response);
+
+                       for (i = 0; i < g_list_length(list); i++) {
+                               GList* node;
+                               node = g_list_nth(list, i);
+                               SECURE_MSG("[%d]th list value is %s", i, (char*)node->data);
+
+                               net_nfc_server_se_convert_to_binary((uint8_t *)node->data, strlen((char *)node->data), &cmd, &size);
+
+                               command = net_nfc_util_create_data(size+7);
+                               command->buffer[0] = (unsigned char)0x80;
+                               command->buffer[1] = (unsigned char)0xF0;
+                               command->buffer[2] = (unsigned char)0x01;
+                               command->buffer[3] = (unsigned char)0x00;
+                               command->buffer[4] = (unsigned char)((size+2) & 0xFF);
+                               command->buffer[5] = (unsigned char)0x4F;
+                               command->buffer[6] = (unsigned char)(size & 0xFF);
+                               memcpy(command->buffer + 7, cmd, size);
+
+                               SECURE_MSG("COMMAND : ");
+                               for (j=0; j<command->length;j++) {
+                                       SECURE_MSG("%02X", (int)command->buffer[j]);
+                               }
+
+                               net_nfc_controller_secure_element_send_apdu(
+                                               handle, command, &response, &result);
+                               SECURE_MSG("net_nfc_controller_secure_element_send_apdu[%d] : %d", i, result);
+
+                               if (response != NULL) {
+                                       for (k=0; k<response->length;k++) {
+                                               SECURE_MSG("%02X", (int)response->buffer[k]);
+                                       }
+                                       if (response->buffer[response->length - 2] == (unsigned char)0x63 && response->buffer[response->length - 1] == (unsigned char)0x20) {
+                                               //remove default card
+                                               net_nfc_util_clear_data(command);
+                                               net_nfc_util_clear_data(response);
+                                               command = net_nfc_util_create_data(size+5);
+                                               command->buffer[0] = (unsigned char)0x80;
+                                               command->buffer[1] = (unsigned char)0xF0;
+                                               command->buffer[2] = (unsigned char)0x28;
+                                               command->buffer[3] = (unsigned char)0x00;
+                                               command->buffer[4] = (unsigned char)(size & 0xFF);
+                                               memcpy(command->buffer + 5, cmd, size);
+
+                                               DEBUG_SERVER_MSG("COMMAND : ");
+                                               for (k =0; k <command->length;k++) {
+                                                       DEBUG_SERVER_MSG("%02X", (int)command->buffer[k]);
+                                               }
+
+                                               net_nfc_controller_secure_element_send_apdu(
+                                                               handle, command, &response, &result);
+                                               DEBUG_SERVER_MSG("net_nfc_controller_secure_element_send_apdu[%d] : %d", i, result);
+                                               for (k=0; k<response->length;k++) {
+                                                       SECURE_MSG("%02X", (int)response->buffer[k]);
+                                               }
+                                       } else {
+                                               DEBUG_SERVER_MSG("error none");
+                                       }
+                               }
+
+                               net_nfc_util_clear_data(command);
+                               net_nfc_util_clear_data(response);
+
+                       }
+                       g_list_free_full(list, (GDestroyNotify)free);
+
+                       result = remove(CSA_USEESE_FILE_PATH);
+                       if (result != 0) {
+                               DEBUG_ERR_MSG("remove failed, [%d]", result);
+                       }
+               }
+               result = net_nfc_server_se_close_ese();
+               SECURE_MSG("net_nfc_server_se_close_ese() : %d", result);
+       }
+       else {
+               DEBUG_ERR_MSG("failed to net_nfc_server_se_open_ese()");
+       }
+}
+
+void net_nfc_server_se_deactivate_card(void)
+{
+       if (net_nfc_server_controller_async_queue_push_force(
+                       net_nfc_server_se_deactivated_card_thread_func,
+                                       NULL) == FALSE)
+       {
+               DEBUG_ERR_MSG("Failed to push onto the queue");
+       }
+}
index eeaf8d4..89734be 100755 (executable)
@@ -294,7 +294,7 @@ static data_s *_get_barcode_from_target_info(net_nfc_current_target_info_s *targ
 
                memcpy(str, pos, length);
 
-               DEBUG_CLIENT_MSG("key = [%s]", str);
+               SECURE_MSG("key = [%s]", str);
 
                pos += length;
 
@@ -466,7 +466,23 @@ static void tag_get_current_tag_info_thread_func(gpointer user_data)
 
 static bool _is_supported_tags(net_nfc_current_target_info_s *target)
 {
-       return true;
+       bool result;
+
+       switch (target->devType) {
+       case NET_NFC_ISO14443_3A_PICC :
+       case NET_NFC_MIFARE_MINI_PICC :
+       case NET_NFC_MIFARE_1K_PICC :
+       case NET_NFC_MIFARE_4K_PICC :
+       case NET_NFC_ISO14443_BPRIME_PICC :
+               result = false;
+               break;
+
+       default :
+               result = true;
+               break;
+       }
+
+       return result;
 }
 
 static void _start_check_presence(net_nfc_current_target_info_s *target)
@@ -474,7 +490,7 @@ static void _start_check_presence(net_nfc_current_target_info_s *target)
        CheckPresenceData *presence_data = NULL;
 
        /* start polling tags presence */
-       INFO_MSG("start polling tags presence");
+       DEBUG_SERVER_MSG("start polling tags presence");
 
        presence_data = g_new0(CheckPresenceData, 1);
        presence_data->dev_type = target->devType;
index acfd319..7ea731c 100755 (executable)
@@ -54,6 +54,17 @@ struct _TestSetEeData
        data_s data;
 };
 
+typedef struct _TestSetListenTechData TestSetListenTechData;
+
+struct _TestSetListenTechData
+{
+       NetNfcGDbusTest *test;
+       GDBusMethodInvocation *invocation;
+
+       guint32 mode;
+};
+
+
 static void test_handle_sim_test_thread_func(gpointer user_data);
 
 static void test_handle_prbs_test_thread_func(gpointer user_data);
@@ -351,7 +362,7 @@ static gboolean test_handle_get_firmware_version(NetNfcGDbusTest *test,
 
        INFO_MSG(">>> REQUEST from [%s]",
                g_dbus_method_invocation_get_sender(invocation));
-
+#if 0
        /* check privilege and update client context */
        if (net_nfc_server_gdbus_check_privilege(invocation, NET_NFC_PRIVILEGE_NFC) == false) {
                DEBUG_ERR_MSG("permission denied, and finished request");
@@ -359,7 +370,7 @@ static gboolean test_handle_get_firmware_version(NetNfcGDbusTest *test,
 
                goto ERROR;
        }
-
+#endif
        data = g_try_new0(TestData, 1);
        if (data == NULL)
        {
@@ -602,6 +613,89 @@ ERROR :
        return TRUE;
 }
 
+static void test_set_listen_tech_mask_thread_func(gpointer user_data)
+{
+       TestSetListenTechData *data = (TestSetListenTechData *)user_data;
+       net_nfc_error_e result = NET_NFC_OK;
+
+       g_assert(data != NULL);
+       g_assert(data->test != NULL);
+       g_assert(data->invocation != NULL);
+
+       DEBUG_SERVER_MSG(">>> Call test_set_listen_tech_mask_thread_func");
+
+       net_nfc_controller_secure_element_set_listen_tech_mask(data->mode, &result);
+
+       net_nfc_gdbus_test_complete_set_listen_tech_mask(
+               data->test, data->invocation, result);
+
+       g_object_unref(data->invocation);
+       g_object_unref(data->test);
+
+       g_free(data);
+}
+
+static gboolean test_handle_set_listen_tech_mask(
+       NetNfcGDbusTest *test,
+       GDBusMethodInvocation *invocation,
+       guint listen_tech_mask,
+       GVariant *smack_privilege)
+{
+       TestSetListenTechData *data = NULL;
+       gint result;
+
+       INFO_MSG(">>> REQUEST from [%s]",
+               g_dbus_method_invocation_get_sender(invocation));
+
+       /* check privilege and update client context */
+       if (net_nfc_server_gdbus_check_privilege(invocation, NET_NFC_PRIVILEGE_NFC) == false) {
+               DEBUG_ERR_MSG("permission denied, and finished request");
+               result = NET_NFC_PERMISSION_DENIED;
+
+               goto ERROR;
+       }
+
+       data = g_try_new0(TestSetListenTechData, 1);
+       if (data == NULL)
+       {
+               DEBUG_ERR_MSG("Memory allocation failed");
+               result = NET_NFC_ALLOC_FAIL;
+
+               goto ERROR;
+       }
+
+       data->test = g_object_ref(test);
+       data->invocation = g_object_ref(invocation);
+       data->mode = listen_tech_mask;
+
+       if (net_nfc_server_controller_async_queue_push_force(
+               test_set_listen_tech_mask_thread_func, data) == FALSE)
+       {
+               /* return error if queue was blocked */
+               DEBUG_SERVER_MSG("controller is processing important message..");
+               result = NET_NFC_BUSY;
+
+               goto ERROR;
+       }
+
+       return TRUE;
+
+ERROR :
+
+       net_nfc_gdbus_test_complete_set_listen_tech_mask(
+               test, invocation, result);
+
+       if (data != NULL) {
+               g_object_unref(data->invocation);
+               g_object_unref(data->test);
+
+               g_free(data);
+       }
+
+       return TRUE;
+}
+
+
 gboolean net_nfc_server_test_init(GDBusConnection *connection)
 {
        GError *error = NULL;
@@ -642,6 +736,11 @@ gboolean net_nfc_server_test_init(GDBusConnection *connection)
                        G_CALLBACK(test_handle_set_se_tech_type),
                        NULL);
 
+       g_signal_connect(test_skeleton,
+                       "handle-set-listen-tech-mask",
+                       G_CALLBACK(test_handle_set_listen_tech_mask),
+                       NULL);
+
        result = g_dbus_interface_skeleton_export(
                G_DBUS_INTERFACE_SKELETON(test_skeleton),
                connection,
index c52784e..2b3fcac 100755 (executable)
@@ -25,6 +25,8 @@
 #include "net_nfc_server_route_table.h"
 #include "net_nfc_server_vconf.h"
 
+static bool is_screen_state_on = false;
+
 static void net_nfc_server_vconf_lock_state_changed(keynode_t *key,
                                                void *user_data);
 
@@ -42,7 +44,6 @@ static void net_nfc_server_vconf_lock_state_changed(keynode_t *key,
 
        gint state = 0;
        gint result;
-       gint lock_state = 0;
 
        result = vconf_get_bool(VCONFKEY_NFC_STATE, &state);
        if (result != 0)
@@ -50,17 +51,11 @@ static void net_nfc_server_vconf_lock_state_changed(keynode_t *key,
 
        if (state == false)
        {
-               DEBUG_MSG("NFC off");
+               DEBUG_SERVER_MSG("NFC off");
                return;
        }
 
-       if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &lock_state) != 0)
-               DEBUG_ERR_MSG("%s does not exist", "VCONFKEY_IDLE_LOCK_STATE");
-
-
-       if (lock_state == VCONFKEY_IDLE_UNLOCK ||
-                           lock_state == VCONFKEY_IDLE_LOCK)
-       {
+       if (is_screen_state_on == true) {
                net_nfc_server_restart_polling_loop();
        }
 
@@ -71,7 +66,6 @@ static void net_nfc_server_vconf_pm_state_changed(keynode_t *key,
 {
        gint state = 0;
        gint pm_state = 0;
-       gint lock_screen_set = 0;
        gint result;
 
        result = vconf_get_bool(VCONFKEY_NFC_STATE, &state);
@@ -81,7 +75,7 @@ static void net_nfc_server_vconf_pm_state_changed(keynode_t *key,
 
        if (state == false)
        {
-               DEBUG_MSG("NFC off");
+               DEBUG_SERVER_MSG("NFC off");
                return;
        }
 
@@ -92,22 +86,12 @@ static void net_nfc_server_vconf_pm_state_changed(keynode_t *key,
 
        DEBUG_SERVER_MSG("pm_state : %d", pm_state);
 
-       result = vconf_get_int(VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &lock_screen_set);
-
-       if (result != 0)
-               DEBUG_ERR_MSG("can not get %s", "VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT");
-
-       DEBUG_SERVER_MSG("lock_screen_set : %d", lock_screen_set);
-
-#if 0
-       if ( lock_screen_set == SETTING_SCREEN_LOCK_TYPE_NONE &&
-               (pm_state == VCONFKEY_PM_STATE_NORMAL ||
-                           pm_state == VCONFKEY_PM_STATE_LCDOFF) )
-#endif
        if (pm_state == VCONFKEY_PM_STATE_NORMAL ||
                pm_state == VCONFKEY_PM_STATE_LCDOFF)
        {
-               net_nfc_server_restart_polling_loop();
+               if (is_screen_state_on == true) {
+                       net_nfc_server_restart_polling_loop();
+               }
        }
 }
 
@@ -115,6 +99,76 @@ static void net_nfc_server_vconf_se_type_changed(keynode_t *key,
                                                void *user_data)
 {
        net_nfc_server_se_policy_apply();
+
+       net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+}
+
+static void net_nfc_server_vconf_wallet_mode_changed(keynode_t *key,
+                                               void *user_data)
+{
+       int wallet_mode;
+
+       g_assert(key != NULL);
+
+       wallet_mode = key->value.i;
+
+       DEBUG_SERVER_MSG("wallet mode [%d]", wallet_mode);
+
+       net_nfc_server_se_change_wallet_mode(wallet_mode);
+
+       net_nfc_server_route_table_do_update(net_nfc_server_manager_get_active());
+}
+
+static void __on_payment_handler_changed_func(gpointer user_data)
+{
+       char *package = (char *)user_data;
+
+       SECURE_MSG("PAYMENT handler changed, [%s]", package);
+
+       net_nfc_server_route_table_update_category_handler(package,
+               NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT);
+
+       if (package != NULL) {
+               g_free(package);
+       }
+}
+
+static void net_nfc_server_vconf_payment_handlers_changed(keynode_t *key,
+       void *user_data)
+{
+       g_assert(key != NULL);
+
+       if (net_nfc_server_controller_async_queue_push(
+               __on_payment_handler_changed_func,
+               g_strdup(key->value.s)) == false) {
+               DEBUG_ERR_MSG("net_nfc_server_controller_async_queue_push failed");
+       }
+}
+
+static void __on_other_handlers_changed_func(gpointer user_data)
+{
+       char *packages = (char *)user_data;
+
+       SECURE_MSG("OTHER handlers changed, [%s]", packages);
+
+       net_nfc_server_route_table_update_category_handler(packages,
+               NET_NFC_CARD_EMULATION_CATEGORY_OTHER);
+
+       if (packages != NULL) {
+               g_free(packages);
+       }
+}
+
+static void net_nfc_server_vconf_other_handlers_changed(keynode_t *key,
+       void *user_data)
+{
+       g_assert(key != NULL);
+
+       if (net_nfc_server_controller_async_queue_push(
+               __on_other_handlers_changed_func,
+               g_strdup(key->value.s)) == false) {
+               DEBUG_ERR_MSG("net_nfc_server_controller_async_queue_push failed");
+       }
 }
 
 void net_nfc_server_vconf_init(void)
@@ -131,6 +185,18 @@ void net_nfc_server_vconf_init(void)
        vconf_notify_key_changed(VCONFKEY_NFC_SE_TYPE,
                        net_nfc_server_vconf_se_type_changed,
                        NULL);
+
+       vconf_notify_key_changed(VCONFKEY_NFC_WALLET_MODE,
+                       net_nfc_server_vconf_wallet_mode_changed,
+                       NULL);
+
+       vconf_notify_key_changed(VCONFKEY_NFC_PAYMENT_HANDLERS,
+                       net_nfc_server_vconf_payment_handlers_changed,
+                       NULL);
+
+       vconf_notify_key_changed(VCONFKEY_NFC_OTHER_HANDLERS,
+                       net_nfc_server_vconf_other_handlers_changed,
+                       NULL);
 }
 
 void net_nfc_server_vconf_deinit(void)
@@ -140,6 +206,15 @@ void net_nfc_server_vconf_deinit(void)
 
        vconf_ignore_key_changed(VCONFKEY_NFC_SE_TYPE,
                        net_nfc_server_vconf_se_type_changed);
+
+       vconf_ignore_key_changed(VCONFKEY_NFC_WALLET_MODE,
+                       net_nfc_server_vconf_wallet_mode_changed);
+
+       vconf_ignore_key_changed(VCONFKEY_NFC_PAYMENT_HANDLERS,
+                       net_nfc_server_vconf_payment_handlers_changed);
+
+       vconf_ignore_key_changed(VCONFKEY_NFC_OTHER_HANDLERS,
+                       net_nfc_server_vconf_other_handlers_changed);
 }
 
 bool net_nfc_check_csc_vconf(void)
@@ -178,6 +253,22 @@ bool net_nfc_check_start_polling_vconf(void)
        DEBUG_SERVER_MSG("lock_screen_set:%d ,pm_state:%d,lock_state:%d",
                lock_screen_set , pm_state , lock_state);
 
+       if (lock_screen_set == SETTING_SCREEN_LOCK_TYPE_NONE)
+       {
+               if (pm_state == VCONFKEY_PM_STATE_NORMAL)
+               {
+                       DEBUG_SERVER_MSG("polling enable");
+                       return TRUE;
+               }
+
+               if (pm_state == VCONFKEY_PM_STATE_LCDOFF)
+               {
+                       DEBUG_SERVER_MSG("polling disabled");
+                       return FALSE;
+               }
+       }
+       else
+       {
                if (lock_state == VCONFKEY_IDLE_UNLOCK)
                {
                        DEBUG_SERVER_MSG("polling enable");
@@ -189,6 +280,12 @@ bool net_nfc_check_start_polling_vconf(void)
                        DEBUG_SERVER_MSG("polling disabled");
                        return FALSE;
                }
+       }
 
        return FALSE;
 }
+
+void net_nfc_server_vconf_set_screen_on_flag(bool flag)
+{
+       is_screen_state_on = flag;
+}
index ae98f33..b793ad8 100755 (executable)
@@ -1,4 +1,5 @@
 [D-BUS Service]
 Name=org.tizen.NetNfcService
-Exec=/usr/bin/nfc-manager-daemon
-User=root
+Exec=/bin/false
+SystemdService=nfc-manager.service
+User=system
diff --git a/src/manager/private/net_nfc_service_access_control_private.c b/src/manager/private/net_nfc_service_access_control_private.c
new file mode 100644 (file)
index 0000000..002184a
--- /dev/null
@@ -0,0 +1,426 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* standard library header */
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <glib.h>
+#include <gio/gio.h>
+
+/* SLP library header */
+#include "cert-service.h"
+#include "pkgmgr-info.h"
+#include "bincfg.h"
+
+/* local header */
+#include "net_nfc_typedef_internal.h"
+#include "net_nfc_debug_internal.h"
+#include "net_nfc_util_internal.h"
+#include "net_nfc_util_gdbus_internal.h"
+#include "net_nfc_server_common.h"
+#include "net_nfc_service_access_control.h"
+
+static GDBusProxy *dbus_proxy;
+
+static bool __check_platform_certificate(pid_t pid);
+static bool __check_se_dependant_access_rule(
+       uint8_t se_type, const char *package);
+static bool __check_ese_access_rule(pid_t pid);
+
+
+bool net_nfc_service_check_access_control(pid_t pid,
+       net_nfc_access_control_type_e type)
+{
+       bool result = false;
+       int ret;
+       char package[1024];
+
+       if (bincfg_get_binary_type() == BIN_TYPE_FACTORY) {
+               result = true;
+               goto END;
+       }
+
+       if (type & NET_NFC_ACCESS_CONTROL_PLATFORM) {
+               result = __check_platform_certificate(pid);
+               if (result == true)
+                       goto END;
+       }
+
+       /* get package id from pid */
+       ret = net_nfc_util_get_pkgid_by_pid(pid, package, sizeof(package));
+       if (ret < 0) {
+               DEBUG_ERR_MSG("aul_app_get_pkgname_bypid failed [%d]", ret);
+               goto END;
+       }
+
+       if (type & NET_NFC_ACCESS_CONTROL_UICC) {
+               result = __check_se_dependant_access_rule(
+                       NET_NFC_SE_TYPE_UICC, package);
+               if (result == true)
+                       goto END;
+       }
+
+       if (type & NET_NFC_ACCESS_CONTROL_ESE) {
+               result = __check_ese_access_rule(pid);
+               if (result == true)
+                       goto END;
+       }
+
+END :
+       return result;
+}
+
+static gboolean __call_is_authorized_nfc_access_sync(GDBusProxy *proxy,
+       guint arg_se_type,
+       const gchar *arg_package,
+       GVariant *arg_aid,
+       gboolean *out_result)
+{
+       GVariant *var;
+       GError *error = NULL;
+
+       var = g_dbus_proxy_call_sync(proxy,
+               "isAuthorizedNfcAccess",
+               g_variant_new("(us@a(y))", arg_se_type,
+                       arg_package, arg_aid),
+               G_DBUS_CALL_FLAGS_NONE, -1,
+               NULL,
+               &error);
+       if (var != NULL) {
+               g_variant_get(var, "(b)", (gboolean *)out_result);
+               g_variant_unref(var);
+       } else {
+               DEBUG_ERR_MSG("g_dbus_proxy_call_sync failed, [%s]", error->message);
+               g_error_free(error);
+       }
+
+       return (var != NULL);
+}
+
+bool net_nfc_service_access_control_is_authorized_nfc_access(uint8_t se_type,
+       const char *package, const uint8_t *aid, const uint32_t len)
+{
+       gboolean result = false;
+       GVariant *var_aid = NULL;
+       int type = 0;
+       GError *error = NULL;
+
+       SECURE_MSG("check nfc access, [%s]", package);
+
+#ifndef CHECK_NFC_ACCESS_FOR_ESE
+       if (se_type == NET_NFC_SE_TYPE_ESE) {
+               return true;
+       }
+#endif
+       if (dbus_proxy == NULL) {
+
+               dbus_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
+                       G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, /* GDBusInterfaceInfo */
+                       "org.tizen.SmartcardService",
+                       "/org/tizen/SmartcardService/AccessControl",
+                       "org.tizen.SmartcardService.AccessControl",
+                       NULL, /* GCancellable */
+                       &error);
+               if (dbus_proxy == NULL) {
+                       DEBUG_ERR_MSG("g_dbus_proxy_new_for_bus_sync failed");
+                       return false;
+               }
+       }
+
+       /* se type value
+        *
+        * 0x1? : UICC, low 4 bits indicates UICC number.
+        *        (ex, 0x10 = "SIM1", 0x11 = "SIM2", ...
+        * 0x2x : eSE. (low 4 bits doesn't be cared)
+        * 0x3x : SD Card..??
+        */
+       switch (se_type) {
+       case NET_NFC_SE_TYPE_UICC :
+               type = 0x10;
+               break;
+
+#ifdef CHECK_NFC_ACCESS_FOR_ESE
+       case NET_NFC_SE_TYPE_ESE :
+               type = 0x20;
+               break;
+#endif
+       case NET_NFC_SE_TYPE_SDCARD :
+               type = 0x30;
+               break;
+
+       default :
+               return false;
+       }
+
+       var_aid = net_nfc_util_gdbus_buffer_to_variant(aid, len);
+
+       /* check access */
+       if (__call_is_authorized_nfc_access_sync(
+               dbus_proxy,
+               type, package, var_aid, &result) == false) {
+               DEBUG_ERR_MSG("_call_is_authorized_nfc_access_sync failed");
+
+               return false;
+       }
+
+       SECURE_MSG("result : %s", result ? "true" : "false");
+
+       return (result == true);
+}
+
+bool net_nfc_service_access_control_is_authorized_nfc_access_by_pid(
+       uint8_t se_type, pid_t pid, const uint8_t *aid, const uint32_t len)
+{
+       char package[1024];
+       int ret;
+
+       /* get package id from pid */
+       ret = net_nfc_util_get_pkgid_by_pid(pid, package, sizeof(package));
+       if (ret < 0) {
+               DEBUG_ERR_MSG("net_nfc_util_get_pkgid_by_pid failed [%d]", ret);
+               return false;
+       }
+
+       return net_nfc_service_access_control_is_authorized_nfc_access(se_type,
+               package, aid, len);
+}
+#if 0
+static gboolean __call_is_authorized_extra_access_sync(GDBusProxy *proxy,
+       guint arg_se_type,
+       const gchar *arg_package,
+       gboolean *out_result)
+{
+       GVariant *var;
+       GError *error = NULL;
+
+       var = g_dbus_proxy_call_sync(proxy,
+               "isAuthorizedExtraAccess",
+               g_variant_new("(us)", arg_se_type,
+                       arg_package),
+               G_DBUS_CALL_FLAGS_NONE, -1,
+               NULL,
+               &error);
+       if (var != NULL) {
+               g_variant_get(var, "(b)", (gboolean *)out_result);
+               g_variant_unref(var);
+       } else {
+               DEBUG_ERR_MSG("g_dbus_proxy_call_sync failed, [%s]", error->message);
+               g_error_free(error);
+       }
+
+       return (var != NULL);
+}
+#endif
+static void __is_authorized_extra_access_cb(GObject *source_object,
+       GAsyncResult *res,
+       gpointer user_data)
+{
+       GError *error = NULL;
+       GVariant *_ret;
+
+       _ret = g_dbus_proxy_call_finish(G_DBUS_PROXY(source_object), res, &error);
+       if (_ret != NULL) {
+               g_variant_get(_ret, "(b)", (gboolean *)user_data);
+               g_variant_unref(_ret);
+       } else {
+               DEBUG_ERR_MSG("g_dbus_proxy_call_sync failed, [%s]", error->message);
+               g_error_free(error);
+               *(gboolean *)user_data = false;
+       }
+
+       net_nfc_server_controller_quit_dispatch_loop();
+}
+
+static gboolean __call_is_authorized_extra_access(GDBusProxy *proxy,
+       guint arg_se_type,
+       const gchar *arg_package,
+       GAsyncReadyCallback callback,
+       gpointer user_param)
+{
+       g_dbus_proxy_call(proxy,
+               "isAuthorizedExtraAccess",
+               g_variant_new("(us)", arg_se_type,
+                       arg_package),
+               G_DBUS_CALL_FLAGS_NONE, -1,
+               NULL,
+               callback,
+               user_param);
+
+       return true;
+}
+
+static bool __check_se_dependant_access_rule(uint8_t se_type,
+       const char *package)
+{
+       gboolean result = false;
+       int type = 0;
+       GError *error = NULL;
+
+       SECURE_MSG("check extra access, [%s]", package);
+
+       if (dbus_proxy == NULL) {
+
+               dbus_proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
+                       G_DBUS_PROXY_FLAGS_NONE,
+                       NULL, /* GDBusInterfaceInfo */
+                       "org.tizen.SmartcardService",
+                       "/org/tizen/SmartcardService/AccessControl",
+                       "org.tizen.SmartcardService.AccessControl",
+                       NULL, /* GCancellable */
+                       &error);
+               if (dbus_proxy == NULL) {
+                       DEBUG_ERR_MSG("g_dbus_proxy_new_for_bus_sync failed");
+                       return false;
+               }
+       }
+
+       /* se type value
+        *
+        * 0x1? : UICC, low 4 bits indicates UICC number.
+        *        (ex, 0x10 = "SIM1", 0x11 = "SIM2", ...
+        * 0x2x : eSE. (low 4 bits doesn't be cared)
+        * 0x3x : SD Card..??
+        */
+       switch (se_type) {
+       case NET_NFC_SE_TYPE_UICC :
+               type = 0x10;
+               break;
+
+       case NET_NFC_SE_TYPE_ESE :
+               type = 0x20;
+               break;
+
+       case NET_NFC_SE_TYPE_SDCARD :
+               type = 0x30;
+               break;
+
+       default :
+               return false;
+       }
+
+       /* check access */
+       if (__call_is_authorized_extra_access(
+               dbus_proxy,
+               type, package,
+               __is_authorized_extra_access_cb,
+               &result) == false) {
+               DEBUG_ERR_MSG("_call_is_authorized_extra_access_sync failed");
+
+               return false;
+       }
+
+       net_nfc_server_controller_run_dispatch_loop();
+
+       SECURE_MSG("result : %s", result ? "true" : "false");
+
+       return (result == true);
+}
+
+static bool __check_certificate_level(pid_t pid, int mask)
+{
+       bool result = false;
+       int ret = 0;
+       pkgmgrinfo_certinfo_h certinfo = NULL;
+       char pkgid[1024];
+       int type;
+
+       if (net_nfc_util_get_pkgid_by_pid(pid, pkgid, sizeof(pkgid)) == false) {
+               DEBUG_ERR_MSG("net_nfc_util_get_pkgid_by_pid failed");
+
+               goto END;
+       }
+
+       /* load certificate hashes */
+       ret = pkgmgrinfo_pkginfo_create_certinfo(&certinfo);
+       if (ret < 0) {
+               DEBUG_ERR_MSG("pkgmgr_pkginfo_create_certinfo failed, [%d]", ret);
+
+               goto END;
+       }
+
+       ret = pkgmgrinfo_pkginfo_load_certinfo(pkgid, certinfo);
+       if (ret < 0) {
+               DEBUG_ERR_MSG("pkgmgr_pkginfo_load_certinfo, failed [%d]", ret);
+
+               goto END;
+       }
+
+       /* check permission */
+       for (type = (int)PMINFO_AUTHOR_ROOT_CERT;
+               type <= (int)PMINFO_DISTRIBUTOR2_SIGNER_CERT; type++)
+       {
+               const char *value = NULL;
+
+               ret = pkgmgrinfo_pkginfo_get_cert_value(certinfo,
+                       (pkgmgrinfo_cert_type)type, &value);
+               if (ret == PMINFO_R_OK &&
+                       value != NULL && strlen(value) > 0)
+               {
+                       int visibility = 0;
+
+                       ret = cert_svc_get_visibility_by_root_certificate(value, strlen(value), &visibility);
+                       if (ret != CERT_SVC_ERR_NO_ERROR) {
+                               if (ret != CERT_SVC_ERR_NO_ROOT_CERT) {
+                                       SECURE_MSG("cert_svc_get_visibility_by_root_certificate failed, type [%d], error [%d]", type, ret);
+                               }
+                               continue;
+                       }
+
+                       if (visibility & mask) {
+                               SECURE_MSG("found!! type [%d], visibility [%08X]", type, visibility);
+
+                               result = true;
+                               break;
+                       }
+               }
+       }
+
+END :
+       if (certinfo != NULL) {
+               pkgmgrinfo_pkginfo_destroy_certinfo(certinfo);
+       }
+
+       return result;
+}
+
+static bool __check_platform_certificate(pid_t pid)
+{
+       bool result = false;
+
+       SECURE_MSG("check platform level, [%d]", pid);
+
+       result = __check_certificate_level(pid, CERT_SVC_VISIBILITY_PLATFORM);
+
+       SECURE_MSG("result : %s", result ? "true" : "false");
+
+       return result;
+}
+
+static bool __check_ese_access_rule(pid_t pid)
+{
+       bool result = false;
+       int mask = CERT_SVC_VISIBILITY_PLATFORM | CERT_SVC_VISIBILITY_PARTNER_MANUFACTURER | CERT_SVC_VISIBILITY_PARTNER_OPERATOR | CERT_SVC_VISIBILITY_PARTNER;
+
+       SECURE_MSG("check ese rule, [%d]", pid);
+
+       result = __check_certificate_level(pid, mask);
+
+       SECURE_MSG("result : %s", result ? "true" : "false");
+
+       return result;
+}
diff --git a/src/plugin/CMakeLists.txt b/src/plugin/CMakeLists.txt
new file mode 100644 (file)
index 0000000..cdbd8d5
--- /dev/null
@@ -0,0 +1,67 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+
+SET (this_target cardemulation_plugin)
+SET (this_target_2 useese_plugin)
+
+SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/cmake_build_tmp/output)
+include_directories(${CMAKE_SOURCE_DIR}/src/commonlib/include)
+include_directories(${CMAKE_SOURCE_DIR}/src/clientlib/include)
+#include_directories(${CMAKE_SOURCE_DIR}/src/clientlib)
+
+#INCLUDE(FindPkgConfig)
+#pkg_check_modules(pkg REQUIRED glib-2.0 dlog libxml-2.0)
+INCLUDE(FindPkgConfig)
+pkg_check_modules(pkgs_plugin REQUIRED
+       glib-2.0
+       pkgmgr-info
+       libxml-2.0
+       dlog
+       syspopup-caller
+)
+
+FOREACH(flag ${pkgs_plugin_CFLAGS})
+    SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET (${this_target}_SOURCE_FILES
+    pkgmgr_plugin_cardemulation.c
+)
+SET (${this_target_2}_SOURCE_FILES
+    pkgmgr_plugin_useese.c
+)
+
+ADD_DEFINITIONS("-DDLOG_ERROR_ENABLED")
+ADD_DEFINITIONS("-DLOG_TAG=\"CARDEMULATION_PLUGIN\"")
+SET(EXTRA_CFLAGS  "${EXTRA_CFLAGS} -Wall" )
+
+SET(CMAKE_C_FLAGS_PROFILING    " -g -pg")
+SET(CMAKE_CXX_FLAGS_PROFILING  " -std=c++0x -g -pg")
+SET(CMAKE_C_FLAGS_DEBUG        " -g")
+SET(CMAKE_CXX_FLAGS_DEBUG      " -std=c++0x -g")
+SET(CMAKE_C_FLAGS_RELEASE      " -g")
+SET(CMAKE_CXX_FLAGS_RELEASE    " -std=c++0x -g")
+SET(CMAKE_C_FLAGS_CCOV         " -g --coverage")
+SET(CMAKE_CXX_FLAGS_CCOV       " -std=c++0x -g --coverage")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -std=gnu99")
+
+SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -pipe -fomit-frame-pointer -Wall -Wno-trigraphs -Werror-implicit-function-declaration  -fno-strict-aliasing -Wl,-zdefs")
+
+SET(ARM_CFLAGS "${ARM_CLAGS} -mapcs -mno-sched-prolog -mabi=aapcs-linux -Uarm -fno-common -fpic")
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror-implicit-function-declaration")
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+
+
+## Create Library
+ADD_LIBRARY (${this_target} SHARED ${${this_target}_SOURCE_FILES} )
+ADD_LIBRARY (${this_target_2} SHARED ${${this_target_2}_SOURCE_FILES} )
+## SET LINKER FLAGS
+#SET(CMAKE_SHARED_LINKER_FLAGS -Wl,--no-undefined)
+
+TARGET_LINK_LIBRARIES(${this_target} ${pkgs_plugin_LDFLAGS} nfc)
+TARGET_LINK_LIBRARIES(${this_target_2} ${pkgs_plugin_LDFLAGS} nfc)
+
+INSTALL(TARGETS ${this_target} DESTINATION "/usr/etc/package-manager/parserlib/metadata")
+INSTALL(TARGETS ${this_target_2} DESTINATION "/usr/etc/package-manager/parserlib/metadata")
+
diff --git a/src/plugin/pkgmgr_plugin_cardemulation.c b/src/plugin/pkgmgr_plugin_cardemulation.c
new file mode 100644 (file)
index 0000000..5347bca
--- /dev/null
@@ -0,0 +1,461 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <pkgmgr-info.h>
+#include <glib.h>
+#include <string.h>
+#include <libxml/parser.h>
+#include <dlog.h>
+#include <syspopup_caller.h>
+#include <bundle_internal.h>
+
+#include "net_nfc.h"
+
+#define DBG(fmt, args ...) \
+       do { \
+               LOGD(fmt, ## args); \
+       } while (0)
+
+#ifndef PKGMGR_MODULE_API
+#define PKGMGR_MODULE_API __attribute__ ((visibility("default")))
+#endif
+
+typedef struct metadata {
+       const char *key;
+       const char *value;
+} metadata;
+
+int _nfc_register_aid(char *xml_path, const char *pkgid)
+{
+       xmlChar *key = NULL;
+       int ret = 0;
+       char *aid = NULL;
+       net_nfc_se_type_e se_type;
+       bool unlock_required;
+       int power;
+       net_nfc_card_emulation_category_t category;
+       xmlNodePtr pRoot, pCurrentElement;
+       char **conflict_handlers = NULL;
+
+       DBG(">>>>");
+       fprintf(stderr, "[NFC CARD EMUL] _nfc_register_aid >>>>>>>>\n");
+
+       xmlDocPtr pDocument = xmlParseFile(xml_path);
+       if (pDocument == NULL) {
+               DBG("xmlDocPtr conversion failed.");
+               return NET_NFC_OPERATION_FAIL;
+       }
+
+       pRoot = xmlDocGetRootElement(pDocument);
+       if (pRoot == NULL) {
+               DBG(" Empty document");
+               xmlFreeDoc(pDocument);
+               return NET_NFC_OPERATION_FAIL;
+       }
+
+       for (pCurrentElement = pRoot->children; pCurrentElement; pCurrentElement = pCurrentElement->next) {
+               if (pCurrentElement->type == XML_ELEMENT_NODE) {
+                       if (!(xmlStrcmp(pCurrentElement->name, (const xmlChar *) "wallet"))) {
+                               xmlNodePtr pSubElement;
+
+                               DBG("wallet element found...");
+
+                               for (pSubElement = pCurrentElement->children; pSubElement; pSubElement = pSubElement->next) {
+                                       if (pSubElement->type == XML_ELEMENT_NODE) {
+                                               if (!(xmlStrcmp(pSubElement->name, (const xmlChar *) "aid-group"))) {
+
+                                                       DBG("aid-group element found...");
+
+                                                       key = xmlGetProp(pSubElement, (const xmlChar*)"category");
+                                                       if (key != NULL) {
+                                                               xmlNodePtr pAidElement;
+
+                                                               DBG( "category : %s, ", (char *)key);
+
+                                                               if (strcasecmp((char*)key, "payment") == 0) {
+                                                                       category = NET_NFC_CARD_EMULATION_CATEGORY_PAYMENT;
+                                                               }
+                                                               else if (strcasecmp((char*)key, "other") == 0) {
+                                                                       category = NET_NFC_CARD_EMULATION_CATEGORY_OTHER;
+                                                               }
+                                                               else {
+                                                                       ret = NET_NFC_NO_DATA_FOUND;
+                                                                       goto ERROR;
+                                                               }
+                                                               xmlFree(key);
+                                                               key = NULL;
+
+                                                               //parsing property
+                                                               for (pAidElement = pSubElement->children; pAidElement; pAidElement = pAidElement->next) {
+                                                                       if (pAidElement->type == XML_ELEMENT_NODE) {
+                                                                               if (!(xmlStrcmp(pAidElement->name, (const xmlChar *) "aid"))) {
+                                                                                       key = xmlGetProp(pAidElement, (const xmlChar*)"aid");
+                                                                                       if (key != NULL) {
+                                                                                               DBG("aid : %s, ", (char *)key);
+                                                                                               aid = strdup((char*)key);
+                                                                                               xmlFree(key);
+                                                                                               key = NULL;
+                                                                                       }
+                                                                                       else {
+                                                                                               DBG("aid is null");
+                                                                                               ret = NET_NFC_NO_DATA_FOUND;
+                                                                                               goto ERROR;
+                                                                                       }
+
+                                                                                       key = xmlGetProp(pAidElement, (const xmlChar*)"se_type");
+                                                                                       if (key != NULL) {
+                                                                                               DBG("se_type : %s, ", (char *)key);
+                                                                                               if (strcasecmp((char*)key, "ese") == 0) {
+                                                                                                       se_type = NET_NFC_SE_TYPE_ESE;
+                                                                                               }
+                                                                                               else if (strcasecmp((char*)key, "uicc") == 0) {
+                                                                                                       se_type = NET_NFC_SE_TYPE_UICC;
+                                                                                               }
+                                                                                               else if (strcasecmp((char*)key, "hce") == 0) {
+                                                                                                       se_type = NET_NFC_SE_TYPE_HCE;
+                                                                                               }
+                                                                                               else {
+                                                                                                       ret = NET_NFC_NO_DATA_FOUND;
+                                                                                                       goto ERROR;
+                                                                                               }
+                                                                                               xmlFree(key);
+                                                                                               key = NULL;
+                                                                                       }
+                                                                                       else {
+                                                                                               DBG("se_type is null");
+                                                                                               ret = NET_NFC_NO_DATA_FOUND;
+                                                                                               goto ERROR;
+                                                                                       }
+
+                                                                                       key = xmlGetProp(pAidElement, (const xmlChar*)"unlock");
+                                                                                       if (key != NULL) {
+                                                                                               DBG("unlock : %s, ", (char *)key);
+                                                                                               if (strcasecmp((char*)key, "true") == 0) {
+                                                                                                       unlock_required = true;
+                                                                                               }
+                                                                                               else if (strcasecmp((char*)key, "false") == 0) {
+                                                                                                       unlock_required = false;
+                                                                                               }
+                                                                                               else {
+                                                                                                       ret = NET_NFC_NO_DATA_FOUND;
+                                                                                                       goto ERROR;
+                                                                                               }
+                                                                                               xmlFree(key);
+                                                                                               key = NULL;
+                                                                                       }
+                                                                                       else {
+                                                                                               DBG( "unlock is null");
+                                                                                               ret = NET_NFC_NO_DATA_FOUND;
+                                                                                               goto ERROR;
+                                                                                       }
+
+                                                                                       key = xmlGetProp(pAidElement, (const xmlChar*)"power");
+                                                                                       if (key != NULL) {
+                                                                                               DBG("power : %s, ", (char *)key);
+                                                                                               if (strcasecmp((char*)key, "on") == 0) {
+                                                                                                       power = unlock_required ? 0x29 : 0x39;
+                                                                                               }
+                                                                                               else if (strcasecmp((char*)key, "off") == 0) {
+                                                                                                       power = unlock_required ? 0x2B : 0x3B;
+                                                                                               }
+                                                                                               else if (strcasecmp((char*)key, "sleep") == 0) {
+                                                                                                       power = unlock_required ? 0x29 : 0x39;
+                                                                                               }
+                                                                                               else {
+                                                                                                       ret = NET_NFC_NO_DATA_FOUND;
+                                                                                                       goto ERROR;
+                                                                                               }
+                                                                                               xmlFree(key);
+                                                                                               key = NULL;
+                                                                                       }
+                                                                                       else {
+                                                                                               DBG("power is null");
+                                                                                               ret = NET_NFC_NO_DATA_FOUND;
+                                                                                               goto ERROR;
+                                                                                       }
+
+                                                                                       ret = net_nfc_client_se_add_route_aid_sync(pkgid, se_type, category, aid, unlock_required, power);
+                                                                                       if (ret == NET_NFC_DATA_CONFLICTED) {
+                                                                                               if (conflict_handlers == NULL) {
+                                                                                                       ret = net_nfc_client_se_get_conflict_handlers_sync(pkgid, category, aid, &conflict_handlers);
+                                                                                                       if (ret == NET_NFC_DATA_CONFLICTED) {
+                                                                                                               DBG("conflicted aid [%s], [%s]", aid, conflict_handlers[0]);
+                                                                                                       } else {
+                                                                                                               DBG("net_nfc_client_se_get_conflict_handlers_sync failed, [%d]", ret);
+                                                                                                       }
+                                                                                               } else {
+                                                                                                       DBG("skip more conflict");
+                                                                                               }
+                                                                                       } else if (ret == NET_NFC_OK) {
+                                                                                               DBG("net_nfc_client_se_add_route_aid_sync() result : [%d]", ret);
+                                                                                       }
+
+                                                                                       free(aid);
+                                                                                       aid = NULL;
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                                       else {
+                                                               DBG("category is null");
+                                                               ret = NET_NFC_NO_DATA_FOUND;
+                                                               goto ERROR;
+                                                       }
+                                               }
+                                               else {
+                                                       DBG("cannot find aid-group element : %s", (char*)pSubElement->name);
+                                                       ret = NET_NFC_NO_DATA_FOUND;
+                                                       goto ERROR;
+                                               }
+                                       }
+                               }
+                       }
+                       else {
+                               DBG("cannot find wallet element : %s", (char*)pCurrentElement->name);
+                               ret = NET_NFC_NO_DATA_FOUND;
+                       }
+               }
+       }
+
+       xmlUnlinkNode(pRoot);
+       xmlFreeNode(pRoot);
+       xmlFreeDoc(pDocument);
+
+       if (conflict_handlers != NULL) {
+               bundle *b;
+
+               DBG("launch popup");
+
+               b = bundle_create();
+
+               bundle_add(b, "package", pkgid);
+               bundle_add_str_array(b, "conflict",
+                       (const char **)conflict_handlers,
+                       g_strv_length(conflict_handlers));
+
+               ret = syspopup_launch("nfc-syspopup", b);
+               DBG("syspopup_launch result [%d]", ret);
+
+               bundle_free(b);
+
+               g_strfreev(conflict_handlers);
+
+               ret = NET_NFC_OK;
+       }
+
+       DBG("<<<<");
+
+       return ret;
+
+ERROR:
+       net_nfc_client_se_remove_package_aids_sync(pkgid);
+
+       if (conflict_handlers != NULL) {
+               g_strfreev(conflict_handlers);
+       }
+
+       if (aid != NULL) {
+               free(aid);
+       }
+
+       if (key != NULL) {
+               xmlFree(key);
+       }
+
+       xmlUnlinkNode(pRoot);
+       xmlFreeNode(pRoot);
+       xmlFreeDoc(pDocument);
+
+       return ret;
+}
+
+static void __build_full_path(const char *prefix, const char *tail,
+       char *output, size_t out_len)
+{
+       size_t len;
+
+       /* add tail slash */
+       len = strlen(prefix);
+       if (len > 0) {
+               len = snprintf(output, out_len, "%s", prefix);
+               if (len > 0 && len < out_len - 1 && output[len - 1] != '/') {
+                       output[len] = '/';
+                       output[len + 1] = '\0';
+                       len++;
+               }
+       }
+
+       if (strlen(tail) > 0) {
+               if (len > 0) {
+                       const char *buf2;
+
+                       if (tail[0] == '/') {
+                               buf2 = tail + 1;
+                       } else {
+                               buf2 = tail;
+                       }
+
+                       strncat(output + len, buf2, out_len - len - 1);
+                       output[out_len - 1] = '\0';
+               } else {
+                       snprintf(output, out_len, "%s", tail);
+               }
+       }
+}
+
+static int __install(const char *pkgid, const char *appid, GList *md_list)
+{
+       GList *list;
+       pkgmgrinfo_pkginfo_h handle = NULL;
+       int ret;
+       char *path = NULL;
+       char buf[1024] = { 0, };
+       metadata *detail;
+
+       fprintf(stderr, "[NFC CARD EMUL] __install >>>>>>>>\n");
+
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       if (ret != 0) {
+               DBG("pkgmgrinfo_pkginfo_get_pkginfo() [%d]", ret);
+               fprintf(stderr, "[NFC CARD EMUL] pkgmgrinfo_pkginfo_get_pkginfo failed [%d]\n", ret);
+               goto END;
+       }
+
+
+       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
+       if (ret != 0) {
+               DBG("pkgmgrinfo_pkginfo_get_root_path() [%d]", ret);
+               fprintf(stderr, "[NFC CARD EMUL] pkgmgrinfo_pkginfo_get_root_path failed [%d]\n", ret);
+               goto END;
+       }
+
+       DBG("pkgid [%s]", pkgid);
+       DBG("root path [%s]", path);
+
+       //get xml path from metadata
+       list = g_list_first(md_list);
+       detail = (metadata*)list->data;
+       DBG("key = %s, value = %s", detail->key, detail->value);
+
+       /* add tail slash */
+       __build_full_path(path, detail->value, buf, sizeof(buf));
+       DBG("full path = %s", buf);
+
+       ret = _nfc_register_aid(buf, pkgid);
+       DBG("_nfc_register_aid() [%d]", ret);
+
+END :
+       if (!handle) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       }
+
+       return ret;
+}
+
+static int __uninstall(const char *pkgid, const char *appid, GList *md_list)
+{
+       int ret;
+
+       fprintf(stderr, "[NFC CARD EMUL] __uninstall >>>>>>>>\n");
+
+       ret = net_nfc_client_se_remove_package_aids_sync(pkgid);
+       DBG("net_nfc_client_se_remove_route_aid_sync() [%d]", ret);
+
+       return ret;
+}
+
+PKGMGR_MODULE_API
+int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *md_list)
+{
+       int ret;
+
+       DBG("PKGMGR_MDPARSER_PLUGIN_INSTALL called");
+
+       fprintf(stderr, "[NFC CARD EMUL] INSTALL >>>>>>>>\n");
+
+       ret = net_nfc_client_initialize();
+       if (ret != 0) {
+               DBG("net_nfc_client_initialize() [%d]", ret);
+
+               fprintf(stderr, "[NFC CARD EMUL] net_nfc_client_initialize failed [%d]\n", ret);
+               return ret;
+       }
+
+       ret = __install(pkgid, appid, md_list);
+
+       net_nfc_client_deinitialize();
+
+       return ret;
+}
+
+
+PKGMGR_MODULE_API
+int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *md_list)
+{
+       int ret;
+
+       DBG("PKGMGR_MDPARSER_PLUGIN_UNINSTALL called");
+
+       fprintf(stderr, "[NFC CARD EMUL] UNINSTALL >>>>>>>>\n");
+
+       ret = net_nfc_client_initialize();
+       if (ret != 0) {
+               DBG("net_nfc_client_initialize() [%d]", ret);
+               fprintf(stderr, "[NFC CARD EMUL] net_nfc_client_initialize failed [%d]\n", ret);
+               return ret;
+       }
+
+       ret = __uninstall(pkgid, appid, md_list);
+
+       net_nfc_client_deinitialize();
+
+       return ret;
+}
+
+PKGMGR_MODULE_API
+int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
+{
+       int ret;
+
+       DBG("PKGMGR_MDPARSER_PLUGIN_UPGRADE called");
+
+       fprintf(stderr, "[NFC CARD EMUL] UPGRADE >>>>>>>>");
+
+       ret = net_nfc_client_initialize();
+       if (ret != 0) {
+               DBG("net_nfc_client_initialize() [%d]", ret);
+               fprintf(stderr, "[NFC CARD EMUL] net_nfc_client_initialize failed [%d]\n", ret);
+               return ret;
+       }
+
+       ret = __uninstall(pkgid, appid, list);
+       if (ret != 0) {
+               DBG("__uninstall() [%d]", ret);
+       } else {
+               DBG("__uninstall() success");
+       }
+
+       ret = __install(pkgid, appid, list);
+       if (ret != 0) {
+               DBG("__install() [%d]", ret);
+       } else {
+               DBG("__install() success");
+       }
+
+       net_nfc_client_deinitialize();
+
+       return ret;
+}
diff --git a/src/plugin/pkgmgr_plugin_useese.c b/src/plugin/pkgmgr_plugin_useese.c
new file mode 100644 (file)
index 0000000..9eb78d6
--- /dev/null
@@ -0,0 +1,281 @@
+/*
+ * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <pkgmgr-info.h>
+#include <glib.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <dlog.h>
+#include <sys/stat.h>
+#include <unistd.h>
+//#include <sys/types.h>
+//#include <syspopup_caller.h>
+
+//#include "net_nfc.h"
+
+#define CSA_USEESE_PATH "/csa/useese"
+#define CSA_USEESE_FILE_PATH "/csa/useese/aid.dat"
+#define DBG(fmt, args ...) \
+       do { \
+               LOGD(fmt, ## args); \
+       } while (0)
+
+#ifndef PKGMGR_MODULE_API
+#define PKGMGR_MODULE_API __attribute__ ((visibility("default")))
+#endif
+
+typedef struct metadata {
+       const char *key;
+       const char *value;
+} metadata;
+
+static GList *list;
+static GList *list_metadata;
+
+static int __add_aid_to_csa(const char *aid_string)
+{
+       int res = 0;
+       const int filePerm = 0777;
+       int fd = -1;
+       int writeLen = 0;
+       char buf[1024 + 1];
+       int count = 0;
+       struct stat stat_buf;
+       const char tk = ';';
+       const char newline = '\n';
+       char *token = NULL;
+       char *token_metadata = NULL;
+       int token_counter = 0;
+       int metadata_token_counter = 0;
+       int i = 0;
+
+       memset(&stat_buf, 0, sizeof(struct stat));
+
+       if (stat(CSA_USEESE_PATH, &stat_buf) < 0) {
+               if (mkdir(CSA_USEESE_PATH, filePerm) == 0) {
+                       DBG("mkdir sucess!!!!!");
+                       res = chmod("/csa/useese", 0777);
+                       if(res < 0) {
+                               DBG("fail chmod /csa/useese");
+                               return -1;
+                       }
+               }
+               else {
+                       DBG("mkdir error");
+                       return -1;
+               }
+       }
+
+       //get aid list from csa
+       fd = open(CSA_USEESE_FILE_PATH, 00000002 | 00000100, filePerm);
+       if (fd >= 0) {
+               count = read(fd, buf, 1024);
+               ///////////
+               if (count > 0 && count <= 1024) {
+                       while (count > 0 && buf[count - 1] == '\n')
+                               count--;
+                       buf[count] = '\0';
+               } else {
+                       buf[0] = '\0';
+               }
+               close(fd);
+       }
+       ///////////
+       DBG("count : %d", count);
+       if (count > 0) {
+               token = strtok(buf, ";");
+               while(token) {
+                       DBG("Token[%d] : %s", token_counter, token);
+                       list = g_list_append(list, strdup(token));
+                       token_counter++;
+                       token = strtok(NULL, ";");
+               }
+       }
+
+       //get aid list from metadata
+       char *aids = strdup(aid_string);
+       token_metadata = strtok(aids, ";");
+       while(token_metadata) {
+               DBG("Metadata Token[%d] : %s", metadata_token_counter, token);
+               list_metadata = g_list_append(list_metadata, strdup(token_metadata));
+               metadata_token_counter++;
+               token_metadata = strtok(NULL, ";");
+       }
+       //csa vs metadata value
+       for (i = 0; i < g_list_length(list_metadata); i++) {
+               GList* node;
+               node = g_list_nth(list_metadata, i);
+               DBG("[%d]th metadata value is %s", i, (char*)node->data);
+
+               if (g_list_find_custom(list, (char*)node->data, (GCompareFunc)strcmp)) {
+                       DBG("AID [%s] is already exist!", (char*)node->data);
+               }
+               else {
+                       DBG("AID [%s] will be added to csa", (char*)node->data);
+                       list = g_list_append(list, strdup((char*)node->data));
+               }
+       }
+
+       //re-write the final aid list
+       fd = open(CSA_USEESE_FILE_PATH, 00000002 | 00000100, filePerm);
+       if (fd >= 0) {
+               for (i = 0; i < g_list_length(list); i++) {
+                       GList* node;
+                       node = g_list_nth(list, i);
+                       DBG("[%d]th list value is %s", i, (char*)node->data);
+
+                       writeLen = write(fd, (char*)node->data, strlen((char*)node->data));
+                       DBG("write to csa file : %d", writeLen);
+                       writeLen = write(fd, &tk, 1);   //temp
+                       DBG("write to csa file : %d", writeLen);
+               }
+               writeLen = write(fd, &newline, 1);      //temp
+               DBG("write to csa file : %d", writeLen);
+
+               close(fd);
+       }
+
+       free(aids);
+
+       return res;
+}
+
+static int __install(const char *pkgid, const char *appid, GList *md_list)
+{
+       GList *list;
+       pkgmgrinfo_pkginfo_h handle = NULL;
+       int ret;
+       char *path = NULL;
+       metadata *detail;
+
+       ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
+       if (ret != 0) {
+               DBG("pkgmgrinfo_pkginfo_get_pkginfo() [%d]", ret);
+               goto END;
+       }
+
+       ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
+       if (ret != 0) {
+               DBG("pkgmgrinfo_pkginfo_get_root_path() [%d]", ret);
+               goto END;
+       }
+
+       DBG("pkgid [%s]", pkgid);
+       DBG("root path [%s]", path);
+
+       //get xml path from metadata
+       list = g_list_first(md_list);
+       detail = (metadata*)list->data;
+       DBG("key = %s, value = %s", detail->key, detail->value);
+
+       /* add tail slash */
+       ret = __add_aid_to_csa(detail->value);
+       DBG("__add_aid_to_csa() result : %d", ret);
+
+//     ret = _nfc_register_aid(buf, pkgid);
+//     DBG("_nfc_register_aid() [%d]", ret);
+
+END :
+       if (!handle) {
+               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+       }
+
+       return ret;
+}
+
+static int __uninstall(const char *pkgid, const char *appid, GList *md_list)
+{
+       int ret = 0;
+
+//     ret = net_nfc_client_se_remove_package_aids_sync(pkgid);
+//     DBG("net_nfc_client_se_remove_route_aid_sync() [%d]", ret);
+
+       return ret;
+}
+
+PKGMGR_MODULE_API
+int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *md_list)
+{
+       int ret;
+
+       DBG("PKGMGR_MDPARSER_PLUGIN_INSTALL called");
+
+//     ret = net_nfc_client_initialize();
+//     if (ret != 0) {
+//             DBG("net_nfc_client_initialize() [%d]", ret);
+//             return ret;
+//     }
+
+       ret = __install(pkgid, appid, md_list);
+
+//     net_nfc_client_deinitialize();
+
+       return ret;
+}
+
+
+PKGMGR_MODULE_API
+int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *md_list)
+{
+       int ret;
+
+       DBG("PKGMGR_MDPARSER_PLUGIN_UNINSTALL called");
+
+//     ret = net_nfc_client_initialize();
+//     if (ret != 0) {
+//             DBG("net_nfc_client_initialize() [%d]", ret);
+//             return ret;
+//     }
+
+       ret = __uninstall(pkgid, appid, md_list);
+
+//     net_nfc_client_deinitialize();
+
+       return ret;
+}
+
+PKGMGR_MODULE_API
+int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list)
+{
+       int ret;
+
+       DBG("PKGMGR_MDPARSER_PLUGIN_UPGRADE called");
+
+//     ret = net_nfc_client_initialize();
+//     if (ret != 0) {
+//             DBG("net_nfc_client_initialize() [%d]", ret);
+//             return ret;
+//     }
+
+       ret = __uninstall(pkgid, appid, list);
+       if (ret != 0) {
+               DBG("__uninstall() [%d]", ret);
+       } else {
+               DBG("__uninstall() success");
+       }
+
+       ret = __install(pkgid, appid, list);
+       if (ret != 0) {
+               DBG("__install() [%d]", ret);
+       } else {
+               DBG("__install() success");
+       }
+
+//     net_nfc_client_deinitialize();
+
+       return ret;
+}