Sylvain Munaut | b559a53 | 2019-05-09 11:14:26 +0200 | [diff] [blame] | 1 | /* OpenBSC Abis input driver for osmo-e1d */ |
| 2 | |
| 3 | /* (C) 2019 by Sylvain Munaut <tnt@246tNt.com> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "config.h" |
| 26 | |
| 27 | #ifdef HAVE_E1D |
| 28 | |
| 29 | #include <errno.h> |
| 30 | #include <unistd.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include <osmocom/core/bits.h> |
| 34 | #include <osmocom/core/logging.h> |
| 35 | |
| 36 | #include <osmocom/vty/vty.h> |
| 37 | |
| 38 | #include <osmocom/abis/subchan_demux.h> |
| 39 | #include <osmocom/abis/e1_input.h> |
| 40 | #include <osmocom/abis/lapd.h> |
| 41 | |
| 42 | #include <osmocom/e1d/proto.h> |
| 43 | #include <osmocom/e1d/proto_clnt.h> |
| 44 | |
| 45 | |
| 46 | #define TS_SIGN_ALLOC_SIZE 300 |
| 47 | |
| 48 | struct osmo_e1dp_client *g_e1d; |
| 49 | |
| 50 | /* pre-declaration */ |
| 51 | extern struct e1inp_driver e1d_driver; |
| 52 | static int e1d_want_write(struct e1inp_ts *e1i_ts); |
| 53 | |
| 54 | |
| 55 | static int |
| 56 | handle_ts_sign_read(struct osmo_fd *bfd) |
| 57 | { |
| 58 | struct e1inp_line *line = bfd->data; |
| 59 | unsigned int ts_nr = bfd->priv_nr; |
| 60 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 61 | struct msgb *msg = msgb_alloc(TS_SIGN_ALLOC_SIZE, "E1D Signaling TS"); |
| 62 | int ret; |
| 63 | |
| 64 | if (!msg) |
| 65 | return -ENOMEM; |
| 66 | |
| 67 | ret = read(bfd->fd, msg->data, TS_SIGN_ALLOC_SIZE - 16); |
| 68 | if (ret < 0) { |
| 69 | perror("read "); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | msgb_put(msg, ret); |
| 74 | if (ret <= 1) { |
| 75 | perror("read "); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | return e1inp_rx_ts_lapd(e1i_ts, msg); |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | timeout_ts_sign_write(void *data) |
| 84 | { |
| 85 | struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data; |
| 86 | |
| 87 | /* trigger write of ts1, due to tx delay timer */ |
| 88 | e1d_want_write(e1i_ts); |
| 89 | } |
| 90 | |
| 91 | static int |
| 92 | handle_ts_sign_write(struct osmo_fd *bfd) |
| 93 | { |
| 94 | struct e1inp_line *line = bfd->data; |
| 95 | unsigned int ts_nr = bfd->priv_nr; |
| 96 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 97 | struct e1inp_sign_link *sign_link; |
| 98 | struct msgb *msg; |
| 99 | |
| 100 | bfd->when &= ~BSC_FD_WRITE; |
| 101 | |
| 102 | /* get the next msg for this timeslot */ |
| 103 | msg = e1inp_tx_ts(e1i_ts, &sign_link); |
| 104 | if (!msg) { |
| 105 | /* no message after tx delay timer */ |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | DEBUGP(DLMI, "TX: %s\n", osmo_hexdump(msg->data, msg->len)); |
| 110 | lapd_transmit(e1i_ts->lapd, sign_link->tei, |
| 111 | sign_link->sapi, msg); |
| 112 | |
| 113 | /* set tx delay timer for next event */ |
| 114 | osmo_timer_setup(&e1i_ts->sign.tx_timer, timeout_ts_sign_write, e1i_ts); |
| 115 | osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, 50000); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static void |
| 122 | e1d_write_msg(struct msgb *msg, void *cbdata) |
| 123 | { |
| 124 | struct osmo_fd *bfd = cbdata; |
| 125 | struct e1inp_line *line = bfd->data; |
| 126 | unsigned int ts_nr = bfd->priv_nr; |
| 127 | struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1]; |
| 128 | int ret; |
| 129 | |
| 130 | ret = write(bfd->fd, msg->data, msg->len); |
| 131 | msgb_free(msg); |
| 132 | if (ret < 0) |
| 133 | LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret); |
| 134 | } |
| 135 | |
| 136 | static int |
| 137 | e1d_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 138 | { |
| 139 | struct e1inp_line *line = bfd->data; |
| 140 | unsigned int ts_nr = bfd->priv_nr; |
| 141 | unsigned int idx = ts_nr-1; |
| 142 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 143 | int ret = 0; |
| 144 | |
| 145 | switch (e1i_ts->type) { |
| 146 | case E1INP_TS_TYPE_SIGN: |
| 147 | //if (what & BSC_FD_EXCEPT) |
| 148 | //FIXME: what to do ?!?! |
| 149 | if (what & BSC_FD_READ) |
| 150 | ret = handle_ts_sign_read(bfd); |
| 151 | if (what & BSC_FD_WRITE) |
| 152 | ret = handle_ts_sign_write(bfd); |
| 153 | break; |
| 154 | default: |
| 155 | LOGP(DLINP, LOGL_NOTICE, |
| 156 | "unknown/unsupported E1 TS type %u\n", e1i_ts->type); |
| 157 | break; |
| 158 | } |
| 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static int |
| 165 | e1d_want_write(struct e1inp_ts *e1i_ts) |
| 166 | { |
| 167 | /* We never include the DAHDI B-Channel FD into the writeset */ |
| 168 | if (e1i_ts->type == E1INP_TS_TYPE_TRAU) { |
| 169 | LOGP(DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | e1i_ts->driver.e1d.fd.when |= BSC_FD_WRITE; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int |
| 179 | e1d_line_update(struct e1inp_line *line) |
| 180 | { |
| 181 | int ts; |
| 182 | int ret; |
| 183 | |
| 184 | if (line->driver != &e1d_driver) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | |
| 188 | LOGP(DLINP, LOGL_ERROR, "Line update %d %d %d\n", line->num, line->port_nr, line->num_ts); |
| 189 | |
| 190 | for (ts=1; ts<line->num_ts; ts++) |
| 191 | { |
| 192 | unsigned int idx = ts-1; |
| 193 | struct e1inp_ts *e1i_ts = &line->ts[idx]; |
| 194 | struct osmo_fd *bfd = &e1i_ts->driver.e1d.fd; |
| 195 | |
| 196 | /* unregister FD if it was already registered */ |
| 197 | if (bfd->list.next && bfd->list.next != LLIST_POISON1) |
| 198 | osmo_fd_unregister(bfd); |
| 199 | |
| 200 | bfd->data = line; |
| 201 | bfd->priv_nr = ts; |
| 202 | bfd->cb = e1d_fd_cb; |
| 203 | |
| 204 | switch (e1i_ts->type) { |
| 205 | case E1INP_TS_TYPE_NONE: |
| 206 | /* close/release LAPD instance, if any */ |
| 207 | if (e1i_ts->lapd) { |
| 208 | lapd_instance_free(e1i_ts->lapd); |
| 209 | e1i_ts->lapd = NULL; |
| 210 | } |
| 211 | if (bfd->fd) { |
| 212 | close(bfd->fd); |
| 213 | bfd->fd = 0; |
| 214 | } |
| 215 | continue; |
| 216 | case E1INP_TS_TYPE_SIGN: |
| 217 | if (bfd->fd <= 0) |
| 218 | bfd->fd = osmo_e1dp_client_ts_open(g_e1d, 0, 0, ts, E1DP_TSMODE_HDLCFCS); |
| 219 | if (bfd->fd < 0) { |
| 220 | LOGP(DLINP, LOGL_ERROR, |
| 221 | "Could not open timeslot %d\n", ts); |
| 222 | return -EIO; |
| 223 | } |
| 224 | bfd->when = BSC_FD_READ | BSC_FD_EXCEPT; |
| 225 | |
| 226 | if (!e1i_ts->lapd) |
| 227 | e1i_ts->lapd = lapd_instance_alloc(1, |
| 228 | e1d_write_msg, bfd, e1inp_dlsap_up, |
| 229 | e1i_ts, &lapd_profile_abis); |
| 230 | break; |
| 231 | case E1INP_TS_TYPE_HDLC: |
| 232 | break; |
| 233 | case E1INP_TS_TYPE_TRAU: |
| 234 | break; |
| 235 | case E1INP_TS_TYPE_RAW: |
| 236 | break; |
| 237 | }; |
| 238 | |
| 239 | ret = osmo_fd_register(bfd); |
| 240 | if (ret < 0) { |
| 241 | LOGP(DLINP, LOGL_ERROR, |
| 242 | "could not register FD: %s\n", |
| 243 | strerror(ret)); |
| 244 | return ret; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static void |
| 252 | e1d_vty_show(struct vty *vty, struct e1inp_line *line) |
| 253 | { |
| 254 | /* FIXME */ |
| 255 | vty_out(vty, "Not supported yet%s", VTY_NEWLINE); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | struct e1inp_driver e1d_driver = { |
| 260 | .name = "e1d", |
| 261 | .want_write = e1d_want_write, |
| 262 | .line_update = e1d_line_update, |
| 263 | .vty_show = e1d_vty_show, |
| 264 | }; |
| 265 | |
| 266 | int |
| 267 | e1inp_e1d_init(void) |
| 268 | { |
| 269 | /* Connect to daemon */ |
| 270 | g_e1d = osmo_e1dp_client_create(NULL, "/tmp/osmo-e1d.ctl"); |
| 271 | if (!g_e1d) { |
| 272 | LOGP(DLINP, LOGL_ERROR, "Unable to connect to osmo-e1d daemon\n"); |
| 273 | return -EPIPE; |
| 274 | } |
| 275 | |
| 276 | /* register the driver with the core */ |
| 277 | return e1inp_driver_register(&e1d_driver); |
| 278 | } |
| 279 | |
| 280 | #endif /* HAVE_E1D */ |