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> |
| 37 | #include "../../osmo-bts/include/osmo-bts/pcuif_proto.h" |
| 38 | |
| 39 | static int pcu_sock_send(struct msgb *msg); |
| 40 | static void pcu_sock_timeout(void *_priv); |
| 41 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 42 | // Variable for storage current FN. |
| 43 | int frame_number; |
| 44 | |
| 45 | int get_current_fn() |
| 46 | { |
| 47 | return frame_number; |
| 48 | } |
| 49 | |
| 50 | void set_current_fn(int fn) |
| 51 | { |
| 52 | frame_number = fn; |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * PCU messages |
| 57 | */ |
| 58 | |
| 59 | struct msgb *pcu_msgb_alloc(uint8_t msg_type, uint8_t bts_nr) |
| 60 | { |
| 61 | struct msgb *msg; |
| 62 | struct gsm_pcu_if *pcu_prim; |
| 63 | |
| 64 | msg = msgb_alloc(sizeof(struct gsm_pcu_if), "pcu_sock_tx"); |
| 65 | if (!msg) |
| 66 | return NULL; |
| 67 | msgb_put(msg, sizeof(struct gsm_pcu_if)); |
| 68 | pcu_prim = (struct gsm_pcu_if *) msg->data; |
| 69 | pcu_prim->msg_type = msg_type; |
| 70 | pcu_prim->bts_nr = bts_nr; |
| 71 | |
| 72 | return msg; |
| 73 | } |
| 74 | |
| 75 | static int pcu_tx_act_req(uint8_t trx, uint8_t ts, uint8_t activate) |
| 76 | { |
| 77 | struct msgb *msg; |
| 78 | struct gsm_pcu_if *pcu_prim; |
| 79 | struct gsm_pcu_if_act_req *act_req; |
| 80 | |
| 81 | LOGP(DL1IF, LOGL_INFO, "Sending %s request: trx=%d ts=%d\n", |
| 82 | (activate) ? "activate" : "deactivate", trx, ts); |
| 83 | |
| 84 | msg = pcu_msgb_alloc(PCU_IF_MSG_ACT_REQ, 0); |
| 85 | if (!msg) |
| 86 | return -ENOMEM; |
| 87 | pcu_prim = (struct gsm_pcu_if *) msg->data; |
| 88 | act_req = &pcu_prim->u.act_req; |
| 89 | act_req->activate = activate; |
| 90 | act_req->trx_nr = trx; |
| 91 | act_req->ts_nr = ts; |
| 92 | |
| 93 | return pcu_sock_send(msg); |
| 94 | } |
| 95 | |
| 96 | static int pcu_tx_data_req(uint8_t trx, uint8_t ts, uint8_t sapi, |
| 97 | uint16_t arfcn, uint32_t fn, uint8_t block_nr, uint8_t *data, |
| 98 | uint8_t len) |
| 99 | { |
| 100 | struct msgb *msg; |
| 101 | struct gsm_pcu_if *pcu_prim; |
| 102 | struct gsm_pcu_if_data *data_req; |
| 103 | |
| 104 | LOGP(DL1IF, LOGL_DEBUG, "Sending data request: trx=%d ts=%d sapi=%d " |
| 105 | "arfcn=%d fn=%d block=%d data=%s\n", trx, ts, sapi, arfcn, fn, |
| 106 | block_nr, osmo_hexdump(data, len)); |
| 107 | |
| 108 | msg = pcu_msgb_alloc(PCU_IF_MSG_DATA_REQ, 0); |
| 109 | if (!msg) |
| 110 | return -ENOMEM; |
| 111 | pcu_prim = (struct gsm_pcu_if *) msg->data; |
| 112 | data_req = &pcu_prim->u.data_req; |
| 113 | |
| 114 | data_req->sapi = sapi; |
| 115 | data_req->fn = fn; |
| 116 | data_req->arfcn = arfcn; |
| 117 | data_req->trx_nr = trx; |
| 118 | data_req->ts_nr = ts; |
| 119 | data_req->block_nr = block_nr; |
| 120 | memcpy(data_req->data, data, len); |
| 121 | data_req->len = len; |
| 122 | |
| 123 | return pcu_sock_send(msg); |
| 124 | } |
| 125 | |
| 126 | void pcu_l1if_tx_pdtch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn, |
| 127 | uint32_t fn, uint8_t block_nr) |
| 128 | { |
| 129 | pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PDTCH, arfcn, fn, block_nr, |
| 130 | msg->data, msg->len); |
| 131 | msgb_free(msg); |
| 132 | } |
| 133 | |
| 134 | void pcu_l1if_tx_ptcch(msgb *msg, uint8_t trx, uint8_t ts, uint16_t arfcn, |
| 135 | uint32_t fn, uint8_t block_nr) |
| 136 | { |
| 137 | pcu_tx_data_req(trx, ts, PCU_IF_SAPI_PTCCH, arfcn, fn, block_nr, |
| 138 | msg->data, msg->len); |
| 139 | msgb_free(msg); |
| 140 | } |
| 141 | |
| 142 | void pcu_l1if_tx_agch(bitvec * block, int len) |
| 143 | { |
| 144 | uint8_t data[24]; /* prefix PLEN */ |
| 145 | |
| 146 | /* FIXME: why does OpenBTS has no PLEN and no fill in message? */ |
| 147 | bitvec_pack(block, data + 1); |
| 148 | data[0] = (len << 2) | 0x01; |
| 149 | pcu_tx_data_req(0, 0, PCU_IF_SAPI_AGCH, 0, 0, 0, data, 23); |
| 150 | } |
| 151 | |
| 152 | static void pcu_l1if_tx_bcch(uint8_t *data, int len) |
| 153 | { |
| 154 | pcu_tx_data_req(0, 0, PCU_IF_SAPI_BCCH, 0, 0, 0, data, len); |
| 155 | } |
| 156 | |
| 157 | static int pcu_rx_data_ind(struct gsm_pcu_if_data *data_ind) |
| 158 | { |
| 159 | int rc = 0; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 160 | |
| 161 | LOGP(DL1IF, LOGL_DEBUG, "Data indication received: sapi=%d arfcn=%d " |
| 162 | "block=%d data=%s\n", data_ind->sapi, |
| 163 | data_ind->arfcn, data_ind->block_nr, |
| 164 | osmo_hexdump(data_ind->data, data_ind->len)); |
| 165 | |
| 166 | switch (data_ind->sapi) { |
| 167 | case PCU_IF_SAPI_PDTCH: |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 168 | rc = gprs_rlcmac_rcv_block(data_ind->data, data_ind->len, |
| 169 | data_ind->fn); |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 170 | break; |
| 171 | default: |
| 172 | LOGP(DL1IF, LOGL_ERROR, "Received PCU data indication with " |
| 173 | "unsupported sapi %d\n", data_ind->sapi); |
| 174 | rc = -EINVAL; |
| 175 | } |
| 176 | |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | static int pcu_rx_rts_req(struct gsm_pcu_if_rts_req *rts_req) |
| 181 | { |
| 182 | int rc = 0; |
| 183 | |
| 184 | LOGP(DL1IF, LOGL_DEBUG, "RTS request received: trx=%d ts=%d sapi=%d " |
| 185 | "arfcn=%d fn=%d block=%d\n", rts_req->trx_nr, rts_req->ts_nr, |
| 186 | rts_req->sapi, rts_req->arfcn, rts_req->fn, rts_req->block_nr); |
| 187 | |
| 188 | switch (rts_req->sapi) { |
| 189 | case PCU_IF_SAPI_PDTCH: |
| 190 | gprs_rlcmac_rcv_rts_block(rts_req->trx_nr, rts_req->ts_nr, |
| 191 | rts_req->arfcn, rts_req->fn, rts_req->block_nr); |
| 192 | break; |
| 193 | case PCU_IF_SAPI_PTCCH: |
| 194 | /* FIXME */ |
| 195 | { |
| 196 | struct msgb *msg = msgb_alloc(23, "l1_prim"); |
| 197 | memset(msgb_put(msg, 23), 0x2b, 23); |
| 198 | pcu_l1if_tx_ptcch(msg, rts_req->trx_nr, rts_req->ts_nr, |
| 199 | rts_req->arfcn, rts_req->fn, rts_req->block_nr); |
| 200 | } |
| 201 | break; |
| 202 | default: |
| 203 | LOGP(DL1IF, LOGL_ERROR, "Received PCU RTS request with " |
| 204 | "unsupported sapi %d\n", rts_req->sapi); |
| 205 | rc = -EINVAL; |
| 206 | } |
| 207 | |
| 208 | return rc; |
| 209 | } |
| 210 | |
| 211 | static int pcu_rx_rach_ind(struct gsm_pcu_if_rach_ind *rach_ind) |
| 212 | { |
| 213 | int rc = 0; |
| 214 | |
| 215 | LOGP(DL1IF, LOGL_INFO, "RACH request received: sapi=%d " |
| 216 | "qta=%d, ra=%d, fn=%d\n", rach_ind->sapi, rach_ind->qta, |
| 217 | rach_ind->ra, rach_ind->fn); |
| 218 | |
| 219 | switch (rach_ind->sapi) { |
| 220 | case PCU_IF_SAPI_RACH: |
| 221 | rc = gprs_rlcmac_rcv_rach(rach_ind->ra, rach_ind->fn, |
| 222 | rach_ind->qta); |
| 223 | break; |
| 224 | default: |
| 225 | LOGP(DL1IF, LOGL_ERROR, "Received PCU rach request with " |
| 226 | "unsupported sapi %d\n", rach_ind->sapi); |
| 227 | rc = -EINVAL; |
| 228 | } |
| 229 | |
| 230 | return rc; |
| 231 | } |
| 232 | |
| 233 | static int pcu_rx_info_ind(struct gsm_pcu_if_info_ind *info_ind) |
| 234 | { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 235 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 236 | int rc = 0; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 237 | int trx, ts, tfi; |
| 238 | struct gprs_rlcmac_tbf *tbf; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 239 | // uint8_t si13[23]; |
| 240 | |
| 241 | LOGP(DL1IF, LOGL_INFO, "Info indication received:\n"); |
| 242 | |
| 243 | if (!(info_ind->flags & PCU_IF_FLAG_ACTIVE)) { |
| 244 | LOGP(DL1IF, LOGL_NOTICE, "BTS not available\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | LOGP(DL1IF, LOGL_INFO, "BTS available\n"); |
| 248 | |
| 249 | for (trx = 0; trx < 8; trx++) { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 250 | bts->trx[trx].arfcn = info_ind->trx[trx].arfcn; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 251 | for (ts = 0; ts < 8; ts++) { |
| 252 | if ((info_ind->trx[trx].pdch_mask & (1 << ts))) { |
| 253 | /* FIXME: activate dynamically at RLCMAC */ |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 254 | if (!bts->trx[trx].pdch[ts].enable) |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 255 | pcu_tx_act_req(trx, ts, 1); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 256 | bts->trx[trx].pdch[ts].enable = 1; |
| 257 | bts->trx[trx].pdch[ts].tsc = |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 258 | info_ind->trx[trx].tsc[ts]; |
| 259 | LOGP(DL1IF, LOGL_INFO, "PDCH: trx=%d ts=%d\n", |
| 260 | trx, ts); |
| 261 | } else { |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 262 | if (bts->trx[trx].pdch[ts].enable) |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 263 | pcu_tx_act_req(trx, ts, 0); |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 264 | bts->trx[trx].pdch[ts].enable = 0; |
| 265 | /* kick all tbf FIXME: multislot */ |
| 266 | for (tfi = 0; tfi < 32; tfi++) { |
| 267 | tbf = bts->trx[trx].pdch[ts].tbf[tfi]; |
| 268 | if (tbf) |
| 269 | tbf_free(tbf); |
| 270 | } |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | #warning FIXME: RAC |
| 276 | // rc = generate_si13(si13, 0 /* rac */); |
| 277 | // printf("rc=%d\n", rc); |
| 278 | // pcu_l1if_tx_bcch(si13, 23); |
| 279 | |
| 280 | return rc; |
| 281 | } |
| 282 | |
| 283 | static int pcu_rx(uint8_t msg_type, struct gsm_pcu_if *pcu_prim) |
| 284 | { |
| 285 | int rc = 0; |
| 286 | |
| 287 | switch (msg_type) { |
| 288 | case PCU_IF_MSG_DATA_IND: |
| 289 | rc = pcu_rx_data_ind(&pcu_prim->u.data_ind); |
| 290 | break; |
| 291 | case PCU_IF_MSG_RTS_REQ: |
| 292 | rc = pcu_rx_rts_req(&pcu_prim->u.rts_req); |
| 293 | break; |
| 294 | case PCU_IF_MSG_RACH_IND: |
| 295 | rc = pcu_rx_rach_ind(&pcu_prim->u.rach_ind); |
| 296 | break; |
| 297 | case PCU_IF_MSG_INFO_IND: |
| 298 | rc = pcu_rx_info_ind(&pcu_prim->u.info_ind); |
| 299 | break; |
| 300 | default: |
| 301 | LOGP(DL1IF, LOGL_ERROR, "Received unknwon PCU msg type %d\n", |
| 302 | msg_type); |
| 303 | rc = -EINVAL; |
| 304 | } |
| 305 | |
| 306 | return rc; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * SYSMO-PCU socket functions |
| 311 | */ |
| 312 | |
| 313 | struct pcu_sock_state { |
| 314 | struct osmo_fd conn_bfd; /* fd for connection to lcr */ |
| 315 | struct osmo_timer_list timer; /* socket connect retry timer */ |
| 316 | struct llist_head upqueue; /* queue for sending messages */ |
| 317 | } *pcu_sock_state = NULL; |
| 318 | |
| 319 | static int pcu_sock_send(struct msgb *msg) |
| 320 | { |
| 321 | struct pcu_sock_state *state = pcu_sock_state; |
| 322 | struct osmo_fd *conn_bfd; |
| 323 | |
| 324 | if (!state) { |
| 325 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket not created, dropping " |
| 326 | "message\n"); |
| 327 | return -EINVAL; |
| 328 | } |
| 329 | conn_bfd = &state->conn_bfd; |
| 330 | if (conn_bfd->fd <= 0) { |
| 331 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket not connected, dropping " |
| 332 | "message\n"); |
| 333 | return -EIO; |
| 334 | } |
| 335 | msgb_enqueue(&state->upqueue, msg); |
| 336 | conn_bfd->when |= BSC_FD_WRITE; |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static void pcu_sock_close(struct pcu_sock_state *state) |
| 342 | { |
| 343 | struct osmo_fd *bfd = &state->conn_bfd; |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 344 | struct gprs_rlcmac_bts *bts = gprs_rlcmac_bts; |
| 345 | struct gprs_rlcmac_tbf *tbf; |
| 346 | uint8_t trx, ts, tfi; |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 347 | |
| 348 | LOGP(DL1IF, LOGL_NOTICE, "PCU socket has LOST connection\n"); |
| 349 | |
| 350 | close(bfd->fd); |
| 351 | bfd->fd = -1; |
| 352 | osmo_fd_unregister(bfd); |
| 353 | |
| 354 | /* flush the queue */ |
| 355 | while (!llist_empty(&state->upqueue)) { |
| 356 | struct msgb *msg = msgb_dequeue(&state->upqueue); |
| 357 | msgb_free(msg); |
| 358 | } |
| 359 | |
Andreas Eversberg | 5dac2f0 | 2012-06-27 15:52:04 +0200 | [diff] [blame] | 360 | /* disable all slots, kick all TBFs */ |
| 361 | for (trx = 0; trx < 8; trx++) { |
| 362 | for (ts = 0; ts < 8; ts++) { |
| 363 | bts->trx[trx].pdch[ts].enable = 0; |
| 364 | for (tfi = 0; tfi < 32; tfi++) { |
| 365 | tbf = bts->trx[trx].pdch[ts].tbf[tfi]; |
| 366 | if (tbf) |
| 367 | tbf_free(tbf); |
| 368 | } |
| 369 | } |
| 370 | } |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 371 | |
| 372 | state->timer.cb = pcu_sock_timeout; |
| 373 | osmo_timer_schedule(&state->timer, 5, 0); |
| 374 | } |
| 375 | |
| 376 | static int pcu_sock_read(struct osmo_fd *bfd) |
| 377 | { |
| 378 | struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data; |
| 379 | struct gsm_pcu_if *pcu_prim; |
| 380 | struct msgb *msg; |
| 381 | int rc; |
| 382 | |
| 383 | msg = msgb_alloc(sizeof(*pcu_prim), "pcu_sock_rx"); |
| 384 | if (!msg) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | pcu_prim = (struct gsm_pcu_if *) msg->tail; |
| 388 | |
| 389 | rc = recv(bfd->fd, msg->tail, msgb_tailroom(msg), 0); |
| 390 | if (rc == 0) |
| 391 | goto close; |
| 392 | |
| 393 | if (rc < 0) { |
| 394 | if (errno == EAGAIN) |
| 395 | return 0; |
| 396 | goto close; |
| 397 | } |
| 398 | |
| 399 | rc = pcu_rx(pcu_prim->msg_type, pcu_prim); |
| 400 | |
| 401 | /* as we always synchronously process the message in pcu_rx() and |
| 402 | * its callbacks, we can free the message here. */ |
| 403 | msgb_free(msg); |
| 404 | |
| 405 | return rc; |
| 406 | |
| 407 | close: |
| 408 | msgb_free(msg); |
| 409 | pcu_sock_close(state); |
| 410 | return -1; |
| 411 | } |
| 412 | |
| 413 | static int pcu_sock_write(struct osmo_fd *bfd) |
| 414 | { |
| 415 | struct pcu_sock_state *state = (struct pcu_sock_state *)bfd->data; |
| 416 | int rc; |
| 417 | |
| 418 | while (!llist_empty(&state->upqueue)) { |
| 419 | struct msgb *msg, *msg2; |
| 420 | struct gsm_pcu_if *pcu_prim; |
| 421 | |
| 422 | /* peek at the beginning of the queue */ |
| 423 | msg = llist_entry(state->upqueue.next, struct msgb, list); |
| 424 | pcu_prim = (struct gsm_pcu_if *)msg->data; |
| 425 | |
| 426 | bfd->when &= ~BSC_FD_WRITE; |
| 427 | |
| 428 | /* bug hunter 8-): maybe someone forgot msgb_put(...) ? */ |
| 429 | if (!msgb_length(msg)) { |
| 430 | LOGP(DL1IF, LOGL_ERROR, "message type (%d) with ZERO " |
| 431 | "bytes!\n", pcu_prim->msg_type); |
| 432 | goto dontsend; |
| 433 | } |
| 434 | |
| 435 | /* try to send it over the socket */ |
| 436 | rc = write(bfd->fd, msgb_data(msg), msgb_length(msg)); |
| 437 | if (rc == 0) |
| 438 | goto close; |
| 439 | if (rc < 0) { |
| 440 | if (errno == EAGAIN) { |
| 441 | bfd->when |= BSC_FD_WRITE; |
| 442 | break; |
| 443 | } |
| 444 | goto close; |
| 445 | } |
| 446 | |
| 447 | dontsend: |
| 448 | /* _after_ we send it, we can deueue */ |
| 449 | msg2 = msgb_dequeue(&state->upqueue); |
| 450 | assert(msg == msg2); |
| 451 | msgb_free(msg); |
| 452 | } |
| 453 | return 0; |
| 454 | |
| 455 | close: |
| 456 | pcu_sock_close(state); |
| 457 | |
| 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | static int pcu_sock_cb(struct osmo_fd *bfd, unsigned int flags) |
| 462 | { |
| 463 | int rc = 0; |
| 464 | |
| 465 | if (flags & BSC_FD_READ) |
| 466 | rc = pcu_sock_read(bfd); |
| 467 | if (rc < 0) |
| 468 | return rc; |
| 469 | |
| 470 | if (flags & BSC_FD_WRITE) |
| 471 | rc = pcu_sock_write(bfd); |
| 472 | |
| 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | int pcu_l1if_open(void) |
| 477 | { |
| 478 | struct pcu_sock_state *state; |
| 479 | struct osmo_fd *bfd; |
| 480 | struct sockaddr_un local; |
| 481 | unsigned int namelen; |
| 482 | int rc; |
| 483 | |
Andreas Eversberg | 0aed654 | 2012-06-23 10:33:16 +0200 | [diff] [blame] | 484 | state = pcu_sock_state; |
| 485 | if (!state) { |
| 486 | state = talloc_zero(NULL, struct pcu_sock_state); |
| 487 | if (!state) |
| 488 | return -ENOMEM; |
| 489 | INIT_LLIST_HEAD(&state->upqueue); |
| 490 | } |
| 491 | |
| 492 | bfd = &state->conn_bfd; |
| 493 | |
| 494 | bfd->fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); |
| 495 | if (bfd->fd < 0) { |
| 496 | LOGP(DL1IF, LOGL_ERROR, "Failed to create PCU-SYSMO socket.\n"); |
| 497 | talloc_free(state); |
| 498 | return -1; |
| 499 | } |
| 500 | |
| 501 | local.sun_family = AF_UNIX; |
| 502 | strncpy(local.sun_path, "/tmp/pcu_bts", sizeof(local.sun_path)); |
| 503 | local.sun_path[sizeof(local.sun_path) - 1] = '\0'; |
| 504 | |
| 505 | /* we use the same magic that X11 uses in Xtranssock.c for |
| 506 | * calculating the proper length of the sockaddr */ |
| 507 | #if defined(BSD44SOCKETS) || defined(__UNIXWARE__) |
| 508 | local.sun_len = strlen(local.sun_path); |
| 509 | #endif |
| 510 | #if defined(BSD44SOCKETS) || defined(SUN_LEN) |
| 511 | namelen = SUN_LEN(&local); |
| 512 | #else |
| 513 | namelen = strlen(local.sun_path) + |
| 514 | offsetof(struct sockaddr_un, sun_path); |
| 515 | #endif |
| 516 | rc = connect(bfd->fd, (struct sockaddr *) &local, namelen); |
| 517 | if (rc != 0) { |
| 518 | LOGP(DL1IF, LOGL_ERROR, "Failed to Connect the PCU-SYSMO " |
| 519 | "socket, delaying... '%s'\n", local.sun_path); |
| 520 | close(bfd->fd); |
| 521 | bfd->fd = -1; |
| 522 | state->timer.cb = pcu_sock_timeout; |
| 523 | osmo_timer_schedule(&state->timer, 5, 0); |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | bfd->when = BSC_FD_READ; |
| 528 | bfd->cb = pcu_sock_cb; |
| 529 | bfd->data = state; |
| 530 | |
| 531 | rc = osmo_fd_register(bfd); |
| 532 | if (rc < 0) { |
| 533 | LOGP(DL1IF, LOGL_ERROR, "Could not register PCU fd: %d\n", rc); |
| 534 | close(bfd->fd); |
| 535 | talloc_free(state); |
| 536 | return rc; |
| 537 | } |
| 538 | |
| 539 | LOGP(DL1IF, LOGL_NOTICE, "PCU-SYSMO socket has been connected\n"); |
| 540 | |
| 541 | pcu_sock_state = state; |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | void pcu_l1if_close(void) |
| 547 | { |
| 548 | struct pcu_sock_state *state = pcu_sock_state; |
| 549 | struct osmo_fd *bfd; |
| 550 | |
| 551 | if (!state) |
| 552 | return; |
| 553 | |
| 554 | if (osmo_timer_pending(&state->timer)) |
| 555 | osmo_timer_del(&state->timer); |
| 556 | |
| 557 | bfd = &state->conn_bfd; |
| 558 | if (bfd->fd > 0) |
| 559 | pcu_sock_close(state); |
| 560 | talloc_free(state); |
| 561 | pcu_sock_state = NULL; |
| 562 | } |
| 563 | |
| 564 | static void pcu_sock_timeout(void *_priv) |
| 565 | { |
| 566 | pcu_l1if_open(); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | |
| 571 | |