hnodeb: Add initial infra to emulate HNB LL socket iface

Change-Id: Ia864a672643a0e42f5afea6fd901e621478c87c0
diff --git a/hnodeb/HNBGW_ConnectionHandler.ttcn b/hnodeb/HNBGW_ConnectionHandler.ttcn
index 071e3e8..554c35f 100644
--- a/hnodeb/HNBGW_ConnectionHandler.ttcn
+++ b/hnodeb/HNBGW_ConnectionHandler.ttcn
@@ -27,11 +27,19 @@
 
 import from Iuh_Emulation all;
 
+import from HNBLLIF_CodecPort all;
+import from HNBLLIF_Types all;
+import from HNBLLIF_Templates all;
+
 /* this component represents a single Iuh connection at the HNBGW. */
 type component HNBGW_ConnHdlr extends StatsD_ConnHdlr {
 	port TELNETasp_PT HNBVTY;
 	port HNBAP_PT HNBAP;
 	port RUA_PT RUA;
+	/* HNBLLIF Interface of HNodeB */
+	port HNBLLIF_CODEC_PT LLSK;
+	var integer g_llsk_conn_id;
+
 	var TestHdlrParams g_pars;
 
 	var boolean g_vty_initialized := false;
@@ -47,12 +55,11 @@
 }
 
 /* initialize all parameters */
-function f_HNBGW_ConnHdlr_init(TestHdlrParams pars) runs on HNBGW_ConnHdlr {
-	var integer i := 0;
+function f_HNBGW_ConnHdlr_init(charstring id, TestHdlrParams pars) runs on HNBGW_ConnHdlr {
 	var Iuh_Emulation_CT vc_Iuh;
 
 	g_pars := valueof(pars);
-	vc_Iuh := Iuh_Emulation_CT.create("HNBGW" & int2str(i));
+	vc_Iuh := Iuh_Emulation_CT.create(id & "-HNBGW");
 	connect(self:HNBAP, vc_Iuh:HNBAP);
 	connect(self:RUA, vc_Iuh:RUA);
 
@@ -61,18 +68,51 @@
 	iuh_pars.remote_sctp_port := -1;
 	iuh_pars.local_ip := g_pars.hnbgw_addr;
 	iuh_pars.local_sctp_port  := g_pars.hnbgw_port;
-	vc_Iuh.start(Iuh_Emulation.main(iuh_pars, "Iuh" & int2str(i)));
+	vc_Iuh.start(Iuh_Emulation.main(iuh_pars, id & "-Iuh"));
 
 	f_HNBGW_ConnHdlr_init_vty();
+
+	/* Connect to HNB on LLSK and do HELLO ping-pong  */
+	f_start_hnbllif(LLSK, id & "-LLSK", g_pars.hnbllif_sk_path,  g_llsk_conn_id);
+}
+
+
+function f_start_hnbllif(HNBLLIF_CODEC_PT pt, charstring id, charstring hnbllif_sk_path, out integer hnbllif_conn_id) {
+	timer T := 2.0;
+	var HNBLLIF_send_data sd;
+	var HNBLLIF_Message last_hello_cnf;
+	if (hnbllif_sk_path == "") {
+		hnbllif_conn_id := -1;
+		return;
+	}
+	hnbllif_conn_id := f_hnbllif_connect(pt, hnbllif_sk_path);
+
+	T.start;
+	pt.send(t_SD_HNBLLIF(hnbllif_conn_id, ts_HNBLLIF_CTL_HELLO_REQ(HNBLL_IF_SAPI_CTL, HNBLLIF_Types.mp_hnbllif_version)));
+	alt {
+	[] as_hnbllif_hello_cnf(pt, hnbllif_conn_id, last_hello_cnf, HNBLL_IF_SAPI_CTL, HNBLLIF_Types.mp_hnbllif_version);
+	[] T.timeout {
+		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for HNBLLIF HELLO.REQ SAPI=CTL");
+		}
+	}
+	pt.send(t_SD_HNBLLIF(hnbllif_conn_id, ts_HNBLLIF_CTL_HELLO_REQ(HNBLL_IF_SAPI_IUH, 0)));
+	alt {
+	[] as_hnbllif_hello_cnf(pt, hnbllif_conn_id, last_hello_cnf, HNBLL_IF_SAPI_IUH, 0);
+	[] T.timeout {
+		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Timeout waiting for HNBLLIF HELLO.REQ SAPI=IUH");
+		}
+	}
 }
 
 type record TestHdlrParams {
+	charstring hnbllif_sk_path, /* "" means don't connect */
 	charstring hnbgw_addr,
 	charstring hnodeb_addr,
 	integer hnbgw_port,
 	uint16_t rnc_id,
 	charstring hNB_Identity_Info,
-	OCT3 plmnid,
+	uint16_t mcc,
+	uint16_t mnc,
 	uint32_t cell_identity,
 	uint16_t lac,
 	uint8_t rac,
@@ -82,23 +122,61 @@
 /* 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. */
 template (value) TestHdlrParams t_def_TestHdlrPars := {
+	hnbllif_sk_path := HNBLL_SOCK_DEFAULT,
 	hnbgw_addr := "127.0.0.1",
 	hnodeb_addr := "127.0.0.1",
 	hnbgw_port := 29169,
 	rnc_id := 23,
 	hNB_Identity_Info := "OsmoHNodeB",
-	plmnid := '00F110'O,
+	mcc := 1,
+	mnc := 1,
 	cell_identity := 1,
 	lac := 2,
 	rac := 3,
 	sac := 4
 }
 
+/* HNBLLIF socket may at any time receive a new INFO.ind */
+altstep as_hnbllif_hello_cnf(HNBLLIF_CODEC_PT pt, integer hnbllif_conn_id,
+				out HNBLLIF_Message last_hello_cnf,
+				template (present) HNBLLIF_Sapi exp_sapi := ?,
+				template (present) uint16_t exp_version := ?) {
+	var HNBLLIF_send_data sd;
+	[] pt.receive(t_SD_HNBLLIF(hnbllif_conn_id, tr_HNBLLIF_CTL_HELLO_CNF(exp_sapi, exp_version))) -> value sd {
+		last_hello_cnf := sd.data;
+		}
+	[] pt.receive(t_SD_HNBLLIF(hnbllif_conn_id, tr_HNBLLIF_CTL_HELLO_CNF(?))) -> value sd {
+		Misc_Helpers.f_shutdown(__BFILE__, __LINE__, fail, "Invalid API_VERSION received");
+		}
+}
+
+function f_llsk_rx(template (present) HNBLLIF_Message exp_tmpl) runs on HNBGW_ConnHdlr
+return template (present) HNBLLIF_send_data {
+	return t_SD_HNBLLIF(g_llsk_conn_id, exp_tmpl);
+}
+
+function f_llsk_tx(template (value) HNBLLIF_Message tx_msg) runs on HNBGW_ConnHdlr
+return template (value) HNBLLIF_send_data {
+	return ts_SD_HNBLLIF(g_llsk_conn_id, tx_msg);
+}
+
+function f_enc_mcc_mnc(uint16_t mcc_uint, uint16_t mnc_uint) return OCT3 {
+	var hexstring mnc;
+	var hexstring mcc := int2hex(mcc_uint, 3);
+
+	if (mnc_uint < 100) {
+		mnc := int2hex(mnc_uint, 2);
+		return hex2oct(mcc[1] & mcc[0] & 'F'H & mcc[2] & mnc[1] & mnc[0]);
+	} else {
+		mnc := int2hex(mnc_uint, 3);
+		return hex2oct(mcc[1] & mcc[0] & mnc[2] & mcc[2] & mnc[1] & mnc[0]);
+	}
+}
 
 function f_handle_hnbap_hnb_register_req()
 runs on HNBGW_ConnHdlr {
 	HNBAP.receive(tr_HNBAP_HNBRegisterRequest(char2oct(g_pars.hNB_Identity_Info),
-						  g_pars.plmnid,
+						  f_enc_mcc_mnc(g_pars.mcc, g_pars.mnc),
 						  int2bit(g_pars.cell_identity, 28),
 						  int2oct(g_pars.lac, 2),
 						  int2oct(g_pars.rac, 1),
diff --git a/hnodeb/HNB_Tests.default b/hnodeb/HNB_Tests.default
index a06dcc6..c385b9a 100644
--- a/hnodeb/HNB_Tests.default
+++ b/hnodeb/HNB_Tests.default
@@ -20,6 +20,7 @@
 *.STATSVTY.CTRL_CLIENT_CLEANUP_LINEFEED := "yes"
 *.STATSVTY.CTRL_DETECT_CONNECTION_ESTABLISHMENT_RESULT := "yes"
 *.STATSVTY.PROMPT1 := "OsmoHNodeB> "
+*.LLSK.socket_type := "SEQPACKET"
 
 [MODULE_PARAMETERS]
 Osmocom_VTY_Functions.mp_prompt_prefix := "OsmoHNodeB";
diff --git a/hnodeb/HNB_Tests.ttcn b/hnodeb/HNB_Tests.ttcn
index 7a668dd..59dd1ee 100644
--- a/hnodeb/HNB_Tests.ttcn
+++ b/hnodeb/HNB_Tests.ttcn
@@ -38,10 +38,16 @@
 import from TELNETasp_PortType all;
 
 import from HNBAP_Templates all;
+import from RUA_IEs all;
+import from RUA_Templates all;
 
 import from HNBGW_ConnectionHandler all;
 import from Iuh_Emulation all;
 
+import from HNBLLIF_CodecPort all;
+import from HNBLLIF_Types all;
+import from HNBLLIF_Templates all;
+
 modulepar {
 	/* IP address at which the HNodeB can be reached */
 	charstring mp_hnodeb_ip := "127.0.0.1";
@@ -49,11 +55,23 @@
 	/* IP address at which the test binds */
 	charstring mp_hnbgw_iuh_ip := "127.0.0.1";
 	integer mp_hnbgw_iuh_port := 29169;
+
+	charstring mp_hnbllif_sk_path := HNBLL_SOCK_DEFAULT;
 }
 
+const hexstring ranap_cm_service_req := '001340400000060003400100000f40060000f11028b6003a40080000f110ffffffff0010400e0d052411035758a605f44e9d4aef004f400300001c0056400500f1100017'H;
+const hexstring ranap_auth_req := '00144032000002001040262505120217dc146aeac56cb5ff6d5fb51f47f19220108ca5a6d0c8110000b9e9272498872764003b400100'H;
+const hexstring ranap_auth_resp := '001440140000010010400d0c0554ccbdd0302104002f3ae4'H;
+const hexstring ranap_paging := '000e401e0000030003400100001740095000010100000000f10040400500b6cf4773'H;
+const hexstring iu_release_compl := '20010003000000'H;
+
 type component test_CT extends CTRL_Adapter_CT {
 	port TELNETasp_PT HNBVTY;
 
+	/* HNBLLIF Interface of HNodeB */
+	port HNBLLIF_CODEC_PT LLSK;
+	var integer g_llsk_conn_id;
+
 	/* global test case guard timer (actual timeout value is set in f_init()) */
 	timer T_guard := 30.0;
 }
@@ -83,6 +101,12 @@
 	f_vty_set_prompts(HNBVTY);
 	f_vty_transceive(HNBVTY, "enable");
 }
+
+private function f_init_hnbllif() runs on test_CT {
+	map(self:LLSK, system:LLSK);
+	f_start_hnbllif(LLSK, testcasename(), mp_hnbllif_sk_path, g_llsk_conn_id);
+}
+
 /* global initialization function */
 function f_init(float guard_timeout := 30.0) runs on test_CT {
 	var integer bssap_idx;
@@ -91,8 +115,6 @@
 	activate(as_Tguard());
 
 	f_init_vty("VirtHNBGW");
-
-	/* TODO: Wait for Iuh connection to be established */
 }
 
 friend function f_shutdown_helper() runs on test_CT {
@@ -101,9 +123,10 @@
 	mtc.stop;
 }
 
-friend function f_gen_test_hdlr_pars() return TestHdlrParams {
+private function f_gen_test_hdlr_pars() runs on test_CT return TestHdlrParams {
 
 	var TestHdlrParams pars := valueof(t_def_TestHdlrPars);
+	pars.hnbllif_sk_path := mp_hnbllif_sk_path;
 	pars.hnodeb_addr := mp_hnodeb_ip;
 	pars.hnbgw_addr := mp_hnbgw_iuh_ip;
 	pars.hnbgw_port := mp_hnbgw_iuh_port;
@@ -113,27 +136,10 @@
 type function void_fn(charstring id) runs on HNBGW_ConnHdlr;
 
 /* helper function to create and connect a HNBGW_ConnHdlr component */
-private function f_connect_handler(inout HNBGW_ConnHdlr vc_conn, integer bssap_idx := 0) runs on test_CT {
-	/*connect(vc_conn:RAN, g_bssap[bssap_idx].vc_RAN:PROC);
-	connect(vc_conn:MGCP_PROC, vc_MGCP:MGCP_PROC);
-	connect(vc_conn:RSL, bts[0].rsl.vc_RSL:CLIENT_PT);
-	connect(vc_conn:RSL_PROC, bts[0].rsl.vc_RSL:RSL_PROC);
-	if (isvalue(bts[1])) {
-		connect(vc_conn:RSL1, bts[1].rsl.vc_RSL:CLIENT_PT);
-		connect(vc_conn:RSL1_PROC, bts[1].rsl.vc_RSL:RSL_PROC);
+private function f_connect_handler(inout HNBGW_ConnHdlr vc_conn, TestHdlrParams pars) runs on test_CT {
+	if (pars.hnbllif_sk_path != "") {
+		map(vc_conn:LLSK, system:LLSK);
 	}
-	if (isvalue(bts[2])) {
-		connect(vc_conn:RSL2, bts[2].rsl.vc_RSL:CLIENT_PT);
-		connect(vc_conn:RSL2_PROC, bts[2].rsl.vc_RSL:RSL_PROC);
-	}
-	connect(vc_conn:BSSAP, g_bssap[bssap_idx].vc_RAN:CLIENT);
-	if (mp_enable_lcs_tests) {
-		connect(vc_conn:BSSAP_LE, g_bssap_le.vc_BSSAP_LE:CLIENT);
-		connect(vc_conn:BSSAP_LE_PROC, g_bssap_le.vc_BSSAP_LE:PROC);
-	}
-	connect(vc_conn:MGCP, vc_MGCP:MGCP_CLIENT);
-	connect(vc_conn:MGCP_MULTI, vc_MGCP:MGCP_CLIENT_MULTI);
-	connect(vc_conn:STATSD_PROC, vc_STATSD:STATSD_PROC);*/
 }
 
 function f_start_handler_create(TestHdlrParams pars)
@@ -141,7 +147,7 @@
 	var charstring id := testcasename();
 	var HNBGW_ConnHdlr vc_conn;
 	vc_conn := HNBGW_ConnHdlr.create(id);
-	f_connect_handler(vc_conn);
+	f_connect_handler(vc_conn, pars);
 	return vc_conn;
 }
 
@@ -168,27 +174,29 @@
 /* first function inside ConnHdlr component; sets g_pars + starts function */
 private function f_handler_init(void_fn fn, charstring id, TestHdlrParams pars)
 runs on HNBGW_ConnHdlr {
-	f_HNBGW_ConnHdlr_init(pars);
+	f_HNBGW_ConnHdlr_init(id, pars);
 	HNBAP.receive(IUHEM_Event:{up_down:=IUHEM_EVENT_UP}); /* Wait for HNB to connect to us */
 	fn.apply(id);
 }
 
-private function f_tc_hnb_register_request(charstring id) runs on HNBGW_ConnHdlr {
+private function f_tc_hnb_register_request_accept(charstring id) runs on HNBGW_ConnHdlr {
 	f_handle_hnbap_hnb_register_req();
+	LLSK.receive(f_llsk_rx(tr_HNBLLIF_IUH_CONFIGURE_IND(g_pars.mcc, g_pars.mnc, g_pars.cell_identity,
+						  g_pars.lac, g_pars.rac, g_pars.sac, g_pars.rnc_id)));
 	f_sleep(1.0);
 }
 testcase TC_hnb_register_request_accept() runs on test_CT {
 	var HNBGW_ConnHdlr vc_conn;
 
 	f_init();
-	vc_conn := f_start_handler(refers(f_tc_hnb_register_request));
+	vc_conn := f_start_handler(refers(f_tc_hnb_register_request_accept));
 	vc_conn.done;
 	f_shutdown_helper();
 }
 
 private function f_tc_hnb_register_reject(charstring id) runs on HNBGW_ConnHdlr {
 	HNBAP.receive(tr_HNBAP_HNBRegisterRequest(char2oct(g_pars.hNB_Identity_Info),
-						  g_pars.plmnid,
+						  f_enc_mcc_mnc(g_pars.mcc, g_pars.mnc),
 						  int2bit(g_pars.cell_identity, 28),
 						  int2oct(g_pars.lac, 2),
 						  int2oct(g_pars.rac, 1),
@@ -206,9 +214,74 @@
 	f_shutdown_helper();
 }
 
+private function f_tc_mo_conn(charstring id) runs on HNBGW_ConnHdlr {
+	const integer context_id := 30;
+	const bitstring context_id_bstr := '000000000000000000011110'B; /* encoded context_id */
+	const Establishment_Cause est_cause := normal_call;
+	f_handle_hnbap_hnb_register_req();
+
+	LLSK.receive(f_llsk_rx(tr_HNBLLIF_IUH_CONFIGURE_IND(g_pars.mcc, g_pars.mnc, g_pars.cell_identity,
+						  g_pars.lac, g_pars.rac, g_pars.sac, g_pars.rnc_id)));
+
+	/* Now an UE attempts CM Service Request: */
+	LLSK.send(f_llsk_tx(ts_HNBLLIF_IUH_CONN_ESTABLISH_REQ(context_id, 1, enum2int(est_cause), hex2oct(ranap_cm_service_req))));
+	/* The related RUA Connect + RANAP message is received on Iuh: */
+	RUA.receive(tr_RUA_Connect(ps_domain, context_id_bstr, est_cause, hex2oct(ranap_cm_service_req)));
+
+	/* Now HNBGW answers with RUA-DirectTransfer(RANAP-AuthenticationRequest) */
+	RUA.send(ts_RUA_DirectTransfer(ps_domain, context_id_bstr, hex2oct(ranap_auth_req)));
+
+	/* Now on LLSK first the Conn establishment is confirmed and then we receive data */
+	LLSK.receive(f_llsk_rx(tr_HNBLLIF_IUH_CONN_ESTABLISH_CNF(context_id, 1, 0)));
+	LLSK.receive(f_llsk_rx(tr_HNBLLIF_IUH_CONN_DATA_IND(context_id, 1, hex2oct(ranap_auth_req))));
+
+	/* UE answers with RANAPAuthenticationResponse: */
+	LLSK.send(f_llsk_tx(ts_HNBLLIF_IUH_CONN_DATA_REQ(context_id, 1, hex2oct(ranap_auth_resp))));
+	RUA.receive(tr_RUA_DirectTransfer(ps_domain, context_id_bstr, hex2oct(ranap_auth_resp)));
+
+	/* UE sends Iu Release Complete to release the conn */
+	LLSK.send(f_llsk_tx(ts_HNBLLIF_IUH_CONN_RELEASE_REQ(context_id, 1, 0, 0, hex2oct(iu_release_compl))));
+	RUA.receive(tr_RUA_Disconnect(ps_domain, context_id_bstr, ts_RUA_Cause(normal), hex2oct(iu_release_compl)));
+}
+testcase TC_mo_conn() runs on test_CT {
+	var HNBGW_ConnHdlr vc_conn;
+
+	f_init();
+	vc_conn := f_start_handler(refers(f_tc_mo_conn));
+	vc_conn.done;
+	f_shutdown_helper();
+}
+
+
+private function f_tc_paging(charstring id) runs on HNBGW_ConnHdlr {
+	const integer context_id := 30;
+	const bitstring context_id_bstr := '000000000000000000011110'B; /* encoded context_id */
+	f_handle_hnbap_hnb_register_req();
+
+	LLSK.receive(f_llsk_rx(tr_HNBLLIF_IUH_CONFIGURE_IND(g_pars.mcc, g_pars.mnc, g_pars.cell_identity,
+						  g_pars.lac, g_pars.rac, g_pars.sac, g_pars.rnc_id)));
+
+	/* HNBGW sends RUA-ConnectionlessTransfer(RANAP-Paging) */
+	RUA.send(ts_RUA_ConnectionlessTransfer(hex2oct(ranap_paging)));
+
+	LLSK.receive(f_llsk_rx(tr_HNBLLIF_IUH_UNITDATA_IND(hex2oct(ranap_paging))));
+
+	/* Here it would continue with UE-side answers with RUA-Connect(RANAP-PagingResponse) */
+}
+testcase TC_paging() runs on test_CT {
+	var HNBGW_ConnHdlr vc_conn;
+
+	f_init();
+	vc_conn := f_start_handler(refers(f_tc_paging));
+	vc_conn.done;
+	f_shutdown_helper();
+}
+
 control {
 	execute( TC_hnb_register_request_accept() );
 	execute( TC_hnb_register_request_reject() );
+	execute( TC_mo_conn() );
+	execute( TC_paging() );
 }
 
 }
diff --git a/hnodeb/gen_links.sh b/hnodeb/gen_links.sh
index ba946e8..6ddb4c0 100755
--- a/hnodeb/gen_links.sh
+++ b/hnodeb/gen_links.sh
@@ -27,6 +27,10 @@
 FILES="SCTPasp_PT.cc  SCTPasp_PT.hh  SCTPasp_PortType.ttcn  SCTPasp_Types.ttcn"
 gen_links $DIR $FILES
 
+DIR=$BASEDIR/titan.TestPorts.UNIX_DOMAIN_SOCKETasp/src
+FILES="UD_PT.cc  UD_PT.hh  UD_PortType.ttcn  UD_Types.ttcn"
+gen_links $DIR $FILES
+
 DIR=$BASEDIR/titan.ProtocolModules.SDP/src
 FILES="SDP_EncDec.cc SDP_Types.ttcn SDP_parse_.tab.c SDP_parse_.tab.h SDP_parse_parser.h SDP_parser.l
 SDP_parser.y lex.SDP_parse_.c"
@@ -51,7 +55,8 @@
 gen_links $DIR $FILES
 
 DIR=../library
-FILES="Iuh_Types.ttcn Iuh_CodecPort.ttcn Iuh_CodecPort_CtrlFunctDef.cc Iuh_CodecPort_CtrlFunct.ttcn Iuh_Emulation.ttcn DNS_Helpers.ttcn "
+FILES="HNBLLIF_Types.ttcn HNBLLIF_Templates.ttcn HNBLLIF_CodecPort.ttcn "
+FILES+="Iuh_Types.ttcn Iuh_CodecPort.ttcn Iuh_CodecPort_CtrlFunctDef.cc Iuh_CodecPort_CtrlFunct.ttcn Iuh_Emulation.ttcn DNS_Helpers.ttcn "
 FILES+="Misc_Helpers.ttcn General_Types.ttcn Osmocom_Types.ttcn Osmocom_VTY_Functions.ttcn Native_Functions.ttcn Native_FunctionDefs.cc IPA_Types.ttcn IPA_CodecPort.ttcn IPA_CodecPort_CtrlFunct.ttcn IPA_CodecPort_CtrlFunctDef.cc IPA_Emulation.ttcnpp Osmocom_CTRL_Types.ttcn Osmocom_CTRL_Functions.ttcn Osmocom_CTRL_Adapter.ttcn RTP_CodecPort.ttcn RTP_CodecPort_CtrlFunct.ttcn RTP_CodecPort_CtrlFunctDef.cc RTP_Emulation.ttcn IuUP_Types.ttcn IuUP_EncDec.cc IuUP_Emulation.ttcn "
 FILES+="StatsD_Types.ttcn StatsD_CodecPort.ttcn StatsD_CodecPort_CtrlFunct.ttcn StatsD_CodecPort_CtrlFunctdef.cc StatsD_Checker.ttcn "
 
diff --git a/hnodeb/regen_makefile.sh b/hnodeb/regen_makefile.sh
index 9ebfaf2..2d13504 100755
--- a/hnodeb/regen_makefile.sh
+++ b/hnodeb/regen_makefile.sh
@@ -24,6 +24,7 @@
 	TELNETasp_PT.cc
 	HNBAP_EncDec.cc
 	RUA_EncDec.cc
+	UD_PT.cc
 "
 
 export CPPFLAGS_TTCN3="