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"
31 #ifndef NET_NFC_EXPORT_API
32 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
35 typedef struct _handover_handler_t {
36 net_nfc_connection_handover_event_cb handover_event_cb;
37 gpointer handover_event_data;
41 static NetNfcGDbusHandover *handover_proxy = NULL;
42 static handover_handler_t handover_callbacks;
44 static void p2p_connection_handover(GObject *source_object,
48 static void __started(NetNfcGDbusHandover *object,
49 GVariant *arg_message)
53 INFO_MSG(">>> SIGNAL arrived");
55 net_nfc_util_gdbus_variant_to_data_s(arg_message, &message);
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,
61 handover_callbacks.handover_event_data);
64 net_nfc_util_clear_data(&message);
67 static void __finished(NetNfcGDbusHandover *object,
70 GVariant *arg_address,
71 GVariant *arg_message)
73 data_s address, message;
75 INFO_MSG(">>> SIGNAL arrived");
77 net_nfc_util_gdbus_variant_to_data_s(arg_address, &address);
78 net_nfc_util_gdbus_variant_to_data_s(arg_message, &message);
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);
86 net_nfc_util_clear_data(&message);
87 net_nfc_util_clear_data(&address);
90 static void p2p_connection_handover(GObject *source_object,
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;
102 g_assert(user_data != NULL);
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 */
112 if (net_nfc_gdbus_handover_call_request_finish(handover_proxy,
118 DEBUG_ERR_MSG("Can not finish"
119 " connection handover: %s", error->message);
120 result = NET_NFC_IPC_FAIL;
125 if (func_data->callback != NULL) {
126 net_nfc_p2p_connection_handover_completed_cb callback =
127 (net_nfc_p2p_connection_handover_completed_cb)func_data->callback;
129 net_nfc_util_gdbus_variant_to_data_s(data, &arg_data);
134 func_data->user_data);
136 net_nfc_util_clear_data(&arg_data);
144 net_nfc_error_e net_nfc_client_handover_free_alternative_carrier_data(
145 net_nfc_connection_handover_info_h info_handle)
147 net_nfc_connection_handover_info_s *info =
148 (net_nfc_connection_handover_info_s *)info_handle;
150 if (info_handle == NULL)
151 return NET_NFC_NULL_PARAMETER;
153 if (info->data.buffer != NULL)
154 _net_nfc_util_free_mem(info->data.buffer);
156 _net_nfc_util_free_mem(info);
163 net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_type(
164 net_nfc_connection_handover_info_h info_handle,
165 net_nfc_conn_handover_carrier_type_e *type)
167 net_nfc_connection_handover_info_s *info =
168 (net_nfc_connection_handover_info_s *)info_handle;
170 if (info_handle == NULL || type == NULL)
171 return NET_NFC_NULL_PARAMETER;
179 net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_data(
180 net_nfc_connection_handover_info_h info_handle,
183 net_nfc_connection_handover_info_s *info =
184 (net_nfc_connection_handover_info_s *)info_handle;
186 if (info_handle == NULL || data == NULL)
187 return NET_NFC_NULL_PARAMETER;
189 return net_nfc_create_data(data, info->data.buffer, info->data.length);
194 net_nfc_error_e net_nfc_client_p2p_connection_handover(
195 net_nfc_target_handle_h handle,
196 net_nfc_conn_handover_carrier_type_e arg_type,
197 net_nfc_p2p_connection_handover_completed_cb callback,
201 net_nfc_target_handle_s *tag_handle = (net_nfc_target_handle_s *)handle;
202 NetNfcCallback *funcdata;
204 if (handover_proxy == NULL) {
205 if (net_nfc_client_handover_init() != NET_NFC_OK) {
206 DEBUG_ERR_MSG("handover_proxy fail");
207 return NET_NFC_NOT_INITIALIZED;
211 /* prevent executing daemon when nfc is off */
212 if (net_nfc_client_manager_is_activated() == false)
213 return NET_NFC_NOT_ACTIVATED;
215 funcdata = g_try_new0(NetNfcCallback, 1);
216 if (funcdata == NULL)
217 return NET_NFC_ALLOC_FAIL;
219 funcdata->callback = (gpointer)callback;
220 funcdata->user_data = cb_data;
222 net_nfc_gdbus_handover_call_request(handover_proxy,
223 GPOINTER_TO_UINT(tag_handle),
226 p2p_connection_handover,
234 net_nfc_error_e net_nfc_client_p2p_connection_handover_sync(
235 net_nfc_target_handle_h handle,
236 net_nfc_conn_handover_carrier_type_e arg_type,
237 net_nfc_conn_handover_carrier_type_e *out_carrier,
240 net_nfc_target_handle_s *tag_handle = (net_nfc_target_handle_s *)handle;
241 GVariant *out_data = NULL;
242 net_nfc_error_e out_result = NET_NFC_OK;
243 net_nfc_conn_handover_carrier_type_e out_type =
244 NET_NFC_CONN_HANDOVER_CARRIER_UNKNOWN;
245 GError *error = NULL;
247 if (handover_proxy == NULL) {
248 if (net_nfc_client_handover_init() != NET_NFC_OK) {
249 DEBUG_ERR_MSG("handover_proxy fail");
250 return NET_NFC_NOT_INITIALIZED;
254 /* prevent executing daemon when nfc is off */
255 if (net_nfc_client_manager_is_activated() == false)
256 return NET_NFC_NOT_ACTIVATED;
258 if (net_nfc_gdbus_handover_call_request_sync(handover_proxy,
259 GPOINTER_TO_UINT(tag_handle),
261 (gint32 *)&out_result,
267 *out_carrier = out_type;
270 *out_ac_data = net_nfc_util_gdbus_variant_to_data(out_data);
272 DEBUG_ERR_MSG("handover (sync call) failed: %s", error->message);
273 out_result = NET_NFC_IPC_FAIL;
282 void net_nfc_client_handover_set_handover_event_cb(
283 net_nfc_connection_handover_event_cb callback,
286 if (callback == NULL)
289 if (handover_proxy == NULL) {
290 if (net_nfc_client_handover_init() != NET_NFC_OK) {
291 DEBUG_ERR_MSG("handover_proxy fail");
296 handover_callbacks.handover_event_cb = callback;
297 handover_callbacks.handover_event_data = user_data;
302 void net_nfc_client_handover_unset_handover_event_cb(void)
304 handover_callbacks.handover_event_cb = NULL;
305 handover_callbacks.handover_event_data = NULL;
309 net_nfc_error_e net_nfc_client_handover_init(void)
311 GError *error = NULL;
313 if (handover_proxy) {
314 DEBUG_CLIENT_MSG("Already initialized");
319 handover_proxy = net_nfc_gdbus_handover_proxy_new_for_bus_sync(
321 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
322 "org.tizen.NetNfcService",
323 "/org/tizen/NetNfcService/Handover",
326 if (handover_proxy == NULL) {
327 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
330 return NET_NFC_UNKNOWN_ERROR;
333 g_signal_connect(handover_proxy, "started",
334 G_CALLBACK(__started), NULL);
336 g_signal_connect(handover_proxy, "finished",
337 G_CALLBACK(__finished), NULL);
343 void net_nfc_client_handover_deinit(void)
345 if (handover_proxy) {
346 g_object_unref(handover_proxy);
347 handover_proxy = NULL;