blob: c15c081f041bca77579aa34178864d3c9c78843d [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 Welte43436032020-01-12 12:16:42 +010069 LOGP(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 Welte43436032020-01-12 12:16:42 +010075 LOGP(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)
133 LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
134}
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:
153 LOGP(DLINP, LOGL_NOTICE,
154 "unknown/unsupported E1 TS type %u\n", e1i_ts->type);
155 break;
156 }
157
158 return ret;
159}
160
161
162static int
163e1d_want_write(struct e1inp_ts *e1i_ts)
164{
165 /* We never include the DAHDI B-Channel FD into the writeset */
166 if (e1i_ts->type == E1INP_TS_TYPE_TRAU) {
167 LOGP(DLINP, LOGL_DEBUG, "Trying to write TRAU ts\n");
168 return 0;
169 }
170
171 e1i_ts->driver.e1d.fd.when |= BSC_FD_WRITE;
172
173 return 0;
174}
175
176static int
177e1d_line_update(struct e1inp_line *line)
178{
179 int ts;
180 int ret;
181
182 if (line->driver != &e1d_driver)
183 return -EINVAL;
184
185
186 LOGP(DLINP, LOGL_ERROR, "Line update %d %d %d\n", line->num, line->port_nr, line->num_ts);
187
188 for (ts=1; ts<line->num_ts; ts++)
189 {
190 unsigned int idx = ts-1;
191 struct e1inp_ts *e1i_ts = &line->ts[idx];
192 struct osmo_fd *bfd = &e1i_ts->driver.e1d.fd;
193
194 /* unregister FD if it was already registered */
195 if (bfd->list.next && bfd->list.next != LLIST_POISON1)
196 osmo_fd_unregister(bfd);
197
198 bfd->data = line;
199 bfd->priv_nr = ts;
200 bfd->cb = e1d_fd_cb;
201
202 switch (e1i_ts->type) {
203 case E1INP_TS_TYPE_NONE:
204 /* close/release LAPD instance, if any */
205 if (e1i_ts->lapd) {
206 lapd_instance_free(e1i_ts->lapd);
207 e1i_ts->lapd = NULL;
208 }
209 if (bfd->fd) {
210 close(bfd->fd);
211 bfd->fd = 0;
212 }
213 continue;
214 case E1INP_TS_TYPE_SIGN:
215 if (bfd->fd <= 0)
216 bfd->fd = osmo_e1dp_client_ts_open(g_e1d, 0, 0, ts, E1DP_TSMODE_HDLCFCS);
217 if (bfd->fd < 0) {
218 LOGP(DLINP, LOGL_ERROR,
219 "Could not open timeslot %d\n", ts);
220 return -EIO;
221 }
Harald Welte6f674de2020-01-12 12:18:26 +0100222 bfd->when = BSC_FD_READ;
Sylvain Munautb559a532019-05-09 11:14:26 +0200223
224 if (!e1i_ts->lapd)
225 e1i_ts->lapd = lapd_instance_alloc(1,
226 e1d_write_msg, bfd, e1inp_dlsap_up,
227 e1i_ts, &lapd_profile_abis);
228 break;
229 case E1INP_TS_TYPE_HDLC:
230 break;
231 case E1INP_TS_TYPE_TRAU:
232 break;
233 case E1INP_TS_TYPE_RAW:
234 break;
235 };
236
237 ret = osmo_fd_register(bfd);
238 if (ret < 0) {
239 LOGP(DLINP, LOGL_ERROR,
240 "could not register FD: %s\n",
241 strerror(ret));
242 return ret;
243 }
244 }
245
246 return 0;
247}
248
Sylvain Munautb559a532019-05-09 11:14:26 +0200249struct e1inp_driver e1d_driver = {
250 .name = "e1d",
251 .want_write = e1d_want_write,
252 .line_update = e1d_line_update,
Sylvain Munautb559a532019-05-09 11:14:26 +0200253};
254
255int
256e1inp_e1d_init(void)
257{
258 /* Connect to daemon */
259 g_e1d = osmo_e1dp_client_create(NULL, "/tmp/osmo-e1d.ctl");
260 if (!g_e1d) {
261 LOGP(DLINP, LOGL_ERROR, "Unable to connect to osmo-e1d daemon\n");
262 return -EPIPE;
263 }
264
265 /* register the driver with the core */
266 return e1inp_driver_register(&e1d_driver);
267}
268
269#endif /* HAVE_E1D */