revise LOG macros
[platform/core/connectivity/nfc-manager-neard.git] / client / 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_manager.h"
25 #include "net_nfc_client_p2p.h"
26
27
28 typedef struct _P2p_SignalHandler P2pSignalHandler;
29
30 struct _P2p_SignalHandler
31 {
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;
35
36         gpointer p2p_device_discovered_data;
37         gpointer p2p_device_detached_data;
38         gpointer p2p_data_received_data;
39 };
40
41 static NetNfcGDbusP2p *p2p_proxy = NULL;
42 static P2pSignalHandler p2p_signal_handler;
43
44 static void p2p_device_detached(GObject *source_object,
45                 gpointer user_data);
46
47 static void p2p_device_discovered(GObject *source_object,
48                 guint arg_handle,
49                 gpointer user_data);
50
51 static void p2p_device_data_received(GObject *source_object,
52                 GVariant *arg_data,
53                 gpointer user_data);
54
55 static void p2p_call_send(GObject *source_object,
56                 GAsyncResult *res,
57                 gpointer user_data);
58
59 static void p2p_device_detached(GObject *source_object,
60                 gpointer user_data)
61 {
62         NFC_INFO(">>> SIGNAL arrived");
63
64         /*llcp client function to set/unset the current target id needs to be implemented*/
65         /*net_nfc_client_llcp_current_target_id(NULL);*/
66
67         if (p2p_signal_handler.p2p_device_detached_cb)
68         {
69                 p2p_signal_handler.p2p_device_detached_cb(
70                                 p2p_signal_handler.p2p_device_detached_data);
71         }
72
73         /*llcp client function to close all socket needs to be implemented*/
74         /*net_nfc_client_llcp_close_all_socket();*/
75 }
76
77 static void p2p_device_discovered(GObject *source_object, guint arg_handle,
78                 gpointer user_data)
79 {
80         net_nfc_target_handle_s *handle_info = NULL;
81
82         NFC_INFO(">>> SIGNAL arrived");
83
84         handle_info = GUINT_TO_POINTER(arg_handle);
85
86         if (p2p_signal_handler.p2p_device_discovered_cb)
87         {
88                 p2p_signal_handler.p2p_device_discovered_cb(handle_info,
89                                 p2p_signal_handler.p2p_device_discovered_data);
90         }
91 }
92
93 static void p2p_device_data_received(GObject *source_object, GVariant *arg_data,
94                 gpointer user_data)
95 {
96         NFC_INFO(">>> SIGNAL arrived");
97
98         if (p2p_signal_handler.p2p_data_received_cb)
99         {
100                 data_s p2p_data = { NULL, };
101
102                 net_nfc_util_gdbus_variant_to_data_s(arg_data, &p2p_data);
103
104                 p2p_signal_handler.p2p_data_received_cb(&p2p_data,
105                                 p2p_signal_handler.p2p_data_received_data);
106
107                 net_nfc_util_free_data(&p2p_data);
108         }
109 }
110
111 static void p2p_call_send(GObject *source_object, GAsyncResult *res,
112                 gpointer user_data)
113 {
114         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
115         net_nfc_error_e out_result;
116         GError *error = NULL;
117
118         g_assert(user_data != NULL);
119
120         if (net_nfc_gdbus_p2p_call_send_finish(
121                                 NET_NFC_GDBUS_P2P(source_object),
122                                 (gint *)&out_result,
123                                 res,
124                                 &error) == FALSE)
125         {
126                 out_result = NET_NFC_IPC_FAIL;
127
128                 NFC_ERR("Can not finish p2p send: %s", error->message);
129                 g_error_free(error);
130         }
131
132         if (func_data->callback != NULL)
133         {
134                 net_nfc_client_p2p_send_completed callback =
135                         (net_nfc_client_p2p_send_completed)func_data->callback;
136
137                 callback(out_result, func_data->user_data);
138         }
139
140         g_free(func_data);
141 }
142
143
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)
146 {
147         GVariant *arg_data;
148         NetNfcCallback *func_data;
149
150         if (p2p_proxy == NULL)
151         {
152                 NFC_ERR("Can not get P2pProxy");
153
154                 return NET_NFC_NOT_INITIALIZED;
155         }
156
157         /* prevent executing daemon when nfc is off */
158         if (net_nfc_client_manager_is_activated() == false) {
159                 return NET_NFC_INVALID_STATE;
160         }
161
162         func_data = g_try_new0(NetNfcCallback, 1);
163         if (func_data == NULL) {
164                 return NET_NFC_ALLOC_FAIL;
165         }
166
167         func_data->callback = (gpointer)callback;
168         func_data->user_data = user_data;
169
170         arg_data = net_nfc_util_gdbus_data_to_variant(data);
171
172         net_nfc_gdbus_p2p_call_send(p2p_proxy,
173                         0 /* FIXME */,
174                         arg_data,
175                         GPOINTER_TO_UINT(handle),
176                         net_nfc_client_gdbus_get_privilege(),
177                         NULL,
178                         p2p_call_send,
179                         func_data);
180
181         return NET_NFC_OK;
182 }
183
184
185
186 API net_nfc_error_e net_nfc_client_p2p_send_sync(net_nfc_target_handle_h handle,
187                 data_h data)
188 {
189         GVariant *arg_data;
190         GError *error = NULL;
191
192         net_nfc_error_e out_result;
193
194         if (p2p_proxy == NULL)
195         {
196                 NFC_ERR("Can not get P2pProxy");
197
198                 return NET_NFC_NOT_INITIALIZED;
199         }
200
201         /* prevent executing daemon when nfc is off */
202         if (net_nfc_client_manager_is_activated() == false) {
203                 return NET_NFC_INVALID_STATE;
204         }
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                                 net_nfc_client_gdbus_get_privilege(),
213                                 (gint *)&out_result,
214                                 NULL,
215                                 &error) == FALSE)
216         {
217                 NFC_ERR("p2p send (sync call) failed: %s",
218                                 error->message);
219
220                 g_error_free(error);
221                 out_result = NET_NFC_IPC_FAIL;
222         }
223
224         return out_result;
225 }
226
227
228 API void net_nfc_client_p2p_set_device_discovered(
229                 net_nfc_client_p2p_device_discovered callback, void *user_data)
230 {
231         if (callback == NULL)
232                 return;
233
234         p2p_signal_handler.p2p_device_discovered_cb = callback;
235         p2p_signal_handler.p2p_device_discovered_data = user_data;
236 }
237
238
239 API void net_nfc_client_p2p_set_device_detached(
240                 net_nfc_client_p2p_device_detached callback, void *user_data)
241 {
242         if (callback == NULL)
243                 return;
244
245         p2p_signal_handler.p2p_device_detached_cb = callback;
246         p2p_signal_handler.p2p_device_detached_data = user_data;
247 }
248
249
250 API void net_nfc_client_p2p_set_data_received(
251                 net_nfc_client_p2p_data_received callback, void *user_data)
252 {
253         if (callback == NULL)
254                 return;
255
256         p2p_signal_handler.p2p_data_received_cb = callback;
257         p2p_signal_handler.p2p_data_received_data = user_data;
258 }
259
260
261 API void net_nfc_client_p2p_unset_device_discovered(void)
262 {
263         p2p_signal_handler.p2p_device_discovered_cb = NULL;
264         p2p_signal_handler.p2p_device_discovered_data = NULL;
265 }
266
267
268 API void net_nfc_client_p2p_unset_device_detached(void)
269 {
270         p2p_signal_handler.p2p_device_detached_cb = NULL;
271         p2p_signal_handler.p2p_device_detached_data = NULL;
272 }
273
274
275 API void net_nfc_client_p2p_unset_data_received(void)
276 {
277         p2p_signal_handler.p2p_data_received_cb = NULL;
278         p2p_signal_handler.p2p_data_received_data = NULL;
279 }
280
281 net_nfc_error_e net_nfc_client_p2p_init(void)
282 {
283         GError *error = NULL;
284
285         if (p2p_proxy)
286         {
287                 NFC_WARN("Already initialized");
288                 return NET_NFC_OK;
289         }
290
291         p2p_proxy = net_nfc_gdbus_p2p_proxy_new_for_bus_sync(
292                         G_BUS_TYPE_SYSTEM,
293                         G_DBUS_PROXY_FLAGS_NONE,
294                         "org.tizen.NetNfcService",
295                         "/org/tizen/NetNfcService/P2p",
296                         NULL,
297                         &error);
298         if (p2p_proxy == NULL)
299         {
300                 NFC_ERR("Can not create proxy : %s", error->message);
301                 g_error_free(error);
302
303                 return NET_NFC_UNKNOWN_ERROR;
304         }
305
306         g_signal_connect(p2p_proxy, "detached",
307                         G_CALLBACK(p2p_device_detached), NULL);
308
309         g_signal_connect(p2p_proxy, "discovered",
310                         G_CALLBACK(p2p_device_discovered), NULL);
311
312         g_signal_connect(p2p_proxy, "received",
313                         G_CALLBACK(p2p_device_data_received), NULL);
314
315         return NET_NFC_OK;
316 }
317
318 void net_nfc_client_p2p_deinit(void)
319 {
320         if (p2p_proxy)
321         {
322                 g_object_unref(p2p_proxy);
323                 p2p_proxy = NULL;
324         }
325 }