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,
34 (guint *)&user_param);
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 > NFC_AC_TYPE_UNKNOWN);
104 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
107 case NFC_AC_TYPE_WIFI:
108 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
111 case NFC_AC_TYPE_WIFI_DIRECT:
112 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
115 case NFC_AC_TYPE_UNKNOWN:
116 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
123 parameter = g_variant_new("(uu)",
124 GPOINTER_TO_UINT(callback),
125 GPOINTER_TO_UINT(user_data));
126 if (parameter != NULL) {
127 ret = net_nfc_client_p2p_connection_handover(
128 (net_nfc_target_handle_h)target,
132 if (ret != NET_NFC_OK)
133 g_variant_unref(parameter);
135 ret = NET_NFC_ALLOC_FAIL;
138 return nfc_common_convert_error_code(__func__, ret);
142 /* LCOV_EXCL_START */
143 static void _connection_handover_event_cb(
144 net_nfc_handover_event_e event,
145 net_nfc_error_e result,
146 net_nfc_conn_handover_carrier_type_e carrier,
153 if (gdbus_nfc_context.on_handover_event_cb != NULL) {
154 nfc_ndef_message_h message;
158 net_nfc_create_ndef_message_from_rawdata(&message, ndef_message);
160 if (event == NET_NFC_HANDOVER_START) {
161 type = NFC_AC_TYPE_UNKNOWN;
165 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
166 type = NFC_AC_TYPE_BT;
167 address = nfc_common_get_bt_address_string(ac_data);
170 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
171 type = NFC_AC_TYPE_WIFI;
172 address = nfc_common_get_bt_address_string(ac_data);
175 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
176 type = NFC_AC_TYPE_WIFI_DIRECT;
177 address = nfc_common_get_bt_address_string(ac_data);
181 type = NFC_AC_TYPE_UNKNOWN;
187 gdbus_nfc_context.on_handover_event_cb(
188 nfc_common_convert_error_code(__func__, result),
193 gdbus_nfc_context.on_handover_event_user_data);
197 net_nfc_free_ndef_message(message);
204 int nfc_connection_handover_set_event_cb(nfc_connection_handover_event_cb callback, void *user_data)
208 CHECK_SUPPORTED(NFC_P2P_FEATURE);
210 /* LCOV_EXCL_START */
212 CHECK_INVALID(callback == NULL);
214 gdbus_nfc_context.on_handover_event_cb = callback;
215 gdbus_nfc_context.on_handover_event_user_data = user_data;
217 net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
219 return NFC_ERROR_NONE;
223 int nfc_connection_handover_unset_event_cb(void)
227 CHECK_SUPPORTED(NFC_P2P_FEATURE);
229 /* LCOV_EXCL_START */
232 net_nfc_client_handover_unset_handover_event_cb();
234 gdbus_nfc_context.on_handover_event_cb = NULL;
235 gdbus_nfc_context.on_handover_event_user_data = NULL;
237 return NFC_ERROR_NONE;
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 /* LCOV_EXCL_START */
255 if (carrier == NFC_AC_TYPE_BT)
256 _is_support_ac_type = true;
258 if (_is_support_ac_type)
259 set_last_result(NFC_ERROR_NONE);
261 set_last_result(NFC_ERROR_NOT_SUPPORTED);
263 return _is_support_ac_type;
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);
275 /* LCOV_EXCL_START */
277 CHECK_INVALID(msg == NULL);
278 CHECK_INVALID(result == NULL);
280 ret = net_nfc_import_handover_from_ndef_message(
281 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
283 return nfc_common_convert_error_code(__func__, ret);
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);
295 /* LCOV_EXCL_START */
297 CHECK_INVALID(message == NULL);
298 CHECK_INVALID(random_number == NULL);
300 result = net_nfc_get_handover_random_number(
301 (net_nfc_ch_message_h)message,
304 return nfc_common_convert_error_code(__func__, result);
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);
316 /* LCOV_EXCL_START */
318 CHECK_INVALID(message == NULL);
319 CHECK_INVALID(count == NULL);
321 result = net_nfc_get_handover_carrier_count(
322 (net_nfc_ch_message_h)message,
325 return nfc_common_convert_error_code(__func__, result);
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);
337 /* LCOV_EXCL_START */
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 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
353 net_nfc_error_e result;
354 net_nfc_conn_handover_carrier_type_e temp;
358 CHECK_SUPPORTED(NFC_P2P_FEATURE);
360 /* LCOV_EXCL_START */
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 int nfc_handover_message_destroy(nfc_handover_message_h message)
393 net_nfc_error_e result;
397 CHECK_SUPPORTED(NFC_P2P_FEATURE);
399 /* LCOV_EXCL_START */
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);
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);
419 /* LCOV_EXCL_START */
421 CHECK_INVALID(carrier == NULL);
422 CHECK_INVALID(cps == NULL);
424 result = net_nfc_get_handover_carrier_cps(
425 (net_nfc_ch_carrier_h)carrier, &temp);
426 if (result == NET_NFC_OK) {
428 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE:
429 *cps = NFC_AC_STATE_INACTIVATE;
432 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE:
433 *cps = NFC_AC_STATE_ACTIVATE;
436 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING:
437 *cps = NFC_AC_STATE_ACTIVATING;
441 *cps = NFC_AC_STATE_UNKNOWN;
446 return nfc_common_convert_error_code(__func__, result);
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);
459 /* LCOV_EXCL_START */
461 CHECK_INVALID(carrier == NULL);
462 CHECK_INVALID(type == NULL);
464 result = net_nfc_get_handover_carrier_type(
465 (net_nfc_ch_carrier_h)carrier, &temp);
466 if (result == NET_NFC_OK) {
468 case NET_NFC_CONN_HANDOVER_CARRIER_BT:
469 *type = NFC_AC_TYPE_BT;
472 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS:
473 *type = NFC_AC_TYPE_WIFI;
476 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P:
477 *type = NFC_AC_TYPE_WIFI_DIRECT;
481 *type = NFC_AC_TYPE_UNKNOWN;
486 return nfc_common_convert_error_code(__func__, result);
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);
498 /* LCOV_EXCL_START */
500 CHECK_INVALID(carrier == NULL);
501 CHECK_INVALID(record == NULL);
503 result = net_nfc_get_handover_carrier_record(
504 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
506 return nfc_common_convert_error_code(__func__, result);
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);
518 /* LCOV_EXCL_START */
520 CHECK_INVALID(carrier == NULL);
521 CHECK_INVALID(count == NULL);
523 result = net_nfc_get_handover_auxiliary_record_count(
524 (net_nfc_ch_carrier_h)carrier, count);
526 return nfc_common_convert_error_code(__func__, result);
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);
538 /* LCOV_EXCL_START */
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 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
553 net_nfc_error_e result;
557 CHECK_SUPPORTED(NFC_P2P_FEATURE);
559 /* LCOV_EXCL_START */
561 CHECK_INVALID(carrier == NULL);
562 CHECK_INVALID(config == NULL);
564 result = net_nfc_create_carrier_config_from_carrier(
565 (net_nfc_carrier_config_h *)config,
566 (net_nfc_ch_carrier_h)carrier);
568 return nfc_common_convert_error_code(__func__, result);
572 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
574 net_nfc_error_e result;
578 CHECK_SUPPORTED(NFC_P2P_FEATURE);
580 /* LCOV_EXCL_START */
582 CHECK_INVALID(carrier == NULL);
584 result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
586 return nfc_common_convert_error_code(__func__, result);
591 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
593 net_nfc_error_e result;
597 CHECK_SUPPORTED(NFC_P2P_FEATURE);
599 /* LCOV_EXCL_START */
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 int nfc_handover_config_destroy(nfc_handover_config_h config)
615 net_nfc_error_e result;
619 CHECK_SUPPORTED(NFC_P2P_FEATURE);
621 /* LCOV_EXCL_START */
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);