module SGsAP_Templates {

/* Templates for the SGsAP protocol as per 3GPP TS 29.118
 * (C) 2018 by Harald Welte <laforg@gnumonks.org>
 * All rights reserved.
 *
 * Released under the terms of the GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

import from SGsAP_Types all;
import from GSM_Types all;
import from General_Types all;
import from Osmocom_Types all;
import from MobileL3_CommonIE_Types all;

/* 9.4.2 */
template (value) EPS_LocationUpdateType ts_SGsAP_IE_EpsLuType(template (value) EPS_location_update_type lut) := {
	iEI := '00001010'B,
	lengthIndicator := 1,
	locationUpdateType := lut
}
template EPS_LocationUpdateType tr_SGsAP_IE_EpsLuType(template EPS_location_update_type lut) := {
	iEI := '00001010'B,
	lengthIndicator := 1,
	locationUpdateType := lut
}

/* 9.4.3 */
template (value) ErroneousMessage ts_SGsAP_IE_ErrMsg(template (value) octetstring err_msg) := {
	iEI := '00011011'B,
	lengthIndicator := lengthof(valueof(err_msg)),
	erroneousMessage := err_msg
}
template ErroneousMessage tr_SGsAP_IE_ErrMsg(template octetstring err_msg) := {
	iEI := '00011011'B,
	lengthIndicator := ?,
	erroneousMessage := err_msg
}


/* 9.4.6 */
private function ts_SGsAP_IMSI(hexstring digits) return template (value) IMSI {
	var template (value) IMSI imsi := {
		iEI := '00000001'B,
		lengthIndicator := lengthof(digits)/2 + 1,
		iMSI := {
			field1 := '001'B,
			parity := '0'B, /* even */
			digits := digits,
			fillerDigit := '1111'B /* even */
		}
	}
	if (lengthof(digits) mod 2 == 1) {
		/* odd number of digits */
		imsi.iMSI.parity := '1'B;
		imsi.iMSI.fillerDigit := omit;
	}
	return imsi;
}
private function ts_SGsAP_IMSI_omit(template (omit) hexstring digits) return template (omit) IMSI {
	if (istemplatekind(digits, "omit")) {
		return omit;
	} else {
		return ts_SGsAP_IMSI(valueof(digits));
	}
}
private function tr_SGsAP_IMSI(template hexstring digits) return template IMSI {
	if (istemplatekind(digits, "omit")) {
		return omit;
	} else if (istemplatekind(digits, "*")) {
		return *;
	} else if (istemplatekind(digits, "?")) {
		return ?;
	}
	log("tr_SGsAP_IMSI: ", digits);
	var template IMSI imsi := {
		iEI := '00000001'B,
		lengthIndicator := lengthof(digits)/2 + 1,
		iMSI := {
			field1 := '001'B,
			parity := '0'B, /* even */
			digits := digits,
			fillerDigit := '1111'B /* even */
		}
	}
	if (lengthof(digits) mod 2 == 1) {
		/* odd number of digits */
		/* odd number of digits */
		imsi.iMSI.parity := '1'B;
		imsi.iMSI.fillerDigit := omit;
	}
	return imsi;
}

/* 9.4.7 */
template (value) IMSI_DetachFromEPS_ServiceType ts_SGsAP_IE_DetServiceType(template (value) IMSI_detachFromEPS_serviceType st) := {
	iEI := '00010000'B,
	lengthIndicator := 1,
	serviceType := st
}
template IMSI_DetachFromEPS_ServiceType tr_SGsAP_IE_DetServiceType(template IMSI_detachFromEPS_serviceType st) := {
	iEI := '00010000'B,
	lengthIndicator := 1,
	serviceType := st
}

/* 9.4.8 */
template (value) IMSI_DetachFromNonEPS_ServiceType ts_SGsAP_IE_DetNonEpsServiceType(template (value) IMSI_detachFromNonEPS_serviceType st) := {
	iEI := '00010001'B,
	lengthIndicator := 1,
	serviceType := st
}
template IMSI_DetachFromNonEPS_ServiceType tr_SGsAP_IE_DetNonEpsServiceType(template IMSI_detachFromNonEPS_serviceType st) := {
	iEI := '00010001'B,
	lengthIndicator := 1,
	serviceType := st
}

/* 9.4.11 */
private function f_enc_mnc_digit3(GsmMnc mnc) return HEX1 {
	if (lengthof(mnc) == 2) {
		return 'F'H;
	} else {
		return mnc[2];
	}
}
template (value) LocationAreaIdValue ts_SGsAP_LAI(GsmMcc mcc, GsmMnc mnc, GsmLac lac) := {
	mccDigit1 := mcc[0],
	mccDigit2 := mcc[1],
	mccDigit3 := mcc[2],
	mncDigit3 := f_enc_mnc_digit3(mnc),
	mncDigit1 := mnc[0],
	mncDigit2 := mnc[1],
	lac := int2oct(lac, 2)
}
template (value) LocationAreaId ts_SGsAP_IE_Lai(template (value) LocationAreaIdValue lai) := {
	iEI := '00000100'B,
	lengthIndicator := 5,
	locationAreaId := lai
}
template LocationAreaId tr_SGsAP_IE_Lai(template LocationAreaIdValue lai) := {
	iEI := '00000100'B,
	lengthIndicator := 5,
	locationAreaId := lai
}
function ts_SGsAP_IE_Lai_omit(template (omit) LocationAreaIdValue lai) return template (omit) LocationAreaId {
	if (istemplatekind(lai, "omit")) {
		return omit;
	} else {
		return ts_SGsAP_IE_Lai(lai);
	}
}
function tr_SGsAP_IE_Lai_omit(template LocationAreaIdValue lai) return template LocationAreaId {
	if (istemplatekind(lai, "omit")) {
		return omit;
	} else if (istemplatekind(lai, "*")) {
		return *;
	} else {
		return tr_SGsAP_IE_Lai(lai);
	}
}

/* 9.4.21a */
template (value) TrackingAreaIdentityValue ts_SGsAP_TAI(GsmMcc mcc, GsmMnc mnc, uint16_t tac) := {
	mccDigit1 := mcc[0],
	mccDigit2 := mcc[1],
	mccDigit3 := mcc[2],
	mncDigit3 := f_enc_mnc_digit3(mnc),
	mncDigit1 := mnc[0],
	mncDigit2 := mnc[1],
	tAC := int2oct(tac, 2)
}
template (value) TrackingAreaIdentity ts_SGsAP_IE_TAI(template (value) TrackingAreaIdentityValue tai) := {
	iEI := '00100011'B,
	lengthIndicator := 5,
	iD := tai
}
function ts_SGsAP_IE_TAI_omit(template (omit) TrackingAreaIdentityValue tai)
return template (omit) TrackingAreaIdentity {
	if (istemplatekind(tai, "omit")) {
		return omit;
	} else {
		return ts_SGsAP_IE_TAI(tai);
	}
}


/* 9.4.12 */
template (value) MM_Information ts_SGsAP_IE_MmInfo(template (value) octetstring mm_info) := {
	iEI := '00010111'B,
	lengthIndicator := lengthof(valueof(mm_info)),
	information := mm_info
}
template MM_Information tr_SGsAP_IE_MmInfo(template octetstring mm_info) := {
	iEI := '00010111'B,
	lengthIndicator := ?,
	information := mm_info
}



/* 9.4.13 */
template (value) MME_Name ts_SGsAP_IE_MmeName(template (value) octetstring name) := {
	iEI := '00001001'B,
	lengthIndicator := lengthof(valueof(name)),
	name := name
};
template MME_Name tr_SGsAP_IE_MmeName(template octetstring name) := {
	iEI := '00001001'B,
	lengthIndicator := ?,
	name := name
};

/* 9.4.14 */
function f_l3_to_sgs_id(MobileIdentityLV l3) return MobileIdentityValue {
	var OddEvenInd_Identity l3v := l3.mobileIdentityV.oddEvenInd_identity;
	var MobileIdentityValue ret;
	ret.typeOfIdentity := l3.mobileIdentityV.typeOfIdentity;
	if (ischosen(l3v.imei)) {
		ret.iD.imei.oddevenIndicator := l3v.imei.oddevenIndicator;
		ret.iD.imei.digits := l3v.imei.digits;
	} else if (ischosen(l3v.imsi)) {
		ret.iD.imsi.oddevenIndicator := l3v.imsi.oddevenIndicator;
		ret.iD.imsi.digits := l3v.imsi.digits;
		ret.iD.imsi.fillerDigit := l3v.imsi.fillerDigit;
	} else if (ischosen(l3v.imei_sv)) {
		ret.iD.imei_sv.oddevenIndicator := l3v.imei_sv.oddevenIndicator;
		ret.iD.imei_sv.digits := l3v.imei_sv.digits;
		ret.iD.imei_sv.fillerDigit := l3v.imei_sv.fillerDigit;
	} else if (ischosen(l3v.tmsi_ptmsi)) {
		ret.iD.tmsi_ptmsi.oddevenIndicator := l3v.tmsi_ptmsi.oddevenIndicator;
		ret.iD.tmsi_ptmsi.fillerDigit := l3v.tmsi_ptmsi.fillerDigit;
		ret.iD.tmsi_ptmsi.octets := l3v.tmsi_ptmsi.octets;
	} else if (ischosen(l3v.tmgi_and_MBMS_SessionID)) {
		ret.iD.tmgi_and_MBMS_SessionID.oddevenIndicator := l3v.tmgi_and_MBMS_SessionID.oddevenIndicator;
		ret.iD.tmgi_and_MBMS_SessionID.mCC_MNCIndicator := l3v.tmgi_and_MBMS_SessionID.mCC_MNCIndicator;
		ret.iD.tmgi_and_MBMS_SessionID.mBMS_SessionIdentityIndicator := l3v.tmgi_and_MBMS_SessionID.mBMS_SessionIdentityIndicator;
		ret.iD.tmgi_and_MBMS_SessionID.spare := l3v.tmgi_and_MBMS_SessionID.spare;
		ret.iD.tmgi_and_MBMS_SessionID.mBMS_ServiceID := l3v.tmgi_and_MBMS_SessionID.mBMS_ServiceID;
		ret.iD.tmgi_and_MBMS_SessionID.mccDigit1 := l3v.tmgi_and_MBMS_SessionID.mccDigit1;
		ret.iD.tmgi_and_MBMS_SessionID.mccDigit2 := l3v.tmgi_and_MBMS_SessionID.mccDigit2;
		ret.iD.tmgi_and_MBMS_SessionID.mccDigit3 := l3v.tmgi_and_MBMS_SessionID.mccDigit3;
		ret.iD.tmgi_and_MBMS_SessionID.mccDigit3 := l3v.tmgi_and_MBMS_SessionID.mccDigit3;
		ret.iD.tmgi_and_MBMS_SessionID.mccDigit1 := l3v.tmgi_and_MBMS_SessionID.mccDigit1;
		ret.iD.tmgi_and_MBMS_SessionID.mccDigit2 := l3v.tmgi_and_MBMS_SessionID.mccDigit2;
		ret.iD.tmgi_and_MBMS_SessionID.mBMS_SessionIdentity := l3v.tmgi_and_MBMS_SessionID.mBMS_SessionIdentity;
	} else if (ischosen(l3v.no_identity)) {
		ret.iD.no_identity.oddevenIndicator := l3v.no_identity.oddevenIndicator;
		ret.iD.no_identity.fillerDigits := l3v.no_identity.fillerDigits;
	}
	return ret;
}
template MobileIdentity ts_SGsAP_IE_MobileID(template (value) MobileIdentityLV l3_mi) := {
	iEI := '00001110'B,
	lengthIndicator := 0, /* overwritten */
	iD := f_l3_to_sgs_id(valueof(l3_mi))
}


/* 9.4.15 */
template (value) NAS_MessageContainer ts_SGsAP_NasContainer(template (value) octetstring nas_pdu) := {
	iEI := '00010110'B,
	lengthIndicator := lengthof(valueof(nas_pdu)),
	nAS_MessageContainer := nas_pdu
}
template NAS_MessageContainer tr_SGsAP_NasContainer(template octetstring nas_pdu) := {
	iEI := '00010110'B,
	lengthIndicator := ?,
	nAS_MessageContainer := nas_pdu
}

/* 9.4.16 */
template (value) RejectCause ts_SGsAP_IE_RejCause(template (value) Reject_Cause cause) := {
	iEI := '00001111'B,
	lengthIndicator := 1,
	cause := cause
}
template RejectCause tr_SGsAP_IE_RejCause(template Reject_Cause cause) := {
	iEI := '00001111'B,
	lengthIndicator := 1,
	cause := cause
}

/* 9.4.17 */
template (value) ServiceIndicator ts_SGsAP_ServiceInd(template (value) Service_Indicator si) := {
	iEI := '00100000'B,
	lengthIndicator := 1,
	indicator := si
}
template ServiceIndicator tr_SGsAP_ServiceInd(template Service_Indicator si) := {
	iEI := '00100000'B,
	lengthIndicator := 1,
	indicator := si
}


/* 9.4.18 */
template (value) SGsCause ts_SGsAP_IE_SgsCause(template (value) SGs_Cause cause) := {
	iEI := '00001000'B,
	lengthIndicator := 1,
	cause := cause
}
function tr_SGsAP_IE_SgsCause(template SGs_Cause cause) return template SGsCause {
	if (istemplatekind(cause, "omit")) {
		return omit;
	} else if (istemplatekind(cause, "*")) {
		return *;
	} else {
		var template SGsCause ret := {
			iEI := '00001000'B,
			lengthIndicator := 1,
			cause := cause
		}
		return ret;
	}
}

/* 9.4.21c */
template (value) UE_EMM_Mode ts_SGsAP_IE_UeEmmMode(template (value) UE_EMM_mode mode) := {
	iEI := '00100101'B,
	lengthIndicator := 1,
	mode := mode
}
template UE_EMM_Mode tr_SGsAP_IE_UeEmmMode(template UE_EMM_mode mode) := {
	iEI := '00100101'B,
	lengthIndicator := 1,
	mode := mode
}


/* 9.4.22 */
template (value) VLR_Name ts_SGsAP_IE_VlrName(template (value) octetstring name) := {
	iEI := '00000010'B,
	lengthIndicator := lengthof(name),
	name := name
}
template VLR_Name tr_SGsAP_IE_VlrName(template octetstring name) := {
	iEI := '00000010'B,
	lengthIndicator := ?,
	name := name
}





/* 3GPP TS 29.118 Section 8.1 */
template (value) PDU_SGsAP ts_SGsAP_ALERT_ACK(hexstring imsi) := {
	sGsAP_ALERT_ACK := {
		messageType := '00001110'B,
		iMSI := ts_SGsAP_IMSI(imsi)
	}
}
template PDU_SGsAP tr_SGsAP_ALERT_ACK(template hexstring imsi) := {
	sGsAP_ALERT_ACK := {
		messageType := '00001110'B,
		iMSI := tr_SGsAP_IMSI(imsi)
	}
}

/* 3GPP TS 29.118 Section 8.2 */
template PDU_SGsAP ts_SGsAP_ALERT_REJECT(hexstring imsi,
					 template (value) SGs_Cause cause) := {
	sGsAP_ALERT_REJECT := {
		messageType := '00001111'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		sGsCause := ts_SGsAP_IE_SgsCause(cause)
	}
}
template PDU_SGsAP tr_SGsAP_ALERT_REJECT(template hexstring imsi,
					 template SGs_Cause cause := ?) := {
	sGsAP_ALERT_REJECT := {
		messageType := '00001111'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		sGsCause := tr_SGsAP_IE_SgsCause(cause)
	}
}

/* 3GPP TS 29.118 Section 8.3 */
template (value) PDU_SGsAP ts_SGsAP_ALERT_REQ(hexstring imsi) := {
	sGsAP_ALERT_REQUEST := {
		messageType := '00001101'B,
		iMSI := ts_SGsAP_IMSI(imsi)
	}
}
template PDU_SGsAP tr_SGsAP_ALERT_REQ(template hexstring imsi) := {
	sGsAP_ALERT_REQUEST := {
		messageType := '00001101'B,
		iMSI := tr_SGsAP_IMSI(imsi)
	}
}

/* 3GPP TS 29.118 Section 8.4 */
template (value) PDU_SGsAP ts_SGsAP_DL_UD(hexstring imsi,
					  template (value) octetstring nas_pdu) := {
	sGsAP_DOWNLINK_UNITDATA := {
		messageType := '00000111'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		nAS_MessageContainer := ts_SGsAP_NasContainer(nas_pdu)
	}
}
template PDU_SGsAP tr_SGsAP_DL_UD(template hexstring imsi, template octetstring nas_pdu := ?) := {
	sGsAP_DOWNLINK_UNITDATA := {
		messageType := '00000111'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		nAS_MessageContainer := tr_SGsAP_NasContainer(nas_pdu)
	}
}

/* 8.5 */
template (value) PDU_SGsAP ts_SGsAP_EPS_DETACH_ACK(hexstring imsi) := {
	sGsAP_EPS_DETACH_ACK:= {
		messageType := '00010010'B,
		iMSI := ts_SGsAP_IMSI(imsi)
	}
}
template PDU_SGsAP tr_SGsAP_EPS_DETACH_ACK(template hexstring imsi) := {
	sGsAP_EPS_DETACH_ACK:= {
		messageType := '00010010'B,
		iMSI := tr_SGsAP_IMSI(valueof(imsi))
	}
}

/* 8.6 */
template (value) PDU_SGsAP ts_SGsAP_EPS_DETACH_IND(hexstring imsi,
						   template (value) octetstring mme_name,
						   template (value) IMSI_detachFromEPS_serviceType det_serv_typ) := {
	sGsAP_EPS_DETACH_INDICATION:= {
		messageType := '00010001'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		mME_Name := ts_SGsAP_IE_MmeName(mme_name),
		iMSI_DetachFromEPS_ServiceType := ts_SGsAP_IE_DetServiceType(det_serv_typ)
	}
}
template PDU_SGsAP tr_SGsAP_EPS_DETACH_IND(template hexstring imsi,
					   template octetstring mme_name,
					   template IMSI_detachFromEPS_serviceType det_serv_typ) := {
	sGsAP_EPS_DETACH_INDICATION:= {
		messageType := '00010001'B,
		iMSI := tr_SGsAP_IMSI(valueof(imsi)),
		mME_Name := tr_SGsAP_IE_MmeName(mme_name),
		iMSI_DetachFromEPS_ServiceType := tr_SGsAP_IE_DetServiceType(det_serv_typ)
	}
}

/* 8.7 */
template (value) PDU_SGsAP ts_SGsAP_IMSI_DETACH_ACK(hexstring imsi) := {
	sGsAP_IMSI_DETACH_ACK := {
		messageType := '00010100'B,
		iMSI := ts_SGsAP_IMSI(imsi)
	}
}
template PDU_SGsAP tr_SGsAP_IMSI_DETACH_ACK(template hexstring imsi) := {
	sGsAP_IMSI_DETACH_ACK := {
		messageType := '00010100'B,
		iMSI := tr_SGsAP_IMSI(imsi)
	}
}

/* 8.8 */
template (value) PDU_SGsAP ts_SGsAP_IMSI_DETACH_IND(hexstring imsi,
						    template (value) octetstring mme_name,
						    template (value) IMSI_detachFromNonEPS_serviceType det_serv_typ) := {
	sGsAP_IMSI_DETACH_INDICATION := {
		messageType := '00010011'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		mME_Name := ts_SGsAP_IE_MmeName(mme_name),
		iMSI_DetachFromNonEPS_ServiceType := ts_SGsAP_IE_DetNonEpsServiceType(det_serv_typ)
	}
}
template PDU_SGsAP tr_SGsAP_IMSI_DETACH_IND(template hexstring imsi,
					    template octetstring mme_name := ?,
					    template IMSI_detachFromNonEPS_serviceType det_serv_typ := ?) := {
	sGsAP_IMSI_DETACH_INDICATION := {
		messageType := '00010011'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		mME_Name := tr_SGsAP_IE_MmeName(mme_name),
		iMSI_DetachFromNonEPS_ServiceType := tr_SGsAP_IE_DetNonEpsServiceType(det_serv_typ)
	}
}

/* 8.9 */
template (value) PDU_SGsAP ts_SGsAP_LU_ACCEPT(hexstring imsi,
					      template (value) LocationAreaIdValue lai,
					      template (value) MobileIdentityLV mobile_id) := {
	sGsAP_LOCATION_UPDATE_ACCEPT := {
		messageType := '00001010'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		locationAreaId := ts_SGsAP_IE_Lai(lai),
		newTMSIorIMSI := ts_SGsAP_IE_MobileID(mobile_id)
	}
}
template PDU_SGsAP tr_SGsAP_LU_ACCEPT(template hexstring imsi,
				      template LocationAreaIdValue lai := ?) := {
	sGsAP_LOCATION_UPDATE_ACCEPT := {
		messageType := '00001010'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		locationAreaId := tr_SGsAP_IE_Lai(lai),
		newTMSIorIMSI := *
	}
}

/* 8.10 */
template (value) PDU_SGsAP ts_SGsAP_LU_REJECT(hexstring imsi,
					      template (value) Reject_Cause rej_cause,
					      template (omit) LocationAreaIdValue lai) := {
	sGsAP_LOCATION_UPDATE_REJECT := {
		messageType := '00001011'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		rejectCause := ts_SGsAP_IE_RejCause(rej_cause),
		locationAreaId := ts_SGsAP_IE_Lai_omit(lai)
	}
}
template PDU_SGsAP tr_SGsAP_LU_REJECT(template hexstring imsi,
				      template Reject_Cause rej_cause,
				      template LocationAreaIdValue lai) := {
	sGsAP_LOCATION_UPDATE_REJECT := {
		messageType := '00001011'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		rejectCause := tr_SGsAP_IE_RejCause(rej_cause),
		locationAreaId := tr_SGsAP_IE_Lai_omit(lai)
	}
}

/* 8.11 */
template (value) PDU_SGsAP ts_SGsAP_LU_REQ(hexstring imsi,
					   template (value) octetstring mme_name,
					   template (value) EPS_location_update_type eps_lu_type,
					   template (value) LocationAreaIdValue new_lai,
					   template (omit) TrackingAreaIdentityValue tAI := omit) := {
	sGsAP_LOCATION_UPDATE_REQUEST := {
		messageType := '00001001'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		mME_Name := ts_SGsAP_IE_MmeName(mme_name),
		ePS_LocationUpdateType := ts_SGsAP_IE_EpsLuType(eps_lu_type),
		newLocationAreaId := ts_SGsAP_IE_Lai(new_lai),
		oldLocationAreaId := omit,
		tMSI_Status := omit,
		iMEI_SV := omit,
		tAI := ts_SGsAP_IE_TAI_omit(tAI),
		eCGI := omit,
		tMSI_NRI := omit,
		cS_DomainOperator := omit
	}
}
template PDU_SGsAP tr_SGsAP_LU_REQ(template hexstring imsi,
				   template octetstring mme_name := ?,
				   template EPS_location_update_type eps_lu_type := ?,
				   template LocationAreaIdValue new_lai := ?) := {
	sGsAP_LOCATION_UPDATE_REQUEST := {
		messageType := '00001001'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		mME_Name := tr_SGsAP_IE_MmeName(mme_name),
		ePS_LocationUpdateType := tr_SGsAP_IE_EpsLuType(eps_lu_type),
		newLocationAreaId := tr_SGsAP_IE_Lai_omit(new_lai),
		oldLocationAreaId := *,
		tMSI_Status := *,
		iMEI_SV := *,
		tAI := *,
		eCGI := *,
		tMSI_NRI := *,
		cS_DomainOperator := *
	}
}

/* 8.12 */
template (value) PDU_SGsAP ts_SGsAP_MM_INFO_REQ(hexstring imsi,
						template (value) octetstring mm_info) := {
	sGsAP_MM_INFORMATION_REQUEST := {
		messageType := '00011010'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		mM_Information := ts_SGsAP_IE_MmInfo(mm_info)
	}
}
template PDU_SGsAP tr_SGsAP_MM_INFO_REQ(template hexstring imsi,
					template octetstring mm_info :=?) := {
	sGsAP_MM_INFORMATION_REQUEST := {
		messageType := '00011010'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		mM_Information := tr_SGsAP_IE_MmInfo(mm_info)
	}
}

/* 8.13 */
template (value) PDU_SGsAP ts_SGsAP_PAGING_REJ(hexstring imsi,
						template (value) SGs_Cause cause) := {
	sGsAP_PAGING_REJECT := {
		messageType := '00000010'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		sGsCause := ts_SGsAP_IE_SgsCause(cause)
	}
}
template PDU_SGsAP tr_SGsAP_PAGING_REJ(template hexstring imsi,
					template SGs_Cause cause := ?) := {
	sGsAP_PAGING_REJECT := {
		messageType := '00000010'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		sGsCause := tr_SGsAP_IE_SgsCause(cause)
	}
}

/* 8.14 */
template (value) PDU_SGsAP ts_SGsAP_PAGING_REQ(hexstring imsi,
						template (value) octetstring vlr_name,
						template (value) Service_Indicator serv_ind,
						template (omit) OCT4 tmsi) :=
{
	sGsAP_PAGING_REQUEST := {
		messageType := '00000001'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		vLR_Name := ts_SGsAP_IE_VlrName(vlr_name),
		serviceIndicator := ts_SGsAP_ServiceInd(serv_ind),
		tMSI := omit,
		cLI := omit,
		locationAreaId := omit,
		globalCN_Id := omit,
		sS_Code := omit,
		lCS_Indicator := omit,
		lCS_ClientIdentity := omit,
		channelNeeded := omit,
		eMLPP_Priority := omit,
		additionalPagingIndicator := omit
	}
}
template PDU_SGsAP tr_SGsAP_PAGING_REQ(template hexstring imsi,
					template octetstring vlr_name,
					template Service_Indicator serv_ind,
					template OCT4 tmsi) :=
{
	sGsAP_PAGING_REQUEST := {
		messageType := '00000001'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		vLR_Name := tr_SGsAP_IE_VlrName(vlr_name),
		serviceIndicator := tr_SGsAP_ServiceInd(serv_ind),
		tMSI := *,
		cLI := *,
		locationAreaId := *,
		globalCN_Id := *,
		sS_Code := *,
		lCS_Indicator := *,
		lCS_ClientIdentity := *,
		channelNeeded := *,
		eMLPP_Priority := *,
		additionalPagingIndicator := *
	}
}

/* 8.15 */
template (value) PDU_SGsAP ts_SGsAP_RESET_ACK_MME(template (value) octetstring mme_name) := {
	sGsAP_RESET_ACK := {
		messageType := '00010110'B,
		mME_Name := ts_SGsAP_IE_MmeName(mme_name),
		vLR_Name := omit
	}
}
template (value) PDU_SGsAP ts_SGsAP_RESET_ACK_VLR(template (value) octetstring vlr_name) := {
	sGsAP_RESET_ACK := {
		messageType := '00010110'B,
		mME_Name := omit,
		vLR_Name := ts_SGsAP_IE_VlrName(vlr_name)
	}
}
template PDU_SGsAP tr_SGsAP_RESET_ACK(template octetstring mme_name, template octetstring vlr_name) := {
	sGsAP_RESET_ACK := {
		messageType := '00010110'B,
		mME_Name := tr_SGsAP_IE_MmeName(mme_name),
		vLR_Name := tr_SGsAP_IE_VlrName(vlr_name)
	}
}

/* 8.16 */
template (value) PDU_SGsAP ts_SGsAP_RESET_IND_MME(template (value) octetstring mme_name) := {
	sGsAP_RESET_INDICATION := {
		messageType := '00010101'B,
		mME_Name := ts_SGsAP_IE_MmeName(mme_name),
		vLR_Name := omit
	}
}
template (value) PDU_SGsAP ts_SGsAP_RESET_IND_VLR(template (value) octetstring vlr_name) := {
	sGsAP_RESET_INDICATION := {
		messageType := '00010101'B,
		mME_Name := omit,
		vLR_Name := ts_SGsAP_IE_VlrName(vlr_name)
	}
}

template PDU_SGsAP tr_SGsAP_RESET_IND_MME(template octetstring mme_name) := {
	sGsAP_RESET_INDICATION := {
		messageType := '00010101'B,
		mME_Name := tr_SGsAP_IE_MmeName(mme_name),
		vLR_Name := omit
	}
}
template PDU_SGsAP tr_SGsAP_RESET_IND_VLR(template octetstring vlr_name) := {
	sGsAP_RESET_INDICATION := {
		messageType := '00010101'B,
		mME_Name := omit,
		vLR_Name := tr_SGsAP_IE_VlrName(vlr_name)
	}
}

/* 8.17 */
template (value) PDU_SGsAP ts_SGsAP_SERVICE_REQ(hexstring imsi,
						template (value) Service_Indicator serv_ind,
					        template (value) UE_EMM_mode emm_mode) := {
	sGsAP_SERVICE_REQUEST := {
		messageType := '00000110'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		serviceIndicator := ts_SGsAP_ServiceInd(serv_ind),
		iMEI_SV := omit,
		uE_TimeZone := omit,
		mobileStationClassmark2 := omit,
		tAI := omit,
		eCGI := omit,
		/* optional, but "the MME shall include this IE." */
		uE_EMM_Mode := ts_SGsAP_IE_UeEmmMode(emm_mode)
	}
}
template PDU_SGsAP tr_SGsAP_SERVICE_REQ(template hexstring imsi,
					template Service_Indicator serv_ind := ?,
				        template UE_EMM_mode emm_mode := ?) := {
	sGsAP_SERVICE_REQUEST := {
		messageType := '00000110'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		serviceIndicator := tr_SGsAP_ServiceInd(serv_ind),
		iMEI_SV := *,
		uE_TimeZone := *,
		mobileStationClassmark2 := *,
		tAI := *,
		eCGI := *,
		/* optional, but "the MME shall include this IE." */
		uE_EMM_Mode := tr_SGsAP_IE_UeEmmMode(emm_mode)
	}
}


/* 8.18 */
template (value) PDU_SGsAP ts_SGsAP_STATUS(template (omit) hexstring imsi,
					   template (value) SGs_Cause cause,
					   template (value) octetstring err_msg) := {
	sGsAP_STATUS := {
		messageType := '00011101'B,
		iMSI := ts_SGsAP_IMSI_omit(imsi),
		sGsCause := ts_SGsAP_IE_SgsCause(cause),
		erroneousMessage := ts_SGsAP_IE_ErrMsg(err_msg)
	}
}
template PDU_SGsAP tr_SGsAP_STATUS(template hexstring imsi,
				   template SGs_Cause cause,
				   template octetstring err_msg) := {
	sGsAP_STATUS := {
		messageType := '00011101'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		sGsCause := tr_SGsAP_IE_SgsCause(cause),
		erroneousMessage := tr_SGsAP_IE_ErrMsg(err_msg)
	}
}


/* 8.19 */
template (value) PDU_SGsAP ts_SGsAP_TMSI_REALL_CMPL(hexstring imsi) := {
	sGsAP_TMSI_REALLOCATION_COMPLETE := {
		messageType := '00001100'B,
		iMSI := ts_SGsAP_IMSI(imsi)
	}
}
template PDU_SGsAP tr_SGsAP_TMSI_REALL_CMPL(template hexstring imsi) := {
	sGsAP_TMSI_REALLOCATION_COMPLETE := {
		messageType := '00001100'B,
		iMSI := tr_SGsAP_IMSI(imsi)
	}
}

/* 8.20 */
template (value) PDU_SGsAP ts_SGsAP_UE_ACT_IND(hexstring imsi) := {
	sGsAP_UE_ACTIVITY_INDICATION := {
		messageType := '00010000'B,
		iMSI := ts_SGsAP_IMSI(imsi)
		/* Rel 14: Max UE Avail Time */
	}
}
template PDU_SGsAP tr_SGsAP_UE_ACT_IND(template hexstring imsi) := {
	sGsAP_UE_ACTIVITY_INDICATION := {
		messageType := '00010000'B,
		iMSI := tr_SGsAP_IMSI(imsi)
		/* Rel 14: Max UE Avail Time */
	}
}

/* 8.21 */
template (value) PDU_SGsAP ts_SGsAP_UE_UNREACHABLE(hexstring imsi,
						   template (value) SGs_Cause cause) := {
	sGsAP_UE_UNREACHABLE := {
		messageType := '00011111'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		sGsCause := ts_SGsAP_IE_SgsCause(cause)
		/* Rel 14: Requested Retransmission Time */
		/* Rel 14: Additional UE Unreachable indicators */
	}
}
template PDU_SGsAP tr_SGsAP_UE_UNREACHABLE(template hexstring imsi,
					   template SGs_Cause cause := ?) := {
	sGsAP_UE_UNREACHABLE := {
		messageType := '00011111'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		sGsCause := tr_SGsAP_IE_SgsCause(cause)
		/* Rel 14: Requested Retransmission Time */
		/* Rel 14: Additional UE Unreachable indicators */
	}
}

/* 8.22 */
template (value) PDU_SGsAP ts_SGsAP_UL_UD(hexstring imsi,
					  template (value) octetstring nas_msg) := {
	sGsAP_UPLINK_UNITDATA := {
		messageType := '00001000'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		nAS_MessageContainer := ts_SGsAP_NasContainer(nas_msg),
		iMEI_SV := omit,
		uE_TimeZone := omit,
		mobileStationClassmark2 := omit,
		tAI := omit,
		eCGI := omit
	}
}
template PDU_SGsAP tr_SGsAP_UL_UD(template hexstring imsi,
				  template octetstring nas_msg := ?) := {
	sGsAP_UPLINK_UNITDATA := {
		messageType := '00001000'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		nAS_MessageContainer := tr_SGsAP_NasContainer(nas_msg),
		iMEI_SV := *,
		uE_TimeZone := *,
		mobileStationClassmark2 := *,
		tAI := *,
		eCGI := *
	}
}

/* 8.23 */
template (value) PDU_SGsAP ts_SGsAP_RELEASE_REQ(hexstring imsi,
						template (value) SGs_Cause cause) := {
	sGsAP_RELEASE_REQUEST := {
		messageType := '00011011'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		sGsCause := ts_SGsAP_IE_SgsCause(cause)
	}
}
template PDU_SGsAP tr_SGsAP_RELEASE_REQ(template hexstring imsi,
					template SGs_Cause cause) := {
	sGsAP_RELEASE_REQUEST := {
		messageType := '00011011'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		sGsCause := tr_SGsAP_IE_SgsCause(cause)
	}
}

/* 8.24 */
template (value) PDU_SGsAP ts_SGsAP_SERVICE_ABORT_REQ(hexstring imsi) := {
	sGsAP_SERVICE_ABORT_REQUEST := {
		messageType := '00010111'B,
		iMSI := ts_SGsAP_IMSI(imsi)
	}
}
template PDU_SGsAP tr_SGsAP_SERVICE_ABORT_REQ(template hexstring imsi) := {
	sGsAP_SERVICE_ABORT_REQUEST := {
		messageType := '00010111'B,
		iMSI := tr_SGsAP_IMSI(imsi)
	}
}

/* 8.25 */
template (value) PDU_SGsAP ts_SGsAP_MO_CSFB_IND(hexstring imsi) := {
	sGsAP_MO_CSFB_INDICATION := {
		messageType := '00011000'B,
		iMSI := ts_SGsAP_IMSI(imsi),
		tAI := omit,
		eCGI := omit
	}
}
template PDU_SGsAP tr_SGsAP_MO_CSFB_IND(template hexstring imsi) := {
	sGsAP_MO_CSFB_INDICATION := {
		messageType := '00011000'B,
		iMSI := tr_SGsAP_IMSI(imsi),
		tAI := *,
		eCGI := *
	}
}






}