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);
82 int nfc_p2p_connection_handover(nfc_p2p_target_h target,
84 nfc_p2p_connection_handover_completed_cb callback,
88 net_nfc_conn_handover_carrier_type_e net_ac_type =
89 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
94 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);
140 /* LCOV_EXCL_START */
141 static void _connection_handover_event_cb(
142 net_nfc_handover_event_e event,
143 net_nfc_error_e result,
144 net_nfc_conn_handover_carrier_type_e carrier,
151 if (gdbus_nfc_context.on_handover_event_cb != NULL) {
152 nfc_ndef_message_h message;
156 net_nfc_create_ndef_message_from_rawdata(&message, ndef_message);
158 if (event == NET_NFC_HANDOVER_START) {
159 type = NFC_AC_TYPE_UNKNOWN;
163 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
164 type = NFC_AC_TYPE_BT;
165 address = nfc_common_get_bt_address_string(ac_data);
168 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
169 type = NFC_AC_TYPE_WIFI;
170 address = nfc_common_get_bt_address_string(ac_data);
173 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
174 type = NFC_AC_TYPE_WIFI_DIRECT;
175 address = nfc_common_get_bt_address_string(ac_data);
179 type = NFC_AC_TYPE_UNKNOWN;
185 gdbus_nfc_context.on_handover_event_cb(
186 nfc_common_convert_error_code(__func__, result),
191 gdbus_nfc_context.on_handover_event_user_data);
195 net_nfc_free_ndef_message(message);
202 int nfc_connection_handover_set_event_cb(nfc_connection_handover_event_cb callback, void *user_data)
206 CHECK_SUPPORTED(NFC_P2P_FEATURE);
208 /* LCOV_EXCL_START */
210 CHECK_INVALID(callback == NULL);
212 gdbus_nfc_context.on_handover_event_cb = callback;
213 gdbus_nfc_context.on_handover_event_user_data = user_data;
215 net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
217 return NFC_ERROR_NONE;
221 int nfc_connection_handover_unset_event_cb(void)
225 CHECK_SUPPORTED(NFC_P2P_FEATURE);
227 /* LCOV_EXCL_START */
230 net_nfc_client_handover_unset_handover_event_cb();
232 gdbus_nfc_context.on_handover_event_cb = NULL;
233 gdbus_nfc_context.on_handover_event_user_data = NULL;
235 return NFC_ERROR_NONE;
239 bool nfc_p2p_is_supported_ac_type(nfc_ac_type_e carrier)
241 bool _is_support_p2p = false;
242 bool _is_support_ac_type = false;
246 _is_support_p2p = nfc_common_is_supported(NFC_P2P_FEATURE);
247 if (!_is_support_p2p) {
248 set_last_result(NFC_ERROR_NOT_SUPPORTED);
252 /* LCOV_EXCL_START */
253 if (carrier == NFC_AC_TYPE_BT)
254 _is_support_ac_type = true;
256 if (_is_support_ac_type)
257 set_last_result(NFC_ERROR_NONE);
259 set_last_result(NFC_ERROR_NOT_SUPPORTED);
261 return _is_support_ac_type;
265 int nfc_handover_message_import_from_ndef_message(nfc_handover_message_h *result, nfc_ndef_message_h msg)
271 CHECK_SUPPORTED(NFC_P2P_FEATURE);
273 /* LCOV_EXCL_START */
275 CHECK_INVALID(msg == NULL);
276 CHECK_INVALID(result == NULL);
278 ret = net_nfc_import_handover_from_ndef_message(
279 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
281 return nfc_common_convert_error_code(__func__, ret);
285 int nfc_handover_message_get_random_number(nfc_handover_message_h message, unsigned short *random_number)
287 net_nfc_error_e result;
291 CHECK_SUPPORTED(NFC_P2P_FEATURE);
293 /* LCOV_EXCL_START */
295 CHECK_INVALID(message == NULL);
296 CHECK_INVALID(random_number == NULL);
298 result = net_nfc_get_handover_random_number(
299 (net_nfc_ch_message_h)message,
302 return nfc_common_convert_error_code(__func__, result);
306 int nfc_handover_message_get_carrier_count(nfc_handover_message_h message, unsigned int *count)
308 net_nfc_error_e result;
312 CHECK_SUPPORTED(NFC_P2P_FEATURE);
314 /* LCOV_EXCL_START */
316 CHECK_INVALID(message == NULL);
317 CHECK_INVALID(count == NULL);
319 result = net_nfc_get_handover_carrier_count(
320 (net_nfc_ch_message_h)message,
323 return nfc_common_convert_error_code(__func__, result);
327 int nfc_handover_message_get_carrier(nfc_handover_message_h message, int index, nfc_handover_carrier_h *carrier)
329 net_nfc_error_e result;
333 CHECK_SUPPORTED(NFC_P2P_FEATURE);
335 /* LCOV_EXCL_START */
337 CHECK_INVALID(message == NULL);
338 CHECK_INVALID(carrier == NULL);
340 result = net_nfc_get_handover_carrier(
341 (net_nfc_ch_message_h)message,
343 (net_nfc_ch_carrier_h *)carrier);
345 return nfc_common_convert_error_code(__func__, result);
349 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
351 net_nfc_error_e result;
352 net_nfc_conn_handover_carrier_type_e temp;
356 CHECK_SUPPORTED(NFC_P2P_FEATURE);
358 /* LCOV_EXCL_START */
360 CHECK_INVALID(message == NULL);
361 CHECK_INVALID(carrier == NULL);
365 temp = NET_NFC_CONN_HANDOVER_CARRIER_BT;
368 case NFC_AC_TYPE_WIFI:
369 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
372 case NFC_AC_TYPE_WIFI_DIRECT:
373 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
377 temp = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
380 result = net_nfc_get_handover_carrier_by_type(
381 (net_nfc_ch_message_h)message,
383 (net_nfc_ch_carrier_h *)carrier);
385 return nfc_common_convert_error_code(__func__, result);
389 int nfc_handover_message_destroy(nfc_handover_message_h message)
391 net_nfc_error_e result;
395 CHECK_SUPPORTED(NFC_P2P_FEATURE);
397 /* LCOV_EXCL_START */
399 CHECK_INVALID(message == NULL);
401 result = net_nfc_free_handover_message((net_nfc_ch_message_h)message);
403 return nfc_common_convert_error_code(__func__, result);
408 int nfc_handover_carrier_get_cps(nfc_handover_carrier_h carrier, nfc_ac_state_e *cps)
410 net_nfc_error_e result;
411 net_nfc_conn_handover_carrier_state_e temp;
415 CHECK_SUPPORTED(NFC_P2P_FEATURE);
417 /* LCOV_EXCL_START */
419 CHECK_INVALID(carrier == NULL);
420 CHECK_INVALID(cps == NULL);
422 result = net_nfc_get_handover_carrier_cps(
423 (net_nfc_ch_carrier_h)carrier, &temp);
424 if (result == NET_NFC_OK) {
426 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE:
427 *cps = NFC_AC_STATE_INACTIVATE;
430 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE:
431 *cps = NFC_AC_STATE_ACTIVATE;
434 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING:
435 *cps = NFC_AC_STATE_ACTIVATING;
439 *cps = NFC_AC_STATE_UNKNOWN;
444 return nfc_common_convert_error_code(__func__, result);
448 int nfc_handover_carrier_get_type(nfc_handover_carrier_h carrier, nfc_ac_type_e *type)
450 net_nfc_error_e result;
451 net_nfc_conn_handover_carrier_type_e temp;
455 CHECK_SUPPORTED(NFC_P2P_FEATURE);
457 /* LCOV_EXCL_START */
459 CHECK_INVALID(carrier == NULL);
460 CHECK_INVALID(type == NULL);
462 result = net_nfc_get_handover_carrier_type(
463 (net_nfc_ch_carrier_h)carrier, &temp);
464 if (result == NET_NFC_OK) {
466 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
467 *type = NFC_AC_TYPE_BT;
470 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
471 *type = NFC_AC_TYPE_WIFI;
474 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
475 *type = NFC_AC_TYPE_WIFI_DIRECT;
479 *type = NFC_AC_TYPE_UNKNOWN;
484 return nfc_common_convert_error_code(__func__, result);
488 int nfc_handover_carrier_get_carrier_record(nfc_handover_carrier_h carrier, nfc_ndef_record_h *record)
490 net_nfc_error_e result;
494 CHECK_SUPPORTED(NFC_P2P_FEATURE);
496 /* LCOV_EXCL_START */
498 CHECK_INVALID(carrier == NULL);
499 CHECK_INVALID(record == NULL);
501 result = net_nfc_get_handover_carrier_record(
502 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
504 return nfc_common_convert_error_code(__func__, result);
508 int nfc_handover_carrier_get_auxiliary_record_count(nfc_handover_carrier_h carrier, unsigned int *count)
510 net_nfc_error_e result;
514 CHECK_SUPPORTED(NFC_P2P_FEATURE);
516 /* LCOV_EXCL_START */
518 CHECK_INVALID(carrier == NULL);
519 CHECK_INVALID(count == NULL);
521 result = net_nfc_get_handover_auxiliary_record_count(
522 (net_nfc_ch_carrier_h)carrier, count);
524 return nfc_common_convert_error_code(__func__, result);
528 int nfc_handover_carrier_get_auxiliary_record(nfc_handover_carrier_h carrier, int index, nfc_ndef_record_h *record)
530 net_nfc_error_e result;
534 CHECK_SUPPORTED(NFC_P2P_FEATURE);
536 /* LCOV_EXCL_START */
538 CHECK_INVALID(carrier == NULL);
539 CHECK_INVALID(record == NULL);
541 result = net_nfc_get_handover_auxiliary_record(
542 (net_nfc_ch_carrier_h)carrier,
543 index, (ndef_record_h *)record);
545 return nfc_common_convert_error_code(__func__, result);
549 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
551 net_nfc_error_e result;
555 CHECK_SUPPORTED(NFC_P2P_FEATURE);
557 /* LCOV_EXCL_START */
559 CHECK_INVALID(carrier == NULL);
560 CHECK_INVALID(config == NULL);
562 result = net_nfc_create_carrier_config_from_carrier(
563 (net_nfc_carrier_config_h *)config,
564 (net_nfc_ch_carrier_h)carrier);
566 return nfc_common_convert_error_code(__func__, result);
570 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
572 net_nfc_error_e result;
576 CHECK_SUPPORTED(NFC_P2P_FEATURE);
578 /* LCOV_EXCL_START */
580 CHECK_INVALID(carrier == NULL);
582 result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
584 return nfc_common_convert_error_code(__func__, result);
589 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
591 net_nfc_error_e result;
595 CHECK_SUPPORTED(NFC_P2P_FEATURE);
597 /* LCOV_EXCL_START */
599 CHECK_INVALID(config == NULL);
600 CHECK_INVALID(size == NULL);
601 CHECK_INVALID(data == NULL);
603 result = net_nfc_get_carrier_config_property(
604 (net_nfc_carrier_config_h)config,
605 attribute, size, data);
607 return nfc_common_convert_error_code(__func__, result);
611 int nfc_handover_config_destroy(nfc_handover_config_h config)
613 net_nfc_error_e result;
617 CHECK_SUPPORTED(NFC_P2P_FEATURE);
619 /* LCOV_EXCL_START */
621 CHECK_INVALID(config == NULL);
623 result = net_nfc_free_carrier_config((net_nfc_carrier_config_h)config);
625 return nfc_common_convert_error_code(__func__, result);