Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for mISDNuser */ |
| 2 | |
| 3 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
| 29 | #include <time.h> |
| 30 | #include <sys/fcntl.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <mISDNif.h> |
| 36 | |
| 37 | //#define AF_COMPATIBILITY_FUNC |
| 38 | //#include <compat_af_isdn.h> |
| 39 | #define AF_ISDN 34 |
| 40 | #define PF_ISDN AF_ISDN |
| 41 | |
| 42 | #include <openbsc/select.h> |
| 43 | #include <openbsc/msgb.h> |
| 44 | #include <openbsc/debug.h> |
| 45 | #include <openbsc/gsm_data.h> |
| 46 | #include <openbsc/abis_nm.h> |
| 47 | #include <openbsc/abis_rsl.h> |
| 48 | #include <openbsc/subchan_demux.h> |
| 49 | #include <openbsc/e1_input.h> |
| 50 | |
| 51 | /* data structure for one E1 interface with A-bis */ |
| 52 | struct mi_e1_handle { |
| 53 | /* The mISDN card number of the card we use */ |
| 54 | int cardnr; |
| 55 | }; |
| 56 | |
| 57 | #define TS1_ALLOC_SIZE 300 |
| 58 | |
| 59 | struct prim_name { |
| 60 | unsigned int prim; |
| 61 | const char *name; |
| 62 | }; |
| 63 | |
| 64 | const struct prim_name prim_names[] = { |
Holger Freyther | cbc7b06 | 2009-02-09 23:10:48 +0000 | [diff] [blame] | 65 | { PH_CONTROL_IND, "PH_CONTROL_IND" }, |
| 66 | { PH_DATA_IND, "PH_DATA_IND" }, |
| 67 | { PH_DATA_CNF, "PH_DATA_CNF" }, |
Harald Welte | 1fa60c8 | 2009-02-09 18:13:26 +0000 | [diff] [blame] | 68 | { DL_ESTABLISH_IND, "DL_ESTABLISH_IND" }, |
| 69 | { DL_ESTABLISH_CNF, "DL_ESTABLISH_CNF" }, |
| 70 | { DL_RELEASE_IND, "DL_RELEASE_IND" }, |
| 71 | { DL_RELEASE_CNF, "DL_RELEASE_CNF" }, |
| 72 | { DL_DATA_IND, "DL_DATA_IND" }, |
| 73 | { DL_UNITDATA_IND, "DL_UNITDATA_IND" }, |
| 74 | { DL_INFORMATION_IND, "DL_INFORMATION_IND" }, |
| 75 | { MPH_ACTIVATE_IND, "MPH_ACTIVATE_IND" }, |
| 76 | { MPH_DEACTIVATE_IND, "MPH_DEACTIVATE_IND" }, |
| 77 | }; |
| 78 | |
| 79 | const char *get_prim_name(unsigned int prim) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < ARRAY_SIZE(prim_names); i++) { |
| 84 | if (prim_names[i].prim == prim) |
| 85 | return prim_names[i].name; |
| 86 | } |
| 87 | |
| 88 | return "UNKNOWN"; |
| 89 | } |
| 90 | |
| 91 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 92 | { |
| 93 | struct e1inp_line *line = bfd->data; |
| 94 | unsigned int ts_nr = bfd->priv_nr; |
| 95 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 96 | struct e1inp_sign_link *link; |
| 97 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
| 98 | struct sockaddr_mISDN l2addr; |
| 99 | struct mISDNhead *hh; |
| 100 | socklen_t alen; |
| 101 | int ret; |
| 102 | |
| 103 | if (!msg) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | hh = (struct mISDNhead *) msg->data; |
| 107 | |
| 108 | alen = sizeof(l2addr); |
| 109 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 110 | (struct sockaddr *) &l2addr, &alen); |
| 111 | if (ret < 0) { |
| 112 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | if (alen != sizeof(l2addr)) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | msgb_put(msg, ret); |
| 120 | |
| 121 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 122 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 123 | |
| 124 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n", |
| 125 | ret, hh->prim, hh->id); |
| 126 | |
| 127 | DEBUGP(DMI, "got %s:\n", get_prim_name(hh->prim)); |
| 128 | |
| 129 | switch (hh->prim) { |
| 130 | case DL_INFORMATION_IND: |
| 131 | /* mISDN tells us which channel number is allocated for this |
| 132 | * tuple of (SAPI, TEI). */ |
| 133 | DEBUGP(DMI, "use channel(%d) sapi(%d) tei(%d) for now\n", |
| 134 | l2addr.channel, l2addr.sapi, l2addr.tei); |
| 135 | link = e1inp_lookup_sign_link(e1i_ts, l2addr.tei, l2addr.sapi); |
| 136 | if (!link) { |
| 137 | DEBUGPC(DMI, "mISDN message for unknown sign_link\n"); |
| 138 | free(msg); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | /* save the channel number in the driver private struct */ |
| 142 | link->driver.misdn.channel = l2addr.channel; |
| 143 | break; |
| 144 | case MPH_ACTIVATE_IND: |
| 145 | ret = e1inp_event(e1i_ts, EVT_E1_TEI_UP, l2addr.tei, l2addr.sapi); |
| 146 | break; |
| 147 | case MPH_DEACTIVATE_IND: |
| 148 | ret = e1inp_event(e1i_ts, EVT_E1_TEI_DN, l2addr.tei, l2addr.sapi); |
| 149 | break; |
| 150 | case DL_DATA_IND: |
| 151 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
| 152 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 153 | if (debug_mask & DMI) { |
| 154 | fprintf(stdout, "RX: "); |
| 155 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 156 | } |
| 157 | ret = e1inp_rx_ts(e1i_ts, msg, l2addr.tei, l2addr.sapi); |
| 158 | break; |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 166 | { |
| 167 | struct e1inp_line *line = bfd->data; |
| 168 | unsigned int ts_nr = bfd->priv_nr; |
| 169 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 170 | struct e1inp_sign_link *sign_link; |
| 171 | struct sockaddr_mISDN sa; |
| 172 | struct msgb *msg; |
| 173 | struct mISDNhead *hh; |
| 174 | u_int8_t *l2_data; |
| 175 | int ret; |
| 176 | |
| 177 | /* get the next msg for this timeslot */ |
| 178 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 179 | if (!msg) { |
| 180 | bfd->when &= ~BSC_FD_WRITE; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | l2_data = msg->data; |
| 185 | |
| 186 | /* prepend the mISDNhead */ |
| 187 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 188 | hh->prim = DL_DATA_REQ; |
| 189 | |
| 190 | if (debug_mask & DMI) { |
| 191 | fprintf(stdout, "TX: "); |
| 192 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 193 | } |
| 194 | |
| 195 | /* construct the sockaddr */ |
| 196 | sa.family = AF_ISDN; |
| 197 | sa.sapi = sign_link->sapi; |
| 198 | sa.dev = sign_link->tei; |
| 199 | sa.channel = sign_link->driver.misdn.channel; |
| 200 | |
| 201 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
| 202 | (struct sockaddr *)&sa, sizeof(sa)); |
| 203 | msgb_free(msg); |
| 204 | |
| 205 | /* FIXME: this has to go */ |
| 206 | usleep(100000); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | #define TSX_ALLOC_SIZE 4096 |
| 212 | |
| 213 | /* FIXME: read from a B channel TS */ |
| 214 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 215 | { |
| 216 | struct e1inp_line *line = bfd->data; |
| 217 | unsigned int ts_nr = bfd->priv_nr; |
| 218 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 219 | struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE); |
| 220 | struct mISDNhead *hh; |
| 221 | int ret; |
| 222 | |
| 223 | if (!msg) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | hh = (struct mISDNhead *) msg->data; |
| 227 | |
| 228 | ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0); |
| 229 | if (ret < 0) { |
| 230 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | msgb_put(msg, ret); |
| 235 | |
| 236 | DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x): %s\n", |
| 237 | ret, hh->prim, hh->id, get_prim_name(hh->prim)); |
| 238 | |
| 239 | switch (hh->prim) { |
| 240 | case PH_DATA_IND: |
| 241 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 242 | if (debug_mask & DMIB) { |
| 243 | fprintf(stdout, "BCHAN RX: "); |
| 244 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 245 | } |
| 246 | ret = e1inp_rx_ts(e1i_ts, msg, 0, 0); |
| 247 | break; |
| 248 | default: |
| 249 | break; |
| 250 | } |
| 251 | /* FIXME: why do we free signalling msgs in the caller, and trau not? */ |
| 252 | msgb_free(msg); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | #define BCHAN_TX_GRAN 40 |
| 258 | /* write to a B channel TS */ |
| 259 | static int handle_tsX_write(struct bsc_fd *bfd) |
| 260 | { |
| 261 | struct e1inp_line *line = bfd->data; |
| 262 | unsigned int ts_nr = bfd->priv_nr; |
| 263 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 264 | struct mISDNhead *hh; |
| 265 | u_int8_t tx_buf[BCHAN_TX_GRAN + sizeof(*hh)]; |
| 266 | struct subch_mux *mx = &e1i_ts->trau.mux; |
| 267 | int ret; |
| 268 | |
| 269 | hh = (struct mISDNhead *) tx_buf; |
| 270 | hh->prim = PH_DATA_REQ; |
| 271 | |
| 272 | subchan_mux_out(mx, tx_buf+sizeof(*hh), BCHAN_TX_GRAN); |
| 273 | |
| 274 | ret = send(bfd->fd, tx_buf, sizeof(*hh) + BCHAN_TX_GRAN, 0); |
| 275 | if (ret < sizeof(*hh) + BCHAN_TX_GRAN) |
| 276 | DEBUGP(DMIB, "send returns %d instead of %u\n", ret, |
| 277 | sizeof(*hh) + BCHAN_TX_GRAN); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /* callback from select.c in case one of the fd's can be read/written */ |
| 283 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
| 284 | { |
| 285 | struct e1inp_line *line = bfd->data; |
| 286 | unsigned int ts_nr = bfd->priv_nr; |
| 287 | unsigned int idx = ts_nr-1; |
| 288 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 289 | int rc = 0; |
| 290 | |
| 291 | switch (e1i_ts->type) { |
| 292 | case E1INP_TS_TYPE_SIGN: |
| 293 | if (what & BSC_FD_READ) |
| 294 | rc = handle_ts1_read(bfd); |
| 295 | if (what & BSC_FD_WRITE) |
| 296 | rc = handle_ts1_write(bfd); |
| 297 | break; |
| 298 | case E1INP_TS_TYPE_TRAU: |
| 299 | if (what & BSC_FD_READ) |
| 300 | rc = handle_tsX_read(bfd); |
| 301 | if (what & BSC_FD_WRITE) |
| 302 | rc = handle_tsX_write(bfd); |
| 303 | break; |
| 304 | default: |
| 305 | fprintf(stderr, "unknown E1 TS type %u\n", e1i_ts->type); |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | return rc; |
| 310 | } |
| 311 | |
| 312 | static int activate_bchan(struct e1inp_line *line, int ts, int act) |
| 313 | { |
| 314 | struct mISDNhead hh; |
| 315 | int ret; |
| 316 | unsigned int idx = ts-1; |
| 317 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 318 | struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd; |
| 319 | |
| 320 | fprintf(stdout, "activate bchan\n"); |
| 321 | if (act) |
| 322 | hh.prim = PH_ACTIVATE_REQ; |
| 323 | else |
| 324 | hh.prim = PH_DEACTIVATE_REQ; |
| 325 | |
| 326 | hh.id = MISDN_ID_ANY; |
| 327 | ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0); |
| 328 | if (ret < 0) { |
| 329 | fprintf(stdout, "could not send ACTIVATE_RQ %s\n", |
| 330 | strerror(errno)); |
| 331 | } |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int ts_want_write(struct e1inp_ts *e1i_ts) |
| 337 | { |
| 338 | e1i_ts->driver.misdn.fd.when |= BSC_FD_WRITE; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | struct e1inp_driver misdn_driver = { |
| 344 | .name = "mISDNuser", |
| 345 | .want_write = ts_want_write, |
| 346 | }; |
| 347 | |
| 348 | static int mi_e1_setup(struct e1inp_line *line) |
| 349 | { |
| 350 | struct mi_e1_handle *e1h = line->driver_data; |
| 351 | int ts, ret; |
| 352 | |
| 353 | /* TS0 is CRC4, don't need any fd for it */ |
| 354 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
| 355 | unsigned int idx = ts-1; |
| 356 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 357 | struct bsc_fd *bfd = &e1i_ts->driver.misdn.fd; |
| 358 | struct sockaddr_mISDN addr; |
| 359 | |
| 360 | bfd->data = line; |
| 361 | bfd->priv_nr = ts; |
| 362 | bfd->cb = misdn_fd_cb; |
| 363 | |
| 364 | switch (e1i_ts->type) { |
| 365 | case E1INP_TS_TYPE_NONE: |
| 366 | continue; |
| 367 | break; |
| 368 | case E1INP_TS_TYPE_SIGN: |
| 369 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
| 370 | bfd->when = BSC_FD_READ; |
| 371 | break; |
| 372 | case E1INP_TS_TYPE_TRAU: |
| 373 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 374 | bfd->when = BSC_FD_READ | BSC_FD_WRITE; |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | if (bfd->fd < 0) { |
| 379 | fprintf(stderr, "could not open socket %s\n", |
| 380 | strerror(errno)); |
| 381 | return bfd->fd; |
| 382 | } |
| 383 | |
| 384 | memset(&addr, 0, sizeof(addr)); |
| 385 | addr.family = AF_ISDN; |
| 386 | addr.dev = e1h->cardnr; |
| 387 | switch (e1i_ts->type) { |
| 388 | case E1INP_TS_TYPE_SIGN: |
| 389 | addr.channel = 0; |
| 390 | /* SAPI not supported yet in kernel */ |
| 391 | //addr.sapi = e1inp_ts->sign.sapi; |
| 392 | addr.sapi = 0; |
| 393 | addr.tei = GROUP_TEI; |
| 394 | break; |
| 395 | case E1INP_TS_TYPE_TRAU: |
| 396 | addr.channel = ts; |
| 397 | break; |
| 398 | default: |
| 399 | DEBUGP(DMI, "unsupported E1 TS type: %u\n", |
| 400 | e1i_ts->type); |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 405 | if (ret < 0) { |
| 406 | fprintf(stderr, "could not bind l2 socket %s\n", |
| 407 | strerror(errno)); |
| 408 | return -EIO; |
| 409 | } |
| 410 | |
| 411 | /* FIXME: only activate B-Channels once we start to |
| 412 | * use them to conserve CPU power */ |
| 413 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) |
| 414 | activate_bchan(line, ts, 1); |
| 415 | |
| 416 | ret = bsc_register_fd(bfd); |
| 417 | if (ret < 0) { |
| 418 | fprintf(stderr, "could not register FD: %s\n", |
| 419 | strerror(ret)); |
| 420 | return ret; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | int mi_setup(int cardnr, struct e1inp_line *line) |
| 428 | { |
| 429 | struct mi_e1_handle *e1h; |
| 430 | int sk, ret, cnt; |
| 431 | struct mISDN_devinfo devinfo; |
| 432 | |
| 433 | /* register the driver with the core */ |
| 434 | /* FIXME: do this in the plugin initializer function */ |
| 435 | ret = e1inp_driver_register(&misdn_driver); |
| 436 | if (ret) |
| 437 | return ret; |
| 438 | |
| 439 | /* create the actual line instance */ |
| 440 | /* FIXME: do this independent of driver registration */ |
| 441 | e1h = malloc(sizeof(*e1h)); |
| 442 | memset(e1h, 0, sizeof(*e1h)); |
| 443 | |
| 444 | e1h->cardnr = cardnr; |
| 445 | |
| 446 | line->driver = &misdn_driver; |
| 447 | line->driver_data = e1h; |
| 448 | |
| 449 | /* open the ISDN card device */ |
| 450 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
| 451 | if (sk < 0) { |
| 452 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 453 | return sk; |
| 454 | } |
| 455 | |
| 456 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 457 | if (ret) { |
| 458 | fprintf(stderr, "error getting interf count: %s\n", |
| 459 | strerror(errno)); |
| 460 | close(sk); |
| 461 | return -ENODEV; |
| 462 | } |
| 463 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 464 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 465 | #if 1 |
| 466 | devinfo.id = e1h->cardnr; |
| 467 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 468 | if (ret < 0) { |
| 469 | fprintf(stdout, "error getting info for device %d: %s\n", |
| 470 | e1h->cardnr, strerror(errno)); |
| 471 | return -ENODEV; |
| 472 | } |
| 473 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 474 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 475 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 476 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 477 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 478 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 479 | #endif |
| 480 | |
| 481 | ret = mi_e1_setup(line); |
| 482 | if (ret) |
| 483 | return ret; |
| 484 | |
| 485 | return e1inp_line_register(line); |
| 486 | } |