Remove storing select response every select command
[platform/core/connectivity/smartcard-service.git] / client / ClientChannel.cpp
index b5b69a0..b4d84da 100644 (file)
@@ -27,6 +27,7 @@
 #include "ClientIPC.h"
 #include "ClientChannel.h"
 #include "ReaderHelper.h"
+#include "APDUHelper.h"
 
 #ifndef EXTERN_API
 #define EXTERN_API __attribute__((visibility("default")))
@@ -36,7 +37,7 @@ namespace smartcard_service_api
 {
        ClientChannel::ClientChannel(void *context, Session *session,
                int channelNum, ByteArray selectResponse, void *handle)
-               :Channel(session)
+               : Channel(session)
        {
                this->channelNum = -1;
                this->handle = NULL;
@@ -44,7 +45,7 @@ namespace smartcard_service_api
 
                if (handle == NULL)
                {
-                       SCARD_DEBUG_ERR("ClientIPC::getInstance() failed");
+                       _ERR("ClientIPC::getInstance() failed");
 
                        return;
                }
@@ -60,7 +61,9 @@ namespace smartcard_service_api
                closeSync();
        }
 
-       void ClientChannel::closeSync() throw(ErrorIO &, ErrorIllegalState &)
+       void ClientChannel::closeSync()
+               throw(ExceptionBase &, ErrorIO &, ErrorSecurity &,
+                       ErrorIllegalState &, ErrorIllegalParameter &)
        {
 #ifdef CLIENT_IPC_THREAD
                if (isClosed() == false)
@@ -72,41 +75,38 @@ namespace smartcard_service_api
 
                                /* send message to server */
                                msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
-                               msg.param1 = (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 */
 
-                               try
+                               syncLock();
+                               if (ClientIPC::getInstance().sendMessage(&msg) == true)
                                {
-                                       syncLock();
-                                       if (ClientIPC::getInstance().sendMessage(&msg) == true)
-                                       {
-                                               rv = waitTimedCondition(0);
-                                               if (rv < 0)
-                                               {
-                                                       SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
-                                               }
-                                       }
-                                       else
+                                       rv = waitTimedCondition(0);
+                                       if (rv < 0)
                                        {
-                                               SCARD_DEBUG_ERR("sendMessage failed");
-                                               throw ErrorIO(SCARD_ERROR_IPC_FAILED);
+                                               _ERR("timeout [%d]", rv);
+                                               this->error = SCARD_ERROR_OPERATION_TIMEOUT;
                                        }
-                                       syncUnlock();
                                }
-                               catch (ExceptionBase &e)
+                               else
                                {
-                                       syncUnlock();
-
-                                       throw e;
+                                       _ERR("sendMessage failed");
+                                       this->error = SCARD_ERROR_IPC_FAILED;
                                }
+                               syncUnlock();
 
                                channelNum = -1;
+
+                               if (this->error != SCARD_ERROR_OK)
+                               {
+                                       ThrowError::throwError(this->error);
+                               }
                        }
                        else
                        {
-                               SCARD_DEBUG_ERR("unavailable channel");
+                               _INFO("unavailable channel");
                        }
                }
 #endif
@@ -114,37 +114,33 @@ namespace smartcard_service_api
 
        int ClientChannel::close(closeCallback callback, void *userParam)
        {
-               int result = 0;
+               int result = SCARD_ERROR_OK;
 
                if (isClosed() == false)
                {
                        if (getSession()->getReader()->isSecureElementPresent() == true)
                        {
                                Message msg;
-
                                channelNum = -1;
 
                                /* send message to server */
                                msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
-                               msg.param1 = (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 = userParam;
 
-                               if (ClientIPC::getInstance().sendMessage(&msg) == true)
+                               if (ClientIPC::getInstance().sendMessage(&msg) == false)
                                {
-                                       result = 0;
-                               }
-                               else
-                               {
-                                       result = -1;
+                                       _ERR("sendMessage failed");
+                                       result = SCARD_ERROR_IPC_FAILED;
                                }
                        }
                        else
                        {
-                               SCARD_DEBUG_ERR("unavailable channel");
-                               result = -1;
+                               _ERR("unavailable channel");
+                               result = SCARD_ERROR_ILLEGAL_STATE;
                        }
                }
 
@@ -152,10 +148,10 @@ namespace smartcard_service_api
        }
 
        int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
-               throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
+               throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
+                       ErrorIllegalParameter &, ErrorSecurity &)
        {
-               int rv = -1;
-
+               int rv = SCARD_ERROR_OK;
                if (getSession()->getReader()->isSecureElementPresent() == true)
                {
                        Message msg;
@@ -163,10 +159,10 @@ namespace smartcard_service_api
 #ifdef CLIENT_IPC_THREAD
                        /* send message to server */
                        msg.message = Message::MSG_REQUEST_TRANSMIT;
-                       msg.param1 = (int)handle;
+                       msg.param1 = (unsigned long)handle;
                        msg.param2 = 0;
                        msg.data = command;
-                       msg.error = (unsigned int)context; /* using error to context */
+                       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 */
 
@@ -177,26 +173,29 @@ namespace smartcard_service_api
                                if (rv >= 0)
                                {
                                        result = response;
-
-                                       rv = 0;
+                                       rv = SCARD_ERROR_OK;
                                }
                                else
                                {
-                                       SCARD_DEBUG_ERR("timeout");
-
-                                       rv = -1;
+                                       _ERR("timeout [%d]", rv);
+                                       this->error = SCARD_ERROR_OPERATION_TIMEOUT;
                                }
                        }
                        else
                        {
-                               SCARD_DEBUG_ERR("sendMessage failed");
+                               _ERR("sendMessage failed");
                        }
                        syncUnlock();
+
+                       if (this->error != SCARD_ERROR_OK)
+                       {
+                               ThrowError::throwError(this->error);
+                       }
 #endif
                }
                else
                {
-                       SCARD_DEBUG_ERR("unavailable channel");
+                       _ERR("unavailable channel");
                        throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
                }
 
@@ -205,7 +204,7 @@ namespace smartcard_service_api
 
        int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
        {
-               int result = -1;
+               int result;
 
                if (getSession()->getReader()->isSecureElementPresent() == true)
                {
@@ -213,26 +212,28 @@ namespace smartcard_service_api
 
                        /* send message to server */
                        msg.message = Message::MSG_REQUEST_TRANSMIT;
-                       msg.param1 = (int)handle;
+                       msg.param1 = (unsigned long)handle;
                        msg.param2 = 0;
                        msg.data = command;
-                       msg.error = (unsigned int)context; /* using error to context */
+                       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 = 0;
+                               result = SCARD_ERROR_OK;
                        }
                        else
                        {
-                               result = -1;
+                               _ERR("sendMessage failed");
+                               result = SCARD_ERROR_IPC_FAILED;
                        }
                }
                else
                {
-                       SCARD_DEBUG_ERR("unavailable channel");
+                       _ERR("unavailable channel");
+                       result = SCARD_ERROR_ILLEGAL_STATE;
                }
 
                return result;
@@ -246,7 +247,7 @@ namespace smartcard_service_api
 
                if (msg == NULL)
                {
-                       SCARD_DEBUG_ERR("message is null");
+                       _ERR("message is null");
                        return result;
                }
 
@@ -257,7 +258,7 @@ namespace smartcard_service_api
                case Message::MSG_REQUEST_TRANSMIT :
                        {
                                /* transmit result */
-                               SCARD_DEBUG("MSG_REQUEST_TRANSMIT");
+                               _INFO("MSG_REQUEST_TRANSMIT");
 
                                if (msg->isSynchronousCall() == true) /* synchronized call */
                                {
@@ -283,7 +284,7 @@ namespace smartcard_service_api
 
                case Message::MSG_REQUEST_CLOSE_CHANNEL :
                        {
-                               SCARD_DEBUG("MSG_REQUEST_CLOSE_CHANNEL");
+                               _INFO("MSG_REQUEST_CLOSE_CHANNEL");
 
                                if (msg->isSynchronousCall() == true) /* synchronized call */
                                {
@@ -306,10 +307,12 @@ namespace smartcard_service_api
                        break;
 
                default:
-                       SCARD_DEBUG("unknwon message : %s", msg->toString());
+                       _DBG("Unknown message : %s", msg->toString());
                        break;
                }
 
+               delete msg;
+
                return result;
        }
 } /* namespace smartcard_service_api */
@@ -324,7 +327,7 @@ namespace smartcard_service_api
        } \
        else \
        { \
-               SCARD_DEBUG_ERR("Invalid param"); \
+               _ERR("Invalid param"); \
        }
 
 using namespace smartcard_service_api;