2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* standard library header */
22 /* SLP library header */
27 #include "ClientIPC.h"
28 #include "ClientChannel.h"
29 #include "ReaderHelper.h"
30 #include "APDUHelper.h"
33 #define EXTERN_API __attribute__((visibility("default")))
36 namespace smartcard_service_api
38 ClientChannel::ClientChannel(void *context, Session *session,
39 int channelNum, ByteArray selectResponse, void *handle)
42 this->channelNum = -1;
48 SCARD_DEBUG_ERR("ClientIPC::getInstance() failed");
53 this->channelNum = channelNum;
54 this->handle = handle;
55 this->selectResponse = selectResponse;
56 this->context = context;
59 ClientChannel::~ClientChannel()
64 void ClientChannel::closeSync() throw(ErrorIO &, ErrorIllegalState &)
66 #ifdef CLIENT_IPC_THREAD
67 if (isClosed() == false)
69 if (getSession()->getReader()->isSecureElementPresent() == true)
74 /* send message to server */
75 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
76 msg.param1 = (unsigned long)handle;
77 msg.error = (unsigned long)context; /* using error to context */
78 msg.caller = (void *)this;
79 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
82 if (ClientIPC::getInstance().sendMessage(&msg) == true)
84 rv = waitTimedCondition(0);
87 SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
88 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
93 SCARD_DEBUG_ERR("sendMessage failed");
94 this->error = SCARD_ERROR_IPC_FAILED;
100 if (this->error != SCARD_ERROR_OK)
102 ThrowError::throwError(this->error);
108 SCARD_DEBUG("unavailable channel");
114 int ClientChannel::close(closeCallback callback, void *userParam)
116 int result = SCARD_ERROR_OK;
118 if (isClosed() == false)
120 if (getSession()->getReader()->isSecureElementPresent() == true)
125 /* send message to server */
126 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
127 msg.param1 = (unsigned long)handle;
128 msg.error = (unsigned long)context; /* using error to context */
129 msg.caller = (void *)this;
130 msg.callback = (void *)callback;
131 msg.userParam = userParam;
133 if (ClientIPC::getInstance().sendMessage(&msg) == false)
135 result = SCARD_ERROR_IPC_FAILED;
140 SCARD_DEBUG_ERR("unavailable channel");
141 result = SCARD_ERROR_ILLEGAL_STATE;
148 int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
149 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
151 int rv = SCARD_ERROR_OK;
153 if (getSession()->getReader()->isSecureElementPresent() == true)
157 #ifdef CLIENT_IPC_THREAD
158 /* send message to server */
159 msg.message = Message::MSG_REQUEST_TRANSMIT;
160 msg.param1 = (unsigned long)handle;
163 msg.error = (unsigned long)context; /* using error to context */
164 msg.caller = (void *)this;
165 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
168 if (ClientIPC::getInstance().sendMessage(&msg) == true)
170 rv = waitTimedCondition(0);
179 SCARD_DEBUG_ERR("timeout");
181 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
186 SCARD_DEBUG_ERR("sendMessage failed");
190 if (this->error != SCARD_ERROR_OK)
192 ThrowError::throwError(this->error);
198 SCARD_DEBUG_ERR("unavailable channel");
199 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
205 int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
209 if (getSession()->getReader()->isSecureElementPresent() == true)
213 /* send message to server */
214 msg.message = Message::MSG_REQUEST_TRANSMIT;
215 msg.param1 = (unsigned long)handle;
218 msg.error = (unsigned long)context; /* using error to context */
219 msg.caller = (void *)this;
220 msg.callback = (void *)callback;
221 msg.userParam = userParam;
223 if (ClientIPC::getInstance().sendMessage(&msg) == true)
225 result = SCARD_ERROR_OK;
229 result = SCARD_ERROR_IPC_FAILED;
234 SCARD_DEBUG_ERR("unavailable channel");
235 result = SCARD_ERROR_ILLEGAL_STATE;
241 bool ClientChannel::dispatcherCallback(void *message)
243 Message *msg = (Message *)message;
244 ClientChannel *channel = NULL;
249 SCARD_DEBUG_ERR("message is null");
253 channel = (ClientChannel *)msg->caller;
255 switch (msg->message)
257 case Message::MSG_REQUEST_TRANSMIT :
259 /* transmit result */
260 SCARD_DEBUG("MSG_REQUEST_TRANSMIT");
262 if (msg->error == 0 &&
263 ResponseHelper::getStatus(msg->data) == 0)
265 /* store select response */
266 if (msg->data.getAt(1) == APDUCommand::INS_SELECT_FILE)
267 channel->setSelectResponse(msg->data);
270 if (msg->isSynchronousCall() == true) /* synchronized call */
276 channel->error = msg->error;
277 channel->response = msg->data;
279 channel->signalCondition();
280 channel->syncUnlock();
282 else if (msg->callback != NULL)
284 transmitCallback cb = (transmitCallback)msg->callback;
287 cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
292 case Message::MSG_REQUEST_CLOSE_CHANNEL :
294 SCARD_DEBUG("MSG_REQUEST_CLOSE_CHANNEL");
296 if (msg->isSynchronousCall() == true) /* synchronized call */
301 channel->error = msg->error;
303 channel->signalCondition();
304 channel->syncUnlock();
306 else if (msg->callback != NULL)
308 closeCallback cb = (closeCallback)msg->callback;
311 cb(msg->error, msg->userParam);
317 SCARD_DEBUG("unknwon message : %s", msg->toString());
323 } /* namespace smartcard_service_api */
326 #define CHANNEL_EXTERN_BEGIN \
327 if (handle != NULL) \
329 ClientChannel *channel = (ClientChannel *)handle;
331 #define CHANNEL_EXTERN_END \
335 SCARD_DEBUG_ERR("Invalid param"); \
338 using namespace smartcard_service_api;
340 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
344 CHANNEL_EXTERN_BEGIN;
345 result = channel->close((closeCallback)callback, userParam);
351 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
352 unsigned int length, channel_transmit_cb callback, void *userParam)
356 CHANNEL_EXTERN_BEGIN;
359 temp.setBuffer(command, length);
360 result = channel->transmit(temp, (transmitCallback)callback, userParam);
366 EXTERN_API void channel_close_sync(channel_h handle)
368 #ifdef CLIENT_IPC_THREAD
369 CHANNEL_EXTERN_BEGIN;
372 channel->closeSync();
381 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
382 unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
386 #ifdef CLIENT_IPC_THREAD
387 if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
390 CHANNEL_EXTERN_BEGIN;
391 ByteArray temp, resp;
393 temp.setBuffer(command, cmd_len);
397 result = channel->transmitSync(temp, resp);
398 if (resp.getLength() > 0)
400 *resp_len = resp.getLength();
401 *response = (unsigned char *)calloc(1, *resp_len);
402 memcpy(*response, resp.getBuffer(), *resp_len);
415 EXTERN_API bool channel_is_basic_channel(channel_h handle)
419 CHANNEL_EXTERN_BEGIN;
420 result = channel->isBasicChannel();
426 EXTERN_API bool channel_is_closed(channel_h handle)
430 CHANNEL_EXTERN_BEGIN;
431 result = channel->isClosed();
437 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
439 unsigned int result = 0;
441 CHANNEL_EXTERN_BEGIN;
442 result = channel->getSelectResponse().getLength();
448 EXTERN_API bool channel_get_select_response(channel_h handle,
449 unsigned char *buffer, unsigned int length)
453 if (buffer == NULL || length == 0)
458 CHANNEL_EXTERN_BEGIN;
461 response = channel->getSelectResponse();
462 if (response.getLength() > 0)
464 memcpy(buffer, response.getBuffer(), MIN(length, response.getLength()));
472 EXTERN_API session_h channel_get_session(channel_h handle)
474 session_h session = NULL;
476 CHANNEL_EXTERN_BEGIN;
477 session = channel->getSession();
483 EXTERN_API void channel_destroy_instance(channel_h handle)