Sync code with Tizen 3.0 branch
[platform/core/api/nfc.git] / src / net_nfc_client_se.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include "vconf.h"
18
19 #include "net_nfc_typedef_internal.h"
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_util_ndef_message.h"
23 #include "net_nfc_util_gdbus_internal.h"
24 #include "net_nfc_gdbus.h"
25 #include "net_nfc_client.h"
26 #include "net_nfc_client_util_internal.h"
27 #include "net_nfc_client_manager.h"
28 #include "net_nfc_client_se.h"
29
30
31 #ifndef NET_NFC_EXPORT_API
32 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
33 #endif
34
35 /* LCOV_EXCL_START */
36
37 typedef struct _SeFuncData SeFuncData;
38
39 struct _SeFuncData {
40         gpointer se_callback;
41         gpointer se_data;
42 };
43
44 typedef struct _SeEventHandler SeEventHandler;
45
46 struct _SeEventHandler {
47         net_nfc_client_se_event se_event_cb;
48         gpointer se_event_data;
49 };
50
51 typedef struct _SeTransEventHandler SeTransEventHandler;
52
53 struct _SeTransEventHandler {
54         net_nfc_client_se_transaction_event transaction_event_cb;
55         gpointer transaction_event_data;
56 };
57
58 typedef struct _SeESEDetectedHandler SeESEDetectedHandler;
59
60 struct _SeESEDetectedHandler {
61         net_nfc_client_se_ese_detected_event se_ese_detected_cb;
62         gpointer se_ese_detected_data;
63 };
64
65
66 static NetNfcGDbusSecureElement *se_proxy = NULL;
67 static NetNfcGDbusSecureElement *auto_start_proxy = NULL;
68
69 static SeEventHandler se_eventhandler;
70 static SeTransEventHandler uicc_transactionEventHandler;
71 static SeTransEventHandler ese_transactionEventHandler;
72 static SeESEDetectedHandler se_esedetecthandler;
73
74 static void se_ese_detected(GObject *source_object,
75                         guint arg_handle,
76                         gint arg_se_type,
77                         GVariant *arg_data);
78
79 static void se_type_changed(GObject *source_object,
80                                 gint arg_se_type);
81
82 static void set_secure_element(GObject *source_object,
83                                 GAsyncResult *res,
84                                 gpointer user_data);
85
86 static void open_secure_element(GObject *source_object,
87                                 GAsyncResult *res,
88                                 gpointer user_data);
89
90 static void close_secure_element(GObject *source_object,
91                                 GAsyncResult *res,
92                                 gpointer user_data);
93
94 static void send_apdu_secure_element(GObject *source_object,
95                                 GAsyncResult *res,
96                                 gpointer user_data);
97
98 static void get_atr_secure_element(GObject *source_object,
99                                 GAsyncResult *res,
100                                 gpointer user_data);
101
102 static void se_rf_detected(GObject *source_object,
103                         gint arg_se_type,
104                         GVariant *arg_data);
105
106
107 static void se_ese_detected(GObject *source_object,
108                         guint arg_handle,
109                         gint arg_se_type,
110                         GVariant *arg_data)
111 {
112         INFO_MSG(">>> SIGNAL arrived");
113
114         if (se_esedetecthandler.se_ese_detected_cb != NULL) {
115                 data_s buffer_data = { NULL, 0 };
116                 net_nfc_client_se_ese_detected_event callback =
117                         (net_nfc_client_se_ese_detected_event)se_esedetecthandler.se_ese_detected_cb;
118
119                 net_nfc_util_gdbus_variant_to_data_s(arg_data, &buffer_data);
120
121                 callback((net_nfc_target_handle_h)GUINT_TO_POINTER(arg_handle),
122                         arg_se_type, &buffer_data,
123                         se_esedetecthandler.se_ese_detected_data);
124
125                 net_nfc_util_clear_data(&buffer_data);
126         }
127 }
128
129 static void se_rf_detected(GObject *source_object,
130                         gint arg_se_type,
131                         GVariant *arg_data)
132 {
133
134         INFO_MSG(">>> SIGNAL arrived");
135
136         if (se_eventhandler.se_event_cb != NULL) {
137                 net_nfc_client_se_event callback =
138                         (net_nfc_client_se_event)se_eventhandler.se_event_cb;
139
140                 callback((net_nfc_message_e)NET_NFC_MESSAGE_SE_FIELD_ON,
141                         se_eventhandler.se_event_data);
142         }
143 }
144
145
146 static void se_type_changed(GObject *source_object,
147                         gint arg_se_type)
148 {
149         INFO_MSG(">>> SIGNAL arrived");
150
151         if (se_eventhandler.se_event_cb != NULL) {
152                 net_nfc_client_se_event callback =
153                         (net_nfc_client_se_event)se_eventhandler.se_event_cb;
154
155                 callback((net_nfc_message_e)NET_NFC_MESSAGE_SE_TYPE_CHANGED,
156                         se_eventhandler.se_event_data);
157         }
158 }
159
160
161 static void se_transaction_event(GObject *source_object,
162         gint arg_se_type,
163         GVariant *arg_aid,
164         GVariant *arg_param,
165         gint fg_dispatch,
166         gint focus_app_pid)
167 {
168         void *user_data = NULL;
169         net_nfc_client_se_transaction_event callback = NULL;
170         pid_t mypid = getpid();
171
172         INFO_MSG(">>> SIGNAL arrived");
173
174         if (fg_dispatch == true && focus_app_pid != getpgid(mypid)) {
175                 SECURE_MSG("skip transaction event, fg_dispatch [%d], focus_app_pid [%d]", fg_dispatch, focus_app_pid);
176                 return;
177         }
178 #ifdef CHECK_NFC_ACCESS_FOR_ESE
179         if (net_nfc_gdbus_secure_element_call_check_transaction_permission_sync(
180                 NET_NFC_GDBUS_SECURE_ELEMENT(source_object),
181                 arg_aid,
182                 &result,
183                 NULL,
184                 &error) == false) {
185                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_check_transaction_permission_sync failed : %s", error->message);
186                 g_error_free(error);
187                 return;
188         }
189
190         if (result != NET_NFC_OK) {
191                 DEBUG_ERR_MSG("not allowed process [%d]", result);
192                 return;
193         }
194 #endif
195         switch (arg_se_type) {
196         case NET_NFC_SE_TYPE_UICC:
197                 if (uicc_transactionEventHandler.transaction_event_cb != NULL) {
198                         callback = uicc_transactionEventHandler.transaction_event_cb;
199                         user_data = uicc_transactionEventHandler.transaction_event_data;
200                 }
201                 break;
202
203         case NET_NFC_SE_TYPE_ESE:
204                 if (ese_transactionEventHandler.transaction_event_cb != NULL) {
205                         callback = ese_transactionEventHandler.transaction_event_cb;
206                         user_data = ese_transactionEventHandler.transaction_event_data;
207                 }
208                 break;
209
210         default:
211                 DEBUG_ERR_MSG("Transaction event SE type wrong [%d]", arg_se_type);
212                 break;
213         }
214
215         if (callback != NULL) {
216                 data_s aid = { NULL, 0 };
217                 data_s param = { NULL, 0 };
218
219                 net_nfc_util_gdbus_variant_to_data_s(arg_aid, &aid);
220                 net_nfc_util_gdbus_variant_to_data_s(arg_param, &param);
221
222                 callback(arg_se_type, &aid, &param, user_data);
223
224                 net_nfc_util_clear_data(&param);
225                 net_nfc_util_clear_data(&aid);
226         }
227 }
228
229 static void se_card_emulation_mode_changed(GObject *source_object,
230                         gint arg_se_type)
231 {
232         INFO_MSG(">>> SIGNAL arrived");
233
234         if (se_eventhandler.se_event_cb != NULL) {
235                 net_nfc_client_se_event callback =
236                         (net_nfc_client_se_event)se_eventhandler.se_event_cb;
237
238                 callback((net_nfc_message_e)NET_NFC_MESSAGE_SE_CARD_EMULATION_CHANGED,
239                         se_eventhandler.se_event_data);
240         }
241 }
242
243 static void set_secure_element(GObject *source_object,
244                                 GAsyncResult *res,
245                                 gpointer user_data)
246 {
247         SeFuncData *func_data = (SeFuncData *)user_data;
248         net_nfc_error_e result;
249         GError *error = NULL;
250
251         g_assert(user_data != NULL);
252
253         if (net_nfc_gdbus_secure_element_call_set_finish(se_proxy,
254                 &result,
255                 res,
256                 &error) == FALSE) {
257
258                 DEBUG_ERR_MSG("Could not set secure element: %s",
259                                 error->message);
260                 result = NET_NFC_IPC_FAIL;
261
262                 g_error_free(error);
263         }
264
265         if (func_data->se_callback != NULL) {
266                 net_nfc_se_set_se_cb se_callback =
267                         (net_nfc_se_set_se_cb)func_data->se_callback;
268
269                 se_callback(result, func_data->se_data);
270         }
271
272         g_free(func_data);
273 }
274
275 static void open_secure_element(GObject *source_object,
276                                 GAsyncResult *res,
277                                 gpointer user_data)
278 {
279         SeFuncData *func_data = (SeFuncData *)user_data;
280         net_nfc_error_e result;
281         guint out_handle = 0;
282         GError *error = NULL;
283
284         g_assert(user_data != NULL);
285
286         if (net_nfc_gdbus_secure_element_call_open_secure_element_finish(
287                 auto_start_proxy,
288                 &result,
289                 &out_handle,
290                 res,
291                 &error) == FALSE) {
292
293                 DEBUG_ERR_MSG("Could not open secure element: %s",
294                                 error->message);
295                 result = NET_NFC_IPC_FAIL;
296
297                 g_error_free(error);
298         }
299
300         if (func_data->se_callback != NULL) {
301                 net_nfc_se_open_se_cb se_callback =
302                         (net_nfc_se_open_se_cb)func_data->se_callback;
303
304                 se_callback(result,
305                         (net_nfc_target_handle_h)GUINT_TO_POINTER(out_handle),
306                         func_data->se_data);
307         }
308
309         g_free(func_data);
310 }
311
312
313 static void close_secure_element(GObject *source_object,
314                                 GAsyncResult *res,
315                                 gpointer user_data)
316 {
317         SeFuncData *func_data = (SeFuncData *)user_data;
318         net_nfc_error_e result;
319         GError *error = NULL;
320
321         g_assert(user_data != NULL);
322
323         if (net_nfc_gdbus_secure_element_call_close_secure_element_finish(
324                 se_proxy,
325                 &result,
326                 res,
327                 &error) == FALSE) {
328                 DEBUG_ERR_MSG("Could not close secure element: %s", error->message);
329                 result = NET_NFC_IPC_FAIL;
330
331                 g_error_free(error);
332         }
333
334         if (func_data->se_callback != NULL) {
335                 net_nfc_se_close_se_cb se_callback =
336                         (net_nfc_se_close_se_cb)func_data->se_callback;
337
338                 se_callback(result, func_data->se_data);
339         }
340
341         g_free(func_data);
342 }
343
344
345 static void send_apdu_secure_element(GObject *source_object,
346                                 GAsyncResult *res,
347                                 gpointer user_data)
348 {
349         SeFuncData *func_data = (SeFuncData *)user_data;
350         net_nfc_error_e result;
351         GVariant *out_response = NULL;
352         GError *error = NULL;
353
354         g_assert(user_data != NULL);
355
356         if (net_nfc_gdbus_secure_element_call_send_apdu_finish(
357                 se_proxy,
358                 &result,
359                 &out_response,
360                 res,
361                 &error) == FALSE) {
362                 DEBUG_ERR_MSG("Could not send apdu: %s", error->message);
363                 result = NET_NFC_IPC_FAIL;
364
365                 g_error_free(error);
366         }
367
368         if (func_data->se_callback != NULL) {
369                 net_nfc_se_send_apdu_cb se_callback =
370                         (net_nfc_se_send_apdu_cb)func_data->se_callback;
371                 data_s data = { NULL, };
372
373                 net_nfc_util_gdbus_variant_to_data_s(out_response, &data);
374
375                 se_callback(result, &data, func_data->se_data);
376
377                 net_nfc_util_clear_data(&data);
378         }
379
380         g_free(func_data);
381 }
382
383
384 static void get_atr_secure_element(GObject *source_object,
385                                 GAsyncResult *res,
386                                 gpointer user_data)
387 {
388         SeFuncData *func_data = (SeFuncData *)user_data;
389         net_nfc_error_e result;
390         GVariant *out_atr = NULL;
391         GError *error = NULL;
392
393         g_assert(user_data != NULL);
394
395         if (net_nfc_gdbus_secure_element_call_get_atr_finish(
396                 se_proxy,
397                 &result,
398                 &out_atr,
399                 res,
400                 &error) == FALSE) {
401                 DEBUG_ERR_MSG("Could not get atr: %s", error->message);
402                 result = NET_NFC_IPC_FAIL;
403
404                 g_error_free(error);
405         }
406
407         if (func_data->se_callback != NULL) {
408                 net_nfc_se_get_atr_cb se_callback =
409                         (net_nfc_se_get_atr_cb)func_data->se_callback;
410                 data_s data = { NULL, };
411
412                 net_nfc_util_gdbus_variant_to_data_s(out_atr, &data);
413
414                 se_callback(result, &data, func_data->se_data);
415
416                 net_nfc_util_clear_data(&data);
417         }
418
419         g_free(func_data);
420 }
421
422
423 NET_NFC_EXPORT_API
424 net_nfc_error_e net_nfc_client_se_set_secure_element_type(
425                                 net_nfc_se_type_e se_type,
426                                 net_nfc_se_set_se_cb callback,
427                                 void *user_data)
428 {
429         SeFuncData *func_data;
430
431         if (se_proxy == NULL) {
432                 if (net_nfc_client_se_init() != NET_NFC_OK) {
433                         DEBUG_ERR_MSG("se_proxy fail");
434                         /* FIXME : return result of this error */
435                         return NET_NFC_NOT_INITIALIZED;
436                 }
437         }
438
439         /* prevent executing daemon when nfc is off */
440         if (net_nfc_client_manager_is_activated() == false)
441                 return NET_NFC_NOT_ACTIVATED;
442
443         func_data = g_try_new0(SeFuncData, 1);
444         if (func_data == NULL)
445                 return NET_NFC_ALLOC_FAIL;
446
447         func_data->se_callback = (gpointer)callback;
448         func_data->se_data = user_data;
449
450         net_nfc_gdbus_secure_element_call_set(
451                                 se_proxy,
452                                 (gint)se_type,
453                                 NULL,
454                                 set_secure_element,
455                                 func_data);
456
457         return NET_NFC_OK;
458 }
459
460
461 NET_NFC_EXPORT_API
462 net_nfc_error_e net_nfc_client_se_set_secure_element_type_sync(
463                                 net_nfc_se_type_e se_type)
464 {
465         net_nfc_error_e result = NET_NFC_OK;
466         GError *error = NULL;
467
468         if (se_proxy == NULL) {
469                 if (net_nfc_client_se_init() != NET_NFC_OK) {
470                         DEBUG_ERR_MSG("se_proxy fail");
471                         return NET_NFC_NOT_INITIALIZED;
472                 }
473         }
474
475         /* prevent executing daemon when nfc is off */
476         if (net_nfc_client_manager_is_activated() == false)
477                 return NET_NFC_NOT_ACTIVATED;
478
479         if (net_nfc_gdbus_secure_element_call_set_sync(
480                         se_proxy,
481                         (gint)se_type,
482                         &result,
483                         NULL,
484                         &error) == FALSE) {
485                 DEBUG_ERR_MSG("Set secure element failed: %s", error->message);
486                 result = NET_NFC_IPC_FAIL;
487
488                 g_error_free(error);
489         }
490
491         return result;
492 }
493
494 NET_NFC_EXPORT_API
495 net_nfc_error_e net_nfc_client_se_get_secure_element_type_sync(
496         net_nfc_se_type_e *se_type)
497 {
498         net_nfc_error_e result = NET_NFC_OK;
499         gint type;
500 #if 1
501         GError *error = NULL;
502 #endif
503         if (se_proxy == NULL) {
504                 if (net_nfc_client_se_init() != NET_NFC_OK) {
505                         DEBUG_ERR_MSG("se_proxy fail");
506                         return NET_NFC_NOT_INITIALIZED;
507                 }
508         }
509
510         /* prevent executing daemon when nfc is off */
511         if (net_nfc_client_manager_is_activated() == false)
512                 return NET_NFC_NOT_ACTIVATED;
513 #if 0
514         if (vconf_get_int(VCONFKEY_NFC_SE_TYPE, &type) == 0)
515                 *se_type = type;
516         else
517                 result = NET_NFC_OPERATION_FAIL;
518 #else
519         if (net_nfc_gdbus_secure_element_call_get_sync(
520                         se_proxy,
521                         &result,
522                         &type,
523                         NULL,
524                         &error) == true) {
525
526                 SECURE_MSG("type [%d]", type);
527                 *se_type = type;
528         } else {
529                 DEBUG_ERR_MSG("get secure element failed: %s", error->message);
530
531                 g_error_free(error);
532
533                 result = NET_NFC_IPC_FAIL;
534         }
535 #endif
536         return result;
537 }
538
539 NET_NFC_EXPORT_API
540 net_nfc_error_e net_nfc_set_card_emulation_mode_sync(
541         net_nfc_card_emulation_mode_t mode)
542 {
543         net_nfc_error_e result = NET_NFC_OK;
544         GError *error = NULL;
545
546         if (se_proxy == NULL) {
547                 if (net_nfc_client_se_init() != NET_NFC_OK) {
548                         DEBUG_ERR_MSG("se_proxy fail");
549                         return NET_NFC_NOT_INITIALIZED;
550                 }
551         }
552
553         /* prevent executing daemon when nfc is off */
554         if (net_nfc_client_manager_is_activated() == false)
555                 return NET_NFC_NOT_ACTIVATED;
556
557         if (net_nfc_gdbus_secure_element_call_set_card_emulation_sync(
558                         se_proxy,
559                         (gint)mode,
560                         &result,
561                         NULL,
562                         &error) == FALSE) {
563                 DEBUG_ERR_MSG("Set card emulation failed: %s", error->message);
564                 result = NET_NFC_IPC_FAIL;
565
566                 g_error_free(error);
567         }
568
569         return result;
570 }
571
572 NET_NFC_EXPORT_API
573 net_nfc_error_e net_nfc_get_card_emulation_mode_sync(
574         net_nfc_card_emulation_mode_t *se_type)
575 {
576         net_nfc_error_e result = NET_NFC_OK;
577         gint type;
578         GError *error = NULL;
579
580         if (se_proxy == NULL) {
581                 if (net_nfc_client_se_init() != NET_NFC_OK) {
582                         DEBUG_ERR_MSG("se_proxy fail");
583                         return NET_NFC_NOT_INITIALIZED;
584                 }
585         }
586
587         /* prevent executing daemon when nfc is off */
588         if (net_nfc_client_manager_is_activated() == false)
589                 return NET_NFC_NOT_ACTIVATED;
590
591         if (net_nfc_gdbus_secure_element_call_get_card_emulation_sync(
592                         se_proxy,
593                         &result,
594                         &type,
595                         NULL,
596                         &error) == true) {
597                 *se_type = type;
598         } else {
599                 DEBUG_ERR_MSG("get secure element failed: %s", error->message);
600
601                 g_error_free(error);
602
603                 result = NET_NFC_IPC_FAIL;
604         }
605
606         return result;
607 }
608
609 NET_NFC_EXPORT_API
610 net_nfc_error_e net_nfc_client_se_open_internal_secure_element(
611                                         net_nfc_se_type_e se_type,
612                                         net_nfc_se_open_se_cb callback,
613                                         void *user_data)
614 {
615         SeFuncData *func_data;
616
617         if (auto_start_proxy == NULL) {
618                 GError *error = NULL;
619
620                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
621                         G_BUS_TYPE_SYSTEM,
622                         G_DBUS_PROXY_FLAGS_NONE,
623                         "org.tizen.NetNfcService",
624                         "/org/tizen/NetNfcService/SecureElement",
625                         NULL,
626                         &error);
627                 if (auto_start_proxy == NULL) {
628                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
629                         g_error_free(error);
630
631                         return NET_NFC_UNKNOWN_ERROR;
632                 }
633         }
634
635         /* allow this function even nfc is off */
636
637         func_data = g_try_new0(SeFuncData, 1);
638         if (func_data == NULL)
639                 return NET_NFC_ALLOC_FAIL;
640
641         func_data->se_callback = (gpointer)callback;
642         func_data->se_data = user_data;
643
644         net_nfc_gdbus_secure_element_call_open_secure_element(
645                                         auto_start_proxy,
646                                         (gint)se_type,
647                                         NULL,
648                                         open_secure_element,
649                                         func_data);
650
651         return NET_NFC_OK;
652 }
653
654
655 NET_NFC_EXPORT_API
656 net_nfc_error_e net_nfc_client_se_open_internal_secure_element_sync(
657                                         net_nfc_se_type_e se_type,
658                                         net_nfc_target_handle_h *handle)
659 {
660         net_nfc_error_e result = NET_NFC_OK;
661         guint out_handle = 0;
662         GError *error =  NULL;
663
664         if (handle == NULL)
665                 return NET_NFC_NULL_PARAMETER;
666
667         if (auto_start_proxy == NULL) {
668                 GError *error = NULL;
669
670                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
671                         G_BUS_TYPE_SYSTEM,
672                         G_DBUS_PROXY_FLAGS_NONE,
673                         "org.tizen.NetNfcService",
674                         "/org/tizen/NetNfcService/SecureElement",
675                         NULL,
676                         &error);
677                 if (auto_start_proxy == NULL) {
678                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
679                         g_error_free(error);
680
681                         return NET_NFC_UNKNOWN_ERROR;
682                 }
683         }
684
685         /* allow this function even nfc is off */
686
687         if (net_nfc_gdbus_secure_element_call_open_secure_element_sync(
688                                         auto_start_proxy,
689                                         se_type,
690                                         &result,
691                                         &out_handle,
692                                         NULL,
693                                         &error) == true) {
694                 *handle = GUINT_TO_POINTER(out_handle);
695         } else {
696                 DEBUG_ERR_MSG("Open internal secure element failed: %s",
697                                         error->message);
698                 result = NET_NFC_IPC_FAIL;
699
700                 g_error_free(error);
701         }
702
703         return result;
704 }
705
706
707 NET_NFC_EXPORT_API
708 net_nfc_error_e net_nfc_client_se_close_internal_secure_element(
709                                 net_nfc_target_handle_h handle,
710                                 net_nfc_se_close_se_cb callback,
711                                 void *user_data)
712 {
713         SeFuncData *func_data;
714
715         if (auto_start_proxy == NULL) {
716                 GError *error = NULL;
717
718                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
719                         G_BUS_TYPE_SYSTEM,
720                         G_DBUS_PROXY_FLAGS_NONE,
721                         "org.tizen.NetNfcService",
722                         "/org/tizen/NetNfcService/SecureElement",
723                         NULL,
724                         &error);
725                 if (auto_start_proxy == NULL) {
726                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
727                         g_error_free(error);
728
729                         return NET_NFC_UNKNOWN_ERROR;
730                 }
731         }
732
733         /* allow this function even nfc is off */
734
735         func_data = g_try_new0(SeFuncData, 1);
736         if (func_data == NULL)
737                 return NET_NFC_ALLOC_FAIL;
738
739         func_data->se_callback = (gpointer)callback;
740         func_data->se_data = user_data;
741
742         net_nfc_gdbus_secure_element_call_close_secure_element(
743                 auto_start_proxy,
744                 GPOINTER_TO_UINT(handle),
745                 NULL,
746                 close_secure_element,
747                 func_data);
748
749         return NET_NFC_OK;
750 }
751
752
753 NET_NFC_EXPORT_API
754 net_nfc_error_e net_nfc_client_se_close_internal_secure_element_sync(
755                                         net_nfc_target_handle_h handle)
756 {
757         net_nfc_error_e result = NET_NFC_OK;
758         GError *error = NULL;
759
760         if (auto_start_proxy == NULL) {
761                 GError *error = NULL;
762
763                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
764                         G_BUS_TYPE_SYSTEM,
765                         G_DBUS_PROXY_FLAGS_NONE,
766                         "org.tizen.NetNfcService",
767                         "/org/tizen/NetNfcService/SecureElement",
768                         NULL,
769                         &error);
770                 if (auto_start_proxy == NULL) {
771                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
772                         g_error_free(error);
773
774                         return NET_NFC_UNKNOWN_ERROR;
775                 }
776         }
777
778         /* allow this function even nfc is off */
779
780         if (net_nfc_gdbus_secure_element_call_close_secure_element_sync(
781                 auto_start_proxy,
782                 GPOINTER_TO_UINT(handle),
783                 &result,
784                 NULL,
785                 &error) == FALSE) {
786                 DEBUG_ERR_MSG("close internal secure element failed: %s",
787                                         error->message);
788                 result = NET_NFC_IPC_FAIL;
789
790                 g_error_free(error);
791         }
792
793         return result;
794 }
795
796
797 NET_NFC_EXPORT_API
798 net_nfc_error_e net_nfc_client_se_get_atr(
799                                 net_nfc_target_handle_h handle,
800                                 net_nfc_se_get_atr_cb callback,
801                                 void *user_data)
802 {
803         SeFuncData *func_data;
804
805         if (auto_start_proxy == NULL) {
806                 GError *error = NULL;
807
808                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
809                         G_BUS_TYPE_SYSTEM,
810                         G_DBUS_PROXY_FLAGS_NONE,
811                         "org.tizen.NetNfcService",
812                         "/org/tizen/NetNfcService/SecureElement",
813                         NULL,
814                         &error);
815                 if (auto_start_proxy == NULL) {
816                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
817                         g_error_free(error);
818
819                         return NET_NFC_UNKNOWN_ERROR;
820                 }
821         }
822
823         /* allow this function even nfc is off */
824
825         func_data = g_try_new0(SeFuncData, 1);
826         if (func_data == NULL)
827                 return NET_NFC_ALLOC_FAIL;
828
829         func_data->se_callback = (gpointer)callback;
830         func_data->se_data = user_data;
831
832         net_nfc_gdbus_secure_element_call_get_atr(
833                 auto_start_proxy,
834                 GPOINTER_TO_UINT(handle),
835                 NULL,
836                 get_atr_secure_element,
837                 func_data);
838
839         return NET_NFC_OK;
840 }
841
842
843 NET_NFC_EXPORT_API
844 net_nfc_error_e net_nfc_client_se_get_atr_sync(
845                                 net_nfc_target_handle_h handle,
846                                 data_h *atr)
847 {
848         net_nfc_error_e result = NET_NFC_OK;
849         GVariant *out_atr = NULL;
850         GError *error = NULL;
851
852         if (atr == NULL)
853                 return NET_NFC_NULL_PARAMETER;
854
855         *atr = NULL;
856
857         if (auto_start_proxy == NULL) {
858                 GError *error = NULL;
859
860                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
861                         G_BUS_TYPE_SYSTEM,
862                         G_DBUS_PROXY_FLAGS_NONE,
863                         "org.tizen.NetNfcService",
864                         "/org/tizen/NetNfcService/SecureElement",
865                         NULL,
866                         &error);
867                 if (auto_start_proxy == NULL) {
868                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
869                         g_error_free(error);
870
871                         return NET_NFC_UNKNOWN_ERROR;
872                 }
873         }
874
875         /* allow this function even nfc is off */
876
877         if (net_nfc_gdbus_secure_element_call_get_atr_sync(
878                 auto_start_proxy,
879                 GPOINTER_TO_UINT(handle),
880                 &result,
881                 &out_atr,
882                 NULL,
883                 &error) == true) {
884                 *atr = net_nfc_util_gdbus_variant_to_data(out_atr);
885         } else {
886                 DEBUG_ERR_MSG("Get attributes failed: %s", error->message);
887                 result = NET_NFC_IPC_FAIL;
888
889                 g_error_free(error);
890         }
891
892         return result;
893 }
894
895
896 NET_NFC_EXPORT_API
897 net_nfc_error_e net_nfc_client_se_send_apdu(
898                                 net_nfc_target_handle_h handle,
899                                 data_h apdu_data,
900                                 net_nfc_se_send_apdu_cb callback,
901                                 void *user_data)
902 {
903         SeFuncData *func_data;
904         GVariant *arg_data;
905
906         if (auto_start_proxy == NULL) {
907                 GError *error = NULL;
908
909                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
910                         G_BUS_TYPE_SYSTEM,
911                         G_DBUS_PROXY_FLAGS_NONE,
912                         "org.tizen.NetNfcService",
913                         "/org/tizen/NetNfcService/SecureElement",
914                         NULL,
915                         &error);
916                 if (auto_start_proxy == NULL) {
917                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
918                         g_error_free(error);
919
920                         return NET_NFC_UNKNOWN_ERROR;
921                 }
922         }
923
924         /* allow this function even nfc is off */
925
926         arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)apdu_data);
927         if (arg_data == NULL)
928                 return NET_NFC_INVALID_PARAM;
929
930         func_data = g_try_new0(SeFuncData, 1);
931         if (func_data == NULL) {
932                 g_variant_unref(arg_data);
933
934                 return NET_NFC_ALLOC_FAIL;
935         }
936
937         func_data->se_callback = (gpointer)callback;
938         func_data->se_data = user_data;
939
940         net_nfc_gdbus_secure_element_call_send_apdu(
941                 auto_start_proxy,
942                 GPOINTER_TO_UINT(handle),
943                 arg_data,
944                 NULL,
945                 send_apdu_secure_element,
946                 func_data);
947
948         return NET_NFC_OK;
949 }
950
951
952 NET_NFC_EXPORT_API
953 net_nfc_error_e net_nfc_client_se_send_apdu_sync(
954                                 net_nfc_target_handle_h handle,
955                                 data_h apdu_data,
956                                 data_h *response)
957 {
958         net_nfc_error_e result = NET_NFC_OK;
959         GVariant *out_data = NULL;
960         GError *error = NULL;
961         GVariant *arg_data;
962
963         if (response == NULL)
964                 return NET_NFC_NULL_PARAMETER;
965
966         *response = NULL;
967
968         if (auto_start_proxy == NULL) {
969                 GError *error = NULL;
970
971                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
972                         G_BUS_TYPE_SYSTEM,
973                         G_DBUS_PROXY_FLAGS_NONE,
974                         "org.tizen.NetNfcService",
975                         "/org/tizen/NetNfcService/SecureElement",
976                         NULL,
977                         &error);
978                 if (auto_start_proxy == NULL) {
979                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
980                         g_error_free(error);
981
982                         return NET_NFC_UNKNOWN_ERROR;
983                 }
984         }
985
986         /* allow this function even nfc is off */
987
988         arg_data = net_nfc_util_gdbus_data_to_variant((data_s *)apdu_data);
989         if (arg_data == NULL)
990                 return NET_NFC_INVALID_PARAM;
991
992         if (net_nfc_gdbus_secure_element_call_send_apdu_sync(
993                 auto_start_proxy,
994                 GPOINTER_TO_UINT(handle),
995                 arg_data,
996                 &result,
997                 &out_data,
998                 NULL,
999                 &error) == true) {
1000                 *response = net_nfc_util_gdbus_variant_to_data(out_data);
1001         } else {
1002                 DEBUG_ERR_MSG("Send APDU failed: %s", error->message);
1003                 result = NET_NFC_IPC_FAIL;
1004
1005                 g_error_free(error);
1006         }
1007
1008         return result;
1009 }
1010
1011
1012 NET_NFC_EXPORT_API
1013 void net_nfc_client_se_set_ese_detection_cb(
1014                         net_nfc_client_se_ese_detected_event callback,
1015                         void *user_data)
1016 {
1017         if (se_proxy == NULL) {
1018                 if (net_nfc_client_se_init() != NET_NFC_OK) {
1019                         DEBUG_ERR_MSG("se_proxy fail");
1020                         /* FIXME : return result of this error */
1021                         return;
1022                 }
1023         }
1024
1025         se_esedetecthandler.se_ese_detected_cb = callback;
1026         se_esedetecthandler.se_ese_detected_data = user_data;
1027 }
1028
1029
1030 NET_NFC_EXPORT_API
1031 void net_nfc_client_se_unset_ese_detection_cb(void)
1032 {
1033         se_esedetecthandler.se_ese_detected_cb = NULL;
1034         se_esedetecthandler.se_ese_detected_data = NULL;
1035 }
1036
1037
1038 NET_NFC_EXPORT_API
1039 void net_nfc_client_se_set_transaction_event_cb(
1040                         net_nfc_se_type_e se_type,
1041                         net_nfc_client_se_transaction_event callback,
1042                         void *user_data)
1043 {
1044         if (se_proxy == NULL) {
1045                 if (net_nfc_client_se_init() != NET_NFC_OK) {
1046                         DEBUG_ERR_MSG("se_proxy fail");
1047                         /* FIXME : return result of this error */
1048                         return;
1049                 }
1050         }
1051
1052         if (se_type == NET_NFC_SE_TYPE_ESE) {
1053                 ese_transactionEventHandler.transaction_event_cb = callback;
1054                 ese_transactionEventHandler.transaction_event_data = user_data;
1055         } else if (se_type == NET_NFC_SE_TYPE_UICC) {
1056                 uicc_transactionEventHandler.transaction_event_cb = callback;
1057                 uicc_transactionEventHandler.transaction_event_data = user_data;
1058         }
1059 }
1060
1061
1062 NET_NFC_EXPORT_API
1063 void net_nfc_client_se_unset_transaction_event_cb(net_nfc_se_type_e type)
1064 {
1065         if (type == NET_NFC_SE_TYPE_ESE) {
1066                 ese_transactionEventHandler.transaction_event_cb = NULL;
1067                 ese_transactionEventHandler.transaction_event_data = NULL;
1068         } else if (type == NET_NFC_SE_TYPE_UICC) {
1069                 uicc_transactionEventHandler.transaction_event_cb = NULL;
1070                 uicc_transactionEventHandler.transaction_event_data = NULL;
1071         }
1072 }
1073
1074 NET_NFC_EXPORT_API
1075 void net_nfc_client_se_set_event_cb(net_nfc_client_se_event callback,
1076                                         void *user_data)
1077 {
1078         if (se_proxy == NULL) {
1079                 if (net_nfc_client_se_init() != NET_NFC_OK) {
1080                         DEBUG_ERR_MSG("se_proxy fail");
1081                         /* FIXME : return result of this error */
1082                         return;
1083                 }
1084         }
1085
1086         se_eventhandler.se_event_cb = callback;
1087         se_eventhandler.se_event_data = user_data;
1088 }
1089
1090
1091 NET_NFC_EXPORT_API
1092 void net_nfc_client_se_unset_event_cb(void)
1093 {
1094         se_eventhandler.se_event_cb = NULL;
1095         se_eventhandler.se_event_data = NULL;
1096 }
1097
1098 NET_NFC_EXPORT_API
1099 net_nfc_error_e net_nfc_client_se_set_transaction_fg_dispatch(int mode)
1100 {
1101         net_nfc_error_e result = NET_NFC_OK;
1102         GError *error = NULL;
1103
1104         if (auto_start_proxy == NULL) {
1105                 GError *error = NULL;
1106
1107                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
1108                         G_BUS_TYPE_SYSTEM,
1109                         G_DBUS_PROXY_FLAGS_NONE,
1110                         "org.tizen.NetNfcService",
1111                         "/org/tizen/NetNfcService/SecureElement",
1112                         NULL,
1113                         &error);
1114                 if (auto_start_proxy == NULL) {
1115                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
1116                         g_error_free(error);
1117
1118                         return NET_NFC_UNKNOWN_ERROR;
1119                 }
1120         }
1121
1122         if (net_nfc_gdbus_secure_element_call_set_transaction_fg_dispatch_sync(
1123                 auto_start_proxy,
1124                 mode,
1125                 &result,
1126                 NULL,
1127                 &error) != true) {
1128
1129                 DEBUG_ERR_MSG("set transaction fg dispatch failed: %s", error->message);
1130                 result = NET_NFC_IPC_FAIL;
1131
1132                 g_error_free(error);
1133         }
1134
1135         return result;
1136 }
1137
1138 NET_NFC_EXPORT_API
1139 net_nfc_error_e net_nfc_client_se_set_default_route_sync(
1140         net_nfc_se_type_e switch_on,
1141         net_nfc_se_type_e switch_off,
1142         net_nfc_se_type_e battery_off)
1143 {
1144         net_nfc_error_e result = NET_NFC_OK;
1145         GError *error = NULL;
1146
1147         if (se_proxy == NULL) {
1148                 result = net_nfc_client_se_init();
1149                 if (result != NET_NFC_OK) {
1150                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1151
1152                         return NET_NFC_NOT_INITIALIZED;
1153                 }
1154         }
1155
1156         if (net_nfc_gdbus_secure_element_call_set_default_route_sync(
1157                 se_proxy,
1158                 switch_on,
1159                 switch_off,
1160                 battery_off,
1161                 &result,
1162                 NULL,
1163                 &error) == FALSE) {
1164                 DEBUG_ERR_MSG("Set Route Aid failed: %s", error->message);
1165                 result = NET_NFC_IPC_FAIL;
1166
1167                 g_error_free(error);
1168         }
1169
1170         DEBUG_CLIENT_MSG("net_nfc_gdbus_secure_element_call_set_default_route_sync end");
1171
1172         return result;
1173 }
1174
1175 NET_NFC_EXPORT_API
1176 net_nfc_error_e net_nfc_client_se_is_activated_aid_handler_sync(
1177         net_nfc_se_type_e se_type, const char *aid, bool *activated)
1178 {
1179         net_nfc_error_e result = NET_NFC_OK;
1180         GError *error = NULL;
1181         gboolean ret = false;
1182
1183         if (activated == NULL)
1184                 return NET_NFC_NULL_PARAMETER;
1185
1186         if (se_proxy == NULL) {
1187                 result = net_nfc_client_se_init();
1188                 if (result != NET_NFC_OK) {
1189                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1190
1191                         return NET_NFC_NOT_INITIALIZED;
1192                 }
1193         }
1194
1195         if (net_nfc_gdbus_secure_element_call_is_activated_aid_handler_sync(
1196                         se_proxy,
1197                         se_type,
1198                         aid,
1199                         &result,
1200                         &ret,
1201                         NULL,
1202                         &error) == true) {
1203                 *activated = ret;
1204         } else {
1205                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_is_activated_aid_handler_sync failed : %s", error->message);
1206                 result = NET_NFC_IPC_FAIL;
1207
1208                 g_error_free(error);
1209         }
1210
1211         return result;
1212 }
1213
1214 NET_NFC_EXPORT_API
1215 net_nfc_error_e net_nfc_client_se_is_activated_category_handler_sync(
1216         net_nfc_se_type_e se_type,
1217         net_nfc_card_emulation_category_t category, bool *activated)
1218 {
1219         net_nfc_error_e result = NET_NFC_OK;
1220         GError *error = NULL;
1221         gboolean ret = false;
1222
1223         if (activated == NULL)
1224                 return NET_NFC_NULL_PARAMETER;
1225
1226         if (se_proxy == NULL) {
1227                 result = net_nfc_client_se_init();
1228                 if (result != NET_NFC_OK) {
1229                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1230
1231                         return NET_NFC_NOT_INITIALIZED;
1232                 }
1233         }
1234
1235         if (net_nfc_gdbus_secure_element_call_is_activated_category_handler_sync(
1236                         se_proxy,
1237                         se_type,
1238                         category,
1239                         &result,
1240                         &ret,
1241                         NULL,
1242                         &error) == true) {
1243                 *activated = ret;
1244         } else {
1245                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_is_activated_category_handler_sync failed : %s", error->message);
1246                 result = NET_NFC_IPC_FAIL;
1247
1248                 g_error_free(error);
1249         }
1250
1251         return result;
1252 }
1253
1254 NET_NFC_EXPORT_API
1255 net_nfc_error_e net_nfc_client_se_get_registered_aids_count_sync(
1256         net_nfc_se_type_e se_type,
1257         net_nfc_card_emulation_category_t category,
1258         size_t *count)
1259 {
1260         net_nfc_error_e result = NET_NFC_OK;
1261         GError *error = NULL;
1262         GVariant *aids = NULL;
1263
1264         if (count == NULL)
1265                 return NET_NFC_NULL_PARAMETER;
1266
1267         if (se_proxy == NULL) {
1268                 result = net_nfc_client_se_init();
1269                 if (result != NET_NFC_OK) {
1270                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1271
1272                         return NET_NFC_NOT_INITIALIZED;
1273                 }
1274         }
1275
1276         if (net_nfc_gdbus_secure_element_call_get_registered_aids_sync(
1277                         se_proxy,
1278                         se_type,
1279                         category,
1280                         &result,
1281                         &aids,
1282                         NULL,
1283                         &error) == FALSE) {
1284                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_registered_aids_sync failed : %s", error->message);
1285                 result = NET_NFC_IPC_FAIL;
1286
1287                 g_error_free(error);
1288         }
1289
1290         if (result == NET_NFC_OK) {
1291                 GVariantIter iter;
1292
1293                 g_variant_iter_init(&iter, aids);
1294
1295                 *count = g_variant_iter_n_children(&iter);
1296
1297                 g_variant_unref(aids);
1298         }
1299
1300         return result;
1301 }
1302
1303 NET_NFC_EXPORT_API
1304 net_nfc_error_e net_nfc_client_se_foreach_registered_aids_sync(
1305         net_nfc_se_type_e se_type,
1306         net_nfc_card_emulation_category_t category,
1307         net_nfc_client_se_registered_aid_cb callback,
1308         void *user_data)
1309 {
1310         net_nfc_error_e result = NET_NFC_OK;
1311         GError *error = NULL;
1312         GVariant *aids = NULL;
1313
1314         if (callback == NULL)
1315                 return NET_NFC_NULL_PARAMETER;
1316
1317         if (se_proxy == NULL) {
1318                 result = net_nfc_client_se_init();
1319                 if (result != NET_NFC_OK) {
1320                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1321
1322                         return NET_NFC_NOT_INITIALIZED;
1323                 }
1324         }
1325
1326         if (net_nfc_gdbus_secure_element_call_get_registered_aids_sync(
1327                         se_proxy,
1328                         se_type,
1329                         category,
1330                         &result,
1331                         &aids,
1332                         NULL,
1333                         &error) == FALSE) {
1334                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_registered_aids_sync failed : %s", error->message);
1335                 result = NET_NFC_IPC_FAIL;
1336
1337                 g_error_free(error);
1338         }
1339
1340         if (result == NET_NFC_OK) {
1341                 GVariantIter iter;
1342                 const gchar *aid;
1343                 gboolean manifest;
1344
1345                 g_variant_iter_init(&iter, aids);
1346
1347                 while (g_variant_iter_loop(&iter, "(sb)", &aid, &manifest) == true)
1348                         callback(se_type, aid, (bool)manifest, user_data);
1349         }
1350
1351         return result;
1352 }
1353
1354
1355 NET_NFC_EXPORT_API
1356 net_nfc_error_e net_nfc_client_se_register_aids_sync(net_nfc_se_type_e se_type,
1357         net_nfc_card_emulation_category_t category, const char *aid, ...)
1358 {
1359         net_nfc_error_e result = NET_NFC_OK;
1360         GError *error = NULL;
1361
1362         if (se_proxy == NULL) {
1363                 result = net_nfc_client_se_init();
1364                 if (result != NET_NFC_OK) {
1365                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1366
1367                         return NET_NFC_NOT_INITIALIZED;
1368                 }
1369         }
1370
1371         if (net_nfc_gdbus_secure_element_call_register_aid_sync(
1372                         se_proxy,
1373                         se_type,
1374                         category,
1375                         aid,
1376                         &result,
1377                         NULL,
1378                         &error) == FALSE) {
1379                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_register_aid_sync failed : %s", error->message);
1380                 result = NET_NFC_IPC_FAIL;
1381
1382                 g_error_free(error);
1383         }
1384
1385         return result;
1386 }
1387
1388 NET_NFC_EXPORT_API
1389 net_nfc_error_e net_nfc_client_se_unregister_aid_sync(net_nfc_se_type_e se_type,
1390         net_nfc_card_emulation_category_t category, const char *aid)
1391 {
1392         net_nfc_error_e result = NET_NFC_OK;
1393         GError *error = NULL;
1394
1395         if (se_proxy == NULL) {
1396                 result = net_nfc_client_se_init();
1397                 if (result != NET_NFC_OK) {
1398                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1399
1400                         return NET_NFC_NOT_INITIALIZED;
1401                 }
1402         }
1403
1404         if (net_nfc_gdbus_secure_element_call_unregister_aid_sync(
1405                         se_proxy,
1406                         se_type,
1407                         category,
1408                         aid,
1409                         &result,
1410                         NULL,
1411                         &error) == FALSE) {
1412                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_unregister_aid_sync failed : %s", error->message);
1413                 result = NET_NFC_IPC_FAIL;
1414
1415                 g_error_free(error);
1416         }
1417
1418         return result;
1419 }
1420
1421 NET_NFC_EXPORT_API
1422 net_nfc_error_e net_nfc_client_se_unregister_aids_sync(net_nfc_se_type_e se_type, net_nfc_card_emulation_category_t category)
1423 {
1424         net_nfc_error_e result = NET_NFC_OK;
1425         GError *error = NULL;
1426
1427         if (se_proxy == NULL) {
1428                 result = net_nfc_client_se_init();
1429                 if (result != NET_NFC_OK) {
1430                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1431
1432                         return NET_NFC_NOT_INITIALIZED;
1433                 }
1434         }
1435
1436         if (net_nfc_gdbus_secure_element_call_unregister_aids_sync(
1437                         se_proxy,
1438                         se_type,
1439                         category,
1440                         &result,
1441                         NULL,
1442                         &error) == FALSE) {
1443                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_unregister_aids_sync failed : %s", error->message);
1444                 result = NET_NFC_IPC_FAIL;
1445
1446                 g_error_free(error);
1447         }
1448
1449         return result;
1450 }
1451
1452 NET_NFC_EXPORT_API
1453 net_nfc_error_e net_nfc_client_se_foreach_registered_handlers_sync(
1454         net_nfc_card_emulation_category_t category,
1455         net_nfc_client_se_registered_handler_cb callback,
1456         void *user_data)
1457 {
1458         net_nfc_error_e result = NET_NFC_OK;
1459         GError *error = NULL;
1460         GVariant *handlers = NULL;
1461
1462         if (callback == NULL)
1463                 return NET_NFC_NULL_PARAMETER;
1464
1465         if (se_proxy == NULL) {
1466                 result = net_nfc_client_se_init();
1467                 if (result != NET_NFC_OK) {
1468                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1469
1470                         return NET_NFC_NOT_INITIALIZED;
1471                 }
1472         }
1473
1474         if (net_nfc_gdbus_secure_element_call_get_registered_handlers_sync(
1475                         se_proxy,
1476                         category,
1477                         &result,
1478                         &handlers,
1479                         NULL,
1480                         &error) == FALSE) {
1481                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_registered_handlers_sync failed : %s", error->message);
1482                 result = NET_NFC_IPC_FAIL;
1483
1484                 g_error_free(error);
1485         }
1486
1487         if (result == NET_NFC_OK) {
1488                 GVariantIter iter;
1489                 const gchar *handler;
1490                 int count;
1491
1492                 g_variant_iter_init(&iter, handlers);
1493
1494                 while (g_variant_iter_loop(&iter, "(is)", &count, &handler) == true)
1495                         callback(handler, count, user_data);
1496         }
1497
1498         return result;
1499 }
1500
1501 NET_NFC_EXPORT_API
1502 net_nfc_error_e net_nfc_client_se_add_route_aid_sync(
1503         const char *package, net_nfc_se_type_e se_type,
1504         net_nfc_card_emulation_category_t category, const char *aid,
1505         bool unlock_required, int power)
1506 {
1507         net_nfc_error_e result = NET_NFC_OK;
1508         GError *error = NULL;
1509
1510         if (auto_start_proxy == NULL) {
1511                 GError *error = NULL;
1512
1513                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
1514                         G_BUS_TYPE_SYSTEM,
1515                         G_DBUS_PROXY_FLAGS_NONE,
1516                         "org.tizen.NetNfcService",
1517                         "/org/tizen/NetNfcService/SecureElement",
1518                         NULL,
1519                         &error);
1520                 if (auto_start_proxy == NULL) {
1521                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
1522                         g_error_free(error);
1523
1524                         return NET_NFC_UNKNOWN_ERROR;
1525                 }
1526         }
1527
1528         if (net_nfc_gdbus_secure_element_call_add_route_aid_sync(
1529                 auto_start_proxy,
1530                 package,
1531                 aid,
1532                 se_type,
1533                 category,
1534                 unlock_required,
1535                 power,
1536                 &result,
1537                 NULL,
1538                 &error) == FALSE) {
1539                 DEBUG_ERR_MSG("Set Route Aid failed: %s", error->message);
1540                 result = NET_NFC_IPC_FAIL;
1541
1542                 g_error_free(error);
1543         }
1544
1545         DEBUG_CLIENT_MSG("net_nfc_gdbus_secure_element_call_add_route_aid_sync end");
1546
1547         return result;
1548 }
1549
1550 NET_NFC_EXPORT_API
1551 net_nfc_error_e net_nfc_client_se_remove_route_aid_sync(
1552         const char *package, const char *aid)
1553 {
1554         net_nfc_error_e result = NET_NFC_OK;
1555         GError *error = NULL;
1556
1557         if (auto_start_proxy == NULL) {
1558                 GError *error = NULL;
1559
1560                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
1561                         G_BUS_TYPE_SYSTEM,
1562                         G_DBUS_PROXY_FLAGS_NONE,
1563                         "org.tizen.NetNfcService",
1564                         "/org/tizen/NetNfcService/SecureElement",
1565                         NULL,
1566                         &error);
1567                 if (auto_start_proxy == NULL) {
1568                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
1569                         g_error_free(error);
1570
1571                         return NET_NFC_UNKNOWN_ERROR;
1572                 }
1573         }
1574
1575         if (net_nfc_gdbus_secure_element_call_remove_route_aid_sync(
1576                 auto_start_proxy,
1577                 package,
1578                 aid,
1579                 &result,
1580                 NULL,
1581                 &error) == FALSE) {
1582                 DEBUG_ERR_MSG("Remove Route Aid failed: %s", error->message);
1583                 result = NET_NFC_IPC_FAIL;
1584
1585                 g_error_free(error);
1586         }
1587
1588         DEBUG_CLIENT_MSG("net_nfc_gdbus_hce_call_set_route_aid_sync end");
1589
1590         return result;
1591 }
1592
1593 NET_NFC_EXPORT_API
1594 net_nfc_error_e net_nfc_client_se_remove_package_aids_sync(
1595         const char *package)
1596 {
1597         net_nfc_error_e result = NET_NFC_OK;
1598         GError *error = NULL;
1599
1600         if (auto_start_proxy == NULL) {
1601                 GError *error = NULL;
1602
1603                 auto_start_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
1604                         G_BUS_TYPE_SYSTEM,
1605                         G_DBUS_PROXY_FLAGS_NONE,
1606                         "org.tizen.NetNfcService",
1607                         "/org/tizen/NetNfcService/SecureElement",
1608                         NULL,
1609                         &error);
1610                 if (auto_start_proxy == NULL) {
1611                         DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
1612                         g_error_free(error);
1613
1614                         return NET_NFC_UNKNOWN_ERROR;
1615                 }
1616         }
1617
1618         if (net_nfc_gdbus_secure_element_call_remove_package_aids_sync(
1619                 auto_start_proxy,
1620                 package,
1621                 &result,
1622                 NULL,
1623                 &error) == FALSE) {
1624                 DEBUG_ERR_MSG("Remove Package Aid failed: %s", error->message);
1625                 result = NET_NFC_IPC_FAIL;
1626
1627                 g_error_free(error);
1628         }
1629
1630         DEBUG_CLIENT_MSG("net_nfc_client_se_remove_package_aids_sync end");
1631
1632         return result;
1633 }
1634
1635 NET_NFC_EXPORT_API
1636 net_nfc_error_e net_nfc_client_se_set_preferred_handler_sync(bool state)
1637 {
1638         net_nfc_error_e result = NET_NFC_OK;
1639         GError *error = NULL;
1640
1641         if (se_proxy == NULL) {
1642                 result = net_nfc_client_se_init();
1643                 if (result != NET_NFC_OK) {
1644                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1645
1646                         return NET_NFC_NOT_INITIALIZED;
1647                 }
1648         }
1649
1650         if (net_nfc_gdbus_secure_element_call_set_preferred_handler_sync(
1651                 se_proxy,
1652                 state,
1653                 &result,
1654                 NULL, &error) == FALSE) {
1655                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_set_preferred_handler_sync failed : %s", error->message);
1656                 result = NET_NFC_IPC_FAIL;
1657
1658                 g_error_free(error);
1659         }
1660
1661         return result;
1662 }
1663
1664 NET_NFC_EXPORT_API
1665 net_nfc_error_e net_nfc_client_se_get_handler_storage_info_sync(
1666         net_nfc_card_emulation_category_t category, int *used, int *max)
1667 {
1668         net_nfc_error_e result = NET_NFC_OK;
1669         GError *error = NULL;
1670
1671         if (se_proxy == NULL) {
1672                 result = net_nfc_client_se_init();
1673                 if (result != NET_NFC_OK) {
1674                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1675
1676                         return NET_NFC_NOT_INITIALIZED;
1677                 }
1678         }
1679
1680         if (net_nfc_gdbus_secure_element_call_get_handler_storage_info_sync(
1681                 se_proxy,
1682                 category,
1683                 &result,
1684                 used,
1685                 max,
1686                 NULL, &error) == FALSE) {
1687                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_handler_storage_info_sync failed : %s", error->message);
1688                 result = NET_NFC_IPC_FAIL;
1689
1690                 g_error_free(error);
1691         }
1692
1693         return result;
1694 }
1695
1696 NET_NFC_EXPORT_API
1697 net_nfc_error_e net_nfc_client_se_get_conflict_handlers_sync(
1698         const char *package, net_nfc_card_emulation_category_t category,
1699         const char *aid, char ***handlers)
1700 {
1701         net_nfc_error_e result = NET_NFC_OK;
1702         GError *error = NULL;
1703         GVariant *packages = NULL;
1704
1705         if (se_proxy == NULL) {
1706                 result = net_nfc_client_se_init();
1707                 if (result != NET_NFC_OK) {
1708                         DEBUG_ERR_MSG("net_nfc_client_se_init failed, [%d]", result);
1709
1710                         return NET_NFC_NOT_INITIALIZED;
1711                 }
1712         }
1713
1714         if (net_nfc_gdbus_secure_element_call_get_conflict_handlers_sync(
1715                 se_proxy,
1716                 package,
1717                 category,
1718                 aid,
1719                 &result,
1720                 &packages,
1721                 NULL, &error) == true) {
1722                 if (result == NET_NFC_DATA_CONFLICTED) {
1723                         GVariantIter iter;
1724                         gchar **pkgs;
1725                         size_t len;
1726
1727                         g_variant_iter_init(&iter, packages);
1728                         len = g_variant_iter_n_children(&iter);
1729
1730                         SECURE_MSG("conflict count [%d]", len);
1731
1732                         if (len > 0) {
1733                                 size_t i;
1734                                 gchar *temp;
1735
1736                                 pkgs = g_new0(gchar *, len + 1);
1737
1738                                 for (i = 0; i < len; i++) {
1739                                         if (g_variant_iter_next(&iter, "(s)", &temp) == true) {
1740                                                 SECURE_MSG("conflict package [%s]", temp);
1741                                                 pkgs[i] = g_strdup(temp);
1742                                         } else {
1743                                                 DEBUG_ERR_MSG("g_variant_iter_next failed");
1744                                         }
1745                                 }
1746
1747                                 *handlers = pkgs;
1748                         }
1749                 }
1750         } else {
1751                 DEBUG_ERR_MSG("net_nfc_gdbus_secure_element_call_get_conflict_handlers_sync failed : %s", error->message);
1752                 result = NET_NFC_IPC_FAIL;
1753
1754                 g_error_free(error);
1755         }
1756
1757         return result;
1758 }
1759
1760 net_nfc_error_e net_nfc_client_se_init(void)
1761 {
1762         GError *error = NULL;
1763
1764         if (se_proxy) {
1765                 DEBUG_CLIENT_MSG("Already initialized");
1766
1767                 return NET_NFC_OK;
1768         }
1769
1770         se_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
1771                                 G_BUS_TYPE_SYSTEM,
1772                                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
1773                                 "org.tizen.NetNfcService",
1774                                 "/org/tizen/NetNfcService/SecureElement",
1775                                 NULL,
1776                                 &error);
1777         if (se_proxy == NULL) {
1778                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
1779
1780                 g_error_free(error);
1781
1782                 return NET_NFC_UNKNOWN_ERROR;
1783         }
1784
1785         g_signal_connect(se_proxy, "se-type-changed",
1786                 G_CALLBACK(se_type_changed), NULL);
1787
1788         g_signal_connect(se_proxy, "ese-detected",
1789                 G_CALLBACK(se_ese_detected), NULL);
1790
1791         g_signal_connect(se_proxy, "transaction-event",
1792                 G_CALLBACK(se_transaction_event), NULL);
1793
1794         g_signal_connect(se_proxy, "card-emulation-mode-changed",
1795                 G_CALLBACK(se_card_emulation_mode_changed), NULL);
1796
1797         g_signal_connect(se_proxy, "rf-detected",
1798                 G_CALLBACK(se_rf_detected), NULL);
1799
1800         return NET_NFC_OK;
1801 }
1802
1803
1804 void net_nfc_client_se_deinit(void)
1805 {
1806         if (se_proxy) {
1807                 g_object_unref(se_proxy);
1808                 se_proxy = NULL;
1809         }
1810
1811         if (auto_start_proxy) {
1812                 g_object_unref(auto_start_proxy);
1813                 auto_start_proxy = NULL;
1814         }
1815 }
1816
1817 /* LCOV_EXCL_STOP */
1818