blob: 6d29404408f7997d9a3b6478ab70c848268c0813 [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
Vadim Yanitskiy20f87002019-10-06 00:51:50 +0700136/* Derive PTCCH/U sub-slot from a given TDMA frame-number */
137function f_tdma_ptcch_fn2ss(integer fn) return integer
138{
139 var integer ss := -1;
140
141 /* See 3GPP TS 45.002, table 6 */
142 select (fn mod 416) {
143 case (12) { ss := 0; }
144 case (38) { ss := 1; }
145 case (64) { ss := 2; }
146 case (90) { ss := 3; }
147
148 case (116) { ss := 4; }
149 case (142) { ss := 5; }
150 case (168) { ss := 6; }
151 case (194) { ss := 7; }
152
153 case (220) { ss := 8; }
154 case (246) { ss := 9; }
155 case (272) { ss := 10; }
156 case (298) { ss := 11; }
157
158 case (324) { ss := 12; }
159 case (350) { ss := 13; }
160 case (376) { ss := 14; }
161 case (402) { ss := 15; }
162 }
163
164 return ss;
165}
166
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200167function f_ClckGen_CT_handler()
168runs on RAW_PCU_ClckGen_CT {
169 var integer fn104, fn52, fn13;
170
171 while (true) {
172 fn104 := fn mod 104;
173 fn52 := fn mod 52;
174 fn13 := fn mod 13;
175
176 if (fn13 == 0 or fn13 == 4 or fn13 == 8) {
177 /* 1/4 bursts of a PDTCH block on both Uplink and Downlink */
178 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_BEG, fn));
179 } else if (fn13 == 3 or fn13 == 7 or fn13 == 11) {
180 /* 4/4 bursts of a PDTCH block on both Uplink and Downlink */
181 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_END, fn));
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200182 } else if (fn52 == 12 or fn52 == 38) {
Vadim Yanitskiyb58b7302019-10-06 01:01:48 +0700183 /* 4/4 bursts of a PTCCH (Timing Advance Control) block on Downlink */
184 if (fn104 == 90) {
185 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_DL_BLOCK, fn));
186 }
187 /* One Access Burst on PTCCH/U (goes 3 time-slots after PTCCH/D) */
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200188 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_UL_BURST, fn));
189 }
190
191 /* TDMA hyperframe period is (2048 * 51 * 26) frames */
192 fn := (fn + 1) mod (2048 * 51 * 26);
193
194 /* (Re)start TDMA clock timer and wait */
195 T_TDMAClock.start;
196 T_TDMAClock.timeout;
197 }
198}
199
200type record of PCUIF_Message PCUIF_MsgQueue;
201
202/* Enqueue a given message to the end of a given queue */
203private function f_PCUIF_MsgQueue_enqueue(inout PCUIF_MsgQueue queue,
204 in PCUIF_Message msg)
205{
206 queue := queue & { msg };
207}
208
209/* Dequeue the first message of a given queue */
210private function f_PCUIF_MsgQueue_dequeue(inout PCUIF_MsgQueue queue,
211 out PCUIF_Message msg)
212{
213 var integer len := lengthof(queue);
214
215 if (len == 0) {
216 setverdict(fail, "Failed to dequeue a message: the queue is empty!");
217 mtc.stop;
218 }
219
220 /* Store the first message */
221 msg := queue[0];
222
223 /* Remove the first message from queue */
224 if (len > 1) {
225 queue := substr(queue, 1, len - 1);
226 } else {
227 queue := { };
228 }
229}
230
231/* Multiple base stations can be connected to the PCU. This component
232 * represents one BTS with an associated TDMA clock generator. */
233type component RAW_PCU_BTS_CT {
234 /* TDMA clock generator */
235 var RAW_PCU_ClckGen_CT vc_CLCK_GEN;
236 port RAW_PCU_MSG_PT CLCK;
237
238 /* Queues of PCUIF messages to be sent
239 * TODO: we may have multiple PDCH time-slots */
240 var PCUIF_MsgQueue pdtch_data_queue := { };
241 var PCUIF_MsgQueue pdtch_rts_queue := { };
242 var PCUIF_MsgQueue ptcch_rts_queue := { };
243
244 /* Connection towards the PCU interface */
245 port RAW_PCU_MSG_PT PCUIF;
246 /* Connection towards the test case */
247 port RAW_PCU_MSG_PT TC;
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700248
249 /* Whether to forward PTCCH/U burst events to the TC */
250 var boolean cfg_ptcch_burst_fwd := false;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200251}
252
253private altstep as_BTS_CT_MsgQueue(integer bts_nr)
254runs on RAW_PCU_BTS_CT {
255 var PCUIF_Message pcu_msg;
256
257 /* Enqueue DATA.ind and RTS.req messages */
258 [] TC.receive(tr_PCUIF_MSG(PCU_IF_MSG_DATA_IND, bts_nr)) -> value pcu_msg {
259 f_PCUIF_MsgQueue_enqueue(pdtch_data_queue, pcu_msg);
260 repeat;
261 }
262 [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PDTCH)) -> value pcu_msg {
263 f_PCUIF_MsgQueue_enqueue(pdtch_rts_queue, pcu_msg);
264 repeat;
265 }
266 [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg {
267 f_PCUIF_MsgQueue_enqueue(ptcch_rts_queue, pcu_msg);
268 repeat;
269 }
270 /* Forward other messages directly to the PCU */
271 [] TC.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg {
272 PCUIF.send(pcu_msg);
273 repeat;
274 }
275}
276
277private altstep as_BTS_CT_TDMASched(integer bts_nr)
278runs on RAW_PCU_BTS_CT {
279 var PCUIF_Message pcu_msg;
280 var RAW_PCU_Event event;
281
282 [] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_BEG)) -> value event {
283 /* If the RTS queue for PDTCH is not empty, send a message */
284 if (lengthof(pdtch_rts_queue) > 0) {
285 f_PCUIF_MsgQueue_dequeue(pdtch_rts_queue, pcu_msg);
286
287 /* Patch TDMA frame / block number and send */
288 pcu_msg.u.rts_req.fn := event.data.tdma_fn;
289 pcu_msg.u.rts_req.block_nr := 0; /* FIXME! */
290 PCUIF.send(pcu_msg);
291 }
292
293 /* We don't really need to send every frame to OsmoPCU, because
294 * it omits frame numbers not starting at a MAC block. */
295 PCUIF.send(ts_PCUIF_TIME_IND(bts_nr, event.data.tdma_fn));
296 repeat;
297 }
298 [lengthof(pdtch_data_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_END)) -> value event {
299 /* Dequeue a DATA.ind message */
300 f_PCUIF_MsgQueue_dequeue(pdtch_data_queue, pcu_msg);
301
302 /* Patch TDMA frame / block number */
303 pcu_msg.u.data_ind.fn := event.data.tdma_fn;
304 pcu_msg.u.data_ind.block_nr := 0; /* FIXME! */
305
306 PCUIF.send(pcu_msg); /* Send to the PCU and notify the TC */
307 TC.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_SENT, event.data.tdma_fn));
308 repeat;
309 }
310 [lengthof(ptcch_rts_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_DL_BLOCK)) -> value event {
311 /* Dequeue an RTS.req message for PTCCH */
312 f_PCUIF_MsgQueue_dequeue(ptcch_rts_queue, pcu_msg);
313
314 /* Patch TDMA frame / block number and send */
315 pcu_msg.u.rts_req.fn := event.data.tdma_fn;
316 pcu_msg.u.rts_req.block_nr := 0; /* FIXME! */
317 PCUIF.send(pcu_msg);
318 repeat;
319 }
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700320 /* Optional forwarding of PTCCH/U burst indications to the test case */
321 [cfg_ptcch_burst_fwd] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_UL_BURST)) -> value event {
322 TC.send(event);
323 repeat;
324 }
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200325 /* Ignore other clock events (and guard against an empty queue) */
326 [] CLCK.receive(tr_RAW_PCU_CLCK_EV) { repeat; }
327}
328
329function f_BTS_CT_handler(integer bts_nr, PCUIF_info_ind info_ind)
330runs on RAW_PCU_BTS_CT {
331 var PCUIF_Message pcu_msg;
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700332 var RAW_PCU_Command cmd;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200333 var RAW_PCU_Event event;
334
335 /* Init TDMA clock generator (so we can stop and start it) */
336 vc_CLCK_GEN := RAW_PCU_ClckGen_CT.create("ClckGen-" & int2str(bts_nr)) alive;
337 connect(vc_CLCK_GEN:CLCK, self:CLCK);
338
339 /* Wait until the PCU is connected */
340 PCUIF.receive(tr_RAW_PCU_EV(PCU_EV_CONNECT));
341
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200342 alt {
343 /* Wait for TXT.ind (PCU_VERSION) and respond with INFO.ind (SI13) */
344 [] PCUIF.receive(tr_PCUIF_TXT_IND(bts_nr, PCU_VERSION, ?)) -> value pcu_msg {
345 log("Rx TXT.ind from the PCU, version is ", pcu_msg.u.txt_ind.text);
346
347 /* Send System Information 13 to the PCU */
348 PCUIF.send(PCUIF_Message:{
349 msg_type := PCU_IF_MSG_INFO_IND,
350 bts_nr := bts_nr,
351 spare := '0000'O,
352 u := { info_ind := info_ind }
353 });
354
355 /* Notify the test case that we're done with SI13 */
356 TC.send(ts_RAW_PCU_EV(BTS_EV_SI13_NEGO));
357
358 /* Start feeding clock to the PCU */
359 vc_CLCK_GEN.start(f_ClckGen_CT_handler());
360 repeat;
361 }
Pau Espin Pedrold16bb272019-12-02 12:57:03 +0100362 /* PCU -> TS becomes active */
363 [] PCUIF.receive(tr_PCUIF_ACT_REQ(bts_nr, ?, ?)) -> value pcu_msg {
364 log("Rx ACT.req from the PCU: TRX" & int2str(pcu_msg.u.act_req.trx_nr) &
365 "/TS" & int2str(pcu_msg.u.act_req.ts_nr));
366 repeat;
367 }
368 /* PCU -> TS becomes inactive */
369 [] PCUIF.receive(tr_PCUIF_DEACT_REQ(bts_nr, ?, ?)) -> value pcu_msg {
370 log("Rx DEACT.req from the PCU: TRX" & int2str(pcu_msg.u.act_req.trx_nr) &
371 "/TS" & int2str(pcu_msg.u.act_req.ts_nr));
372 repeat;
373 }
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200374 /* PCU -> test case forwarding (filter by the BTS number) */
375 [] PCUIF.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg {
376 TC.send(pcu_msg);
377 repeat;
378 }
379
380 /* TC -> [Queue] -> PCU forwarding */
381 [] as_BTS_CT_MsgQueue(bts_nr);
382 /* TDMA scheduler (clock and queue handling) */
383 [] as_BTS_CT_TDMASched(bts_nr);
384
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700385 /* Command handling */
386 [] TC.receive(tr_RAW_PCU_CMD(TDMA_CMD_ENABLE_PTCCH_UL_FWD)) {
387 log("Enabling forwarding of PTCCH/U TDMA events to the TC");
388 cfg_ptcch_burst_fwd := true;
389 repeat;
390 }
391 [] TC.receive(tr_RAW_PCU_CMD) -> value cmd {
392 log("Ignore unhandled command: ", cmd);
393 repeat;
394 }
395
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200396 /* TODO: handle events (e.g. disconnection) from the PCU interface */
397 [] PCUIF.receive(tr_RAW_PCU_EV) -> value event {
398 log("Ignore unhandled event: ", event);
399 repeat;
400 }
401 }
402}
403
404/* PCU interface (UNIX domain socket) handler */
405type component RAW_PCUIF_CT {
406 var ConnectionId g_pcu_conn_id := -1;
407 var boolean g_pcu_connected := false;
408 port PCUIF_CODEC_PT PCU;
409
410 /* Connection towards BTS component(s) */
411 port RAW_PCU_MSG_PT BTS;
412 /* Connection to the MTC (test case) */
413 port RAW_PCU_MSG_PT MTC;
414};
415
416/* All received messages and events from OsmoPCU can be additionally sent
417 * directly to the MTC component. Pass direct := true to enable this mode. */
418function f_PCUIF_CT_handler(charstring pcu_sock_path, boolean direct := false)
419runs on RAW_PCUIF_CT {
420 var PCUIF_send_data pcu_sd_msg;
421 var PCUIF_Message pcu_msg;
422 timer T_Conn := 10.0;
423
424 log("Init PCU interface on '" & pcu_sock_path & "', waiting for connection...");
425 g_pcu_conn_id := f_pcuif_listen(PCU, pcu_sock_path);
426
427 /* Wait for connection */
428 T_Conn.start;
429 alt {
430 [not g_pcu_connected] PCU.receive(UD_connected:?) {
431 log("OsmoPCU is now connected");
432 /* Duplicate this event to the MTC if requested */
433 if (direct) { MTC.send(ts_RAW_PCU_EV(PCU_EV_CONNECT)); }
434 BTS.send(ts_RAW_PCU_EV(PCU_EV_CONNECT));
435 g_pcu_connected := true;
436 setverdict(pass);
437 T_Conn.stop;
438 repeat;
439 }
440 /* PCU -> BTS / MTC message forwarding */
441 [g_pcu_connected] PCU.receive(PCUIF_send_data:?) -> value pcu_sd_msg {
442 /* Send to the BTSes if at least one is connected */
443 if (BTS.checkstate("Connected")) { BTS.send(pcu_sd_msg.data); }
444 /* Duplicate this message to the MTC if requested */
445 if (direct) { MTC.send(pcu_sd_msg.data); }
446 repeat;
447 }
448 /* MTC -> PCU message forwarding */
449 [g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg {
450 PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg));
451 repeat;
452 }
453 /* BTS -> PCU message forwarding */
454 [g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg {
455 PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg));
456 repeat;
457 }
458 [not g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg {
459 log("PCU is not connected, dropping ", pcu_msg);
460 repeat;
461 }
462 [not g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg {
463 log("PCU is not connected, dropping ", pcu_msg);
464 repeat;
465 }
466 /* TODO: handle disconnect and reconnect of the PCU */
467 [] PCU.receive {
468 log("Unhandled message on PCU interface");
469 repeat;
470 }
471 [not g_pcu_connected] T_Conn.timeout {
472 setverdict(fail, "Timeout waiting for PCU connection");
473 mtc.stop;
474 }
475 }
476}
477
478}