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 /* init default context */
51 proxy = smartcard_service_reader_proxy_new_for_bus_sync(
52 G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
53 "org.tizen.SmartcardService",
54 "/org/tizen/SmartcardService/Reader",
58 _ERR("Can not create proxy : %s", error->message);
74 for (i = 0; i < sessions.size(); i++)
76 delete (Session *)sessions[i];
82 void Reader::closeSessions()
83 throw(ErrorIO &, ErrorIllegalState &)
87 for (i = 0; i < sessions.size(); i++)
89 sessions[i]->closeSync();
93 SessionHelper *Reader::openSessionSync()
94 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
95 ErrorIllegalParameter &, ErrorSecurity &)
97 Session *session = NULL;
99 if (isSecureElementPresent() == true)
102 GError *error = NULL;
105 if (smartcard_service_reader_call_open_session_sync(
106 (SmartcardServiceReader *)proxy,
107 GPOINTER_TO_UINT(context),
108 GPOINTER_TO_UINT(handle),
109 &result, &session_id, NULL, &error) == true) {
110 if (result == SCARD_ERROR_OK) {
111 /* create new instance of channel */
112 session = new Session(context, this,
113 GUINT_TO_POINTER(session_id));
114 if (session != NULL) {
115 sessions.push_back(session);
117 _ERR("Session creating instance failed");
119 THROW_ERROR(SCARD_ERROR_OUT_OF_MEMORY);
122 _ERR("smartcard_service_reader_call_open_session_sync failed, [%d]", result);
127 _ERR("smartcard_service_reader_call_open_session_sync failed, [%s]", error->message);
130 THROW_ERROR(SCARD_ERROR_IPC_FAILED);
135 _ERR("unavailable reader");
136 throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
142 void Reader::reader_open_session_cb(GObject *source_object,
143 GAsyncResult *res, gpointer user_data)
145 CallbackParam *param = (CallbackParam *)user_data;
147 openSessionCallback callback;
148 Session *session = NULL;
151 GError *error = NULL;
153 _INFO("MSG_REQUEST_OPEN_SESSION");
156 _ERR("null parameter!!!");
160 reader = (Reader *)param->instance;
161 callback = (openSessionCallback)param->callback;
163 if (smartcard_service_reader_call_open_session_finish(
164 SMARTCARD_SERVICE_READER(source_object),
165 &result, &handle, res, &error) == true) {
166 if (result == SCARD_ERROR_OK) {
167 /* create new instance of channel */
168 session = new Session(reader->context, reader,
169 GUINT_TO_POINTER(handle));
170 if (session != NULL) {
171 reader->sessions.push_back(session);
173 _ERR("Session creating instance failed");
175 result = SCARD_ERROR_OUT_OF_MEMORY;
178 _ERR("smartcard_service_reader_call_open_session failed, [%d]", result);
181 _ERR("smartcard_service_reader_call_open_session failed, [%s]", error->message);
184 result = SCARD_ERROR_IPC_FAILED;
187 if (callback != NULL) {
188 callback(session, result, param->user_param);
193 int Reader::openSession(openSessionCallback callback, void *userData)
199 if (isSecureElementPresent() == true)
201 CallbackParam *param = new CallbackParam();
203 param->instance = this;
204 param->callback = (void *)callback;
205 param->user_param = userData;
207 smartcard_service_reader_call_open_session(
208 (SmartcardServiceReader *)proxy,
209 GPOINTER_TO_UINT(context),
210 GPOINTER_TO_UINT(handle),
211 NULL, &Reader::reader_open_session_cb, param);
213 result = SCARD_ERROR_OK;
217 _ERR("unavailable reader");
218 result = SCARD_ERROR_ILLEGAL_STATE;
225 } /* namespace smartcard_service_api */
228 #define READER_EXTERN_BEGIN \
229 if (handle != NULL) \
231 Reader *reader = (Reader *)handle;
233 #define READER_EXTERN_END \
237 _ERR("Invalid param"); \
240 using namespace smartcard_service_api;
242 EXTERN_API int reader_get_name(reader_h handle, char** reader_name)
244 int result = SCARD_ERROR_OK;
250 *reader_name = g_strdup(reader->getName());
252 catch (ExceptionBase &e)
254 _ERR("Error occur : %s\n", e.what());
255 result = e.getErrorCode();
259 _ERR("Error occur : unknown error\n");
260 result = SCARD_ERROR_UNKNOWN;
268 EXTERN_API int reader_is_secure_element_present(reader_h handle, bool* is_present)
270 int result = SCARD_ERROR_OK;
276 *is_present = reader->isSecureElementPresent();
280 _ERR("Error occur : unknown error\n");
281 result = SCARD_ERROR_UNKNOWN;
289 EXTERN_API int reader_open_session_sync(reader_h handle, int *session_handle)
292 int result = SCARD_ERROR_OK;
298 session = (session_h)reader->openSessionSync();
299 //*session_handle = (int)session;
301 catch (ExceptionBase &e)
303 _ERR("Error occur : %s\n", e.what());
304 result = e.getErrorCode();
309 _ERR("Error occur : unknown error\n");
310 result = SCARD_ERROR_UNKNOWN;
319 EXTERN_API int reader_close_sessions(reader_h handle)
321 int result = SCARD_ERROR_OK;
327 reader->closeSessions();
329 catch (ExceptionBase &e)
331 _ERR("Error occur : %s\n", e.what());
332 result = e.getErrorCode();
336 _ERR("Error occur : unknown error\n");
337 result = SCARD_ERROR_UNKNOWN;
346 EXTERN_API se_service_h reader_get_se_service(reader_h handle)
348 se_service_h service = NULL;
351 service = (se_service_h)reader->getSEService();
357 EXTERN_API int reader_open_session(reader_h handle, reader_open_session_cb callback, void *userData)
362 result = reader->openSession((openSessionCallback)callback, userData);
368 EXTERN_API void reader_destroy_instance(reader_h handle)