Tizen 2.0 Release
[framework/connectivity/neard.git] / src / main.c
1 /*
2  *
3  *  neard - Near Field Communication manager
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <signal.h>
31
32 #include <gdbus.h>
33
34 #include "near.h"
35
36 static struct {
37         near_bool_t constant_poll;
38 } near_settings  = {
39         .constant_poll = FALSE,
40 };
41
42 static GKeyFile *load_config(const char *file)
43 {
44         GError *err = NULL;
45         GKeyFile *keyfile;
46
47         keyfile = g_key_file_new();
48
49         g_key_file_set_list_separator(keyfile, ',');
50
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,
54                                                                 err->message);
55                 }
56
57                 g_error_free(err);
58                 g_key_file_free(keyfile);
59                 return NULL;
60         }
61
62         return keyfile;
63 }
64
65 static void parse_config(GKeyFile *config)
66 {
67         GError *error = NULL;
68         gboolean boolean;
69
70         if (config == NULL)
71                 return;
72
73         DBG("parsing main.conf");
74
75         boolean = g_key_file_get_boolean(config, "General",
76                                                 "ConstantPoll", &error);
77         if (error == NULL)
78                 near_settings.constant_poll = boolean;
79
80         g_clear_error(&error);
81 }
82
83 static GMainLoop *main_loop = NULL;
84
85 static volatile sig_atomic_t __terminated = 0;
86
87 static void sig_term(int sig)
88 {
89         if (__terminated > 0)
90                 return;
91
92         __terminated = 1;
93
94         near_info("Terminating");
95
96         g_main_loop_quit(main_loop);
97 }
98
99 static void disconnect_callback(DBusConnection *conn, void *user_data)
100 {
101         near_error("D-Bus disconnect");
102
103         g_main_loop_quit(main_loop);
104 }
105
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;
111
112 static gboolean parse_debug(const char *key, const char *value,
113                                         gpointer user_data, GError **error)
114 {
115         if (value)
116                 option_debug = g_strdup(value);
117         else
118                 option_debug = g_strdup("*");
119
120         return TRUE;
121 }
122
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" },
136         { NULL },
137 };
138
139 near_bool_t near_setting_get_bool(const char *key)
140 {
141         if (g_str_equal(key, "ConstantPoll") == TRUE)
142                 return near_settings.constant_poll;
143
144         return FALSE;
145 }
146
147 int main(int argc, char *argv[])
148 {
149         GOptionContext *context;
150         GError *error = NULL;
151         DBusConnection *conn;
152         DBusError err;
153         GKeyFile *config;
154         struct sigaction sa;
155
156         context = g_option_context_new(NULL);
157         g_option_context_add_main_entries(context, options, NULL);
158
159         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
160                 if (error != NULL) {
161                         g_printerr("%s\n", error->message);
162                         g_error_free(error);
163                 } else
164                         g_printerr("An unknown error occurred\n");
165                 exit(1);
166         }
167
168         g_option_context_free(context);
169
170         if (option_version == TRUE) {
171                 printf("%s\n", VERSION);
172                 exit(0);
173         }
174
175         if (option_detach == TRUE) {
176                 if (daemon(0, 0)) {
177                         perror("Can't start daemon");
178                         exit(1);
179                 }
180         }
181
182         main_loop = g_main_loop_new(NULL, FALSE);
183
184         dbus_error_init(&err);
185
186         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NFC_SERVICE, &err);
187         if (conn == NULL) {
188                 if (dbus_error_is_set(&err) == TRUE) {
189                         fprintf(stderr, "%s\n", err.message);
190                         dbus_error_free(&err);
191                 } else
192                         fprintf(stderr, "Can't register with system bus\n");
193                 exit(1);
194         }
195
196         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
197
198         __near_log_init(option_debug, option_detach);
199         __near_dbus_init(conn);
200
201         config = load_config(CONFIGDIR "/main.conf");
202
203         parse_config(config);
204
205         __near_netlink_init();
206         __near_tag_init();
207         __near_device_init();
208         __near_adapter_init();
209         __near_ndef_init();
210         __near_manager_init(conn);
211         __near_bluetooth_init();
212         __near_agent_init();
213
214         __near_plugin_init(option_plugin, option_noplugin);
215
216         memset(&sa, 0, sizeof(sa));
217         sa.sa_handler = sig_term;
218         sigaction(SIGINT, &sa, NULL);
219         sigaction(SIGTERM, &sa, NULL);
220
221         g_main_loop_run(main_loop);
222
223         __near_plugin_cleanup();
224
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();
233
234         __near_dbus_cleanup();
235         __near_log_cleanup();
236
237         dbus_connection_unref(conn);
238
239         g_main_loop_unref(main_loop);
240
241         if (config)
242                 g_key_file_free(config);
243
244         return 0;
245 }