Initialize Tizen 2.3
[framework/connectivity/bluez.git] / mobile / monitor / main.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2011-2012  Intel Corporation
6  *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32
33 #include "mainloop.h"
34 #include "packet.h"
35 #include "control.h"
36 #include "hcidump.h"
37 #include "btsnoop.h"
38
39 static void signal_callback(int signum, void *user_data)
40 {
41         switch (signum) {
42         case SIGINT:
43         case SIGTERM:
44                 mainloop_quit();
45                 break;
46         }
47 }
48
49 static void usage(void)
50 {
51         printf("btmon - Bluetooth monitor\n"
52                 "Usage:\n");
53         printf("\tbtmon [options]\n");
54         printf("options:\n"
55                 "\t-b, --btsnoop <file>  Save dump in btsnoop format\n"
56                 "\t-h, --help            Show help options\n");
57 }
58
59 static const struct option main_options[] = {
60         { "btsnoop",    required_argument, NULL, 'b'    },
61         { "version",    no_argument,       NULL, 'v'    },
62         { "help",       no_argument,       NULL, 'h'    },
63         { }
64 };
65
66 int main(int argc, char *argv[])
67 {
68         unsigned long filter_mask = 0;
69         sigset_t mask;
70
71         mainloop_init();
72
73         for (;;) {
74                 int opt;
75
76                 opt = getopt_long(argc, argv, "bvh", main_options, NULL);
77                 if (opt < 0)
78                         break;
79
80                 switch (opt) {
81                 case 'b':
82                         btsnoop_open(optarg);
83                         break;
84                 case 'v':
85                         printf("%s\n", VERSION);
86                         return EXIT_SUCCESS;
87                 case 'h':
88                         usage();
89                         return EXIT_SUCCESS;
90                 default:
91                         return EXIT_FAILURE;
92                 }
93         }
94
95         sigemptyset(&mask);
96         sigaddset(&mask, SIGINT);
97         sigaddset(&mask, SIGTERM);
98
99         mainloop_set_signal(&mask, signal_callback, NULL, NULL);
100
101         filter_mask |= PACKET_FILTER_SHOW_INDEX;
102         filter_mask |= PACKET_FILTER_SHOW_TIME;
103         filter_mask |= PACKET_FILTER_SHOW_ACL_DATA;
104
105         packet_set_filter(filter_mask);
106
107         printf("Bluetooth monitor ver %s\n", VERSION);
108
109         if (control_tracing() < 0) {
110                 if (hcidump_tracing() < 0)
111                         return EXIT_FAILURE;
112         }
113
114         return mainloop_run();
115 }