blob: 4684c666ddcd2ad00e410af6f880b616b6979f91 [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>
34#include <mISDNif.h>
35
Harald Welte8470bf22008-12-25 23:28:35 +000036//#define AF_COMPATIBILITY_FUNC
37//#include <compat_af_isdn.h>
38#define AF_ISDN 34
39#define PF_ISDN AF_ISDN
40
41#include <openbsc/select.h>
42#include <openbsc/msgb.h>
43#include <openbsc/debug.h>
44#include <openbsc/gsm_data.h>
45#include <openbsc/abis_nm.h>
46#include <openbsc/abis_rsl.h>
Harald Welte52b1f982008-12-23 20:25:15 +000047
48#define NUM_E1_TS 32
49
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000050
51/*
52 * pcap writing of the misdn load
53 * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat
54 */
55#define WTAP_ENCAP_ISDN 17
56#define PCAP_INPUT 0
57#define PCAP_OUTPUT 1
58
59struct pcap_hdr {
60 u_int32_t magic_number;
61 u_int16_t version_major;
62 u_int16_t version_minor;
63 int32_t thiszone;
64 u_int32_t sigfigs;
65 u_int32_t snaplen;
66 u_int32_t network;
67} __attribute__((packed));
68
69struct pcaprec_hdr {
70 u_int32_t ts_sec;
71 u_int32_t ts_usec;
72 u_int32_t incl_len;
73 u_int32_t orig_len;
74} __attribute__((packed));
75
76struct fake_lapd_frame {
77 u_int8_t ea1 : 1;
78 u_int8_t cr : 1;
79 u_int8_t sapi : 6;
80 u_int8_t ea2 : 1;
81 u_int8_t tei : 7;
82 u_int8_t control_foo; /* fake UM's ... */
83} __attribute__((packed));
84
85static int pcap_fd = -1;
86
87void mi_set_pcap_fd(int fd)
88{
89 int ret;
90 struct pcap_hdr header = {
91 .magic_number = 0xa1b2c3d4,
92 .version_major = 2,
93 .version_minor = 4,
94 .thiszone = 0,
95 .sigfigs = 0,
96 .snaplen = 65535,
97 .network = WTAP_ENCAP_ISDN,
98 };
99
100 pcap_fd = fd;
101 ret = write(pcap_fd, &header, sizeof(header));
102}
103
104static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
105 struct msgb *msg) {
106 if (pcap_fd < 0)
107 return;
108
109 int ret;
Holger Freyther2139b242009-01-02 01:17:48 +0000110 time_t cur_time;
111 struct tm *tm;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000112 struct fake_lapd_frame header = {
113 .ea1 = 0,
114 .cr = PCAP_OUTPUT ? 1 : 0,
115 .sapi = addr->sapi & 0x3F,
116 .ea2 = 1,
117 .tei = addr->tei & 0x7F,
118 .control_foo = 0x13 /* UI with P set */,
119 };
120
121 struct pcaprec_hdr payload_header = {
122 .ts_sec = 0,
123 .ts_usec = 0,
124 .incl_len = msg->len + sizeof(header) - MISDN_HEADER_LEN,
125 .orig_len = msg->len + sizeof(header) - MISDN_HEADER_LEN,
126 };
127
Holger Freyther2139b242009-01-02 01:17:48 +0000128 cur_time = time(NULL);
129 tm = localtime(&cur_time);
130 payload_header.ts_sec = mktime(tm);
131
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000132 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
Harald Welteca6bf782009-01-03 00:41:04 +0000133 ret = write(pcap_fd, &header, sizeof(header));
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000134 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
135 msg->len - MISDN_HEADER_LEN);
136}
137
Harald Welte52b1f982008-12-23 20:25:15 +0000138/* data structure for one E1 interface with A-bis */
139struct mi_e1_handle {
140 struct gsm_bts *bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000141 /* The mISDN card number of the card we use */
142 int cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000143 /* The RSL adress */
144 struct sockaddr_mISDN l2addr;
Harald Welte52b1f982008-12-23 20:25:15 +0000145 /* The OML adress */
146 struct sockaddr_mISDN omladdr;
Harald Welte8470bf22008-12-25 23:28:35 +0000147 /* list (queue) of to-be-sent msgb's */
148 struct llist_head rsl_tx_list;
149 struct llist_head oml_tx_list;
Harald Welte52b1f982008-12-23 20:25:15 +0000150
Harald Weltead384642008-12-26 10:20:07 +0000151 void (*cb)(int event, struct gsm_bts *bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000152 struct bsc_fd fd[NUM_E1_TS];
Harald Weltee9a82612008-12-27 16:45:29 +0000153
154 int ts2_fd;
Harald Welte52b1f982008-12-23 20:25:15 +0000155};
156
Harald Welte8470bf22008-12-25 23:28:35 +0000157/* FIXME: this needs to go */
158static struct mi_e1_handle *global_e1h;
159
Harald Welte52b1f982008-12-23 20:25:15 +0000160#define SAPI_L2ML 0
161#define SAPI_OML 62
Harald Weltead384642008-12-26 10:20:07 +0000162#define SAPI_RSL 0 /* 63 ? */
Harald Welte52b1f982008-12-23 20:25:15 +0000163
164#define TEI_L2ML 127
165#define TEI_OML 25
166#define TEI_RSL 1
167
Harald Welte702d8702008-12-26 20:25:35 +0000168void hexdump(unsigned char *buf, int len)
Harald Welte52b1f982008-12-23 20:25:15 +0000169{
170 int i;
171 for (i = 0; i < len; i++) {
172 fprintf(stdout, "%02x ", buf[i]);
173 }
174 fprintf(stdout, "\n");
175}
176
177#define TS1_ALLOC_SIZE 300
178
179static int handle_ts1_read(struct bsc_fd *bfd)
180{
181 struct mi_e1_handle *e1h = bfd->data;
182 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
Harald Welte8470bf22008-12-25 23:28:35 +0000183 struct sockaddr_mISDN l2addr;
184 struct mISDNhead *hh;
Harald Welte52b1f982008-12-23 20:25:15 +0000185 socklen_t alen;
Harald Welte8470bf22008-12-25 23:28:35 +0000186 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000187
188 if (!msg)
189 return -ENOMEM;
190
Harald Welte8470bf22008-12-25 23:28:35 +0000191 hh = (struct mISDNhead *) msg->data;
192
193 /* FIXME: Map TEI/SAPI to TRX */
194 msg->trx = e1h->bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000195
196 alen = sizeof(l2addr);
197 ret = recvfrom(bfd->fd, msg->data, 300, 0,
198 (struct sockaddr *) &l2addr, &alen);
199 if (ret < 0) {
200 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
201 return ret;
202 }
203
204 if (alen != sizeof(l2addr))
205 return -EINVAL;
206
207 msgb_put(msg, ret);
208
209 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
210 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
211
212 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n",
213 ret, hh->prim, hh->id);
214
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000215
Harald Welte52b1f982008-12-23 20:25:15 +0000216 switch (hh->prim) {
217 case DL_INFORMATION_IND:
218 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000219 struct sockaddr_mISDN *sa = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000220 char *lstr = "UNKN";
221
222 switch (l2addr.tei) {
223 case TEI_OML:
224 sa = &e1h->omladdr;
225 lstr = "OML";
226 break;
227 case TEI_RSL:
228 sa = &e1h->l2addr;
229 lstr = "RSL";
230 break;
231 default:
Harald Welte8470bf22008-12-25 23:28:35 +0000232 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000233 }
Harald Welte8470bf22008-12-25 23:28:35 +0000234 if (sa) {
235 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
236 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
237 memcpy(sa, &l2addr, sizeof(l2addr));
238 }
Harald Welte52b1f982008-12-23 20:25:15 +0000239 break;
240 case DL_ESTABLISH_IND:
241 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
242 break;
243 case DL_ESTABLISH_CNF:
244 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
245 break;
246 case DL_RELEASE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000247 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000248 break;
249 case MPH_ACTIVATE_IND:
250 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000251 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
252 e1h->cb(EVT_E1_OML_UP, e1h->bts);
253 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
254 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000255 break;
256 case MPH_DEACTIVATE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000257 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
258 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
259 e1h->cb(EVT_E1_OML_DN, e1h->bts);
260 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
261 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
262 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000263 case DL_DATA_IND:
264 DEBUGP(DMI, "got DL_DATA_IND\n");
Holger Freyther52af7d42009-01-02 01:18:28 +0000265 write_pcap_packet(PCAP_INPUT, &l2addr, msg);
Harald Weltead384642008-12-26 10:20:07 +0000266
Harald Weltead384642008-12-26 10:20:07 +0000267 msg->l2h = msg->data + MISDN_HEADER_LEN;
268
Harald Welte6cc38d72008-12-30 14:58:19 +0000269 if (debug_mask & DMI) {
270 fprintf(stdout, "RX: ");
271 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
272 }
Harald Welte52b1f982008-12-23 20:25:15 +0000273 switch (l2addr.tei) {
274 case TEI_OML:
275 ret = abis_nm_rcvmsg(msg);
276 break;
277 case TEI_RSL:
278 ret = abis_rsl_rcvmsg(msg);
279 break;
280 default:
281 fprintf(stderr, "DATA_IND for unknown TEI\n");
282 break;
283 }
284 break;
285 default:
286 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
287 break;
288 }
289 return ret;
290}
291
292static int handle_ts1_write(struct bsc_fd *bfd)
293{
294 struct mi_e1_handle *e1h = bfd->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000295 struct msgb *msg;
296 struct mISDNhead *hh;
Harald Weltead384642008-12-26 10:20:07 +0000297 int ret, no_oml = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000298
Harald Weltead384642008-12-26 10:20:07 +0000299 msg = msgb_dequeue(&e1h->oml_tx_list);
Harald Welte8470bf22008-12-25 23:28:35 +0000300 if (!msg)
Harald Weltead384642008-12-26 10:20:07 +0000301 no_oml = 1;
Harald Welte8470bf22008-12-25 23:28:35 +0000302 else {
Harald Weltead384642008-12-26 10:20:07 +0000303 u_int8_t *l2_data = msg->data;
304
Harald Welte8470bf22008-12-25 23:28:35 +0000305 /* prepend the mISDNhead */
306 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
307 hh->prim = DL_DATA_REQ;
Harald Welte52b1f982008-12-23 20:25:15 +0000308
Harald Welte6cc38d72008-12-30 14:58:19 +0000309 if (debug_mask & DMI) {
310 fprintf(stdout, "OML TX: ");
311 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
312 }
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000313
314 write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000315 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000316 (struct sockaddr *)&e1h->omladdr,
317 sizeof(e1h->omladdr));
Harald Weltee571fb82008-12-25 23:50:30 +0000318 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000319 usleep(100000);
Harald Weltead384642008-12-26 10:20:07 +0000320 /* we always dequeue all OML messages */
321 return ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000322 }
Harald Weltead384642008-12-26 10:20:07 +0000323
Harald Welte8470bf22008-12-25 23:28:35 +0000324 msg = msgb_dequeue(&e1h->rsl_tx_list);
325 if (!msg) {
Harald Weltead384642008-12-26 10:20:07 +0000326 if (no_oml)
Harald Welte8470bf22008-12-25 23:28:35 +0000327 bfd->when &= ~BSC_FD_WRITE;
328 } else {
Harald Weltead384642008-12-26 10:20:07 +0000329 u_int8_t *l2_data = msg->data;
330
Harald Welte8470bf22008-12-25 23:28:35 +0000331 /* prepend the mISDNhead */
332 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
333 hh->prim = DL_DATA_REQ;
334
Harald Welte6cc38d72008-12-30 14:58:19 +0000335 if (debug_mask & DMI) {
336 fprintf(stdout, "RSL TX: ");
337 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
338 }
Harald Weltead384642008-12-26 10:20:07 +0000339
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000340 write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000341 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000342 (struct sockaddr *)&e1h->l2addr,
343 sizeof(e1h->l2addr));
Harald Weltee571fb82008-12-25 23:50:30 +0000344 msgb_free(msg);
Harald Welteb2750422008-12-27 11:24:23 +0000345 usleep(10000);
Harald Welte702d8702008-12-26 20:25:35 +0000346 //sleep(1);
Harald Welte8470bf22008-12-25 23:28:35 +0000347 }
348
349 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000350}
351
Harald Weltee9a82612008-12-27 16:45:29 +0000352#define TSX_ALLOC_SIZE 4096
353
354/* FIXME: read from a B channel TS */
Harald Welte52b1f982008-12-23 20:25:15 +0000355static int handle_tsX_read(struct bsc_fd *bfd)
356{
Harald Weltee9a82612008-12-27 16:45:29 +0000357 struct mi_e1_handle *e1h = bfd->data;
358 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
359 struct mISDNhead *hh;
360 int ret;
361
362 if (!msg)
363 return -ENOMEM;
364
365 hh = (struct mISDNhead *) msg->data;
366
367 /* FIXME: Map TEI/SAPI to TRX */
368 msg->trx = e1h->bts->c0;
369
370 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
371 if (ret < 0) {
372 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
373 return ret;
374 }
375
376 msgb_put(msg, ret);
377
378 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id);
379
380 switch (hh->prim) {
381 case PH_CONTROL_IND:
382 DEBUGP(DMIB, "got PH_CONTROL_IND\n");
383 break;
384 case PH_DATA_IND:
385 DEBUGP(DMIB, "got PH_DATA_IND\n");
386
387 msg->l2h = msg->data + MISDN_HEADER_LEN;
388
Harald Welte6cc38d72008-12-30 14:58:19 +0000389 if (debug_mask & DMIB) {
390 fprintf(stdout, "BCHAN RX: ");
391 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
392 }
Harald Weltee9a82612008-12-27 16:45:29 +0000393 if (!e1h->ts2_fd)
394 e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660);
395
396 write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN);
397
398 break;
399 default:
400 DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim);
401 break;
402 }
Harald Weltee27bb342008-12-29 06:06:35 +0000403 /* FIXME: don't free it if we still hold reference! */
404 msgb_free(msg);
405
Harald Weltee9a82612008-12-27 16:45:29 +0000406 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000407}
408
Harald Welte8470bf22008-12-25 23:28:35 +0000409static int handle_tsX_write(struct bsc_fd *bfd)
Harald Welte52b1f982008-12-23 20:25:15 +0000410{
411 /* FIXME: write to a B channel TS */
Holger Freyther059c2542008-12-27 09:39:48 +0000412 return -1;
Harald Welte52b1f982008-12-23 20:25:15 +0000413}
414
415/* callback from select.c in case one of the fd's can be read/written */
Harald Welte8470bf22008-12-25 23:28:35 +0000416static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
Harald Welte52b1f982008-12-23 20:25:15 +0000417{
418 unsigned int e1_ts = bfd->priv_nr;
419 int rc = 0;
420
421 switch (e1_ts) {
422 case 1:
423 if (what & BSC_FD_READ)
424 rc = handle_ts1_read(bfd);
425 if (what & BSC_FD_WRITE)
426 rc = handle_ts1_write(bfd);
427 break;
428 default:
429 if (what & BSC_FD_READ)
430 rc = handle_tsX_read(bfd);
431 if (what & BSC_FD_WRITE)
432 rc = handle_tsX_write(bfd);
433 break;
434 }
435
436 return rc;
437}
438
Harald Welte8470bf22008-12-25 23:28:35 +0000439int abis_rsl_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000440{
Harald Welte8470bf22008-12-25 23:28:35 +0000441 struct mi_e1_handle *e1h = global_e1h;
442
Harald Weltead384642008-12-26 10:20:07 +0000443 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000444 msgb_enqueue(&e1h->rsl_tx_list, msg);
445 e1h->fd[0].when |= BSC_FD_WRITE;
446
447 return 0;
448}
449
Harald Weltead384642008-12-26 10:20:07 +0000450int _abis_nm_sendmsg(struct msgb *msg)
Harald Welte8470bf22008-12-25 23:28:35 +0000451{
452 struct mi_e1_handle *e1h = global_e1h;
453
Harald Weltead384642008-12-26 10:20:07 +0000454 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000455 msgb_enqueue(&e1h->oml_tx_list, msg);
456 e1h->fd[0].when |= BSC_FD_WRITE;
457
458 return 0;
459}
460
Harald Weltee9a82612008-12-27 16:45:29 +0000461static int activate_bchan(struct mi_e1_handle *e1h, int ts)
462{
463 struct mISDNhead hh;
464 int ret;
465 struct bsc_fd *bfd = &e1h->fd[ts-1];
466
467 fprintf(stdout, "activate bchan\n");
468 hh.prim = PH_ACTIVATE_REQ;
469 hh.id = MISDN_ID_ANY;
470 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
471 if (ret < 0) {
472 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
473 strerror(errno));
474 return 0;
475 }
476
477 return ret;
478}
479
Harald Welte8470bf22008-12-25 23:28:35 +0000480static int mi_e1_setup(struct mi_e1_handle *e1h)
481{
482 int ts, sk, ret, cnt;
483 struct mISDN_devinfo devinfo;
Harald Welte52b1f982008-12-23 20:25:15 +0000484
485 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
Harald Welte8470bf22008-12-25 23:28:35 +0000486 if (sk < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000487 fprintf(stderr, "could not open socket %s\n", strerror(errno));
488 return sk;
489 }
490
491 ret = ioctl(sk, IMGETCOUNT, &cnt);
492 if (ret) {
493 fprintf(stderr, "error getting interf count: %s\n",
494 strerror(errno));
495 close(sk);
496 return -ENODEV;
497 }
Harald Weltead384642008-12-26 10:20:07 +0000498 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
499 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
500#if 1
501 devinfo.id = e1h->cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000502 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
503 if (ret < 0) {
504 fprintf(stdout, "error getting info for device %d: %s\n",
Harald Weltead384642008-12-26 10:20:07 +0000505 e1h->cardnr, strerror(errno));
Harald Welte52b1f982008-12-23 20:25:15 +0000506 return -ENODEV;
507 }
508 fprintf(stdout, " id: %d\n", devinfo.id);
509 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
510 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
511 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
512 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
513 fprintf(stdout, " name: %s\n", devinfo.name);
514#endif
515
516 /* TS0 is CRC4, don't need any fd for it */
517 for (ts = 1; ts < NUM_E1_TS; ts++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000518 unsigned int idx = ts-1;
Harald Welte52b1f982008-12-23 20:25:15 +0000519 struct bsc_fd *bfd = &e1h->fd[idx];
520 struct sockaddr_mISDN addr;
521
Harald Welte8470bf22008-12-25 23:28:35 +0000522 bfd->data = e1h;
523 bfd->priv_nr = ts;
524 bfd->cb = misdn_fd_cb;
525
526 if (ts == 1) {
Harald Weltead384642008-12-26 10:20:07 +0000527 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
Harald Welte8470bf22008-12-25 23:28:35 +0000528 bfd->when = BSC_FD_READ;
Harald Welte6cc38d72008-12-30 14:58:19 +0000529 } else
Harald Welte52b1f982008-12-23 20:25:15 +0000530 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
531
Harald Welte8470bf22008-12-25 23:28:35 +0000532 if (bfd->fd < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000533 fprintf(stderr, "could not open socket %s\n",
534 strerror(errno));
535 return bfd->fd;
536 }
537
538 memset(&addr, 0, sizeof(addr));
539 addr.family = AF_ISDN;
540 addr.dev = e1h->cardnr;
541 if (ts == 1) {
542 addr.channel = 0;
543 addr.sapi = 0;/* SAPI not supported yet in kernel */
544 addr.tei = TEI_L2ML;
545 } else
Harald Welte6cc38d72008-12-30 14:58:19 +0000546 addr.channel = ts;
Harald Welte52b1f982008-12-23 20:25:15 +0000547
548 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
549 if (ret < 0) {
Harald Welte8470bf22008-12-25 23:28:35 +0000550 fprintf(stderr, "could not bind l2 socket %s\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000551 strerror(errno));
552 return -EIO;
553 }
Harald Welte8470bf22008-12-25 23:28:35 +0000554
Harald Weltee9a82612008-12-27 16:45:29 +0000555 if (ts == 2) {
556 bfd->when = BSC_FD_READ;
557 activate_bchan(e1h, ts);
558 }
559
Harald Welte8470bf22008-12-25 23:28:35 +0000560 ret = bsc_register_fd(bfd);
561 if (ret < 0) {
562 fprintf(stderr, "could not register FD: %s\n",
563 strerror(ret));
564 return ret;
565 }
Harald Welte52b1f982008-12-23 20:25:15 +0000566 }
Harald Welte8470bf22008-12-25 23:28:35 +0000567
568 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000569}
570
Harald Weltead384642008-12-26 10:20:07 +0000571int mi_setup(struct gsm_bts *bts, int cardnr,
572 void (cb)(int event, struct gsm_bts *bts))
Harald Welte8470bf22008-12-25 23:28:35 +0000573{
574 struct mi_e1_handle *e1h;
575
576 e1h = malloc(sizeof(*e1h));
577 memset(e1h, 0, sizeof(*e1h));
578
579 e1h->cardnr = cardnr;
580 e1h->bts = bts;
Harald Weltead384642008-12-26 10:20:07 +0000581 e1h->cb = cb;
Harald Welte8470bf22008-12-25 23:28:35 +0000582 INIT_LLIST_HEAD(&e1h->oml_tx_list);
583 INIT_LLIST_HEAD(&e1h->rsl_tx_list);
584
585 global_e1h = e1h;
586
587 return mi_e1_setup(e1h);
588}