blob: b038ce5b5894aa953de00b3371e4a368af9b89d9 [file] [log] [blame]
Harald Welte365f4ed2017-11-23 00:00:43 +01001module MSC_ConnectionHandler {
2
3import from General_Types all;
4import from Osmocom_Types all;
5import from SCCPasp_Types all;
6import from BSSAP_Types all;
Harald Welte004f5fb2017-12-16 17:54:40 +01007import from BSSAP_CodecPort all;
Harald Welte365f4ed2017-11-23 00:00:43 +01008import from BSSMAP_Emulation all;
9import from BSSMAP_Templates all;
10
Harald Weltec82eef42017-11-24 20:40:12 +010011import from MGCP_Types all;
12import from MGCP_Templates all;
13import from SDP_Types all;
14
Harald Welte365f4ed2017-11-23 00:00:43 +010015/* this component represents a single subscriber connection at the MSC.
16 * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components.
17 * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */
18type component MSC_ConnHdlr extends BSSAP_ConnHdlr {
Harald Welte365f4ed2017-11-23 00:00:43 +010019 /* SCCP Connecction Identifier for the underlying SCCP connection */
20 var integer g_sccp_conn_id;
Harald Weltec82eef42017-11-24 20:40:12 +010021
22 var MSC_State g_state := MSC_STATE_NONE;
23 var MgcpEndpoint g_ep_name;
24 var MgcpCallId g_call_id;
25 var MgcpConnectionId g_mgcp_conn_id;
Harald Welte365f4ed2017-11-23 00:00:43 +010026}
27
28/* Callback function from general BSSMAP_Emulation whenever a new incoming
29 * SCCP connection arrivces. Must create + start a new component */
Harald Welte004f5fb2017-12-16 17:54:40 +010030private function CreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id)
Harald Welte365f4ed2017-11-23 00:00:43 +010031runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
32 var MSC_ConnHdlr vc_conn;
33 /* Create a new BSSAP_ConnHdlr component */
Harald Weltebe620f62017-11-25 00:23:54 +010034 vc_conn := MSC_ConnHdlr.create(g_bssmap_id & "-Conn-" & int2str(conn_ind.connectionId));
Harald Welte365f4ed2017-11-23 00:00:43 +010035 /* connect it to the port */
36 connect(vc_conn:BSSAP, self:CLIENT);
37 /* start it */
Harald Welte66fecd42017-11-24 23:53:23 +010038 vc_conn.start(MSC_ConnectionHandler.main(conn_ind.connectionId, g_next_e1_ts));
39 /* increment next E1 timeslot */
40 g_next_e1_ts := g_next_e1_ts + 1;
Harald Welte365f4ed2017-11-23 00:00:43 +010041 return vc_conn;
42}
43
44/* Callback function from general BSSMAP_Emulation whenever a connectionless
45 * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */
46private function UnitdataCallback(PDU_BSSAP bssap)
47runs on BSSMAP_Emulation_CT return template PDU_BSSAP {
48 var template PDU_BSSAP resp := omit;
49
50 if (match(bssap, tr_BSSMAP_Reset)) {
51 resp := ts_BSSMAP_ResetAck;
52 }
53
54 return resp;
55}
56
57const BssmapOps MSC_BssmapOps := {
58 create_cb := refers(CreateCallback),
59 unitdata_cb := refers(UnitdataCallback)
60}
61
Harald Weltec82eef42017-11-24 20:40:12 +010062type enumerated MSC_State {
63 MSC_STATE_NONE,
64 MSC_STATE_WAIT_ASS_COMPL,
65 MSC_STATE_WAIT_CRCX_ACK,
66 MSC_STATE_WAIT_MDCX_ACK,
67 MSC_STATE_WAIT_CLEAR_COMPL,
68 MSC_STATE_WAIT_DLCX_ACK
69}
70
Harald Welte365f4ed2017-11-23 00:00:43 +010071/* main function processing various incoming events */
Harald Welte66fecd42017-11-24 23:53:23 +010072function main(integer connection_id, integer e1_timeslot) runs on MSC_ConnHdlr {
Harald Weltec82eef42017-11-24 20:40:12 +010073 var MgcpResponse mgcp_rsp;
Harald Welte98f1f412017-11-25 01:21:49 +010074 timer T := 5.0;
Harald Weltec82eef42017-11-24 20:40:12 +010075
Harald Weltec82eef42017-11-24 20:40:12 +010076 g_sccp_conn_id := connection_id;
77 g_call_id := f_mgcp_alloc_call_id();
Harald Welte66fecd42017-11-24 23:53:23 +010078 g_ep_name := hex2str(int2hex(e1_timeslot, 1)) & "@mgw";
Harald Weltec82eef42017-11-24 20:40:12 +010079
Harald Welte98f1f412017-11-25 01:21:49 +010080 /* we just accepted an incoming SCCP connection, start guard timer */
81 T.start;
82
Harald Welte365f4ed2017-11-23 00:00:43 +010083 while (true) {
84 var PDU_BSSAP bssap;
85 alt {
86 /* new SCCP-level connection indication from BSC */
Harald Weltec82eef42017-11-24 20:40:12 +010087 [g_state == MSC_STATE_NONE] BSSAP.receive(tr_BSSMAP_ComplL3) -> value bssap {
Harald Welte365f4ed2017-11-23 00:00:43 +010088 /* respond with ASSIGNMENT CMD */
Harald Weltec82eef42017-11-24 20:40:12 +010089 g_state := MSC_STATE_WAIT_ASS_COMPL;
Harald Weltecc7e4dc2017-12-14 21:55:10 +010090 BSSAP.send(ts_BSSMAP_AssignmentReq(ts_BSSMAP_IE_CIC(0, e1_timeslot)));
Harald Weltec82eef42017-11-24 20:40:12 +010091 }
Harald Weltecc7e4dc2017-12-14 21:55:10 +010092 [g_state == MSC_STATE_WAIT_ASS_COMPL] BSSAP.receive(tr_BSSMAP_AssignmentComplete(?,*)) {
Harald Weltec82eef42017-11-24 20:40:12 +010093 /* FIXME: Send MGCP CRCX */
94 g_state := MSC_STATE_WAIT_CRCX_ACK;
95 var MgcpTransId trans_id := f_mgcp_alloc_tid();
96 //template SDP_Message sdp := omit;
97 BSSAP.send(ts_CRCX(trans_id, g_ep_name, "recvonly", g_call_id)); //, sdp));
Harald Welte365f4ed2017-11-23 00:00:43 +010098 }
99 /*
Harald Welte365f4ed2017-11-23 00:00:43 +0100100 [] BSSAP.receive(tr_BSSMAP_AssignmentFail) {
101 }
102 */
Harald Weltec82eef42017-11-24 20:40:12 +0100103
104 /* receive CRCX ACK: transmit MDCX */
105 [g_state == MSC_STATE_WAIT_CRCX_ACK] BSSAP.receive(tr_CRCX_ACK) -> value mgcp_rsp {
106 /* extract connection ID */
Harald Welte4c11d562017-11-24 23:39:00 +0100107 g_mgcp_conn_id := f_MgcpResp_extract_conn_id(mgcp_rsp);
Harald Weltec82eef42017-11-24 20:40:12 +0100108 g_state := MSC_STATE_WAIT_MDCX_ACK;
109 var MgcpTransId trans_id := f_mgcp_alloc_tid();
110 BSSAP.send(ts_MDCX(trans_id, g_ep_name, "sendrecv", g_call_id, g_mgcp_conn_id));
111 }
112
113 /* receive MDCX ACK: wait + transmit CLEAR COMMAND */
114 [g_state == MSC_STATE_WAIT_MDCX_ACK] BSSAP.receive(tr_CRCX_ACK) -> value mgcp_rsp {
115 g_state := MSC_STATE_WAIT_CLEAR_COMPL
Harald Welte8ac9f022017-11-24 23:48:14 +0100116 BSSAP.send(ts_BSSMAP_ClearCommand(9)); /* Cause: call control */
Harald Weltec82eef42017-11-24 20:40:12 +0100117 }
118
119 /* CLEAR COMPLETE from BSS (response to CLEAR COMMAND) */
Harald Welte11e78082017-11-24 23:42:07 +0100120 [g_state == MSC_STATE_WAIT_CLEAR_COMPL] BSSAP.receive(tr_BSSMAP_ClearComplete) {
Harald Weltec82eef42017-11-24 20:40:12 +0100121 /* send DLCX */
122 g_state := MSC_STATE_WAIT_DLCX_ACK;
123 var MgcpTransId trans_id := f_mgcp_alloc_tid();
124 BSSAP.send(ts_DLCX(trans_id, g_ep_name, g_call_id));
125 }
126
127 [g_state == MSC_STATE_WAIT_DLCX_ACK] BSSAP.receive(tr_DLCX_ACK) {
Harald Welte0a4317a2017-11-25 00:32:46 +0100128 BSSAP.send(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ);
Harald Weltec82eef42017-11-24 20:40:12 +0100129 setverdict(pass);
130 self.stop;
131 }
132
Harald Welte365f4ed2017-11-23 00:00:43 +0100133 /* TODO: CLEAR REQUEST from BSS */
Harald Welte365f4ed2017-11-23 00:00:43 +0100134
135 [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) {
Harald Weltec82eef42017-11-24 20:40:12 +0100136 setverdict(fail);
Harald Welte365f4ed2017-11-23 00:00:43 +0100137 self.stop;
138 }
139
140 [] BSSAP.receive(PDU_BSSAP:?) -> value bssap {
141 log("Received unhandled SCCP-CO: ", bssap);
142 }
Harald Welte98f1f412017-11-25 01:21:49 +0100143
144 /* Guard timer has expired, close connection */
145 [] T.timeout {
146 BSSAP.send(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ);
147 setverdict(inconc);
148 self.stop;
149 }
150
Harald Welte365f4ed2017-11-23 00:00:43 +0100151 }
152 }
153}
154
155}