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_vconf.h"
26 #include "net_nfc_server_manager.h"
27 #include "net_nfc_server_util.h"
28 #include "net_nfc_server_controller.h"
29 #include "net_nfc_server_tag.h"
30 #include "net_nfc_server_ndef.h"
31 #include "net_nfc_server_llcp.h"
32 #include "net_nfc_server_p2p.h"
33 #include "net_nfc_server_transceive.h"
34 #include "net_nfc_server_test.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,
71 g_variant_get(_ret, "(u)", &pid);
72 g_variant_unref(_ret);
78 static void _name_owner_changed(GDBusProxy *proxy,
79 const gchar *name, const gchar *old_owner,
80 const gchar *new_owner, void *user_data)
82 if (name == NULL || old_owner == NULL || new_owner == NULL) {
83 DEBUG_ERR_MSG("invalid parameter");
88 if (strlen(new_owner) == 0) {
89 if (net_nfc_server_gdbus_check_client_is_running(old_owner)) {
90 /* unregister service */
91 net_nfc_server_llcp_unregister_services(old_owner);
93 /* remove client context */
94 net_nfc_server_gdbus_cleanup_client_context(old_owner);
99 static void _on_name_owner_changed(GDBusConnection *connection,
100 const gchar *sender_name, const gchar *object_path,
101 const gchar *interface_name, const gchar *signal_name,
102 GVariant *parameters, gpointer user_data)
108 g_variant_get(parameters,
114 _name_owner_changed((GDBusProxy *)connection,
115 name, old_owner, new_owner, user_data);
118 static void _subscribe_name_owner_changed_event()
120 if (connection == NULL)
123 /* subscribe signal */
124 subscribe_id = g_dbus_connection_signal_subscribe(connection,
125 "org.freedesktop.DBus", /* bus name */
126 "org.freedesktop.DBus", /* interface */
127 "NameOwnerChanged", /* member */
128 "/org/freedesktop/DBus", /* path */
130 G_DBUS_SIGNAL_FLAGS_NONE,
131 _on_name_owner_changed,
135 static void _unsubscribe_name_owner_changed_event()
137 if (connection == NULL)
140 /* subscribe signal */
141 if (subscribe_id > 0) {
142 g_dbus_connection_signal_unsubscribe(connection, subscribe_id);
146 static void net_nfc_server_gdbus_init(void)
148 GError *error = NULL;
151 g_object_unref(connection);
153 connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
154 if (connection == NULL)
156 DEBUG_ERR_MSG("Can not get connection %s", error->message);
157 g_error_free (error);
161 net_nfc_server_gdbus_init_client_context();
163 if (net_nfc_server_manager_init(connection) == FALSE)
165 DEBUG_ERR_MSG("Can not init manager");
169 if (net_nfc_server_tag_init(connection) == FALSE)
171 DEBUG_ERR_MSG("Can not init tag");
175 if (net_nfc_server_ndef_init(connection) == FALSE)
177 DEBUG_ERR_MSG("Can not init ndef");
181 if (net_nfc_server_llcp_init(connection) == FALSE)
183 DEBUG_ERR_MSG("Can not init llcp");
187 if (net_nfc_server_p2p_init(connection) == FALSE)
189 DEBUG_ERR_MSG("Can not init tag");
193 if (net_nfc_server_transceive_init(connection) == FALSE)
195 DEBUG_ERR_MSG("Can not initialize transceive");
199 if (net_nfc_server_test_init(connection) == FALSE)
201 DEBUG_ERR_MSG("Can not init Test");
205 if (net_nfc_server_handover_init(connection) == FALSE)
207 DEBUG_ERR_MSG("Can not initialize transceive");
211 if (net_nfc_server_se_init(connection) == FALSE)
213 DEBUG_ERR_MSG("Can not init Test");
217 if (net_nfc_server_snep_init(connection) == FALSE)
219 DEBUG_ERR_MSG("Can not init controller thread");
223 if (net_nfc_server_system_handler_init(connection) == FALSE)
225 DEBUG_ERR_MSG("Can not init controller thread");
229 if (net_nfc_server_controller_thread_init() == FALSE)
231 DEBUG_ERR_MSG("Can not init controller thread");
235 _subscribe_name_owner_changed_event();
238 static void net_nfc_server_gdbus_deinit(void)
240 _unsubscribe_name_owner_changed_event();
242 net_nfc_server_manager_deinit();
243 net_nfc_server_tag_deinit();
244 net_nfc_server_ndef_deinit();
245 net_nfc_server_llcp_deinit();
246 net_nfc_server_transceive_deinit();
247 net_nfc_server_test_deinit();
248 net_nfc_server_handover_deinit();
249 net_nfc_server_se_deinit();
250 net_nfc_server_snep_deinit();
251 net_nfc_server_system_handler_deinit();
253 net_nfc_server_gdbus_deinit_client_context();
255 net_nfc_server_controller_thread_deinit();
259 g_object_unref(connection);
264 void net_nfc_manager_quit()
266 DEBUG_MSG("net_nfc_manager_quit kill the nfc-manager daemon!!");
269 g_main_loop_quit(loop);
272 static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
277 DEBUG_MSG("bus path : %s", path);
279 net_nfc_server_gdbus_init();
281 net_nfc_server_controller_init();
283 if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
285 DEBUG_MSG("VCONFKEY_NFC_STATE is not exist");
286 net_nfc_manager_quit();
291 net_nfc_server_vconf_init();
294 net_nfc_server_manager_set_active(TRUE);
295 #ifndef ESE_ALWAYS_ON
296 else if (use_daemon == TRUE)
297 net_nfc_server_controller_deinit();
301 static void on_name_acquired(GDBusConnection *connection, const gchar *name,
304 DEBUG_SERVER_MSG("name : %s", name);
307 static void on_name_lost(GDBusConnection *connnection, const gchar *name,
310 DEBUG_SERVER_MSG("name : %s", name);
312 net_nfc_manager_quit();
316 int main(int argc, char *argv[])
321 gboolean use_daemon = FALSE;
322 GOptionContext *option_context;
323 GError *error = NULL;
325 option_context = g_option_context_new("Nfc manager");
326 g_option_context_add_main_entries(option_context, option_entries, NULL);
328 if (g_option_context_parse(option_context, &argc, &argv, &error) == FALSE)
330 DEBUG_ERR_MSG("can not parse option: %s", error->message);
333 g_option_context_free(option_context);
337 DEBUG_SERVER_MSG("start nfc manager");
338 DEBUG_SERVER_MSG("use_daemon : %d", use_daemon);
340 net_nfc_manager_init_log();
342 net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
344 handle = net_nfc_controller_onload();
347 DEBUG_ERR_MSG("load plugin library is failed");
349 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_OFF) != 0)
350 DEBUG_ERR_MSG("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
352 if (vconf_set_bool(VCONFKEY_NFC_STATE, 0) != 0)
353 DEBUG_ERR_MSG("VCONFKEY_NFC_STATE set to %d failed", 0);
358 if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_ON) != 0)
359 DEBUG_ERR_MSG("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
361 id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
362 "org.tizen.NetNfcService",
363 G_BUS_NAME_OWNER_FLAGS_NONE,
370 loop = g_main_loop_new(NULL, FALSE);
371 g_main_loop_run(loop);
374 net_nfc_server_vconf_deinit();
375 net_nfc_server_controller_deinit();
376 net_nfc_server_gdbus_deinit();
379 g_bus_unown_name(id);
381 net_nfc_controller_unload(handle);
383 net_nfc_manager_fini_log();
385 g_option_context_free(option_context);