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