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;
175 _ERR("dbus proxy is not initialized yet");
176 throw ErrorIllegalState(SCARD_ERROR_NOT_INITIALIZED);
179 if (smartcard_service_channel_call_close_channel_sync(
180 (SmartcardServiceChannel *)proxy,
181 GPOINTER_TO_UINT(context),
182 GPOINTER_TO_UINT(handle),
183 &ret, NULL, &error) == true) {
184 if (ret != SCARD_ERROR_OK) {
185 _ERR("smartcard_service_channel_call_close_channel_sync failed, [%d]", ret);
189 _ERR("smartcard_service_channel_call_close_channel_sync failed, [%s]", error->message);
192 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
197 _INFO("unavailable channel");
202 int ClientChannel::close(closeChannelCallback callback, void *userParam)
204 int result = SCARD_ERROR_OK;
206 if (isClosed() == false)
208 if (getSession()->getReader()->isSecureElementPresent() == true)
210 CallbackParam *param = new CallbackParam();
212 param->instance = this;
213 param->callback = (void *)callback;
214 param->user_param = userParam;
216 smartcard_service_channel_call_close_channel(
217 (SmartcardServiceChannel *)proxy,
218 GPOINTER_TO_UINT(context),
219 GPOINTER_TO_UINT(handle), NULL,
220 &ClientChannel::channel_close_cb, param);
224 _ERR("unavailable channel");
225 result = SCARD_ERROR_ILLEGAL_STATE;
232 int ClientChannel::transmitSync(const ByteArray &command, ByteArray &result)
233 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
234 ErrorIllegalParameter &, ErrorSecurity &)
236 int rv = SCARD_ERROR_OK;
238 if (getSession()->getReader()->isSecureElementPresent() == true)
240 GVariant *var_command = NULL, *var_response = NULL;
241 GError *error = NULL;
243 var_command = GDBusHelper::convertByteArrayToVariant(command);
245 if (smartcard_service_channel_call_transmit_sync(
246 (SmartcardServiceChannel *)proxy,
247 GPOINTER_TO_UINT(context),
248 GPOINTER_TO_UINT(handle),
249 var_command, &rv, &var_response,
250 NULL, &error) == true) {
252 if (rv == SCARD_ERROR_OK) {
253 GDBusHelper::convertVariantToByteArray(var_response, result);
255 _ERR("smartcard_service_session_call_get_atr_sync failed, [%d]", rv);
259 _ERR("smartcard_service_session_call_get_atr_sync failed, [%s]", error->message);
262 throw ErrorIO(SCARD_ERROR_IPC_FAILED);
267 _ERR("unavailable channel");
268 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
274 int ClientChannel::transmit(const ByteArray &command, transmitCallback callback, void *userParam)
278 if (getSession()->getReader()->isSecureElementPresent() == true)
280 GVariant *var_command;
281 CallbackParam *param = new CallbackParam();
283 param->instance = this;
284 param->callback = (void *)callback;
285 param->user_param = userParam;
287 var_command = GDBusHelper::convertByteArrayToVariant(command);
289 smartcard_service_channel_call_transmit(
290 (SmartcardServiceChannel *)proxy,
291 GPOINTER_TO_UINT(context),
292 GPOINTER_TO_UINT(handle),
294 &ClientChannel::channel_transmit_cb, param);
296 result = SCARD_ERROR_OK;
300 _ERR("unavailable channel");
301 result = SCARD_ERROR_ILLEGAL_STATE;
306 } /* namespace smartcard_service_api */
309 #define CHANNEL_EXTERN_BEGIN \
310 if (handle != NULL) \
312 ClientChannel *channel = (ClientChannel *)handle;
314 #define CHANNEL_EXTERN_END \
318 _ERR("Invalid param"); \
321 using namespace smartcard_service_api;
323 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
327 CHANNEL_EXTERN_BEGIN;
328 result = channel->close((closeChannelCallback)callback, userParam);
334 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command,
335 unsigned int length, channel_transmit_cb callback, void *userParam)
339 CHANNEL_EXTERN_BEGIN;
342 temp.assign(command, length);
343 result = channel->transmit(temp, (transmitCallback)callback, userParam);
349 EXTERN_API void channel_close_sync(channel_h handle)
351 CHANNEL_EXTERN_BEGIN;
354 channel->closeSync();
362 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command,
363 unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
367 if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
370 CHANNEL_EXTERN_BEGIN;
371 ByteArray temp, resp;
373 temp.assign(command, cmd_len);
377 result = channel->transmitSync(temp, resp);
380 *resp_len = resp.size();
381 *response = (unsigned char *)calloc(1, *resp_len);
382 memcpy(*response, resp.getBuffer(), *resp_len);
394 EXTERN_API bool channel_is_basic_channel(channel_h handle)
398 CHANNEL_EXTERN_BEGIN;
399 result = channel->isBasicChannel();
405 EXTERN_API bool channel_is_closed(channel_h handle)
409 CHANNEL_EXTERN_BEGIN;
410 result = channel->isClosed();
416 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
418 unsigned int result = 0;
420 CHANNEL_EXTERN_BEGIN;
421 result = channel->getSelectResponse().size();
427 EXTERN_API bool channel_get_select_response(channel_h handle,
428 unsigned char *buffer, unsigned int length)
432 if (buffer == NULL || length == 0)
437 CHANNEL_EXTERN_BEGIN;
440 response = channel->getSelectResponse();
441 if (response.size() > 0)
443 memcpy(buffer, response.getBuffer(), MIN(length, response.size()));
451 EXTERN_API session_h channel_get_session(channel_h handle)
453 session_h session = NULL;
455 CHANNEL_EXTERN_BEGIN;
456 session = channel->getSession();
462 EXTERN_API void channel_destroy_instance(channel_h handle)