Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* 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 Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 21 | #include <arpa/inet.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 22 | #include <pcu_l1_if.h> |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 23 | #include <gprs_rlcmac.h> |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 24 | #include <gsm_timer.h> |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 25 | #include <gprs_debug.h> |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame^] | 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 28 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 29 | struct gprs_rlcmac_bts *gprs_rlcmac_bts; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame^] | 30 | uint16_t spoof_mcc = 0, spoof_mnc = 0; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 31 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 32 | // 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 Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 36 | |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame^] | 37 | static 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 */ |
| 49 | static 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 Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 84 | int 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 Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 93 | LOGP(DPCU, LOGL_ERROR, "RLCMAC: Unknown event %u from NS\n", event); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 94 | if (msg) |
| 95 | talloc_free(msg); |
| 96 | rc = -EIO; |
| 97 | break; |
| 98 | } |
| 99 | return rc; |
| 100 | } |
| 101 | |
| 102 | int main(int argc, char *argv[]) |
| 103 | { |
| 104 | uint16_t nsvci = NSVCI; |
| 105 | struct gprs_ns_inst *sgsn_nsi; |
| 106 | struct gprs_nsvc *nsvc; |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 107 | struct gprs_rlcmac_bts *bts; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 108 | |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 109 | bts = gprs_rlcmac_bts = talloc_zero(NULL, struct gprs_rlcmac_bts); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 110 | if (!gprs_rlcmac_bts) |
| 111 | return -ENOMEM; |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 112 | gprs_rlcmac_bts->initial_cs = 1; |
Andreas Eversberg | dfa563c | 2012-07-06 08:13:59 +0200 | [diff] [blame] | 113 | 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 Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 123 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 124 | osmo_init_logging(&gprs_log_info); |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame^] | 125 | |
| 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 Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 133 | pcu_l1if_open(); |
| 134 | |
Harald Welte | d679009 | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 135 | sgsn_nsi = gprs_ns_instantiate(&sgsn_ns_cb, NULL); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 136 | bssgp_nsi = sgsn_nsi; |
| 137 | |
| 138 | if (!bssgp_nsi) |
| 139 | { |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 140 | LOGP(DPCU, LOGL_ERROR, "Unable to instantiate NS\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 141 | exit(1); |
| 142 | } |
| 143 | bctx = btsctx_alloc(BVCI, NSEI); |
| 144 | bctx->cell_id = CELL_ID; |
| 145 | bctx->nsei = NSEI; |
Andreas Eversberg | 81e895b | 2012-07-06 08:24:53 +0200 | [diff] [blame^] | 146 | bctx->ra_id.mnc = spoof_mcc ? : MNC; |
| 147 | bctx->ra_id.mcc = spoof_mnc ? : MCC; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 148 | 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 Welte | d679009 | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 159 | nsvc = gprs_ns_nsip_connect(sgsn_nsi, &dest, NSEI, nsvci); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 160 | unsigned i = 0; |
| 161 | while (1) |
| 162 | { |
Ivan Kluchnikov | 61a33f7 | 2012-04-12 15:22:06 +0400 | [diff] [blame] | 163 | osmo_gsm_timers_check(); |
| 164 | osmo_gsm_timers_prepare(); |
| 165 | osmo_gsm_timers_update(); |
| 166 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 167 | osmo_select_main(0); |
| 168 | if (i == 7) |
| 169 | { |
| 170 | bssgp_tx_bvc_reset(bctx, BVCI, cause); |
| 171 | } |
| 172 | i++; |
| 173 | } |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 174 | |
| 175 | talloc_free(gprs_rlcmac_bts); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 176 | } |
Ivan Kluchnikov | e3a0596 | 2012-03-18 15:48:51 +0400 | [diff] [blame] | 177 | |