Harald Welte | 28d943e | 2017-11-25 15:00:50 +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 | |
| 10 | import from MGCP_Types all; |
| 11 | import from MGCP_Templates all; |
| 12 | import from SDP_Types all; |
| 13 | |
| 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 { |
| 18 | /* SCCP Connecction Identifier for the underlying SCCP connection */ |
| 19 | var integer g_sccp_conn_id; |
| 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; |
| 25 | } |
| 26 | |
| 27 | /* Callback function from general BSSMAP_Emulation whenever a new incoming |
| 28 | * SCCP connection arrivces. Must create + start a new component */ |
| 29 | private function CreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind, charstring id) |
| 30 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 31 | var MSC_ConnHdlr vc_conn; |
| 32 | /* Create a new BSSAP_ConnHdlr component */ |
| 33 | vc_conn := MSC_ConnHdlr.create(g_bssmap_id & "-Conn-" & int2str(conn_ind.connectionId)); |
| 34 | /* connect it to the port */ |
| 35 | connect(vc_conn:BSSAP, self:CLIENT); |
| 36 | /* start it */ |
| 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; |
| 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 | |
| 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 | |
| 70 | /* main function processing various incoming events */ |
| 71 | function main(integer connection_id, integer e1_timeslot) runs on MSC_ConnHdlr { |
| 72 | var MgcpResponse mgcp_rsp; |
| 73 | timer T := 5.0; |
| 74 | |
| 75 | g_sccp_conn_id := connection_id; |
| 76 | g_call_id := f_mgcp_alloc_call_id(); |
| 77 | g_ep_name := hex2str(int2hex(e1_timeslot, 1)) & "@mgw"; |
| 78 | |
| 79 | /* we just accepted an incoming SCCP connection, start guard timer */ |
| 80 | T.start; |
| 81 | |
| 82 | while (true) { |
| 83 | var PDU_BSSAP bssap; |
| 84 | alt { |
| 85 | /* new SCCP-level connection indication from BSC */ |
| 86 | [g_state == MSC_STATE_NONE] BSSAP.receive(tr_BSSMAP_ComplL3) -> value bssap { |
| 87 | /* respond with ASSIGNMENT CMD */ |
| 88 | g_state := MSC_STATE_WAIT_ASS_COMPL; |
Harald Welte | cc7e4dc | 2017-12-14 21:55:10 +0100 | [diff] [blame^] | 89 | var BSSMAP_IE_AoIP_TransportLayerAddress tla; |
| 90 | tla := valueof(ts_BSSMAP_IE_AoIP_TLA({ipv4:='01020304'O}, 12345)); |
| 91 | BSSAP.send(ts_BSSMAP_AssignmentReq(omit, tla)); |
Harald Welte | 28d943e | 2017-11-25 15:00:50 +0100 | [diff] [blame] | 92 | } |
Harald Welte | cc7e4dc | 2017-12-14 21:55:10 +0100 | [diff] [blame^] | 93 | [g_state == MSC_STATE_WAIT_ASS_COMPL] BSSAP.receive(tr_BSSMAP_AssignmentComplete(*,?)) { |
Harald Welte | 28d943e | 2017-11-25 15:00:50 +0100 | [diff] [blame] | 94 | /* FIXME: Send MGCP CRCX */ |
| 95 | g_state := MSC_STATE_WAIT_CRCX_ACK; |
| 96 | var MgcpTransId trans_id := f_mgcp_alloc_tid(); |
| 97 | //template SDP_Message sdp := omit; |
| 98 | BSSAP.send(ts_CRCX(trans_id, g_ep_name, "recvonly", g_call_id)); //, sdp)); |
| 99 | } |
| 100 | /* |
| 101 | [] BSSAP.receive(tr_BSSMAP_AssignmentFail) { |
| 102 | } |
| 103 | */ |
| 104 | |
| 105 | /* receive CRCX ACK: transmit MDCX */ |
| 106 | [g_state == MSC_STATE_WAIT_CRCX_ACK] BSSAP.receive(tr_CRCX_ACK) -> value mgcp_rsp { |
| 107 | /* extract connection ID */ |
| 108 | g_mgcp_conn_id := f_MgcpResp_extract_conn_id(mgcp_rsp); |
| 109 | g_state := MSC_STATE_WAIT_MDCX_ACK; |
| 110 | var MgcpTransId trans_id := f_mgcp_alloc_tid(); |
| 111 | BSSAP.send(ts_MDCX(trans_id, g_ep_name, "sendrecv", g_call_id, g_mgcp_conn_id)); |
| 112 | } |
| 113 | |
| 114 | /* receive MDCX ACK: wait + transmit CLEAR COMMAND */ |
| 115 | [g_state == MSC_STATE_WAIT_MDCX_ACK] BSSAP.receive(tr_CRCX_ACK) -> value mgcp_rsp { |
| 116 | g_state := MSC_STATE_WAIT_CLEAR_COMPL |
| 117 | BSSAP.send(ts_BSSMAP_ClearCommand(9)); /* Cause: call control */ |
| 118 | } |
| 119 | |
| 120 | /* CLEAR COMPLETE from BSS (response to CLEAR COMMAND) */ |
| 121 | [g_state == MSC_STATE_WAIT_CLEAR_COMPL] BSSAP.receive(tr_BSSMAP_ClearComplete) { |
| 122 | /* send DLCX */ |
| 123 | g_state := MSC_STATE_WAIT_DLCX_ACK; |
| 124 | var MgcpTransId trans_id := f_mgcp_alloc_tid(); |
| 125 | BSSAP.send(ts_DLCX(trans_id, g_ep_name, g_call_id)); |
| 126 | } |
| 127 | |
| 128 | [g_state == MSC_STATE_WAIT_DLCX_ACK] BSSAP.receive(tr_DLCX_ACK) { |
| 129 | BSSAP.send(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ); |
| 130 | setverdict(pass); |
| 131 | self.stop; |
| 132 | } |
| 133 | |
| 134 | /* TODO: CLEAR REQUEST from BSS */ |
| 135 | |
| 136 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
| 137 | setverdict(fail); |
| 138 | self.stop; |
| 139 | } |
| 140 | |
| 141 | [] BSSAP.receive(PDU_BSSAP:?) -> value bssap { |
| 142 | log("Received unhandled SCCP-CO: ", bssap); |
| 143 | } |
| 144 | |
| 145 | /* Guard timer has expired, close connection */ |
| 146 | [] T.timeout { |
| 147 | BSSAP.send(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ); |
| 148 | setverdict(inconc); |
| 149 | self.stop; |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } |