blob: 6e45f2a4967acb103cb9248a391fe9b750fac3f9 [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 Welte4e0d1372009-01-03 03:04:07 +000085static_assert((int)&((struct fake_linux_lapd_header*)NULL)->hatype == 2, hatype_offset);
86static_assert((int)&((struct fake_linux_lapd_header*)NULL)->halen == 4, halen_offset);
87static_assert((int)&((struct fake_linux_lapd_header*)NULL)->addr == 6, addr_offset);
88static_assert((int)&((struct fake_linux_lapd_header*)NULL)->protocol == 14, proto_offset);
89static_assert(sizeof(struct fake_linux_lapd_header) == 16, lapd_header_size);
90
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000091
92static int pcap_fd = -1;
93
94void mi_set_pcap_fd(int fd)
95{
96 int ret;
97 struct pcap_hdr header = {
98 .magic_number = 0xa1b2c3d4,
99 .version_major = 2,
100 .version_minor = 4,
101 .thiszone = 0,
102 .sigfigs = 0,
103 .snaplen = 65535,
Harald Welte62a923d2009-01-03 01:32:00 +0000104 .network = DLT_LINUX_LAPD,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000105 };
106
107 pcap_fd = fd;
108 ret = write(pcap_fd, &header, sizeof(header));
109}
110
Harald Welte62a923d2009-01-03 01:32:00 +0000111/* This currently only works for the D-Channel */
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000112static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
113 struct msgb *msg) {
114 if (pcap_fd < 0)
115 return;
116
117 int ret;
Holger Freyther2139b242009-01-02 01:17:48 +0000118 time_t cur_time;
119 struct tm *tm;
Harald Welte62a923d2009-01-03 01:32:00 +0000120
Harald Welte4e0d1372009-01-03 03:04:07 +0000121 struct fake_linux_lapd_header header = {
122 .pkttype = 4,
123 .hatype = 0,
124 .halen = 0,
125 .addr = 0x1,
126 .protocol = ntohs(48),
Harald Welte62a923d2009-01-03 01:32:00 +0000127 };
Harald Welte4e0d1372009-01-03 03:04:07 +0000128
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000129
130 struct pcaprec_hdr payload_header = {
131 .ts_sec = 0,
132 .ts_usec = 0,
Harald Welte4e0d1372009-01-03 03:04:07 +0000133 .incl_len = msg->len + sizeof(struct fake_linux_lapd_header)
Harald Welte62a923d2009-01-03 01:32:00 +0000134 - MISDN_HEADER_LEN,
Harald Welte4e0d1372009-01-03 03:04:07 +0000135 .orig_len = msg->len + sizeof(struct fake_linux_lapd_header)
Harald Welte62a923d2009-01-03 01:32:00 +0000136 - MISDN_HEADER_LEN,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000137 };
138
Harald Welte62a923d2009-01-03 01:32:00 +0000139
Holger Freyther2139b242009-01-02 01:17:48 +0000140 cur_time = time(NULL);
141 tm = localtime(&cur_time);
142 payload_header.ts_sec = mktime(tm);
143
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000144 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
Harald Welteca6bf782009-01-03 00:41:04 +0000145 ret = write(pcap_fd, &header, sizeof(header));
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000146 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
147 msg->len - MISDN_HEADER_LEN);
148}
149
Harald Welte52b1f982008-12-23 20:25:15 +0000150/* data structure for one E1 interface with A-bis */
151struct mi_e1_handle {
152 struct gsm_bts *bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000153 /* The mISDN card number of the card we use */
154 int cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000155 /* The RSL adress */
156 struct sockaddr_mISDN l2addr;
Harald Welte52b1f982008-12-23 20:25:15 +0000157 /* The OML adress */
158 struct sockaddr_mISDN omladdr;
Harald Welte8470bf22008-12-25 23:28:35 +0000159 /* list (queue) of to-be-sent msgb's */
160 struct llist_head rsl_tx_list;
161 struct llist_head oml_tx_list;
Harald Welte52b1f982008-12-23 20:25:15 +0000162
Harald Weltead384642008-12-26 10:20:07 +0000163 void (*cb)(int event, struct gsm_bts *bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000164 struct bsc_fd fd[NUM_E1_TS];
Harald Weltee9a82612008-12-27 16:45:29 +0000165
166 int ts2_fd;
Harald Welte52b1f982008-12-23 20:25:15 +0000167};
168
Harald Welte8470bf22008-12-25 23:28:35 +0000169/* FIXME: this needs to go */
170static struct mi_e1_handle *global_e1h;
171
Harald Welte52b1f982008-12-23 20:25:15 +0000172#define SAPI_L2ML 0
173#define SAPI_OML 62
Harald Weltead384642008-12-26 10:20:07 +0000174#define SAPI_RSL 0 /* 63 ? */
Harald Welte52b1f982008-12-23 20:25:15 +0000175
176#define TEI_L2ML 127
177#define TEI_OML 25
178#define TEI_RSL 1
179
Harald Welte702d8702008-12-26 20:25:35 +0000180void hexdump(unsigned char *buf, int len)
Harald Welte52b1f982008-12-23 20:25:15 +0000181{
182 int i;
183 for (i = 0; i < len; i++) {
184 fprintf(stdout, "%02x ", buf[i]);
185 }
186 fprintf(stdout, "\n");
187}
188
189#define TS1_ALLOC_SIZE 300
190
191static int handle_ts1_read(struct bsc_fd *bfd)
192{
193 struct mi_e1_handle *e1h = bfd->data;
194 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
Harald Welte8470bf22008-12-25 23:28:35 +0000195 struct sockaddr_mISDN l2addr;
196 struct mISDNhead *hh;
Harald Welte52b1f982008-12-23 20:25:15 +0000197 socklen_t alen;
Harald Welte8470bf22008-12-25 23:28:35 +0000198 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000199
200 if (!msg)
201 return -ENOMEM;
202
Harald Welte8470bf22008-12-25 23:28:35 +0000203 hh = (struct mISDNhead *) msg->data;
204
205 /* FIXME: Map TEI/SAPI to TRX */
206 msg->trx = e1h->bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000207
208 alen = sizeof(l2addr);
209 ret = recvfrom(bfd->fd, msg->data, 300, 0,
210 (struct sockaddr *) &l2addr, &alen);
211 if (ret < 0) {
212 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
213 return ret;
214 }
215
216 if (alen != sizeof(l2addr))
217 return -EINVAL;
218
219 msgb_put(msg, ret);
220
221 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
222 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
223
224 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n",
225 ret, hh->prim, hh->id);
226
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000227
Harald Welte52b1f982008-12-23 20:25:15 +0000228 switch (hh->prim) {
229 case DL_INFORMATION_IND:
230 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000231 struct sockaddr_mISDN *sa = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000232 char *lstr = "UNKN";
233
234 switch (l2addr.tei) {
235 case TEI_OML:
236 sa = &e1h->omladdr;
237 lstr = "OML";
238 break;
239 case TEI_RSL:
240 sa = &e1h->l2addr;
241 lstr = "RSL";
242 break;
243 default:
Harald Welte8470bf22008-12-25 23:28:35 +0000244 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000245 }
Harald Welte8470bf22008-12-25 23:28:35 +0000246 if (sa) {
247 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
248 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
249 memcpy(sa, &l2addr, sizeof(l2addr));
250 }
Harald Welte52b1f982008-12-23 20:25:15 +0000251 break;
252 case DL_ESTABLISH_IND:
253 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
254 break;
255 case DL_ESTABLISH_CNF:
256 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
257 break;
258 case DL_RELEASE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000259 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000260 break;
261 case MPH_ACTIVATE_IND:
262 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000263 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
264 e1h->cb(EVT_E1_OML_UP, e1h->bts);
265 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
266 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000267 break;
268 case MPH_DEACTIVATE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000269 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
270 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
271 e1h->cb(EVT_E1_OML_DN, e1h->bts);
272 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
273 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
274 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000275 case DL_DATA_IND:
276 DEBUGP(DMI, "got DL_DATA_IND\n");
Holger Freyther52af7d42009-01-02 01:18:28 +0000277 write_pcap_packet(PCAP_INPUT, &l2addr, msg);
Harald Weltead384642008-12-26 10:20:07 +0000278
Harald Weltead384642008-12-26 10:20:07 +0000279 msg->l2h = msg->data + MISDN_HEADER_LEN;
280
Harald Welte6cc38d72008-12-30 14:58:19 +0000281 if (debug_mask & DMI) {
282 fprintf(stdout, "RX: ");
283 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
284 }
Harald Welte52b1f982008-12-23 20:25:15 +0000285 switch (l2addr.tei) {
286 case TEI_OML:
287 ret = abis_nm_rcvmsg(msg);
288 break;
289 case TEI_RSL:
290 ret = abis_rsl_rcvmsg(msg);
291 break;
292 default:
293 fprintf(stderr, "DATA_IND for unknown TEI\n");
294 break;
295 }
296 break;
297 default:
298 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
299 break;
300 }
301 return ret;
302}
303
304static int handle_ts1_write(struct bsc_fd *bfd)
305{
306 struct mi_e1_handle *e1h = bfd->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000307 struct msgb *msg;
308 struct mISDNhead *hh;
Harald Weltead384642008-12-26 10:20:07 +0000309 int ret, no_oml = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000310
Harald Weltead384642008-12-26 10:20:07 +0000311 msg = msgb_dequeue(&e1h->oml_tx_list);
Harald Welte8470bf22008-12-25 23:28:35 +0000312 if (!msg)
Harald Weltead384642008-12-26 10:20:07 +0000313 no_oml = 1;
Harald Welte8470bf22008-12-25 23:28:35 +0000314 else {
Harald Weltead384642008-12-26 10:20:07 +0000315 u_int8_t *l2_data = msg->data;
316
Harald Welte8470bf22008-12-25 23:28:35 +0000317 /* prepend the mISDNhead */
318 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
319 hh->prim = DL_DATA_REQ;
Harald Welte52b1f982008-12-23 20:25:15 +0000320
Harald Welte6cc38d72008-12-30 14:58:19 +0000321 if (debug_mask & DMI) {
322 fprintf(stdout, "OML TX: ");
323 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
324 }
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000325
326 write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000327 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000328 (struct sockaddr *)&e1h->omladdr,
329 sizeof(e1h->omladdr));
Harald Weltee571fb82008-12-25 23:50:30 +0000330 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000331 usleep(100000);
Harald Weltead384642008-12-26 10:20:07 +0000332 /* we always dequeue all OML messages */
333 return ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000334 }
Harald Weltead384642008-12-26 10:20:07 +0000335
Harald Welte8470bf22008-12-25 23:28:35 +0000336 msg = msgb_dequeue(&e1h->rsl_tx_list);
337 if (!msg) {
Harald Weltead384642008-12-26 10:20:07 +0000338 if (no_oml)
Harald Welte8470bf22008-12-25 23:28:35 +0000339 bfd->when &= ~BSC_FD_WRITE;
340 } else {
Harald Weltead384642008-12-26 10:20:07 +0000341 u_int8_t *l2_data = msg->data;
342
Harald Welte8470bf22008-12-25 23:28:35 +0000343 /* prepend the mISDNhead */
344 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
345 hh->prim = DL_DATA_REQ;
346
Harald Welte6cc38d72008-12-30 14:58:19 +0000347 if (debug_mask & DMI) {
348 fprintf(stdout, "RSL TX: ");
349 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
350 }
Harald Weltead384642008-12-26 10:20:07 +0000351
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000352 write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000353 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000354 (struct sockaddr *)&e1h->l2addr,
355 sizeof(e1h->l2addr));
Harald Weltee571fb82008-12-25 23:50:30 +0000356 msgb_free(msg);
Harald Welteb2750422008-12-27 11:24:23 +0000357 usleep(10000);
Harald Welte702d8702008-12-26 20:25:35 +0000358 //sleep(1);
Harald Welte8470bf22008-12-25 23:28:35 +0000359 }
360
361 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000362}
363
Harald Weltee9a82612008-12-27 16:45:29 +0000364#define TSX_ALLOC_SIZE 4096
365
366/* FIXME: read from a B channel TS */
Harald Welte52b1f982008-12-23 20:25:15 +0000367static int handle_tsX_read(struct bsc_fd *bfd)
368{
Harald Weltee9a82612008-12-27 16:45:29 +0000369 struct mi_e1_handle *e1h = bfd->data;
370 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
371 struct mISDNhead *hh;
372 int ret;
373
374 if (!msg)
375 return -ENOMEM;
376
377 hh = (struct mISDNhead *) msg->data;
378
379 /* FIXME: Map TEI/SAPI to TRX */
380 msg->trx = e1h->bts->c0;
381
382 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
383 if (ret < 0) {
384 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
385 return ret;
386 }
387
388 msgb_put(msg, ret);
389
390 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id);
391
392 switch (hh->prim) {
393 case PH_CONTROL_IND:
394 DEBUGP(DMIB, "got PH_CONTROL_IND\n");
395 break;
396 case PH_DATA_IND:
397 DEBUGP(DMIB, "got PH_DATA_IND\n");
398
399 msg->l2h = msg->data + MISDN_HEADER_LEN;
400
Harald Welte6cc38d72008-12-30 14:58:19 +0000401 if (debug_mask & DMIB) {
402 fprintf(stdout, "BCHAN RX: ");
403 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
404 }
Harald Weltee9a82612008-12-27 16:45:29 +0000405 if (!e1h->ts2_fd)
406 e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660);
407
408 write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN);
409
410 break;
411 default:
412 DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim);
413 break;
414 }
Harald Weltee27bb342008-12-29 06:06:35 +0000415 /* FIXME: don't free it if we still hold reference! */
416 msgb_free(msg);
417
Harald Weltee9a82612008-12-27 16:45:29 +0000418 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000419}
420
Harald Welte8470bf22008-12-25 23:28:35 +0000421static int handle_tsX_write(struct bsc_fd *bfd)
Harald Welte52b1f982008-12-23 20:25:15 +0000422{
423 /* FIXME: write to a B channel TS */
Holger Freyther059c2542008-12-27 09:39:48 +0000424 return -1;
Harald Welte52b1f982008-12-23 20:25:15 +0000425}
426
427/* callback from select.c in case one of the fd's can be read/written */
Harald Welte8470bf22008-12-25 23:28:35 +0000428static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
Harald Welte52b1f982008-12-23 20:25:15 +0000429{
430 unsigned int e1_ts = bfd->priv_nr;
431 int rc = 0;
432
433 switch (e1_ts) {
434 case 1:
435 if (what & BSC_FD_READ)
436 rc = handle_ts1_read(bfd);
437 if (what & BSC_FD_WRITE)
438 rc = handle_ts1_write(bfd);
439 break;
440 default:
441 if (what & BSC_FD_READ)
442 rc = handle_tsX_read(bfd);
443 if (what & BSC_FD_WRITE)
444 rc = handle_tsX_write(bfd);
445 break;
446 }
447
448 return rc;
449}
450
Harald Welte8470bf22008-12-25 23:28:35 +0000451int abis_rsl_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000452{
Harald Welte8470bf22008-12-25 23:28:35 +0000453 struct mi_e1_handle *e1h = global_e1h;
454
Harald Weltead384642008-12-26 10:20:07 +0000455 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000456 msgb_enqueue(&e1h->rsl_tx_list, msg);
457 e1h->fd[0].when |= BSC_FD_WRITE;
458
459 return 0;
460}
461
Harald Weltead384642008-12-26 10:20:07 +0000462int _abis_nm_sendmsg(struct msgb *msg)
Harald Welte8470bf22008-12-25 23:28:35 +0000463{
464 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->oml_tx_list, msg);
468 e1h->fd[0].when |= BSC_FD_WRITE;
469
470 return 0;
471}
472
Harald Weltee9a82612008-12-27 16:45:29 +0000473static int activate_bchan(struct mi_e1_handle *e1h, int ts)
474{
475 struct mISDNhead hh;
476 int ret;
477 struct bsc_fd *bfd = &e1h->fd[ts-1];
478
479 fprintf(stdout, "activate bchan\n");
480 hh.prim = PH_ACTIVATE_REQ;
481 hh.id = MISDN_ID_ANY;
482 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
483 if (ret < 0) {
484 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
485 strerror(errno));
486 return 0;
487 }
488
489 return ret;
490}
491
Harald Welte8470bf22008-12-25 23:28:35 +0000492static int mi_e1_setup(struct mi_e1_handle *e1h)
493{
494 int ts, sk, ret, cnt;
495 struct mISDN_devinfo devinfo;
Harald Welte52b1f982008-12-23 20:25:15 +0000496
497 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
Harald Welte8470bf22008-12-25 23:28:35 +0000498 if (sk < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000499 fprintf(stderr, "could not open socket %s\n", strerror(errno));
500 return sk;
501 }
502
503 ret = ioctl(sk, IMGETCOUNT, &cnt);
504 if (ret) {
505 fprintf(stderr, "error getting interf count: %s\n",
506 strerror(errno));
507 close(sk);
508 return -ENODEV;
509 }
Harald Weltead384642008-12-26 10:20:07 +0000510 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
511 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
512#if 1
513 devinfo.id = e1h->cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000514 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
515 if (ret < 0) {
516 fprintf(stdout, "error getting info for device %d: %s\n",
Harald Weltead384642008-12-26 10:20:07 +0000517 e1h->cardnr, strerror(errno));
Harald Welte52b1f982008-12-23 20:25:15 +0000518 return -ENODEV;
519 }
520 fprintf(stdout, " id: %d\n", devinfo.id);
521 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
522 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
523 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
524 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
525 fprintf(stdout, " name: %s\n", devinfo.name);
526#endif
527
528 /* TS0 is CRC4, don't need any fd for it */
529 for (ts = 1; ts < NUM_E1_TS; ts++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000530 unsigned int idx = ts-1;
Harald Welte52b1f982008-12-23 20:25:15 +0000531 struct bsc_fd *bfd = &e1h->fd[idx];
532 struct sockaddr_mISDN addr;
533
Harald Welte8470bf22008-12-25 23:28:35 +0000534 bfd->data = e1h;
535 bfd->priv_nr = ts;
536 bfd->cb = misdn_fd_cb;
537
538 if (ts == 1) {
Harald Weltead384642008-12-26 10:20:07 +0000539 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
Harald Welte8470bf22008-12-25 23:28:35 +0000540 bfd->when = BSC_FD_READ;
Harald Welte6cc38d72008-12-30 14:58:19 +0000541 } else
Harald Welte52b1f982008-12-23 20:25:15 +0000542 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
543
Harald Welte8470bf22008-12-25 23:28:35 +0000544 if (bfd->fd < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000545 fprintf(stderr, "could not open socket %s\n",
546 strerror(errno));
547 return bfd->fd;
548 }
549
550 memset(&addr, 0, sizeof(addr));
551 addr.family = AF_ISDN;
552 addr.dev = e1h->cardnr;
553 if (ts == 1) {
554 addr.channel = 0;
555 addr.sapi = 0;/* SAPI not supported yet in kernel */
556 addr.tei = TEI_L2ML;
557 } else
Harald Welte6cc38d72008-12-30 14:58:19 +0000558 addr.channel = ts;
Harald Welte52b1f982008-12-23 20:25:15 +0000559
560 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
561 if (ret < 0) {
Harald Welte8470bf22008-12-25 23:28:35 +0000562 fprintf(stderr, "could not bind l2 socket %s\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000563 strerror(errno));
564 return -EIO;
565 }
Harald Welte8470bf22008-12-25 23:28:35 +0000566
Harald Weltee9a82612008-12-27 16:45:29 +0000567 if (ts == 2) {
568 bfd->when = BSC_FD_READ;
569 activate_bchan(e1h, ts);
570 }
571
Harald Welte8470bf22008-12-25 23:28:35 +0000572 ret = bsc_register_fd(bfd);
573 if (ret < 0) {
574 fprintf(stderr, "could not register FD: %s\n",
575 strerror(ret));
576 return ret;
577 }
Harald Welte52b1f982008-12-23 20:25:15 +0000578 }
Harald Welte8470bf22008-12-25 23:28:35 +0000579
580 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000581}
582
Harald Weltead384642008-12-26 10:20:07 +0000583int mi_setup(struct gsm_bts *bts, int cardnr,
584 void (cb)(int event, struct gsm_bts *bts))
Harald Welte8470bf22008-12-25 23:28:35 +0000585{
586 struct mi_e1_handle *e1h;
587
588 e1h = malloc(sizeof(*e1h));
589 memset(e1h, 0, sizeof(*e1h));
590
591 e1h->cardnr = cardnr;
592 e1h->bts = bts;
Harald Weltead384642008-12-26 10:20:07 +0000593 e1h->cb = cb;
Harald Welte8470bf22008-12-25 23:28:35 +0000594 INIT_LLIST_HEAD(&e1h->oml_tx_list);
595 INIT_LLIST_HEAD(&e1h->rsl_tx_list);
596
597 global_e1h = e1h;
598
599 return mi_e1_setup(e1h);
600}