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 "net_nfc_typedef_internal.h"
18 #include "net_nfc_debug_internal.h"
19 #include "net_nfc_util_internal.h"
20 #include "net_nfc_util_ndef_message.h"
21 #include "net_nfc_util_gdbus_internal.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_client.h"
24 #include "net_nfc_client_manager.h"
25 #include "net_nfc_client_p2p.h"
26 #include "net_nfc_neard.h"
29 typedef struct _P2p_SignalHandler P2pSignalHandler;
31 struct _P2p_SignalHandler
33 net_nfc_client_p2p_device_discovered p2p_device_discovered_cb;
34 net_nfc_client_p2p_device_detached p2p_device_detached_cb;
35 net_nfc_client_p2p_data_received p2p_data_received_cb;
37 gpointer p2p_device_discovered_data;
38 gpointer p2p_device_detached_data;
39 gpointer p2p_data_received_data;
42 static NetNfcGDbusP2p *p2p_proxy = NULL;
43 static P2pSignalHandler p2p_signal_handler;
45 static void p2p_device_detached(GObject *source_object,
48 NFC_INFO(">>> SIGNAL arrived");
50 RET_IF(NULL == p2p_signal_handler.p2p_device_detached_cb);
52 /*llcp client function to set/unset the current target id needs to be implemented*/
53 /*net_nfc_client_llcp_current_target_id(NULL);*/
55 p2p_signal_handler.p2p_device_detached_cb(p2p_signal_handler.p2p_device_detached_data);
57 /*llcp client function to close all socket needs to be implemented*/
58 /*net_nfc_client_llcp_close_all_socket();*/
61 static void p2p_device_discovered(GObject *source_object, guint arg_handle,
64 net_nfc_target_handle_s *handle_info = NULL;
66 NFC_INFO(">>> SIGNAL arrived");
68 RET_IF(NULL == p2p_signal_handler.p2p_device_discovered_cb);
70 handle_info = GUINT_TO_POINTER(arg_handle);
73 p2p_signal_handler.p2p_device_discovered_cb(handle_info,
74 p2p_signal_handler.p2p_device_discovered_data);
78 static void p2p_device_data_received(GObject *source_object, GVariant *arg_data,
81 data_s p2p_data = { NULL, };
83 NFC_INFO(">>> SIGNAL arrived");
85 RET_IF(NULL == p2p_signal_handler.p2p_data_received_cb);
87 net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
88 p2p_signal_handler.p2p_data_received_cb(&p2p_data,
89 p2p_signal_handler.p2p_data_received_data);
91 net_nfc_util_free_data(&p2p_data);
94 API net_nfc_error_e net_nfc_client_p2p_send(net_nfc_target_handle_s *handle,
95 data_s *data, net_nfc_client_p2p_send_completed callback, void *user_data)
97 /* prevent executing daemon when nfc is off */
98 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
100 return net_nfc_neard_send_p2p(handle, data, callback, user_data);
105 API net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_s *handle,
110 GError *error = NULL;
111 net_nfc_error_e out_result;
113 RETV_IF(NULL == p2p_proxy, NET_NFC_NOT_INITIALIZED);
115 /* prevent executing daemon when nfc is off */
116 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
118 arg_data = net_nfc_util_gdbus_data_to_variant(data);
120 ret = net_nfc_gdbus_p2p_call_send_sync(p2p_proxy,
123 GPOINTER_TO_UINT(handle),
124 net_nfc_client_gdbus_get_privilege(),
131 NFC_ERR("p2p send (sync call) failed: %s", error->message);
134 out_result = NET_NFC_IPC_FAIL;
141 API void net_nfc_client_p2p_set_device_discovered(
142 net_nfc_client_p2p_device_discovered callback, void *user_data)
144 RET_IF(NULL == callback);
146 net_nfc_neard_set_p2p_discovered(callback, user_data);
150 API void net_nfc_client_p2p_set_device_detached(
151 net_nfc_client_p2p_device_detached callback, void *user_data)
153 RET_IF(NULL == callback);
155 net_nfc_neard_set_p2p_detached(callback, user_data);
159 API void net_nfc_client_p2p_set_data_received(
160 net_nfc_client_p2p_data_received callback, void *user_data)
162 RET_IF(NULL == callback);
164 net_nfc_neard_set_p2p_data_received(callback, user_data);
168 API void net_nfc_client_p2p_unset_device_discovered(void)
170 net_nfc_neard_unset_p2p_discovered();
174 API void net_nfc_client_p2p_unset_device_detached(void)
176 net_nfc_neard_unset_p2p_detached();
180 API void net_nfc_client_p2p_unset_data_received(void)
182 net_nfc_neard_unset_p2p_data_received();
185 net_nfc_error_e net_nfc_client_p2p_init(void)
187 GError *error = NULL;
191 NFC_WARN("Already initialized");
195 p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
197 G_DBUS_PROXY_FLAGS_NONE,
198 "org.tizen.NetNfcService",
199 "/org/tizen/NetNfcService/P2p",
203 if (NULL == p2p_proxy)
205 NFC_ERR("Can not create proxy : %s", error->message);
208 return NET_NFC_UNKNOWN_ERROR;
211 g_signal_connect(p2p_proxy, "detached", G_CALLBACK(p2p_device_detached), NULL);
212 g_signal_connect(p2p_proxy, "discovered", G_CALLBACK(p2p_device_discovered), NULL);
213 g_signal_connect(p2p_proxy, "received", G_CALLBACK(p2p_device_data_received), NULL);
218 void net_nfc_client_p2p_deinit(void)
222 g_object_unref(p2p_proxy);