src: use new libosmogsm and include/osmocom/[gsm|core] path to headers
[osmocom-bb.git] / src / host / layer23 / src / mobile / main.c
1 /* Main method of the layer2/3 stack */
2
3 /* (C) 2010 by Holger Hans Peter Freyther
4  * (C) 2010 by Harald Welte <laforge@gnumonks.org>
5  *
6  * All Rights Reserved
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 along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23
24 #include <osmocom/bb/common/osmocom_data.h>
25 #include <osmocom/bb/common/logging.h>
26 #include <osmocom/bb/mobile/app_mobile.h>
27
28 #include <osmocom/core/talloc.h>
29 #include <osmocom/core/linuxlist.h>
30 #include <osmocom/core/gsmtap_util.h>
31 #include <osmocom/core/signal.h>
32
33 #include <arpa/inet.h>
34
35 #define _GNU_SOURCE
36 #include <getopt.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <time.h>
45 #include <libgen.h>
46
47 struct log_target *stderr_target;
48
49 void *l23_ctx = NULL;
50 struct llist_head ms_list;
51 static uint32_t gsmtap_ip = 0;
52 unsigned short vty_port = 4247;
53 int debug_set = 0;
54 char *config_dir = NULL;
55
56 int mobile_delete(struct osmocom_ms *ms, int force);
57 int mobile_signal_cb(unsigned int subsys, unsigned int signal,
58                      void *handler_data, void *signal_data);
59 int mobile_work(struct osmocom_ms *ms);
60 int mobile_exit(struct osmocom_ms *ms, int force);
61
62
63 const char *openbsc_copyright =
64         "Copyright (C) 2008-2010 ...\n"
65         "Contributions by ...\n\n"
66         "License GPLv2+: GNU GPL version 2 or later "
67                 "<http://gnu.org/licenses/gpl.html>\n"
68         "This is free software: you are free to change and redistribute it.\n"
69         "There is NO WARRANTY, to the extent permitted by law.\n";
70
71 static void print_usage(const char *app)
72 {
73         printf("Usage: %s\n", app);
74 }
75
76 static void print_help()
77 {
78         printf(" Some help...\n");
79         printf("  -h --help             this text\n");
80         printf("  -i --gsmtap-ip        The destination IP used for GSMTAP.\n");
81         printf("  -v --vty-port         The VTY port number to telnet to. "
82                 "(default %u)\n", vty_port);
83         printf("  -d --debug            Change debug flags.\n");
84 }
85
86 static void handle_options(int argc, char **argv)
87 {
88         struct sockaddr_in gsmtap;
89         while (1) {
90                 int option_index = 0, c;
91                 static struct option long_options[] = {
92                         {"help", 0, 0, 'h'},
93                         {"gsmtap-ip", 1, 0, 'i'},
94                         {"vty-port", 1, 0, 'v'},
95                         {"debug", 1, 0, 'd'},
96                         {0, 0, 0, 0},
97                 };
98
99                 c = getopt_long(argc, argv, "hi:v:d:",
100                                 long_options, &option_index);
101                 if (c == -1)
102                         break;
103
104                 switch (c) {
105                 case 'h':
106                         print_usage(argv[0]);
107                         print_help();
108                         exit(0);
109                         break;
110                 case 'i':
111                         if (!inet_aton(optarg, &gsmtap.sin_addr)) {
112                                 perror("inet_aton");
113                                 exit(2);
114                         }
115                         gsmtap_ip = ntohl(gsmtap.sin_addr.s_addr);
116                         break;
117                 case 'v':
118                         vty_port = atoi(optarg);
119                         break;
120                 case 'd':
121                         log_parse_category_mask(stderr_target, optarg);
122                         debug_set = 1;
123                         break;
124                 default:
125                         break;
126                 }
127         }
128 }
129
130 void sighandler(int sigset)
131 {
132         if (sigset == SIGHUP || sigset == SIGPIPE)
133                 return;
134
135         fprintf(stderr, "Signal %d received.\n", sigset);
136
137         /* in case there is a lockup during exit */
138         signal(SIGINT, SIG_DFL);
139         signal(SIGHUP, SIG_DFL);
140         signal(SIGTERM, SIG_DFL);
141         signal(SIGPIPE, SIG_DFL);
142
143         dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
144 }
145
146 int main(int argc, char **argv)
147 {
148         int quit = 0;
149         int rc;
150         char const * home;
151         size_t len;
152         const char osmocomcfg[] = ".osmocom/bb/mobile.cfg";
153         char *config_file = NULL;
154
155         printf("%s\n", openbsc_copyright);
156
157         srand(time(NULL));
158
159         INIT_LLIST_HEAD(&ms_list);
160         log_init(&log_info);
161         stderr_target = log_target_create_stderr();
162         log_add_target(stderr_target);
163         log_set_all_filter(stderr_target, 1);
164
165         l23_ctx = talloc_named_const(NULL, 1, "layer2 context");
166
167         handle_options(argc, argv);
168
169         if (!debug_set)
170                 log_parse_category_mask(stderr_target, "DCS:DPLMN:DRR:DMM:DSIM:DCC:DMNCC:DPAG:DSUM");
171         log_set_log_level(stderr_target, LOGL_INFO);
172
173         if (gsmtap_ip) {
174                 rc = gsmtap_init(gsmtap_ip);
175                 if (rc < 0) {
176                         fprintf(stderr, "Failed during gsmtap_init()\n");
177                         exit(1);
178                 }
179         }
180
181         home = getenv("HOME");
182         if (home != NULL) {
183                 len = strlen(home) + 1 + sizeof(osmocomcfg);
184                 config_file = talloc_size(l23_ctx, len);
185                 if (config_file != NULL)
186                         snprintf(config_file, len, "%s/%s", home, osmocomcfg);
187         }
188         /* save the config file directory name */
189         config_dir = talloc_strdup(l23_ctx, config_file);
190         config_dir = dirname(config_dir);
191
192         rc = l23_app_init(NULL, config_file, vty_port);
193         if (rc)
194                 exit(rc);
195
196         signal(SIGINT, sighandler);
197         signal(SIGHUP, sighandler);
198         signal(SIGTERM, sighandler);
199         signal(SIGPIPE, sighandler);
200
201         while (1) {
202                 l23_app_work(&quit);
203                 if (quit && llist_empty(&ms_list))
204                         break;
205                 bsc_select_main(0);
206         }
207
208         l23_app_exit();
209
210         return 0;
211 }