2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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"
31 #define EXTERN_API __attribute__((visibility("default")))
34 namespace smartcard_service_api
36 ClientChannel::ClientChannel(void *context, Session *session, int channelNum, ByteArray selectResponse, void *handle):Channel(session)
38 this->channelNum = -1;
44 SCARD_DEBUG_ERR("ClientIPC::getInstance() failed");
49 this->channelNum = channelNum;
50 this->handle = handle;
51 this->selectResponse = selectResponse;
52 this->context = context;
55 ClientChannel::~ClientChannel()
60 void ClientChannel::closeSync()
65 #ifdef CLIENT_IPC_THREAD
66 if (isClosed() == false)
68 /* send message to server */
69 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
70 msg.param1 = (int)handle;
71 msg.error = (unsigned int)context; /* using error to context */
72 msg.caller = (void *)this;
73 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
75 ClientIPC::getInstance().sendMessage(&msg);
78 rv = waitTimedCondition(0);
83 SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
91 int ClientChannel::close(closeCallback callback, void *userParam)
95 if (isClosed() == false)
99 /* send message to server */
100 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
101 msg.param1 = (int)handle;
102 msg.error = (unsigned int)context; /* using error to context */
103 msg.caller = (void *)this;
104 msg.callback = (void *)callback;
105 msg.userParam = userParam;
107 ClientIPC::getInstance().sendMessage(&msg);
113 int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
118 #ifdef CLIENT_IPC_THREAD
119 /* send message to server */
120 msg.message = Message::MSG_REQUEST_TRANSMIT;
121 msg.param1 = (int)handle;
124 msg.error = (unsigned int)context; /* using error to context */
125 msg.caller = (void *)this;
126 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
128 ClientIPC::getInstance().sendMessage(&msg);
131 rv = waitTimedCondition(0);
136 SCARD_DEBUG_ERR("clientIPC is null");
146 int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
150 /* send message to server */
151 msg.message = Message::MSG_REQUEST_TRANSMIT;
152 msg.param1 = (int)handle;
155 msg.error = (unsigned int)context; /* using error to context */
156 msg.caller = (void *)this;
157 msg.callback = (void *)callback;
158 msg.userParam = userParam;
160 ClientIPC::getInstance().sendMessage(&msg);
165 bool ClientChannel::dispatcherCallback(void *message)
167 Message *msg = (Message *)message;
168 ClientChannel *channel = NULL;
173 SCARD_DEBUG_ERR("message is null");
177 channel = (ClientChannel *)msg->caller;
179 switch (msg->message)
181 case Message::MSG_REQUEST_TRANSMIT :
183 /* transmit result */
184 SCARD_DEBUG("MSG_REQUEST_TRANSMIT");
186 if (msg->callback == (void *)channel) /* synchronized call */
192 channel->error = msg->error;
193 channel->response = msg->data;
195 channel->signalCondition();
196 channel->syncUnlock();
198 else if (msg->callback != NULL)
200 transmitCallback cb = (transmitCallback)msg->callback;
203 cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
208 case Message::MSG_REQUEST_CLOSE_CHANNEL :
210 SCARD_DEBUG("MSG_REQUEST_CLOSE_CHANNEL");
212 if (msg->callback == (void *)channel) /* synchronized call */
217 channel->error = msg->error;
219 channel->signalCondition();
220 channel->syncUnlock();
222 else if (msg->callback != NULL)
224 closeCallback cb = (closeCallback)msg->callback;
227 cb(msg->error, msg->userParam);
233 SCARD_DEBUG("unknwon message : %s", msg->toString());
241 } /* namespace smartcard_service_api */
244 #define CHANNEL_EXTERN_BEGIN \
245 if (handle != NULL) \
247 ClientChannel *channel = (ClientChannel *)handle;
249 #define CHANNEL_EXTERN_END \
253 SCARD_DEBUG_ERR("Invalid param"); \
256 using namespace smartcard_service_api;
258 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
262 CHANNEL_EXTERN_BEGIN;
263 result = channel->close((closeCallback)callback, userParam);
269 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command, unsigned int length, channel_transmit_cb callback, void *userParam)
273 CHANNEL_EXTERN_BEGIN;
276 temp.setBuffer(command, length);
277 result = channel->transmit(temp, (transmitCallback)callback, userParam);
283 EXTERN_API void channel_close_sync(channel_h handle)
285 #ifdef CLIENT_IPC_THREAD
286 CHANNEL_EXTERN_BEGIN;
287 channel->closeSync();
292 EXTERN_API int channel_transmit_sync(channel_h handle, unsigned char *command, unsigned int cmd_len, unsigned char **response, unsigned int *resp_len)
296 #ifdef CLIENT_IPC_THREAD
297 if (command == NULL || cmd_len == 0 || response == NULL || resp_len == NULL)
300 CHANNEL_EXTERN_BEGIN;
301 ByteArray temp, resp;
303 temp.setBuffer(command, cmd_len);
304 result = channel->transmitSync(temp, resp);
305 if (resp.getLength() > 0)
307 *resp_len = resp.getLength();
308 *response = (unsigned char *)calloc(1, *resp_len);
309 memcpy(*response, resp.getBuffer(), *resp_len);
317 EXTERN_API bool channel_is_basic_channel(channel_h handle)
321 CHANNEL_EXTERN_BEGIN;
322 result = channel->isBasicChannel();
328 EXTERN_API bool channel_is_closed(channel_h handle)
332 CHANNEL_EXTERN_BEGIN;
333 result = channel->isClosed();
339 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
341 unsigned int result = 0;
343 CHANNEL_EXTERN_BEGIN;
344 result = channel->getSelectResponse().getLength();
350 EXTERN_API bool channel_get_select_response(channel_h handle, unsigned char *buffer, unsigned int length)
354 if (buffer == NULL || length == 0)
359 CHANNEL_EXTERN_BEGIN;
362 response = channel->getSelectResponse();
363 if (response.getLength() > 0)
365 memcpy(buffer, response.getBuffer(), MIN(length, response.getLength()));
373 EXTERN_API session_h channel_get_session(channel_h handle)
375 session_h session = NULL;
377 CHANNEL_EXTERN_BEGIN;
378 session = channel->getSession();
384 EXTERN_API void channel_destroy_instance(channel_h handle)
386 CHANNEL_EXTERN_BEGIN;