Change some indent for Tizen 3.0 Coding rule
[platform/core/api/smartcard.git] / src / Reader.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /* standard library header */
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "Reader.h"
27 #include "Session.h"
28 #include "ClientGDBus.h"
29
30 #ifndef EXTERN_API
31 #define EXTERN_API __attribute__((visibility("default")))
32 #endif
33
34 namespace smartcard_service_api
35 {
36         Reader::Reader(void *context, const char *name, void *handle) :
37                 ReaderHelper(name), context(context), handle(handle)
38         {
39                 _BEGIN();
40
41                 if (context == NULL || handle == NULL)
42                 {
43                         _ERR("invalid param");
44
45                         return;
46                 }
47
48                 /* init default context */
49                 GError *error = NULL;
50
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",
55                         NULL, &error);
56                 if (proxy == NULL)
57                 {
58                         _ERR("Can not create proxy : %s", error->message);
59                         g_error_free(error);
60                         return;
61                 }
62
63                 present = true;
64
65                 _END();
66         }
67
68         Reader::~Reader()
69         {
70                 size_t i;
71
72                 closeSessions();
73
74                 for (i = 0; i < sessions.size(); i++)
75                 {
76                         delete (Session *)sessions[i];
77                 }
78
79                 sessions.clear();
80         }
81
82         void Reader::closeSessions()
83                 throw(ErrorIO &, ErrorIllegalState &)
84         {
85                 size_t i;
86
87                 for (i = 0; i < sessions.size(); i++)
88                 {
89                         sessions[i]->closeSync();
90                 }
91         }
92
93         SessionHelper *Reader::openSessionSync()
94                 throw(ExceptionBase &, ErrorIO &, ErrorIllegalState &,
95                         ErrorIllegalParameter &, ErrorSecurity &)
96         {
97                 Session *session = NULL;
98
99                 if (isSecureElementPresent() == true) {
100                         gint result;
101                         GError *error = NULL;
102                         guint session_id;
103
104                         if (smartcard_service_reader_call_open_session_sync(
105                                 (SmartcardServiceReader *)proxy,
106                                 GPOINTER_TO_UINT(context),
107                                 GPOINTER_TO_UINT(handle),
108                                 &result, &session_id, NULL, &error) == true) {
109                                 if (result == SCARD_ERROR_OK) {
110                                         /* create new instance of channel */
111                                         session = new Session(context, this,
112                                                 GUINT_TO_POINTER(session_id));
113                                         if (session != NULL) {
114                                                 sessions.push_back(session);
115                                         } else {
116                                                 _ERR("Session creating instance failed");
117
118                                                 THROW_ERROR(SCARD_ERROR_OUT_OF_MEMORY);
119                                         }
120                                 } else {
121                                         _ERR("smartcard_service_reader_call_open_session_sync failed, [%d]", result);
122
123                                         THROW_ERROR(result);
124                                 }
125                         } else {
126                                 _ERR("smartcard_service_reader_call_open_session_sync failed, [%s]", error->message);
127                                 g_error_free(error);
128
129                                 THROW_ERROR(SCARD_ERROR_IPC_FAILED);
130                         }
131                 } else {
132                         _ERR("unavailable reader");
133                         throw ErrorIllegalState(SCARD_ERROR_UNAVAILABLE);
134                 }
135
136                 return session;
137         }
138
139         void Reader::reader_open_session_cb(GObject *source_object,
140                 GAsyncResult *res, gpointer user_data)
141         {
142                 CallbackParam *param = (CallbackParam *)user_data;
143                 Reader *reader;
144                 openSessionCallback callback;
145                 Session *session = NULL;
146                 gint result;
147                 guint handle;
148                 GError *error = NULL;
149
150                 _INFO("MSG_REQUEST_OPEN_SESSION");
151
152                 if (param == NULL) {
153                         _ERR("null parameter!!!");
154                         return;
155                 }
156
157                 reader = (Reader *)param->instance;
158                 callback = (openSessionCallback)param->callback;
159
160                 if (smartcard_service_reader_call_open_session_finish(
161                         SMARTCARD_SERVICE_READER(source_object),
162                         &result, &handle, res, &error) == true) {
163                         if (result == SCARD_ERROR_OK) {
164                                 /* create new instance of channel */
165                                 session = new Session(reader->context, reader,
166                                         GUINT_TO_POINTER(handle));
167                                 if (session != NULL) {
168                                         reader->sessions.push_back(session);
169                                 } else {
170                                         _ERR("Session creating instance failed");
171
172                                         result = SCARD_ERROR_OUT_OF_MEMORY;
173                                 }
174                         } else {
175                                 _ERR("smartcard_service_reader_call_open_session failed, [%d]", result);
176                         }
177                 } else {
178                         _ERR("smartcard_service_reader_call_open_session failed, [%s]", error->message);
179                         g_error_free(error);
180
181                         result = SCARD_ERROR_IPC_FAILED;
182                 }
183
184                 if (callback != NULL) {
185                         callback(session, result, param->user_param);
186                 }
187
188                 delete param;
189         }
190         int Reader::openSession(openSessionCallback callback, void *userData)
191         {
192                 int result;
193
194                 _BEGIN();
195
196                 if (isSecureElementPresent() == true) {
197                         CallbackParam *param = new CallbackParam();
198
199                         param->instance = this;
200                         param->callback = (void *)callback;
201                         param->user_param = userData;
202
203                         smartcard_service_reader_call_open_session(
204                                 (SmartcardServiceReader *)proxy,
205                                 GPOINTER_TO_UINT(context),
206                                 GPOINTER_TO_UINT(handle),
207                                 NULL, &Reader::reader_open_session_cb, param);
208
209                         result = SCARD_ERROR_OK;
210                 } else {
211                         _ERR("unavailable reader");
212                         result = SCARD_ERROR_ILLEGAL_STATE;
213                 }
214
215                 _END();
216
217                 return result;
218         }
219 } /* namespace smartcard_service_api */
220
221 /* export C API */
222 #define READER_EXTERN_BEGIN \
223         if (handle != NULL) \
224         { \
225                 Reader *reader = (Reader *)handle;
226
227 #define READER_EXTERN_END \
228         } \
229         else \
230         { \
231                 _ERR("Invalid param"); \
232         }
233
234 using namespace smartcard_service_api;
235
236 EXTERN_API int reader_get_name(reader_h handle, char** reader_name)
237 {
238         int result = SCARD_ERROR_OK;
239
240         READER_EXTERN_BEGIN;
241
242         try
243         {
244                 *reader_name = g_strdup(reader->getName());
245         }
246         catch (ExceptionBase &e)
247         {
248                 _ERR("Error occur : %s\n", e.what());
249                 result = e.getErrorCode();
250         }
251         catch (...)
252         {
253                 _ERR("Error occur : unknown error\n");
254                 result = SCARD_ERROR_UNKNOWN;
255         }
256
257         READER_EXTERN_END;
258
259         return result;
260 }
261
262 EXTERN_API  int reader_is_secure_element_present(reader_h handle, bool* is_present)
263 {
264         int result = SCARD_ERROR_OK;
265
266         READER_EXTERN_BEGIN;
267
268         try
269         {
270                 *is_present = reader->isSecureElementPresent();
271         }
272         catch (...)
273         {
274                 _ERR("Error occur : unknown error\n");
275                 result = SCARD_ERROR_UNKNOWN;
276         }
277
278         READER_EXTERN_END;
279
280         return result;
281 }
282
283 EXTERN_API int reader_open_session_sync(reader_h handle, int *session_handle)
284 {
285         session_h session;
286         int result = SCARD_ERROR_OK;
287
288         READER_EXTERN_BEGIN;
289
290         try
291         {
292                 session = (session_h)reader->openSessionSync();
293                 *session_handle = (long)session;
294         }
295         catch (ExceptionBase &e)
296         {
297                 _ERR("Error occur : %s\n", e.what());
298                 result = e.getErrorCode();
299                 *session_handle = 0;
300         }
301         catch (...)
302         {
303                 _ERR("Error occur : unknown error\n");
304                 result = SCARD_ERROR_UNKNOWN;
305                 *session_handle = 0;
306         }
307
308         READER_EXTERN_END;
309
310         return result;
311 }
312
313 EXTERN_API int reader_close_sessions(reader_h handle)
314 {
315         int result = SCARD_ERROR_OK;
316
317         READER_EXTERN_BEGIN;
318
319         try
320         {
321                 reader->closeSessions();
322         }
323         catch (ExceptionBase &e)
324         {
325                 _ERR("Error occur : %s\n", e.what());
326                 result = e.getErrorCode();
327         }
328         catch (...)
329         {
330                 _ERR("Error occur : unknown error\n");
331                 result = SCARD_ERROR_UNKNOWN;
332         }
333
334         READER_EXTERN_END;
335
336         return result;
337 }
338
339
340 EXTERN_API se_service_h reader_get_se_service(reader_h handle)
341 {
342         se_service_h service = NULL;
343
344         READER_EXTERN_BEGIN;
345         service = (se_service_h)reader->getSEService();
346         READER_EXTERN_END;
347
348         return service;
349 }
350
351 EXTERN_API int reader_open_session(reader_h handle, reader_open_session_cb callback, void *userData)
352 {
353         int result = -1;
354
355         READER_EXTERN_BEGIN;
356         result = reader->openSession((openSessionCallback)callback, userData);
357         READER_EXTERN_END;
358
359         return result;
360 }
361
362 EXTERN_API void reader_destroy_instance(reader_h handle)
363 {
364 }