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_send_cb(net_nfc_error_e result, void *user_data)
22 nfc_p2p_send_completed_cb callback;
27 if (user_data == NULL)
30 g_variant_get((GVariant *)user_data,
36 callback(nfc_common_convert_error_code(__func__, result), user_param);
38 g_variant_unref((GVariant *)user_data);
42 int nfc_p2p_send(nfc_p2p_target_h target,
43 nfc_ndef_message_h message,
44 nfc_p2p_send_completed_cb callback,
53 CHECK_SUPPORTED(NFC_P2P_FEATURE);
57 CHECK_INVALID(message == NULL);
58 CHECK_INVALID(target == NULL);
60 CHECK_APP_PERMISSION();
62 parameter = g_variant_new("(tt)",
65 if (parameter != NULL) {
66 net_nfc_create_rawdata_from_ndef_message(message, &rawdata);
67 ret = net_nfc_client_p2p_send((net_nfc_target_handle_h)target,
71 if (ret != NET_NFC_OK)
72 g_variant_unref(parameter);
74 net_nfc_free_data(rawdata);
76 ret = NET_NFC_ALLOC_FAIL;
79 return nfc_common_convert_error_code(__func__, ret);
83 int nfc_p2p_send_no_permission(
84 nfc_p2p_target_h target,
85 nfc_ndef_message_h message,
86 nfc_p2p_send_completed_cb callback,
95 CHECK_SUPPORTED(NFC_P2P_FEATURE);
99 CHECK_INVALID(target == NULL);
100 CHECK_INVALID(message == NULL);
103 /* skip check app permission */
105 parameter = g_variant_new("(tt)",
108 if (parameter != NULL) {
109 net_nfc_create_rawdata_from_ndef_message(message, &rawdata);
110 ret = net_nfc_client_p2p_send((net_nfc_target_handle_h)target,
114 if (ret != NET_NFC_OK)
115 g_variant_unref(parameter);
117 net_nfc_free_data(rawdata);
119 ret = NET_NFC_ALLOC_FAIL;
122 return nfc_common_convert_error_code(__func__, ret);
126 /* LCOV_EXCL_START */
127 static void _p2p_set_data_received_cb(data_h data, void *user_data)
131 if (gdbus_nfc_context.on_p2p_recv_cb != NULL) {
132 ndef_message_h ndef_message;
134 net_nfc_create_ndef_message_from_rawdata(&ndef_message, data);
136 gdbus_nfc_context.on_p2p_recv_cb(
137 (nfc_p2p_target_h)(gdbus_nfc_context.current_target),
139 gdbus_nfc_context.on_p2p_recv_user_data);
141 net_nfc_free_ndef_message(ndef_message);
146 int nfc_p2p_set_data_received_cb(nfc_p2p_target_h target,
147 nfc_p2p_data_received_cb callback,
152 CHECK_SUPPORTED(NFC_P2P_FEATURE);
154 /* LCOV_EXCL_START */
156 CHECK_INVALID(target == NULL);
157 CHECK_INVALID(callback == NULL);
158 CHECK_INVALID(gdbus_nfc_context.current_target != target);
160 gdbus_nfc_context.on_p2p_recv_cb = callback;
161 gdbus_nfc_context.on_p2p_recv_user_data = user_data;
163 net_nfc_client_p2p_set_data_received(_p2p_set_data_received_cb, NULL);
165 return NFC_ERROR_NONE;
169 int nfc_p2p_unset_data_received_cb(nfc_p2p_target_h target)
173 CHECK_SUPPORTED(NFC_P2P_FEATURE);
175 /* LCOV_EXCL_START */
177 CHECK_INVALID(target == NULL);
178 CHECK_INVALID(gdbus_nfc_context.current_target != target);
180 net_nfc_client_p2p_unset_data_received();
182 gdbus_nfc_context.on_p2p_recv_cb = NULL;
183 gdbus_nfc_context.on_p2p_recv_user_data = NULL;
185 return NFC_ERROR_NONE;
189 /* LCOV_EXCL_START */
190 static void _snep_start_server_cb(
191 net_nfc_snep_handle_h arg_handle,
192 net_nfc_snep_type_t event,
193 net_nfc_error_e result,
194 nfc_ndef_message_h msg,
197 nfc_snep_event_cb callback;
202 if (user_data == NULL) {
203 LOG_ERR("user_data is NULL");
207 g_variant_get((GVariant *)user_data,
212 if (callback != NULL) {
213 nfc_snep_event_e snep_event;
215 switch ((int)event) {
216 case NET_NFC_SNEP_GET:
217 snep_event = NFC_SNEP_EVENT_GET;
220 case NET_NFC_SNEP_PUT:
221 snep_event = NFC_SNEP_EVENT_PUT;
224 case NET_NFC_LLCP_REGISTERED:
225 snep_event = NFC_SNEP_EVENT_REGISTER;
228 case NET_NFC_LLCP_UNREGISTERED:
229 snep_event = NFC_SNEP_EVENT_UNREGISTER;
232 case NET_NFC_LLCP_START:
233 snep_event = NFC_SNEP_EVENT_START;
236 case NET_NFC_LLCP_STOP:
238 snep_event = NFC_SNEP_EVENT_STOP;
242 callback(arg_handle, snep_event,
243 nfc_common_convert_error_code(__func__, result),
246 if (snep_event == NFC_SNEP_EVENT_STOP)
247 g_variant_unref(user_data);
252 int nfc_snep_start_server(nfc_p2p_target_h target,
255 nfc_snep_event_cb callback,
258 net_nfc_error_e result;
263 CHECK_SUPPORTED(NFC_P2P_FEATURE);
265 /* LCOV_EXCL_START */
267 CHECK_INVALID(target == NULL);
269 parameter = g_variant_new("(tt)",
272 if (parameter != NULL) {
273 result = net_nfc_client_snep_start_server(
274 (net_nfc_target_handle_h)target,
277 _snep_start_server_cb,
279 if (result != NET_NFC_OK)
280 g_variant_unref(parameter);
282 result = NET_NFC_ALLOC_FAIL;
285 return nfc_common_convert_error_code(__func__, result);
289 /* LCOV_EXCL_START */
290 static void _snep_start_client_cb(net_nfc_snep_handle_h arg_handle,
291 net_nfc_snep_type_t event,
292 net_nfc_error_e result,
293 nfc_ndef_message_h msg,
296 nfc_snep_event_cb callback;
301 if (user_data == NULL) {
302 LOG_ERR("user_data is NULL");
306 g_variant_get((GVariant *)user_data,
311 if (callback != NULL) {
312 nfc_snep_event_e snep_event;
314 switch ((int)event) {
315 case NET_NFC_SNEP_GET:
316 snep_event = NFC_SNEP_EVENT_GET;
319 case NET_NFC_SNEP_PUT:
320 snep_event = NFC_SNEP_EVENT_PUT;
323 case NET_NFC_LLCP_REGISTERED:
324 snep_event = NFC_SNEP_EVENT_REGISTER;
327 case NET_NFC_LLCP_UNREGISTERED:
328 snep_event = NFC_SNEP_EVENT_UNREGISTER;
331 case NET_NFC_LLCP_START:
332 snep_event = NFC_SNEP_EVENT_START;
335 case NET_NFC_LLCP_STOP:
337 snep_event = NFC_SNEP_EVENT_STOP;
341 callback(arg_handle, snep_event,
342 nfc_common_convert_error_code(__func__, result),
345 if (snep_event == NFC_SNEP_EVENT_STOP)
346 g_variant_unref(user_data);
351 int nfc_snep_start_client(nfc_p2p_target_h target,
354 nfc_snep_event_cb callback,
357 net_nfc_error_e result;
362 CHECK_SUPPORTED(NFC_P2P_FEATURE);
364 /* LCOV_EXCL_START */
366 CHECK_INVALID(target == NULL);
368 parameter = g_variant_new("(tt)",
371 if (parameter != NULL) {
372 result = net_nfc_client_snep_start_client(
373 (net_nfc_target_handle_h)target,
376 _snep_start_client_cb,
378 if (result != NET_NFC_OK)
379 g_variant_unref(parameter);
381 result = NET_NFC_ALLOC_FAIL;
384 return nfc_common_convert_error_code(__func__, result);
388 /* LCOV_EXCL_START */
389 static void _snep_send_request_cb(net_nfc_snep_handle_h target,
390 net_nfc_snep_type_t event, net_nfc_error_e result, ndef_message_h msg, void *user_data)
392 nfc_snep_event_cb callback;
397 if (user_data == NULL) {
398 LOG_ERR("user_data is NULL");
402 g_variant_get((GVariant *)user_data,
407 if (callback != NULL) {
408 callback((nfc_p2p_snep_h)target,
410 nfc_common_convert_error_code(__func__, result),
411 (nfc_ndef_message_h)msg,
415 g_variant_unref((GVariant *)user_data);
419 int nfc_snep_send_client_request(nfc_p2p_snep_h handle,
420 nfc_snep_type_e type,
421 nfc_ndef_message_h msg,
422 nfc_snep_event_cb callback,
425 net_nfc_error_e result;
430 CHECK_SUPPORTED(NFC_P2P_FEATURE);
432 /* LCOV_EXCL_START */
434 CHECK_INVALID(handle == NULL);
435 CHECK_INVALID(msg == NULL);
437 parameter = g_variant_new("(tt)",
440 if (parameter != NULL) {
441 result = net_nfc_client_snep_send_client_request(
442 (net_nfc_target_handle_h)handle,
445 _snep_send_request_cb,
447 if (result != NET_NFC_OK)
448 g_variant_unref(parameter);
450 result = NET_NFC_ALLOC_FAIL;
453 return nfc_common_convert_error_code(__func__, result);
457 /* LCOV_EXCL_START */
458 static void _snep_event_cb(net_nfc_snep_handle_h target, net_nfc_snep_type_t event,
459 net_nfc_error_e result, ndef_message_h msg, void *user_data)
461 nfc_snep_event_cb callback;
466 if (user_data == NULL) {
467 LOG_ERR("user_data is NULL");
471 g_variant_get((GVariant *)user_data,
476 if (callback != NULL) {
477 switch ((int)event) {
478 case NET_NFC_SNEP_GET:
479 event = NFC_SNEP_EVENT_GET;
482 case NET_NFC_SNEP_PUT:
483 event = NFC_SNEP_EVENT_PUT;
486 case NET_NFC_LLCP_REGISTERED:
487 event = NFC_SNEP_EVENT_REGISTER;
490 case NET_NFC_LLCP_UNREGISTERED:
491 event = NFC_SNEP_EVENT_UNREGISTER;
494 case NET_NFC_LLCP_START:
495 event = NFC_SNEP_EVENT_START;
498 case NET_NFC_LLCP_STOP:
499 event = NFC_SNEP_EVENT_STOP;
503 callback(target, event,
504 nfc_common_convert_error_code(__func__, result),
505 (ndef_message_h)msg, user_param);
510 int nfc_snep_register_server(const char *san, int sap,
511 nfc_snep_event_cb callback, void *user_data)
513 net_nfc_error_e result;
518 CHECK_SUPPORTED(NFC_P2P_FEATURE);
520 /* LCOV_EXCL_START */
522 CHECK_INVALID(san == NULL);
523 CHECK_INVALID(sap == 0);
525 parameter = g_variant_new("(tt)",
528 if (parameter != NULL) {
529 result = net_nfc_client_snep_register_server(san, (sap_t)sap,
530 _snep_event_cb, parameter);
531 if (result != NET_NFC_OK)
532 g_variant_unref(parameter);
534 result = NET_NFC_ALLOC_FAIL;
537 return nfc_common_convert_error_code(__func__, result);
541 int nfc_snep_unregister_server(const char *san, int sap)
543 net_nfc_error_e result;
547 CHECK_SUPPORTED(NFC_P2P_FEATURE);
549 /* LCOV_EXCL_START */
552 result = net_nfc_client_snep_unregister_server(san, (sap_t)sap);
554 return nfc_common_convert_error_code(__func__, result);
558 int nfc_snep_stop_service(nfc_p2p_target_h target, nfc_p2p_snep_h clients)
560 net_nfc_error_e result;
564 CHECK_SUPPORTED(NFC_P2P_FEATURE);
566 /* LCOV_EXCL_START */
569 result = net_nfc_client_snep_stop_service_sync(target, clients);
571 return nfc_common_convert_error_code(__func__, result);