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