blob: 408228e4814c8b0a4bd0ed7264e4fb1f3811f210 [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{
310 struct e1inp_ts *ts = sign_link->ts;
311 struct osmo_fd *bfd = &ts->driver.ipaccess.fd;
Pablo Neira Ayusode668912011-08-16 17:26:23 +0200312 e1inp_event(ts, S_L_INP_TEI_DN, sign_link->tei, sign_link->sapi);
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200313 /* the first e1inp_sign_link_destroy call closes the socket. */
314 if (bfd->fd != -1) {
315 osmo_fd_unregister(bfd);
316 close(bfd->fd);
317 bfd->fd = -1;
318 }
319}
320
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200321static int hsl_line_update(struct e1inp_line *line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200322
323struct e1inp_driver hsl_driver = {
324 .name = "hsl",
325 .want_write = ts_want_write,
326 .line_update = hsl_line_update,
Pablo Neira Ayuso34073fb2011-07-08 20:36:05 +0200327 .close = hsl_close,
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200328 .default_delay = 0,
329};
330
331/* callback of the OML listening filedescriptor */
332static int listen_fd_cb(struct osmo_fd *listen_bfd, unsigned int what)
333{
334 int ret;
335 int idx = 0;
336 int i;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200337 struct e1inp_line *line;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200338 struct e1inp_ts *e1i_ts;
339 struct osmo_fd *bfd;
340 struct sockaddr_in sa;
341 socklen_t sa_len = sizeof(sa);
342
343 if (!(what & BSC_FD_READ))
344 return 0;
345
346 ret = accept(listen_bfd->fd, (struct sockaddr *) &sa, &sa_len);
347 if (ret < 0) {
348 perror("accept");
349 return ret;
350 }
Harald Weltecc2241b2011-07-19 16:06:06 +0200351 LOGP(DLINP, LOGL_NOTICE, "accept()ed new HSL link from %s\n",
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200352 inet_ntoa(sa.sin_addr));
353
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200354 /* clone virtual E1 line for this new signalling link. */
355 line = e1inp_line_clone(tall_hsl_ctx, listen_bfd->data);
356 if (line == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200357 LOGP(DLINP, LOGL_ERROR, "could not clone E1 line\n");
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200358 return -1;
359 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200360 /* create virrtual E1 timeslots for signalling */
361 e1inp_ts_config_sign(&line->ts[1-1], line);
362
363 /* initialize the fds */
364 for (i = 0; i < ARRAY_SIZE(line->ts); ++i)
365 line->ts[i].driver.ipaccess.fd.fd = -1;
366
367 e1i_ts = &line->ts[idx];
368
369 bfd = &e1i_ts->driver.ipaccess.fd;
370 bfd->fd = ret;
371 bfd->data = line;
372 bfd->priv_nr = PRIV_OML;
373 bfd->cb = hsl_fd_cb;
374 bfd->when = BSC_FD_READ;
375 ret = osmo_fd_register(bfd);
376 if (ret < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200377 LOGP(DLINP, LOGL_ERROR, "could not register FD\n");
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200378 close(bfd->fd);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200379 e1inp_line_put(line);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200380 return ret;
381 }
382
383 return ret;
384}
385
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200386static int hsl_bts_process(struct ipa_client_conn *link, struct msgb *msg)
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200387{
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200388 struct ipaccess_head *hh;
389 struct e1inp_sign_link *sign_link;
390 struct e1inp_ts *e1i_ts = &link->line->ts[0];
391
392 hh = (struct ipaccess_head *) msg->data;
393 if (hh->proto == HSL_PROTO_DEBUG) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200394 LOGP(DLINP, LOGL_NOTICE, "HSL debug: %s\n",
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200395 msg->data + sizeof(*hh));
396 msgb_free(msg);
397 return 0;
398 }
399 sign_link = e1inp_lookup_sign_link(e1i_ts, hh->proto, 0);
400 if (!sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200401 LOGP(DLINP, LOGL_ERROR, "no matching signalling link for "
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200402 "hh->proto=0x%02x\n", hh->proto);
403 msgb_free(msg);
404 return -EIO;
405 }
406 msg->dst = sign_link;
407
408 /* XXX better use e1inp_ts_rx? */
409 if (!link->line->ops->sign_link) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200410 LOGP(DLINP, LOGL_ERROR, "Fix your application, "
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200411 "no action set for signalling messages.\n");
412 return -ENOENT;
413 }
414 link->line->ops->sign_link(msg);
415 return 0;
416}
417
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200418static int hsl_bts_connect(struct ipa_client_conn *link)
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200419{
420 struct msgb *msg;
421 uint8_t *serno;
422 char serno_buf[16];
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200423 struct hsl_unit *unit = link->line->ops->cfg.ipa.dev;
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200424 struct e1inp_sign_link *sign_link;
425
426 /* send the minimal message to identify this BTS. */
427 msg = ipa_msg_alloc(0);
428 if (!msg)
429 return -ENOMEM;
430
431 *msgb_put(msg, 1) = 0x80;
432 *msgb_put(msg, 1) = 0x80;
433 *msgb_put(msg, 1) = unit->swversion;
Harald Welte3e1d84b2011-08-19 13:32:18 +0200434 snprintf(serno_buf, sizeof(serno_buf), "%"PRIx64, unit->serno);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200435 serno = msgb_put(msg, strlen(serno_buf)+1);
436 memcpy(serno, serno_buf, strlen(serno_buf));
437 ipa_msg_push_header(msg, 0);
438 send(link->ofd->fd, msg->data, msg->len, 0);
439 msgb_free(msg);
440
441 /* ... and enable the signalling link. */
442 if (!link->line->ops->sign_link_up) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200443 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200444 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200445 ipa_client_conn_close(link);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200446 return -EINVAL;
447 }
448 sign_link = link->line->ops->sign_link_up(&unit,
449 link->line, E1INP_SIGN_NONE);
450 if (sign_link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200451 LOGP(DLINP, LOGL_ERROR,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200452 "Unable to set signal link, closing socket.\n");
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200453 ipa_client_conn_close(link);
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200454 return -EINVAL;
455 }
Pablo Neira Ayuso591ddad2011-06-21 18:16:42 +0200456 return 0;
457}
458
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200459struct hsl_line {
460 int line_already_initialized;
461};
462
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200463static int hsl_line_update(struct e1inp_line *line)
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200464{
465 int ret = -ENOENT;
Pablo Neira Ayusod6216402011-08-31 16:47:44 +0200466 struct hsl_line *hl;
467
468 if (!line->driver_data)
469 line->driver_data = talloc_zero(line, struct hsl_line);
470
471 if (!line->driver_data) {
472 LOGP(DLINP, LOGL_NOTICE, "hsl: OOM in line update\n");
473 return -ENOMEM;
474 }
475 hl = line->driver_data;
476
477 /* We only initialize this line once. */
478 if (hl->line_already_initialized)
479 return 0;
480
481 hl->line_already_initialized = 1;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200482
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200483 switch(line->ops->cfg.ipa.role) {
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200484 case E1INP_LINE_R_BSC:
Harald Weltecc2241b2011-07-19 16:06:06 +0200485 LOGP(DLINP, LOGL_NOTICE, "enabling hsl BSC mode\n");
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200486
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200487 ret = osmo_sock_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200488 line->ops->cfg.ipa.addr,
489 HSL_TCP_PORT, OSMO_SOCK_F_BIND);
Pablo Neira Ayusof67471f2011-06-07 11:25:49 +0200490 if (ret < 0)
491 return ret;
492
493 e1h->listen_fd.fd = ret;
494 e1h->listen_fd.when |= BSC_FD_READ;
495 e1h->listen_fd.cb = listen_fd_cb;
496 e1h->listen_fd.data = line;
497
498 if (osmo_fd_register(&e1h->listen_fd) < 0) {
499 close(ret);
500 return ret;
501 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200502 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200503 case E1INP_LINE_R_BTS: {
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200504 struct ipa_client_conn *link;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200505
Harald Weltecc2241b2011-07-19 16:06:06 +0200506 LOGP(DLINP, LOGL_NOTICE, "enabling hsl BTS mode\n");
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200507
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200508 link = ipa_client_conn_create(tall_hsl_ctx,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200509 &line->ts[E1INP_SIGN_OML-1],
Pablo Neira Ayuso00af7722011-09-08 12:47:06 +0200510 E1INP_SIGN_OML,
Pablo Neira Ayuso4e862cb2011-08-19 18:43:38 +0200511 line->ops->cfg.ipa.addr,
512 HSL_TCP_PORT,
Pablo Neira Ayuso88136fc2011-07-08 16:21:55 +0200513 hsl_bts_connect,
514 hsl_bts_process,
515 hsl_bts_write,
516 NULL);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200517 if (link == NULL) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200518 LOGP(DLINP, LOGL_ERROR, "cannot create BTS link: %s\n",
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200519 strerror(errno));
520 return -ENOMEM;
521 }
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200522 if (ipa_client_conn_open(link) < 0) {
Harald Weltecc2241b2011-07-19 16:06:06 +0200523 LOGP(DLINP, LOGL_ERROR, "cannot open BTS link: %s\n",
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200524 strerror(errno));
Pablo Neira Ayusof0995672011-09-08 12:58:38 +0200525 ipa_client_conn_close(link);
526 ipa_client_conn_destroy(link);
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200527 return -EIO;
528 }
529 ret = 0;
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200530 break;
Pablo Neira Ayuso9b3a33c2011-06-21 13:52:41 +0200531 }
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200532 default:
533 break;
534 }
535 return ret;
536}
537
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200538void e1inp_hsl_init(void)
539{
Pablo Neira Ayuso54b49792011-06-07 12:15:26 +0200540 tall_hsl_ctx = talloc_named_const(libosmo_abis_ctx, 1, "hsl");
541
542 e1h = talloc_zero(tall_hsl_ctx, struct hsl_e1_handle);
Pablo Neira Ayuso0ba77d52011-06-05 18:32:44 +0200543 if (!e1h)
544 return;
545
546 e1inp_driver_register(&hsl_driver);
547}