Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 1 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 2 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2009-2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 9 | * (at your option) any later version. |
| 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 14 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 15 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 21 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 22 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 23 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 24 | #include <openbsc/osmo_bsc_rf.h> |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 25 | #include <openbsc/osmo_msc_data.h> |
| 26 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 27 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 30 | #include <osmocore/process.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 31 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 32 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 34 | #define _GNU_SOURCE |
| 35 | #include <getopt.h> |
| 36 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 37 | #include <signal.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <time.h> |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 41 | #include <unistd.h> |
| 42 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 43 | |
| 44 | #include "bscconfig.h" |
| 45 | |
| 46 | static struct log_target *stderr_target; |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 47 | struct gsm_network *bsc_gsmnet = 0; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 48 | static const char *config_file = "openbsc.cfg"; |
| 49 | static const char *rf_ctl = NULL; |
Holger Hans Peter Freyther | 971532f | 2010-09-11 13:28:42 +0800 | [diff] [blame] | 50 | extern const char *openbsc_copyright; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 51 | static int daemonize = 0; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 52 | |
Holger Hans Peter Freyther | 7f66183 | 2011-01-06 14:13:44 +0100 | [diff] [blame] | 53 | extern int bsc_bootstrap_network(int (*layer4)(struct gsm_network *, struct msgb *), const char *cfg_file); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 54 | |
| 55 | static void print_usage() |
| 56 | { |
| 57 | printf("Usage: bsc_msc_ip\n"); |
| 58 | } |
| 59 | |
| 60 | static void print_help() |
| 61 | { |
| 62 | printf(" Some useful help...\n"); |
| 63 | printf(" -h --help this text\n"); |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 64 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 65 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 66 | printf(" -s --disable-color\n"); |
| 67 | printf(" -T --timestamp. Print a timestamp in the debug output.\n"); |
| 68 | printf(" -c --config-file filename The config file to use.\n"); |
| 69 | printf(" -l --local=IP. The local address of the MGCP.\n"); |
| 70 | printf(" -e --log-level number. Set a global loglevel.\n"); |
| 71 | printf(" -r --rf-ctl NAME. A unix domain socket to listen for cmds.\n"); |
| 72 | printf(" -t --testmode. A special mode to provoke failures at the MSC.\n"); |
| 73 | } |
| 74 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 75 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 76 | { |
| 77 | while (1) { |
| 78 | int option_index = 0, c; |
| 79 | static struct option long_options[] = { |
| 80 | {"help", 0, 0, 'h'}, |
| 81 | {"debug", 1, 0, 'd'}, |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 82 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 83 | {"config-file", 1, 0, 'c'}, |
| 84 | {"disable-color", 0, 0, 's'}, |
| 85 | {"timestamp", 0, 0, 'T'}, |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 86 | {"local", 1, 0, 'l'}, |
| 87 | {"log-level", 1, 0, 'e'}, |
| 88 | {"rf-ctl", 1, 0, 'r'}, |
| 89 | {"testmode", 0, 0, 't'}, |
| 90 | {0, 0, 0, 0} |
| 91 | }; |
| 92 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 93 | c = getopt_long(argc, argv, "hd:DsTc:e:r:t", |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 94 | long_options, &option_index); |
| 95 | if (c == -1) |
| 96 | break; |
| 97 | |
| 98 | switch (c) { |
| 99 | case 'h': |
| 100 | print_usage(); |
| 101 | print_help(); |
| 102 | exit(0); |
| 103 | case 's': |
| 104 | log_set_use_color(stderr_target, 0); |
| 105 | break; |
| 106 | case 'd': |
| 107 | log_parse_category_mask(stderr_target, optarg); |
| 108 | break; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 109 | case 'D': |
| 110 | daemonize = 1; |
| 111 | break; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 112 | case 'c': |
| 113 | config_file = strdup(optarg); |
| 114 | break; |
| 115 | case 'T': |
| 116 | log_set_print_timestamp(stderr_target, 1); |
| 117 | break; |
| 118 | case 'P': |
| 119 | ipacc_rtp_direct = 0; |
| 120 | break; |
| 121 | case 'e': |
| 122 | log_set_log_level(stderr_target, atoi(optarg)); |
| 123 | break; |
| 124 | case 'r': |
| 125 | rf_ctl = optarg; |
| 126 | break; |
| 127 | default: |
| 128 | /* ignore */ |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | extern int bts_model_unknown_init(void); |
| 135 | extern int bts_model_bs11_init(void); |
| 136 | extern int bts_model_nanobts_init(void); |
| 137 | |
| 138 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 139 | |
| 140 | static struct vty_app_info vty_info = { |
| 141 | .name = "OpenBSC Osmo BSC", |
| 142 | .version = PACKAGE_VERSION, |
| 143 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 144 | .is_config_node = bsc_vty_is_config_node, |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 145 | }; |
| 146 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 147 | extern int bsc_shutdown_net(struct gsm_network *net); |
| 148 | static void signal_handler(int signal) |
| 149 | { |
| 150 | fprintf(stdout, "signal %u received\n", signal); |
| 151 | |
| 152 | switch (signal) { |
| 153 | case SIGINT: |
| 154 | bsc_shutdown_net(bsc_gsmnet); |
| 155 | dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
| 156 | sleep(3); |
| 157 | exit(0); |
| 158 | break; |
| 159 | case SIGABRT: |
| 160 | /* in case of abort, we want to obtain a talloc report |
| 161 | * and then return to the caller, who will abort the process */ |
| 162 | case SIGUSR1: |
| 163 | talloc_report(tall_vty_ctx, stderr); |
| 164 | talloc_report_full(tall_bsc_ctx, stderr); |
| 165 | break; |
| 166 | case SIGUSR2: |
| 167 | if (!bsc_gsmnet->msc_data) |
| 168 | return; |
| 169 | if (!bsc_gsmnet->msc_data->msc_con) |
| 170 | return; |
| 171 | if (!bsc_gsmnet->msc_data->msc_con->is_connected) |
| 172 | return; |
| 173 | bsc_msc_lost(bsc_gsmnet->msc_data->msc_con); |
| 174 | break; |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | } |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 179 | |
| 180 | int main(int argc, char **argv) |
| 181 | { |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 182 | int rc; |
| 183 | |
| 184 | log_init(&log_info); |
| 185 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
| 186 | stderr_target = log_target_create_stderr(); |
| 187 | log_add_target(stderr_target); |
| 188 | |
| 189 | bts_model_unknown_init(); |
| 190 | bts_model_bs11_init(); |
| 191 | bts_model_nanobts_init(); |
| 192 | |
| 193 | /* enable filters */ |
| 194 | log_set_all_filter(stderr_target, 1); |
| 195 | |
| 196 | /* This needs to precede handle_options() */ |
Holger Hans Peter Freyther | 971532f | 2010-09-11 13:28:42 +0800 | [diff] [blame] | 197 | vty_info.copyright = openbsc_copyright; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 198 | vty_init(&vty_info); |
| 199 | bsc_vty_init(); |
| 200 | |
| 201 | /* parse options */ |
| 202 | handle_options(argc, argv); |
| 203 | |
| 204 | /* seed the PRNG */ |
| 205 | srand(time(NULL)); |
| 206 | |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 207 | /* initialize SCCP */ |
| 208 | sccp_set_log_area(DSCCP); |
| 209 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 210 | |
| 211 | rc = bsc_bootstrap_network(NULL, config_file); |
| 212 | if (rc < 0) { |
| 213 | fprintf(stderr, "Bootstrapping the network failed. exiting.\n"); |
| 214 | exit(1); |
| 215 | } |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 216 | bsc_api_init(bsc_gsmnet, osmo_bsc_api()); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 217 | |
| 218 | if (rf_ctl) { |
Holger Hans Peter Freyther | cdcb600 | 2010-09-15 23:53:54 +0800 | [diff] [blame] | 219 | struct osmo_msc_data *data = bsc_gsmnet->msc_data; |
| 220 | data->rf_ctl = osmo_bsc_rf_create(rf_ctl, bsc_gsmnet); |
| 221 | if (!data->rf_ctl) { |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 222 | fprintf(stderr, "Failed to create the RF service.\n"); |
| 223 | exit(1); |
| 224 | } |
| 225 | } |
| 226 | |
Holger Hans Peter Freyther | 05394bc | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 227 | if (osmo_bsc_msc_init(bsc_gsmnet) != 0) { |
| 228 | LOGP(DNAT, LOGL_ERROR, "Failed to start up. Exiting.\n"); |
| 229 | exit(1); |
| 230 | } |
| 231 | |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 232 | if (osmo_bsc_sccp_init(bsc_gsmnet) != 0) { |
| 233 | LOGP(DNM, LOGL_ERROR, "Failed to register SCCP.\n"); |
| 234 | exit(1); |
| 235 | } |
| 236 | |
Holger Hans Peter Freyther | 3f79397 | 2010-11-14 20:38:06 +0100 | [diff] [blame] | 237 | if (osmo_bsc_audio_init(bsc_gsmnet) != 0) { |
| 238 | LOGP(DMSC, LOGL_ERROR, "Failed to register audio support.\n"); |
| 239 | exit(1); |
| 240 | } |
| 241 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 242 | signal(SIGINT, &signal_handler); |
| 243 | signal(SIGABRT, &signal_handler); |
| 244 | signal(SIGUSR1, &signal_handler); |
| 245 | signal(SIGUSR2, &signal_handler); |
| 246 | signal(SIGPIPE, SIG_IGN); |
| 247 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 248 | if (daemonize) { |
| 249 | rc = osmo_daemonize(); |
| 250 | if (rc < 0) { |
| 251 | perror("Error during daemonize"); |
| 252 | exit(1); |
| 253 | } |
| 254 | } |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 255 | |
| 256 | while (1) { |
| 257 | bsc_select_main(0); |
| 258 | } |
| 259 | |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 260 | return 0; |
| 261 | } |