Fix problems
[platform/core/connectivity/smartcard-service.git] / client / Session.cpp
index c8ab8bf..18b2275 100644 (file)
@@ -1,19 +1,18 @@
 /*
-* Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
-*
-* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
-*
-* 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.
-*/
-
+ * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 "Session.h"
 #include "Reader.h"
 #include "ClientChannel.h"
+#ifdef USE_GDBUS
+#include "ClientGDBus.h"
+#else
 #include "ClientIPC.h"
+#endif
 
 #ifndef EXTERN_API
 #define EXTERN_API __attribute__((visibility("default")))
 
 namespace smartcard_service_api
 {
-       Session::Session(void *context, Reader *reader, void *handle):SessionHelper(reader)
+       Session::Session(void *context, Reader *reader, void *handle) :
+               SessionHelper(reader)
        {
                this->context = NULL;
 
                if (context == NULL || handle == NULL)
                {
-                       SCARD_DEBUG_ERR("handle is null");
+                       _ERR("handle is null");
 
                        return;
                }
 
                this->context = context;
                this->handle = handle;
+
+#ifdef USE_GDBUS
+               /* initialize client */
+               if (!g_thread_supported())
+               {
+                       g_thread_init(NULL);
+               }
+
+               g_type_init();
+
+               /* init default context */
+               GError *error = NULL;
+
+               proxy = smartcard_service_session_proxy_new_for_bus_sync(
+                       G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
+                       "org.tizen.SmartcardService",
+                       "/org/tizen/SmartcardService/Session",
+                       NULL, &error);
+               if (proxy == NULL)
+               {
+                       _ERR("Can not create proxy : %s", error->message);
+                       g_error_free(error);
+                       return;
+               }
+#endif
                closed = false;
        }
 
        Session::~Session()
        {
-               close(NULL, this);
+               size_t i;
+
+               closeSync();
+
+               for (i = 0; i < channels.size(); i++)
+               {
+                       delete (ClientChannel *)channels[i];
+               }
+
+               channels.clear();
        }
 
-       void Session::closeChannels()
+       void Session::closeChannels() throw (ErrorIO &, ErrorIllegalState &)
        {
                size_t i;
 
                for (i = 0; i < channels.size(); i++)
                {
                        channels[i]->closeSync();
-                       delete (ClientChannel *)channels[i];
+               }
+       }
+#ifdef USE_GDBUS
+       void Session::session_get_atr_cb(GObject *source_object,
+               GAsyncResult *res, gpointer user_data)
+       {
+               CallbackParam *param = (CallbackParam *)user_data;
+               Session *session;
+               getATRCallback callback;
+               gint result;
+               GVariant *var_atr;
+               GError *error = NULL;
+               ByteArray atr;
+
+               _INFO("MSG_REQUEST_GET_ATR");
+
+               if (param == NULL) {
+                       _ERR("null parameter!!!");
+                       return;
                }
 
-               channels.clear();
+               session = (Session *)param->instance;
+               callback = (getATRCallback)param->callback;
+
+               if (smartcard_service_session_call_get_atr_finish(
+                       SMARTCARD_SERVICE_SESSION(source_object),
+                       &result, &var_atr, res, &error) == true) {
+                       if (result == SCARD_ERROR_OK) {
+                               GDBusHelper::convertVariantToByteArray(var_atr, atr);
+
+                               session->atr = atr;
+                       } else {
+                               _ERR("smartcard_service_session_call_get_atr failed, [%d]", result);
+                       }
+               } else {
+                       _ERR("smartcard_service_session_call_get_atr failed, [%s]", error->message);
+                       g_error_free(error);
+
+                       result = SCARD_ERROR_IPC_FAILED;
+               }
+
+               if (callback != NULL) {
+                       callback(atr.getBuffer(),
+                               atr.size(), result, param->user_param);
+               }
+
+               delete param;
        }
 
-       ByteArray Session::getATRSync()
+       void Session::session_open_channel_cb(GObject *source_object,
+               GAsyncResult *res, gpointer user_data)
        {
-               Message msg;
-               int rv;
+               CallbackParam *param = (CallbackParam *)user_data;
+               Session *session;
+               openChannelCallback callback;
+               gint result;
+               guint channel_id;
+               GVariant *var_response;
+               GError *error = NULL;
+               Channel *channel;
+
+               _INFO("MSG_REQUEST_OPEN_CHANNEL");
+
+               if (param == NULL) {
+                       _ERR("null parameter!!!");
+                       return;
+               }
 
-#ifdef CLIENT_IPC_THREAD
-               /* request channel handle from server */
-               msg.message = Message::MSG_REQUEST_GET_ATR;
-               msg.param1 = (unsigned int)handle;
-               msg.error = (unsigned int)context; /* using error to context */
-               msg.caller = (void *)this;
-               msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
+               session = (Session *)param->instance;
+               callback = (openChannelCallback)param->callback;
+
+               if (smartcard_service_session_call_open_channel_finish(
+                       SMARTCARD_SERVICE_SESSION(source_object),
+                       &result, &channel_id, &var_response,
+                       res, &error) == true) {
+                       if (result == SCARD_ERROR_OK) {
+                               ByteArray response;
+
+                               GDBusHelper::convertVariantToByteArray(
+                                       var_response, response);
+
+                               /* create new instance of channel */
+                               channel = new ClientChannel(session->context,
+                                       session, channel_id,
+                                       response, (void *)channel_id);
+                               if (channel != NULL) {
+                                       session->channels.push_back(channel);
+                               } else {
+                                       _ERR("alloc failed");
+
+                                       result = SCARD_ERROR_OUT_OF_MEMORY;
+                               }
+                       } else {
+                               _ERR("smartcard_service_session_call_open_channel failed, [%d]", result);
+                       }
+               } else {
+                       _ERR("smartcard_service_session_call_open_channel failed, [%s]", error->message);
+                       g_error_free(error);
 
-               ClientIPC::getInstance().sendMessage(&msg);
+                       result = SCARD_ERROR_IPC_FAILED;
+               }
 
-               syncLock();
-               rv = waitTimedCondition(0);
-               syncUnlock();
+               if (callback != NULL) {
+                       callback(channel, result, param->user_param);
+               }
 
-               if (rv != 0)
-               {
-                       SCARD_DEBUG_ERR("time over");
+               delete param;
+       }
+
+       void Session::session_close_cb(GObject *source_object,
+               GAsyncResult *res, gpointer user_data)
+       {
+               CallbackParam *param = (CallbackParam *)user_data;
+               Session *session;
+               closeSessionCallback callback;
+               gint result;
+               GError *error = NULL;
+
+               _INFO("MSG_REQUEST_CLOSE_SESSION");
+
+               if (param == NULL) {
+                       _ERR("null parameter!!!");
+                       return;
+               }
+
+               session = (Session *)param->instance;
+               callback = (closeSessionCallback)param->callback;
+
+               if (smartcard_service_session_call_close_session_finish(
+                       SMARTCARD_SERVICE_SESSION(source_object),
+                       &result, res, &error) == true) {
+                       if (result == SCARD_ERROR_OK) {
+                               session->closed = true;
+                       } else {
+                               _ERR("smartcard_service_session_call_close_session failed, [%d]", result);
+                       }
+               } else {
+                       _ERR("smartcard_service_session_call_close_session failed, [%s]", error->message);
+                       g_error_free(error);
+
+                       result = SCARD_ERROR_IPC_FAILED;
+               }
 
-                       atr.releaseBuffer();
+               if (callback != NULL) {
+                       callback(result, param->user_param);
                }
+
+               delete param;
+       }
+#endif
+       const ByteArray Session::getATRSync()
+               throw (ExceptionBase &, ErrorIO &, ErrorSecurity &,
+                       ErrorIllegalState &, ErrorIllegalParameter &)
+       {
+               ByteArray result;
+
+               if (getReader()->isSecureElementPresent() == true)
+               {
+                       if (atr.isEmpty() == true)
+                       {
+#ifdef USE_GDBUS
+                               gint ret;
+                               GVariant *var_atr = NULL;
+                               GError *error = NULL;
+
+                               if (smartcard_service_session_call_get_atr_sync(
+                                       (SmartcardServiceSession *)proxy,
+                                       ClientGDBus::getCookie(),
+                                       GPOINTER_TO_UINT(context),
+                                       GPOINTER_TO_UINT(handle),
+                                       &ret, &var_atr, NULL, &error) == true) {
+                                       if (ret == SCARD_ERROR_OK) {
+                                               GDBusHelper::convertVariantToByteArray(var_atr, result);
+
+                                               atr = result;
+                                       } else {
+                                               _ERR("smartcard_service_session_call_get_atr_sync failed, [%d]", ret);
+
+                                               THROW_ERROR(ret);
+                                       }
+                               } else {
+                                       _ERR("smartcard_service_session_call_get_atr_sync failed, [%s]", error->message);
+                                       g_error_free(error);
+
+                                       THROW_ERROR(SCARD_ERROR_IPC_FAILED);
+                               }
+#else
+                               Message msg;
+                               int rv;
+#ifdef CLIENT_IPC_THREAD
+                               /* request channel handle from server */
+                               msg.message = Message::MSG_REQUEST_GET_ATR;
+                               msg.param1 = (unsigned long)handle;
+                               msg.error = (unsigned long)context; /* using error to context */
+                               msg.caller = (void *)this;
+                               msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
+
+                               syncLock();
+                               if (ClientIPC::getInstance().sendMessage(msg) == true)
+                               {
+                                       rv = waitTimedCondition(0);
+                                       if (rv != 0)
+                                       {
+                                               _ERR("time over");
+                                               this->error = SCARD_ERROR_OPERATION_TIMEOUT;
+                                       }
+                               }
+                               else
+                               {
+                                       _ERR("sendMessage failed");
+                                       this->error = SCARD_ERROR_IPC_FAILED;
+                               }
+                               syncUnlock();
+
+                               if (this->error != SCARD_ERROR_OK)
+                               {
+                                       ThrowError::throwError(this->error);
+                               }
+#endif
 #endif
+                       }
 
-               return atr;
+                       result = atr;
+               }
+               else
+               {
+                       _ERR("unavailable session");
+                       throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
+               }
+
+               return result;
        }
 
        int Session::getATR(getATRCallback callback, void *userData)
        {
-               Message msg;
+               int result;
 
-               /* request channel handle from server */
-               msg.message = Message::MSG_REQUEST_GET_ATR;
-               msg.param1 = (unsigned int)handle;
-               msg.error = (unsigned int)context; /* using error to context */
-               msg.caller = (void *)this;
-               msg.callback = (void *)callback;
-               msg.userParam = userData;
+               if (getReader()->isSecureElementPresent() == true)
+               {
+                       if (atr.isEmpty() == true)
+                       {
+#ifdef USE_GDBUS
+                               CallbackParam *param = new CallbackParam();
+
+                               param->instance = this;
+                               param->callback = (void *)callback;
+                               param->user_param = userData;
+
+                               smartcard_service_session_call_get_atr(
+                                       (SmartcardServiceSession *)proxy,
+                                       ClientGDBus::getCookie(),
+                                       GPOINTER_TO_UINT(context),
+                                       GPOINTER_TO_UINT(handle), NULL,
+                                       &Session::session_get_atr_cb, param);
+
+                               result = SCARD_ERROR_OK;
+#else
+                               Message msg;
+
+                               /* request channel handle from server */
+                               msg.message = Message::MSG_REQUEST_GET_ATR;
+                               msg.param1 = (unsigned long)handle;
+                               msg.error = (unsigned long)context; /* using error to context */
+                               msg.caller = (void *)this;
+                               msg.callback = (void *)callback;
+                               msg.userParam = userData;
+
+                               if (ClientIPC::getInstance().sendMessage(msg) == true)
+                               {
+                                       result = SCARD_ERROR_OK;
+                               }
+                               else
+                               {
+                                       _ERR("sendMessage failed");
+                                       result = SCARD_ERROR_IPC_FAILED;
+                               }
+#endif
+                       }
+                       else
+                       {
+                               result = SCARD_ERROR_OK;
 
-               ClientIPC::getInstance().sendMessage(&msg);
+                               /* TODO : invoke callback directly */
+                               callback(atr.getBuffer(),
+                                       atr.size(), 0, userData);
+                       }
+               }
+               else
+               {
+                       _ERR("unavailable session");
+                       result = SCARD_ERROR_ILLEGAL_STATE;
+               }
 
-               return 0;
+               return result;
        }
 
        void Session::closeSync()
+               throw (ExceptionBase &, ErrorIO &, ErrorSecurity &,
+                       ErrorIllegalState &, ErrorIllegalParameter &)
        {
-               Message msg;
-               int rv;
-
-#ifdef CLIENT_IPC_THREAD
                if (isClosed() == false)
                {
                        closed = true;
                        closeChannels();
+#ifdef USE_GDBUS
+                       gint ret;
+                       GError *error = NULL;
+
+                       if (smartcard_service_session_call_close_session_sync(
+                               (SmartcardServiceSession *)proxy,
+                               ClientGDBus::getCookie(),
+                               GPOINTER_TO_UINT(context),
+                               GPOINTER_TO_UINT(handle),
+                               &ret, NULL, &error) == true) {
+                               if (ret == SCARD_ERROR_OK) {
+                                       closed = true;
+                               } else {
+                                       _ERR("smartcard_service_session_call_close_session_sync failed, [%d]", ret);
+
+                                       THROW_ERROR(ret);
+                               }
+                       } else {
+                               _ERR("smartcard_service_session_call_get_atr_sync failed, [%s]", error->message);
+                               g_error_free(error);
+
+                               THROW_ERROR(SCARD_ERROR_IPC_FAILED);
+                       }
+#else
+                       int rv;
+                       Message msg;
 
+#ifdef CLIENT_IPC_THREAD
                        /* request channel handle from server */
                        msg.message = Message::MSG_REQUEST_CLOSE_SESSION;
-                       msg.param1 = (unsigned int)handle;
-                       msg.error = (unsigned int)context; /* using error to context */
+                       msg.param1 = (unsigned long)handle;
+                       msg.error = (unsigned long)context; /* using error to context */
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-                       ClientIPC::getInstance().sendMessage(&msg);
-
                        syncLock();
-                       rv = waitTimedCondition(0);
+                       if (ClientIPC::getInstance().sendMessage(msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
+
+                               if (rv != 0)
+                               {
+                                       _ERR("time over");
+                                       this->error = SCARD_ERROR_OPERATION_TIMEOUT;
+                               }
+                       }
+                       else
+                       {
+                               _ERR("sendMessage failed");
+                               this->error = SCARD_ERROR_IPC_FAILED;
+                       }
                        syncUnlock();
 
-                       if (rv != 0)
+                       if (this->error != SCARD_ERROR_OK)
                        {
-                               SCARD_DEBUG_ERR("time over");
+                               ThrowError::throwError(this->error);
                        }
-               }
 #endif
+#endif
+               }
        }
 
        int Session::close(closeSessionCallback callback, void *userData)
        {
-               Message msg;
+               int result = SCARD_ERROR_OK;
 
                if (isClosed() == false)
                {
                        closed = true;
                        closeChannels();
-
+#ifdef USE_GDBUS
+                       CallbackParam *param = new CallbackParam();
+
+                       param->instance = this;
+                       param->callback = (void *)callback;
+                       param->user_param = userData;
+
+                       smartcard_service_session_call_close_session(
+                               (SmartcardServiceSession *)proxy,
+                               ClientGDBus::getCookie(),
+                               GPOINTER_TO_UINT(context),
+                               GPOINTER_TO_UINT(handle), NULL,
+                               &Session::session_close_cb, param);
+#else
+                       Message msg;
                        /* request channel handle from server */
                        msg.message = Message::MSG_REQUEST_CLOSE_SESSION;
-                       msg.param1 = (unsigned int)handle;
-                       msg.error = (unsigned int)context; /* using error to context */
+                       msg.param1 = (unsigned long)handle;
+                       msg.error = (unsigned long)context; /* using error to context */
                        msg.caller = (void *)this;
                        msg.callback = (void *)callback;
                        msg.userParam = userData;
 
-                       ClientIPC::getInstance().sendMessage(&msg);
+                       if (ClientIPC::getInstance().sendMessage(msg) == false)
+                       {
+                               _ERR("sendMessage failed");
+                               result = SCARD_ERROR_IPC_FAILED;
+                       }
+#endif
                }
 
-               return 0;
+               return result;
        }
 
        unsigned int Session::getChannelCountSync()
        {
-               Message msg;
-               int rv;
+               unsigned int count = 0;
+
+               if (getReader()->isSecureElementPresent() == true)
+               {
+#ifdef USE_GDBUS
+                       count = channels.size();
+#else
+                       Message msg;
+                       int rv;
 
+                       channelCount = -1;
 #ifdef CLIENT_IPC_THREAD
-               /* request channel handle from server */
-               msg.message = Message::MSG_REQUEST_GET_CHANNEL_COUNT;
-               msg.param1 = (unsigned int)handle;
-               msg.error = (unsigned int)context; /* using error to context */
-               msg.caller = (void *)this;
-               msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
+                       /* request channel handle from server */
+                       msg.message = Message::MSG_REQUEST_GET_CHANNEL_COUNT;
+                       msg.param1 = (unsigned long)handle;
+                       msg.error = (unsigned long)context; /* using error to context */
+                       msg.caller = (void *)this;
+                       msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
 
-               ClientIPC::getInstance().sendMessage(&msg);
+                       syncLock();
+                       if (ClientIPC::getInstance().sendMessage(msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
+                               if (rv != 0)
+                               {
+                                       _ERR("time over");
+                                       this->error = SCARD_ERROR_OPERATION_TIMEOUT;
+                               }
 
-               syncLock();
-               rv = waitTimedCondition(0);
-               syncUnlock();
+                               count = channelCount;
+                       }
+                       else
+                       {
+                               _ERR("sendMessage failed");
+                               this->error = SCARD_ERROR_IPC_FAILED;
+                       }
+                       syncUnlock();
 
-               if (rv != 0)
+                       if (this->error != SCARD_ERROR_OK)
+                       {
+                               ThrowError::throwError(this->error);
+                       }
+#endif
+#endif
+               }
+               else
                {
-                       SCARD_DEBUG_ERR("time over");
-
-                       channelCount = -1;
+                       _ERR("unavailable session");
+                       throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
                }
-#endif
 
-               return channelCount;
+               return count;
        }
 
        int Session::getChannelCount(getChannelCountCallback callback, void *userData)
        {
-               Message msg;
+               int result;
 
-               msg.message = Message::MSG_REQUEST_GET_CHANNEL_COUNT;
-               msg.param1 = (unsigned int)handle;
-               msg.error = (unsigned int)context; /* using error to context */
-               msg.caller = (void *)this;
-               msg.callback = (void *)callback;
-               msg.userParam = userData;
+               if (getReader()->isSecureElementPresent() == true)
+               {
+#ifdef USE_GDBUS
+#else
+                       Message msg;
 
-               ClientIPC::getInstance().sendMessage(&msg);
+                       msg.message = Message::MSG_REQUEST_GET_CHANNEL_COUNT;
+                       msg.param1 = (unsigned long)handle;
+                       msg.error = (unsigned long)context; /* using error to context */
+                       msg.caller = (void *)this;
+                       msg.callback = (void *)callback;
+                       msg.userParam = userData;
 
-               return 0;
+                       if (ClientIPC::getInstance().sendMessage(msg) == true)
+                       {
+                               result = SCARD_ERROR_OK;
+                       }
+                       else
+                       {
+                               _ERR("sendMessage failed");
+                               result = SCARD_ERROR_IPC_FAILED;
+                       }
+#endif
+               }
+               else
+               {
+                       _ERR("unavailable session");
+                       result = SCARD_ERROR_ILLEGAL_STATE;
+               }
+
+               return result;
        }
 
-       Channel *Session::openChannelSync(int id, ByteArray aid)
+       Channel *Session::openChannelSync(int id, const ByteArray &aid)
+               throw (ExceptionBase &, ErrorIO &, ErrorIllegalState &,
+                       ErrorIllegalParameter &, ErrorSecurity &)
        {
-               Message msg;
-               int rv;
+               Channel *channel = NULL;
+
+               if (getReader()->isSecureElementPresent() == true)
+               {
+#ifdef USE_GDBUS
+                       gint ret;
+                       GVariant *var_aid = NULL, *var_response = NULL;
+                       guint channel_id;
+                       GError *error = NULL;
+
+                       var_aid = GDBusHelper::convertByteArrayToVariant(aid);
+
+                       if (smartcard_service_session_call_open_channel_sync(
+                               (SmartcardServiceSession *)proxy,
+                               ClientGDBus::getCookie(),
+                               GPOINTER_TO_UINT(context),
+                               GPOINTER_TO_UINT(handle),
+                               (guint)id, var_aid, &ret, &channel_id,
+                               &var_response, NULL, &error) == true) {
+                               if (ret == SCARD_ERROR_OK && channel_id != 0) {
+                                       ByteArray response;
+
+                                       GDBusHelper::convertVariantToByteArray(
+                                               var_response, response);
+
+                                       /* create new instance of channel */
+                                       channel = new ClientChannel(context,
+                                               this, channel_id,
+                                               response, (void *)channel_id);
+                                       if (channel != NULL)
+                                       {
+                                               channels.push_back(channel);
+                                       }
+                                       else
+                                       {
+                                               _ERR("alloc failed");
+
+                                               THROW_ERROR(SCARD_ERROR_OUT_OF_MEMORY);
+                                       }
+                               } else {
+                                       _ERR("smartcard_service_session_call_open_channel_sync failed, [%d]", ret);
+
+                                       THROW_ERROR(ret);
+                               }
+                       } else {
+                               _ERR("smartcard_service_session_call_open_channel_sync failed, [%s]", error->message);
+                               g_error_free(error);
+
+                               THROW_ERROR(SCARD_ERROR_IPC_FAILED);
+                       }
+#else
+                       Message msg;
+                       int rv;
 
 #ifdef CLIENT_IPC_THREAD
-               /* request channel handle from server */
-               msg.message = Message::MSG_REQUEST_OPEN_CHANNEL;
-               msg.param1 = id;
-               msg.param2 = (unsigned int)handle;
-               msg.data = aid;
-               msg.error = (unsigned int)context; /* using error to context */
-               msg.caller = (void *)this;
-               msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
-
-               ClientIPC::getInstance().sendMessage(&msg);
-
-               syncLock();
-               rv = waitTimedCondition(0);
-               syncUnlock();
-
-               if (rv != 0)
+                       /* request channel handle from server */
+                       msg.message = Message::MSG_REQUEST_OPEN_CHANNEL;
+                       msg.param1 = id;
+                       msg.param2 = (unsigned long)handle;
+                       msg.data = aid;
+                       msg.error = (unsigned long)context; /* using error to context */
+                       msg.caller = (void *)this;
+                       msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
+
+                       syncLock();
+                       if (ClientIPC::getInstance().sendMessage(msg) == true)
+                       {
+                               rv = waitTimedCondition(0);
+                               if (rv != 0)
+                               {
+                                       _ERR("time over");
+                                       this->error = SCARD_ERROR_OPERATION_TIMEOUT;
+                               }
+
+                               channel = openedChannel;
+                       }
+                       else
+                       {
+                               _ERR("sendMessage failed");
+                               this->error = SCARD_ERROR_IPC_FAILED;
+                       }
+                       syncUnlock();
+#endif
+                       if (this->error != SCARD_ERROR_OK)
+                       {
+                               ThrowError::throwError(this->error);
+                       }
+#endif
+               }
+               else
                {
-                       SCARD_DEBUG_ERR("time over");
+                       _ERR("unavailable session");
 
-                       openedChannel = NULL;
+                       throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
                }
-#endif
 
-               return (Channel *)openedChannel;
+               return (Channel *)channel;
        }
 
-       int Session::openChannel(int id, ByteArray aid, openChannelCallback callback, void *userData)
+       int Session::openChannel(int id, const ByteArray &aid, openChannelCallback callback, void *userData)
        {
-               Message msg;
+               int result;
+
+               if (getReader()->isSecureElementPresent() == true)
+               {
+#ifdef USE_GDBUS
+                       GVariant *var_aid;
+
+                       CallbackParam *param = new CallbackParam();
+
+                       param->instance = this;
+                       param->callback = (void *)callback;
+                       param->user_param = userData;
 
-               /* request channel handle from server */
-               msg.message = Message::MSG_REQUEST_OPEN_CHANNEL;
-               msg.param1 = id;
-               msg.param2 = (unsigned int)handle;
-               msg.data = aid;
-               msg.error = (unsigned int)context; /* using error to context */
-               msg.caller = (void *)this;
-               msg.callback = (void *)callback;
-               msg.userParam = userData;
+                       var_aid = GDBusHelper::convertByteArrayToVariant(aid);
 
-               ClientIPC::getInstance().sendMessage(&msg);
+                       smartcard_service_session_call_open_channel(
+                               (SmartcardServiceSession *)proxy,
+                               ClientGDBus::getCookie(),
+                               GPOINTER_TO_UINT(context),
+                               GPOINTER_TO_UINT(handle),
+                               (guint)id, var_aid, NULL,
+                               &Session::session_open_channel_cb, param);
+#else
+                       Message msg;
 
-               return 0;
+                       /* request channel handle from server */
+                       msg.message = Message::MSG_REQUEST_OPEN_CHANNEL;
+                       msg.param1 = id;
+                       msg.param2 = (unsigned long)handle;
+                       msg.data = aid;
+                       msg.error = (unsigned long)context; /* using error to context */
+                       msg.caller = (void *)this;
+                       msg.callback = (void *)callback;
+                       msg.userParam = userData;
+
+                       if (ClientIPC::getInstance().sendMessage(msg) == true)
+                       {
+                               result = SCARD_ERROR_OK;
+                       }
+                       else
+                       {
+                               _ERR("sendMessage failed");
+                               result = SCARD_ERROR_IPC_FAILED;
+                       }
+#endif
+               }
+               else
+               {
+                       _ERR("unavailable session");
+                       result = SCARD_ERROR_ILLEGAL_STATE;
+               }
+
+               return result;
        }
 
-       Channel *Session::openBasicChannelSync(ByteArray aid)
+       Channel *Session::openBasicChannelSync(const ByteArray &aid)
+               throw (ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
        {
                return openChannelSync(0, aid);
        }
 
-       Channel *Session::openBasicChannelSync(unsigned char *aid, unsigned int length)
+       Channel *Session::openBasicChannelSync(const unsigned char *aid, unsigned int length)
+               throw (ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
        {
-               return openBasicChannelSync(ByteArray(aid, length));
+               ByteArray temp(aid, length);
+
+               return openBasicChannelSync(temp);
        }
 
-       int Session::openBasicChannel(ByteArray aid, openChannelCallback callback, void *userData)
+       int Session::openBasicChannel(const ByteArray &aid, openChannelCallback callback, void *userData)
        {
                return openChannel(0, aid, callback, userData);
        }
 
-       int Session::openBasicChannel(unsigned char *aid, unsigned int length, openChannelCallback callback, void *userData)
+       int Session::openBasicChannel(const unsigned char *aid, unsigned int length,
+               openChannelCallback callback, void *userData)
        {
-               return openBasicChannel(ByteArray(aid, length), callback, userData);
+               ByteArray temp(aid, length);
+
+               return openBasicChannel(temp, callback, userData);
        }
 
-       Channel *Session::openLogicalChannelSync(ByteArray aid)
+       Channel *Session::openLogicalChannelSync(const ByteArray &aid)
+               throw (ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
        {
                return openChannelSync(1, aid);
        }
 
-       Channel *Session::openLogicalChannelSync(unsigned char *aid, unsigned int length)
+       Channel *Session::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
+               throw (ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
        {
-               return openLogicalChannelSync(ByteArray(aid, length));
+               ByteArray temp(aid, length);
+
+               return openLogicalChannelSync(temp);
        }
 
-       int Session::openLogicalChannel(ByteArray aid, openChannelCallback callback, void *userData)
+       int Session::openLogicalChannel(const ByteArray &aid, openChannelCallback callback, void *userData)
        {
                return openChannel(1, aid, callback, userData);
        }
 
-       int Session::openLogicalChannel(unsigned char *aid, unsigned int length, openChannelCallback callback, void *userData)
+       int Session::openLogicalChannel(const unsigned char *aid, unsigned int length,
+               openChannelCallback callback, void *userData)
        {
-               return openLogicalChannel(ByteArray(aid, length), callback, userData);
+               ByteArray temp(aid, length);
+
+               return openLogicalChannel(temp, callback, userData);
        }
 
+#ifndef USE_GDBUS
        bool Session::dispatcherCallback(void *message)
        {
                Message *msg = (Message *)message;
-               Session *session = NULL;
+               Session *session;
                bool result = false;
 
                if (msg == NULL)
                {
-                       SCARD_DEBUG_ERR("message is null");
+                       _ERR("message is null");
                        return result;
                }
 
@@ -329,25 +838,26 @@ namespace smartcard_service_api
                        {
                                Channel *channel = NULL;
 
-                               SCARD_DEBUG("MSG_REQUEST_OPEN_CHANNEL");
+                               _INFO("MSG_REQUEST_OPEN_CHANNEL");
 
                                if (msg->param1 != 0)
                                {
                                        /* create new instance of channel */
-                                       channel = new ClientChannel(session->context, session, msg->param2, msg->data, (void *)msg->param1);
+                                       channel = new ClientChannel(session->context,
+                                               session, msg->param2, msg->data, (void *)msg->param1);
                                        if (channel != NULL)
                                        {
                                                session->channels.push_back(channel);
                                        }
                                        else
                                        {
-                                               SCARD_DEBUG_ERR("alloc failed");
+                                               _ERR("alloc failed");
 
-                                               msg->error = -1;
+                                               msg->error = SCARD_ERROR_OUT_OF_MEMORY;
                                        }
                                }
 
-                               if (msg->callback == (void *)session) /* synchronized call */
+                               if (msg->isSynchronousCall() == true) /* synchronized call */
                                {
                                        /* sync call */
                                        session->syncLock();
@@ -371,9 +881,9 @@ namespace smartcard_service_api
 
                case Message::MSG_REQUEST_GET_ATR :
                        {
-                               SCARD_DEBUG("MSG_REQUEST_GET_ATR");
+                               _INFO("MSG_REQUEST_GET_ATR");
 
-                               if (msg->callback == (void *)session) /* synchronized call */
+                               if (msg->isSynchronousCall() == true) /* synchronized call */
                                {
                                        /* sync call */
                                        session->syncLock();
@@ -389,16 +899,19 @@ namespace smartcard_service_api
                                        getATRCallback cb = (getATRCallback)msg->callback;
 
                                        /* async call */
-                                       cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
+                                       cb(msg->data.getBuffer(),
+                                               msg->data.size(),
+                                               msg->error,
+                                               msg->userParam);
                                }
                        }
                        break;
 
                case Message::MSG_REQUEST_CLOSE_SESSION :
                        {
-                               SCARD_DEBUG("MSG_REQUEST_CLOSE_SESSION");
+                               _INFO("MSG_REQUEST_CLOSE_SESSION");
 
-                               if (msg->callback == (void *)session) /* synchronized call */
+                               if (msg->isSynchronousCall() == true) /* synchronized call */
                                {
                                        /* sync call */
                                        session->syncLock();
@@ -420,9 +933,9 @@ namespace smartcard_service_api
 
                case Message::MSG_REQUEST_GET_CHANNEL_COUNT :
                        {
-                               SCARD_DEBUG("MSG_REQUEST_GET_CHANNEL_COUNT");
+                               _INFO("MSG_REQUEST_GET_CHANNEL_COUNT");
 
-                               if (msg->callback == (void *)session) /* synchronized call */
+                               if (msg->isSynchronousCall() == true) /* synchronized call */
                                {
                                        /* sync call */
                                        session->syncLock();
@@ -443,8 +956,8 @@ namespace smartcard_service_api
                        }
                        break;
 
-               default:
-                       SCARD_DEBUG("unknown message : %s", msg->toString());
+               default :
+                       _DBG("unknown message : %s", msg->toString().c_str());
                        break;
                }
 
@@ -452,6 +965,7 @@ namespace smartcard_service_api
 
                return result;
        }
+#endif
 } /* namespace smartcard_service_api */
 
 /* export C API */
@@ -464,7 +978,7 @@ namespace smartcard_service_api
        } \
        else \
        { \
-               SCARD_DEBUG_ERR("Invalid param"); \
+               _ERR("Invalid param"); \
        }
 
 using namespace smartcard_service_api;
@@ -474,7 +988,7 @@ EXTERN_API reader_h session_get_reader(session_h handle)
        reader_h reader = NULL;
 
        SESSION_EXTERN_BEGIN;
-       reader = session->getReader();
+               reader = session->getReader();
        SESSION_EXTERN_END;
 
        return reader;
@@ -485,7 +999,7 @@ EXTERN_API int session_get_atr(session_h handle, session_get_atr_cb callback, vo
        int result = -1;
 
        SESSION_EXTERN_BEGIN;
-       result = session->getATR((getATRCallback)callback, userData);
+               result = session->getATR((getATRCallback)callback, userData);
        SESSION_EXTERN_END;
 
        return result;
@@ -496,7 +1010,7 @@ EXTERN_API int session_close(session_h handle, session_close_session_cb callback
        int result = -1;
 
        SESSION_EXTERN_BEGIN;
-       result = session->close((closeSessionCallback)callback, userData);
+               result = session->close((closeSessionCallback)callback, userData);
        SESSION_EXTERN_END;
 
        return result;
@@ -507,7 +1021,7 @@ EXTERN_API bool session_is_closed(session_h handle)
        bool result = false;
 
        SESSION_EXTERN_BEGIN;
-       result = session->isClosed();
+               result = session->isClosed();
        SESSION_EXTERN_END;
 
        return result;
@@ -516,27 +1030,29 @@ EXTERN_API bool session_is_closed(session_h handle)
 EXTERN_API void session_close_channels(session_h handle)
 {
        SESSION_EXTERN_BEGIN;
-       session->closeChannels();
+               session->closeChannels();
        SESSION_EXTERN_END;
 }
 
-EXTERN_API int session_open_basic_channel(session_h handle, unsigned char *aid, unsigned int length, session_open_channel_cb callback, void *userData)
+EXTERN_API int session_open_basic_channel(session_h handle, unsigned char *aid,
+       unsigned int length, session_open_channel_cb callback, void *userData)
 {
        int result = -1;
 
        SESSION_EXTERN_BEGIN;
-       result = session->openBasicChannel(aid, length, (openChannelCallback)callback, userData);
+               result = session->openBasicChannel(aid, length, (openChannelCallback)callback, userData);
        SESSION_EXTERN_END;
 
        return result;
 }
 
-EXTERN_API int session_open_logical_channel(session_h handle, unsigned char *aid, unsigned int length, session_open_channel_cb callback, void *userData)
+EXTERN_API int session_open_logical_channel(session_h handle, unsigned char *aid,
+       unsigned int length, session_open_channel_cb callback, void *userData)
 {
        int result = -1;
 
        SESSION_EXTERN_BEGIN;
-       result = session->openLogicalChannel(aid, length, (openChannelCallback)callback, userData);
+               result = session->openLogicalChannel(aid, length, (openChannelCallback)callback, userData);
        SESSION_EXTERN_END;
 
        return result;
@@ -547,7 +1063,7 @@ EXTERN_API int session_get_channel_count(session_h handle, session_get_channel_c
        int result = -1;
 
        SESSION_EXTERN_BEGIN;
-       result = session->getChannelCount((getChannelCountCallback)callback, userData);
+               result = session->getChannelCount((getChannelCountCallback)callback, userData);
        SESSION_EXTERN_END;
 
        return result;
@@ -555,9 +1071,6 @@ EXTERN_API int session_get_channel_count(session_h handle, session_get_channel_c
 
 EXTERN_API void session_destroy_instance(session_h handle)
 {
-       SESSION_EXTERN_BEGIN;
-       delete session;
-       SESSION_EXTERN_END;
 }
 
 EXTERN_API int session_get_atr_sync(session_h handle, unsigned char **buffer, unsigned int *length)
@@ -570,16 +1083,16 @@ EXTERN_API int session_get_atr_sync(session_h handle, unsigned char **buffer, un
                return result;
 
        SESSION_EXTERN_BEGIN;
-       temp = session->getATRSync();
-       if (temp.getLength() > 0)
-       {
-               *length = temp.getLength();
-               *buffer = (unsigned char *)calloc(1, *length);
-               memcpy(*buffer, temp.getBuffer(), *length);
+               temp = session->getATRSync();
+               if (temp.size() > 0)
+               {
+                       *length = temp.size();
+                       *buffer = (unsigned char *)calloc(1, *length);
+                       memcpy(*buffer, temp.getBuffer(), *length);
 
-               result = 0;
-       }
-       SESSION_EXTERN_END;
+                       result = 0;
+               }
+               SESSION_EXTERN_END;
 #endif
 
        return result;
@@ -589,7 +1102,7 @@ EXTERN_API void session_close_sync(session_h handle)
 {
 #ifdef CLIENT_IPC_THREAD
        SESSION_EXTERN_BEGIN;
-       session->closeSync();
+               session->closeSync();
        SESSION_EXTERN_END;
 #endif
 }
@@ -600,7 +1113,7 @@ EXTERN_API channel_h session_open_basic_channel_sync(session_h handle, unsigned
 
 #ifdef CLIENT_IPC_THREAD
        SESSION_EXTERN_BEGIN;
-       result = session->openBasicChannelSync(aid, length);
+               result = session->openBasicChannelSync(aid, length);
        SESSION_EXTERN_END;
 #endif
 
@@ -613,7 +1126,7 @@ EXTERN_API channel_h session_open_logical_channel_sync(session_h handle, unsigne
 
 #ifdef CLIENT_IPC_THREAD
        SESSION_EXTERN_BEGIN;
-       result = session->openBasicChannelSync(aid, length);
+               result = session->openLogicalChannelSync(aid, length);
        SESSION_EXTERN_END;
 #endif
 
@@ -626,7 +1139,7 @@ EXTERN_API unsigned int session_get_channel_count_sync(session_h handle)
 
 #ifdef CLIENT_IPC_THREAD
        SESSION_EXTERN_BEGIN;
-       result = session->getChannelCountSync();
+               result = session->getChannelCountSync();
        SESSION_EXTERN_END;
 #endif