blob: 7a47fbcb78ec57e360647ddd5ef02885f71c770d [file] [log] [blame]
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +08001/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther55ef5fb2011-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 Freyther5ccab102010-06-30 14:33:01 +08004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-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 Freyther5ccab102010-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 Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +080015 *
Harald Welte9af6ddf2011-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 Freyther5ccab102010-06-30 14:33:01 +080018 *
19 */
20
Holger Hans Peter Freyther4d319002012-03-16 11:54:55 +010021#include <openbsc/bss.h>
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080022#include <openbsc/debug.h>
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +080023#include <openbsc/gsm_data.h>
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +080024#include <openbsc/osmo_bsc.h>
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080025#include <openbsc/osmo_bsc_rf.h>
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +080026#include <openbsc/osmo_msc_data.h>
27#include <openbsc/signal.h>
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +080028#include <openbsc/vty.h>
Daniel Willmannbc070902011-02-18 16:17:58 +010029#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +080030
Daniel Willmann6fc4a982011-07-22 17:55:42 +020031#include <openbsc/control_cmd.h>
32#include <openbsc/control_if.h>
33
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020034#include <osmocom/core/application.h>
Daniel Willmann31181912011-06-30 11:03:07 +020035#include <osmocom/core/linuxlist.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010036#include <osmocom/core/talloc.h>
Daniel Willmannbc070902011-02-18 16:17:58 +010037#include <osmocom/gsm/protocol/gsm_12_21.h>
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080038
Holger Hans Peter Freyther4d319002012-03-16 11:54:55 +010039#include <osmocom/abis/abis.h>
40
Harald Welted5db12c2010-08-03 15:11:51 +020041#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +080042
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080043#define _GNU_SOURCE
44#include <getopt.h>
45
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +080046#include <signal.h>
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080047#include <stdio.h>
48#include <stdlib.h>
49#include <time.h>
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +080050#include <unistd.h>
51
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080052
Harald Weltec08e8be2011-03-04 13:53:51 +010053#include "../../bscconfig.h"
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080054
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +080055struct gsm_network *bsc_gsmnet = 0;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080056static const char *config_file = "openbsc.cfg";
Holger Hans Peter Freyther5e3bbba2011-06-04 09:32:35 +020057static const char *rf_ctrl = NULL;
Holger Hans Peter Freyther38158fb2010-09-11 13:28:42 +080058extern const char *openbsc_copyright;
Harald Welte2c869ef2010-08-25 19:43:54 +020059static int daemonize = 0;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080060
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +080061static void print_usage()
62{
Holger Hans Peter Freytherfede54c2011-04-13 09:17:40 +020063 printf("Usage: osmo-bsc\n");
Holger Hans Peter Freythercacbc732010-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 Welte2c869ef2010-08-25 19:43:54 +020070 printf(" -D --daemonize Fork the process into a background daemon\n");
Holger Hans Peter Freythercacbc732010-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 Freytheradb6e1c2010-09-18 06:44:24 +080081static void handle_options(int argc, char **argv)
Holger Hans Peter Freythercacbc732010-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 Welte2c869ef2010-08-25 19:43:54 +020088 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freythercacbc732010-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 Freythercacbc732010-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 Welte2c869ef2010-08-25 19:43:54 +020099 c = getopt_long(argc, argv, "hd:DsTc:e:r:t",
Holger Hans Peter Freythercacbc732010-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 Freyther67cd75f2011-05-12 16:02:07 +0200110 log_set_use_color(osmo_stderr_target, 0);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800111 break;
112 case 'd':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200113 log_parse_category_mask(osmo_stderr_target, optarg);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800114 break;
Harald Welte2c869ef2010-08-25 19:43:54 +0200115 case 'D':
116 daemonize = 1;
117 break;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800118 case 'c':
119 config_file = strdup(optarg);
120 break;
121 case 'T':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200122 log_set_print_timestamp(osmo_stderr_target, 1);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800123 break;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800124 case 'e':
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200125 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800126 break;
127 case 'r':
Holger Hans Peter Freyther5e3bbba2011-06-04 09:32:35 +0200128 rf_ctrl = optarg;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800129 break;
130 default:
131 /* ignore */
132 break;
133 }
134 }
135}
136
Holger Hans Peter Freythercacbc732010-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 Welteec1921d2011-02-24 23:57:06 +0100140 .name = "OsmoBSC",
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800141 .version = PACKAGE_VERSION,
142 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther7a2c86b2010-08-26 15:38:42 +0800143 .is_config_node = bsc_vty_is_config_node,
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800144};
145
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +0800146extern int bsc_shutdown_net(struct gsm_network *net);
147static void signal_handler(int signal)
148{
Holger Hans Peter Freyther20fea242011-06-04 19:58:26 +0200149 struct osmo_msc_data *msc;
150
Holger Hans Peter Freyther3e8e0462010-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 Ayusoed5cacb2011-08-17 22:44:07 +0200156 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Holger Hans Peter Freyther3e8e0462010-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 Freyther8ec49522011-08-15 15:53:00 +0200168 if (!bsc_gsmnet->bsc_data)
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +0800169 return;
Holger Hans Peter Freyther20fea242011-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 Freyther3e8e0462010-09-15 22:29:25 +0800172 break;
173 default:
174 break;
175 }
176}
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +0800177
Daniel Willmannadcde9f2011-07-22 18:09:11 +0200178void osmo_bsc_send_trap(struct ctrl_cmd *cmd, struct bsc_msc_connection *msc_con)
179{
180 struct ctrl_cmd *trap;
181 struct ctrl_handle *ctrl;
182 struct osmo_msc_data *msc_data;
183
184 msc_data = (struct osmo_msc_data *) msc_con->write_queue.bfd.data;
185 ctrl = msc_data->network->ctrl;
186
187 trap = ctrl_cmd_trap(cmd);
188 if (!trap) {
189 LOGP(DCTRL, LOGL_ERROR, "Failed to create trap.\n");
190 return;
191 }
192
193 ctrl_cmd_send_to_all(ctrl, trap);
194 ctrl_cmd_send(&msc_con->write_queue, trap);
195
196 talloc_free(trap);
197}
198
Daniel Willmannbc070902011-02-18 16:17:58 +0100199struct location {
Daniel Willmann31181912011-06-30 11:03:07 +0200200 struct llist_head list;
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200201 unsigned long age;
202 int valid;
Daniel Willmannbc070902011-02-18 16:17:58 +0100203 double lat;
204 double lon;
205 double height;
Daniel Willmannbc070902011-02-18 16:17:58 +0100206};
207
Daniel Willmann31181912011-06-30 11:03:07 +0200208static LLIST_HEAD(locations);
209
Daniel Willmann57630932011-08-05 11:48:18 +0200210static void cleanup_locations()
Daniel Willmann31181912011-06-30 11:03:07 +0200211{
212 struct location *myloc, *tmp;
Daniel Willmann8d9876e2011-07-14 12:12:01 +0200213 int invalpos = 0, i = 0;
Daniel Willmann31181912011-06-30 11:03:07 +0200214
215 LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n");
216 llist_for_each_entry_safe(myloc, tmp, &locations, list) {
217 i++;
218 if (i > 3) {
219 LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n");
220 llist_del(&myloc->list);
221 talloc_free(myloc);
Daniel Willmann8d9876e2011-07-14 12:12:01 +0200222 } else if (!myloc->valid) { /* Only capture the newest of subsequent invalid positions */
223 invalpos++;
224 if (invalpos > 1) {
225 LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n");
226 invalpos--;
227 i--;
228 llist_del(&myloc->list);
229 talloc_free(myloc);
230 }
231 } else {
232 invalpos = 0;
Daniel Willmann31181912011-06-30 11:03:07 +0200233 }
234 }
235 LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i);
236}
Daniel Willmannbc070902011-02-18 16:17:58 +0100237
238CTRL_CMD_DEFINE(net_loc, "location");
Daniel Willmann57630932011-08-05 11:48:18 +0200239static int get_net_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100240{
Daniel Willmann31181912011-06-30 11:03:07 +0200241 struct location *myloc;
242
243 if (llist_empty(&locations)) {
244 cmd->reply = talloc_asprintf(cmd, "0,0,0,0,0");
245 return CTRL_CMD_REPLY;
246 } else {
247 myloc = llist_entry(locations.next, struct location, list);
248 }
249
250 cmd->reply = talloc_asprintf(cmd, "%lu,%i,%f,%f,%f", myloc->age, myloc->valid, myloc->lat, myloc->lon, myloc->height);
Daniel Willmannbc070902011-02-18 16:17:58 +0100251 if (!cmd->reply) {
252 cmd->reply = "OOM";
253 return CTRL_CMD_ERROR;
254 }
255
256 return CTRL_CMD_REPLY;
257}
258
Daniel Willmann57630932011-08-05 11:48:18 +0200259static int set_net_loc(struct ctrl_cmd *cmd, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100260{
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200261 char *saveptr, *lat, *lon, *height, *age, *valid, *tmp;
Daniel Willmannadcde9f2011-07-22 18:09:11 +0200262 struct osmo_msc_data *msc;
Daniel Willmann31181912011-06-30 11:03:07 +0200263 struct location *myloc;
Daniel Willmannadcde9f2011-07-22 18:09:11 +0200264 int ret;
265 struct gsm_network *gsmnet = (struct gsm_network *)data;
Daniel Willmannbc070902011-02-18 16:17:58 +0100266
267 tmp = talloc_strdup(cmd, cmd->value);
268 if (!tmp)
269 goto oom;
270
Daniel Willmann31181912011-06-30 11:03:07 +0200271 myloc = talloc_zero(tall_bsc_ctx, struct location);
272 if (!myloc) {
273 talloc_free(tmp);
274 goto oom;
275 }
276 INIT_LLIST_HEAD(&myloc->list);
277
Daniel Willmannbc070902011-02-18 16:17:58 +0100278
279 age = strtok_r(tmp, ",", &saveptr);
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200280 valid = strtok_r(NULL, ",", &saveptr);
Daniel Willmannbc070902011-02-18 16:17:58 +0100281 lat = strtok_r(NULL, ",", &saveptr);
282 lon = strtok_r(NULL, ",", &saveptr);
283 height = strtok_r(NULL, "\0", &saveptr);
284
Daniel Willmann31181912011-06-30 11:03:07 +0200285 myloc->age = atol(age);
286 myloc->valid = atoi(valid);
287 myloc->lat = atof(lat);
288 myloc->lon = atof(lon);
289 myloc->height = atof(height);
Daniel Willmannbc070902011-02-18 16:17:58 +0100290 talloc_free(tmp);
291
Daniel Willmann31181912011-06-30 11:03:07 +0200292 /* Add location to the end of the list */
293 llist_add(&myloc->list, &locations);
294 cleanup_locations();
295
Daniel Willmannadcde9f2011-07-22 18:09:11 +0200296 ret = get_net_loc(cmd, data);
297
298 llist_for_each_entry(msc, &gsmnet->bsc_data->mscs, entry)
299 osmo_bsc_send_trap(cmd, msc->msc_con);
300
301 return ret;
302
Daniel Willmannbc070902011-02-18 16:17:58 +0100303oom:
304 cmd->reply = "OOM";
305 return CTRL_CMD_ERROR;
306}
307
Daniel Willmann57630932011-08-05 11:48:18 +0200308static int verify_net_loc(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100309{
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200310 char *saveptr, *latstr, *lonstr, *heightstr, *agestr, *validstr, *tmp;
Daniel Willmannbc070902011-02-18 16:17:58 +0100311 unsigned long age;
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200312 int valid;
Daniel Willmannbc070902011-02-18 16:17:58 +0100313 double lat, lon, height;
314
315 tmp = talloc_strdup(cmd, value);
316 if (!tmp)
317 return 1;
318
319 agestr = strtok_r(tmp, ",", &saveptr);
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200320 validstr = strtok_r(NULL, ",", &saveptr);
Daniel Willmannbc070902011-02-18 16:17:58 +0100321 latstr = strtok_r(NULL, ",", &saveptr);
322 lonstr = strtok_r(NULL, ",", &saveptr);
323 heightstr = strtok_r(NULL, "\0", &saveptr);
324
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200325 if ((agestr == NULL) || (validstr == NULL) || (latstr == NULL) ||
Daniel Willmann44fb1512011-07-16 21:05:43 +0200326 (lonstr == NULL) || (heightstr == NULL))
Daniel Willmann6d718c02011-07-16 21:50:13 +0200327 return 1;
Daniel Willmannbc070902011-02-18 16:17:58 +0100328
329 age = atol(agestr);
Daniel Willmannfa2218c2011-06-24 04:16:03 +0200330 valid = atoi(validstr);
Daniel Willmannbc070902011-02-18 16:17:58 +0100331 lat = atof(latstr);
332 lon = atof(lonstr);
333 height = atof(heightstr);
334 talloc_free(tmp);
335
Daniel Willmann47824542011-07-28 19:24:42 +0200336 if (((age == 0) && (valid !=0)) || (lat < -90) || (lat > 90) ||
337 (lon < -180) || (lon > 180) || (valid < 0) || (valid > 2))
Daniel Willmannbc070902011-02-18 16:17:58 +0100338 return 1;
339
Daniel Willmann6d718c02011-07-16 21:50:13 +0200340 return 0;
Daniel Willmannbc070902011-02-18 16:17:58 +0100341}
342
343CTRL_CMD_DEFINE(trx_rf_lock, "rf_locked");
Daniel Willmann57630932011-08-05 11:48:18 +0200344static int get_trx_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100345{
346 struct gsm_bts_trx *trx = cmd->node;
347 if (!trx) {
348 cmd->reply = "trx not found.";
349 return CTRL_CMD_ERROR;
350 }
351
Harald Weltef505f5d2011-07-13 14:09:45 +0200352 cmd->reply = talloc_asprintf(cmd, "%u", trx->mo.nm_state.administrative == NM_STATE_LOCKED ? 1 : 0);
Daniel Willmannbc070902011-02-18 16:17:58 +0100353 return CTRL_CMD_REPLY;
354}
355
Daniel Willmann57630932011-08-05 11:48:18 +0200356static int set_trx_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100357{
358 int locked = atoi(cmd->value);
359 struct gsm_bts_trx *trx = cmd->node;
360 if (!trx) {
361 cmd->reply = "trx not found.";
362 return CTRL_CMD_ERROR;
363 }
364
365 gsm_trx_lock_rf(trx, locked);
366
367 return get_trx_rf_lock(cmd, data);
368}
369
Daniel Willmann57630932011-08-05 11:48:18 +0200370static int verify_trx_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100371{
372 int locked = atoi(cmd->value);
373
374 if ((locked != 0) && (locked != 1))
375 return 1;
376
377 return 0;
378}
379
380CTRL_CMD_DEFINE(net_rf_lock, "rf_locked");
Daniel Willmann57630932011-08-05 11:48:18 +0200381static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100382{
383 cmd->reply = "get only works for the individual trx properties.";
384 return CTRL_CMD_ERROR;
385}
386
Daniel Willmann57630932011-08-05 11:48:18 +0200387static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100388{
389 int locked = atoi(cmd->value);
390 struct gsm_network *net = cmd->node;
391 struct gsm_bts *bts;
392 if (!net) {
393 cmd->reply = "net not found.";
394 return CTRL_CMD_ERROR;
395 }
396
397 llist_for_each_entry(bts, &net->bts_list, list) {
398 struct gsm_bts_trx *trx;
399 llist_for_each_entry(trx, &bts->trx_list, list) {
400 gsm_trx_lock_rf(trx, locked);
401 }
402 }
403
404 cmd->reply = talloc_asprintf(cmd, "%u", locked);
405 if (!cmd->reply) {
406 cmd->reply = "OOM.";
407 return CTRL_CMD_ERROR;
408 }
409
410 return CTRL_CMD_REPLY;
411}
412
Daniel Willmann57630932011-08-05 11:48:18 +0200413static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data)
Daniel Willmannbc070902011-02-18 16:17:58 +0100414{
415 int locked = atoi(cmd->value);
416
417 if ((locked != 0) && (locked != 1))
418 return 1;
419
420 return 0;
421}
422
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +0800423int main(int argc, char **argv)
424{
Holger Hans Peter Freyther20fea242011-06-04 19:58:26 +0200425 struct osmo_msc_data *msc;
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200426 struct osmo_bsc_data *data;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800427 int rc;
428
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800429 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200430
431 osmo_init_logging(&log_info);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800432
Pablo Neira Ayuso0d2881a2011-05-14 11:32:48 +0200433 bts_init();
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200434 libosmo_abis_init(tall_bsc_ctx);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800435
436 /* enable filters */
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800437
438 /* This needs to precede handle_options() */
Holger Hans Peter Freyther38158fb2010-09-11 13:28:42 +0800439 vty_info.copyright = openbsc_copyright;
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800440 vty_init(&vty_info);
Pablo Neira Ayuso739a5662011-03-09 13:36:32 +0100441 bsc_vty_init(&log_info);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800442
443 /* parse options */
444 handle_options(argc, argv);
445
446 /* seed the PRNG */
447 srand(time(NULL));
448
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +0800449 /* initialize SCCP */
450 sccp_set_log_area(DSCCP);
451
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800452
453 rc = bsc_bootstrap_network(NULL, config_file);
454 if (rc < 0) {
455 fprintf(stderr, "Bootstrapping the network failed. exiting.\n");
456 exit(1);
457 }
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800458 bsc_api_init(bsc_gsmnet, osmo_bsc_api());
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800459
Daniel Willmann6fc4a982011-07-22 17:55:42 +0200460 bsc_gsmnet->ctrl = controlif_setup(bsc_gsmnet, 4249);
461
Daniel Willmannbc070902011-02-18 16:17:58 +0100462 ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_loc);
463 ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_rf_lock);
464 ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_rf_lock);
Daniel Willmannf7d557c2011-02-18 14:30:25 +0100465
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +0200466 data = bsc_gsmnet->bsc_data;
Holger Hans Peter Freyther5e3bbba2011-06-04 09:32:35 +0200467 if (rf_ctrl)
468 bsc_replace_string(data, &data->rf_ctrl_name, rf_ctrl);
Holger Hans Peter Freyther55ef5fb2011-04-07 23:23:27 +0200469
470 if (data->rf_ctrl_name) {
Holger Hans Peter Freyther5e3bbba2011-06-04 09:32:35 +0200471 data->rf_ctrl = osmo_bsc_rf_create(data->rf_ctrl_name,
Holger Hans Peter Freyther55ef5fb2011-04-07 23:23:27 +0200472 bsc_gsmnet);
Holger Hans Peter Freyther5e3bbba2011-06-04 09:32:35 +0200473 if (!data->rf_ctrl) {
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800474 fprintf(stderr, "Failed to create the RF service.\n");
475 exit(1);
476 }
477 }
478
Holger Hans Peter Freyther20fea242011-06-04 19:58:26 +0200479 llist_for_each_entry(msc, &bsc_gsmnet->bsc_data->mscs, entry) {
480 if (osmo_bsc_msc_init(msc) != 0) {
481 LOGP(DNAT, LOGL_ERROR, "Failed to start up. Exiting.\n");
482 exit(1);
483 }
Holger Hans Peter Freyther5832b3e2010-09-16 02:14:41 +0800484 }
485
Holger Hans Peter Freyther20fea242011-06-04 19:58:26 +0200486
Holger Hans Peter Freytherf3d1ae72010-11-03 16:40:03 +0100487 if (osmo_bsc_sccp_init(bsc_gsmnet) != 0) {
488 LOGP(DNM, LOGL_ERROR, "Failed to register SCCP.\n");
489 exit(1);
490 }
491
Holger Hans Peter Freyther620c2e62010-11-14 20:38:06 +0100492 if (osmo_bsc_audio_init(bsc_gsmnet) != 0) {
493 LOGP(DMSC, LOGL_ERROR, "Failed to register audio support.\n");
494 exit(1);
495 }
496
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +0800497 signal(SIGINT, &signal_handler);
498 signal(SIGABRT, &signal_handler);
499 signal(SIGUSR1, &signal_handler);
500 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +0200501 osmo_init_ignore_signals();
Holger Hans Peter Freyther3e8e0462010-09-15 22:29:25 +0800502
Harald Welte2c869ef2010-08-25 19:43:54 +0200503 if (daemonize) {
504 rc = osmo_daemonize();
505 if (rc < 0) {
506 perror("Error during daemonize");
507 exit(1);
508 }
509 }
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800510
511 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200512 osmo_select_main(0);
Holger Hans Peter Freythercacbc732010-06-30 14:59:23 +0800513 }
514
Holger Hans Peter Freyther5ccab102010-06-30 14:33:01 +0800515 return 0;
516}