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