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.
18 /* 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 if (isClosed() == false)
67 /* send message to server */
68 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
69 msg.param1 = (int)handle;
70 msg.error = (unsigned int)context; /* using error to context */
71 msg.caller = (void *)this;
72 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
74 ClientIPC::getInstance().sendMessage(&msg);
77 rv = waitTimedCondition(0);
82 SCARD_DEBUG_ERR("closeSync failed [%d]", rv);
89 int ClientChannel::close(closeCallback callback, void *userParam)
93 if (isClosed() == false)
97 /* send message to server */
98 msg.message = Message::MSG_REQUEST_CLOSE_CHANNEL;
99 msg.param1 = (int)handle;
100 msg.error = (unsigned int)context; /* using error to context */
101 msg.caller = (void *)this;
102 msg.callback = (void *)callback;
103 msg.userParam = userParam;
105 ClientIPC::getInstance().sendMessage(&msg);
111 int ClientChannel::transmitSync(ByteArray command, ByteArray &result)
116 /* send message to server */
117 msg.message = Message::MSG_REQUEST_TRANSMIT;
118 msg.param1 = (int)handle;
121 msg.error = (unsigned int)context; /* using error to context */
122 msg.caller = (void *)this;
123 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
125 ClientIPC::getInstance().sendMessage(&msg);
128 rv = waitTimedCondition(0);
133 SCARD_DEBUG_ERR("clientIPC is null");
143 int ClientChannel::transmit(ByteArray command, transmitCallback callback, void *userParam)
147 /* send message to server */
148 msg.message = Message::MSG_REQUEST_TRANSMIT;
149 msg.param1 = (int)handle;
152 msg.error = (unsigned int)context; /* using error to context */
153 msg.caller = (void *)this;
154 msg.callback = (void *)callback;
155 msg.userParam = userParam;
157 ClientIPC::getInstance().sendMessage(&msg);
162 bool ClientChannel::dispatcherCallback(void *message)
164 Message *msg = (Message *)message;
165 ClientChannel *channel = NULL;
170 SCARD_DEBUG_ERR("message is null");
174 channel = (ClientChannel *)msg->caller;
176 switch (msg->message)
178 case Message::MSG_REQUEST_TRANSMIT :
180 /* transmit result */
181 SCARD_DEBUG("MSG_REQUEST_TRANSMIT");
183 if (msg->callback == (void *)channel) /* synchronized call */
189 channel->error = msg->error;
190 channel->response = msg->data;
192 channel->signalCondition();
193 channel->syncUnlock();
195 else if (msg->callback != NULL)
197 transmitCallback cb = (transmitCallback)msg->callback;
200 cb(msg->data.getBuffer(), msg->data.getLength(), msg->error, msg->userParam);
205 case Message::MSG_REQUEST_CLOSE_CHANNEL :
207 SCARD_DEBUG("MSG_REQUEST_CLOSE_CHANNEL");
209 if (msg->callback == (void *)channel) /* synchronized call */
214 channel->error = msg->error;
216 channel->signalCondition();
217 channel->syncUnlock();
219 else if (msg->callback != NULL)
221 closeCallback cb = (closeCallback)msg->callback;
224 cb(msg->error, msg->userParam);
230 SCARD_DEBUG("unknwon message : %s", msg->toString());
238 } /* namespace smartcard_service_api */
241 #define CHANNEL_EXTERN_BEGIN \
242 if (handle != NULL) \
244 ClientChannel *channel = (ClientChannel *)handle;
246 #define CHANNEL_EXTERN_END \
250 SCARD_DEBUG_ERR("Invalid param"); \
253 using namespace smartcard_service_api;
255 EXTERN_API int channel_close(channel_h handle, channel_close_cb callback, void *userParam)
259 CHANNEL_EXTERN_BEGIN;
260 result = channel->close((closeCallback)callback, userParam);
266 EXTERN_API int channel_transmit(channel_h handle, unsigned char *command, unsigned int length, channel_transmit_cb callback, void *userParam)
270 CHANNEL_EXTERN_BEGIN;
273 temp.setBuffer(command, length);
274 result = channel->transmit(temp, (transmitCallback)callback, userParam);
280 EXTERN_API bool channel_is_basic_channel(channel_h handle)
284 CHANNEL_EXTERN_BEGIN;
285 result = channel->isBasicChannel();
291 EXTERN_API bool channel_is_closed(channel_h handle)
295 CHANNEL_EXTERN_BEGIN;
296 result = channel->isClosed();
302 EXTERN_API unsigned int channel_get_select_response_length(channel_h handle)
304 unsigned int result = 0;
306 CHANNEL_EXTERN_BEGIN;
307 result = channel->getSelectResponse().getLength();
313 EXTERN_API bool channel_get_select_response(channel_h handle, unsigned char *buffer, unsigned int length)
317 if (buffer == NULL || length == 0)
322 CHANNEL_EXTERN_BEGIN;
325 response = channel->getSelectResponse();
326 if (response.getLength() > 0)
328 memcpy(buffer, response.getBuffer(), MIN(length, response.getLength()));
336 EXTERN_API session_h channel_get_session(channel_h handle)
338 session_h session = NULL;
340 CHANNEL_EXTERN_BEGIN;
341 session = channel->getSession();
347 EXTERN_API void channel_destroy_instance(channel_h handle)
349 CHANNEL_EXTERN_BEGIN;