module SS_Templates {

/* SS/USSD Facility IE templates, building on top of SS_Types.
 *
 * (C) 2018 by 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
 */

import from General_Types all;
import from SS_Types all;

/* GSM TS 04.80, section 4.5, SS operation code */
type enumerated SS_Op_Code {
	SS_OP_CODE_REGISTER_SS			('0A'O),
	SS_OP_CODE_ERASE_SS			('0B'O),
	SS_OP_CODE_ACTIVATE_SS			('0C'O),
	SS_OP_CODE_DEACTIVATE_SS		('0D'O),
	SS_OP_CODE_INTERROGATE_SS		('0E'O),
	SS_OP_CODE_NOTIFY_SS			('10'O),
	SS_OP_CODE_REGISTER_PASSWORD		('11'O),
	SS_OP_CODE_GET_PASSWORD			('12'O),
	SS_OP_CODE_PROCESS_USS_DATA		('13'O),
	SS_OP_CODE_FORWARD_CHECK_SS_IND		('26'O),
	SS_OP_CODE_PROCESS_USS_REQ		('3B'O),
	SS_OP_CODE_USS_REQUEST			('3C'O),
	SS_OP_CODE_USS_NOTIFY			('3D'O),
	SS_OP_CODE_FORWARD_CUG_INFO		('78'O),
	SS_OP_CODE_SPLIT_MPTY			('79'O),
	SS_OP_CODE_RETRIEVE_MPTY		('7A'O),
	SS_OP_CODE_HOLD_MPTY			('7B'O),
	SS_OP_CODE_BUILD_MPTY			('7C'O),
	SS_OP_CODE_FORWARD_CHARGE_ADVICE	('7D'O)
}

/* GSM TS 04.80, section 4.5, SS error code */
type enumerated SS_Err_Code {
	SS_ERR_CODE_UNKNOWN_SUBSCRIBER			('01'O),
	SS_ERR_CODE_ILLEGAL_SUBSCRIBER			('09'O),
	SS_ERR_CODE_BEARER_SERVICE_NOT_PROVISIONED	('0A'O),
	SS_ERR_CODE_TELESERVICE_NOT_PROVISIONED		('0B'O),
	SS_ERR_CODE_ILLEGAL_EQUIPMENT			('0C'O),
	SS_ERR_CODE_CALL_BARRED				('0D'O),
	SS_ERR_CODE_ILLEGAL_SS_OPERATION		('10'O),
	SS_ERR_CODE_SS_ERROR_STATUS			('11'O),
	SS_ERR_CODE_SS_NOT_AVAILABLE			('12'O),
	SS_ERR_CODE_SS_SUBSCRIPTION_VIOLATION		('13'O),
	SS_ERR_CODE_SS_INCOMPATIBILITY			('14'O),
	SS_ERR_CODE_FACILITY_NOT_SUPPORTED		('15'O),
	SS_ERR_CODE_ABSENT_SUBSCRIBER			('1B'O),
	SS_ERR_CODE_SYSTEM_FAILURE			('22'O),
	SS_ERR_CODE_DATA_MISSING			('23'O),
	SS_ERR_CODE_UNEXPECTED_DATA_VALUE		('24'O),
	SS_ERR_CODE_PW_REGISTRATION_FAILURE		('25'O),
	SS_ERR_CODE_NEGATIVE_PW_CHECK			('26'O),
	SS_ERR_CODE_NUM_PW_ATTEMPTS_VIOLATION		('2B'O),
	SS_ERR_CODE_UNKNOWN_ALPHABET			('47'O),
	SS_ERR_CODE_USSD_BUSY				('48'O),
	SS_ERR_CODE_MAX_MPTY_PARTICIPANTS		('7E'O),
	SS_ERR_CODE_RESOURCES_NOT_AVAILABLE		('7F'O)
}

/**
 * According to GSM TS 02.90, section 4.1.1,
 * the mobile initiated USSD request shall contain
 * an alphabet indicator set to "SMS default alphabet"
 * and language indicator set to "language unspecified".
 *
 * This DCS value is used quite often, so it makes
 * sense to define it here as a constant.
 */
const octetstring SS_USSD_DEFAULT_DCS := '0F'O;

template (value) SS_FacilityInformation ts_SS_USSD_FACILITY_INVOKE(
	integer invoke_id := 1,
	SS_Op_Code op_code := SS_OP_CODE_PROCESS_USS_REQ,
	octetstring ussd_dcs := SS_USSD_DEFAULT_DCS,
	octetstring ussd_string := 'AA180C3602'O // *#100#
) := {
	{
		invoke := {
			invokeId := { present_ := invoke_id },
			linkedId := omit,
			opcode := {
				local := enum2int(op_code)
			},
			argument := {
				uSSD_Arg := {
					ussd_DataCodingScheme := ussd_dcs,
					ussd_String := ussd_string,
					alertingPattern := omit,
					msisdn := omit
				}
			}
		}
	}
}
template SS_FacilityInformation tr_SS_USSD_FACILITY_INVOKE(
	template integer invoke_id := ?,
	template integer op_code := ?,
	template octetstring ussd_dcs := ?,
	template octetstring ussd_string := ?
) := {
	{
		invoke := {
			invokeId := { present_ := invoke_id },
			linkedId := omit,
			opcode := {
				local := op_code
			},
			argument := {
				uSSD_Arg := {
					ussd_DataCodingScheme := ussd_dcs,
					ussd_String := ussd_string,
					alertingPattern := omit,
					msisdn := omit
				}
			}
		}
	}
}

template (value) SS_FacilityInformation ts_SS_FACILITY_INVOKE(
	integer invoke_id := 1,
	SS_Op_Code op_code,
	OCT1 ss_code
) := {
	{
		invoke := {
			invokeId := { present_ := invoke_id },
			linkedId := omit,
			opcode := { local := enum2int(op_code) },
			argument := {
				sS_ForBS_Code := {
					ss_Code := ss_code,
					basicService := omit,
					longFTN_Supported := omit
				}
			}
		}
	}
}
template SS_FacilityInformation tr_SS_FACILITY_INVOKE(
	template integer invoke_id := 1,
	template integer op_code := ?,
	template OCT1 ss_code := ?
) := {
	{
		invoke := {
			invokeId := { present_ := invoke_id },
			linkedId := omit,
			opcode := { local := op_code },
			argument := {
				sS_ForBS_Code := {
					ss_Code := ss_code,
					basicService := omit,
					longFTN_Supported := omit
				}
			}
		}
	}
}

template (value) SS_FacilityInformation ts_SS_USSD_FACILITY_RETURN_RESULT(
	integer invoke_id := 1,
	SS_Op_Code op_code := SS_OP_CODE_PROCESS_USS_REQ,
	octetstring ussd_dcs,
	octetstring ussd_string
) := {
	{
		returnResult := {
			invokeId := { present_ := invoke_id },
			result := {
				opcode := {
					local := enum2int(op_code)
				},
				result := {
					uSSD_Res := {
						ussd_DataCodingScheme := ussd_dcs,
						ussd_String := ussd_string
					}
				}
			}
		}
	}
}
template SS_FacilityInformation tr_SS_USSD_FACILITY_RETURN_RESULT(
	template integer invoke_id := ?,
	template integer op_code := ?,
	template octetstring ussd_dcs := ?,
	template octetstring ussd_string := ?
) := {
	{
		returnResult := {
			invokeId := { present_ := invoke_id },
			result := {
				opcode := {
					local := op_code
				},
				result := {
					uSSD_Res := {
						ussd_DataCodingScheme := ussd_dcs,
						ussd_String := ussd_string
					}
				}
			}
		}
	}
}

template (value) SS_FacilityInformation ts_SS_USSD_FACILITY_RETURN_RESULT_EMPTY(
	integer invoke_id := 1
) := {
	{
		returnResult := {
			invokeId := { present_ := invoke_id },
			result := omit
		}
	}
}
template SS_FacilityInformation tr_SS_USSD_FACILITY_RETURN_RESULT_EMPTY(
	template integer invoke_id := ?
) := {
	{
		returnResult := {
			invokeId := { present_ := invoke_id },
			result := omit
		}
	}
}

/* Common for both structured and unstructured SS */
template (value) SS_FacilityInformation ts_SS_FACILITY_RETURN_ERROR(
	integer invoke_id := 1,
	SS_Err_Code err_code
) := {
	{
		returnError := {
			invokeId := { present_ := invoke_id },
			errcode := {
				local := enum2int(err_code)
			},
			parameter := omit
		}
	}
}
template SS_FacilityInformation tr_SS_FACILITY_RETURN_ERROR(
	template integer invoke_id := ?,
	template integer err_code := ?
) := {
	{
		returnError := {
			invokeId := { present_ := invoke_id },
			errcode := {
				local := err_code
			},
			parameter := omit
		}
	}
}

}
