blob: 5937a278f492a48fd07e11b7d6649fb367c2a582 [file] [log] [blame]
Harald Welte1733a382017-07-23 17:26:17 +02001module 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 Welte13d391e2017-07-23 19:52:33 +020014 template BssgpStatusIndication t_BssgpStsInd(template Nsei nsei, template BssgpBvci bvci, template BvcState state) := {
Harald Welte1733a382017-07-23 17:26:17 +020015 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 Welte13d391e2017-07-23 19:52:33 +020059 var boolean g_sgsn_role := true;
Harald Welte1733a382017-07-23 17:26:17 +020060 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 Welte13d391e2017-07-23 19:52:33 +020066 Nsvci mp_nsei := 96;
67 Nsvci mp_bvci := 196;
Harald Welte1733a382017-07-23 17:26:17 +020068 BssgpCellId mp_cellid := { ra_id := { lai := { mcc_mnc := '234F04'H, lac := 200}, rac := 0 }, cell_id := 20960 };
69 };
70
Harald Welte13d391e2017-07-23 19:52:33 +020071 function f_BnsUdReq(template BssgpPdu pdu, BssgpBvci bvci := mp_bvci) return NsUnitdataRequest {
Harald Welte1733a382017-07-23 17:26:17 +020072 var NsUnitdataRequest udr := {
Harald Welte13d391e2017-07-23 19:52:33 +020073 bvci := bvci,
Harald Welte1733a382017-07-23 17:26:17 +020074 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 Welte13d391e2017-07-23 19:52:33 +020085 function f_BnsUdInd(template BssgpPdu pdu, template BssgpBvci bvci := mp_bvci) return template NsUnitdataIndication {
Harald Welte1733a382017-07-23 17:26:17 +020086 var template NsUnitdataIndication udi := {
Harald Welte13d391e2017-07-23 19:52:33 +020087 bvci := bvci,
Harald Welte1733a382017-07-23 17:26:17 +020088 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 Welte13d391e2017-07-23 19:52:33 +0200106 /* BVC-RESET is always sent via the SIGNALLING BVCI, see Table 5.4.1 */
107 BSCP.send(f_BnsUdReq(pdu, 0));
Harald Welte1733a382017-07-23 17:26:17 +0200108 g_T2.start;
109 //f_change_state(BVC_S_WAIT_RESET);
110 }
111
112 private function f_sendUnblock() runs on BSSGP_CT {
Harald Welte13d391e2017-07-23 19:52:33 +0200113 BSCP.send(f_BnsUdReq(t_BVC_UNBLOCK(mp_bvci), 0));
Harald Welte1733a382017-07-23 17:26:17 +0200114 g_T1.start;
115 }
116
117 private function f_sendBlock(BssgpCause cause) runs on BSSGP_CT {
Harald Welte13d391e2017-07-23 19:52:33 +0200118 BSCP.send(f_BnsUdReq(t_BVC_BLOCK(mp_bvci, cause), 0));
Harald Welte1733a382017-07-23 17:26:17 +0200119 g_T1.start;
120 }
121
Harald Welte13d391e2017-07-23 19:52:33 +0200122 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 Welte1733a382017-07-23 17:26:17 +0200127 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 Welte13d391e2017-07-23 19:52:33 +0200133 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK(?, ?), 0)) -> value udi {
Harald Welte1733a382017-07-23 17:26:17 +0200134 log("Rx BVC-BLOCK for unknown BVCI");
Harald Welte13d391e2017-07-23 19:52:33 +0200135 f_sendStatus(BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp);
Harald Welte1733a382017-07-23 17:26:17 +0200136 }
137
Harald Welte13d391e2017-07-23 19:52:33 +0200138 /* 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 Welte1733a382017-07-23 17:26:17 +0200142 f_change_state(BVC_S_UNBLOCKED);
Harald Welte13d391e2017-07-23 19:52:33 +0200143 }
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 Welte1733a382017-07-23 17:26:17 +0200149 }
150
151 /* Respond to RESET with wrong NSEI/NSVCI */
Harald Welte13d391e2017-07-23 19:52:33 +0200152 [] BSCP.receive(f_BnsUdInd(t_BVC_RESET(?, ?, ?), 0)) -> value udi {
Harald Welte1733a382017-07-23 17:26:17 +0200153 log("Rx BVC-RESET for unknown BVCI");
Harald Welte13d391e2017-07-23 19:52:33 +0200154 f_sendStatus(BSSGP_CAUSE_BVCI_UNKNOWN, udi.bssgp);
Harald Welte1733a382017-07-23 17:26:17 +0200155 }
156
157 /* default case of handling unknown PDUs */
Harald Welte13d391e2017-07-23 19:52:33 +0200158 [] BSCP.receive(f_BnsUdInd(?, ?)) -> value udi {
Harald Welte1733a382017-07-23 17:26:17 +0200159 log("Rx Unexpected BSSGP PDU ", udi.bssgp," in state ", g_ptp_bvc_state);
Harald Welte13d391e2017-07-23 19:52:33 +0200160 f_sendStatus(BSSGP_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, udi.bssgp);
Harald Welte1733a382017-07-23 17:26:17 +0200161 }
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 Welte13d391e2017-07-23 19:52:33 +0200167 if (g_sgsn_role == false) {
168 f_sendReset();
169 }
170 /* Idea: We coudl send BVC-UNBLOCK here like some SGSN do */
Harald Welte1733a382017-07-23 17:26:17 +0200171 }
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 Welte13d391e2017-07-23 19:52:33 +0200181
182 log("matching against ", t_BVC_RESET(?, mp_bvci, mp_cellid));
183
Harald Welte1733a382017-07-23 17:26:17 +0200184 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 Welte13d391e2017-07-23 19:52:33 +0200200 [] 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 Welte1733a382017-07-23 17:26:17 +0200202 }
203 /* Respond to BLOCK with BLOCK-ACK + change state */
Harald Welte13d391e2017-07-23 19:52:33 +0200204 [] 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 Welte1733a382017-07-23 17:26:17 +0200206 g_T1.stop;
207 f_change_state(BVC_S_BLOCKED);
208 }
209 [] g_T1.timeout {
210 f_sendBlock(BSSGP_CAUSE_OM_INTERVENTION);
211 }
Harald Welte13d391e2017-07-23 19:52:33 +0200212 [] BSCP.receive(f_BnsUdInd(t_BVC_BLOCK_ACK(mp_bvci), 0)) -> value udi {
Harald Welte1733a382017-07-23 17:26:17 +0200213 g_T1.stop;
214 f_change_state(BVC_S_BLOCKED);
215 }
Harald Welte13d391e2017-07-23 19:52:33 +0200216 [] BSCP.receive(f_BnsUdInd(t_BVC_RESET_ACK(mp_bvci, mp_cellid), 0)) -> value udi {
Harald Welte1733a382017-07-23 17:26:17 +0200217 g_T2.stop;
218 f_change_state(BVC_S_UNBLOCKED);
219 }
Harald Welte13d391e2017-07-23 19:52:33 +0200220
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 Welte1733a382017-07-23 17:26:17 +0200229 /* 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}