blob: 142c9f98848113cd341fc4bd9055cec5579ed1b1 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* 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 Kluchnikova9f1ff22012-05-24 22:25:06 +040021#include <arpa/inet.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040022#include <pcu_l1_if.h>
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020023#include <gprs_rlcmac.h>
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +040024#include <gsm_timer.h>
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040025#include <gprs_debug.h>
Andreas Eversberg81e895b2012-07-06 08:24:53 +020026#include <unistd.h>
27#include <getopt.h>
Andreas Eversberge266bd42012-07-13 14:00:21 +020028#include <signal.h>
Andreas Eversbergebde64f2012-07-12 09:18:42 +020029extern "C" {
30#include "pcu_vty.h"
31#include <osmocom/vty/telnet_interface.h>
32#include <osmocom/vty/logging.h>
33}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040034
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020035struct gprs_rlcmac_bts *gprs_rlcmac_bts;
Andreas Eversberg3e372d52012-07-06 09:28:15 +020036extern struct gprs_nsvc *nsvc;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020037uint16_t spoof_mcc = 0, spoof_mnc = 0;
Andreas Eversbergebde64f2012-07-12 09:18:42 +020038static int config_given = 0;
39static const char *config_file = "osmo-pcu.cfg";
40extern struct vty_app_info pcu_vty_info;
41void *tall_pcu_ctx;
Andreas Eversberge266bd42012-07-13 14:00:21 +020042static int quit = 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040043
Andreas Eversbergb83e2a72012-10-07 15:26:00 +020044#ifdef DEBUG_DIAGRAM
45extern struct timeval diagram_time;
46#endif
47
Andreas Eversberg81e895b2012-07-06 08:24:53 +020048static void print_help()
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040049{
Andreas Eversberg81e895b2012-07-06 08:24:53 +020050 printf( "Some useful options:\n"
51 " -h --help this text\n"
Andreas Eversbergebde64f2012-07-12 09:18:42 +020052 " -c --config-file Specify the filename of the config "
53 "file\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020054 " -m --mcc MCC use given MCC instead of value "
55 "provided by BTS\n"
56 " -n --mnc MNC use given MNC instead of value "
57 "provided by BTS\n"
58 );
59}
60
61/* FIXME: finally get some option parsing code into libosmocore */
62static void handle_options(int argc, char **argv)
63{
64 while (1) {
65 int option_idx = 0, c;
66 static const struct option long_options[] = {
67 { "help", 0, 0, 'h' },
Andreas Eversbergebde64f2012-07-12 09:18:42 +020068 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +020069 { "mcc", 1, 0, 'm' },
70 { "mnc", 1, 0, 'n' },
Andreas Eversberg81e895b2012-07-06 08:24:53 +020071 { 0, 0, 0, 0 }
72 };
73
Andreas Eversbergebde64f2012-07-12 09:18:42 +020074 c = getopt_long(argc, argv, "hc:m:n:",
Andreas Eversberg81e895b2012-07-06 08:24:53 +020075 long_options, &option_idx);
76 if (c == -1)
77 break;
78
79 switch (c) {
80 case 'h':
81 print_help();
82 exit(0);
83 break;
Andreas Eversbergebde64f2012-07-12 09:18:42 +020084 case 'c':
85 config_file = strdup(optarg);
86 config_given = 1;
87 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020088 case 'm':
89 spoof_mcc = atoi(optarg);
90 break;
91 case 'n':
92 spoof_mnc = atoi(optarg);
93 break;
94 default:
95 fprintf(stderr, "Unknown option '%c'\n", c);
96 exit(0);
97 break;
98 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040099 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400100}
101
Andreas Eversberge266bd42012-07-13 14:00:21 +0200102void sighandler(int sigset)
103{
104 if (sigset == SIGHUP || sigset == SIGPIPE)
105 return;
106
107 fprintf(stderr, "Signal %d received.\n", sigset);
108
109 switch (sigset) {
110 case SIGINT:
111 /* If another signal is received afterwards, the program
112 * is terminated without finishing shutdown process.
113 */
114 signal(SIGINT, SIG_DFL);
115 signal(SIGHUP, SIG_DFL);
116 signal(SIGTERM, SIG_DFL);
117 signal(SIGPIPE, SIG_DFL);
118 signal(SIGABRT, SIG_DFL);
119 signal(SIGUSR1, SIG_DFL);
120 signal(SIGUSR2, SIG_DFL);
121
122 quit = 1;
123 break;
124 case SIGABRT:
125 /* in case of abort, we want to obtain a talloc report
126 * and then return to the caller, who will abort the process
127 */
128 case SIGUSR1:
129 case SIGUSR2:
130 talloc_report_full(tall_pcu_ctx, stderr);
131 break;
132 }
133}
134
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400135int main(int argc, char *argv[])
136{
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200137 struct gprs_rlcmac_bts *bts;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200138 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200139
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200140 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
141 if (!tall_pcu_ctx)
142 return -ENOMEM;
143
144 bts = gprs_rlcmac_bts = talloc_zero(tall_pcu_ctx,
145 struct gprs_rlcmac_bts);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200146 if (!gprs_rlcmac_bts)
147 return -ENOMEM;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200148 bts->fc_interval = 1;
Andreas Eversberg499ff412012-10-03 14:21:36 +0200149 bts->initial_cs_dl = bts->initial_cs_ul = 1;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200150 bts->cs1 = 1;
151 bts->t3142 = 20;
152 bts->t3169 = 5;
153 bts->t3191 = 5;
154 bts->t3193_msec = 100;
155 bts->t3195 = 5;
156 bts->n3101 = 10;
157 bts->n3103 = 4;
158 bts->n3105 = 8;
Andreas Eversbergaafcbbb2012-09-27 09:20:45 +0200159 bts->alpha = 10; /* a = 1.0 */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200160
Andreas Eversberge266bd42012-07-13 14:00:21 +0200161 msgb_set_talloc_ctx(tall_pcu_ctx);
162
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400163 osmo_init_logging(&gprs_log_info);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400164
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200165 vty_init(&pcu_vty_info);
166 pcu_vty_init(&gprs_log_info);
167
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200168 handle_options(argc, argv);
169 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
170 fprintf(stderr, "--mcc and --mnc must be specified "
171 "together.\n");
172 exit(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400173 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200174
175 rc = vty_read_config_file(config_file, NULL);
176 if (rc < 0 && config_given) {
177 fprintf(stderr, "Failed to parse the config file: '%s'\n",
178 config_file);
179 exit(1);
180 }
181 if (rc < 0)
182 fprintf(stderr, "No config file: '%s' Using default config.\n",
183 config_file);
184
185 rc = telnet_init(tall_pcu_ctx, NULL, 4240);
186 if (rc < 0) {
187 fprintf(stderr, "Error initializing telnet\n");
188 exit(1);
189 }
190
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200191 if (!bts->alloc_algorithm)
Andreas Eversberg53f47252012-07-15 07:10:10 +0200192 bts->alloc_algorithm = alloc_algorithm_b;
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200193
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200194 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400195
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200196 if (rc < 0)
197 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400198
Andreas Eversberge266bd42012-07-13 14:00:21 +0200199 signal(SIGINT, sighandler);
200 signal(SIGHUP, sighandler);
201 signal(SIGTERM, sighandler);
202 signal(SIGPIPE, sighandler);
203 signal(SIGABRT, sighandler);
204 signal(SIGUSR1, sighandler);
205 signal(SIGUSR2, sighandler);
206
207 while (!quit) {
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400208 osmo_gsm_timers_check();
209 osmo_gsm_timers_prepare();
210 osmo_gsm_timers_update();
211
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400212 osmo_select_main(0);
Andreas Eversbergb83e2a72012-10-07 15:26:00 +0200213#ifdef DEBUG_DIAGRAM
214 gettimeofday(&diagram_time, NULL);
215#endif
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400216 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400217
Andreas Eversberge266bd42012-07-13 14:00:21 +0200218 telnet_exit();
219
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200220 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200221
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200222 talloc_free(gprs_rlcmac_bts);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200223
224 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200225 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200226
227 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400228}