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.
20 #include "net_nfc_debug_internal.h"
21 #include "net_nfc_util_internal.h"
22 #include "net_nfc_gdbus.h"
23 #include "net_nfc_server.h"
24 #include "net_nfc_server_common.h"
25 #include "net_nfc_server_phdc.h"
26 #include "net_nfc_server_vconf.h"
27 #include "net_nfc_server_manager.h"
28 #include "net_nfc_server_util.h"
29 #include "net_nfc_server_controller.h"
30 #include "net_nfc_server_tag.h"
31 #include "net_nfc_server_ndef.h"
32 #include "net_nfc_server_llcp.h"
33 #include "net_nfc_server_p2p.h"
34 #include "net_nfc_server_transceive.h"
35 #include "net_nfc_server_handover.h"
36 #include "net_nfc_server_se.h"
37 #include "net_nfc_server_snep.h"
38 #include "net_nfc_server_system_handler.h"
39 #include "net_nfc_server_context.h"
41 static gboolean use_daemon = FALSE;
42 static GMainLoop *loop = NULL;
44 static GDBusConnection *connection = NULL;
45 static guint subscribe_id;
47 GOptionEntry option_entries[] = {
48 { "daemon", 'd', 0, G_OPTION_ARG_NONE, &use_daemon,
49 "Use Daemon mode", NULL },
53 pid_t net_nfc_server_gdbus_get_pid(const char *name)
59 _ret = g_dbus_connection_call_sync(connection,
60 "org.freedesktop.DBus",
61 "/org/freedesktop/DBus",
62 "org.freedesktop.DBus",
63 "GetConnectionUnixProcessID",
64 g_variant_new("(s)", name),
66 G_DBUS_CALL_FLAGS_NONE,
73 g_variant_get(_ret, "(u)", &pid);
74 g_variant_unref(_ret);
80 static void _name_owner_changed(GDBusProxy *proxy,
81 const gchar *name, const gchar *old_owner,
82 const gchar *new_owner, void *user_data)
85 RET_IF(NULL == old_owner);
86 RET_IF(NULL == new_owner);
88 if (strlen(new_owner) == 0)
90 if (net_nfc_server_gdbus_check_client_is_running(old_owner))
92 /* unregister service */
93 net_nfc_server_llcp_unregister_services(old_owner);
95 /* remove client context */
96 net_nfc_server_gdbus_cleanup_client_context(old_owner);
101 static void _on_name_owner_changed(GDBusConnection *connection,
102 const gchar *sender_name,
103 const gchar *object_path,
104 const gchar *interface_name,
105 const gchar *signal_name,
106 GVariant *parameters,
113 g_variant_get(parameters, "(sss)", &name, &old_owner, &new_owner);
115 _name_owner_changed((GDBusProxy *)connection, name, old_owner, new_owner, user_data);
118 static void _subscribe_name_owner_changed_event()
120 RET_IF(NULL == connection);
122 /* subscribe signal */
123 subscribe_id = g_dbus_connection_signal_subscribe(connection,
124 "org.freedesktop.DBus", /* bus name */
125 "org.freedesktop.DBus", /* interface */
126 "NameOwnerChanged", /* member */
127 "/org/freedesktop/DBus", /* path */
129 G_DBUS_SIGNAL_FLAGS_NONE,
130 _on_name_owner_changed,
134 static void _unsubscribe_name_owner_changed_event()
136 RET_IF(NULL == connection);
138 /* subscribe signal */
139 if (subscribe_id > 0)
140 g_dbus_connection_signal_unsubscribe(connection, subscribe_id);
143 static void net_nfc_server_gdbus_init(void)
145 GError *error = NULL;
148 g_object_unref(connection);
150 connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
151 if (NULL == connection)
153 NFC_ERR("Can not get connection %s", error->message);
154 g_error_free (error);
158 net_nfc_server_gdbus_init_client_context();
160 if (net_nfc_server_manager_init(connection) == FALSE)
162 NFC_ERR("Can not init manager");
166 if (net_nfc_server_tag_init(connection) == FALSE)
168 NFC_ERR("Can not init tag");
172 if (net_nfc_server_ndef_init(connection) == FALSE)
174 NFC_ERR("Can not init ndef");
178 if (net_nfc_server_llcp_init(connection) == FALSE)
180 NFC_ERR("Can not init llcp");
184 if (net_nfc_server_p2p_init(connection) == FALSE)
186 NFC_ERR("Can not init p2p");
190 if (net_nfc_server_transceive_init(connection) == FALSE)
192 NFC_ERR("Can not initialize transceive");
196 if (net_nfc_server_handover_init(connection) == FALSE)
198 NFC_ERR("Can not initialize handover");
202 if (net_nfc_server_se_init(connection) == FALSE)
204 NFC_ERR("Can not init se");
208 if (net_nfc_server_snep_init(connection) == FALSE)
210 NFC_ERR("Can not init snep");
214 if (net_nfc_server_system_handler_init(connection) == FALSE)
216 NFC_ERR("Can not init system handler");
220 if (net_nfc_server_phdc_init(connection) == FALSE)
222 NFC_ERR("Can not init phdc");
226 if (net_nfc_server_controller_thread_init() == FALSE)
228 NFC_ERR("Can not init controller thread");
232 _subscribe_name_owner_changed_event();
235 static void net_nfc_server_gdbus_deinit(void)
237 _unsubscribe_name_owner_changed_event();
239 net_nfc_server_manager_deinit();
240 net_nfc_server_tag_deinit();
241 net_nfc_server_ndef_deinit();
242 net_nfc_server_llcp_deinit();
243 net_nfc_server_transceive_deinit();
244 net_nfc_server_handover_deinit();
245 net_nfc_server_se_deinit();
246 net_nfc_server_snep_deinit();
247 net_nfc_server_system_handler_deinit();
248 net_nfc_server_phdc_deinit();
249 net_nfc_server_gdbus_deinit_client_context();
251 net_nfc_server_controller_thread_deinit();
255 g_object_unref(connection);
260 void net_nfc_manager_quit()
262 NFC_DBG("net_nfc_manager_quit kill the nfc-manager daemon!!");
265 g_main_loop_quit(loop);
268 static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
273 NFC_DBG("bus path : %s", path);
275 net_nfc_server_gdbus_init();
277 net_nfc_server_controller_init();
279 if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
281 NFC_DBG("VCONFKEY_NFC_STATE is not exist");
282 net_nfc_manager_quit();
287 net_nfc_server_vconf_init();
290 net_nfc_server_manager_set_active(TRUE);
291 #ifndef ESE_ALWAYS_ON
292 else if (use_daemon == TRUE)
293 net_nfc_server_controller_deinit();
297 static void on_name_acquired(GDBusConnection *connection, const gchar *name,
300 NFC_INFO("name : %s", name);
303 static void on_name_lost(GDBusConnection *connnection, const gchar *name,
306 NFC_INFO("name : %s", name);
308 net_nfc_manager_quit();
312 int main(int argc, char *argv[])
316 GError *error = NULL;
317 gboolean use_daemon = FALSE;
318 GOptionContext *option_context;
320 net_nfc_change_log_tag();
322 option_context = g_option_context_new("Nfc manager");
323 g_option_context_add_main_entries(option_context, option_entries, NULL);
325 if (g_option_context_parse(option_context, &argc, &argv, &error) == FALSE)
327 NFC_ERR("can not parse option: %s", error->message);
330 g_option_context_free(option_context);
334 NFC_DBG("start nfc manager");
335 NFC_INFO("use_daemon : %d", use_daemon);
337 net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
339 handle = net_nfc_controller_onload();
342 NFC_ERR("load plugin library is failed");
344 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_OFF) != 0)
345 NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
347 if (vconf_set_bool(VCONFKEY_NFC_STATE, 0) != 0)
348 NFC_ERR("VCONFKEY_NFC_STATE set to %d failed", 0);
353 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_ON) != 0)
354 NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
356 id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
357 "org.tizen.NetNfcService",
358 G_BUS_NAME_OWNER_FLAGS_NONE,
365 loop = g_main_loop_new(NULL, FALSE);
366 g_main_loop_run(loop);
369 net_nfc_server_vconf_deinit();
370 net_nfc_server_controller_deinit();
371 net_nfc_server_gdbus_deinit();
374 g_bus_unown_name(id);
376 net_nfc_controller_unload(handle);
378 g_option_context_free(option_context);