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