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,
119 DEBUG_ERR_MSG("Can not finish connection handover: %s", error->message);
123 result = NET_NFC_IPC_FAIL;
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;
130 net_nfc_util_gdbus_variant_to_data_s(data, &arg_data);
135 func_data->user_data);
137 net_nfc_util_clear_data(&arg_data);
145 net_nfc_error_e net_nfc_client_handover_free_alternative_carrier_data(
146 net_nfc_connection_handover_info_h info_handle)
148 net_nfc_connection_handover_info_s *info =
149 (net_nfc_connection_handover_info_s *)info_handle;
151 if (info_handle == NULL)
152 return NET_NFC_NULL_PARAMETER;
154 if (info->data.buffer != NULL)
155 _net_nfc_util_free_mem(info->data.buffer);
157 _net_nfc_util_free_mem(info);
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)
168 net_nfc_connection_handover_info_s *info =
169 (net_nfc_connection_handover_info_s *)info_handle;
171 if (info_handle == NULL || type == NULL)
172 return NET_NFC_NULL_PARAMETER;
180 net_nfc_error_e net_nfc_client_handover_get_alternative_carrier_data(
181 net_nfc_connection_handover_info_h info_handle,
184 net_nfc_connection_handover_info_s *info =
185 (net_nfc_connection_handover_info_s *)info_handle;
187 if (info_handle == NULL || data == NULL)
188 return NET_NFC_NULL_PARAMETER;
190 return net_nfc_create_data(data, info->data.buffer, info->data.length);
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,
202 net_nfc_target_handle_s *tag_handle = (net_nfc_target_handle_s *)handle;
203 NetNfcCallback *funcdata;
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;
212 /* prevent executing daemon when nfc is off */
213 if (net_nfc_client_manager_is_activated() == false)
214 return NET_NFC_NOT_ACTIVATED;
216 funcdata = g_try_new0(NetNfcCallback, 1);
217 if (funcdata == NULL)
218 return NET_NFC_ALLOC_FAIL;
220 funcdata->callback = (gpointer)callback;
221 funcdata->user_data = cb_data;
223 net_nfc_gdbus_handover_call_request(handover_proxy,
224 GPOINTER_TO_UINT(tag_handle),
227 p2p_connection_handover,
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,
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;
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;
255 /* prevent executing daemon when nfc is off */
256 if (net_nfc_client_manager_is_activated() == false)
257 return NET_NFC_NOT_ACTIVATED;
259 if (net_nfc_gdbus_handover_call_request_sync(handover_proxy,
260 GPOINTER_TO_UINT(tag_handle),
262 (gint32 *)&out_result,
268 *out_carrier = out_type;
271 *out_ac_data = net_nfc_util_gdbus_variant_to_data(out_data);
274 DEBUG_ERR_MSG("handover (sync call) failed: %s", error->message);
278 out_result = NET_NFC_IPC_FAIL;
285 void net_nfc_client_handover_set_handover_event_cb(
286 net_nfc_connection_handover_event_cb callback,
289 if (callback == NULL)
292 if (handover_proxy == NULL) {
293 if (net_nfc_client_handover_init() != NET_NFC_OK) {
294 DEBUG_ERR_MSG("handover_proxy fail");
299 handover_callbacks.handover_event_cb = callback;
300 handover_callbacks.handover_event_data = user_data;
305 void net_nfc_client_handover_unset_handover_event_cb(void)
307 handover_callbacks.handover_event_cb = NULL;
308 handover_callbacks.handover_event_data = NULL;
312 net_nfc_error_e net_nfc_client_handover_init(void)
314 GError *error = NULL;
316 if (handover_proxy) {
317 DEBUG_CLIENT_MSG("Already initialized");
322 handover_proxy = net_nfc_gdbus_handover_proxy_new_for_bus_sync(
324 G_DBUS_PROXY_FLAGS_NONE,
325 "org.tizen.NetNfcService",
326 "/org/tizen/NetNfcService/Handover",
329 if (handover_proxy == NULL) {
331 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
335 return NET_NFC_UNKNOWN_ERROR;
338 g_signal_connect(handover_proxy, "started",
339 G_CALLBACK(__started), NULL);
341 g_signal_connect(handover_proxy, "finished",
342 G_CALLBACK(__finished), NULL);
348 void net_nfc_client_handover_deinit(void)
350 if (handover_proxy) {
351 g_object_unref(handover_proxy);
352 handover_proxy = NULL;