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")))
31 #define DEACTIVATE_DELAY 500 /* ms */
32 #define ACTIVATE_DELAY 100 /* ms */
34 typedef struct _ManagerFuncData ManagerFuncData;
36 struct _ManagerFuncData {
39 net_nfc_error_e result;
42 static NetNfcGDbusManager *manager_proxy = NULL;
43 static NetNfcGDbusManager *auto_start_proxy = NULL;
44 static ManagerFuncData activated_func_data;
45 static int is_activated = -1;
46 static guint timeout_id[2];
48 static void manager_call_set_active_callback(GObject *source_object,
52 static void manager_call_get_server_state_callback(GObject *source_object,
57 static void manager_activated(NetNfcGDbusManager *manager,
62 static gboolean _set_activate_time_elapsed_callback(gpointer user_data)
64 ManagerFuncData *func_data = (ManagerFuncData *)user_data;
65 net_nfc_client_manager_set_active_completed callback;
67 if (timeout_id[0] > 0) {
68 g_assert(func_data != NULL);
70 g_source_remove(timeout_id[0]);
73 callback = (net_nfc_client_manager_set_active_completed)func_data->callback;
75 callback(func_data->result, func_data->user_data);
83 static void manager_call_set_active_callback(GObject *source_object,
87 ManagerFuncData *func_data = (ManagerFuncData *)user_data;
88 net_nfc_error_e result;
91 g_assert(user_data != NULL);
93 if (net_nfc_gdbus_manager_call_set_active_finish(
94 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),
137 DEBUG_ERR_MSG("Can not finish get_server_state: %s",
139 result = NET_NFC_IPC_FAIL;
144 if (func_data->callback != NULL) {
145 net_nfc_client_manager_get_server_state_completed callback =
146 (net_nfc_client_manager_get_server_state_completed)func_data->callback;
148 callback(result, out_state, func_data->user_data);
154 static gboolean _activated_time_elapsed_callback(gpointer user_data)
156 net_nfc_client_manager_activated callback =
157 (net_nfc_client_manager_activated)activated_func_data.callback;
159 if (timeout_id[1] > 0) {
160 g_source_remove(timeout_id[1]);
163 callback(is_activated, activated_func_data.user_data);
169 static void manager_activated(NetNfcGDbusManager *manager,
173 INFO_MSG(">>> SIGNAL arrived");
174 DEBUG_CLIENT_MSG("activated %d", activated);
176 /* update current state */
177 is_activated = (int)activated;
179 if (activated_func_data.callback != NULL) {
180 if (is_activated == false) {
181 /* FIXME : wait several times */
182 timeout_id[1] = g_timeout_add(DEACTIVATE_DELAY,
183 _activated_time_elapsed_callback,
186 timeout_id[1] = g_timeout_add(ACTIVATE_DELAY,
187 _activated_time_elapsed_callback,
194 void net_nfc_client_manager_set_activated(
195 net_nfc_client_manager_activated callback,
198 if (callback == NULL)
201 if (manager_proxy == NULL) {
202 if (net_nfc_client_manager_init() != NET_NFC_OK) {
203 DEBUG_ERR_MSG("manager_proxy fail");
204 /* FIXME : return result of this error */
209 activated_func_data.callback = callback;
210 activated_func_data.user_data = user_data;
214 void net_nfc_client_manager_unset_activated(void)
216 activated_func_data.callback = NULL;
217 activated_func_data.user_data = NULL;
221 net_nfc_error_e net_nfc_client_manager_set_active(int state,
222 net_nfc_client_manager_set_active_completed callback,
225 gboolean active = FALSE;
226 ManagerFuncData *func_data;
228 if (auto_start_proxy == NULL) {
229 GError *error = NULL;
231 auto_start_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
233 G_DBUS_PROXY_FLAGS_NONE,
234 "org.tizen.NetNfcService",
235 "/org/tizen/NetNfcService/Manager",
238 if (auto_start_proxy == NULL) {
239 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
242 return NET_NFC_UNKNOWN_ERROR;
246 /* allow this function even nfc is off */
248 func_data = g_try_new0(ManagerFuncData, 1);
249 if (func_data == NULL)
250 return NET_NFC_ALLOC_FAIL;
252 func_data->callback = (gpointer)callback;
253 func_data->user_data = user_data;
258 net_nfc_gdbus_manager_call_set_active(auto_start_proxy,
261 manager_call_set_active_callback,
268 net_nfc_error_e net_nfc_client_manager_set_active_sync(int state)
270 net_nfc_error_e out_result = NET_NFC_OK;
271 GError *error = NULL;
273 if (auto_start_proxy == NULL) {
274 GError *error = NULL;
276 auto_start_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
278 G_DBUS_PROXY_FLAGS_NONE,
279 "org.tizen.NetNfcService",
280 "/org/tizen/NetNfcService/Manager",
283 if (auto_start_proxy == NULL) {
284 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
287 return NET_NFC_UNKNOWN_ERROR;
291 /* allow this function even nfc is off */
293 if (net_nfc_gdbus_manager_call_set_active_sync(auto_start_proxy,
298 DEBUG_ERR_MSG("can not call SetActive: %s",
300 out_result = NET_NFC_IPC_FAIL;
309 net_nfc_error_e net_nfc_client_manager_get_server_state(
310 net_nfc_client_manager_get_server_state_completed callback,
313 NetNfcCallback *func_data;
315 if (manager_proxy == NULL)
316 return NET_NFC_NOT_INITIALIZED;
318 /* prevent executing daemon when nfc is off */
319 if (net_nfc_client_manager_is_activated() == false)
320 return NET_NFC_NOT_ACTIVATED;
322 func_data = g_try_new0(NetNfcCallback, 1);
323 if (func_data == NULL)
324 return NET_NFC_ALLOC_FAIL;
326 func_data->callback = (gpointer) callback;
327 func_data->user_data = user_data;
329 net_nfc_gdbus_manager_call_get_server_state(manager_proxy,
331 manager_call_get_server_state_callback,
338 net_nfc_error_e net_nfc_client_manager_get_server_state_sync(
341 net_nfc_error_e out_result = NET_NFC_OK;
343 GError *error = NULL;
346 return NET_NFC_NULL_PARAMETER;
350 if (manager_proxy == NULL)
351 return NET_NFC_NOT_INITIALIZED;
353 /* prevent executing daemon when nfc is off */
354 if (net_nfc_client_manager_is_activated() == false)
355 return NET_NFC_NOT_ACTIVATED;
357 if (net_nfc_gdbus_manager_call_get_server_state_sync(manager_proxy,
364 DEBUG_ERR_MSG("can not call GetServerState: %s",
366 out_result = NET_NFC_IPC_FAIL;
375 net_nfc_error_e net_nfc_client_manager_init(void)
377 GError *error = NULL;
380 DEBUG_CLIENT_MSG("Already initialized");
385 manager_proxy = net_nfc_gdbus_manager_proxy_new_for_bus_sync(
387 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
388 "org.tizen.NetNfcService",
389 "/org/tizen/NetNfcService/Manager",
392 if (manager_proxy == NULL) {
393 DEBUG_ERR_MSG("Can not create proxy : %s", error->message);
396 return NET_NFC_UNKNOWN_ERROR;
399 g_signal_connect(manager_proxy, "activated",
400 G_CALLBACK(manager_activated), NULL);
405 void net_nfc_client_manager_deinit(void)
410 for (i = 0; i < 2; i++) {
411 if (timeout_id[i] > 0) {
412 g_source_remove(timeout_id[i]);
417 g_object_unref(manager_proxy);
418 manager_proxy = NULL;
421 if (auto_start_proxy) {
422 g_object_unref(auto_start_proxy);
423 auto_start_proxy = NULL;
427 /* internal function */
428 bool net_nfc_client_manager_is_activated()
430 if (is_activated < 0)
431 net_nfc_client_get_nfc_state(&is_activated);