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