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