[BSC] Keep a SYSTEM INFORMATION cache for each BTS

This will later be useful for handover where we need to copy the cell
channel allocation into a normal 04.08 message
diff --git a/openbsc/include/openbsc/gsm_data.h b/openbsc/include/openbsc/gsm_data.h
index 170ebab..5533fea 100644
--- a/openbsc/include/openbsc/gsm_data.h
+++ b/openbsc/include/openbsc/gsm_data.h
@@ -43,6 +43,7 @@
 #include <osmocore/timer.h>
 #include <openbsc/gsm_04_08.h>
 #include <openbsc/abis_rsl.h>
+#include <openbsc/system_information.h>
 #include <openbsc/mncc.h>
 #include <osmocore/tlv.h>
 #include <osmocore/bitvec.h>
@@ -393,6 +394,8 @@
 	struct gsm_bts_trx_ts ts[TRX_NR_TS];
 };
 
+#define GSM_BTS_SI(bts, i)	(void *)(bts->si_buf[i])
+
 enum gsm_bts_type {
 	GSM_BTS_TYPE_UNKNOWN,
 	GSM_BTS_TYPE_BS11,
@@ -541,6 +544,9 @@
 		} data;
 	} si_common;
 
+	/* buffers where we put the pre-computed SI */
+	sysinfo_buf_t si_buf[_MAX_SYSINFO_TYPE];
+
 	/* ip.accesss Unit ID's have Site/BTS/TRX layout */
 	union {
 		struct {
diff --git a/openbsc/include/openbsc/system_information.h b/openbsc/include/openbsc/system_information.h
index 982a9ac..55c00d8 100644
--- a/openbsc/include/openbsc/system_information.h
+++ b/openbsc/include/openbsc/system_information.h
@@ -1,6 +1,39 @@
 #ifndef _SYSTEM_INFO_H
 #define _SYSTEM_INFO_H
 
-int gsm_generate_si(u_int8_t *output, struct gsm_bts *bts, int type);
+#define GSM_MACBLOCK_LEN 		23
+
+enum osmo_sysinfo_type {
+	SYSINFO_TYPE_NONE,
+	SYSINFO_TYPE_1,
+	SYSINFO_TYPE_2,
+	SYSINFO_TYPE_3,
+	SYSINFO_TYPE_4,
+	SYSINFO_TYPE_5,
+	SYSINFO_TYPE_6,
+	SYSINFO_TYPE_7,
+	SYSINFO_TYPE_8,
+	SYSINFO_TYPE_9,
+	SYSINFO_TYPE_10,
+	SYSINFO_TYPE_13,
+	SYSINFO_TYPE_16,
+	SYSINFO_TYPE_17,
+	SYSINFO_TYPE_18,
+	SYSINFO_TYPE_19,
+	SYSINFO_TYPE_20,
+	SYSINFO_TYPE_2bis,
+	SYSINFO_TYPE_2ter,
+	SYSINFO_TYPE_2quater,
+	SYSINFO_TYPE_5bis,
+	SYSINFO_TYPE_5ter,
+	/* FIXME all the various bis and ter */
+	_MAX_SYSINFO_TYPE
+};
+
+typedef u_int8_t sysinfo_buf_t[GSM_MACBLOCK_LEN];
+
+uint8_t gsm_sitype2rsl(enum osmo_sysinfo_type si_type);
+const char *gsm_sitype_name(enum osmo_sysinfo_type si_type);
+int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type type);
 
 #endif
diff --git a/openbsc/src/bsc_init.c b/openbsc/src/bsc_init.c
index b69aebd..0066811 100644
--- a/openbsc/src/bsc_init.c
+++ b/openbsc/src/bsc_init.c
@@ -782,54 +782,71 @@
 	return 0;
 }
 
+static int generate_and_rsl_si(struct gsm_bts_trx *trx, enum osmo_sysinfo_type i)
+{
+	struct gsm_bts *bts = trx->bts;
+	int rc;
+
+	rc = gsm_generate_si(bts, i);
+	if (rc < 0)
+		return rc;
+
+	DEBUGP(DRR, "SI%s: %s\n", gsm_sitype_name(i),
+		hexdump(GSM_BTS_SI(bts, i), GSM_MACBLOCK_LEN));
+
+	switch (i) {
+	case SYSINFO_TYPE_5:
+	case SYSINFO_TYPE_5bis:
+	case SYSINFO_TYPE_5ter:
+	case SYSINFO_TYPE_6:
+		rc = rsl_sacch_filling(trx, gsm_sitype2rsl(i),
+				       GSM_BTS_SI(bts, i), rc);
+		break;
+	default:
+		rc = rsl_bcch_info(trx, gsm_sitype2rsl(i),
+				   GSM_BTS_SI(bts, i), rc);
+		break;
+	}
+
+	return rc;
+}
+
 /* set all system information types */
 static int set_system_infos(struct gsm_bts_trx *trx)
 {
 	int i, rc;
-	u_int8_t si_tmp[23];
 	struct gsm_bts *bts = trx->bts;
 
 	bts->si_common.cell_sel_par.ms_txpwr_max_ccch =
 			ms_pwr_ctl_lvl(bts->band, bts->ms_max_power);
 	bts->si_common.cell_sel_par.neci = bts->network->neci;
 
-	if (trx == trx->bts->c0) {
-		for (i = 1; i <= 4; i++) {
-			rc = gsm_generate_si(si_tmp, trx->bts, i);
+	if (trx == bts->c0) {
+		for (i = SYSINFO_TYPE_1; i <= SYSINFO_TYPE_4; i++) {
+			rc = generate_and_rsl_si(trx, i);
 			if (rc < 0)
 				goto err_out;
-			DEBUGP(DRR, "SI%2u: %s\n", i, hexdump(si_tmp, rc));
-			rsl_bcch_info(trx, i, si_tmp, sizeof(si_tmp));
 		}
 		if (bts->gprs.mode != BTS_GPRS_NONE) {
-			i = 13;
-			rc = gsm_generate_si(si_tmp, trx->bts, RSL_SYSTEM_INFO_13);
+			rc = generate_and_rsl_si(trx, SYSINFO_TYPE_13);
 			if (rc < 0)
 				goto err_out;
-			DEBUGP(DRR, "SI%2u: %s\n", i, hexdump(si_tmp, rc));
-			rsl_bcch_info(trx, RSL_SYSTEM_INFO_13, si_tmp, rc);
 		}
 	}
 
-	i = 5;
-	rc = gsm_generate_si(si_tmp, trx->bts, RSL_SYSTEM_INFO_5);
+	rc = generate_and_rsl_si(trx, SYSINFO_TYPE_5);
 	if (rc < 0)
 		goto err_out;
-	DEBUGP(DRR, "SI%2u: %s\n", i, hexdump(si_tmp, rc));
-	rsl_sacch_filling(trx, RSL_SYSTEM_INFO_5, si_tmp, rc);
 
-	i = 6;
-	rc = gsm_generate_si(si_tmp, trx->bts, RSL_SYSTEM_INFO_6);
+	rc = generate_and_rsl_si(trx, SYSINFO_TYPE_6);
 	if (rc < 0)
 		goto err_out;
-	DEBUGP(DRR, "SI%2u: %s\n", i, hexdump(si_tmp, rc));
-	rsl_sacch_filling(trx, RSL_SYSTEM_INFO_6, si_tmp, rc);
 
 	return 0;
 err_out:
 	LOGP(DRR, LOGL_ERROR, "Cannot generate SI %u for BTS %u, most likely "
 		"a problem with neighbor cell list generation\n",
-		i, trx->bts->nr);
+		i, bts->nr);
 	return rc;
 }
 
diff --git a/openbsc/src/system_information.c b/openbsc/src/system_information.c
index 508787d..a142258 100644
--- a/openbsc/src/system_information.c
+++ b/openbsc/src/system_information.c
@@ -1,7 +1,7 @@
 /* GSM 04.08 System Information (SI) encoding and decoding
  * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
 
-/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
+/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
  *
  * All Rights Reserved
  *
@@ -32,10 +32,10 @@
 #include <openbsc/abis_rsl.h>
 #include <openbsc/rest_octets.h>
 #include <osmocore/bitvec.h>
+#include <osmocore/utils.h>
 #include <openbsc/debug.h>
 
 #define GSM48_CELL_CHAN_DESC_SIZE	16
-#define GSM_MACBLOCK_LEN 		23
 #define GSM_MACBLOCK_PADDING		0x2b
 
 /* verify the sizes of the system information type structs */
@@ -240,6 +240,7 @@
 
 	/* SI1 Rest Octets (10.5.2.32), contains NCH position */
 	rc = rest_octets_si1(si1->rest_octets, NULL);
+
 	return sizeof(*si1) + rc;
 }
 
@@ -474,8 +475,91 @@
 	return sizeof (*si13) + ret;
 }
 
-int gsm_generate_si(u_int8_t *output, struct gsm_bts *bts, int type)
+static const uint8_t sitype2rsl[_MAX_SYSINFO_TYPE] = {
+	[SYSINFO_TYPE_1]	= RSL_SYSTEM_INFO_1,
+	[SYSINFO_TYPE_2]	= RSL_SYSTEM_INFO_2,
+	[SYSINFO_TYPE_3]	= RSL_SYSTEM_INFO_3,
+	[SYSINFO_TYPE_4]	= RSL_SYSTEM_INFO_4,
+	[SYSINFO_TYPE_5]	= RSL_SYSTEM_INFO_5,
+	[SYSINFO_TYPE_6]	= RSL_SYSTEM_INFO_6,
+	[SYSINFO_TYPE_7]	= RSL_SYSTEM_INFO_7,
+	[SYSINFO_TYPE_8]	= RSL_SYSTEM_INFO_8,
+	[SYSINFO_TYPE_9]	= RSL_SYSTEM_INFO_9,
+	[SYSINFO_TYPE_10]	= RSL_SYSTEM_INFO_10,
+	[SYSINFO_TYPE_13]	= RSL_SYSTEM_INFO_13,
+	[SYSINFO_TYPE_16]	= RSL_SYSTEM_INFO_16,
+	[SYSINFO_TYPE_17]	= RSL_SYSTEM_INFO_17,
+	[SYSINFO_TYPE_18]	= RSL_SYSTEM_INFO_18,
+	[SYSINFO_TYPE_19]	= RSL_SYSTEM_INFO_19,
+	[SYSINFO_TYPE_20]	= RSL_SYSTEM_INFO_20,
+	[SYSINFO_TYPE_2bis]	= RSL_SYSTEM_INFO_2bis,
+	[SYSINFO_TYPE_2ter]	= RSL_SYSTEM_INFO_2ter,
+	[SYSINFO_TYPE_2quater]	= RSL_SYSTEM_INFO_2quater,
+	[SYSINFO_TYPE_5bis]	= RSL_SYSTEM_INFO_5bis,
+	[SYSINFO_TYPE_5ter]	= RSL_SYSTEM_INFO_5ter,
+};
+
+static const uint8_t rsl2sitype[0xff] = {
+	[RSL_SYSTEM_INFO_1] = SYSINFO_TYPE_1,
+	[RSL_SYSTEM_INFO_2] = SYSINFO_TYPE_2,
+	[RSL_SYSTEM_INFO_3] = SYSINFO_TYPE_3,
+	[RSL_SYSTEM_INFO_4] = SYSINFO_TYPE_4,
+	[RSL_SYSTEM_INFO_5] = SYSINFO_TYPE_5,
+	[RSL_SYSTEM_INFO_6] = SYSINFO_TYPE_6,
+	[RSL_SYSTEM_INFO_13] = SYSINFO_TYPE_13,
+};
+
+typedef int (*gen_si_fn_t)(uint8_t *output, struct gsm_bts *bts);
+
+static const gen_si_fn_t gen_si_fn[_MAX_SYSINFO_TYPE] = {
+	[SYSINFO_TYPE_1] = &generate_si1,
+	[SYSINFO_TYPE_2] = &generate_si2,
+	[SYSINFO_TYPE_3] = &generate_si3,
+	[SYSINFO_TYPE_4] = &generate_si4,
+	[SYSINFO_TYPE_5] = &generate_si5,
+	[SYSINFO_TYPE_6] = &generate_si6,
+	[SYSINFO_TYPE_13] = &generate_si13,
+};
+
+static const struct value_string sitype_strs[] = {
+	{ SYSINFO_TYPE_1,	"1" },
+	{ SYSINFO_TYPE_2,	"2" },
+	{ SYSINFO_TYPE_3,	"3" },
+	{ SYSINFO_TYPE_4,	"4" },
+	{ SYSINFO_TYPE_5,	"5" },
+	{ SYSINFO_TYPE_6,	"6" },
+	{ SYSINFO_TYPE_7,	"7" },
+	{ SYSINFO_TYPE_8,	"8" },
+	{ SYSINFO_TYPE_9,	"9" },
+	{ SYSINFO_TYPE_10,	"10" },
+	{ SYSINFO_TYPE_13,	"13" },
+	{ SYSINFO_TYPE_16,	"16" },
+	{ SYSINFO_TYPE_17,	"17" },
+	{ SYSINFO_TYPE_18,	"18" },
+	{ SYSINFO_TYPE_19,	"19" },
+	{ SYSINFO_TYPE_20,	"20" },
+	{ SYSINFO_TYPE_2bis,	"2bis" },
+	{ SYSINFO_TYPE_2ter,	"2ter" },
+	{ SYSINFO_TYPE_2quater,	"2quater" },
+	{ SYSINFO_TYPE_5bis,	"5bis" },
+	{ SYSINFO_TYPE_5ter,	"5ter" },
+	{ 0, NULL }
+};
+
+uint8_t gsm_sitype2rsl(enum osmo_sysinfo_type si_type)
 {
+	return sitype2rsl[si_type];
+}
+
+const char *gsm_sitype_name(enum osmo_sysinfo_type si_type)
+{
+	return get_value_string(sitype_strs, si_type);
+}
+
+int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type)
+{
+	gen_si_fn_t gen_si;
+
 	switch (bts->gprs.mode) {
 	case BTS_GPRS_EGPRS:
 		si13_default.cell_opts.ext_info_present = 1;
@@ -489,24 +573,9 @@
 		break;
 	}
 
-	switch (type) {
-	case RSL_SYSTEM_INFO_1:
-		return generate_si1(output, bts);
-	case RSL_SYSTEM_INFO_2:
-		return generate_si2(output, bts);
-	case RSL_SYSTEM_INFO_3:
-		return generate_si3(output, bts);
-	case RSL_SYSTEM_INFO_4:
-		return generate_si4(output, bts);
-	case RSL_SYSTEM_INFO_5:
-		return generate_si5(output, bts);
-	case RSL_SYSTEM_INFO_6:
-		return generate_si6(output, bts);
-	case RSL_SYSTEM_INFO_13:
-		return generate_si13(output, bts);
-	default:
+	gen_si = gen_si_fn[si_type];
+	if (!gen_si)
 		return -EINVAL;
-	}
 
-	return 0;
+	return gen_si(bts->si_buf[si_type], bts);
 }