Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 1 | module BSSGP_Emulation { |
| 2 | import from NS_Types all; |
| 3 | import from NS_Emulation all; |
| 4 | import from BSSGP_Types all; |
| 5 | import from BSSGP_Helper_Functions all; |
| 6 | import from IPL4asp_Types all; |
| 7 | |
| 8 | type record BssgpStatusIndication { |
| 9 | Nsei nsei, |
| 10 | BssgpBvci bvci, |
| 11 | BvcState state |
| 12 | } |
| 13 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 14 | template BssgpStatusIndication t_BssgpStsInd(template Nsei nsei, template BssgpBvci bvci, template BvcState state) := { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 15 | nsei := nsei, |
| 16 | bvci := bvci, |
| 17 | state := state |
| 18 | } |
| 19 | |
| 20 | type enumerated BvcState { |
| 21 | BVC_S_BLOCKED, |
| 22 | BVC_S_UNBLOCKED |
| 23 | }; |
| 24 | |
| 25 | /* port from our (internal) point of view */ |
| 26 | type port BSSGP_SP_PT message { |
| 27 | in BssgpPdu; |
| 28 | out BssgpPdu, |
| 29 | NsStatusIndication, |
| 30 | BssgpStatusIndication, |
| 31 | ASP_Event; |
| 32 | } with { extension "internal" }; |
| 33 | |
| 34 | /* port from the user point of view */ |
| 35 | type port BSSGP_PT message { |
| 36 | in ASP_Event, |
| 37 | NsStatusIndication, |
| 38 | BssgpStatusIndication, |
| 39 | BssgpPdu; |
| 40 | out BssgpPdu; |
| 41 | } with { extension "internal" }; |
| 42 | |
| 43 | function BssgpStart() runs on BSSGP_CT { |
| 44 | f_init(); |
| 45 | f_ScanEvents(); |
| 46 | } |
| 47 | |
| 48 | private function f_init() runs on BSSGP_CT { |
| 49 | /* Connect the UDP socket */ |
| 50 | f_change_state(BVC_S_BLOCKED); |
| 51 | } |
| 52 | |
| 53 | type component BSSGP_CT { |
| 54 | /* UDP port towards the bottom (IUT) */ |
| 55 | port NS_PT BSCP; |
| 56 | /* NS-User SAP towards the user */ |
| 57 | port BSSGP_SP_PT BSSGP_SP; |
| 58 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 59 | var boolean g_sgsn_role := true; |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 60 | var BvcState g_ptp_bvc_state := BVC_S_BLOCKED; |
| 61 | timer g_T1 := 15.0; |
| 62 | timer g_T2 := 60.0; |
| 63 | } |
| 64 | |
| 65 | modulepar { |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 66 | Nsvci mp_nsei := 96; |
| 67 | Nsvci mp_bvci := 196; |
Harald Welte | 244cd8a | 2017-08-26 09:25:20 +0200 | [diff] [blame] | 68 | BssgpCellId mp_cellid := { ra_id := { lai := { mcc_mnc := '262F42'H, lac := 13135}, rac := 0 }, cell_id := 20960 }; |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 71 | function f_BnsUdReq(template BssgpPdu pdu, BssgpBvci bvci := mp_bvci) return NsUnitdataRequest { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 72 | var NsUnitdataRequest udr := { |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 73 | bvci := bvci, |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 74 | nsei := mp_nsei, |
| 75 | /* for some weird reason we get "Dynamic test case error: Text encoder: Encoding an |
| 76 | * unbound integer value." when trying to send the reocrd rather than the octetstring */ |
| 77 | //sdu := omit, |
| 78 | //bssgp := valueof(pdu) |
| 79 | sdu := f_BSSGP_compact_len(enc_BssgpPdu(valueof(pdu))), |
| 80 | bssgp := omit |
| 81 | } |
| 82 | return udr; |
| 83 | } |
| 84 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 85 | function f_BnsUdInd(template BssgpPdu pdu, template BssgpBvci bvci := mp_bvci) return template NsUnitdataIndication { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 86 | var template NsUnitdataIndication udi := { |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 87 | bvci := bvci, |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 88 | nsei := mp_nsei, |
| 89 | sdu := *, |
| 90 | bssgp := pdu |
| 91 | } |
| 92 | return udi; |
| 93 | } |
| 94 | |
| 95 | private function f_change_state(BvcState new_state) runs on BSSGP_CT { |
| 96 | log("BSSGP State Transition: ", g_ptp_bvc_state, " -> ", new_state); |
| 97 | g_ptp_bvc_state := new_state; |
| 98 | BSSGP_SP.send(t_BssgpStsInd(mp_nsei, mp_bvci, g_ptp_bvc_state)); |
| 99 | } |
| 100 | |
| 101 | private function f_sendReset() runs on BSSGP_CT { |
| 102 | var BssgpPdu pdu := valueof(t_BVC_RESET(BSSGP_CAUSE_OM_INTERVENTION, mp_bvci, mp_cellid)); |
| 103 | log("PDU: ", pdu); |
| 104 | log("ENC: ", enc_BssgpPdu(pdu)); |
| 105 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 106 | /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */ |
| 107 | BSCP.send(f_BnsUdReq(pdu, 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 108 | g_T2.start; |
| 109 | //f_change_state(BVC_S_WAIT_RESET); |
| 110 | } |
| 111 | |
| 112 | private function f_sendUnblock() runs on BSSGP_CT { |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 113 | BSCP.send(f_BnsUdReq(t_BVC_UNBLOCK(mp_bvci), 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 114 | g_T1.start; |
| 115 | } |
| 116 | |
| 117 | private function f_sendBlock(BssgpCause cause) runs on BSSGP_CT { |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 118 | BSCP.send(f_BnsUdReq(t_BVC_BLOCK(mp_bvci, cause), 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 119 | g_T1.start; |
| 120 | } |
| 121 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 122 | private function f_sendStatus(BssgpCause cause, BssgpPdu pdu) runs on BSSGP_CT { |
| 123 | /* FIXME: Make sure correct Signaling or PTP BVCI is used! */ |
| 124 | BSCP.send(f_BnsUdReq(t_BSSGP_STATUS({ t_BSSGP_IE_Cause(cause), t_BSSGP_IE_Bvci(mp_bvci), t_BSSGP_IE_PDU(pdu)}))); |
| 125 | } |
| 126 | |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 127 | altstep as_allstate() runs on BSSGP_CT { |
| 128 | var NsUnitdataIndication udi; |
| 129 | var NsStatusIndication nsi; |
| 130 | var ASP_Event evt; |
| 131 | |
| 132 | /* Respond to BLOCK for wrong NSVCI */ |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 133 | [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(?, ?), 0)) -> value udi { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 134 | log("Rx BVC-BLOCK for unknown BVCI"); |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 135 | f_sendStatus(BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 138 | /* Respond to RESET with correct BVCI/CellID */ |
| 139 | [] BSCP.receive(f_BnsUdInd(t_BVC_RESET(?, mp_bvci, mp_cellid), 0)) -> value udi { |
| 140 | log("Rx BVC-RESET for Our BVCI=", mp_bvci); |
| 141 | BSCP.send(f_BnsUdReq(t_BVC_RESET_ACK(mp_bvci, mp_cellid), 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 142 | f_change_state(BVC_S_UNBLOCKED); |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Respond to RESET for signalling BVCI 0 */ |
| 146 | [] BSCP.receive(f_BnsUdInd(t_BVC_RESET(?, 0, mp_cellid), 0)) -> value udi { |
| 147 | log("Rx BVC-RESET for Signaling BVCI=0"); |
| 148 | BSCP.send(f_BnsUdReq(t_BVC_RESET_ACK(0, mp_cellid), 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Respond to RESET with wrong NSEI/NSVCI */ |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 152 | [] BSCP.receive(f_BnsUdInd(t_BVC_RESET(?, ?, ?), 0)) -> value udi { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 153 | log("Rx BVC-RESET for unknown BVCI"); |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 154 | f_sendStatus(BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* default case of handling unknown PDUs */ |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 158 | [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 159 | log("Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_ptp_bvc_state); |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 160 | f_sendStatus(BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 161 | } |
| 162 | /* Forwarding of ASP_Event and NsStatusIndication to user */ |
| 163 | [] BSCP.receive(ASP_Event:?) -> value evt { BSSGP_SP.send(evt); } |
| 164 | [] BSCP.receive(NsStatusIndication:?) -> value nsi { |
| 165 | /* if we just became NS-unblocked, send a BCC-RESET */ |
| 166 | if (nsi.old_state != NSE_S_ALIVE_UNBLOCKED and nsi.new_state == NSE_S_ALIVE_UNBLOCKED) { |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 167 | if (g_sgsn_role == false) { |
| 168 | f_sendReset(); |
| 169 | } |
| 170 | /* Idea: We coudl send BVC-UNBLOCK here like some SGSN do */ |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 171 | } |
| 172 | BSSGP_SP.send(nsi); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private function f_ScanEvents() runs on BSSGP_CT { |
| 177 | var NsUnitdataIndication udi; |
| 178 | var BssgpPdu bs_pdu; |
| 179 | var default d; |
| 180 | |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 181 | |
| 182 | log("matching against ", t_BVC_RESET(?, mp_bvci, mp_cellid)); |
| 183 | |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 184 | d := activate(as_allstate()); |
| 185 | |
| 186 | while (true) { |
| 187 | if (g_ptp_bvc_state == BVC_S_BLOCKED) { |
| 188 | alt { |
| 189 | [] g_T1.timeout { |
| 190 | f_sendUnblock(); |
| 191 | } |
| 192 | [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK_ACK(mp_bvci))) { |
| 193 | g_T1.stop; |
| 194 | f_change_state(BVC_S_UNBLOCKED); |
| 195 | } |
| 196 | } |
| 197 | } else if (g_ptp_bvc_state == BVC_S_UNBLOCKED) { |
| 198 | alt { |
| 199 | /* bogus unblock, just respond with ACK */ |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 200 | [] BSCP.receive(f_BnsUdInd(t_BVC_UNBLOCK(mp_bvci), 0)) -> value udi { |
| 201 | BSCP.send(f_BnsUdReq(t_BVC_UNBLOCK_ACK(mp_bvci), 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 202 | } |
| 203 | /* Respond to BLOCK with BLOCK-ACK + change state */ |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 204 | [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(mp_bvci, ?), 0)) -> value udi { |
| 205 | BSCP.send(f_BnsUdReq(t_BVC_BLOCK_ACK(mp_bvci), 0)); |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 206 | g_T1.stop; |
| 207 | f_change_state(BVC_S_BLOCKED); |
| 208 | } |
| 209 | [] g_T1.timeout { |
| 210 | f_sendBlock(BSSGP_CAUSE_OM_INTERVENTION); |
| 211 | } |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 212 | [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(mp_bvci), 0)) -> value udi { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 213 | g_T1.stop; |
| 214 | f_change_state(BVC_S_BLOCKED); |
| 215 | } |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 216 | [] BSCP.receive(f_BnsUdInd(t_BVC_RESET_ACK(mp_bvci, mp_cellid), 0)) -> value udi { |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 217 | g_T2.stop; |
| 218 | f_change_state(BVC_S_UNBLOCKED); |
| 219 | } |
Harald Welte | 13d391e | 2017-07-23 19:52:33 +0200 | [diff] [blame] | 220 | |
| 221 | /* simply acknowledge all Flow Control Messages */ |
| 222 | [g_sgsn_role] BSCP.receive(f_BnsUdInd(t_BVC_FC_BVC)) { |
| 223 | BSCP.send(f_BnsUdReq(t_BVC_FC_BVC_ACK)); |
| 224 | } |
| 225 | [g_sgsn_role] BSCP.receive(f_BnsUdInd(t_BVC_FC_MS)) { |
| 226 | BSCP.send(f_BnsUdReq(t_BVC_FC_MS_ACK)); |
| 227 | } |
| 228 | |
Harald Welte | 1733a38 | 2017-07-23 17:26:17 +0200 | [diff] [blame] | 229 | /* BSSGP-UNITDATA PDUs from network to NS-UNITDATA.ind to user */ |
| 230 | [] BSCP.receive(f_BnsUdInd(tr_BSSGP_type(DL_UNITDATA))) -> value udi { |
| 231 | BSSGP_SP.send(udi.bssgp); |
| 232 | } |
| 233 | [] BSCP.receive(f_BnsUdInd(tr_BSSGP_type(UL_UNITDATA))) -> value udi { |
| 234 | BSSGP_SP.send(udi.bssgp); |
| 235 | } |
| 236 | /* pass virtually any PDU from user to NS-UNITDATA PDU on network */ |
| 237 | [] BSSGP_SP.receive(BssgpPdu:?) -> value bs_pdu { |
| 238 | BSCP.send(f_BnsUdReq(bs_pdu)); |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | } /* while */ |
| 245 | //deactivate(d); |
| 246 | } |
| 247 | } |