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 */
23 /* SLP library header */
27 #include "ClientChannel.h"
28 #include "ReaderHelper.h"
29 #include "APDUHelper.h"
30 #include "ClientGDBus.h"
33 #define EXTERN_API __attribute__((visibility("default")))
36 namespace smartcard_service_api
38 ClientChannel::ClientChannel(void *context, Session *session,
39 int channelNum, const ByteArray &selectResponse, void *handle)
42 this->channelNum = -1;
48 _ERR("ClientIPC::getInstance() failed");
53 this->channelNum = channelNum;
54 this->handle = handle;
55 this->selectResponse = selectResponse;
56 this->context = context;
58 /* init default context */
61 proxy = smartcard_service_channel_proxy_new_for_bus_sync(
62 G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
63 "org.tizen.SmartcardService",
64 "/org/tizen/SmartcardService/Channel",
68 _ERR("Can not create proxy : %s", error->message);
74 ClientChannel::~ClientChannel()
79 void ClientChannel::channel_transmit_cb(GObject *source_object,
80 GAsyncResult *res, gpointer user_data)
82 CallbackParam *param = (CallbackParam *)user_data;
83 transmitCallback callback;
85 GVariant *var_response;
89 _INFO("MSG_REQUEST_TRANSMIT");
92 _ERR("null parameter!!!");
96 callback = (transmitCallback)param->callback;
98 if (smartcard_service_channel_call_transmit_finish(
99 SMARTCARD_SERVICE_CHANNEL(source_object),
100 &result, &var_response, res, &error) == true) {
101 if (result == SCARD_ERROR_OK) {
102 GDBusHelper::convertVariantToByteArray(var_response, response);
104 _ERR("smartcard_service_channel_call_transmit failed, [%d]", result);
107 _ERR("smartcard_service_channel_call_transmit failed, [%s]", error->message);
110 result = SCARD_ERROR_IPC_FAILED;
113 if (callback != NULL) {
114 callback(response.getBuffer(),
116 result, param->user_param);
122 void ClientChannel::channel_close_cb(GObject *source_object,
123 GAsyncResult *res, gpointer user_data)
125 CallbackParam *param = (CallbackParam *)user_data;
126 ClientChannel *channel;
127 closeChannelCallback callback;
129 GError *error = NULL;
131 _INFO("MSG_REQUEST_CLOSE_CHANNEL");
134 _ERR("null parameter!!!");
138 channel = (ClientChannel *)param->instance;
139 callback = (closeChannelCallback)param->callback;
141 if (smartcard_service_channel_call_close_channel_finish(
142 SMARTCARD_SERVICE_CHANNEL(source_object),
143 &result, res, &error) == true) {
144 if (result == SCARD_ERROR_OK) {
145 channel->channelNum = -1;
147 _ERR("smartcard_service_channel_call_close_channel failed, [%d]", result);
150 _ERR("smartcard_service_channel_call_close_channel failed, [%s]", error->message);
153 result = SCARD_ERROR_IPC_FAILED;
156 if (callback != NULL) {
157 callback(result, param->user_param);
163 void ClientChannel::closeSync()
164 throw(ExceptionBase &, ErrorIO &, ErrorSecurity &,
165 ErrorIllegalState &, ErrorIllegalParameter &)
167 if (isClosed() == false)
169 if (getSession()->getReader()->isSecureElementPresent() == true)
172 GError *error = NULL;
176 _ERR("dbus proxy is not initialized yet");
177 throw ErrorIllegalState(SCARD_ERROR_NOT_INITIALIZED);
180 if (smartcard_service_channel_call_close_channel_sync(
181 (SmartcardServiceChannel *)proxy,
182 GPOINTER_TO_UINT(context),
183 GPOINTER_TO_UINT(handle),
184 &ret, NULL, &error) == true) {
185 if (ret == SCARD_ERROR_OK) {
188 _ERR("smartcard_service_channel_call_close_channel_sync failed, [%d]", ret);
192 _ERR("smartcard_service_channel_call_close_channel_sync failed, [%s]", error->message);
195 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
200 _INFO("unavailable channel");
205 int ClientChannel::close(closeChannelCallback callback, void *userParam)
207 int result = SCARD_ERROR_OK;
209 if (isClosed() == false)
211 if (getSession()->getReader()->isSecureElementPresent() == true)
213 CallbackParam *param = new CallbackParam();
215 param->instance = this;
216 param->callback = (void *)callback;
217 param->user_param = userParam;
219 smartcard_service_channel_call_close_channel(
220 (SmartcardServiceChannel *)proxy,
221 GPOINTER_TO_UINT(context),
222 GPOINTER_TO_UINT(handle), NULL,
223 &ClientChannel::channel_close_cb, param);
227 _ERR("unavailable channel");
228 result = SCARD_ERROR_ILLEGAL_STATE;
235 int ClientChannel::transmitSync(const ByteArray &command, ByteArray &result)
236 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
237 ErrorIllegalParameter &, ErrorSecurity &)
239 int rv = SCARD_ERROR_OK;
241 if (getSession()->getReader()->isSecureElementPresent() == true)
243 GVariant *var_command = NULL, *var_response = NULL;
244 GError *error = NULL;
246 var_command = GDBusHelper::convertByteArrayToVariant(command);
248 if (smartcard_service_channel_call_transmit_sync(
249 (SmartcardServiceChannel *)proxy,
250 GPOINTER_TO_UINT(context),
251 GPOINTER_TO_UINT(handle),
252 var_command, &rv, &var_response,
253 NULL, &error) == true) {
255 if (rv == SCARD_ERROR_OK) {
256 GDBusHelper::convertVariantToByteArray(var_response, transmitResponse);
257 result = transmitResponse;
259 _ERR("smartcard_service_session_call_transmit failed, [%d]", rv);
263 _ERR("smartcard_service_session_call_transmit failed, [%s]", error->message);
266 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
271 _ERR("unavailable channel");
272 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
278 int ClientChannel::transmit(const ByteArray &command, transmitCallback callback, void *userParam)
282 if (getSession()->getReader()->isSecureElementPresent() == true)
284 GVariant *var_command;
285 CallbackParam *param = new CallbackParam();
287 param->instance = this;
288 param->callback = (void *)callback;
289 param->user_param = userParam;
291 var_command = GDBusHelper::convertByteArrayToVariant(command);
293 smartcard_service_channel_call_transmit(
294 (SmartcardServiceChannel *)proxy,
295 GPOINTER_TO_UINT(context),
296 GPOINTER_TO_UINT(handle),
298 &ClientChannel::channel_transmit_cb, param);
300 result = SCARD_ERROR_OK;
304 _ERR("unavailable channel");
305 result = SCARD_ERROR_ILLEGAL_STATE;
310 } /* namespace smartcard_service_api */
313 #define CHANNEL_EXTERN_BEGIN \
314 if (handle != NULL) \
316 ClientChannel *channel = (ClientChannel *)handle;
318 #define CHANNEL_EXTERN_END \
322 _ERR("Invalid param"); \
325 using namespace smartcard_service_api;
327 EXTERN_API int channel_close_sync(channel_h handle)
329 int result = SCARD_ERROR_OK;
331 CHANNEL_EXTERN_BEGIN;
335 channel->closeSync();
337 catch (ExceptionBase &e)
339 _ERR("Error occur : %s\n", e.what());
340 result = e.getErrorCode();
344 _ERR("Error occur : unknown error\n");
345 result = SCARD_ERROR_UNKNOWN;
353 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
354 unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
356 int result = SCARD_ERROR_OK;
358 if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
359 return SCARD_ERROR_UNKNOWN;
361 CHANNEL_EXTERN_BEGIN;
365 ByteArray temp, resp;
367 temp.assign(command, cmd_len);
368 channel->transmitSync(temp, resp);
372 *response = (unsigned char *)calloc(resp.size(), sizeof(unsigned char));
373 *resp_len = resp.size();
374 memcpy(*response, resp.getBuffer(), *resp_len);
377 catch (ExceptionBase &e)
379 _ERR("Error occur : %s\n", e.what());
380 result = e.getErrorCode();
385 _ERR("Error occur : unknown error\n");
386 result = SCARD_ERROR_UNKNOWN;
395 EXTERN_API int channel_is_basic_channel(channel_h handle, bool* is_basic_channel)
397 int result = SCARD_ERROR_OK;
399 CHANNEL_EXTERN_BEGIN;
403 *is_basic_channel = channel->isBasicChannel();
405 catch (ExceptionBase &e)
407 _ERR("Error occur : %s\n", e.what());
408 result = e.getErrorCode();
412 _ERR("Error occur : unknown error\n");
413 result = SCARD_ERROR_UNKNOWN;
421 EXTERN_API int channel_is_closed(channel_h handle, bool* is_closed)
423 int result = SCARD_ERROR_OK;
425 CHANNEL_EXTERN_BEGIN;
429 *is_closed = channel->isClosed();
431 catch (ExceptionBase &e)
433 _ERR("Error occur : %s\n", e.what());
434 result = e.getErrorCode();
438 _ERR("Error occur : unknown error\n");
439 result = SCARD_ERROR_UNKNOWN;
447 EXTERN_API int channel_get_select_response(channel_h handle,
448 unsigned char **buffer, size_t *length)
450 int result = SCARD_ERROR_OK;
452 CHANNEL_EXTERN_BEGIN;
456 ByteArray response = channel->getSelectResponse();
457 uint8_t* get_buffer = response.getBuffer();
459 *length = response.size();
463 *buffer = (unsigned char *)calloc(*length, sizeof(unsigned char));
464 if(*buffer == NULL || get_buffer == NULL)
467 return SCARD_ERROR_NOT_ENOUGH_RESOURCE;
470 memcpy(*buffer, get_buffer, *length);
473 catch (ExceptionBase &e)
475 _ERR("Error occur : %s\n", e.what());
476 result = e.getErrorCode();
481 _ERR("Error occur : unknown error\n");
482 result = SCARD_ERROR_UNKNOWN;
491 EXTERN_API int channel_get_transmit_response(channel_h handle,
492 unsigned char **buffer, size_t *length)
494 int result = SCARD_ERROR_OK;
496 CHANNEL_EXTERN_BEGIN;
502 response = channel->getTransmitResponse();
504 *length = response.size();
508 *buffer = (unsigned char *)calloc(*length, sizeof(unsigned char));
509 memcpy(*buffer, response.getBuffer(), *length);
512 catch (ExceptionBase &e)
514 _ERR("Error occur : %s\n", e.what());
515 result = e.getErrorCode();
520 _ERR("Error occur : unknown error\n");
521 result = SCARD_ERROR_UNKNOWN;
530 EXTERN_API int channel_get_session(channel_h handle, int *session_handle)
532 int result = SCARD_ERROR_OK;
533 session_h session = NULL;
535 CHANNEL_EXTERN_BEGIN;
539 session = channel->getSession();
540 *session_handle = (long)session;
542 catch (ExceptionBase &e)
544 _ERR("Error occur : %s\n", e.what());
545 result = e.getErrorCode();
549 _ERR("Error occur : unknown error\n");
550 result = SCARD_ERROR_UNKNOWN;
558 EXTERN_API int channel_select_next(channel_h handle, bool *pSuccess)
560 int result = SCARD_ERROR_OK;
562 CHANNEL_EXTERN_BEGIN;
566 *pSuccess = channel->selectNext();
568 catch (ExceptionBase &e)
570 _ERR("Error occur : %s\n", e.what());
571 result = e.getErrorCode();
575 _ERR("Error occur : unknown error\n");
576 result = SCARD_ERROR_UNKNOWN;
584 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
586 unsigned int result = 0;
588 CHANNEL_EXTERN_BEGIN;
589 result = channel->getSelectResponse().size();
595 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
599 CHANNEL_EXTERN_BEGIN;
600 result = channel->close((closeChannelCallback)callback, userParam);
606 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
607 unsigned int length, channel_transmit_cb callback, void *userParam)
611 CHANNEL_EXTERN_BEGIN;
614 temp.assign(command, length);
615 result = channel->transmit(temp, (transmitCallback)callback, userParam);
621 EXTERN_API void channel_destroy_instance(channel_h handle)