Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 1 | /* sysmo_l1_if.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu> |
| 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 <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <errno.h> |
| 25 | #include <assert.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/un.h> |
| 28 | extern "C" { |
| 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/core/select.h> |
| 31 | #include <osmocom/core/msgb.h> |
| 32 | } |
| 33 | |
| 34 | #include <gprs_rlcmac.h> |
| 35 | #include <pcu_l1_if.h> |
| 36 | #include <gprs_debug.h> |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 37 | #include <gprs_bssgp_pcu.h> |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 38 | #include "../../osmo-bts/include/osmo-bts/pcuif_proto.h" |
| 39 | |
| 40 | static int pcu_sock_send(struct msgb *msg); |
| 41 | static void pcu_sock_timeout(void *_priv); |
| 42 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 43 | // Variable for storage current FN. |
| 44 | int frame_number; |
| 45 | |
| 46 | int get_current_fn() |
| 47 | { |
| 48 | return frame_number; |
| 49 | } |
| 50 | |
| 51 | void set_current_fn(int fn) |
| 52 | { |
| 53 | frame_number = fn; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * PCU messages |
| 58 | */ |
| 59 | |
| 60 | struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr) |
| 61 | { |
| 62 | struct msgb *msg; |
| 63 | struct gsm_pcu_if *pcu_prim; |
| 64 | |
| 65 | msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx"); |
| 66 | if (!msg) |
| 67 | return NULL; |
| 68 | msgb_put(msg, sizeof(struct gsm_pcu_if)); |
| 69 | pcu_prim = (struct gsm_pcu_if *) msg->data; |
| 70 | pcu_prim->msg_type = msg_type; |
| 71 | pcu_prim->bts_nr = bts_nr; |
| 72 | |
| 73 | return msg; |
| 74 | } |
| 75 | |
| 76 | static int pcu_tx_act_req(uint8_t trx, uint8_t ts, uint8_t activate) |
| 77 | { |
| 78 | struct msgb *msg; |
| 79 | struct gsm_pcu_if *pcu_prim; |
| 80 | struct gsm_pcu_if_act_req *act_req; |
| 81 | |
| 82 | LOGP(DL1IF, LOGL_INFO, "Sending %s request: trx=%d ts=%d\n", |
| 83 | (activate) ? "activate" : "deactivate", trx, ts); |
| 84 | |
| 85 | msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, 0); |
| 86 | if (!msg) |
| 87 | return -ENOMEM; |
| 88 | pcu_prim = (struct gsm_pcu_if *) msg->data; |
| 89 | act_req = &pcu_prim->u.act_req; |
| 90 | act_req->activate = activate; |
| 91 | act_req->trx_nr = trx; |
| 92 | act_req->ts_nr = ts; |
| 93 | |
| 94 | return pcu_sock_send(msg); |
| 95 | } |
| 96 | |
| 97 | static int pcu_tx_data_req(uint8_t trx, uint8_t ts, uint8_t sapi, |
| 98 | uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data, |
| 99 | uint8_t len) |
| 100 | { |
| 101 | struct msgb *msg; |
| 102 | struct gsm_pcu_if *pcu_prim; |
| 103 | struct gsm_pcu_if_data *data_req; |
| 104 | |
| 105 | LOGP(DL1IF, LOGL_DEBUG, "Sending data request: trx=%d ts=%d sapi=%d " |
| 106 | "arfcn=%d fn=%d block=%d data=%s\n", trx, ts, sapi, arfcn, fn, |
| 107 | block_nr, osmo_hexdump(data, len)); |
| 108 | |
| 109 | msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, 0); |
| 110 | if (!msg) |
| 111 | return -ENOMEM; |
| 112 | pcu_prim = (struct gsm_pcu_if *) msg->data; |
| 113 | data_req = &pcu_prim->u.data_req; |
| 114 | |
| 115 | data_req->sapi = sapi; |
| 116 | data_req->fn = fn; |
| 117 | data_req->arfcn = arfcn; |
| 118 | data_req->trx_nr = trx; |
| 119 | data_req->ts_nr = ts; |
| 120 | data_req->block_nr = block_nr; |
| 121 | memcpy(data_req->data, data, len); |
| 122 | data_req->len = len; |
| 123 | |
| 124 | return pcu_sock_send(msg); |
| 125 | } |
| 126 | |
| 127 | void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn, |
| 128 | uint32_t fn, uint8_t block_nr) |
| 129 | { |
| 130 | pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr, |
| 131 | msg->data, msg->len); |
| 132 | msgb_free(msg); |
| 133 | } |
| 134 | |
| 135 | void pcu_l1if_tx_ptcch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn, |
| 136 | uint32_t fn, uint8_t block_nr) |
| 137 | { |
| 138 | pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, |
| 139 | msg->data, msg->len); |
| 140 | msgb_free(msg); |
| 141 | } |
| 142 | |
Andreas Eversberg | 7b04501 | 2012-07-05 07:38:49 +0200 | [diff] [blame^] | 143 | void pcu_l1if_tx_agch(bitvec * block, int plen) |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 144 | { |
Andreas Eversberg | 7b04501 | 2012-07-05 07:38:49 +0200 | [diff] [blame^] | 145 | uint8_t data[23]; /* prefix PLEN */ |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 146 | |
| 147 | /* FIXME: why does OpenBTS has no PLEN and no fill in message? */ |
| 148 | bitvec_pack(block, data + 1); |
Andreas Eversberg | 7b04501 | 2012-07-05 07:38:49 +0200 | [diff] [blame^] | 149 | data[0] = (plen << 2) | 0x01; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 150 | pcu_tx_data_req(0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, 23); |
| 151 | } |
| 152 | |
| 153 | static void pcu_l1if_tx_bcch(uint8_t *data, int len) |
| 154 | { |
| 155 | pcu_tx_data_req(0, 0, PCU_IF_SAPI_BCCH, 0, 0, 0, data, len); |
| 156 | } |
| 157 | |
| 158 | static int pcu_rx_data_ind(struct gsm_pcu_if_data *data_ind) |
| 159 | { |
| 160 | int rc = 0; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 161 | |
| 162 | LOGP(DL1IF, LOGL_DEBUG, "Data indication received: sapi=%d arfcn=%d " |
| 163 | "block=%d data=%s\n", data_ind->sapi, |
| 164 | data_ind->arfcn, data_ind->block_nr, |
| 165 | osmo_hexdump(data_ind->data, data_ind->len)); |
| 166 | |
| 167 | switch (data_ind->sapi) { |
| 168 | case PCU_IF_SAPI_PDTCH: |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 169 | rc = gprs_rlcmac_rcv_block(data_ind->data, data_ind->len, |
| 170 | data_ind->fn); |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 171 | break; |
| 172 | default: |
| 173 | LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with " |
| 174 | "unsupported sapi %d\n", data_ind->sapi); |
| 175 | rc = -EINVAL; |
| 176 | } |
| 177 | |
| 178 | return rc; |
| 179 | } |
| 180 | |
| 181 | static int pcu_rx_rts_req(struct gsm_pcu_if_rts_req *rts_req) |
| 182 | { |
| 183 | int rc = 0; |
| 184 | |
| 185 | LOGP(DL1IF, LOGL_DEBUG, "RTS request received: trx=%d ts=%d sapi=%d " |
| 186 | "arfcn=%d fn=%d block=%d\n", rts_req->trx_nr, rts_req->ts_nr, |
| 187 | rts_req->sapi, rts_req->arfcn, rts_req->fn, rts_req->block_nr); |
| 188 | |
| 189 | switch (rts_req->sapi) { |
| 190 | case PCU_IF_SAPI_PDTCH: |
| 191 | gprs_rlcmac_rcv_rts_block(rts_req->trx_nr, rts_req->ts_nr, |
| 192 | rts_req->arfcn, rts_req->fn, rts_req->block_nr); |
| 193 | break; |
| 194 | case PCU_IF_SAPI_PTCCH: |
| 195 | /* FIXME */ |
| 196 | { |
| 197 | struct msgb *msg = msgb_alloc(23, "l1_prim"); |
| 198 | memset(msgb_put(msg, 23), 0x2b, 23); |
| 199 | pcu_l1if_tx_ptcch(msg, rts_req->trx_nr, rts_req->ts_nr, |
| 200 | rts_req->arfcn, rts_req->fn, rts_req->block_nr); |
| 201 | } |
| 202 | break; |
| 203 | default: |
| 204 | LOGP(DL1IF, LOGL_ERROR, "Received PCU RTS request with " |
| 205 | "unsupported sapi %d\n", rts_req->sapi); |
| 206 | rc = -EINVAL; |
| 207 | } |
| 208 | |
| 209 | return rc; |
| 210 | } |
| 211 | |
| 212 | static int pcu_rx_rach_ind(struct gsm_pcu_if_rach_ind *rach_ind) |
| 213 | { |
| 214 | int rc = 0; |
| 215 | |
| 216 | LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d " |
| 217 | "qta=%d, ra=%d, fn=%d\n", rach_ind->sapi, rach_ind->qta, |
| 218 | rach_ind->ra, rach_ind->fn); |
| 219 | |
| 220 | switch (rach_ind->sapi) { |
| 221 | case PCU_IF_SAPI_RACH: |
| 222 | rc = gprs_rlcmac_rcv_rach(rach_ind->ra, rach_ind->fn, |
| 223 | rach_ind->qta); |
| 224 | break; |
| 225 | default: |
| 226 | LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with " |
| 227 | "unsupported sapi %d\n", rach_ind->sapi); |
| 228 | rc = -EINVAL; |
| 229 | } |
| 230 | |
| 231 | return rc; |
| 232 | } |
| 233 | |
| 234 | static int pcu_rx_info_ind(struct gsm_pcu_if_info_ind *info_ind) |
| 235 | { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 236 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 237 | int rc = 0; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 238 | int trx, ts, tfi; |
| 239 | struct gprs_rlcmac_tbf *tbf; |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 240 | int i; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 241 | |
| 242 | LOGP(DL1IF, LOGL_INFO, "Info indication received:\n"); |
| 243 | |
| 244 | if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) { |
| 245 | LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n"); |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 246 | bssgp_failed: |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 247 | /* free all TBF */ |
| 248 | for (trx = 0; trx < 8; trx++) { |
| 249 | bts->trx[trx].arfcn = info_ind->trx[trx].arfcn; |
| 250 | for (ts = 0; ts < 8; ts++) { |
| 251 | for (tfi = 0; tfi < 32; tfi++) { |
| 252 | tbf = bts->trx[trx].pdch[ts].tbf[tfi]; |
| 253 | if (tbf) |
| 254 | tbf_free(tbf); |
| 255 | } |
| 256 | } |
| 257 | } |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 258 | gprs_bssgp_destroy(); |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | LOGP(DL1IF, LOGL_INFO, "BTS available\n"); |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 262 | LOGP(DL1IF, LOGL_INFO, " mcc=%d\n", info_ind->mcc); |
| 263 | LOGP(DL1IF, LOGL_INFO, " mnc=%d\n", info_ind->mnc); |
| 264 | LOGP(DL1IF, LOGL_INFO, " lac=%d\n", info_ind->lac); |
| 265 | LOGP(DL1IF, LOGL_INFO, " rac=%d\n", info_ind->rac); |
| 266 | LOGP(DL1IF, LOGL_INFO, " cell_id=%d\n", info_ind->cell_id); |
| 267 | LOGP(DL1IF, LOGL_INFO, " nsei=%d\n", info_ind->nsei); |
| 268 | LOGP(DL1IF, LOGL_INFO, " nse_timer=%d %d %d %d %d %d %d\n", |
| 269 | info_ind->nse_timer[0], info_ind->nse_timer[1], |
| 270 | info_ind->nse_timer[2], info_ind->nse_timer[3], |
| 271 | info_ind->nse_timer[4], info_ind->nse_timer[5], |
| 272 | info_ind->nse_timer[6]); |
| 273 | LOGP(DL1IF, LOGL_INFO, " cell_timer=%d %d %d %d %d %d %d %d %d %d %d\n", |
| 274 | info_ind->cell_timer[0], info_ind->cell_timer[1], |
| 275 | info_ind->cell_timer[2], info_ind->cell_timer[3], |
| 276 | info_ind->cell_timer[4], info_ind->cell_timer[5], |
| 277 | info_ind->cell_timer[6], info_ind->cell_timer[7], |
| 278 | info_ind->cell_timer[8], info_ind->cell_timer[9], |
| 279 | info_ind->cell_timer[10]); |
| 280 | LOGP(DL1IF, LOGL_INFO, " repeat_time=%d\n", info_ind->repeat_time); |
| 281 | LOGP(DL1IF, LOGL_INFO, " repeat_count=%d\n", info_ind->repeat_count); |
| 282 | LOGP(DL1IF, LOGL_INFO, " bvci=%d\n", info_ind->bvci); |
| 283 | LOGP(DL1IF, LOGL_INFO, " t3142=%d\n", info_ind->t3142); |
| 284 | LOGP(DL1IF, LOGL_INFO, " t3169=%d\n", info_ind->t3169); |
| 285 | LOGP(DL1IF, LOGL_INFO, " t3191=%d\n", info_ind->t3191); |
| 286 | LOGP(DL1IF, LOGL_INFO, " t3193=%d (ms)\n", info_ind->t3193_10ms * 10); |
| 287 | LOGP(DL1IF, LOGL_INFO, " t3195=%d\n", info_ind->t3195); |
| 288 | LOGP(DL1IF, LOGL_INFO, " n3101=%d\n", info_ind->n3101); |
| 289 | LOGP(DL1IF, LOGL_INFO, " n3103=%d\n", info_ind->n3103); |
| 290 | LOGP(DL1IF, LOGL_INFO, " n3105=%d\n", info_ind->n3105); |
| 291 | LOGP(DL1IF, LOGL_INFO, " cv_countdown=%d\n", info_ind->cv_countdown); |
| 292 | LOGP(DL1IF, LOGL_INFO, " dl_tbf_ext=%d\n", info_ind->dl_tbf_ext); |
| 293 | LOGP(DL1IF, LOGL_INFO, " ul_tbf_ext=%d\n", info_ind->ul_tbf_ext); |
| 294 | for (i = 0; i < 4; i++) { |
| 295 | if ((info_ind->flags & (PCU_IF_FLAG_CS1 << i))) |
| 296 | LOGP(DL1IF, LOGL_INFO, " Use CS%d\n", i+1); |
| 297 | } |
| 298 | for (i = 0; i < 9; i++) { |
| 299 | if ((info_ind->flags & (PCU_IF_FLAG_MCS1 << i))) |
| 300 | LOGP(DL1IF, LOGL_INFO, " Use MCS%d\n", i+1); |
| 301 | } |
| 302 | LOGP(DL1IF, LOGL_INFO, " initial_cs=%d\n", info_ind->initial_cs); |
| 303 | LOGP(DL1IF, LOGL_INFO, " initial_mcs=%d\n", info_ind->initial_mcs); |
| 304 | LOGP(DL1IF, LOGL_INFO, " nsvci=%d\n", info_ind->nsvci[0]); |
| 305 | LOGP(DL1IF, LOGL_INFO, " local_port=%d\n", info_ind->local_port[0]); |
| 306 | LOGP(DL1IF, LOGL_INFO, " remote_port=%d\n", info_ind->remote_port[0]); |
| 307 | LOGP(DL1IF, LOGL_INFO, " remote_ip=%d\n", info_ind->remote_ip[0]); |
| 308 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 309 | rc = gprs_bssgp_create(info_ind->remote_ip[0], info_ind->remote_port[0], |
| 310 | info_ind->nsei, info_ind->nsvci[0], info_ind->bvci, |
| 311 | info_ind->mcc, info_ind->mnc, info_ind->lac, info_ind->rac, |
| 312 | info_ind->cell_id); |
| 313 | if (rc < 0) { |
| 314 | LOGP(DL1IF, LOGL_NOTICE, "SGSN not available\n"); |
| 315 | goto bssgp_failed; |
| 316 | } |
| 317 | |
Andreas Eversberg | b3c6f6c | 2012-07-06 07:40:08 +0200 | [diff] [blame] | 318 | bts->cs1 = !!(info_ind->flags & PCU_IF_FLAG_CS1); |
| 319 | bts->cs2 = !!(info_ind->flags & PCU_IF_FLAG_CS2); |
| 320 | bts->cs3 = !!(info_ind->flags & PCU_IF_FLAG_CS3); |
| 321 | bts->cs4 = !!(info_ind->flags & PCU_IF_FLAG_CS4); |
| 322 | if (!bts->cs1 && !bts->cs2 && !bts->cs3 && !bts->cs4) |
| 323 | bts->cs1 = 1; |
| 324 | if (info_ind->t3142) { /* if timer values are set */ |
| 325 | bts->t3142 = info_ind->t3142; |
| 326 | bts->t3169 = info_ind->t3169; |
| 327 | bts->t3191 = info_ind->t3191; |
| 328 | bts->t3193_msec = info_ind->t3193_10ms * 10; |
| 329 | bts->t3195 = info_ind->t3195; |
| 330 | bts->n3101 = info_ind->n3101; |
| 331 | bts->n3103 = info_ind->n3103; |
| 332 | bts->n3105 = info_ind->n3105; |
| 333 | } |
| 334 | if (info_ind->initial_cs < 1 || info_ind->initial_cs > 4) |
| 335 | bts->initial_cs = 1; |
| 336 | else |
| 337 | bts->initial_cs = info_ind->initial_cs; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 338 | |
| 339 | for (trx = 0; trx < 8; trx++) { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 340 | bts->trx[trx].arfcn = info_ind->trx[trx].arfcn; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 341 | for (ts = 0; ts < 8; ts++) { |
| 342 | if ((info_ind->trx[trx].pdch_mask & (1 << ts))) { |
| 343 | /* FIXME: activate dynamically at RLCMAC */ |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 344 | if (!bts->trx[trx].pdch[ts].enable) |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 345 | pcu_tx_act_req(trx, ts, 1); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 346 | bts->trx[trx].pdch[ts].enable = 1; |
| 347 | bts->trx[trx].pdch[ts].tsc = |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 348 | info_ind->trx[trx].tsc[ts]; |
| 349 | LOGP(DL1IF, LOGL_INFO, "PDCH: trx=%d ts=%d\n", |
| 350 | trx, ts); |
| 351 | } else { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 352 | if (bts->trx[trx].pdch[ts].enable) |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 353 | pcu_tx_act_req(trx, ts, 0); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 354 | bts->trx[trx].pdch[ts].enable = 0; |
| 355 | /* kick all tbf FIXME: multislot */ |
| 356 | for (tfi = 0; tfi < 32; tfi++) { |
| 357 | tbf = bts->trx[trx].pdch[ts].tbf[tfi]; |
| 358 | if (tbf) |
| 359 | tbf_free(tbf); |
| 360 | } |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 365 | return rc; |
| 366 | } |
| 367 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 368 | static int pcu_rx_time_ind(struct gsm_pcu_if_time_ind *time_ind) |
| 369 | { |
| 370 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
| 371 | int trx, ts, tfi; |
| 372 | struct gprs_rlcmac_tbf *tbf; |
| 373 | uint32_t elapsed; |
| 374 | uint8_t fn13 = time_ind->fn % 13; |
| 375 | |
| 376 | /* omit frame numbers not starting at a MAC block */ |
| 377 | if (fn13 != 0 && fn13 != 4 && fn13 != 8) |
| 378 | return 0; |
| 379 | |
| 380 | LOGP(DL1IF, LOGL_DEBUG, "Time indication received: %d\n", |
| 381 | time_ind->fn % 52); |
| 382 | |
| 383 | set_current_fn(time_ind->fn); |
| 384 | |
| 385 | /* check for poll timeout */ |
| 386 | for (trx = 0; trx < 8; trx++) { |
| 387 | for (ts = 0; ts < 8; ts++) { |
| 388 | for (tfi = 0; tfi < 32; tfi++) { |
| 389 | tbf = bts->trx[trx].pdch[ts].tbf[tfi]; |
| 390 | if (!tbf) |
| 391 | continue; |
| 392 | if (tbf->poll_state != GPRS_RLCMAC_POLL_SCHED) |
| 393 | continue; |
| 394 | elapsed = (frame_number - tbf->poll_fn) |
| 395 | % 2715648; |
| 396 | if (elapsed >= 20 && elapsed < 200) |
| 397 | gprs_rlcmac_poll_timeout(tbf); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 405 | static int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim) |
| 406 | { |
| 407 | int rc = 0; |
| 408 | |
| 409 | switch (msg_type) { |
| 410 | case PCU_IF_MSG_DATA_IND: |
| 411 | rc = pcu_rx_data_ind(&pcu_prim->u.data_ind); |
| 412 | break; |
| 413 | case PCU_IF_MSG_RTS_REQ: |
| 414 | rc = pcu_rx_rts_req(&pcu_prim->u.rts_req); |
| 415 | break; |
| 416 | case PCU_IF_MSG_RACH_IND: |
| 417 | rc = pcu_rx_rach_ind(&pcu_prim->u.rach_ind); |
| 418 | break; |
| 419 | case PCU_IF_MSG_INFO_IND: |
| 420 | rc = pcu_rx_info_ind(&pcu_prim->u.info_ind); |
| 421 | break; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 422 | case PCU_IF_MSG_TIME_IND: |
| 423 | rc = pcu_rx_time_ind(&pcu_prim->u.time_ind); |
| 424 | break; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 425 | default: |
| 426 | LOGP(DL1IF, LOGL_ERROR, "Received unknwon PCU msg type %d\n", |
| 427 | msg_type); |
| 428 | rc = -EINVAL; |
| 429 | } |
| 430 | |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * SYSMO-PCU socket functions |
| 436 | */ |
| 437 | |
| 438 | struct pcu_sock_state { |
| 439 | struct osmo_fd conn_bfd; /* fd for connection to lcr */ |
| 440 | struct osmo_timer_list timer; /* socket connect retry timer */ |
| 441 | struct llist_head upqueue; /* queue for sending messages */ |
| 442 | } *pcu_sock_state = NULL; |
| 443 | |
| 444 | static int pcu_sock_send(struct msgb *msg) |
| 445 | { |
| 446 | struct pcu_sock_state *state = pcu_sock_state; |
| 447 | struct osmo_fd *conn_bfd; |
| 448 | |
| 449 | if (!state) { |
| 450 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping " |
| 451 | "message\n"); |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | conn_bfd = &state->conn_bfd; |
| 455 | if (conn_bfd->fd <= 0) { |
| 456 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping " |
| 457 | "message\n"); |
| 458 | return -EIO; |
| 459 | } |
| 460 | msgb_enqueue(&state->upqueue, msg); |
| 461 | conn_bfd->when |= BSC_FD_WRITE; |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static void pcu_sock_close(struct pcu_sock_state *state) |
| 467 | { |
| 468 | struct osmo_fd *bfd = &state->conn_bfd; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 469 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
| 470 | struct gprs_rlcmac_tbf *tbf; |
| 471 | uint8_t trx, ts, tfi; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 472 | |
| 473 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket has LOST connection\n"); |
| 474 | |
| 475 | close(bfd->fd); |
| 476 | bfd->fd = -1; |
| 477 | osmo_fd_unregister(bfd); |
| 478 | |
| 479 | /* flush the queue */ |
| 480 | while (!llist_empty(&state->upqueue)) { |
| 481 | struct msgb *msg = msgb_dequeue(&state->upqueue); |
| 482 | msgb_free(msg); |
| 483 | } |
| 484 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 485 | /* disable all slots, kick all TBFs */ |
| 486 | for (trx = 0; trx < 8; trx++) { |
| 487 | for (ts = 0; ts < 8; ts++) { |
| 488 | bts->trx[trx].pdch[ts].enable = 0; |
| 489 | for (tfi = 0; tfi < 32; tfi++) { |
| 490 | tbf = bts->trx[trx].pdch[ts].tbf[tfi]; |
| 491 | if (tbf) |
| 492 | tbf_free(tbf); |
| 493 | } |
| 494 | } |
| 495 | } |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 496 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 497 | gprs_bssgp_destroy(); |
| 498 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 499 | state->timer.cb = pcu_sock_timeout; |
| 500 | osmo_timer_schedule(&state->timer, 5, 0); |
| 501 | } |
| 502 | |
| 503 | static int pcu_sock_read(struct osmo_fd *bfd) |
| 504 | { |
| 505 | struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data; |
| 506 | struct gsm_pcu_if *pcu_prim; |
| 507 | struct msgb *msg; |
| 508 | int rc; |
| 509 | |
| 510 | msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx"); |
| 511 | if (!msg) |
| 512 | return -ENOMEM; |
| 513 | |
| 514 | pcu_prim = (struct gsm_pcu_if *) msg->tail; |
| 515 | |
| 516 | rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0); |
| 517 | if (rc == 0) |
| 518 | goto close; |
| 519 | |
| 520 | if (rc < 0) { |
| 521 | if (errno == EAGAIN) |
| 522 | return 0; |
| 523 | goto close; |
| 524 | } |
| 525 | |
| 526 | rc = pcu_rx(pcu_prim->msg_type, pcu_prim); |
| 527 | |
| 528 | /* as we always synchronously process the message in pcu_rx() and |
| 529 | * its callbacks, we can free the message here. */ |
| 530 | msgb_free(msg); |
| 531 | |
| 532 | return rc; |
| 533 | |
| 534 | close: |
| 535 | msgb_free(msg); |
| 536 | pcu_sock_close(state); |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | static int pcu_sock_write(struct osmo_fd *bfd) |
| 541 | { |
| 542 | struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data; |
| 543 | int rc; |
| 544 | |
| 545 | while (!llist_empty(&state->upqueue)) { |
| 546 | struct msgb *msg, *msg2; |
| 547 | struct gsm_pcu_if *pcu_prim; |
| 548 | |
| 549 | /* peek at the beginning of the queue */ |
| 550 | msg = llist_entry(state->upqueue.next, struct msgb, list); |
| 551 | pcu_prim = (struct gsm_pcu_if *)msg->data; |
| 552 | |
| 553 | bfd->when &= ~BSC_FD_WRITE; |
| 554 | |
| 555 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 556 | if (!msgb_length(msg)) { |
| 557 | LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO " |
| 558 | "bytes!\n", pcu_prim->msg_type); |
| 559 | goto dontsend; |
| 560 | } |
| 561 | |
| 562 | /* try to send it over the socket */ |
| 563 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 564 | if (rc == 0) |
| 565 | goto close; |
| 566 | if (rc < 0) { |
| 567 | if (errno == EAGAIN) { |
| 568 | bfd->when |= BSC_FD_WRITE; |
| 569 | break; |
| 570 | } |
| 571 | goto close; |
| 572 | } |
| 573 | |
| 574 | dontsend: |
| 575 | /* _after_ we send it, we can deueue */ |
| 576 | msg2 = msgb_dequeue(&state->upqueue); |
| 577 | assert(msg == msg2); |
| 578 | msgb_free(msg); |
| 579 | } |
| 580 | return 0; |
| 581 | |
| 582 | close: |
| 583 | pcu_sock_close(state); |
| 584 | |
| 585 | return -1; |
| 586 | } |
| 587 | |
| 588 | static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
| 589 | { |
| 590 | int rc = 0; |
| 591 | |
| 592 | if (flags & BSC_FD_READ) |
| 593 | rc = pcu_sock_read(bfd); |
| 594 | if (rc < 0) |
| 595 | return rc; |
| 596 | |
| 597 | if (flags & BSC_FD_WRITE) |
| 598 | rc = pcu_sock_write(bfd); |
| 599 | |
| 600 | return rc; |
| 601 | } |
| 602 | |
| 603 | int pcu_l1if_open(void) |
| 604 | { |
| 605 | struct pcu_sock_state *state; |
| 606 | struct osmo_fd *bfd; |
| 607 | struct sockaddr_un local; |
| 608 | unsigned int namelen; |
| 609 | int rc; |
| 610 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 611 | state = pcu_sock_state; |
| 612 | if (!state) { |
| 613 | state = talloc_zero(NULL, struct pcu_sock_state); |
| 614 | if (!state) |
| 615 | return -ENOMEM; |
| 616 | INIT_LLIST_HEAD(&state->upqueue); |
| 617 | } |
| 618 | |
| 619 | bfd = &state->conn_bfd; |
| 620 | |
| 621 | bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 622 | if (bfd->fd < 0) { |
| 623 | LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n"); |
| 624 | talloc_free(state); |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | local.sun_family = AF_UNIX; |
| 629 | strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path)); |
| 630 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 631 | |
| 632 | /* we use the same magic that X11 uses in Xtranssock.c for |
| 633 | * calculating the proper length of the sockaddr */ |
| 634 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 635 | local.sun_len = strlen(local.sun_path); |
| 636 | #endif |
| 637 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 638 | namelen = SUN_LEN(&local); |
| 639 | #else |
| 640 | namelen = strlen(local.sun_path) + |
| 641 | offsetof(struct sockaddr_un, sun_path); |
| 642 | #endif |
| 643 | rc = connect(bfd->fd, (struct sockaddr *) &local, namelen); |
| 644 | if (rc != 0) { |
| 645 | LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO " |
| 646 | "socket, delaying... '%s'\n", local.sun_path); |
| 647 | close(bfd->fd); |
| 648 | bfd->fd = -1; |
| 649 | state->timer.cb = pcu_sock_timeout; |
| 650 | osmo_timer_schedule(&state->timer, 5, 0); |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | bfd->when = BSC_FD_READ; |
| 655 | bfd->cb = pcu_sock_cb; |
| 656 | bfd->data = state; |
| 657 | |
| 658 | rc = osmo_fd_register(bfd); |
| 659 | if (rc < 0) { |
| 660 | LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc); |
| 661 | close(bfd->fd); |
| 662 | talloc_free(state); |
| 663 | return rc; |
| 664 | } |
| 665 | |
| 666 | LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n"); |
| 667 | |
| 668 | pcu_sock_state = state; |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | void pcu_l1if_close(void) |
| 674 | { |
| 675 | struct pcu_sock_state *state = pcu_sock_state; |
| 676 | struct osmo_fd *bfd; |
| 677 | |
| 678 | if (!state) |
| 679 | return; |
| 680 | |
| 681 | if (osmo_timer_pending(&state->timer)) |
| 682 | osmo_timer_del(&state->timer); |
| 683 | |
| 684 | bfd = &state->conn_bfd; |
| 685 | if (bfd->fd > 0) |
| 686 | pcu_sock_close(state); |
| 687 | talloc_free(state); |
| 688 | pcu_sock_state = NULL; |
| 689 | } |
| 690 | |
| 691 | static void pcu_sock_timeout(void *_priv) |
| 692 | { |
| 693 | pcu_l1if_open(); |
| 694 | } |
| 695 | |
| 696 | |
| 697 | |
| 698 | |