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 */
25 /* SLP library header */
29 #include "ClientChannel.h"
30 #include "ReaderHelper.h"
31 #include "APDUHelper.h"
33 #include "ClientGDBus.h"
36 #include "ClientIPC.h"
40 #define EXTERN_API __attribute__((visibility("default")))
43 namespace smartcard_service_api
45 ClientChannel::ClientChannel(void *context, Session *session,
46 int channelNum, const ByteArray &selectResponse, void *handle)
49 this->channelNum = -1;
55 _ERR("ClientIPC::getInstance() failed");
60 this->channelNum = channelNum;
61 this->handle = handle;
62 this->selectResponse = selectResponse;
63 this->context = context;
65 /* initialize client */
66 if (!g_thread_supported())
73 /* init default context */
76 proxy = smartcard_service_channel_proxy_new_for_bus_sync(
77 G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
78 "org.tizen.SmartcardService",
79 "/org/tizen/SmartcardService/Channel",
83 _ERR("Can not create proxy : %s", error->message);
90 ClientChannel::~ClientChannel()
96 void ClientChannel::channel_transmit_cb(GObject *source_object,
97 GAsyncResult *res, gpointer user_data)
99 CallbackParam *param = (CallbackParam *)user_data;
100 transmitCallback callback;
102 GVariant *var_response;
103 GError *error = NULL;
106 _INFO("MSG_REQUEST_TRANSMIT");
109 _ERR("null parameter!!!");
113 callback = (transmitCallback)param->callback;
115 if (smartcard_service_channel_call_transmit_finish(
116 SMARTCARD_SERVICE_CHANNEL(source_object),
117 &result, &var_response, res, &error) == true) {
118 if (result == SCARD_ERROR_OK) {
119 GDBusHelper::convertVariantToByteArray(var_response, response);
121 _ERR("smartcard_service_channel_call_transmit failed, [%d]", result);
124 _ERR("smartcard_service_channel_call_transmit failed, [%s]", error->message);
127 result = SCARD_ERROR_IPC_FAILED;
130 if (callback != NULL) {
131 callback(response.getBuffer(),
133 result, param->user_param);
139 void ClientChannel::channel_close_cb(GObject *source_object,
140 GAsyncResult *res, gpointer user_data)
142 CallbackParam *param = (CallbackParam *)user_data;
143 ClientChannel *channel;
144 closeChannelCallback callback;
146 GError *error = NULL;
148 _INFO("MSG_REQUEST_CLOSE_CHANNEL");
151 _ERR("null parameter!!!");
155 channel = (ClientChannel *)param->instance;
156 callback = (closeChannelCallback)param->callback;
158 if (smartcard_service_channel_call_close_channel_finish(
159 SMARTCARD_SERVICE_CHANNEL(source_object),
160 &result, res, &error) == true) {
161 if (result == SCARD_ERROR_OK) {
162 channel->channelNum = -1;
164 _ERR("smartcard_service_channel_call_close_channel failed, [%d]", result);
167 _ERR("smartcard_service_channel_call_close_channel failed, [%s]", error->message);
170 result = SCARD_ERROR_IPC_FAILED;
173 if (callback != NULL) {
174 callback(result, param->user_param);
180 void ClientChannel::closeSync()
181 throw(ExceptionBase &, ErrorIO &, ErrorSecurity &,
182 ErrorIllegalState &, ErrorIllegalParameter &)
184 if (isClosed() == false)
186 if (getSession()->getReader()->isSecureElementPresent() == true)
190 GError *error = NULL;
193 _ERR("dbus proxy is not initialized yet");
194 throw ErrorIllegalState(SCARD_ERROR_NOT_INITIALIZED);
197 if (smartcard_service_channel_call_close_channel_sync(
198 (SmartcardServiceChannel *)proxy,
199 GPOINTER_TO_UINT(context),
200 GPOINTER_TO_UINT(handle),
201 &ret, NULL, &error) == true) {
202 if (ret != SCARD_ERROR_OK) {
203 _ERR("smartcard_service_channel_call_close_channel_sync failed, [%d]", ret);
207 _ERR("smartcard_service_channel_call_close_channel_sync failed, [%s]", error->message);
210 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
216 #ifdef CLIENT_IPC_THREAD
217 /* send message to server */
218 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
219 msg.param1 = (unsigned long)handle;
220 msg.error = (unsigned long)context; /* using error to context */
221 msg.caller = (void *)this;
222 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
225 if (ClientIPC::getInstance().sendMessage(msg) == true)
227 rv = waitTimedCondition(0);
230 _ERR("timeout [%d]", rv);
231 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
236 _ERR("sendMessage failed");
237 this->error = SCARD_ERROR_IPC_FAILED;
243 if (this->error != SCARD_ERROR_OK)
245 ThrowError::throwError(this->error);
252 _INFO("unavailable channel");
257 int ClientChannel::close(closeChannelCallback callback, void *userParam)
259 int result = SCARD_ERROR_OK;
261 if (isClosed() == false)
263 if (getSession()->getReader()->isSecureElementPresent() == true)
266 CallbackParam *param = new CallbackParam();
268 param->instance = this;
269 param->callback = (void *)callback;
270 param->user_param = userParam;
272 smartcard_service_channel_call_close_channel(
273 (SmartcardServiceChannel *)proxy,
274 GPOINTER_TO_UINT(context),
275 GPOINTER_TO_UINT(handle), NULL,
276 &ClientChannel::channel_close_cb, param);
281 /* send message to server */
282 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
283 msg.param1 = (unsigned long)handle;
284 msg.error = (unsigned long)context; /* using error to context */
285 msg.caller = (void *)this;
286 msg.callback = (void *)callback;
287 msg.userParam = userParam;
289 if (ClientIPC::getInstance().sendMessage(msg) == false)
291 _ERR("sendMessage failed");
292 result = SCARD_ERROR_IPC_FAILED;
298 _ERR("unavailable channel");
299 result = SCARD_ERROR_ILLEGAL_STATE;
306 int ClientChannel::transmitSync(const ByteArray &command, ByteArray &result)
307 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
308 ErrorIllegalParameter &, ErrorSecurity &)
310 int rv = SCARD_ERROR_OK;
312 if (getSession()->getReader()->isSecureElementPresent() == true)
315 GVariant *var_command = NULL, *var_response = NULL;
316 GError *error = NULL;
318 var_command = GDBusHelper::convertByteArrayToVariant(command);
320 if (smartcard_service_channel_call_transmit_sync(
321 (SmartcardServiceChannel *)proxy,
322 GPOINTER_TO_UINT(context),
323 GPOINTER_TO_UINT(handle),
324 var_command, &rv, &var_response,
325 NULL, &error) == true) {
327 if (rv == SCARD_ERROR_OK) {
328 GDBusHelper::convertVariantToByteArray(var_response, result);
330 _ERR("smartcard_service_session_call_get_atr_sync failed, [%d]", rv);
334 _ERR("smartcard_service_session_call_get_atr_sync failed, [%s]", error->message);
337 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
341 #ifdef CLIENT_IPC_THREAD
342 /* send message to server */
343 msg.message = Message::MSG_REQUEST_TRANSMIT;
344 msg.param1 = (unsigned long)handle;
347 msg.error = (unsigned long)context; /* using error to context */
348 msg.caller = (void *)this;
349 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
352 if (ClientIPC::getInstance().sendMessage(msg) == true)
354 rv = waitTimedCondition(0);
362 _ERR("timeout [%d]", rv);
363 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
368 _ERR("sendMessage failed");
372 if (this->error != SCARD_ERROR_OK)
374 ThrowError::throwError(this->error);
381 _ERR("unavailable channel");
382 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
388 int ClientChannel::transmit(const ByteArray &command, transmitCallback callback, void *userParam)
392 if (getSession()->getReader()->isSecureElementPresent() == true)
395 GVariant *var_command;
396 CallbackParam *param = new CallbackParam();
398 param->instance = this;
399 param->callback = (void *)callback;
400 param->user_param = userParam;
402 var_command = GDBusHelper::convertByteArrayToVariant(command);
404 smartcard_service_channel_call_transmit(
405 (SmartcardServiceChannel *)proxy,
406 GPOINTER_TO_UINT(context),
407 GPOINTER_TO_UINT(handle),
409 &ClientChannel::channel_transmit_cb, param);
411 result = SCARD_ERROR_OK;
415 /* send message to server */
416 msg.message = Message::MSG_REQUEST_TRANSMIT;
417 msg.param1 = (unsigned long)handle;
420 msg.error = (unsigned long)context; /* using error to context */
421 msg.caller = (void *)this;
422 msg.callback = (void *)callback;
423 msg.userParam = userParam;
425 if (ClientIPC::getInstance().sendMessage(msg) == true)
427 result = SCARD_ERROR_OK;
431 _ERR("sendMessage failed");
432 result = SCARD_ERROR_IPC_FAILED;
438 _ERR("unavailable channel");
439 result = SCARD_ERROR_ILLEGAL_STATE;
446 bool ClientChannel::dispatcherCallback(void *message)
448 Message *msg = (Message *)message;
449 ClientChannel *channel = NULL;
454 _ERR("message is null");
458 channel = (ClientChannel *)msg->caller;
460 switch (msg->message)
462 case Message::MSG_REQUEST_TRANSMIT :
464 /* transmit result */
465 _INFO("MSG_REQUEST_TRANSMIT");
467 if (msg->isSynchronousCall() == true) /* synchronized call */
473 channel->error = msg->error;
474 channel->response = msg->data;
476 channel->signalCondition();
477 channel->syncUnlock();
479 else if (msg->callback != NULL)
481 transmitCallback cb = (transmitCallback)msg->callback;
484 cb(msg->data.getBuffer(),
492 case Message::MSG_REQUEST_CLOSE_CHANNEL :
494 _INFO("MSG_REQUEST_CLOSE_CHANNEL");
496 if (msg->isSynchronousCall() == true) /* synchronized call */
501 channel->error = msg->error;
503 channel->signalCondition();
504 channel->syncUnlock();
506 else if (msg->callback != NULL)
508 closeChannelCallback cb = (closeChannelCallback)msg->callback;
511 cb(msg->error, msg->userParam);
517 _DBG("Unknown message : %s", msg->toString().c_str());
525 #endif /* USE_GDBUS */
526 } /* namespace smartcard_service_api */
529 #define CHANNEL_EXTERN_BEGIN \
530 if (handle != NULL) \
532 ClientChannel *channel = (ClientChannel *)handle;
534 #define CHANNEL_EXTERN_END \
538 _ERR("Invalid param"); \
541 using namespace smartcard_service_api;
543 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
547 CHANNEL_EXTERN_BEGIN;
548 result = channel->close((closeChannelCallback)callback, userParam);
554 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
555 unsigned int length, channel_transmit_cb callback, void *userParam)
559 CHANNEL_EXTERN_BEGIN;
562 temp.assign(command, length);
563 result = channel->transmit(temp, (transmitCallback)callback, userParam);
569 EXTERN_API void channel_close_sync(channel_h handle)
571 #ifdef CLIENT_IPC_THREAD
572 CHANNEL_EXTERN_BEGIN;
575 channel->closeSync();
584 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
585 unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
589 #ifdef CLIENT_IPC_THREAD
590 if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
593 CHANNEL_EXTERN_BEGIN;
594 ByteArray temp, resp;
596 temp.assign(command, cmd_len);
600 result = channel->transmitSync(temp, resp);
603 *resp_len = resp.size();
604 *response = (unsigned char *)calloc(1, *resp_len);
605 memcpy(*response, resp.getBuffer(), *resp_len);
618 EXTERN_API bool channel_is_basic_channel(channel_h handle)
622 CHANNEL_EXTERN_BEGIN;
623 result = channel->isBasicChannel();
629 EXTERN_API bool channel_is_closed(channel_h handle)
633 CHANNEL_EXTERN_BEGIN;
634 result = channel->isClosed();
640 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
642 unsigned int result = 0;
644 CHANNEL_EXTERN_BEGIN;
645 result = channel->getSelectResponse().size();
651 EXTERN_API bool channel_get_select_response(channel_h handle,
652 unsigned char *buffer, unsigned int length)
656 if (buffer == NULL || length == 0)
661 CHANNEL_EXTERN_BEGIN;
664 response = channel->getSelectResponse();
665 if (response.size() > 0)
667 memcpy(buffer, response.getBuffer(), MIN(length, response.size()));
675 EXTERN_API session_h channel_get_session(channel_h handle)
677 session_h session = NULL;
679 CHANNEL_EXTERN_BEGIN;
680 session = channel->getSession();
686 EXTERN_API void channel_destroy_instance(channel_h handle)