upload tizen1.0 source
[profile/ivi/obexd.git] / client / main.c
1 /*
2  *
3  *  OBEX Client
4  *
5  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <syslog.h>
33
34 #include <glib.h>
35 #include <gdbus.h>
36
37 #include "log.h"
38 #include "manager.h"
39
40 static GMainLoop *event_loop = NULL;
41
42 static char *option_debug = NULL;
43 static gboolean option_stderr = FALSE;
44
45 static gboolean parse_debug(const char *key, const char *value,
46                                 gpointer user_data, GError **error)
47 {
48         if (value)
49                 option_debug = g_strdup(value);
50         else
51                 option_debug = g_strdup("*");
52
53         return TRUE;
54 }
55
56 static GOptionEntry options[] = {
57         { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
58                                 G_OPTION_ARG_CALLBACK, parse_debug,
59                                 "Enable debug information output", "DEBUG" },
60         { "stderr", 's', 0, G_OPTION_ARG_NONE, &option_stderr,
61                                 "Write log information to stderr" },
62         { NULL },
63 };
64
65 static void sig_term(int sig)
66 {
67         g_main_loop_quit(event_loop);
68 }
69
70 int main(int argc, char *argv[])
71 {
72         GOptionContext *context;
73         struct sigaction sa;
74         GError *gerr = NULL;
75
76         context = g_option_context_new(NULL);
77         g_option_context_add_main_entries(context, options, NULL);
78
79         g_option_context_parse(context, &argc, &argv, &gerr);
80         if (gerr != NULL) {
81                 g_printerr("%s\n", gerr->message);
82                 g_error_free(gerr);
83                 exit(EXIT_FAILURE);
84         }
85
86         g_option_context_free(context);
87
88         event_loop = g_main_loop_new(NULL, FALSE);
89
90         __obex_log_init("obex-client", option_debug, !option_stderr);
91
92         if (manager_init() < 0)
93                 exit(EXIT_FAILURE);
94
95         DBG("Entering main loop");
96
97         memset(&sa, 0, sizeof(sa));
98         sa.sa_handler = sig_term;
99         sigaction(SIGINT, &sa, NULL);
100         sigaction(SIGTERM, &sa, NULL);
101
102         g_main_loop_run(event_loop);
103
104         manager_exit();
105
106         g_main_loop_unref(event_loop);
107
108         __obex_log_cleanup();
109
110         return 0;
111 }