27019649a19563981dd9569fb3b78c50ae7c3dd6
[platform/upstream/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 GMainLoop *main_loop = NULL;
37
38 static volatile sig_atomic_t __terminated = 0;
39
40 static void sig_term(int sig)
41 {
42         if (__terminated > 0)
43                 return;
44
45         __terminated = 1;
46
47         near_info("Terminating");
48
49         g_main_loop_quit(main_loop);
50 }
51
52 static void disconnect_callback(DBusConnection *conn, void *user_data)
53 {
54         near_error("D-Bus disconnect");
55
56         g_main_loop_quit(main_loop);
57 }
58
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;
64
65 static gboolean parse_debug(const char *key, const char *value,
66                                         gpointer user_data, GError **error)
67 {
68         if (value)
69                 option_debug = g_strdup(value);
70         else
71                 option_debug = g_strdup("*");
72
73         return TRUE;
74 }
75
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" },
89         { NULL },
90 };
91
92 int main(int argc, char *argv[])
93 {
94         GOptionContext *context;
95         GError *error = NULL;
96         DBusConnection *conn;
97         DBusError err;
98         struct sigaction sa;
99
100         context = g_option_context_new(NULL);
101         g_option_context_add_main_entries(context, options, NULL);
102
103         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
104                 if (error != NULL) {
105                         g_printerr("%s\n", error->message);
106                         g_error_free(error);
107                 } else
108                         g_printerr("An unknown error occurred\n");
109                 exit(1);
110         }
111
112         g_option_context_free(context);
113
114         if (option_version == TRUE) {
115                 printf("%s\n", VERSION);
116                 exit(0);
117         }
118
119         if (option_detach == TRUE) {
120                 if (daemon(0, 0)) {
121                         perror("Can't start daemon");
122                         exit(1);
123                 }
124         }
125
126         main_loop = g_main_loop_new(NULL, FALSE);
127
128         dbus_error_init(&err);
129
130         conn = g_dbus_setup_bus(DBUS_BUS_SESSION, NFC_SERVICE, &err);
131         if (conn == NULL) {
132                 if (dbus_error_is_set(&err) == TRUE) {
133                         fprintf(stderr, "%s\n", err.message);
134                         dbus_error_free(&err);
135                 } else
136                         fprintf(stderr, "Can't register with session bus\n");
137                 exit(1);
138         }
139
140         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
141
142         __near_log_init(option_debug, option_detach);
143         __near_dbus_init(conn);
144
145         __near_netlink_init();
146         __near_target_init();
147         __near_adapter_init();
148         __near_manager_init(conn);
149
150         __near_plugin_init(option_plugin, option_noplugin);
151
152         memset(&sa, 0, sizeof(sa));
153         sa.sa_handler = sig_term;
154         sigaction(SIGINT, &sa, NULL);
155         sigaction(SIGTERM, &sa, NULL);
156
157         g_main_loop_run(main_loop);
158
159         __near_plugin_cleanup();
160
161         __near_manager_cleanup();
162         __near_adapter_cleanup();
163         __near_target_cleanup();
164         __near_netlink_cleanup();
165
166         __near_dbus_cleanup();
167         __near_log_cleanup();
168
169         dbus_connection_unref(conn);
170
171         g_main_loop_unref(main_loop);
172
173         return 0;
174 }