module PCO_Types {

/* PCO_Types, defining abstract TTCN-3 data types for the Protocol Configuration Options (PCO).
 *
 * (C) 2024 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * 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
 */

/* 3GPP TS 24.008 10.5.6.3, 3GPP TS 29.060 7.7.31 */

import from General_Types all;
import from Osmocom_Types all;

type enumerated PCO_P {
	PCO_P_LCP			('C021'O),
	PCO_P_PAP			('C023'O),
	PCO_P_CHAP			('C223'O),
	PCO_P_IPCP			('8021'O),
	PCO_P_PCSCF_ADDR		('0001'O),
	PCO_P_IM_CN_SS_F		('0002'O),
	PCO_P_DNS_IPv6_ADDR		('0003'O),
	PCO_P_POLICY_CTRL_REJ		('0004'O),	/* only in Network->MS */
	PCO_P_MS_SUP_NETREQ_BCI		('0005'O),
	/* reserved */
	PCO_P_DSMIPv6_HA_ADDR		('0007'O),
	PCO_P_DSMIPv6_HN_PREF		('0008'O),
	PCO_P_DSMIPv6_v4_HA_ADDR	('0009'O),
	PCO_P_IP_ADDR_VIA_NAS		('000a'O),	/* only MS->Network */
	PCO_P_IPv4_ADDR_VIA_DHCP	('000b'O),	/* only MS->Netowrk */
	PCO_P_PCSCF_IPv4_ADDR		('000c'O),
	PCO_P_DNS_IPv4_ADDR		('000d'O),
	PCO_P_MSISDN			('000e'O),
	PCO_P_IFOM_SUPPORT		('000f'O),
	PCO_P_IPv4_LINK_MTU		('0010'O),
	PCO_P_MS_SUPP_LOC_A_TFT		('0011'O),
	PCO_P_PCSCF_RESEL_SUP		('0012'O),	/* only MS->Network */
	PCO_P_NBIFOM_REQ		('0013'O),
	PCO_P_NBIFOM_MODE		('0014'O),
	PCO_P_NONIP_LINK_MTU		('0015'O),
	PCO_P_APN_RATE_CTRL_SUP		('0016'O),
	PCO_P_PS_DATA_OFF_UE		('0017'O),
	PCO_P_REL_DATA_SVC		('0018'O)
} with { variant "FIELDLENGTH(16)";
	 variant "BYTEORDER(last)" };

function PCO_P_to_OCT2(PCO_P p) return OCT2 {
	return int2oct(enum2int(p), 2);
}

type record ProtocolElementU16 {
	uint16_t	val
};

type union ProtocolElementPayloadUnion {
	ProtocolElementU16	u16,
	octetstring		octstr
};

type record ProtocolElement {
	PCO_P		protocolID,
	uint8_t		lengthProtoID,
	ProtocolElementPayloadUnion u
} with { variant (lengthProtoID) "LENGTHTO(u)"
	 variant (u) "CROSSTAG(u16, protocolID = PCO_P_IPv4_LINK_MTU;
			       octstr,	OTHERWISE)"
	};
type set of ProtocolElement ProtocolIDList;

type record PCO_DATA {
	BIT1		extension0,
	BIT4		spare,
	BIT3	 	configProtocol,
	ProtocolIDList	protocols
};

external function enc_PCO_DATA(in PCO_DATA pco_data) return octetstring
with { extension "prototype(convert)" extension "encode(RAW)" }

external function dec_PCO_DATA(in octetstring pco_payload) return PCO_DATA
with { extension "prototype(convert) decode(RAW)" };

/**********************
 * PCO_Templates:
 **********************/

/* Union value (payload) templates */
template (value) ProtocolElementU16 ts_PCO_P_UV_U16(template (value) uint16_t val)
:= { val := val }
template (present) ProtocolElementU16 tr_PCO_P_UV_U16(template (present) uint16_t val := ?)
:= { val := val }

/* Union templates */
template (value) ProtocolElementPayloadUnion ts_PCO_P_U_U16(template (value) uint16_t val)
:= { u16 := ts_PCO_P_UV_U16(val) }
template (present) ProtocolElementPayloadUnion tr_PCO_P_U_U16(template (present) uint16_t val := ?)
:= { u16 := tr_PCO_P_UV_U16(val) }

template (value) ProtocolElementPayloadUnion ts_PCO_P_U_OCTSTR(template (value) octetstring protoIDContents := ''O)
:= { octstr := protoIDContents }
template (present) ProtocolElementPayloadUnion tr_PCO_P_U_OCTSTR(template (present) octetstring protoIDContents := ?)
:= { octstr := protoIDContents }

/* PCOElement templates */
template (value) ProtocolElement ts_PCO_P_U16(template (value) PCO_P protocolID,
					      template (value) uint16_t val)
:= {
	protocolID := protocolID,
	lengthProtoID := 2,
	u := ts_PCO_P_U_U16(val)
}
template (present) ProtocolElement tr_PCO_P_U16(template (present) PCO_P protocolID := ?,
					        template (present) uint16_t val := ?)
:= {
	protocolID := protocolID,
	lengthProtoID := 2,
	u := tr_PCO_P_U_U16(val)
}

template (value) ProtocolElement ts_PCO_P_OCTSTR(template (value) PCO_P protocolID,
					  template (value) uint8_t lengthProtoID := 0,
					  template (value) octetstring protoIDContents := ''O)
:= {
	protocolID := protocolID,
	lengthProtoID := lengthProtoID,
	u := ts_PCO_P_U_OCTSTR(protoIDContents)
}

template (present) ProtocolElement tr_PCO_P_OCTSTR(template (present) PCO_P protocolID := ?,
					    template (present) uint8_t lengthProtoID := ?,
					    template (present) octetstring protoIDContents := ?)
:= {
	protocolID := protocolID,
	lengthProtoID := lengthProtoID,
	u := tr_PCO_P_U_OCTSTR(protoIDContents)
}


template (value) ProtocolElement ts_PCO_P_IPv4_LINK_MTU(template (value) uint16_t mtu) :=
	ts_PCO_P_U16(PCO_P_IPv4_LINK_MTU, mtu);
template (present) ProtocolElement tr_PCO_P_IPv4_LINK_MTU(template (present) uint16_t mtu := ?) :=
	tr_PCO_P_U16(PCO_P_IPv4_LINK_MTU, mtu);

template (value) ProtocolElement ts_PCO_P_DNS_IPv4(template (value) octetstring dns4 := ''O) :=
	ts_PCO_P_OCTSTR(PCO_P_DNS_IPv4_ADDR, protoIDContents := dns4);
template (present) ProtocolElement tr_PCO_P_DNS_IPv4(template (present) octetstring dns4 := ?) :=
	tr_PCO_P_OCTSTR(PCO_P_DNS_IPv4_ADDR, protoIDContents := dns4);

template (value) ProtocolElement ts_PCO_P_DNS_IPv6(template (value) octetstring dns6 := ''O) :=
	ts_PCO_P_OCTSTR(PCO_P_DNS_IPv6_ADDR, protoIDContents := dns6);
template (present) ProtocolElement tr_PCO_P_DNS_IPv6(template (present) octetstring dns6 := ?) :=
	tr_PCO_P_OCTSTR(PCO_P_DNS_IPv6_ADDR, protoIDContents := dns6);

template (value) ProtocolElement ts_PCO_P_PCSCF_IPv4(template (value) octetstring pcscf4 := ''O) :=
	ts_PCO_P_OCTSTR(PCO_P_PCSCF_IPv4_ADDR, protoIDContents := pcscf4);
template (present) ProtocolElement tr_PCO_P_PCSCF_IPv4(template (present) octetstring pcscf4 := ?) :=
	tr_PCO_P_OCTSTR(PCO_P_PCSCF_IPv4_ADDR, protoIDContents := pcscf4);

template (value) ProtocolElement ts_PCO_P_PCSCF_IPv6(template (value) octetstring pcscf6 := ''O) :=
	ts_PCO_P_OCTSTR(PCO_P_PCSCF_ADDR, protoIDContents := pcscf6);
template (present) ProtocolElement tr_PCO_P_PCSCF_IPv6(template (present) octetstring pcscf6 := ?) :=
	tr_PCO_P_OCTSTR(PCO_P_PCSCF_ADDR, protoIDContents := pcscf6);

/* PCO send base template */
template (value) PCO_DATA ts_PCO(template (value) ProtocolIDList protocols := {}) := {
	extension0 := '1'B,
	spare := '0000'B,
	configProtocol := '000'B,
	protocols := protocols
}
/* PCO receive base template */
template (present) PCO_DATA tr_PCO(template (present) ProtocolIDList protocols := ?) := {
	extension0 := '1'B,
	spare := ?,
	configProtocol := '000'B,
	protocols := protocols
}

template (value) PCO_DATA ts_PCO_IPv4_DNS(template (value) octetstring dns4 := ''O) :=
	ts_PCO({ ts_PCO_P_DNS_IPv4(dns4) });

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