blob: aa53eef07a287fc71b08d6ec5d2bb1203ed047e1 [file] [log] [blame]
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +08001/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther52b09212011-04-07 23:23:27 +02002 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
3 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +08004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +01007 * 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 Freyther770954e2010-06-30 14:33:01 +08009 * (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 Welte0e3e88e2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +080015 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010016 * 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 Freyther770954e2010-06-30 14:33:01 +080018 *
19 */
20
Holger Hans Peter Freyther13aa5452012-03-16 11:54:55 +010021#include <openbsc/bss.h>
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080022#include <openbsc/debug.h>
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +080023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther7d231302010-09-16 17:26:35 +080024#include <openbsc/osmo_bsc.h>
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080025#include <openbsc/osmo_bsc_rf.h>
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +080026#include <openbsc/osmo_msc_data.h>
27#include <openbsc/signal.h>
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +080028#include <openbsc/vty.h>
Daniel Willmannc3e308d2011-02-18 16:17:58 +010029#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +080030
Daniel Willmann5c3bad92011-07-22 17:55:42 +020031#include <openbsc/control_cmd.h>
32#include <openbsc/control_if.h>
33
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +020034#include <osmocom/core/application.h>
Daniel Willmannba5eb242011-06-30 11:03:07 +020035#include <osmocom/core/linuxlist.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010036#include <osmocom/core/talloc.h>
Daniel Willmannc3e308d2011-02-18 16:17:58 +010037#include <osmocom/gsm/protocol/gsm_12_21.h>
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080038
Holger Hans Peter Freyther13aa5452012-03-16 11:54:55 +010039#include <osmocom/abis/abis.h>
40
Harald Welte90e5eae2010-08-03 15:11:51 +020041#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freythera8dffc12010-07-30 02:53:14 +080042
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080043#define _GNU_SOURCE
44#include <getopt.h>
45
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +080046#include <signal.h>
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080047#include <stdio.h>
48#include <stdlib.h>
49#include <time.h>
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +080050#include <unistd.h>
51
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080052
Harald Welted9dea592011-03-04 13:53:51 +010053#include "../../bscconfig.h"
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080054
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +080055struct gsm_network *bsc_gsmnet = 0;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080056static const char *config_file = "openbsc.cfg";
Holger Hans Peter Freyther410ee272011-06-04 09:32:35 +020057static const char *rf_ctrl = NULL;
Holger Hans Peter Freyther971532f2010-09-11 13:28:42 +080058extern const char *openbsc_copyright;
Harald Weltec4ae1762010-08-25 19:43:54 +020059static int daemonize = 0;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080060
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080061static void print_usage()
62{
Holger Hans Peter Freyther0eebfcc2011-04-13 09:17:40 +020063 printf("Usage: osmo-bsc\n");
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080064}
65
66static void print_help()
67{
68 printf(" Some useful help...\n");
69 printf(" -h --help this text\n");
Harald Weltec4ae1762010-08-25 19:43:54 +020070 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080071 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
72 printf(" -s --disable-color\n");
73 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
74 printf(" -c --config-file filename The config file to use.\n");
75 printf(" -l --local=IP. The local address of the MGCP.\n");
76 printf(" -e --log-level number. Set a global loglevel.\n");
77 printf(" -r --rf-ctl NAME. A unix domain socket to listen for cmds.\n");
78 printf(" -t --testmode. A special mode to provoke failures at the MSC.\n");
79}
80
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +080081static void handle_options(int argc, char **argv)
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080082{
83 while (1) {
84 int option_index = 0, c;
85 static struct option long_options[] = {
86 {"help", 0, 0, 'h'},
87 {"debug", 1, 0, 'd'},
Harald Weltec4ae1762010-08-25 19:43:54 +020088 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080089 {"config-file", 1, 0, 'c'},
90 {"disable-color", 0, 0, 's'},
91 {"timestamp", 0, 0, 'T'},
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +080092 {"local", 1, 0, 'l'},
93 {"log-level", 1, 0, 'e'},
94 {"rf-ctl", 1, 0, 'r'},
95 {"testmode", 0, 0, 't'},
96 {0, 0, 0, 0}
97 };
98
Harald Weltec4ae1762010-08-25 19:43:54 +020099 c = getopt_long(argc, argv, "hd:DsTc:e:r:t",
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800100 long_options, &option_index);
101 if (c == -1)
102 break;
103
104 switch (c) {
105 case 'h':
106 print_usage();
107 print_help();
108 exit(0);
109 case 's':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200110 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800111 break;
112 case 'd':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200113 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800114 break;
Harald Weltec4ae1762010-08-25 19:43:54 +0200115 case 'D':
116 daemonize = 1;
117 break;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800118 case 'c':
119 config_file = strdup(optarg);
120 break;
121 case 'T':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200122 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800123 break;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800124 case 'e':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200125 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800126 break;
127 case 'r':
Holger Hans Peter Freyther410ee272011-06-04 09:32:35 +0200128 rf_ctrl = optarg;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800129 break;
130 default:
131 /* ignore */
132 break;
133 }
134 }
135}
136
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800137extern enum node_type bsc_vty_go_parent(struct vty *vty);
138
139static struct vty_app_info vty_info = {
Harald Welte9af42c32011-02-24 23:57:06 +0100140 .name = "OsmoBSC",
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800141 .version = PACKAGE_VERSION,
142 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +0800143 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800144};
145
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800146extern int bsc_shutdown_net(struct gsm_network *net);
147static void signal_handler(int signal)
148{
Holger Hans Peter Freyther9c5dc142011-06-04 19:58:26 +0200149 struct osmo_msc_data *msc;
150
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800151 fprintf(stdout, "signal %u received\n", signal);
152
153 switch (signal) {
154 case SIGINT:
155 bsc_shutdown_net(bsc_gsmnet);
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200156 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800157 sleep(3);
158 exit(0);
159 break;
160 case SIGABRT:
161 /* in case of abort, we want to obtain a talloc report
162 * and then return to the caller, who will abort the process */
163 case SIGUSR1:
164 talloc_report(tall_vty_ctx, stderr);
165 talloc_report_full(tall_bsc_ctx, stderr);
166 break;
167 case SIGUSR2:
Holger Hans Peter Freyther4d614172011-08-15 15:53:00 +0200168 if (!bsc_gsmnet->bsc_data)
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800169 return;
Holger Hans Peter Freyther9c5dc142011-06-04 19:58:26 +0200170 llist_for_each_entry(msc, &bsc_gsmnet->bsc_data->mscs, entry)
171 bsc_msc_lost(msc->msc_con);
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800172 break;
173 default:
174 break;
175 }
176}
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +0800177
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100178struct location {
Daniel Willmannba5eb242011-06-30 11:03:07 +0200179 struct llist_head list;
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200180 unsigned long age;
181 int valid;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100182 double lat;
183 double lon;
184 double height;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100185};
186
Daniel Willmannba5eb242011-06-30 11:03:07 +0200187static LLIST_HEAD(locations);
188
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200189static void cleanup_locations()
Daniel Willmannba5eb242011-06-30 11:03:07 +0200190{
191 struct location *myloc, *tmp;
Daniel Willmanne2ffee92011-07-14 12:12:01 +0200192 int invalpos = 0, i = 0;
Daniel Willmannba5eb242011-06-30 11:03:07 +0200193
194 LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
195 llist_for_each_entry_safe(myloc, tmp, &locations, list) {
196 i++;
197 if (i > 3) {
198 LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
199 llist_del(&myloc->list);
200 talloc_free(myloc);
Daniel Willmanne2ffee92011-07-14 12:12:01 +0200201 } else if (!myloc->valid) { /* Only capture the newest of subsequent invalid positions */
202 invalpos++;
203 if (invalpos > 1) {
204 LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n");
205 invalpos--;
206 i--;
207 llist_del(&myloc->list);
208 talloc_free(myloc);
209 }
210 } else {
211 invalpos = 0;
Daniel Willmannba5eb242011-06-30 11:03:07 +0200212 }
213 }
214 LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i);
215}
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100216
217CTRL_CMD_DEFINE(net_loc, "location");
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200218static int get_net_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100219{
Daniel Willmannba5eb242011-06-30 11:03:07 +0200220 struct location *myloc;
221
222 if (llist_empty(&locations)) {
223 cmd->reply = talloc_asprintf(cmd, "0,0,0,0,0");
224 return CTRL_CMD_REPLY;
225 } else {
226 myloc = llist_entry(locations.next, struct location, list);
227 }
228
229 cmd->reply = talloc_asprintf(cmd, "%lu,%i,%f,%f,%f", myloc->age, myloc->valid, myloc->lat, myloc->lon, myloc->height);
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100230 if (!cmd->reply) {
231 cmd->reply = "OOM";
232 return CTRL_CMD_ERROR;
233 }
234
235 return CTRL_CMD_REPLY;
236}
237
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200238static int set_net_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100239{
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200240 char *saveptr, *lat, *lon, *height, *age, *valid, *tmp;
Daniel Willmannba5eb242011-06-30 11:03:07 +0200241 struct location *myloc;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100242
243 tmp = talloc_strdup(cmd, cmd->value);
244 if (!tmp)
245 goto oom;
246
Daniel Willmannba5eb242011-06-30 11:03:07 +0200247 myloc = talloc_zero(tall_bsc_ctx, struct location);
248 if (!myloc) {
249 talloc_free(tmp);
250 goto oom;
251 }
252 INIT_LLIST_HEAD(&myloc->list);
253
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100254
255 age = strtok_r(tmp, ",", &saveptr);
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200256 valid = strtok_r(NULL, ",", &saveptr);
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100257 lat = strtok_r(NULL, ",", &saveptr);
258 lon = strtok_r(NULL, ",", &saveptr);
259 height = strtok_r(NULL, "\0", &saveptr);
260
Daniel Willmannba5eb242011-06-30 11:03:07 +0200261 myloc->age = atol(age);
262 myloc->valid = atoi(valid);
263 myloc->lat = atof(lat);
264 myloc->lon = atof(lon);
265 myloc->height = atof(height);
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100266 talloc_free(tmp);
267
Daniel Willmannba5eb242011-06-30 11:03:07 +0200268 /* Add location to the end of the list */
269 llist_add(&myloc->list, &locations);
270 cleanup_locations();
271
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100272 return get_net_loc(cmd, data);
273oom:
274 cmd->reply = "OOM";
275 return CTRL_CMD_ERROR;
276}
277
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200278static int verify_net_loc(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100279{
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200280 char *saveptr, *latstr, *lonstr, *heightstr, *agestr, *validstr, *tmp;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100281 unsigned long age;
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200282 int valid;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100283 double lat, lon, height;
284
285 tmp = talloc_strdup(cmd, value);
286 if (!tmp)
287 return 1;
288
289 agestr = strtok_r(tmp, ",", &saveptr);
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200290 validstr = strtok_r(NULL, ",", &saveptr);
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100291 latstr = strtok_r(NULL, ",", &saveptr);
292 lonstr = strtok_r(NULL, ",", &saveptr);
293 heightstr = strtok_r(NULL, "\0", &saveptr);
294
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200295 if ((agestr == NULL) || (validstr == NULL) || (latstr == NULL) ||
Daniel Willmann72d807c2011-07-16 21:05:43 +0200296 (lonstr == NULL) || (heightstr == NULL))
Daniel Willmannfdc3c152011-07-16 21:50:13 +0200297 return 1;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100298
299 age = atol(agestr);
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200300 valid = atoi(validstr);
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100301 lat = atof(latstr);
302 lon = atof(lonstr);
303 height = atof(heightstr);
304 talloc_free(tmp);
305
Daniel Willmanne11f8c82011-06-24 04:16:03 +0200306 if ((age == 0) || (lat < -90) || (lat > 90) || (lon < -180) ||
307 (lon > 180) || (valid < 0) || (valid > 2))
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100308 return 1;
309
Daniel Willmannfdc3c152011-07-16 21:50:13 +0200310 return 0;
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100311}
312
313CTRL_CMD_DEFINE(trx_rf_lock, "rf_locked");
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200314static int get_trx_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100315{
316 struct gsm_bts_trx *trx = cmd->node;
317 if (!trx) {
318 cmd->reply = "trx not found.";
319 return CTRL_CMD_ERROR;
320 }
321
Harald Welte0466ba42011-07-13 14:09:45 +0200322 cmd->reply = talloc_asprintf(cmd, "%u", trx->mo.nm_state.administrative == NM_STATE_LOCKED ? 1 : 0);
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100323 return CTRL_CMD_REPLY;
324}
325
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200326static int set_trx_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100327{
328 int locked = atoi(cmd->value);
329 struct gsm_bts_trx *trx = cmd->node;
330 if (!trx) {
331 cmd->reply = "trx not found.";
332 return CTRL_CMD_ERROR;
333 }
334
335 gsm_trx_lock_rf(trx, locked);
336
337 return get_trx_rf_lock(cmd, data);
338}
339
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200340static int verify_trx_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100341{
342 int locked = atoi(cmd->value);
343
344 if ((locked != 0) && (locked != 1))
345 return 1;
346
347 return 0;
348}
349
350CTRL_CMD_DEFINE(net_rf_lock, "rf_locked");
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200351static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100352{
353 cmd->reply = "get only works for the individual trx properties.";
354 return CTRL_CMD_ERROR;
355}
356
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200357static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100358{
359 int locked = atoi(cmd->value);
360 struct gsm_network *net = cmd->node;
361 struct gsm_bts *bts;
362 if (!net) {
363 cmd->reply = "net not found.";
364 return CTRL_CMD_ERROR;
365 }
366
367 llist_for_each_entry(bts, &net->bts_list, list) {
368 struct gsm_bts_trx *trx;
369 llist_for_each_entry(trx, &bts->trx_list, list) {
370 gsm_trx_lock_rf(trx, locked);
371 }
372 }
373
374 cmd->reply = talloc_asprintf(cmd, "%u", locked);
375 if (!cmd->reply) {
376 cmd->reply = "OOM.";
377 return CTRL_CMD_ERROR;
378 }
379
380 return CTRL_CMD_REPLY;
381}
382
Daniel Willmann1c081dd2011-08-05 11:48:18 +0200383static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100384{
385 int locked = atoi(cmd->value);
386
387 if ((locked != 0) && (locked != 1))
388 return 1;
389
390 return 0;
391}
392
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +0800393int main(int argc, char **argv)
394{
Holger Hans Peter Freyther9c5dc142011-06-04 19:58:26 +0200395 struct osmo_msc_data *msc;
Holger Hans Peter Freyther4d614172011-08-15 15:53:00 +0200396 struct osmo_bsc_data *data;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800397 int rc;
398
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800399 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200400
401 osmo_init_logging(&log_info);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800402
Pablo Neira Ayuso5bd96a72011-05-14 11:32:48 +0200403 bts_init();
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200404 libosmo_abis_init(tall_bsc_ctx);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800405
406 /* enable filters */
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800407
408 /* This needs to precede handle_options() */
Holger Hans Peter Freyther971532f2010-09-11 13:28:42 +0800409 vty_info.copyright = openbsc_copyright;
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800410 vty_init(&vty_info);
Pablo Neira Ayuso36ad9a42011-03-09 13:36:32 +0100411 bsc_vty_init(&log_info);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800412
413 /* parse options */
414 handle_options(argc, argv);
415
416 /* seed the PRNG */
417 srand(time(NULL));
418
Holger Hans Peter Freythera8dffc12010-07-30 02:53:14 +0800419 /* initialize SCCP */
420 sccp_set_log_area(DSCCP);
421
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800422
423 rc = bsc_bootstrap_network(NULL, config_file);
424 if (rc < 0) {
425 fprintf(stderr, "Bootstrapping the network failed. exiting.\n");
426 exit(1);
427 }
Holger Hans Peter Freyther7d231302010-09-16 17:26:35 +0800428 bsc_api_init(bsc_gsmnet, osmo_bsc_api());
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800429
Daniel Willmann5c3bad92011-07-22 17:55:42 +0200430 bsc_gsmnet->ctrl = controlif_setup(bsc_gsmnet, 4249);
431
Daniel Willmannc3e308d2011-02-18 16:17:58 +0100432 ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_loc);
433 ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_rf_lock);
434 ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_rf_lock);
Daniel Willmann5a6cd372011-02-18 14:30:25 +0100435
Holger Hans Peter Freyther4d614172011-08-15 15:53:00 +0200436 data = bsc_gsmnet->bsc_data;
Holger Hans Peter Freyther410ee272011-06-04 09:32:35 +0200437 if (rf_ctrl)
438 bsc_replace_string(data, &data->rf_ctrl_name, rf_ctrl);
Holger Hans Peter Freyther52b09212011-04-07 23:23:27 +0200439
440 if (data->rf_ctrl_name) {
Holger Hans Peter Freyther410ee272011-06-04 09:32:35 +0200441 data->rf_ctrl = osmo_bsc_rf_create(data->rf_ctrl_name,
Holger Hans Peter Freyther52b09212011-04-07 23:23:27 +0200442 bsc_gsmnet);
Holger Hans Peter Freyther410ee272011-06-04 09:32:35 +0200443 if (!data->rf_ctrl) {
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800444 fprintf(stderr, "Failed to create the RF service.\n");
445 exit(1);
446 }
447 }
448
Holger Hans Peter Freyther9c5dc142011-06-04 19:58:26 +0200449 llist_for_each_entry(msc, &bsc_gsmnet->bsc_data->mscs, entry) {
450 if (osmo_bsc_msc_init(msc) != 0) {
451 LOGP(DNAT, LOGL_ERROR, "Failed to start up. Exiting.\n");
452 exit(1);
453 }
Holger Hans Peter Freyther05394bc2010-09-16 02:14:41 +0800454 }
455
Holger Hans Peter Freyther9c5dc142011-06-04 19:58:26 +0200456
Holger Hans Peter Freyther689e3cc2010-11-03 16:40:03 +0100457 if (osmo_bsc_sccp_init(bsc_gsmnet) != 0) {
458 LOGP(DNM, LOGL_ERROR, "Failed to register SCCP.\n");
459 exit(1);
460 }
461
Holger Hans Peter Freyther3f793972010-11-14 20:38:06 +0100462 if (osmo_bsc_audio_init(bsc_gsmnet) != 0) {
463 LOGP(DMSC, LOGL_ERROR, "Failed to register audio support.\n");
464 exit(1);
465 }
466
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800467 signal(SIGINT, &signal_handler);
468 signal(SIGABRT, &signal_handler);
469 signal(SIGUSR1, &signal_handler);
470 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200471 osmo_init_ignore_signals();
Holger Hans Peter Freyther58b3df52010-09-15 22:29:25 +0800472
Harald Weltec4ae1762010-08-25 19:43:54 +0200473 if (daemonize) {
474 rc = osmo_daemonize();
475 if (rc < 0) {
476 perror("Error during daemonize");
477 exit(1);
478 }
479 }
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800480
481 while (1) {
Pablo Neira Ayuso04d24cd2011-05-06 12:11:23 +0200482 osmo_select_main(0);
Holger Hans Peter Freyther58ef6e42010-06-30 14:59:23 +0800483 }
484
Holger Hans Peter Freyther770954e2010-06-30 14:33:01 +0800485 return 0;
486}