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 | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 42 | static int quit = 0; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 43 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 44 | static void print_help() |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 45 | { |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 46 | printf( "Some useful options:\n" |
| 47 | " -h --help this text\n" |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 48 | " -c --config-file Specify the filename of the config " |
| 49 | "file\n" |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 50 | " -m --mcc MCC use given MCC instead of value " |
| 51 | "provided by BTS\n" |
| 52 | " -n --mnc MNC use given MNC instead of value " |
| 53 | "provided by BTS\n" |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /* FIXME: finally get some option parsing code into libosmocore */ |
| 58 | static void handle_options(int argc, char **argv) |
| 59 | { |
| 60 | while (1) { |
| 61 | int option_idx = 0, c; |
| 62 | static const struct option long_options[] = { |
| 63 | { "help", 0, 0, 'h' }, |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 64 | { "config-file", 1, 0, 'c' }, |
Andreas Eversberg | 1944bd5 | 2012-07-06 09:32:39 +0200 | [diff] [blame] | 65 | { "mcc", 1, 0, 'm' }, |
| 66 | { "mnc", 1, 0, 'n' }, |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 67 | { 0, 0, 0, 0 } |
| 68 | }; |
| 69 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 70 | c = getopt_long(argc, argv, "hc:m:n:", |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 71 | long_options, &option_idx); |
| 72 | if (c == -1) |
| 73 | break; |
| 74 | |
| 75 | switch (c) { |
| 76 | case 'h': |
| 77 | print_help(); |
| 78 | exit(0); |
| 79 | break; |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 80 | case 'c': |
| 81 | config_file = strdup(optarg); |
| 82 | config_given = 1; |
| 83 | break; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 84 | case 'm': |
| 85 | spoof_mcc = atoi(optarg); |
| 86 | break; |
| 87 | case 'n': |
| 88 | spoof_mnc = atoi(optarg); |
| 89 | break; |
| 90 | default: |
| 91 | fprintf(stderr, "Unknown option '%c'\n", c); |
| 92 | exit(0); |
| 93 | break; |
| 94 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 95 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 96 | } |
| 97 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 98 | void sighandler(int sigset) |
| 99 | { |
| 100 | if (sigset == SIGHUP || sigset == SIGPIPE) |
| 101 | return; |
| 102 | |
| 103 | fprintf(stderr, "Signal %d received.\n", sigset); |
| 104 | |
| 105 | switch (sigset) { |
| 106 | case SIGINT: |
| 107 | /* If another signal is received afterwards, the program |
| 108 | * is terminated without finishing shutdown process. |
| 109 | */ |
| 110 | signal(SIGINT, SIG_DFL); |
| 111 | signal(SIGHUP, SIG_DFL); |
| 112 | signal(SIGTERM, SIG_DFL); |
| 113 | signal(SIGPIPE, SIG_DFL); |
| 114 | signal(SIGABRT, SIG_DFL); |
| 115 | signal(SIGUSR1, SIG_DFL); |
| 116 | signal(SIGUSR2, SIG_DFL); |
| 117 | |
| 118 | quit = 1; |
| 119 | break; |
| 120 | case SIGABRT: |
| 121 | /* in case of abort, we want to obtain a talloc report |
| 122 | * and then return to the caller, who will abort the process |
| 123 | */ |
| 124 | case SIGUSR1: |
| 125 | case SIGUSR2: |
| 126 | talloc_report_full(tall_pcu_ctx, stderr); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 131 | int main(int argc, char *argv[]) |
| 132 | { |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 133 | struct gprs_rlcmac_bts *bts; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 134 | int rc; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 135 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 136 | tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context"); |
| 137 | if (!tall_pcu_ctx) |
| 138 | return -ENOMEM; |
| 139 | |
| 140 | bts = gprs_rlcmac_bts = talloc_zero(tall_pcu_ctx, |
| 141 | struct gprs_rlcmac_bts); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 142 | if (!gprs_rlcmac_bts) |
| 143 | return -ENOMEM; |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 144 | gprs_rlcmac_bts->initial_cs = 1; |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 145 | bts->initial_cs = 1; |
| 146 | bts->cs1 = 1; |
| 147 | bts->t3142 = 20; |
| 148 | bts->t3169 = 5; |
| 149 | bts->t3191 = 5; |
| 150 | bts->t3193_msec = 100; |
| 151 | bts->t3195 = 5; |
| 152 | bts->n3101 = 10; |
| 153 | bts->n3103 = 4; |
| 154 | bts->n3105 = 8; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 155 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 156 | msgb_set_talloc_ctx(tall_pcu_ctx); |
| 157 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 158 | osmo_init_logging(&gprs_log_info); |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 159 | |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 160 | vty_init(&pcu_vty_info); |
| 161 | pcu_vty_init(&gprs_log_info); |
| 162 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame] | 163 | handle_options(argc, argv); |
| 164 | if ((!!spoof_mcc) + (!!spoof_mnc) == 1) { |
| 165 | fprintf(stderr, "--mcc and --mnc must be specified " |
| 166 | "together.\n"); |
| 167 | exit(0); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 168 | } |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 169 | |
| 170 | rc = vty_read_config_file(config_file, NULL); |
| 171 | if (rc < 0 && config_given) { |
| 172 | fprintf(stderr, "Failed to parse the config file: '%s'\n", |
| 173 | config_file); |
| 174 | exit(1); |
| 175 | } |
| 176 | if (rc < 0) |
| 177 | fprintf(stderr, "No config file: '%s' Using default config.\n", |
| 178 | config_file); |
| 179 | |
| 180 | rc = telnet_init(tall_pcu_ctx, NULL, 4240); |
| 181 | if (rc < 0) { |
| 182 | fprintf(stderr, "Error initializing telnet\n"); |
| 183 | exit(1); |
| 184 | } |
| 185 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 186 | rc = pcu_l1if_open(); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 187 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 188 | if (rc < 0) |
| 189 | return rc; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 190 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 191 | signal(SIGINT, sighandler); |
| 192 | signal(SIGHUP, sighandler); |
| 193 | signal(SIGTERM, sighandler); |
| 194 | signal(SIGPIPE, sighandler); |
| 195 | signal(SIGABRT, sighandler); |
| 196 | signal(SIGUSR1, sighandler); |
| 197 | signal(SIGUSR2, sighandler); |
| 198 | |
| 199 | while (!quit) { |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 200 | osmo_gsm_timers_check(); |
| 201 | osmo_gsm_timers_prepare(); |
| 202 | osmo_gsm_timers_update(); |
| 203 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 204 | osmo_select_main(0); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 205 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 206 | |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 207 | telnet_exit(); |
| 208 | |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 209 | pcu_l1if_close(); |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 210 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 211 | talloc_free(gprs_rlcmac_bts); |
Andreas Eversberg | e266bd4 | 2012-07-13 14:00:21 +0200 | [diff] [blame] | 212 | |
| 213 | talloc_report_full(tall_pcu_ctx, stderr); |
Andreas Eversberg | ebde64f | 2012-07-12 09:18:42 +0200 | [diff] [blame] | 214 | talloc_free(tall_pcu_ctx); |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 215 | |
| 216 | return 0; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 217 | } |