blob: dd3385205eaf9e6a88c83e75db3d787b9b5fb355 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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 Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080010 * (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 Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080016 *
Harald Welte0e3e88e2011-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 Welte59b04682009-06-10 05:40:52 +080019 *
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>
Harald Welte59b04682009-06-10 05:40:52 +080029#include <sys/socket.h>
30#include <sys/ioctl.h>
31#include <arpa/inet.h>
32#include <mISDNif.h>
33
34//#define AF_COMPATIBILITY_FUNC
35//#include <compat_af_isdn.h>
36#ifndef AF_ISDN
37#define AF_ISDN 34
38#define PF_ISDN AF_ISDN
39#endif
40
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010041#include <osmocom/core/select.h>
42#include <osmocom/core/msgb.h>
Harald Welte59b04682009-06-10 05:40:52 +080043#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
45#include <openbsc/e1_input.h>
46#include <openbsc/abis_nm.h>
47#include <openbsc/abis_rsl.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010048#include <osmocom/core/linuxlist.h>
Harald Welte59b04682009-06-10 05:40:52 +080049#include <openbsc/subchan_demux.h>
50#include <openbsc/trau_frame.h>
51#include <openbsc/trau_mux.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010052#include <osmocom/core/talloc.h>
Harald Welte4d42bd82009-11-30 19:43:54 +010053#include <openbsc/signal.h>
Holger Hans Peter Freyther735cd9e2009-08-10 07:54:02 +020054#include <openbsc/misdn.h>
Harald Welte59b04682009-06-10 05:40:52 +080055
Harald Welted9dea592011-03-04 13:53:51 +010056#include "../../bscconfig.h"
Harald Weltec7841cc2011-02-05 15:13:27 +010057
Harald Welte59b04682009-06-10 05:40:52 +080058#define NUM_E1_TS 32
59
60/* list of all E1 drivers */
61LLIST_HEAD(e1inp_driver_list);
62
63/* list of all E1 lines */
64LLIST_HEAD(e1inp_line_list);
65
Harald Weltea8379772009-06-20 22:36:41 +020066static void *tall_sigl_ctx;
67
Harald Welte59b04682009-06-10 05:40:52 +080068/*
69 * pcap writing of the misdn load
70 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
71 */
72#define DLT_LINUX_LAPD 177
73#define PCAP_INPUT 0
74#define PCAP_OUTPUT 1
75
76struct pcap_hdr {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020077 uint32_t magic_number;
78 uint16_t version_major;
79 uint16_t version_minor;
Harald Welte59b04682009-06-10 05:40:52 +080080 int32_t thiszone;
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020081 uint32_t sigfigs;
82 uint32_t snaplen;
83 uint32_t network;
Harald Welte59b04682009-06-10 05:40:52 +080084} __attribute__((packed));
85
86struct pcaprec_hdr {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020087 uint32_t ts_sec;
88 uint32_t ts_usec;
89 uint32_t incl_len;
90 uint32_t orig_len;
Harald Welte59b04682009-06-10 05:40:52 +080091} __attribute__((packed));
92
93struct fake_linux_lapd_header {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +020094 uint16_t pkttype;
95 uint16_t hatype;
96 uint16_t halen;
97 uint64_t addr;
Harald Welte59b04682009-06-10 05:40:52 +080098 int16_t protocol;
99} __attribute__((packed));
100
101struct lapd_header {
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200102 uint8_t ea1 : 1;
103 uint8_t cr : 1;
104 uint8_t sapi : 6;
105 uint8_t ea2 : 1;
106 uint8_t tei : 7;
107 uint8_t control_foo; /* fake UM's ... */
Harald Welte59b04682009-06-10 05:40:52 +0800108} __attribute__((packed));
109
Holger Hans Peter Freythera73c02f2010-11-09 23:10:16 +0100110static_assert(offsetof(struct fake_linux_lapd_header, hatype) == 2, hatype_offset);
111static_assert(offsetof(struct fake_linux_lapd_header, halen) == 4, halen_offset);
112static_assert(offsetof(struct fake_linux_lapd_header, addr) == 6, addr_offset);
113static_assert(offsetof(struct fake_linux_lapd_header, protocol) == 14, proto_offset);
114static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
Harald Welte59b04682009-06-10 05:40:52 +0800115
116
117static int pcap_fd = -1;
118
119void e1_set_pcap_fd(int fd)
120{
121 int ret;
122 struct pcap_hdr header = {
123 .magic_number = 0xa1b2c3d4,
124 .version_major = 2,
125 .version_minor = 4,
126 .thiszone = 0,
127 .sigfigs = 0,
128 .snaplen = 65535,
129 .network = DLT_LINUX_LAPD,
130 };
131
132 pcap_fd = fd;
133 ret = write(pcap_fd, &header, sizeof(header));
134}
135
136/* This currently only works for the D-Channel */
137static void write_pcap_packet(int direction, int sapi, int tei,
138 struct msgb *msg) {
139 if (pcap_fd < 0)
140 return;
141
142 int ret;
143 time_t cur_time;
144 struct tm *tm;
145
146 struct fake_linux_lapd_header header = {
147 .pkttype = 4,
148 .hatype = 0,
149 .halen = 0,
150 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
151 .protocol = ntohs(48),
152 };
153
154 struct lapd_header lapd_header = {
155 .ea1 = 0,
156 .cr = direction == PCAP_OUTPUT ? 1 : 0,
157 .sapi = sapi & 0x3F,
158 .ea2 = 1,
159 .tei = tei & 0x7F,
160 .control_foo = 0x03 /* UI */,
161 };
162
163 struct pcaprec_hdr payload_header = {
164 .ts_sec = 0,
165 .ts_usec = 0,
Harald Welte81c51a82011-02-12 14:00:23 +0100166 .incl_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
167 + sizeof(struct lapd_header),
168 .orig_len = msgb_l2len(msg) + sizeof(struct fake_linux_lapd_header)
169 + sizeof(struct lapd_header),
Harald Welte59b04682009-06-10 05:40:52 +0800170 };
171
172
173 cur_time = time(NULL);
174 tm = localtime(&cur_time);
175 payload_header.ts_sec = mktime(tm);
176
177 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
178 ret = write(pcap_fd, &header, sizeof(header));
179 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Harald Welte81c51a82011-02-12 14:00:23 +0100180 ret = write(pcap_fd, msg->l2h, msgb_l2len(msg));
Harald Welte59b04682009-06-10 05:40:52 +0800181}
182
183static const char *sign_types[] = {
184 [E1INP_SIGN_NONE] = "None",
185 [E1INP_SIGN_OML] = "OML",
186 [E1INP_SIGN_RSL] = "RSL",
187};
188const char *e1inp_signtype_name(enum e1inp_sign_type tp)
189{
190 if (tp >= ARRAY_SIZE(sign_types))
191 return "undefined";
192 return sign_types[tp];
193}
194
195static const char *ts_types[] = {
196 [E1INP_TS_TYPE_NONE] = "None",
197 [E1INP_TS_TYPE_SIGN] = "Signalling",
198 [E1INP_TS_TYPE_TRAU] = "TRAU",
199};
200
201const char *e1inp_tstype_name(enum e1inp_ts_type tp)
202{
203 if (tp >= ARRAY_SIZE(ts_types))
204 return "undefined";
205 return ts_types[tp];
206}
207
208/* callback when a TRAU frame was received */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200209static int subch_cb(struct subch_demux *dmx, int ch, uint8_t *data, int len,
Harald Welte59b04682009-06-10 05:40:52 +0800210 void *_priv)
211{
212 struct e1inp_ts *e1i_ts = _priv;
213 struct gsm_e1_subslot src_ss;
214
215 src_ss.e1_nr = e1i_ts->line->num;
216 src_ss.e1_ts = e1i_ts->num;
217 src_ss.e1_ts_ss = ch;
218
219 return trau_mux_input(&src_ss, data, len);
220}
221
222int abis_rsl_sendmsg(struct msgb *msg)
223{
224 struct e1inp_sign_link *sign_link;
225 struct e1inp_driver *e1inp_driver;
226 struct e1inp_ts *e1i_ts;
227
228 msg->l2h = msg->data;
229
Harald Welte (local)42351bf2009-12-28 17:05:43 +0100230 if (!msg->trx) {
231 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx == NULL: %s\n",
232 hexdump(msg->data, msg->len));
Harald Welteed831842009-06-27 03:09:08 +0200233 talloc_free(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800234 return -EINVAL;
Harald Welte (local)42351bf2009-12-28 17:05:43 +0100235 } else if (!msg->trx->rsl_link) {
236 LOGP(DRSL, LOGL_ERROR, "rsl_sendmsg: msg->trx->rsl_link == NULL: %s\n",
237 hexdump(msg->data, msg->len));
238 talloc_free(msg);
239 return -EIO;
Harald Welte59b04682009-06-10 05:40:52 +0800240 }
241
242 sign_link = msg->trx->rsl_link;
243 e1i_ts = sign_link->ts;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200244 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
Harald Welte59b04682009-06-10 05:40:52 +0800245 /* notify the driver we have something to write */
246 e1inp_driver = sign_link->ts->line->driver;
247 e1inp_driver->want_write(e1i_ts);
248 }
249 msgb_enqueue(&sign_link->tx_list, msg);
250
251 /* dump it */
252 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
253
254 return 0;
255}
256
Harald Weltec5845042011-02-14 15:26:13 +0100257int _abis_nm_sendmsg(struct msgb *msg, int to_trx_oml)
Harald Welte59b04682009-06-10 05:40:52 +0800258{
259 struct e1inp_sign_link *sign_link;
260 struct e1inp_driver *e1inp_driver;
261 struct e1inp_ts *e1i_ts;
262
263 msg->l2h = msg->data;
264
265 if (!msg->trx || !msg->trx->bts || !msg->trx->bts->oml_link) {
Harald Weltee053f3f2011-02-05 20:18:18 +0100266 LOGP(DNM, LOGL_ERROR, "nm_sendmsg: msg->trx == NULL\n");
Harald Welte59b04682009-06-10 05:40:52 +0800267 return -EINVAL;
268 }
269
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100270 /* Check for TRX-specific OML link first */
Harald Weltec5845042011-02-14 15:26:13 +0100271 if (to_trx_oml) {
272 if (!msg->trx->oml_link)
273 return -ENODEV;
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100274 sign_link = msg->trx->oml_link;
Harald Weltec5845042011-02-14 15:26:13 +0100275 } else
Harald Weltecaeb9bc2011-02-13 19:36:18 +0100276 sign_link = msg->trx->bts->oml_link;
277
Harald Welte59b04682009-06-10 05:40:52 +0800278 e1i_ts = sign_link->ts;
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200279 if (!osmo_timer_pending(&e1i_ts->sign.tx_timer)) {
Harald Welte59b04682009-06-10 05:40:52 +0800280 /* notify the driver we have something to write */
281 e1inp_driver = sign_link->ts->line->driver;
282 e1inp_driver->want_write(e1i_ts);
283 }
284 msgb_enqueue(&sign_link->tx_list, msg);
285
286 /* dump it */
287 write_pcap_packet(PCAP_OUTPUT, sign_link->sapi, sign_link->tei, msg);
288
289 return 0;
290}
291
292/* Timeslot */
293
294/* configure and initialize one e1inp_ts */
295int e1inp_ts_config(struct e1inp_ts *ts, struct e1inp_line *line,
296 enum e1inp_ts_type type)
297{
Harald Welte62868882009-08-08 16:12:58 +0200298 if (ts->type == type && ts->line && line)
299 return 0;
300
Harald Welte59b04682009-06-10 05:40:52 +0800301 ts->type = type;
302 ts->line = line;
303
304 switch (type) {
305 case E1INP_TS_TYPE_SIGN:
Holger Hans Peter Freyther5f833822011-01-16 18:12:13 +0100306 if (line && line->driver)
Holger Hans Peter Freyther038834b2010-04-26 16:02:04 +0800307 ts->sign.delay = line->driver->default_delay;
308 else
309 ts->sign.delay = 100000;
Harald Welte59b04682009-06-10 05:40:52 +0800310 INIT_LLIST_HEAD(&ts->sign.sign_links);
311 break;
312 case E1INP_TS_TYPE_TRAU:
313 subchan_mux_init(&ts->trau.mux);
314 ts->trau.demux.out_cb = subch_cb;
315 ts->trau.demux.data = ts;
316 subch_demux_init(&ts->trau.demux);
317 break;
318 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100319 LOGP(DMI, LOGL_ERROR, "unsupported E1 timeslot type %u\n",
Harald Welte59b04682009-06-10 05:40:52 +0800320 ts->type);
321 return -EINVAL;
322 }
323 return 0;
324}
325
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200326struct e1inp_line *e1inp_line_get(uint8_t e1_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800327{
328 struct e1inp_line *e1i_line;
329
330 /* iterate over global list of e1 lines */
331 llist_for_each_entry(e1i_line, &e1inp_line_list, list) {
332 if (e1i_line->num == e1_nr)
333 return e1i_line;
334 }
335 return NULL;
336}
337
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200338struct e1inp_line *e1inp_line_create(uint8_t e1_nr, const char *driver_name)
Harald Weltee76bea02011-02-05 13:54:41 +0100339{
340 struct e1inp_driver *driver;
341 struct e1inp_line *line;
342 int i;
343
344 line = e1inp_line_get(e1_nr);
Harald Weltecc5e9f82011-02-05 15:57:42 +0100345 if (line) {
346 LOGP(DINP, LOGL_ERROR, "E1 Line %u already exists\n",
347 e1_nr);
Harald Weltee76bea02011-02-05 13:54:41 +0100348 return NULL;
Harald Weltecc5e9f82011-02-05 15:57:42 +0100349 }
Harald Weltee76bea02011-02-05 13:54:41 +0100350
351 driver = e1inp_driver_find(driver_name);
Harald Weltecc5e9f82011-02-05 15:57:42 +0100352 if (!driver) {
353 LOGP(DINP, LOGL_ERROR, "No such E1 driver '%s'\n",
354 driver_name);
Harald Weltee76bea02011-02-05 13:54:41 +0100355 return NULL;
Harald Weltecc5e9f82011-02-05 15:57:42 +0100356 }
Harald Weltee76bea02011-02-05 13:54:41 +0100357
358 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
359 if (!line)
360 return NULL;
361
362 line->driver = driver;
363
364 line->num = e1_nr;
365 for (i = 0; i < NUM_E1_TS; i++) {
366 line->ts[i].num = i+1;
367 line->ts[i].line = line;
368 }
369 llist_add_tail(&line->list, &e1inp_line_list);
370
371 return line;
372}
373
374#if 0
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200375struct e1inp_line *e1inp_line_get_create(uint8_t e1_nr)
Harald Welte62868882009-08-08 16:12:58 +0200376{
377 struct e1inp_line *line;
378 int i;
379
380 line = e1inp_line_get(e1_nr);
381 if (line)
382 return line;
383
384 line = talloc_zero(tall_bsc_ctx, struct e1inp_line);
385 if (!line)
386 return NULL;
387
388 line->num = e1_nr;
389 for (i = 0; i < NUM_E1_TS; i++) {
390 line->ts[i].num = i+1;
391 line->ts[i].line = line;
392 }
393 llist_add_tail(&line->list, &e1inp_line_list);
394
395 return line;
396}
Harald Weltee76bea02011-02-05 13:54:41 +0100397#endif
Harald Welte62868882009-08-08 16:12:58 +0200398
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200399static struct e1inp_ts *e1inp_ts_get(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800400{
401 struct e1inp_line *e1i_line;
402
403 e1i_line = e1inp_line_get(e1_nr);
404 if (!e1i_line)
405 return NULL;
406
407 return &e1i_line->ts[ts_nr-1];
408}
409
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200410struct subch_mux *e1inp_get_mux(uint8_t e1_nr, uint8_t ts_nr)
Harald Welte59b04682009-06-10 05:40:52 +0800411{
412 struct e1inp_ts *e1i_ts = e1inp_ts_get(e1_nr, ts_nr);
413
414 if (!e1i_ts)
415 return NULL;
416
417 return &e1i_ts->trau.mux;
418}
419
420/* Signalling Link */
421
422struct e1inp_sign_link *e1inp_lookup_sign_link(struct e1inp_ts *e1i,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200423 uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800424{
425 struct e1inp_sign_link *link;
426
427 llist_for_each_entry(link, &e1i->sign.sign_links, list) {
428 if (link->sapi == sapi && link->tei == tei)
429 return link;
430 }
431
432 return NULL;
433}
434
435/* create a new signalling link in a E1 timeslot */
436
437struct e1inp_sign_link *
438e1inp_sign_link_create(struct e1inp_ts *ts, enum e1inp_sign_type type,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200439 struct gsm_bts_trx *trx, uint8_t tei,
440 uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800441{
442 struct e1inp_sign_link *link;
443
444 if (ts->type != E1INP_TS_TYPE_SIGN)
445 return NULL;
446
Harald Welte857e00d2009-06-26 20:25:23 +0200447 link = talloc_zero(tall_sigl_ctx, struct e1inp_sign_link);
Harald Welte59b04682009-06-10 05:40:52 +0800448 if (!link)
449 return NULL;
450
Harald Welte59b04682009-06-10 05:40:52 +0800451 link->ts = ts;
452 link->type = type;
453 INIT_LLIST_HEAD(&link->tx_list);
454 link->trx = trx;
455 link->tei = tei;
456 link->sapi = sapi;
457
458 llist_add_tail(&link->list, &ts->sign.sign_links);
459
460 return link;
461}
462
Harald Welte62868882009-08-08 16:12:58 +0200463void e1inp_sign_link_destroy(struct e1inp_sign_link *link)
464{
Holger Hans Peter Freyther311a9932010-04-11 14:13:10 +0200465 struct msgb *msg;
466
Harald Welte62868882009-08-08 16:12:58 +0200467 llist_del(&link->list);
Holger Hans Peter Freyther311a9932010-04-11 14:13:10 +0200468 while (!llist_empty(&link->tx_list)) {
469 msg = msgb_dequeue(&link->tx_list);
470 msgb_free(msg);
471 }
472
Holger Hans Peter Freyther2da4f722010-04-11 17:18:02 +0200473 if (link->ts->type == E1INP_TS_TYPE_SIGN)
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200474 osmo_timer_del(&link->ts->sign.tx_timer);
Holger Hans Peter Freyther2da4f722010-04-11 17:18:02 +0200475
Harald Welte62868882009-08-08 16:12:58 +0200476 talloc_free(link);
477}
478
Harald Welte59b04682009-06-10 05:40:52 +0800479/* the E1 driver tells us he has received something on a TS */
480int e1inp_rx_ts(struct e1inp_ts *ts, struct msgb *msg,
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200481 uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800482{
483 struct e1inp_sign_link *link;
Harald Welte94809802011-02-12 12:29:21 +0100484 struct gsm_bts *bts;
Harald Welte59b04682009-06-10 05:40:52 +0800485 int ret;
486
487 switch (ts->type) {
488 case E1INP_TS_TYPE_SIGN:
489 /* consult the list of signalling links */
490 write_pcap_packet(PCAP_INPUT, sapi, tei, msg);
491 link = e1inp_lookup_sign_link(ts, tei, sapi);
492 if (!link) {
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100493 LOGP(DMI, LOGL_ERROR, "didn't find signalling link for "
Harald Welte59b04682009-06-10 05:40:52 +0800494 "tei %d, sapi %d\n", tei, sapi);
495 return -EINVAL;
496 }
Holger Hans Peter Freytherc8d862e2009-12-22 22:32:51 +0100497
Harald Welte51d2a592010-03-26 21:28:59 +0800498 log_set_context(BSC_CTX_BTS, link->trx->bts);
Harald Welte59b04682009-06-10 05:40:52 +0800499 switch (link->type) {
500 case E1INP_SIGN_OML:
501 msg->trx = link->trx;
Harald Welte94809802011-02-12 12:29:21 +0100502 bts = msg->trx->bts;
503 ret = bts->model->oml_rcvmsg(msg);
Harald Welte59b04682009-06-10 05:40:52 +0800504 break;
505 case E1INP_SIGN_RSL:
506 msg->trx = link->trx;
507 ret = abis_rsl_rcvmsg(msg);
508 break;
509 default:
510 ret = -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100511 LOGP(DMI, LOGL_ERROR, "unknown link type %u\n", link->type);
Harald Welte59b04682009-06-10 05:40:52 +0800512 break;
513 }
514 break;
515 case E1INP_TS_TYPE_TRAU:
516 ret = subch_demux_in(&ts->trau.demux, msg->l2h, msgb_l2len(msg));
517 break;
518 default:
519 ret = -EINVAL;
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100520 LOGP(DMI, LOGL_ERROR, "unknown TS type %u\n", ts->type);
Harald Welte59b04682009-06-10 05:40:52 +0800521 break;
522 }
523
524 return ret;
525}
526
527#define TSX_ALLOC_SIZE 4096
528
529/* called by driver if it wants to transmit on a given TS */
530struct msgb *e1inp_tx_ts(struct e1inp_ts *e1i_ts,
531 struct e1inp_sign_link **sign_link)
532{
533 struct e1inp_sign_link *link;
534 struct msgb *msg = NULL;
535 int len;
536
537 switch (e1i_ts->type) {
538 case E1INP_TS_TYPE_SIGN:
539 /* FIXME: implement this round robin */
540 llist_for_each_entry(link, &e1i_ts->sign.sign_links, list) {
541 msg = msgb_dequeue(&link->tx_list);
542 if (msg) {
543 if (sign_link)
544 *sign_link = link;
545 break;
546 }
547 }
548 break;
549 case E1INP_TS_TYPE_TRAU:
Harald Welte9cfc9352009-06-26 19:39:35 +0200550 msg = msgb_alloc(TSX_ALLOC_SIZE, "TRAU_TX");
Harald Welte59b04682009-06-10 05:40:52 +0800551 if (!msg)
552 return NULL;
553 len = subchan_mux_out(&e1i_ts->trau.mux, msg->data, 40);
554 msgb_put(msg, 40);
555 break;
556 default:
Harald Weltecf2ec4a2009-12-17 23:10:46 +0100557 LOGP(DMI, LOGL_ERROR, "unsupported E1 TS type %u\n", e1i_ts->type);
Harald Welte59b04682009-06-10 05:40:52 +0800558 return NULL;
559 }
560 return msg;
561}
562
563/* called by driver in case some kind of link state event */
Holger Hans Peter Freyther7eb8a9a2011-04-18 17:04:00 +0200564int e1inp_event(struct e1inp_ts *ts, int evt, uint8_t tei, uint8_t sapi)
Harald Welte59b04682009-06-10 05:40:52 +0800565{
566 struct e1inp_sign_link *link;
Harald Welte4c826f72011-01-14 15:55:42 +0100567 struct input_signal_data isd;
Harald Welte59b04682009-06-10 05:40:52 +0800568
569 link = e1inp_lookup_sign_link(ts, tei, sapi);
570 if (!link)
571 return -EINVAL;
572
Harald Welte4c826f72011-01-14 15:55:42 +0100573 isd.link_type = link->type;
574 isd.trx = link->trx;
Harald Welte586d7702011-02-13 19:44:21 +0100575 isd.tei = tei;
576 isd.sapi = sapi;
Harald Welte4c826f72011-01-14 15:55:42 +0100577
578 /* report further upwards */
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200579 osmo_signal_dispatch(SS_INPUT, evt, &isd);
Harald Welte59b04682009-06-10 05:40:52 +0800580 return 0;
581}
582
583/* register a driver with the E1 core */
584int e1inp_driver_register(struct e1inp_driver *drv)
585{
586 llist_add_tail(&drv->list, &e1inp_driver_list);
587 return 0;
588}
589
Harald Weltee76bea02011-02-05 13:54:41 +0100590struct e1inp_driver *e1inp_driver_find(const char *name)
591{
592 struct e1inp_driver *drv;
593
Harald Weltee76bea02011-02-05 13:54:41 +0100594 llist_for_each_entry(drv, &e1inp_driver_list, list) {
595 if (!strcasecmp(name, drv->name))
596 return drv;
597 }
598 return NULL;
599}
600
Harald Welte62868882009-08-08 16:12:58 +0200601int e1inp_line_update(struct e1inp_line *line)
Harald Welte59b04682009-06-10 05:40:52 +0800602{
Harald Welte18e489a2011-02-11 16:49:41 +0100603 struct input_signal_data isd;
604 int rc;
605
Harald Welte760d4712011-02-05 12:26:55 +0100606 if (line->driver && line->driver->line_update)
Harald Welte18e489a2011-02-11 16:49:41 +0100607 rc = line->driver->line_update(line);
Harald Welte760d4712011-02-05 12:26:55 +0100608 else
Harald Welte18e489a2011-02-11 16:49:41 +0100609 rc = 0;
610
611 /* Send a signal to anyone who is interested in new lines being
612 * configured */
613 memset(&isd, 0, sizeof(isd));
614 isd.line = line;
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200615 osmo_signal_dispatch(SS_INPUT, S_INP_LINE_INIT, &isd);
Harald Welte18e489a2011-02-11 16:49:41 +0100616
617 return rc;
Harald Welte59b04682009-06-10 05:40:52 +0800618}
Harald Welte932e20d2009-07-28 00:41:45 +0200619
Harald Welte4d42bd82009-11-30 19:43:54 +0100620static int e1i_sig_cb(unsigned int subsys, unsigned int signal,
621 void *handler_data, void *signal_data)
622{
623 if (subsys != SS_GLOBAL ||
624 signal != S_GLOBAL_SHUTDOWN)
625 return 0;
626
627 if (pcap_fd) {
628 close(pcap_fd);
629 pcap_fd = -1;
630 }
631
632 return 0;
633}
634
Harald Weltee76bea02011-02-05 13:54:41 +0100635void e1inp_misdn_init(void);
Harald Welte890fe4a2011-02-05 13:58:46 +0100636void e1inp_dahdi_init(void);
Harald Weltee76bea02011-02-05 13:54:41 +0100637
638void e1inp_init(void)
Harald Welte932e20d2009-07-28 00:41:45 +0200639{
640 tall_sigl_ctx = talloc_named_const(tall_bsc_ctx, 1,
641 "e1inp_sign_link");
Pablo Neira Ayusoef717c62011-05-06 12:12:31 +0200642 osmo_signal_register_handler(SS_GLOBAL, e1i_sig_cb, NULL);
Harald Weltee76bea02011-02-05 13:54:41 +0100643
644 e1inp_misdn_init();
Harald Weltec7841cc2011-02-05 15:13:27 +0100645#ifdef HAVE_DAHDI_USER_H
Harald Welte890fe4a2011-02-05 13:58:46 +0100646 e1inp_dahdi_init();
Harald Weltec7841cc2011-02-05 15:13:27 +0100647#endif
Harald Welte932e20d2009-07-28 00:41:45 +0200648}