blob: 923d7db0bc6c39bb551ca9b62447c09c67821721 [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
57#define NUM_E1_TS 32
58
59/* list of all E1 drivers */
Harald Welte44d542e2009-03-10 18:24:35 +000060LLIST_HEAD(e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000061
62/* list of all E1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +000063LLIST_HEAD(e1inp_line_list);
Harald Welte1fa60c82009-02-09 18:13:26 +000064
Harald Welte2cf161b2009-06-20 22:36:41 +020065static void *tall_sigl_ctx;
66
Holger Freytherff9592f2009-03-09 16:17:14 +000067/* to be implemented, e.g. by bsc_hack.c */
68void input_event(int event, enum e1inp_sign_type type, struct gsm_bts_trx *trx);
69
Harald Welte1fa60c82009-02-09 18:13:26 +000070/*
71 * pcap writing of the misdn load
72 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
73 */
74#define DLT_LINUX_LAPD 177
75#define PCAP_INPUT 0
76#define PCAP_OUTPUT 1
77
78struct pcap_hdr {
79 u_int32_t magic_number;
80 u_int16_t version_major;
81 u_int16_t version_minor;
82 int32_t thiszone;
83 u_int32_t sigfigs;
84 u_int32_t snaplen;
85 u_int32_t network;
86} __attribute__((packed));
87
88struct pcaprec_hdr {
89 u_int32_t ts_sec;
90 u_int32_t ts_usec;
91 u_int32_t incl_len;
92 u_int32_t orig_len;
93} __attribute__((packed));
94
95struct fake_linux_lapd_header {
96 u_int16_t pkttype;
97 u_int16_t hatype;
98 u_int16_t halen;
99 u_int64_t addr;
100 int16_t protocol;
101} __attribute__((packed));
102
103struct lapd_header {
104 u_int8_t ea1 : 1;
105 u_int8_t cr : 1;
106 u_int8_t sapi : 6;
107 u_int8_t ea2 : 1;
108 u_int8_t tei : 7;
109 u_int8_t control_foo; /* fake UM's ... */
110} __attribute__((packed));
111
Holger Hans Peter Freyther0b369c52010-11-09 23:10:16 +0100112static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
113static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
114static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
115static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
116static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
Harald Welte1fa60c82009-02-09 18:13:26 +0000117
118
119static int pcap_fd = -1;
120
Holger Freyther0469cf62009-03-31 12:14:16 +0000121void e1_set_pcap_fd(int fd)
Harald Welte1fa60c82009-02-09 18:13:26 +0000122{
123 int ret;
124 struct pcap_hdr header = {
125 .magic_number = 0xa1b2c3d4,
126 .version_major = 2,
127 .version_minor = 4,
128 .thiszone = 0,
129 .sigfigs = 0,
130 .snaplen = 65535,
131 .network = DLT_LINUX_LAPD,
132 };
133
134 pcap_fd = fd;
135 ret = write(pcap_fd, &header, sizeof(header));
136}
137
138/* This currently only works for the D-Channel */
Holger Freyther0469cf62009-03-31 12:14:16 +0000139static void write_pcap_packet(int direction, int sapi, int tei,
Harald Welte1fa60c82009-02-09 18:13:26 +0000140 struct msgb *msg) {
141 if (pcap_fd < 0)
142 return;
143
144 int ret;
145 time_t cur_time;
146 struct tm *tm;
Andreas Eversberg20152a32009-06-10 14:47:33 +0200147 int mi_head = (direction==PCAP_INPUT) ? MISDN_HEADER_LEN : 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000148
149 struct fake_linux_lapd_header header = {
150 .pkttype = 4,
151 .hatype = 0,
152 .halen = 0,
153 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
154 .protocol = ntohs(48),
155 };
156
157 struct lapd_header lapd_header = {
158 .ea1 = 0,
159 .cr = direction == PCAP_OUTPUT ? 1 : 0,
Holger Freyther0469cf62009-03-31 12:14:16 +0000160 .sapi = sapi & 0x3F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000161 .ea2 = 1,
Holger Freyther0469cf62009-03-31 12:14:16 +0000162 .tei = tei & 0x7F,
Harald Welte1fa60c82009-02-09 18:13:26 +0000163 .control_foo = 0x03 /* UI */,
164 };
165
166 struct pcaprec_hdr payload_header = {
167 .ts_sec = 0,
168 .ts_usec = 0,
169 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
170 + sizeof(struct lapd_header)
Andreas Eversberg20152a32009-06-10 14:47:33 +0200171 - mi_head,
Harald Welte1fa60c82009-02-09 18:13:26 +0000172 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
173 + sizeof(struct lapd_header)
Andreas Eversberg20152a32009-06-10 14:47:33 +0200174 - mi_head,
Harald Welte1fa60c82009-02-09 18:13:26 +0000175 };
176
177
178 cur_time = time(NULL);
179 tm = localtime(&cur_time);
180 payload_header.ts_sec = mktime(tm);
181
182 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
183 ret = write(pcap_fd, &header, sizeof(header));
184 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Andreas Eversberg20152a32009-06-10 14:47:33 +0200185 ret = write(pcap_fd, msg->data + mi_head,
186 msg->len - mi_head);
Harald Welte1fa60c82009-02-09 18:13:26 +0000187}
Harald Welte1fa60c82009-02-09 18:13:26 +0000188
Harald Welted256d4f2009-03-10 19:44:48 +0000189static const char *sign_types[] = {
190 [E1INP_SIGN_NONE] = "None",
191 [E1INP_SIGN_OML] = "OML",
192 [E1INP_SIGN_RSL] = "RSL",
193};
194const char *e1inp_signtype_name(enum e1inp_sign_type tp)
195{
196 if (tp >= ARRAY_SIZE(sign_types))
197 return "undefined";
198 return sign_types[tp];
199}
200
201static const char *ts_types[] = {
202 [E1INP_TS_TYPE_NONE] = "None",
203 [E1INP_TS_TYPE_SIGN] = "Signalling",
204 [E1INP_TS_TYPE_TRAU] = "TRAU",
205};
206
207const char *e1inp_tstype_name(enum e1inp_ts_type tp)
208{
209 if (tp >= ARRAY_SIZE(ts_types))
210 return "undefined";
211 return ts_types[tp];
212}
213
Harald Welte1fa60c82009-02-09 18:13:26 +0000214/* callback when a TRAU frame was received */
215static int subch_cb(struct subch_demux *dmx, int ch, u_int8_t *data, int len,
216 void *_priv)
217{
218 struct e1inp_ts *e1i_ts = _priv;
219 struct gsm_e1_subslot src_ss;
220
221 src_ss.e1_nr = e1i_ts->line->num;
222 src_ss.e1_ts = e1i_ts->num;
223 src_ss.e1_ts_ss = ch;
224
225 return trau_mux_input(&src_ss, data, len);
226}
227
228int abis_rsl_sendmsg(struct msgb *msg)
229{
230 struct e1inp_sign_link *sign_link;
231 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000232 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000233
234 msg->l2h = msg->data;
235
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100236 if (!msg->trx) {
237 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
238 hexdump(msg->data, msg->len));
Harald Welteeab33352009-06-27 03:09:08 +0200239 talloc_free(msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000240 return -EINVAL;
Harald Welte (local)5fe33182009-12-28 17:05:43 +0100241 } else if (!msg->trx->rsl_link) {
242 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
243 hexdump(msg->data, msg->len));
244 talloc_free(msg);
245 return -EIO;
Harald Welte1fa60c82009-02-09 18:13:26 +0000246 }
247
248 sign_link = msg->trx->rsl_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000249 e1i_ts = sign_link->ts;
250 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
251 /* notify the driver we have something to write */
252 e1inp_driver = sign_link->ts->line->driver;
253 e1inp_driver->want_write(e1i_ts);
254 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000255 msgb_enqueue(&sign_link->tx_list, msg);
256
Holger Freyther0469cf62009-03-31 12:14:16 +0000257 /* dump it */
258 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
259
Harald Welte1fa60c82009-02-09 18:13:26 +0000260 return 0;
261}
262
263int _abis_nm_sendmsg(struct msgb *msg)
264{
265 struct e1inp_sign_link *sign_link;
266 struct e1inp_driver *e1inp_driver;
Harald Weltef55b49f2009-05-23 06:20:41 +0000267 struct e1inp_ts *e1i_ts;
Harald Welte1fa60c82009-02-09 18:13:26 +0000268
269 msg->l2h = msg->data;
270
271 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100272 LOGP(DRSL, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte1fa60c82009-02-09 18:13:26 +0000273 return -EINVAL;
274 }
Holger Freytherfb3f5192009-02-09 23:09:15 +0000275
Harald Welte1fa60c82009-02-09 18:13:26 +0000276 sign_link = msg->trx->bts->oml_link;
Harald Weltef55b49f2009-05-23 06:20:41 +0000277 e1i_ts = sign_link->ts;
278 if (!bsc_timer_pending(&e1i_ts->sign.tx_timer)) {
279 /* notify the driver we have something to write */
280 e1inp_driver = sign_link->ts->line->driver;
281 e1inp_driver->want_write(e1i_ts);
282 }
Harald Welte1fa60c82009-02-09 18:13:26 +0000283 msgb_enqueue(&sign_link->tx_list, msg);
284
Holger Freyther0469cf62009-03-31 12:14:16 +0000285 /* dump it */
286 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
287
Harald Welte1fa60c82009-02-09 18:13:26 +0000288 return 0;
289}
290
291/* Timeslot */
292
293/* configure and initialize one e1inp_ts */
294int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
295 enum e1inp_ts_type type)
296{
Harald Welte42581822009-08-08 16:12:58 +0200297 if (ts->type == type && ts->line && line)
298 return 0;
299
Harald Welte1fa60c82009-02-09 18:13:26 +0000300 ts->type = type;
301 ts->line = line;
302
303 switch (type) {
304 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther91f587e2011-01-16 18:12:13 +0100305 if (line && line->driver)
Holger Hans Peter Freytherd85642a2010-04-26 16:02:04 +0800306 ts->sign.delay = line->driver->default_delay;
307 else
308 ts->sign.delay = 100000;
Harald Welte1fa60c82009-02-09 18:13:26 +0000309 INIT_LLIST_HEAD(&ts->sign.sign_links);
310 break;
311 case E1INP_TS_TYPE_TRAU:
312 subchan_mux_init(&ts->trau.mux);
313 ts->trau.demux.out_cb = subch_cb;
314 ts->trau.demux.data = ts;
315 subch_demux_init(&ts->trau.demux);
316 break;
317 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100318 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte1fa60c82009-02-09 18:13:26 +0000319 ts->type);
320 return -EINVAL;
321 }
322 return 0;
323}
324
Harald Welte3016d9f2011-02-05 13:54:41 +0100325struct e1inp_line *e1inp_line_get(u_int8_t e1_nr)
Harald Welte1fa60c82009-02-09 18:13:26 +0000326{
327 struct e1inp_line *e1i_line;
328
329 /* iterate over global list of e1 lines */
Harald Welte44d542e2009-03-10 18:24:35 +0000330 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
Harald Welte1fa60c82009-02-09 18:13:26 +0000331 if (e1i_line->num == e1_nr)
332 return e1i_line;
333 }
334 return NULL;
335}
336
Harald Welte3016d9f2011-02-05 13:54:41 +0100337struct e1inp_line *e1inp_line_create(u_int8_t e1_nr, const char *driver_name)
338{
339 struct e1inp_driver *driver;
340 struct e1inp_line *line;
341 int i;
342
343 line = e1inp_line_get(e1_nr);
344 if (line)
345 return NULL;
346
347 driver = e1inp_driver_find(driver_name);
348 if (!driver)
349 return NULL;
350
351 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
352 if (!line)
353 return NULL;
354
355 line->driver = driver;
356
357 line->num = e1_nr;
358 for (i = 0; i < NUM_E1_TS; i++) {
359 line->ts[i].num = i+1;
360 line->ts[i].line = line;
361 }
362 llist_add_tail(&line->list, &e1inp_line_list);
363
364 return line;
365}
366
367#if 0
Harald Welte42581822009-08-08 16:12:58 +0200368struct e1inp_line *e1inp_line_get_create(u_int8_t e1_nr)
369{
370 struct e1inp_line *line;
371 int i;
372
373 line = e1inp_line_get(e1_nr);
374 if (line)
375 return line;
376
377 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
378 if (!line)
379 return NULL;
380
381 line->num = e1_nr;
382 for (i = 0; i < NUM_E1_TS; i++) {
383 line->ts[i].num = i+1;
384 line->ts[i].line = line;
385 }
386 llist_add_tail(&line->list, &e1inp_line_list);
387
388 return line;
389}
Harald Welte3016d9f2011-02-05 13:54:41 +0100390#endif
Harald Welte42581822009-08-08 16:12:58 +0200391
Harald Welte1fa60c82009-02-09 18:13:26 +0000392static struct e1inp_ts *e1inp_ts_get(u_int8_t e1_nr, u_int8_t ts_nr)
393{
394 struct e1inp_line *e1i_line;
395
396 e1i_line = e1inp_line_get(e1_nr);
397 if (!e1i_line)
398 return NULL;
399
400 return &e1i_line->ts[ts_nr-1];
401}
402
403struct subch_mux *e1inp_get_mux(u_int8_t e1_nr, u_int8_t ts_nr)
404{
405 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
406
407 if (!e1i_ts)
408 return NULL;
409
410 return &e1i_ts->trau.mux;
411}
412
413/* Signalling Link */
414
415struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
416 u_int8_t tei, u_int8_t sapi)
417{
418 struct e1inp_sign_link *link;
419
420 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
421 if (link->sapi == sapi && link->tei == tei)
422 return link;
423 }
424
425 return NULL;
426}
427
428/* create a new signalling link in a E1 timeslot */
429
430struct e1inp_sign_link *
431e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
432 struct gsm_bts_trx *trx, u_int8_t tei,
433 u_int8_t sapi)
434{
435 struct e1inp_sign_link *link;
436
437 if (ts->type != E1INP_TS_TYPE_SIGN)
438 return NULL;
439
Harald Welte470ec292009-06-26 20:25:23 +0200440 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte1fa60c82009-02-09 18:13:26 +0000441 if (!link)
442 return NULL;
443
Harald Welte1fa60c82009-02-09 18:13:26 +0000444 link->ts = ts;
445 link->type = type;
446 INIT_LLIST_HEAD(&link->tx_list);
447 link->trx = trx;
Holger Freytherfb3f5192009-02-09 23:09:15 +0000448 link->tei = tei;
449 link->sapi = sapi;
Harald Welte1fa60c82009-02-09 18:13:26 +0000450
451 llist_add_tail(&link->list, &ts->sign.sign_links);
452
453 return link;
454}
455
Harald Welte42581822009-08-08 16:12:58 +0200456void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
457{
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200458 struct msgb *msg;
459
Harald Welte42581822009-08-08 16:12:58 +0200460 llist_del(&link->list);
Holger Hans Peter Freyther4ac100e2010-04-11 14:13:10 +0200461 while (!llist_empty(&link->tx_list)) {
462 msg = msgb_dequeue(&link->tx_list);
463 msgb_free(msg);
464 }
465
Holger Hans Peter Freytherf51b9002010-04-11 17:18:02 +0200466 if (link->ts->type == E1INP_TS_TYPE_SIGN)
467 bsc_del_timer(&link->ts->sign.tx_timer);
468
Harald Welte42581822009-08-08 16:12:58 +0200469 talloc_free(link);
470}
471
Harald Welte1fa60c82009-02-09 18:13:26 +0000472/* the E1 driver tells us he has received something on a TS */
473int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
474 u_int8_t tei, u_int8_t sapi)
475{
476 struct e1inp_sign_link *link;
477 int ret;
478
479 switch (ts->type) {
480 case E1INP_TS_TYPE_SIGN:
Harald Welte1fa60c82009-02-09 18:13:26 +0000481 /* consult the list of signalling links */
Holger Freyther0469cf62009-03-31 12:14:16 +0000482 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
Harald Welte1fa60c82009-02-09 18:13:26 +0000483 link = e1inp_lookup_sign_link(ts, tei, sapi);
484 if (!link) {
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100485 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte1fa60c82009-02-09 18:13:26 +0000486 "tei %d, sapi %d\n", tei, sapi);
487 return -EINVAL;
488 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100489
Harald Weltedc5062b2010-03-26 21:28:59 +0800490 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte1fa60c82009-02-09 18:13:26 +0000491 switch (link->type) {
492 case E1INP_SIGN_OML:
493 msg->trx = link->trx;
494 ret = abis_nm_rcvmsg(msg);
495 break;
496 case E1INP_SIGN_RSL:
497 msg->trx = link->trx;
498 ret = abis_rsl_rcvmsg(msg);
499 break;
500 default:
501 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100502 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000503 break;
504 }
505 break;
506 case E1INP_TS_TYPE_TRAU:
Harald Welte9e783312009-02-17 19:02:29 +0000507 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
Harald Welte1fa60c82009-02-09 18:13:26 +0000508 break;
509 default:
510 ret = -EINVAL;
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100511 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000512 break;
513 }
514
515 return ret;
516}
517
518#define TSX_ALLOC_SIZE 4096
519
520/* called by driver if it wants to transmit on a given TS */
521struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
522 struct e1inp_sign_link **sign_link)
523{
524 struct e1inp_sign_link *link;
525 struct msgb *msg = NULL;
526 int len;
527
528 switch (e1i_ts->type) {
529 case E1INP_TS_TYPE_SIGN:
530 /* FIXME: implement this round robin */
531 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
532 msg = msgb_dequeue(&link->tx_list);
533 if (msg) {
534 if (sign_link)
535 *sign_link = link;
536 break;
537 }
538 }
539 break;
540 case E1INP_TS_TYPE_TRAU:
Harald Welte966636f2009-06-26 19:39:35 +0200541 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte1fa60c82009-02-09 18:13:26 +0000542 if (!msg)
543 return NULL;
544 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
545 msgb_put(msg, 40);
546 break;
547 default:
Harald Welteb1d4c8e2009-12-17 23:10:46 +0100548 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte1fa60c82009-02-09 18:13:26 +0000549 return NULL;
550 }
551 return msg;
552}
553
554/* called by driver in case some kind of link state event */
555int e1inp_event(struct e1inp_ts *ts, int evt, u_int8_t tei, u_int8_t sapi)
556{
557 struct e1inp_sign_link *link;
558
559 link = e1inp_lookup_sign_link(ts, tei, sapi);
560 if (!link)
561 return -EINVAL;
562
563 /* FIXME: report further upwards */
Holger Freytherff9592f2009-03-09 16:17:14 +0000564 input_event(evt, link->type, link->trx);
565 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000566}
567
568/* register a driver with the E1 core */
569int e1inp_driver_register(struct e1inp_driver *drv)
570{
Harald Welte3016d9f2011-02-05 13:54:41 +0100571 printf("registering driver %s\n", drv->name);
Harald Welte44d542e2009-03-10 18:24:35 +0000572 llist_add_tail(&drv->list, &e1inp_driver_list);
Harald Welte1fa60c82009-02-09 18:13:26 +0000573 return 0;
574}
575
Harald Welte3016d9f2011-02-05 13:54:41 +0100576struct e1inp_driver *e1inp_driver_find(const char *name)
577{
578 struct e1inp_driver *drv;
579
580 printf("trying to find driver %s\n", name);
581 llist_for_each_entry(drv, &e1inp_driver_list, list) {
582 if (!strcasecmp(name, drv->name))
583 return drv;
584 }
585 return NULL;
586}
587
Harald Welte42581822009-08-08 16:12:58 +0200588int e1inp_line_update(struct e1inp_line *line)
Harald Welte1fa60c82009-02-09 18:13:26 +0000589{
Harald Welte54552432011-02-05 12:26:55 +0100590 if (line->driver && line->driver->line_update)
591 return line->driver->line_update(line);
592 else
593 return 0;
Harald Welte1fa60c82009-02-09 18:13:26 +0000594}
Harald Welte7bfc2672009-07-28 00:41:45 +0200595
Harald Welte29b9cf82009-11-30 19:43:54 +0100596static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
597 void *handler_data, void *signal_data)
598{
599 if (subsys != SS_GLOBAL ||
600 signal != S_GLOBAL_SHUTDOWN)
601 return 0;
602
603 if (pcap_fd) {
604 close(pcap_fd);
605 pcap_fd = -1;
606 }
607
608 return 0;
609}
610
Harald Welte3016d9f2011-02-05 13:54:41 +0100611void e1inp_misdn_init(void);
612
613void e1inp_init(void)
Harald Welte7bfc2672009-07-28 00:41:45 +0200614{
615 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
616 "e1inp_sign_link");
Harald Welte29b9cf82009-11-30 19:43:54 +0100617 register_signal_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Welte3016d9f2011-02-05 13:54:41 +0100618
619 e1inp_misdn_init();
Harald Welte7bfc2672009-07-28 00:41:45 +0200620}