library: implement SI3 Rest Octets as per 3GPP TS 44.018

Change-Id: Iaf86f1451a956bf1deef0a9d98fa0513aeb8164b
Signed-off-by: Vadim Yanitskiy <axilirator@gmail.com>
diff --git a/library/GSM_RestOctets.ttcn b/library/GSM_RestOctets.ttcn
new file mode 100644
index 0000000..8f0346d
--- /dev/null
+++ b/library/GSM_RestOctets.ttcn
@@ -0,0 +1,119 @@
+/**
+ * GSM Rest Octets definitions as per 3GPP TS 44.018.
+ *
+ * (C) 2020 Vadim Yanitskiy <axilirator@gmail.com>
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+module GSM_RestOctets {
+
+import from General_Types all;
+import from Osmocom_Types all;
+
+/* 10.5.2.34 SI 3 Rest Octets */
+type record SI3RestOctets {
+	SelectionParamsOpt	sel_params,
+	PowerOffsetOpt		pwr_offset,
+	BIT1			si_2ter_ind,
+	BIT1			early_cm_ind,
+	SchedIfAndWhere		sched_where,
+	GPRSIndicatorOpt	gprs_ind,
+	BIT1			umts_early_cm_ind,
+	SI2quaterIndicatorOpt	si2_quater_ind,
+	BIT1			iu_mode_ind ('1'B) optional,
+	SI21IndicatorOpt	si21_ind optional
+	/* ... spare padding ... */
+} with {
+	variant (si_2ter_ind) "CSN.1 L/H"
+	variant (early_cm_ind) "CSN.1 L/H"
+	variant (umts_early_cm_ind) "CSN.1 L/H"
+
+	/* If Iu mode is not supported in the cell, the Iu Indicator is not sent
+	 * within this cell. Iu Indicator is included if and only if GPRS is
+	 * not supported, and Iu mode is supported in the cell. */
+	variant (iu_mode_ind) "PRESENCE(gprs_ind.presence = '0'B)"
+	/* SI21 field is only present if 'WHERE' information is not present. */
+	variant (si21_ind) "PRESENCE(sched_where.presence = '0'B)"
+};
+
+/* Selection Parameters */
+type record SelectionParams {
+	boolean			cbq,
+	uint6_t			cr_offset,
+	uint3_t			temp_offset,
+	uint5_t			penalty_time
+} with {
+	variant (cbq) "FIELDLENGTH(1)"
+};
+
+/* Optional Selection Parameters: L | H < Selection Parameters > */
+type record SelectionParamsOpt {
+	BIT1			presence, // L/H
+	SelectionParams		params optional
+} with {
+	variant (presence) "CSN.1 L/H"
+	variant (params) "PRESENCE(presence = '1'B)"
+};
+
+/* Optional Power Offset: L | H < Power Offset bit(2) > */
+type record PowerOffsetOpt {
+	BIT1			presence, // L/H
+	uint2_t			offset optional
+} with {
+	variant (presence) "CSN.1 L/H"
+	variant (offset) "PRESENCE(presence = '1'B)"
+};
+
+/* Scheduling if and where: L | H < WHERE bit(3) > */
+type record SchedIfAndWhere {
+	BIT1			presence, // L/H
+	uint3_t			where optional
+} with {
+	variant (presence) "CSN.1 L/H"
+	variant (where) "PRESENCE(presence = '1'B)"
+};
+
+type record GPRSIndicator {
+	uint3_t			ra_colour,
+	BIT1			si13_pos
+} with { variant "" };
+
+/* Optional GPRS Indicator: L | H < GPRS Indicator > */
+type record GPRSIndicatorOpt {
+	BIT1			presence, // L/H
+	GPRSIndicator		ind optional
+} with {
+	variant (presence) "CSN.1 L/H"
+	variant (ind) "PRESENCE(presence = '1'B)"
+};
+
+/* Optional SI2quater Indicator: L | H < SI2quater Indicator > */
+type record SI2quaterIndicatorOpt {
+	BIT1			presence, // L/H
+	BIT1			ind optional
+} with {
+	variant (presence) "CSN.1 L/H"
+	variant (ind) "PRESENCE(presence = '1'B)"
+};
+
+/* Optional SI21 Indicator: L | H < SI21 Position > */
+type record SI21IndicatorOpt {
+	BIT1			presence, // L/H
+	BIT1			pos optional
+} with {
+	variant (presence) "CSN.1 L/H"
+	variant (pos) "PRESENCE(presence = '1'B)"
+};
+
+external function enc_SI3RestOctets(in SI3RestOctets ro) return octetstring
+	with { extension "prototype(convert) encode(RAW)" };
+external function dec_SI3RestOctets(in octetstring stream) return SI3RestOctets
+	with { extension "prototype(convert) decode(RAW)" };
+
+
+} with { encode "RAW"; variant "FIELDORDER(msb)" }