blob: 6e956ba87c77a283dd4ddf9ef7d27bbe3b53b794 [file] [log] [blame]
Pau Espin Pedrol8dd59fb2020-04-29 15:08:16 +02001module PCUIF_Components {
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +02002
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
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +010022import from Osmocom_Types all;
Philipp Maierdb9d8942023-07-18 15:23:15 +020023import from General_Types all;
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +010024import from RLCMAC_Types all;
25import from GSM_RR_Types all;
26
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +020027/* Component communication diagram:
28 *
29 * +-----+ +----------+ +---------+
30 * | MTC +---------------+ PCUIF_CT +------+ OsmoPCU |
31 * +--+--+ +----+-----+ +---------+
32 * | |
33 * | |
34 * | |
35 * | +-----------+ | +---------------+
36 * +----+ BTS_CT #1 +------+ | ClckGen_CT #1 |
37 * | +-----+-----+ | +-------+-------+
38 * | | | |
39 * | +---------------------------+
40 * | |
41 * | +-----------+ | +---------------+
42 * +----+ BTS_CT #2 +------+ | ClckGen_CT #2 |
43 * | +-----+-----+ | +-------+-------+
44 * | | | |
45 * | +---------------------------+
46 * | |
47 * | +-----------+ | +---------------+
48 * +----+ BTS_CT #N +------+ | ClckGen_CT #N |
49 * +-----+-----+ +-------+-------+
50 * | |
51 * +---------------------------+
52 */
53
54/* Events are used by the components to indicate that something
55 * has happened, e.g. we have got a connection from the PCU. */
56type enumerated RAW_PCU_EventType {
57 /* Events related to RAW_PCUIF_CT */
58 PCU_EV_DISCONNECT, /*!< OsmoPCU has disconnected */
59 PCU_EV_CONNECT, /*!< OsmoPCU is now connected */
60
61 /* Events related to RAW_PCU_BTS_CT */
62 BTS_EV_SI13_NEGO, /*!< SI13 negotiation complete */
63
64 /* TDMA clock related events (TDMA frame-number in parameters) */
65 TDMA_EV_PDTCH_BLOCK_BEG, /*!< 1/4 bursts of a PDTCH block on both Uplink and Downlink */
66 TDMA_EV_PDTCH_BLOCK_END, /*!< 4/4 bursts of a PDTCH block on both Uplink and Downlink */
67 TDMA_EV_PTCCH_DL_BLOCK, /*!< 4/4 bursts of a PTCCH block on Downlink */
68 TDMA_EV_PTCCH_UL_BURST, /*!< One Access Burst on PTCCH/U */
69
70 TDMA_EV_PDTCH_BLOCK_SENT /*!< A PDTCH block has been sent to the PCU */
71};
72
73/* Union of all possible parameters of the events */
74type union RAW_PCU_EventParam {
75 integer tdma_fn
76};
77
78type record RAW_PCU_Event {
79 RAW_PCU_EventType event,
80 /* TODO: can we use 'anytype' here? */
81 RAW_PCU_EventParam data optional
82};
83
84template (value) RAW_PCU_Event ts_RAW_PCU_EV(RAW_PCU_EventType event) := {
85 event := event,
86 data := omit
87}
88template RAW_PCU_Event tr_RAW_PCU_EV(template RAW_PCU_EventType event := ?,
89 template RAW_PCU_EventParam data := *) := {
90 event := event,
91 data := data
92}
93
94template (value) RAW_PCU_Event ts_RAW_PCU_CLCK_EV(RAW_PCU_EventType event, integer fn) := {
95 event := event,
96 data := { tdma_fn := fn }
97}
98template RAW_PCU_Event tr_RAW_PCU_CLCK_EV := {
99 event := (TDMA_EV_PDTCH_BLOCK_BEG, TDMA_EV_PDTCH_BLOCK_END,
100 TDMA_EV_PTCCH_DL_BLOCK, TDMA_EV_PTCCH_UL_BURST,
101 TDMA_EV_PDTCH_BLOCK_SENT),
102 data := { tdma_fn := ? }
103}
104
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700105/* Commands are mostly used by the MTC to configure the components
106 * at run-time, e.g. to enable or disable some optional features. */
107type enumerated RAW_PCU_CommandType {
Vadim Yanitskiy91f8a092020-05-06 21:41:05 +0700108 GENERAL_CMD_SHUTDOWN, /*!< Shut down component and all its child components */
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700109 TDMA_CMD_ENABLE_PTCCH_UL_FWD /*!< Enable forwarding of TDMA_EV_PTCCH_UL_BURST to the MTC */
110};
111
112type record RAW_PCU_Command {
113 RAW_PCU_CommandType cmd,
114 anytype data optional
115};
116
117template (value) RAW_PCU_Command ts_RAW_PCU_CMD(RAW_PCU_CommandType cmd) := {
118 cmd := cmd,
119 data := omit
120}
121template RAW_PCU_Command tr_RAW_PCU_CMD(template RAW_PCU_CommandType cmd := ?,
122 template anytype data := *) := {
123 cmd := cmd,
124 data := data
125}
126
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100127/* PCUIF req_data containing decoded rlcmac/rr, for users to be able to match
128 * directly on receive()
129 */
130type record BTS_PDTCH_Block {
131 uint8_t bts_nr,
132 PCUIF_data raw,
Pau Espin Pedrolcf2ada52021-09-22 14:11:44 +0200133 RlcmacDlBlock dl_block optional
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100134};
135type record BTS_PTCCH_Block {
136 uint8_t bts_nr,
137 PCUIF_data raw,
Pau Espin Pedrolcf2ada52021-09-22 14:11:44 +0200138 PTCCHDownlinkMsg dl_block optional
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100139};
140type record BTS_CCCH_Block {
141 uint8_t bts_nr,
142 PCUIF_data raw,
Philipp Maiera6708cf2023-08-08 16:12:27 +0200143 OCT4 msg_id optional,
Philipp Maierdb9d8942023-07-18 15:23:15 +0200144 charstring imsi optional,
Philipp Maier02212482023-08-24 13:13:10 +0200145 GsmRrMessage rr_msg,
Philipp Maier4e769262023-08-30 15:38:02 +0200146 boolean confirm optional
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100147};
148template BTS_PDTCH_Block tr_PCUIF_DATA_PDTCH(template uint8_t bts_nr,
149 template PCUIF_data raw,
150 template RlcmacDlBlock dl_block := ?) := {
151 bts_nr := bts_nr,
152 raw := raw,
153 dl_block := dl_block
154};
155template BTS_PTCCH_Block tr_PCUIF_DATA_PTCCH(template uint8_t bts_nr,
156 template PCUIF_data raw,
157 template PTCCHDownlinkMsg dl_block := ?) := {
158 bts_nr := bts_nr,
159 raw := raw,
160 dl_block := dl_block
161};
162template BTS_CCCH_Block tr_PCUIF_DATA_RR(template uint8_t bts_nr,
163 template PCUIF_data raw,
Philipp Maierdb9d8942023-07-18 15:23:15 +0200164 template GsmRrMessage rr_msg := ?,
Philipp Maiera6708cf2023-08-08 16:12:27 +0200165 template OCT4 msg_id := *,
Philipp Maier02212482023-08-24 13:13:10 +0200166 template charstring imsi := *,
167 template boolean confirm := *) := {
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100168 bts_nr := bts_nr,
169 raw := raw,
Philipp Maiera6708cf2023-08-08 16:12:27 +0200170 msg_id := msg_id,
Philipp Maierdb9d8942023-07-18 15:23:15 +0200171 imsi := imsi,
Philipp Maier02212482023-08-24 13:13:10 +0200172 rr_msg := rr_msg,
173 confirm := confirm
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100174};
175
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200176/* Generic port for messages and events */
177type port RAW_PCU_MSG_PT message {
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700178 inout RAW_PCU_Command;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200179 inout RAW_PCU_Event;
180 inout PCUIF_Message;
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100181 inout BTS_PDTCH_Block;
182 inout BTS_PTCCH_Block;
183 inout BTS_CCCH_Block;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200184} with { extension "internal" };
185
186/* TDMA frame clock generator */
187type component RAW_PCU_ClckGen_CT {
188 /* One TDMA frame is 4.615 ms long */
189 timer T_TDMAClock := 4.615 / 1000.0;
190 port RAW_PCU_MSG_PT CLCK;
191 var integer fn := 0;
192}
193
Vadim Yanitskiy20f87002019-10-06 00:51:50 +0700194/* Derive PTCCH/U sub-slot from a given TDMA frame-number */
195function f_tdma_ptcch_fn2ss(integer fn) return integer
196{
197 var integer ss := -1;
198
199 /* See 3GPP TS 45.002, table 6 */
200 select (fn mod 416) {
201 case (12) { ss := 0; }
202 case (38) { ss := 1; }
203 case (64) { ss := 2; }
204 case (90) { ss := 3; }
205
206 case (116) { ss := 4; }
207 case (142) { ss := 5; }
208 case (168) { ss := 6; }
209 case (194) { ss := 7; }
210
211 case (220) { ss := 8; }
212 case (246) { ss := 9; }
213 case (272) { ss := 10; }
214 case (298) { ss := 11; }
215
216 case (324) { ss := 12; }
217 case (350) { ss := 13; }
218 case (376) { ss := 14; }
219 case (402) { ss := 15; }
220 }
221
222 return ss;
223}
224
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100225function f_ClckGen_CT_handler(integer start_fn := 0)
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200226runs on RAW_PCU_ClckGen_CT {
227 var integer fn104, fn52, fn13;
228
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100229 fn := start_fn;
230
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200231 while (true) {
232 fn104 := fn mod 104;
233 fn52 := fn mod 52;
234 fn13 := fn mod 13;
235
236 if (fn13 == 0 or fn13 == 4 or fn13 == 8) {
237 /* 1/4 bursts of a PDTCH block on both Uplink and Downlink */
238 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_BEG, fn));
239 } else if (fn13 == 3 or fn13 == 7 or fn13 == 11) {
240 /* 4/4 bursts of a PDTCH block on both Uplink and Downlink */
241 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_END, fn));
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200242 } else if (fn52 == 12 or fn52 == 38) {
Vadim Yanitskiyb58b7302019-10-06 01:01:48 +0700243 /* 4/4 bursts of a PTCCH (Timing Advance Control) block on Downlink */
244 if (fn104 == 90) {
245 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_DL_BLOCK, fn));
246 }
247 /* One Access Burst on PTCCH/U (goes 3 time-slots after PTCCH/D) */
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200248 CLCK.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PTCCH_UL_BURST, fn));
249 }
250
251 /* TDMA hyperframe period is (2048 * 51 * 26) frames */
252 fn := (fn + 1) mod (2048 * 51 * 26);
253
254 /* (Re)start TDMA clock timer and wait */
255 T_TDMAClock.start;
256 T_TDMAClock.timeout;
257 }
258}
259
260type record of PCUIF_Message PCUIF_MsgQueue;
261
262/* Enqueue a given message to the end of a given queue */
263private function f_PCUIF_MsgQueue_enqueue(inout PCUIF_MsgQueue queue,
264 in PCUIF_Message msg)
265{
266 queue := queue & { msg };
267}
268
269/* Dequeue the first message of a given queue */
270private function f_PCUIF_MsgQueue_dequeue(inout PCUIF_MsgQueue queue,
271 out PCUIF_Message msg)
272{
273 var integer len := lengthof(queue);
274
275 if (len == 0) {
276 setverdict(fail, "Failed to dequeue a message: the queue is empty!");
277 mtc.stop;
278 }
279
280 /* Store the first message */
281 msg := queue[0];
282
283 /* Remove the first message from queue */
284 if (len > 1) {
285 queue := substr(queue, 1, len - 1);
286 } else {
287 queue := { };
288 }
289}
290
Pau Espin Pedrol65bab9e2019-12-04 21:05:10 +0100291/* Get first message from queue. true if non-empty, false otherwise */
292private function f_PCUIF_MsgQueue_first(inout PCUIF_MsgQueue queue,
293 out PCUIF_Message msg) return boolean
294{
295 if (lengthof(queue) == 0) {
296 return false;
297 }
298
299 msg := queue[0];
300 return true;
301}
302
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200303/* Multiple base stations can be connected to the PCU. This component
304 * represents one BTS with an associated TDMA clock generator. */
305type component RAW_PCU_BTS_CT {
306 /* TDMA clock generator */
307 var RAW_PCU_ClckGen_CT vc_CLCK_GEN;
308 port RAW_PCU_MSG_PT CLCK;
309
310 /* Queues of PCUIF messages to be sent
311 * TODO: we may have multiple PDCH time-slots */
312 var PCUIF_MsgQueue pdtch_data_queue := { };
313 var PCUIF_MsgQueue pdtch_rts_queue := { };
314 var PCUIF_MsgQueue ptcch_rts_queue := { };
315
316 /* Connection towards the PCU interface */
317 port RAW_PCU_MSG_PT PCUIF;
318 /* Connection towards the test case */
319 port RAW_PCU_MSG_PT TC;
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700320
321 /* Whether to forward PTCCH/U burst events to the TC */
322 var boolean cfg_ptcch_burst_fwd := false;
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100323
324 var PCUIF_info_ind g_info_ind;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200325}
326
Pau Espin Pedrol65bab9e2019-12-04 21:05:10 +0100327/* Queue received messages from Test Case, they will eventually be scheduled and
328 * sent according to their FN. FN value of 0 has the special meaning of "schedule
329 * as soon as possible". */
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200330private altstep as_BTS_CT_MsgQueue(integer bts_nr)
331runs on RAW_PCU_BTS_CT {
332 var PCUIF_Message pcu_msg;
333
334 /* Enqueue DATA.ind and RTS.req messages */
335 [] TC.receive(tr_PCUIF_MSG(PCU_IF_MSG_DATA_IND, bts_nr)) -> value pcu_msg {
Pau Espin Pedrolaa457072021-04-19 18:06:16 +0200336 if (pcu_msg.u.data_ind.sapi == PCU_IF_SAPI_BCCH) {
337 PCUIF.send(pcu_msg); /* Forward directly ASAP */
338 } else {
339 f_PCUIF_MsgQueue_enqueue(pdtch_data_queue, pcu_msg);
340 }
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200341 repeat;
342 }
343 [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PDTCH)) -> value pcu_msg {
344 f_PCUIF_MsgQueue_enqueue(pdtch_rts_queue, pcu_msg);
345 repeat;
346 }
347 [] TC.receive(tr_PCUIF_RTS_REQ(bts_nr, sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg {
348 f_PCUIF_MsgQueue_enqueue(ptcch_rts_queue, pcu_msg);
349 repeat;
350 }
351 /* Forward other messages directly to the PCU */
352 [] TC.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg {
353 PCUIF.send(pcu_msg);
354 repeat;
355 }
356}
357
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100358/* Submit empty data on any available TS, to set up initial TDMA clock */
359private function f_tx_first_data_ind(integer bts_nr, PCUIF_info_ind info_ind, integer start_fn)
360runs on RAW_PCU_BTS_CT
361{
362 var PCUIF_Message pcu_msg;
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100363
364 /* Find an active TS: */
365 for (var uint8_t ts_nr := 0; ts_nr < 8; ts_nr := ts_nr + 1) {
Vadim Yanitskiy1da1fef2021-03-23 04:28:18 +0100366 for (var integer trx_nr := 0; trx_nr < lengthof(g_info_ind.trx); trx_nr := trx_nr + 1) {
367 if (g_info_ind.trx[trx_nr].pdch_mask[ts_nr] == '0'B) {
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100368 continue; /* TRX+TS not activated */
369 }
370
371 /* Send empty DATA.ind to set up FN */
372 pcu_msg := valueof(ts_PCUIF_DATA_IND(bts_nr, trx_nr, ts_nr, 0 /* FIXME */,
373 PCU_IF_SAPI_PDTCH, ''O, start_fn,
Vadim Yanitskiy1da1fef2021-03-23 04:28:18 +0100374 g_info_ind.trx[trx_nr].arfcn,
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100375 rssi := -80, ber10k := 0,
376 ta_offs_qbits := 0, lqual_cb := 10));
377 PCUIF.send(pcu_msg);
378 return;
379 }
380 }
381}
382
Pau Espin Pedrol9fc065a2021-05-11 17:05:39 +0200383private function fn2macblock(uint32_t fn) return uint8_t
384{
385 return (fn mod 52) / 4;
386}
387
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100388/* Get first message from queue. true if non-empty, false otherwise */
389private function f_tx_data_ind_fn(integer bts_nr, integer fn)
390runs on RAW_PCU_BTS_CT
391{
392 var PCUIF_Message pcu_msg;
393 var boolean has_msg, use_msg;
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100394
395 for (var uint8_t ts_nr := 0; ts_nr < 8; ts_nr := ts_nr + 1) {
Vadim Yanitskiy1da1fef2021-03-23 04:28:18 +0100396 for (var integer trx_nr := 0; trx_nr < lengthof(g_info_ind.trx); trx_nr := trx_nr + 1) {
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100397 //var charstring prefix := "BTS=" & int2str(bts_nr) & ",TRX=" & int2str(trx_nr) & ",TS=" & int2str(ts_nr) & ",FN=" & int2str(fn) & ": ";
Vadim Yanitskiy1da1fef2021-03-23 04:28:18 +0100398 if (g_info_ind.trx[trx_nr].pdch_mask[ts_nr] == '0'B) {
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100399 //log(prefix, "disabled");
400 continue; /* TRX+TS not activated */
401 }
402
403 /* Check if we reached time to serve the first DATA.ind message in the queue: */
404 has_msg := f_PCUIF_MsgQueue_first(pdtch_data_queue, pcu_msg) and
405 pcu_msg.u.data_ind.trx_nr == trx_nr and
406 pcu_msg.u.data_ind.ts_nr == ts_nr;
407 use_msg := has_msg and (pcu_msg.u.data_ind.fn == 0 or pcu_msg.u.data_ind.fn == fn);
408 if (use_msg) {
409 /* Dequeue a DATA.ind message */
410 f_PCUIF_MsgQueue_dequeue(pdtch_data_queue, pcu_msg);
411 /* Patch TDMA frame / block number */
412 pcu_msg.u.data_ind.fn := fn;
Pau Espin Pedrol9fc065a2021-05-11 17:05:39 +0200413 pcu_msg.u.data_ind.block_nr := fn2macblock(fn);
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100414 //log(prefix, "DATA.ind");
415 } else if (has_msg and pcu_msg.u.data_ind.fn < fn) {
Vadim Yanitskiy1030b7a2022-10-05 01:03:01 +0700416 setverdict(fail, "We are late scheduling the block! ", pcu_msg.u.data_ind.fn, " < ", fn);
417 mtc.stop;
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100418 } else {
419 /* NOPE.ind: */
420 pcu_msg := valueof(ts_PCUIF_DATA_IND(bts_nr, trx_nr, ts_nr, 0 /* FIXME */,
421 PCU_IF_SAPI_PDTCH, ''O, fn,
Vadim Yanitskiy1da1fef2021-03-23 04:28:18 +0100422 g_info_ind.trx[trx_nr].arfcn,
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100423 rssi := -80, ber10k := 0,
424 ta_offs_qbits := 0, lqual_cb := 10));
425 //log(prefix, "DATA.ind (len=0)");
426 }
427
Pau Espin Pedrol4c23aea2021-11-16 19:14:00 +0100428 PCUIF.send(pcu_msg); /* Send to the PCU and notify the TC */
429 if (use_msg) {
430 TC.send(ts_RAW_PCU_CLCK_EV(TDMA_EV_PDTCH_BLOCK_SENT, fn));
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100431 }
432 }
433 }
434}
435
Pau Espin Pedrol65bab9e2019-12-04 21:05:10 +0100436/* Handle schedule events and manage actions: Send msgs over PCUIF to PCU,
437 * advertise Test Case about sent messages, etc. */
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200438private altstep as_BTS_CT_TDMASched(integer bts_nr)
439runs on RAW_PCU_BTS_CT {
440 var PCUIF_Message pcu_msg;
441 var RAW_PCU_Event event;
Pau Espin Pedrol60727252019-12-04 21:40:25 +0100442 var integer ev_begin_fn;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200443
444 [] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_BEG)) -> value event {
445 /* If the RTS queue for PDTCH is not empty, send a message */
446 if (lengthof(pdtch_rts_queue) > 0) {
447 f_PCUIF_MsgQueue_dequeue(pdtch_rts_queue, pcu_msg);
448
449 /* Patch TDMA frame / block number and send */
450 pcu_msg.u.rts_req.fn := event.data.tdma_fn;
Pau Espin Pedrol9fc065a2021-05-11 17:05:39 +0200451 pcu_msg.u.rts_req.block_nr := fn2macblock(event.data.tdma_fn);
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200452 PCUIF.send(pcu_msg);
453 }
454
455 /* We don't really need to send every frame to OsmoPCU, because
456 * it omits frame numbers not starting at a MAC block. */
457 PCUIF.send(ts_PCUIF_TIME_IND(bts_nr, event.data.tdma_fn));
458 repeat;
459 }
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100460 [] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PDTCH_BLOCK_END)) -> value event {
Pau Espin Pedrol60727252019-12-04 21:40:25 +0100461 /* FN matching the beginning of current block: */
462 ev_begin_fn := event.data.tdma_fn - 3;
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100463 f_tx_data_ind_fn(bts_nr, ev_begin_fn);
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200464 repeat;
465 }
466 [lengthof(ptcch_rts_queue) > 0] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_DL_BLOCK)) -> value event {
Pau Espin Pedrol60727252019-12-04 21:40:25 +0100467 /* FN matching the beginning of current block (PTCCH is interleaved over 4 non-consecutive bursts): */
468 ev_begin_fn := event.data.tdma_fn - 78;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200469 /* Dequeue an RTS.req message for PTCCH */
470 f_PCUIF_MsgQueue_dequeue(ptcch_rts_queue, pcu_msg);
471
472 /* Patch TDMA frame / block number and send */
Pau Espin Pedrol60727252019-12-04 21:40:25 +0100473 pcu_msg.u.rts_req.fn := ev_begin_fn;
Pau Espin Pedrol9fc065a2021-05-11 17:05:39 +0200474 pcu_msg.u.rts_req.block_nr := fn2macblock(ev_begin_fn);
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200475 PCUIF.send(pcu_msg);
476 repeat;
477 }
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700478 /* Optional forwarding of PTCCH/U burst indications to the test case */
479 [cfg_ptcch_burst_fwd] CLCK.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_UL_BURST)) -> value event {
480 TC.send(event);
481 repeat;
482 }
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200483 /* Ignore other clock events (and guard against an empty queue) */
484 [] CLCK.receive(tr_RAW_PCU_CLCK_EV) { repeat; }
485}
486
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100487function f_BTS_CT_handler(integer bts_nr, PCUIF_info_ind info_ind, boolean decode_data_req := false)
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200488runs on RAW_PCU_BTS_CT {
489 var PCUIF_Message pcu_msg;
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700490 var RAW_PCU_Command cmd;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200491 var RAW_PCU_Event event;
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100492 var BTS_PDTCH_Block pcu_msg_pdtch;
493 var BTS_PTCCH_Block pcu_msg_ptcch;
494 var BTS_CCCH_Block pcu_msg_rr;
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200495
Pau Espin Pedrol873686f2021-03-05 14:57:58 +0100496 g_info_ind := info_ind;
497
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200498 /* Init TDMA clock generator (so we can stop and start it) */
499 vc_CLCK_GEN := RAW_PCU_ClckGen_CT.create("ClckGen-" & int2str(bts_nr)) alive;
500 connect(vc_CLCK_GEN:CLCK, self:CLCK);
501
502 /* Wait until the PCU is connected */
503 PCUIF.receive(tr_RAW_PCU_EV(PCU_EV_CONNECT));
504
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200505 alt {
506 /* Wait for TXT.ind (PCU_VERSION) and respond with INFO.ind (SI13) */
507 [] PCUIF.receive(tr_PCUIF_TXT_IND(bts_nr, PCU_VERSION, ?)) -> value pcu_msg {
508 log("Rx TXT.ind from the PCU, version is ", pcu_msg.u.txt_ind.text);
509
510 /* Send System Information 13 to the PCU */
511 PCUIF.send(PCUIF_Message:{
512 msg_type := PCU_IF_MSG_INFO_IND,
513 bts_nr := bts_nr,
514 spare := '0000'O,
515 u := { info_ind := info_ind }
516 });
517
Pau Espin Pedrolc62ebac2021-03-12 15:07:06 +0100518 const integer start_fn := 0;
Pau Espin Pedrol4c23aea2021-11-16 19:14:00 +0100519 f_tx_first_data_ind(bts_nr, info_ind, start_fn);
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100520
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200521 /* Notify the test case that we're done with SI13 */
522 TC.send(ts_RAW_PCU_EV(BTS_EV_SI13_NEGO));
523
524 /* Start feeding clock to the PCU */
Pau Espin Pedroled9ca972021-03-11 20:53:21 +0100525 vc_CLCK_GEN.start(f_ClckGen_CT_handler(start_fn));
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200526 repeat;
527 }
Pau Espin Pedrold16bb272019-12-02 12:57:03 +0100528 /* PCU -> TS becomes active */
529 [] PCUIF.receive(tr_PCUIF_ACT_REQ(bts_nr, ?, ?)) -> value pcu_msg {
530 log("Rx ACT.req from the PCU: TRX" & int2str(pcu_msg.u.act_req.trx_nr) &
531 "/TS" & int2str(pcu_msg.u.act_req.ts_nr));
532 repeat;
533 }
534 /* PCU -> TS becomes inactive */
535 [] PCUIF.receive(tr_PCUIF_DEACT_REQ(bts_nr, ?, ?)) -> value pcu_msg {
536 log("Rx DEACT.req from the PCU: TRX" & int2str(pcu_msg.u.act_req.trx_nr) &
537 "/TS" & int2str(pcu_msg.u.act_req.ts_nr));
538 repeat;
539 }
Vadim Yanitskiy8f86bbe2023-12-15 15:19:09 +0700540 [decode_data_req] PCUIF.receive(tr_PCUIF_DATA_REQ(bts_nr, ?, ?, sapi := PCU_IF_SAPI_PCH_2)) -> value pcu_msg {
Philipp Maiere76efaa2023-09-21 14:52:06 +0200541 var charstring imsi_filter_regexp := "(\d*)"; /* numbers only */
Vadim Yanitskiy8f86bbe2023-12-15 15:19:09 +0700542 var PCUIF_pch pch;
Philipp Maiere76efaa2023-09-21 14:52:06 +0200543
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100544 pcu_msg_rr.bts_nr := bts_nr;
545 pcu_msg_rr.raw := pcu_msg.u.data_req;
Philipp Maierdb9d8942023-07-18 15:23:15 +0200546
Vadim Yanitskiy8f86bbe2023-12-15 15:19:09 +0700547 pch := dec_PCUIF_pch(pcu_msg_rr.raw.data);
548 pcu_msg_rr.msg_id := pch.msg_id;
549 pcu_msg_rr.imsi := regexp(pch.imsi, imsi_filter_regexp, 0);
550 pcu_msg_rr.rr_msg := dec_GsmRrMessage(pch.data);
551 pcu_msg_rr.confirm := pch.confirm;
552
553 TC.send(pcu_msg_rr);
554 repeat;
555 }
556 [decode_data_req] PCUIF.receive(tr_PCUIF_DATA_REQ(bts_nr, ?, ?, sapi := PCU_IF_SAPI_AGCH_2)) -> value pcu_msg {
557 var PCUIF_agch agch;
558
559 pcu_msg_rr.bts_nr := bts_nr;
560 pcu_msg_rr.raw := pcu_msg.u.data_req;
561
562 agch := dec_PCUIF_agch(pcu_msg_rr.raw.data);
563 pcu_msg_rr.imsi := omit;
564 pcu_msg_rr.msg_id := agch.msg_id;
565 pcu_msg_rr.rr_msg := dec_GsmRrMessage(agch.data);
566 pcu_msg_rr.confirm := agch.confirm;
567
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100568 TC.send(pcu_msg_rr);
569 repeat;
570 }
571 [decode_data_req] PCUIF.receive(tr_PCUIF_DATA_REQ(bts_nr, ?, ?, sapi := PCU_IF_SAPI_PDTCH)) -> value pcu_msg {
572 pcu_msg_pdtch.bts_nr := bts_nr;
573 pcu_msg_pdtch.raw := pcu_msg.u.data_req;
Pau Espin Pedrolcf2ada52021-09-22 14:11:44 +0200574 if (pcu_msg_pdtch.raw.len != 0) {
575 pcu_msg_pdtch.dl_block := dec_RlcmacDlBlock(pcu_msg_pdtch.raw.data);
576 } else {
577 pcu_msg_pdtch.dl_block := omit;
578 }
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100579 TC.send(pcu_msg_pdtch);
580 repeat;
581 }
582 [decode_data_req] PCUIF.receive(tr_PCUIF_DATA_REQ(bts_nr, ?, ?, sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg {
583 pcu_msg_ptcch.bts_nr := bts_nr;
584 pcu_msg_ptcch.raw := pcu_msg.u.data_req;
Pau Espin Pedrolcf2ada52021-09-22 14:11:44 +0200585 if (pcu_msg_ptcch.raw.len != 0) {
586 pcu_msg_ptcch.dl_block := dec_PTCCHDownlinkMsg(pcu_msg_ptcch.raw.data);
587 } else {
588 pcu_msg_ptcch.dl_block := omit;
589 }
Pau Espin Pedrolcf5c33e2021-01-19 18:56:55 +0100590 TC.send(pcu_msg_ptcch);
591 repeat;
592 }
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200593 /* PCU -> test case forwarding (filter by the BTS number) */
594 [] PCUIF.receive(tr_PCUIF_MSG(?, bts_nr)) -> value pcu_msg {
595 TC.send(pcu_msg);
596 repeat;
597 }
598
599 /* TC -> [Queue] -> PCU forwarding */
600 [] as_BTS_CT_MsgQueue(bts_nr);
601 /* TDMA scheduler (clock and queue handling) */
602 [] as_BTS_CT_TDMASched(bts_nr);
603
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700604 /* Command handling */
Vadim Yanitskiy91f8a092020-05-06 21:41:05 +0700605 [] TC.receive(tr_RAW_PCU_CMD(GENERAL_CMD_SHUTDOWN)) {
606 log("Shutting down virtual BTS #", bts_nr, "...");
607 vc_CLCK_GEN.stop;
608 break;
609 }
Vadim Yanitskiy02f77d82019-10-04 17:12:35 +0700610 [] TC.receive(tr_RAW_PCU_CMD(TDMA_CMD_ENABLE_PTCCH_UL_FWD)) {
611 log("Enabling forwarding of PTCCH/U TDMA events to the TC");
612 cfg_ptcch_burst_fwd := true;
613 repeat;
614 }
615 [] TC.receive(tr_RAW_PCU_CMD) -> value cmd {
616 log("Ignore unhandled command: ", cmd);
617 repeat;
618 }
619
Vadim Yanitskiyf7d9c0f2019-09-06 00:08:17 +0200620 /* TODO: handle events (e.g. disconnection) from the PCU interface */
621 [] PCUIF.receive(tr_RAW_PCU_EV) -> value event {
622 log("Ignore unhandled event: ", event);
623 repeat;
624 }
625 }
626}
627
628/* PCU interface (UNIX domain socket) handler */
629type component RAW_PCUIF_CT {
630 var ConnectionId g_pcu_conn_id := -1;
631 var boolean g_pcu_connected := false;
632 port PCUIF_CODEC_PT PCU;
633
634 /* Connection towards BTS component(s) */
635 port RAW_PCU_MSG_PT BTS;
636 /* Connection to the MTC (test case) */
637 port RAW_PCU_MSG_PT MTC;
638};
639
640/* All received messages and events from OsmoPCU can be additionally sent
641 * directly to the MTC component. Pass direct := true to enable this mode. */
642function f_PCUIF_CT_handler(charstring pcu_sock_path, boolean direct := false)
643runs on RAW_PCUIF_CT {
644 var PCUIF_send_data pcu_sd_msg;
645 var PCUIF_Message pcu_msg;
646 timer T_Conn := 10.0;
647
648 log("Init PCU interface on '" & pcu_sock_path & "', waiting for connection...");
649 g_pcu_conn_id := f_pcuif_listen(PCU, pcu_sock_path);
650
651 /* Wait for connection */
652 T_Conn.start;
653 alt {
654 [not g_pcu_connected] PCU.receive(UD_connected:?) {
655 log("OsmoPCU is now connected");
656 /* Duplicate this event to the MTC if requested */
657 if (direct) { MTC.send(ts_RAW_PCU_EV(PCU_EV_CONNECT)); }
658 BTS.send(ts_RAW_PCU_EV(PCU_EV_CONNECT));
659 g_pcu_connected := true;
660 setverdict(pass);
661 T_Conn.stop;
662 repeat;
663 }
664 /* PCU -> BTS / MTC message forwarding */
665 [g_pcu_connected] PCU.receive(PCUIF_send_data:?) -> value pcu_sd_msg {
666 /* Send to the BTSes if at least one is connected */
667 if (BTS.checkstate("Connected")) { BTS.send(pcu_sd_msg.data); }
668 /* Duplicate this message to the MTC if requested */
669 if (direct) { MTC.send(pcu_sd_msg.data); }
670 repeat;
671 }
672 /* MTC -> PCU message forwarding */
673 [g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg {
674 PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg));
675 repeat;
676 }
677 /* BTS -> PCU message forwarding */
678 [g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg {
679 PCU.send(t_SD_PCUIF(g_pcu_conn_id, pcu_msg));
680 repeat;
681 }
682 [not g_pcu_connected] MTC.receive(PCUIF_Message:?) -> value pcu_msg {
683 log("PCU is not connected, dropping ", pcu_msg);
684 repeat;
685 }
686 [not g_pcu_connected] BTS.receive(PCUIF_Message:?) -> value pcu_msg {
687 log("PCU is not connected, dropping ", pcu_msg);
688 repeat;
689 }
690 /* TODO: handle disconnect and reconnect of the PCU */
691 [] PCU.receive {
692 log("Unhandled message on PCU interface");
693 repeat;
694 }
695 [not g_pcu_connected] T_Conn.timeout {
696 setverdict(fail, "Timeout waiting for PCU connection");
697 mtc.stop;
698 }
699 }
700}
701
702}