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(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
96 if (isSecureElementPresent() == true)
101 #ifdef CLIENT_IPC_THREAD
102 /* request channel handle from server */
103 msg.message = Message::MSG_REQUEST_OPEN_SESSION;
104 msg.param1 = (unsigned long)handle;
105 msg.error = (unsigned long)context; /* using error to context */
106 msg.caller = (void *)this;
107 msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
110 if (ClientIPC::getInstance().sendMessage(&msg) == true)
112 rv = waitTimedCondition(0);
115 SCARD_DEBUG_ERR("time over");
116 this->error = SCARD_ERROR_OPERATION_TIMEOUT;
121 SCARD_DEBUG_ERR("sendMessage failed");
122 this->error = SCARD_ERROR_IPC_FAILED;
126 if (this->error != SCARD_ERROR_OK)
128 ThrowError::throwError(this->error);
134 SCARD_DEBUG_ERR("unavailable reader");
135 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
138 return (Session *)openedSession;
141 int Reader::openSession(openSessionCallback callback, void *userData)
147 if (isSecureElementPresent() == true)
151 /* request channel handle from server */
152 msg.message = Message::MSG_REQUEST_OPEN_SESSION;
153 msg.param1 = (unsigned long)handle;
154 msg.error = (unsigned long)context; /* using error to context */
155 msg.caller = (void *)this;
156 msg.callback = (void *)callback;
157 msg.userParam = userData;
159 if (ClientIPC::getInstance().sendMessage(&msg) == true)
161 result = SCARD_ERROR_OK;
165 SCARD_DEBUG_ERR("sendMessage failed");
166 result = SCARD_ERROR_IPC_FAILED;
171 SCARD_DEBUG_ERR("unavailable reader");
172 result = SCARD_ERROR_ILLEGAL_STATE;
180 bool Reader::dispatcherCallback(void *message)
182 Message *msg = (Message *)message;
190 SCARD_DEBUG_ERR("message is null");
194 reader = (Reader *)msg->caller;
196 switch (msg->message)
198 case Message::MSG_REQUEST_OPEN_SESSION :
200 Session *session = NULL;
202 SCARD_DEBUG("MSG_REQUEST_OPEN_SESSION");
204 if (msg->param1 != 0)
206 /* create new instance of channel */
207 session = new Session(reader->context, reader, (void *)msg->param1);
210 SCARD_DEBUG_ERR("Session creating instance failed");
215 reader->sessions.push_back(session);
218 if (msg->isSynchronousCall() == true) /* synchronized call */
224 reader->error = msg->error;
225 reader->openedSession = session;
226 reader->signalCondition();
228 reader->syncUnlock();
230 else if (msg->callback != NULL)
232 openSessionCallback cb = (openSessionCallback)msg->callback;
235 cb(session, msg->error, msg->userParam);
241 SCARD_DEBUG("unknown [%s]", msg->toString());
250 } /* namespace smartcard_service_api */
253 #define READER_EXTERN_BEGIN \
254 if (handle != NULL) \
256 Reader *reader = (Reader *)handle;
258 #define READER_EXTERN_END \
262 SCARD_DEBUG_ERR("Invalid param"); \
265 using namespace smartcard_service_api;
267 EXTERN_API const char *reader_get_name(reader_h handle)
269 const char *name = NULL;
272 name = reader->getName();
278 EXTERN_API se_service_h reader_get_se_service(reader_h handle)
280 se_service_h service = NULL;
283 service = (se_service_h)reader->getSEService();
289 EXTERN_API bool reader_is_secure_element_present(reader_h handle)
294 result = reader->isSecureElementPresent();
300 EXTERN_API int reader_open_session(reader_h handle, reader_open_session_cb callback, void *userData)
305 result = reader->openSession((openSessionCallback)callback, userData);
311 EXTERN_API session_h reader_open_session_sync(reader_h handle)
313 session_h result = NULL;
315 #ifdef CLIENT_IPC_THREAD
317 result = (session_h)reader->openSessionSync();
324 EXTERN_API void reader_close_sessions(reader_h handle)
327 reader->closeSessions();
331 EXTERN_API void reader_destroy_instance(reader_h handle)