bsc: on-demand setup of nanoBTS and HSL femto sockets

The daemons set up nanoBTS and HSL femto sockets by default, ie. the
three sockets to support these two drivers are open even if we have
no BTS of that kind.

This patch enables on-demand socket creation, ie. we only enable them
if we have one BTS at least that requires it.

I added two new attributes to the gsm_bts object, they are:

* the start() function includes the code that we need to run to start
the BTS. This new function contains the socket creation in the
particular case of nanoBTS and HSL femto.

* the started boolean, which is used to know if we have already
started the BTS, ie. we have already invoked start().

Note that, I have splitted the bts_model_*_init() function into two
functions, the _init() functions that register the BTS driver
and the _start() functions that start BTS driver on-demand.

While I was at it, I added several changes/cleanups to this patch:

* Group all bts_model_*_init() calls into one function bts_init(),
which is called in the initialization path of osmo-nitb and
osmo-bsc.

* Add openbsc/bss.h that contains the declaration of
bsc_bootstrap_network, bsc_shutdown_net and bts_init.

* Add missing e1inp_init() in osmo-bsc.

* Fix missing declaration of hsl_setup in openbsc/e1_input.h
diff --git a/openbsc/include/openbsc/Makefile.am b/openbsc/include/openbsc/Makefile.am
index 325d66d..f87eca1 100644
--- a/openbsc/include/openbsc/Makefile.am
+++ b/openbsc/include/openbsc/Makefile.am
@@ -11,7 +11,8 @@
 		gb_proxy.h gprs_sgsn.h gsm_04_08_gprs.h sgsn.h \
 		gprs_ns_frgre.h auth.h osmo_msc.h bsc_msc.h bsc_nat.h \
 		osmo_bsc_rf.h osmo_bsc.h network_listen.h bsc_nat_sccp.h \
-		osmo_msc_data.h osmo_bsc_grace.h sms_queue.h abis_om2000.h
+		osmo_msc_data.h osmo_bsc_grace.h sms_queue.h abis_om2000.h \
+		bss.h
 
 openbsc_HEADERS = gsm_04_08.h meas_rep.h bsc_api.h
 openbscdir = $(includedir)/openbsc
diff --git a/openbsc/include/openbsc/bss.h b/openbsc/include/openbsc/bss.h
new file mode 100644
index 0000000..05495dd
--- /dev/null
+++ b/openbsc/include/openbsc/bss.h
@@ -0,0 +1,17 @@
+#ifndef _BSS_H_
+#define _BSS_H_
+
+struct gsm_network;
+struct msgb;
+
+/* start and stop network */
+extern int bsc_bootstrap_network(int (*mncc_recv)(struct gsm_network *, struct msgb *), const char *cfg_file);
+extern int bsc_shutdown_net(struct gsm_network *net);
+
+/* register all supported BTS */
+extern int bts_init(void);
+extern int bts_model_bs11_init(void);
+extern int bts_model_rbs2k_init(void);
+extern int bts_model_nanobts_init(void);
+extern int bts_model_hslfemto_init(void);
+#endif
diff --git a/openbsc/include/openbsc/e1_input.h b/openbsc/include/openbsc/e1_input.h
index 0eae5d9..0d79c7d 100644
--- a/openbsc/include/openbsc/e1_input.h
+++ b/openbsc/include/openbsc/e1_input.h
@@ -175,6 +175,7 @@
 
 int ia_config_connect(struct gsm_bts *bts, struct sockaddr_in *sin);
 int ipaccess_setup(struct gsm_network *gsmnet);
+int hsl_setup(struct gsm_network *gsmnet);
 
 extern struct llist_head e1inp_driver_list;
 extern struct llist_head e1inp_line_list;
diff --git a/openbsc/include/openbsc/gsm_data.h b/openbsc/include/openbsc/gsm_data.h
index bc276d1..c3cf104 100644
--- a/openbsc/include/openbsc/gsm_data.h
+++ b/openbsc/include/openbsc/gsm_data.h
@@ -1,6 +1,7 @@
 #ifndef _GSM_DATA_H
 #define _GSM_DATA_H
 
+#include <stdbool.h>
 
 struct osmo_msc_data;
 struct osmo_bsc_sccp_con;
@@ -434,6 +435,8 @@
 	enum gsm_bts_type type;
 	const char *name;
 
+	bool started;
+	int (*start)(struct gsm_network *net);
 	int (*oml_rcvmsg)(struct msgb *msg);
 
 	void (*config_write_bts)(struct vty *vty, struct gsm_bts *bts);
diff --git a/openbsc/src/libbsc/Makefile.am b/openbsc/src/libbsc/Makefile.am
index 70f6c1c..3af4a2a 100644
--- a/openbsc/src/libbsc/Makefile.am
+++ b/openbsc/src/libbsc/Makefile.am
@@ -20,5 +20,5 @@
 			e1_config.c \
 			bsc_api.c bsc_msc.c bsc_vty.c \
 			gsm_04_08_utils.c \
-			bsc_init.c
+			bsc_init.c bts_init.c
 
diff --git a/openbsc/src/libbsc/bsc_init.c b/openbsc/src/libbsc/bsc_init.c
index ecb4e44..19453f7 100644
--- a/openbsc/src/libbsc/bsc_init.c
+++ b/openbsc/src/libbsc/bsc_init.c
@@ -315,6 +315,14 @@
 {
 	int i, n;
 
+	if (bts->model->start && !bts->model->started) {
+		int ret = bts->model->start(bts->network);
+		if (ret < 0)
+			return ret;
+
+		bts->model->started = true;
+	}
+
 	/* FIXME: What about secondary TRX of a BTS?  What about a BTS that has TRX
 	 * in different bands? Why is 'band' a parameter of the BTS and not of the TRX? */
 	switch (bts->band) {
@@ -456,10 +464,5 @@
 			return rc;
 		}
 	}
-
-	/* initialize nanoBTS support omce */
-	rc = ipaccess_setup(bsc_gsmnet);
-	rc = hsl_setup(bsc_gsmnet);
-
 	return 0;
 }
diff --git a/openbsc/src/libbsc/bts_ericsson_rbs2000.c b/openbsc/src/libbsc/bts_ericsson_rbs2000.c
index c2ae0e7..9c98a9f 100644
--- a/openbsc/src/libbsc/bts_ericsson_rbs2000.c
+++ b/openbsc/src/libbsc/bts_ericsson_rbs2000.c
@@ -242,14 +242,17 @@
 	abis_om2k_config_write_bts(vty, bts);
 }
 
+static int bts_model_rbs2k_start(struct gsm_network *net);
+
 static struct gsm_bts_model model_rbs2k = {
 	.type = GSM_BTS_TYPE_RBS2000,
 	.name = "rbs2000",
+	.start = bts_model_rbs2k_start,
 	.oml_rcvmsg = &abis_om2k_rcvmsg,
 	.config_write_bts = &config_write_bts,
 };
 
-int bts_model_rbs2k_init(void)
+static int bts_model_rbs2k_start(struct gsm_network *net)
 {
 	model_rbs2k.features.data = &model_rbs2k._features_data[0];
 	model_rbs2k.features.data_len = sizeof(model_rbs2k._features_data);
@@ -261,5 +264,10 @@
 	osmo_signal_register_handler(SS_GLOBAL, gbl_sig_cb, NULL);
 	osmo_signal_register_handler(SS_NM, nm_sig_cb, NULL);
 
+	return 0;
+}
+
+int bts_model_rbs2k_init(void)
+{
 	return gsm_bts_model_register(&model_rbs2k);
 }
diff --git a/openbsc/src/libbsc/bts_hsl_femtocell.c b/openbsc/src/libbsc/bts_hsl_femtocell.c
index 7264a21..f943693 100644
--- a/openbsc/src/libbsc/bts_hsl_femtocell.c
+++ b/openbsc/src/libbsc/bts_hsl_femtocell.c
@@ -30,8 +30,11 @@
 #include <openbsc/signal.h>
 #include <openbsc/e1_input.h>
 
+static int bts_model_hslfemto_start(struct gsm_network *net);
+
 static struct gsm_bts_model model_hslfemto = {
 	.type = GSM_BTS_TYPE_HSL_FEMTO,
+	.start = bts_model_hslfemto_start,
 	.nm_att_tlvdef = {
 		.def = {
 			/* no HSL specific OML attributes that we know of */
@@ -148,7 +151,7 @@
 	return 0;
 }
 
-int bts_model_hslfemto_init(void)
+static int bts_model_hslfemto_start(struct gsm_network *net)
 {
 	model_hslfemto.features.data = &model_hslfemto._features_data[0];
 	model_hslfemto.features.data_len = sizeof(model_hslfemto._features_data);
@@ -158,5 +161,11 @@
 
 	osmo_signal_register_handler(SS_INPUT, inp_sig_cb, NULL);
 
+	/* Call A-bis input driver, start socket for OML and RSL. */
+	return hsl_setup(net);
+}
+
+int bts_model_hslfemto_init(void)
+{
 	return gsm_bts_model_register(&model_hslfemto);
 }
diff --git a/openbsc/src/libbsc/bts_init.c b/openbsc/src/libbsc/bts_init.c
new file mode 100644
index 0000000..87bdde0
--- /dev/null
+++ b/openbsc/src/libbsc/bts_init.c
@@ -0,0 +1,29 @@
+/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
+ *
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+#include <openbsc/bss.h>
+
+int bts_init(void)
+{
+	bts_model_bs11_init();
+	bts_model_rbs2k_init();
+	bts_model_nanobts_init();
+	bts_model_hslfemto_init();
+	/* Your new BTS here. */
+	return 0;
+}
diff --git a/openbsc/src/libbsc/bts_ipaccess_nanobts.c b/openbsc/src/libbsc/bts_ipaccess_nanobts.c
index 13bed22..706712e 100644
--- a/openbsc/src/libbsc/bts_ipaccess_nanobts.c
+++ b/openbsc/src/libbsc/bts_ipaccess_nanobts.c
@@ -26,10 +26,14 @@
 #include <openbsc/gsm_data.h>
 #include <openbsc/signal.h>
 #include <openbsc/abis_nm.h>
+#include <openbsc/e1_input.h> /* for ipaccess_setup() */
+
+static int bts_model_nanobts_start(struct gsm_network *net);
 
 static struct gsm_bts_model model_nanobts = {
 	.type = GSM_BTS_TYPE_NANOBTS,
 	.name = "nanobts",
+	.start = bts_model_nanobts_start,
 	.oml_rcvmsg = &abis_nm_rcvmsg,
 	.nm_att_tlvdef = {
 		.def = {
@@ -435,7 +439,7 @@
 	return 0;
 }
 
-int bts_model_nanobts_init(void)
+static int bts_model_nanobts_start(struct gsm_network *net)
 {
 	model_nanobts.features.data = &model_nanobts._features_data[0];
 	model_nanobts.features.data_len = sizeof(model_nanobts._features_data);
@@ -445,5 +449,11 @@
 
 	osmo_signal_register_handler(SS_NM, nm_sig_cb, NULL);
 
+	/* Call A-bis input driver, start server sockets for OML and RSL. */
+	return ipaccess_setup(net);
+}
+
+int bts_model_nanobts_init(void)
+{
 	return gsm_bts_model_register(&model_nanobts);
 }
diff --git a/openbsc/src/libbsc/bts_siemens_bs11.c b/openbsc/src/libbsc/bts_siemens_bs11.c
index 44f9358..df4a1dc 100644
--- a/openbsc/src/libbsc/bts_siemens_bs11.c
+++ b/openbsc/src/libbsc/bts_siemens_bs11.c
@@ -28,9 +28,12 @@
 #include <openbsc/e1_input.h>
 #include <openbsc/signal.h>
 
+static int bts_model_bs11_start(struct gsm_network *net);
+
 static struct gsm_bts_model model_bs11 = {
 	.type = GSM_BTS_TYPE_BS11,
 	.name = "bs11",
+	.start = bts_model_bs11_start,
 	.oml_rcvmsg = &abis_nm_rcvmsg,
 	.nm_att_tlvdef = {
 		.def = {
@@ -575,7 +578,7 @@
 	return 0;
 }
 
-int bts_model_bs11_init(void)
+static int bts_model_bs11_start(struct gsm_network *net)
 {
 	model_bs11.features.data = &model_bs11._features_data[0];
 	model_bs11.features.data_len = sizeof(model_bs11._features_data);
@@ -586,5 +589,10 @@
 	osmo_signal_register_handler(SS_INPUT, inp_sig_cb, NULL);
 	osmo_signal_register_handler(SS_GLOBAL, gbl_sig_cb, NULL);
 
+	return 0;
+}
+
+int bts_model_bs11_init(void)
+{
 	return gsm_bts_model_register(&model_bs11);
 }
diff --git a/openbsc/src/libcommon/gsm_data.c b/openbsc/src/libcommon/gsm_data.c
index 45e56af..480eb80 100644
--- a/openbsc/src/libcommon/gsm_data.c
+++ b/openbsc/src/libcommon/gsm_data.c
@@ -573,6 +573,14 @@
 	bts->type = type;
 	bts->model = model;
 
+	if (model->start && !model->started) {
+		int ret = model->start(bts->network);
+		if (ret < 0)
+			return ret;
+
+		model->started = true;
+	}
+
 	switch (bts->type) {
 	case GSM_BTS_TYPE_HSL_FEMTO:
 		bts->c0->rsl_tei = 0;
diff --git a/openbsc/src/osmo-bsc/osmo_bsc_main.c b/openbsc/src/osmo-bsc/osmo_bsc_main.c
index 16495d8..42c74cc 100644
--- a/openbsc/src/osmo-bsc/osmo_bsc_main.c
+++ b/openbsc/src/osmo-bsc/osmo_bsc_main.c
@@ -50,8 +50,6 @@
 extern const char *openbsc_copyright;
 static int daemonize = 0;
 
-extern int bsc_bootstrap_network(int (*layer4)(struct gsm_network *, struct msgb *), const char *cfg_file);
-
 static void print_usage()
 {
 	printf("Usage: osmo-bsc\n");
@@ -128,10 +126,6 @@
 	}
 }
 
-extern int bts_model_unknown_init(void);
-extern int bts_model_bs11_init(void);
-extern int bts_model_nanobts_init(void);
-
 extern enum node_type bsc_vty_go_parent(struct vty *vty);
 
 static struct vty_app_info vty_info = {
@@ -183,9 +177,8 @@
 
 	osmo_init_logging(&log_info);
 
-	bts_model_unknown_init();
-	bts_model_bs11_init();
-	bts_model_nanobts_init();
+	bts_init();
+	e1inp_init();
 
 	/* enable filters */
 
diff --git a/openbsc/src/osmo-nitb/bsc_hack.c b/openbsc/src/osmo-nitb/bsc_hack.c
index 1fa039b..2406873 100644
--- a/openbsc/src/osmo-nitb/bsc_hack.c
+++ b/openbsc/src/osmo-nitb/bsc_hack.c
@@ -40,6 +40,7 @@
 #include <openbsc/osmo_msc.h>
 #include <openbsc/sms_queue.h>
 #include <openbsc/vty.h>
+#include <openbsc/bss.h>
 
 #include "../../bscconfig.h"
 
@@ -55,10 +56,6 @@
 #define DB_SYNC_INTERVAL	60, 0
 static struct osmo_timer_list db_sync_timer;
 
-extern int bsc_bootstrap_network(int (*mncc_recv)(struct gsm_network *, struct msgb *),
-				 const char *cfg_file);
-extern int bsc_shutdown_net(struct gsm_network *net);
-
 static void create_pcap_file(char *file)
 {
 	mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
@@ -207,11 +204,6 @@
 	osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
 }
 
-extern int bts_model_unknown_init(void);
-extern int bts_model_bs11_init(void);
-extern int bts_model_nanobts_init(void);
-extern int bts_model_rbs2k_init(void);
-extern int bts_model_hslfemto_init(void);
 void talloc_ctx_init(void);
 
 extern enum node_type bsc_vty_go_parent(struct vty *vty);
@@ -237,12 +229,7 @@
 
 	osmo_init_logging(&log_info);
 
-	bts_model_unknown_init();
-	bts_model_bs11_init();
-	bts_model_nanobts_init();
-	bts_model_rbs2k_init();
-	bts_model_hslfemto_init();
-
+	bts_init();
 	e1inp_init();
 
 	/* This needs to precede handle_options() */