/**
 * GSM Rest Octets definitions as per 3GPP TS 44.018.
 *
 * (C) 2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
 * (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.33b SI 2quater Rest Octets */
type record SI2quaterRestOctets {
	BIT1				ba_ind,
	BIT1				ba_3g_ind,
	BIT1				mp_change_mark,
	uint4_t				si2quater_index,
	uint4_t				si2quater_count,

	/* Measurement Parameters Description */
	MeasParamsDescOpt	meas_params_desc,
	/* GPRS specific parameters */
	record {
		GPRS_RealTimeDiffDescOpt	rt_diff_desc,
		GPRS_BSICDescOpt		bsic_desc,
		GPRS_ReportPrioDescOpt		rep_prio_desc,
		MeasParamsDescOpt		meas_params_desc
	} gprs,
	/* NC Measurement Parameters Description */
	NCMeasParamsOpt			nc_meas_params,
	/* SI2quater Extension Information */
	SI2quaterExtInfoOpt		ext_info,

	/* 3G Neighbour Cell Description */
	UTRAN_NeighDescOpt		utran_neigh_desc,
	/* 3G Measurement Parameters Description */
	UTRAN_MeasParamsDescOpt		utran_meas_params_desc,
	/* GPRS 3G Measurement Parameters Description */
	UTRAN_GPRSMeasParamsDescOpt	utran_gprs_meas_params_desc,

	/* SI2quater Rel-{5,6,7,8,9,10} Additions (Matrioshka!) */
	SI2quaterAdditions		rel_additions
} with {
	/* The TITAN's RAW encoder generates an octet-aligned octetstring,
	 * so we should make sure that unused bits contain proper padding. */
	variant "PADDING(yes), PADDING_PATTERN('00101011'B)"
};
type record of SI2quaterRestOctets SI2quaterRestOctetsList;

/* Measurement Parameters Description */
private type record MeasParamsDescOpt {
	BIT1				presence, // 0/1
	MeasParamsDesc			desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record MeasParamsDesc {
	BIT1				report_type,
	BIT2				serving_band_reporting
} with { variant "" };

/* GPRS Real Time Difference Description (not implemented) */
private type record GPRS_RealTimeDiffDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* GPRS BSIC Description (not implemented) */
private type record GPRS_BSICDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* GPRS Report Priority Description (not implemented) */
private type record GPRS_ReportPrioDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* NC (Network Controlled?) Measurement Parameters */
private type record NCMeasParams {
	/* Network Control Order */
	BIT2				nco,
	BIT1				p_presence,
	NCMeasPeriods			p optional
} with {
	variant (p) "PRESENCE(p_presence = '1'B)"
};

/* NC Measurement Periods */
private type record NCMeasParamsOpt {
	BIT1				presence, // 0/1
	NCMeasParams			params optional
} with {
	variant (params) "PRESENCE(presence = '1'B)"
};

private type record NCMeasPeriods {
	BIT3				non_drx_period,
	BIT3				rep_period_i,
	BIT3				rep_period_t
} with { variant "" };

/* SI2quater Extersion Information */
private type record SI2quaterExtInfoOpt {
	BIT1				presence, // 0/1
	SI2quaterExtInfo		info optional
} with {
	variant (info) "PRESENCE(presence = '1'B)"
};

private type record SI2quaterExtInfo {
	uint8_t				len,
	CCNSupportDescOpt		ccn_supp_desc optional,
	bitstring			padding /* Octet alignment? */
} with {
	variant (len) "LENGTHTO(ccn_supp_desc,padding) + 1"
	variant (len) "UNIT(bits)"
};

/* CCN Support Description */
private type record CCNSupportDescOpt {
	BIT1				presence, // 0/1
	CCNSupportDesc			desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record CCNSupportDesc {
	uint7_t				nr_of_cells,
	bitstring			ccn_supported
} with {
	variant (nr_of_cells) "LENGTHTO(ccn_supported)"
	variant (nr_of_cells) "UNIT(bits)"
};

/* 3G Neighbour Cell Description */
private type record UTRAN_NeighDescOpt {
	BIT1				presence, // 0/1
	UTRAN_NeighDesc			desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record UTRAN_NeighDesc {
	BIT1				idx_start_3g_presence,
	uint7_t				idx_start_3g optional,
	BIT1				abs_idx_start_emr_presence,
	uint7_t				abs_idx_start_emr optional,
	UTRAN_FDDDescOpt		fdd_desc,
	UTRAN_TDDDescOpt		tdd_desc
} with {
	variant (idx_start_3g) "PRESENCE(idx_start_3g_presence = '1'B)"
	variant (abs_idx_start_emr) "PRESENCE(abs_idx_start_emr_presence = '1'B)"
};

/* UTRAN (3G) FDD/TDD Description (not implementaed).
 *
 * TODO: Repeated UTRAN FDD/TDD Neighbour Cells structure contains a variable length
 * field, that needs to be computed using tables (see 9.1.54.1a and 9.1.54.1b) with
 * magic numbers. This cannot be described uing TITAN's RAW codec attributes. */
private type record UTRAN_FDDDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record UTRAN_TDDDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* 3G Measurement Parameters Description (not implemented) */
private type record UTRAN_MeasParamsDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* GPRS 3G Measurement Parameters Description (not implemented) */
private type record UTRAN_GPRSMeasParamsDescOpt {
	BIT1				presence ('0'B), // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* SI2quater Rel-{5,6,7,8,9,10} Additions */
private type record SI2quaterAdditions {
	BIT1				rel5_presence, // L/H
	SI2quaterR5Additions		rel5 optional
} with {
	variant (rel5_presence) "CSN.1 L/H"
	variant (rel5) "PRESENCE(rel5_presence = '1'B)"
};

/* SI2quater Rel-5 and Rel-{6,7,8,9,10} Additions */
private type record SI2quaterR5Additions {
	UMTS_AddMeasParamsDescOpt	umts_add_meas_params_desc,
	UMTS_AddMeasParamsDesc2Opt	umts_add_meas_params_desc2,
	BIT1				rel6_presence, // L/H
	SI2quaterR6Additions		rel6 optional
} with {
	variant (rel6_presence) "CSN.1 L/H"
	variant (rel6) "PRESENCE(rel6_presence = '1'B)"
};

/* 3G Additional Measurement Parameters Description */
private type record UMTS_AddMeasParamsDescOpt {
	BIT1				presence, // 0/1
	UMTS_AddMeasParamsDesc		desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record UMTS_AddMeasParamsDesc {
	BIT3				fdd_qmin_offset,
	BIT4				fdd_rscpmin
} with { variant "" };

/* 3G Additional Measurement Parameters Description 2 */
private type record UMTS_AddMeasParamsDesc2Opt {
	BIT1				presence, // 0/1
	UMTS_AddMeasParamsDesc		desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record UMTS_AddMeasParamsDesc2 {
	/* FDD Reporting Threshold 2 */
	BIT1				presence, // 0/1
	uint6_t				threshold optional
} with {
	variant (threshold) "PRESENCE(presence = '1'B)"
};

/* SI2quater Rel-6 and Rel-{7,8,9,10} Additions */
private type record SI2quaterR6Additions {
	BIT1				umts_ccn_active,
	BIT1				rel7_presence, // L/H
	SI2quaterR7Additions		rel7 optional
} with {
	variant (rel7_presence) "CSN.1 L/H"
	variant (rel7) "PRESENCE(rel7_presence = '1'B)"
};

/* SI2quater Rel-7 and Rel-{8,9,10} Additions */
private type record SI2quaterR7Additions {
	Rel7RepOffseThresholdOpt	rep700,
	Rel7RepOffseThresholdOpt	rep810,
	BIT1				rel8_presence, // L/H
	SI2quaterR8Additions		rel8 optional
} with {
	variant (rel8_presence) "CSN.1 L/H"
	variant (rel8) "PRESENCE(rel8_presence = '1'B)"
};

/* Additions in Rel-7: Reporting Offset & Threshold */
private type record Rel7RepOffseThresholdOpt {
	BIT1				presence, // 0/1
	Rel7RepOffseThreshold		val optional
} with {
	variant (val) "PRESENCE(presence = '1'B)"
};

private type record Rel7RepOffseThreshold {
	BIT3				offset,
	BIT3				threshold
} with { variant "" };

/* SI2quater Rel-8 and Rel-{9,10} Additions */
private type record SI2quaterR8Additions {
	Rel8PrioEUTRANParamsDescOpt	prio_eutran_params_desc,
	Rel8UTRANCSGDescOpt		utran_csg_desc,
	Rel8EUTRANCSGDescOpt		eutran_csg_desc
	// TODO: Rel9 Additions (not implemented)
} with { variant "" };

/* Additions in Rel-8: Priority and E-UTRAN Parameters Description */
private type record Rel8PrioEUTRANParamsDescOpt {
	BIT1				presence, // 0/1
	Rel8PrioEUTRANParamsDesc	desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record Rel8PrioEUTRANParamsDesc {
	ServingCellPrioParamsDescOpt	sc_prio_params_desc,
	UTRAN_PrioParamsDescOpt		utran_prio_params_desc,
	EUTRAN_ParamsDescOpt		eutran_params_desc
} with { variant "" };

/* Serving Cell Priority Parameters Description */
private type record ServingCellPrioParamsDescOpt {
	BIT1				presence, // 0/1
	ServingCellPrioParamsDesc	desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record ServingCellPrioParamsDesc {
	uint3_t				geran_priority,
	uint4_t				thresh_priority_search,
	uint4_t				thresh_gsm_low,
	uint2_t				h_prio,
	uint2_t				t_reselection
} with { variant "" };

/* 3G Priority Parameters Description (not implemented) */
private type record UTRAN_PrioParamsDescOpt {
	BIT1				presence, // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* E-UTRAN Parameters Description */
private type record EUTRAN_ParamsDescOpt {
	BIT1				presence, // 0/1
	EUTRAN_ParamsDesc		desc optional
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

private type record EUTRAN_ParamsDesc {
	BIT1				ccn_active,
	BIT1				e_start,
	BIT1				e_stop,

	/* E-UTRAN Measurement Parameters Description */
	EUTRAN_MeasParamsDescOpt	meas_params_desc,
	/* GPRS E-UTRAN Measurement Parameters Description */
	EUTRAN_GPRSMeasParamsDescOpt	gprs_meas_params_desc,

	/* { 1 < Repeated E-UTRAN Neighbour Cells > } ** 0 */
	EUTRAN_RepeatedNeighbourCells	repeated_neigh_cells optional,
	BIT1				repeated_neigh_cells_term ('0'B),
	/* { 1 < Repeated E-UTRAN Not Allowed Cells > } ** 0 */
	EUTRAN_RepeatedNotAllowedCells	repeated_not_allowed_cells optional,
	BIT1				repeated_not_allowed_cells_term ('0'B),
	/* { 1 < Repeated E-UTRAN PCID to TA mapping > } ** 0 */
	EUTRAN_PCID2TAMaps		pcid2ta_map_list optional,
	BIT1				pcid2ta_map_list_term ('0'B)
} with { variant "" };

/* E-UTRAN Measurement Parameters Description (not implemented) */
private type record EUTRAN_MeasParamsDescOpt {
	BIT1				presence, // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* GPRS E-UTRAN Measurement Parameters Description (not implemented) */
private type record EUTRAN_GPRSMeasParamsDescOpt {
	BIT1				presence, // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* Repeated E-UTRAN Neighbour Cells */
private type record of EUTRAN_NeighbourCells EUTRAN_RepeatedNeighbourCells;
type record EUTRAN_NeighbourCells {
	BIT1				item_ind ('1'B),
	/* { 1 < Repeated E-UTRAN Neighbour Cells > } ** 0 */
	EUTRAN_CellDescs		cell_desc_list optional,
	BIT1				cell_desc_list_term ('0'B),
	BIT1				prio_presence, // 0/1
	uint3_t				prio optional,
	uint5_t				thresh_high,
	BIT1				thresh_low_presence, // 0/1
	uint5_t				thresh_low optional,
	BIT1				qrxlevmin_presence, // 0/1
	uint5_t				qrxlevmin optional
} with {
	variant "PRESENCE(item_ind = '1'B)"
	variant (prio) "PRESENCE(prio_presence = '1'B)"
	variant (thresh_low) "PRESENCE(thresh_low_presence = '1'B)"
	variant (qrxlevmin) "PRESENCE(qrxlevmin_presence = '1'B)"
};

/* Repeated E-UTRAN Cell Description (E-ARFCN & Measurement Bandwidth) List */
type record of EUTRAN_CellDesc EUTRAN_CellDescs;
type record EUTRAN_CellDesc {
	BIT1				item_ind ('1'B),
	uint16_t			e_arfcn,
	BIT1				meas_bw_presence, // 0/1
	uint3_t				meas_bw optional
} with {
	variant "PRESENCE(item_ind = '1'B)"
	variant (meas_bw) "PRESENCE(meas_bw_presence = '1'B)"
};

/* Repeated E-UTRAN Not Allowed Cells */
private type record of EUTRAN_NotAllowedCells EUTRAN_RepeatedNotAllowedCells;
private type record EUTRAN_NotAllowedCells {
	BIT1				item_ind ('1'B),
	/* FIXME: Not Allowed Cells : < PCID Group IE > (not implemented) */
	BIT1				pcid_group_presence ('0'B),
	/* { 1 < Repeated E-UTRAN_FREQUENCY_INDEX > } ** 0 */
	EUTRAN_FreqIndexes		freq_idx_list optional,
	BIT1				freq_idx_list_term ('0'B)
} with {
	variant "PRESENCE(item_ind = '1'B)"
};

/* Repeated E-UTRAN PCID to TA mapping List */
private type record of EUTRAN_PCID2TAMap EUTRAN_PCID2TAMaps;
private type record EUTRAN_PCID2TAMap {
	BIT1				item_ind ('1'B),
	/* FIXME: PCID to TA mapping : < PCID Group IE > (not implemented) */
	BIT1				pcid_group_presence ('0'B),
	/* { 1 < Repeated E-UTRAN_FREQUENCY_INDEX > } ** 0 */
	EUTRAN_FreqIndexes		freq_idx_list optional,
	BIT1				freq_idx_list_term ('0'B)
} with {
	variant "PRESENCE(item_ind = '1'B)"
};

/* Repeated E-UTRAN Frequency Index List */
private type record of EUTRAN_FreqIndex EUTRAN_FreqIndexes;
private type record EUTRAN_FreqIndex {
	BIT1				item_ind ('1'B),
	uint3_t				idx
} with {
	variant "PRESENCE(item_ind = '1'B)"
};

/* Additions in Rel-8: 3G CSG Description (not implemented) */
private type record Rel8UTRANCSGDescOpt {
	BIT1				presence, // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};

/* Additions in Rel-8: E-UTRAN CSG Description (not implemented) */
private type record Rel8EUTRANCSGDescOpt {
	BIT1				presence, // 0/1
	bitstring			desc optional // TODO
} with {
	variant (desc) "PRESENCE(presence = '1'B)"
};


/* Basic any-/omit-matching templates. To be inherited by other templates (see below).
 * The point is that you cannot fit everything into 20 octets, so usually several kinds
 * of SI2quater are being sub-multiplexed by the BTS.  This is achieved using both
 * 'si2quater_index' and 'si2quater_count' fields. */
template SI2quaterRestOctets tra_SI2quaterRestOctets_base := {
	ba_ind := ?,
	ba_3g_ind := ?,
	mp_change_mark := ?,
	si2quater_index := ?,
	si2quater_count := ?,

	meas_params_desc := { presence := ?, desc := * },
	gprs := {
		rt_diff_desc := { presence := ?, desc := * },
		bsic_desc := { presence := ?, desc := * },
		rep_prio_desc := { presence := ?, desc := * },
		meas_params_desc := { presence := ?, desc := * }
	},
	nc_meas_params := { presence := ?, params := * },
	ext_info := { presence := ?, info := * },

	utran_neigh_desc := { presence := ?, desc := * },
	utran_meas_params_desc := { presence := ?, desc := * },
	utran_gprs_meas_params_desc := { presence := ?, desc := * },

	rel_additions := ?
};

template SI2quaterRestOctets tro_SI2quaterRestOctets_base := {
	ba_ind := ?,
	ba_3g_ind := ?,
	mp_change_mark := ?,
	si2quater_index := ?,
	si2quater_count := ?,

	meas_params_desc := { presence := '0'B, desc := omit },
	gprs := {
		rt_diff_desc := { presence := '0'B, desc := omit },
		bsic_desc := { presence := '0'B, desc := omit },
		rep_prio_desc := { presence := '0'B, desc := omit },
		meas_params_desc := { presence := '0'B, desc := omit }
	},
	nc_meas_params := { presence := '0'B, params := omit },
	ext_info := { presence := '0'B, info := omit },

	utran_neigh_desc := { presence := '0'B, desc := omit },
	utran_meas_params_desc := { presence := '0'B, desc := omit },
	utran_gprs_meas_params_desc := { presence := '0'B, desc := omit },

	rel_additions := { rel5_presence := CSN1_L, rel5 := omit }
};

template SI2quaterRestOctets tro_SI2quaterRestOctets_rel8_EUTRAN
modifies tro_SI2quaterRestOctets_base
:= {
	ba_ind := ?,
	ba_3g_ind := ?,
	mp_change_mark := ?,
	si2quater_index := ?,
	si2quater_count := ?,

	rel_additions := {
		rel5_presence := CSN1_H,
		rel5 := {
			umts_add_meas_params_desc := { presence := '0'B, desc := omit },
			umts_add_meas_params_desc2 := { presence := '0'B, desc := omit },
			rel6_presence := CSN1_H,
			rel6 := {
				umts_ccn_active := '0'B,
				rel7_presence := CSN1_H,
				rel7 := {
					rep700 := { presence := '0'B, val := omit },
					rep810 := { presence := '0'B, val := omit },
					rel8_presence := CSN1_H,
					rel8 := {
						prio_eutran_params_desc := {
							presence := '1'B,
							desc := {
								sc_prio_params_desc := { presence := '0'B, desc := omit },
								utran_prio_params_desc := { presence := '0'B, desc := omit },
								eutran_params_desc := {
									presence := '1'B,
									desc := {
										ccn_active := ?,
										e_start := ?,
										e_stop := ?,
										meas_params_desc := { presence := '0'B, desc := omit },
										gprs_meas_params_desc := { presence := '0'B, desc := omit },
										repeated_neigh_cells := *,
										repeated_neigh_cells_term := '0'B,
										repeated_not_allowed_cells := omit,
										repeated_not_allowed_cells_term := '0'B,
										pcid2ta_map_list := omit,
										pcid2ta_map_list_term := '0'B
									}
								}
							}
						},
						utran_csg_desc := { presence := '0'B, desc := omit },
						eutran_csg_desc := { presence := '0'B, desc := omit }
					}
				}
			}
		}
	}
};

template EUTRAN_CellDesc tr_EUTRAN_CellDesc(template (present) uint16_t e_arfcn := ?,
					    template (present) BIT1 meas_bw_presence := ?,
					    template uint3_t meas_bw := *)
:= {
	item_ind := '1'B,
	e_arfcn := e_arfcn,
	meas_bw_presence := meas_bw_presence,
	meas_bw := meas_bw
}

/* Defaults correspond to osmo-bsc cfg:
 *   si2quater neighbor-list add earfcn 111 thresh-hi 20 thresh-lo 10 prio 3 qrxlv 22 meas 3
 */
template EUTRAN_NeighbourCells tr_EUTRAN_NeighbourCells(template (present) EUTRAN_CellDescs cell_desc_list := { tr_EUTRAN_CellDesc },
							template (present) BIT1 prio_presence := ?,
							template uint3_t prio := *,
							template (present) uint5_t thresh_high := ?,
							template (present) BIT1 thresh_low_presence := ?,
							template uint5_t thresh_low := *,
							template (present) BIT1 qrxlevmin_presence := ?,
							template uint5_t qrxlevmin := *)
:= {
	item_ind := '1'B,
	cell_desc_list := cell_desc_list,
	cell_desc_list_term := '0'B,
	prio_presence := prio_presence,
	prio := prio,
	thresh_high := thresh_high,
	thresh_low_presence := thresh_low_presence,
	thresh_low := thresh_low,
	qrxlevmin_presence := qrxlevmin_presence,
	qrxlevmin := qrxlevmin
};

template SI2quaterRestOctets tr_SI2quaterRestOctets_EUTRAN(
	template integer index := 0,
	template integer count := 0,
	template EUTRAN_RepeatedNeighbourCells repeated_neigh_cells := { tr_EUTRAN_NeighbourCells }
) modifies tro_SI2quaterRestOctets_rel8_EUTRAN := {
	si2quater_index := index,
	si2quater_count := count,
	rel_additions := {
		rel5 := {
			rel6 := {
				rel7 := {
					rel8 := {
						prio_eutran_params_desc := {
							desc := {
								sc_prio_params_desc := {
									presence := '1'B,
									desc := {
										geran_priority := 0,
										thresh_priority_search := 0,
										thresh_gsm_low := 0,
										h_prio := 0,
										t_reselection := 0
									}
								},
								eutran_params_desc := {
									desc := {
										ccn_active := '0'B,
										e_start := '1'B,
										e_stop := '1'B,
										repeated_neigh_cells := repeated_neigh_cells
									}
								}
							}
						}
					}
				}
			}
		}
	}
};


/* 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)"

	/* The TITAN's RAW encoder generates an octet-aligned octetstring,
	 * so we should make sure that unused bits contain proper padding. */
	variant "PADDING(yes), PADDING_PATTERN('00101011'B)"
};

/* 10.5.2.35 SI 4 Rest Octets (O & S) */
type record SI4RestOctets {
	SelectionParamsOpt	sel_params,
	PowerOffsetOpt		pwr_offset,
	GPRSIndicatorOpt	gprs_ind,
	BIT1			s_presence, // L/H
	/* TODO: optional "Rest Octets S" part */
	bitstring		s optional
} with {
	variant (s_presence) "CSN.1 L/H"
	variant (s) "PRESENCE(s_presence = '1'B)"

	/* The TITAN's RAW encoder generates an octet-aligned octetstring,
	 * so we should make sure that unused bits contain proper padding. */
	variant "PADDING(yes), PADDING_PATTERN('00101011'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_SI2quaterRestOctets(in SI2quaterRestOctets ro) return octetstring
	with { extension "prototype(convert) encode(RAW)" };
external function dec_SI2quaterRestOctets(in octetstring stream) return SI2quaterRestOctets
	with { extension "prototype(convert) decode(RAW)" };

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)" };

external function enc_SI4RestOctets(in SI4RestOctets ro) return octetstring
	with { extension "prototype(convert) encode(RAW)" };
external function dec_SI4RestOctets(in octetstring stream) return SI4RestOctets
	with { extension "prototype(convert) decode(RAW)" };


/* Basic templates to be extended in place */
template (value) SI3RestOctets ts_SI3RestOctets := {
	sel_params := {
		presence := CSN1_L,
		params := omit
	},
	pwr_offset := {
		presence := CSN1_L,
		offset := omit
	},
	si_2ter_ind := CSN1_L,
	early_cm_ind := CSN1_L,
	sched_where := {
		presence := CSN1_L,
		where := omit
	},
	gprs_ind := {
		presence := CSN1_L,
		ind := omit
	},
	umts_early_cm_ind := CSN1_L,
	si2_quater_ind := {
		presence := CSN1_L,
		ind := omit
	},
	iu_mode_ind := omit,
	si21_ind := {
		presence := CSN1_L,
		pos := omit
	}
}

template (value) SI4RestOctets ts_SI4RestOctets := {
	sel_params := {
		presence := CSN1_L,
		params := omit
	},
	pwr_offset := {
		presence := CSN1_L,
		offset := omit
	},
	gprs_ind := {
		presence := CSN1_L,
		ind := omit
	},
	s_presence := CSN1_L,
	s := omit
}


} with { encode "RAW"; variant "FIELDORDER(msb)" }
