blob: 75cdb8365abf0e8e169a2a2c1fdfc2cc7cda110a [file] [log] [blame]
Harald Welte8470bf22008-12-25 23:28:35 +00001/* OpenBSC Abis interface to mISDNuser */
2
3/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freyther9a3ee0f2009-01-02 00:40:15 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte52b1f982008-12-23 20:25:15 +00005 *
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>
Holger Freyther2139b242009-01-02 01:17:48 +000029#include <time.h>
Harald Weltee9a82612008-12-27 16:45:29 +000030#include <sys/fcntl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031#include <sys/types.h>
32#include <sys/socket.h>
33#include <sys/ioctl.h>
Harald Welte4e0d1372009-01-03 03:04:07 +000034#include <arpa/inet.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035#include <mISDNif.h>
36
Harald Welte8470bf22008-12-25 23:28:35 +000037//#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>
Harald Welte52b1f982008-12-23 20:25:15 +000048
49#define NUM_E1_TS 32
50
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000051
52/*
53 * pcap writing of the misdn load
54 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
55 */
Harald Welte62a923d2009-01-03 01:32:00 +000056#define DLT_LINUX_LAPD 177
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000057#define PCAP_INPUT 0
58#define PCAP_OUTPUT 1
59
60struct pcap_hdr {
61 u_int32_t magic_number;
62 u_int16_t version_major;
63 u_int16_t version_minor;
64 int32_t thiszone;
65 u_int32_t sigfigs;
66 u_int32_t snaplen;
67 u_int32_t network;
68} __attribute__((packed));
69
70struct pcaprec_hdr {
71 u_int32_t ts_sec;
72 u_int32_t ts_usec;
73 u_int32_t incl_len;
74 u_int32_t orig_len;
75} __attribute__((packed));
76
Harald Welte4e0d1372009-01-03 03:04:07 +000077struct fake_linux_lapd_header {
78 u_int16_t pkttype;
79 u_int16_t hatype;
80 u_int16_t halen;
81 u_int64_t addr;
82 int16_t protocol;
Harald Welte62a923d2009-01-03 01:32:00 +000083} __attribute__((packed));
84
Harald Welte599639e2009-01-03 03:28:10 +000085struct lapd_header {
86 u_int8_t ea1 : 1;
87 u_int8_t cr : 1;
88 u_int8_t sapi : 6;
89 u_int8_t ea2 : 1;
90 u_int8_t tei : 7;
91 u_int8_t control_foo; /* fake UM's ... */
92} __attribute__((packed));
93
Harald Welte4e0d1372009-01-03 03:04:07 +000094static_assert((int)&((struct fake_linux_lapd_header*)NULL)->hatype == 2, hatype_offset);
95static_assert((int)&((struct fake_linux_lapd_header*)NULL)->halen == 4, halen_offset);
96static_assert((int)&((struct fake_linux_lapd_header*)NULL)->addr == 6, addr_offset);
97static_assert((int)&((struct fake_linux_lapd_header*)NULL)->protocol == 14, proto_offset);
98static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
99
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000100
101static int pcap_fd = -1;
102
103void mi_set_pcap_fd(int fd)
104{
105 int ret;
106 struct pcap_hdr header = {
107 .magic_number = 0xa1b2c3d4,
108 .version_major = 2,
109 .version_minor = 4,
110 .thiszone = 0,
111 .sigfigs = 0,
112 .snaplen = 65535,
Harald Welte62a923d2009-01-03 01:32:00 +0000113 .network = DLT_LINUX_LAPD,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000114 };
115
116 pcap_fd = fd;
117 ret = write(pcap_fd, &header, sizeof(header));
118}
119
Harald Welte62a923d2009-01-03 01:32:00 +0000120/* This currently only works for the D-Channel */
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000121static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
122 struct msgb *msg) {
123 if (pcap_fd < 0)
124 return;
125
126 int ret;
Holger Freyther2139b242009-01-02 01:17:48 +0000127 time_t cur_time;
128 struct tm *tm;
Harald Welte62a923d2009-01-03 01:32:00 +0000129
Harald Welte4e0d1372009-01-03 03:04:07 +0000130 struct fake_linux_lapd_header header = {
131 .pkttype = 4,
132 .hatype = 0,
133 .halen = 0,
Harald Welte599639e2009-01-03 03:28:10 +0000134 .addr = direction == PCAP_OUTPUT ? 0x0 : 0x1,
Harald Welte4e0d1372009-01-03 03:04:07 +0000135 .protocol = ntohs(48),
Harald Welte62a923d2009-01-03 01:32:00 +0000136 };
Harald Welte599639e2009-01-03 03:28:10 +0000137
138 struct lapd_header lapd_header = {
139 .ea1 = 0,
140 .cr = direction == PCAP_OUTPUT ? 1 : 0,
141 .sapi = addr->sapi & 0x3F,
142 .ea2 = 1,
143 .tei = addr->tei & 0x7F,
Holger Freyther52aab542009-01-03 05:49:22 +0000144 .control_foo = 0x03 /* UI */,
Harald Welte599639e2009-01-03 03:28:10 +0000145 };
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000146
147 struct pcaprec_hdr payload_header = {
148 .ts_sec = 0,
149 .ts_usec = 0,
Harald Welte4e0d1372009-01-03 03:04:07 +0000150 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
Harald Welte599639e2009-01-03 03:28:10 +0000151 + sizeof(struct lapd_header)
Harald Welte62a923d2009-01-03 01:32:00 +0000152 - MISDN_HEADER_LEN,
Harald Welte4e0d1372009-01-03 03:04:07 +0000153 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
Harald Welte599639e2009-01-03 03:28:10 +0000154 + sizeof(struct lapd_header)
Harald Welte62a923d2009-01-03 01:32:00 +0000155 - MISDN_HEADER_LEN,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000156 };
157
Harald Welte62a923d2009-01-03 01:32:00 +0000158
Holger Freyther2139b242009-01-02 01:17:48 +0000159 cur_time = time(NULL);
160 tm = localtime(&cur_time);
161 payload_header.ts_sec = mktime(tm);
162
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000163 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
Harald Welteca6bf782009-01-03 00:41:04 +0000164 ret = write(pcap_fd, &header, sizeof(header));
Harald Welte599639e2009-01-03 03:28:10 +0000165 ret = write(pcap_fd, &lapd_header, sizeof(lapd_header));
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000166 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
167 msg->len - MISDN_HEADER_LEN);
168}
169
Harald Welte52b1f982008-12-23 20:25:15 +0000170/* data structure for one E1 interface with A-bis */
171struct mi_e1_handle {
172 struct gsm_bts *bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000173 /* The mISDN card number of the card we use */
174 int cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000175 /* The RSL adress */
176 struct sockaddr_mISDN l2addr;
Harald Welte52b1f982008-12-23 20:25:15 +0000177 /* The OML adress */
178 struct sockaddr_mISDN omladdr;
Harald Welte8470bf22008-12-25 23:28:35 +0000179 /* list (queue) of to-be-sent msgb's */
180 struct llist_head rsl_tx_list;
181 struct llist_head oml_tx_list;
Harald Welte52b1f982008-12-23 20:25:15 +0000182
Harald Weltead384642008-12-26 10:20:07 +0000183 void (*cb)(int event, struct gsm_bts *bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000184 struct bsc_fd fd[NUM_E1_TS];
Harald Weltee9a82612008-12-27 16:45:29 +0000185
186 int ts2_fd;
Harald Welte52b1f982008-12-23 20:25:15 +0000187};
188
Harald Welte8470bf22008-12-25 23:28:35 +0000189/* FIXME: this needs to go */
190static struct mi_e1_handle *global_e1h;
191
Harald Welte52b1f982008-12-23 20:25:15 +0000192#define SAPI_L2ML 0
193#define SAPI_OML 62
Harald Weltead384642008-12-26 10:20:07 +0000194#define SAPI_RSL 0 /* 63 ? */
Harald Welte52b1f982008-12-23 20:25:15 +0000195
196#define TEI_L2ML 127
197#define TEI_OML 25
198#define TEI_RSL 1
199
Harald Welte52b1f982008-12-23 20:25:15 +0000200#define TS1_ALLOC_SIZE 300
201
202static int handle_ts1_read(struct bsc_fd *bfd)
203{
204 struct mi_e1_handle *e1h = bfd->data;
205 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
Harald Welte8470bf22008-12-25 23:28:35 +0000206 struct sockaddr_mISDN l2addr;
207 struct mISDNhead *hh;
Harald Welte52b1f982008-12-23 20:25:15 +0000208 socklen_t alen;
Harald Welte8470bf22008-12-25 23:28:35 +0000209 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000210
211 if (!msg)
212 return -ENOMEM;
213
Harald Welte8470bf22008-12-25 23:28:35 +0000214 hh = (struct mISDNhead *) msg->data;
215
216 /* FIXME: Map TEI/SAPI to TRX */
217 msg->trx = e1h->bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000218
219 alen = sizeof(l2addr);
220 ret = recvfrom(bfd->fd, msg->data, 300, 0,
221 (struct sockaddr *) &l2addr, &alen);
222 if (ret < 0) {
223 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
224 return ret;
225 }
226
227 if (alen != sizeof(l2addr))
228 return -EINVAL;
229
230 msgb_put(msg, ret);
231
232 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
233 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
234
235 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n",
236 ret, hh->prim, hh->id);
237
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000238
Harald Welte52b1f982008-12-23 20:25:15 +0000239 switch (hh->prim) {
240 case DL_INFORMATION_IND:
241 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000242 struct sockaddr_mISDN *sa = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000243 char *lstr = "UNKN";
244
245 switch (l2addr.tei) {
246 case TEI_OML:
247 sa = &e1h->omladdr;
248 lstr = "OML";
249 break;
250 case TEI_RSL:
251 sa = &e1h->l2addr;
252 lstr = "RSL";
253 break;
254 default:
Harald Welte8470bf22008-12-25 23:28:35 +0000255 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000256 }
Harald Welte8470bf22008-12-25 23:28:35 +0000257 if (sa) {
258 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
259 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
260 memcpy(sa, &l2addr, sizeof(l2addr));
261 }
Harald Welte52b1f982008-12-23 20:25:15 +0000262 break;
263 case DL_ESTABLISH_IND:
264 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
265 break;
266 case DL_ESTABLISH_CNF:
267 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
268 break;
269 case DL_RELEASE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000270 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000271 break;
272 case MPH_ACTIVATE_IND:
273 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000274 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
275 e1h->cb(EVT_E1_OML_UP, e1h->bts);
276 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
277 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000278 break;
279 case MPH_DEACTIVATE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000280 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
281 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
282 e1h->cb(EVT_E1_OML_DN, e1h->bts);
283 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
284 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
285 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000286 case DL_DATA_IND:
287 DEBUGP(DMI, "got DL_DATA_IND\n");
Holger Freyther52af7d42009-01-02 01:18:28 +0000288 write_pcap_packet(PCAP_INPUT, &l2addr, msg);
Harald Weltead384642008-12-26 10:20:07 +0000289
Harald Weltead384642008-12-26 10:20:07 +0000290 msg->l2h = msg->data + MISDN_HEADER_LEN;
291
Harald Welte6cc38d72008-12-30 14:58:19 +0000292 if (debug_mask & DMI) {
293 fprintf(stdout, "RX: ");
294 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
295 }
Harald Welte52b1f982008-12-23 20:25:15 +0000296 switch (l2addr.tei) {
297 case TEI_OML:
298 ret = abis_nm_rcvmsg(msg);
299 break;
300 case TEI_RSL:
301 ret = abis_rsl_rcvmsg(msg);
302 break;
303 default:
304 fprintf(stderr, "DATA_IND for unknown TEI\n");
305 break;
306 }
307 break;
308 default:
309 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
310 break;
311 }
312 return ret;
313}
314
315static int handle_ts1_write(struct bsc_fd *bfd)
316{
317 struct mi_e1_handle *e1h = bfd->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000318 struct msgb *msg;
319 struct mISDNhead *hh;
Harald Weltead384642008-12-26 10:20:07 +0000320 int ret, no_oml = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000321
Harald Weltead384642008-12-26 10:20:07 +0000322 msg = msgb_dequeue(&e1h->oml_tx_list);
Harald Welte8470bf22008-12-25 23:28:35 +0000323 if (!msg)
Harald Weltead384642008-12-26 10:20:07 +0000324 no_oml = 1;
Harald Welte8470bf22008-12-25 23:28:35 +0000325 else {
Harald Weltead384642008-12-26 10:20:07 +0000326 u_int8_t *l2_data = msg->data;
327
Harald Welte8470bf22008-12-25 23:28:35 +0000328 /* prepend the mISDNhead */
329 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
330 hh->prim = DL_DATA_REQ;
Harald Welte52b1f982008-12-23 20:25:15 +0000331
Harald Welte6cc38d72008-12-30 14:58:19 +0000332 if (debug_mask & DMI) {
333 fprintf(stdout, "OML TX: ");
334 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
335 }
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000336
337 write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000338 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000339 (struct sockaddr *)&e1h->omladdr,
340 sizeof(e1h->omladdr));
Harald Weltee571fb82008-12-25 23:50:30 +0000341 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000342 usleep(100000);
Harald Weltead384642008-12-26 10:20:07 +0000343 /* we always dequeue all OML messages */
344 return ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000345 }
Harald Weltead384642008-12-26 10:20:07 +0000346
Harald Welte8470bf22008-12-25 23:28:35 +0000347 msg = msgb_dequeue(&e1h->rsl_tx_list);
348 if (!msg) {
Harald Weltead384642008-12-26 10:20:07 +0000349 if (no_oml)
Harald Welte8470bf22008-12-25 23:28:35 +0000350 bfd->when &= ~BSC_FD_WRITE;
351 } else {
Harald Weltead384642008-12-26 10:20:07 +0000352 u_int8_t *l2_data = msg->data;
353
Harald Welte8470bf22008-12-25 23:28:35 +0000354 /* prepend the mISDNhead */
355 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
356 hh->prim = DL_DATA_REQ;
357
Harald Welte6cc38d72008-12-30 14:58:19 +0000358 if (debug_mask & DMI) {
359 fprintf(stdout, "RSL TX: ");
360 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
361 }
Harald Weltead384642008-12-26 10:20:07 +0000362
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000363 write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000364 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000365 (struct sockaddr *)&e1h->l2addr,
366 sizeof(e1h->l2addr));
Harald Weltee571fb82008-12-25 23:50:30 +0000367 msgb_free(msg);
Harald Welteb2750422008-12-27 11:24:23 +0000368 usleep(10000);
Harald Welte702d8702008-12-26 20:25:35 +0000369 //sleep(1);
Harald Welte8470bf22008-12-25 23:28:35 +0000370 }
371
372 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000373}
374
Harald Weltee9a82612008-12-27 16:45:29 +0000375#define TSX_ALLOC_SIZE 4096
376
377/* FIXME: read from a B channel TS */
Harald Welte52b1f982008-12-23 20:25:15 +0000378static int handle_tsX_read(struct bsc_fd *bfd)
379{
Harald Weltee9a82612008-12-27 16:45:29 +0000380 struct mi_e1_handle *e1h = bfd->data;
381 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
382 struct mISDNhead *hh;
Holger Freytherca362a62009-01-04 21:05:01 +0000383 int ret, dummy;
Harald Weltee9a82612008-12-27 16:45:29 +0000384
385 if (!msg)
386 return -ENOMEM;
387
388 hh = (struct mISDNhead *) msg->data;
389
390 /* FIXME: Map TEI/SAPI to TRX */
391 msg->trx = e1h->bts->c0;
392
393 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
394 if (ret < 0) {
395 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
396 return ret;
397 }
398
399 msgb_put(msg, ret);
400
401 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id);
402
403 switch (hh->prim) {
404 case PH_CONTROL_IND:
405 DEBUGP(DMIB, "got PH_CONTROL_IND\n");
406 break;
407 case PH_DATA_IND:
408 DEBUGP(DMIB, "got PH_DATA_IND\n");
409
410 msg->l2h = msg->data + MISDN_HEADER_LEN;
411
Harald Welte6cc38d72008-12-30 14:58:19 +0000412 if (debug_mask & DMIB) {
413 fprintf(stdout, "BCHAN RX: ");
414 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
415 }
Harald Weltee9a82612008-12-27 16:45:29 +0000416 if (!e1h->ts2_fd)
417 e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660);
418
Holger Freytherca362a62009-01-04 21:05:01 +0000419 dummy = write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN);
Harald Weltee9a82612008-12-27 16:45:29 +0000420
421 break;
422 default:
423 DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim);
424 break;
425 }
Harald Weltee27bb342008-12-29 06:06:35 +0000426 /* FIXME: don't free it if we still hold reference! */
427 msgb_free(msg);
428
Harald Weltee9a82612008-12-27 16:45:29 +0000429 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000430}
431
Harald Welte8470bf22008-12-25 23:28:35 +0000432static int handle_tsX_write(struct bsc_fd *bfd)
Harald Welte52b1f982008-12-23 20:25:15 +0000433{
434 /* FIXME: write to a B channel TS */
Holger Freyther059c2542008-12-27 09:39:48 +0000435 return -1;
Harald Welte52b1f982008-12-23 20:25:15 +0000436}
437
438/* callback from select.c in case one of the fd's can be read/written */
Harald Welte8470bf22008-12-25 23:28:35 +0000439static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
Harald Welte52b1f982008-12-23 20:25:15 +0000440{
441 unsigned int e1_ts = bfd->priv_nr;
442 int rc = 0;
443
444 switch (e1_ts) {
445 case 1:
446 if (what & BSC_FD_READ)
447 rc = handle_ts1_read(bfd);
448 if (what & BSC_FD_WRITE)
449 rc = handle_ts1_write(bfd);
450 break;
451 default:
452 if (what & BSC_FD_READ)
453 rc = handle_tsX_read(bfd);
454 if (what & BSC_FD_WRITE)
455 rc = handle_tsX_write(bfd);
456 break;
457 }
458
459 return rc;
460}
461
Harald Welte8470bf22008-12-25 23:28:35 +0000462int abis_rsl_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000463{
Harald Welte8470bf22008-12-25 23:28:35 +0000464 struct mi_e1_handle *e1h = global_e1h;
465
Harald Weltead384642008-12-26 10:20:07 +0000466 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000467 msgb_enqueue(&e1h->rsl_tx_list, msg);
468 e1h->fd[0].when |= BSC_FD_WRITE;
469
470 return 0;
471}
472
Harald Weltead384642008-12-26 10:20:07 +0000473int _abis_nm_sendmsg(struct msgb *msg)
Harald Welte8470bf22008-12-25 23:28:35 +0000474{
475 struct mi_e1_handle *e1h = global_e1h;
476
Harald Weltead384642008-12-26 10:20:07 +0000477 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000478 msgb_enqueue(&e1h->oml_tx_list, msg);
479 e1h->fd[0].when |= BSC_FD_WRITE;
480
481 return 0;
482}
483
Harald Weltee9a82612008-12-27 16:45:29 +0000484static int activate_bchan(struct mi_e1_handle *e1h, int ts)
485{
486 struct mISDNhead hh;
487 int ret;
488 struct bsc_fd *bfd = &e1h->fd[ts-1];
489
490 fprintf(stdout, "activate bchan\n");
491 hh.prim = PH_ACTIVATE_REQ;
492 hh.id = MISDN_ID_ANY;
493 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
494 if (ret < 0) {
495 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
496 strerror(errno));
497 return 0;
498 }
499
500 return ret;
501}
502
Harald Welte8470bf22008-12-25 23:28:35 +0000503static int mi_e1_setup(struct mi_e1_handle *e1h)
504{
505 int ts, sk, ret, cnt;
506 struct mISDN_devinfo devinfo;
Harald Welte52b1f982008-12-23 20:25:15 +0000507
508 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
Harald Welte8470bf22008-12-25 23:28:35 +0000509 if (sk < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000510 fprintf(stderr, "could not open socket %s\n", strerror(errno));
511 return sk;
512 }
513
514 ret = ioctl(sk, IMGETCOUNT, &cnt);
515 if (ret) {
516 fprintf(stderr, "error getting interf count: %s\n",
517 strerror(errno));
518 close(sk);
519 return -ENODEV;
520 }
Harald Weltead384642008-12-26 10:20:07 +0000521 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
522 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
523#if 1
524 devinfo.id = e1h->cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000525 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
526 if (ret < 0) {
527 fprintf(stdout, "error getting info for device %d: %s\n",
Harald Weltead384642008-12-26 10:20:07 +0000528 e1h->cardnr, strerror(errno));
Harald Welte52b1f982008-12-23 20:25:15 +0000529 return -ENODEV;
530 }
531 fprintf(stdout, " id: %d\n", devinfo.id);
532 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
533 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
534 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
535 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
536 fprintf(stdout, " name: %s\n", devinfo.name);
537#endif
538
539 /* TS0 is CRC4, don't need any fd for it */
540 for (ts = 1; ts < NUM_E1_TS; ts++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000541 unsigned int idx = ts-1;
Harald Welte52b1f982008-12-23 20:25:15 +0000542 struct bsc_fd *bfd = &e1h->fd[idx];
543 struct sockaddr_mISDN addr;
544
Harald Welte8470bf22008-12-25 23:28:35 +0000545 bfd->data = e1h;
546 bfd->priv_nr = ts;
547 bfd->cb = misdn_fd_cb;
548
549 if (ts == 1) {
Harald Weltead384642008-12-26 10:20:07 +0000550 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
Harald Welte8470bf22008-12-25 23:28:35 +0000551 bfd->when = BSC_FD_READ;
Harald Welte6cc38d72008-12-30 14:58:19 +0000552 } else
Harald Welte52b1f982008-12-23 20:25:15 +0000553 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
554
Harald Welte8470bf22008-12-25 23:28:35 +0000555 if (bfd->fd < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000556 fprintf(stderr, "could not open socket %s\n",
557 strerror(errno));
558 return bfd->fd;
559 }
560
561 memset(&addr, 0, sizeof(addr));
562 addr.family = AF_ISDN;
563 addr.dev = e1h->cardnr;
564 if (ts == 1) {
565 addr.channel = 0;
566 addr.sapi = 0;/* SAPI not supported yet in kernel */
567 addr.tei = TEI_L2ML;
568 } else
Harald Welte6cc38d72008-12-30 14:58:19 +0000569 addr.channel = ts;
Harald Welte52b1f982008-12-23 20:25:15 +0000570
571 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
572 if (ret < 0) {
Harald Welte8470bf22008-12-25 23:28:35 +0000573 fprintf(stderr, "could not bind l2 socket %s\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000574 strerror(errno));
575 return -EIO;
576 }
Harald Welte8470bf22008-12-25 23:28:35 +0000577
Harald Weltee9a82612008-12-27 16:45:29 +0000578 if (ts == 2) {
579 bfd->when = BSC_FD_READ;
580 activate_bchan(e1h, ts);
581 }
582
Harald Welte8470bf22008-12-25 23:28:35 +0000583 ret = bsc_register_fd(bfd);
584 if (ret < 0) {
585 fprintf(stderr, "could not register FD: %s\n",
586 strerror(ret));
587 return ret;
588 }
Harald Welte52b1f982008-12-23 20:25:15 +0000589 }
Harald Welte8470bf22008-12-25 23:28:35 +0000590
591 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000592}
593
Harald Weltead384642008-12-26 10:20:07 +0000594int mi_setup(struct gsm_bts *bts, int cardnr,
595 void (cb)(int event, struct gsm_bts *bts))
Harald Welte8470bf22008-12-25 23:28:35 +0000596{
597 struct mi_e1_handle *e1h;
598
599 e1h = malloc(sizeof(*e1h));
600 memset(e1h, 0, sizeof(*e1h));
601
602 e1h->cardnr = cardnr;
603 e1h->bts = bts;
Harald Weltead384642008-12-26 10:20:07 +0000604 e1h->cb = cb;
Harald Welte8470bf22008-12-25 23:28:35 +0000605 INIT_LLIST_HEAD(&e1h->oml_tx_list);
606 INIT_LLIST_HEAD(&e1h->rsl_tx_list);
607
608 global_e1h = e1h;
609
610 return mi_e1_setup(e1h);
611}