blob: 9fd0455990b2789ea0111e3ab5850f105544d161 [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 Eversberg81e895b2012-07-06 08:24:53 +020044static void print_help()
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040045{
Andreas Eversberg81e895b2012-07-06 08:24:53 +020046 printf( "Some useful options:\n"
47 " -h --help this text\n"
Andreas Eversbergebde64f2012-07-12 09:18:42 +020048 " -c --config-file Specify the filename of the config "
49 "file\n"
Andreas Eversberg81e895b2012-07-06 08:24:53 +020050 " -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 */
58static 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 Eversbergebde64f2012-07-12 09:18:42 +020064 { "config-file", 1, 0, 'c' },
Andreas Eversberg1944bd52012-07-06 09:32:39 +020065 { "mcc", 1, 0, 'm' },
66 { "mnc", 1, 0, 'n' },
Andreas Eversberg81e895b2012-07-06 08:24:53 +020067 { 0, 0, 0, 0 }
68 };
69
Andreas Eversbergebde64f2012-07-12 09:18:42 +020070 c = getopt_long(argc, argv, "hc:m:n:",
Andreas Eversberg81e895b2012-07-06 08:24:53 +020071 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 Eversbergebde64f2012-07-12 09:18:42 +020080 case 'c':
81 config_file = strdup(optarg);
82 config_given = 1;
83 break;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020084 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 Kluchnikov8ee60512012-03-05 19:24:57 +040095 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040096}
97
Andreas Eversberge266bd42012-07-13 14:00:21 +020098void 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 Kluchnikov8ee60512012-03-05 19:24:57 +0400131int main(int argc, char *argv[])
132{
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200133 struct gprs_rlcmac_bts *bts;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200134 int rc;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200135
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200136 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 Eversberg5dac2f02012-06-27 15:52:04 +0200142 if (!gprs_rlcmac_bts)
143 return -ENOMEM;
Andreas Eversbergb3c6f6c2012-07-06 07:40:08 +0200144 gprs_rlcmac_bts->initial_cs = 1;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200145 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 Eversberg5dac2f02012-06-27 15:52:04 +0200155
Andreas Eversberge266bd42012-07-13 14:00:21 +0200156 msgb_set_talloc_ctx(tall_pcu_ctx);
157
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400158 osmo_init_logging(&gprs_log_info);
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400159
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200160 vty_init(&pcu_vty_info);
161 pcu_vty_init(&gprs_log_info);
162
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200163 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 Kluchnikov8ee60512012-03-05 19:24:57 +0400168 }
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200169
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 Eversbergf298fa82012-07-13 14:50:57 +0200186 if (!bts->alloc_algorithm)
187 bts->alloc_algorithm = alloc_algorithm_a;
188
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200189 rc = pcu_l1if_open();
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400190
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200191 if (rc < 0)
192 return rc;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400193
Andreas Eversberge266bd42012-07-13 14:00:21 +0200194 signal(SIGINT, sighandler);
195 signal(SIGHUP, sighandler);
196 signal(SIGTERM, sighandler);
197 signal(SIGPIPE, sighandler);
198 signal(SIGABRT, sighandler);
199 signal(SIGUSR1, sighandler);
200 signal(SIGUSR2, sighandler);
201
202 while (!quit) {
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400203 osmo_gsm_timers_check();
204 osmo_gsm_timers_prepare();
205 osmo_gsm_timers_update();
206
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400207 osmo_select_main(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400208 }
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400209
Andreas Eversberge266bd42012-07-13 14:00:21 +0200210 telnet_exit();
211
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200212 pcu_l1if_close();
Andreas Eversberge266bd42012-07-13 14:00:21 +0200213
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200214 talloc_free(gprs_rlcmac_bts);
Andreas Eversberge266bd42012-07-13 14:00:21 +0200215
216 talloc_report_full(tall_pcu_ctx, stderr);
Andreas Eversbergebde64f2012-07-12 09:18:42 +0200217 talloc_free(tall_pcu_ctx);
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200218
219 return 0;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400220}