blob: 1a60c2bc3a5120fff1225b800124655b63603e6f [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 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020086}
87
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +020088static int process_hsl_rsl(struct msgb *msg, struct e1inp_line *line,
89 struct osmo_fd *bfd)
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020090{
91 char serno_buf[16];
92 uint8_t serno_len;
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +020093 struct e1inp_sign_link *sign_link;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +020094 struct hsl_unit unit_data;
95
96 switch (msg->l2h[1]) {
97 case 0x80:
98 /*, contains Serial Number + SW version */
99 if (msg->l2h[2] != 0xc0)
100 break;
101 serno_len = msg->l2h[3];
102 if (serno_len > sizeof(serno_buf)-1)
103 serno_len = sizeof(serno_buf)-1;
104 memcpy(serno_buf, msg->l2h+4, serno_len);
105 serno_buf[serno_len] = '\0';
106 unit_data.serno = strtoul(serno_buf, NULL, 10);
107
108 if (!line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200109 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200110 "Unable to set signal link, closing socket.\n");
111 osmo_fd_unregister(bfd);
112 close(bfd->fd);
113 bfd->fd = -1;
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200114 return -EINVAL;
115 }
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200116 sign_link = line->ops->sign_link_up(&unit_data,
117 line, E1INP_SIGN_NONE);
118 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200119 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200120 "Unable to set signal link, closing socket.\n");
121 osmo_fd_unregister(bfd);
122 close(bfd->fd);
123 bfd->fd = -1;
124 return -EINVAL;
125 }
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200126 msgb_free(msg);
127 return 1; /* == we have taken over the msg */
128 case 0x82:
129 /* FIXME: do something with BSSGP, i.e. forward it over
130 * NSIP to OsmoSGSN */
131 msgb_free(msg);
132 return 1;
133 }
134 return 0;
135}
136
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200137static int handle_ts1_read(struct osmo_fd *bfd)
138{
139 struct e1inp_line *line = bfd->data;
140 unsigned int ts_nr = bfd->priv_nr;
141 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200142 struct e1inp_sign_link *link;
143 struct ipaccess_head *hh;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200144 struct msgb *msg;
145 int ret = 0, error;
146
Pablo Neira Ayuso7a249402011-06-21 14:15:46 +0200147 error = ipa_msg_recv(bfd->fd, &msg);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200148 if (error < 0)
149 return error;
150 else if (error == 0) {
151 hsl_drop(e1i_ts->line, bfd);
Harald Weltecc2241b2011-07-19 16:06:06 +0200152 LOGP(DLINP, LOGL_NOTICE, "Sign link vanished, dead socket\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200153 return error;
154 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200155 DEBUGP(DLMI, "RX %u: %s\n", ts_nr, osmo_hexdump(msgb_l2(msg), msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200156
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200157 hh = (struct ipaccess_head *) msg->data;
158 if (hh->proto == HSL_PROTO_DEBUG) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200159 LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n", msg->data + sizeof(*hh));
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200160 msgb_free(msg);
161 return ret;
162 }
163
164 /* HSL proprietary RSL extension */
165 if (hh->proto == 0 && (msg->l2h[0] == 0x81 || msg->l2h[0] == 0x80)) {
Pablo Neira Ayusocd8d2e52011-07-02 17:05:21 +0200166 ret = process_hsl_rsl(msg, line, bfd);
Pablo Neira Ayusoc9c4fd32011-06-30 12:19:42 +0200167 if (ret < 0) {
168 hsl_drop(e1i_ts->line, bfd);
169 return ret;
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200170 } else if (ret == 1)
171 return 0;
172 /* else: continue... */
173 }
174
175#ifdef HSL_SR_1_0
176 /* HSL for whatever reason chose to use 0x81 instead of 0x80 for FOM */
177 if (hh->proto == 255 && msg->l2h[0] == (ABIS_OM_MDISC_FOM | 0x01))
178 msg->l2h[0] = ABIS_OM_MDISC_FOM;
179#endif
180 link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
181 if (!link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200182 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200183 "hh->proto=0x%02x\n", hh->proto);
184 msgb_free(msg);
185 return -EIO;
186 }
187 msg->dst = link;
188
189 /* XXX: better use e1inp_ts_rx? */
Pablo Neira Ayusof163d232011-06-25 18:42:55 +0200190 if (!e1i_ts->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200191 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso5a4b7c52011-06-07 14:07:48 +0200192 "no action set for signalling messages.\n");
193 return -ENOENT;
194 }
Pablo Neira Ayusodbd82fb2011-07-05 15:29:23 +0200195 e1i_ts->line->ops->sign_link(msg);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200196
197 return ret;
198}
199
200static int ts_want_write(struct e1inp_ts *e1i_ts)
201{
202 e1i_ts->driver.ipaccess.fd.when |= BSC_FD_WRITE;
203
204 return 0;
205}
206
207static void timeout_ts1_write(void *data)
208{
209 struct e1inp_ts *e1i_ts = (struct e1inp_ts *)data;
210
211 /* trigger write of ts1, due to tx delay timer */
212 ts_want_write(e1i_ts);
213}
214
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200215static int __handle_ts1_write(struct osmo_fd *bfd, struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200216{
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200217 unsigned int ts_nr = bfd->priv_nr;
218 struct e1inp_ts *e1i_ts = &line->ts[ts_nr-1];
219 struct e1inp_sign_link *sign_link;
220 struct msgb *msg;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200221 int ret;
222
223 bfd->when &= ~BSC_FD_WRITE;
224
225 /* get the next msg for this timeslot */
226 msg = e1inp_tx_ts(e1i_ts, &sign_link);
227 if (!msg) {
228 /* no message after tx delay timer */
229 return 0;
230 }
231
232 switch (sign_link->type) {
233 case E1INP_SIGN_OML:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200234#ifdef HSL_SR_1_0
235 /* HSL uses 0x81 for FOM for some reason */
236 if (msg->data[0] == ABIS_OM_MDISC_FOM)
237 msg->data[0] = ABIS_OM_MDISC_FOM | 0x01;
238#endif
239 break;
240 case E1INP_SIGN_RSL:
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200241 break;
242 default:
243 msgb_free(msg);
244 bfd->when |= BSC_FD_WRITE; /* come back for more msg */
245 return -EINVAL;
246 }
247
248 msg->l2h = msg->data;
249 ipaccess_prepend_header(msg, sign_link->tei);
250
Harald Weltecc2241b2011-07-19 16:06:06 +0200251 DEBUGP(DLMI, "TX %u: %s\n", ts_nr, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200252
253 ret = send(bfd->fd, msg->data, msg->len, 0);
254 msgb_free(msg);
255
256 /* set tx delay timer for next event */
257 e1i_ts->sign.tx_timer.cb = timeout_ts1_write;
258 e1i_ts->sign.tx_timer.data = e1i_ts;
259
260 /* Reducing this might break the nanoBTS 900 init. */
261 osmo_timer_schedule(&e1i_ts->sign.tx_timer, 0, e1i_ts->sign.delay);
262
263 return ret;
264}
265
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200266static int handle_ts1_write(struct osmo_fd *bfd)
267{
268 struct e1inp_line *line = bfd->data;
269
270 return __handle_ts1_write(bfd, line);
271}
272
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200273int hsl_bts_write(struct ipa_client_conn *link)
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200274{
275 struct e1inp_line *line = link->line;
276
277 return __handle_ts1_write(link->ofd, line);
278}
279
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200280/* callback from select.c in case one of the fd's can be read/written */
281static int hsl_fd_cb(struct osmo_fd *bfd, unsigned int what)
282{
283 struct e1inp_line *line = bfd->data;
284 unsigned int ts_nr = bfd->priv_nr;
285 unsigned int idx = ts_nr-1;
286 struct e1inp_ts *e1i_ts;
287 int rc = 0;
288
289 /* In case of early RSL we might not yet have a line */
290
291 if (line)
292 e1i_ts = &line->ts[idx];
293
294 if (!line || e1i_ts->type == E1INP_TS_TYPE_SIGN) {
295 if (what & BSC_FD_READ)
296 rc = handle_ts1_read(bfd);
297 if (what & BSC_FD_WRITE)
298 rc = handle_ts1_write(bfd);
299 } else
Harald Weltecc2241b2011-07-19 16:06:06 +0200300 LOGP(DLINP, LOGL_ERROR, "unknown E1 TS type %u\n", e1i_ts->type);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200301
302 return rc;
303}
304
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200305static void hsl_close(struct e1inp_sign_link *sign_link)
306{
Holger Hans Peter Freyther55467f02011-12-28 19:47:07 +0100307 struct e1inp_ts *e1i_ts = sign_link->ts;
308 struct osmo_fd *bfd = &e1i_ts->driver.ipaccess.fd;
309 return e1inp_close_socket(e1i_ts, sign_link, bfd);
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200310}
311
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200312static int hsl_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200313
314struct e1inp_driver hsl_driver = {
315 .name = "hsl",
316 .want_write = ts_want_write,
317 .line_update = hsl_line_update,
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200318 .close = hsl_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200319 .default_delay = 0,
320};
321
322/* callback of the OML listening filedescriptor */
323static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
324{
325 int ret;
326 int idx = 0;
327 int i;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200328 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200329 struct e1inp_ts *e1i_ts;
330 struct osmo_fd *bfd;
331 struct sockaddr_in sa;
332 socklen_t sa_len = sizeof(sa);
333
334 if (!(what & BSC_FD_READ))
335 return 0;
336
337 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
338 if (ret < 0) {
339 perror("accept");
340 return ret;
341 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200342 LOGP(DLINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200343 inet_ntoa(sa.sin_addr));
344
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200345 /* clone virtual E1 line for this new signalling link. */
346 line = e1inp_line_clone(tall_hsl_ctx, listen_bfd->data);
347 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200348 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200349 return -1;
350 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200351 /* create virrtual E1 timeslots for signalling */
352 e1inp_ts_config_sign(&line->ts[1-1], line);
353
354 /* initialize the fds */
355 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
356 line->ts[i].driver.ipaccess.fd.fd = -1;
357
358 e1i_ts = &line->ts[idx];
359
360 bfd = &e1i_ts->driver.ipaccess.fd;
361 bfd->fd = ret;
362 bfd->data = line;
363 bfd->priv_nr = PRIV_OML;
364 bfd->cb = hsl_fd_cb;
365 bfd->when = BSC_FD_READ;
366 ret = osmo_fd_register(bfd);
367 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200368 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200369 close(bfd->fd);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200370 e1inp_line_put(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200371 return ret;
372 }
373
374 return ret;
375}
376
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200377static int hsl_bts_process(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200378{
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200379 struct ipaccess_head *hh;
380 struct e1inp_sign_link *sign_link;
381 struct e1inp_ts *e1i_ts = &link->line->ts[0];
382
383 hh = (struct ipaccess_head *) msg->data;
384 if (hh->proto == HSL_PROTO_DEBUG) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200385 LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n",
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200386 msg->data + sizeof(*hh));
387 msgb_free(msg);
388 return 0;
389 }
390 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
391 if (!sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200392 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200393 "hh->proto=0x%02x\n", hh->proto);
394 msgb_free(msg);
395 return -EIO;
396 }
397 msg->dst = sign_link;
398
399 /* XXX better use e1inp_ts_rx? */
400 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200401 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200402 "no action set for signalling messages.\n");
403 return -ENOENT;
404 }
405 link->line->ops->sign_link(msg);
406 return 0;
407}
408
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200409static int hsl_bts_connect(struct ipa_client_conn *link)
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200410{
411 struct msgb *msg;
412 uint8_t *serno;
413 char serno_buf[16];
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200414 struct hsl_unit *unit = link->line->ops->cfg.ipa.dev;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200415 struct e1inp_sign_link *sign_link;
416
417 /* send the minimal message to identify this BTS. */
418 msg = ipa_msg_alloc(0);
419 if (!msg)
420 return -ENOMEM;
421
422 *msgb_put(msg, 1) = 0x80;
423 *msgb_put(msg, 1) = 0x80;
424 *msgb_put(msg, 1) = unit->swversion;
Harald Welte3e1d84b2011-08-19 13:32:18 +0200425 snprintf(serno_buf, sizeof(serno_buf), "%"PRIx64, unit->serno);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200426 serno = msgb_put(msg, strlen(serno_buf)+1);
427 memcpy(serno, serno_buf, strlen(serno_buf));
428 ipa_msg_push_header(msg, 0);
429 send(link->ofd->fd, msg->data, msg->len, 0);
430 msgb_free(msg);
431
432 /* ... and enable the signalling link. */
433 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200434 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200435 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200436 ipa_client_conn_close(link);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200437 return -EINVAL;
438 }
439 sign_link = link->line->ops->sign_link_up(&unit,
440 link->line, E1INP_SIGN_NONE);
441 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200442 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200443 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200444 ipa_client_conn_close(link);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200445 return -EINVAL;
446 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200447 return 0;
448}
449
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200450struct hsl_line {
451 int line_already_initialized;
452};
453
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200454static int hsl_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200455{
456 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200457 struct hsl_line *hl;
458
459 if (!line->driver_data)
460 line->driver_data = talloc_zero(line, struct hsl_line);
461
462 if (!line->driver_data) {
463 LOGP(DLINP, LOGL_NOTICE, "hsl: OOM in line update\n");
464 return -ENOMEM;
465 }
466 hl = line->driver_data;
467
468 /* We only initialize this line once. */
469 if (hl->line_already_initialized)
470 return 0;
471
472 hl->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200473
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200474 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200475 case E1INP_LINE_R_BSC:
Harald Weltecc2241b2011-07-19 16:06:06 +0200476 LOGP(DLINP, LOGL_NOTICE, "enabling hsl BSC mode\n");
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200477
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200478 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200479 line->ops->cfg.ipa.addr,
480 HSL_TCP_PORT, OSMO_SOCK_F_BIND);
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200481 if (ret < 0)
482 return ret;
483
484 e1h->listen_fd.fd = ret;
485 e1h->listen_fd.when |= BSC_FD_READ;
486 e1h->listen_fd.cb = listen_fd_cb;
487 e1h->listen_fd.data = line;
488
489 if (osmo_fd_register(&e1h->listen_fd) < 0) {
490 close(ret);
491 return ret;
492 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200493 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200494 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200495 struct ipa_client_conn *link;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200496
Harald Weltecc2241b2011-07-19 16:06:06 +0200497 LOGP(DLINP, LOGL_NOTICE, "enabling hsl BTS mode\n");
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200498
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200499 link = ipa_client_conn_create(tall_hsl_ctx,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200500 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200501 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200502 line->ops->cfg.ipa.addr,
503 HSL_TCP_PORT,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200504 hsl_bts_connect,
505 hsl_bts_process,
506 hsl_bts_write,
507 NULL);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200508 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200509 LOGP(DLINP, LOGL_ERROR, "cannot create BTS link: %s\n",
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200510 strerror(errno));
511 return -ENOMEM;
512 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200513 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200514 LOGP(DLINP, LOGL_ERROR, "cannot open BTS link: %s\n",
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200515 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200516 ipa_client_conn_close(link);
517 ipa_client_conn_destroy(link);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200518 return -EIO;
519 }
520 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200521 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200522 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200523 default:
524 break;
525 }
526 return ret;
527}
528
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200529void e1inp_hsl_init(void)
530{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200531 tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl");
532
533 e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200534 if (!e1h)
535 return;
536
537 e1inp_driver_register(&hsl_driver);
538}