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
37 near_bool_t constant_poll;
39 .constant_poll = FALSE,
42 static GKeyFile *load_config(const char *file)
47 keyfile = g_key_file_new();
49 g_key_file_set_list_separator(keyfile, ',');
51 if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
52 if (err->code != G_FILE_ERROR_NOENT) {
53 near_error("Parsing %s failed: %s", file,
58 g_key_file_free(keyfile);
65 static void parse_config(GKeyFile *config)
73 DBG("parsing main.conf");
75 boolean = g_key_file_get_boolean(config, "General",
76 "ConstantPoll", &error);
78 near_settings.constant_poll = boolean;
80 g_clear_error(&error);
83 static GMainLoop *main_loop = NULL;
85 static volatile sig_atomic_t __terminated = 0;
87 static void sig_term(int sig)
94 near_info("Terminating");
96 g_main_loop_quit(main_loop);
99 static void disconnect_callback(DBusConnection *conn, void *user_data)
101 near_error("D-Bus disconnect");
103 g_main_loop_quit(main_loop);
106 static gchar *option_debug = NULL;
107 static gchar *option_plugin = NULL;
108 static gchar *option_noplugin = NULL;
109 static gboolean option_detach = TRUE;
110 static gboolean option_version = FALSE;
112 static gboolean parse_debug(const char *key, const char *value,
113 gpointer user_data, GError **error)
116 option_debug = g_strdup(value);
118 option_debug = g_strdup("*");
123 static GOptionEntry options[] = {
124 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
125 G_OPTION_ARG_CALLBACK, parse_debug,
126 "Specify debug options to enable", "DEBUG" },
127 { "nodaemon", 'n', G_OPTION_FLAG_REVERSE,
128 G_OPTION_ARG_NONE, &option_detach,
129 "Don't fork daemon to background" },
130 { "plugin", 'p', 0, G_OPTION_ARG_STRING, &option_plugin,
131 "Specify plugins to load", "NAME,..." },
132 { "noplugin", 'P', 0, G_OPTION_ARG_STRING, &option_noplugin,
133 "Specify plugins not to load", "NAME,..." },
134 { "version", 'v', 0, G_OPTION_ARG_NONE, &option_version,
135 "Show version information and exit" },
139 near_bool_t near_setting_get_bool(const char *key)
141 if (g_str_equal(key, "ConstantPoll") == TRUE)
142 return near_settings.constant_poll;
147 int main(int argc, char *argv[])
149 GOptionContext *context;
150 GError *error = NULL;
151 DBusConnection *conn;
156 context = g_option_context_new(NULL);
157 g_option_context_add_main_entries(context, options, NULL);
159 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
161 g_printerr("%s\n", error->message);
164 g_printerr("An unknown error occurred\n");
168 g_option_context_free(context);
170 if (option_version == TRUE) {
171 printf("%s\n", VERSION);
175 if (option_detach == TRUE) {
177 perror("Can't start daemon");
182 main_loop = g_main_loop_new(NULL, FALSE);
184 dbus_error_init(&err);
186 conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NFC_SERVICE, &err);
188 if (dbus_error_is_set(&err) == TRUE) {
189 fprintf(stderr, "%s\n", err.message);
190 dbus_error_free(&err);
192 fprintf(stderr, "Can't register with system bus\n");
196 g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
198 __near_log_init(option_debug, option_detach);
199 __near_dbus_init(conn);
201 config = load_config(CONFIGDIR "/main.conf");
203 parse_config(config);
205 __near_netlink_init();
207 __near_device_init();
208 __near_adapter_init();
210 __near_manager_init(conn);
211 __near_bluetooth_init();
214 __near_plugin_init(option_plugin, option_noplugin);
216 memset(&sa, 0, sizeof(sa));
217 sa.sa_handler = sig_term;
218 sigaction(SIGINT, &sa, NULL);
219 sigaction(SIGTERM, &sa, NULL);
221 g_main_loop_run(main_loop);
223 __near_plugin_cleanup();
225 __near_agent_cleanup();
226 __near_bluetooth_cleanup();
227 __near_manager_cleanup();
228 __near_ndef_cleanup();
229 __near_adapter_cleanup();
230 __near_device_cleanup();
231 __near_tag_cleanup();
232 __near_netlink_cleanup();
234 __near_dbus_cleanup();
235 __near_log_cleanup();
237 dbus_connection_unref(conn);
239 g_main_loop_unref(main_loop);
242 g_key_file_free(config);