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 static void _p2p_handover_cb(net_nfc_error_e result,
20 net_nfc_conn_handover_carrier_type_e carrier, data_h ac_data, void *user_data)
22 nfc_p2p_connection_handover_completed_cb callback;
27 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,
79 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);
96 CHECK_INVALID(target == NULL);
97 CHECK_INVALID(type > NFC_AC_TYPE_UNKNOWN);
102 case NFC_AC_TYPE_BT :
103 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_BT;
106 case NFC_AC_TYPE_WIFI :
107 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
110 case NFC_AC_TYPE_WIFI_DIRECT :
111 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
114 case NFC_AC_TYPE_UNKNOWN :
115 net_ac_type = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
122 parameter = g_variant_new("(uu)",
123 GPOINTER_TO_UINT(callback),
124 GPOINTER_TO_UINT(user_data));
125 if (parameter != NULL) {
126 ret = net_nfc_client_p2p_connection_handover(
127 (net_nfc_target_handle_h)target,
131 if (ret != NET_NFC_OK) {
132 g_variant_unref(parameter);
135 ret = NET_NFC_ALLOC_FAIL;
138 return nfc_common_convert_error_code(__func__, ret);
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);
193 if (address != NULL) {
196 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 CHECK_INVALID(callback == NULL);
210 gdbus_nfc_context.on_handover_event_cb = callback;
211 gdbus_nfc_context.on_handover_event_user_data = user_data;
213 net_nfc_client_handover_set_handover_event_cb(_connection_handover_event_cb, NULL);
215 return NFC_ERROR_NONE;
218 int nfc_connection_handover_unset_event_cb(void)
222 CHECK_SUPPORTED(NFC_P2P_FEATURE);
225 net_nfc_client_handover_unset_handover_event_cb();
227 gdbus_nfc_context.on_handover_event_cb = NULL;
228 gdbus_nfc_context.on_handover_event_user_data = NULL;
230 return NFC_ERROR_NONE;
233 bool nfc_p2p_is_supported_ac_type(nfc_ac_type_e carrier)
239 if (carrier == NFC_AC_TYPE_BT)
245 int nfc_handover_message_import_from_ndef_message(nfc_handover_message_h *result, nfc_ndef_message_h msg)
251 CHECK_SUPPORTED(NFC_P2P_FEATURE);
253 CHECK_INVALID(msg == NULL);
254 CHECK_INVALID(result == NULL);
256 ret = net_nfc_import_handover_from_ndef_message(
257 (ndef_message_h)msg, (net_nfc_ch_message_h *)result);
259 return nfc_common_convert_error_code(__func__, ret);
262 int nfc_handover_message_get_random_number(nfc_handover_message_h message, unsigned short *random_number)
264 net_nfc_error_e result;
268 CHECK_SUPPORTED(NFC_P2P_FEATURE);
270 CHECK_INVALID(message == NULL);
271 CHECK_INVALID(random_number == NULL);
273 result = net_nfc_get_handover_random_number(
274 (net_nfc_ch_message_h)message,
277 return nfc_common_convert_error_code(__func__, result);
280 int nfc_handover_message_get_carrier_count(nfc_handover_message_h message, unsigned int *count)
282 net_nfc_error_e result;
286 CHECK_SUPPORTED(NFC_P2P_FEATURE);
288 CHECK_INVALID(message == NULL);
289 CHECK_INVALID(count == NULL);
291 result = net_nfc_get_handover_carrier_count(
292 (net_nfc_ch_message_h)message,
295 return nfc_common_convert_error_code(__func__, result);
298 int nfc_handover_message_get_carrier(nfc_handover_message_h message, int index, nfc_handover_carrier_h *carrier)
300 net_nfc_error_e result;
304 CHECK_SUPPORTED(NFC_P2P_FEATURE);
306 CHECK_INVALID(message == NULL);
307 CHECK_INVALID(carrier == NULL);
309 result = net_nfc_get_handover_carrier(
310 (net_nfc_ch_message_h)message,
312 (net_nfc_ch_carrier_h *)carrier);
314 return nfc_common_convert_error_code(__func__, result);
317 int nfc_handover_message_get_carrier_by_type(nfc_handover_message_h message, nfc_ac_type_e type, nfc_handover_carrier_h *carrier)
319 net_nfc_error_e result;
320 net_nfc_conn_handover_carrier_type_e temp;
324 CHECK_SUPPORTED(NFC_P2P_FEATURE);
326 CHECK_INVALID(message == NULL);
327 CHECK_INVALID(carrier == NULL);
330 case NFC_AC_TYPE_BT :
331 temp = NET_NFC_CONN_HANDOVER_CARRIER_BT;
334 case NFC_AC_TYPE_WIFI :
335 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS;
338 case NFC_AC_TYPE_WIFI_DIRECT :
339 temp = NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P;
343 temp = NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
346 result = net_nfc_get_handover_carrier_by_type(
347 (net_nfc_ch_message_h)message,
349 (net_nfc_ch_carrier_h *)carrier);
351 return nfc_common_convert_error_code(__func__, result);
354 int nfc_handover_message_destroy(nfc_handover_message_h message)
356 net_nfc_error_e result;
360 CHECK_SUPPORTED(NFC_P2P_FEATURE);
362 CHECK_INVALID(message == NULL);
364 result = net_nfc_free_handover_message((net_nfc_ch_message_h)message);
366 return nfc_common_convert_error_code(__func__, result);
370 int nfc_handover_carrier_get_cps(nfc_handover_carrier_h carrier, nfc_ac_state_e *cps)
372 net_nfc_error_e result;
373 net_nfc_conn_handover_carrier_state_e temp;
377 CHECK_SUPPORTED(NFC_P2P_FEATURE);
379 CHECK_INVALID(carrier == NULL);
380 CHECK_INVALID(cps == NULL);
382 result = net_nfc_get_handover_carrier_cps(
383 (net_nfc_ch_carrier_h)carrier, &temp);
384 if (result == NET_NFC_OK) {
386 case NET_NFC_CONN_HANDOVER_CARRIER_INACTIVATE :
387 *cps = NFC_AC_STATE_INACTIVATE;
390 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATE :
391 *cps = NFC_AC_STATE_ACTIVATE;
394 case NET_NFC_CONN_HANDOVER_CARRIER_ACTIVATING :
395 *cps = NFC_AC_STATE_ACTIVATING;
399 *cps = NFC_AC_STATE_UNKNOWN;
404 return nfc_common_convert_error_code(__func__, result);
407 int nfc_handover_carrier_get_type(nfc_handover_carrier_h carrier, nfc_ac_type_e *type)
409 net_nfc_error_e result;
410 net_nfc_conn_handover_carrier_type_e temp;
414 CHECK_SUPPORTED(NFC_P2P_FEATURE);
416 CHECK_INVALID(carrier == NULL);
417 CHECK_INVALID(type == NULL);
419 result = net_nfc_get_handover_carrier_type(
420 (net_nfc_ch_carrier_h)carrier, &temp);
421 if (result == NET_NFC_OK) {
423 case NET_NFC_CONN_HANDOVER_CARRIER_BT :
424 *type = NFC_AC_TYPE_BT;
427 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_WPS :
428 *type = NFC_AC_TYPE_WIFI;
431 case NET_NFC_CONN_HANDOVER_CARRIER_WIFI_P2P :
432 *type = NFC_AC_TYPE_WIFI_DIRECT;
436 *type = NFC_AC_TYPE_UNKNOWN;
441 return nfc_common_convert_error_code(__func__, result);
444 int nfc_handover_carrier_get_carrier_record(nfc_handover_carrier_h carrier, nfc_ndef_record_h *record)
446 net_nfc_error_e result;
450 CHECK_SUPPORTED(NFC_P2P_FEATURE);
452 CHECK_INVALID(carrier == NULL);
453 CHECK_INVALID(record == NULL);
455 result = net_nfc_get_handover_carrier_record(
456 (net_nfc_ch_carrier_h)carrier, (ndef_record_h *)record);
458 return nfc_common_convert_error_code(__func__, result);
461 int nfc_handover_carrier_get_auxiliary_record_count(nfc_handover_carrier_h carrier, unsigned int *count)
463 net_nfc_error_e result;
467 CHECK_SUPPORTED(NFC_P2P_FEATURE);
469 CHECK_INVALID(carrier == NULL);
470 CHECK_INVALID(count == NULL);
472 result = net_nfc_get_handover_auxiliary_record_count(
473 (net_nfc_ch_carrier_h)carrier, count);
475 return nfc_common_convert_error_code(__func__, result);
478 int nfc_handover_carrier_get_auxiliary_record(nfc_handover_carrier_h carrier, int index, nfc_ndef_record_h *record)
480 net_nfc_error_e result;
484 CHECK_SUPPORTED(NFC_P2P_FEATURE);
486 CHECK_INVALID(carrier == NULL);
487 CHECK_INVALID(record == NULL);
489 result = net_nfc_get_handover_auxiliary_record(
490 (net_nfc_ch_carrier_h)carrier,
491 index, (ndef_record_h *)record);
493 return nfc_common_convert_error_code(__func__, result);
496 int nfc_handover_carrier_get_handover_config(nfc_handover_carrier_h carrier, nfc_handover_config_h *config)
498 net_nfc_error_e result;
502 CHECK_SUPPORTED(NFC_P2P_FEATURE);
504 CHECK_INVALID(carrier == NULL);
505 CHECK_INVALID(config == NULL);
507 result = net_nfc_create_carrier_config_from_carrier(
508 (net_nfc_carrier_config_h *)config,
509 (net_nfc_ch_carrier_h)carrier);
511 return nfc_common_convert_error_code(__func__, result);
514 int nfc_handover_carrier_destroy(nfc_handover_carrier_h carrier)
516 net_nfc_error_e result;
520 CHECK_SUPPORTED(NFC_P2P_FEATURE);
522 CHECK_INVALID(carrier == NULL);
524 result = net_nfc_free_handover_carrier((net_nfc_ch_carrier_h)carrier);
526 return nfc_common_convert_error_code(__func__, result);
530 int nfc_handover_config_get_property(nfc_handover_config_h config, unsigned short attribute, unsigned short *size, unsigned char **data)
532 net_nfc_error_e result;
536 CHECK_SUPPORTED(NFC_P2P_FEATURE);
538 CHECK_INVALID(config == NULL);
539 CHECK_INVALID(size == NULL);
540 CHECK_INVALID(data == NULL);
542 result = net_nfc_get_carrier_config_property(
543 (net_nfc_carrier_config_h)config,
544 attribute, size, data);
546 return nfc_common_convert_error_code(__func__, result);
549 int nfc_handover_config_destroy(nfc_handover_config_h config)
551 net_nfc_error_e result;
555 CHECK_SUPPORTED(NFC_P2P_FEATURE);
557 CHECK_INVALID(config == NULL);
559 result = net_nfc_free_carrier_config((net_nfc_carrier_config_h)config);
561 return nfc_common_convert_error_code(__func__, result);