[Fix 64bit Build Error] Correctly type casting unsigned int to pointer using GLIB...
[platform/core/api/nfc.git] / src / net_nfc_client_handover.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_data.h"
24 #include "net_nfc_client.h"
25 #include "net_nfc_client_util_internal.h"
26 #include "net_nfc_client_manager.h"
27 #include "net_nfc_client_handover.h"
28
29 #ifndef NET_NFC_EXPORT_API
30 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
31 #endif
32
33 typedef struct _handover_handler_t
34 {
35         net_nfc_connection_handover_event_cb handover_event_cb;
36         gpointer handover_event_data;
37 }
38 handover_handler_t;
39
40 static NetNfcGDbusHandover *handover_proxy = NULL;
41 static handover_handler_t handover_callbacks;
42
43 static void p2p_connection_handover(GObject *source_object,
44                                 GAsyncResult *res,
45                                 gpointer user_data);
46
47 static void __started(NetNfcGDbusHandover *object,
48         GVariant *arg_message)
49 {
50         data_s message;
51
52         INFO_MSG(">>> SIGNAL arrived");
53
54         net_nfc_util_gdbus_variant_to_data_s(arg_message, &message);
55
56         if (handover_callbacks.handover_event_cb != NULL) {
57                 handover_callbacks.handover_event_cb(NET_NFC_HANDOVER_START,
58                         NET_NFC_OK, NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN,
59                         NULL, &message,
60                         handover_callbacks.handover_event_data);
61         }
62
63         net_nfc_util_clear_data(&message);
64 }
65
66 static void __finished(NetNfcGDbusHandover *object,
67         gint arg_result,
68         gint arg_type,
69         GVariant *arg_address,
70         GVariant *arg_message)
71 {
72         data_s address, message;
73
74         INFO_MSG(">>> SIGNAL arrived");
75
76         net_nfc_util_gdbus_variant_to_data_s(arg_address, &address);
77         net_nfc_util_gdbus_variant_to_data_s(arg_message, &message);
78
79         if (handover_callbacks.handover_event_cb != NULL) {
80                 handover_callbacks.handover_event_cb(NET_NFC_HANDOVER_FINISH,
81                         arg_result, arg_type, &address, &message,
82                         handover_callbacks.handover_event_data);
83         }
84
85         net_nfc_util_clear_data(&message);
86         net_nfc_util_clear_data(&address);
87 }
88
89 static void p2p_connection_handover(GObject *source_object,
90                                 GAsyncResult *res,
91                                 gpointer user_data)
92 {
93         NetNfcCallback *func_data = (NetNfcCallback *)user_data;
94         net_nfc_error_e result;
95         GVariant *data = NULL;
96         net_nfc_conn_handover_carrier_type_e type =
97                 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
98         GError *error = NULL;
99         data_s arg_data;
100
101         g_assert(user_data != NULL);
102
103         if (handover_proxy == NULL)
104         {
105                 if (net_nfc_client_handover_init() != NET_NFC_OK)
106                 {
107                         DEBUG_ERR_MSG("handover_proxy fail");
108                         /* FIXME : return result of this error */
109                         return;
110                 }
111         }
112
113         if (net_nfc_gdbus_handover_call_request_finish(handover_proxy,
114                                                 (gint *)&result,
115                                                 (gint *)&type,
116                                                 &data,
117                                                 res,
118                                                 &error) == FALSE)
119         {
120                 DEBUG_ERR_MSG("Can not finish"
121                          " connection handover: %s", error->message);
122                 result = NET_NFC_IPC_FAIL;
123
124                 g_error_free(error);
125         }
126
127         if (func_data->callback != NULL)
128         {
129                 net_nfc_p2p_connection_handover_completed_cb callback =
130                         (net_nfc_p2p_connection_handover_completed_cb)func_data->callback;
131
132                 net_nfc_util_gdbus_variant_to_data_s(data, &arg_data);
133
134                 callback(result,
135                         type,
136                         &arg_data,
137                         func_data->user_data);
138
139                 net_nfc_util_clear_data(&arg_data);
140         }
141
142         g_free(func_data);
143 }
144
145
146 NET_NFC_EXPORT_API
147 net_nfc_error_e net_nfc_client_handover_free_alternative_carrier_data(
148                         net_nfc_connection_handover_info_h info_handle)
149 {
150         net_nfc_connection_handover_info_s *info =
151                 (net_nfc_connection_handover_info_s *)info_handle;
152
153         if (info_handle == NULL)
154                 return NET_NFC_NULL_PARAMETER;
155
156         if (info->data.buffer != NULL)
157         {
158                 _net_nfc_util_free_mem(info->data.buffer);
159         }
160
161         _net_nfc_util_free_mem(info);
162
163         return NET_NFC_OK;
164 }
165
166
167 NET_NFC_EXPORT_API
168 net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_type(
169                         net_nfc_connection_handover_info_h info_handle,
170                         net_nfc_conn_handover_carrier_type_e *type)
171 {
172         net_nfc_connection_handover_info_s *info =
173                 (net_nfc_connection_handover_info_s *)info_handle;
174
175         if (info_handle == NULL || type == NULL)
176                 return NET_NFC_NULL_PARAMETER;
177
178         *type = info->type;
179
180         return NET_NFC_OK;
181 }
182
183 NET_NFC_EXPORT_API
184 net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_data(
185                         net_nfc_connection_handover_info_h info_handle,
186                         data_h *data)
187 {
188         net_nfc_connection_handover_info_s *info =
189                 (net_nfc_connection_handover_info_s *)info_handle;
190
191         if (info_handle == NULL || data == NULL)
192                 return NET_NFC_NULL_PARAMETER;
193
194         return net_nfc_create_data(data, info->data.buffer, info->data.length);
195 }
196
197
198 NET_NFC_EXPORT_API
199 net_nfc_error_e net_nfc_client_p2p_connection_handover(
200                         net_nfc_target_handle_h handle,
201                         net_nfc_conn_handover_carrier_type_e arg_type,
202                         net_nfc_p2p_connection_handover_completed_cb callback,
203                         void *cb_data)
204 {
205
206         net_nfc_target_handle_s *tag_handle = (net_nfc_target_handle_s *)handle;
207         NetNfcCallback *funcdata;
208
209         if (handover_proxy == NULL)
210         {
211                 if(net_nfc_client_handover_init() != NET_NFC_OK)
212                 {
213                         DEBUG_ERR_MSG("handover_proxy fail");
214                         return NET_NFC_NOT_INITIALIZED;
215                 }
216         }
217
218         /* prevent executing daemon when nfc is off */
219         if (net_nfc_client_manager_is_activated() == false) {
220                 return NET_NFC_NOT_ACTIVATED;
221         }
222
223         funcdata = g_try_new0(NetNfcCallback, 1);
224         if (funcdata == NULL) {
225                 return NET_NFC_ALLOC_FAIL;
226         }
227
228         funcdata->callback = (gpointer)callback;
229         funcdata->user_data = cb_data;
230
231         net_nfc_gdbus_handover_call_request(handover_proxy,
232                                         GPOINTER_TO_UINT(tag_handle),
233                                         arg_type,
234                                         NULL,
235                                         p2p_connection_handover,
236                                         funcdata);
237
238         return NET_NFC_OK;
239 }
240
241
242 NET_NFC_EXPORT_API
243 net_nfc_error_e net_nfc_client_p2p_connection_handover_sync(
244         net_nfc_target_handle_h handle,
245         net_nfc_conn_handover_carrier_type_e arg_type,
246         net_nfc_conn_handover_carrier_type_e *out_carrier,
247         data_h *out_ac_data)
248 {
249         net_nfc_target_handle_s *tag_handle = (net_nfc_target_handle_s *)handle;
250         GVariant *out_data = NULL;
251         net_nfc_error_e out_result = NET_NFC_OK;
252         net_nfc_conn_handover_carrier_type_e out_type =
253                 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
254         GError *error = NULL;
255
256         if (handover_proxy == NULL)
257         {
258                 if(net_nfc_client_handover_init() != NET_NFC_OK)
259                 {
260                         DEBUG_ERR_MSG("handover_proxy fail");
261                         return NET_NFC_NOT_INITIALIZED;
262                 }
263         }
264
265         /* prevent executing daemon when nfc is off */
266         if (net_nfc_client_manager_is_activated() == false) {
267                 return NET_NFC_NOT_ACTIVATED;
268         }
269
270         if (net_nfc_gdbus_handover_call_request_sync(handover_proxy,
271                 GPOINTER_TO_UINT(tag_handle),
272                 arg_type,
273                 (gint32 *)&out_result,
274                 (gint32 *)&out_type,
275                 &out_data,
276                 NULL,
277                 &error) == TRUE) {
278                 if (out_carrier) {
279                         *out_carrier = out_type;
280                 }
281
282                 if (out_ac_data) {
283                         *out_ac_data = net_nfc_util_gdbus_variant_to_data(out_data);
284                 }
285         } else {
286                 DEBUG_ERR_MSG("handover (sync call) failed: %s",error->message);
287                 out_result = NET_NFC_IPC_FAIL;
288
289                 g_error_free(error);
290         }
291
292         return out_result;
293 }
294
295 NET_NFC_EXPORT_API
296 void net_nfc_client_handover_set_handover_event_cb(
297         net_nfc_connection_handover_event_cb callback,
298         void *user_data)
299 {
300         if (callback == NULL)
301                 return;
302
303         if (handover_proxy == NULL)
304         {
305                 if (net_nfc_client_handover_init() != NET_NFC_OK)
306                 {
307                         DEBUG_ERR_MSG("handover_proxy fail");
308                         return;
309                 }
310         }
311
312         handover_callbacks.handover_event_cb = callback;
313         handover_callbacks.handover_event_data = user_data;
314 }
315
316
317 NET_NFC_EXPORT_API
318 void net_nfc_client_handover_unset_handover_event_cb(void)
319 {
320         handover_callbacks.handover_event_cb = NULL;
321         handover_callbacks.handover_event_data = NULL;
322 }
323
324
325 net_nfc_error_e net_nfc_client_handover_init(void)
326 {
327         GError *error = NULL;
328
329         if (handover_proxy)
330         {
331                 DEBUG_CLIENT_MSG("Already initialized");
332
333                 return NET_NFC_OK;
334         }
335
336         handover_proxy = net_nfc_gdbus_handover_proxy_new_for_bus_sync(
337                                         G_BUS_TYPE_SYSTEM,
338                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
339                                         "org.tizen.NetNfcService",
340                                         "/org/tizen/NetNfcService/Handover",
341                                         NULL,
342                                         &error);
343         if (handover_proxy == NULL)
344         {
345                 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
346                 g_error_free(error);
347
348                 return NET_NFC_UNKNOWN_ERROR;
349         }
350
351         g_signal_connect(handover_proxy, "started",
352                 G_CALLBACK(__started), NULL);
353
354         g_signal_connect(handover_proxy, "finished",
355                 G_CALLBACK(__finished), NULL);
356
357         return NET_NFC_OK;
358 }
359
360
361 void net_nfc_client_handover_deinit(void)
362 {
363         if (handover_proxy)
364         {
365                 g_object_unref(handover_proxy);
366                 handover_proxy = NULL;
367         }
368 }