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> |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 28 | #include <sys/fcntl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <mISDNif.h> |
| 33 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 34 | //#define AF_COMPATIBILITY_FUNC |
| 35 | //#include <compat_af_isdn.h> |
| 36 | #define AF_ISDN 34 |
| 37 | #define PF_ISDN AF_ISDN |
| 38 | |
| 39 | #include <openbsc/select.h> |
| 40 | #include <openbsc/msgb.h> |
| 41 | #include <openbsc/debug.h> |
| 42 | #include <openbsc/gsm_data.h> |
| 43 | #include <openbsc/abis_nm.h> |
| 44 | #include <openbsc/abis_rsl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 45 | |
| 46 | #define NUM_E1_TS 32 |
| 47 | |
| 48 | /* data structure for one E1 interface with A-bis */ |
| 49 | struct mi_e1_handle { |
| 50 | struct gsm_bts *bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 51 | /* The mISDN card number of the card we use */ |
| 52 | int cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 53 | /* The RSL adress */ |
| 54 | struct sockaddr_mISDN l2addr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 55 | /* The OML adress */ |
| 56 | struct sockaddr_mISDN omladdr; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 57 | /* list (queue) of to-be-sent msgb's */ |
| 58 | struct llist_head rsl_tx_list; |
| 59 | struct llist_head oml_tx_list; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 60 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 61 | void (*cb)(int event, struct gsm_bts *bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 62 | struct bsc_fd fd[NUM_E1_TS]; |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 63 | |
| 64 | int ts2_fd; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 67 | /* FIXME: this needs to go */ |
| 68 | static struct mi_e1_handle *global_e1h; |
| 69 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 70 | #define SAPI_L2ML 0 |
| 71 | #define SAPI_OML 62 |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 72 | #define SAPI_RSL 0 /* 63 ? */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 73 | |
| 74 | #define TEI_L2ML 127 |
| 75 | #define TEI_OML 25 |
| 76 | #define TEI_RSL 1 |
| 77 | |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 78 | void hexdump(unsigned char *buf, int len) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 79 | { |
| 80 | int i; |
| 81 | for (i = 0; i < len; i++) { |
| 82 | fprintf(stdout, "%02x ", buf[i]); |
| 83 | } |
| 84 | fprintf(stdout, "\n"); |
| 85 | } |
| 86 | |
| 87 | #define TS1_ALLOC_SIZE 300 |
| 88 | |
| 89 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 90 | { |
| 91 | struct mi_e1_handle *e1h = bfd->data; |
| 92 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 93 | struct sockaddr_mISDN l2addr; |
| 94 | struct mISDNhead *hh; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 95 | socklen_t alen; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 96 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 97 | |
| 98 | if (!msg) |
| 99 | return -ENOMEM; |
| 100 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 101 | hh = (struct mISDNhead *) msg->data; |
| 102 | |
| 103 | /* FIXME: Map TEI/SAPI to TRX */ |
| 104 | msg->trx = e1h->bts->c0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 105 | |
| 106 | alen = sizeof(l2addr); |
| 107 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 108 | (struct sockaddr *) &l2addr, &alen); |
| 109 | if (ret < 0) { |
| 110 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | if (alen != sizeof(l2addr)) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | msgb_put(msg, ret); |
| 118 | |
| 119 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 120 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 121 | |
| 122 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n", |
| 123 | ret, hh->prim, hh->id); |
| 124 | |
| 125 | switch (hh->prim) { |
| 126 | case DL_INFORMATION_IND: |
| 127 | DEBUGP(DMI, "got DL_INFORMATION_IND\n"); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 128 | struct sockaddr_mISDN *sa = NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 129 | char *lstr = "UNKN"; |
| 130 | |
| 131 | switch (l2addr.tei) { |
| 132 | case TEI_OML: |
| 133 | sa = &e1h->omladdr; |
| 134 | lstr = "OML"; |
| 135 | break; |
| 136 | case TEI_RSL: |
| 137 | sa = &e1h->l2addr; |
| 138 | lstr = "RSL"; |
| 139 | break; |
| 140 | default: |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 141 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 142 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 143 | if (sa) { |
| 144 | DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n", |
| 145 | lstr, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 146 | memcpy(sa, &l2addr, sizeof(l2addr)); |
| 147 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 148 | break; |
| 149 | case DL_ESTABLISH_IND: |
| 150 | DEBUGP(DMI, "got DL_ESTABLISH_IND\n"); |
| 151 | break; |
| 152 | case DL_ESTABLISH_CNF: |
| 153 | DEBUGP(DMI, "got DL_ESTABLISH_CNF\n"); |
| 154 | break; |
| 155 | case DL_RELEASE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 156 | DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 157 | break; |
| 158 | case MPH_ACTIVATE_IND: |
| 159 | DEBUGP(DMI, "got MPH_ACTIVATE_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 160 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 161 | e1h->cb(EVT_E1_OML_UP, e1h->bts); |
| 162 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 163 | e1h->cb(EVT_E1_RSL_UP, e1h->bts); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 164 | break; |
| 165 | case MPH_DEACTIVATE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 166 | DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n"); |
| 167 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 168 | e1h->cb(EVT_E1_OML_DN, e1h->bts); |
| 169 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 170 | e1h->cb(EVT_E1_RSL_DN, e1h->bts); |
| 171 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 172 | case DL_DATA_IND: |
| 173 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 174 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 175 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 176 | |
| 177 | fprintf(stdout, "RX: "); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 178 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 179 | switch (l2addr.tei) { |
| 180 | case TEI_OML: |
| 181 | ret = abis_nm_rcvmsg(msg); |
| 182 | break; |
| 183 | case TEI_RSL: |
| 184 | ret = abis_rsl_rcvmsg(msg); |
| 185 | break; |
| 186 | default: |
| 187 | fprintf(stderr, "DATA_IND for unknown TEI\n"); |
| 188 | break; |
| 189 | } |
| 190 | break; |
| 191 | default: |
| 192 | DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim); |
| 193 | break; |
| 194 | } |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 199 | { |
| 200 | struct mi_e1_handle *e1h = bfd->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 201 | struct msgb *msg; |
| 202 | struct mISDNhead *hh; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 203 | int ret, no_oml = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 204 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 205 | msg = msgb_dequeue(&e1h->oml_tx_list); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 206 | if (!msg) |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 207 | no_oml = 1; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 208 | else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 209 | u_int8_t *l2_data = msg->data; |
| 210 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 211 | /* prepend the mISDNhead */ |
| 212 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 213 | hh->prim = DL_DATA_REQ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 214 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 215 | fprintf(stdout, "OML TX: "); |
| 216 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 217 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 218 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 219 | (struct sockaddr *)&e1h->omladdr, |
| 220 | sizeof(e1h->omladdr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 221 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 222 | usleep(100000); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 223 | /* we always dequeue all OML messages */ |
| 224 | return ret; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 225 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 226 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 227 | msg = msgb_dequeue(&e1h->rsl_tx_list); |
| 228 | if (!msg) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 229 | if (no_oml) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 230 | bfd->when &= ~BSC_FD_WRITE; |
| 231 | } else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 232 | u_int8_t *l2_data = msg->data; |
| 233 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 234 | /* prepend the mISDNhead */ |
| 235 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 236 | hh->prim = DL_DATA_REQ; |
| 237 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 238 | fprintf(stdout, "RSL TX: "); |
| 239 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 240 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 241 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 242 | (struct sockaddr *)&e1h->l2addr, |
| 243 | sizeof(e1h->l2addr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 244 | msgb_free(msg); |
Harald Welte | b275042 | 2008-12-27 11:24:23 +0000 | [diff] [blame] | 245 | usleep(10000); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 246 | //sleep(1); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 252 | #define TSX_ALLOC_SIZE 4096 |
| 253 | |
| 254 | /* FIXME: read from a B channel TS */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 255 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 256 | { |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 257 | struct mi_e1_handle *e1h = bfd->data; |
| 258 | struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE); |
| 259 | struct mISDNhead *hh; |
| 260 | int ret; |
| 261 | |
| 262 | if (!msg) |
| 263 | return -ENOMEM; |
| 264 | |
| 265 | hh = (struct mISDNhead *) msg->data; |
| 266 | |
| 267 | /* FIXME: Map TEI/SAPI to TRX */ |
| 268 | msg->trx = e1h->bts->c0; |
| 269 | |
| 270 | ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0); |
| 271 | if (ret < 0) { |
| 272 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | msgb_put(msg, ret); |
| 277 | |
| 278 | DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id); |
| 279 | |
| 280 | switch (hh->prim) { |
| 281 | case PH_CONTROL_IND: |
| 282 | DEBUGP(DMIB, "got PH_CONTROL_IND\n"); |
| 283 | break; |
| 284 | case PH_DATA_IND: |
| 285 | DEBUGP(DMIB, "got PH_DATA_IND\n"); |
| 286 | |
| 287 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 288 | |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 289 | #if 0 |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 290 | fprintf(stdout, "BCHAN RX: "); |
| 291 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 292 | #endif |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 293 | if (!e1h->ts2_fd) |
| 294 | e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660); |
| 295 | |
| 296 | write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 297 | |
| 298 | break; |
| 299 | default: |
| 300 | DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim); |
| 301 | break; |
| 302 | } |
Harald Welte | e27bb34 | 2008-12-29 06:06:35 +0000 | [diff] [blame] | 303 | /* FIXME: don't free it if we still hold reference! */ |
| 304 | msgb_free(msg); |
| 305 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 306 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 309 | static int handle_tsX_write(struct bsc_fd *bfd) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 310 | { |
| 311 | /* FIXME: write to a B channel TS */ |
Holger Freyther | 059c254 | 2008-12-27 09:39:48 +0000 | [diff] [blame] | 312 | return -1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* 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] | 316 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 317 | { |
| 318 | unsigned int e1_ts = bfd->priv_nr; |
| 319 | int rc = 0; |
| 320 | |
| 321 | switch (e1_ts) { |
| 322 | case 1: |
| 323 | if (what & BSC_FD_READ) |
| 324 | rc = handle_ts1_read(bfd); |
| 325 | if (what & BSC_FD_WRITE) |
| 326 | rc = handle_ts1_write(bfd); |
| 327 | break; |
| 328 | default: |
| 329 | if (what & BSC_FD_READ) |
| 330 | rc = handle_tsX_read(bfd); |
| 331 | if (what & BSC_FD_WRITE) |
| 332 | rc = handle_tsX_write(bfd); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | return rc; |
| 337 | } |
| 338 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 339 | int abis_rsl_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 340 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 341 | struct mi_e1_handle *e1h = global_e1h; |
| 342 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 343 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 344 | msgb_enqueue(&e1h->rsl_tx_list, msg); |
| 345 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 350 | int _abis_nm_sendmsg(struct msgb *msg) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 351 | { |
| 352 | struct mi_e1_handle *e1h = global_e1h; |
| 353 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 354 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 355 | msgb_enqueue(&e1h->oml_tx_list, msg); |
| 356 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 361 | static int activate_bchan(struct mi_e1_handle *e1h, int ts) |
| 362 | { |
| 363 | struct mISDNhead hh; |
| 364 | int ret; |
| 365 | struct bsc_fd *bfd = &e1h->fd[ts-1]; |
| 366 | |
| 367 | fprintf(stdout, "activate bchan\n"); |
| 368 | hh.prim = PH_ACTIVATE_REQ; |
| 369 | hh.id = MISDN_ID_ANY; |
| 370 | ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0); |
| 371 | if (ret < 0) { |
| 372 | fprintf(stdout, "could not send ACTIVATE_RQ %s\n", |
| 373 | strerror(errno)); |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | return ret; |
| 378 | } |
| 379 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 380 | static int mi_e1_setup(struct mi_e1_handle *e1h) |
| 381 | { |
| 382 | int ts, sk, ret, cnt; |
| 383 | struct mISDN_devinfo devinfo; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 384 | |
| 385 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 386 | if (sk < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 387 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 388 | return sk; |
| 389 | } |
| 390 | |
| 391 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 392 | if (ret) { |
| 393 | fprintf(stderr, "error getting interf count: %s\n", |
| 394 | strerror(errno)); |
| 395 | close(sk); |
| 396 | return -ENODEV; |
| 397 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 398 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 399 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 400 | #if 1 |
| 401 | devinfo.id = e1h->cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 402 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 403 | if (ret < 0) { |
| 404 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 405 | e1h->cardnr, strerror(errno)); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 406 | return -ENODEV; |
| 407 | } |
| 408 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 409 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 410 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 411 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 412 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 413 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 414 | #endif |
| 415 | |
| 416 | /* TS0 is CRC4, don't need any fd for it */ |
| 417 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 418 | unsigned int idx = ts-1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 419 | struct bsc_fd *bfd = &e1h->fd[idx]; |
| 420 | struct sockaddr_mISDN addr; |
| 421 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 422 | bfd->data = e1h; |
| 423 | bfd->priv_nr = ts; |
| 424 | bfd->cb = misdn_fd_cb; |
| 425 | |
| 426 | if (ts == 1) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 427 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 428 | bfd->when = BSC_FD_READ; |
| 429 | } else |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 430 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 431 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 432 | if (bfd->fd < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 433 | fprintf(stderr, "could not open socket %s\n", |
| 434 | strerror(errno)); |
| 435 | return bfd->fd; |
| 436 | } |
| 437 | |
| 438 | memset(&addr, 0, sizeof(addr)); |
| 439 | addr.family = AF_ISDN; |
| 440 | addr.dev = e1h->cardnr; |
| 441 | if (ts == 1) { |
| 442 | addr.channel = 0; |
| 443 | addr.sapi = 0;/* SAPI not supported yet in kernel */ |
| 444 | addr.tei = TEI_L2ML; |
| 445 | } else |
| 446 | addr.channel = ts; |
| 447 | |
| 448 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 449 | if (ret < 0) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 450 | fprintf(stderr, "could not bind l2 socket %s\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 451 | strerror(errno)); |
| 452 | return -EIO; |
| 453 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 454 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 455 | if (ts == 2) { |
| 456 | bfd->when = BSC_FD_READ; |
| 457 | activate_bchan(e1h, ts); |
| 458 | } |
| 459 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 460 | ret = bsc_register_fd(bfd); |
| 461 | if (ret < 0) { |
| 462 | fprintf(stderr, "could not register FD: %s\n", |
| 463 | strerror(ret)); |
| 464 | return ret; |
| 465 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 466 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 467 | |
| 468 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 471 | int mi_setup(struct gsm_bts *bts, int cardnr, |
| 472 | void (cb)(int event, struct gsm_bts *bts)) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 473 | { |
| 474 | struct mi_e1_handle *e1h; |
| 475 | |
| 476 | e1h = malloc(sizeof(*e1h)); |
| 477 | memset(e1h, 0, sizeof(*e1h)); |
| 478 | |
| 479 | e1h->cardnr = cardnr; |
| 480 | e1h->bts = bts; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 481 | e1h->cb = cb; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 482 | INIT_LLIST_HEAD(&e1h->oml_tx_list); |
| 483 | INIT_LLIST_HEAD(&e1h->rsl_tx_list); |
| 484 | |
| 485 | global_e1h = e1h; |
| 486 | |
| 487 | return mi_e1_setup(e1h); |
| 488 | } |