Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 1 | module MSC_ConnectionHandler { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from SCCPasp_Types all; |
| 6 | import from BSSAP_Types all; |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 7 | import from BSSAP_CodecPort all; |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 8 | import from RAN_Emulation all; |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 9 | import from BSSMAP_Templates all; |
| 10 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 11 | import from MGCP_Types all; |
| 12 | import from MGCP_Templates all; |
| 13 | import from SDP_Types all; |
| 14 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 15 | /* this component represents a single subscriber connection at the MSC. |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 16 | * There is a 1:1 mapping between SCCP connections and RAN_ConnHdlr components. |
| 17 | * We inherit all component variables, ports, functions, ... from RAN_ConnHdlr */ |
| 18 | type component MSC_ConnHdlr extends RAN_ConnHdlr { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 19 | /* SCCP Connecction Identifier for the underlying SCCP connection */ |
| 20 | var integer g_sccp_conn_id; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 21 | |
| 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 Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /* Callback function from general BSSMAP_Emulation whenever a new incoming |
| 29 | * SCCP connection arrivces. Must create + start a new component */ |
Harald Welte | 004f5fb | 2017-12-16 17:54:40 +0100 | [diff] [blame] | 30 | private function CreateCallback(BSSAP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 31 | runs on RAN_Emulation_CT return RAN_ConnHdlr { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 32 | var MSC_ConnHdlr vc_conn; |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 33 | /* Create a new RAN_ConnHdlr component */ |
| 34 | vc_conn := MSC_ConnHdlr.create(g_ran_id & "-Conn-" & int2str(conn_ind.connectionId)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 35 | /* connect it to the port */ |
| 36 | connect(vc_conn:BSSAP, self:CLIENT); |
| 37 | /* start it */ |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 38 | 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 Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 41 | 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 */ |
| 46 | private function UnitdataCallback(PDU_BSSAP bssap) |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 47 | runs on RAN_Emulation_CT return template PDU_BSSAP { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 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 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 57 | const RanOps MSC_RanOps := { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 58 | create_cb := refers(CreateCallback), |
Harald Welte | 0b47606 | 2018-01-21 19:07:32 +0100 | [diff] [blame] | 59 | unitdata_cb := refers(UnitdataCallback), |
| 60 | decode_dtap := false, |
Daniel Willmann | aa17059 | 2018-10-24 18:33:10 +0200 | [diff] [blame] | 61 | role_ms := false, |
| 62 | sccp_addr_local := omit, |
| 63 | sccp_addr_peer := omit |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 66 | type enumerated MSC_State { |
| 67 | MSC_STATE_NONE, |
| 68 | MSC_STATE_WAIT_ASS_COMPL, |
| 69 | MSC_STATE_WAIT_CRCX_ACK, |
| 70 | MSC_STATE_WAIT_MDCX_ACK, |
| 71 | MSC_STATE_WAIT_CLEAR_COMPL, |
| 72 | MSC_STATE_WAIT_DLCX_ACK |
| 73 | } |
| 74 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 75 | /* main function processing various incoming events */ |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 76 | function main(integer connection_id, integer e1_timeslot) runs on MSC_ConnHdlr { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 77 | var MgcpResponse mgcp_rsp; |
Harald Welte | 98f1f41 | 2017-11-25 01:21:49 +0100 | [diff] [blame] | 78 | timer T := 5.0; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 79 | |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 80 | g_sccp_conn_id := connection_id; |
| 81 | g_call_id := f_mgcp_alloc_call_id(); |
Harald Welte | 66fecd4 | 2017-11-24 23:53:23 +0100 | [diff] [blame] | 82 | g_ep_name := hex2str(int2hex(e1_timeslot, 1)) & "@mgw"; |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 83 | |
Harald Welte | 98f1f41 | 2017-11-25 01:21:49 +0100 | [diff] [blame] | 84 | /* we just accepted an incoming SCCP connection, start guard timer */ |
| 85 | T.start; |
| 86 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 87 | while (true) { |
| 88 | var PDU_BSSAP bssap; |
| 89 | alt { |
| 90 | /* new SCCP-level connection indication from BSC */ |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 91 | [g_state == MSC_STATE_NONE] BSSAP.receive(tr_BSSMAP_ComplL3) -> value bssap { |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 92 | /* respond with ASSIGNMENT CMD */ |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 93 | g_state := MSC_STATE_WAIT_ASS_COMPL; |
Harald Welte | cc7e4dc | 2017-12-14 21:55:10 +0100 | [diff] [blame] | 94 | BSSAP.send(ts_BSSMAP_AssignmentReq(ts_BSSMAP_IE_CIC(0, e1_timeslot))); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 95 | } |
Harald Welte | cc7e4dc | 2017-12-14 21:55:10 +0100 | [diff] [blame] | 96 | [g_state == MSC_STATE_WAIT_ASS_COMPL] BSSAP.receive(tr_BSSMAP_AssignmentComplete(?,*)) { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 97 | /* FIXME: Send MGCP CRCX */ |
| 98 | g_state := MSC_STATE_WAIT_CRCX_ACK; |
| 99 | var MgcpTransId trans_id := f_mgcp_alloc_tid(); |
| 100 | //template SDP_Message sdp := omit; |
| 101 | BSSAP.send(ts_CRCX(trans_id, g_ep_name, "recvonly", g_call_id)); //, sdp)); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 102 | } |
| 103 | /* |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 104 | [] BSSAP.receive(tr_BSSMAP_AssignmentFail) { |
| 105 | } |
| 106 | */ |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 107 | |
| 108 | /* receive CRCX ACK: transmit MDCX */ |
| 109 | [g_state == MSC_STATE_WAIT_CRCX_ACK] BSSAP.receive(tr_CRCX_ACK) -> value mgcp_rsp { |
| 110 | /* extract connection ID */ |
Harald Welte | 4c11d56 | 2017-11-24 23:39:00 +0100 | [diff] [blame] | 111 | g_mgcp_conn_id := f_MgcpResp_extract_conn_id(mgcp_rsp); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 112 | g_state := MSC_STATE_WAIT_MDCX_ACK; |
| 113 | var MgcpTransId trans_id := f_mgcp_alloc_tid(); |
| 114 | BSSAP.send(ts_MDCX(trans_id, g_ep_name, "sendrecv", g_call_id, g_mgcp_conn_id)); |
| 115 | } |
| 116 | |
| 117 | /* receive MDCX ACK: wait + transmit CLEAR COMMAND */ |
| 118 | [g_state == MSC_STATE_WAIT_MDCX_ACK] BSSAP.receive(tr_CRCX_ACK) -> value mgcp_rsp { |
| 119 | g_state := MSC_STATE_WAIT_CLEAR_COMPL |
Harald Welte | 8ac9f02 | 2017-11-24 23:48:14 +0100 | [diff] [blame] | 120 | BSSAP.send(ts_BSSMAP_ClearCommand(9)); /* Cause: call control */ |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* CLEAR COMPLETE from BSS (response to CLEAR COMMAND) */ |
Harald Welte | 11e7808 | 2017-11-24 23:42:07 +0100 | [diff] [blame] | 124 | [g_state == MSC_STATE_WAIT_CLEAR_COMPL] BSSAP.receive(tr_BSSMAP_ClearComplete) { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 125 | /* send DLCX */ |
| 126 | g_state := MSC_STATE_WAIT_DLCX_ACK; |
| 127 | var MgcpTransId trans_id := f_mgcp_alloc_tid(); |
| 128 | BSSAP.send(ts_DLCX(trans_id, g_ep_name, g_call_id)); |
| 129 | } |
| 130 | |
| 131 | [g_state == MSC_STATE_WAIT_DLCX_ACK] BSSAP.receive(tr_DLCX_ACK) { |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 132 | BSSAP.send(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ); |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 133 | setverdict(pass); |
| 134 | self.stop; |
| 135 | } |
| 136 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 137 | /* TODO: CLEAR REQUEST from BSS */ |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 138 | |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 139 | [] BSSAP.receive(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
Harald Welte | c82eef4 | 2017-11-24 20:40:12 +0100 | [diff] [blame] | 140 | setverdict(fail); |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 141 | self.stop; |
| 142 | } |
| 143 | |
| 144 | [] BSSAP.receive(PDU_BSSAP:?) -> value bssap { |
| 145 | log("Received unhandled SCCP-CO: ", bssap); |
| 146 | } |
Harald Welte | 98f1f41 | 2017-11-25 01:21:49 +0100 | [diff] [blame] | 147 | |
| 148 | /* Guard timer has expired, close connection */ |
| 149 | [] T.timeout { |
Harald Welte | 6811d10 | 2019-04-14 22:23:14 +0200 | [diff] [blame] | 150 | BSSAP.send(RAN_Conn_Prim:MSC_CONN_PRIM_DISC_REQ); |
Harald Welte | 458fd37 | 2018-03-21 11:26:23 +0100 | [diff] [blame] | 151 | setverdict(fail, "Timeout of guard timer"); |
Harald Welte | 98f1f41 | 2017-11-25 01:21:49 +0100 | [diff] [blame] | 152 | self.stop; |
| 153 | } |
| 154 | |
Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | } |