Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* pcu_main.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <gprs_bssgp_pcu.h> |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 21 | #include <arpa/inet.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 22 | #include <pcu_l1_if.h> |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 23 | #include <gprs_rlcmac.h> |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 24 | #include <gsm_timer.h> |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 25 | #include <gprs_debug.h> |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 28 | #include <signal.h> |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 29 | extern "C" { |
| 30 | #include "pcu_vty.h" |
| 31 | #include <osmocom/vty/telnet_interface.h> |
| 32 | #include <osmocom/vty/logging.h> |
| 33 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 34 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 35 | struct gprs_rlcmac_bts *gprs_rlcmac_bts; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 36 | extern struct gprs_nsvc *nsvc; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 37 | uint16_t spoof_mcc = 0, spoof_mnc = 0; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 38 | static int config_given = 0; |
| 39 | static const char *config_file = "osmo-pcu.cfg"; |
| 40 | extern struct vty_app_info pcu_vty_info; |
| 41 | void *tall_pcu_ctx; |
Andreas Eversberg | 7a5a67a | 2013-01-16 08:43:24 +0100 | [diff] [blame] | 42 | extern void *bv_tall_ctx; |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 43 | static int quit = 0; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 44 | |
Andreas Eversberg | b83e2a7 | 2012-10-07 15:26:00 +0200 | [diff] [blame] | 45 | #ifdef DEBUG_DIAGRAM |
| 46 | extern struct timeval diagram_time; |
| 47 | #endif |
| 48 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 49 | static void print_help() |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 50 | { |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 51 | printf( "Some useful options:\n" |
| 52 | " -h --help this text\n" |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 53 | " -c --config-file Specify the filename of the config " |
| 54 | "file\n" |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 55 | " -m --mcc MCC use given MCC instead of value " |
| 56 | "provided by BTS\n" |
| 57 | " -n --mnc MNC use given MNC instead of value " |
| 58 | "provided by BTS\n" |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /* FIXME: finally get some option parsing code into libosmocore */ |
| 63 | static void handle_options(int argc, char **argv) |
| 64 | { |
| 65 | while (1) { |
| 66 | int option_idx = 0, c; |
| 67 | static const struct option long_options[] = { |
| 68 | { "help", 0, 0, 'h' }, |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 69 | { "config-file", 1, 0, 'c' }, |
Andreas Eversberg | 1944bd5 | 2012-07-06 09:32:39 +0200 | [diff] [blame] | 70 | { "mcc", 1, 0, 'm' }, |
| 71 | { "mnc", 1, 0, 'n' }, |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 72 | { 0, 0, 0, 0 } |
| 73 | }; |
| 74 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 75 | c = getopt_long(argc, argv, "hc:m:n:", |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 76 | long_options, &option_idx); |
| 77 | if (c == -1) |
| 78 | break; |
| 79 | |
| 80 | switch (c) { |
| 81 | case 'h': |
| 82 | print_help(); |
| 83 | exit(0); |
| 84 | break; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 85 | case 'c': |
| 86 | config_file = strdup(optarg); |
| 87 | config_given = 1; |
| 88 | break; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 89 | case 'm': |
| 90 | spoof_mcc = atoi(optarg); |
| 91 | break; |
| 92 | case 'n': |
| 93 | spoof_mnc = atoi(optarg); |
| 94 | break; |
| 95 | default: |
| 96 | fprintf(stderr, "Unknown option '%c'\n", c); |
| 97 | exit(0); |
| 98 | break; |
| 99 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 100 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 101 | } |
| 102 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 103 | void sighandler(int sigset) |
| 104 | { |
| 105 | if (sigset == SIGHUP || sigset == SIGPIPE) |
| 106 | return; |
| 107 | |
| 108 | fprintf(stderr, "Signal %d received.\n", sigset); |
| 109 | |
| 110 | switch (sigset) { |
| 111 | case SIGINT: |
| 112 | /* If another signal is received afterwards, the program |
| 113 | * is terminated without finishing shutdown process. |
| 114 | */ |
| 115 | signal(SIGINT, SIG_DFL); |
| 116 | signal(SIGHUP, SIG_DFL); |
| 117 | signal(SIGTERM, SIG_DFL); |
| 118 | signal(SIGPIPE, SIG_DFL); |
| 119 | signal(SIGABRT, SIG_DFL); |
| 120 | signal(SIGUSR1, SIG_DFL); |
| 121 | signal(SIGUSR2, SIG_DFL); |
| 122 | |
| 123 | quit = 1; |
| 124 | break; |
| 125 | case SIGABRT: |
| 126 | /* in case of abort, we want to obtain a talloc report |
| 127 | * and then return to the caller, who will abort the process |
| 128 | */ |
| 129 | case SIGUSR1: |
| 130 | case SIGUSR2: |
| 131 | talloc_report_full(tall_pcu_ctx, stderr); |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 136 | int main(int argc, char *argv[]) |
| 137 | { |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 138 | struct gprs_rlcmac_bts *bts; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 139 | int rc; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 140 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 141 | tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context"); |
| 142 | if (!tall_pcu_ctx) |
| 143 | return -ENOMEM; |
Andreas Eversberg | 7a5a67a | 2013-01-16 08:43:24 +0100 | [diff] [blame] | 144 | bv_tall_ctx = tall_pcu_ctx; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 145 | |
| 146 | bts = gprs_rlcmac_bts = talloc_zero(tall_pcu_ctx, |
| 147 | struct gprs_rlcmac_bts); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 148 | if (!gprs_rlcmac_bts) |
| 149 | return -ENOMEM; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 150 | bts->fc_interval = 1; |
Andreas Eversberg | 499ff41 | 2012-10-03 14:21:36 +0200 | [diff] [blame] | 151 | bts->initial_cs_dl = bts->initial_cs_ul = 1; |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 152 | bts->cs1 = 1; |
| 153 | bts->t3142 = 20; |
| 154 | bts->t3169 = 5; |
| 155 | bts->t3191 = 5; |
| 156 | bts->t3193_msec = 100; |
| 157 | bts->t3195 = 5; |
| 158 | bts->n3101 = 10; |
| 159 | bts->n3103 = 4; |
| 160 | bts->n3105 = 8; |
Andreas Eversberg | aafcbbb | 2012-09-27 09:20:45 +0200 | [diff] [blame] | 161 | bts->alpha = 10; /* a = 1.0 */ |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 162 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 163 | msgb_set_talloc_ctx(tall_pcu_ctx); |
| 164 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 165 | osmo_init_logging(&gprs_log_info); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 166 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 167 | vty_init(&pcu_vty_info); |
| 168 | pcu_vty_init(&gprs_log_info); |
| 169 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 170 | handle_options(argc, argv); |
| 171 | if ((!!spoof_mcc) + (!!spoof_mnc) == 1) { |
| 172 | fprintf(stderr, "--mcc and --mnc must be specified " |
| 173 | "together.\n"); |
| 174 | exit(0); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 175 | } |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 176 | |
| 177 | rc = vty_read_config_file(config_file, NULL); |
| 178 | if (rc < 0 && config_given) { |
| 179 | fprintf(stderr, "Failed to parse the config file: '%s'\n", |
| 180 | config_file); |
| 181 | exit(1); |
| 182 | } |
| 183 | if (rc < 0) |
| 184 | fprintf(stderr, "No config file: '%s' Using default config.\n", |
| 185 | config_file); |
| 186 | |
| 187 | rc = telnet_init(tall_pcu_ctx, NULL, 4240); |
| 188 | if (rc < 0) { |
| 189 | fprintf(stderr, "Error initializing telnet\n"); |
| 190 | exit(1); |
| 191 | } |
| 192 | |
Andreas Eversberg | f298fa8 | 2012-07-13 14:50:57 +0200 | [diff] [blame] | 193 | if (!bts->alloc_algorithm) |
Andreas Eversberg | 53f4725 | 2012-07-15 07:10:10 +0200 | [diff] [blame] | 194 | bts->alloc_algorithm = alloc_algorithm_b; |
Andreas Eversberg | f298fa8 | 2012-07-13 14:50:57 +0200 | [diff] [blame] | 195 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 196 | rc = pcu_l1if_open(); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 197 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 198 | if (rc < 0) |
| 199 | return rc; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 200 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 201 | signal(SIGINT, sighandler); |
| 202 | signal(SIGHUP, sighandler); |
| 203 | signal(SIGTERM, sighandler); |
| 204 | signal(SIGPIPE, sighandler); |
| 205 | signal(SIGABRT, sighandler); |
| 206 | signal(SIGUSR1, sighandler); |
| 207 | signal(SIGUSR2, sighandler); |
| 208 | |
| 209 | while (!quit) { |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 210 | osmo_gsm_timers_check(); |
| 211 | osmo_gsm_timers_prepare(); |
| 212 | osmo_gsm_timers_update(); |
| 213 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 214 | osmo_select_main(0); |
Andreas Eversberg | b83e2a7 | 2012-10-07 15:26:00 +0200 | [diff] [blame] | 215 | #ifdef DEBUG_DIAGRAM |
| 216 | gettimeofday(&diagram_time, NULL); |
| 217 | #endif |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 218 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 219 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 220 | telnet_exit(); |
| 221 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 222 | pcu_l1if_close(); |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 223 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 224 | talloc_free(gprs_rlcmac_bts); |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 225 | |
| 226 | talloc_report_full(tall_pcu_ctx, stderr); |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 227 | talloc_free(tall_pcu_ctx); |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 228 | |
| 229 | return 0; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 230 | } |