Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1 | /* OpenBSC Abis interface to mISDNuser */ |
| 2 | |
| 3 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (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 |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <unistd.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <errno.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/socket.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <mISDNif.h> |
| 32 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 33 | //#define AF_COMPATIBILITY_FUNC |
| 34 | //#include <compat_af_isdn.h> |
| 35 | #define AF_ISDN 34 |
| 36 | #define PF_ISDN AF_ISDN |
| 37 | |
| 38 | #include <openbsc/select.h> |
| 39 | #include <openbsc/msgb.h> |
| 40 | #include <openbsc/debug.h> |
| 41 | #include <openbsc/gsm_data.h> |
| 42 | #include <openbsc/abis_nm.h> |
| 43 | #include <openbsc/abis_rsl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 44 | |
| 45 | #define NUM_E1_TS 32 |
| 46 | |
| 47 | /* data structure for one E1 interface with A-bis */ |
| 48 | struct mi_e1_handle { |
| 49 | struct gsm_bts *bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 50 | /* The mISDN card number of the card we use */ |
| 51 | int cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 52 | /* The RSL adress */ |
| 53 | struct sockaddr_mISDN l2addr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 54 | /* The OML adress */ |
| 55 | struct sockaddr_mISDN omladdr; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 56 | /* list (queue) of to-be-sent msgb's */ |
| 57 | struct llist_head rsl_tx_list; |
| 58 | struct llist_head oml_tx_list; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 59 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 60 | void (*cb)(int event, struct gsm_bts *bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 61 | struct bsc_fd fd[NUM_E1_TS]; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 64 | /* FIXME: this needs to go */ |
| 65 | static struct mi_e1_handle *global_e1h; |
| 66 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 67 | #define SAPI_L2ML 0 |
| 68 | #define SAPI_OML 62 |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 69 | #define SAPI_RSL 0 /* 63 ? */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 70 | |
| 71 | #define TEI_L2ML 127 |
| 72 | #define TEI_OML 25 |
| 73 | #define TEI_RSL 1 |
| 74 | |
| 75 | static void hexdump(unsigned char *buf, int len) |
| 76 | { |
| 77 | int i; |
| 78 | for (i = 0; i < len; i++) { |
| 79 | fprintf(stdout, "%02x ", buf[i]); |
| 80 | } |
| 81 | fprintf(stdout, "\n"); |
| 82 | } |
| 83 | |
| 84 | #define TS1_ALLOC_SIZE 300 |
| 85 | |
| 86 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 87 | { |
| 88 | struct mi_e1_handle *e1h = bfd->data; |
| 89 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 90 | struct sockaddr_mISDN l2addr; |
| 91 | struct mISDNhead *hh; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 92 | socklen_t alen; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 93 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 94 | |
| 95 | if (!msg) |
| 96 | return -ENOMEM; |
| 97 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 98 | hh = (struct mISDNhead *) msg->data; |
| 99 | |
| 100 | /* FIXME: Map TEI/SAPI to TRX */ |
| 101 | msg->trx = e1h->bts->c0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 102 | |
| 103 | alen = sizeof(l2addr); |
| 104 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 105 | (struct sockaddr *) &l2addr, &alen); |
| 106 | if (ret < 0) { |
| 107 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | if (alen != sizeof(l2addr)) |
| 112 | return -EINVAL; |
| 113 | |
| 114 | msgb_put(msg, ret); |
| 115 | |
| 116 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 117 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 118 | |
| 119 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n", |
| 120 | ret, hh->prim, hh->id); |
| 121 | |
| 122 | switch (hh->prim) { |
| 123 | case DL_INFORMATION_IND: |
| 124 | DEBUGP(DMI, "got DL_INFORMATION_IND\n"); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 125 | struct sockaddr_mISDN *sa = NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 126 | char *lstr = "UNKN"; |
| 127 | |
| 128 | switch (l2addr.tei) { |
| 129 | case TEI_OML: |
| 130 | sa = &e1h->omladdr; |
| 131 | lstr = "OML"; |
| 132 | break; |
| 133 | case TEI_RSL: |
| 134 | sa = &e1h->l2addr; |
| 135 | lstr = "RSL"; |
| 136 | break; |
| 137 | default: |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 138 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 139 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 140 | if (sa) { |
| 141 | DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n", |
| 142 | lstr, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 143 | memcpy(sa, &l2addr, sizeof(l2addr)); |
| 144 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 145 | break; |
| 146 | case DL_ESTABLISH_IND: |
| 147 | DEBUGP(DMI, "got DL_ESTABLISH_IND\n"); |
| 148 | break; |
| 149 | case DL_ESTABLISH_CNF: |
| 150 | DEBUGP(DMI, "got DL_ESTABLISH_CNF\n"); |
| 151 | break; |
| 152 | case DL_RELEASE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 153 | DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 154 | break; |
| 155 | case MPH_ACTIVATE_IND: |
| 156 | DEBUGP(DMI, "got MPH_ACTIVATE_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 157 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 158 | e1h->cb(EVT_E1_OML_UP, e1h->bts); |
| 159 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 160 | e1h->cb(EVT_E1_RSL_UP, e1h->bts); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 161 | break; |
| 162 | case MPH_DEACTIVATE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 163 | DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n"); |
| 164 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 165 | e1h->cb(EVT_E1_OML_DN, e1h->bts); |
| 166 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 167 | e1h->cb(EVT_E1_RSL_DN, e1h->bts); |
| 168 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 169 | break; |
| 170 | case DL_DATA_IND: |
| 171 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 172 | |
| 173 | /* FIXME: this stinks */ |
| 174 | msg->trx = e1h->bts->c0; |
| 175 | |
| 176 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 177 | |
| 178 | fprintf(stdout, "RX: "); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 179 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 180 | switch (l2addr.tei) { |
| 181 | case TEI_OML: |
| 182 | ret = abis_nm_rcvmsg(msg); |
| 183 | break; |
| 184 | case TEI_RSL: |
| 185 | ret = abis_rsl_rcvmsg(msg); |
| 186 | break; |
| 187 | default: |
| 188 | fprintf(stderr, "DATA_IND for unknown TEI\n"); |
| 189 | break; |
| 190 | } |
| 191 | break; |
| 192 | default: |
| 193 | DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim); |
| 194 | break; |
| 195 | } |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 200 | { |
| 201 | struct mi_e1_handle *e1h = bfd->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 202 | struct msgb *msg; |
| 203 | struct mISDNhead *hh; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 204 | int ret, no_oml = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 205 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 206 | msg = msgb_dequeue(&e1h->oml_tx_list); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 207 | if (!msg) |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 208 | no_oml = 1; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 209 | else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 210 | u_int8_t *l2_data = msg->data; |
| 211 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 212 | /* prepend the mISDNhead */ |
| 213 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 214 | hh->prim = DL_DATA_REQ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 215 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 216 | fprintf(stdout, "OML TX: "); |
| 217 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 218 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 219 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
| 220 | (struct sockaddr *)&e1h->l2addr, |
| 221 | sizeof(e1h->l2addr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 222 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 223 | usleep(100000); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 224 | /* we always dequeue all OML messages */ |
| 225 | return ret; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 226 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 227 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 228 | msg = msgb_dequeue(&e1h->rsl_tx_list); |
| 229 | if (!msg) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 230 | if (no_oml) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 231 | bfd->when &= ~BSC_FD_WRITE; |
| 232 | } else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 233 | u_int8_t *l2_data = msg->data; |
| 234 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 235 | /* prepend the mISDNhead */ |
| 236 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 237 | hh->prim = DL_DATA_REQ; |
| 238 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 239 | fprintf(stdout, "RSL TX: "); |
| 240 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 241 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 242 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
| 243 | (struct sockaddr *)&e1h->omladdr, |
| 244 | sizeof(e1h->omladdr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 245 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 246 | usleep(100000); |
| 247 | } |
| 248 | |
| 249 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 253 | { |
| 254 | /* FIXME: read from a B channel TS */ |
| 255 | } |
| 256 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 257 | static int handle_tsX_write(struct bsc_fd *bfd) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 258 | { |
| 259 | /* FIXME: write to a B channel TS */ |
| 260 | } |
| 261 | |
| 262 | /* callback from select.c in case one of the fd's can be read/written */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 263 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 264 | { |
| 265 | unsigned int e1_ts = bfd->priv_nr; |
| 266 | int rc = 0; |
| 267 | |
| 268 | switch (e1_ts) { |
| 269 | case 1: |
| 270 | if (what & BSC_FD_READ) |
| 271 | rc = handle_ts1_read(bfd); |
| 272 | if (what & BSC_FD_WRITE) |
| 273 | rc = handle_ts1_write(bfd); |
| 274 | break; |
| 275 | default: |
| 276 | if (what & BSC_FD_READ) |
| 277 | rc = handle_tsX_read(bfd); |
| 278 | if (what & BSC_FD_WRITE) |
| 279 | rc = handle_tsX_write(bfd); |
| 280 | break; |
| 281 | } |
| 282 | |
| 283 | return rc; |
| 284 | } |
| 285 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 286 | int abis_rsl_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 287 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 288 | struct mi_e1_handle *e1h = global_e1h; |
| 289 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 290 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 291 | msgb_enqueue(&e1h->rsl_tx_list, msg); |
| 292 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 297 | int _abis_nm_sendmsg(struct msgb *msg) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 298 | { |
| 299 | struct mi_e1_handle *e1h = global_e1h; |
| 300 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 301 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 302 | msgb_enqueue(&e1h->oml_tx_list, msg); |
| 303 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int mi_e1_setup(struct mi_e1_handle *e1h) |
| 309 | { |
| 310 | int ts, sk, ret, cnt; |
| 311 | struct mISDN_devinfo devinfo; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 312 | |
| 313 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 314 | if (sk < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 315 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 316 | return sk; |
| 317 | } |
| 318 | |
| 319 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 320 | if (ret) { |
| 321 | fprintf(stderr, "error getting interf count: %s\n", |
| 322 | strerror(errno)); |
| 323 | close(sk); |
| 324 | return -ENODEV; |
| 325 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 326 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 327 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 328 | #if 1 |
| 329 | devinfo.id = e1h->cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 330 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 331 | if (ret < 0) { |
| 332 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 333 | e1h->cardnr, strerror(errno)); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 334 | return -ENODEV; |
| 335 | } |
| 336 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 337 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 338 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 339 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 340 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 341 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 342 | #endif |
| 343 | |
| 344 | /* TS0 is CRC4, don't need any fd for it */ |
| 345 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 346 | unsigned int idx = ts-1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 347 | struct bsc_fd *bfd = &e1h->fd[idx]; |
| 348 | struct sockaddr_mISDN addr; |
| 349 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 350 | bfd->data = e1h; |
| 351 | bfd->priv_nr = ts; |
| 352 | bfd->cb = misdn_fd_cb; |
| 353 | |
| 354 | if (ts == 1) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 355 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 356 | bfd->when = BSC_FD_READ; |
| 357 | } else |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 358 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 359 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 360 | |
| 361 | if (bfd->fd < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 362 | fprintf(stderr, "could not open socket %s\n", |
| 363 | strerror(errno)); |
| 364 | return bfd->fd; |
| 365 | } |
| 366 | |
| 367 | memset(&addr, 0, sizeof(addr)); |
| 368 | addr.family = AF_ISDN; |
| 369 | addr.dev = e1h->cardnr; |
| 370 | if (ts == 1) { |
| 371 | addr.channel = 0; |
| 372 | addr.sapi = 0;/* SAPI not supported yet in kernel */ |
| 373 | addr.tei = TEI_L2ML; |
| 374 | } else |
| 375 | addr.channel = ts; |
| 376 | |
| 377 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 378 | if (ret < 0) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 379 | fprintf(stderr, "could not bind l2 socket %s\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 380 | strerror(errno)); |
| 381 | return -EIO; |
| 382 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 383 | |
| 384 | ret = bsc_register_fd(bfd); |
| 385 | if (ret < 0) { |
| 386 | fprintf(stderr, "could not register FD: %s\n", |
| 387 | strerror(ret)); |
| 388 | return ret; |
| 389 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 390 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 391 | |
| 392 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 395 | int mi_setup(struct gsm_bts *bts, int cardnr, |
| 396 | void (cb)(int event, struct gsm_bts *bts)) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 397 | { |
| 398 | struct mi_e1_handle *e1h; |
| 399 | |
| 400 | e1h = malloc(sizeof(*e1h)); |
| 401 | memset(e1h, 0, sizeof(*e1h)); |
| 402 | |
| 403 | e1h->cardnr = cardnr; |
| 404 | e1h->bts = bts; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame^] | 405 | e1h->cb = cb; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 406 | INIT_LLIST_HEAD(&e1h->oml_tx_list); |
| 407 | INIT_LLIST_HEAD(&e1h->rsl_tx_list); |
| 408 | |
| 409 | global_e1h = e1h; |
| 410 | |
| 411 | return mi_e1_setup(e1h); |
| 412 | } |