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 */
22 /* SLP library header */
27 #include "ClientIPC.h"
32 #define EXTERN_API __attribute__((visibility("default")))
35 namespace smartcard_service_api
37 Reader::Reader(void *context, const char *name, void *handle) : ReaderHelper()
39 unsigned int length = 0;
46 if (context == NULL || name == NULL || strlen(name) == 0 || handle == NULL)
48 SCARD_DEBUG_ERR("invalid param");
53 this->handle = handle;
54 this->context = context;
56 length = strlen(name);
57 length = (length < sizeof(this->name)) ? length : sizeof(this->name);
58 memcpy(this->name, name, length);
71 for (i = 0; i < sessions.size(); i++)
73 delete (Session *)sessions[i];
79 void Reader::closeSessions()
80 throw(ErrorIO &, ErrorIllegalState &)
84 for (i = 0; i < sessions.size(); i++)
86 sessions[i]->closeSync();
90 SessionHelper *Reader::openSessionSync()
91 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
95 if (isSecureElementPresent() == true)
100 #ifdef CLIENT_IPC_THREAD
101 /* request channel handle from server */
102 msg.message = Message::MSG_REQUEST_OPEN_SESSION;
103 msg.param1 = (unsigned int)handle;
104 msg.error = (unsigned int)context; /* using error to context */
105 msg.caller = (void *)this;
106 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
109 if (ClientIPC::getInstance().sendMessage(&msg) == true)
111 rv = waitTimedCondition(0);
114 SCARD_DEBUG_ERR("time over");
119 SCARD_DEBUG_ERR("sendMessage failed");
126 SCARD_DEBUG_ERR("unavailable reader");
129 return (Session *)openedSession;
132 int Reader::openSession(openSessionCallback callback, void *userData)
138 if (isSecureElementPresent() == true)
142 /* request channel handle from server */
143 msg.message = Message::MSG_REQUEST_OPEN_SESSION;
144 msg.param1 = (unsigned int)handle;
145 msg.error = (unsigned int)context; /* using error to context */
146 msg.caller = (void *)this;
147 msg.callback = (void *)callback;
148 msg.userParam = userData;
150 if (ClientIPC::getInstance().sendMessage(&msg) == true)
156 SCARD_DEBUG_ERR("sendMessage failed");
161 SCARD_DEBUG_ERR("unavailable reader");
169 bool Reader::dispatcherCallback(void *message)
171 Message *msg = (Message *)message;
172 Reader *reader = NULL;
179 SCARD_DEBUG_ERR("message is null");
183 reader = (Reader *)msg->caller;
185 switch (msg->message)
187 case Message::MSG_REQUEST_OPEN_SESSION :
189 Session *session = NULL;
191 SCARD_DEBUG("MSG_REQUEST_OPEN_SESSION");
193 if (msg->param1 != 0)
195 /* create new instance of channel */
196 session = new Session(reader->context, reader, (void *)msg->param1);
199 SCARD_DEBUG_ERR("Session creating instance failed");
204 reader->sessions.push_back(session);
207 if (msg->isSynchronousCall() == true) /* synchronized call */
213 reader->error = msg->error;
214 reader->openedSession = session;
215 reader->signalCondition();
217 reader->syncUnlock();
219 else if (msg->callback != NULL)
221 openSessionCallback cb = (openSessionCallback)msg->callback;
224 cb(session, msg->error, msg->userParam);
230 SCARD_DEBUG("unknown [%s]", msg->toString());
239 } /* namespace smartcard_service_api */
242 #define READER_EXTERN_BEGIN \
243 if (handle != NULL) \
245 Reader *reader = (Reader *)handle;
247 #define READER_EXTERN_END \
251 SCARD_DEBUG_ERR("Invalid param"); \
254 using namespace smartcard_service_api;
256 EXTERN_API const char *reader_get_name(reader_h handle)
258 const char *name = NULL;
261 name = reader->getName();
267 EXTERN_API se_service_h reader_get_se_service(reader_h handle)
269 se_service_h service = NULL;
272 service = (se_service_h)reader->getSEService();
278 EXTERN_API bool reader_is_secure_element_present(reader_h handle)
283 result = reader->isSecureElementPresent();
289 EXTERN_API int reader_open_session(reader_h handle, reader_open_session_cb callback, void *userData)
294 result = reader->openSession((openSessionCallback)callback, userData);
300 EXTERN_API session_h reader_open_session_sync(reader_h handle)
302 session_h result = NULL;
304 #ifdef CLIENT_IPC_THREAD
306 result = (session_h)reader->openSessionSync();
313 EXTERN_API void reader_close_sessions(reader_h handle)
316 reader->closeSessions();
320 EXTERN_API void reader_destroy_instance(reader_h handle)