blob: 465e3980ee08ef1032dd2412f5bf6d9a65495605 [file] [log] [blame]
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +02001module PCUIF_RAW_Components {
2
3/*
4 * Components for (RAW) PCU test cases.
5 *
6 * (C) 2019 Vadim Yanitskiy <axilirator@gmail.com>
7 *
8 * All rights reserved.
9 *
10 * Released under the terms of GNU General Public License, Version 2 or
11 * (at your option) any later version.
12 *
13 * SPDX-License-Identifier: GPL-2.0-or-later
14 */
15
16import from IPL4asp_Types all;
17import from UD_Types all;
18
19import from PCUIF_Types all;
20import from PCUIF_CodecPort all;
21
22/* Component communication diagram:
23 *
24 * +-----+ +----------+ +---------+
25 * | MTC +---------------+ PCUIF_CT +------+ OsmoPCU |
26 * +--+--+ +----+-----+ +---------+
27 * | |
28 * | |
29 * | |
30 * | +-----------+ | +---------------+
31 * +----+ BTS_CT #1 +------+ | ClckGen_CT #1 |
32 * | +-----+-----+ | +-------+-------+
33 * | | | |
34 * | +---------------------------+
35 * | |
36 * | +-----------+ | +---------------+
37 * +----+ BTS_CT #2 +------+ | ClckGen_CT #2 |
38 * | +-----+-----+ | +-------+-------+
39 * | | | |
40 * | +---------------------------+
41 * | |
42 * | +-----------+ | +---------------+
43 * +----+ BTS_CT #N +------+ | ClckGen_CT #N |
44 * +-----+-----+ +-------+-------+
45 * | |
46 * +---------------------------+
47 */
48
49/* Events are used by the components to indicate that something
50 * has happened, e.g. we have got a connection from the PCU. */
51type enumerated RAW_PCU_EventType {
52 /* Events related to RAW_PCUIF_CT */
53 PCU_EV_DISCONNECT, /*!< OsmoPCU has disconnected */
54 PCU_EV_CONNECT, /*!< OsmoPCU is now connected */
55
56 /* Events related to RAW_PCU_BTS_CT */
57 BTS_EV_SI13_NEGO, /*!< SI13 negotiation complete */
58
59 /* TDMA clock related events (TDMA frame-number in parameters) */
60 TDMA_EV_PDTCH_BLOCK_BEG, /*!< 1/4 bursts of a PDTCH block on both Uplink and Downlink */
61 TDMA_EV_PDTCH_BLOCK_END, /*!< 4/4 bursts of a PDTCH block on both Uplink and Downlink */
62 TDMA_EV_PTCCH_DL_BLOCK, /*!< 4/4 bursts of a PTCCH block on Downlink */
63 TDMA_EV_PTCCH_UL_BURST, /*!< One Access Burst on PTCCH/U */
64
65 TDMA_EV_PDTCH_BLOCK_SENT /*!< A PDTCH block has been sent to the PCU */
66};
67
68/* Union of all possible parameters of the events */
69type union RAW_PCU_EventParam {
70 integer tdma_fn
71};
72
73type record RAW_PCU_Event {
74 RAW_PCU_EventType event,
75 /* TODO: can we use 'anytype' here? */
76 RAW_PCU_EventParam data optional
77};
78
79template (value) RAW_PCU_Event ts_RAW_PCU_EV(RAW_PCU_EventType event) := {
80 event := event,
81 data := omit
82}
83template RAW_PCU_Event tr_RAW_PCU_EV(template RAW_PCU_EventType event := ?,
84 template RAW_PCU_EventParam data := *) := {
85 event := event,
86 data := data
87}
88
89template (value) RAW_PCU_Event ts_RAW_PCU_CLCK_EV(RAW_PCU_EventType event, integer fn) := {
90 event := event,
91 data := { tdma_fn := fn }
92}
93template RAW_PCU_Event tr_RAW_PCU_CLCK_EV := {
94 event := (TDMA_EV_PDTCH_BLOCK_BEG, TDMA_EV_PDTCH_BLOCK_END,
95 TDMA_EV_PTCCH_DL_BLOCK, TDMA_EV_PTCCH_UL_BURST,
96 TDMA_EV_PDTCH_BLOCK_SENT),
97 data := { tdma_fn := ? }
98}
99
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700100/* Commands are mostly used by the MTC to configure the components
101 * at run-time, e.g. to enable or disable some optional features. */
102type enumerated RAW_PCU_CommandType {
103 TDMA_CMD_ENABLE_PTCCH_UL_FWD /*!< Enable forwarding of TDMA_EV_PTCCH_UL_BURST to the MTC */
104};
105
106type record RAW_PCU_Command {
107 RAW_PCU_CommandType cmd,
108 anytype data optional
109};
110
111template (value) RAW_PCU_Command ts_RAW_PCU_CMD(RAW_PCU_CommandType cmd) := {
112 cmd := cmd,
113 data := omit
114}
115template RAW_PCU_Command tr_RAW_PCU_CMD(template RAW_PCU_CommandType cmd := ?,
116 template anytype data := *) := {
117 cmd := cmd,
118 data := data
119}
120
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200121/* Generic port for messages and events */
122type port RAW_PCU_MSG_PT message {
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700123 inout RAW_PCU_Command;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200124 inout RAW_PCU_Event;
125 inout PCUIF_Message;
126} with { extension "internal" };
127
128/* TDMA frame clock generator */
129type component RAW_PCU_ClckGen_CT {
130 /* One TDMA frame is 4.615 ms long */
131 timer T_TDMAClock := 4.615 / 1000.0;
132 port RAW_PCU_MSG_PT CLCK;
133 var integer fn := 0;
134}
135
136function f_ClckGen_CT_handler()
137runs on RAW_PCU_ClckGen_CT {
138 var integer fn104, fn52, fn13;
139
140 while (true) {
141 fn104 := fn mod 104;
142 fn52 := fn mod 52;
143 fn13 := fn mod 13;
144
145 if (fn13 == 0 or fn13 == 4 or fn13 == 8) {
146 /* 1/4 bursts of a PDTCH block on both Uplink and Downlink */
147 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_BEG, fn));
148 } else if (fn13 == 3 or fn13 == 7 or fn13 == 11) {
149 /* 4/4 bursts of a PDTCH block on both Uplink and Downlink */
150 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_END, fn));
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200151 } else if (fn52 == 12 or fn52 == 38) {
Vadim Yanitskiyb58b7302019-10-06 01:01:48 +0700152 /* 4/4 bursts of a PTCCH (Timing Advance Control) block on Downlink */
153 if (fn104 == 90) {
154 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_DL_BLOCK, fn));
155 }
156 /* One Access Burst on PTCCH/U (goes 3 time-slots after PTCCH/D) */
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200157 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_UL_BURST, fn));
158 }
159
160 /* TDMA hyperframe period is (2048 * 51 * 26) frames */
161 fn := (fn + 1) mod (2048 * 51 * 26);
162
163 /* (Re)start TDMA clock timer and wait */
164 T_TDMAClock.start;
165 T_TDMAClock.timeout;
166 }
167}
168
169type record of PCUIF_Message PCUIF_MsgQueue;
170
171/* Enqueue a given message to the end of a given queue */
172private function f_PCUIF_MsgQueue_enqueue(inout PCUIF_MsgQueue queue,
173 in PCUIF_Message msg)
174{
175 queue := queue & { msg };
176}
177
178/* Dequeue the first message of a given queue */
179private function f_PCUIF_MsgQueue_dequeue(inout PCUIF_MsgQueue queue,
180 out PCUIF_Message msg)
181{
182 var integer len := lengthof(queue);
183
184 if (len == 0) {
185 setverdict(fail, "Failed to dequeue a message: the queue is empty!");
186 mtc.stop;
187 }
188
189 /* Store the first message */
190 msg := queue[0];
191
192 /* Remove the first message from queue */
193 if (len > 1) {
194 queue := substr(queue, 1, len - 1);
195 } else {
196 queue := { };
197 }
198}
199
200/* Multiple base stations can be connected to the PCU. This component
201 * represents one BTS with an associated TDMA clock generator. */
202type component RAW_PCU_BTS_CT {
203 /* TDMA clock generator */
204 var RAW_PCU_ClckGen_CT vc_CLCK_GEN;
205 port RAW_PCU_MSG_PT CLCK;
206
207 /* Queues of PCUIF messages to be sent
208 * TODO: we may have multiple PDCH time-slots */
209 var PCUIF_MsgQueue pdtch_data_queue := { };
210 var PCUIF_MsgQueue pdtch_rts_queue := { };
211 var PCUIF_MsgQueue ptcch_rts_queue := { };
212
213 /* Connection towards the PCU interface */
214 port RAW_PCU_MSG_PT PCUIF;
215 /* Connection towards the test case */
216 port RAW_PCU_MSG_PT TC;
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700217
218 /* Whether to forward PTCCH/U burst events to the TC */
219 var boolean cfg_ptcch_burst_fwd := false;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200220}
221
222private altstep as_BTS_CT_MsgQueue(integer bts_nr)
223runs on RAW_PCU_BTS_CT {
224 var PCUIF_Message pcu_msg;
225
226 /* Enqueue DATA.ind and RTS.req messages */
227 [] TC.receive(tr_PCUIF_MSG(PCU_IF_MSG_DATA_IND, bts_nr)) -> value pcu_msg {
228 f_PCUIF_MsgQueue_enqueue(pdtch_data_queue, pcu_msg);
229 repeat;
230 }
231 [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PDTCH)) -> value pcu_msg {
232 f_PCUIF_MsgQueue_enqueue(pdtch_rts_queue, pcu_msg);
233 repeat;
234 }
235 [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg {
236 f_PCUIF_MsgQueue_enqueue(ptcch_rts_queue, pcu_msg);
237 repeat;
238 }
239 /* Forward other messages directly to the PCU */
240 [] TC.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg {
241 PCUIF.send(pcu_msg);
242 repeat;
243 }
244}
245
246private altstep as_BTS_CT_TDMASched(integer bts_nr)
247runs on RAW_PCU_BTS_CT {
248 var PCUIF_Message pcu_msg;
249 var RAW_PCU_Event event;
250
251 [] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_BEG)) -> value event {
252 /* If the RTS queue for PDTCH is not empty, send a message */
253 if (lengthof(pdtch_rts_queue) > 0) {
254 f_PCUIF_MsgQueue_dequeue(pdtch_rts_queue, pcu_msg);
255
256 /* Patch TDMA frame / block number and send */
257 pcu_msg.u.rts_req.fn := event.data.tdma_fn;
258 pcu_msg.u.rts_req.block_nr := 0; /* FIXME! */
259 PCUIF.send(pcu_msg);
260 }
261
262 /* We don't really need to send every frame to OsmoPCU, because
263 * it omits frame numbers not starting at a MAC block. */
264 PCUIF.send(ts_PCUIF_TIME_IND(bts_nr, event.data.tdma_fn));
265 repeat;
266 }
267 [lengthof(pdtch_data_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_END)) -> value event {
268 /* Dequeue a DATA.ind message */
269 f_PCUIF_MsgQueue_dequeue(pdtch_data_queue, pcu_msg);
270
271 /* Patch TDMA frame / block number */
272 pcu_msg.u.data_ind.fn := event.data.tdma_fn;
273 pcu_msg.u.data_ind.block_nr := 0; /* FIXME! */
274
275 PCUIF.send(pcu_msg); /* Send to the PCU and notify the TC */
276 TC.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_SENT, event.data.tdma_fn));
277 repeat;
278 }
279 [lengthof(ptcch_rts_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_DL_BLOCK)) -> value event {
280 /* Dequeue an RTS.req message for PTCCH */
281 f_PCUIF_MsgQueue_dequeue(ptcch_rts_queue, pcu_msg);
282
283 /* Patch TDMA frame / block number and send */
284 pcu_msg.u.rts_req.fn := event.data.tdma_fn;
285 pcu_msg.u.rts_req.block_nr := 0; /* FIXME! */
286 PCUIF.send(pcu_msg);
287 repeat;
288 }
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700289 /* Optional forwarding of PTCCH/U burst indications to the test case */
290 [cfg_ptcch_burst_fwd] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_UL_BURST)) -> value event {
291 TC.send(event);
292 repeat;
293 }
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200294 /* Ignore other clock events (and guard against an empty queue) */
295 [] CLCK.receive(tr_RAW_PCU_CLCK_EV) { repeat; }
296}
297
298function f_BTS_CT_handler(integer bts_nr, PCUIF_info_ind info_ind)
299runs on RAW_PCU_BTS_CT {
300 var PCUIF_Message pcu_msg;
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700301 var RAW_PCU_Command cmd;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200302 var RAW_PCU_Event event;
303
304 /* Init TDMA clock generator (so we can stop and start it) */
305 vc_CLCK_GEN := RAW_PCU_ClckGen_CT.create("ClckGen-" & int2str(bts_nr)) alive;
306 connect(vc_CLCK_GEN:CLCK, self:CLCK);
307
308 /* Wait until the PCU is connected */
309 PCUIF.receive(tr_RAW_PCU_EV(PCU_EV_CONNECT));
310
311 /* TODO: implement ACT.req handling */
312 alt {
313 /* Wait for TXT.ind (PCU_VERSION) and respond with INFO.ind (SI13) */
314 [] PCUIF.receive(tr_PCUIF_TXT_IND(bts_nr, PCU_VERSION, ?)) -> value pcu_msg {
315 log("Rx TXT.ind from the PCU, version is ", pcu_msg.u.txt_ind.text);
316
317 /* Send System Information 13 to the PCU */
318 PCUIF.send(PCUIF_Message:{
319 msg_type := PCU_IF_MSG_INFO_IND,
320 bts_nr := bts_nr,
321 spare := '0000'O,
322 u := { info_ind := info_ind }
323 });
324
325 /* Notify the test case that we're done with SI13 */
326 TC.send(ts_RAW_PCU_EV(BTS_EV_SI13_NEGO));
327
328 /* Start feeding clock to the PCU */
329 vc_CLCK_GEN.start(f_ClckGen_CT_handler());
330 repeat;
331 }
332 /* PCU -> test case forwarding (filter by the BTS number) */
333 [] PCUIF.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg {
334 TC.send(pcu_msg);
335 repeat;
336 }
337
338 /* TC -> [Queue] -> PCU forwarding */
339 [] as_BTS_CT_MsgQueue(bts_nr);
340 /* TDMA scheduler (clock and queue handling) */
341 [] as_BTS_CT_TDMASched(bts_nr);
342
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700343 /* Command handling */
344 [] TC.receive(tr_RAW_PCU_CMD(TDMA_CMD_ENABLE_PTCCH_UL_FWD)) {
345 log("Enabling forwarding of PTCCH/U TDMA events to the TC");
346 cfg_ptcch_burst_fwd := true;
347 repeat;
348 }
349 [] TC.receive(tr_RAW_PCU_CMD) -> value cmd {
350 log("Ignore unhandled command: ", cmd);
351 repeat;
352 }
353
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200354 /* TODO: handle events (e.g. disconnection) from the PCU interface */
355 [] PCUIF.receive(tr_RAW_PCU_EV) -> value event {
356 log("Ignore unhandled event: ", event);
357 repeat;
358 }
359 }
360}
361
362/* PCU interface (UNIX domain socket) handler */
363type component RAW_PCUIF_CT {
364 var ConnectionId g_pcu_conn_id := -1;
365 var boolean g_pcu_connected := false;
366 port PCUIF_CODEC_PT PCU;
367
368 /* Connection towards BTS component(s) */
369 port RAW_PCU_MSG_PT BTS;
370 /* Connection to the MTC (test case) */
371 port RAW_PCU_MSG_PT MTC;
372};
373
374/* All received messages and events from OsmoPCU can be additionally sent
375 * directly to the MTC component. Pass direct := true to enable this mode. */
376function f_PCUIF_CT_handler(charstring pcu_sock_path, boolean direct := false)
377runs on RAW_PCUIF_CT {
378 var PCUIF_send_data pcu_sd_msg;
379 var PCUIF_Message pcu_msg;
380 timer T_Conn := 10.0;
381
382 log("Init PCU interface on '" & pcu_sock_path & "', waiting for connection...");
383 g_pcu_conn_id := f_pcuif_listen(PCU, pcu_sock_path);
384
385 /* Wait for connection */
386 T_Conn.start;
387 alt {
388 [not g_pcu_connected] PCU.receive(UD_connected:?) {
389 log("OsmoPCU is now connected");
390 /* Duplicate this event to the MTC if requested */
391 if (direct) { MTC.send(ts_RAW_PCU_EV(PCU_EV_CONNECT)); }
392 BTS.send(ts_RAW_PCU_EV(PCU_EV_CONNECT));
393 g_pcu_connected := true;
394 setverdict(pass);
395 T_Conn.stop;
396 repeat;
397 }
398 /* PCU -> BTS / MTC message forwarding */
399 [g_pcu_connected] PCU.receive(PCUIF_send_data:?) -> value pcu_sd_msg {
400 /* Send to the BTSes if at least one is connected */
401 if (BTS.checkstate("Connected")) { BTS.send(pcu_sd_msg.data); }
402 /* Duplicate this message to the MTC if requested */
403 if (direct) { MTC.send(pcu_sd_msg.data); }
404 repeat;
405 }
406 /* MTC -> PCU message forwarding */
407 [g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg {
408 PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg));
409 repeat;
410 }
411 /* BTS -> PCU message forwarding */
412 [g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg {
413 PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg));
414 repeat;
415 }
416 [not g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg {
417 log("PCU is not connected, dropping ", pcu_msg);
418 repeat;
419 }
420 [not g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg {
421 log("PCU is not connected, dropping ", pcu_msg);
422 repeat;
423 }
424 /* TODO: handle disconnect and reconnect of the PCU */
425 [] PCU.receive {
426 log("Unhandled message on PCU interface");
427 repeat;
428 }
429 [not g_pcu_connected] T_Conn.timeout {
430 setverdict(fail, "Timeout waiting for PCU connection");
431 mtc.stop;
432 }
433 }
434}
435
436}