blob: 8ef5acc497549ad10ec96e60a11389d7e014b3ee [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>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040028
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020029struct gprs_rlcmac_bts *gprs_rlcmac_bts;
Andreas Eversberg81e895b2012-07-06 08:24:53 +020030uint16_t spoof_mcc = 0, spoof_mnc = 0;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +020031
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040032// TODO: We should move this parameters to config file.
33#define SGSN_IP "127.0.0.1"
34#define SGSN_PORT 23000
35#define NSVCI 4
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040036
Andreas Eversberg81e895b2012-07-06 08:24:53 +020037static void print_help()
38{
39 printf( "Some useful options:\n"
40 " -h --help this text\n"
41 " -m --mcc MCC use given MCC instead of value "
42 "provided by BTS\n"
43 " -n --mnc MNC use given MNC instead of value "
44 "provided by BTS\n"
45 );
46}
47
48/* FIXME: finally get some option parsing code into libosmocore */
49static void handle_options(int argc, char **argv)
50{
51 while (1) {
52 int option_idx = 0, c;
53 static const struct option long_options[] = {
54 { "help", 0, 0, 'h' },
55 { "mcc", 0, 0, 'm' },
56 { "mnc", 0, 0, 'n' },
57 { 0, 0, 0, 0 }
58 };
59
60 c = getopt_long(argc, argv, "hm:n:",
61 long_options, &option_idx);
62 if (c == -1)
63 break;
64
65 switch (c) {
66 case 'h':
67 print_help();
68 exit(0);
69 break;
70 case 'm':
71 spoof_mcc = atoi(optarg);
72 break;
73 case 'n':
74 spoof_mnc = atoi(optarg);
75 break;
76 default:
77 fprintf(stderr, "Unknown option '%c'\n", c);
78 exit(0);
79 break;
80 }
81 }
82}
83
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040084int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci)
85{
86 int rc = 0;
87 switch (event) {
88 case GPRS_NS_EVT_UNIT_DATA:
89 /* hand the message into the BSSGP implementation */
90 rc = gprs_bssgp_pcu_rcvmsg(msg);
91 break;
92 default:
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040093 LOGP(DPCU, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040094 if (msg)
95 talloc_free(msg);
96 rc = -EIO;
97 break;
98 }
99 return rc;
100}
101
102int main(int argc, char *argv[])
103{
104 uint16_t nsvci = NSVCI;
105 struct gprs_ns_inst *sgsn_nsi;
106 struct gprs_nsvc *nsvc;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200107 struct gprs_rlcmac_bts *bts;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200108
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200109 bts = gprs_rlcmac_bts = talloc_zero(NULL, struct gprs_rlcmac_bts);
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200110 if (!gprs_rlcmac_bts)
111 return -ENOMEM;
Andreas Eversbergb3c6f6c2012-07-06 07:40:08 +0200112 gprs_rlcmac_bts->initial_cs = 1;
Andreas Eversbergdfa563c2012-07-06 08:13:59 +0200113 bts->initial_cs = 1;
114 bts->cs1 = 1;
115 bts->t3142 = 20;
116 bts->t3169 = 5;
117 bts->t3191 = 5;
118 bts->t3193_msec = 100;
119 bts->t3195 = 5;
120 bts->n3101 = 10;
121 bts->n3103 = 4;
122 bts->n3105 = 8;
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200123
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400124 osmo_init_logging(&gprs_log_info);
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200125
126 handle_options(argc, argv);
127 if ((!!spoof_mcc) + (!!spoof_mnc) == 1) {
128 fprintf(stderr, "--mcc and --mnc must be specified "
129 "together.\n");
130 exit(0);
131 }
132
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400133 pcu_l1if_open();
134
Harald Welted6790092012-06-18 12:21:03 +0800135 sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, NULL);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400136 bssgp_nsi = sgsn_nsi;
137
138 if (!bssgp_nsi)
139 {
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +0400140 LOGP(DPCU, LOGL_ERROR, "Unable to instantiate NS\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400141 exit(1);
142 }
143 bctx = btsctx_alloc(BVCI, NSEI);
144 bctx->cell_id = CELL_ID;
145 bctx->nsei = NSEI;
Andreas Eversberg81e895b2012-07-06 08:24:53 +0200146 bctx->ra_id.mnc = spoof_mcc ? : MNC;
147 bctx->ra_id.mcc = spoof_mnc ? : MCC;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400148 bctx->ra_id.lac = PCU_LAC;
149 bctx->ra_id.rac = PCU_RAC;
150 bctx->bvci = BVCI;
151 uint8_t cause = 39;
152 gprs_ns_nsip_listen(sgsn_nsi);
153
154 struct sockaddr_in dest;
155 dest.sin_family = AF_INET;
156 dest.sin_port = htons(SGSN_PORT);
157 inet_aton(SGSN_IP, &dest.sin_addr);
158
Harald Welted6790092012-06-18 12:21:03 +0800159 nsvc = gprs_ns_nsip_connect(sgsn_nsi, &dest, NSEI, nsvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400160 unsigned i = 0;
161 while (1)
162 {
Ivan Kluchnikov61a33f72012-04-12 15:22:06 +0400163 osmo_gsm_timers_check();
164 osmo_gsm_timers_prepare();
165 osmo_gsm_timers_update();
166
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400167 osmo_select_main(0);
168 if (i == 7)
169 {
170 bssgp_tx_bvc_reset(bctx, BVCI, cause);
171 }
172 i++;
173 }
Andreas Eversberg5dac2f02012-06-27 15:52:04 +0200174
175 talloc_free(gprs_rlcmac_bts);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400176}
Ivan Kluchnikove3a05962012-03-18 15:48:51 +0400177