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"
20 static void _p2p_handover_cb(net_nfc_error_e result,
21 net_nfc_conn_handover_carrier_type_e carrier, data_h ac_data, void *user_data)
23 nfc_p2p_connection_handover_completed_cb callback;
28 if (user_data == NULL)
31 g_variant_get((GVariant *)user_data,
36 if (callback != NULL) {
37 nfc_ac_type_e carrior_type;
42 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
43 carrior_type = NFC_AC_TYPE_BT;
44 buffer = nfc_common_get_bt_address_string(ac_data);
49 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
50 carrior_type = NFC_AC_TYPE_WIFI;
51 buffer = nfc_common_get_bt_address_string(ac_data);
56 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
57 carrior_type = NFC_AC_TYPE_WIFI_DIRECT;
58 buffer = nfc_common_get_bt_address_string(ac_data);
64 carrior_type = NFC_AC_TYPE_UNKNOWN;
70 callback(nfc_common_convert_error_code(__func__, result),
71 carrior_type, (void *)buffer,
78 g_variant_unref((GVariant *)user_data);
83 int nfc_p2p_connection_handover(nfc_p2p_target_h target,
85 nfc_p2p_connection_handover_completed_cb callback,
89 net_nfc_conn_handover_carrier_type_e net_ac_type =
90 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
95 CHECK_SUPPORTED(NFC_P2P_FEATURE);
98 CHECK_INVALID(target == NULL);
99 CHECK_INVALID(type < 0);
100 CHECK_INVALID(type > NFC_AC_TYPE_UNKNOWN);
105 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
108 case NFC_AC_TYPE_WIFI:
109 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
112 case NFC_AC_TYPE_WIFI_DIRECT:
113 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
116 case NFC_AC_TYPE_UNKNOWN:
117 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
121 parameter = g_variant_new("(tt)",
124 if (parameter != NULL) {
125 ret = net_nfc_client_p2p_connection_handover(
126 (net_nfc_target_handle_h)target,
130 if (ret != NET_NFC_OK)
131 g_variant_unref(parameter);
133 ret = NET_NFC_ALLOC_FAIL;
136 return nfc_common_convert_error_code(__func__, ret);
141 /* LCOV_EXCL_START */
142 static void _connection_handover_event_cb(
143 net_nfc_handover_event_e event,
144 net_nfc_error_e result,
145 net_nfc_conn_handover_carrier_type_e carrier,
152 if (gdbus_nfc_context.on_handover_event_cb != NULL) {
153 nfc_ndef_message_h message;
157 net_nfc_create_ndef_message_from_rawdata(&message, ndef_message);
159 if (event == NET_NFC_HANDOVER_START) {
160 type = NFC_AC_TYPE_UNKNOWN;
164 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
165 type = NFC_AC_TYPE_BT;
166 address = nfc_common_get_bt_address_string(ac_data);
169 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
170 type = NFC_AC_TYPE_WIFI;
171 address = nfc_common_get_bt_address_string(ac_data);
174 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
175 type = NFC_AC_TYPE_WIFI_DIRECT;
176 address = nfc_common_get_bt_address_string(ac_data);
180 type = NFC_AC_TYPE_UNKNOWN;
186 gdbus_nfc_context.on_handover_event_cb(
187 nfc_common_convert_error_code(__func__, result),
192 gdbus_nfc_context.on_handover_event_user_data);
196 net_nfc_free_ndef_message(message);
203 /* LCOV_EXCL_START */
204 int nfc_connection_handover_set_event_cb(nfc_connection_handover_event_cb callback, void *user_data)
208 CHECK_SUPPORTED(NFC_P2P_FEATURE);
211 CHECK_INVALID(callback == NULL);
213 gdbus_nfc_context.on_handover_event_cb = callback;
214 gdbus_nfc_context.on_handover_event_user_data = user_data;
216 net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
218 return NFC_ERROR_NONE;
222 /* LCOV_EXCL_START */
223 int nfc_connection_handover_unset_event_cb(void)
227 CHECK_SUPPORTED(NFC_P2P_FEATURE);
231 net_nfc_client_handover_unset_handover_event_cb();
233 gdbus_nfc_context.on_handover_event_cb = NULL;
234 gdbus_nfc_context.on_handover_event_user_data = NULL;
236 return NFC_ERROR_NONE;
240 /* LCOV_EXCL_START */
241 bool nfc_p2p_is_supported_ac_type(nfc_ac_type_e carrier)
243 bool _is_support_p2p = false;
244 bool _is_support_ac_type = false;
248 _is_support_p2p = nfc_common_is_supported(NFC_P2P_FEATURE);
249 if (!_is_support_p2p) {
250 set_last_result(NFC_ERROR_NOT_SUPPORTED);
254 if (carrier == NFC_AC_TYPE_BT)
255 _is_support_ac_type = true;
257 if (_is_support_ac_type)
258 set_last_result(NFC_ERROR_NONE);
260 set_last_result(NFC_ERROR_NOT_SUPPORTED);
262 return _is_support_ac_type;
266 /* LCOV_EXCL_START */
267 int nfc_handover_message_import_from_ndef_message(nfc_handover_message_h *result, nfc_ndef_message_h msg)
273 CHECK_SUPPORTED(NFC_P2P_FEATURE);
276 CHECK_INVALID(msg == NULL);
277 CHECK_INVALID(result == NULL);
279 ret = net_nfc_import_handover_from_ndef_message(
280 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
282 return nfc_common_convert_error_code(__func__, ret);
286 /* LCOV_EXCL_START */
287 int nfc_handover_message_get_random_number(nfc_handover_message_h message, unsigned short *random_number)
289 net_nfc_error_e result;
293 CHECK_SUPPORTED(NFC_P2P_FEATURE);
296 CHECK_INVALID(message == NULL);
297 CHECK_INVALID(random_number == NULL);
299 result = net_nfc_get_handover_random_number(
300 (net_nfc_ch_message_h)message,
303 return nfc_common_convert_error_code(__func__, result);
307 /* LCOV_EXCL_START */
308 int nfc_handover_message_get_carrier_count(nfc_handover_message_h message, unsigned int *count)
310 net_nfc_error_e result;
314 CHECK_SUPPORTED(NFC_P2P_FEATURE);
317 CHECK_INVALID(message == NULL);
318 CHECK_INVALID(count == NULL);
320 result = net_nfc_get_handover_carrier_count(
321 (net_nfc_ch_message_h)message,
324 return nfc_common_convert_error_code(__func__, result);
328 /* LCOV_EXCL_START */
329 int nfc_handover_message_get_carrier(nfc_handover_message_h message, int index, nfc_handover_carrier_h *carrier)
331 net_nfc_error_e result;
335 CHECK_SUPPORTED(NFC_P2P_FEATURE);
339 CHECK_INVALID(message == NULL);
340 CHECK_INVALID(carrier == NULL);
342 result = net_nfc_get_handover_carrier(
343 (net_nfc_ch_message_h)message,
345 (net_nfc_ch_carrier_h *)carrier);
347 return nfc_common_convert_error_code(__func__, result);
351 /* LCOV_EXCL_START */
352 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
354 net_nfc_error_e result;
355 net_nfc_conn_handover_carrier_type_e temp;
359 CHECK_SUPPORTED(NFC_P2P_FEATURE);
362 CHECK_INVALID(message == NULL);
363 CHECK_INVALID(carrier == NULL);
367 temp = NET_NFC_CONN_HANDOVER_CARRIER_BT;
370 case NFC_AC_TYPE_WIFI:
371 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
374 case NFC_AC_TYPE_WIFI_DIRECT:
375 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
379 temp = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
382 result = net_nfc_get_handover_carrier_by_type(
383 (net_nfc_ch_message_h)message,
385 (net_nfc_ch_carrier_h *)carrier);
387 return nfc_common_convert_error_code(__func__, result);
391 /* LCOV_EXCL_START */
392 int nfc_handover_message_destroy(nfc_handover_message_h message)
394 net_nfc_error_e result;
398 CHECK_SUPPORTED(NFC_P2P_FEATURE);
401 CHECK_INVALID(message == NULL);
403 result = net_nfc_free_handover_message((net_nfc_ch_message_h)message);
405 return nfc_common_convert_error_code(__func__, result);
409 /* LCOV_EXCL_START */
410 int nfc_handover_carrier_get_cps(nfc_handover_carrier_h carrier, nfc_ac_state_e *cps)
412 net_nfc_error_e result;
413 net_nfc_conn_handover_carrier_state_e temp;
417 CHECK_SUPPORTED(NFC_P2P_FEATURE);
420 CHECK_INVALID(carrier == NULL);
421 CHECK_INVALID(cps == NULL);
423 result = net_nfc_get_handover_carrier_cps(
424 (net_nfc_ch_carrier_h)carrier, &temp);
425 if (result == NET_NFC_OK) {
427 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE:
428 *cps = NFC_AC_STATE_INACTIVATE;
431 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE:
432 *cps = NFC_AC_STATE_ACTIVATE;
435 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING:
436 *cps = NFC_AC_STATE_ACTIVATING;
440 *cps = NFC_AC_STATE_UNKNOWN;
445 return nfc_common_convert_error_code(__func__, result);
449 /* LCOV_EXCL_START */
450 int nfc_handover_carrier_get_type(nfc_handover_carrier_h carrier, nfc_ac_type_e *type)
452 net_nfc_error_e result;
453 net_nfc_conn_handover_carrier_type_e temp;
457 CHECK_SUPPORTED(NFC_P2P_FEATURE);
460 CHECK_INVALID(carrier == NULL);
461 CHECK_INVALID(type == NULL);
463 result = net_nfc_get_handover_carrier_type(
464 (net_nfc_ch_carrier_h)carrier, &temp);
465 if (result == NET_NFC_OK) {
467 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
468 *type = NFC_AC_TYPE_BT;
471 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
472 *type = NFC_AC_TYPE_WIFI;
475 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
476 *type = NFC_AC_TYPE_WIFI_DIRECT;
480 *type = NFC_AC_TYPE_UNKNOWN;
485 return nfc_common_convert_error_code(__func__, result);
489 /* LCOV_EXCL_START */
490 int nfc_handover_carrier_get_carrier_record(nfc_handover_carrier_h carrier, nfc_ndef_record_h *record)
492 net_nfc_error_e result;
496 CHECK_SUPPORTED(NFC_P2P_FEATURE);
499 CHECK_INVALID(carrier == NULL);
500 CHECK_INVALID(record == NULL);
502 result = net_nfc_get_handover_carrier_record(
503 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
505 return nfc_common_convert_error_code(__func__, result);
509 /* LCOV_EXCL_START */
510 int nfc_handover_carrier_get_auxiliary_record_count(nfc_handover_carrier_h carrier, unsigned int *count)
512 net_nfc_error_e result;
516 CHECK_SUPPORTED(NFC_P2P_FEATURE);
519 CHECK_INVALID(carrier == NULL);
520 CHECK_INVALID(count == NULL);
522 result = net_nfc_get_handover_auxiliary_record_count(
523 (net_nfc_ch_carrier_h)carrier, count);
525 return nfc_common_convert_error_code(__func__, result);
529 /* LCOV_EXCL_START */
530 int nfc_handover_carrier_get_auxiliary_record(nfc_handover_carrier_h carrier, int index, nfc_ndef_record_h *record)
532 net_nfc_error_e result;
536 CHECK_SUPPORTED(NFC_P2P_FEATURE);
540 CHECK_INVALID(carrier == NULL);
541 CHECK_INVALID(record == NULL);
543 result = net_nfc_get_handover_auxiliary_record(
544 (net_nfc_ch_carrier_h)carrier,
545 index, (ndef_record_h *)record);
547 return nfc_common_convert_error_code(__func__, result);
551 /* LCOV_EXCL_START */
552 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
554 net_nfc_error_e result;
558 CHECK_SUPPORTED(NFC_P2P_FEATURE);
562 CHECK_INVALID(carrier == NULL);
563 CHECK_INVALID(config == NULL);
565 result = net_nfc_create_carrier_config_from_carrier(
566 (net_nfc_carrier_config_h *)config,
567 (net_nfc_ch_carrier_h)carrier);
569 return nfc_common_convert_error_code(__func__, result);
573 /* LCOV_EXCL_START */
574 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
576 net_nfc_error_e result;
580 CHECK_SUPPORTED(NFC_P2P_FEATURE);
583 CHECK_INVALID(carrier == NULL);
585 result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
587 return nfc_common_convert_error_code(__func__, result);
591 /* LCOV_EXCL_START */
592 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
594 net_nfc_error_e result;
598 CHECK_SUPPORTED(NFC_P2P_FEATURE);
601 CHECK_INVALID(config == NULL);
602 CHECK_INVALID(size == NULL);
603 CHECK_INVALID(data == NULL);
605 result = net_nfc_get_carrier_config_property(
606 (net_nfc_carrier_config_h)config,
607 attribute, size, data);
609 return nfc_common_convert_error_code(__func__, result);
613 /* LCOV_EXCL_START */
614 int nfc_handover_config_destroy(nfc_handover_config_h config)
616 net_nfc_error_e result;
620 CHECK_SUPPORTED(NFC_P2P_FEATURE);
623 CHECK_INVALID(config == NULL);
625 result = net_nfc_free_carrier_config((net_nfc_carrier_config_h)config);
627 return nfc_common_convert_error_code(__func__, result);