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> |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 5 | * |
| 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 Freyther | 2139b24 | 2009-01-02 01:17:48 +0000 | [diff] [blame^] | 29 | #include <time.h> |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 30 | #include <sys/fcntl.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <mISDNif.h> |
| 35 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 36 | //#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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 47 | |
| 48 | #define NUM_E1_TS 32 |
| 49 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * pcap writing of the misdn load |
| 53 | * pcap format is from http://wiki.wireshark.org/Development/LibpcapFileFormat |
| 54 | */ |
| 55 | #define WTAP_ENCAP_ISDN 17 |
| 56 | #define PCAP_INPUT 0 |
| 57 | #define PCAP_OUTPUT 1 |
| 58 | |
| 59 | struct 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 | |
| 69 | struct 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 | |
| 76 | struct fake_lapd_frame { |
| 77 | u_int8_t ea1 : 1; |
| 78 | u_int8_t cr : 1; |
| 79 | u_int8_t sapi : 6; |
| 80 | u_int8_t ea2 : 1; |
| 81 | u_int8_t tei : 7; |
| 82 | u_int8_t control_foo; /* fake UM's ... */ |
| 83 | } __attribute__((packed)); |
| 84 | |
| 85 | static int pcap_fd = -1; |
| 86 | |
| 87 | void mi_set_pcap_fd(int fd) |
| 88 | { |
| 89 | int ret; |
| 90 | struct pcap_hdr header = { |
| 91 | .magic_number = 0xa1b2c3d4, |
| 92 | .version_major = 2, |
| 93 | .version_minor = 4, |
| 94 | .thiszone = 0, |
| 95 | .sigfigs = 0, |
| 96 | .snaplen = 65535, |
| 97 | .network = WTAP_ENCAP_ISDN, |
| 98 | }; |
| 99 | |
| 100 | pcap_fd = fd; |
| 101 | ret = write(pcap_fd, &header, sizeof(header)); |
| 102 | } |
| 103 | |
| 104 | static void write_pcap_packet(int direction, struct sockaddr_mISDN* addr, |
| 105 | struct msgb *msg) { |
| 106 | if (pcap_fd < 0) |
| 107 | return; |
| 108 | |
| 109 | int ret; |
Holger Freyther | 2139b24 | 2009-01-02 01:17:48 +0000 | [diff] [blame^] | 110 | time_t cur_time; |
| 111 | struct tm *tm; |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 112 | struct fake_lapd_frame header = { |
| 113 | .ea1 = 0, |
| 114 | .cr = PCAP_OUTPUT ? 1 : 0, |
| 115 | .sapi = addr->sapi & 0x3F, |
| 116 | .ea2 = 1, |
| 117 | .tei = addr->tei & 0x7F, |
| 118 | .control_foo = 0x13 /* UI with P set */, |
| 119 | }; |
| 120 | |
| 121 | struct pcaprec_hdr payload_header = { |
| 122 | .ts_sec = 0, |
| 123 | .ts_usec = 0, |
| 124 | .incl_len = msg->len + sizeof(header) - MISDN_HEADER_LEN, |
| 125 | .orig_len = msg->len + sizeof(header) - MISDN_HEADER_LEN, |
| 126 | }; |
| 127 | |
Holger Freyther | 2139b24 | 2009-01-02 01:17:48 +0000 | [diff] [blame^] | 128 | cur_time = time(NULL); |
| 129 | tm = localtime(&cur_time); |
| 130 | payload_header.ts_sec = mktime(tm); |
| 131 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 132 | ret = write(pcap_fd, &header, sizeof(header)); |
| 133 | ret = write(pcap_fd, &payload_header, sizeof(payload_header)); |
| 134 | ret = write(pcap_fd, msg->data + MISDN_HEADER_LEN, |
| 135 | msg->len - MISDN_HEADER_LEN); |
| 136 | } |
| 137 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 138 | /* data structure for one E1 interface with A-bis */ |
| 139 | struct mi_e1_handle { |
| 140 | struct gsm_bts *bts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 141 | /* The mISDN card number of the card we use */ |
| 142 | int cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 143 | /* The RSL adress */ |
| 144 | struct sockaddr_mISDN l2addr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 145 | /* The OML adress */ |
| 146 | struct sockaddr_mISDN omladdr; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 147 | /* list (queue) of to-be-sent msgb's */ |
| 148 | struct llist_head rsl_tx_list; |
| 149 | struct llist_head oml_tx_list; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 150 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 151 | void (*cb)(int event, struct gsm_bts *bts); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 152 | struct bsc_fd fd[NUM_E1_TS]; |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 153 | |
| 154 | int ts2_fd; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 157 | /* FIXME: this needs to go */ |
| 158 | static struct mi_e1_handle *global_e1h; |
| 159 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 160 | #define SAPI_L2ML 0 |
| 161 | #define SAPI_OML 62 |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 162 | #define SAPI_RSL 0 /* 63 ? */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 163 | |
| 164 | #define TEI_L2ML 127 |
| 165 | #define TEI_OML 25 |
| 166 | #define TEI_RSL 1 |
| 167 | |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 168 | void hexdump(unsigned char *buf, int len) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 169 | { |
| 170 | int i; |
| 171 | for (i = 0; i < len; i++) { |
| 172 | fprintf(stdout, "%02x ", buf[i]); |
| 173 | } |
| 174 | fprintf(stdout, "\n"); |
| 175 | } |
| 176 | |
| 177 | #define TS1_ALLOC_SIZE 300 |
| 178 | |
| 179 | static int handle_ts1_read(struct bsc_fd *bfd) |
| 180 | { |
| 181 | struct mi_e1_handle *e1h = bfd->data; |
| 182 | struct msgb *msg = msgb_alloc(TS1_ALLOC_SIZE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 183 | struct sockaddr_mISDN l2addr; |
| 184 | struct mISDNhead *hh; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 185 | socklen_t alen; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 186 | int ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 187 | |
| 188 | if (!msg) |
| 189 | return -ENOMEM; |
| 190 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 191 | hh = (struct mISDNhead *) msg->data; |
| 192 | |
| 193 | /* FIXME: Map TEI/SAPI to TRX */ |
| 194 | msg->trx = e1h->bts->c0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 195 | |
| 196 | alen = sizeof(l2addr); |
| 197 | ret = recvfrom(bfd->fd, msg->data, 300, 0, |
| 198 | (struct sockaddr *) &l2addr, &alen); |
| 199 | if (ret < 0) { |
| 200 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | if (alen != sizeof(l2addr)) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | msgb_put(msg, ret); |
| 208 | |
| 209 | DEBUGP(DMI, "alen =%d, dev(%d) channel(%d) sapi(%d) tei(%d)\n", |
| 210 | alen, l2addr.dev, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 211 | |
| 212 | DEBUGP(DMI, "<= len = %d, prim(0x%x) id(0x%x)\n", |
| 213 | ret, hh->prim, hh->id); |
| 214 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 215 | write_pcap_packet(PCAP_INPUT, &l2addr, msg); |
| 216 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 217 | switch (hh->prim) { |
| 218 | case DL_INFORMATION_IND: |
| 219 | DEBUGP(DMI, "got DL_INFORMATION_IND\n"); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 220 | struct sockaddr_mISDN *sa = NULL; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 221 | char *lstr = "UNKN"; |
| 222 | |
| 223 | switch (l2addr.tei) { |
| 224 | case TEI_OML: |
| 225 | sa = &e1h->omladdr; |
| 226 | lstr = "OML"; |
| 227 | break; |
| 228 | case TEI_RSL: |
| 229 | sa = &e1h->l2addr; |
| 230 | lstr = "RSL"; |
| 231 | break; |
| 232 | default: |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 233 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 234 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 235 | if (sa) { |
| 236 | DEBUGP(DMI, "%s use channel(%d) sapi(%d) tei(%d) for now\n", |
| 237 | lstr, l2addr.channel, l2addr.sapi, l2addr.tei); |
| 238 | memcpy(sa, &l2addr, sizeof(l2addr)); |
| 239 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 240 | break; |
| 241 | case DL_ESTABLISH_IND: |
| 242 | DEBUGP(DMI, "got DL_ESTABLISH_IND\n"); |
| 243 | break; |
| 244 | case DL_ESTABLISH_CNF: |
| 245 | DEBUGP(DMI, "got DL_ESTABLISH_CNF\n"); |
| 246 | break; |
| 247 | case DL_RELEASE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 248 | DEBUGP(DMI, "got DL_RELEASE_IND: E1 Layer 1 disappeared?\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 249 | break; |
| 250 | case MPH_ACTIVATE_IND: |
| 251 | DEBUGP(DMI, "got MPH_ACTIVATE_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 252 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 253 | e1h->cb(EVT_E1_OML_UP, e1h->bts); |
| 254 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 255 | e1h->cb(EVT_E1_RSL_UP, e1h->bts); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 256 | break; |
| 257 | case MPH_DEACTIVATE_IND: |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 258 | DEBUGP(DMI, "got MPH_DEACTIVATE_IND: TEI link closed?\n"); |
| 259 | if (l2addr.tei == TEI_OML && l2addr.sapi == SAPI_OML) |
| 260 | e1h->cb(EVT_E1_OML_DN, e1h->bts); |
| 261 | else if (l2addr.tei == TEI_RSL && l2addr.sapi == SAPI_RSL) |
| 262 | e1h->cb(EVT_E1_RSL_DN, e1h->bts); |
| 263 | break; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 264 | case DL_DATA_IND: |
| 265 | DEBUGP(DMI, "got DL_DATA_IND\n"); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 266 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 267 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 268 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 269 | if (debug_mask & DMI) { |
| 270 | fprintf(stdout, "RX: "); |
| 271 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 272 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 273 | switch (l2addr.tei) { |
| 274 | case TEI_OML: |
| 275 | ret = abis_nm_rcvmsg(msg); |
| 276 | break; |
| 277 | case TEI_RSL: |
| 278 | ret = abis_rsl_rcvmsg(msg); |
| 279 | break; |
| 280 | default: |
| 281 | fprintf(stderr, "DATA_IND for unknown TEI\n"); |
| 282 | break; |
| 283 | } |
| 284 | break; |
| 285 | default: |
| 286 | DEBUGP(DMI, "got unexpected 0x%x prim\n", hh->prim); |
| 287 | break; |
| 288 | } |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static int handle_ts1_write(struct bsc_fd *bfd) |
| 293 | { |
| 294 | struct mi_e1_handle *e1h = bfd->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 295 | struct msgb *msg; |
| 296 | struct mISDNhead *hh; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 297 | int ret, no_oml = 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 298 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 299 | msg = msgb_dequeue(&e1h->oml_tx_list); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 300 | if (!msg) |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 301 | no_oml = 1; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 302 | else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 303 | u_int8_t *l2_data = msg->data; |
| 304 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 305 | /* prepend the mISDNhead */ |
| 306 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 307 | hh->prim = DL_DATA_REQ; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 308 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 309 | if (debug_mask & DMI) { |
| 310 | fprintf(stdout, "OML TX: "); |
| 311 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 312 | } |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 313 | |
| 314 | write_pcap_packet(PCAP_OUTPUT, &e1h->omladdr, msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 315 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 316 | (struct sockaddr *)&e1h->omladdr, |
| 317 | sizeof(e1h->omladdr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 318 | msgb_free(msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 319 | usleep(100000); |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 320 | /* we always dequeue all OML messages */ |
| 321 | return ret; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 322 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 323 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 324 | msg = msgb_dequeue(&e1h->rsl_tx_list); |
| 325 | if (!msg) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 326 | if (no_oml) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 327 | bfd->when &= ~BSC_FD_WRITE; |
| 328 | } else { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 329 | u_int8_t *l2_data = msg->data; |
| 330 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 331 | /* prepend the mISDNhead */ |
| 332 | hh = (struct mISDNhead *) msgb_push(msg, sizeof(*hh)); |
| 333 | hh->prim = DL_DATA_REQ; |
| 334 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 335 | if (debug_mask & DMI) { |
| 336 | fprintf(stdout, "RSL TX: "); |
| 337 | hexdump(l2_data, msg->len - MISDN_HEADER_LEN); |
| 338 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 339 | |
Holger Freyther | 9a3ee0f | 2009-01-02 00:40:15 +0000 | [diff] [blame] | 340 | write_pcap_packet(PCAP_OUTPUT, &e1h->l2addr, msg); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 341 | ret = sendto(bfd->fd, msg->data, msg->len, 0, |
Harald Welte | 5d2f8ec | 2008-12-26 10:46:24 +0000 | [diff] [blame] | 342 | (struct sockaddr *)&e1h->l2addr, |
| 343 | sizeof(e1h->l2addr)); |
Harald Welte | e571fb8 | 2008-12-25 23:50:30 +0000 | [diff] [blame] | 344 | msgb_free(msg); |
Harald Welte | b275042 | 2008-12-27 11:24:23 +0000 | [diff] [blame] | 345 | usleep(10000); |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 346 | //sleep(1); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 352 | #define TSX_ALLOC_SIZE 4096 |
| 353 | |
| 354 | /* FIXME: read from a B channel TS */ |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 355 | static int handle_tsX_read(struct bsc_fd *bfd) |
| 356 | { |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 357 | struct mi_e1_handle *e1h = bfd->data; |
| 358 | struct msgb *msg = msgb_alloc(TSX_ALLOC_SIZE); |
| 359 | struct mISDNhead *hh; |
| 360 | int ret; |
| 361 | |
| 362 | if (!msg) |
| 363 | return -ENOMEM; |
| 364 | |
| 365 | hh = (struct mISDNhead *) msg->data; |
| 366 | |
| 367 | /* FIXME: Map TEI/SAPI to TRX */ |
| 368 | msg->trx = e1h->bts->c0; |
| 369 | |
| 370 | ret = recv(bfd->fd, msg->data, TSX_ALLOC_SIZE, 0); |
| 371 | if (ret < 0) { |
| 372 | fprintf(stderr, "recvfrom error %s\n", strerror(errno)); |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | msgb_put(msg, ret); |
| 377 | |
| 378 | DEBUGP(DMIB, "<= BCHAN len = %d, prim(0x%x) id(0x%x)\n", ret, hh->prim, hh->id); |
| 379 | |
| 380 | switch (hh->prim) { |
| 381 | case PH_CONTROL_IND: |
| 382 | DEBUGP(DMIB, "got PH_CONTROL_IND\n"); |
| 383 | break; |
| 384 | case PH_DATA_IND: |
| 385 | DEBUGP(DMIB, "got PH_DATA_IND\n"); |
| 386 | |
| 387 | msg->l2h = msg->data + MISDN_HEADER_LEN; |
| 388 | |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 389 | if (debug_mask & DMIB) { |
| 390 | fprintf(stdout, "BCHAN RX: "); |
| 391 | hexdump(msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 392 | } |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 393 | if (!e1h->ts2_fd) |
| 394 | e1h->ts2_fd = open("/tmp/ts2.dump", O_WRONLY|O_APPEND|O_CREAT, 0660); |
| 395 | |
| 396 | write(e1h->ts2_fd, msgb_l2(msg), ret - MISDN_HEADER_LEN); |
| 397 | |
| 398 | break; |
| 399 | default: |
| 400 | DEBUGP(DMIB, "got unexpected 0x%x prim\n", hh->prim); |
| 401 | break; |
| 402 | } |
Harald Welte | e27bb34 | 2008-12-29 06:06:35 +0000 | [diff] [blame] | 403 | /* FIXME: don't free it if we still hold reference! */ |
| 404 | msgb_free(msg); |
| 405 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 406 | return ret; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 409 | static int handle_tsX_write(struct bsc_fd *bfd) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 410 | { |
| 411 | /* FIXME: write to a B channel TS */ |
Holger Freyther | 059c254 | 2008-12-27 09:39:48 +0000 | [diff] [blame] | 412 | return -1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* 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] | 416 | static int misdn_fd_cb(struct bsc_fd *bfd, unsigned int what) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 417 | { |
| 418 | unsigned int e1_ts = bfd->priv_nr; |
| 419 | int rc = 0; |
| 420 | |
| 421 | switch (e1_ts) { |
| 422 | case 1: |
| 423 | if (what & BSC_FD_READ) |
| 424 | rc = handle_ts1_read(bfd); |
| 425 | if (what & BSC_FD_WRITE) |
| 426 | rc = handle_ts1_write(bfd); |
| 427 | break; |
| 428 | default: |
| 429 | if (what & BSC_FD_READ) |
| 430 | rc = handle_tsX_read(bfd); |
| 431 | if (what & BSC_FD_WRITE) |
| 432 | rc = handle_tsX_write(bfd); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | return rc; |
| 437 | } |
| 438 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 439 | int abis_rsl_sendmsg(struct msgb *msg) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 440 | { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 441 | struct mi_e1_handle *e1h = global_e1h; |
| 442 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 443 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 444 | msgb_enqueue(&e1h->rsl_tx_list, msg); |
| 445 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 450 | int _abis_nm_sendmsg(struct msgb *msg) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 451 | { |
| 452 | struct mi_e1_handle *e1h = global_e1h; |
| 453 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 454 | msg->l2h = msg->data; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 455 | msgb_enqueue(&e1h->oml_tx_list, msg); |
| 456 | e1h->fd[0].when |= BSC_FD_WRITE; |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 461 | static int activate_bchan(struct mi_e1_handle *e1h, int ts) |
| 462 | { |
| 463 | struct mISDNhead hh; |
| 464 | int ret; |
| 465 | struct bsc_fd *bfd = &e1h->fd[ts-1]; |
| 466 | |
| 467 | fprintf(stdout, "activate bchan\n"); |
| 468 | hh.prim = PH_ACTIVATE_REQ; |
| 469 | hh.id = MISDN_ID_ANY; |
| 470 | ret = sendto(bfd->fd, &hh, sizeof(hh), 0, NULL, 0); |
| 471 | if (ret < 0) { |
| 472 | fprintf(stdout, "could not send ACTIVATE_RQ %s\n", |
| 473 | strerror(errno)); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | return ret; |
| 478 | } |
| 479 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 480 | static int mi_e1_setup(struct mi_e1_handle *e1h) |
| 481 | { |
| 482 | int ts, sk, ret, cnt; |
| 483 | struct mISDN_devinfo devinfo; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 484 | |
| 485 | sk = socket(PF_ISDN, SOCK_RAW, ISDN_P_BASE); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 486 | if (sk < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 487 | fprintf(stderr, "could not open socket %s\n", strerror(errno)); |
| 488 | return sk; |
| 489 | } |
| 490 | |
| 491 | ret = ioctl(sk, IMGETCOUNT, &cnt); |
| 492 | if (ret) { |
| 493 | fprintf(stderr, "error getting interf count: %s\n", |
| 494 | strerror(errno)); |
| 495 | close(sk); |
| 496 | return -ENODEV; |
| 497 | } |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 498 | //DEBUGP(DMI,"%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 499 | printf("%d device%s found\n", cnt, (cnt==1)?"":"s"); |
| 500 | #if 1 |
| 501 | devinfo.id = e1h->cardnr; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 502 | ret = ioctl(sk, IMGETDEVINFO, &devinfo); |
| 503 | if (ret < 0) { |
| 504 | fprintf(stdout, "error getting info for device %d: %s\n", |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 505 | e1h->cardnr, strerror(errno)); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 506 | return -ENODEV; |
| 507 | } |
| 508 | fprintf(stdout, " id: %d\n", devinfo.id); |
| 509 | fprintf(stdout, " Dprotocols: %08x\n", devinfo.Dprotocols); |
| 510 | fprintf(stdout, " Bprotocols: %08x\n", devinfo.Bprotocols); |
| 511 | fprintf(stdout, " protocol: %d\n", devinfo.protocol); |
| 512 | fprintf(stdout, " nrbchan: %d\n", devinfo.nrbchan); |
| 513 | fprintf(stdout, " name: %s\n", devinfo.name); |
| 514 | #endif |
| 515 | |
| 516 | /* TS0 is CRC4, don't need any fd for it */ |
| 517 | for (ts = 1; ts < NUM_E1_TS; ts++) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 518 | unsigned int idx = ts-1; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 519 | struct bsc_fd *bfd = &e1h->fd[idx]; |
| 520 | struct sockaddr_mISDN addr; |
| 521 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 522 | bfd->data = e1h; |
| 523 | bfd->priv_nr = ts; |
| 524 | bfd->cb = misdn_fd_cb; |
| 525 | |
| 526 | if (ts == 1) { |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 527 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_LAPD_NT); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 528 | bfd->when = BSC_FD_READ; |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 529 | } else |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 530 | bfd->fd = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW); |
| 531 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 532 | if (bfd->fd < 0) { |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 533 | fprintf(stderr, "could not open socket %s\n", |
| 534 | strerror(errno)); |
| 535 | return bfd->fd; |
| 536 | } |
| 537 | |
| 538 | memset(&addr, 0, sizeof(addr)); |
| 539 | addr.family = AF_ISDN; |
| 540 | addr.dev = e1h->cardnr; |
| 541 | if (ts == 1) { |
| 542 | addr.channel = 0; |
| 543 | addr.sapi = 0;/* SAPI not supported yet in kernel */ |
| 544 | addr.tei = TEI_L2ML; |
| 545 | } else |
Harald Welte | 6cc38d7 | 2008-12-30 14:58:19 +0000 | [diff] [blame] | 546 | addr.channel = ts; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 547 | |
| 548 | ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr)); |
| 549 | if (ret < 0) { |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 550 | fprintf(stderr, "could not bind l2 socket %s\n", |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 551 | strerror(errno)); |
| 552 | return -EIO; |
| 553 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 554 | |
Harald Welte | e9a8261 | 2008-12-27 16:45:29 +0000 | [diff] [blame] | 555 | if (ts == 2) { |
| 556 | bfd->when = BSC_FD_READ; |
| 557 | activate_bchan(e1h, ts); |
| 558 | } |
| 559 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 560 | ret = bsc_register_fd(bfd); |
| 561 | if (ret < 0) { |
| 562 | fprintf(stderr, "could not register FD: %s\n", |
| 563 | strerror(ret)); |
| 564 | return ret; |
| 565 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 566 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 567 | |
| 568 | return 0; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 571 | int mi_setup(struct gsm_bts *bts, int cardnr, |
| 572 | void (cb)(int event, struct gsm_bts *bts)) |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 573 | { |
| 574 | struct mi_e1_handle *e1h; |
| 575 | |
| 576 | e1h = malloc(sizeof(*e1h)); |
| 577 | memset(e1h, 0, sizeof(*e1h)); |
| 578 | |
| 579 | e1h->cardnr = cardnr; |
| 580 | e1h->bts = bts; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 581 | e1h->cb = cb; |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 582 | INIT_LLIST_HEAD(&e1h->oml_tx_list); |
| 583 | INIT_LLIST_HEAD(&e1h->rsl_tx_list); |
| 584 | |
| 585 | global_e1h = e1h; |
| 586 | |
| 587 | return mi_e1_setup(e1h); |
| 588 | } |