Remove build error in Tizen 3.0
[platform/core/connectivity/nfc-manager-neard.git] / client / net_nfc_client_se.c
1 /*
2  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *                               http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <vconf.h>
19
20 #include "net_nfc_typedef_internal.h"
21 #include "net_nfc_debug_internal.h"
22 #include "net_nfc_util_internal.h"
23 #include "net_nfc_util_ndef_message.h"
24 #include "net_nfc_util_gdbus_internal.h"
25 #include "net_nfc_gdbus.h"
26 #include "net_nfc_client.h"
27 #include "net_nfc_client_manager.h"
28 #include "net_nfc_client_se.h"
29
30 typedef struct _SeFuncData SeFuncData;
31
32 struct _SeFuncData
33 {
34         gpointer se_callback;
35         gpointer se_data;
36 };
37
38 typedef struct _SeEventHandler SeEventHandler;
39
40 struct _SeEventHandler
41 {
42         net_nfc_client_se_event se_event_cb;
43         gpointer se_event_data;
44 };
45
46 typedef struct _SeTransEventHandler SeTransEventHandler;
47
48 struct _SeTransEventHandler
49 {
50         net_nfc_se_type_e se_type;
51         net_nfc_client_se_transaction_event eSE_transaction_event_cb;
52         net_nfc_client_se_transaction_event UICC_transaction_event_cb;
53         gpointer eSE_transaction_event_data;
54         gpointer UICC_transaction_event_data;
55 };
56
57 typedef struct _SeESEDetectedHandler SeESEDetectedHandler;
58
59 struct _SeESEDetectedHandler
60 {
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
68 static SeEventHandler se_eventhandler;
69 static SeTransEventHandler se_transeventhandler;
70 static SeESEDetectedHandler se_esedetecthandler;
71
72 static void se_ese_detected(GObject *source_object, guint arg_handle,
73                 gint arg_se_type, GVariant *arg_data)
74 {
75         data_s buffer_data = { NULL, 0 };
76         net_nfc_client_se_ese_detected_event callback;
77
78         NFC_INFO(">>> SIGNAL arrived");
79
80         RET_IF(NULL == se_esedetecthandler.se_ese_detected_cb);
81
82         net_nfc_util_gdbus_variant_to_data_s(arg_data, &buffer_data);
83
84         callback = se_esedetecthandler.se_ese_detected_cb;
85         callback((net_nfc_target_handle_s*)arg_handle, arg_se_type, &buffer_data,
86                         se_esedetecthandler.se_ese_detected_data);
87
88         net_nfc_util_free_data(&buffer_data);
89 }
90
91
92 static void se_type_changed(GObject *source_object, gint arg_se_type)
93 {
94         net_nfc_client_se_event callback;
95
96         NFC_INFO(">>> SIGNAL arrived");
97
98         RET_IF(NULL == se_eventhandler.se_event_cb);
99
100         callback = se_eventhandler.se_event_cb;
101         callback((net_nfc_message_e)NET_NFC_MESSAGE_SE_TYPE_CHANGED,
102                 se_eventhandler.se_event_data);
103 }
104
105
106 static void se_transaction_event(GObject *source_object,
107                 gint arg_se_type,
108                 GVariant *arg_aid,
109                 GVariant *arg_param,
110                 gint fg_dispatch,
111                 gint focus_app_pid)
112 {
113         data_s aid = { NULL, 0 };
114         data_s param = { NULL, 0 };
115
116         NFC_INFO(">>> SIGNAL arrived");
117
118         RET_IF(NULL == se_transeventhandler.eSE_transaction_event_cb);
119         RET_IF(NULL == se_transeventhandler.UICC_transaction_event_cb);
120
121         if (se_transeventhandler.se_type == arg_se_type)
122         {
123                 pid_t mypid = getpid();
124                 if(fg_dispatch == false ||
125                                 (fg_dispatch == true && focus_app_pid == (getpgid(mypid))))
126                 {
127                         net_nfc_util_gdbus_variant_to_data_s(arg_aid, &aid);
128                         net_nfc_util_gdbus_variant_to_data_s(arg_param, &param);
129
130                         net_nfc_util_free_data(&param);
131                         net_nfc_util_free_data(&aid);
132                 }
133         }
134 }
135
136 static void se_card_emulation_mode_changed(GObject *source_object,
137                 gint arg_se_type)
138 {
139         net_nfc_client_se_event callback;
140         NFC_DBG(">>> SIGNAL arrived");
141
142         RET_IF(NULL == se_eventhandler.se_event_cb);
143
144         callback = se_eventhandler.se_event_cb;
145         callback((net_nfc_message_e)NET_NFC_MESSAGE_SE_CARD_EMULATION_CHANGED,
146                         se_eventhandler.se_event_data);
147 }
148
149 static void set_secure_element(GObject *source_object,
150                 GAsyncResult *res, gpointer user_data)
151 {
152         gboolean ret;
153         GError *error = NULL;
154         net_nfc_error_e result;
155         net_nfc_se_set_se_cb se_callback;
156         SeFuncData *func_data = user_data;
157
158         g_assert(user_data != NULL);
159
160         ret = net_nfc_gdbus_secure_element_call_set_finish(se_proxy, &result, res, &error);
161
162         if(FALSE == ret)
163         {
164                 NFC_ERR("Could not set secure element: %s", error->message);
165                 g_error_free(error);
166
167                 result = NET_NFC_IPC_FAIL;
168         }
169
170         if (func_data->se_callback != NULL)
171         {
172                 se_callback = (net_nfc_se_set_se_cb)func_data->se_callback;
173
174                 se_callback(result, func_data->se_data);
175         }
176
177         g_free(func_data);
178 }
179
180 static void get_secure_element(GObject *source_object,
181                 GAsyncResult *res, gpointer user_data)
182 {
183         gboolean ret;
184         gint type = 0;
185         GError *error = NULL;
186         net_nfc_error_e result;
187         net_nfc_se_get_se_cb se_callback;
188         NetNfcCallback *func_data = user_data;
189
190         g_assert(user_data != NULL);
191
192         ret = net_nfc_gdbus_secure_element_call_get_finish(se_proxy,
193                         &result, &type, res, &error);
194
195         if (FALSE == ret)
196         {
197                 NFC_ERR("Could not set secure element: %s", error->message);
198                 g_error_free(error);
199
200                 result = NET_NFC_IPC_FAIL;
201         }
202
203         if (func_data->callback != NULL)
204         {
205                 se_callback = (net_nfc_se_get_se_cb)func_data->callback;
206
207                 se_callback(result, type, func_data->user_data);
208         }
209
210         g_free(func_data);
211 }
212
213 static void _set_card_emulation_cb(GObject *source_object,
214                 GAsyncResult *res, gpointer user_data)
215 {
216         gboolean ret;
217         GError *error = NULL;
218         net_nfc_error_e result;
219         net_nfc_se_set_se_cb se_callback;
220         NetNfcCallback *func_data = user_data;
221
222         g_assert(user_data != NULL);
223
224
225         ret = net_nfc_gdbus_secure_element_call_set_card_emulation_finish(
226                         se_proxy, &result, res, &error);
227
228         if(FALSE == ret)
229         {
230                 NFC_ERR("Could not set card emulation: %s", error->message);
231                 g_error_free(error);
232
233                 result = NET_NFC_IPC_FAIL;
234         }
235
236         if (func_data->callback != NULL)
237         {
238                 se_callback = (net_nfc_se_set_se_cb)func_data->callback;
239
240                 se_callback(result, func_data->user_data);
241         }
242
243         g_free(func_data);
244 }
245
246
247 static void open_secure_element(GObject *source_object,
248                 GAsyncResult *res, gpointer user_data)
249 {
250         gboolean ret;
251         GError *error = NULL;
252         guint out_handle = 0;
253         net_nfc_error_e result;
254         SeFuncData *func_data = user_data;
255         net_nfc_se_open_se_cb se_callback;
256
257         g_assert(user_data != NULL);
258
259         ret = net_nfc_gdbus_secure_element_call_open_secure_element_finish(
260                         se_proxy, &result, &out_handle, res, &error);
261
262         if (FALSE == ret)
263         {
264                 NFC_ERR("Could not open secure element: %s", error->message);
265                 g_error_free(error);
266
267                 result = NET_NFC_IPC_FAIL;
268         }
269
270         if (func_data->se_callback != NULL)
271         {
272                 se_callback = (net_nfc_se_open_se_cb)func_data->se_callback;
273
274                 se_callback(result, (net_nfc_target_handle_s*)out_handle, func_data->se_data);
275         }
276
277         g_free(func_data);
278 }
279
280
281 static void close_secure_element(GObject *source_object,
282                 GAsyncResult *res, gpointer user_data)
283 {
284         gboolean ret;
285         GError *error = NULL;
286         net_nfc_error_e result;
287         SeFuncData *func_data = user_data;
288         net_nfc_se_close_se_cb se_callback;
289
290         g_assert(user_data != NULL);
291
292         ret = net_nfc_gdbus_secure_element_call_close_secure_element_finish(
293                         se_proxy, &result, res, &error);
294
295         if (FALSE == ret)
296         {
297                 NFC_ERR("Could not close secure element: %s", error->message);
298                 g_error_free(error);
299                 result = NET_NFC_IPC_FAIL;
300         }
301
302         if (func_data->se_callback != NULL)
303         {
304                 se_callback = (net_nfc_se_close_se_cb)func_data->se_callback;
305
306                 se_callback(result, func_data->se_data);
307         }
308
309         g_free(func_data);
310 }
311
312
313 static void send_apdu_secure_element(GObject *source_object,
314                 GAsyncResult *res, gpointer user_data)
315 {
316         gboolean ret;
317         GError *error = NULL;
318         net_nfc_error_e result;
319         GVariant *out_response = NULL;
320         SeFuncData *func_data = user_data;
321         net_nfc_se_send_apdu_cb se_callback;
322
323         g_assert(user_data != NULL);
324
325         ret = net_nfc_gdbus_secure_element_call_send_apdu_finish(
326                         se_proxy, &result, &out_response, res, &error);
327
328         if (FALSE == ret)
329         {
330                 NFC_ERR("Could not send apdu: %s", error->message);
331                 g_error_free(error);
332                 result = NET_NFC_IPC_FAIL;
333         }
334
335         if (func_data->se_callback != NULL)
336         {
337                 se_callback = (net_nfc_se_send_apdu_cb)func_data->se_callback;
338                 data_s data = { NULL, };
339
340                 net_nfc_util_gdbus_variant_to_data_s(out_response, &data);
341
342                 se_callback(result, &data, func_data->se_data);
343
344                 net_nfc_util_free_data(&data);
345         }
346
347         g_free(func_data);
348 }
349
350
351 static void get_atr_secure_element(GObject *source_object,
352                 GAsyncResult *res, gpointer user_data)
353 {
354         gboolean ret;
355         GError *error = NULL;
356         net_nfc_error_e result;
357         GVariant *out_atr = NULL;
358         SeFuncData *func_data = user_data;
359         net_nfc_se_get_atr_cb se_callback;
360
361         g_assert(user_data != NULL);
362
363         RET_IF(NULL == func_data->se_callback);
364
365         ret = net_nfc_gdbus_secure_element_call_get_atr_finish(
366                         se_proxy, &result, &out_atr, res, &error);
367
368         if (FALSE == ret)
369         {
370                 NFC_ERR("Could not get atr: %s", error->message);
371                 g_error_free(error);
372                 result = NET_NFC_IPC_FAIL;
373         }
374
375         if (func_data->se_callback != NULL)
376         {
377                 se_callback = (net_nfc_se_get_atr_cb)func_data->se_callback;
378                 data_s data = { NULL, };
379
380                 net_nfc_util_gdbus_variant_to_data_s(out_atr, &data);
381
382                 se_callback(result, &data, func_data->se_data);
383
384                 net_nfc_util_free_data(&data);
385         }
386
387         g_free(func_data);
388 }
389
390
391 API net_nfc_error_e net_nfc_client_se_set_secure_element_type(
392                 net_nfc_se_type_e se_type, net_nfc_se_set_se_cb callback, void *user_data)
393 {
394         SeFuncData *func_data;
395
396         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
397
398         /* prevent executing daemon when nfc is off */
399         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
400
401         func_data = g_try_new0(SeFuncData, 1);
402         if (NULL == func_data)
403                 return NET_NFC_ALLOC_FAIL;
404
405         func_data->se_callback = (gpointer)callback;
406         func_data->se_data = user_data;
407
408         net_nfc_gdbus_secure_element_call_set(se_proxy, (gint)se_type,
409                         net_nfc_client_gdbus_get_privilege(), NULL, set_secure_element, func_data);
410
411         return NET_NFC_OK;
412 }
413
414
415 API net_nfc_error_e net_nfc_client_se_set_secure_element_type_sync(
416                 net_nfc_se_type_e se_type)
417 {
418         gboolean ret;
419         GError *error = NULL;
420         net_nfc_error_e result = NET_NFC_OK;
421
422         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
423
424         /* prevent executing daemon when nfc is off */
425         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
426
427         ret = net_nfc_gdbus_secure_element_call_set_sync(se_proxy, (gint)se_type,
428                         net_nfc_client_gdbus_get_privilege(), &result, NULL, &error);
429
430         if (FALSE == ret)
431         {
432                 NFC_ERR("Set secure element failed: %s", error->message);
433                 g_error_free(error);
434                 result = NET_NFC_IPC_FAIL;
435         }
436
437         return result;
438 }
439
440 API net_nfc_error_e net_nfc_client_se_get_secure_element_type(
441                 net_nfc_se_get_se_cb callback, void *user_data)
442 {
443         NetNfcCallback *func_data;
444
445         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
446
447         /* prevent executing daemon when nfc is off */
448         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
449
450         func_data = g_try_new0(NetNfcCallback, 1);
451         if (NULL == func_data)
452                 return NET_NFC_ALLOC_FAIL;
453
454         func_data->callback = (gpointer)callback;
455         func_data->user_data = user_data;
456
457         net_nfc_gdbus_secure_element_call_get(se_proxy, net_nfc_client_gdbus_get_privilege(),
458                         NULL, get_secure_element, func_data);
459
460         return NET_NFC_OK;
461 }
462
463 API net_nfc_error_e net_nfc_client_se_get_secure_element_type_sync(
464                 net_nfc_se_type_e *se_type)
465 {
466         gint type;
467         net_nfc_error_e result = NET_NFC_OK;
468
469 #if 0
470         gboolean ret;
471         GError *error = NULL;
472 #endif
473         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
474
475         /* prevent executing daemon when nfc is off */
476         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
477
478 #if 1
479         if (vconf_get_int(VCONFKEY_NFC_SE_TYPE, &type) == 0)
480                 *se_type = type;
481         else
482                 result = NET_NFC_OPERATION_FAIL;
483 #else
484         ret = net_nfc_gdbus_secure_element_call_get_sync(se_proxy,
485                         net_nfc_client_gdbus_get_privilege(), &result, (gint)&type, NULL, &error);
486
487         if (TRUE == ret)
488         {
489                 *se_type = type;
490         }
491         else
492         {
493                 NFC_ERR("Set secure element failed: %s", error->message);
494
495                 g_error_free(error);
496
497                 result = NET_NFC_IPC_FAIL;
498         }
499 #endif
500         return result;
501 }
502
503 API net_nfc_error_e net_nfc_set_card_emulation_mode(
504                 net_nfc_card_emulation_mode_t mode,
505                 net_nfc_se_set_card_emulation_cb callback,
506                 void *user_data)
507 {
508         NetNfcCallback *func_data;
509
510         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
511
512         /* prevent executing daemon when nfc is off */
513         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
514
515         func_data = g_try_new0(NetNfcCallback, 1);
516         if (NULL == func_data)
517                 return NET_NFC_ALLOC_FAIL;
518
519         func_data->callback = (gpointer)callback;
520         func_data->user_data = user_data;
521
522         net_nfc_gdbus_secure_element_call_set_card_emulation(se_proxy, (gint)mode,
523                         net_nfc_client_gdbus_get_privilege(), NULL, _set_card_emulation_cb, func_data);
524
525         return NET_NFC_OK;
526 }
527
528 API net_nfc_error_e net_nfc_get_card_emulation_mode(net_nfc_se_type_e *type)
529 {
530         int ret;
531         int se_type;
532         net_nfc_error_e result = NET_NFC_OK;
533
534         ret = vconf_get_int(VCONFKEY_NFC_SE_TYPE, &se_type);
535
536         if (0 == ret)
537         {
538                 switch(se_type)
539                 {
540                         case VCONFKEY_NFC_SE_POLICY_UICC_ON:
541                                 *type = NET_NFC_SE_TYPE_UICC;
542                                 break;
543
544                         case VCONFKEY_NFC_SE_POLICY_ESE_ON:
545                                 *type = NET_NFC_SE_TYPE_ESE;
546                                 break;
547
548                         default :
549                                 *type = NET_NFC_SE_TYPE_NONE;
550                                 break;
551                 }
552         }
553         else
554         {
555                 result = NET_NFC_UNKNOWN_ERROR;
556         }
557
558         return result;
559 }
560
561 API net_nfc_error_e net_nfc_set_card_emulation_mode_sync(
562                 net_nfc_card_emulation_mode_t mode)
563 {
564         gboolean ret;
565         GError *error = NULL;
566         net_nfc_error_e result = NET_NFC_OK;
567
568         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
569
570         /* prevent executing daemon when nfc is off */
571         RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
572
573         ret = net_nfc_gdbus_secure_element_call_set_card_emulation_sync(se_proxy, (gint)mode,
574                         net_nfc_client_gdbus_get_privilege(), &result, NULL, &error);
575
576         if (FALSE == ret)
577         {
578                 NFC_ERR("Set card emulation failed: %s", error->message);
579                 g_error_free(error);
580                 result = NET_NFC_IPC_FAIL;
581         }
582
583         return result;
584 }
585
586
587 API net_nfc_error_e net_nfc_client_se_open_internal_secure_element(
588                 net_nfc_se_type_e se_type, net_nfc_se_open_se_cb callback, void *user_data)
589 {
590         SeFuncData *func_data;
591
592         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
593
594         /* allow this function even nfc is off */
595
596         func_data = g_try_new0(SeFuncData, 1);
597         if (NULL == func_data)
598                 return NET_NFC_ALLOC_FAIL;
599
600         func_data->se_callback = (gpointer)callback;
601         func_data->se_data = user_data;
602
603         net_nfc_gdbus_secure_element_call_open_secure_element(se_proxy, (gint)se_type,
604                         net_nfc_client_gdbus_get_privilege(), NULL, open_secure_element, func_data);
605
606         return NET_NFC_OK;
607 }
608
609
610 API net_nfc_error_e net_nfc_client_se_open_internal_secure_element_sync(
611                 net_nfc_se_type_e se_type, net_nfc_target_handle_s **handle)
612 {
613         gboolean ret;
614         guint out_handle = 0;
615         GError *error =  NULL;
616         net_nfc_error_e result = NET_NFC_OK;
617
618         RETV_IF(NULL == handle, NET_NFC_NULL_PARAMETER);
619         RETV_IF(NULL == se_proxy, NET_NFC_NOT_INITIALIZED);
620
621         /* allow this function even nfc is off */
622
623         ret = net_nfc_gdbus_secure_element_call_open_secure_element_sync(se_proxy, se_type,
624                         net_nfc_client_gdbus_get_privilege(), &result, &out_handle, NULL, &error);
625
626         if (TRUE == ret)
627         {
628                 *handle = GUINT_TO_POINTER(out_handle);
629         }
630         else
631         {
632                 NFC_ERR("Open internal secure element failed: %s", error->message);
633                 g_error_free(error);
634
635                 result = NET_NFC_IPC_FAIL;
636         }
637
638         return result;
639 }
640
641
642 API net_nfc_error_e net_nfc_client_se_close_internal_secure_element(
643                 net_nfc_target_handle_s *handle, net_nfc_se_close_se_cb callback, void *user_data)
644 {
645         GError *error = NULL;
646         SeFuncData *func_data;
647         NetNfcGDbusSecureElement* auto_proxy;
648
649         if(se_proxy != NULL)
650         {
651                 auto_proxy = se_proxy;
652         }
653         else
654         {
655                 auto_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
656                                 G_BUS_TYPE_SYSTEM,
657                                 G_DBUS_PROXY_FLAGS_NONE,
658                                 "org.tizen.NetNfcService",
659                                 "/org/tizen/NetNfcService/SecureElement",
660                                 NULL,
661                                 &error);
662                 if(NULL == auto_proxy)
663                 {
664                         NFC_ERR("Can not create proxy : %s", error->message);
665                         g_error_free(error);
666
667                         return NET_NFC_UNKNOWN_ERROR;
668                 }
669         }
670
671         /* allow this function even nfc is off */
672
673         func_data = g_try_new0(SeFuncData, 1);
674         if (NULL == func_data)
675         {
676                 g_object_unref(auto_proxy);
677                 return NET_NFC_ALLOC_FAIL;
678         }
679
680         func_data->se_callback = (gpointer)callback;
681         func_data->se_data = user_data;
682
683         net_nfc_gdbus_secure_element_call_close_secure_element(
684                         auto_proxy,
685                         GPOINTER_TO_UINT(handle),
686                         net_nfc_client_gdbus_get_privilege(),
687                         NULL,
688                         close_secure_element,
689                         func_data);
690
691         g_object_unref(auto_proxy);
692         return NET_NFC_OK;
693 }
694
695
696 API net_nfc_error_e net_nfc_client_se_close_internal_secure_element_sync(
697                 net_nfc_target_handle_s *handle)
698 {
699         gboolean ret;
700         GError *error = NULL;
701         net_nfc_error_e result = NET_NFC_OK;
702         NetNfcGDbusSecureElement* auto_proxy;
703
704         if(se_proxy != NULL)
705         {
706                 auto_proxy = se_proxy;
707         }
708         else
709         {
710                 auto_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 (NULL == auto_proxy)
718                 {
719                         NFC_ERR("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         ret = net_nfc_gdbus_secure_element_call_close_secure_element_sync(
729                         auto_proxy,
730                         GPOINTER_TO_UINT(handle),
731                         net_nfc_client_gdbus_get_privilege(),
732                         &result,
733                         NULL,
734                         &error);
735
736         if (FALSE == ret)
737         {
738                 NFC_ERR("close internal secure element failed: %s", error->message);
739                 g_error_free(error);
740                 result = NET_NFC_IPC_FAIL;
741         }
742
743         g_object_unref(auto_proxy);
744         return result;
745 }
746
747
748 API net_nfc_error_e net_nfc_client_se_get_atr(net_nfc_target_handle_s *handle,
749                 net_nfc_se_get_atr_cb callback, void *user_data)
750 {
751         GError *error = NULL;
752         SeFuncData *func_data;
753         NetNfcGDbusSecureElement* auto_proxy;
754
755         if(se_proxy != NULL)
756         {
757                 auto_proxy = se_proxy;
758         }
759         else
760         {
761                 auto_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
762                                 G_BUS_TYPE_SYSTEM,
763                                 G_DBUS_PROXY_FLAGS_NONE,
764                                 "org.tizen.NetNfcService",
765                                 "/org/tizen/NetNfcService/SecureElement",
766                                 NULL,
767                                 &error);
768                 if (NULL == auto_proxy)
769                 {
770                         NFC_ERR("Can not create proxy : %s", error->message);
771                         g_error_free(error);
772
773                         return NET_NFC_UNKNOWN_ERROR;
774                 }
775         }
776
777         /* allow this function even nfc is off */
778
779         func_data = g_try_new0(SeFuncData, 1);
780         if (NULL == func_data)
781         {
782                 g_object_unref(auto_proxy);
783                 return NET_NFC_ALLOC_FAIL;
784         }
785
786         func_data->se_callback = (gpointer)callback;
787         func_data->se_data = user_data;
788
789         net_nfc_gdbus_secure_element_call_get_atr(
790                         auto_proxy,
791                         GPOINTER_TO_UINT(handle),
792                         net_nfc_client_gdbus_get_privilege(),
793                         NULL,
794                         get_atr_secure_element,
795                         func_data);
796
797         g_object_unref(auto_proxy);
798         return NET_NFC_OK;
799 }
800
801
802 API net_nfc_error_e net_nfc_client_se_get_atr_sync(
803                 net_nfc_target_handle_s *handle, data_s **atr)
804 {
805         gboolean ret;
806         GError *error = NULL;
807         GVariant *out_atr = NULL;
808         net_nfc_error_e result = NET_NFC_OK;
809         NetNfcGDbusSecureElement* auto_proxy;
810
811         RETV_IF(NULL == atr, NET_NFC_NULL_PARAMETER);
812
813         if(se_proxy != NULL)
814         {
815                 auto_proxy = se_proxy;
816         }
817         else
818         {
819                 auto_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
820                                 G_BUS_TYPE_SYSTEM,
821                                 G_DBUS_PROXY_FLAGS_NONE,
822                                 "org.tizen.NetNfcService",
823                                 "/org/tizen/NetNfcService/SecureElement",
824                                 NULL,
825                                 &error);
826                 if (NULL == auto_proxy)
827                 {
828                         NFC_DBG("Can not create proxy : %s", error->message);
829                         g_error_free(error);
830
831                         return NET_NFC_UNKNOWN_ERROR;
832                 }
833         }
834
835         *atr = NULL;
836
837         /* allow this function even nfc is off */
838
839         ret = net_nfc_gdbus_secure_element_call_get_atr_sync(
840                         auto_proxy,
841                         GPOINTER_TO_UINT(handle),
842                         net_nfc_client_gdbus_get_privilege(),
843                         &result,
844                         &out_atr,
845                         NULL,
846                         &error);
847
848         if (TRUE == ret)
849         {
850                 *atr = net_nfc_util_gdbus_variant_to_data(out_atr);
851         }
852         else
853         {
854                 NFC_ERR("Get attributes failed: %s", error->message);
855                 g_error_free(error);
856                 result = NET_NFC_IPC_FAIL;
857         }
858
859         g_object_unref(auto_proxy);
860         return result;
861 }
862
863
864 API net_nfc_error_e net_nfc_client_se_send_apdu(net_nfc_target_handle_s *handle,
865                 data_s *apdu_data, net_nfc_se_send_apdu_cb callback, void *user_data)
866 {
867         GVariant *arg_data;
868         GError *error = NULL;
869         SeFuncData *func_data;
870         NetNfcGDbusSecureElement* auto_proxy;
871
872         if(se_proxy != NULL)
873         {
874                 auto_proxy = se_proxy;
875         }
876         else
877         {
878                 auto_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
879                                 G_BUS_TYPE_SYSTEM,
880                                 G_DBUS_PROXY_FLAGS_NONE,
881                                 "org.tizen.NetNfcService",
882                                 "/org/tizen/NetNfcService/SecureElement",
883                                 NULL,
884                                 &error);
885                 if (NULL == auto_proxy)
886                 {
887                         NFC_ERR("Can not create proxy : %s", error->message);
888                         g_error_free(error);
889
890                         return NET_NFC_UNKNOWN_ERROR;
891                 }
892         }
893
894         /* allow this function even nfc is off */
895
896         arg_data = net_nfc_util_gdbus_data_to_variant(apdu_data);
897         if (arg_data == NULL)
898         {
899                 g_object_unref(auto_proxy);
900                 return NET_NFC_INVALID_PARAM;
901         }
902
903         func_data = g_try_new0(SeFuncData, 1);
904         if (NULL == func_data)
905         {
906                 g_variant_unref(arg_data);
907                 g_object_unref(auto_proxy);
908                 return NET_NFC_ALLOC_FAIL;
909         }
910
911         func_data->se_callback = (gpointer)callback;
912         func_data->se_data = user_data;
913
914         net_nfc_gdbus_secure_element_call_send_apdu(
915                         auto_proxy,
916                         GPOINTER_TO_UINT(handle),
917                         arg_data,
918                         net_nfc_client_gdbus_get_privilege(),
919                         NULL,
920                         send_apdu_secure_element,
921                         func_data);
922
923         g_object_unref(auto_proxy);
924         return NET_NFC_OK;
925 }
926
927
928 API net_nfc_error_e net_nfc_client_se_send_apdu_sync(
929                 net_nfc_target_handle_s *handle, data_s *apdu_data, data_s **response)
930 {
931         gboolean ret;
932         GVariant *arg_data;
933         GError *error = NULL;
934         GVariant *out_data = NULL;
935         net_nfc_error_e result = NET_NFC_OK;
936         NetNfcGDbusSecureElement* auto_proxy;
937
938         RETV_IF(NULL == response, NET_NFC_NULL_PARAMETER);
939
940         if(se_proxy != NULL)
941         {
942                 auto_proxy = se_proxy;
943         }
944         else
945         {
946                 GError *error = NULL;
947
948                 auto_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
949                                 G_BUS_TYPE_SYSTEM,
950                                 G_DBUS_PROXY_FLAGS_NONE,
951                                 "org.tizen.NetNfcService",
952                                 "/org/tizen/NetNfcService/SecureElement",
953                                 NULL,
954                                 &error);
955                 if (NULL == auto_proxy)
956                 {
957                         NFC_ERR("Can not create proxy : %s", error->message);
958                         g_error_free(error);
959
960                         return NET_NFC_UNKNOWN_ERROR;
961                 }
962         }
963
964         *response = NULL;
965
966         /* allow this function even nfc is off */
967
968         arg_data = net_nfc_util_gdbus_data_to_variant(apdu_data);
969         if (NULL == arg_data)
970         {
971                 g_object_unref(auto_proxy);
972                 return NET_NFC_INVALID_PARAM;
973         }
974
975         ret = net_nfc_gdbus_secure_element_call_send_apdu_sync(
976                         auto_proxy,
977                         GPOINTER_TO_UINT(handle),
978                         arg_data,
979                         net_nfc_client_gdbus_get_privilege(),
980                         &result,
981                         &out_data,
982                         NULL,
983                         &error);
984
985         if (TRUE == ret)
986         {
987                 *response = net_nfc_util_gdbus_variant_to_data(out_data);
988         }
989         else
990         {
991                 NFC_ERR("Send APDU failed: %s", error->message);
992                 g_error_free(error);
993                 result = NET_NFC_IPC_FAIL;
994         }
995
996         g_object_unref(auto_proxy);
997         return result;
998 }
999
1000
1001 API void net_nfc_client_se_set_ese_detection_cb(
1002                 net_nfc_client_se_ese_detected_event callback, void *user_data)
1003 {
1004         se_esedetecthandler.se_ese_detected_cb = callback;
1005         se_esedetecthandler.se_ese_detected_data = user_data;
1006 }
1007
1008
1009 API void net_nfc_client_se_unset_ese_detection_cb(void)
1010 {
1011         net_nfc_client_se_set_ese_detection_cb(NULL, NULL);
1012 }
1013
1014
1015 API void net_nfc_client_se_set_transaction_event_cb(
1016                 net_nfc_se_type_e se_type,
1017                 net_nfc_client_se_transaction_event callback,
1018                 void *user_data)
1019 {
1020         se_transeventhandler.se_type = se_type;
1021
1022         if(se_type == NET_NFC_SE_TYPE_ESE)
1023         {
1024                 se_transeventhandler.eSE_transaction_event_cb = callback;
1025                 se_transeventhandler.eSE_transaction_event_data = user_data;
1026         }
1027         else if(se_type == NET_NFC_SE_TYPE_UICC)
1028         {
1029                 se_transeventhandler.UICC_transaction_event_cb = callback;
1030                 se_transeventhandler.UICC_transaction_event_data = user_data;
1031         }
1032 }
1033
1034
1035 API void net_nfc_client_se_unset_transaction_event_cb(net_nfc_se_type_e type)
1036 {
1037         net_nfc_client_se_set_transaction_event_cb(type, NULL, NULL);
1038         net_nfc_client_se_set_transaction_event_cb(NET_NFC_SE_TYPE_NONE, NULL, NULL);
1039 }
1040
1041
1042 API void net_nfc_client_se_set_event_cb(net_nfc_client_se_event callback,
1043                 void *user_data)
1044 {
1045         se_eventhandler.se_event_cb = callback;
1046         se_eventhandler.se_event_data = user_data;
1047 }
1048
1049
1050 API void net_nfc_client_se_unset_event_cb(void)
1051 {
1052         net_nfc_client_se_set_event_cb(NULL, NULL);
1053 }
1054
1055
1056 net_nfc_error_e net_nfc_client_se_init(void)
1057 {
1058         GError *error = NULL;
1059
1060         if (se_proxy)
1061         {
1062                 NFC_WARN("Already initialized");
1063                 return NET_NFC_OK;
1064         }
1065
1066         se_proxy = net_nfc_gdbus_secure_element_proxy_new_for_bus_sync(
1067                         G_BUS_TYPE_SYSTEM,
1068                         G_DBUS_PROXY_FLAGS_NONE,
1069                         "org.tizen.NetNfcService",
1070                         "/org/tizen/NetNfcService/SecureElement",
1071                         NULL,
1072                         &error);
1073         if (NULL == se_proxy)
1074         {
1075                 NFC_ERR("Can not create proxy : %s", error->message);
1076
1077                 g_error_free(error);
1078
1079                 return NET_NFC_UNKNOWN_ERROR;
1080         }
1081
1082         g_signal_connect(se_proxy, "se-type-changed", G_CALLBACK(se_type_changed), NULL);
1083         g_signal_connect(se_proxy, "ese-detected", G_CALLBACK(se_ese_detected), NULL);
1084
1085         g_signal_connect(se_proxy, "transaction-event",
1086                         G_CALLBACK(se_transaction_event), NULL);
1087
1088         g_signal_connect(se_proxy, "card-emulation-mode-changed",
1089                         G_CALLBACK(se_card_emulation_mode_changed), NULL);
1090
1091         return NET_NFC_OK;
1092 }
1093
1094
1095 void net_nfc_client_se_deinit(void)
1096 {
1097         if (se_proxy)
1098         {
1099                 g_object_unref(se_proxy);
1100                 se_proxy = NULL;
1101         }
1102 }