blob: 3dcba1d5b4bf4c2d283b88438c41402f1f1414b2 [file] [log] [blame]
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +02001/* OpenBSC Abis input driver for HSL Femto */
2
3/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2011 by On-Waves
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 Affero General Public License as published by
10 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23/* HSL uses a much more primitive/simplified version of the IPA multiplex.
24 *
25 * They have taken out the nice parts like the ID_GET / ID_RESP for resolving
26 * the UNIT ID, as well as the keepalive ping/pong messages. Furthermore, the
27 * Stream Identifiers are fixed on the BTS side (RSL always 0, OML always 0xff)
28 * and both OML+RSL share a single TCP connection.
29 *
30 * Other oddities include the encapsulation of BSSGP messages in the L3_INFO IE
31 * of RSL
32 */
33
34#include "internal.h"
35
36#include <stdio.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include <errno.h>
40#include <string.h>
41#include <time.h>
Harald Welte3e1d84b2011-08-19 13:32:18 +020042#include <inttypes.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020043#include <sys/fcntl.h>
44#include <sys/socket.h>
45#include <sys/ioctl.h>
46#include <arpa/inet.h>
47
48#include <osmocom/core/select.h>
49#include <osmocom/gsm/tlv.h>
50#include <osmocom/core/msgb.h>
Pablo Neira Ayuso177094b2011-06-07 12:21:51 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020052#include <osmocom/core/logging.h>
Pablo Neira Ayusoe47a9782011-06-07 18:03:48 +020053#include <osmocom/abis/ipaccess.h>
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +020054#include <osmocom/core/socket.h>
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +020055#include <osmocom/abis/ipa.h>
Harald Welte71d87b22011-07-18 14:49:56 +020056#include <osmocom/core/talloc.h>
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020057
58#define HSL_TCP_PORT 2500
59#define HSL_PROTO_DEBUG 0xdd
60
61#define PRIV_OML 1
62#define PRIV_RSL 2
63
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +020064static void *tall_hsl_ctx;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +020065
66/* data structure for one E1 interface with A-bis */
67struct hsl_e1_handle {
68 struct osmo_fd listen_fd;
69 struct gsm_network *gsmnet;
70};
71
72static struct hsl_e1_handle *e1h;
73
74
75#define TS1_ALLOC_SIZE 900
76
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020077static void hsl_drop(struct e1inp_line *line, struct osmo_fd *bfd)
78{
79 line->ops->sign_link_down(line);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +020080
81 if (bfd->fd != -1) {
82 osmo_fd_unregister(bfd);
83 close(bfd->fd);
84 bfd->fd = -1;
85 }
86 /* put the virtual E1 line that we cloned for this socket, if
87 * it becomes unused, it gets released. */
88 e1inp_line_put(line);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020089}
90
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +020091static int process_hsl_rsl(struct msgb *msg, struct e1inp_line *line,
92 struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020093{
94 char serno_buf[16];
95 uint8_t serno_len;
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +020096 struct e1inp_sign_link *sign_link;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020097 struct hsl_unit unit_data;
98
99 switch (msg->l2h[1]) {
100 case 0x80:
101 /*, contains Serial Number + SW version */
102 if (msg->l2h[2] != 0xc0)
103 break;
104 serno_len = msg->l2h[3];
105 if (serno_len > sizeof(serno_buf)-1)
106 serno_len = sizeof(serno_buf)-1;
107 memcpy(serno_buf, msg->l2h+4, serno_len);
108 serno_buf[serno_len] = '\0';
109 unit_data.serno = strtoul(serno_buf, NULL, 10);
110
111 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200112 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200113 "Unable to set signal link, closing socket.\n");
114 osmo_fd_unregister(bfd);
115 close(bfd->fd);
116 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200117 return -EINVAL;
118 }
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200119 sign_link = line->ops->sign_link_up(&unit_data,
120 line, E1INP_SIGN_NONE);
121 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200122 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200123 "Unable to set signal link, closing socket.\n");
124 osmo_fd_unregister(bfd);
125 close(bfd->fd);
126 bfd->fd = -1;
127 return -EINVAL;
128 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200129 msgb_free(msg);
130 return 1; /* == we have taken over the msg */
131 case 0x82:
132 /* FIXME: do something with BSSGP, i.e. forward it over
133 * NSIP to OsmoSGSN */
134 msgb_free(msg);
135 return 1;
136 }
137 return 0;
138}
139
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200140static int handle_ts1_read(struct osmo_fd *bfd)
141{
142 struct e1inp_line *line = bfd->data;
143 unsigned int ts_nr = bfd->priv_nr;
144 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200145 struct e1inp_sign_link *link;
146 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200147 struct msgb *msg;
148 int ret = 0, error;
149
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200150 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200151 if (error < 0)
152 return error;
153 else if (error == 0) {
154 hsl_drop(e1i_ts->line, bfd);
Harald Weltecc2241b2011-07-19 16:06:06 +0200155 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200156 return error;
157 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200158 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200159
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200160 hh = (struct ipaccess_head *) msg->data;
161 if (hh->proto == HSL_PROTO_DEBUG) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200162 LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh));
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200163 msgb_free(msg);
164 return ret;
165 }
166
167 /* HSL proprietary RSL extension */
168 if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200169 ret = process_hsl_rsl(msg, line, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200170 if (ret < 0) {
171 hsl_drop(e1i_ts->line, bfd);
172 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200173 } else if (ret == 1)
174 return 0;
175 /* else: continue... */
176 }
177
178#ifdef HSL_SR_1_0
179 /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */
180 if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01))
181 msg->l2h[0] = ABIS_OM_MDISC_FOM;
182#endif
183 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
184 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200185 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200186 "hh->proto=0x%02x\n", hh->proto);
187 msgb_free(msg);
188 return -EIO;
189 }
190 msg->dst = link;
191
192 /* XXX: better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200193 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200194 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200195 "no action set for signalling messages.\n");
196 return -ENOENT;
197 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200198 e1i_ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200199
200 return ret;
201}
202
203static int ts_want_write(struct e1inp_ts *e1i_ts)
204{
205 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
206
207 return 0;
208}
209
210static void timeout_ts1_write(void *data)
211{
212 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
213
214 /* trigger write of ts1, due to tx delay timer */
215 ts_want_write(e1i_ts);
216}
217
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200218static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200219{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200220 unsigned int ts_nr = bfd->priv_nr;
221 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
222 struct e1inp_sign_link *sign_link;
223 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200224 int ret;
225
226 bfd->when &= ~BSC_FD_WRITE;
227
228 /* get the next msg for this timeslot */
229 msg = e1inp_tx_ts(e1i_ts, &sign_link);
230 if (!msg) {
231 /* no message after tx delay timer */
232 return 0;
233 }
234
235 switch (sign_link->type) {
236 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200237#ifdef HSL_SR_1_0
238 /* HSL uses 0x81 for FOM for some reason */
239 if (msg->data[0] == ABIS_OM_MDISC_FOM)
240 msg->data[0] = ABIS_OM_MDISC_FOM | 0x01;
241#endif
242 break;
243 case E1INP_SIGN_RSL:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200244 break;
245 default:
246 msgb_free(msg);
247 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
248 return -EINVAL;
249 }
250
251 msg->l2h = msg->data;
252 ipaccess_prepend_header(msg, sign_link->tei);
253
Harald Weltecc2241b2011-07-19 16:06:06 +0200254 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200255
256 ret = send(bfd->fd, msg->data, msg->len, 0);
257 msgb_free(msg);
258
259 /* set tx delay timer for next event */
260 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
261 e1i_ts->sign.tx_timer.data = e1i_ts;
262
263 /* Reducing this might break the nanoBTS 900 init. */
264 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
265
266 return ret;
267}
268
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200269static int handle_ts1_write(struct osmo_fd *bfd)
270{
271 struct e1inp_line *line = bfd->data;
272
273 return __handle_ts1_write(bfd, line);
274}
275
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200276int hsl_bts_write(struct ipa_client_conn *link)
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200277{
278 struct e1inp_line *line = link->line;
279
280 return __handle_ts1_write(link->ofd, line);
281}
282
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200283/* callback from select.c in case one of the fd's can be read/written */
284static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what)
285{
286 struct e1inp_line *line = bfd->data;
287 unsigned int ts_nr = bfd->priv_nr;
288 unsigned int idx = ts_nr-1;
289 struct e1inp_ts *e1i_ts;
290 int rc = 0;
291
292 /* In case of early RSL we might not yet have a line */
293
294 if (line)
295 e1i_ts = &line->ts[idx];
296
297 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
298 if (what & BSC_FD_READ)
299 rc = handle_ts1_read(bfd);
300 if (what & BSC_FD_WRITE)
301 rc = handle_ts1_write(bfd);
302 } else
Harald Weltecc2241b2011-07-19 16:06:06 +0200303 LOGP(DLINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200304
305 return rc;
306}
307
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200308static void hsl_close(struct e1inp_sign_link *sign_link)
309{
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100310 struct e1inp_ts *e1i_ts = sign_link->ts;
311 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
312 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200313}
314
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200315static int hsl_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200316
317struct e1inp_driver hsl_driver = {
318 .name = "hsl",
319 .want_write = ts_want_write,
320 .line_update = hsl_line_update,
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200321 .close = hsl_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200322 .default_delay = 0,
323};
324
325/* callback of the OML listening filedescriptor */
326static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
327{
328 int ret;
329 int idx = 0;
330 int i;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200331 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200332 struct e1inp_ts *e1i_ts;
333 struct osmo_fd *bfd;
334 struct sockaddr_in sa;
335 socklen_t sa_len = sizeof(sa);
336
337 if (!(what & BSC_FD_READ))
338 return 0;
339
340 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
341 if (ret < 0) {
342 perror("accept");
343 return ret;
344 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200345 LOGP(DLINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200346 inet_ntoa(sa.sin_addr));
347
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200348 /* clone virtual E1 line for this new signalling link. */
349 line = e1inp_line_clone(tall_hsl_ctx, listen_bfd->data);
350 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200351 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200352 return -1;
353 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200354 /* create virrtual E1 timeslots for signalling */
355 e1inp_ts_config_sign(&line->ts[1-1], line);
356
357 /* initialize the fds */
358 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
359 line->ts[i].driver.ipaccess.fd.fd = -1;
360
361 e1i_ts = &line->ts[idx];
362
363 bfd = &e1i_ts->driver.ipaccess.fd;
364 bfd->fd = ret;
365 bfd->data = line;
366 bfd->priv_nr = PRIV_OML;
367 bfd->cb = hsl_fd_cb;
368 bfd->when = BSC_FD_READ;
369 ret = osmo_fd_register(bfd);
370 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200371 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200372 close(bfd->fd);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200373 e1inp_line_put(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200374 return ret;
375 }
376
377 return ret;
378}
379
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200380static int hsl_bts_process(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200381{
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200382 struct ipaccess_head *hh;
383 struct e1inp_sign_link *sign_link;
384 struct e1inp_ts *e1i_ts = &link->line->ts[0];
385
386 hh = (struct ipaccess_head *) msg->data;
387 if (hh->proto == HSL_PROTO_DEBUG) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200388 LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n",
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200389 msg->data + sizeof(*hh));
390 msgb_free(msg);
391 return 0;
392 }
393 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
394 if (!sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200395 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200396 "hh->proto=0x%02x\n", hh->proto);
397 msgb_free(msg);
398 return -EIO;
399 }
400 msg->dst = sign_link;
401
402 /* XXX better use e1inp_ts_rx? */
403 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200404 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200405 "no action set for signalling messages.\n");
406 return -ENOENT;
407 }
408 link->line->ops->sign_link(msg);
409 return 0;
410}
411
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200412static int hsl_bts_connect(struct ipa_client_conn *link)
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200413{
414 struct msgb *msg;
415 uint8_t *serno;
416 char serno_buf[16];
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200417 struct hsl_unit *unit = link->line->ops->cfg.ipa.dev;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200418 struct e1inp_sign_link *sign_link;
419
420 /* send the minimal message to identify this BTS. */
421 msg = ipa_msg_alloc(0);
422 if (!msg)
423 return -ENOMEM;
424
425 *msgb_put(msg, 1) = 0x80;
426 *msgb_put(msg, 1) = 0x80;
427 *msgb_put(msg, 1) = unit->swversion;
Harald Welte3e1d84b2011-08-19 13:32:18 +0200428 snprintf(serno_buf, sizeof(serno_buf), "%"PRIx64, unit->serno);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200429 serno = msgb_put(msg, strlen(serno_buf)+1);
430 memcpy(serno, serno_buf, strlen(serno_buf));
431 ipa_msg_push_header(msg, 0);
432 send(link->ofd->fd, msg->data, msg->len, 0);
433 msgb_free(msg);
434
435 /* ... and enable the signalling link. */
436 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200437 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200438 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200439 ipa_client_conn_close(link);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200440 return -EINVAL;
441 }
442 sign_link = link->line->ops->sign_link_up(&unit,
443 link->line, E1INP_SIGN_NONE);
444 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200445 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200446 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200447 ipa_client_conn_close(link);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200448 return -EINVAL;
449 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200450 return 0;
451}
452
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200453struct hsl_line {
454 int line_already_initialized;
455};
456
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200457static int hsl_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200458{
459 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200460 struct hsl_line *hl;
461
462 if (!line->driver_data)
463 line->driver_data = talloc_zero(line, struct hsl_line);
464
465 if (!line->driver_data) {
466 LOGP(DLINP, LOGL_NOTICE, "hsl: OOM in line update\n");
467 return -ENOMEM;
468 }
469 hl = line->driver_data;
470
471 /* We only initialize this line once. */
472 if (hl->line_already_initialized)
473 return 0;
474
475 hl->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200476
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200477 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200478 case E1INP_LINE_R_BSC:
Harald Weltecc2241b2011-07-19 16:06:06 +0200479 LOGP(DLINP, LOGL_NOTICE, "enabling hsl BSC mode\n");
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200480
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200481 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200482 line->ops->cfg.ipa.addr,
483 HSL_TCP_PORT, OSMO_SOCK_F_BIND);
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200484 if (ret < 0)
485 return ret;
486
487 e1h->listen_fd.fd = ret;
488 e1h->listen_fd.when |= BSC_FD_READ;
489 e1h->listen_fd.cb = listen_fd_cb;
490 e1h->listen_fd.data = line;
491
492 if (osmo_fd_register(&e1h->listen_fd) < 0) {
493 close(ret);
494 return ret;
495 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200496 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200497 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200498 struct ipa_client_conn *link;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200499
Harald Weltecc2241b2011-07-19 16:06:06 +0200500 LOGP(DLINP, LOGL_NOTICE, "enabling hsl BTS mode\n");
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200501
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200502 link = ipa_client_conn_create(tall_hsl_ctx,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200503 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200504 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200505 line->ops->cfg.ipa.addr,
506 HSL_TCP_PORT,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200507 hsl_bts_connect,
508 hsl_bts_process,
509 hsl_bts_write,
510 NULL);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200511 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200512 LOGP(DLINP, LOGL_ERROR, "cannot create BTS link: %s\n",
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200513 strerror(errno));
514 return -ENOMEM;
515 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200516 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200517 LOGP(DLINP, LOGL_ERROR, "cannot open BTS link: %s\n",
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200518 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200519 ipa_client_conn_close(link);
520 ipa_client_conn_destroy(link);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200521 return -EIO;
522 }
523 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200524 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200525 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200526 default:
527 break;
528 }
529 return ret;
530}
531
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532void e1inp_hsl_init(void)
533{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200534 tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl");
535
536 e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200537 if (!e1h)
538 return;
539
540 e1inp_driver_register(&hsl_driver);
541}