2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "nfc_common.h"
19 bool nfc_manager_is_supported(void)
21 bool nfc_supported = false;
25 nfc_supported = nfc_common_is_supported(NFC_FEATURE);
27 if (nfc_supported == true)
28 set_last_result(NFC_ERROR_NONE);
30 set_last_result(NFC_ERROR_NOT_SUPPORTED);
36 static void _activation_changed_cb(net_nfc_error_e result, void *user_data)
38 nfc_activation_data_s *data = user_data;
42 if (user_data == NULL) {
43 LOG_ERR("user_data is NULL");
47 if (data->callback != NULL)
48 data->callback(nfc_common_convert_error_code(__func__, result), data->user_data);
54 int nfc_manager_set_activation(bool activation,
55 nfc_activation_completed_cb callback,
62 CHECK_SUPPORTED(NFC_FEATURE);
67 if (nfc_manager_is_activated() == activation) {
69 ret = NFC_ERROR_ALREADY_ACTIVATED;
71 ret = NFC_ERROR_ALREADY_DEACTIVATED;
73 nfc_activation_data_s *data = NULL;
75 data = (nfc_activation_data_s *)malloc(sizeof(nfc_activation_data_s));
78 data->callback = callback;
79 data->user_data = user_data;
80 ret = net_nfc_client_manager_set_active(activation,
81 _activation_changed_cb,
83 if (ret != NET_NFC_OK) {
84 LOG_ERR("net_nfc_client_manager_set_active fail");
89 ret = NET_NFC_ALLOC_FAIL;
92 ret = nfc_common_convert_error_code(__func__, ret);
99 bool nfc_manager_is_activated(void)
106 if (nfc_common_is_supported(NFC_FEATURE) == false) {
107 set_last_result(NFC_ERROR_NOT_SUPPORTED);
111 /* LCOV_EXCL_START */
112 ret = net_nfc_client_get_nfc_state(&activated);
114 set_last_result(nfc_common_convert_error_code(__func__, ret));
116 return (!!activated);
120 int nfc_manager_set_activation_changed_cb(nfc_activation_changed_cb callback,
125 CHECK_SUPPORTED(NFC_FEATURE);
127 /* LCOV_EXCL_START */
129 CHECK_INVALID(callback == NULL);
131 net_nfc_client_manager_set_activated(callback, user_data);
133 return NFC_ERROR_NONE;
137 void nfc_manager_unset_activation_changed_cb(void)
141 if (nfc_common_is_supported(NFC_FEATURE) == false) {
142 LOG_ERR("NFC not supported");
143 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
154 net_nfc_client_manager_unset_activated();
156 set_last_result(NFC_ERROR_NONE);
160 int nfc_manager_initialize(void)
162 int ret = NET_NFC_OK;
166 CHECK_SUPPORTED(NFC_FEATURE);
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);
174 memset(&gdbus_nfc_context, 0, sizeof(gdbus_nfc_context));
176 gdbus_nfc_context.initialized = true;
179 return nfc_common_convert_error_code(__func__, ret);
183 int nfc_manager_deinitialize(void)
185 int ret = NET_NFC_OK;
189 CHECK_SUPPORTED(NFC_FEATURE);
191 /* LCOV_EXCL_START */
192 if (nfc_common_is_initialized()) {
193 net_nfc_client_se_unset_event_cb();
195 net_nfc_client_p2p_unset_device_discovered();
196 net_nfc_client_p2p_unset_device_detached();
198 net_nfc_client_tag_unset_tag_discovered();
199 net_nfc_client_tag_unset_tag_detached();
201 ret = net_nfc_client_deinitialize();
203 gdbus_nfc_context.initialized = false;
206 return nfc_common_convert_error_code(__func__, ret);
210 /* LCOV_EXCL_START */
211 static void _tag_discovered_cb(net_nfc_target_info_h info, void *user_data)
215 gdbus_nfc_context.current_tag = info;
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);
224 /* ndef discovered cb */
225 if (gdbus_nfc_context.on_ndef_discovered_cb) {
226 ndef_message_h ndef_message = NULL;
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);
233 net_nfc_free_ndef_message(ndef_message);
238 static void _tag_detached_cb(void *user_data)
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);
249 gdbus_nfc_context.current_tag = NULL;
253 int nfc_manager_set_ndef_discovered_cb(
254 nfc_ndef_discovered_cb callback,
259 CHECK_SUPPORTED(NFC_FEATURE);
261 /* LCOV_EXCL_START */
263 CHECK_INVALID(callback == NULL);
265 net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
266 net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
268 gdbus_nfc_context.on_ndef_discovered_cb = callback;
269 gdbus_nfc_context.on_ndef_discovered_user_data = user_data;
271 return NFC_ERROR_NONE;
275 void nfc_manager_unset_ndef_discovered_cb(void)
279 if (nfc_common_is_supported(NFC_FEATURE) == false) {
280 LOG_ERR("NFC not supported");
281 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
292 gdbus_nfc_context.on_ndef_discovered_cb = NULL;
293 gdbus_nfc_context.on_ndef_discovered_user_data = NULL;
295 set_last_result(NFC_ERROR_NONE);
299 void nfc_manager_set_tag_filter(int filter)
303 if (nfc_common_is_supported(NFC_TAG_FEATURE) == false) {
304 LOG_ERR("NFC not supported");
305 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
316 if (filter < NET_NFC_ALL_ENABLE) {
317 LOG_ERR("Invalid parameter");
318 set_last_result(NFC_ERROR_INVALID_PARAMETER);
322 net_nfc_client_tag_set_filter(filter);
324 set_last_result(NFC_ERROR_NONE);
328 int nfc_manager_get_tag_filter(void)
332 if (nfc_common_is_supported(NFC_TAG_FEATURE) == false) {
333 LOG_ERR("NFC not supported");
334 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
345 set_last_result(NFC_ERROR_NONE);
347 return net_nfc_client_tag_get_filter();
351 int nfc_manager_get_connected_tag(nfc_tag_h *tag)
354 net_nfc_target_info_h result = NULL;
358 CHECK_SUPPORTED(NFC_TAG_FEATURE);
360 /* LCOV_EXCL_START */
362 CHECK_INVALID(tag == NULL);
366 net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
367 net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
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;
376 *tag = gdbus_nfc_context.current_tag;
381 return nfc_common_convert_error_code(__func__, ret);
385 int nfc_manager_get_connected_target(nfc_p2p_target_h *target)
388 net_nfc_target_handle_h result = NULL;
392 CHECK_SUPPORTED(NFC_P2P_FEATURE);
394 /* LCOV_EXCL_START */
396 CHECK_INVALID(target == NULL);
400 net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
401 net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
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;
408 *target = gdbus_nfc_context.current_target;
412 *target = gdbus_nfc_context.current_target;
417 return nfc_common_convert_error_code(__func__, ret);
421 int nfc_manager_set_tag_discovered_cb(nfc_tag_discovered_cb callback,
426 CHECK_SUPPORTED(NFC_TAG_FEATURE);
428 /* LCOV_EXCL_START */
430 CHECK_INVALID(callback == NULL);
432 net_nfc_client_tag_set_tag_discovered(_tag_discovered_cb, NULL);
433 net_nfc_client_tag_set_tag_detached(_tag_detached_cb, NULL);
435 gdbus_nfc_context.on_tag_discovered_cb = callback;
436 gdbus_nfc_context.on_tag_discovered_user_data = user_data;
438 return NFC_ERROR_NONE;
442 void nfc_manager_unset_tag_discovered_cb(void)
446 if (nfc_common_is_supported(NFC_TAG_FEATURE) == false) {
447 LOG_ERR("NFC not supported");
448 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
459 gdbus_nfc_context.on_tag_discovered_cb = NULL;
460 gdbus_nfc_context.on_tag_discovered_user_data = NULL;
462 set_last_result(NFC_ERROR_NONE);
466 /* LCOV_EXCL_START */
467 static void _p2p_discovered_cb(
468 net_nfc_target_handle_h handle_info,
473 gdbus_nfc_context.current_target = handle_info;
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);
483 static void _p2p_detached_cb(void *user_data)
485 nfc_p2p_target_h handle =
486 (nfc_p2p_target_h)gdbus_nfc_context.current_target;
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,
494 gdbus_nfc_context.on_p2p_target_discovered_user_data);
497 /* unset data_received callback */
498 nfc_p2p_unset_data_received_cb(handle);
500 gdbus_nfc_context.current_target = NULL;
504 int nfc_manager_set_p2p_target_discovered_cb(
505 nfc_p2p_target_discovered_cb callback,
510 CHECK_SUPPORTED(NFC_P2P_FEATURE);
512 /* LCOV_EXCL_START */
514 CHECK_INVALID(callback == NULL);
516 net_nfc_client_p2p_set_device_discovered(_p2p_discovered_cb, NULL);
517 net_nfc_client_p2p_set_device_detached(_p2p_detached_cb, NULL);
519 gdbus_nfc_context.on_p2p_target_discovered_cb = callback;
520 gdbus_nfc_context.on_p2p_target_discovered_user_data = user_data;
522 return NFC_ERROR_NONE;
526 void nfc_manager_unset_p2p_target_discovered_cb(void)
530 if (nfc_common_is_supported(NFC_P2P_FEATURE) == false) {
531 LOG_ERR("NFC not supported");
532 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
543 gdbus_nfc_context.on_p2p_target_discovered_cb = NULL;
544 gdbus_nfc_context.on_p2p_target_discovered_user_data = NULL;
546 set_last_result(NFC_ERROR_NONE);
550 int nfc_manager_set_system_handler_enable(bool enable)
557 CHECK_SUPPORTED(NFC_FEATURE);
559 /* LCOV_EXCL_START */
567 ret = net_nfc_client_sys_handler_set_launch_popup_state(state);
569 return nfc_common_convert_error_code(__func__, ret);
573 int nfc_manager_set_system_handler_enable_force(bool enable)
580 CHECK_SUPPORTED(NFC_FEATURE);
582 /* LCOV_EXCL_START */
590 ret = net_nfc_client_sys_handler_set_launch_popup_state_force(state);
592 return nfc_common_convert_error_code(__func__, ret);
596 bool nfc_manager_is_system_handler_enabled(void)
603 if (nfc_common_is_supported(NFC_FEATURE) == false) {
604 LOG_ERR("NFC not supported");
605 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
616 ret = net_nfc_client_sys_handler_get_launch_popup_state(&state);
618 set_last_result(nfc_common_convert_error_code(__func__, ret));
624 int nfc_manager_get_cached_message(nfc_ndef_message_h *ndef_message)
630 CHECK_SUPPORTED(NFC_FEATURE);
632 /* LCOV_EXCL_START */
634 CHECK_INVALID(ndef_message == NULL);
636 ret = net_nfc_retrieve_current_ndef_message(ndef_message);
638 return nfc_common_convert_error_code(__func__, ret);
642 /* LCOV_EXCL_START */
643 static void _se_event_cb(net_nfc_message_e message, void *user_data)
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);
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);
665 int nfc_manager_set_se_event_cb(nfc_se_event_cb callback, void *user_data)
669 CHECK_SUPPORTED(NFC_CE_FEATURE);
671 /* LCOV_EXCL_START */
673 CHECK_INVALID(callback == NULL);
675 net_nfc_client_se_set_event_cb(_se_event_cb, NULL);
677 gdbus_nfc_context.on_se_event_cb = callback;
678 gdbus_nfc_context.on_se_event_user_data = user_data;
680 return NFC_ERROR_NONE;
684 void nfc_manager_unset_se_event_cb(void)
688 if (nfc_common_is_supported(NFC_CE_FEATURE) == false) {
689 LOG_ERR("NFC not supported");
690 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
701 gdbus_nfc_context.on_se_event_cb = NULL;
702 gdbus_nfc_context.on_se_event_user_data = NULL;
704 set_last_result(NFC_ERROR_NONE);
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)
714 if (gdbus_nfc_context.on_se_transaction_event_cb != NULL) {
715 gdbus_nfc_context.on_se_transaction_event_cb(
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);
726 int nfc_manager_set_se_transaction_event_cb(
727 nfc_se_type_e se_type,
728 nfc_se_transaction_event_cb callback,
731 net_nfc_se_type_e type = NET_NFC_SE_TYPE_NONE;
735 CHECK_SUPPORTED(NFC_CE_FEATURE);
737 /* LCOV_EXCL_START */
739 CHECK_INVALID(callback == NULL);
741 gdbus_nfc_context.on_se_transaction_event_cb = callback;
742 gdbus_nfc_context.on_se_transaction_event_user_data = user_data;
745 case NFC_SE_TYPE_ESE:
746 type = NET_NFC_SE_TYPE_ESE;
749 case NFC_SE_TYPE_UICC:
750 type = NET_NFC_SE_TYPE_UICC;
752 case NET_NFC_SE_TYPE_HCE:
753 type = NET_NFC_SE_TYPE_HCE;
756 return NFC_ERROR_INVALID_PARAMETER;
760 net_nfc_client_se_set_transaction_event_cb(type, _se_transaction_event_cb, user_data);
762 return NFC_ERROR_NONE;
766 void nfc_manager_unset_se_transaction_event_cb(nfc_se_type_e se_type)
768 net_nfc_se_type_e type = NET_NFC_SE_TYPE_NONE;
772 if (nfc_common_is_supported(NFC_CE_FEATURE) == false) {
773 LOG_ERR("NFC not supported");
774 set_last_result(NFC_ERROR_NOT_SUPPORTED);
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);
786 case NFC_SE_TYPE_ESE:
787 type = NET_NFC_SE_TYPE_ESE;
790 case NFC_SE_TYPE_UICC:
791 type = NET_NFC_SE_TYPE_UICC;
794 type = NET_NFC_SE_TYPE_NONE;
798 net_nfc_client_se_unset_transaction_event_cb(type);
800 set_last_result(NFC_ERROR_NONE);
804 int nfc_manager_enable_transaction_fg_dispatch()
806 net_nfc_error_e result;
810 CHECK_SUPPORTED(NFC_CE_FEATURE);
812 /* LCOV_EXCL_START */
815 result = net_nfc_client_se_set_transaction_fg_dispatch(true);
817 return nfc_common_convert_error_code(__func__, result);
821 int nfc_manager_disable_transaction_fg_dispatch()
823 net_nfc_error_e result;
825 CHECK_SUPPORTED(NFC_CE_FEATURE);
827 /* LCOV_EXCL_START */
830 result = net_nfc_client_se_set_transaction_fg_dispatch(false);
832 return nfc_common_convert_error_code(__func__, result);
836 int nfc_manager_set_se_type(nfc_se_type_e type)
839 net_nfc_se_type_e se_type = NET_NFC_SE_TYPE_NONE;
843 CHECK_SUPPORTED(NFC_CE_FEATURE);
845 /* LCOV_EXCL_START */
847 CHECK_INVALID(type < NFC_SE_TYPE_DISABLE);
848 CHECK_INVALID(type > NFC_SE_TYPE_HCE);
851 case NFC_SE_TYPE_ESE:
852 se_type = NET_NFC_SE_TYPE_ESE;
854 case NFC_SE_TYPE_UICC:
855 se_type = NET_NFC_SE_TYPE_UICC;
857 case NFC_SE_TYPE_HCE:
858 se_type = NET_NFC_SE_TYPE_HCE;
861 se_type = NET_NFC_SE_TYPE_NONE;
865 ret = net_nfc_client_se_set_secure_element_type_sync(se_type);
867 return nfc_common_convert_error_code(__func__, ret);
871 int nfc_manager_get_se_type(nfc_se_type_e *type)
874 net_nfc_se_type_e se_type = NET_NFC_SE_TYPE_NONE;
878 CHECK_SUPPORTED(NFC_CE_FEATURE);
880 /* LCOV_EXCL_START */
882 CHECK_INVALID(type == NULL);
884 ret = net_nfc_client_se_get_secure_element_type_sync(&se_type);
887 case NET_NFC_SE_TYPE_ESE:
888 *type = NFC_SE_TYPE_ESE;
890 case NET_NFC_SE_TYPE_UICC:
891 *type = NFC_SE_TYPE_UICC;
893 case NET_NFC_SE_TYPE_HCE:
894 *type = NFC_SE_TYPE_HCE;
897 *type = NFC_SE_TYPE_DISABLE;
901 return nfc_common_convert_error_code(__func__, ret);
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)
911 if (gdbus_nfc_context.on_hce_event_cb != NULL) {
912 gdbus_nfc_context.on_hce_event_cb(
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);
922 int nfc_manager_set_hce_event_cb(nfc_hce_event_cb callback, void *user_data)
924 net_nfc_error_e result;
927 CHECK_SUPPORTED(NFC_CE_HCE_FEATURE);
929 /* LCOV_EXCL_START */
931 CHECK_INVALID(callback == NULL);
933 gdbus_nfc_context.on_hce_event_cb = callback;
934 gdbus_nfc_context.on_hce_event_user_data = user_data;
936 result = net_nfc_client_hce_set_event_received_cb(_hce_event_cb, user_data);
938 return nfc_common_convert_error_code(__func__, result);
942 void nfc_manager_unset_hce_event_cb(void)
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);
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);
959 net_nfc_client_hce_unset_event_received_cb();
961 set_last_result(NFC_ERROR_NONE);