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