3 * neard - Near Field Communication manager
5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 static GMainLoop *main_loop = NULL;
38 static volatile sig_atomic_t __terminated = 0;
40 static void sig_term(int sig)
47 near_info("Terminating");
49 g_main_loop_quit(main_loop);
52 static void disconnect_callback(DBusConnection *conn, void *user_data)
54 near_error("D-Bus disconnect");
56 g_main_loop_quit(main_loop);
59 static gchar *option_debug = NULL;
60 static gchar *option_plugin = NULL;
61 static gchar *option_noplugin = NULL;
62 static gboolean option_detach = TRUE;
63 static gboolean option_version = FALSE;
65 static gboolean parse_debug(const char *key, const char *value,
66 gpointer user_data, GError **error)
69 option_debug = g_strdup(value);
71 option_debug = g_strdup("*");
76 static GOptionEntry options[] = {
77 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
78 G_OPTION_ARG_CALLBACK, parse_debug,
79 "Specify debug options to enable", "DEBUG" },
80 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
81 G_OPTION_ARG_NONE, &option_detach,
82 "Don't fork daemon to background" },
83 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
84 "Specify plugins to load", "NAME,..." },
85 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
86 "Specify plugins not to load", "NAME,..." },
87 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
88 "Show version information and exit" },
92 int main(int argc, char *argv[])
94 GOptionContext *context;
100 context = g_option_context_new(NULL);
101 g_option_context_add_main_entries(context, options, NULL);
103 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
105 g_printerr("%s\n", error->message);
108 g_printerr("An unknown error occurred\n");
112 g_option_context_free(context);
114 if (option_version == TRUE) {
115 printf("%s\n", VERSION);
119 if (option_detach == TRUE) {
121 perror("Can't start daemon");
126 main_loop = g_main_loop_new(NULL, FALSE);
128 dbus_error_init(&err);
130 conn = g_dbus_setup_bus(DBUS_BUS_SESSION, NFC_SERVICE, &err);
132 if (dbus_error_is_set(&err) == TRUE) {
133 fprintf(stderr, "%s\n", err.message);
134 dbus_error_free(&err);
136 fprintf(stderr, "Can't register with session bus\n");
140 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
142 __near_log_init(option_debug, option_detach);
143 __near_dbus_init(conn);
145 __near_netlink_init();
146 __near_target_init();
147 __near_adapter_init();
149 __near_manager_init(conn);
151 __near_plugin_init(option_plugin, option_noplugin);
153 memset(&sa, 0, sizeof(sa));
154 sa.sa_handler = sig_term;
155 sigaction(SIGINT, &sa, NULL);
156 sigaction(SIGTERM, &sa, NULL);
158 g_main_loop_run(main_loop);
160 __near_plugin_cleanup();
162 __near_manager_cleanup();
163 __near_ndef_cleanup();
164 __near_adapter_cleanup();
165 __near_target_cleanup();
166 __near_netlink_cleanup();
168 __near_dbus_cleanup();
169 __near_log_cleanup();
171 dbus_connection_unref(conn);
173 g_main_loop_unref(main_loop);