IPA/MSC/BSC Emulation: Framework for handling multiple SCCP connections
diff --git a/ipa/MSC_ConnectionHandler.ttcn b/ipa/MSC_ConnectionHandler.ttcn
new file mode 100644
index 0000000..0d7cd20
--- /dev/null
+++ b/ipa/MSC_ConnectionHandler.ttcn
@@ -0,0 +1,86 @@
+module MSC_ConnectionHandler {
+
+import from General_Types all;
+import from Osmocom_Types all;
+import from SCCPasp_Types all;
+import from BSSAP_Types all;
+import from BSSMAP_Emulation all;
+import from BSSMAP_Templates all;
+
+/* this component represents a single subscriber connection at the MSC.
+ * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components.
+ * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */
+type component MSC_ConnHdlr extends BSSAP_ConnHdlr {
+	/* E1 timeslot that is allocated to this component/connection */
+	var uint5_t g_e1_timeslot;
+	/* SCCP Connecction Identifier for the underlying SCCP connection */
+	var integer g_sccp_conn_id;
+}
+
+/* Callback function from general BSSMAP_Emulation whenever a new incoming
+ * SCCP connection arrivces. Must create + start a new component */
+private function CreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind)
+runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
+	var MSC_ConnHdlr vc_conn;
+	/* Create a new BSSAP_ConnHdlr component */
+	vc_conn := MSC_ConnHdlr.create;
+	/* connect it to the port */
+	connect(vc_conn:BSSAP, self:CLIENT);
+	/* start it */
+	vc_conn.start(MSC_ConnectionHandler.main(conn_ind.connectionId, conn_ind.connectionId));
+	return vc_conn;
+}
+
+/* Callback function from general BSSMAP_Emulation whenever a connectionless
+ * BSSMAP message arrives.  Can retunr a PDU_BSSAP that should be sent in return */
+private function UnitdataCallback(PDU_BSSAP bssap)
+runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
+	var template PDU_BSSAP resp := omit;
+
+	if (match(bssap, tr_BSSMAP_Reset)) {
+		resp := ts_BSSMAP_ResetAck;
+	}
+
+	return resp;
+}
+
+const BssmapOps MSC_BssmapOps := {
+	create_cb := refers(CreateCallback),
+	unitdata_cb := refers(UnitdataCallback)
+}
+
+/* main function processing various incoming events */
+function main(integer connection_id, integer timeslot) runs on MSC_ConnHdlr {
+	g_sccp_conn_id := connection_id;
+	g_e1_timeslot := timeslot
+
+	while (true) {
+		var PDU_BSSAP bssap;
+		alt {
+		/* new SCCP-level connection indication from BSC */
+		[] BSSAP.receive(tr_BSSMAP_ComplL3) -> value bssap {
+			/* respond with ASSIGNMENT CMD  */
+			BSSAP.send(ts_BSSMAP_AssignmentCmd(0, g_e1_timeslot));
+			/* FIXME: Send MGCP */
+			}
+		/*
+		[] BSSAP.receive(tr_BSSMAP_AssignmentCompl) {
+		}
+		[] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
+		}
+		*/
+		/* TODO: CLEAR REQUEST from BSS */
+		/* TODO: CLEAR COMPLETE from BSS (response to CLEAR COMMAND) */
+
+		[] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
+			self.stop;
+			}
+
+		[] BSSAP.receive(PDU_BSSAP:?) -> value bssap {
+			log("Received unhandled SCCP-CO: ", bssap);
+			}
+		}
+	}
+}
+
+}