code cleanup and fix build problems
[platform/core/connectivity/smartcard-service.git] / client / ClientChannel.cpp
index e3fd2df..596d044 100644 (file)
@@ -18,9 +18,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
-#ifdef USE_GDBUS
 #include <glib.h>
-#endif
 
 /* SLP library header */
 
 #include "ClientChannel.h"
 #include "ReaderHelper.h"
 #include "APDUHelper.h"
-#ifdef USE_GDBUS
-#include "smartcard-service-gdbus.h"
-#include "GDBusHelper.h"
-#else
-#include "Message.h"
-#include "ClientIPC.h"
-#endif
+#include "ClientGDBus.h"
 
 #ifndef EXTERN_API
 #define EXTERN_API __attribute__((visibility("default")))
@@ -44,7 +36,7 @@
 namespace smartcard_service_api
 {
        ClientChannel::ClientChannel(void *context, Session *session,
-               int channelNum, ByteArray selectResponse, void *handle)
+               int channelNum, const ByteArray &selectResponse, void *handle)
                : Channel(session)
        {
                this->channelNum = -1;
@@ -62,14 +54,6 @@ namespace smartcard_service_api
                this->handle = handle;
                this->selectResponse = selectResponse;
                this->context = context;
-#ifdef USE_GDBUS
-               /* initialize client */
-               if (!g_thread_supported())
-               {
-                       g_thread_init(NULL);
-               }
-
-               g_type_init();
 
                /* init default context */
                GError *error = NULL;
@@ -85,7 +69,6 @@ namespace smartcard_service_api
                        g_error_free(error);
                        return;
                }
-#endif
        }
 
        ClientChannel::~ClientChannel()
@@ -93,7 +76,6 @@ namespace smartcard_service_api
                closeSync();
        }
 
-#ifdef USE_GDBUS
        void ClientChannel::channel_transmit_cb(GObject *source_object,
                GAsyncResult *res, gpointer user_data)
        {
@@ -130,7 +112,7 @@ namespace smartcard_service_api
 
                if (callback != NULL) {
                        callback(response.getBuffer(),
-                               response.getLength(),
+                               response.size(),
                                result, param->user_param);
                }
 
@@ -177,7 +159,7 @@ namespace smartcard_service_api
 
                delete param;
        }
-#endif
+
        void ClientChannel::closeSync()
                throw(ExceptionBase &, ErrorIO &, ErrorSecurity &,
                        ErrorIllegalState &, ErrorIllegalParameter &)
@@ -186,7 +168,6 @@ namespace smartcard_service_api
                {
                        if (getSession()->getReader()->isSecureElementPresent() == true)
                        {
-#ifdef USE_GDBUS
                                gint ret;
                                GError *error = NULL;
 
@@ -210,43 +191,6 @@ namespace smartcard_service_api
 
                                        throw ErrorIO(SCARD_ERROR_IPC_FAILED);
                                }
-#else
-                               Message msg;
-                               int rv;
-
-#ifdef CLIENT_IPC_THREAD
-                               /* send message to server */
-                               msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
-                               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("timeout [%d]", rv);
-                                               this->error = SCARD_ERROR_OPERATION_TIMEOUT;
-                                       }
-                               }
-                               else
-                               {
-                                       _ERR("sendMessage failed");
-                                       this->error = SCARD_ERROR_IPC_FAILED;
-                               }
-                               syncUnlock();
-
-                               channelNum = -1;
-
-                               if (this->error != SCARD_ERROR_OK)
-                               {
-                                       ThrowError::throwError(this->error);
-                               }
-#endif
-#endif
                        }
                        else
                        {
@@ -263,7 +207,6 @@ namespace smartcard_service_api
                {
                        if (getSession()->getReader()->isSecureElementPresent() == true)
                        {
-#ifdef USE_GDBUS
                                CallbackParam *param = new CallbackParam();
 
                                param->instance = this;
@@ -275,24 +218,6 @@ namespace smartcard_service_api
                                        GPOINTER_TO_UINT(context),
                                        GPOINTER_TO_UINT(handle), NULL,
                                        &ClientChannel::channel_close_cb, param);
-#else
-                               Message msg;
-                               channelNum = -1;
-
-                               /* send message to server */
-                               msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
-                               msg.param1 = (unsigned long)handle;
-                               msg.error = (unsigned long)context; /* using error to context */
-                               msg.caller = (void *)this;
-                               msg.callback = (void *)callback;
-                               msg.userParam = userParam;
-
-                               if (ClientIPC::getInstance().sendMessage(&msg) == false)
-                               {
-                                       _ERR("sendMessage failed");
-                                       result = SCARD_ERROR_IPC_FAILED;
-                               }
-#endif
                        }
                        else
                        {
@@ -304,7 +229,7 @@ namespace smartcard_service_api
                return result;
        }
 
-       int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
+       int ClientChannel::transmitSync(const ByteArray &command, ByteArray &result)
                throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
                        ErrorIllegalParameter &, ErrorSecurity &)
        {
@@ -312,7 +237,6 @@ namespace smartcard_service_api
 
                if (getSession()->getReader()->isSecureElementPresent() == true)
                {
-#ifdef USE_GDBUS
                        GVariant *var_command = NULL, *var_response = NULL;
                        GError *error = NULL;
 
@@ -337,45 +261,6 @@ namespace smartcard_service_api
 
                                throw ErrorIO(SCARD_ERROR_IPC_FAILED);
                        }
-#else
-                       Message msg;
-#ifdef CLIENT_IPC_THREAD
-                       /* send message to server */
-                       msg.message = Message::MSG_REQUEST_TRANSMIT;
-                       msg.param1 = (unsigned long)handle;
-                       msg.param2 = 0;
-                       msg.data = command;
-                       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)
-                               {
-                                       result = response;
-                                       rv = SCARD_ERROR_OK;
-                               }
-                               else
-                               {
-                                       _ERR("timeout [%d]", rv);
-                                       this->error = SCARD_ERROR_OPERATION_TIMEOUT;
-                               }
-                       }
-                       else
-                       {
-                               _ERR("sendMessage failed");
-                       }
-                       syncUnlock();
-
-                       if (this->error != SCARD_ERROR_OK)
-                       {
-                               ThrowError::throwError(this->error);
-                       }
-#endif
-#endif
                }
                else
                {
@@ -386,13 +271,12 @@ namespace smartcard_service_api
                return rv;
        }
 
-       int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
+       int ClientChannel::transmit(const ByteArray &command, transmitCallback callback, void *userParam)
        {
                int result;
 
                if (getSession()->getReader()->isSecureElementPresent() == true)
                {
-#ifdef USE_GDBUS
                        GVariant *var_command;
                        CallbackParam *param = new CallbackParam();
 
@@ -407,32 +291,9 @@ namespace smartcard_service_api
                                GPOINTER_TO_UINT(context),
                                GPOINTER_TO_UINT(handle),
                                var_command, NULL,
-                               &ClientChannel::channel_close_cb, param);
+                               &ClientChannel::channel_transmit_cb, param);
 
                        result = SCARD_ERROR_OK;
-#else
-                       Message msg;
-
-                       /* send message to server */
-                       msg.message = Message::MSG_REQUEST_TRANSMIT;
-                       msg.param1 = (unsigned long)handle;
-                       msg.param2 = 0;
-                       msg.data = command;
-                       msg.error = (unsigned long)context; /* using error to context */
-                       msg.caller = (void *)this;
-                       msg.callback = (void *)callback;
-                       msg.userParam = userParam;
-
-                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
-                       {
-                               result = SCARD_ERROR_OK;
-                       }
-                       else
-                       {
-                               _ERR("sendMessage failed");
-                               result = SCARD_ERROR_IPC_FAILED;
-                       }
-#endif
                }
                else
                {
@@ -442,85 +303,6 @@ namespace smartcard_service_api
 
                return result;
        }
-
-#ifndef USE_GDBUS
-       bool ClientChannel::dispatcherCallback(void *message)
-       {
-               Message *msg = (Message *)message;
-               ClientChannel *channel = NULL;
-               bool result = false;
-
-               if (msg == NULL)
-               {
-                       _ERR("message is null");
-                       return result;
-               }
-
-               channel = (ClientChannel *)msg->caller;
-
-               switch (msg->message)
-               {
-               case Message::MSG_REQUEST_TRANSMIT :
-                       {
-                               /* transmit result */
-                               _INFO("MSG_REQUEST_TRANSMIT");
-
-                               if (msg->isSynchronousCall() == true) /* synchronized call */
-                               {
-                                       /* sync call */
-                                       channel->syncLock();
-
-                                       /* copy result */
-                                       channel->error = msg->error;
-                                       channel->response = msg->data;
-
-                                       channel->signalCondition();
-                                       channel->syncUnlock();
-                               }
-                               else if (msg->callback != NULL)
-                               {
-                                       transmitCallback cb = (transmitCallback)msg->callback;
-
-                                       /* async call */
-                                       cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
-                               }
-                       }
-                       break;
-
-               case Message::MSG_REQUEST_CLOSE_CHANNEL :
-                       {
-                               _INFO("MSG_REQUEST_CLOSE_CHANNEL");
-
-                               if (msg->isSynchronousCall() == true) /* synchronized call */
-                               {
-                                       /* sync call */
-                                       channel->syncLock();
-
-                                       channel->error = msg->error;
-
-                                       channel->signalCondition();
-                                       channel->syncUnlock();
-                               }
-                               else if (msg->callback != NULL)
-                               {
-                                       closeChannelCallback cb = (closeChannelCallback)msg->callback;
-
-                                       /* async call */
-                                       cb(msg->error, msg->userParam);
-                               }
-                       }
-                       break;
-
-               default:
-                       _DBG("Unknown message : %s", msg->toString());
-                       break;
-               }
-
-               delete msg;
-
-               return result;
-       }
-#endif /* USE_GDBUS */
 } /* namespace smartcard_service_api */
 
 /* export C API */
@@ -557,7 +339,7 @@ EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
        CHANNEL_EXTERN_BEGIN;
        ByteArray temp;
 
-       temp.setBuffer(command, length);
+       temp.assign(command, length);
        result = channel->transmit(temp, (transmitCallback)callback, userParam);
        CHANNEL_EXTERN_END;
 
@@ -566,7 +348,6 @@ EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
 
 EXTERN_API void channel_close_sync(channel_h handle)
 {
-#ifdef CLIENT_IPC_THREAD
        CHANNEL_EXTERN_BEGIN;
        try
        {
@@ -576,7 +357,6 @@ EXTERN_API void channel_close_sync(channel_h handle)
        {
        }
        CHANNEL_EXTERN_END;
-#endif
 }
 
 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
@@ -584,21 +364,20 @@ EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
 {
        int result = -1;
 
-#ifdef CLIENT_IPC_THREAD
        if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
                return result;
 
        CHANNEL_EXTERN_BEGIN;
        ByteArray temp, resp;
 
-       temp.setBuffer(command, cmd_len);
+       temp.assign(command, cmd_len);
 
        try
        {
                result = channel->transmitSync(temp, resp);
-               if (resp.getLength() > 0)
+               if (resp.size() > 0)
                {
-                       *resp_len = resp.getLength();
+                       *resp_len = resp.size();
                        *response = (unsigned char *)calloc(1, *resp_len);
                        memcpy(*response, resp.getBuffer(), *resp_len);
                }
@@ -608,7 +387,6 @@ EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
                result = -1;
        }
        CHANNEL_EXTERN_END;
-#endif
 
        return result;
 }
@@ -640,7 +418,7 @@ EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
        unsigned int result = 0;
 
        CHANNEL_EXTERN_BEGIN;
-       result = channel->getSelectResponse().getLength();
+       result = channel->getSelectResponse().size();
        CHANNEL_EXTERN_END;
 
        return result;
@@ -660,9 +438,9 @@ EXTERN_API bool channel_get_select_response(channel_h handle,
        ByteArray response;
 
        response = channel->getSelectResponse();
-       if (response.getLength() > 0)
+       if (response.size() > 0)
        {
-               memcpy(buffer, response.getBuffer(), MIN(length, response.getLength()));
+               memcpy(buffer, response.getBuffer(), MIN(length, response.size()));
                result = true;
        }
        CHANNEL_EXTERN_END;