port rest octets encoding code from osmo-bsc

As part of fixing issue OS#3075, we want to migrate support
for encoding system information from osmo-bsc to libosmocore.

This change ports osmo-bsc code for encoding SI rest octets.

The conversion was a bit tricky in some places because some
functions receive a 'struct gsm_bts' parameter in osmo-bsc.
In this libosmocore version, such functions expect parameters
which correspond to the individual fields of 'struct gsm_bts'
which are used by these functions.

Several structs from osmo-bsc's system_information.h are now
also declared in libosmocore headers, with an added osmo_ prefix
to avoid collisions with existing definitions in osmo-bsc.

Some helpers were ported from osmo-bsc's system_information.c
to libosmocore's gsm48_rest_octets.c. Contrary to osmo-bsc's
implementation they are now only visible within this file.

Unfortunately, this code ported from osmo-bsc lacks unit tests.

Change-Id: I47888965ab11bba1186c21987f1365c9270abeab
Related: OS#3075
diff --git a/include/Makefile.am b/include/Makefile.am
index 6da5ab6..25a6d75 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -93,6 +93,7 @@
                        osmocom/gsm/gsm48.h \
                        osmocom/gsm/gsm48_arfcn_range_encode.h \
                        osmocom/gsm/gsm48_ie.h \
+                       osmocom/gsm/gsm48_rest_octets.h \
                        osmocom/gsm/gsm_utils.h \
                        osmocom/gsm/gsup.h \
                        osmocom/gsm/gsup_sms.h \
diff --git a/include/osmocom/gprs/protocol/gsm_04_60.h b/include/osmocom/gprs/protocol/gsm_04_60.h
index 5d5fca9..f592b14 100644
--- a/include/osmocom/gprs/protocol/gsm_04_60.h
+++ b/include/osmocom/gprs/protocol/gsm_04_60.h
@@ -312,3 +312,42 @@
 #endif
 } __attribute__ ((packed));
 #endif
+
+/* TS 03.60 Chapter 6.3.3.1: Network Mode of Operation */
+enum osmo_gprs_nmo {
+	GPRS_NMO_I	= 0,	/* CS pagin on GPRS paging or traffic channel */
+	GPRS_NMO_II	= 1,	/* all paging on CCCH */
+	GPRS_NMO_III	= 2,	/* no paging coordination */
+};
+
+/* TS 04.60 12.24 */
+struct osmo_gprs_cell_options {
+	enum osmo_gprs_nmo nmo;
+	/* T3168: wait for packet uplink assignment message */
+	uint32_t t3168;	/* in milliseconds */
+	/* T3192: wait for release of the TBF after reception of the final block */
+	uint32_t t3192;	/* in milliseconds */
+	uint32_t drx_timer_max;/* in seconds */
+	uint32_t bs_cv_max;
+	uint8_t  supports_egprs_11bit_rach;
+	bool ctrl_ack_type_use_block; /* use PACKET CONTROL ACKNOWLEDGMENT */
+
+	uint8_t ext_info_present;
+	struct {
+		uint8_t egprs_supported;
+		uint8_t use_egprs_p_ch_req;
+		uint8_t bep_period;
+		uint8_t pfc_supported;
+		uint8_t dtm_supported;
+		uint8_t bss_paging_coordination;
+	} ext_info;
+};
+
+/* TS 04.60 Table 12.9.2 */
+struct osmo_gprs_power_ctrl_pars {
+	uint8_t alpha;
+	uint8_t t_avg_w;
+	uint8_t t_avg_t;
+	uint8_t pc_meas_chan;
+	uint8_t n_avg_i;
+};
diff --git a/include/osmocom/gsm/gsm48_rest_octets.h b/include/osmocom/gsm/gsm48_rest_octets.h
new file mode 100644
index 0000000..6280b6a
--- /dev/null
+++ b/include/osmocom/gsm/gsm48_rest_octets.h
@@ -0,0 +1,87 @@
+#pragma once
+
+#include <stdbool.h>
+#include <osmocom/gsm/sysinfo.h>
+#include <osmocom/gprs/protocol/gsm_04_60.h>
+
+/* 16 is the max. number of SI2quater messages according to 3GPP TS 44.018 Table 10.5.2.33b.1:
+   4-bit index is used (2#1111 = 10#15) */
+#define SI2Q_MAX_NUM 16
+/* length in bits (for single SI2quater message) */
+#define SI2Q_MAX_LEN 160
+#define SI2Q_MIN_LEN 18
+
+/* generate SI1 rest octets */
+int osmo_gsm48_rest_octets_si1(uint8_t *data, uint8_t *nch_pos, int is1800_net);
+int osmo_gsm48_rest_octets_si2quater(uint8_t *data, uint8_t si2q_index, uint8_t si2q_count, const uint16_t *uarfcn_list,
+				size_t *u_offset, size_t uarfcn_length, uint16_t *scramble_list,
+				struct osmo_earfcn_si2q *si2quater_neigh_list, size_t *e_offset);
+int osmo_gsm48_rest_octets_si2ter(uint8_t *data);
+int osmo_gsm48_rest_octets_si2bis(uint8_t *data);
+int osmo_gsm48_rest_octets_si6(uint8_t *data, bool is1800_net);
+
+struct osmo_gsm48_si_selection_params {
+	uint16_t penalty_time:5,
+		  temp_offs:3,
+		  cell_resel_off:6,
+		  cbq:1,
+		  present:1;
+};
+
+struct osmo_gsm48_si_power_offset {
+	uint8_t power_offset:2,
+		 present:1;
+};
+
+struct osmo_gsm48_si3_gprs_ind {
+	uint8_t si13_position:1,
+		 ra_colour:3,
+		 present:1;
+};
+
+struct osmo_gsm48_lsa_params {
+	uint32_t prio_thr:3,
+		 lsa_offset:3,
+		 mcc:12,
+		 mnc:12;
+	unsigned int present;
+};
+
+struct osmo_gsm48_si_ro_info {
+	struct osmo_gsm48_si_selection_params selection_params;
+	struct osmo_gsm48_si_power_offset power_offset;
+	bool si2ter_indicator;
+	bool early_cm_ctrl;
+	struct {
+		uint8_t where:3,
+			 present:1;
+	} scheduling;
+	struct osmo_gsm48_si3_gprs_ind gprs_ind;
+	/* SI 3 specific */
+	bool early_cm_restrict_3g;
+	bool si2quater_indicator;
+	/* SI 4 specific */
+	struct osmo_gsm48_lsa_params lsa_params;
+	uint16_t cell_id;
+	uint8_t break_ind;	/* do we have SI7 + SI8 ? */
+};
+
+/* Generate SI3 Rest Octests (Chapter 10.5.2.34 / Table 10.4.72) */
+int osmo_gsm48_rest_octets_si3(uint8_t *data, const struct osmo_gsm48_si_ro_info *si3);
+
+/* Generate SI4 Rest Octets (Chapter 10.5.2.35) */
+int osmo_gsm48_rest_octets_si4(uint8_t *data, const struct osmo_gsm48_si_ro_info *si4, int len);
+
+struct osmo_gsm48_si13_info {
+	struct osmo_gprs_cell_options cell_opts;
+	struct osmo_gprs_power_ctrl_pars pwr_ctrl_pars;
+	uint8_t bcch_change_mark;
+	uint8_t si_change_field;
+	uint8_t rac;
+	uint8_t spgc_ccch_sup;
+	uint8_t net_ctrl_ord;
+	uint8_t prio_acc_thr;
+};
+
+/* Generate SI13 Rest Octests (Chapter 10.5.2.37b) */
+int osmo_gsm48_rest_octets_si13(uint8_t *data, const struct osmo_gsm48_si13_info *si13);