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")))
34 typedef struct _P2p_SignalHandler P2pSignalHandler;
36 struct _P2p_SignalHandler {
37 net_nfc_client_p2p_device_discovered p2p_device_discovered_cb;
38 net_nfc_client_p2p_device_detached p2p_device_detached_cb;
39 net_nfc_client_p2p_data_received p2p_data_received_cb;
41 gpointer p2p_device_discovered_data;
42 gpointer p2p_device_detached_data;
43 gpointer p2p_data_received_data;
46 static NetNfcGDbusP2p *p2p_proxy = NULL;
47 static P2pSignalHandler p2p_signal_handler;
49 static void p2p_device_detached(GObject *source_object,
52 static void p2p_device_discovered(GObject *source_object,
56 static void p2p_device_data_received(GObject *source_object,
60 static void p2p_call_send(GObject *source_object,
64 static void p2p_device_detached(GObject *source_object,
67 INFO_MSG(">>> SIGNAL arrived");
69 /*llcp client function to set/unset the current target id needs to be implemented*/
70 /*net_nfc_client_llcp_current_target_id(NULL);*/
72 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) {
92 p2p_signal_handler.p2p_device_discovered_cb(handle_info,
93 p2p_signal_handler.p2p_device_discovered_data);
97 static void p2p_device_data_received(GObject *source_object,
101 INFO_MSG(">>> SIGNAL arrived");
103 if (p2p_signal_handler.p2p_data_received_cb) {
104 data_s p2p_data = { NULL, };
106 net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
108 p2p_signal_handler.p2p_data_received_cb(&p2p_data,
109 p2p_signal_handler.p2p_data_received_data);
111 net_nfc_util_clear_data(&p2p_data);
115 static void p2p_call_send(GObject *source_object,
119 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
120 net_nfc_error_e out_result;
121 GError *error = NULL;
123 g_assert(user_data != NULL);
125 if (net_nfc_gdbus_p2p_call_send_finish(
126 NET_NFC_GDBUS_P2P(source_object),
130 DEBUG_ERR_MSG("Can not finish p2p send: %s", error->message);
131 out_result = NET_NFC_IPC_FAIL;
136 if (func_data->callback != NULL) {
137 net_nfc_client_p2p_send_completed callback =
138 (net_nfc_client_p2p_send_completed)func_data->callback;
140 callback(out_result, func_data->user_data);
148 net_nfc_error_e net_nfc_client_p2p_send(net_nfc_target_handle_h handle,
150 net_nfc_client_p2p_send_completed callback,
154 NetNfcCallback *func_data;
156 if (p2p_proxy == NULL) {
157 DEBUG_ERR_MSG("Can not get P2pProxy");
159 return NET_NFC_NOT_INITIALIZED;
162 /* prevent executing daemon when nfc is off */
163 if (net_nfc_client_manager_is_activated() == false)
164 return NET_NFC_NOT_ACTIVATED;
166 func_data = g_try_new0(NetNfcCallback, 1);
167 if (func_data == NULL)
168 return NET_NFC_ALLOC_FAIL;
170 func_data->callback = (gpointer)callback;
171 func_data->user_data = user_data;
173 arg_data = net_nfc_util_gdbus_data_to_variant(data);
175 net_nfc_gdbus_p2p_call_send(p2p_proxy,
178 GPOINTER_TO_UINT(handle),
188 net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_h handle,
192 GError *error = NULL;
194 net_nfc_error_e out_result;
196 if (p2p_proxy == NULL) {
197 DEBUG_ERR_MSG("Can not get P2pProxy");
199 return NET_NFC_NOT_INITIALIZED;
202 /* prevent executing daemon when nfc is off */
203 if (net_nfc_client_manager_is_activated() == false)
204 return NET_NFC_NOT_ACTIVATED;
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),
215 DEBUG_ERR_MSG("p2p send (sync call) failed: %s",
217 out_result = NET_NFC_IPC_FAIL;
227 void net_nfc_client_p2p_set_device_discovered(
228 net_nfc_client_p2p_device_discovered callback,
231 if (callback == NULL)
234 if (p2p_proxy == NULL) {
235 if (net_nfc_client_p2p_init() != NET_NFC_OK) {
236 DEBUG_ERR_MSG("P2pProxy fail");
237 /* FIXME : return result of this error */
242 p2p_signal_handler.p2p_device_discovered_cb = callback;
243 p2p_signal_handler.p2p_device_discovered_data = user_data;
248 void net_nfc_client_p2p_set_device_detached(
249 net_nfc_client_p2p_device_detached callback,
252 if (callback == NULL)
255 if (p2p_proxy == NULL) {
256 if (net_nfc_client_p2p_init() != NET_NFC_OK) {
257 DEBUG_ERR_MSG("P2pProxy fail");
258 /* FIXME : return result of this error */
263 p2p_signal_handler.p2p_device_detached_cb = callback;
264 p2p_signal_handler.p2p_device_detached_data = user_data;
269 void net_nfc_client_p2p_set_data_received(
270 net_nfc_client_p2p_data_received callback,
273 if (callback == NULL)
276 if (p2p_proxy == NULL) {
277 if (net_nfc_client_p2p_init() != NET_NFC_OK) {
278 DEBUG_ERR_MSG("P2pProxy fail");
279 /* FIXME : return result of this error */
284 p2p_signal_handler.p2p_data_received_cb = callback;
285 p2p_signal_handler.p2p_data_received_data = user_data;
290 void net_nfc_client_p2p_unset_device_discovered(void)
292 p2p_signal_handler.p2p_device_discovered_cb = NULL;
293 p2p_signal_handler.p2p_device_discovered_data = NULL;
298 void net_nfc_client_p2p_unset_device_detached(void)
300 p2p_signal_handler.p2p_device_detached_cb = NULL;
301 p2p_signal_handler.p2p_device_detached_data = NULL;
306 void net_nfc_client_p2p_unset_data_received(void)
308 p2p_signal_handler.p2p_data_received_cb = NULL;
309 p2p_signal_handler.p2p_data_received_data = NULL;
312 net_nfc_error_e net_nfc_client_p2p_init(void)
314 GError *error = NULL;
317 DEBUG_CLIENT_MSG("Already initialized");
322 p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
324 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
325 "org.tizen.NetNfcService",
326 "/org/tizen/NetNfcService/P2p",
329 if (p2p_proxy == NULL) {
330 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
333 return NET_NFC_UNKNOWN_ERROR;
336 /* FIXME : set timeout to infinite */
337 g_dbus_proxy_set_default_timeout(G_DBUS_PROXY(p2p_proxy), G_MAXINT32);
339 g_signal_connect(p2p_proxy, "detached",
340 G_CALLBACK(p2p_device_detached), NULL);
342 g_signal_connect(p2p_proxy, "discovered",
343 G_CALLBACK(p2p_device_discovered), NULL);
345 g_signal_connect(p2p_proxy, "received",
346 G_CALLBACK(p2p_device_data_received), NULL);
351 void net_nfc_client_p2p_deinit(void)
354 g_object_unref(p2p_proxy);