module GSUP_Templates {

/* GSUP_Templates, defining TTCN-3 templates for the GSUP protocol.
 *
 * GSUP is a non-standard protocol used between OsmoMSC/OsmoSGSN and OsmoHLR
 * in order to replace the complex TCAP/MAP protocol.
 *
 * (C) 2017-2019 by Harald Welte <laforge@gnumonks.org>
 * contributions by sysmocom - s.f.m.c. GmbH
 * 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
 */

import from General_Types all;
import from Osmocom_Types all;
import from PCO_Types all;
import from GSUP_Types all;

function f_gsup_postprocess_decoded(inout GSUP_PDU gsup) {
	if (gsup.ies[0].tag == OSMO_GSUP_IMSI_IE) {
		/* if last digit is 'F', then there's an odd number of digits and we must strip the F */
		var integer num_digits := lengthof(gsup.ies[0].val.imsi);
		if (gsup.ies[0].val.imsi[num_digits-1] == 'F'H) {
			gsup.ies[0].val.imsi := substr(gsup.ies[0].val.imsi, 0, num_digits-1);
		}
	}
}

function f_gsup_preprocess_encoded(inout GSUP_PDU gsup) {
	if (ischosen(gsup.ies[0].val.imsi)) {
		/* if number of digits is odd, add a 'F' as padding at the end */
		var integer num_digits := lengthof(gsup.ies[0].val.imsi);
		if (num_digits rem 2 == 1) {
			gsup.ies[0].val.imsi := gsup.ies[0].val.imsi & 'F'H;
		}
	}
}

template (value) GSUP_MSISDN ts_GSUP_MSISDN(template (value) hexstring digits,
					    template (value) BIT3 ton := '000'B,
					    template (value) BIT4 npi := '0000'B) := {
	len := 0, /* overwritten */
	/* numberingPlanIdentification := npi,
	typeOfNumber := ton,
	ext1 := '0'B, */
	digits := digits
}

template (present) GSUP_MSISDN tr_GSUP_MSISDN(template (present) hexstring digits,
					      template (present) BIT3 ton := ?,
					      template (present) BIT4 npi := ?) := {
	len := ?,
	/* numberingPlanIdentification := npi,
	typeOfNumber := ton,
	ext1 := '0'B, */
	digits := digits
}

template (value) GSUP_PDP_Address ts_GSUP_PDP_Address_IPv4(template (omit) OCT4 ip_addr) := {
	ipv4 := {
		spare := '1111'B,
		pdp_typeorg := '0001'B,
		pdp_typenum := '21'O,
		ipv4_address := ip_addr
	}
}
template (value) GSUP_PDP_Address ts_EuaIPv4Dyn := ts_GSUP_PDP_Address_IPv4(omit);

template (present) GSUP_PDP_Address tr_GSUP_PDP_Address_IPv4(template OCT4 ip_addr) := {
	ipv4 := {
		spare := ?,
		pdp_typeorg := '0001'B,
		pdp_typenum := '21'O,
		ipv4_address := ip_addr
	}
}

template (value) GSUP_IE
ts_GSUP_IE_AuthTuple2G(template (value) octetstring rand,
		       template (value) octetstring sres,
		       template (value) octetstring kc) := {
	tag := OSMO_GSUP_AUTH_TUPLE_IE,
	len := 0, /* overwritten */
	val := {
		auth_tuple := {
			ts_GSUP_IE_RAND(rand),
			ts_GSUP_IE_SRES(sres),
			ts_GSUP_IE_Kc(kc)
		}
	}
}

template (present) GSUP_IE
tr_GSUP_IE_AuthTuple3G(template (present) octetstring rand := ?,
		       template (present) octetstring ik := ?,
		       template (present) octetstring ck := ?,
		       template (present) octetstring autn := ?,
		       template (present) octetstring res := ?) := {
	tag := OSMO_GSUP_AUTH_TUPLE_IE,
	len := ?,
	val := {
		auth_tuple := {
			tr_GSUP_IE_RAND(rand),
			tr_GSUP_IE_IK(ik),
			tr_GSUP_IE_CK(ck),
			tr_GSUP_IE_AUTN(autn),
			tr_GSUP_IE_RES(res)
		}
	}
}

template (value) GSUP_IE
ts_GSUP_IE_AuthTuple3G(template (value) octetstring rand,
		       template (value) octetstring ik,
		       template (value) octetstring ck,
		       template (value) octetstring autn,
		       template (value) octetstring res) := {
	tag := OSMO_GSUP_AUTH_TUPLE_IE,
	len := 0, /* overwritten */
	val := {
		auth_tuple := {
			ts_GSUP_IE_RAND(rand),
			ts_GSUP_IE_IK(ik),
			ts_GSUP_IE_CK(ck),
			ts_GSUP_IE_AUTN(autn),
			ts_GSUP_IE_RES(res)
		}
	}
}

template (present) GSUP_IE
tr_GSUP_IE_AuthTuple2G3G(template (present) octetstring rand := ?,
			 template (present) octetstring sres := ?,
			 template (present) octetstring kc := ?,
			 template (present) octetstring ik := ?,
			 template (present) octetstring ck := ?,
			 template (present) octetstring autn := ?,
			 template (present) octetstring res := ?) := {
	tag := OSMO_GSUP_AUTH_TUPLE_IE,
	len := ?,
	val := {
		auth_tuple := {
			tr_GSUP_IE_RAND(rand),
			tr_GSUP_IE_SRES(sres),
			tr_GSUP_IE_Kc(kc),
			tr_GSUP_IE_IK(ik),
			tr_GSUP_IE_CK(ck),
			tr_GSUP_IE_AUTN(autn),
			tr_GSUP_IE_RES(res)
		}
	}
}

template (value) GSUP_IE
ts_GSUP_IE_AuthTuple2G3G(template (value)octetstring rand,
			 template (value) octetstring sres,
			 template (value) octetstring kc,
			 template (value) octetstring ik,
			 template (value) octetstring ck,
			 template (value) octetstring autn,
			 template (value) octetstring res) := {
	tag := OSMO_GSUP_AUTH_TUPLE_IE,
	len := 0, /* overwritten */
	val := {
		auth_tuple := {
			ts_GSUP_IE_RAND(rand),
			ts_GSUP_IE_SRES(sres),
			ts_GSUP_IE_Kc(kc),
			ts_GSUP_IE_IK(ik),
			ts_GSUP_IE_CK(ck),
			ts_GSUP_IE_AUTN(autn),
			ts_GSUP_IE_RES(res)
		}
	}
}

template (value) GSUP_IE ts_GSUP_IE_PdpInfoCompl := {
	tag := OSMO_GSUP_PDP_INFO_COMPL_IE,
	len := 0, /* overwritten */
	val := {
		pdp_info_compl := ''O
	}
}

template (present) GSUP_IE tr_GSUP_IE_PdpInfoCompl := {
	tag := OSMO_GSUP_PDP_INFO_COMPL_IE,
	len := 0, /* overwritten */
	val := {
		pdp_info_compl := ''O
	}
}

template (value) GSUP_IE ts_GSUP_IE_PdpInfo(template (value) OCT1 ctx_id,
					    template (value) octetstring apn,
					    template (value) GSUP_PDP_Address pdp_address,
					    template (value) octetstring pdp_qos) := {
	tag := OSMO_GSUP_PDP_INFO_IE,
	len := 0, /* overwritten */
	val := {
		pdp_info := {
			ts_GSUP_IE_PDP_CONTEXT_ID(ctx_id),
			ts_GSUP_IE_PDP_ADDRESS(pdp_address),
			ts_GSUP_IE_APN(apn),
			ts_GSUP_IE_PDP_QOS(pdp_qos)
		}
	}
}
template (value) GSUP_IE ts_GSUP_IE_PdpInfo_ie(template (value) GSUP_IEs pdp_info) := {
	tag := OSMO_GSUP_PDP_INFO_IE,
	len := 0, /* overwritten */
	val := {
		pdp_info := pdp_info
	}
}

template (present) GSUP_IE tr_GSUP_IE_PdpInfo(template (present) OCT1 ctx_id,
					      template (present) octetstring apn,
					      template (present) GSUP_PDP_Address pdp_address) := {
	tag := OSMO_GSUP_PDP_INFO_IE,
	len := ?,
	val := {
		pdp_info := {
			tr_GSUP_IE_PDP_CONTEXT_ID(ctx_id),
			tr_GSUP_IE_PDP_ADDRESS(pdp_address),
			tr_GSUP_IE_APN(apn)
		}
	}
}
template (present) GSUP_IE tr_GSUP_IE_PdpInfo_ie(template (present) GSUP_IEs pdp_info := ?) := {
	tag := OSMO_GSUP_PDP_INFO_IE,
	len := ?,
	val := {
		pdp_info := pdp_info
	}
}

template (value) GSUP_IE ts_GSUP_IE_PDP_CONTEXT_ID(template (value) OCT1 ctx_id) := {
	tag := OSMO_GSUP_PDP_CONTEXT_ID_IE,
	len := 0,
	val := {
		pdp_ctx_id := ctx_id
	}
}

template (present) GSUP_IE tr_GSUP_IE_PDP_CONTEXT_ID(template (present) OCT1 ctx_id) := {
	tag := OSMO_GSUP_PDP_CONTEXT_ID_IE,
	len := ?,
	val := {
		pdp_ctx_id := ctx_id
	}
}

template (value) GSUP_IE ts_GSUP_IE_PDP_ADDRESS(template (value) GSUP_PDP_Address pdp_address) := {
	tag := OSMO_GSUP_PDP_ADDRESS_IE,
	len := 0,
	val := {
		pdp_address := pdp_address
	}
}

template (present) GSUP_IE tr_GSUP_IE_PDP_ADDRESS(template (present) GSUP_PDP_Address pdp_address := ?) := {
	tag := OSMO_GSUP_PDP_ADDRESS_IE,
	len := ?,
	val := {
		pdp_address := pdp_address
	}
}

template (value) GSUP_IE ts_GSUP_IE_PDP_QOS(template (value) octetstring pdp_qos) := {
	tag := OSMO_GSUP_PDP_QOS_IE,
	len := 0,
	val := {
		pdp_qos := pdp_qos
	}
}

template (present) GSUP_IE tr_GSUP_IE_PDP_QOS(template (present) octetstring pdp_qos := ?) := {
	tag := OSMO_GSUP_PDP_QOS_IE,
	len := ?,
	val := {
		pdp_qos := pdp_qos
	}
}

template (value) GSUP_IE ts_GSUP_IE_Charging_Characteristics(template (value) octetstring charg_char) := {
	tag := OSMO_GSUP_CHARG_CHAR_IE,
	len := 0,
	val := {
		charg_char := charg_char
	}
}

template (present) GSUP_IE tr_GSUP_IE_Charging_Characteristics(template (present) octetstring charg_char := ?) := {
	tag := OSMO_GSUP_CHARG_CHAR_IE,
	len := ?,
	val := {
		charg_char := charg_char
	}
}

template (value) GSUP_IE ts_GSUP_IE_PCO(template (value) PCO_DATA pco) := {
	tag := OSMO_GSUP_PCO_IE,
	len := 0,
	val := {
		pco := pco
	}
}

template (present) GSUP_IE tr_GSUP_IE_PCO(template (present) PCO_DATA pco := ?) := {
	tag := OSMO_GSUP_PCO_IE,
	len := ?,
	val := {
		pco := pco
	}
}

template (value) GSUP_IMEI ts_GSUP_IMEI(template (value) hexstring digits) := {
	len := 0, /* overwritten */
	digits := digits
}

template (present) GSUP_IMEI tr_GSUP_IMEI(template (present) hexstring digits) := {
	len := ?,
	digits := digits
}


template (value) GSUP_PDU ts_GSUP(template (value) GSUP_MessageType msgt,
				  template (value) GSUP_IEs ies := {}) := {
	msg_type := msgt,
	ies := ies
}
template (present) GSUP_PDU tr_GSUP(template (present) GSUP_MessageType msgt := ?,
				    template (present) GSUP_IEs ies := ?) := {
	msg_type := msgt,
	ies := ies
}
template (present) GSUP_PDU tr_GSUP_IMSI(template (present) GSUP_MessageType msgt := ?,
					 template (present) hexstring imsi := ?) := {
	msg_type := msgt,
	ies := { tr_GSUP_IE_IMSI(imsi), * }
}

template (value) GSUP_PDU ts_GSUP_SAI_REQ(hexstring imsi) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, { valueof(ts_GSUP_IE_IMSI(imsi)) });

template (value) GSUP_PDU ts_GSUP_SAI_REQ_EPS(hexstring imsi) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, {
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_CURRENT_RAT_TYPE(RAT_TYPE_EUTRAN_SGs))
	});

template (value) GSUP_PDU ts_GSUP_SAI_REQ_NUM_AUTH(hexstring imsi, OCT1 num_auth_vectors) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, {
			valueof(ts_GSUP_IE_IMSI(imsi)),
			valueof(ts_GSUP_IE_NUM_VECTORS_REQ(num_auth_vectors))
			});

template (value) GSUP_PDU ts_GSUP_SAI_REQ_PDP_INFO(hexstring imsi, template (value) GSUP_IEs pdp_info) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, {
			valueof(ts_GSUP_IE_IMSI(imsi)),
			valueof(ts_GSUP_IE_PdpInfo_ie(pdp_info))
			});

template GSUP_PDU ts_GSUP_SAI_REQ_PDP_INFO_UMTS_AKA_RESYNC(
		template (value) hexstring imsi,
		template (value) GSUP_IEs pdp_info,
		template (value) octetstring auts,
		template (value) octetstring rand) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, {
			valueof(ts_GSUP_IE_IMSI(imsi)),
			valueof(ts_GSUP_IE_PdpInfo_ie(pdp_info)),
			valueof(ts_GSUP_IE_AUTS(auts)),
			valueof(ts_GSUP_IE_RAND(rand))
			});

template GSUP_PDU tr_GSUP_SAI_REQ(template (present) hexstring imsi) :=
	tr_GSUP_IMSI(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, imsi);

template GSUP_PDU tr_GSUP_SAI_REQ_UMTS_AKA_RESYNC(
		template hexstring imsi,
		template octetstring auts,
		template octetstring rand) :=
	tr_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_REQUEST, {
			tr_GSUP_IE_IMSI(imsi),
			tr_GSUP_IE_AUTS(auts),
			tr_GSUP_IE_RAND(rand),
			*
			});

template (value) GSUP_PDU ts_GSUP_SAI_RES(hexstring imsi, GSUP_IE auth_tuple) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT, {
		valueof(ts_GSUP_IE_IMSI(imsi)), auth_tuple });

template GSUP_PDU tr_GSUP_SAI_ERR(template hexstring imsi, template (present) integer cause := ?) :=
	tr_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR, {
			tr_GSUP_IE_IMSI(imsi), tr_GSUP_IE_Cause(cause), *});

template (value) GSUP_PDU ts_GSUP_SAI_ERR(hexstring imsi, integer cause) :=
	ts_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_ERROR, {
			valueof(ts_GSUP_IE_IMSI(imsi)), valueof(ts_GSUP_IE_Cause(cause)) });


template GSUP_PDU tr_GSUP_SAI_RES(template (present) hexstring imsi,
				  template (present) GSUP_IE auth_tuple_ie := tr_GSUP_IE(OSMO_GSUP_AUTH_TUPLE_IE)) :=
	tr_GSUP(OSMO_GSUP_MSGT_SEND_AUTH_INFO_RESULT, {
			tr_GSUP_IE_IMSI(imsi), *, auth_tuple_ie, * });

template (value) GSUP_PDU ts_GSUP_UL_REQ(hexstring imsi, GSUP_CnDomain dom := OSMO_GSUP_CN_DOMAIN_PS,
					 template (omit) octetstring source_name := omit) :=
	ts_GSUP(OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST, f_gen_ts_ies(imsi, dom := dom,
								     source_name := source_name));

template GSUP_PDU tr_GSUP_UL_REQ(template hexstring imsi) :=
	tr_GSUP_IMSI(OSMO_GSUP_MSGT_UPDATE_LOCATION_REQUEST, imsi);

template (value) GSUP_PDU ts_GSUP_UL_RES(hexstring imsi, template (omit) octetstring destination_name := omit) :=
	ts_GSUP(OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT,
			f_gen_ts_ies(imsi, destination_name := destination_name));

template GSUP_PDU tr_GSUP_UL_RES(template hexstring imsi, template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_UPDATE_LOCATION_RESULT, f_gen_tr_ies(imsi, destination_name := destination_name));

template (value) GSUP_PDU ts_GSUP_UL_ERR(hexstring imsi, integer cause) :=
	ts_GSUP(OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR, {
			valueof(ts_GSUP_IE_IMSI(imsi)), valueof(ts_GSUP_IE_Cause(cause)) });

template GSUP_PDU tr_GSUP_UL_ERR(template hexstring imsi, template integer cause := ?,
		template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_UPDATE_LOCATION_ERROR,
		f_gen_tr_ies(imsi, cause := cause, destination_name := destination_name));

template (value) GSUP_PDU ts_GSUP_ISD_REQ(hexstring imsi, hexstring msisdn, template (omit) octetstring destination_name := omit) :=
	ts_GSUP(OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
			f_gen_ts_ies(imsi, destination_name := destination_name) & {
				valueof(ts_GSUP_IE_MSISDN(msisdn)) });

template GSUP_PDU tr_GSUP_ISD_REQ(template hexstring imsi, template hexstring msisdn := ?,
		template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_INSERT_DATA_REQUEST,
		f_gen_tr_ies(imsi, msisdn := msisdn, destination_name := destination_name));

template (value) GSUP_PDU ts_GSUP_ISD_RES(hexstring imsi,
					  template (omit) octetstring source_name := omit,
					  template (omit) octetstring destination_name := omit) :=
	ts_GSUP(OSMO_GSUP_MSGT_INSERT_DATA_RESULT,
		f_gen_ts_ies(imsi, source_name := source_name,
			     destination_name := destination_name));

template GSUP_PDU tr_GSUP_ISD_RES(template hexstring imsi) :=
	tr_GSUP_IMSI(OSMO_GSUP_MSGT_INSERT_DATA_RESULT, imsi);

template GSUP_PDU tr_GSUP_AUTH_FAIL_IND(hexstring imsi) :=
	tr_GSUP_IMSI(OSMO_GSUP_MSGT_AUTH_FAIL_REPORT, imsi);

template (present) GSUP_PDU tr_GSUP_CL_REQ(template (present) hexstring imsi := ?,
					   template GSUP_CnDomain dom := omit,
					   template GSUP_CancelType ctype := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST,
		f_gen_tr_ies(imsi,
			     cancel_type := ctype,
			     cn_domain := dom));

template (value) GSUP_PDU ts_GSUP_CL_REQ(hexstring imsi, GSUP_CancelType ctype) :=
	ts_GSUP(OSMO_GSUP_MSGT_LOCATION_CANCEL_REQUEST, {
		valueof(ts_GSUP_IE_IMSI(imsi)), valueof(ts_GSUP_IE_CancelType(ctype)) });

template GSUP_PDU tr_GSUP_CL_RES(template hexstring imsi) :=
	tr_GSUP_IMSI(OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT, imsi);

template (value) GSUP_PDU ts_GSUP_CL_RES(template (value) hexstring imsi) :=
	ts_GSUP(OSMO_GSUP_MSGT_LOCATION_CANCEL_RESULT, {valueof(ts_GSUP_IE_IMSI(imsi))});

template GSUP_PDU tr_GSUP_CL_ERR(template hexstring imsi, template integer cause := ?) :=
	tr_GSUP(OSMO_GSUP_MSGT_LOCATION_CANCEL_ERROR, {
			tr_GSUP_IE_IMSI(imsi), tr_GSUP_IE_Cause(cause), * });

template (value) GSUP_PDU ts_GSUP_PURGE_MS_REQ(hexstring imsi, GSUP_CnDomain dom) :=
	ts_GSUP(OSMO_GSUP_MSGT_PURGE_MS_REQUEST, {
			valueof(ts_GSUP_IE_IMSI(imsi)), valueof(ts_GSUP_IE_CnDomain(dom)) });

template GSUP_PDU tr_GSUP_PURGE_MS_REQ(template hexstring imsi, template GSUP_CnDomain dom := ?) :=
	tr_GSUP(OSMO_GSUP_MSGT_PURGE_MS_REQUEST, {
			tr_GSUP_IE_IMSI(imsi), *, tr_GSUP_IE_CnDomain(dom), * });

template (value) GSUP_PDU ts_GSUP_PURGE_MS_RES(hexstring imsi) :=
	ts_GSUP(OSMO_GSUP_MSGT_PURGE_MS_RESULT, {
		valueof(ts_GSUP_IE_IMSI(imsi)) });

template GSUP_PDU tr_GSUP_PURGE_MS_RES(template hexstring imsi) :=
	tr_GSUP(OSMO_GSUP_MSGT_PURGE_MS_RESULT, {
		tr_GSUP_IE_IMSI(imsi), * });

template GSUP_PDU tr_GSUP_PURGE_MS_ERR(template hexstring imsi, template integer cause) :=
	tr_GSUP(OSMO_GSUP_MSGT_PURGE_MS_ERROR, {
		tr_GSUP_IE_IMSI(imsi), tr_GSUP_IE_Cause(cause) });

template (value) GSUP_PDU ts_GSUP_CHECK_IMEI_REQ(hexstring imsi, hexstring imei,
						 template (omit) octetstring source_name := omit) :=
	ts_GSUP(OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST, f_gen_ts_ies(imsi, imei := imei, source_name := source_name));

template GSUP_PDU tr_GSUP_CHECK_IMEI_REQ(
	template hexstring imsi,
	template hexstring imei
) := tr_GSUP(
	OSMO_GSUP_MSGT_CHECK_IMEI_REQUEST,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_IMEI(imei),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SUBSCRIBER_MANAGEMENT)
	}
);

template (value) GSUP_PDU ts_GSUP_CHECK_IMEI_RES(hexstring imsi, GSUP_IMEIResult result) :=
	ts_GSUP(OSMO_GSUP_MSGT_CHECK_IMEI_RESULT, {
		valueof(ts_GSUP_IE_IMSI(imsi)), valueof(ts_GSUP_IE_IMEI_Result(result)) });

template GSUP_PDU tr_GSUP_CHECK_IMEI_RES(template hexstring imsi, template GSUP_IMEIResult result,
					 template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_CHECK_IMEI_RESULT,
		f_gen_tr_ies(imsi, imei_result := result, destination_name := destination_name));

template (value) GSUP_PDU ts_GSUP_CHECK_IMEI_ERR(hexstring imsi, integer cause) :=
	ts_GSUP(OSMO_GSUP_MSGT_CHECK_IMEI_ERROR, {
		valueof(ts_GSUP_IE_IMSI(imsi)), valueof(ts_GSUP_IE_Cause(cause)) });

template GSUP_PDU tr_GSUP_CHECK_IMEI_ERR(template hexstring imsi, template integer cause,
					 template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_CHECK_IMEI_ERROR, f_gen_tr_ies(imsi, cause := cause, destination_name := destination_name));


/* EPDG Tunnel */
template (value) GSUP_PDU ts_GSUP_EPDGTunnel_REQ(hexstring imsi,
						 template (value) PCO_DATA pco,
						 GSUP_Message_Class message_class := OSMO_GSUP_MESSAGE_CLASS_IPSEC_EPDG,
						 GSUP_CnDomain dom := OSMO_GSUP_CN_DOMAIN_PS,
						 template (omit) octetstring source_name := omit) :=
	ts_GSUP(OSMO_GSUP_MSGT_EPDG_TUNNEL_REQUEST, f_gen_ts_ies(imsi,
								 message_class := message_class,
								 pco := pco,
								 dom := dom,
								 source_name := source_name));

template (present) GSUP_PDU tr_GSUP_EPDGTunnel_REQ(template (present) hexstring imsi := ?,
						   template (present) GSUP_Message_Class message_class := OSMO_GSUP_MESSAGE_CLASS_IPSEC_EPDG) :=
	tr_GSUP(OSMO_GSUP_MSGT_EPDG_TUNNEL_REQUEST,
		f_gen_tr_ies(imsi,
			     message_class := message_class));


template (value) GSUP_PDU ts_GSUP_EPDGTunnel_RES(hexstring imsi,
						template (value) GSUP_IEs pdp_info,
						GSUP_Message_Class message_class := OSMO_GSUP_MESSAGE_CLASS_IPSEC_EPDG,
						octetstring destination_name := ''O) :=
	ts_GSUP(OSMO_GSUP_MSGT_EPDG_TUNNEL_RESULT, {
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_PdpInfoCompl),
		valueof(ts_GSUP_IE_PdpInfo_ie(pdp_info)),
		valueof(ts_GSUP_IE_Message_Class(message_class)),
		valueof(ts_GSUP_IE_Destination_Name(destination_name))
		});

template (present) GSUP_PDU tr_GSUP_EPDGTunnel_RES(template (present) hexstring imsi,
						   template (present) GSUP_IEs pdp_info,
						   template (present) PCO_DATA pco := ?,
						   template (present) GSUP_Message_Class message_class := OSMO_GSUP_MESSAGE_CLASS_IPSEC_EPDG,
						   template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_EPDG_TUNNEL_RESULT,
		f_gen_tr_ies(imsi,
			     pdp_info_compl := true,
			     pdp_info := pdp_info,
			     message_class := message_class,
			     pco := pco,
			     destination_name := destination_name));

template (value) GSUP_PDU ts_GSUP_EPDGTunnel_ERR(hexstring imsi,
						 GSUP_Message_Class message_class := OSMO_GSUP_MESSAGE_CLASS_IPSEC_EPDG,
						 integer cause := 0) :=
	ts_GSUP(OSMO_GSUP_MSGT_EPDG_TUNNEL_ERROR, {
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_Cause(cause)),
		valueof(ts_GSUP_IE_Message_Class(message_class))
		});

template (present) GSUP_PDU tr_GSUP_EPDGTunnel_ERR(template (present) hexstring imsi,
						   template (present) GSUP_Message_Class message_class := OSMO_GSUP_MESSAGE_CLASS_IPSEC_EPDG,
						   template (present) integer cause := ?,
						   template octetstring destination_name := omit) :=
	tr_GSUP(OSMO_GSUP_MSGT_EPDG_TUNNEL_ERROR,
		f_gen_tr_ies(imsi,
			     message_class := message_class,
			     cause := cause,
			     destination_name := destination_name));


template (value) GSUP_IE ts_GSUP_IE_CancelType(template (value) GSUP_CancelType ctype) := {
	tag := OSMO_GSUP_CANCEL_TYPE_IE,
	len := 0, /* overwritten */
	val := {
		cancel_type := ctype
	}
}

template (present) GSUP_IE tr_GSUP_IE_CancelType(template (present) GSUP_CancelType ctype) :=
	tr_GSUP_IE(OSMO_GSUP_CANCEL_TYPE_IE, GSUP_IeValue:{cancel_type:=ctype});

template (present) GSUP_IE tr_GSUP_IE_CnDomain(template (present) GSUP_CnDomain domain) :=
	tr_GSUP_IE(OSMO_GSUP_CN_DOMAIN_IE, GSUP_IeValue:{cn_domain:=domain});

template (present) GSUP_IE tr_GSUP_IE(template (present) GSUP_IEI iei,
				      template (present) GSUP_IeValue val := ?) := {
	tag := iei,
	len := ?,
	val := val
}

template (value) GSUP_IE ts_GSUP_IE_IMSI(template (value) hexstring imsi) := {
	tag := OSMO_GSUP_IMSI_IE,
	len := 0, /* overwritten */
	val := {
		imsi := imsi
	}
}

template (present) GSUP_IE tr_GSUP_IE_IMSI(template (present) hexstring imsi := ?) := {
	tag := OSMO_GSUP_IMSI_IE,
	len := ?,
	val := {
		imsi := imsi
	}
}

template (value) GSUP_IE ts_GSUP_IE_MSISDN(template (value) hexstring msisdn) := {
	tag := OSMO_GSUP_MSISDN_IE,
	len := 0, /* overwritten */
	val := {
		msisdn := ts_GSUP_MSISDN(msisdn)
	}
}

template (present) GSUP_IE tr_GSUP_IE_MSISDN(template (present) hexstring msisdn) := {
	tag := OSMO_GSUP_MSISDN_IE,
	len := ?,
	val := {
		msisdn := tr_GSUP_MSISDN(msisdn)
	}
}


template (value) GSUP_IE ts_GSUP_IE_Cause(template (value) integer cause) := {
	tag := OSMO_GSUP_CAUSE_IE,
	len := 0, /* overwritten */
	val := {
		cause := cause
	}
}

template (present) GSUP_IE tr_GSUP_IE_Cause(template (present) integer cause) := {
	tag := OSMO_GSUP_CAUSE_IE,
	len := ?,
	val := {
		cause := cause
	}
}

template (value) GSUP_IE ts_GSUP_IE_AUTS(template (value) octetstring auts) := {
	tag := OSMO_GSUP_AUTS_IE,
	len := 0, /* overwritten */
	val := {
		auts := auts
	}
}

template (present) GSUP_IE tr_GSUP_IE_AUTS(template (present) octetstring auts) := {
	tag := OSMO_GSUP_AUTS_IE,
	len := ?,
	val := {
		auts := auts
	}
}

template (value) GSUP_IE ts_GSUP_IE_RAND(template (value) octetstring rand) := {
	tag := OSMO_GSUP_RAND_IE,
	len := 0, /* overwritten */
	val := {
		rand := rand
	}
}

template (present) GSUP_IE tr_GSUP_IE_RAND(template (present) octetstring rand := ?) := {
	tag := OSMO_GSUP_RAND_IE,
	len := ?,
	val := {
		rand := rand
	}
}

template (present) GSUP_IE tr_GSUP_IE_SRES(template (present) octetstring sres := ?) := {
	tag := OSMO_GSUP_SRES_IE,
	len := ?,
	val := {
		sres := sres
	}
}

template (value) GSUP_IE ts_GSUP_IE_SRES(template (value) octetstring sres) := {
	tag := OSMO_GSUP_SRES_IE,
	len := 0, /* overwritten */
	val := {
		sres := sres
	}
}

template (present) GSUP_IE tr_GSUP_IE_Kc(template (present) octetstring kc := ?) := {
	tag := OSMO_GSUP_KC_IE,
	len := ?,
	val := {
		kc := kc
	}
}

template (value) GSUP_IE ts_GSUP_IE_Kc(template (value) octetstring kc) := {
	tag := OSMO_GSUP_KC_IE,
	len := 0, /* overwritten */
	val := {
		kc := kc
	}
}

template (present) GSUP_IE tr_GSUP_IE_IK(template (present) octetstring ik := ?) := {
	tag := OSMO_GSUP_IK_IE,
	len := ?,
	val := {
		ik := ik
	}
}

template (value) GSUP_IE ts_GSUP_IE_IK(template (value) octetstring ik) := {
	tag := OSMO_GSUP_IK_IE,
	len := 0, /* overwritten */
	val := {
		ik := ik
	}
}

template (present) GSUP_IE tr_GSUP_IE_CK(template (present) octetstring ck := ?) := {
	tag := OSMO_GSUP_CK_IE,
	len := ?,
	val := {
		ck := ck
	}
}

template (value) GSUP_IE ts_GSUP_IE_CK(template (value) octetstring ck) := {
	tag := OSMO_GSUP_CK_IE,
	len := 0, /* overwritten */
	val := {
		ck := ck
	}
}

template (present) GSUP_IE tr_GSUP_IE_AUTN(template (present) octetstring autn := ?) := {
	tag := OSMO_GSUP_AUTN_IE,
	len := ?,
	val := {
		autn := autn
	}
}

template (value) GSUP_IE ts_GSUP_IE_AUTN(template (value) octetstring autn) := {
	tag := OSMO_GSUP_AUTN_IE,
	len := 0, /* overwritten */
	val := {
		autn := autn
	}
}

template (present) GSUP_IE tr_GSUP_IE_RES(template (present) octetstring res := ?) := {
	tag := OSMO_GSUP_RES_IE,
	len := ?,
	val := {
		res := res
	}
}

template (value) GSUP_IE ts_GSUP_IE_RES(template (value) octetstring res) := {
	tag := OSMO_GSUP_RES_IE,
	len := 0, /* overwritten */
	val := {
		res := res
	}
}

template (value) GSUP_IE ts_GSUP_IE_APN(template (value) octetstring apn) := {
	tag := OSMO_GSUP_ACCESS_POINT_NAME_IE,
	len := 0, /* overwritten */
	val := {
		apn := apn
	}
}

template (present) GSUP_IE tr_GSUP_IE_APN(template (present) octetstring apn) := {
	tag := OSMO_GSUP_ACCESS_POINT_NAME_IE,
	len := ?,
	val := {
		apn := apn
	}
}

template (value) GSUP_IE ts_GSUP_IE_CnDomain(template (value) GSUP_CnDomain dom) := {
	tag := OSMO_GSUP_CN_DOMAIN_IE,
	len := 0, /* overwritten */
	val := {
		cn_domain := dom
	}
}

template (value) GSUP_IE ts_GSUP_IE_SessionId(template (value) OCT4 sid) := {
	tag := OSMO_GSUP_SESSION_ID_IE,
	len := 0, /* overwritten */
	val := {
		session_id := sid
	}
}
template (present) GSUP_IE tr_GSUP_IE_SessionId(template (present) OCT4 sid) := {
	tag := OSMO_GSUP_SESSION_ID_IE,
	len := ?,
	val := {
		session_id := sid
	}
}

template (value) GSUP_IE ts_GSUP_IE_SessionState(template (value) GSUP_SessionState state) := {
	tag := OSMO_GSUP_SESSION_STATE_IE,
	len := 0, /* overwritten */
	val := {
		session_state := state
	}
}
template (present) GSUP_IE tr_GSUP_IE_SessionState(template (present) GSUP_SessionState state) := {
	tag := OSMO_GSUP_SESSION_STATE_IE,
	len := ?,
	val := {
		session_state := state
	}
}

template (value) GSUP_IE ts_GSUP_IE_SM_RP_MR(template (value) OCT1 ref) := {
	tag := OSMO_GSUP_SM_RP_MR_IE,
	len := 0, /* overwritten */
	val := {
		sm_rp_mr := ref
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_RP_MR(template (present) OCT1 ref) := {
	tag := OSMO_GSUP_SM_RP_MR_IE,
	len := ?,
	val := {
		sm_rp_mr := ref
	}
}

template (value) GSUP_IE ts_GSUP_IE_SM_RP_CAUSE(template (value) OCT1 cause) := {
	tag := OSMO_GSUP_SM_RP_CAUSE_IE,
	len := 0, /* overwritten */
	val := {
		sm_rp_cause := cause
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_RP_CAUSE(template (present) OCT1 cause) := {
	tag := OSMO_GSUP_SM_RP_CAUSE_IE,
	len := ?,
	val := {
		sm_rp_cause := cause
	}
}

template (value) GSUP_IE ts_GSUP_IE_SM_RP_MMS(template (value) OCT1 mms) := {
	tag := OSMO_GSUP_SM_RP_MMS_IE,
	len := 0, /* overwritten */
	val := {
		sm_rp_mms := mms
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_RP_MMS(template (present) OCT1 mms) := {
	tag := OSMO_GSUP_SM_RP_MMS_IE,
	len := ?,
	val := {
		sm_rp_mms := mms
	}
}

template (value) GSUP_IE ts_GSUP_IE_IMEI(template (value) hexstring imei) := {
	tag := OSMO_GSUP_IMEI_IE,
	len := 0, /* overwritten */
	val := {
		imei := ts_GSUP_IMEI(imei)
	}
}
template (present) GSUP_IE tr_GSUP_IE_IMEI(template (present) hexstring imei) := {
	tag := OSMO_GSUP_IMEI_IE,
	len := ?,
	val := {
		imei := tr_GSUP_IMEI(imei)
	}
}

template (value) GSUP_IE ts_GSUP_IE_IMEI_Result(template (value) GSUP_IMEIResult result) := {
	tag := OSMO_GSUP_IMEI_RESULT_IE,
	len := 0, /* overwritten */
	val := {
		imei_result := result
	}
}
template (present) GSUP_IE tr_GSUP_IE_IMEI_Result(template (present) GSUP_IMEIResult result) := {
	tag := OSMO_GSUP_IMEI_RESULT_IE,
	len := ?,
	val := {
		imei_result := result
	}
}

template (value) GSUP_IE ts_GSUP_IE_NUM_VECTORS_REQ(template (value) OCT1 num) := {
	tag := OSMO_GSUP_NUM_VECTORS_REQ_IE,
	len := 0, /* overwritten */
	val := {
		num_auth_vectors := num
	}
}
template (present) GSUP_IE tr_GSUP_IE_NUM_VECTORS_REQ(template (present) OCT1 num) := {
	tag := OSMO_GSUP_NUM_VECTORS_REQ_IE,
	len := ?,
	val := {
		num_auth_vectors := num
	}
}

/* See 3GPP TS 24.011, figures 8.5 and 8.6 */
private function f_pad_SM_RP_Addr(template hexstring number)
return template hexstring {
	if (isvalue(number) and not istemplatekind(number, "omit")) {
		return f_pad_bcd_number(valueof(number));
	} else {
		return number;
	}
}

template GSUP_SM_RP_Addr t_GSUP_SM_RP_Addr(template hexstring number,
					   template BIT4 npi := '0001'B,
					   template BIT3 ton := '001'B,
					   template BIT1 ext := '1'B) := {
	ext := ext,
	ton := ton,
	npi := npi,
	/* Work around TITAN's padding problems: encoding works fine,
	 * but it does not consider 'F'H as padding in decoded data. */
	number := f_pad_SM_RP_Addr(number)
}

/**
 * SM-RP-DA represents the SM Destination Address, see 7.6.8.1.
 * It can be either of the following:
 *  - IMSI
 *  - LMSI (not implemented)
 *  - MSISDN
 *  - roaming number (not implemented)
 *  - service centre address
 */
template (value) GSUP_SM_RP_DA ts_GSUP_SM_RP_DA_IMSI(hexstring imsi) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_IMSI,
	id_enc := { imsi := imsi }
}
template GSUP_SM_RP_DA tr_GSUP_SM_RP_DA_IMSI(template hexstring imsi) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_IMSI,
	id_enc := { imsi := imsi }
}

template (value) GSUP_SM_RP_DA ts_GSUP_SM_RP_DA_MSISDN(GSUP_SM_RP_Addr msisdn) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_MSISDN,
	id_enc := { msisdn := msisdn }
}
template GSUP_SM_RP_DA tr_GSUP_SM_RP_DA_MSISDN(template GSUP_SM_RP_Addr msisdn) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_MSISDN,
	id_enc := { msisdn := msisdn }
}

template (value) GSUP_SM_RP_DA ts_GSUP_SM_RP_DA_SMSC_ADDR(GSUP_SM_RP_Addr smsc_addr) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_SMSC_ADDR,
	id_enc := { smsc_addr := smsc_addr }
}
template GSUP_SM_RP_DA tr_GSUP_SM_RP_DA_SMSC_ADDR(template GSUP_SM_RP_Addr smsc_addr) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_SMSC_ADDR,
	id_enc := { smsc_addr := smsc_addr }
}

template (value) GSUP_SM_RP_DA ts_GSUP_SM_RP_DA_NULL := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_NULL,
	id_enc := omit
}
template GSUP_SM_RP_DA tr_GSUP_SM_RP_DA_NULL := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_NULL,
	id_enc := omit
}

template (value) GSUP_IE ts_GSUP_IE_SM_RP_DA(template (value) GSUP_SM_RP_DA val) := {
	tag := OSMO_GSUP_SM_RP_DA_IE,
	len := 0, /* overwritten */
	val := {
		sm_rp_da := val
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_RP_DA(template (present) GSUP_SM_RP_DA val) := {
	tag := OSMO_GSUP_SM_RP_DA_IE,
	len := ?,
	val := {
		sm_rp_da := val
	}
}

/**
 * SM-RP-OA represents the SM Originating Address, see 7.6.8.2.
 * It can be either of the following:
 *  - MSISDN
 *  - service centre address
 */
template (value) GSUP_SM_RP_OA ts_GSUP_SM_RP_OA_MSISDN(GSUP_SM_RP_Addr msisdn) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_MSISDN,
	id_enc := { msisdn := msisdn }
}
template GSUP_SM_RP_OA tr_GSUP_SM_RP_OA_MSISDN(template GSUP_SM_RP_Addr msisdn) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_MSISDN,
	id_enc := { msisdn := msisdn }
}

template (value) GSUP_SM_RP_OA ts_GSUP_SM_RP_OA_SMSC_ADDR(GSUP_SM_RP_Addr smsc_addr) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_SMSC_ADDR,
	id_enc := { smsc_addr := smsc_addr }
}
template GSUP_SM_RP_OA tr_GSUP_SM_RP_OA_SMSC_ADDR(template GSUP_SM_RP_Addr smsc_addr) := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_SMSC_ADDR,
	id_enc := { smsc_addr := smsc_addr }
}

template (value) GSUP_SM_RP_OA ts_GSUP_SM_RP_OA_NULL := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_NULL,
	id_enc := omit
}
template GSUP_SM_RP_OA tr_GSUP_SM_RP_OA_NULL := {
	id_type := OSMO_GSUP_SM_RP_ODA_ID_NULL,
	id_enc := omit
}

template (value) GSUP_IE ts_GSUP_IE_SM_RP_OA(template (value) GSUP_SM_RP_OA val) := {
	tag := OSMO_GSUP_SM_RP_OA_IE,
	len := 0, /* overwritten */
	val := {
		sm_rp_oa := val
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_RP_OA(template (present) GSUP_SM_RP_OA val) := {
	tag := OSMO_GSUP_SM_RP_OA_IE,
	len := ?,
	val := {
		sm_rp_oa := val
	}
}

/* SM-RP-UI represents the SM TPDU, see 7.6.8.4 */
template (value) GSUP_IE ts_GSUP_IE_SM_RP_UI(template (value) octetstring val) := {
	tag := OSMO_GSUP_SM_RP_UI_IE,
	len := 0, /* overwritten */
	val := {
		sm_rp_ui := val
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_RP_UI(template (present) octetstring val) := {
	tag := OSMO_GSUP_SM_RP_UI_IE,
	len := ?,
	val := {
		sm_rp_ui := val
	}
}

/* SM Alert Reason IE (used in READY-FOR-SM), see 7.6.8.8 */
template (value) GSUP_IE ts_GSUP_IE_SM_ALERT_RSN(template (value) GSUP_SM_ALERT_RSN_Type rsn) := {
	tag := OSMO_GSUP_SM_ALERT_RSN_IE,
	len := 0, /* overwritten */
	val := {
		sm_alert_rsn := rsn
	}
}
template (present) GSUP_IE tr_GSUP_IE_SM_ALERT_RSN(template (present) GSUP_SM_ALERT_RSN_Type rsn) := {
	tag := OSMO_GSUP_SM_ALERT_RSN_IE,
	len := ?,
	val := {
		sm_alert_rsn := rsn
	}
}

template (value) GSUP_IE ts_GSUP_IE_SSInfo(template (value) octetstring ss) := {
	tag := OSMO_GSUP_SS_INFO_IE,
	len := 0, /* overwritten */
	val := {
		ss_info := ss
	}
}
template (present) GSUP_IE tr_GSUP_IE_SSInfo(template (present) octetstring ss) := {
	tag := OSMO_GSUP_SS_INFO_IE,
	len := ?,
	val := {
		ss_info := ss
	}
}

template (present) GSUP_IE tr_GSUP_IE_Message_Class(template (present) GSUP_Message_Class val) := {
	tag := OSMO_GSUP_MESSAGE_CLASS_IE,
	len := ?,
	val := {
		message_class := val
	}
}

template (value) GSUP_IE ts_GSUP_IE_Message_Class(template (value) GSUP_Message_Class val) := {
	tag := OSMO_GSUP_MESSAGE_CLASS_IE,
	len := 0, /* overwritten */
	val := {
		message_class := val
	}
}

template (present) GSUP_IE tr_GSUP_IE_Source_Name(template (present) octetstring name) := {
	tag := OSMO_GSUP_SOURCE_NAME_IE,
	len := ?,
	val := {
		source_name := name
	}
}

template (value) GSUP_IE ts_GSUP_IE_Source_Name(template (value) octetstring name) := {
	tag := OSMO_GSUP_SOURCE_NAME_IE,
	len := 0, /* overwritten */
	val := {
		source_name := name
	}
}

template (present) GSUP_IE tr_GSUP_IE_Destination_Name(template (present) octetstring name) := {
	tag := OSMO_GSUP_DESTINATION_NAME_IE,
	len := ?,
	val := {
		destination_name := name
	}
}

template (value) GSUP_IE ts_GSUP_IE_Destination_Name(template (value) octetstring name) := {
	tag := OSMO_GSUP_DESTINATION_NAME_IE,
	len := 0, /* overwritten */
	val := {
		destination_name := name
	}
}

template (present) GSUP_IE tr_GSUP_IE_AN_APDU(template (present) GSUP_AN_APDU an_apdu) := {
	tag := OSMO_GSUP_AN_APDU_IE,
	len := ?,
	val := {
		an_apdu := an_apdu
	}
}

template (value) GSUP_IE ts_GSUP_IE_AN_APDU(template (value) GSUP_AN_APDU an_apdu) := {
	tag := OSMO_GSUP_AN_APDU_IE,
	len := 0, /* overwritten */
	val := {
		an_apdu := an_apdu
	}
}

template (present) GSUP_IE tr_GSUP_IE_SUPPORTED_RAT_TYPES(template (present) GSUP_RatTypes ratt) := {
	tag := OSMO_GSUP_SUPPORTED_RAT_TYPES_IE,
	len := ?,
	val := {
		supported_rat_types := ratt
	}
}
template (value) GSUP_IE ts_GSUP_IE_SUPPORTED_RAT_TYPES(template (value) GSUP_RatTypes ratt) := {
	tag := OSMO_GSUP_SUPPORTED_RAT_TYPES_IE,
	len := 0, /* overwritten */
	val := {
		supported_rat_types := ratt
	}
}

template (present) GSUP_IE tr_GSUP_IE_CURRENT_RAT_TYPE(template (present) GSUP_RatType ratt) := {
	tag := OSMO_GSUP_CURRENT_RAT_TYPE_IE,
	len := ?,
	val := {
		current_rat_type := ratt
	}
}
template (value) GSUP_IE ts_GSUP_IE_CURRENT_RAT_TYPE(template (value) GSUP_RatType ratt) := {
	tag := OSMO_GSUP_CURRENT_RAT_TYPE_IE,
	len := 0, /* overwritten */
	val := {
		current_rat_type := ratt
	}
}

private function f_gen_ts_ies(hexstring imsi,
			      template (omit) boolean pdp_info_compl := omit,
			      template (omit) GSUP_Message_Class message_class := omit,
			      template (omit) hexstring imei := omit,
			      template (omit) PCO_DATA pco := omit,
			      template (omit) GSUP_CnDomain dom := omit,
			      template (omit) octetstring source_name := omit,
			      template (omit) octetstring destination_name := omit)
return GSUP_IEs {
	var GSUP_IEs ies := {
		valueof(ts_GSUP_IE_IMSI(imsi))
	};

	if (isvalue(dom)) {
		ies := ies & { valueof(ts_GSUP_IE_CnDomain(dom)) };
	}

	if (isvalue(pdp_info_compl) and valueof(pdp_info_compl)) {
		ies := ies & { valueof(ts_GSUP_IE_PdpInfoCompl) };
	}

	if (isvalue(imei)) {
		ies := ies & { valueof(ts_GSUP_IE_IMEI(valueof(imei))) };
	}

	if (isvalue(pco)) {
		ies := ies & { valueof(ts_GSUP_IE_PCO(valueof(pco))) };
	}

	if (isvalue(message_class)) {
		ies := ies & { valueof(ts_GSUP_IE_Message_Class(valueof(message_class))) };
	}

	if (isvalue(source_name)) {
		ies := ies & { valueof(ts_GSUP_IE_Source_Name(valueof(source_name))) };
	}

	if (isvalue(destination_name)) {
		ies := ies & { valueof(ts_GSUP_IE_Destination_Name(valueof(destination_name))) };
	}

	return ies;
}

private function f_gen_tr_ies(template hexstring imsi,
			      template boolean pdp_info_compl := omit,
			      template GSUP_IEs pdp_info := omit,
			      template GSUP_Message_Class message_class := omit,
			      template integer cause := omit,
			      template GSUP_CancelType cancel_type := omit,
			      template hexstring msisdn := omit,
			      template GSUP_IMEIResult imei_result := omit,
			      template PCO_DATA pco := omit,
			      template GSUP_CnDomain cn_domain := omit,
			      template octetstring source_name := omit,
			      template octetstring destination_name := omit)
return template GSUP_IEs {
	var template GSUP_IEs ies := {
		tr_GSUP_IE_IMSI(imsi)
	};
	var integer idx := 1;

	if (not istemplatekind(msisdn, "omit")) {
		ies[idx] := tr_GSUP_IE_MSISDN(msisdn);
		idx := idx + 1;
	}

	if (not istemplatekind(cause, "omit")) {
		ies[idx] := tr_GSUP_IE_Cause(cause);
		idx := idx + 1;
	}

	if (not istemplatekind(cancel_type, "omit")) {
		ies[idx] := tr_GSUP_IE_CancelType(cancel_type);
		idx := idx + 1;
	}

	if (not istemplatekind(pdp_info_compl, "omit")) {
		ies[idx] := tr_GSUP_IE_PdpInfoCompl;
		idx := idx + 1;
	}

	if (not istemplatekind(pdp_info, "omit")) {
		ies[idx] := tr_GSUP_IE_PdpInfo_ie(pdp_info);
		idx := idx + 1;
	}

	if (not istemplatekind(cn_domain, "omit")) {
		ies[idx] := tr_GSUP_IE_CnDomain(cn_domain);
		idx := idx + 1;
	}

	if (not istemplatekind(imei_result, "omit")) {
		ies[idx] := tr_GSUP_IE_IMEI_Result(imei_result);
		idx := idx + 1;
	}

	if (not istemplatekind(message_class, "omit")) {
		ies[idx] := tr_GSUP_IE_Message_Class(message_class);
		idx := idx + 1;
	}

	if (not istemplatekind(pco, "omit")) {
		ies[idx] := tr_GSUP_IE_PCO(pco);
		idx := idx + 1;
	}

	if (not istemplatekind(source_name, "omit")) {
		ies[idx] := tr_GSUP_IE_Source_Name(source_name);
		idx := idx + 1;
	}

	ies[idx] := *;
	idx := idx + 1;

	if (not istemplatekind(destination_name, "omit")) {
		if (istemplatekind(destination_name, "*")) {
			ies[idx] := *;
		} else {
			ies[idx] := tr_GSUP_IE_Destination_Name(destination_name);
		}
		idx := idx + 1;
	}

	return ies;
}

private function f_gen_ts_ss_ies(template (value) hexstring imsi,
				 template (value) OCT4 sid,
				 template (value) GSUP_SessionState state,
				 /* 'ss' and 'cause' are mutually exclusive */
				 template (omit) octetstring ss := omit,
				 template (omit) integer cause := omit,
				 template (omit) octetstring src_name := omit,
				 template (omit) octetstring dst_name := omit)
return template (value) GSUP_IEs {
	/* Mandatory IEs */
	var template (value) GSUP_IEs ies := {
		ts_GSUP_IE_IMSI(imsi)
	};

	/* OSMO_GSUP_CAUSE_IE (needed for PROC_SS_ERR) */
	if (not istemplatekind(cause, "omit")) {
		ies[lengthof(ies)] := ts_GSUP_IE_Cause(cause);
	}

	/* OSMO_GSUP_SESSION_{ID,STATE}_IE */
	ies[lengthof(ies)] := ts_GSUP_IE_SessionId(sid);
	ies[lengthof(ies)] := ts_GSUP_IE_SessionState(state);

	/* OSMO_GSUP_SS_INFO_IE */
	if (not istemplatekind(ss, "omit")) {
		ies[lengthof(ies)] := ts_GSUP_IE_SSInfo(ss);
	}

	/* OSMO_GSUP_{SOURCE,DESTINATION}_NAME_IE */
	if (not istemplatekind(src_name, "omit")) {
		ies[lengthof(ies)] := ts_GSUP_IE_Source_Name(src_name);
	}
	if (not istemplatekind(dst_name, "omit")) {
		ies[lengthof(ies)] := ts_GSUP_IE_Destination_Name(dst_name);
	}

	return ies;
}
private function f_gen_tr_ss_ies(template (present) hexstring imsi,
				 template (present) OCT4 sid := ?,
				 template (present) GSUP_SessionState state := ?,
				 /* 'ss' and 'cause' are mutually exclusive */
				 template octetstring ss := omit,
				 template integer cause := omit,
				 template octetstring src_name := *,
				 template octetstring dst_name := *)
return template GSUP_IEs {
	/* Mandatory IEs */
	var template GSUP_IEs ies := {
		tr_GSUP_IE_IMSI(imsi)
	};
	var integer idx := lengthof(ies);

	/* OSMO_GSUP_CAUSE_IE (needed for PROC_SS_ERR) */
	if (ispresent(cause)) {
		ies[idx] := tr_GSUP_IE_Cause(cause);
		idx := idx + 1;
	} else if (istemplatekind(cause, "*")) {
		ies[idx] := *;
		idx := idx + 1;
	}

	/* OSMO_GSUP_SESSION_{ID,STATE}_IE */
	ies[idx] := tr_GSUP_IE_SessionId(sid);
	idx := idx + 1;
	ies[idx] := tr_GSUP_IE_SessionState(state);
	idx := idx + 1;

	/* OSMO_GSUP_SS_INFO_IE */
	if (ispresent(ss)) {
		ies[idx] := tr_GSUP_IE_SSInfo(ss);
		idx := idx + 1;
	} else if (istemplatekind(ss, "*")) {
		ies[idx] := *;
		idx := idx + 1;
	}

	/* FIXME: OSMO_GSUP_MESSAGE_CLASS_IE goes here */

	/* OSMO_GSUP_{SOURCE,DESTINATION}_NAME_IE */
	if (ispresent(src_name)) {
		ies[idx] := tr_GSUP_IE_Source_Name(src_name);
		idx := idx + 1;
	} else if (istemplatekind(src_name, "*")) {
		ies[idx] := *;
		idx := idx + 1;
	}
	if (ispresent(dst_name)) {
		ies[idx] := tr_GSUP_IE_Destination_Name(dst_name);
		idx := idx + 1;
	} else if (istemplatekind(dst_name, "*")) {
		ies[idx] := *;
		idx := idx + 1;
	}

	/* the GSUP Message Class IE is optional, as old implementations don't have it yet */
	var template GSUP_IEs ies2 := ies;
	ies2[idx] := tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_USSD);
	idx := idx + 1;

	return (ies, ies2);
}

template (value) GSUP_PDU
ts_GSUP_PROC_SS_REQ(template (value) hexstring imsi,
		    template (value) OCT4 sid,
		    template (value) GSUP_SessionState state,
		    template (omit) octetstring ss := omit,
		    template (omit) octetstring source_name := omit,
		    template (omit) octetstring destination_name := omit) := {
	msg_type := OSMO_GSUP_MSGT_PROC_SS_REQUEST,
	ies := f_gen_ts_ss_ies(imsi, sid, state, ss,
			       src_name := source_name,
			       dst_name := destination_name)
};
template (present) GSUP_PDU
tr_GSUP_PROC_SS_REQ(template (present) hexstring imsi,
		    template (present) OCT4 sid := ?,
		    template (present) GSUP_SessionState state := ?,
		    template octetstring ss := *,
		    template octetstring source_name := *,
		    template octetstring destination_name := *) := {
	msg_type := OSMO_GSUP_MSGT_PROC_SS_REQUEST,
	ies := f_gen_tr_ss_ies(imsi, sid, state, ss,
			       src_name := source_name,
			       dst_name := destination_name)
};

template (value) GSUP_PDU
ts_GSUP_PROC_SS_RES(template (value) hexstring imsi,
		    template (value) OCT4 sid,
		    template (value) GSUP_SessionState state,
		    template (omit) octetstring ss := omit,
		    template (omit) octetstring source_name := omit,
		    template (omit) octetstring destination_name := omit) := {
	msg_type := OSMO_GSUP_MSGT_PROC_SS_RESULT,
	ies := f_gen_ts_ss_ies(imsi, sid, state, ss,
			       src_name := source_name,
			       dst_name := destination_name)
};
template (present) GSUP_PDU
tr_GSUP_PROC_SS_RES(template (present) hexstring imsi,
		    template (present) OCT4 sid := ?,
		    template (present) GSUP_SessionState state := ?,
		    template octetstring ss := *,
		    template octetstring source_name := *,
		    template octetstring destination_name := *) := {
	msg_type := OSMO_GSUP_MSGT_PROC_SS_RESULT,
	ies := f_gen_tr_ss_ies(imsi, sid, state, ss,
			       src_name := source_name,
			       dst_name := destination_name)
};

template (value) GSUP_PDU
ts_GSUP_PROC_SS_ERR(template (value) hexstring imsi,
		    template (value) OCT4 sid,
		    template (value) GSUP_SessionState state,
		    template (value) integer cause,
		    template (omit) octetstring source_name := omit,
		    template (omit) octetstring destination_name := omit) := {
	msg_type := OSMO_GSUP_MSGT_PROC_SS_ERROR,
	ies := f_gen_ts_ss_ies(imsi, sid, state,
			       cause := cause,
			       src_name := source_name,
			       dst_name := destination_name)
};
template (present) GSUP_PDU
tr_GSUP_PROC_SS_ERR(template (present) hexstring imsi,
		    template (present) OCT4 sid := ?,
		    template (present) GSUP_SessionState state := ?,
		    template (present) integer cause := ?,
		    template octetstring source_name := *,
		    template octetstring destination_name := *) := {
	msg_type := OSMO_GSUP_MSGT_PROC_SS_ERROR,
	ies := f_gen_tr_ss_ies(imsi, sid, state,
			       cause := cause,
			       src_name := source_name,
			       dst_name := destination_name)
};

template (value) GSUP_PDU ts_GSUP_MO_FORWARD_SM_REQ(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	GSUP_SM_RP_DA sm_rp_da, /* Destination Address, see 7.6.8.1 */
	GSUP_SM_RP_OA sm_rp_oa, /* Originating Address, see 7.6.8.2 */
	octetstring sm_rp_ui /* SM TPDU, see 7.6.8.4 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_SM_RP_DA(sm_rp_da)),
		valueof(ts_GSUP_IE_SM_RP_OA(sm_rp_oa)),
		valueof(ts_GSUP_IE_SM_RP_UI(sm_rp_ui)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MO_FORWARD_SM_REQ(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template GSUP_SM_RP_DA sm_rp_da, /* Destination Address, see 7.6.8.1 */
	template GSUP_SM_RP_OA sm_rp_oa, /* Originating Address, see 7.6.8.2 */
	template octetstring sm_rp_ui /* SM TPDU, see 7.6.8.4 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MO_FORWARD_SM_REQUEST,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_SM_RP_DA(sm_rp_da),
		tr_GSUP_IE_SM_RP_OA(sm_rp_oa),
		tr_GSUP_IE_SM_RP_UI(sm_rp_ui),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MO_FORWARD_SM_RES(
	hexstring imsi,
	OCT1 sm_rp_mr /* Message Reference, see GSM TS 04.11, 8.2.3 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MO_FORWARD_SM_RES(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ? /* Message Reference, see GSM TS 04.11, 8.2.3 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MO_FORWARD_SM_ERR(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	OCT1 sm_rp_cause /* RP-Cause value, see GSM TS 04.11, 8.2.5.4 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_SM_RP_CAUSE(sm_rp_cause)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MO_FORWARD_SM_ERR(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template OCT1 sm_rp_cause := ? /* RP-Cause value, see GSM TS 04.11, 8.2.5.4 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_SM_RP_CAUSE(sm_rp_cause),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MT_FORWARD_SM_REQ(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	GSUP_SM_RP_DA sm_rp_da, /* Destination Address, see 7.6.8.1 */
	GSUP_SM_RP_OA sm_rp_oa, /* Originating Address, see 7.6.8.2 */
	octetstring sm_rp_ui, /* SM TPDU, see 7.6.8.4 */
	OCT1 sm_rp_mms /* MMS (More Messages to Send), see 7.6.8.7 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST,
	{
		/**
		 * TODO: add MT-specific fields (and IEs):
		 *  - smDeliveryTimer
		 *  - smDeliveryStartTime
		 */
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_SM_RP_DA(sm_rp_da)),
		valueof(ts_GSUP_IE_SM_RP_OA(sm_rp_oa)),
		valueof(ts_GSUP_IE_SM_RP_UI(sm_rp_ui)),
		valueof(ts_GSUP_IE_SM_RP_MMS(sm_rp_mms)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MT_FORWARD_SM_REQ(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template GSUP_SM_RP_DA sm_rp_da, /* Destination Address, see 7.6.8.1 */
	template GSUP_SM_RP_OA sm_rp_oa, /* Originating Address, see 7.6.8.2 */
	template octetstring sm_rp_ui, /* SM TPDU, see 7.6.8.4 */
	template OCT1 sm_rp_mms /* MMS (More Messages to Send), see 7.6.8.7 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST,
	{
		/**
		 * TODO: add MT-specific fields (and IEs):
		 *  - smDeliveryTimer
		 *  - smDeliveryStartTime
		 */
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_SM_RP_DA(sm_rp_da),
		tr_GSUP_IE_SM_RP_OA(sm_rp_oa),
		tr_GSUP_IE_SM_RP_UI(sm_rp_ui),
		tr_GSUP_IE_SM_RP_MMS(sm_rp_mms),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MT_FORWARD_SM_RES(
	hexstring imsi,
	OCT1 sm_rp_mr /* Message Reference, see GSM TS 04.11, 8.2.3 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MT_FORWARD_SM_RES(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ? /* Message Reference, see GSM TS 04.11, 8.2.3 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_RESULT,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

/* MT delivery error indicated by the MS/UE */
template (value) GSUP_PDU ts_GSUP_MT_FORWARD_SM_ERR_MS(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	OCT1 sm_rp_cause /* RP-Cause value, see GSM TS 04.11, 8.2.5.4 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_SM_RP_CAUSE(sm_rp_cause)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MT_FORWARD_SM_ERR_MS(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template OCT1 sm_rp_cause := ? /* RP-Cause value, see GSM TS 04.11, 8.2.5.4 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_SM_RP_CAUSE(sm_rp_cause),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

/* MT delivery error indicated by the network */
template (value) GSUP_PDU ts_GSUP_MT_FORWARD_SM_ERR_NET(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	integer cause /* Cause (indicated by network) */
) := ts_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_Cause(cause)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		/* Cause IE replaces the SM-RP-Cause IE */
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MT_FORWARD_SM_ERR_NET(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template integer cause := ? /* Cause (indicated by network) */
) := tr_GSUP(
	OSMO_GSUP_MSGT_MT_FORWARD_SM_ERROR,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_Cause(cause),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		/* Cause IE replaces the SM-RP-Cause IE */
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MO_READY_FOR_SM_REQ(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	GSUP_SM_ALERT_RSN_Type sm_alert_rsn /* SM Alert Reason, see 7.6.8.8 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_SM_ALERT_RSN(sm_alert_rsn)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MO_READY_FOR_SM_REQ(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template GSUP_SM_ALERT_RSN_Type sm_alert_rsn := ? /* SM Alert Reason, see 7.6.8.8 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_READY_FOR_SM_REQUEST,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_SM_ALERT_RSN(sm_alert_rsn),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MO_READY_FOR_SM_RES(
	hexstring imsi,
	OCT1 sm_rp_mr /* Message Reference, see GSM TS 04.11, 8.2.3 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_READY_FOR_SM_RESULT,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MO_READY_FOR_SM_RES(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ? /* Message Reference, see GSM TS 04.11, 8.2.3 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_READY_FOR_SM_RESULT,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

template (value) GSUP_PDU ts_GSUP_MO_READY_FOR_SM_ERR(
	hexstring imsi,
	OCT1 sm_rp_mr, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	OCT1 sm_rp_cause /* RP-Cause value, see GSM TS 04.11, 8.2.5.4 */
) := ts_GSUP(
	OSMO_GSUP_MSGT_READY_FOR_SM_ERROR,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_SM_RP_MR(sm_rp_mr)),
		valueof(ts_GSUP_IE_SM_RP_CAUSE(sm_rp_cause)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS))
	}
);
template GSUP_PDU tr_GSUP_MO_READY_FOR_SM_ERR(
	template hexstring imsi := ?,
	template OCT1 sm_rp_mr := ?, /* Message Reference, see GSM TS 04.11, 8.2.3 */
	template OCT1 sm_rp_cause := ? /* RP-Cause value, see GSM TS 04.11, 8.2.5.4 */
) := tr_GSUP(
	OSMO_GSUP_MSGT_READY_FOR_SM_ERROR,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_SM_RP_MR(sm_rp_mr),
		tr_GSUP_IE_SM_RP_CAUSE(sm_rp_cause),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_SMS),
		tr_GSUP_IE_Source_Name(?)
	}
);

function f_gsup_find_nested_ie_multiple(GSUP_IEs ies, GSUP_IEI iei, integer nth,  out GSUP_IeValue ret) return boolean {
	var integer current := 0;
	for (var integer i := 0; i < sizeof(ies); i := i+1) {
		if (ies[i].tag == iei) {
			if (current == nth) {
				ret := ies[i].val;
				return true;
			} else {
				current := current + 1;
			}
		}
	}
	return false;
}

function f_gsup_find_nested_ie(GSUP_IEs ies, GSUP_IEI iei, out GSUP_IeValue ret) return boolean {
	for (var integer i := 0; i < sizeof(ies); i := i+1) {
		if (ies[i].tag == iei) {
			ret := ies[i].val;
			return true;
		}
	}
	return false;
}

function f_gsup_find_ie(GSUP_PDU msg, GSUP_IEI iei, out GSUP_IeValue ret) return boolean {
	return f_gsup_find_nested_ie(msg.ies, iei, ret);
}

template GSUP_AN_APDU t_GSUP_AN_APDU(
	template GSUP_AN_PROTO an_proto := ?,
	template octetstring pdu := ?
) := {
	proto := an_proto,
	pdu := pdu
};

template GSUP_PDU tr_GSUP_E_AN_APDU(
	template GSUP_MessageType msgt,
	template hexstring imsi := ?,
	template octetstring source_name := ?,
	template octetstring destination_name := ?,
	template GSUP_AN_APDU an_apdu := ?
) := tr_GSUP(
	msgt,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_INTER_MSC),
		tr_GSUP_IE_Source_Name(source_name),
		tr_GSUP_IE_Destination_Name(destination_name),
		tr_GSUP_IE_AN_APDU(an_apdu)
	}
);

template GSUP_PDU tr_GSUP_E_NO_PDU(
	template GSUP_MessageType msgt,
	template hexstring imsi := ?,
	template octetstring source_name := ?,
	template octetstring destination_name := ?
) := tr_GSUP(
	msgt,
	{
		tr_GSUP_IE_IMSI(imsi),
		tr_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_INTER_MSC),
		tr_GSUP_IE_Source_Name(source_name),
		tr_GSUP_IE_Destination_Name(destination_name)
	}
);

template (value) GSUP_PDU ts_GSUP_E_AN_APDU(
	GSUP_MessageType msgt,
	hexstring imsi,
	octetstring source_name,
	octetstring destination_name,
	GSUP_AN_APDU an_apdu
) := ts_GSUP(
	msgt,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_INTER_MSC)),
		valueof(ts_GSUP_IE_Source_Name(source_name)),
		valueof(ts_GSUP_IE_Destination_Name(destination_name)),
		valueof(ts_GSUP_IE_AN_APDU(an_apdu))
	}
);

template (value) GSUP_PDU ts_GSUP_E_PrepareHandoverResult(
	hexstring imsi,
	hexstring msisdn,
	octetstring source_name,
	octetstring destination_name,
	GSUP_AN_APDU an_apdu
) := ts_GSUP(
	OSMO_GSUP_MSGT_E_PREPARE_HANDOVER_RESULT,
	{
		valueof(ts_GSUP_IE_IMSI(imsi)),
		valueof(ts_GSUP_IE_MSISDN(msisdn)),
		valueof(ts_GSUP_IE_Message_Class(OSMO_GSUP_MESSAGE_CLASS_INTER_MSC)),
		valueof(ts_GSUP_IE_Source_Name(source_name)),
		valueof(ts_GSUP_IE_Destination_Name(destination_name)),
		valueof(ts_GSUP_IE_AN_APDU(an_apdu))
	}
);

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