blob: 2915b9e452b8e205922e905bed37dec27da2d44b [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 Eversberg783aa4b2013-05-05 09:15:11 +020029#include <sched.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020030#include <bts.h>
Andreas Eversbergebde64f2012-07-12 09:18:42 +020031extern "C" {
32#include "pcu_vty.h"
33#include <osmocom/vty/telnet_interface.h>
34#include <osmocom/vty/logging.h>
35}
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040036
Andreas Eversberg3e372d52012-07-06 09:28:15 +020037extern struct gprs_nsvc *nsvc;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020038uint16_t spoof_mcc = 0, spoof_mnc = 0;
Andreas Eversbergebde64f2012-07-12 09:18:42 +020039static int config_given = 0;
Holger Hans Peter Freyther4e8b50c2013-11-11 20:12:30 +010040static char *config_file = strdup("osmo-pcu.cfg");
Andreas Eversbergebde64f2012-07-12 09:18:42 +020041extern struct vty_app_info pcu_vty_info;
42void *tall_pcu_ctx;
Andreas Eversberg7a5a67a2013-01-16 08:43:24 +010043extern void *bv_tall_ctx;
Andreas Eversberge266bd42012-07-13 14:00:21 +020044static int quit = 0;
Andreas Eversberg783aa4b2013-05-05 09:15:11 +020045static int rt_prio = -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040046
Andreas Eversbergb83e2a72012-10-07 15:26:00 +020047#ifdef DEBUG_DIAGRAM
48extern struct timeval diagram_time;
49#endif
50
Andreas Eversberg81e895b2012-07-06 08:24:53 +020051static void print_help()
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040052{
Andreas Eversberg81e895b2012-07-06 08:24:53 +020053 printf( "Some useful options:\n"
54 " -h --help this text\n"
Andreas Eversbergebde64f2012-07-12 09:18:42 +020055 " -c --config-file Specify the filename of the config "
56 "file\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020057 " -m --mcc MCC use given MCC instead of value "
58 "provided by BTS\n"
59 " -n --mnc MNC use given MNC instead of value "
60 "provided by BTS\n"
Andreas Eversberg783aa4b2013-05-05 09:15:11 +020061 " -r --realtime PRIO Use SCHED_RR with the specified "
62 "priority\n"
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +020063 " -e --exit Exit the application on disconnect\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020064 );
65}
66
67/* FIXME: finally get some option parsing code into libosmocore */
68static void handle_options(int argc, char **argv)
69{
70 while (1) {
71 int option_idx = 0, c;
72 static const struct option long_options[] = {
73 { "help", 0, 0, 'h' },
Andreas Eversbergebde64f2012-07-12 09:18:42 +020074 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +020075 { "mcc", 1, 0, 'm' },
76 { "mnc", 1, 0, 'n' },
Harald Weltee5a09392013-01-17 12:28:16 +010077 { "version", 0, 0, 'V' },
Andreas Eversberg783aa4b2013-05-05 09:15:11 +020078 { "realtime", 1, 0, 'r' },
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +020079 { "exit", 0, 0, 'e' },
Andreas Eversberg81e895b2012-07-06 08:24:53 +020080 { 0, 0, 0, 0 }
81 };
82
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +020083 c = getopt_long(argc, argv, "hc:m:n:Vr:e",
Andreas Eversberg81e895b2012-07-06 08:24:53 +020084 long_options, &option_idx);
85 if (c == -1)
86 break;
87
88 switch (c) {
89 case 'h':
90 print_help();
91 exit(0);
92 break;
Andreas Eversbergebde64f2012-07-12 09:18:42 +020093 case 'c':
Holger Hans Peter Freyther4e8b50c2013-11-11 20:12:30 +010094 free(config_file);
Andreas Eversbergebde64f2012-07-12 09:18:42 +020095 config_file = strdup(optarg);
96 config_given = 1;
97 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020098 case 'm':
99 spoof_mcc = atoi(optarg);
100 break;
101 case 'n':
102 spoof_mnc = atoi(optarg);
103 break;
Harald Weltee5a09392013-01-17 12:28:16 +0100104 case 'V':
105 print_version(1);
106 exit(0);
107 break;
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200108 case 'r':
109 rt_prio = atoi(optarg);
110 break;
Holger Hans Peter Freythera30f4762013-07-11 16:13:38 +0200111 case 'e':
112 gprs_bssgp_exit_on_destroy();
113 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200114 default:
115 fprintf(stderr, "Unknown option '%c'\n", c);
116 exit(0);
117 break;
118 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400119 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400120}
121
Andreas Eversberge266bd42012-07-13 14:00:21 +0200122void sighandler(int sigset)
123{
124 if (sigset == SIGHUP || sigset == SIGPIPE)
125 return;
126
127 fprintf(stderr, "Signal %d received.\n", sigset);
128
129 switch (sigset) {
130 case SIGINT:
Holger Hans Peter Freyther72075f02013-03-19 18:33:06 +0400131 case SIGTERM:
Andreas Eversberge266bd42012-07-13 14:00:21 +0200132 /* If another signal is received afterwards, the program
133 * is terminated without finishing shutdown process.
134 */
135 signal(SIGINT, SIG_DFL);
136 signal(SIGHUP, SIG_DFL);
137 signal(SIGTERM, SIG_DFL);
138 signal(SIGPIPE, SIG_DFL);
139 signal(SIGABRT, SIG_DFL);
140 signal(SIGUSR1, SIG_DFL);
141 signal(SIGUSR2, SIG_DFL);
142
143 quit = 1;
144 break;
145 case SIGABRT:
146 /* in case of abort, we want to obtain a talloc report
147 * and then return to the caller, who will abort the process
148 */
149 case SIGUSR1:
150 case SIGUSR2:
151 talloc_report_full(tall_pcu_ctx, stderr);
152 break;
153 }
154}
155
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400156int main(int argc, char *argv[])
157{
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200158 struct sched_param param;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200159 struct gprs_rlcmac_bts *bts;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200160 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200161
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200162 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
163 if (!tall_pcu_ctx)
164 return -ENOMEM;
Andreas Eversberg7a5a67a2013-01-16 08:43:24 +0100165 bv_tall_ctx = tall_pcu_ctx;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200166
Holger Hans Peter Freytherb6acfda2013-10-17 19:41:11 +0200167 bts = bts_main_data();
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200168 bts->fc_interval = 1;
Andreas Eversberg499ff412012-10-03 14:21:36 +0200169 bts->initial_cs_dl = bts->initial_cs_ul = 1;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200170 bts->cs1 = 1;
171 bts->t3142 = 20;
172 bts->t3169 = 5;
173 bts->t3191 = 5;
174 bts->t3193_msec = 100;
175 bts->t3195 = 5;
176 bts->n3101 = 10;
177 bts->n3103 = 4;
178 bts->n3105 = 8;
Andreas Eversberg0b874b62013-03-16 15:56:01 +0100179 bts->alpha = 0; /* a = 0.0 */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200180
Andreas Eversberge266bd42012-07-13 14:00:21 +0200181 msgb_set_talloc_ctx(tall_pcu_ctx);
182
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400183 osmo_init_logging(&gprs_log_info);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400184
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200185 vty_init(&pcu_vty_info);
186 pcu_vty_init(&gprs_log_info);
187
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200188 handle_options(argc, argv);
189 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
190 fprintf(stderr, "--mcc and --mnc must be specified "
191 "together.\n");
192 exit(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400193 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200194
195 rc = vty_read_config_file(config_file, NULL);
196 if (rc < 0 && config_given) {
197 fprintf(stderr, "Failed to parse the config file: '%s'\n",
198 config_file);
199 exit(1);
200 }
201 if (rc < 0)
202 fprintf(stderr, "No config file: '%s' Using default config.\n",
203 config_file);
204
205 rc = telnet_init(tall_pcu_ctx, NULL, 4240);
206 if (rc < 0) {
207 fprintf(stderr, "Error initializing telnet\n");
208 exit(1);
209 }
210
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200211 if (!bts->alloc_algorithm)
Andreas Eversberg53f47252012-07-15 07:10:10 +0200212 bts->alloc_algorithm = alloc_algorithm_b;
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200213
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200214 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400215
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200216 if (rc < 0)
217 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400218
Andreas Eversberge266bd42012-07-13 14:00:21 +0200219 signal(SIGINT, sighandler);
220 signal(SIGHUP, sighandler);
221 signal(SIGTERM, sighandler);
222 signal(SIGPIPE, sighandler);
223 signal(SIGABRT, sighandler);
224 signal(SIGUSR1, sighandler);
225 signal(SIGUSR2, sighandler);
226
Andreas Eversberg783aa4b2013-05-05 09:15:11 +0200227 /* enable realtime priority for us */
228 if (rt_prio != -1) {
229 memset(&param, 0, sizeof(param));
230 param.sched_priority = rt_prio;
231 rc = sched_setscheduler(getpid(), SCHED_RR, &param);
232 if (rc != 0) {
233 fprintf(stderr, "Setting SCHED_RR priority(%d) failed: %s\n",
234 param.sched_priority, strerror(errno));
235 exit(1);
236 }
237 }
238
Andreas Eversberge266bd42012-07-13 14:00:21 +0200239 while (!quit) {
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400240 osmo_gsm_timers_check();
241 osmo_gsm_timers_prepare();
242 osmo_gsm_timers_update();
243
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400244 osmo_select_main(0);
Andreas Eversbergb83e2a72012-10-07 15:26:00 +0200245#ifdef DEBUG_DIAGRAM
246 gettimeofday(&diagram_time, NULL);
247#endif
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400248 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400249
Andreas Eversberge266bd42012-07-13 14:00:21 +0200250 telnet_exit();
251
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200252 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200253
Holger Hans Peter Freyther111614a2013-10-19 20:04:57 +0200254 bts->bts->timing_advance()->flush();
Andreas Eversberga004e6a2013-05-13 16:45:21 +0200255
Andreas Eversberge266bd42012-07-13 14:00:21 +0200256 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200257 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200258
259 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400260}