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