[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_p2p.c
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
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"
27
28 #ifndef NET_NFC_EXPORT_API
29 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
30 #endif
31
32 typedef struct _P2p_SignalHandler P2pSignalHandler;
33
34 struct _P2p_SignalHandler
35 {
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;
39
40         gpointer p2p_device_discovered_data;
41         gpointer p2p_device_detached_data;
42         gpointer p2p_data_received_data;
43 };
44
45 static NetNfcGDbusP2p *p2p_proxy = NULL;
46 static P2pSignalHandler p2p_signal_handler;
47
48 static void p2p_device_detached(GObject *source_object,
49         gpointer user_data);
50
51 static void p2p_device_discovered(GObject *source_object,
52         guint arg_handle,
53         gpointer user_data);
54
55 static void p2p_device_data_received(GObject *source_object,
56         GVariant *arg_data,
57         gpointer user_data);
58
59 static void p2p_call_send(GObject *source_object,
60         GAsyncResult *res,
61         gpointer user_data);
62
63 static void p2p_device_detached(GObject *source_object,
64         gpointer user_data)
65 {
66         INFO_MSG(">>> SIGNAL arrived");
67
68         /*llcp client function to set/unset the current target id needs to be implemented*/
69         /*net_nfc_client_llcp_current_target_id(NULL);*/
70
71         if (p2p_signal_handler.p2p_device_detached_cb)
72         {
73                 p2p_signal_handler.p2p_device_detached_cb(
74                         p2p_signal_handler.p2p_device_detached_data);
75         }
76
77         /*llcp client function to close all socket needs to be implemented*/
78         /*net_nfc_client_llcp_close_all_socket();*/
79 }
80
81 static void p2p_device_discovered(GObject *source_object,
82         guint arg_handle,
83         gpointer user_data)
84 {
85         net_nfc_target_handle_s *handle_info = NULL;
86
87         INFO_MSG(">>> SIGNAL arrived");
88
89         handle_info = GUINT_TO_POINTER(arg_handle);
90
91         if (p2p_signal_handler.p2p_device_discovered_cb)
92         {
93                 p2p_signal_handler.p2p_device_discovered_cb(handle_info,
94                                 p2p_signal_handler.p2p_device_discovered_data);
95         }
96 }
97
98 static void p2p_device_data_received(GObject *source_object,
99         GVariant *arg_data,
100         gpointer user_data)
101 {
102         INFO_MSG(">>> SIGNAL arrived");
103
104         if (p2p_signal_handler.p2p_data_received_cb)
105         {
106                 data_s p2p_data = { NULL, };
107
108                 net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
109
110                 p2p_signal_handler.p2p_data_received_cb(&p2p_data,
111                                 p2p_signal_handler.p2p_data_received_data);
112
113                 net_nfc_util_clear_data(&p2p_data);
114         }
115 }
116
117 static void p2p_call_send(GObject *source_object,
118                         GAsyncResult *res,
119                         gpointer user_data)
120 {
121         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
122         net_nfc_error_e out_result;
123         GError *error = NULL;
124
125         g_assert(user_data != NULL);
126
127         if (net_nfc_gdbus_p2p_call_send_finish(
128                 NET_NFC_GDBUS_P2P(source_object),
129                 (gint *)&out_result,
130                 res,
131                 &error) == FALSE)
132         {
133                 DEBUG_ERR_MSG("Can not finish p2p send: %s", error->message);
134                 out_result = NET_NFC_IPC_FAIL;
135
136                 g_error_free(error);
137         }
138
139         if (func_data->callback != NULL)
140         {
141                 net_nfc_client_p2p_send_completed callback =
142                         (net_nfc_client_p2p_send_completed)func_data->callback;
143
144                 callback(out_result, func_data->user_data);
145         }
146
147         g_free(func_data);
148 }
149
150
151 NET_NFC_EXPORT_API
152 net_nfc_error_e net_nfc_client_p2p_send(net_nfc_target_handle_h handle,
153         data_h data,
154         net_nfc_client_p2p_send_completed callback,
155         void *user_data)
156 {
157         GVariant *arg_data;
158         NetNfcCallback *func_data;
159
160         if (p2p_proxy == NULL)
161         {
162                 DEBUG_ERR_MSG("Can not get P2pProxy");
163
164                 return NET_NFC_NOT_INITIALIZED;
165         }
166
167         /* prevent executing daemon when nfc is off */
168         if (net_nfc_client_manager_is_activated() == false) {
169                 return NET_NFC_NOT_ACTIVATED;
170         }
171
172         func_data = g_try_new0(NetNfcCallback, 1);
173         if (func_data == NULL) {
174                 return NET_NFC_ALLOC_FAIL;
175         }
176
177         func_data->callback = (gpointer)callback;
178         func_data->user_data = user_data;
179
180         arg_data = net_nfc_util_gdbus_data_to_variant(data);
181
182         net_nfc_gdbus_p2p_call_send(p2p_proxy,
183                 0 /* FIXME */,
184                 arg_data,
185                 GPOINTER_TO_UINT(handle),
186                 NULL,
187                 p2p_call_send,
188                 func_data);
189
190         return NET_NFC_OK;
191 }
192
193
194 NET_NFC_EXPORT_API
195 net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_h handle,
196         data_h data)
197 {
198         GVariant *arg_data;
199         GError *error = NULL;
200
201         net_nfc_error_e out_result;
202
203         if (p2p_proxy == NULL)
204         {
205                 DEBUG_ERR_MSG("Can not get P2pProxy");
206
207                 return NET_NFC_NOT_INITIALIZED;
208         }
209
210         /* prevent executing daemon when nfc is off */
211         if (net_nfc_client_manager_is_activated() == false) {
212                 return NET_NFC_NOT_ACTIVATED;
213         }
214
215         arg_data = net_nfc_util_gdbus_data_to_variant(data);
216
217         if (net_nfc_gdbus_p2p_call_send_sync(p2p_proxy,
218                 0 /* FIXME */,
219                 arg_data,
220                 GPOINTER_TO_UINT(handle),
221                 (gint *)&out_result,
222                 NULL,
223                 &error) == FALSE)
224         {
225                 DEBUG_ERR_MSG("p2p send (sync call) failed: %s",
226                         error->message);
227                 out_result = NET_NFC_IPC_FAIL;
228
229                 g_error_free(error);
230         }
231
232         return out_result;
233 }
234
235
236 NET_NFC_EXPORT_API
237 void net_nfc_client_p2p_set_device_discovered(
238         net_nfc_client_p2p_device_discovered callback,
239         void *user_data)
240 {
241         if (callback == NULL)
242                 return;
243
244         if (p2p_proxy == NULL)
245         {
246                 if (net_nfc_client_p2p_init() != NET_NFC_OK)
247                 {
248                         DEBUG_ERR_MSG("P2pProxy fail");
249                         /* FIXME : return result of this error */
250                         return;
251                 }
252         }
253
254         p2p_signal_handler.p2p_device_discovered_cb = callback;
255         p2p_signal_handler.p2p_device_discovered_data = user_data;
256 }
257
258
259 NET_NFC_EXPORT_API
260 void net_nfc_client_p2p_set_device_detached(
261         net_nfc_client_p2p_device_detached callback,
262         void *user_data)
263 {
264         if (callback == NULL)
265                 return;
266
267         if (p2p_proxy == NULL)
268         {
269                 if (net_nfc_client_p2p_init() != NET_NFC_OK)
270                 {
271                         DEBUG_ERR_MSG("P2pProxy fail");
272                         /* FIXME : return result of this error */
273                         return;
274                 }
275         }
276
277         p2p_signal_handler.p2p_device_detached_cb = callback;
278         p2p_signal_handler.p2p_device_detached_data = user_data;
279 }
280
281
282 NET_NFC_EXPORT_API
283 void net_nfc_client_p2p_set_data_received(
284         net_nfc_client_p2p_data_received callback,
285         void *user_data)
286 {
287         if (callback == NULL)
288                 return;
289
290         if (p2p_proxy == NULL)
291         {
292                 if (net_nfc_client_p2p_init() != NET_NFC_OK)
293                 {
294                         DEBUG_ERR_MSG("P2pProxy fail");
295                         /* FIXME : return result of this error */
296                         return;
297                 }
298         }
299
300         p2p_signal_handler.p2p_data_received_cb = callback;
301         p2p_signal_handler.p2p_data_received_data = user_data;
302 }
303
304
305 NET_NFC_EXPORT_API
306 void net_nfc_client_p2p_unset_device_discovered(void)
307 {
308         p2p_signal_handler.p2p_device_discovered_cb = NULL;
309         p2p_signal_handler.p2p_device_discovered_data = NULL;
310 }
311
312
313 NET_NFC_EXPORT_API
314 void net_nfc_client_p2p_unset_device_detached(void)
315 {
316         p2p_signal_handler.p2p_device_detached_cb = NULL;
317         p2p_signal_handler.p2p_device_detached_data = NULL;
318 }
319
320
321 NET_NFC_EXPORT_API
322 void net_nfc_client_p2p_unset_data_received(void)
323 {
324         p2p_signal_handler.p2p_data_received_cb = NULL;
325         p2p_signal_handler.p2p_data_received_data = NULL;
326 }
327
328 net_nfc_error_e net_nfc_client_p2p_init(void)
329 {
330         GError *error = NULL;
331
332         if (p2p_proxy)
333         {
334                 DEBUG_CLIENT_MSG("Already initialized");
335
336                 return NET_NFC_OK;
337         }
338
339         p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
340                 G_BUS_TYPE_SYSTEM,
341                 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
342                 "org.tizen.NetNfcService",
343                 "/org/tizen/NetNfcService/P2p",
344                 NULL,
345                 &error);
346         if (p2p_proxy == NULL)
347         {
348                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
349                 g_error_free(error);
350
351                 return NET_NFC_UNKNOWN_ERROR;
352         }
353
354         /* FIXME : set timeout to infinite */
355         g_dbus_proxy_set_default_timeout(G_DBUS_PROXY(p2p_proxy), G_MAXINT32);
356
357         g_signal_connect(p2p_proxy, "detached",
358                         G_CALLBACK(p2p_device_detached), NULL);
359
360         g_signal_connect(p2p_proxy, "discovered",
361                         G_CALLBACK(p2p_device_discovered), NULL);
362
363         g_signal_connect(p2p_proxy, "received",
364                         G_CALLBACK(p2p_device_data_received), NULL);
365
366         return NET_NFC_OK;
367 }
368
369 void net_nfc_client_p2p_deinit(void)
370 {
371         if (p2p_proxy)
372         {
373                 g_object_unref(p2p_proxy);
374                 p2p_proxy = NULL;
375         }
376 }