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 */
28 #include "ClientGDBus.h"
31 #define EXTERN_API __attribute__((visibility("default")))
34 namespace smartcard_service_api
36 Reader::Reader(void *context, const char *name, void *handle) :
37 ReaderHelper(name), context(context), handle(handle)
41 if (context == NULL || handle == NULL)
43 _ERR("invalid param");
48 /* initialize client */
49 if (!g_thread_supported())
56 /* init default context */
59 proxy = smartcard_service_reader_proxy_new_for_bus_sync(
60 G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
61 "org.tizen.SmartcardService",
62 "/org/tizen/SmartcardService/Reader",
66 _ERR("Can not create proxy : %s", error->message);
82 for (i = 0; i < sessions.size(); i++)
84 delete (Session *)sessions[i];
90 void Reader::closeSessions()
91 throw(ErrorIO &, ErrorIllegalState &)
95 for (i = 0; i < sessions.size(); i++)
97 sessions[i]->closeSync();
101 SessionHelper *Reader::openSessionSync()
102 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
103 ErrorIllegalParameter &, ErrorSecurity &)
105 Session *session = NULL;
107 if (isSecureElementPresent() == true)
110 GError *error = NULL;
113 if (smartcard_service_reader_call_open_session_sync(
114 (SmartcardServiceReader *)proxy,
115 GPOINTER_TO_UINT(context),
116 GPOINTER_TO_UINT(handle),
117 &result, &session_id, NULL, &error) == true) {
118 if (result == SCARD_ERROR_OK) {
119 /* create new instance of channel */
120 session = new Session(context, this,
121 GUINT_TO_POINTER(session_id));
122 if (session != NULL) {
123 sessions.push_back(session);
125 _ERR("Session creating instance failed");
127 THROW_ERROR(SCARD_ERROR_OUT_OF_MEMORY);
130 _ERR("smartcard_service_reader_call_open_session_sync failed, [%d]", result);
135 _ERR("smartcard_service_reader_call_open_session_sync failed, [%s]", error->message);
138 THROW_ERROR(SCARD_ERROR_IPC_FAILED);
143 _ERR("unavailable reader");
144 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
150 void Reader::reader_open_session_cb(GObject *source_object,
151 GAsyncResult *res, gpointer user_data)
153 CallbackParam *param = (CallbackParam *)user_data;
155 openSessionCallback callback;
156 Session *session = NULL;
159 GError *error = NULL;
161 _INFO("MSG_REQUEST_OPEN_SESSION");
164 _ERR("null parameter!!!");
168 reader = (Reader *)param->instance;
169 callback = (openSessionCallback)param->callback;
171 if (smartcard_service_reader_call_open_session_finish(
172 SMARTCARD_SERVICE_READER(source_object),
173 &result, &handle, res, &error) == true) {
174 if (result == SCARD_ERROR_OK) {
175 /* create new instance of channel */
176 session = new Session(reader->context, reader,
177 GUINT_TO_POINTER(handle));
178 if (session != NULL) {
179 reader->sessions.push_back(session);
181 _ERR("Session creating instance failed");
183 result = SCARD_ERROR_OUT_OF_MEMORY;
186 _ERR("smartcard_service_reader_call_open_session failed, [%d]", result);
189 _ERR("smartcard_service_reader_call_open_session failed, [%s]", error->message);
192 result = SCARD_ERROR_IPC_FAILED;
195 if (callback != NULL) {
196 callback(session, result, param->user_param);
201 int Reader::openSession(openSessionCallback callback, void *userData)
207 if (isSecureElementPresent() == true)
209 CallbackParam *param = new CallbackParam();
211 param->instance = this;
212 param->callback = (void *)callback;
213 param->user_param = userData;
215 smartcard_service_reader_call_open_session(
216 (SmartcardServiceReader *)proxy,
217 GPOINTER_TO_UINT(context),
218 GPOINTER_TO_UINT(handle),
219 NULL, &Reader::reader_open_session_cb, param);
221 result = SCARD_ERROR_OK;
225 _ERR("unavailable reader");
226 result = SCARD_ERROR_ILLEGAL_STATE;
233 } /* namespace smartcard_service_api */
236 #define READER_EXTERN_BEGIN \
237 if (handle != NULL) \
239 Reader *reader = (Reader *)handle;
241 #define READER_EXTERN_END \
245 _ERR("Invalid param"); \
248 using namespace smartcard_service_api;
250 EXTERN_API const char *reader_get_name(reader_h handle)
252 const char *name = NULL;
255 name = reader->getName();
261 EXTERN_API se_service_h reader_get_se_service(reader_h handle)
263 se_service_h service = NULL;
266 service = (se_service_h)reader->getSEService();
272 EXTERN_API bool reader_is_secure_element_present(reader_h handle)
277 result = reader->isSecureElementPresent();
283 EXTERN_API int reader_open_session(reader_h handle, reader_open_session_cb callback, void *userData)
288 result = reader->openSession((openSessionCallback)callback, userData);
294 EXTERN_API session_h reader_open_session_sync(reader_h handle)
296 session_h result = NULL;
299 result = (session_h)reader->openSessionSync();
305 EXTERN_API void reader_close_sessions(reader_h handle)
308 reader->closeSessions();
312 EXTERN_API void reader_destroy_instance(reader_h handle)