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_util_internal.h"
25 #include "net_nfc_client_manager.h"
26 #include "net_nfc_client_p2p.h"
28 #ifndef NET_NFC_EXPORT_API
29 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
32 typedef struct _P2p_SignalHandler P2pSignalHandler;
34 struct _P2p_SignalHandler
36 net_nfc_client_p2p_device_discovered p2p_device_discovered_cb;
37 net_nfc_client_p2p_device_detached p2p_device_detached_cb;
38 net_nfc_client_p2p_data_received p2p_data_received_cb;
40 gpointer p2p_device_discovered_data;
41 gpointer p2p_device_detached_data;
42 gpointer p2p_data_received_data;
45 static NetNfcGDbusP2p *p2p_proxy = NULL;
46 static P2pSignalHandler p2p_signal_handler;
48 static void p2p_device_detached(GObject *source_object,
51 static void p2p_device_discovered(GObject *source_object,
55 static void p2p_device_data_received(GObject *source_object,
59 static void p2p_call_send(GObject *source_object,
63 static void p2p_device_detached(GObject *source_object,
66 INFO_MSG(">>> SIGNAL arrived");
68 /*llcp client function to set/unset the current target id needs to be implemented*/
69 /*net_nfc_client_llcp_current_target_id(NULL);*/
71 if (p2p_signal_handler.p2p_device_detached_cb)
73 p2p_signal_handler.p2p_device_detached_cb(
74 p2p_signal_handler.p2p_device_detached_data);
77 /*llcp client function to close all socket needs to be implemented*/
78 /*net_nfc_client_llcp_close_all_socket();*/
81 static void p2p_device_discovered(GObject *source_object,
85 net_nfc_target_handle_s *handle_info = NULL;
87 INFO_MSG(">>> SIGNAL arrived");
89 handle_info = GUINT_TO_POINTER(arg_handle);
91 if (p2p_signal_handler.p2p_device_discovered_cb)
93 p2p_signal_handler.p2p_device_discovered_cb(handle_info,
94 p2p_signal_handler.p2p_device_discovered_data);
98 static void p2p_device_data_received(GObject *source_object,
102 INFO_MSG(">>> SIGNAL arrived");
104 if (p2p_signal_handler.p2p_data_received_cb)
106 data_s p2p_data = { NULL, };
108 net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
110 p2p_signal_handler.p2p_data_received_cb(&p2p_data,
111 p2p_signal_handler.p2p_data_received_data);
113 net_nfc_util_clear_data(&p2p_data);
117 static void p2p_call_send(GObject *source_object,
121 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
122 net_nfc_error_e out_result;
123 GError *error = NULL;
125 g_assert(user_data != NULL);
127 if (net_nfc_gdbus_p2p_call_send_finish(
128 NET_NFC_GDBUS_P2P(source_object),
133 DEBUG_ERR_MSG("Can not finish p2p send: %s", error->message);
134 out_result = NET_NFC_IPC_FAIL;
139 if (func_data->callback != NULL)
141 net_nfc_client_p2p_send_completed callback =
142 (net_nfc_client_p2p_send_completed)func_data->callback;
144 callback(out_result, func_data->user_data);
152 net_nfc_error_e net_nfc_client_p2p_send(net_nfc_target_handle_h handle,
154 net_nfc_client_p2p_send_completed callback,
158 NetNfcCallback *func_data;
160 if (p2p_proxy == NULL)
162 DEBUG_ERR_MSG("Can not get P2pProxy");
164 return NET_NFC_NOT_INITIALIZED;
167 /* prevent executing daemon when nfc is off */
168 if (net_nfc_client_manager_is_activated() == false) {
169 return NET_NFC_NOT_ACTIVATED;
172 func_data = g_try_new0(NetNfcCallback, 1);
173 if (func_data == NULL) {
174 return NET_NFC_ALLOC_FAIL;
177 func_data->callback = (gpointer)callback;
178 func_data->user_data = user_data;
180 arg_data = net_nfc_util_gdbus_data_to_variant(data);
182 net_nfc_gdbus_p2p_call_send(p2p_proxy,
185 GPOINTER_TO_UINT(handle),
195 net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_h handle,
199 GError *error = NULL;
201 net_nfc_error_e out_result;
203 if (p2p_proxy == NULL)
205 DEBUG_ERR_MSG("Can not get P2pProxy");
207 return NET_NFC_NOT_INITIALIZED;
210 /* prevent executing daemon when nfc is off */
211 if (net_nfc_client_manager_is_activated() == false) {
212 return NET_NFC_NOT_ACTIVATED;
215 arg_data = net_nfc_util_gdbus_data_to_variant(data);
217 if (net_nfc_gdbus_p2p_call_send_sync(p2p_proxy,
220 GPOINTER_TO_UINT(handle),
225 DEBUG_ERR_MSG("p2p send (sync call) failed: %s",
227 out_result = NET_NFC_IPC_FAIL;
237 void net_nfc_client_p2p_set_device_discovered(
238 net_nfc_client_p2p_device_discovered callback,
241 if (callback == NULL)
244 if (p2p_proxy == NULL)
246 if (net_nfc_client_p2p_init() != NET_NFC_OK)
248 DEBUG_ERR_MSG("P2pProxy fail");
249 /* FIXME : return result of this error */
254 p2p_signal_handler.p2p_device_discovered_cb = callback;
255 p2p_signal_handler.p2p_device_discovered_data = user_data;
260 void net_nfc_client_p2p_set_device_detached(
261 net_nfc_client_p2p_device_detached callback,
264 if (callback == NULL)
267 if (p2p_proxy == NULL)
269 if (net_nfc_client_p2p_init() != NET_NFC_OK)
271 DEBUG_ERR_MSG("P2pProxy fail");
272 /* FIXME : return result of this error */
277 p2p_signal_handler.p2p_device_detached_cb = callback;
278 p2p_signal_handler.p2p_device_detached_data = user_data;
283 void net_nfc_client_p2p_set_data_received(
284 net_nfc_client_p2p_data_received callback,
287 if (callback == NULL)
290 if (p2p_proxy == NULL)
292 if (net_nfc_client_p2p_init() != NET_NFC_OK)
294 DEBUG_ERR_MSG("P2pProxy fail");
295 /* FIXME : return result of this error */
300 p2p_signal_handler.p2p_data_received_cb = callback;
301 p2p_signal_handler.p2p_data_received_data = user_data;
306 void net_nfc_client_p2p_unset_device_discovered(void)
308 p2p_signal_handler.p2p_device_discovered_cb = NULL;
309 p2p_signal_handler.p2p_device_discovered_data = NULL;
314 void net_nfc_client_p2p_unset_device_detached(void)
316 p2p_signal_handler.p2p_device_detached_cb = NULL;
317 p2p_signal_handler.p2p_device_detached_data = NULL;
322 void net_nfc_client_p2p_unset_data_received(void)
324 p2p_signal_handler.p2p_data_received_cb = NULL;
325 p2p_signal_handler.p2p_data_received_data = NULL;
328 net_nfc_error_e net_nfc_client_p2p_init(void)
330 GError *error = NULL;
334 DEBUG_CLIENT_MSG("Already initialized");
339 p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
341 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
342 "org.tizen.NetNfcService",
343 "/org/tizen/NetNfcService/P2p",
346 if (p2p_proxy == NULL)
348 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
351 return NET_NFC_UNKNOWN_ERROR;
354 /* FIXME : set timeout to infinite */
355 g_dbus_proxy_set_default_timeout(G_DBUS_PROXY(p2p_proxy), G_MAXINT32);
357 g_signal_connect(p2p_proxy, "detached",
358 G_CALLBACK(p2p_device_detached), NULL);
360 g_signal_connect(p2p_proxy, "discovered",
361 G_CALLBACK(p2p_device_discovered), NULL);
363 g_signal_connect(p2p_proxy, "received",
364 G_CALLBACK(p2p_device_data_received), NULL);
369 void net_nfc_client_p2p_deinit(void)
373 g_object_unref(p2p_proxy);