blob: 2b67141c780bc183c5386c7bcd2950fa29bc26e6 [file] [log] [blame]
Sylvain Munautb559a532019-05-09 11:14:26 +02001/* 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
48struct osmo_e1dp_client *g_e1d;
49
50/* pre-declaration */
51extern struct e1inp_driver e1d_driver;
52static int e1d_want_write(struct e1inp_ts *e1i_ts);
53
54
55static int
56handle_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) {
Harald Welte28898d12020-01-12 13:34:07 +010069 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Sylvain Munautb559a532019-05-09 11:14:26 +020070 return ret;
71 }
72
73 msgb_put(msg, ret);
74 if (ret <= 1) {
Harald Welte28898d12020-01-12 13:34:07 +010075 LOGPITS(e1i_ts, DLMI, LOGL_ERROR, "%s read failed %d (%s)\n", __func__, ret, strerror(errno));
Sylvain Munautb559a532019-05-09 11:14:26 +020076 return ret;
77 }
78
79 return e1inp_rx_ts_lapd(e1i_ts, msg);
80}
81
82static void
83timeout_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
91static int
92handle_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
121static void
122e1d_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)
Harald Welte28898d12020-01-12 13:34:07 +0100133 LOGPITS(e1i_ts, DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
Sylvain Munautb559a532019-05-09 11:14:26 +0200134}
135
136static int
137e1d_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:
Sylvain Munautb559a532019-05-09 11:14:26 +0200147 if (what & BSC_FD_READ)
148 ret = handle_ts_sign_read(bfd);
149 if (what & BSC_FD_WRITE)
150 ret = handle_ts_sign_write(bfd);
151 break;
152 default:
Harald Welte28898d12020-01-12 13:34:07 +0100153 LOGPITS(e1i_ts, DLINP, LOGL_NOTICE, "unknown/unsupported E1 TS type %u\n", e1i_ts->type);
Sylvain Munautb559a532019-05-09 11:14:26 +0200154 break;
155 }
156
157 return ret;
158}
159
160
161static int
162e1d_want_write(struct e1inp_ts *e1i_ts)
163{
164 /* We never include the DAHDI B-Channel FD into the writeset */
165 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
Harald Welte28898d12020-01-12 13:34:07 +0100166 LOGPITS(e1i_ts, DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
Sylvain Munautb559a532019-05-09 11:14:26 +0200167 return 0;
168 }
169
170 e1i_ts->driver.e1d.fd.when |= BSC_FD_WRITE;
171
172 return 0;
173}
174
175static int
176e1d_line_update(struct e1inp_line *line)
177{
178 int ts;
179 int ret;
180
Harald Welteccf84ea2020-01-12 12:31:45 +0100181 /* we use higher 4 bits for interface, lower 4 bits for line,
182 * resulting in max. 16 interfaces with 16 lines each */
183 uint8_t e1d_intf = (line->port_nr >> 4) & 0xF;
184 uint8_t e1d_line = line->port_nr & 0xF;
185
Sylvain Munautb559a532019-05-09 11:14:26 +0200186 if (line->driver != &e1d_driver)
187 return -EINVAL;
188
189
Harald Welte28898d12020-01-12 13:34:07 +0100190 LOGPIL(line, DLINP, LOGL_NOTICE, "Line update %d %d=E1D(%d:%d) %d\n", line->num, line->port_nr,
Harald Welteccf84ea2020-01-12 12:31:45 +0100191 e1d_intf, e1d_line, line->num_ts);
Sylvain Munautb559a532019-05-09 11:14:26 +0200192
193 for (ts=1; ts<line->num_ts; ts++)
194 {
195 unsigned int idx = ts-1;
196 struct e1inp_ts *e1i_ts = &line->ts[idx];
197 struct osmo_fd *bfd = &e1i_ts->driver.e1d.fd;
198
199 /* unregister FD if it was already registered */
200 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
201 osmo_fd_unregister(bfd);
202
203 bfd->data = line;
204 bfd->priv_nr = ts;
205 bfd->cb = e1d_fd_cb;
206
207 switch (e1i_ts->type) {
208 case E1INP_TS_TYPE_NONE:
209 /* close/release LAPD instance, if any */
210 if (e1i_ts->lapd) {
211 lapd_instance_free(e1i_ts->lapd);
212 e1i_ts->lapd = NULL;
213 }
214 if (bfd->fd) {
215 close(bfd->fd);
216 bfd->fd = 0;
217 }
218 continue;
219 case E1INP_TS_TYPE_SIGN:
Harald Welteccf84ea2020-01-12 12:31:45 +0100220 if (bfd->fd <= 0) {
221 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, e1d_intf, e1d_line, ts,
222 E1DP_TSMODE_HDLCFCS);
223 }
Sylvain Munautb559a532019-05-09 11:14:26 +0200224 if (bfd->fd < 0) {
Harald Welte28898d12020-01-12 13:34:07 +0100225 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "Could not open timeslot %d\n", ts);
Sylvain Munautb559a532019-05-09 11:14:26 +0200226 return -EIO;
227 }
Harald Welte6f674de2020-01-12 12:18:26 +0100228 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200229
230 if (!e1i_ts->lapd)
231 e1i_ts->lapd = lapd_instance_alloc(1,
232 e1d_write_msg, bfd, e1inp_dlsap_up,
233 e1i_ts, &lapd_profile_abis);
234 break;
235 case E1INP_TS_TYPE_HDLC:
236 break;
237 case E1INP_TS_TYPE_TRAU:
238 break;
239 case E1INP_TS_TYPE_RAW:
240 break;
241 };
242
243 ret = osmo_fd_register(bfd);
244 if (ret < 0) {
Harald Welte28898d12020-01-12 13:34:07 +0100245 LOGPITS(e1i_ts, DLINP, LOGL_ERROR, "could not register FD: %s\n", strerror(ret));
Sylvain Munautb559a532019-05-09 11:14:26 +0200246 return ret;
247 }
248 }
249
250 return 0;
251}
252
Sylvain Munautb559a532019-05-09 11:14:26 +0200253struct e1inp_driver e1d_driver = {
254 .name = "e1d",
255 .want_write = e1d_want_write,
256 .line_update = e1d_line_update,
Sylvain Munautb559a532019-05-09 11:14:26 +0200257};
258
259int
260e1inp_e1d_init(void)
261{
262 /* Connect to daemon */
263 g_e1d = osmo_e1dp_client_create(NULL, "/tmp/osmo-e1d.ctl");
264 if (!g_e1d) {
Harald Welte28898d12020-01-12 13:34:07 +0100265 LOGP(DLINP, LOGL_ERROR, "Unable to connect to osmo-e1d daemon\n");
Sylvain Munautb559a532019-05-09 11:14:26 +0200266 return -EPIPE;
267 }
268
269 /* register the driver with the core */
270 return e1inp_driver_register(&e1d_driver);
271}
272
273#endif /* HAVE_E1D */