blob: 8265c31981f905d498eafef30437824b644ea48c [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 Eversberg7a5a67a2013-01-16 08:43:24 +010042extern void *bv_tall_ctx;
Andreas Eversberge266bd42012-07-13 14:00:21 +020043static int quit = 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040044
Andreas Eversbergb83e2a72012-10-07 15:26:00 +020045#ifdef DEBUG_DIAGRAM
46extern struct timeval diagram_time;
47#endif
48
Andreas Eversberg81e895b2012-07-06 08:24:53 +020049static void print_help()
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040050{
Andreas Eversberg81e895b2012-07-06 08:24:53 +020051 printf( "Some useful options:\n"
52 " -h --help this text\n"
Andreas Eversbergebde64f2012-07-12 09:18:42 +020053 " -c --config-file Specify the filename of the config "
54 "file\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020055 " -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 */
63static 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 Eversbergebde64f2012-07-12 09:18:42 +020069 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +020070 { "mcc", 1, 0, 'm' },
71 { "mnc", 1, 0, 'n' },
Harald Weltee5a09392013-01-17 12:28:16 +010072 { "version", 0, 0, 'V' },
Andreas Eversberg81e895b2012-07-06 08:24:53 +020073 { 0, 0, 0, 0 }
74 };
75
Harald Weltee5a09392013-01-17 12:28:16 +010076 c = getopt_long(argc, argv, "hc:m:n:V",
Andreas Eversberg81e895b2012-07-06 08:24:53 +020077 long_options, &option_idx);
78 if (c == -1)
79 break;
80
81 switch (c) {
82 case 'h':
83 print_help();
84 exit(0);
85 break;
Andreas Eversbergebde64f2012-07-12 09:18:42 +020086 case 'c':
87 config_file = strdup(optarg);
88 config_given = 1;
89 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020090 case 'm':
91 spoof_mcc = atoi(optarg);
92 break;
93 case 'n':
94 spoof_mnc = atoi(optarg);
95 break;
Harald Weltee5a09392013-01-17 12:28:16 +010096 case 'V':
97 print_version(1);
98 exit(0);
99 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200100 default:
101 fprintf(stderr, "Unknown option '%c'\n", c);
102 exit(0);
103 break;
104 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400105 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400106}
107
Andreas Eversberge266bd42012-07-13 14:00:21 +0200108void sighandler(int sigset)
109{
110 if (sigset == SIGHUP || sigset == SIGPIPE)
111 return;
112
113 fprintf(stderr, "Signal %d received.\n", sigset);
114
115 switch (sigset) {
116 case SIGINT:
117 /* If another signal is received afterwards, the program
118 * is terminated without finishing shutdown process.
119 */
120 signal(SIGINT, SIG_DFL);
121 signal(SIGHUP, SIG_DFL);
122 signal(SIGTERM, SIG_DFL);
123 signal(SIGPIPE, SIG_DFL);
124 signal(SIGABRT, SIG_DFL);
125 signal(SIGUSR1, SIG_DFL);
126 signal(SIGUSR2, SIG_DFL);
127
128 quit = 1;
129 break;
130 case SIGABRT:
131 /* in case of abort, we want to obtain a talloc report
132 * and then return to the caller, who will abort the process
133 */
134 case SIGUSR1:
135 case SIGUSR2:
136 talloc_report_full(tall_pcu_ctx, stderr);
137 break;
138 }
139}
140
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400141int main(int argc, char *argv[])
142{
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200143 struct gprs_rlcmac_bts *bts;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200144 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200145
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200146 tall_pcu_ctx = talloc_named_const(NULL, 1, "Osmo-PCU context");
147 if (!tall_pcu_ctx)
148 return -ENOMEM;
Andreas Eversberg7a5a67a2013-01-16 08:43:24 +0100149 bv_tall_ctx = tall_pcu_ctx;
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200150
151 bts = gprs_rlcmac_bts = talloc_zero(tall_pcu_ctx,
152 struct gprs_rlcmac_bts);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200153 if (!gprs_rlcmac_bts)
154 return -ENOMEM;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200155 bts->fc_interval = 1;
Andreas Eversberg499ff412012-10-03 14:21:36 +0200156 bts->initial_cs_dl = bts->initial_cs_ul = 1;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200157 bts->cs1 = 1;
158 bts->t3142 = 20;
159 bts->t3169 = 5;
160 bts->t3191 = 5;
161 bts->t3193_msec = 100;
162 bts->t3195 = 5;
163 bts->n3101 = 10;
164 bts->n3103 = 4;
165 bts->n3105 = 8;
Andreas Eversberg0b874b62013-03-16 15:56:01 +0100166 bts->alpha = 0; /* a = 0.0 */
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200167
Andreas Eversberge266bd42012-07-13 14:00:21 +0200168 msgb_set_talloc_ctx(tall_pcu_ctx);
169
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400170 osmo_init_logging(&gprs_log_info);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400171
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200172 vty_init(&pcu_vty_info);
173 pcu_vty_init(&gprs_log_info);
174
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200175 handle_options(argc, argv);
176 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
177 fprintf(stderr, "--mcc and --mnc must be specified "
178 "together.\n");
179 exit(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400180 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200181
182 rc = vty_read_config_file(config_file, NULL);
183 if (rc < 0 && config_given) {
184 fprintf(stderr, "Failed to parse the config file: '%s'\n",
185 config_file);
186 exit(1);
187 }
188 if (rc < 0)
189 fprintf(stderr, "No config file: '%s' Using default config.\n",
190 config_file);
191
192 rc = telnet_init(tall_pcu_ctx, NULL, 4240);
193 if (rc < 0) {
194 fprintf(stderr, "Error initializing telnet\n");
195 exit(1);
196 }
197
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200198 if (!bts->alloc_algorithm)
Andreas Eversberg53f47252012-07-15 07:10:10 +0200199 bts->alloc_algorithm = alloc_algorithm_b;
Andreas Eversbergf298fa82012-07-13 14:50:57 +0200200
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200201 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400202
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200203 if (rc < 0)
204 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400205
Andreas Eversberge266bd42012-07-13 14:00:21 +0200206 signal(SIGINT, sighandler);
207 signal(SIGHUP, sighandler);
208 signal(SIGTERM, sighandler);
209 signal(SIGPIPE, sighandler);
210 signal(SIGABRT, sighandler);
211 signal(SIGUSR1, sighandler);
212 signal(SIGUSR2, sighandler);
213
214 while (!quit) {
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400215 osmo_gsm_timers_check();
216 osmo_gsm_timers_prepare();
217 osmo_gsm_timers_update();
218
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400219 osmo_select_main(0);
Andreas Eversbergb83e2a72012-10-07 15:26:00 +0200220#ifdef DEBUG_DIAGRAM
221 gettimeofday(&diagram_time, NULL);
222#endif
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400223 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400224
Andreas Eversberge266bd42012-07-13 14:00:21 +0200225 telnet_exit();
226
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200227 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200228
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200229 talloc_free(gprs_rlcmac_bts);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200230
231 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200232 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200233
234 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400235}