blob: def6aca95e928ab84ed3715579edc9a65c238a16 [file] [log] [blame]
Harald Welte1fa60c82009-02-09 18:13:26 +00001/* OpenBSC Abis interface to E1 */
2
3/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte1fa60c82009-02-09 18:13:26 +000010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte1fa60c82009-02-09 18:13:26 +000016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte1fa60c82009-02-09 18:13:26 +000019 *
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <errno.h>
26#include <string.h>
27#include <time.h>
28#include <sys/fcntl.h>
29#include <sys/types.h>
30#include <sys/socket.h>
31#include <sys/ioctl.h>
32#include <arpa/inet.h>
33#include <mISDNif.h>
34
35//#define AF_COMPATIBILITY_FUNC
36//#include <compat_af_isdn.h>
Holger Freyther66e092b2009-03-31 12:13:50 +000037#ifndef AF_ISDN
Harald Welte1fa60c82009-02-09 18:13:26 +000038#define AF_ISDN 34
39#define PF_ISDN AF_ISDN
Holger Freyther66e092b2009-03-31 12:13:50 +000040#endif
Harald Welte1fa60c82009-02-09 18:13:26 +000041
Harald Weltedfe6c7d2010-02-20 16:24:02 +010042#include <osmocore/select.h>
43#include <osmocore/msgb.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000044#include <openbsc/debug.h>
45#include <openbsc/gsm_data.h>
46#include <openbsc/e1_input.h>
47#include <openbsc/abis_nm.h>
48#include <openbsc/abis_rsl.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010049#include <osmocore/linuxlist.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000050#include <openbsc/subchan_demux.h>
Harald Welte45b407a2009-05-23 15:51:12 +000051#include <openbsc/trau_frame.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000052#include <openbsc/trau_mux.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010053#include <osmocore/talloc.h>
Harald Welte29b9cf82009-11-30 19:43:54 +010054#include <openbsc/signal.h>
Holger Hans Peter Freyther34e97492009-08-10 07:54:02 +020055#include <openbsc/misdn.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000056
Harald Welteca17ef82011-02-05 15:13:27 +010057#include "../bscconfig.h"
58
Harald Welte1fa60c82009-02-09 18:13:26 +000059#define NUM_E1_TS 32
60
61/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000062LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000063
64/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000065LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000066
Harald Welte2cf161b2009-06-20 22:36:41 +020067static void *tall_sigl_ctx;
68
Holger Freytherff9592f2009-03-09 16:17:14 +000069/* to be implemented, e.g. by bsc_hack.c */
70void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx);
71
Harald Welte1fa60c82009-02-09 18:13:26 +000072/*
73 * pcap writing of the misdn load
74 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
75 */
76#define DLT_LINUX_LAPD 177
77#define PCAP_INPUT 0
78#define PCAP_OUTPUT 1
79
80struct pcap_hdr {
81 u_int32_t magic_number;
82 u_int16_t version_major;
83 u_int16_t version_minor;
84 int32_t thiszone;
85 u_int32_t sigfigs;
86 u_int32_t snaplen;
87 u_int32_t network;
88} __attribute__((packed));
89
90struct pcaprec_hdr {
91 u_int32_t ts_sec;
92 u_int32_t ts_usec;
93 u_int32_t incl_len;
94 u_int32_t orig_len;
95} __attribute__((packed));
96
97struct fake_linux_lapd_header {
98 u_int16_t pkttype;
99 u_int16_t hatype;
100 u_int16_t halen;
101 u_int64_t addr;
102 int16_t protocol;
103} __attribute__((packed));
104
105struct lapd_header {
106 u_int8_t ea1 : 1;
107 u_int8_t cr : 1;
108 u_int8_t sapi : 6;
109 u_int8_t ea2 : 1;
110 u_int8_t tei : 7;
111 u_int8_t control_foo; /* fake UM's ... */
112} __attribute__((packed));
113
Holger Hans Peter Freyther0b369c52010-11-09 23:10:16 +0100114static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
115static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
116static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
117static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
118static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
Harald Welte1fa60c82009-02-09 18:13:26 +0000119
120
121static int pcap_fd = -1;
122
Holger Freyther0469cf62009-03-31 12:14:16 +0000123void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000124{
125 int ret;
126 struct pcap_hdr header = {
127 .magic_number = 0xa1b2c3d4,
128 .version_major = 2,
129 .version_minor = 4,
130 .thiszone = 0,
131 .sigfigs = 0,
132 .snaplen = 65535,
133 .network = DLT_LINUX_LAPD,
134 };
135
136 pcap_fd = fd;
137 ret = write(pcap_fd, &header, sizeof(header));
138}
139
140/* This currently only works for the D-Channel */
Holger Freyther0469cf62009-03-31 12:14:16 +0000141static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000142 struct msgb *msg) {
143 if (pcap_fd < 0)
144 return;
145
146 int ret;
147 time_t cur_time;
148 struct tm *tm;
Andreas Eversberg20152a32009-06-10 14:47:33 +0200149 int mi_head = (direction==PCAP_INPUT) ? MISDN_HEADER_LEN : 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000150
151 struct fake_linux_lapd_header header = {
152 .pkttype = 4,
153 .hatype = 0,
154 .halen = 0,
155 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
156 .protocol = ntohs(48),
157 };
158
159 struct lapd_header lapd_header = {
160 .ea1 = 0,
161 .cr = direction == PCAP_OUTPUT ? 1 : 0,
Holger Freyther0469cf62009-03-31 12:14:16 +0000162 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000163 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000164 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000165 .control_foo = 0x03 /* UI */,
166 };
167
168 struct pcaprec_hdr payload_header = {
169 .ts_sec = 0,
170 .ts_usec = 0,
171 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
172 + sizeof(struct lapd_header)
Andreas Eversberg20152a32009-06-10 14:47:33 +0200173 - mi_head,
Harald Welte1fa60c82009-02-09 18:13:26 +0000174 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
175 + sizeof(struct lapd_header)
Andreas Eversberg20152a32009-06-10 14:47:33 +0200176 - mi_head,
Harald Welte1fa60c82009-02-09 18:13:26 +0000177 };
178
179
180 cur_time = time(NULL);
181 tm = localtime(&cur_time);
182 payload_header.ts_sec = mktime(tm);
183
184 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
185 ret = write(pcap_fd, &header, sizeof(header));
186 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Andreas Eversberg20152a32009-06-10 14:47:33 +0200187 ret = write(pcap_fd, msg->data + mi_head,
188 msg->len - mi_head);
Harald Welte1fa60c82009-02-09 18:13:26 +0000189}
Harald Welte1fa60c82009-02-09 18:13:26 +0000190
Harald Welted256d4f2009-03-10 19:44:48 +0000191static const char *sign_types[] = {
192 [E1INP_SIGN_NONE] = "None",
193 [E1INP_SIGN_OML] = "OML",
194 [E1INP_SIGN_RSL] = "RSL",
195};
196const char *e1inp_signtype_name(enum e1inp_sign_type tp)
197{
198 if (tp >= ARRAY_SIZE(sign_types))
199 return "undefined";
200 return sign_types[tp];
201}
202
203static const char *ts_types[] = {
204 [E1INP_TS_TYPE_NONE] = "None",
205 [E1INP_TS_TYPE_SIGN] = "Signalling",
206 [E1INP_TS_TYPE_TRAU] = "TRAU",
207};
208
209const char *e1inp_tstype_name(enum e1inp_ts_type tp)
210{
211 if (tp >= ARRAY_SIZE(ts_types))
212 return "undefined";
213 return ts_types[tp];
214}
215
Harald Welte1fa60c82009-02-09 18:13:26 +0000216/* callback when a TRAU frame was received */
217static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
218 void *_priv)
219{
220 struct e1inp_ts *e1i_ts = _priv;
221 struct gsm_e1_subslot src_ss;
222
223 src_ss.e1_nr = e1i_ts->line->num;
224 src_ss.e1_ts = e1i_ts->num;
225 src_ss.e1_ts_ss = ch;
226
227 return trau_mux_input(&src_ss, data, len);
228}
229
230int abis_rsl_sendmsg(struct msgb *msg)
231{
232 struct e1inp_sign_link *sign_link;
233 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000234 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000235
236 msg->l2h = msg->data;
237
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100238 if (!msg->trx) {
239 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
240 hexdump(msg->data, msg->len));
Harald Welteeab33352009-06-27 03:09:08 +0200241 talloc_free(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000242 return -EINVAL;
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100243 } else if (!msg->trx->rsl_link) {
244 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
245 hexdump(msg->data, msg->len));
246 talloc_free(msg);
247 return -EIO;
Harald Welte1fa60c82009-02-09 18:13:26 +0000248 }
249
250 sign_link = msg->trx->rsl_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000251 e1i_ts = sign_link->ts;
252 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
253 /* notify the driver we have something to write */
254 e1inp_driver = sign_link->ts->line->driver;
255 e1inp_driver->want_write(e1i_ts);
256 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000257 msgb_enqueue(&sign_link->tx_list, msg);
258
Holger Freyther0469cf62009-03-31 12:14:16 +0000259 /* dump it */
260 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
261
Harald Welte1fa60c82009-02-09 18:13:26 +0000262 return 0;
263}
264
265int _abis_nm_sendmsg(struct msgb *msg)
266{
267 struct e1inp_sign_link *sign_link;
268 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000269 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000270
271 msg->l2h = msg->data;
272
273 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Harald Weltef933de92011-02-05 20:18:18 +0100274 LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000275 return -EINVAL;
276 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000277
Harald Welte1fa60c82009-02-09 18:13:26 +0000278 sign_link = msg->trx->bts->oml_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000279 e1i_ts = sign_link->ts;
280 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
281 /* notify the driver we have something to write */
282 e1inp_driver = sign_link->ts->line->driver;
283 e1inp_driver->want_write(e1i_ts);
284 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000285 msgb_enqueue(&sign_link->tx_list, msg);
286
Holger Freyther0469cf62009-03-31 12:14:16 +0000287 /* dump it */
288 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
289
Harald Welte1fa60c82009-02-09 18:13:26 +0000290 return 0;
291}
292
293/* Timeslot */
294
295/* configure and initialize one e1inp_ts */
296int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
297 enum e1inp_ts_type type)
298{
Harald Welte42581822009-08-08 16:12:58 +0200299 if (ts->type == type && ts->line && line)
300 return 0;
301
Harald Welte1fa60c82009-02-09 18:13:26 +0000302 ts->type = type;
303 ts->line = line;
304
305 switch (type) {
306 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther91f587e2011-01-16 18:12:13 +0100307 if (line && line->driver)
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800308 ts->sign.delay = line->driver->default_delay;
309 else
310 ts->sign.delay = 100000;
Harald Welte1fa60c82009-02-09 18:13:26 +0000311 INIT_LLIST_HEAD(&ts->sign.sign_links);
312 break;
313 case E1INP_TS_TYPE_TRAU:
314 subchan_mux_init(&ts->trau.mux);
315 ts->trau.demux.out_cb = subch_cb;
316 ts->trau.demux.data = ts;
317 subch_demux_init(&ts->trau.demux);
318 break;
319 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100320 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte1fa60c82009-02-09 18:13:26 +0000321 ts->type);
322 return -EINVAL;
323 }
324 return 0;
325}
326
Harald Welte3016d9f2011-02-05 13:54:41 +0100327struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000328{
329 struct e1inp_line *e1i_line;
330
331 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000332 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000333 if (e1i_line->num == e1_nr)
334 return e1i_line;
335 }
336 return NULL;
337}
338
Harald Welte3016d9f2011-02-05 13:54:41 +0100339struct e1inp_line *e1inp_line_create(u_int8_t e1_nr, const char *driver_name)
340{
341 struct e1inp_driver *driver;
342 struct e1inp_line *line;
343 int i;
344
345 line = e1inp_line_get(e1_nr);
Harald Welte07bb0da2011-02-05 15:57:42 +0100346 if (line) {
347 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
348 e1_nr);
Harald Welte3016d9f2011-02-05 13:54:41 +0100349 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100350 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100351
352 driver = e1inp_driver_find(driver_name);
Harald Welte07bb0da2011-02-05 15:57:42 +0100353 if (!driver) {
354 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
355 driver_name);
Harald Welte3016d9f2011-02-05 13:54:41 +0100356 return NULL;
Harald Welte07bb0da2011-02-05 15:57:42 +0100357 }
Harald Welte3016d9f2011-02-05 13:54:41 +0100358
359 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
360 if (!line)
361 return NULL;
362
363 line->driver = driver;
364
365 line->num = e1_nr;
366 for (i = 0; i < NUM_E1_TS; i++) {
367 line->ts[i].num = i+1;
368 line->ts[i].line = line;
369 }
370 llist_add_tail(&line->list, &e1inp_line_list);
371
372 return line;
373}
374
375#if 0
Harald Welte42581822009-08-08 16:12:58 +0200376struct e1inp_line *e1inp_line_get_create(u_int8_t e1_nr)
377{
378 struct e1inp_line *line;
379 int i;
380
381 line = e1inp_line_get(e1_nr);
382 if (line)
383 return line;
384
385 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
386 if (!line)
387 return NULL;
388
389 line->num = e1_nr;
390 for (i = 0; i < NUM_E1_TS; i++) {
391 line->ts[i].num = i+1;
392 line->ts[i].line = line;
393 }
394 llist_add_tail(&line->list, &e1inp_line_list);
395
396 return line;
397}
Harald Welte3016d9f2011-02-05 13:54:41 +0100398#endif
Harald Welte42581822009-08-08 16:12:58 +0200399
Harald Welte1fa60c82009-02-09 18:13:26 +0000400static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
401{
402 struct e1inp_line *e1i_line;
403
404 e1i_line = e1inp_line_get(e1_nr);
405 if (!e1i_line)
406 return NULL;
407
408 return &e1i_line->ts[ts_nr-1];
409}
410
411struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
412{
413 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
414
415 if (!e1i_ts)
416 return NULL;
417
418 return &e1i_ts->trau.mux;
419}
420
421/* Signalling Link */
422
423struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
424 u_int8_t tei, u_int8_t sapi)
425{
426 struct e1inp_sign_link *link;
427
428 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
429 if (link->sapi == sapi && link->tei == tei)
430 return link;
431 }
432
433 return NULL;
434}
435
436/* create a new signalling link in a E1 timeslot */
437
438struct e1inp_sign_link *
439e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
440 struct gsm_bts_trx *trx, u_int8_t tei,
441 u_int8_t sapi)
442{
443 struct e1inp_sign_link *link;
444
445 if (ts->type != E1INP_TS_TYPE_SIGN)
446 return NULL;
447
Harald Welte470ec292009-06-26 20:25:23 +0200448 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte1fa60c82009-02-09 18:13:26 +0000449 if (!link)
450 return NULL;
451
Harald Welte1fa60c82009-02-09 18:13:26 +0000452 link->ts = ts;
453 link->type = type;
454 INIT_LLIST_HEAD(&link->tx_list);
455 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000456 link->tei = tei;
457 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000458
459 llist_add_tail(&link->list, &ts->sign.sign_links);
460
461 return link;
462}
463
Harald Welte42581822009-08-08 16:12:58 +0200464void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
465{
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200466 struct msgb *msg;
467
Harald Welte42581822009-08-08 16:12:58 +0200468 llist_del(&link->list);
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200469 while (!llist_empty(&link->tx_list)) {
470 msg = msgb_dequeue(&link->tx_list);
471 msgb_free(msg);
472 }
473
Holger Hans Peter Freytherf51b9002010-04-11 17:18:02 +0200474 if (link->ts->type == E1INP_TS_TYPE_SIGN)
475 bsc_del_timer(&link->ts->sign.tx_timer);
476
Harald Welte42581822009-08-08 16:12:58 +0200477 talloc_free(link);
478}
479
Harald Welte1fa60c82009-02-09 18:13:26 +0000480/* the E1 driver tells us he has received something on a TS */
481int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
482 u_int8_t tei, u_int8_t sapi)
483{
484 struct e1inp_sign_link *link;
485 int ret;
486
487 switch (ts->type) {
488 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000489 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000490 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000491 link = e1inp_lookup_sign_link(ts, tei, sapi);
492 if (!link) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100493 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte1fa60c82009-02-09 18:13:26 +0000494 "tei %d, sapi %d\n", tei, sapi);
495 return -EINVAL;
496 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100497
Harald Weltedc5062b2010-03-26 21:28:59 +0800498 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte1fa60c82009-02-09 18:13:26 +0000499 switch (link->type) {
500 case E1INP_SIGN_OML:
501 msg->trx = link->trx;
502 ret = abis_nm_rcvmsg(msg);
503 break;
504 case E1INP_SIGN_RSL:
505 msg->trx = link->trx;
506 ret = abis_rsl_rcvmsg(msg);
507 break;
508 default:
509 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100510 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000511 break;
512 }
513 break;
514 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000515 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000516 break;
517 default:
518 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100519 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000520 break;
521 }
522
523 return ret;
524}
525
526#define TSX_ALLOC_SIZE 4096
527
528/* called by driver if it wants to transmit on a given TS */
529struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
530 struct e1inp_sign_link **sign_link)
531{
532 struct e1inp_sign_link *link;
533 struct msgb *msg = NULL;
534 int len;
535
536 switch (e1i_ts->type) {
537 case E1INP_TS_TYPE_SIGN:
538 /* FIXME: implement this round robin */
539 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
540 msg = msgb_dequeue(&link->tx_list);
541 if (msg) {
542 if (sign_link)
543 *sign_link = link;
544 break;
545 }
546 }
547 break;
548 case E1INP_TS_TYPE_TRAU:
Harald Welte966636f2009-06-26 19:39:35 +0200549 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte1fa60c82009-02-09 18:13:26 +0000550 if (!msg)
551 return NULL;
552 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
553 msgb_put(msg, 40);
554 break;
555 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100556 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000557 return NULL;
558 }
559 return msg;
560}
561
562/* called by driver in case some kind of link state event */
563int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
564{
565 struct e1inp_sign_link *link;
566
567 link = e1inp_lookup_sign_link(ts, tei, sapi);
568 if (!link)
569 return -EINVAL;
570
571 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000572 input_event(evt, link->type, link->trx);
573 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000574}
575
576/* register a driver with the E1 core */
577int e1inp_driver_register(struct e1inp_driver *drv)
578{
Harald Welte44d542e2009-03-10 18:24:35 +0000579 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000580 return 0;
581}
582
Harald Welte3016d9f2011-02-05 13:54:41 +0100583struct e1inp_driver *e1inp_driver_find(const char *name)
584{
585 struct e1inp_driver *drv;
586
Harald Welte3016d9f2011-02-05 13:54:41 +0100587 llist_for_each_entry(drv, &e1inp_driver_list, list) {
588 if (!strcasecmp(name, drv->name))
589 return drv;
590 }
591 return NULL;
592}
593
Harald Welte42581822009-08-08 16:12:58 +0200594int e1inp_line_update(struct e1inp_line *line)
Harald Welte1fa60c82009-02-09 18:13:26 +0000595{
Harald Welte54552432011-02-05 12:26:55 +0100596 if (line->driver && line->driver->line_update)
597 return line->driver->line_update(line);
598 else
599 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000600}
Harald Welte7bfc2672009-07-28 00:41:45 +0200601
Harald Welte29b9cf82009-11-30 19:43:54 +0100602static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
603 void *handler_data, void *signal_data)
604{
605 if (subsys != SS_GLOBAL ||
606 signal != S_GLOBAL_SHUTDOWN)
607 return 0;
608
609 if (pcap_fd) {
610 close(pcap_fd);
611 pcap_fd = -1;
612 }
613
614 return 0;
615}
616
Harald Welte3016d9f2011-02-05 13:54:41 +0100617void e1inp_misdn_init(void);
Harald Welte1dd68c32011-02-05 13:58:46 +0100618void e1inp_dahdi_init(void);
Harald Welte3016d9f2011-02-05 13:54:41 +0100619
620void e1inp_init(void)
Harald Welte7bfc2672009-07-28 00:41:45 +0200621{
622 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
623 "e1inp_sign_link");
Harald Welte29b9cf82009-11-30 19:43:54 +0100624 register_signal_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Welte3016d9f2011-02-05 13:54:41 +0100625
626 e1inp_misdn_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100627#ifdef HAVE_DAHDI_USER_H
Harald Welte1dd68c32011-02-05 13:58:46 +0100628 e1inp_dahdi_init();
Harald Welteca17ef82011-02-05 15:13:27 +0100629#endif
Harald Welte7bfc2672009-07-28 00:41:45 +0200630}