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 | |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 75 | void hexdump(unsigned char *buf, int len) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 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 | case DL_DATA_IND: |
| 170 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 171 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 172 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 173 | |
| 174 | fprintf(stdout, "RX: "); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 175 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 176 | switch (l2addr.tei) { |
| 177 | case TEI_OML: |
| 178 | ret = abis_nm_rcvmsg(msg); |
| 179 | break; |
| 180 | case TEI_RSL: |
| 181 | ret = abis_rsl_rcvmsg(msg); |
| 182 | break; |
| 183 | default: |
| 184 | fprintf(stderr, "DATA_IND for unknown TEI\n"); |
| 185 | break; |
| 186 | } |
| 187 | break; |
| 188 | default: |
| 189 | DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim); |
| 190 | break; |
| 191 | } |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 196 | { |
| 197 | struct mi_e1_handle *e1h = bfd->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 198 | struct msgb *msg; |
| 199 | struct mISDNhead *hh; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 200 | int ret, no_oml = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 201 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 202 | msg = msgb_dequeue(&e1h->oml_tx_list); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 203 | if (!msg) |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 204 | no_oml = 1; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 205 | else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 206 | u_int8_t *l2_data = msg->data; |
| 207 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 208 | /* prepend the mISDNhead */ |
| 209 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 210 | hh->prim = DL_DATA_REQ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 211 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 212 | fprintf(stdout, "OML TX: "); |
| 213 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 214 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 215 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 216 | (struct sockaddr *)&e1h->omladdr, |
| 217 | sizeof(e1h->omladdr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 218 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 219 | usleep(100000); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 220 | /* we always dequeue all OML messages */ |
| 221 | return ret; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 222 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 223 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 224 | msg = msgb_dequeue(&e1h->rsl_tx_list); |
| 225 | if (!msg) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 226 | if (no_oml) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 227 | bfd->when &= ~BSC_FD_WRITE; |
| 228 | } else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 229 | u_int8_t *l2_data = msg->data; |
| 230 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 231 | /* prepend the mISDNhead */ |
| 232 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 233 | hh->prim = DL_DATA_REQ; |
| 234 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 235 | fprintf(stdout, "RSL TX: "); |
| 236 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 237 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 238 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 239 | (struct sockaddr *)&e1h->l2addr, |
| 240 | sizeof(e1h->l2addr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 241 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 242 | usleep(100000); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 243 | //sleep(1); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 250 | { |
| 251 | /* FIXME: read from a B channel TS */ |
| 252 | } |
| 253 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 254 | static int handle_tsX_write(struct bsc_fd *bfd) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 255 | { |
| 256 | /* FIXME: write to a B channel TS */ |
| 257 | } |
| 258 | |
| 259 | /* 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] | 260 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 261 | { |
| 262 | unsigned int e1_ts = bfd->priv_nr; |
| 263 | int rc = 0; |
| 264 | |
| 265 | switch (e1_ts) { |
| 266 | case 1: |
| 267 | if (what & BSC_FD_READ) |
| 268 | rc = handle_ts1_read(bfd); |
| 269 | if (what & BSC_FD_WRITE) |
| 270 | rc = handle_ts1_write(bfd); |
| 271 | break; |
| 272 | default: |
| 273 | if (what & BSC_FD_READ) |
| 274 | rc = handle_tsX_read(bfd); |
| 275 | if (what & BSC_FD_WRITE) |
| 276 | rc = handle_tsX_write(bfd); |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | return rc; |
| 281 | } |
| 282 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 283 | int abis_rsl_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 284 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 285 | struct mi_e1_handle *e1h = global_e1h; |
| 286 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 287 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 288 | msgb_enqueue(&e1h->rsl_tx_list, msg); |
| 289 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 294 | int _abis_nm_sendmsg(struct msgb *msg) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 295 | { |
| 296 | struct mi_e1_handle *e1h = global_e1h; |
| 297 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 298 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 299 | msgb_enqueue(&e1h->oml_tx_list, msg); |
| 300 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int mi_e1_setup(struct mi_e1_handle *e1h) |
| 306 | { |
| 307 | int ts, sk, ret, cnt; |
| 308 | struct mISDN_devinfo devinfo; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 309 | |
| 310 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 311 | if (sk < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 312 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 313 | return sk; |
| 314 | } |
| 315 | |
| 316 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 317 | if (ret) { |
| 318 | fprintf(stderr, "error getting interf count: %s\n", |
| 319 | strerror(errno)); |
| 320 | close(sk); |
| 321 | return -ENODEV; |
| 322 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 323 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 324 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 325 | #if 1 |
| 326 | devinfo.id = e1h->cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 327 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 328 | if (ret < 0) { |
| 329 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 330 | e1h->cardnr, strerror(errno)); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 331 | return -ENODEV; |
| 332 | } |
| 333 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 334 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 335 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 336 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 337 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 338 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 339 | #endif |
| 340 | |
| 341 | /* TS0 is CRC4, don't need any fd for it */ |
| 342 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 343 | unsigned int idx = ts-1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 344 | struct bsc_fd *bfd = &e1h->fd[idx]; |
| 345 | struct sockaddr_mISDN addr; |
| 346 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 347 | bfd->data = e1h; |
| 348 | bfd->priv_nr = ts; |
| 349 | bfd->cb = misdn_fd_cb; |
| 350 | |
| 351 | if (ts == 1) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 352 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 353 | bfd->when = BSC_FD_READ; |
| 354 | } else |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 355 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 356 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 357 | |
| 358 | if (bfd->fd < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 359 | fprintf(stderr, "could not open socket %s\n", |
| 360 | strerror(errno)); |
| 361 | return bfd->fd; |
| 362 | } |
| 363 | |
| 364 | memset(&addr, 0, sizeof(addr)); |
| 365 | addr.family = AF_ISDN; |
| 366 | addr.dev = e1h->cardnr; |
| 367 | if (ts == 1) { |
| 368 | addr.channel = 0; |
| 369 | addr.sapi = 0;/* SAPI not supported yet in kernel */ |
| 370 | addr.tei = TEI_L2ML; |
| 371 | } else |
| 372 | addr.channel = ts; |
| 373 | |
| 374 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 375 | if (ret < 0) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 376 | fprintf(stderr, "could not bind l2 socket %s\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 377 | strerror(errno)); |
| 378 | return -EIO; |
| 379 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 380 | |
| 381 | ret = bsc_register_fd(bfd); |
| 382 | if (ret < 0) { |
| 383 | fprintf(stderr, "could not register FD: %s\n", |
| 384 | strerror(ret)); |
| 385 | return ret; |
| 386 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 387 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 388 | |
| 389 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 392 | int mi_setup(struct gsm_bts *bts, int cardnr, |
| 393 | void (cb)(int event, struct gsm_bts *bts)) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 394 | { |
| 395 | struct mi_e1_handle *e1h; |
| 396 | |
| 397 | e1h = malloc(sizeof(*e1h)); |
| 398 | memset(e1h, 0, sizeof(*e1h)); |
| 399 | |
| 400 | e1h->cardnr = cardnr; |
| 401 | e1h->bts = bts; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 402 | e1h->cb = cb; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 403 | INIT_LLIST_HEAD(&e1h->oml_tx_list); |
| 404 | INIT_LLIST_HEAD(&e1h->rsl_tx_list); |
| 405 | |
| 406 | global_e1h = e1h; |
| 407 | |
| 408 | return mi_e1_setup(e1h); |
| 409 | } |