2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://floralicense.org/license/
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.
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"
29 #ifndef NET_NFC_EXPORT_API
30 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
33 typedef struct _handover_handler_t
35 net_nfc_connection_handover_event_cb handover_event_cb;
36 gpointer handover_event_data;
40 static NetNfcGDbusHandover *handover_proxy = NULL;
41 static handover_handler_t handover_callbacks;
43 static void p2p_connection_handover(GObject *source_object,
47 static void __started(NetNfcGDbusHandover *object,
48 GVariant *arg_message)
52 INFO_MSG(">>> SIGNAL arrived");
54 net_nfc_util_gdbus_variant_to_data_s(arg_message, &message);
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,
60 handover_callbacks.handover_event_data);
63 net_nfc_util_clear_data(&message);
66 static void __finished(NetNfcGDbusHandover *object,
69 GVariant *arg_address,
70 GVariant *arg_message)
72 data_s address, message;
74 INFO_MSG(">>> SIGNAL arrived");
76 net_nfc_util_gdbus_variant_to_data_s(arg_address, &address);
77 net_nfc_util_gdbus_variant_to_data_s(arg_message, &message);
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);
85 net_nfc_util_clear_data(&message);
86 net_nfc_util_clear_data(&address);
89 static void p2p_connection_handover(GObject *source_object,
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;
101 g_assert(user_data != NULL);
103 if (handover_proxy == NULL)
105 if (net_nfc_client_handover_init() != NET_NFC_OK)
107 DEBUG_ERR_MSG("handover_proxy fail");
108 /* FIXME : return result of this error */
113 if (net_nfc_gdbus_handover_call_request_finish(handover_proxy,
120 DEBUG_ERR_MSG("Can not finish"
121 " connection handover: %s", error->message);
122 result = NET_NFC_IPC_FAIL;
127 if (func_data->callback != NULL)
129 net_nfc_p2p_connection_handover_completed_cb callback =
130 (net_nfc_p2p_connection_handover_completed_cb)func_data->callback;
132 net_nfc_util_gdbus_variant_to_data_s(data, &arg_data);
137 func_data->user_data);
139 net_nfc_util_clear_data(&arg_data);
147 net_nfc_error_e net_nfc_client_handover_free_alternative_carrier_data(
148 net_nfc_connection_handover_info_h info_handle)
150 net_nfc_connection_handover_info_s *info =
151 (net_nfc_connection_handover_info_s *)info_handle;
153 if (info_handle == NULL)
154 return NET_NFC_NULL_PARAMETER;
156 if (info->data.buffer != NULL)
158 _net_nfc_util_free_mem(info->data.buffer);
161 _net_nfc_util_free_mem(info);
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)
172 net_nfc_connection_handover_info_s *info =
173 (net_nfc_connection_handover_info_s *)info_handle;
175 if (info_handle == NULL || type == NULL)
176 return NET_NFC_NULL_PARAMETER;
184 net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_data(
185 net_nfc_connection_handover_info_h info_handle,
188 net_nfc_connection_handover_info_s *info =
189 (net_nfc_connection_handover_info_s *)info_handle;
191 if (info_handle == NULL || data == NULL)
192 return NET_NFC_NULL_PARAMETER;
194 return net_nfc_create_data(data, info->data.buffer, info->data.length);
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,
206 net_nfc_target_handle_s *tag_handle = (net_nfc_target_handle_s *)handle;
207 NetNfcCallback *funcdata;
209 if (handover_proxy == NULL)
211 if(net_nfc_client_handover_init() != NET_NFC_OK)
213 DEBUG_ERR_MSG("handover_proxy fail");
214 return NET_NFC_NOT_INITIALIZED;
218 /* prevent executing daemon when nfc is off */
219 if (net_nfc_client_manager_is_activated() == false) {
220 return NET_NFC_NOT_ACTIVATED;
223 funcdata = g_try_new0(NetNfcCallback, 1);
224 if (funcdata == NULL) {
225 return NET_NFC_ALLOC_FAIL;
228 funcdata->callback = (gpointer)callback;
229 funcdata->user_data = cb_data;
231 net_nfc_gdbus_handover_call_request(handover_proxy,
232 GPOINTER_TO_UINT(tag_handle),
235 p2p_connection_handover,
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,
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;
256 if (handover_proxy == NULL)
258 if(net_nfc_client_handover_init() != NET_NFC_OK)
260 DEBUG_ERR_MSG("handover_proxy fail");
261 return NET_NFC_NOT_INITIALIZED;
265 /* prevent executing daemon when nfc is off */
266 if (net_nfc_client_manager_is_activated() == false) {
267 return NET_NFC_NOT_ACTIVATED;
270 if (net_nfc_gdbus_handover_call_request_sync(handover_proxy,
271 GPOINTER_TO_UINT(tag_handle),
273 (gint32 *)&out_result,
279 *out_carrier = out_type;
283 *out_ac_data = net_nfc_util_gdbus_variant_to_data(out_data);
286 DEBUG_ERR_MSG("handover (sync call) failed: %s",error->message);
287 out_result = NET_NFC_IPC_FAIL;
296 void net_nfc_client_handover_set_handover_event_cb(
297 net_nfc_connection_handover_event_cb callback,
300 if (callback == NULL)
303 if (handover_proxy == NULL)
305 if (net_nfc_client_handover_init() != NET_NFC_OK)
307 DEBUG_ERR_MSG("handover_proxy fail");
312 handover_callbacks.handover_event_cb = callback;
313 handover_callbacks.handover_event_data = user_data;
318 void net_nfc_client_handover_unset_handover_event_cb(void)
320 handover_callbacks.handover_event_cb = NULL;
321 handover_callbacks.handover_event_data = NULL;
325 net_nfc_error_e net_nfc_client_handover_init(void)
327 GError *error = NULL;
331 DEBUG_CLIENT_MSG("Already initialized");
336 handover_proxy = net_nfc_gdbus_handover_proxy_new_for_bus_sync(
338 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
339 "org.tizen.NetNfcService",
340 "/org/tizen/NetNfcService/Handover",
343 if (handover_proxy == NULL)
345 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
348 return NET_NFC_UNKNOWN_ERROR;
351 g_signal_connect(handover_proxy, "started",
352 G_CALLBACK(__started), NULL);
354 g_signal_connect(handover_proxy, "finished",
355 G_CALLBACK(__finished), NULL);
361 void net_nfc_client_handover_deinit(void)
365 g_object_unref(handover_proxy);
366 handover_proxy = NULL;