2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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"
28 typedef struct _P2p_SignalHandler P2pSignalHandler;
30 struct _P2p_SignalHandler
32 net_nfc_client_p2p_device_discovered p2p_device_discovered_cb;
33 net_nfc_client_p2p_device_detached p2p_device_detached_cb;
34 net_nfc_client_p2p_data_received p2p_data_received_cb;
36 gpointer p2p_device_discovered_data;
37 gpointer p2p_device_detached_data;
38 gpointer p2p_data_received_data;
41 static NetNfcGDbusP2p *p2p_proxy = NULL;
42 static P2pSignalHandler p2p_signal_handler;
44 static void p2p_device_detached(GObject *source_object,
47 static void p2p_device_discovered(GObject *source_object,
51 static void p2p_device_data_received(GObject *source_object,
55 static void p2p_call_send(GObject *source_object,
59 static void p2p_device_detached(GObject *source_object,
62 NFC_INFO(">>> SIGNAL arrived");
64 /*llcp client function to set/unset the current target id needs to be implemented*/
65 /*net_nfc_client_llcp_current_target_id(NULL);*/
67 if (p2p_signal_handler.p2p_device_detached_cb)
69 p2p_signal_handler.p2p_device_detached_cb(
70 p2p_signal_handler.p2p_device_detached_data);
73 /*llcp client function to close all socket needs to be implemented*/
74 /*net_nfc_client_llcp_close_all_socket();*/
77 static void p2p_device_discovered(GObject *source_object, guint arg_handle,
80 net_nfc_target_handle_s *handle_info = NULL;
82 NFC_INFO(">>> SIGNAL arrived");
84 handle_info = GUINT_TO_POINTER(arg_handle);
86 if (p2p_signal_handler.p2p_device_discovered_cb)
88 p2p_signal_handler.p2p_device_discovered_cb(handle_info,
89 p2p_signal_handler.p2p_device_discovered_data);
93 static void p2p_device_data_received(GObject *source_object, GVariant *arg_data,
96 NFC_INFO(">>> SIGNAL arrived");
98 if (p2p_signal_handler.p2p_data_received_cb)
100 data_s p2p_data = { NULL, };
102 net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
104 p2p_signal_handler.p2p_data_received_cb(&p2p_data,
105 p2p_signal_handler.p2p_data_received_data);
107 net_nfc_util_free_data(&p2p_data);
111 static void p2p_call_send(GObject *source_object, GAsyncResult *res,
114 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
115 net_nfc_error_e out_result;
116 GError *error = NULL;
118 g_assert(user_data != NULL);
120 if (net_nfc_gdbus_p2p_call_send_finish(
121 NET_NFC_GDBUS_P2P(source_object),
126 out_result = NET_NFC_IPC_FAIL;
128 NFC_ERR("Can not finish p2p send: %s", error->message);
132 if (func_data->callback != NULL)
134 net_nfc_client_p2p_send_completed callback =
135 (net_nfc_client_p2p_send_completed)func_data->callback;
137 callback(out_result, func_data->user_data);
144 API net_nfc_error_e net_nfc_client_p2p_send(net_nfc_target_handle_h handle,
145 data_h data, net_nfc_client_p2p_send_completed callback, void *user_data)
148 NetNfcCallback *func_data;
150 if (p2p_proxy == NULL)
152 NFC_ERR("Can not get P2pProxy");
154 return NET_NFC_NOT_INITIALIZED;
157 /* prevent executing daemon when nfc is off */
158 if (net_nfc_client_manager_is_activated() == false) {
159 return NET_NFC_INVALID_STATE;
162 func_data = g_try_new0(NetNfcCallback, 1);
163 if (func_data == NULL) {
164 return NET_NFC_ALLOC_FAIL;
167 func_data->callback = (gpointer)callback;
168 func_data->user_data = user_data;
170 arg_data = net_nfc_util_gdbus_data_to_variant(data);
172 net_nfc_gdbus_p2p_call_send(p2p_proxy,
175 GPOINTER_TO_UINT(handle),
176 net_nfc_client_gdbus_get_privilege(),
186 API net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_h handle,
190 GError *error = NULL;
192 net_nfc_error_e out_result;
194 if (p2p_proxy == NULL)
196 NFC_ERR("Can not get P2pProxy");
198 return NET_NFC_NOT_INITIALIZED;
201 /* prevent executing daemon when nfc is off */
202 if (net_nfc_client_manager_is_activated() == false) {
203 return NET_NFC_INVALID_STATE;
206 arg_data = net_nfc_util_gdbus_data_to_variant(data);
208 if (net_nfc_gdbus_p2p_call_send_sync(p2p_proxy,
211 GPOINTER_TO_UINT(handle),
212 net_nfc_client_gdbus_get_privilege(),
217 NFC_ERR("p2p send (sync call) failed: %s",
221 out_result = NET_NFC_IPC_FAIL;
228 API void net_nfc_client_p2p_set_device_discovered(
229 net_nfc_client_p2p_device_discovered callback, void *user_data)
231 if (callback == NULL)
234 p2p_signal_handler.p2p_device_discovered_cb = callback;
235 p2p_signal_handler.p2p_device_discovered_data = user_data;
239 API void net_nfc_client_p2p_set_device_detached(
240 net_nfc_client_p2p_device_detached callback, void *user_data)
242 if (callback == NULL)
245 p2p_signal_handler.p2p_device_detached_cb = callback;
246 p2p_signal_handler.p2p_device_detached_data = user_data;
250 API void net_nfc_client_p2p_set_data_received(
251 net_nfc_client_p2p_data_received callback, void *user_data)
253 if (callback == NULL)
256 p2p_signal_handler.p2p_data_received_cb = callback;
257 p2p_signal_handler.p2p_data_received_data = user_data;
261 API void net_nfc_client_p2p_unset_device_discovered(void)
263 p2p_signal_handler.p2p_device_discovered_cb = NULL;
264 p2p_signal_handler.p2p_device_discovered_data = NULL;
268 API void net_nfc_client_p2p_unset_device_detached(void)
270 p2p_signal_handler.p2p_device_detached_cb = NULL;
271 p2p_signal_handler.p2p_device_detached_data = NULL;
275 API void net_nfc_client_p2p_unset_data_received(void)
277 p2p_signal_handler.p2p_data_received_cb = NULL;
278 p2p_signal_handler.p2p_data_received_data = NULL;
281 net_nfc_error_e net_nfc_client_p2p_init(void)
283 GError *error = NULL;
287 NFC_WARN("Already initialized");
291 p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
293 G_DBUS_PROXY_FLAGS_NONE,
294 "org.tizen.NetNfcService",
295 "/org/tizen/NetNfcService/P2p",
298 if (p2p_proxy == NULL)
300 NFC_ERR("Can not create proxy : %s", error->message);
303 return NET_NFC_UNKNOWN_ERROR;
306 g_signal_connect(p2p_proxy, "detached",
307 G_CALLBACK(p2p_device_detached), NULL);
309 g_signal_connect(p2p_proxy, "discovered",
310 G_CALLBACK(p2p_device_discovered), NULL);
312 g_signal_connect(p2p_proxy, "received",
313 G_CALLBACK(p2p_device_data_received), NULL);
318 void net_nfc_client_p2p_deinit(void)
322 g_object_unref(p2p_proxy);