blob: d4c0914262955a980ca110ff27170a743e20cef4 [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 */
Harald Welte62a923d2009-01-03 01:32:00 +000055#define DLT_LINUX_LAPD 177
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000056#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
Harald Welte62a923d2009-01-03 01:32:00 +000076struct pseudo_isdn_header {
77 int32_t uton;
78 u_int8_t channel;
79} __attribute__((packed));
80
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000081struct fake_lapd_frame {
82 u_int8_t ea1 : 1;
83 u_int8_t cr : 1;
84 u_int8_t sapi : 6;
85 u_int8_t ea2 : 1;
86 u_int8_t tei : 7;
87 u_int8_t control_foo; /* fake UM's ... */
88} __attribute__((packed));
89
90static int pcap_fd = -1;
91
92void mi_set_pcap_fd(int fd)
93{
94 int ret;
95 struct pcap_hdr header = {
96 .magic_number = 0xa1b2c3d4,
97 .version_major = 2,
98 .version_minor = 4,
99 .thiszone = 0,
100 .sigfigs = 0,
101 .snaplen = 65535,
Harald Welte62a923d2009-01-03 01:32:00 +0000102 .network = DLT_LINUX_LAPD,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000103 };
104
105 pcap_fd = fd;
106 ret = write(pcap_fd, &header, sizeof(header));
107}
108
Harald Welte62a923d2009-01-03 01:32:00 +0000109/* This currently only works for the D-Channel */
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000110static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr,
111 struct msgb *msg) {
112 if (pcap_fd < 0)
113 return;
114
115 int ret;
Holger Freyther2139b242009-01-02 01:17:48 +0000116 time_t cur_time;
117 struct tm *tm;
Harald Welte62a923d2009-01-03 01:32:00 +0000118
119 struct pseudo_isdn_header isdn = {
120 /* user to network.. we are the network.. gboolean is a init */
121 .uton = 0,
122 .channel = addr->channel,
123 };
124
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000125 struct fake_lapd_frame header = {
126 .ea1 = 0,
127 .cr = PCAP_OUTPUT ? 1 : 0,
128 .sapi = addr->sapi & 0x3F,
129 .ea2 = 1,
130 .tei = addr->tei & 0x7F,
131 .control_foo = 0x13 /* UI with P set */,
132 };
133
134 struct pcaprec_hdr payload_header = {
135 .ts_sec = 0,
136 .ts_usec = 0,
Harald Welte62a923d2009-01-03 01:32:00 +0000137 .incl_len = msg->len + sizeof(struct fake_lapd_frame)
138 + sizeof(struct pseudo_isdn_header)
139 - MISDN_HEADER_LEN,
140 .orig_len = msg->len + sizeof(struct fake_lapd_frame)
141 + sizeof(struct pseudo_isdn_header)
142 - MISDN_HEADER_LEN,
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000143 };
144
Harald Welte62a923d2009-01-03 01:32:00 +0000145
146
Holger Freyther2139b242009-01-02 01:17:48 +0000147 cur_time = time(NULL);
148 tm = localtime(&cur_time);
149 payload_header.ts_sec = mktime(tm);
150
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000151 ret = write(pcap_fd, &payload_header, sizeof(payload_header));
Harald Welte62a923d2009-01-03 01:32:00 +0000152 ret = write(pcap_fd, &isdn, sizeof(isdn));
Harald Welteca6bf782009-01-03 00:41:04 +0000153 ret = write(pcap_fd, &header, sizeof(header));
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000154 ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN,
155 msg->len - MISDN_HEADER_LEN);
156}
157
Harald Welte52b1f982008-12-23 20:25:15 +0000158/* data structure for one E1 interface with A-bis */
159struct mi_e1_handle {
160 struct gsm_bts *bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000161 /* The mISDN card number of the card we use */
162 int cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000163 /* The RSL adress */
164 struct sockaddr_mISDN l2addr;
Harald Welte52b1f982008-12-23 20:25:15 +0000165 /* The OML adress */
166 struct sockaddr_mISDN omladdr;
Harald Welte8470bf22008-12-25 23:28:35 +0000167 /* list (queue) of to-be-sent msgb's */
168 struct llist_head rsl_tx_list;
169 struct llist_head oml_tx_list;
Harald Welte52b1f982008-12-23 20:25:15 +0000170
Harald Weltead384642008-12-26 10:20:07 +0000171 void (*cb)(int event, struct gsm_bts *bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000172 struct bsc_fd fd[NUM_E1_TS];
Harald Weltee9a82612008-12-27 16:45:29 +0000173
174 int ts2_fd;
Harald Welte52b1f982008-12-23 20:25:15 +0000175};
176
Harald Welte8470bf22008-12-25 23:28:35 +0000177/* FIXME: this needs to go */
178static struct mi_e1_handle *global_e1h;
179
Harald Welte52b1f982008-12-23 20:25:15 +0000180#define SAPI_L2ML 0
181#define SAPI_OML 62
Harald Weltead384642008-12-26 10:20:07 +0000182#define SAPI_RSL 0 /* 63 ? */
Harald Welte52b1f982008-12-23 20:25:15 +0000183
184#define TEI_L2ML 127
185#define TEI_OML 25
186#define TEI_RSL 1
187
Harald Welte702d8702008-12-26 20:25:35 +0000188void hexdump(unsigned char *buf, int len)
Harald Welte52b1f982008-12-23 20:25:15 +0000189{
190 int i;
191 for (i = 0; i < len; i++) {
192 fprintf(stdout, "%02x ", buf[i]);
193 }
194 fprintf(stdout, "\n");
195}
196
197#define TS1_ALLOC_SIZE 300
198
199static int handle_ts1_read(struct bsc_fd *bfd)
200{
201 struct mi_e1_handle *e1h = bfd->data;
202 struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE);
Harald Welte8470bf22008-12-25 23:28:35 +0000203 struct sockaddr_mISDN l2addr;
204 struct mISDNhead *hh;
Harald Welte52b1f982008-12-23 20:25:15 +0000205 socklen_t alen;
Harald Welte8470bf22008-12-25 23:28:35 +0000206 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000207
208 if (!msg)
209 return -ENOMEM;
210
Harald Welte8470bf22008-12-25 23:28:35 +0000211 hh = (struct mISDNhead *) msg->data;
212
213 /* FIXME: Map TEI/SAPI to TRX */
214 msg->trx = e1h->bts->c0;
Harald Welte52b1f982008-12-23 20:25:15 +0000215
216 alen = sizeof(l2addr);
217 ret = recvfrom(bfd->fd, msg->data, 300, 0,
218 (struct sockaddr *) &l2addr, &alen);
219 if (ret < 0) {
220 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
221 return ret;
222 }
223
224 if (alen != sizeof(l2addr))
225 return -EINVAL;
226
227 msgb_put(msg, ret);
228
229 DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n",
230 alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei);
231
232 DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n",
233 ret, hh->prim, hh->id);
234
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000235
Harald Welte52b1f982008-12-23 20:25:15 +0000236 switch (hh->prim) {
237 case DL_INFORMATION_IND:
238 DEBUGP(DMI, "got DL_INFORMATION_IND\n");
Harald Welte8470bf22008-12-25 23:28:35 +0000239 struct sockaddr_mISDN *sa = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000240 char *lstr = "UNKN";
241
242 switch (l2addr.tei) {
243 case TEI_OML:
244 sa = &e1h->omladdr;
245 lstr = "OML";
246 break;
247 case TEI_RSL:
248 sa = &e1h->l2addr;
249 lstr = "RSL";
250 break;
251 default:
Harald Welte8470bf22008-12-25 23:28:35 +0000252 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000253 }
Harald Welte8470bf22008-12-25 23:28:35 +0000254 if (sa) {
255 DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n",
256 lstr, l2addr.channel, l2addr.sapi, l2addr.tei);
257 memcpy(sa, &l2addr, sizeof(l2addr));
258 }
Harald Welte52b1f982008-12-23 20:25:15 +0000259 break;
260 case DL_ESTABLISH_IND:
261 DEBUGP(DMI, "got DL_ESTABLISH_IND\n");
262 break;
263 case DL_ESTABLISH_CNF:
264 DEBUGP(DMI, "got DL_ESTABLISH_CNF\n");
265 break;
266 case DL_RELEASE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000267 DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000268 break;
269 case MPH_ACTIVATE_IND:
270 DEBUGP(DMI, "got MPH_ACTIVATE_IND\n");
Harald Weltead384642008-12-26 10:20:07 +0000271 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
272 e1h->cb(EVT_E1_OML_UP, e1h->bts);
273 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
274 e1h->cb(EVT_E1_RSL_UP, e1h->bts);
Harald Welte52b1f982008-12-23 20:25:15 +0000275 break;
276 case MPH_DEACTIVATE_IND:
Harald Weltead384642008-12-26 10:20:07 +0000277 DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n");
278 if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML)
279 e1h->cb(EVT_E1_OML_DN, e1h->bts);
280 else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL)
281 e1h->cb(EVT_E1_RSL_DN, e1h->bts);
282 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000283 case DL_DATA_IND:
284 DEBUGP(DMI, "got DL_DATA_IND\n");
Holger Freyther52af7d42009-01-02 01:18:28 +0000285 write_pcap_packet(PCAP_INPUT, &l2addr, msg);
Harald Weltead384642008-12-26 10:20:07 +0000286
Harald Weltead384642008-12-26 10:20:07 +0000287 msg->l2h = msg->data + MISDN_HEADER_LEN;
288
Harald Welte6cc38d72008-12-30 14:58:19 +0000289 if (debug_mask & DMI) {
290 fprintf(stdout, "RX: ");
291 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
292 }
Harald Welte52b1f982008-12-23 20:25:15 +0000293 switch (l2addr.tei) {
294 case TEI_OML:
295 ret = abis_nm_rcvmsg(msg);
296 break;
297 case TEI_RSL:
298 ret = abis_rsl_rcvmsg(msg);
299 break;
300 default:
301 fprintf(stderr, "DATA_IND for unknown TEI\n");
302 break;
303 }
304 break;
305 default:
306 DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim);
307 break;
308 }
309 return ret;
310}
311
312static int handle_ts1_write(struct bsc_fd *bfd)
313{
314 struct mi_e1_handle *e1h = bfd->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000315 struct msgb *msg;
316 struct mISDNhead *hh;
Harald Weltead384642008-12-26 10:20:07 +0000317 int ret, no_oml = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000318
Harald Weltead384642008-12-26 10:20:07 +0000319 msg = msgb_dequeue(&e1h->oml_tx_list);
Harald Welte8470bf22008-12-25 23:28:35 +0000320 if (!msg)
Harald Weltead384642008-12-26 10:20:07 +0000321 no_oml = 1;
Harald Welte8470bf22008-12-25 23:28:35 +0000322 else {
Harald Weltead384642008-12-26 10:20:07 +0000323 u_int8_t *l2_data = msg->data;
324
Harald Welte8470bf22008-12-25 23:28:35 +0000325 /* prepend the mISDNhead */
326 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
327 hh->prim = DL_DATA_REQ;
Harald Welte52b1f982008-12-23 20:25:15 +0000328
Harald Welte6cc38d72008-12-30 14:58:19 +0000329 if (debug_mask & DMI) {
330 fprintf(stdout, "OML TX: ");
331 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
332 }
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000333
334 write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000335 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000336 (struct sockaddr *)&e1h->omladdr,
337 sizeof(e1h->omladdr));
Harald Weltee571fb82008-12-25 23:50:30 +0000338 msgb_free(msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000339 usleep(100000);
Harald Weltead384642008-12-26 10:20:07 +0000340 /* we always dequeue all OML messages */
341 return ret;
Harald Welte8470bf22008-12-25 23:28:35 +0000342 }
Harald Weltead384642008-12-26 10:20:07 +0000343
Harald Welte8470bf22008-12-25 23:28:35 +0000344 msg = msgb_dequeue(&e1h->rsl_tx_list);
345 if (!msg) {
Harald Weltead384642008-12-26 10:20:07 +0000346 if (no_oml)
Harald Welte8470bf22008-12-25 23:28:35 +0000347 bfd->when &= ~BSC_FD_WRITE;
348 } else {
Harald Weltead384642008-12-26 10:20:07 +0000349 u_int8_t *l2_data = msg->data;
350
Harald Welte8470bf22008-12-25 23:28:35 +0000351 /* prepend the mISDNhead */
352 hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh));
353 hh->prim = DL_DATA_REQ;
354
Harald Welte6cc38d72008-12-30 14:58:19 +0000355 if (debug_mask & DMI) {
356 fprintf(stdout, "RSL TX: ");
357 hexdump(l2_data, msg->len - MISDN_HEADER_LEN);
358 }
Harald Weltead384642008-12-26 10:20:07 +0000359
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000360 write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg);
Harald Welte8470bf22008-12-25 23:28:35 +0000361 ret = sendto(bfd->fd, msg->data, msg->len, 0,
Harald Welte5d2f8ec2008-12-26 10:46:24 +0000362 (struct sockaddr *)&e1h->l2addr,
363 sizeof(e1h->l2addr));
Harald Weltee571fb82008-12-25 23:50:30 +0000364 msgb_free(msg);
Harald Welteb2750422008-12-27 11:24:23 +0000365 usleep(10000);
Harald Welte702d8702008-12-26 20:25:35 +0000366 //sleep(1);
Harald Welte8470bf22008-12-25 23:28:35 +0000367 }
368
369 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000370}
371
Harald Weltee9a82612008-12-27 16:45:29 +0000372#define TSX_ALLOC_SIZE 4096
373
374/* FIXME: read from a B channel TS */
Harald Welte52b1f982008-12-23 20:25:15 +0000375static int handle_tsX_read(struct bsc_fd *bfd)
376{
Harald Weltee9a82612008-12-27 16:45:29 +0000377 struct mi_e1_handle *e1h = bfd->data;
378 struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE);
379 struct mISDNhead *hh;
380 int ret;
381
382 if (!msg)
383 return -ENOMEM;
384
385 hh = (struct mISDNhead *) msg->data;
386
387 /* FIXME: Map TEI/SAPI to TRX */
388 msg->trx = e1h->bts->c0;
389
390 ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0);
391 if (ret < 0) {
392 fprintf(stderr, "recvfrom error %s\n", strerror(errno));
393 return ret;
394 }
395
396 msgb_put(msg, ret);
397
398 DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id);
399
400 switch (hh->prim) {
401 case PH_CONTROL_IND:
402 DEBUGP(DMIB, "got PH_CONTROL_IND\n");
403 break;
404 case PH_DATA_IND:
405 DEBUGP(DMIB, "got PH_DATA_IND\n");
406
407 msg->l2h = msg->data + MISDN_HEADER_LEN;
408
Harald Welte6cc38d72008-12-30 14:58:19 +0000409 if (debug_mask & DMIB) {
410 fprintf(stdout, "BCHAN RX: ");
411 hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN);
412 }
Harald Weltee9a82612008-12-27 16:45:29 +0000413 if (!e1h->ts2_fd)
414 e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660);
415
416 write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN);
417
418 break;
419 default:
420 DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim);
421 break;
422 }
Harald Weltee27bb342008-12-29 06:06:35 +0000423 /* FIXME: don't free it if we still hold reference! */
424 msgb_free(msg);
425
Harald Weltee9a82612008-12-27 16:45:29 +0000426 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000427}
428
Harald Welte8470bf22008-12-25 23:28:35 +0000429static int handle_tsX_write(struct bsc_fd *bfd)
Harald Welte52b1f982008-12-23 20:25:15 +0000430{
431 /* FIXME: write to a B channel TS */
Holger Freyther059c2542008-12-27 09:39:48 +0000432 return -1;
Harald Welte52b1f982008-12-23 20:25:15 +0000433}
434
435/* callback from select.c in case one of the fd's can be read/written */
Harald Welte8470bf22008-12-25 23:28:35 +0000436static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what)
Harald Welte52b1f982008-12-23 20:25:15 +0000437{
438 unsigned int e1_ts = bfd->priv_nr;
439 int rc = 0;
440
441 switch (e1_ts) {
442 case 1:
443 if (what & BSC_FD_READ)
444 rc = handle_ts1_read(bfd);
445 if (what & BSC_FD_WRITE)
446 rc = handle_ts1_write(bfd);
447 break;
448 default:
449 if (what & BSC_FD_READ)
450 rc = handle_tsX_read(bfd);
451 if (what & BSC_FD_WRITE)
452 rc = handle_tsX_write(bfd);
453 break;
454 }
455
456 return rc;
457}
458
Harald Welte8470bf22008-12-25 23:28:35 +0000459int abis_rsl_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000460{
Harald Welte8470bf22008-12-25 23:28:35 +0000461 struct mi_e1_handle *e1h = global_e1h;
462
Harald Weltead384642008-12-26 10:20:07 +0000463 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000464 msgb_enqueue(&e1h->rsl_tx_list, msg);
465 e1h->fd[0].when |= BSC_FD_WRITE;
466
467 return 0;
468}
469
Harald Weltead384642008-12-26 10:20:07 +0000470int _abis_nm_sendmsg(struct msgb *msg)
Harald Welte8470bf22008-12-25 23:28:35 +0000471{
472 struct mi_e1_handle *e1h = global_e1h;
473
Harald Weltead384642008-12-26 10:20:07 +0000474 msg->l2h = msg->data;
Harald Welte8470bf22008-12-25 23:28:35 +0000475 msgb_enqueue(&e1h->oml_tx_list, msg);
476 e1h->fd[0].when |= BSC_FD_WRITE;
477
478 return 0;
479}
480
Harald Weltee9a82612008-12-27 16:45:29 +0000481static int activate_bchan(struct mi_e1_handle *e1h, int ts)
482{
483 struct mISDNhead hh;
484 int ret;
485 struct bsc_fd *bfd = &e1h->fd[ts-1];
486
487 fprintf(stdout, "activate bchan\n");
488 hh.prim = PH_ACTIVATE_REQ;
489 hh.id = MISDN_ID_ANY;
490 ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0);
491 if (ret < 0) {
492 fprintf(stdout, "could not send ACTIVATE_RQ %s\n",
493 strerror(errno));
494 return 0;
495 }
496
497 return ret;
498}
499
Harald Welte8470bf22008-12-25 23:28:35 +0000500static int mi_e1_setup(struct mi_e1_handle *e1h)
501{
502 int ts, sk, ret, cnt;
503 struct mISDN_devinfo devinfo;
Harald Welte52b1f982008-12-23 20:25:15 +0000504
505 sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE);
Harald Welte8470bf22008-12-25 23:28:35 +0000506 if (sk < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000507 fprintf(stderr, "could not open socket %s\n", strerror(errno));
508 return sk;
509 }
510
511 ret = ioctl(sk, IMGETCOUNT, &cnt);
512 if (ret) {
513 fprintf(stderr, "error getting interf count: %s\n",
514 strerror(errno));
515 close(sk);
516 return -ENODEV;
517 }
Harald Weltead384642008-12-26 10:20:07 +0000518 //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s");
519 printf("%d device%s found\n", cnt, (cnt==1)?"":"s");
520#if 1
521 devinfo.id = e1h->cardnr;
Harald Welte52b1f982008-12-23 20:25:15 +0000522 ret = ioctl(sk, IMGETDEVINFO, &devinfo);
523 if (ret < 0) {
524 fprintf(stdout, "error getting info for device %d: %s\n",
Harald Weltead384642008-12-26 10:20:07 +0000525 e1h->cardnr, strerror(errno));
Harald Welte52b1f982008-12-23 20:25:15 +0000526 return -ENODEV;
527 }
528 fprintf(stdout, " id: %d\n", devinfo.id);
529 fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols);
530 fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols);
531 fprintf(stdout, " protocol: %d\n", devinfo.protocol);
532 fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan);
533 fprintf(stdout, " name: %s\n", devinfo.name);
534#endif
535
536 /* TS0 is CRC4, don't need any fd for it */
537 for (ts = 1; ts < NUM_E1_TS; ts++) {
Harald Welte8470bf22008-12-25 23:28:35 +0000538 unsigned int idx = ts-1;
Harald Welte52b1f982008-12-23 20:25:15 +0000539 struct bsc_fd *bfd = &e1h->fd[idx];
540 struct sockaddr_mISDN addr;
541
Harald Welte8470bf22008-12-25 23:28:35 +0000542 bfd->data = e1h;
543 bfd->priv_nr = ts;
544 bfd->cb = misdn_fd_cb;
545
546 if (ts == 1) {
Harald Weltead384642008-12-26 10:20:07 +0000547 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT);
Harald Welte8470bf22008-12-25 23:28:35 +0000548 bfd->when = BSC_FD_READ;
Harald Welte6cc38d72008-12-30 14:58:19 +0000549 } else
Harald Welte52b1f982008-12-23 20:25:15 +0000550 bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
551
Harald Welte8470bf22008-12-25 23:28:35 +0000552 if (bfd->fd < 0) {
Harald Welte52b1f982008-12-23 20:25:15 +0000553 fprintf(stderr, "could not open socket %s\n",
554 strerror(errno));
555 return bfd->fd;
556 }
557
558 memset(&addr, 0, sizeof(addr));
559 addr.family = AF_ISDN;
560 addr.dev = e1h->cardnr;
561 if (ts == 1) {
562 addr.channel = 0;
563 addr.sapi = 0;/* SAPI not supported yet in kernel */
564 addr.tei = TEI_L2ML;
565 } else
Harald Welte6cc38d72008-12-30 14:58:19 +0000566 addr.channel = ts;
Harald Welte52b1f982008-12-23 20:25:15 +0000567
568 ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
569 if (ret < 0) {
Harald Welte8470bf22008-12-25 23:28:35 +0000570 fprintf(stderr, "could not bind l2 socket %s\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000571 strerror(errno));
572 return -EIO;
573 }
Harald Welte8470bf22008-12-25 23:28:35 +0000574
Harald Weltee9a82612008-12-27 16:45:29 +0000575 if (ts == 2) {
576 bfd->when = BSC_FD_READ;
577 activate_bchan(e1h, ts);
578 }
579
Harald Welte8470bf22008-12-25 23:28:35 +0000580 ret = bsc_register_fd(bfd);
581 if (ret < 0) {
582 fprintf(stderr, "could not register FD: %s\n",
583 strerror(ret));
584 return ret;
585 }
Harald Welte52b1f982008-12-23 20:25:15 +0000586 }
Harald Welte8470bf22008-12-25 23:28:35 +0000587
588 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000589}
590
Harald Weltead384642008-12-26 10:20:07 +0000591int mi_setup(struct gsm_bts *bts, int cardnr,
592 void (cb)(int event, struct gsm_bts *bts))
Harald Welte8470bf22008-12-25 23:28:35 +0000593{
594 struct mi_e1_handle *e1h;
595
596 e1h = malloc(sizeof(*e1h));
597 memset(e1h, 0, sizeof(*e1h));
598
599 e1h->cardnr = cardnr;
600 e1h->bts = bts;
Harald Weltead384642008-12-26 10:20:07 +0000601 e1h->cb = cb;
Harald Welte8470bf22008-12-25 23:28:35 +0000602 INIT_LLIST_HEAD(&e1h->oml_tx_list);
603 INIT_LLIST_HEAD(&e1h->rsl_tx_list);
604
605 global_e1h = e1h;
606
607 return mi_e1_setup(e1h);
608}