Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 1 | module BSC_MS_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 MobileL3_Types all; |
| 11 | import from MobileL3_CommonIE_Types all; |
| 12 | import from L3_Templates all; |
| 13 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 14 | import from MGCP_Types all; |
| 15 | import from MGCP_Templates all; |
| 16 | import from SDP_Types all; |
| 17 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 18 | /* this component represents a single subscriber connection at the MSC. |
| 19 | * There is a 1:1 mapping between SCCP connections and BSSAP_ConnHdlr components. |
| 20 | * We inherit all component variables, ports, functions, ... from BSSAP_ConnHdlr */ |
| 21 | type component BSC_MS_ConnHdlr extends BSSAP_ConnHdlr { |
| 22 | /* SCCP Connecction Identifier for the underlying SCCP connection */ |
| 23 | var integer g_sccp_conn_id; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 24 | var MgcpConnectionId g_mgcp_conn_id; |
| 25 | var SDP_Message g_sdp; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +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 | be620f6 | 2017-11-25 00:23:54 +0100 | [diff] [blame^] | 30 | private function CreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind, charstring id) |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 31 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 32 | log("Incoming SCCP Connection on BSC ?!?"); |
| 33 | self.stop; |
| 34 | } |
| 35 | |
| 36 | /* Callback function from general BSSMAP_Emulation whenever a connectionless |
| 37 | * BSSMAP message arrives. Can retunr a PDU_BSSAP that should be sent in return */ |
| 38 | private function UnitdataCallback(PDU_BSSAP bssap) |
| 39 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP { |
| 40 | var template PDU_BSSAP resp := omit; |
| 41 | |
| 42 | if (match(bssap, tr_BSSMAP_Reset)) { |
| 43 | resp := ts_BSSMAP_ResetAck; |
| 44 | } |
| 45 | |
| 46 | return resp; |
| 47 | } |
| 48 | |
| 49 | const BssmapOps BSC_MS_BssmapOps := { |
| 50 | create_cb := refers(CreateCallback), |
| 51 | unitdata_cb := refers(UnitdataCallback) |
| 52 | } |
| 53 | |
| 54 | |
| 55 | function f_gen_cl3(hexstring imsi) return PDU_BSSAP { |
| 56 | var MobileIdentityLV mi := valueof(ts_MI_IMSI_LV(imsi)); |
| 57 | var PDU_ML3_MS_NW l3 := valueof(ts_CM_SERV_REQ('0001'B, mi)); |
| 58 | var BSSMAP_IE_CellIdentifier cell_id := valueof(ts_CellID_LAC_CI(23, 42)); |
| 59 | var PDU_BSSAP bssap := valueof(ts_BSSMAP_ComplL3(cell_id, enc_PDU_ML3_MS_NW(l3))); |
| 60 | return bssap; |
| 61 | } |
| 62 | |
| 63 | /* main function processing various incoming events */ |
| 64 | function main(SCCP_PAR_Address sccp_addr_own, SCCP_PAR_Address sccp_addr_remote) |
| 65 | runs on BSC_MS_ConnHdlr { |
| 66 | var PDU_BSSAP bssap; |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 67 | var MgcpCommand mgcp_cmd; |
| 68 | var MgcpResponse mgcp_resp; |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 69 | |
| 70 | log("Starting main of BSC_MS_ConnHdlr"); |
| 71 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 72 | g_mgcp_conn_id := f_mgcp_alloc_conn_id(); |
| 73 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 74 | /* generate and send the Complete Layer3 Info */ |
| 75 | bssap := f_gen_cl3('901770123456789'H); |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 76 | var BSSAP_Conn_Req creq := { |
| 77 | addr_peer := sccp_addr_remote, |
| 78 | addr_own := sccp_addr_own, |
| 79 | bssap := bssap |
| 80 | } |
| 81 | BSSAP.send(creq); |
| 82 | |
| 83 | while (true) { |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 84 | /* TODO: Use an actual state machine to care about ordering */ |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 85 | alt { |
| 86 | /* new SCCP-level connection indication from BSC */ |
Harald Welte | ab05d6c | 2017-11-23 18:50:52 +0100 | [diff] [blame] | 87 | [] BSSAP.receive(tr_BSSMAP_AssignmentReq) -> value bssap { |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 88 | /* TODO: Read CIC */ |
| 89 | /* respond with ASSIGNMENT COMPL */ |
| 90 | BSSAP.send(ts_BSSMAP_AssignmentComplete(bssap.pdu.bssmap.assignmentRequest.circuitIdentityCode)); |
| 91 | } |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 92 | |
| 93 | /* CRCX -> OK */ |
| 94 | [] BSSAP.receive(tr_CRCX) -> value mgcp_cmd { |
| 95 | g_sdp := valueof(ts_SDP("127.0.0.1", "127.0.0.1", "foo", "21", 1000, { "98" }, |
| 96 | { valueof(ts_SDP_rtpmap(98, "AMR/8000")), |
| 97 | valueof(ts_SDP_ptime(20)) })); |
| 98 | /* respond with CRCX_ACK */ |
| 99 | BSSAP.send(ts_CRCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 100 | } |
| 101 | |
| 102 | /* MDCX -> OK */ |
| 103 | [] BSSAP.receive(tr_MDCX) -> value mgcp_cmd { |
| 104 | /* respond with CRCX_ACK */ |
| 105 | BSSAP.send(ts_MDCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id, g_sdp)); |
| 106 | } |
| 107 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 108 | /* CLEAR COMMAND from MSC; respond with CLEAR COMPLETE) */ |
| 109 | [] BSSAP.receive(tr_BSSMAP_ClearCommand) -> value bssap { |
| 110 | BSSAP.send(ts_BSSMAP_ClearComplete); |
| 111 | /* FIXME: local release? */ |
| 112 | } |
| 113 | |
Harald Welte | c859df5 | 2017-11-24 23:41:41 +0100 | [diff] [blame] | 114 | /* DLCX -> OK */ |
| 115 | [] BSSAP.receive(tr_DLCX) -> value mgcp_cmd { |
| 116 | BSSAP.send(ts_DLCX_ACK(mgcp_cmd.line.trans_id, g_mgcp_conn_id)); |
| 117 | } |
| 118 | |
Harald Welte | b3414b2 | 2017-11-23 18:22:10 +0100 | [diff] [blame] | 119 | [] BSSAP.receive(tr_BSSAP_DTAP) -> value bssap { |
| 120 | var PDU_ML3_MS_NW l3 := dec_PDU_ML3_MS_NW(bssap.pdu.dtap); |
| 121 | log("Unhandled DTAP ", l3); |
| 122 | } |
| 123 | |
| 124 | [] BSSAP.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_IND) { |
| 125 | self.stop; |
| 126 | } |
| 127 | |
| 128 | [] BSSAP.receive(PDU_BSSAP:?) -> value bssap { |
| 129 | log("Received unhandled SCCP-CO: ", bssap); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } |