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 = (int)handle;
77 msg.error = (unsigned int)context; /* using error to context */
78 msg.caller = (void *)this;
79 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
84 if (ClientIPC::getInstance().sendMessage(&msg) == true)
86 rv = waitTimedCondition(0);
89 SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
94 SCARD_DEBUG_ERR("sendMessage failed");
95 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
99 catch (ExceptionBase &e)
110 SCARD_DEBUG_ERR("unavailable channel");
116 int ClientChannel::close(closeCallback callback, void *userParam)
120 if (isClosed() == false)
122 if (getSession()->getReader()->isSecureElementPresent() == true)
128 /* send message to server */
129 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
130 msg.param1 = (int)handle;
131 msg.error = (unsigned int)context; /* using error to context */
132 msg.caller = (void *)this;
133 msg.callback = (void *)callback;
134 msg.userParam = userParam;
136 if (ClientIPC::getInstance().sendMessage(&msg) == true)
147 SCARD_DEBUG_ERR("unavailable channel");
155 int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
156 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
160 if (getSession()->getReader()->isSecureElementPresent() == true)
164 #ifdef CLIENT_IPC_THREAD
165 /* send message to server */
166 msg.message = Message::MSG_REQUEST_TRANSMIT;
167 msg.param1 = (int)handle;
170 msg.error = (unsigned int)context; /* using error to context */
171 msg.caller = (void *)this;
172 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
175 if (ClientIPC::getInstance().sendMessage(&msg) == true)
177 rv = waitTimedCondition(0);
186 SCARD_DEBUG_ERR("timeout");
193 SCARD_DEBUG_ERR("sendMessage failed");
200 SCARD_DEBUG_ERR("unavailable channel");
201 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
207 int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
211 if (getSession()->getReader()->isSecureElementPresent() == true)
215 /* send message to server */
216 msg.message = Message::MSG_REQUEST_TRANSMIT;
217 msg.param1 = (int)handle;
220 msg.error = (unsigned int)context; /* using error to context */
221 msg.caller = (void *)this;
222 msg.callback = (void *)callback;
223 msg.userParam = userParam;
225 if (ClientIPC::getInstance().sendMessage(&msg) == true)
236 SCARD_DEBUG_ERR("unavailable channel");
242 bool ClientChannel::dispatcherCallback(void *message)
244 Message *msg = (Message *)message;
245 ClientChannel *channel = NULL;
250 SCARD_DEBUG_ERR("message is null");
254 channel = (ClientChannel *)msg->caller;
256 switch (msg->message)
258 case Message::MSG_REQUEST_TRANSMIT :
260 /* transmit result */
261 SCARD_DEBUG("MSG_REQUEST_TRANSMIT");
263 if (msg->error == 0 &&
264 ResponseHelper::getStatus(msg->data) == 0)
266 /* store select response */
267 if (msg->data.getAt(1) == APDUCommand::INS_SELECT_FILE)
268 channel->setSelectResponse(msg->data);
271 if (msg->isSynchronousCall() == true) /* synchronized call */
277 channel->error = msg->error;
278 channel->response = msg->data;
280 channel->signalCondition();
281 channel->syncUnlock();
283 else if (msg->callback != NULL)
285 transmitCallback cb = (transmitCallback)msg->callback;
288 cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
293 case Message::MSG_REQUEST_CLOSE_CHANNEL :
295 SCARD_DEBUG("MSG_REQUEST_CLOSE_CHANNEL");
297 if (msg->isSynchronousCall() == true) /* synchronized call */
302 channel->error = msg->error;
304 channel->signalCondition();
305 channel->syncUnlock();
307 else if (msg->callback != NULL)
309 closeCallback cb = (closeCallback)msg->callback;
312 cb(msg->error, msg->userParam);
318 SCARD_DEBUG("unknwon message : %s", msg->toString());
324 } /* namespace smartcard_service_api */
327 #define CHANNEL_EXTERN_BEGIN \
328 if (handle != NULL) \
330 ClientChannel *channel = (ClientChannel *)handle;
332 #define CHANNEL_EXTERN_END \
336 SCARD_DEBUG_ERR("Invalid param"); \
339 using namespace smartcard_service_api;
341 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
345 CHANNEL_EXTERN_BEGIN;
346 result = channel->close((closeCallback)callback, userParam);
352 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
353 unsigned int length, channel_transmit_cb callback, void *userParam)
357 CHANNEL_EXTERN_BEGIN;
360 temp.setBuffer(command, length);
361 result = channel->transmit(temp, (transmitCallback)callback, userParam);
367 EXTERN_API void channel_close_sync(channel_h handle)
369 #ifdef CLIENT_IPC_THREAD
370 CHANNEL_EXTERN_BEGIN;
373 channel->closeSync();
382 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
383 unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
387 #ifdef CLIENT_IPC_THREAD
388 if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
391 CHANNEL_EXTERN_BEGIN;
392 ByteArray temp, resp;
394 temp.setBuffer(command, cmd_len);
398 result = channel->transmitSync(temp, resp);
399 if (resp.getLength() > 0)
401 *resp_len = resp.getLength();
402 *response = (unsigned char *)calloc(1, *resp_len);
403 memcpy(*response, resp.getBuffer(), *resp_len);
416 EXTERN_API bool channel_is_basic_channel(channel_h handle)
420 CHANNEL_EXTERN_BEGIN;
421 result = channel->isBasicChannel();
427 EXTERN_API bool channel_is_closed(channel_h handle)
431 CHANNEL_EXTERN_BEGIN;
432 result = channel->isClosed();
438 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
440 unsigned int result = 0;
442 CHANNEL_EXTERN_BEGIN;
443 result = channel->getSelectResponse().getLength();
449 EXTERN_API bool channel_get_select_response(channel_h handle,
450 unsigned char *buffer, unsigned int length)
454 if (buffer == NULL || length == 0)
459 CHANNEL_EXTERN_BEGIN;
462 response = channel->getSelectResponse();
463 if (response.getLength() > 0)
465 memcpy(buffer, response.getBuffer(), MIN(length, response.getLength()));
473 EXTERN_API session_h channel_get_session(channel_h handle)
475 session_h session = NULL;
477 CHANNEL_EXTERN_BEGIN;
478 session = channel->getSession();
484 EXTERN_API void channel_destroy_instance(channel_h handle)