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_client.h"
24 #include "net_nfc_client_manager.h"
25 #include "net_nfc_client_phdc.h"
27 typedef struct _Phdc_SignalHandler PhdcSignalHandler;
29 static NetNfcGDbusPhdc *phdc_proxy = NULL;
30 static PhdcSignalHandler phdc_signal_handler;
32 struct _Phdc_SignalHandler
34 net_nfc_client_phdc_transport_connect_indication phdc_transport_connect_indication_cb;
35 net_nfc_client_phdc_transport_disconnect_indication phdc_transport_disconnect_indication_cb;
36 net_nfc_client_phdc_data_received phdc_data_received_cb;
38 gpointer phdc_transport_connect_indication_data;
39 gpointer phdc_transport_disconnect_indication_data;
40 gpointer phdc_data_received_data;
44 static void _phdc_event_cb(NetNfcGDbusPhdc *object, gint arg_result,
45 guint arg_event,guint arg_user_data)
47 GVariant *parameter = (GVariant *)GUINT_TO_POINTER(arg_user_data);
49 NFC_DBG(" result [%d], event [%d], user_data [%p]",
50 arg_result, arg_event, parameter);
52 if (parameter != NULL)
55 net_nfc_client_phdc_event_cb callback;
57 g_variant_get(parameter, "(uu)",
63 callback( arg_result, arg_event, user_data);
67 if (arg_event == NET_NFC_LLCP_UNREGISTERED)
69 g_variant_unref(parameter);
75 static void phdc_transport_disconnect_indication(GObject *source_object, gpointer user_data)
77 NFC_INFO(">>> SIGNAL arrived");
79 RET_IF(NULL == phdc_signal_handler.phdc_transport_disconnect_indication_cb);
81 phdc_signal_handler.phdc_transport_disconnect_indication_cb(
82 phdc_signal_handler.phdc_transport_disconnect_indication_data);
85 static void phdc_transport_connect_indication(GObject *source_object,
86 guint arg_handle, gpointer user_data)
88 net_nfc_phdc_handle_h handle_info = NULL;
90 NFC_INFO(">>> SIGNAL arrived");
92 handle_info = GUINT_TO_POINTER(arg_handle);
94 RET_IF(NULL == phdc_signal_handler.phdc_transport_connect_indication_cb);
96 phdc_signal_handler.phdc_transport_connect_indication_cb(handle_info,
97 phdc_signal_handler.phdc_transport_connect_indication_data);
100 static void phdc_call_send(GObject *source_object,GAsyncResult *res, gpointer user_data)
103 GVariant *parameter = (GVariant *)user_data;
104 GError *error = NULL;
105 net_nfc_error_e out_result;
106 net_nfc_client_phdc_send_completed callback;
107 net_nfc_phdc_handle_h handle;
110 NFC_INFO(">>> phdc_call_send \n");
112 ret = net_nfc_gdbus_phdc_call_send_finish(NET_NFC_GDBUS_PHDC(source_object),
113 (gint *)&out_result, res, &error);
117 out_result = NET_NFC_IPC_FAIL;
119 NFC_ERR("Can not finish phdc send: %s", error->message);
123 g_variant_get(parameter, "(uuu)",
125 (guint *)&user_param,
128 NFC_INFO(">>> phdc_call_send %p\n", handle);
130 if (callback != NULL)
132 callback( out_result, user_param);
135 g_variant_unref(parameter);
138 static void phdc_device_data_received(GObject *source_object,GVariant *arg_data,
141 data_s phdc_data = { NULL, };
143 NFC_INFO(">>> SIGNAL arrived");
145 RET_IF(NULL == phdc_signal_handler.phdc_data_received_cb);
147 net_nfc_util_gdbus_variant_to_data_s(arg_data, &phdc_data);
148 phdc_signal_handler.phdc_data_received_cb(&phdc_data,
149 phdc_signal_handler.phdc_data_received_data);
151 net_nfc_util_free_data(&phdc_data);
154 API net_nfc_error_e net_nfc_client_phdc_send(net_nfc_phdc_handle_h handle,
155 data_s *data, net_nfc_client_phdc_send_completed callback, void *user_data)
160 NFC_INFO(">>> net_nfc_client_phdc_send \n");
162 RETV_IF(NULL == phdc_proxy, NET_NFC_NOT_INITIALIZED);
164 /* prevent executing daemon when nfc is off */
165 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
167 parameter = g_variant_new("(uuu)",
168 GPOINTER_TO_UINT(callback),
169 GPOINTER_TO_UINT(user_data),
170 GPOINTER_TO_UINT(handle));
173 arg_data = net_nfc_util_gdbus_data_to_variant(data);
175 net_nfc_gdbus_phdc_call_send(phdc_proxy,
176 GPOINTER_TO_UINT(handle),
178 net_nfc_client_gdbus_get_privilege(),
187 API net_nfc_error_e net_nfc_client_phdc_send_sync(net_nfc_phdc_handle_h handle,
192 GError *error = NULL;
194 net_nfc_error_e out_result;
196 RETV_IF(NULL == phdc_proxy, NET_NFC_NOT_INITIALIZED);
198 /* prevent executing daemon when nfc is off */
199 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
201 arg_data = net_nfc_util_gdbus_data_to_variant(data);
203 ret = net_nfc_gdbus_phdc_call_send_sync(phdc_proxy,
204 GPOINTER_TO_UINT(handle),
206 net_nfc_client_gdbus_get_privilege(),
213 NFC_ERR("phdc send (sync call) failed: %s", error->message);
216 out_result = NET_NFC_IPC_FAIL;
222 API net_nfc_error_e net_nfc_client_phdc_register(net_nfc_phdc_role_e role,
223 const char *san,net_nfc_client_phdc_event_cb callback, void *user_data)
225 GError *error = NULL;
227 net_nfc_error_e result = NET_NFC_OK;
229 RETV_IF(NULL == phdc_proxy, NET_NFC_NOT_INITIALIZED);
231 /* prevent executing daemon when nfc is off */
232 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
234 parameter = g_variant_new("(uu)",
235 GPOINTER_TO_UINT(callback),
236 GPOINTER_TO_UINT(user_data));
238 if (net_nfc_gdbus_phdc_call_register_role_sync(phdc_proxy,
241 GPOINTER_TO_UINT(parameter),
242 net_nfc_client_gdbus_get_privilege(),
247 NFC_ERR("phdc register role(sync call) failed: %s", error->message);
249 result = NET_NFC_IPC_FAIL;
250 g_variant_unref(parameter);
256 API net_nfc_error_e net_nfc_client_phdc_unregister( net_nfc_phdc_role_e role,
259 GError *error = NULL;
260 net_nfc_error_e result = NET_NFC_OK;
262 RETV_IF(NULL == phdc_proxy, NET_NFC_NOT_INITIALIZED);
264 /* prevent executing daemon when nfc is off */
265 RETV_IF(net_nfc_client_manager_is_activated() == false, NET_NFC_INVALID_STATE);
267 if (net_nfc_gdbus_phdc_call_unregister_role_sync(phdc_proxy,
270 net_nfc_client_gdbus_get_privilege(),
275 NFC_ERR("phdc unregister role(sync call) failed: %s", error->message);
278 result = NET_NFC_IPC_FAIL;
285 API void net_nfc_client_phdc_set_transport_connect_indication(
286 net_nfc_client_phdc_transport_connect_indication callback, void *user_data)
288 RET_IF(NULL == callback);
290 phdc_signal_handler.phdc_transport_connect_indication_cb = callback;
291 phdc_signal_handler.phdc_transport_connect_indication_data = user_data;
294 API void net_nfc_client_phdc_set_transport_disconnect_indication(
295 net_nfc_client_phdc_transport_disconnect_indication callback, void *user_data)
297 RET_IF(NULL == callback);
299 phdc_signal_handler.phdc_transport_disconnect_indication_cb = callback;
300 phdc_signal_handler.phdc_transport_disconnect_indication_data = user_data;
303 API void net_nfc_client_phdc_set_data_received(
304 net_nfc_client_phdc_data_received callback, void *user_data)
306 RET_IF(NULL == callback);
308 phdc_signal_handler.phdc_data_received_cb = callback;
309 phdc_signal_handler.phdc_data_received_data = user_data;
312 API void net_nfc_client_phdc_unset_transport_connect_indication(void)
314 phdc_signal_handler.phdc_transport_connect_indication_cb = NULL;
315 phdc_signal_handler.phdc_transport_connect_indication_data= NULL;
319 API void net_nfc_client_phdc_unset_transport_disconnect_indication(void)
321 phdc_signal_handler.phdc_transport_disconnect_indication_cb = NULL;
322 phdc_signal_handler.phdc_transport_disconnect_indication_data = NULL;
325 API void net_nfc_client_phdc_unset_data_received(void)
327 phdc_signal_handler.phdc_data_received_cb = NULL;
328 phdc_signal_handler.phdc_data_received_data = NULL;
331 net_nfc_error_e net_nfc_client_phdc_init(void)
333 GError *error = NULL;
337 NFC_WARN("Already initialized");
341 phdc_proxy = net_nfc_gdbus_phdc_proxy_new_for_bus_sync(
343 G_DBUS_PROXY_FLAGS_NONE,
344 "org.tizen.NetNfcService",
345 "/org/tizen/NetNfcService/Phdc",
348 if (NULL == phdc_proxy)
350 NFC_ERR("Can not create proxy : %s", error->message);
353 return NET_NFC_UNKNOWN_ERROR;
356 net_nfc_client_phdc_unset_transport_connect_indication();
357 net_nfc_client_phdc_unset_transport_disconnect_indication();
358 net_nfc_client_phdc_unset_data_received();
360 g_signal_connect(phdc_proxy, "phdc_connect",
361 G_CALLBACK(phdc_transport_connect_indication), NULL);
363 g_signal_connect(phdc_proxy, "phdc_disconnect",
364 G_CALLBACK(phdc_transport_disconnect_indication), NULL);
366 g_signal_connect(phdc_proxy, "phdc_received",
367 G_CALLBACK(phdc_device_data_received), NULL);
369 g_signal_connect(phdc_proxy, "phdc-event", G_CALLBACK(_phdc_event_cb), NULL);
375 void net_nfc_client_phdc_deinit(void)
379 g_object_unref(phdc_proxy);
383 net_nfc_client_phdc_unset_transport_connect_indication();
384 net_nfc_client_phdc_unset_transport_disconnect_indication();
385 net_nfc_client_phdc_unset_data_received();