Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 1 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
Holger Hans Peter Freyther | 52b0921 | 2011-04-07 23:23:27 +0200 | [diff] [blame] | 2 | * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2009-2011 by On-Waves |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 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 | 13aa545 | 2012-03-16 11:54:55 +0100 | [diff] [blame] | 21 | #include <openbsc/bss.h> |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 22 | #include <openbsc/control_cmd.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 23 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 24 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 25 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 26 | #include <openbsc/osmo_bsc_rf.h> |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 27 | #include <openbsc/osmo_msc_data.h> |
| 28 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 29 | #include <openbsc/vty.h> |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 30 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 32 | #include <osmocom/core/application.h> |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 33 | #include <osmocom/core/linuxlist.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 34 | #include <osmocom/core/talloc.h> |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 35 | #include <osmocom/gsm/protocol/gsm_12_21.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | 13aa545 | 2012-03-16 11:54:55 +0100 | [diff] [blame] | 37 | #include <osmocom/abis/abis.h> |
| 38 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 39 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 41 | #define _GNU_SOURCE |
| 42 | #include <getopt.h> |
| 43 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 44 | #include <signal.h> |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <time.h> |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 48 | #include <unistd.h> |
| 49 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 50 | |
Harald Welte | d9dea59 | 2011-03-04 13:53:51 +0100 | [diff] [blame] | 51 | #include "../../bscconfig.h" |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 52 | |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 53 | struct gsm_network *bsc_gsmnet = 0; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 54 | static const char *config_file = "openbsc.cfg"; |
Holger Hans Peter Freyther | 410ee27 | 2011-06-04 09:32:35 +0200 | [diff] [blame] | 55 | static const char *rf_ctrl = NULL; |
Holger Hans Peter Freyther | 971532f | 2010-09-11 13:28:42 +0800 | [diff] [blame] | 56 | extern const char *openbsc_copyright; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 57 | static int daemonize = 0; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 58 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 59 | static void print_usage() |
| 60 | { |
Holger Hans Peter Freyther | 0eebfcc | 2011-04-13 09:17:40 +0200 | [diff] [blame] | 61 | printf("Usage: osmo-bsc\n"); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static void print_help() |
| 65 | { |
| 66 | printf(" Some useful help...\n"); |
| 67 | printf(" -h --help this text\n"); |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 68 | 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] | 69 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 70 | printf(" -s --disable-color\n"); |
| 71 | printf(" -T --timestamp. Print a timestamp in the debug output.\n"); |
| 72 | printf(" -c --config-file filename The config file to use.\n"); |
| 73 | printf(" -l --local=IP. The local address of the MGCP.\n"); |
| 74 | printf(" -e --log-level number. Set a global loglevel.\n"); |
| 75 | printf(" -r --rf-ctl NAME. A unix domain socket to listen for cmds.\n"); |
| 76 | printf(" -t --testmode. A special mode to provoke failures at the MSC.\n"); |
| 77 | } |
| 78 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 79 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 80 | { |
| 81 | while (1) { |
| 82 | int option_index = 0, c; |
| 83 | static struct option long_options[] = { |
| 84 | {"help", 0, 0, 'h'}, |
| 85 | {"debug", 1, 0, 'd'}, |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 86 | {"daemonize", 0, 0, 'D'}, |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 87 | {"config-file", 1, 0, 'c'}, |
| 88 | {"disable-color", 0, 0, 's'}, |
| 89 | {"timestamp", 0, 0, 'T'}, |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 90 | {"local", 1, 0, 'l'}, |
| 91 | {"log-level", 1, 0, 'e'}, |
| 92 | {"rf-ctl", 1, 0, 'r'}, |
| 93 | {"testmode", 0, 0, 't'}, |
| 94 | {0, 0, 0, 0} |
| 95 | }; |
| 96 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 97 | c = getopt_long(argc, argv, "hd:DsTc:e:r:t", |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 98 | long_options, &option_index); |
| 99 | if (c == -1) |
| 100 | break; |
| 101 | |
| 102 | switch (c) { |
| 103 | case 'h': |
| 104 | print_usage(); |
| 105 | print_help(); |
| 106 | exit(0); |
| 107 | case 's': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 108 | log_set_use_color(osmo_stderr_target, 0); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 109 | break; |
| 110 | case 'd': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 111 | log_parse_category_mask(osmo_stderr_target, optarg); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 112 | break; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 113 | case 'D': |
| 114 | daemonize = 1; |
| 115 | break; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 116 | case 'c': |
| 117 | config_file = strdup(optarg); |
| 118 | break; |
| 119 | case 'T': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 120 | log_set_print_timestamp(osmo_stderr_target, 1); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 121 | break; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 122 | case 'e': |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 123 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 124 | break; |
| 125 | case 'r': |
Holger Hans Peter Freyther | 410ee27 | 2011-06-04 09:32:35 +0200 | [diff] [blame] | 126 | rf_ctrl = optarg; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 127 | break; |
| 128 | default: |
| 129 | /* ignore */ |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 135 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 136 | |
| 137 | static struct vty_app_info vty_info = { |
Harald Welte | 9af42c3 | 2011-02-24 23:57:06 +0100 | [diff] [blame] | 138 | .name = "OsmoBSC", |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 139 | .version = PACKAGE_VERSION, |
| 140 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | 19ce77c | 2010-08-26 15:38:42 +0800 | [diff] [blame] | 141 | .is_config_node = bsc_vty_is_config_node, |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 142 | }; |
| 143 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 144 | extern int bsc_shutdown_net(struct gsm_network *net); |
| 145 | static void signal_handler(int signal) |
| 146 | { |
Holger Hans Peter Freyther | 9c5dc14 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 147 | struct osmo_msc_data *msc; |
| 148 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 149 | fprintf(stdout, "signal %u received\n", signal); |
| 150 | |
| 151 | switch (signal) { |
| 152 | case SIGINT: |
| 153 | bsc_shutdown_net(bsc_gsmnet); |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 154 | osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL); |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 155 | sleep(3); |
| 156 | exit(0); |
| 157 | break; |
| 158 | case SIGABRT: |
| 159 | /* in case of abort, we want to obtain a talloc report |
| 160 | * and then return to the caller, who will abort the process */ |
| 161 | case SIGUSR1: |
| 162 | talloc_report(tall_vty_ctx, stderr); |
| 163 | talloc_report_full(tall_bsc_ctx, stderr); |
| 164 | break; |
| 165 | case SIGUSR2: |
Holger Hans Peter Freyther | 4d61417 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 166 | if (!bsc_gsmnet->bsc_data) |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 167 | return; |
Holger Hans Peter Freyther | 9c5dc14 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 168 | llist_for_each_entry(msc, &bsc_gsmnet->bsc_data->mscs, entry) |
| 169 | bsc_msc_lost(msc->msc_con); |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 170 | break; |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | } |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 175 | |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 176 | struct location { |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 177 | struct llist_head list; |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 178 | unsigned long age; |
| 179 | int valid; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 180 | double lat; |
| 181 | double lon; |
| 182 | double height; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 183 | }; |
| 184 | |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 185 | static LLIST_HEAD(locations); |
| 186 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 187 | static void cleanup_locations() |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 188 | { |
| 189 | struct location *myloc, *tmp; |
Daniel Willmann | e2ffee9 | 2011-07-14 12:12:01 +0200 | [diff] [blame] | 190 | int invalpos = 0, i = 0; |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 191 | |
| 192 | LOGP(DCTRL, LOGL_DEBUG, "Checking position list.\n"); |
| 193 | llist_for_each_entry_safe(myloc, tmp, &locations, list) { |
| 194 | i++; |
| 195 | if (i > 3) { |
| 196 | LOGP(DCTRL, LOGL_DEBUG, "Deleting old position.\n"); |
| 197 | llist_del(&myloc->list); |
| 198 | talloc_free(myloc); |
Daniel Willmann | e2ffee9 | 2011-07-14 12:12:01 +0200 | [diff] [blame] | 199 | } else if (!myloc->valid) { /* Only capture the newest of subsequent invalid positions */ |
| 200 | invalpos++; |
| 201 | if (invalpos > 1) { |
| 202 | LOGP(DCTRL, LOGL_DEBUG, "Deleting subsequent invalid position.\n"); |
| 203 | invalpos--; |
| 204 | i--; |
| 205 | llist_del(&myloc->list); |
| 206 | talloc_free(myloc); |
| 207 | } |
| 208 | } else { |
| 209 | invalpos = 0; |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | LOGP(DCTRL, LOGL_DEBUG, "Found %i positions.\n", i); |
| 213 | } |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 214 | |
| 215 | CTRL_CMD_DEFINE(net_loc, "location"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 216 | static int get_net_loc(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 217 | { |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 218 | struct location *myloc; |
| 219 | |
| 220 | if (llist_empty(&locations)) { |
| 221 | cmd->reply = talloc_asprintf(cmd, "0,0,0,0,0"); |
| 222 | return CTRL_CMD_REPLY; |
| 223 | } else { |
| 224 | myloc = llist_entry(locations.next, struct location, list); |
| 225 | } |
| 226 | |
| 227 | cmd->reply = talloc_asprintf(cmd, "%lu,%i,%f,%f,%f", myloc->age, myloc->valid, myloc->lat, myloc->lon, myloc->height); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 228 | if (!cmd->reply) { |
| 229 | cmd->reply = "OOM"; |
| 230 | return CTRL_CMD_ERROR; |
| 231 | } |
| 232 | |
| 233 | return CTRL_CMD_REPLY; |
| 234 | } |
| 235 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 236 | static int set_net_loc(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 237 | { |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 238 | char *saveptr, *lat, *lon, *height, *age, *valid, *tmp; |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 239 | struct location *myloc; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 240 | |
| 241 | tmp = talloc_strdup(cmd, cmd->value); |
| 242 | if (!tmp) |
| 243 | goto oom; |
| 244 | |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 245 | myloc = talloc_zero(tall_bsc_ctx, struct location); |
| 246 | if (!myloc) { |
| 247 | talloc_free(tmp); |
| 248 | goto oom; |
| 249 | } |
| 250 | INIT_LLIST_HEAD(&myloc->list); |
| 251 | |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 252 | |
| 253 | age = strtok_r(tmp, ",", &saveptr); |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 254 | valid = strtok_r(NULL, ",", &saveptr); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 255 | lat = strtok_r(NULL, ",", &saveptr); |
| 256 | lon = strtok_r(NULL, ",", &saveptr); |
| 257 | height = strtok_r(NULL, "\0", &saveptr); |
| 258 | |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 259 | myloc->age = atol(age); |
| 260 | myloc->valid = atoi(valid); |
| 261 | myloc->lat = atof(lat); |
| 262 | myloc->lon = atof(lon); |
| 263 | myloc->height = atof(height); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 264 | talloc_free(tmp); |
| 265 | |
Daniel Willmann | ba5eb24 | 2011-06-30 11:03:07 +0200 | [diff] [blame] | 266 | /* Add location to the end of the list */ |
| 267 | llist_add(&myloc->list, &locations); |
| 268 | cleanup_locations(); |
| 269 | |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 270 | return get_net_loc(cmd, data); |
| 271 | oom: |
| 272 | cmd->reply = "OOM"; |
| 273 | return CTRL_CMD_ERROR; |
| 274 | } |
| 275 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 276 | static int verify_net_loc(struct ctrl_cmd *cmd, const char *value, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 277 | { |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 278 | char *saveptr, *latstr, *lonstr, *heightstr, *agestr, *validstr, *tmp; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 279 | unsigned long age; |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 280 | int valid; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 281 | double lat, lon, height; |
| 282 | |
| 283 | tmp = talloc_strdup(cmd, value); |
| 284 | if (!tmp) |
| 285 | return 1; |
| 286 | |
| 287 | agestr = strtok_r(tmp, ",", &saveptr); |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 288 | validstr = strtok_r(NULL, ",", &saveptr); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 289 | latstr = strtok_r(NULL, ",", &saveptr); |
| 290 | lonstr = strtok_r(NULL, ",", &saveptr); |
| 291 | heightstr = strtok_r(NULL, "\0", &saveptr); |
| 292 | |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 293 | if ((agestr == NULL) || (validstr == NULL) || (latstr == NULL) || |
Daniel Willmann | 72d807c | 2011-07-16 21:05:43 +0200 | [diff] [blame] | 294 | (lonstr == NULL) || (heightstr == NULL)) |
Daniel Willmann | fdc3c15 | 2011-07-16 21:50:13 +0200 | [diff] [blame] | 295 | return 1; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 296 | |
| 297 | age = atol(agestr); |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 298 | valid = atoi(validstr); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 299 | lat = atof(latstr); |
| 300 | lon = atof(lonstr); |
| 301 | height = atof(heightstr); |
| 302 | talloc_free(tmp); |
| 303 | |
Daniel Willmann | e11f8c8 | 2011-06-24 04:16:03 +0200 | [diff] [blame] | 304 | if ((age == 0) || (lat < -90) || (lat > 90) || (lon < -180) || |
| 305 | (lon > 180) || (valid < 0) || (valid > 2)) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 306 | return 1; |
| 307 | |
Daniel Willmann | fdc3c15 | 2011-07-16 21:50:13 +0200 | [diff] [blame] | 308 | return 0; |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | CTRL_CMD_DEFINE(trx_rf_lock, "rf_locked"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 312 | static int get_trx_rf_lock(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 313 | { |
| 314 | struct gsm_bts_trx *trx = cmd->node; |
| 315 | if (!trx) { |
| 316 | cmd->reply = "trx not found."; |
| 317 | return CTRL_CMD_ERROR; |
| 318 | } |
| 319 | |
Harald Welte | 0466ba4 | 2011-07-13 14:09:45 +0200 | [diff] [blame] | 320 | cmd->reply = talloc_asprintf(cmd, "%u", trx->mo.nm_state.administrative == NM_STATE_LOCKED ? 1 : 0); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 321 | return CTRL_CMD_REPLY; |
| 322 | } |
| 323 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 324 | static int set_trx_rf_lock(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 325 | { |
| 326 | int locked = atoi(cmd->value); |
| 327 | struct gsm_bts_trx *trx = cmd->node; |
| 328 | if (!trx) { |
| 329 | cmd->reply = "trx not found."; |
| 330 | return CTRL_CMD_ERROR; |
| 331 | } |
| 332 | |
| 333 | gsm_trx_lock_rf(trx, locked); |
| 334 | |
| 335 | return get_trx_rf_lock(cmd, data); |
| 336 | } |
| 337 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 338 | static int verify_trx_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 339 | { |
| 340 | int locked = atoi(cmd->value); |
| 341 | |
| 342 | if ((locked != 0) && (locked != 1)) |
| 343 | return 1; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | CTRL_CMD_DEFINE(net_rf_lock, "rf_locked"); |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 349 | static int get_net_rf_lock(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 350 | { |
| 351 | cmd->reply = "get only works for the individual trx properties."; |
| 352 | return CTRL_CMD_ERROR; |
| 353 | } |
| 354 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 355 | static int set_net_rf_lock(struct ctrl_cmd *cmd, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 356 | { |
| 357 | int locked = atoi(cmd->value); |
| 358 | struct gsm_network *net = cmd->node; |
| 359 | struct gsm_bts *bts; |
| 360 | if (!net) { |
| 361 | cmd->reply = "net not found."; |
| 362 | return CTRL_CMD_ERROR; |
| 363 | } |
| 364 | |
| 365 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 366 | struct gsm_bts_trx *trx; |
| 367 | llist_for_each_entry(trx, &bts->trx_list, list) { |
| 368 | gsm_trx_lock_rf(trx, locked); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | cmd->reply = talloc_asprintf(cmd, "%u", locked); |
| 373 | if (!cmd->reply) { |
| 374 | cmd->reply = "OOM."; |
| 375 | return CTRL_CMD_ERROR; |
| 376 | } |
| 377 | |
| 378 | return CTRL_CMD_REPLY; |
| 379 | } |
| 380 | |
Daniel Willmann | 1c081dd | 2011-08-05 11:48:18 +0200 | [diff] [blame] | 381 | static int verify_net_rf_lock(struct ctrl_cmd *cmd, const char *value, void *data) |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 382 | { |
| 383 | int locked = atoi(cmd->value); |
| 384 | |
| 385 | if ((locked != 0) && (locked != 1)) |
| 386 | return 1; |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 391 | int main(int argc, char **argv) |
| 392 | { |
Holger Hans Peter Freyther | 9c5dc14 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 393 | struct osmo_msc_data *msc; |
Holger Hans Peter Freyther | 4d61417 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 394 | struct osmo_bsc_data *data; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 395 | int rc; |
| 396 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 397 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 398 | |
| 399 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 400 | |
Pablo Neira Ayuso | 5bd96a7 | 2011-05-14 11:32:48 +0200 | [diff] [blame] | 401 | bts_init(); |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 402 | libosmo_abis_init(tall_bsc_ctx); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 403 | |
| 404 | /* enable filters */ |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 405 | |
| 406 | /* This needs to precede handle_options() */ |
Holger Hans Peter Freyther | 971532f | 2010-09-11 13:28:42 +0800 | [diff] [blame] | 407 | vty_info.copyright = openbsc_copyright; |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 408 | vty_init(&vty_info); |
Pablo Neira Ayuso | 36ad9a4 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 409 | bsc_vty_init(&log_info); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 410 | |
| 411 | /* parse options */ |
| 412 | handle_options(argc, argv); |
| 413 | |
| 414 | /* seed the PRNG */ |
| 415 | srand(time(NULL)); |
| 416 | |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 417 | /* initialize SCCP */ |
| 418 | sccp_set_log_area(DSCCP); |
| 419 | |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 420 | |
| 421 | rc = bsc_bootstrap_network(NULL, config_file); |
| 422 | if (rc < 0) { |
| 423 | fprintf(stderr, "Bootstrapping the network failed. exiting.\n"); |
| 424 | exit(1); |
| 425 | } |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 426 | bsc_api_init(bsc_gsmnet, osmo_bsc_api()); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 427 | |
Daniel Willmann | 5a6cd37 | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 428 | controlif_setup(bsc_gsmnet, 4249); |
Daniel Willmann | c3e308d | 2011-02-18 16:17:58 +0100 | [diff] [blame] | 429 | ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_loc); |
| 430 | ctrl_cmd_install(CTRL_NODE_NET, &cmd_net_rf_lock); |
| 431 | ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_rf_lock); |
Daniel Willmann | 5a6cd37 | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 432 | |
Holger Hans Peter Freyther | 4d61417 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 433 | data = bsc_gsmnet->bsc_data; |
Holger Hans Peter Freyther | 410ee27 | 2011-06-04 09:32:35 +0200 | [diff] [blame] | 434 | if (rf_ctrl) |
| 435 | bsc_replace_string(data, &data->rf_ctrl_name, rf_ctrl); |
Holger Hans Peter Freyther | 52b0921 | 2011-04-07 23:23:27 +0200 | [diff] [blame] | 436 | |
| 437 | if (data->rf_ctrl_name) { |
Holger Hans Peter Freyther | 410ee27 | 2011-06-04 09:32:35 +0200 | [diff] [blame] | 438 | data->rf_ctrl = osmo_bsc_rf_create(data->rf_ctrl_name, |
Holger Hans Peter Freyther | 52b0921 | 2011-04-07 23:23:27 +0200 | [diff] [blame] | 439 | bsc_gsmnet); |
Holger Hans Peter Freyther | 410ee27 | 2011-06-04 09:32:35 +0200 | [diff] [blame] | 440 | if (!data->rf_ctrl) { |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 441 | fprintf(stderr, "Failed to create the RF service.\n"); |
| 442 | exit(1); |
| 443 | } |
| 444 | } |
| 445 | |
Holger Hans Peter Freyther | 9c5dc14 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 446 | llist_for_each_entry(msc, &bsc_gsmnet->bsc_data->mscs, entry) { |
| 447 | if (osmo_bsc_msc_init(msc) != 0) { |
| 448 | LOGP(DNAT, LOGL_ERROR, "Failed to start up. Exiting.\n"); |
| 449 | exit(1); |
| 450 | } |
Holger Hans Peter Freyther | 05394bc | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 451 | } |
| 452 | |
Holger Hans Peter Freyther | 9c5dc14 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 453 | |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 454 | if (osmo_bsc_sccp_init(bsc_gsmnet) != 0) { |
| 455 | LOGP(DNM, LOGL_ERROR, "Failed to register SCCP.\n"); |
| 456 | exit(1); |
| 457 | } |
| 458 | |
Holger Hans Peter Freyther | 3f79397 | 2010-11-14 20:38:06 +0100 | [diff] [blame] | 459 | if (osmo_bsc_audio_init(bsc_gsmnet) != 0) { |
| 460 | LOGP(DMSC, LOGL_ERROR, "Failed to register audio support.\n"); |
| 461 | exit(1); |
| 462 | } |
| 463 | |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 464 | signal(SIGINT, &signal_handler); |
| 465 | signal(SIGABRT, &signal_handler); |
| 466 | signal(SIGUSR1, &signal_handler); |
| 467 | signal(SIGUSR2, &signal_handler); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 468 | osmo_init_ignore_signals(); |
Holger Hans Peter Freyther | 58b3df5 | 2010-09-15 22:29:25 +0800 | [diff] [blame] | 469 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 470 | if (daemonize) { |
| 471 | rc = osmo_daemonize(); |
| 472 | if (rc < 0) { |
| 473 | perror("Error during daemonize"); |
| 474 | exit(1); |
| 475 | } |
| 476 | } |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 477 | |
| 478 | while (1) { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 479 | osmo_select_main(0); |
Holger Hans Peter Freyther | 58ef6e4 | 2010-06-30 14:59:23 +0800 | [diff] [blame] | 480 | } |
| 481 | |
Holger Hans Peter Freyther | 770954e | 2010-06-30 14:33:01 +0800 | [diff] [blame] | 482 | return 0; |
| 483 | } |