Sync code with Tizen 3.0 branch
[platform/core/api/nfc.git] / src / nfc_manager.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 #include "nfc_common.h"
18
19 bool nfc_manager_is_supported(void)
20 {
21         bool nfc_supported = false;
22
23         LOG_BEGIN();
24
25         nfc_supported = nfc_common_is_supported(NFC_FEATURE);
26
27         if (nfc_supported == true)
28                 set_last_result(NFC_ERROR_NONE);
29         else
30                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
31
32         return nfc_supported;
33 }
34
35 /* LCOV_EXCL_START */
36 static void _activation_changed_cb(net_nfc_error_e result, void *user_data)
37 {
38         nfc_activation_data_s *data = user_data;
39
40         LOG_BEGIN();
41
42         if (user_data == NULL) {
43                 LOG_ERR("user_data is NULL");
44                 return;
45         }
46
47         if (data->callback != NULL)
48                 data->callback(nfc_common_convert_error_code(__func__, result), data->user_data);
49
50         free(data);
51 }
52 /* LCOV_EXCL_STOP */
53
54 int nfc_manager_set_activation(bool activation,
55         nfc_activation_completed_cb callback,
56         void *user_data)
57 {
58         int ret;
59
60         LOG_BEGIN();
61
62         CHECK_SUPPORTED(NFC_FEATURE);
63
64         /* LCOV_EXCL_START */
65         CHECK_INIT();
66
67         if (nfc_manager_is_activated() == activation) {
68                 if (activation)
69                         ret = NFC_ERROR_ALREADY_ACTIVATED;
70                 else
71                         ret = NFC_ERROR_ALREADY_DEACTIVATED;
72         } else {
73                 nfc_activation_data_s *data = NULL;
74
75                 data = (nfc_activation_data_s *)malloc(sizeof(nfc_activation_data_s));
76
77                 if (data != NULL) {
78                         data->callback = callback;
79                         data->user_data = user_data;
80                         ret = net_nfc_client_manager_set_active(activation,
81                                 _activation_changed_cb,
82                                 data);
83                         if (ret != NET_NFC_OK) {
84                                 LOG_ERR("net_nfc_client_manager_set_active fail");
85
86                                 free(data);
87                         }
88                 } else {
89                         ret = NET_NFC_ALLOC_FAIL;
90                 }
91
92                 ret = nfc_common_convert_error_code(__func__, ret);
93         }
94
95         return ret;
96         /* LCOV_EXCL_STOP */
97 }
98
99 bool nfc_manager_is_activated(void)
100 {
101         int ret;
102         int activated = 0;
103
104         LOG_BEGIN();
105
106         if (nfc_common_is_supported(NFC_FEATURE) == false) {
107                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
108                 return false;
109         }
110
111         /* LCOV_EXCL_START */
112         ret = net_nfc_client_get_nfc_state(&activated);
113
114         set_last_result(nfc_common_convert_error_code(__func__, ret));
115
116         return (!!activated);
117         /* LCOV_EXCL_STOP */
118 }
119
120 int nfc_manager_set_activation_changed_cb(nfc_activation_changed_cb callback,
121         void *user_data)
122 {
123         LOG_BEGIN();
124
125         CHECK_SUPPORTED(NFC_FEATURE);
126
127         /* LCOV_EXCL_START */
128         CHECK_INIT();
129         CHECK_INVALID(callback == NULL);
130
131         net_nfc_client_manager_set_activated(callback, user_data);
132
133         return NFC_ERROR_NONE;
134         /* LCOV_EXCL_STOP */
135 }
136
137 void nfc_manager_unset_activation_changed_cb(void)
138 {
139         LOG_BEGIN();
140
141         if (nfc_common_is_supported(NFC_FEATURE) == false) {
142                 LOG_ERR("NFC not supported");
143                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
144                 return;
145         }
146
147         /* LCOV_EXCL_START */
148         if (nfc_common_is_initialized() == false) {
149                 LOG_ERR("NFC not initialized");
150                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
151                 return;
152         }
153
154         net_nfc_client_manager_unset_activated();
155
156         set_last_result(NFC_ERROR_NONE);
157         /* LCOV_EXCL_STOP */
158 }
159
160 int nfc_manager_initialize(void)
161 {
162         int ret = NET_NFC_OK;
163
164         LOG_BEGIN();
165
166         CHECK_SUPPORTED(NFC_FEATURE);
167
168         /* LCOV_EXCL_START */
169         if (!nfc_common_is_initialized()) {
170                 ret = net_nfc_client_initialize();
171                 if (ret != NET_NFC_OK)
172                         return nfc_common_convert_error_code(__func__, ret);
173
174                 memset(&gdbus_nfc_context, 0, sizeof(gdbus_nfc_context));
175
176                 gdbus_nfc_context.initialized = true;
177         }
178
179         return nfc_common_convert_error_code(__func__, ret);
180         /* LCOV_EXCL_STOP */
181 }
182
183 int nfc_manager_deinitialize(void)
184 {
185         int ret = NET_NFC_OK;
186
187         LOG_BEGIN();
188
189         CHECK_SUPPORTED(NFC_FEATURE);
190
191         /* LCOV_EXCL_START */
192         if (nfc_common_is_initialized()) {
193                 net_nfc_client_se_unset_event_cb();
194
195                 net_nfc_client_p2p_unset_device_discovered();
196                 net_nfc_client_p2p_unset_device_detached();
197
198                 net_nfc_client_tag_unset_tag_discovered();
199                 net_nfc_client_tag_unset_tag_detached();
200
201                 ret = net_nfc_client_deinitialize();
202
203                 gdbus_nfc_context.initialized = false;
204         }
205
206         return nfc_common_convert_error_code(__func__, ret);
207         /* LCOV_EXCL_STOP */
208 }
209
210 /* LCOV_EXCL_START */
211 static void _tag_discovered_cb(net_nfc_target_info_h info, void *user_data)
212 {
213         LOG_BEGIN();
214
215         gdbus_nfc_context.current_tag = info;
216
217         if (gdbus_nfc_context.on_tag_discovered_cb != NULL) {
218                 gdbus_nfc_context.on_tag_discovered_cb(
219                         NFC_DISCOVERED_TYPE_ATTACHED,
220                         (nfc_tag_h)gdbus_nfc_context.current_tag,
221                         gdbus_nfc_context.on_tag_discovered_user_data);
222         }
223
224         /* ndef discovered cb */
225         if (gdbus_nfc_context.on_ndef_discovered_cb) {
226                 ndef_message_h ndef_message = NULL;
227
228                 if (net_nfc_get_tag_ndef_message((net_nfc_target_info_h)info,
229                         &ndef_message) == NET_NFC_OK) {
230                         gdbus_nfc_context.on_ndef_discovered_cb(ndef_message,
231                                 gdbus_nfc_context.on_ndef_discovered_user_data);
232
233                         net_nfc_free_ndef_message(ndef_message);
234                 }
235         }
236 }
237
238 static void _tag_detached_cb(void *user_data)
239 {
240         LOG_BEGIN();
241
242         if (gdbus_nfc_context.on_tag_discovered_cb != NULL) {
243                 gdbus_nfc_context.on_tag_discovered_cb(
244                         NFC_DISCOVERED_TYPE_DETACHED,
245                         (nfc_tag_h)gdbus_nfc_context.current_tag,
246                         gdbus_nfc_context.on_tag_discovered_user_data);
247         }
248
249         gdbus_nfc_context.current_tag = NULL;
250 }
251 /* LCOV_EXCL_STOP */
252
253 int nfc_manager_set_ndef_discovered_cb(
254         nfc_ndef_discovered_cb callback,
255         void *user_data)
256 {
257         LOG_BEGIN();
258
259         CHECK_SUPPORTED(NFC_FEATURE);
260
261         /* LCOV_EXCL_START */
262         CHECK_INIT();
263         CHECK_INVALID(callback == NULL);
264
265         net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
266         net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
267
268         gdbus_nfc_context.on_ndef_discovered_cb = callback;
269         gdbus_nfc_context.on_ndef_discovered_user_data = user_data;
270
271         return NFC_ERROR_NONE;
272         /* LCOV_EXCL_STOP */
273 }
274
275 void nfc_manager_unset_ndef_discovered_cb(void)
276 {
277         LOG_BEGIN();
278
279         if (nfc_common_is_supported(NFC_FEATURE) == false) {
280                 LOG_ERR("NFC not supported");
281                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
282                 return;
283         }
284
285         /* LCOV_EXCL_START */
286         if (nfc_common_is_initialized() == false) {
287                 LOG_ERR("NFC not initialized");
288                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
289                 return;
290         }
291
292         gdbus_nfc_context.on_ndef_discovered_cb = NULL;
293         gdbus_nfc_context.on_ndef_discovered_user_data = NULL;
294
295         set_last_result(NFC_ERROR_NONE);
296         /* LCOV_EXCL_STOP */
297 }
298
299 void nfc_manager_set_tag_filter(int filter)
300 {
301         LOG_BEGIN();
302
303         if (nfc_common_is_supported(NFC_TAG_FEATURE) == false) {
304                 LOG_ERR("NFC not supported");
305                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
306                 return;
307         }
308
309         /* LCOV_EXCL_START */
310         if (nfc_common_is_initialized() == false) {
311                 LOG_ERR("NFC not initialized");
312                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
313                 return;
314         }
315
316         if (filter < NET_NFC_ALL_ENABLE) {
317                 LOG_ERR("Invalid parameter");
318                 set_last_result(NFC_ERROR_INVALID_PARAMETER);
319                 return;
320         }
321
322         net_nfc_client_tag_set_filter(filter);
323
324         set_last_result(NFC_ERROR_NONE);
325         /* LCOV_EXCL_STOP */
326 }
327
328 int nfc_manager_get_tag_filter(void)
329 {
330         LOG_BEGIN();
331
332         if (nfc_common_is_supported(NFC_TAG_FEATURE) == false) {
333                 LOG_ERR("NFC not supported");
334                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
335                 return 0;
336         }
337
338         /* LCOV_EXCL_START */
339         if (nfc_common_is_initialized() == false) {
340                 LOG_ERR("NFC not initialized");
341                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
342                 return 0;
343         }
344
345         set_last_result(NFC_ERROR_NONE);
346
347         return net_nfc_client_tag_get_filter();
348         /* LCOV_EXCL_STOP */
349 }
350
351 int nfc_manager_get_connected_tag(nfc_tag_h *tag)
352 {
353         int ret;
354         net_nfc_target_info_h result = NULL;
355
356         LOG_BEGIN();
357
358         CHECK_SUPPORTED(NFC_TAG_FEATURE);
359
360         /* LCOV_EXCL_START */
361         CHECK_INIT();
362         CHECK_INVALID(tag == NULL);
363
364         *tag = NULL;
365
366         net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
367         net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
368
369
370         if (gdbus_nfc_context.current_tag == NULL) {
371                 ret = net_nfc_client_tag_get_current_tag_info_sync(&result);
372                 if (ret == NET_NFC_OK)
373                         *tag = (nfc_tag_h)result;
374         } else {
375                 /* FIXME ??? */
376                 *tag = gdbus_nfc_context.current_tag;
377
378                 ret = NET_NFC_OK;
379         }
380
381         return nfc_common_convert_error_code(__func__, ret);
382         /* LCOV_EXCL_STOP */
383 }
384
385 int nfc_manager_get_connected_target(nfc_p2p_target_h *target)
386 {
387         int ret;
388         net_nfc_target_handle_h result = NULL;
389
390         LOG_BEGIN();
391
392         CHECK_SUPPORTED(NFC_P2P_FEATURE);
393
394         /* LCOV_EXCL_START */
395         CHECK_INIT();
396         CHECK_INVALID(target == NULL);
397
398         *target = NULL;
399
400         net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
401         net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
402
403         if (gdbus_nfc_context.current_target == NULL) {
404                 ret = net_nfc_client_tag_get_current_target_handle_sync(&result);
405                 if (ret == NET_NFC_OK) {
406                         gdbus_nfc_context.current_target = result;
407
408                         *target = gdbus_nfc_context.current_target;
409                 }
410         } else {
411                 /* FIXME ??? */
412                 *target = gdbus_nfc_context.current_target;
413
414                 ret = NET_NFC_OK;
415         }
416
417         return nfc_common_convert_error_code(__func__, ret);
418         /* LCOV_EXCL_STOP */
419 }
420
421 int nfc_manager_set_tag_discovered_cb(nfc_tag_discovered_cb callback,
422         void *user_data)
423 {
424         LOG_BEGIN();
425
426         CHECK_SUPPORTED(NFC_TAG_FEATURE);
427
428         /* LCOV_EXCL_START */
429         CHECK_INIT();
430         CHECK_INVALID(callback == NULL);
431
432         net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
433         net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
434
435         gdbus_nfc_context.on_tag_discovered_cb = callback;
436         gdbus_nfc_context.on_tag_discovered_user_data = user_data;
437
438         return NFC_ERROR_NONE;
439         /* LCOV_EXCL_STOP */
440 }
441
442 void nfc_manager_unset_tag_discovered_cb(void)
443 {
444         LOG_BEGIN();
445
446         if (nfc_common_is_supported(NFC_TAG_FEATURE) == false) {
447                 LOG_ERR("NFC not supported");
448                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
449                 return;
450         }
451
452         /* LCOV_EXCL_START */
453         if (nfc_common_is_initialized() == false) {
454                 LOG_ERR("NFC not initialized");
455                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
456                 return;
457         }
458
459         gdbus_nfc_context.on_tag_discovered_cb = NULL;
460         gdbus_nfc_context.on_tag_discovered_user_data = NULL;
461
462         set_last_result(NFC_ERROR_NONE);
463         /* LCOV_EXCL_STOP */
464 }
465
466 /* LCOV_EXCL_START */
467 static void _p2p_discovered_cb(
468         net_nfc_target_handle_h handle_info,
469         void *user_data)
470 {
471         LOG_BEGIN();
472
473         gdbus_nfc_context.current_target = handle_info;
474
475         if (gdbus_nfc_context.on_p2p_target_discovered_cb != NULL) {
476                 gdbus_nfc_context.on_p2p_target_discovered_cb(
477                         NFC_DISCOVERED_TYPE_ATTACHED,
478                         (nfc_p2p_target_h)gdbus_nfc_context.current_target,
479                         gdbus_nfc_context.on_p2p_target_discovered_user_data);
480         }
481 }
482
483 static void _p2p_detached_cb(void *user_data)
484 {
485         nfc_p2p_target_h handle =
486                 (nfc_p2p_target_h)gdbus_nfc_context.current_target;
487
488         LOG_BEGIN();
489
490         if (gdbus_nfc_context.on_p2p_target_discovered_cb != NULL) {
491                 gdbus_nfc_context.on_p2p_target_discovered_cb(
492                         NFC_DISCOVERED_TYPE_DETACHED,
493                         handle,
494                         gdbus_nfc_context.on_p2p_target_discovered_user_data);
495         }
496
497         /* unset data_received callback */
498         nfc_p2p_unset_data_received_cb(handle);
499
500         gdbus_nfc_context.current_target = NULL;
501 }
502 /* LCOV_EXCL_STOP */
503
504 int nfc_manager_set_p2p_target_discovered_cb(
505         nfc_p2p_target_discovered_cb callback,
506         void *user_data)
507 {
508         LOG_BEGIN();
509
510         CHECK_SUPPORTED(NFC_P2P_FEATURE);
511
512         /* LCOV_EXCL_START */
513         CHECK_INIT();
514         CHECK_INVALID(callback == NULL);
515
516         net_nfc_client_p2p_set_device_discovered(_p2p_discovered_cb, NULL);
517         net_nfc_client_p2p_set_device_detached(_p2p_detached_cb, NULL);
518
519         gdbus_nfc_context.on_p2p_target_discovered_cb = callback;
520         gdbus_nfc_context.on_p2p_target_discovered_user_data = user_data;
521
522         return NFC_ERROR_NONE;
523         /* LCOV_EXCL_STOP */
524 }
525
526 void nfc_manager_unset_p2p_target_discovered_cb(void)
527 {
528         LOG_BEGIN();
529
530         if (nfc_common_is_supported(NFC_P2P_FEATURE) == false) {
531                 LOG_ERR("NFC not supported");
532                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
533                 return;
534         }
535
536         /* LCOV_EXCL_START */
537         if (nfc_common_is_initialized() == false) {
538                 LOG_ERR("NFC not initialized");
539                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
540                 return;
541         }
542
543         gdbus_nfc_context.on_p2p_target_discovered_cb = NULL;
544         gdbus_nfc_context.on_p2p_target_discovered_user_data = NULL;
545
546         set_last_result(NFC_ERROR_NONE);
547         /* LCOV_EXCL_STOP */
548 }
549
550 int nfc_manager_set_system_handler_enable(bool enable)
551 {
552         int ret;
553         int state;
554
555         LOG_BEGIN();
556
557         CHECK_SUPPORTED(NFC_FEATURE);
558
559         /* LCOV_EXCL_START */
560         CHECK_INIT();
561
562         if (enable == true)
563                 state = 0;
564         else
565                 state = 1;
566
567         ret = net_nfc_client_sys_handler_set_launch_popup_state(state);
568
569         return nfc_common_convert_error_code(__func__, ret);
570         /* LCOV_EXCL_STOP */
571 }
572
573 int nfc_manager_set_system_handler_enable_force(bool enable)
574 {
575         int ret;
576         int state;
577
578         LOG_BEGIN();
579
580         CHECK_SUPPORTED(NFC_FEATURE);
581
582         /* LCOV_EXCL_START */
583         CHECK_INIT();
584
585         if (enable == true)
586                 state = 0;
587         else
588                 state = 1;
589
590         ret = net_nfc_client_sys_handler_set_launch_popup_state_force(state);
591
592         return nfc_common_convert_error_code(__func__, ret);
593         /* LCOV_EXCL_STOP */
594 }
595
596 bool nfc_manager_is_system_handler_enabled(void)
597 {
598         int ret;
599         int state = 0;
600
601         LOG_BEGIN();
602
603         if (nfc_common_is_supported(NFC_FEATURE) == false) {
604                 LOG_ERR("NFC not supported");
605                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
606                 return false;
607         }
608
609         /* LCOV_EXCL_START */
610         if (nfc_common_is_initialized() == false) {
611                 LOG_ERR("NFC not initialized");
612                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
613                 return false;
614         }
615
616         ret = net_nfc_client_sys_handler_get_launch_popup_state(&state);
617
618         set_last_result(nfc_common_convert_error_code(__func__, ret));
619
620         return (state == 0);
621         /* LCOV_EXCL_STOP */
622 }
623
624 int nfc_manager_get_cached_message(nfc_ndef_message_h *ndef_message)
625 {
626         int ret;
627
628         LOG_BEGIN();
629
630         CHECK_SUPPORTED(NFC_FEATURE);
631
632         /* LCOV_EXCL_START */
633         CHECK_INIT();
634         CHECK_INVALID(ndef_message == NULL);
635
636         ret = net_nfc_retrieve_current_ndef_message(ndef_message);
637
638         return nfc_common_convert_error_code(__func__, ret);
639         /* LCOV_EXCL_STOP */
640 }
641
642 /* LCOV_EXCL_START */
643 static void _se_event_cb(net_nfc_message_e message, void *user_data)
644 {
645         LOG_BEGIN();
646
647         if (gdbus_nfc_context.on_se_event_cb != NULL) {
648                 if (message == NET_NFC_MESSAGE_SE_CARD_EMULATION_CHANGED)
649                         gdbus_nfc_context.on_se_event_cb(
650                                 NFC_SE_EVENT_CARD_EMULATION_CHANGED,
651                                 gdbus_nfc_context.on_se_event_user_data);
652
653                 else if (message == NET_NFC_MESSAGE_SE_TYPE_CHANGED)
654                         gdbus_nfc_context.on_se_event_cb(
655                                 NFC_SE_EVENT_SE_TYPE_CHANGED,
656                                 gdbus_nfc_context.on_se_event_user_data);
657                 else if (message == NET_NFC_MESSAGE_SE_FIELD_ON)
658                         gdbus_nfc_context.on_se_event_cb(
659                                 NFC_SE_EVENT_FIELD_ON,
660                                 gdbus_nfc_context.on_se_event_user_data);
661         }
662 }
663 /* LCOV_EXCL_STOP */
664
665 int nfc_manager_set_se_event_cb(nfc_se_event_cb callback, void *user_data)
666 {
667         LOG_BEGIN();
668
669         CHECK_SUPPORTED(NFC_CE_FEATURE);
670
671         /* LCOV_EXCL_START */
672         CHECK_INIT();
673         CHECK_INVALID(callback == NULL);
674
675         net_nfc_client_se_set_event_cb(_se_event_cb, NULL);
676
677         gdbus_nfc_context.on_se_event_cb = callback;
678         gdbus_nfc_context.on_se_event_user_data = user_data;
679
680         return NFC_ERROR_NONE;
681         /* LCOV_EXCL_STOP */
682 }
683
684 void nfc_manager_unset_se_event_cb(void)
685 {
686         LOG_BEGIN();
687
688         if (nfc_common_is_supported(NFC_CE_FEATURE) == false) {
689                 LOG_ERR("NFC not supported");
690                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
691                 return;
692         }
693
694         /* LCOV_EXCL_START */
695         if (nfc_common_is_initialized() == false) {
696                 LOG_ERR("NFC not initialized");
697                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
698                 return;
699         }
700
701         gdbus_nfc_context.on_se_event_cb = NULL;
702         gdbus_nfc_context.on_se_event_user_data = NULL;
703
704         set_last_result(NFC_ERROR_NONE);
705         /* LCOV_EXCL_STOP */
706 }
707
708 /* LCOV_EXCL_START */
709 static void _se_transaction_event_cb(net_nfc_se_type_e se_type, data_h aid,
710         data_h param, void *user_data)
711 {
712         LOG_BEGIN();
713
714         if (gdbus_nfc_context.on_se_transaction_event_cb != NULL) {
715                 gdbus_nfc_context.on_se_transaction_event_cb(
716                         se_type,
717                         net_nfc_get_data_buffer(aid),
718                         net_nfc_get_data_length(aid),
719                         net_nfc_get_data_buffer(param),
720                         net_nfc_get_data_length(param),
721                         gdbus_nfc_context.on_se_transaction_event_user_data);
722         }
723 }
724 /* LCOV_EXCL_STOP */
725
726 int nfc_manager_set_se_transaction_event_cb(
727         nfc_se_type_e se_type,
728         nfc_se_transaction_event_cb callback,
729         void *user_data)
730 {
731         net_nfc_se_type_e type = NET_NFC_SE_TYPE_NONE;
732
733         LOG_BEGIN();
734
735         CHECK_SUPPORTED(NFC_CE_FEATURE);
736
737         /* LCOV_EXCL_START */
738         CHECK_INIT();
739         CHECK_INVALID(callback == NULL);
740
741         gdbus_nfc_context.on_se_transaction_event_cb = callback;
742         gdbus_nfc_context.on_se_transaction_event_user_data = user_data;
743
744         switch (se_type) {
745         case NFC_SE_TYPE_ESE:
746                 type = NET_NFC_SE_TYPE_ESE;
747                 break;
748
749         case NFC_SE_TYPE_UICC:
750                 type = NET_NFC_SE_TYPE_UICC;
751                 break;
752         case NET_NFC_SE_TYPE_HCE:
753                 type = NET_NFC_SE_TYPE_HCE;
754                 break;
755         default:
756                 return NFC_ERROR_INVALID_PARAMETER;
757                 break;
758         }
759
760         net_nfc_client_se_set_transaction_event_cb(type, _se_transaction_event_cb, user_data);
761
762         return NFC_ERROR_NONE;
763         /* LCOV_EXCL_STOP */
764 }
765
766 void nfc_manager_unset_se_transaction_event_cb(nfc_se_type_e se_type)
767 {
768         net_nfc_se_type_e type = NET_NFC_SE_TYPE_NONE;
769
770         LOG_BEGIN();
771
772         if (nfc_common_is_supported(NFC_CE_FEATURE) == false) {
773                 LOG_ERR("NFC not supported");
774                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
775                 return;
776         }
777
778         /* LCOV_EXCL_START */
779         if (nfc_common_is_initialized() == false) {
780                 LOG_ERR("NFC not initialized");
781                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
782                 return;
783         }
784
785         switch (se_type) {
786         case NFC_SE_TYPE_ESE:
787                 type = NET_NFC_SE_TYPE_ESE;
788                 break;
789
790         case NFC_SE_TYPE_UICC:
791                 type = NET_NFC_SE_TYPE_UICC;
792                 break;
793         default:
794                 type = NET_NFC_SE_TYPE_NONE;
795                 break;
796         }
797
798         net_nfc_client_se_unset_transaction_event_cb(type);
799
800         set_last_result(NFC_ERROR_NONE);
801         /* LCOV_EXCL_STOP */
802 }
803
804 int nfc_manager_enable_transaction_fg_dispatch()
805 {
806         net_nfc_error_e result;
807
808         LOG_BEGIN();
809
810         CHECK_SUPPORTED(NFC_CE_FEATURE);
811
812         /* LCOV_EXCL_START */
813         CHECK_INIT();
814
815         result = net_nfc_client_se_set_transaction_fg_dispatch(true);
816
817         return nfc_common_convert_error_code(__func__, result);
818         /* LCOV_EXCL_STOP */
819 }
820
821 int nfc_manager_disable_transaction_fg_dispatch()
822 {
823         net_nfc_error_e result;
824
825         CHECK_SUPPORTED(NFC_CE_FEATURE);
826
827         /* LCOV_EXCL_START */
828         CHECK_INIT();
829
830         result = net_nfc_client_se_set_transaction_fg_dispatch(false);
831
832         return nfc_common_convert_error_code(__func__, result);
833         /* LCOV_EXCL_STOP */
834 }
835
836 int nfc_manager_set_se_type(nfc_se_type_e type)
837 {
838         int ret;
839         net_nfc_se_type_e se_type = NET_NFC_SE_TYPE_NONE;
840
841         LOG_BEGIN();
842
843         CHECK_SUPPORTED(NFC_CE_FEATURE);
844
845         /* LCOV_EXCL_START */
846         CHECK_INIT();
847         CHECK_INVALID(type < NFC_SE_TYPE_DISABLE);
848         CHECK_INVALID(type > NFC_SE_TYPE_HCE);
849
850         switch (type) {
851         case NFC_SE_TYPE_ESE:
852                 se_type = NET_NFC_SE_TYPE_ESE;
853                 break;
854         case NFC_SE_TYPE_UICC:
855                 se_type = NET_NFC_SE_TYPE_UICC;
856                 break;
857         case NFC_SE_TYPE_HCE:
858                 se_type = NET_NFC_SE_TYPE_HCE;
859                 break;
860         default:
861                 se_type = NET_NFC_SE_TYPE_NONE;
862                 break;
863         }
864
865         ret = net_nfc_client_se_set_secure_element_type_sync(se_type);
866
867         return nfc_common_convert_error_code(__func__, ret);
868         /* LCOV_EXCL_STOP */
869 }
870
871 int nfc_manager_get_se_type(nfc_se_type_e *type)
872 {
873         int ret;
874         net_nfc_se_type_e se_type = NET_NFC_SE_TYPE_NONE;
875
876         LOG_BEGIN();
877
878         CHECK_SUPPORTED(NFC_CE_FEATURE);
879
880         /* LCOV_EXCL_START */
881         CHECK_INIT();
882         CHECK_INVALID(type == NULL);
883
884         ret = net_nfc_client_se_get_secure_element_type_sync(&se_type);
885
886         switch (se_type) {
887         case NET_NFC_SE_TYPE_ESE:
888                 *type = NFC_SE_TYPE_ESE;
889                 break;
890         case NET_NFC_SE_TYPE_UICC:
891                 *type = NFC_SE_TYPE_UICC;
892                 break;
893         case NET_NFC_SE_TYPE_HCE:
894                 *type = NFC_SE_TYPE_HCE;
895                 break;
896         default:
897                 *type = NFC_SE_TYPE_DISABLE;
898                 break;
899         }
900
901         return nfc_common_convert_error_code(__func__, ret);
902         /* LCOV_EXCL_STOP */
903 }
904
905 /* LCOV_EXCL_START */
906 static void _hce_event_cb(net_nfc_target_handle_h handle,
907                 net_nfc_hce_event_t event, data_h apdu, void *user_data)
908 {
909         LOG_BEGIN();
910
911         if (gdbus_nfc_context.on_hce_event_cb != NULL) {
912                 gdbus_nfc_context.on_hce_event_cb(
913                         (nfc_se_h)handle,
914                         (nfc_hce_event_type_e)event,
915                         net_nfc_get_data_buffer(apdu),
916                         net_nfc_get_data_length(apdu),
917                         gdbus_nfc_context.on_hce_event_user_data);
918         }
919 }
920 /* LCOV_EXCL_STOP */
921
922 int nfc_manager_set_hce_event_cb(nfc_hce_event_cb callback, void *user_data)
923 {
924         net_nfc_error_e result;
925         LOG_BEGIN();
926
927         CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
928
929         /* LCOV_EXCL_START */
930         CHECK_INIT();
931         CHECK_INVALID(callback == NULL);
932
933         gdbus_nfc_context.on_hce_event_cb = callback;
934         gdbus_nfc_context.on_hce_event_user_data = user_data;
935
936         result = net_nfc_client_hce_set_event_received_cb(_hce_event_cb, user_data);
937
938         return nfc_common_convert_error_code(__func__, result);
939         /* LCOV_EXCL_STOP */
940 }
941
942 void nfc_manager_unset_hce_event_cb(void)
943 {
944         LOG_BEGIN();
945
946         if (nfc_common_is_supported(NFC_CE_HCE_FEATURE) == false) {
947                 LOG_ERR("NFC not supported");
948                 set_last_result(NFC_ERROR_NOT_SUPPORTED);
949                 return;
950         }
951
952         /* LCOV_EXCL_START */
953         if (nfc_common_is_initialized() == false) {
954                 LOG_ERR("NFC not initialized");
955                 set_last_result(NFC_ERROR_NOT_INITIALIZED);
956                 return;
957         }
958
959         net_nfc_client_hce_unset_event_received_cb();
960
961         set_last_result(NFC_ERROR_NONE);
962         /* LCOV_EXCL_STOP */
963 }
964