Add card Inserted implementation and fix bug
[platform/core/connectivity/smartcard-service.git] / server / ServerGDBus.cpp
index f0f1b08..f12f39b 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+/* standard library header */
 #include <unistd.h>
 #include <glib.h>
 #include <gio/gio.h>
 #include <string>
 #include <sys/socket.h>
 
-#ifdef USER_SPACE_SMACK
-#include "security-server.h"
-#endif
-
+/* local header */
 #include "smartcard-types.h"
 #include "Debug.h"
 #include "ByteArray.h"
 #include "GDBusHelper.h"
 #include "ServerGDBus.h"
 
+#ifdef USE_CYNARA
+#include "cynara-client.h"
+#include "cynara-creds-gdbus.h"
+#include "cynara-session.h"
+
+#define SMARTCARD_PRIVILEGE "http://tizen.org/privilege/secureelement"
+#endif
+
 using namespace std;
 
+#ifdef __PRIVATE
+extern "C" bool smartcard_service_init_access_control(void *connection);
+extern "C" void smartcard_service_deinit_access_control();
+#endif
+
 namespace smartcard_service_api
 {
        GDBusDispatcher::GDBusDispatcher() : Synchronous()
@@ -57,7 +68,7 @@ namespace smartcard_service_api
                syncLock();
 
                q.push(make_pair(cb, params));
-               _INFO("request pushed, count [%d]", q.size());
+               _INFO("request pushed, count [%zu]", q.size());
 
                if (q.size() == 1) {
                        /* start dispatch */
@@ -93,10 +104,12 @@ namespace smartcard_service_api
                dispatcher->syncLock();
 
                dispatcher->q.pop();
+               /* LCOV_EXCL_START */
                if (dispatcher->q.size() > 0) {
-                       _INFO("remaining messages : %d", dispatcher->q.size());
+                       _INFO("remaining messages : %zu", dispatcher->q.size());
 
                        result = true;
+               /* LCOV_EXCL_STOP */
                } else {
                        _INFO("dispatch finished");
                }
@@ -129,11 +142,12 @@ namespace smartcard_service_api
                const gchar *name, const gchar *old_owner,
                const gchar *new_owner, void *user_data)
        {
+               /* LCOV_EXCL_START */
                if (name == NULL || old_owner == NULL || new_owner == NULL) {
                        _ERR("invalid parameter");
                        return;
                }
-
+               /* LCOV_EXCL_STOP */
                if (strlen(new_owner) == 0) {
                        ClientInstance *client;
 
@@ -166,6 +180,10 @@ namespace smartcard_service_api
 
                name_owner_changed((GDBusProxy *)connection,
                        name, old_owner, new_owner, user_data);
+
+               g_free(name);
+               g_free(old_owner);
+               g_free(new_owner);
        }
 
        bool ServerGDBus::_init()
@@ -183,10 +201,12 @@ namespace smartcard_service_api
                        &error);
                if (dbus_proxy == NULL)
                {
+                       /* LCOV_EXCL_START */
                        _ERR("Can not create proxy : %s", error->message);
                        g_error_free(error);
 
                        return false;
+                       /* LCOV_EXCL_STOP */
                }
 
                /* subscribe signal */
@@ -217,12 +237,14 @@ namespace smartcard_service_api
 
                connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
                if (connection != NULL) {
+               /* LCOV_EXCL_START */
                } else {
                        _ERR("Can not get connection %s", error->message);
                        g_error_free(error);
 
                        return false;
                }
+               /* LCOV_EXCL_STOP */
 
                _init();
 
@@ -230,12 +252,17 @@ namespace smartcard_service_api
                initReader();
                initSession();
                initChannel();
-
+#ifdef __PRIVATE
+               smartcard_service_init_access_control(connection);
+#endif
                return true;
        }
 
        void ServerGDBus::deinit()
        {
+#ifdef __PRIVATE
+               smartcard_service_deinit_access_control();
+#endif
                deinitChannel();
                deinitSession();
                deinitReader();
@@ -275,37 +302,99 @@ namespace smartcard_service_api
                if (_call_get_connection_unix_process_id_sync(
                        (GDBusProxy *)dbus_proxy, name,
                        &pid, NULL, &error) == true) {
+               /* LCOV_EXCL_START */
                } else {
                        _ERR("_g_freedesktop_dbus_call_get_connection_unix_process_id_sync failed  : %s", error->message);
                        g_error_free(error);
                }
+               /* LCOV_EXCL_STOP */
 
                return pid;
        }
 
-       static bool _is_authorized_request(GDBusMethodInvocation *invocation,
-               const char *rights)
+#ifdef USE_CYNARA
+       static bool _get_credentials(GDBusMethodInvocation *invocation)
        {
-               bool result = true;
-#ifdef USER_SPACE_SMACK
-               pid_t pid;
-               const char *name;
-               ClientInstance *instance;
+               int ret = 0;
+               int pid = 0;
+               char *user;
+               char *client;
+               char *client_session;
+               cynara *p_cynara = NULL;
+               const char *sender_unique_name;
+               GDBusConnection *connection;
+
+               connection = g_dbus_method_invocation_get_connection(invocation);
+               sender_unique_name = g_dbus_method_invocation_get_sender(invocation);
+
+               pid = ServerGDBus::getInstance().getPID(sender_unique_name);
+
+               ret = cynara_initialize(&p_cynara, NULL);
+               /* LCOV_EXCL_START */
+               if (ret != CYNARA_API_SUCCESS) {
+                       _ERR("cynara_initialize() failed");
+                       return false;
+               }
+               /* LCOV_EXCL_STOP */
 
-               name = g_dbus_method_invocation_get_sender(invocation);
+               ret = cynara_creds_gdbus_get_user(connection, sender_unique_name, USER_METHOD_DEFAULT, &user);
+               /* LCOV_EXCL_START */
+               if (ret != CYNARA_API_SUCCESS) {
+                       _ERR("cynara_creds_gdbus_get_user() failed");
+                       return false;
+               }
+               /* LCOV_EXCL_STOP */
 
-               instance = ServerResource::getInstance().getClient(name);
-               if (instance != NULL) {
-                       pid = instance->getPID();
-               } else {
-                       pid = ServerGDBus::getInstance().getPID(name);
+               ret = cynara_creds_gdbus_get_client(connection, sender_unique_name, CLIENT_METHOD_DEFAULT, &client);
+               /* LCOV_EXCL_START */
+               if (ret != CYNARA_API_SUCCESS) {
+                       _ERR("cynara_creds_gdbus_get_client() failed");
+                       g_free(user);
+                       return false;
                }
+               /* LCOV_EXCL_STOP */
+
+               _ERR("user :%s , client :%s ,unique_name : %s, pid() : %d", user, client, sender_unique_name, pid);
+
+               client_session = cynara_session_from_pid(pid);
 
-               result = (security_server_check_privilege_by_pid(
-                       pid,
-                       "smartcard-service",
-                       rights) == SECURITY_SERVER_API_SUCCESS);
+               /* LCOV_EXCL_START */
+               if (!client_session) {
+                       _ERR("cynara_session_from_pid() failed");
+                       g_free(client);
+                       g_free(user);
+                       return false;
+               }
+               /* LCOV_EXCL_STOP */
+
+               ret = cynara_check(p_cynara, client, client_session, user, SMARTCARD_PRIVILEGE);
+               if ( ret == CYNARA_API_ACCESS_ALLOWED ) {
+                       _INFO("cynara PASS");
+               }
+
+               g_free(client_session);
+               g_free(client);
+               g_free(user);
+
+               if (p_cynara) {
+                       cynara_finish(p_cynara);
+                       p_cynara = NULL;
+               }
+
+               return (ret == CYNARA_API_ACCESS_ALLOWED ) ? true : false;
+       }
 #endif
+
+       static bool _is_authorized_request(GDBusMethodInvocation *invocation)
+       {
+               bool result = false;
+
+#ifdef USE_CYNARA
+               result = _get_credentials(invocation);
+#endif
+
+               _ERR("api security check result : %d", result);
+
                return result;
        }
 
@@ -367,7 +456,7 @@ namespace smartcard_service_api
                                        /* generate certification hashes */
                                        instance->generateCertificationHashes();
                                } else {
-                                       _ERR("createClient failed");
+                                       _ERR("createClient failed"); /* LCOV_EXCL_LINE */
                                }
                        }
 
@@ -377,13 +466,13 @@ namespace smartcard_service_api
                                /* create service */
                                service = resource.createService(name);
                                if (service != NULL) {
-
                                        handle = service->getHandle();
                                        resource.getReaders(list);
 
                                        if (list.size() == 0) {
                                                _INFO("no secure elements");
                                        }
+                               /* LCOV_EXCL_START */
                                } else {
                                        _ERR("createService failed");
 
@@ -399,6 +488,7 @@ namespace smartcard_service_api
 
                        result = SCARD_ERROR_IPC_FAILED;
                }
+               /* LCOV_EXCL_STOP */
 
                readers = _reader_to_variant(list);
 
@@ -415,11 +505,13 @@ namespace smartcard_service_api
                GDBusMethodInvocation *invocation;
                void *user_data;
 
+               /* LCOV_EXCL_START */
                if (params.size() != 3) {
                        _ERR("invalid parameter");
 
                        return;
                }
+               /* LCOV_EXCL_STOP */
 
                object = (SmartcardServiceSeService *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
@@ -438,7 +530,7 @@ namespace smartcard_service_api
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -448,6 +540,7 @@ namespace smartcard_service_api
                        params.push_back((void *)user_data);
 
                        GDBusDispatcher::push(_process_se_service, params);
+               /* LCOV_EXCL_START */
                } else {
                        vector<pair<unsigned int, string> > list;
 
@@ -460,13 +553,14 @@ namespace smartcard_service_api
                                IntegerHandle::INVALID_HANDLE,
                                _reader_to_variant(list));
                }
+               /* LCOV_EXCL_STOP */
 
                return true;
        }
 
        static gboolean __process_shutdown(SmartcardServiceSeService *object,
                GDBusMethodInvocation *invocation,
-               intptr_t handle, void *user_data)
+               guint handle, void *user_data)
        {
                const char *name;
 
@@ -492,18 +586,20 @@ namespace smartcard_service_api
        {
                SmartcardServiceSeService *object;
                GDBusMethodInvocation *invocation;
-               intptr_t handle;
+               guint handle;
                void *user_data;
 
+               /* LCOV_EXCL_START */
                if (params.size() != 4) {
                        _ERR("invalid parameter");
 
                        return;
                }
+               /* LCOV_EXCL_STOP */
 
                object = (SmartcardServiceSeService *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               handle = (intptr_t)params[2];
+               handle = (gulong)params[2];
                user_data = params[3];
 
                __process_shutdown(object, invocation, handle, user_data);
@@ -514,13 +610,13 @@ namespace smartcard_service_api
 
        static gboolean _handle_shutdown(SmartcardServiceSeService *object,
                GDBusMethodInvocation *invocation,
-               intptr_t handle,
+               guint handle,
                void *user_data)
        {
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -531,6 +627,7 @@ namespace smartcard_service_api
                        params.push_back(user_data);
 
                        GDBusDispatcher::push(_process_shutdown, params);
+               /* LCOV_EXCL_START */
                } else {
                        _ERR("access denied");
 
@@ -538,6 +635,7 @@ namespace smartcard_service_api
                        smartcard_service_se_service_complete_shutdown(object,
                                invocation, SCARD_ERROR_SECURITY_NOT_ALLOWED);
                }
+               /* LCOV_EXCL_STOP */
 
                return true;
        }
@@ -558,6 +656,7 @@ namespace smartcard_service_api
                        G_CALLBACK(_handle_shutdown),
                        this);
 
+               /* LCOV_EXCL_START */
                if (g_dbus_interface_skeleton_export(
                        G_DBUS_INTERFACE_SKELETON(seService),
                        connection,
@@ -572,6 +671,7 @@ namespace smartcard_service_api
 
                        return false;
                }
+               /* LCOV_EXCL_STOP */
 
                return true;
        }
@@ -587,17 +687,41 @@ namespace smartcard_service_api
        void ServerGDBus::emitReaderInserted(unsigned int reader_id,
                const char *reader_name)
        {
+               /* LCOV_EXCL_START */
                smartcard_service_se_service_emit_reader_inserted(
                        SMARTCARD_SERVICE_SE_SERVICE(seService),
                        reader_id, reader_name);
+               /* LCOV_EXCL_STOP */
        }
 
        void ServerGDBus::emitReaderRemoved(unsigned int reader_id,
                const char *reader_name)
        {
+               /* LCOV_EXCL_START */
                smartcard_service_se_service_emit_reader_removed(
                        SMARTCARD_SERVICE_SE_SERVICE(seService),
                        reader_id, reader_name);
+               /* LCOV_EXCL_STOP */
+       }
+
+       void ServerGDBus::emitCardInserted(unsigned int reader_id,
+               const char *reader_name)
+       {
+               /* LCOV_EXCL_START */
+               smartcard_service_se_service_emit_card_inserted(
+                       SMARTCARD_SERVICE_SE_SERVICE(seService),
+                       reader_id, reader_name);
+               /* LCOV_EXCL_STOP */
+       }
+
+       void ServerGDBus::emitCardRemoved(unsigned int reader_id,
+               const char *reader_name)
+       {
+               /* LCOV_EXCL_START */
+               smartcard_service_se_service_emit_card_removed(
+                       SMARTCARD_SERVICE_SE_SERVICE(seService),
+                       reader_id, reader_name);
+               /* LCOV_EXCL_STOP */
        }
 
        /* Reader *
@@ -605,8 +729,8 @@ namespace smartcard_service_api
         *
         */
        static gboolean __process_open_session(SmartcardServiceReader *object,
-               GDBusMethodInvocation *invocation, intptr_t service_id,
-               intptr_t reader_id, void *user_data)
+               GDBusMethodInvocation *invocation, guint service_id,
+               guint reader_id, void *user_data)
        {
                unsigned int handle = IntegerHandle::INVALID_HANDLE;
                int result;
@@ -629,6 +753,7 @@ namespace smartcard_service_api
                        if (handle != IntegerHandle::INVALID_HANDLE) {
                                result = SCARD_ERROR_OK;
                        } else {
+                               /* LCOV_EXCL_START */
                                _ERR("createSession failed [%d]", handle);
 
                                result = SCARD_ERROR_OUT_OF_MEMORY;
@@ -637,6 +762,7 @@ namespace smartcard_service_api
                        _ERR("request invalid reader handle [%d]", reader_id);
 
                        result = SCARD_ERROR_ILLEGAL_PARAM;
+                       /* LCOV_EXCL_STOP */
                }
 
                /* response to client */
@@ -650,20 +776,22 @@ namespace smartcard_service_api
        {
                SmartcardServiceReader *object;
                GDBusMethodInvocation *invocation;
-               intptr_t service_id;
-               intptr_t reader_id;
+               guint service_id;
+               guint reader_id;
                void *user_data;
 
                if (params.size() != 5) {
+                       /* LCOV_EXCL_START */
                        _ERR("invalid parameter");
 
                        return;
+                       /* LCOV_EXCL_STOP */
                }
 
                object = (SmartcardServiceReader *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               service_id = (intptr_t)params[2];
-               reader_id = (intptr_t)params[3];
+               service_id = (gulong)params[2];
+               reader_id = (gulong)params[3];
                user_data = params[4];
 
                __process_open_session(object, invocation, service_id,
@@ -675,13 +803,13 @@ namespace smartcard_service_api
 
        static gboolean _handle_open_session(SmartcardServiceReader *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id,
-               intptr_t reader_id, void *user_data)
+               guint service_id,
+               guint reader_id, void *user_data)
        {
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -694,6 +822,7 @@ namespace smartcard_service_api
 
                        GDBusDispatcher::push(_process_open_session, params);
                } else {
+                       /* LCOV_EXCL_START */
                        _ERR("access denied");
 
                        /* response to client */
@@ -701,6 +830,7 @@ namespace smartcard_service_api
                                invocation,
                                SCARD_ERROR_SECURITY_NOT_ALLOWED,
                                IntegerHandle::INVALID_HANDLE);
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
@@ -723,6 +853,7 @@ namespace smartcard_service_api
                        "/org/tizen/SmartcardService/Reader",
                        &error) == false)
                {
+                       /* LCOV_EXCL_START */
                        _ERR("Can not skeleton_export %s", error->message);
 
                        g_error_free(error);
@@ -730,6 +861,7 @@ namespace smartcard_service_api
                        reader = NULL;
 
                        return false;
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
@@ -737,10 +869,12 @@ namespace smartcard_service_api
 
        void ServerGDBus::deinitReader()
        {
+       /* LCOV_EXCL_START */
                if (reader != NULL) {
                        g_object_unref(reader);
                        reader = NULL;
                }
+       /* LCOV_EXCL_STOP */
        }
 
        /* Session *
@@ -748,8 +882,8 @@ namespace smartcard_service_api
         *
         */
        static gboolean __process_close_session(SmartcardServiceSession *object,
-               GDBusMethodInvocation *invocation, intptr_t service_id,
-               intptr_t session_id, void *user_data)
+               GDBusMethodInvocation *invocation, guint service_id,
+               guint session_id, void *user_data)
        {
                const char *name;
 
@@ -764,7 +898,7 @@ namespace smartcard_service_api
                        resource.removeSession(name, service_id,
                                session_id);
                } else {
-                       _ERR("invalid parameters");
+                       _ERR("invalid parameters"); //LCOV_EXCL_LINE
                }
 
                /* response to client */
@@ -778,20 +912,22 @@ namespace smartcard_service_api
        {
                SmartcardServiceSession *object;
                GDBusMethodInvocation *invocation;
-               intptr_t service_id;
-               intptr_t session_id;
+               guint service_id;
+               guint session_id;
                void *user_data;
 
                if (params.size() != 5) {
+                       /* LCOV_EXCL_START */
                        _ERR("invalid parameter");
 
                        return;
+                       /* LCOV_EXCL_STOP */
                }
 
                object = (SmartcardServiceSession *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               service_id = (intptr_t)params[2];
-               session_id = (intptr_t)params[3];
+               service_id = (gulong)params[2];
+               session_id = (gulong)params[3];
                user_data = params[4];
 
                __process_close_session(object, invocation, service_id,
@@ -803,13 +939,13 @@ namespace smartcard_service_api
 
        static gboolean _handle_close_session(SmartcardServiceSession *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id,
-               intptr_t session_id, void *user_data)
+               guint service_id,
+               guint session_id, void *user_data)
        {
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -822,19 +958,21 @@ namespace smartcard_service_api
 
                        GDBusDispatcher::push(_process_close_session, params);
                } else {
+                       /* LCOV_EXCL_START */
                        _ERR("access denied");
 
                        /* response to client */
                        smartcard_service_session_complete_close_session(object,
                                invocation, SCARD_ERROR_SECURITY_NOT_ALLOWED);
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
        }
 
        static gboolean __process_get_atr(SmartcardServiceSession *object,
-               GDBusMethodInvocation *invocation, intptr_t service_id,
-               intptr_t session_id, void *user_data)
+               GDBusMethodInvocation *invocation, guint service_id,
+               guint session_id, void *user_data)
        {
                int result;
                ByteArray resp;
@@ -849,19 +987,16 @@ namespace smartcard_service_api
                name = g_dbus_method_invocation_get_sender(invocation);
 
                client = resource.getService(name, service_id);
+               /* LCOV_EXCL_START */
                if (client != NULL) {
                        Terminal *terminal;
 
                        terminal = client->getTerminal(session_id);
                        if (terminal != NULL) {
-                               int rv;
-
                                if (terminal->open() == true) {
-                                       rv = terminal->getATRSync(resp);
-                                       if (rv < SCARD_ERROR_OK) {
+                                       result = terminal->getATRSync(resp);
+                                       if (result < SCARD_ERROR_OK) {
                                                _ERR("getATRSync failed : name [%s], service_id [%d], session_id [%d]", name, service_id, session_id);
-
-                                               result = rv;
                                        }
 
                                        terminal->close();
@@ -880,6 +1015,7 @@ namespace smartcard_service_api
 
                        result = SCARD_ERROR_UNAVAILABLE;
                }
+               /* LCOV_EXCL_STOP */
 
                atr = GDBusHelper::convertByteArrayToVariant(resp);
 
@@ -894,20 +1030,22 @@ namespace smartcard_service_api
        {
                SmartcardServiceSession *object;
                GDBusMethodInvocation *invocation;
-               intptr_t service_id;
-               intptr_t session_id;
+               guint service_id;
+               guint session_id;
                void *user_data;
 
                if (params.size() != 5) {
+       /* LCOV_EXCL_START */
                        _ERR("invalid parameter");
 
                        return;
+       /* LCOV_EXCL_STOP */
                }
 
                object = (SmartcardServiceSession *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               service_id = (intptr_t)params[2];
-               session_id = (intptr_t)params[3];
+               service_id = (gulong)params[2];
+               session_id = (gulong)params[3];
                user_data = params[4];
 
                __process_get_atr(object, invocation, service_id,
@@ -919,13 +1057,13 @@ namespace smartcard_service_api
 
        static gboolean _handle_get_atr(SmartcardServiceSession *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id,
-               intptr_t session_id, void *user_data)
+               guint service_id,
+               guint session_id, void *user_data)
        {
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -938,6 +1076,7 @@ namespace smartcard_service_api
 
                        GDBusDispatcher::push(_process_get_atr, params);
                } else {
+                       /* LCOV_EXCL_START */
                        ByteArray resp;
 
                        _ERR("access denied");
@@ -948,15 +1087,17 @@ namespace smartcard_service_api
                                invocation,
                                SCARD_ERROR_SECURITY_NOT_ALLOWED,
                                GDBusHelper::convertByteArrayToVariant(resp));
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
        }
 
        static gboolean __process_open_channel(SmartcardServiceSession *object,
-               GDBusMethodInvocation *invocation, intptr_t service_id,
-               intptr_t session_id, intptr_t type, GVariant *aid, void *user_data)
+               GDBusMethodInvocation *invocation, guint service_id,
+               guint session_id, guint type, GVariant *aid, guint8 P2, void *user_data)
        {
+               int channelNum = -1;
                int result = SCARD_ERROR_UNKNOWN;
                ByteArray resp;
                GVariant *response = NULL;
@@ -964,6 +1105,7 @@ namespace smartcard_service_api
                const char *name;
 
                _INFO("[MSG_REQUEST_OPEN_CHANNEL]");
+               _INFO("P2 is %x", P2);
 
                ServerResource &resource = ServerResource::getInstance();
 
@@ -978,6 +1120,7 @@ namespace smartcard_service_api
 
                        channelID = resource.createChannel(name,
                                service_id, session_id, type, tempAid);
+                       /* LCOV_EXCL_START */
                        if (channelID != IntegerHandle::INVALID_HANDLE) {
                                ServerChannel *temp;
 
@@ -985,8 +1128,11 @@ namespace smartcard_service_api
                                        name, service_id, channelID);
                                if (temp != NULL) {
                                        resp = temp->getSelectResponse();
-
+                                       channelNum = temp->getChannelNumber();
                                        result = SCARD_ERROR_OK;
+                               } else {
+                                       _ERR("getChannel is failed");
+                                       result = SCARD_ERROR_UNAVAILABLE;
                                }
                        } else {
                                _ERR("channel is null.");
@@ -994,6 +1140,7 @@ namespace smartcard_service_api
                                /* set error value */
                                result = SCARD_ERROR_UNAVAILABLE;
                        }
+                       /* LCOV_EXCL_STOP */
                }
                catch (ExceptionBase &e)
                {
@@ -1004,7 +1151,7 @@ namespace smartcard_service_api
 
                /* response to client */
                smartcard_service_session_complete_open_channel(object,
-                       invocation, result, channelID, response);
+                       invocation, result, channelID, channelNum, response);
 
                return true;
        }
@@ -1013,43 +1160,47 @@ namespace smartcard_service_api
        {
                SmartcardServiceSession *object;
                GDBusMethodInvocation *invocation;
-               intptr_t service_id;
-               intptr_t session_id;
-               intptr_t type;
+               guint service_id;
+               guint session_id;
+               guint type;
+               guint8 P2;
                GVariant *aid;
                void *user_data;
 
-               if (params.size() != 7) {
+               if (params.size() != 8) {
+                       /* LCOV_EXCL_START */
                        _ERR("invalid parameter");
 
                        return;
+                       /* LCOV_EXCL_STOP */
                }
 
                object = (SmartcardServiceSession *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               service_id = (intptr_t)params[2];
-               session_id = (intptr_t)params[3];
-               type = (intptr_t)params[4];
+               service_id = (gulong)params[2];
+               session_id = (gulong)params[3];
+               type = (gulong)params[4];
                aid = (GVariant *)params[5];
-               user_data = params[6];
+               P2 = (guint8)((gulong)params[6]);
+               user_data = params[7];
 
                __process_open_channel(object, invocation, service_id,
-                       session_id, type, aid, user_data);
+                       session_id, type, aid, P2, user_data);
 
                g_object_unref(object);
                g_object_unref(invocation);
-               g_object_unref(aid);
+               g_variant_unref(aid);
        }
 
        static gboolean _handle_open_channel(SmartcardServiceSession *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id,
-               intptr_t session_id, intptr_t type, GVariant *aid, void *user_data)
+               guint service_id,
+               guint session_id, guint type, GVariant *aid, guint8 P2, void *user_data)
        {
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "rw") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -1060,12 +1211,14 @@ namespace smartcard_service_api
                        params.push_back((void *)session_id);
                        params.push_back((void *)type);
 
-                       g_object_ref(aid);
+                       g_variant_ref(aid);
                        params.push_back((void *)aid);
+                       params.push_back((void *)((int)P2));
                        params.push_back(user_data);
 
                        GDBusDispatcher::push(_process_open_channel, params);
                } else {
+                       /* LCOV_EXCL_START */
                        ByteArray resp;
 
                        _ERR("access denied");
@@ -1075,7 +1228,9 @@ namespace smartcard_service_api
                                invocation,
                                SCARD_ERROR_SECURITY_NOT_ALLOWED,
                                IntegerHandle::INVALID_HANDLE,
+                               -1,
                                GDBusHelper::convertByteArrayToVariant(resp));
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
@@ -1108,6 +1263,7 @@ namespace smartcard_service_api
                        "/org/tizen/SmartcardService/Session",
                        &error) == false)
                {
+                       /* LCOV_EXCL_START */
                        _ERR("Can not skeleton_export %s", error->message);
 
                        g_error_free(error);
@@ -1115,6 +1271,7 @@ namespace smartcard_service_api
                        session = NULL;
 
                        return false;
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
@@ -1133,8 +1290,8 @@ namespace smartcard_service_api
         *
         */
        static gboolean __process_close_channel(SmartcardServiceChannel *object,
-               GDBusMethodInvocation *invocation, intptr_t service_id,
-               intptr_t channel_id, void *user_data)
+               GDBusMethodInvocation *invocation, guint service_id,
+               guint channel_id, void *user_data)
        {
                int result;
                const char *name;
@@ -1160,20 +1317,22 @@ namespace smartcard_service_api
        {
                SmartcardServiceChannel *object;
                GDBusMethodInvocation *invocation;
-               intptr_t service_id;
-               intptr_t channel_id;
+               guint service_id;
+               guint channel_id;
                void *user_data;
 
                if (params.size() != 5) {
+                       /* LCOV_EXCL_START */
                        _ERR("invalid parameter");
 
                        return;
+                       /* LCOV_EXCL_STOP */
                }
 
                object = (SmartcardServiceChannel *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               service_id = (intptr_t)params[2];
-               channel_id = (intptr_t)params[3];
+               service_id = (gulong)params[2];
+               channel_id = (gulong)params[3];
                user_data = params[4];
 
                __process_close_channel(object, invocation, service_id,
@@ -1185,12 +1344,12 @@ namespace smartcard_service_api
 
        static gboolean _handle_close_channel(SmartcardServiceChannel *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id, intptr_t channel_id, void *user_data)
+               guint service_id, guint channel_id, void *user_data)
        {
                vector<void *> params;
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        g_object_ref(object);
                        params.push_back((void *)object);
 
@@ -1203,6 +1362,7 @@ namespace smartcard_service_api
 
                        GDBusDispatcher::push(_process_close_channel, params);
                } else {
+                       /* LCOV_EXCL_START */
                        _ERR("access denied");
 
                        /* response to client */
@@ -1210,6 +1370,7 @@ namespace smartcard_service_api
                                object,
                                invocation,
                                SCARD_ERROR_SECURITY_NOT_ALLOWED);
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
@@ -1217,8 +1378,8 @@ namespace smartcard_service_api
 
        static gboolean __process_transmit(SmartcardServiceChannel *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id,
-               intptr_t channel_id,
+               guint service_id,
+               guint channel_id,
                GVariant *command,
                void *user_data)
        {
@@ -1235,6 +1396,7 @@ namespace smartcard_service_api
                name = g_dbus_method_invocation_get_sender(invocation);
 
                channel = resource.getChannel(name, service_id, channel_id);
+               /* LCOV_EXCL_START */
                if (channel != NULL) {
                        int rv;
                        ByteArray cmd;
@@ -1254,6 +1416,7 @@ namespace smartcard_service_api
 
                        result = SCARD_ERROR_UNAVAILABLE;
                }
+               /* LCOV_EXCL_STOP */
 
                response = GDBusHelper::convertByteArrayToVariant(resp);
 
@@ -1268,21 +1431,23 @@ namespace smartcard_service_api
        {
                SmartcardServiceChannel *object;
                GDBusMethodInvocation *invocation;
-               intptr_t service_id;
-               intptr_t channel_id;
+               guint service_id;
+               guint channel_id;
                GVariant *command;
                void *user_data;
 
                if (params.size() != 6) {
+                       /* LCOV_EXCL_START */
                        _ERR("invalid parameter");
 
                        return;
+                       /* LCOV_EXCL_STOP */
                }
 
                object = (SmartcardServiceChannel *)params[0];
                invocation = (GDBusMethodInvocation *)params[1];
-               service_id = (intptr_t)params[2];
-               channel_id = (intptr_t)params[3];
+               service_id = (gulong)params[2];
+               channel_id = (gulong)params[3];
                command = (GVariant *)params[4];
                user_data = params[5];
 
@@ -1294,17 +1459,41 @@ namespace smartcard_service_api
                g_object_unref(command);
        }
 
+       static GVariant *_copy_variant(GVariant *src)
+       {
+               GVariantBuilder builder;
+
+               GVariantIter *iter;
+               guint8 element;
+               guint i;
+
+               g_variant_get(src, "a(y)", &iter);
+
+               g_variant_builder_init(&builder, G_VARIANT_TYPE("a(y)"));
+
+               for (i = 0; g_variant_iter_loop(iter, "(y)", &element); i++) {
+                       g_variant_builder_add(&builder, "(y)", element);
+               }
+
+               g_variant_iter_free(iter);
+
+               return g_variant_builder_end(&builder);
+       }
+
        static gboolean _handle_transmit(SmartcardServiceChannel *object,
                GDBusMethodInvocation *invocation,
-               intptr_t service_id,
-               intptr_t channel_id,
+               guint service_id,
+               guint channel_id,
                GVariant *command,
                void *user_data)
        {
                vector<void *> params;
+               GVariant *_command;
+
+               _command = _copy_variant(command);
 
                /* apply user space smack */
-               if (_is_authorized_request(invocation, "r") == true) {
+               if (_is_authorized_request(invocation) == true) {
                        /* enqueue message */
                        g_object_ref(object);
                        params.push_back((void *)object);
@@ -1315,13 +1504,14 @@ namespace smartcard_service_api
                        params.push_back((void *)service_id);
                        params.push_back((void *)channel_id);
 
-                       g_object_ref(command);
-                       params.push_back((void *)command);
+                       g_object_ref(_command);
+                       params.push_back((void *)_command);
 
                        params.push_back(user_data);
 
                        GDBusDispatcher::push(_process_transmit, params);
                } else {
+                       /* LCOV_EXCL_START */
                        ByteArray resp;
 
                        _ERR("access denied");
@@ -1331,6 +1521,7 @@ namespace smartcard_service_api
                                invocation,
                                SCARD_ERROR_SECURITY_NOT_ALLOWED,
                                GDBusHelper::convertByteArrayToVariant(resp));
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;
@@ -1358,6 +1549,7 @@ namespace smartcard_service_api
                        "/org/tizen/SmartcardService/Channel",
                        &error) == false)
                {
+                       /* LCOV_EXCL_START */
                        _ERR("Can not skeleton_export %s", error->message);
 
                        g_error_free(error);
@@ -1365,6 +1557,7 @@ namespace smartcard_service_api
                        channel = NULL;
 
                        return false;
+                       /* LCOV_EXCL_STOP */
                }
 
                return true;