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)
40 unsigned int length = 0;
47 if (context == NULL || name == NULL || strlen(name) == 0 || handle == NULL)
49 SCARD_DEBUG_ERR("invalid param");
54 this->handle = handle;
55 this->context = context;
57 length = strlen(name);
58 length = (length < sizeof(this->name)) ? length : sizeof(this->name);
59 memcpy(this->name, name, length);
72 for (i = 0; i < sessions.size(); i++)
74 delete (Session *)sessions[i];
80 void Reader::closeSessions()
81 throw(ErrorIO &, ErrorIllegalState &)
85 for (i = 0; i < sessions.size(); i++)
87 sessions[i]->closeSync();
91 SessionHelper *Reader::openSessionSync()
92 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
93 ErrorIllegalParameter &, ErrorSecurity &)
97 if (isSecureElementPresent() == true)
102 #ifdef CLIENT_IPC_THREAD
103 /* request channel handle from server */
104 msg.message = Message::MSG_REQUEST_OPEN_SESSION;
105 msg.param1 = (unsigned long)handle;
106 msg.error = (unsigned long)context; /* using error to context */
107 msg.caller = (void *)this;
108 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
111 if (ClientIPC::getInstance().sendMessage(&msg) == true)
113 rv = waitTimedCondition(0);
116 SCARD_DEBUG_ERR("time over");
117 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
122 SCARD_DEBUG_ERR("sendMessage failed");
123 this->error = SCARD_ERROR_IPC_FAILED;
127 if (this->error != SCARD_ERROR_OK)
129 ThrowError::throwError(this->error);
135 SCARD_DEBUG_ERR("unavailable reader");
136 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
139 return (Session *)openedSession;
142 int Reader::openSession(openSessionCallback callback, void *userData)
148 if (isSecureElementPresent() == true)
152 /* request channel handle from server */
153 msg.message = Message::MSG_REQUEST_OPEN_SESSION;
154 msg.param1 = (unsigned long)handle;
155 msg.error = (unsigned long)context; /* using error to context */
156 msg.caller = (void *)this;
157 msg.callback = (void *)callback;
158 msg.userParam = userData;
160 if (ClientIPC::getInstance().sendMessage(&msg) == true)
162 result = SCARD_ERROR_OK;
166 SCARD_DEBUG_ERR("sendMessage failed");
167 result = SCARD_ERROR_IPC_FAILED;
172 SCARD_DEBUG_ERR("unavailable reader");
173 result = SCARD_ERROR_ILLEGAL_STATE;
181 bool Reader::dispatcherCallback(void *message)
183 Message *msg = (Message *)message;
191 SCARD_DEBUG_ERR("message is null");
195 reader = (Reader *)msg->caller;
197 switch (msg->message)
199 case Message::MSG_REQUEST_OPEN_SESSION :
201 Session *session = NULL;
203 SCARD_DEBUG("MSG_REQUEST_OPEN_SESSION");
205 if (msg->param1 != 0)
207 /* create new instance of channel */
208 session = new Session(reader->context, reader, (void *)msg->param1);
211 SCARD_DEBUG_ERR("Session creating instance failed");
216 reader->sessions.push_back(session);
219 if (msg->isSynchronousCall() == true) /* synchronized call */
225 reader->error = msg->error;
226 reader->openedSession = session;
227 reader->signalCondition();
229 reader->syncUnlock();
231 else if (msg->callback != NULL)
233 openSessionCallback cb = (openSessionCallback)msg->callback;
236 cb(session, msg->error, msg->userParam);
242 SCARD_DEBUG("unknown [%s]", msg->toString());
251 } /* namespace smartcard_service_api */
254 #define READER_EXTERN_BEGIN \
255 if (handle != NULL) \
257 Reader *reader = (Reader *)handle;
259 #define READER_EXTERN_END \
263 SCARD_DEBUG_ERR("Invalid param"); \
266 using namespace smartcard_service_api;
268 EXTERN_API const char *reader_get_name(reader_h handle)
270 const char *name = NULL;
273 name = reader->getName();
279 EXTERN_API se_service_h reader_get_se_service(reader_h handle)
281 se_service_h service = NULL;
284 service = (se_service_h)reader->getSEService();
290 EXTERN_API bool reader_is_secure_element_present(reader_h handle)
295 result = reader->isSecureElementPresent();
301 EXTERN_API int reader_open_session(reader_h handle, reader_open_session_cb callback, void *userData)
306 result = reader->openSession((openSessionCallback)callback, userData);
312 EXTERN_API session_h reader_open_session_sync(reader_h handle)
314 session_h result = NULL;
316 #ifdef CLIENT_IPC_THREAD
318 result = (session_h)reader->openSessionSync();
325 EXTERN_API void reader_close_sessions(reader_h handle)
328 reader->closeSessions();
332 EXTERN_API void reader_destroy_instance(reader_h handle)