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_debug_internal.h"
19 #include "net_nfc_gdbus.h"
20 #include "net_nfc_client.h"
21 #include "net_nfc_client_util_internal.h"
22 #include "net_nfc_client_context.h"
23 #include "net_nfc_client_manager.h"
25 #ifndef NET_NFC_EXPORT_API
26 #define NET_NFC_EXPORT_API __attribute__((visibility("default")))
29 #define DEACTIVATE_DELAY 500 /* ms */
30 #define ACTIVATE_DELAY 100 /* ms */
32 typedef struct _ManagerFuncData ManagerFuncData;
34 struct _ManagerFuncData
38 net_nfc_error_e result;
41 static NetNfcGDbusManager *manager_proxy = NULL;
42 static NetNfcGDbusManager *auto_start_proxy = NULL;
43 static ManagerFuncData activated_func_data;
44 static int is_activated = -1;
45 static guint timeout_id[2];
47 static void manager_call_set_active_callback(GObject *source_object,
51 static void manager_call_get_server_state_callback(GObject *source_object,
56 static void manager_activated(NetNfcGDbusManager *manager,
61 static gboolean _set_activate_time_elapsed_callback(gpointer user_data)
63 ManagerFuncData *func_data = (ManagerFuncData *)user_data;
64 net_nfc_client_manager_set_active_completed callback;
66 if (timeout_id[0] > 0) {
67 g_assert(func_data != NULL);
69 g_source_remove(timeout_id[0]);
72 callback = (net_nfc_client_manager_set_active_completed)func_data->callback;
74 callback(func_data->result, func_data->user_data);
82 static void manager_call_set_active_callback(GObject *source_object,
86 ManagerFuncData *func_data = (ManagerFuncData *)user_data;
87 net_nfc_error_e result;
90 g_assert(user_data != NULL);
92 if (net_nfc_gdbus_manager_call_set_active_finish(
93 NET_NFC_GDBUS_MANAGER(source_object),
98 DEBUG_ERR_MSG("Can not finish call_set_active: %s",
100 result = NET_NFC_IPC_FAIL;
105 func_data->result = result;
106 net_nfc_client_get_nfc_state(&is_activated);
108 if (is_activated == false) {
109 /* FIXME : wait several times */
110 timeout_id[0] = g_timeout_add(DEACTIVATE_DELAY,
111 _set_activate_time_elapsed_callback,
114 timeout_id[0] = g_timeout_add(ACTIVATE_DELAY,
115 _set_activate_time_elapsed_callback,
120 static void manager_call_get_server_state_callback(GObject *source_object,
124 NetNfcCallback *func_data = (NetNfcCallback *)user_data;
125 net_nfc_error_e result;
127 GError *error = NULL;
129 g_assert(user_data != NULL);
131 if (net_nfc_gdbus_manager_call_get_server_state_finish(
132 NET_NFC_GDBUS_MANAGER(source_object),
138 DEBUG_ERR_MSG("Can not finish get_server_state: %s",
140 result = NET_NFC_IPC_FAIL;
145 if (func_data->callback != NULL)
147 net_nfc_client_manager_get_server_state_completed callback =
148 (net_nfc_client_manager_get_server_state_completed)func_data->callback;
150 callback(result, out_state, func_data->user_data);
156 static gboolean _activated_time_elapsed_callback(gpointer user_data)
158 net_nfc_client_manager_activated callback =
159 (net_nfc_client_manager_activated)activated_func_data.callback;
161 if (timeout_id[1] > 0) {
162 g_source_remove(timeout_id[1]);
165 callback(is_activated, activated_func_data.user_data);
171 static void manager_activated(NetNfcGDbusManager *manager,
175 INFO_MSG(">>> SIGNAL arrived");
176 DEBUG_CLIENT_MSG("activated %d", activated);
178 /* update current state */
179 is_activated = (int)activated;
181 if (activated_func_data.callback != NULL)
183 if (is_activated == false) {
184 /* FIXME : wait several times */
185 timeout_id[1] = g_timeout_add(DEACTIVATE_DELAY,
186 _activated_time_elapsed_callback,
189 timeout_id[1] = g_timeout_add(ACTIVATE_DELAY,
190 _activated_time_elapsed_callback,
197 void net_nfc_client_manager_set_activated(
198 net_nfc_client_manager_activated callback,
201 if (callback == NULL)
204 if (manager_proxy == NULL)
206 if (net_nfc_client_manager_init() != NET_NFC_OK)
208 DEBUG_ERR_MSG("manager_proxy fail");
209 /* FIXME : return result of this error */
214 activated_func_data.callback = callback;
215 activated_func_data.user_data = user_data;
219 void net_nfc_client_manager_unset_activated(void)
221 activated_func_data.callback = NULL;
222 activated_func_data.user_data = NULL;
226 net_nfc_error_e net_nfc_client_manager_set_active(int state,
227 net_nfc_client_manager_set_active_completed callback,
230 gboolean active = FALSE;
231 ManagerFuncData *func_data;
233 if (auto_start_proxy == NULL) {
234 GError *error = NULL;
236 auto_start_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
238 G_DBUS_PROXY_FLAGS_NONE,
239 "org.tizen.NetNfcService",
240 "/org/tizen/NetNfcService/Manager",
243 if (auto_start_proxy == NULL)
245 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
248 return NET_NFC_UNKNOWN_ERROR;
252 /* allow this function even nfc is off */
254 func_data = g_try_new0(ManagerFuncData, 1);
255 if (func_data == NULL)
256 return NET_NFC_ALLOC_FAIL;
258 func_data->callback = (gpointer)callback;
259 func_data->user_data = user_data;
264 net_nfc_gdbus_manager_call_set_active(auto_start_proxy,
267 manager_call_set_active_callback,
274 net_nfc_error_e net_nfc_client_manager_set_active_sync(int state)
276 net_nfc_error_e out_result = NET_NFC_OK;
277 GError *error = NULL;
279 if (auto_start_proxy == NULL) {
280 GError *error = NULL;
282 auto_start_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
284 G_DBUS_PROXY_FLAGS_NONE,
285 "org.tizen.NetNfcService",
286 "/org/tizen/NetNfcService/Manager",
289 if (auto_start_proxy == NULL)
291 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
294 return NET_NFC_UNKNOWN_ERROR;
298 /* allow this function even nfc is off */
300 if (net_nfc_gdbus_manager_call_set_active_sync(auto_start_proxy,
306 DEBUG_ERR_MSG("can not call SetActive: %s",
308 out_result = NET_NFC_IPC_FAIL;
317 net_nfc_error_e net_nfc_client_manager_get_server_state(
318 net_nfc_client_manager_get_server_state_completed callback,
321 NetNfcCallback *func_data;
323 if (manager_proxy == NULL)
324 return NET_NFC_NOT_INITIALIZED;
326 /* prevent executing daemon when nfc is off */
327 if (net_nfc_client_manager_is_activated() == false) {
328 return NET_NFC_NOT_ACTIVATED;
331 func_data = g_try_new0(NetNfcCallback, 1);
332 if (func_data == NULL)
333 return NET_NFC_ALLOC_FAIL;
335 func_data->callback = (gpointer) callback;
336 func_data->user_data = user_data;
338 net_nfc_gdbus_manager_call_get_server_state(manager_proxy,
340 manager_call_get_server_state_callback,
347 net_nfc_error_e net_nfc_client_manager_get_server_state_sync(
350 net_nfc_error_e out_result = NET_NFC_OK;
352 GError *error = NULL;
355 return NET_NFC_NULL_PARAMETER;
359 if (manager_proxy == NULL)
360 return NET_NFC_NOT_INITIALIZED;
362 /* prevent executing daemon when nfc is off */
363 if (net_nfc_client_manager_is_activated() == false) {
364 return NET_NFC_NOT_ACTIVATED;
367 if (net_nfc_gdbus_manager_call_get_server_state_sync(manager_proxy,
377 DEBUG_ERR_MSG("can not call GetServerState: %s",
379 out_result = NET_NFC_IPC_FAIL;
388 net_nfc_error_e net_nfc_client_manager_init(void)
390 GError *error = NULL;
394 DEBUG_CLIENT_MSG("Already initialized");
399 manager_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
401 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
402 "org.tizen.NetNfcService",
403 "/org/tizen/NetNfcService/Manager",
406 if (manager_proxy == NULL)
408 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
411 return NET_NFC_UNKNOWN_ERROR;
414 g_signal_connect(manager_proxy, "activated",
415 G_CALLBACK(manager_activated), NULL);
420 void net_nfc_client_manager_deinit(void)
426 for (i = 0; i < 2; i++) {
427 if (timeout_id[i] > 0) {
428 g_source_remove(timeout_id[i]);
433 g_object_unref(manager_proxy);
434 manager_proxy = NULL;
437 if (auto_start_proxy) {
438 g_object_unref(auto_start_proxy);
439 auto_start_proxy = NULL;
443 /* internal function */
444 bool net_nfc_client_manager_is_activated()
446 if (is_activated < 0) {
447 net_nfc_client_get_nfc_state(&is_activated);