aboutsummaryrefslogtreecommitdiffstats
path: root/src/input/unixsocket.c
blob: f43405937a9e9d16b28f979ae040d3c49ce3171c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* OpenBSC Abis receive lapd over a unix socket */

/* (C) 2016 by sysmocom s.f.m.c. GmbH
 *
 * Author: Alexander Couzens <lynxis@fe80.eu>
 * Based on other e1_input drivers.
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#include <osmocom/core/talloc.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/socket.h>

#include <osmocom/abis/e1_input.h>
#include <osmocom/abis/lapd.h>
#include <osmocom/abis/e1_input.h>

#include "internal.h"

void *tall_unixsocket_ctx;
#define UNIXSOCKET_ALLOC_SIZE 1600

struct unixsocket_line {
	struct osmo_fd fd;
	struct osmo_timer_list timer;
};

static int unixsocket_line_update(struct e1inp_line *line);
static int ts_want_write(struct e1inp_ts *e1i_ts);

static int unixsocket_exception_cb(struct osmo_fd *bfd)
{
	struct e1inp_line *line = bfd->data;

	LOGP(DLINP, LOGL_ERROR,
	     "Socket connection failure, reconnecting... (line=%p, fd=%d)\n",
	     line, bfd->fd);

	/* Unregister faulty file descriptor from select loop */
	if(osmo_fd_register_check(bfd)) {
		LOGP(DLINP, LOGL_DEBUG,
		     "removing inactive socket from select loop... (line=%p, fd=%d)\n",
		     line, bfd->fd);
		osmo_fd_unregister(bfd);
	}

	/* Close faulty file descriptor */
	close(bfd->fd);

	unixsocket_line_update(line);

	return 0;
}

static int unixsocket_read_cb(struct osmo_fd *bfd)
{
	struct e1inp_line *line = bfd->data;
	struct msgb *msg = msgb_alloc(UNIXSOCKET_ALLOC_SIZE, "UNIXSOCKET TS");
	int ret;

	if (!msg)
		return -ENOMEM;

	ret = read(bfd->fd, msg->data, UNIXSOCKET_ALLOC_SIZE - 16);
	if (ret == 0) {
		unixsocket_exception_cb(bfd);
		return ret;
	} else if (ret < 0) {
		perror("read ");
		return ret;
	}
	msgb_put(msg, ret);

	LOGP(DLMI, LOGL_DEBUG, "rx msg: %s (fd=%d)\n",
	     osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);

	return e1inp_rx_ts_lapd(&line->ts[0], msg);
}

static void timeout_ts1_write(void *data)
{
	struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;

	/* trigger write of ts1, due to tx delay timer */
	ts_want_write(e1i_ts);
}

static int unixsocket_write_cb(struct osmo_fd *bfd)
{
	struct e1inp_line *line = bfd->data;
	struct e1inp_ts *e1i_ts = &line->ts[0];
	struct msgb *msg;
	struct e1inp_sign_link *sign_link;

	bfd->when &= ~BSC_FD_WRITE;

	/* get the next msg for this timeslot */
	msg = e1inp_tx_ts(e1i_ts, &sign_link);
	if (!msg) {
		/* no message after tx delay timer */
		LOGP(DLINP, LOGL_INFO,
		     "no message available (line=%p)\n", line);
		return 0;
	}

	/* set tx delay timer for next event */
	e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
	e1i_ts->sign.tx_timer.data = e1i_ts;

	osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);

	LOGP(DLINP, LOGL_INFO, "sending: %s (line=%p)\n",
	     msgb_hexdump(msg), line);
	lapd_transmit(e1i_ts->lapd, sign_link->tei,
			sign_link->sapi, msg);

	return 0;
}

static int unixsocket_cb(struct osmo_fd *bfd, unsigned int what)
{
	int ret = 0;

	if (what & BSC_FD_READ)
		ret = unixsocket_read_cb(bfd);
	if (what & BSC_FD_WRITE)
		ret = unixsocket_write_cb(bfd);

	return ret;
}

static int ts_want_write(struct e1inp_ts *e1i_ts)
{
	struct unixsocket_line *line = e1i_ts->line->driver_data;

	line->fd.when |= BSC_FD_WRITE;

	return 0;
}

/*!
 * \brief unixsocket_write_msg lapd callback for data to unixsocket
 * \param msg
 * \param cbdata
 */
static void unixsocket_write_msg(struct msgb *msg, void *cbdata)
{
	struct osmo_fd *bfd = cbdata;
	int ret;

	LOGP(DLMI, LOGL_DEBUG, "tx msg: %s (fd=%d)\n",
	     osmo_hexdump_nospc(msg->data, msg->len), bfd->fd);

	ret = write(bfd->fd, msg->data, msg->len);
	msgb_free(msg);
	if (ret == -1)
		unixsocket_exception_cb(bfd);
	else if (ret < 0)
		LOGP(DLMI, LOGL_NOTICE, "%s write failed %d\n", __func__, ret);
}

static int unixsocket_line_update(struct e1inp_line *line)
{
	struct unixsocket_line *config;
	const char *sock_path = "/tmp/rsl_oml";
	int ret = 0;
	int i;

	LOGP(DLINP, LOGL_NOTICE, "line update (line=%p)\n", line);

	if (!line->driver_data)
		line->driver_data = talloc_zero(line, struct unixsocket_line);

	if (!line->driver_data) {
		LOGP(DLINP, LOGL_ERROR,
		     "OOM in line update (line=%p)\n", line);
		return -ENOMEM;
	}

	config = line->driver_data;
	config->fd.data = line;
	config->fd.when = BSC_FD_READ;
	config->fd.cb = unixsocket_cb;

	/* Open unix domain socket */
	ret = osmo_sock_unix_init(SOCK_SEQPACKET, 0, sock_path,
				  OSMO_SOCK_F_CONNECT);
	if (ret < 0) {
		/* Note: We will not free the allocated driver_data memory if
		 * opening the socket fails. The caller may want to call this
		 * function multiple times using config->fd.data as line
		 * parameter. Freeing now would destroy that reference. */
		LOGP(DLINP, LOGL_ERROR,
		     "unable to open socket: %s (line=%p, fd=%d)\n", sock_path,
		     line, config->fd.fd);
		return ret;
	}
	LOGP(DLINP, LOGL_DEBUG,
	     "successfully opend (new) socket (line=%p, fd=%d, ret=%d)\n",
	     line, config->fd.fd, ret);
	config->fd.fd = ret;

	/* Register socket in select loop */
	if (osmo_fd_register(&config->fd) < 0) {
		LOGP(DLINP, LOGL_ERROR,
		     "error registering new socket (line=%p, fd=%d)\n",
		     line, config->fd.fd);
		close(config->fd.fd);
		return -EIO;
	}

	/* Set line parameter */
	for (i = 0; i < ARRAY_SIZE(line->ts); i++) {
		struct e1inp_ts *e1i_ts = &line->ts[i];
		if (!e1i_ts->lapd) {
			e1i_ts->lapd = lapd_instance_alloc(1,
				unixsocket_write_msg, &config->fd,
				e1inp_dlsap_up, e1i_ts, &lapd_profile_abis);
		}
	}

	/* Ensure ericsson-superchannel is turned of when
	 * a new connection is made */
	e1inp_ericsson_set_altc(line, 0);

	return ret;
}

struct e1inp_driver unixsocket_driver = {
	.name = "unixsocket",
	.want_write = ts_want_write,
	.line_update = unixsocket_line_update,
	.default_delay = 0,
};

void e1inp_unixsocket_init(void)
{
	tall_unixsocket_ctx = talloc_named_const(libosmo_abis_ctx, 1, "unixsocket");
	e1inp_driver_register(&unixsocket_driver);
}

void e1inp_ericsson_set_altc(struct e1inp_line *unixline, int superchannel)
{
	struct unixsocket_line *config;
	struct msgb *msg;

	if (!unixline)
		return;

	if (unixline->driver != &unixsocket_driver) {
		LOGP(DLMI, LOGL_NOTICE, "altc is only supported by unixsocket\n");
		return;
	}

	config = unixline->driver_data;
	if (!config) {
		LOGP(DLMI, LOGL_NOTICE, "e1inp driver not yet initialized.\n");
		return;
	}


	msg = msgb_alloc_headroom(200, 100, "ALTTC");

	/* magic */
	msgb_put_u32(msg, 0x23004200);
	msgb_put_u8(msg, superchannel ? 1 : 0);

	unixsocket_write_msg(msg, &config->fd);
}