module BSC_ConnectionHandler {

/* BSC Connection Handler of SMLC Tests in TTCN-3
 * (C) 2020 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
 */

import from Misc_Helpers all;
import from General_Types all;
import from Osmocom_Types all;
import from SCCPasp_Types all;
import from BSSAP_Types all;
import from BSSAP_CodecPort all;
import from RAN_Emulation all;
import from BSSMAP_Templates all;
import from BSSAP_LE_Emulation all;
import from BSSAP_LE_Types all;
import from BSSMAP_LE_Templates all;

import from TELNETasp_PortType all;
import from Osmocom_VTY_Functions all;

/* this component represents a single subscriber connection at the SMLC. */
type component BSC_ConnHdlr extends BSSAP_LE_ConnHdlr {
	/* SCCP Connecction Identifier for the underlying SCCP connection */
	var integer g_sccp_conn_id;

	port TELNETasp_PT SMLCVTY;

	var TestHdlrParams g_pars;

	var charstring host_bsc := "127.0.0.4";

	var boolean g_vty_initialized := false;
}

function f_BscConnHdlr_init_vty() runs on BSC_ConnHdlr {
	if (not g_vty_initialized) {
		map(self:SMLCVTY, system:SMLCVTY);
		f_vty_set_prompts(SMLCVTY);
		f_vty_transceive(SMLCVTY, "enable");
		g_vty_initialized := true;
	}
}

/* initialize all parameters */
function f_BscConnHdlr_init() runs on BSC_ConnHdlr {
	f_BscConnHdlr_init_vty();
}

/* Callback function from general BSSAP_LE_Emulation whenever a connectionless
 * BSSAP_LE message arrives.  Can return a PDU_BSSAP_LE that should be sent in return */
private function BSSAP_LE_UnitdataCallback(PDU_BSSAP_LE bssap)
runs on BSSAP_LE_Emulation_CT return template PDU_BSSAP_LE {
	var template PDU_BSSAP_LE resp := omit;

	/* answer all RESET with a RESET ACK */
	if (match(bssap, tr_BSSMAP_LE_Reset)) {
		resp := ts_BSSMAP_LE_ResetAck;
	}

	return resp;
}

const BssapLeOps BSC_BssapLeOps := {
	create_cb := refers(BSSAP_LE_Emulation.ExpectedCreateCallback),
	unitdata_cb := refers(BSSAP_LE_UnitdataCallback),
	decode_dtap := false,
	role_ms := false,
	sccp_addr_local := omit,
	sccp_addr_peer := omit
}
type record TestHdlrParams {
	hexstring imsi,
	integer bssap_le_idx,
	SCCP_PAR_Address sccp_addr_bsc optional,
	SCCP_PAR_Address sccp_addr_smlc optional
};

/* Note: Do not use valueof() to get a value of this template, use
 * f_gen_test_hdlr_pars() instead in order to get a configuration that is
 * matched to the current test situation (aoip vs. sccplite) */
template (value) TestHdlrParams t_def_TestHdlrPars := {
	imsi := '001019876543210'H,
	bssap_le_idx := 0,
	sccp_addr_bsc := omit,
	sccp_addr_smlc := omit
}

}
