summaryrefslogtreecommitdiffstats
path: root/pcu/PCU_Tests.ttcn
blob: 565e633377a606320e6b63460a6f98e2536edb43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
module PCU_Tests {

/* "RAW" PCU tests: Talk directly to the PCU socket of OsmoPCU on the one hand side (emulating
   the BTS/BSC side PCU socket server) and the Gb interface on the other hand side.  No NS/BSSGP
   Emulation is used; rather, we simply use the NS_CodecPort to implement both standard and non-
   standard procedures on the NS and BSSGP level.  The goal of these tests is to test exactly
   those NS and BSSGP implementations on the BSS (PCU) side. */

/* (C) 2018-2019 Harald Welte <laforge@gnumonks.org>
 * (C) 2019-2020 Vadim Yanitskiy <axilirator@gmail.com>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

friend module PCU_Tests_NS;

import from General_Types all;
import from Osmocom_Types all;
import from GSM_Types all;
import from GSM_RR_Types all;

import from Osmocom_VTY_Functions all;
import from TELNETasp_PortType all;

import from MobileL3_GMM_SM_Types all;
import from RLCMAC_CSN1_Types all;
import from RLCMAC_CSN1_Templates all;
import from RLCMAC_Types all;
import from RLCMAC_Templates all;

import from MobileL3_CommonIE_Types all;
import from L3_Templates all;

import from NS_Types all;
import from BSSGP_Types all;
import from Osmocom_Gb_Types all;

import from BSSGP_Emulation all; /* BssgpConfig */
import from NS_Emulation all; /* NSConfiguration */

import from UD_Types all;
import from PCUIF_Types all;
import from PCUIF_CodecPort all;
import from PCUIF_Components all;
import from IPL4asp_Types all;
import from Native_Functions all;
import from SGSN_Components all;
import from GPRS_Components all;

import from StatsD_Types all;
import from StatsD_CodecPort all;
import from StatsD_CodecPort_CtrlFunct all;
import from StatsD_Checker all;

modulepar {
	charstring mp_pcu_sock_path := PCU_SOCK_DEFAULT;

	float X2002 := 0.2; /* Timer -2002, IMM ASSIGN confirm delay */

	charstring mp_pcu_statsd_ip := "127.0.0.1";
	integer mp_pcu_statsd_port := 8125;
}


/* FIXME: make sure to use parameters from mp_gb_cfg.cell_id in the PCU INFO IND */
friend template (value) PCUIF_info_ind ts_PCUIF_INFO_default(template (value) PCUIF_Flags flags := c_PCUIF_Flags_default)
:= {
	version := PCUIF_Types.mp_pcuif_version,
	flags := flags,
	trx := f_PCUIF_ver_INFO_Trxs(GPRS_Components.mp_base_arfcn),
	bsic := 7,
	mcc := 262,
	mnc := 42,
	mnc_3_digits := 0,
	lac := 13135,
	rac := 0,
	nsei := mp_nsconfig.nsei,
	nse_timer := { 3, 3, 3, 3, 30, 3, 10 },
	cell_timer := { 3, 3, 3, 3, 3, 10, 3, 10, 3, 10, 3 },
	cell_id := 20960,
	repeat_time := 5 * 50,
	repeat_count := 3,
	bvci := mp_gb_cfg.bvc[0].bvci,
	t3142 := 20,
	t3169 := 5,
	t3191 := 5,
	t3193_10ms := 160,
	t3195 := 5,
	t3101 := 10,
	t3103 := 4,
	t3105 := 8,
	cv_countdown := 15,
	dl_tbf_ext := 250 * 10, /* ms */
	ul_tbf_ext := 250 * 10, /* ms */
	initial_cs := 2,
	initial_mcs := 1,
	nsvci := { mp_nsconfig.nsvc[0].nsvci, 0 },
	local_port := { mp_nsconfig.nsvc[0].provider.ip.remote_udp_port, 0 },
	remote_port := { mp_nsconfig.nsvc[0].provider.ip.local_udp_port, 0 },
	remote_addr := f_PCUIF_ver_INFO_RemoteAddr(
		f_PCUIF_AF2addr_type(mp_nsconfig.nsvc[0].provider.ip.address_family), mp_nsconfig.nsvc[0].provider.ip.local_ip)
}

type record lqual_range {
	/* component reference to the IPA_Client component used for RSL */
	uint8_t low,
	uint8_t high
}

type component RAW_PCU_Test_CT extends bssgp_CT, MS_BTS_IFACE_CT, StatsD_ConnHdlr {
	/* PCU interface abstraction component */
	var RAW_PCUIF_CT vc_PCUIF;

	/* StatsD */
	var StatsD_Checker_CT vc_STATSD;

	/* Connection to the PCUIF component */
	port RAW_PCU_MSG_PT PCUIF;
	/* VTY connection to the PCU */
	port TELNETasp_PT PCUVTY;

	/* Uplink CS/MCS thresholds, default from pcu_main.c: */
	var lqual_range g_cs_lqual_ranges[4] := {{low := 0, high := 6},
						 {low := 5, high := 8},
						 {low := 7, high := 13},
						 {low := 12,high := 35}};
	var lqual_range g_mcs_lqual_ranges[9] := {{low := 0, high := 6},
						 {low := 5, high := 8},
						 {low := 7, high := 13},
						 {low := 12,high := 15},
						 {low := 14, high := 17},
						 {low := 16, high := 18},
						 {low := 17,high := 20},
						 {low := 19, high := 24},
						 {low := 23,high := 35}};
	var uint8_t g_cs_initial_dl := 1;
	var uint8_t g_cs_initial_ul := 1;
	var uint8_t g_mcs_initial_dl := 1;
	var uint8_t g_mcs_initial_ul := 1;
	var uint8_t g_cs_max_dl := 4;
	var uint8_t g_cs_max_ul := 4;
	var uint8_t g_mcs_max_dl := 9;
	var uint8_t g_mcs_max_ul := 9;

	var boolean g_force_two_phase_access := false;

	/* Guard timeout */
	timer g_T_guard := 60.0;
};

private altstep as_Tguard_RAW() runs on RAW_PCU_Test_CT {
	[] g_T_guard.timeout {
		setverdict(fail, "Timeout of T_guard");
		f_shutdown(__BFILE__, __LINE__);
		}
}

private function f_pcuvty_set_allowed_cs_mcs() runs on RAW_PCU_Test_CT {
	f_vty_config2(PCUVTY, {"pcu"}, "cs " & int2str(g_cs_initial_dl) & " " & int2str(g_cs_initial_ul));
	f_vty_config2(PCUVTY, {"pcu"}, "cs max " & int2str(g_cs_max_dl) & " " & int2str(g_cs_max_ul));

	f_vty_config2(PCUVTY, {"pcu"}, "mcs " & int2str(g_mcs_initial_dl) & " " & int2str(g_mcs_initial_ul));
	f_vty_config2(PCUVTY, {"pcu"}, "mcs max " & int2str(g_mcs_max_dl) & " " & int2str(g_mcs_max_ul));
}

private function f_pcuvty_set_link_quality_ranges() runs on RAW_PCU_Test_CT {
	var charstring cmd;

	cmd := "cs link-quality-ranges" &
	       " cs1 " & int2str(g_cs_lqual_ranges[0].high) &
	       " cs2 " & int2str(g_cs_lqual_ranges[1].low) & " " & int2str(g_cs_lqual_ranges[1].high) &
	       " cs3 " & int2str(g_cs_lqual_ranges[2].low) & " " & int2str(g_cs_lqual_ranges[2].high) &
	       " cs4 " & int2str(g_cs_lqual_ranges[3].low);
	f_vty_config2(PCUVTY, {"pcu"}, cmd);

	cmd := "mcs link-quality-ranges" &
	       " mcs1 " & int2str(g_mcs_lqual_ranges[0].high) &
	       " mcs2 " & int2str(g_mcs_lqual_ranges[1].low) & " " & int2str(g_mcs_lqual_ranges[1].high) &
	       " mcs3 " & int2str(g_mcs_lqual_ranges[2].low) & " " & int2str(g_mcs_lqual_ranges[2].high) &
	       " mcs4 " & int2str(g_mcs_lqual_ranges[3].low) & " " & int2str(g_mcs_lqual_ranges[3].high) &
	       " mcs5 " & int2str(g_mcs_lqual_ranges[4].low) & " " & int2str(g_mcs_lqual_ranges[4].high) &
	       " mcs6 " & int2str(g_mcs_lqual_ranges[5].low) & " " & int2str(g_mcs_lqual_ranges[5].high) &
	       " mcs7 " & int2str(g_mcs_lqual_ranges[6].low) & " " & int2str(g_mcs_lqual_ranges[6].high) &
	       " mcs8 " & int2str(g_mcs_lqual_ranges[7].low) & " " & int2str(g_mcs_lqual_ranges[7].high) &
	       " mcs9 " & int2str(g_mcs_lqual_ranges[8].low);
	f_vty_config2(PCUVTY, {"pcu"}, cmd);
}

private function f_init_vty(charstring id, boolean egprs_only) runs on RAW_PCU_Test_CT {
	map(self:PCUVTY, system:PCUVTY);
	f_vty_set_prompts(PCUVTY);
	f_vty_transceive(PCUVTY, "enable");

	/* This will be removed soon, not needed. EGPRS support is controlled through pcu_ind flags */
	if (egprs_only) {
		f_vty_config2(PCUVTY, {"pcu"}, "egprs only");
	} else {
		f_vty_config2(PCUVTY, {"pcu"}, "no egprs");
	}

	if (g_force_two_phase_access) {
		f_vty_config2(PCUVTY, {"pcu"}, "two-phase-access");
	} else {
		f_vty_config2(PCUVTY, {"pcu"}, "no two-phase-access");
	}
}

function f_init_raw(charstring id, template (value) PCUIF_info_ind info_ind := ts_PCUIF_INFO_default)
runs on RAW_PCU_Test_CT {
	/* Start the guard timer */
	g_T_guard.start;
	activate(as_Tguard_RAW());

	/* Init PCU interface component */
	vc_PCUIF := RAW_PCUIF_CT.create("PCUIF");
	connect(vc_PCUIF:MTC, self:PCUIF);
	map(vc_PCUIF:PCU, system:PCU);

	/* Create one BTS component (we may want more some day) */
	vc_BTS := RAW_PCU_BTS_CT.create("BTS");
	connect(vc_BTS:PCUIF, vc_PCUIF:BTS);
	connect(vc_BTS:TC, self:BTS);

	f_init_vty(id, f_pcuif_ind_flags_egprs_enabled(valueof(info_ind.flags)));

	f_init_statsd(id, vc_STATSD, mp_pcu_statsd_ip, mp_pcu_statsd_port);
	/* This is normally done in the ConnHdlr component, but here
	 * the Test_CT doubles as ConnHdlr */
	connect(self:STATSD_PROC, vc_STATSD:STATSD_PROC);

	vc_PCUIF.start(f_PCUIF_CT_handler(mp_pcu_sock_path));
	vc_BTS.start(f_BTS_CT_handler(0, valueof(info_ind)));

	/* Wait until the BTS is ready (SI13 negotiated) */
	BTS.receive(tr_RAW_PCU_EV(BTS_EV_SI13_NEGO));
}

/* Register TLLI of each allocated GprsMS instance */
private function f_multi_ms_bssgp_register()
runs on RAW_PCU_Test_CT {
	for (var integer i := 0; i < lengthof(g_ms); i := i + 1) {
		f_bssgp_client_llgmm_assign(TLLI_UNUSED, g_ms[i].tlli);
	}
}

/* Allocate [and activate] an Uplink TBF for each allocated GprsMS instance */
private function f_multi_ms_establish_tbf(boolean do_activate := false)
runs on RAW_PCU_Test_CT {
	for (var integer i := 0; i < lengthof(g_ms); i := i + 1) {
		/* Establish an Uplink TBF */
		f_ms_establish_ul_tbf(g_ms[i]);

		/* Send a random block, so this TBF becomes "active" */
		if (do_activate) {
			/* FIXME: use the new APU by Pau to get correct TRX/TS here */
			var template TsTrxBtsNum nr := ts_TsTrxBtsNum(7, i mod 8);
			var octetstring dummy := f_rnd_octstring(12);
			var RlcmacDlBlock dl_block;
			var uint32_t poll_fn;

			f_ms_tx_ul_data_block(g_ms[i], dummy, with_tlli := true, nr := nr);
			f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, poll_fn, nr := nr);
		}
	}
}

private function f_ms_establish_ul_tbf_2phase_access(inout GprsMS ms,
						     template (omit) RlcmacUlCtrlMsg pkt_res_req := omit)
runs on RAW_PCU_Test_CT return PollFnCtx {
	var PollFnCtx pollctx;

	/* Single block (two phase) packet access */
	var uint16_t ra := bit2int(chan_req_sb);
	if (g_force_two_phase_access) {
		/* If 2phase access is enforced by the network, then let's
		 * request a One phase packet access, we'll receive a single block
		 * anyway
		 */
		 ra := bit2int(chan_req_def);
	}

	/* Establish an Uplink TBF */
	f_ms_use_ra(ms, ra, ra_is_11bit := 0);
	f_ms_establish_ul_tbf(ms);

	/* Make sure we've got an Uplink TBF assignment */
	if (not match(ms.ul_tbf.ass.ccch, tr_PacketUlSglAssign)) {
		setverdict(fail, "Wrong Packet Uplink Assignment received: ", ms.ul_tbf.ass.ccch, " vs exp: ", tr_PacketUlSglAssign);
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Send PACKET RESOURCE REQUEST
	 * (see 3GPP TS 04.60 "7.1.3.1 Initiation of the Packet resource request procedure")
	 */
	if (istemplatekind(pkt_res_req, "omit")) {
		pkt_res_req := ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, omit);
	}

	f_ms_tx_ul_block(ms, ts_RLC_UL_CTRL_ACK(pkt_res_req), 0, nr := f_ms_tx_TsTrxBtsNum(ms));
	/* Store 1st UlTBF context before receiving next one, will will
	 * overwrite the TS allocation on MS with info from new UL TBF:
	 */
	pollctx.tstrxbts := f_ms_tx_TsTrxBtsNum(ms);
	f_ms_rx_pkt_ass_pacch(ms, pollctx.fn, tr_RLCMAC_UL_PACKET_ASS, nr := f_ms_tx_TsTrxBtsNum(ms));
	return pollctx;
}

testcase TC_pcuif_suspend() runs on RAW_PCU_Test_CT {
	var octetstring ra_id := enc_RoutingAreaIdentification(mp_gb_cfg.bvc[0].cell_id.ra_id);
	var GprsTlli tlli := 'FFFFFFFF'O;
	timer T;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();

	BTS.send(ts_PCUIF_SUSP_REQ(0, tlli, ra_id, 0));

	T.start(2.0);
	alt {
	[] BSSGP_SIG[0].receive(tr_BSSGP_SUSPEND(tlli, mp_gb_cfg.bvc[0].cell_id.ra_id)) {
		setverdict(pass);
		}
	[] T.timeout {
		setverdict(fail, "Timeout waiting for BSSGP SUSPEND");
		}
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test of correct Timing Advance at the time of TBF establishment
 * (derived from timing offset of the Access Burst). */
testcase TC_ta_rach_imm_ass() runs on RAW_PCU_Test_CT {
	var GprsMS ms;

	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */
	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* We cannot send too many TBF requests in a short time because
	 * at some point the PCU will fail to allocate a new TBF. */
	for (var TimingAdvance ta := 0; ta < 64; ta := ta + 16) {
		/* Establish an Uplink TBF (send RACH.ind with current TA) */
		ms.ta := ta;
		f_ms_establish_ul_tbf(ms);

		/* Make sure Timing Advance IE matches out expectations */
		if (ms.ul_tbf.rr_imm_ass.payload.imm_ass.timing_advance != ta) {
			setverdict(fail, "Timing Advance mismatch: ",
				   ms.ul_tbf.rr_imm_ass.payload.imm_ass.timing_advance,
				   " vs expected ", ta);
			break;
		}
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify Timing Advance value(s) indicated during the packet Downlink assignment
 * procedure as per 3GPP TS 44.018, section 3.5.3. There seems to be a bug in the
 * IUT that causes it to send an unreasonable Timing Advance value > 0 despite
 * no active TBF exists at the moment of establishment (idle mode). */
testcase TC_ta_idle_dl_tbf_ass() runs on RAW_PCU_Test_CT {
	var OCT4 tlli := f_rnd_octstring(4);
	var GsmRrMessage rr_imm_ass;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, tlli);

	/* SGSN sends some DL data, PCU will initiate Packet Downlink
	 * Assignment on CCCH (PCH). We don't care about the payload. */
	BSSGP[0].send(ts_BSSGP_DL_UD(tlli, f_rnd_octstring(10)));
	rr_imm_ass := f_pcuif_rx_imm_ass(PCU_IF_SAPI_PCH, tr_IMM_TBF_ASS(dl := true));

	/* Make sure that Timing Advance is 0 (the actual value is not known yet).
	 * As per 3GPP S 44.018, section 3.5.3.1.2, the network *shall* initiate
	 * the procedures defined in 3GPP TS 44.060 or use the polling mechanism. */
	if (rr_imm_ass.payload.imm_ass.timing_advance != 0) {
		setverdict(fail, "Timing Advance value doesn't match");
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify that the PCU generates valid PTCCH/D messages
 * while neither Uplink nor Downlink TBF is established. */
testcase TC_ta_ptcch_idle() runs on RAW_PCU_Test_CT {
	var PTCCHDownlinkMsg ptcch_msg;
	var PCUIF_Message pcu_msg;
	timer T;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Sent an RTS.req for PTCCH/D */
	BTS.send(ts_PCUIF_RTS_REQ(bts_nr := 0, trx_nr := 0, ts_nr := 7,
				  sapi := PCU_IF_SAPI_PTCCH, fn := 0,
				  arfcn := 871, block_nr := 0));
	T.start(5.0);
	alt {
	[] BTS.receive(tr_PCUIF_DATA_REQ(bts_nr := 0, trx_nr := 0, ts_nr := 7,
					 sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg {
		log("Rx DATA.req message: ", pcu_msg);
		setverdict(pass);
		}
	[] BTS.receive(PCUIF_Message:?) { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for a PTCCH/D block");
		f_shutdown(__BFILE__, __LINE__);
		}
	}

	ptcch_msg := dec_PTCCHDownlinkMsg(pcu_msg.u.data_req.data);
	log("Decoded PTCCH/D message: ", ptcch_msg);

	/* Make sure the message is encoded correctly
	 * TODO: do we expect all TA values to be equal '1111111'B? */
	if (not match(ptcch_msg, tr_PTCCHDownlinkMsg)) {
		setverdict(fail, "Malformed PTCCH/D message");
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test of correct Timing Advance during an active Uplink TBF.
 *
 * Unlike the circuit-switched domain, Uplink transmissions on PDCH time-slots
 * are not continuous and there can be long time gaps between them. This happens
 * due to a bursty nature of packet data. The actual Timing Advance of a MS may
 * significantly change between such rare Uplink transmissions, so GPRS introduces
 * additional mechanisms to control Timing Advance, and thus reduce interference
 * between neighboring TDMA time-slots.
 *
 * At the moment of Uplink TBF establishment, initial Timing Advance is measured
 * from ToA (Timing of Arrival) of an Access Burst. This is covered by another
 * test case - TC_ta_rach_imm_ass. In response to that Access Burst the network
 * sends Immediate Assignment on AGCH, which _may_ contain Timing Advance Index
 * among with the initial Timing Advance value. And here PTCCH comes to play.
 *
 * PTCCH is a unidirectional channel on which the network can instruct a sub-set
 * of 16 MS (whether TBFs are active or not) to adjust their Timing Advance
 * continuously. To ensure continuous measurements of the signal propagation
 * delay, the MSs shall transmit Access Bursts on Uplink (PTCCH/U) on sub-slots
 * defined by an assigned Timing Advance Index (see 3GPP TS 45.002).
 *
 * The purpose of this test case is to verify the assignment of Timing Advance
 * Index, and the process of Timing Advance notification on PTCCH/D. The MTC
 * first establishes several Uplink TBFs, but does not transmit any Uplink
 * blocks on them. During 4 TDMA multi-frame periods the MTC is sending RACH
 * indications to the PCU, checking the correctness of two received PTCCH/D
 * messages (period of PTCCH/D is two multi-frames).
 */

/* List of ToA values for Access Bursts to be sent on PTCCH/U,
 * each ToA (Timing of Arrival) value is in units of 1/4 of
 * a symbol (i.e. 1 symbol is 4 QTA units). */
type record length(16) of int16_t PTCCH_TAI_ToA_MAP;
const PTCCH_TAI_ToA_MAP ptcch_toa_map_def := {
	0, 0, 0, 0,
	0, 0, 0, 0,
	0, 0, 0, 0,
	0, 0, 0, 0
};

private altstep as_ta_ptcch(uint8_t bts_nr := 0, uint8_t trx_nr := 0, uint8_t ts_nr := 7,
			    in PTCCH_TAI_ToA_MAP toa_map := ptcch_toa_map_def)
runs on RAW_PCU_Test_CT {
	var RAW_PCU_Event event;
	var integer ss;

	/* Send Access Bursts on PTCCH/U for every TA Index */
	[] BTS.receive(tr_RAW_PCU_EV(TDMA_EV_PTCCH_UL_BURST)) -> value event {
		ss := f_tdma_ptcch_fn2ss(event.data.tdma_fn);
		if (ss < 0) { /* Shall not happen */
			f_shutdown(__BFILE__, __LINE__);
		}

		log("Sending an Access Burst on PTCCH/U",
		    ", sub-slot=", ss, " (TAI)",
		    ", fn=", event.data.tdma_fn,
		    ", ToA=", toa_map[ss], " (QTA)");
		/* TODO: do we care about RA and burst format? */
		BTS.send(ts_PCUIF_RACH_IND(bts_nr, trx_nr, ts_nr,
					   ra := oct2int('3A'O),
					   is_11bit := 0,
					   burst_type := BURST_TYPE_0,
					   fn := event.data.tdma_fn,
					   arfcn := 871,
					   qta := toa_map[ss],
					   sapi := PCU_IF_SAPI_PTCCH));
		repeat;
		}
}

private function f_TC_ta_ptcch_ul_multi_tbf(in PTCCH_TAI_ToA_MAP ptcch_toa_map,
					    template PTCCHDownlinkMsg t_ta_msg)
runs on RAW_PCU_Test_CT {
	var PTCCHDownlinkMsg ta_msg;
	var PCUIF_Message pcu_msg;
	timer T;

	/* First, send an RTS.req for the upcoming PTCCH/D block */
	BTS.send(ts_PCUIF_RTS_REQ(bts_nr := 0, trx_nr := 0, ts_nr := 7,
				  sapi := PCU_IF_SAPI_PTCCH, fn := 0,
				  arfcn := 871, block_nr := 0));
	T.start(2.0);
	alt {
	/* Keep sending of Access Bursts during two multi-frames (period of PTCCH/D)
	 * with increasing ToA (Timing of Arrival) values: 0, 7, 14, 28, 35... */
	[] as_ta_ptcch(bts_nr := 0, trx_nr := 0, ts_nr := 7, toa_map := ptcch_toa_map);
	/* In the end of 2nd multi-frame we should receive a PTCCH/D block */
	[] BTS.receive(tr_PCUIF_DATA_REQ(bts_nr := 0, trx_nr := 0, ts_nr := 7,
					 sapi := PCU_IF_SAPI_PTCCH)) -> value pcu_msg {
		ta_msg := dec_PTCCHDownlinkMsg(pcu_msg.u.data_req.data);
		log("Rx PTCCH/D message: ", ta_msg);

		/* Make sure Timing Advance values match our expectations */
		if (not match(ta_msg, t_ta_msg)) {
			setverdict(fail, "PTCCH/D message does not match: ", t_ta_msg);
		}
		}
	[] BTS.receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Timeout waiting for a PTCCH/D block");
		}
	}
}

testcase TC_ta_ptcch_ul_multi_tbf() runs on RAW_PCU_Test_CT {
	var template PacketUlAssign t_ul_tbf_ass;
	var GprsMS ms;

	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Enable forwarding of PTCCH/U TDMA events to us */
	BTS.send(ts_RAW_PCU_CMD(TDMA_CMD_ENABLE_PTCCH_UL_FWD));

	/* Establish 7 Uplink TBFs (USF flag is 3 bits long, '111'B is reserved) */
	for (var integer i := 0; i < 7; i := i + 1) {
		/* Establish an Uplink TBF */
		f_ms_establish_ul_tbf(ms);

		/* We expect incremental TFI/USF assignment (dynamic allocation) */
		t_ul_tbf_ass := tr_PacketUlDynAssign(tfi := i, usf := i);
		if (not match(ms.ul_tbf.ass.ccch, t_ul_tbf_ass)) {
			setverdict(fail, "Failed to match Packet Uplink Assignment for #", i);
			break;
		}

		/* We also expect Timing Advance Index to be a part of the assignment */
		if (ms.ul_tbf.ass.ccch.dynamic.ta_index != i) {
			setverdict(fail, "Failed to match Timing Advance Index for #", i);
			/* Keep going, the current OsmoPCU does not assign TA Index */
		}
	}

	/* Prepare a list of ToA values for Access Bursts to be sent on PTCCH/U */
	var PTCCH_TAI_ToA_MAP toa_map := ptcch_toa_map_def;
	for (var integer i := 0; i < 7; i := i + 1) {
		/* ToA in units of 1/4 of a symbol */
		toa_map[i] := (i + 1) * 7 * 4;
	}

	/* Now we have all 7 TBFs established in one-phase access mode,
	 * however we will not be sending any data on them. Instead, we
	 * will be sending RACH.ind on PTCCH/U during 4 multi-frame
	 * periods (TAI 0..8), and then will check two PTCCH/D blocks.
	 *
	 * Why not 4 TBFs at once? Because Uplink is delayed by 3 TDMA
	 * time-slots, so at the moment of scheduling a PTCCH/D block
	 * the PCU has odd number of PTCCH/U Access Bursts received. */
	f_TC_ta_ptcch_ul_multi_tbf(toa_map, tr_PTCCHDownlinkMsg(
		tai0_ta :=  7, tai1_ta := 14, tai2_ta := 21,
		/* Other values are not known (yet) */
		tai3_ta := ?));
	f_TC_ta_ptcch_ul_multi_tbf(toa_map, tr_PTCCHDownlinkMsg(
		tai0_ta :=  7, tai1_ta := 14, tai2_ta := 21,
		tai3_ta := 28, tai4_ta := 35, tai5_ta := 42,
		/* Other values are out of our interest */
		tai6_ta := ?));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Default link quality adaptation (Coding Scheme) ranges (inclusive).
 * OsmoPCU (VTY): cs link-quality-ranges cs1 6 cs2 5 8 cs3 7 13 cs4 12
 *
 * NOTE: the ranges are intentionally overlapping because OsmoPCU
 * does not change CS/MCS on the range borders (5-6, 7-8, 12-13). */
private template integer CS1_lqual_dB_range := (-infinity .. 6);
private template integer CS2_lqual_dB_range := (5 .. 8);
private template integer CS3_lqual_dB_range := (7 .. 13);
private template integer CS4_lqual_dB_range := (12 .. infinity);

testcase TC_cs_lqual_ul_tbf() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var GprsMS ms;
	var uint32_t unused_fn, sched_fn;
	var uint4_t cv;

	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);


	/* The actual / old link quality values. We need to keep track of the old
	 * (basically previous) link quality value, because OsmoPCU actually
	 * changes the coding scheme if not only the actual, but also the old
	 * value leaves the current link quality range (window). */
	var integer lqual_old;
	ms.lqual_cb := 0;

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine. */
	/* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
	/* Set CV = 15 to signal there's still more than BS_CV_MAX blocks to be sent */
	f_ms_tx_ul_data_block(ms, f_rnd_octstring(16), cv := 15, with_tlli := true)
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* 16 UL blocks (0 .. 15 dB, step = 1 cB) */
	for (var integer i := 150; i >= 0; i := i - 1) {
		/* Update the old / actual link quality */
		lqual_old := ms.lqual_cb;
		ms.lqual_cb := 150 - i;

		/* Enqueue DATA.ind (both TDMA frame and block numbers to be patched) */
		log("Sending DATA.ind with link quality (dB): ", ms.lqual_cb);
		if (i > g_bs_cv_max) {
			cv := 15;
		} else {
			cv := i;
		}

		f_ms_tx_ul_data_block(ms, f_rnd_octstring(10), cv := cv)

		/* we will receive UL ACK/NACK from time to time. In that case, check CdCofing increases */
		f_rx_rlcmac_dl_block(dl_block, unused_fn);
		if (match(dl_block, tr_RLCMAC_DUMMY_CTRL())) {
			continue;
		}
		if (not match(dl_block, tr_RLCMAC_UL_ACK_NACK_GPRS(ul_tfi := ?)) and
		    not match(dl_block, tr_RLCMAC_UL_ACK_NACK_EGPRS(ul_tfi := ?))) {
			setverdict(fail, "Failed to match Packet Uplink ACK / NACK:", dl_block);
			f_shutdown(__BFILE__, __LINE__);
		}

		log("Rx Packet Uplink ACK / NACK with Channel Coding Command: ",
		    dl_block.ctrl.payload.u.ul_ack_nack.gprs.ch_coding_cmd);

		/* Match the received Channel Coding Command. Since we are increasing
		 * the link quality value on each iteration and not decreasing, there
		 * is no need to check the both old and current link quality values. */
		var template ChCodingCommand ch_coding;
		select (lqual_old / 10) {
		case (CS1_lqual_dB_range) { ch_coding := CH_CODING_CS1; }
		case (CS2_lqual_dB_range) { ch_coding := CH_CODING_CS2; }
		case (CS3_lqual_dB_range) { ch_coding := CH_CODING_CS3; }
		case (CS4_lqual_dB_range) { ch_coding := CH_CODING_CS4; }
		}

		if (not match(dl_block.ctrl.payload.u.ul_ack_nack.gprs.ch_coding_cmd, ch_coding)) {
			setverdict(fail, "Channel Coding does not match our expectations: ", ch_coding);
			f_shutdown(__BFILE__, __LINE__);
		}
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test the max UL CS set by VTY works fine */
testcase TC_cs_initial_ul() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var ChCodingCommand last_ch_coding;
	var uint32_t unused_fn, sched_fn;
	var GprsMS ms;

	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Set initial UL CS to 3 */
	g_cs_initial_ul := 3;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Take lqual (dB->cB) so that we stay in that CS */
	ms.lqual_cb := g_cs_lqual_ranges[2].low * 10;

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine. */
	/* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
	/* Set CV = 15 to signal there's still more than BS_CV_MAX blocks to be sent */
	f_ms_tx_ul_data_block(ms, f_rnd_octstring(16), cv := 15, with_tlli := true)
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* Send UL blocks, until we receive UL ACK/NACK and check we are in same initial CS: */
	while (true) {
		f_ms_tx_ul_data_block(ms, f_rnd_octstring(10), cv := 15);
		f_rx_rlcmac_dl_block(dl_block, unused_fn);
		if (match(dl_block, tr_RLCMAC_DUMMY_CTRL())) {
			continue;
		}

		if (not match(dl_block, tr_RLCMAC_UL_ACK_NACK_GPRS(ul_tfi := ?)) and
		    not match(dl_block, tr_RLCMAC_UL_ACK_NACK_EGPRS(ul_tfi := ?))) {
			setverdict(fail, "Failed to match Packet Uplink ACK / NACK:", dl_block);
			f_shutdown(__BFILE__, __LINE__);
			break;
		}

		last_ch_coding := dl_block.ctrl.payload.u.ul_ack_nack.gprs.ch_coding_cmd;
		break;
	}
	if (last_ch_coding != CH_CODING_CS3) {
		setverdict(fail, "Channel Coding does not match our expectations (CS-3): ", last_ch_coding);
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Remaining UL blocks are used to make sure regardless of initial
	/* lqual, we can go lower at any time */
	 /* 0 dB, make sure we downgrade CS */
	ms.lqual_cb := 0;
	/* 5 UL blocks, check we are in same initial CS: */
	f_ms_tx_ul_data_block_multi(ms, 5);
	/* Enqueue RTS.req, expect DATA.req with UL ACK from the PCU */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, unused_fn);
	last_ch_coding := dl_block.ctrl.payload.u.ul_ack_nack.gprs.ch_coding_cmd;

	if (last_ch_coding != CH_CODING_CS1) {
		setverdict(fail, "Channel Coding does not match our expectations (CS-1): ", last_ch_coding);
		f_shutdown(__BFILE__, __LINE__);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test the max UL CS set by VTY works fine */
testcase TC_cs_max_ul() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var ChCodingCommand last_ch_coding;
	var uint32_t unused_fn, sched_fn;
	var GprsMS ms;

	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Set maximum allowed UL CS to 3 */
	g_cs_max_ul := 3;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine. */
	/* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
	/* Set CV = 15 to signal there's still more than BS_CV_MAX blocks to be sent */
	f_ms_tx_ul_data_block(ms, f_rnd_octstring(16), cv := 15, with_tlli := true)
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	ms.lqual_cb :=  40*10; /* 40 dB */
	f_ms_tx_ul_data_block_multi(ms, 16);

	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, unused_fn);
	last_ch_coding := dl_block.ctrl.payload.u.ul_ack_nack.gprs.ch_coding_cmd;

	if (last_ch_coding != CH_CODING_CS3) {
		setverdict(fail, "Channel Coding does not match our expectations (CS-3): ", last_ch_coding);
		f_shutdown(__BFILE__, __LINE__);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test the initial DL CS set by VTY works fine */
testcase TC_cs_initial_dl() runs on RAW_PCU_Test_CT {
	var octetstring data := f_rnd_octstring(10);
	var CodingScheme exp_dl_cs_mcs;
	var RlcmacDlBlock dl_block;
	var uint32_t poll_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Set initial allowed DL CS to 3 */
	g_cs_initial_dl := 3;
	exp_dl_cs_mcs := CS_3;
	/* Set maximum allowed UL CS to 4 */
	g_cs_max_dl := 4;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, poll_fn, data, 0, exp_dl_cs_mcs);

	/* ACK the DL block */
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block, '1'B);
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc),
			 f_dl_block_ack_fn(dl_block, poll_fn));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify scheduling of multiple Downlink data blocks, enough to reach CS4 */
function f_dl_data_exp_cs(template CodingScheme exp_cs := ?) runs on RAW_PCU_Test_CT {
	var octetstring data := f_rnd_octstring(1000);
	var RlcmacDlBlock prev_dl_block, dl_block;
	var uint32_t ack_fn;
	var uint32_t fn;
	var GprsMS ms;
	var integer tx_data_remain := 5;
	var integer bsn := 0;

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();

	ms := g_ms[0]; /* We only use first MS in this test */
	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS */
	f_sleep(X2002);

	for (var integer i := 0; i < 250; i := i + 1) {
		bsn := i mod 128;
		f_rx_rlcmac_dl_block(dl_block, fn);

		if (match(dl_block, tr_RLCMAC_DUMMY_CTRL)) {
			/* No more data to receive, done */
			break;
		}

		f_rlcmac_dl_block_exp_data(dl_block, ?, bsn, cs_gprs_any);

		/* Keep Ack/Nack description updated */
		f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block);

		/* TDMA frame number on which we are supposed to send the ACK */
		if (dl_block.data.mac_hdr.mac_hdr.rrbp_valid) {
			ack_fn := f_dl_block_ack_fn(dl_block, fn);
			f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc), ack_fn);
			if (tx_data_remain != 0) {
				/* Submit more data from time to time to keep the TBF ongoing */
				BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
				tx_data_remain := tx_data_remain - 1;
			}
		}
		prev_dl_block := dl_block;
	}

	bsn := (bsn + 127) mod 128; /* previous bsn: bsn -1 */
	f_rlcmac_dl_block_exp_data(prev_dl_block, ?, bsn, exp_cs);


	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify DL CS above "cs max" set by VTY is never used */
testcase TC_cs_max_dl() runs on RAW_PCU_Test_CT {
	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Set maximum allowed DL CS to 3 */
	g_cs_initial_dl := 1;
	g_cs_max_dl := 3;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	f_dl_data_exp_cs(f_rlcmac_block_int2cs_mcs(g_cs_max_dl, false));
}

/* Check DL CS4 is used in good link conditions if allowed by config */
testcase TC_dl_cs1_to_cs4() runs on RAW_PCU_Test_CT {
	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Set initial DL CS to 1 & maximum allowed DL CS to 4 */
	g_cs_initial_dl := 1;
	g_cs_max_dl := 4;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	f_dl_data_exp_cs(f_rlcmac_block_int2cs_mcs(g_cs_max_dl, false));
}

/* Test the initial UL MCS set by VTY works fine */
testcase TC_mcs_initial_ul() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var PollFnCtx pollctx;
	var EgprsChCodingCommand last_ch_coding;
	var uint32_t unused_fn, sched_fn;
	var GprsMS ms;
	var CodingScheme exp_ul_mcs;
	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	};
	var MultislotCap_EGPRS mscap_egprs := {
		egprsmultislotclass := '00011'B,
		egprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, mscap_egprs)) };

	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Set initial UL MCS to 3 */
	g_mcs_initial_ul := 3;
	exp_ul_mcs := f_rlcmac_block_int2cs_mcs(g_mcs_initial_ul, true);
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Take lqual (dB->cB) so that we stay in that MCS */
	ms.lqual_cb := g_mcs_lqual_ranges[2].low * 10;

	/* Send PACKET RESOURCE REQUEST to upgrade to EGPRS */
	pollctx := f_ms_establish_ul_tbf_2phase_access(ms, ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, ms_racap));

	if (not match(ms.ul_tbf.tx_cs_mcs, exp_ul_mcs)) {
		setverdict(fail, "Wrong CS_MCS ", ms.ul_tbf.tx_cs_mcs, " received vs exp ", exp_ul_mcs);
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Pkt Uplink Assignment above sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), pollctx.fn, nr := pollctx.tstrxbts);

	/* Send UL blocks, until we receive UL ACK/NACK and check we are in same initial CS: */
	while (true) {
		f_ms_tx_ul_data_block(ms, f_rnd_octstring(10), cv := 15);
		f_rx_rlcmac_dl_block(dl_block, unused_fn);
		if (match(dl_block, tr_RLCMAC_DUMMY_CTRL())) {
			continue;
		}

		if (not match(dl_block, tr_RLCMAC_UL_ACK_NACK_EGPRS(ul_tfi := ?))) {
			setverdict(fail, "Failed to match Packet Uplink ACK / NACK:", dl_block);
			f_shutdown(__BFILE__, __LINE__);
			break;
		}

		last_ch_coding := dl_block.ctrl.payload.u.ul_ack_nack.egprs.ch_coding_cmd;
		break;
	}
	if (f_rlcmac_block_EgprsChCodingCommand2cs_mcs(last_ch_coding) != exp_ul_mcs) {
		setverdict(fail, "Channel Coding does not match our expectations ", exp_ul_mcs, ": ", last_ch_coding);
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Remaining UL blocks are used to make sure regardless of initial
	 * lqual, we can go lower at any time
	 * 0 dB, make sure we downgrade MCS */
	ms.lqual_cb := 0;
	/* 5 UL blocks, check we are in same initial MCS: */
	f_ms_tx_ul_data_block_multi(ms, 5);
	/* Enqueue RTS.req, expect DATA.req with UL ACK from the PCU */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, unused_fn);
	last_ch_coding := dl_block.ctrl.payload.u.ul_ack_nack.egprs.ch_coding_cmd;

	if (last_ch_coding != CH_CODING_MCS1) {
		setverdict(fail, "Channel Coding does not match our expectations (MCS-1): ", last_ch_coding);
		f_shutdown(__BFILE__, __LINE__);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test the maximum UL MCS set by VTY works fine */
testcase TC_mcs_max_ul() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var EgprsChCodingCommand last_ch_coding;
	var PollFnCtx pollctx;
	var uint32_t unused_fn, sched_fn;
	var GprsMS ms;
	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	};
	var MultislotCap_EGPRS mscap_egprs := {
		egprsmultislotclass := '00011'B,
		egprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, mscap_egprs)) };


	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Set maximum allowed UL MCS to 5 */
	g_mcs_max_ul := 5;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Send PACKET RESOURCE REQUEST to upgrade to EGPRS */
	pollctx := f_ms_establish_ul_tbf_2phase_access(ms, ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, ms_racap));
	/* Pkt Uplink Assignment above sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), pollctx.fn, nr := pollctx.tstrxbts);

	ms.lqual_cb :=  40*10; /* 40 dB */
	f_ms_tx_ul_data_block_multi(ms, 16);

	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, unused_fn);
	last_ch_coding := dl_block.ctrl.payload.u.ul_ack_nack.egprs.ch_coding_cmd;

	if (last_ch_coding != CH_CODING_MCS5) {
		setverdict(fail, "Channel Coding does not match our expectations (MCS-5): ", last_ch_coding);
		f_shutdown(__BFILE__, __LINE__);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test the initial DL CS set by VTY works fine */
testcase TC_mcs_initial_dl() runs on RAW_PCU_Test_CT {
	var octetstring data := f_rnd_octstring(10);
	var CodingScheme exp_dl_cs_mcs;
	var RlcmacDlBlock dl_block;
	var uint32_t poll_fn;
	var GprsMS ms;
	var MultislotCap_GPRS_BSSGP mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	};
	var MultislotCap_EGPRS_BSSGP mscap_egprs := {
		egprsmultislotclass := '00011'B,
		egprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV_BSSGP ms_racap := { valueof(ts_RaCapRec_BSSGP('0001'B /* E-GSM */, mscap_gprs, mscap_egprs)) };

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Set initial allowed DL MCS to 3 */
	g_mcs_initial_dl := 3;
	exp_dl_cs_mcs := MCS_3;
	/* Set maximum allowed DL MCS to 4 */
	g_mcs_max_dl := 4;
	f_pcuvty_set_allowed_cs_mcs();
	f_pcuvty_set_link_quality_ranges();

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data, ms_racap));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, poll_fn, data, 0, exp_dl_cs_mcs);

	/* ACK the DL block */
	f_dltbf_ack_block(ms.dl_tbf, dl_block, '1'B);
	f_ms_tx_ul_block(ms, f_dltbf_ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf, ischosen(dl_block.data_egprs)),
			 f_dl_block_ack_fn(dl_block, poll_fn));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify PCU drops TBF after some time of inactivity. */
testcase TC_t3169() runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind;
	var RlcmacDlBlock dl_block;
	var uint32_t unused_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	info_ind := valueof(ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));
	/* Set timer to 1 sec (default 5) to speedup test: */
	info_ind.t3169 := 1;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine */
	f_ms_tx_ul_data_block(ms, f_rnd_octstring(10), cv := 1, with_tlli := true)
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, unused_fn);
	/* UL block should NOT be received in SGSN, since we didn't get CV=0 */

	/* Wait until T3169 fires (plus 1 extra sec to make sure) */
	f_sleep(int2float(info_ind.t3169) + 1.0);

	/* Send an UL block once again, the TBF should be gone by now so no ACK */
	f_ms_tx_ul_data_block(ms, f_rnd_octstring(10), cv := 0)
	f_rx_rlcmac_dl_block_exp_dummy(dl_block);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify that a Downlink TBF can be assigned using PACCH shortly after the
 * release of prev DL TBF due to MS staying in PDCH for a while (T3192, in PCU
 * T3193) after DL TBF release */
testcase TC_t3193() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var boolean ok;
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var GprsMS ms;
	var AckNackDescription ack_nack_desc := valueof(t_AckNackDescription_init);

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0);

	/* ACK the DL block */
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block, '1'B);
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc),
			 f_dl_block_ack_fn(dl_block, dl_fn));

	/* Now that final DL block is ACKED and TBF is released, T3193 in PCU
	   (T3192 in MS) was started and until it fires the MS will be available
	   on PDCH in case new data arrives from SGSN. Let's verify it: */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_rx_pkt_ass_pacch(ms, sched_fn, tr_RLCMAC_DL_PACKET_ASS);

	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* Now that we confirmed the new assignment in the dl-tbf, lets receive the data and ack it */
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0);
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block, '1'B);
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc),
			 f_dl_block_ack_fn(dl_block, dl_fn));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify PCU handles correctly Countdown Procedure based on BS_CV_MAX */
testcase TC_countdown_procedure() runs on RAW_PCU_Test_CT  {
	var RlcmacDlBlock dl_block;
	var uint32_t sched_fn;
	var octetstring total_payload;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine. */
	total_payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true));
	/* Set CV = 15 to signal there's still more than BS_CV_MAX blocks to be sent */
	f_ms_tx_ul_data_block(ms, total_payload, cv := 15, with_tlli := true)
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* Send enough blocks to test whole procedure: Until Nth block
	   (N=BS_CV_MAX), CV=15 is sent, and then the decreasing countdown value is sent.
	 */
	total_payload := total_payload & f_ms_tx_ul_data_block_multi(ms, 20);
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* receive one message on BSSGP with all aggregated data in payload: */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, total_payload));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify PCU handles correctly CS1..4 with all possible LLC payload sizes fitting alone in one RLC block */
testcase TC_ul_all_sizes() runs on RAW_PCU_Test_CT  {
	var RlcmacDlBlock dl_block;
	var uint32_t dl_fn, sched_fn;
	var octetstring payload;
	var template (value) RlcmacUlBlock ul_data;
	var template (value) LlcBlockHdr blk_hdr;
	var template (value) LlcBlocks blocks;
	var integer blk_len;
	var CodingScheme tx_cs;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine. */
	payload := f_rnd_octstring(16); /* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
	blk_hdr := t_RLCMAC_LLCBLOCK_HDR(length_ind := lengthof(payload),
					 more := false, e := true);
	blocks := { t_RLCMAC_LLCBLOCK(payload, blk_hdr) };
	/* Set CV = 15 to signal there's still more than BS_CV_MAX blocks to be sent */
	ul_data := t_RLCMAC_UL_DATA_TLLI(cs := ms.ul_tbf.tx_cs_mcs,
					 tfi := ms.ul_tbf.tfi,
					 cv := 15,
					 bsn := ms.ul_tbf.bsn,
					 blocks := blocks,
					 tlli := ms.tlli);
	f_ultbf_inc_bsn(ms.ul_tbf);
	f_ms_tx_ul_block(ms, ul_data);

	/* ACK and check it was received fine */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);
	/* receive one message on BSSGP with all aggregated data in payload: */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, payload));

	/* Test sending LLC PDUS of incrementing size */
	var integer max_size := 49;
	for (var integer i := 1; i <= max_size; i := i + 1) {
		var integer cv;
		/* Enqueue DATA.ind (both TDMA frame and block numbers to be patched) */
		log("Sending DATA.ind with LLC payload size ", i);
		if (i < max_size - g_bs_cv_max) {
			cv := 15;
		} else {
			cv := max_size - i;
		}

		blk_len := 3 + 1 + i; /* 3 Header bytes + LI byte + payload length */
		tx_cs := f_rlcmac_block_len_required_cs_mcs(blk_len, false);
		payload := f_rnd_octstring(i);
		blk_hdr := t_RLCMAC_LLCBLOCK_HDR(length_ind := lengthof(payload),
						 more := false, e := true);
		blocks := { t_RLCMAC_LLCBLOCK(payload, blk_hdr) };
		/* Set CV = 15 to signal there's still more than BS_CV_MAX blocks to be sent */
		ul_data := t_RLCMAC_UL_DATA(cs := tx_cs,
					    tfi := ms.ul_tbf.tfi,
					    cv := cv,
					    bsn := ms.ul_tbf.bsn,
					    blocks := blocks);
		f_ultbf_inc_bsn(ms.ul_tbf);
		f_ms_tx_ul_block(ms, ul_data);

		/* receive one message on BSSGP with all aggregated data in payload: */
		BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, payload));

		/* we will receive UL ACK/NACK from time to time, handle it. */
		f_rx_rlcmac_dl_block(dl_block, dl_fn);
		if (match(dl_block, tr_RLCMAC_DUMMY_CTRL())) {
			continue;
		}
		if (not match(dl_block, tr_RLCMAC_UL_ACK_NACK_GPRS(ul_tfi := ?)) and
		    not match(dl_block, tr_RLCMAC_UL_ACK_NACK_EGPRS(ul_tfi := ?))) {
			setverdict(fail, "Failed to match Packet Uplink ACK / NACK:", dl_block);
			f_shutdown(__BFILE__, __LINE__);
		}

		log("Rx Packet Uplink ACK / NACK");
		sched_fn := f_rrbp_ack_fn(dl_fn, dl_block.ctrl.mac_hdr.rrbp);
		/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
		f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

function f_TC_ul_data_toolong_fills_padding_cs(inout GprsMS ms, CodingScheme cs, integer cv) runs on RAW_PCU_Test_CT {
	var octetstring payload;
	var template (value) RlcmacUlBlock ul_data;
	var template (value) LlcBlockHdr blk_hdr;
	var template (value) LlcBlocks blocks;
	var integer block_len, max_valid_data_len;
	timer T;

	block_len := f_rlcmac_cs_mcs2block_len(cs);
	/* We need to send with TLLI since we are in One-Phase Access Contenion
	 * resoultion), so that's -4 bytes of data, -3 for headers, -1 for LI
	 * indicator, -1 for spare bits octet at the end */
	max_valid_data_len := block_len - 4 - 3 - 1 - 1;
	payload := f_rnd_octstring(max_valid_data_len + 1); /* +1 to write LLC data on last padding octet */
	blk_hdr := t_RLCMAC_LLCBLOCK_HDR(length_ind := lengthof(payload),
					 more := false, e := true);
	blocks := { t_RLCMAC_LLCBLOCK(payload, blk_hdr) };
	ul_data := t_RLCMAC_UL_DATA_TLLI(cs := cs,
					 tfi := ms.ul_tbf.tfi,
					 cv := cv,
					 bsn := ms.ul_tbf.bsn,
					 blocks := blocks,
					 tlli := ms.tlli);
	f_ultbf_inc_bsn(ms.ul_tbf);
	f_ms_tx_data_ind(ms, enc_RlcmacUlBlock(valueof(ul_data)));

	T.start(0.5);
	alt {
	[] BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, ?)) {
		setverdict(fail, "LLC PDU in Malformed RLC block was forwarded");
		f_shutdown(__BFILE__, __LINE__);
	}
	[] T.timeout {
		setverdict(pass);
		}
	}
}
/* Verify PCU finds out incorrectly formated RLC block and discards it. This
   blocks intentionally contain last byte of data placed in last byte of RLC
   containing padding/spare bits, which is incorrect. Spare bits exist and are
   described for CS2..4 in 3GPP TS 44.060 Table 10.2.1: "RLC data block size,
   discounting padding in octet" */
testcase TC_ul_data_toolong_fills_padding() runs on RAW_PCU_Test_CT  {
	var GprsMS ms;
	var integer block_len, max_valid_data_len;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	f_TC_ul_data_toolong_fills_padding_cs(ms, CS_2, 2);
	f_TC_ul_data_toolong_fills_padding_cs(ms, CS_3, 1);
	f_TC_ul_data_toolong_fills_padding_cs(ms, CS_4, 0);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test scenario where MS wants to send some data on PDCH against SGSN and it is
 * answered, so TBFs for uplink and later for downlink are created.
 */
private function f_TC_mo_ping_pong_1phase_access(template (present) CodingScheme exp_cs_mcs := ?) runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true);
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	/* Now SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0, exp_cs_mcs);

	/* ACK the DL block */
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block, '1'B);
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc),
			     f_dl_block_ack_fn(dl_block, dl_fn));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test scenario where MS wants to send some data on PDCH against SGSN and it is
 * answered, so TBFs for uplink and later for downlink are created.
 */
testcase TC_mo_ping_pong() runs on RAW_PCU_Test_CT {
	var template CodingScheme exp_cs_mcs := cs_gprs_any;
	f_TC_mo_ping_pong_1phase_access(exp_cs_mcs);
}

/* Test scenario where MS wants to send some data on PDCH against SGSN and it is
 * answered, so TBFs for uplink and later for downlink are created.
 */
private function f_TC_mo_ping_pong_2phase_access(PCUIF_Flags ind_flags,
						 template (value) MSRadioAccessCapabilityV ms_racap,
						 template (present) CodingScheme exp_ul_cs_mcs := ?,
						 template (present) CodingScheme exp_dl_cs_mcs := ?)
runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var PollFnCtx pollctx;
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var uint32_t unused_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(ind_flags));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Send PACKET RESOURCE REQUEST to upgrade to EGPRS */
	pollctx := f_ms_establish_ul_tbf_2phase_access(ms, ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, ms_racap));

	if (not match(ms.ul_tbf.tx_cs_mcs, exp_ul_cs_mcs)) {
		setverdict(fail, "Wrong CS_MCS ", ms.ul_tbf.tx_cs_mcs, " received vs exp ", exp_ul_cs_mcs);
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Send one UL block (without TLLI since we are in Second-Phase Access)
	   and make sure it is ACKED fine */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true);

	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), pollctx.fn, nr := pollctx.tstrxbts);

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	/* Now SGSN sends some DL data, PCU will page on PACCH */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_rx_pkt_ass_pacch(ms, sched_fn, tr_RLCMAC_DL_PACKET_ASS);
	/* DL Ass sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* PCU acks the UL data after having received CV=0) */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, unused_fn);

	/* After acking the dl assignment, dl tbf goes into FLOW state and PCU will provide DL data when BTS asks for it */
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0, exp_dl_cs_mcs);

	/* ACK the DL block */
	f_dltbf_ack_block(ms.dl_tbf, dl_block, '1'B);
	f_ms_tx_ul_block(ms, f_dltbf_ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf, ischosen(dl_block.data_egprs)),
			 f_dl_block_ack_fn(dl_block, dl_fn));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_mo_ping_pong_with_ul_racap() runs on RAW_PCU_Test_CT {
	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, omit)) };
	var template CodingScheme exp_ul_cs_mcs := cs_gprs_any;
	var template CodingScheme exp_dl_cs_mcs := cs_gprs_any;

	f_TC_mo_ping_pong_2phase_access(c_PCUIF_Flags_noMCS, ms_racap, exp_ul_cs_mcs, exp_dl_cs_mcs);
}

testcase TC_mo_ping_pong_with_ul_racap_egprs_only() runs on RAW_PCU_Test_CT {
	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	};
	var MultislotCap_EGPRS mscap_egprs := {
		egprsmultislotclass := '00011'B,
		egprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, mscap_egprs)) };
	var template CodingScheme exp_ul_cs_mcs := mcs_egprs_any;
	var template CodingScheme exp_dl_cs_mcs := mcs_egprs_any;

	f_TC_mo_ping_pong_2phase_access(c_PCUIF_Flags_default, ms_racap, exp_ul_cs_mcs, exp_dl_cs_mcs);
}

testcase TC_force_two_phase_access() runs on RAW_PCU_Test_CT {
	/* Configure PCU to force two phase access */
	g_force_two_phase_access := true;

	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, omit)) };
	var CodingScheme exp_ul_cs_mcs := f_rlcmac_block_int2cs_mcs(g_mcs_initial_ul, false);
	var template CodingScheme exp_dl_cs_mcs := cs_gprs_any;

	f_TC_mo_ping_pong_2phase_access(c_PCUIF_Flags_noMCS, ms_racap, exp_ul_cs_mcs, exp_dl_cs_mcs);
}

/* Test scenario where SGSN wants to send some data against MS and it is
 * answered by the MS on PDCH, so TBFs for downlink and later for uplink are created.
 */
private function f_TC_mt_ping_pong(template (omit) MSRadioAccessCapabilityV_BSSGP ms_racap := omit,
				   template (present) CodingScheme exp_cs_mcs := ?)
runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data, ms_racap));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0, exp_cs_mcs);

	/* ACK the DL block, and request UL TBF at the same time */
	f_dltbf_ack_block(ms.dl_tbf, dl_block, '1'B);
	f_ms_tx_ul_block(ms, f_dltbf_ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf, ischosen(dl_block.data_egprs), c_ChReqDesc_default),
			 f_dl_block_ack_fn(dl_block, dl_fn));

	/* Expect UL ass */
	f_ms_rx_pkt_ass_pacch(ms, sched_fn, tr_RLCMAC_UL_PACKET_ASS);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true);
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_mt_ping_pong() runs on RAW_PCU_Test_CT {
	var template CodingScheme exp_cs_mcs := cs_gprs_any;
	f_TC_mt_ping_pong(omit, exp_cs_mcs);
}

/* TC_mt_ping_pong, but DL-UNITDATA contains RA Access capability with (M)CS
/* information about the MS */
testcase TC_mt_ping_pong_with_dl_racap() runs on RAW_PCU_Test_CT {
	var MultislotCap_GPRS_BSSGP mscap_gprs := {
		gprsmultislotclass := '00011'B,
		gprsextendeddynalloccap := '0'B
	} ;
	var MSRadioAccessCapabilityV_BSSGP ms_racap := { valueof(ts_RaCapRec_BSSGP('0001'B /* E-GSM */, mscap_gprs, omit)) };
	var template CodingScheme exp_cs_mcs := cs_gprs_any;
	f_TC_mt_ping_pong(ms_racap, exp_cs_mcs);
}

/* Verify that if PCU doesn't get one of the intermediate UL data blocks in a UL
 * TBF, it will request retransmission through UL ACK/NACK (with missing block
 * in its bitmap) when CV=0 is received (and hence it knows no more data is to
 * be transferred).
 */
testcase TC_ul_intermediate_retrans() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var template (value) RlcmacUlBlock ul_data;
	var uint32_t sched_fn;
	var octetstring total_payload;
	var octetstring payload;
	var octetstring lost_payload;
	var uint5_t tfi;
	var GprsMS ms;
	var uint32_t payload_fill_len;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);
	tfi := ms.ul_tbf.tfi;

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine. */
	payload := f_rnd_octstring(f_ultbf_payload_fill_length(ms.ul_tbf, true)); /* 16 bytes fills the llc block (because TLLI takes 4 bytes) */
	f_ms_tx_ul_data_block(ms, payload, cv := 15, with_tlli := true);

	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);
	total_payload := payload;

	payload_fill_len := f_ultbf_payload_fill_length(ms.ul_tbf);

	/* Send 2 packets, skip 1 (inc bsn) and send another one */
	payload := f_rnd_octstring(payload_fill_len);
	f_ms_tx_ul_data_block(ms, payload, cv := 15);
	total_payload := total_payload & payload;

	payload := f_rnd_octstring(payload_fill_len);
	f_ms_tx_ul_data_block(ms, payload, cv := 15);
	total_payload := total_payload & payload;

	lost_payload := f_rnd_octstring(payload_fill_len);
	ms.ul_tbf.bsn := ms.ul_tbf.bsn + 1;  /* LOST PAYLOAD bsn=3, will be retransmitted, next bsn is increased +2 */
	total_payload := total_payload & lost_payload;

	payload := f_rnd_octstring(payload_fill_len)
	f_ms_tx_ul_data_block(ms, payload, cv := 15);
	total_payload := total_payload & payload;

	/* Send enough blocks to finish the transmission (since we were sending BSN=15, send BS_CV_MAX packets) */
	total_payload := total_payload & f_ms_tx_ul_data_block_multi(ms, g_bs_cv_max);

	/* On CV=0, we'll receive a UL ACK asking about missing block */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* TODO: check ack ack bitmap (URBB) */
	ul_data := t_RLCMAC_UL_DATA(cs := ms.ul_tbf.tx_cs_mcs,
				    tfi := tfi,
				    cv := 15,
				    bsn := 3,
				    blocks := {t_RLCMAC_LLCBLOCK(lost_payload)});
	f_ms_tx_ul_block(ms, ul_data);

	/* Now final ack is recieved */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* receive one message on BSSGP with all aggregated data in payload: */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, total_payload));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify that if PCU doesn't get an ACK for first DL block after IMM ASS, it
 * will retry by retransmitting both the IMM ASS + DL block after poll (ack)
 * timeout occurs (specified by sent RRBP on DL block). */
testcase TC_imm_ass_dl_block_retrans() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var uint32_t dl_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0);

	/* Now we don't ack the dl block (emulate MS failed receiveing IMM ASS
	 * or GPRS DL, or DL ACK was lost for some reason). As a result, PCU
	 * should retrigger IMM ASS + GPRS DL procedure after poll timeout. */
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0);

	/* ACK the DL block */
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block, '1'B);
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc),
			 f_dl_block_ack_fn(dl_block, dl_fn));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify scheduling of multiple Downlink data blocks during one RRBP. */
testcase TC_dl_flow_more_blocks() runs on RAW_PCU_Test_CT {
	var AckNackDescription ack_nack_desc := valueof(t_AckNackDescription_init);
	var octetstring data := f_rnd_octstring(16);
	var PacketDlAssign dl_tbf_ass;
	var RlcmacDlBlock dl_block;
	var uint32_t ack_fn;
	var uint32_t fn;
	var GprsMS ms;
	timer T := 5.0;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	f_statsd_reset();

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS */
	f_sleep(X2002);

	/* Expect the first (GPRS DL) block with bsn=0 and rrbp_valid=1 */
	f_rx_rlcmac_dl_block_exp_data(dl_block, fn, data, 0);
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block);

	/* TDMA frame number on which we are supposed to send the ACK */
	ack_fn := f_dl_block_ack_fn(dl_block, fn);

	/* SGSN sends more blocks during the indicated RRBP */
	for (var integer bsn := 1; bsn < 63; bsn := bsn + 1) {
		data := f_rnd_octstring(16); /* Random LLC data */
		BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));

		f_rx_rlcmac_dl_block_exp_data(dl_block, fn, data, bsn);

		/* Make sure this block has the same TFI as was assigned
		 * FIXME: this is only valid for GPRS, not EGPRS. */
		if (dl_block.data.mac_hdr.hdr_ext.tfi != ms.dl_tbf.tfi) {
			setverdict(fail, "Rx DL data block with unexpected TFI: ",
				   dl_block.data.mac_hdr.hdr_ext.tfi);
			f_shutdown(__BFILE__, __LINE__);
		}

		/* Keep Ack/Nack description updated */
		f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block);

		/* Break if this is the end of RRBP */
		if (fn == ack_fn) {
			ms.dl_tbf.acknack_desc.final_ack := '1'B;
			break;
		}
	}

	/* This is the end of RRBP, send Packet Downlink Ack/Nack */
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc), fn := fn);

	/* Make sure that the next block (after the Ack) is dummy */
	f_rx_rlcmac_dl_block_exp_dummy(dl_block);

	var StatsDExpects expect := {
		{ name := "TTCN3.bts.0.rach.requests", mtype := "c", min := 0, max := 0},
		{ name := "TTCN3.bts.0.immediate.assignment_DL", mtype := "c", min := 1, max := 1},
		{ name := "TTCN3.bts.0.immediate.assignment_UL", mtype := "c", min := 0, max := 0},
		{ name := "TTCN3.bts.0.tbf.dl.alloc", mtype := "c", min := 1, max := 1},
		{ name := "TTCN3.bts.0.tbf.ul.alloc", mtype := "c", min := 0, max := 0},
		{ name := "TTCN3.bts.0.rlc.dl_payload_bytes", mtype := "c", min := 112, max := 112},
		{ name := "TTCN3.bts.0.rlc.ul_payload_bytes", mtype := "c", min := 0, max := 0}
	};
	f_statsd_expect(expect);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify Decoding and segmentation of UL LLC PDUs into RLC data blocks, OS#4559.
 * Check "GPRS from A-Z" slide "Example of LI-Field and E-Bit" page 186.
 * Check "3GPP TS 44.060" Annex B. */
testcase TC_ul_flow_multiple_llc_blocks() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring dataA := f_rnd_octstring(20);
	var octetstring dataB := f_rnd_octstring(13);
	var octetstring dataC := f_rnd_octstring(3);
	var octetstring dataD := f_rnd_octstring(12);
	var uint32_t sched_fn;
	var GprsMS ms;
	var template (value) RlcmacUlBlock ul_data;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Summary of what's transmitted:
	 * 1- UL RlcDataBlock(dataA) [BSN=0, CV=3]
	 * 2- UL RlcDataBlock(dataA finished, dataB starts) [BSN=1, CV=2]
	 * 3- UL RlcDataBlock(dataB finished, dataC starts and finishes, dataD starts) [BSN=2, CV=1]
	 * 4- UL RlcDataBlock(dataD finishes) [BSN=3, CV=0]
	 * And on SGSN we receive 4 packets, one for each LlcBlock dataA..D.
	 * We'll also receive some UL ACK/NACK we need to reply with CTRL ACK.
	 */

	/*  UL RlcDataBlock(dataA) [BSN=0, CV=3] */
	ul_data := t_RLCMAC_UL_DATA_TLLI(cs := CS_1,
				    tfi := ms.ul_tbf.tfi,
				    cv := 3,
				    bsn := ms.ul_tbf.bsn,
				    blocks := { t_RLCMAC_LLCBLOCK(substr(dataA, 0, 16)) },
				    tlli := ms.tlli);
	/* Indicate no llc header, meaning first LLC block doesn't finish in current
	 * RLCMAC block being sent. */
	ul_data.data.mac_hdr.e := true;
	f_ultbf_inc_bsn(ms.ul_tbf);
	f_ms_tx_ul_block(ms, ul_data);

	/* UL RlcDataBlock(dataA finished, dataB starts) [BSN=1, CV=2] */
	ul_data := t_RLCMAC_UL_DATA_TLLI(cs := CS_1,
				    tfi := ms.ul_tbf.tfi,
				    cv := 2,
				    bsn := ms.ul_tbf.bsn,
				    blocks := { t_RLCMAC_LLCBLOCK(substr(dataA, 16, 4),
								  t_RLCMAC_LLCBLOCK_HDR(length_ind := 4, more := true, e := true)),
						t_RLCMAC_LLCBLOCK(substr(dataB, 0, 11))
					      },
				    tlli := ms.tlli);
	f_ultbf_inc_bsn(ms.ul_tbf);
	f_ms_tx_ul_block(ms, ul_data);

	/* UL block dataA should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, dataA));

	/* UL RlcDataBlock(dataB finished, dataC starts and finishes, dataD starts) [BSN=2, CV=1] */
	ul_data := t_RLCMAC_UL_DATA_TLLI(cs := CS_1,
				    tfi := ms.ul_tbf.tfi,
				    cv := 1,
				    bsn := ms.ul_tbf.bsn,
				    blocks := { t_RLCMAC_LLCBLOCK(substr(dataB, 11, 2),
								 t_RLCMAC_LLCBLOCK_HDR(length_ind := 2, more := true, e := false)),
						t_RLCMAC_LLCBLOCK(substr(dataC, 0, 3),
								  t_RLCMAC_LLCBLOCK_HDR(length_ind := 3, more := true, e := true)),
						t_RLCMAC_LLCBLOCK(substr(dataD, 0, 9))
					      },
				    tlli := ms.tlli);
	f_ultbf_inc_bsn(ms.ul_tbf);
	f_ms_tx_ul_block(ms, ul_data);

	/* UL block dataB and dataC should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, dataB));
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, dataC));

	/* UL RlcDataBlock(dataD finishes) [BSN=3, CV=0] */
	ul_data := t_RLCMAC_UL_DATA_TLLI(
				    cs := CS_1,
				    tfi := ms.ul_tbf.tfi,
				    cv := 0,
				    bsn := ms.ul_tbf.bsn,
				    blocks := { t_RLCMAC_LLCBLOCK(substr(dataD, 9, 3),
								  t_RLCMAC_LLCBLOCK_HDR(length_ind := 3, more := false, e := true))
					      },
				    tlli := ms.tlli);
	f_ultbf_inc_bsn(ms.ul_tbf);
	f_ms_tx_ul_block(ms, ul_data);

	/* UL block dataB and dataD should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id, dataD));

	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify allocation and use of multislot tbf, triggered by MS class provided in SGSN. SYS#5131 */
testcase TC_dl_multislot_tbf_ms_class_from_sgsn() runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind := valueof(ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));
	var octetstring data := f_rnd_octstring(10);
	var PacketDlAssign dl_tbf_ass;
	var RlcmacDlBlock dl_block;
	var uint32_t poll_fn;
	var uint32_t sched_fn;
	var GprsMS ms;
	timer T := 5.0;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Only 1 TRX with 8 PDCH */
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '11111111'B, 0);
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000000'B, (1 .. 7));

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);

	/* Establish an Uplink TBF, this way the PCU can send DL Assignment
	  through PDCH (no multiblock assignment possible through PCH) */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine */
	f_ms_tx_ul_data_block(ms, data, with_tlli := true, nr := f_ms_tx_TsTrxBtsNum(ms));
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, poll_fn, nr := f_ms_tx_TsTrxBtsNum(ms));

	/* SGSN sends some DL data, PCU will assign DL TBF through PACCH */
	var MultislotCap_GPRS_BSSGP mscap_gprs := {
		gprsmultislotclass := '10010'B, /* MS class 18, supports 8 DL and 8 UL */
		gprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV_BSSGP ms_racap := { valueof(ts_RaCapRec_BSSGP('0001'B /* E-GSM */, mscap_gprs, omit)) };
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data, ms_racap));
	dl_block := f_ms_rx_pkt_ass_pacch(ms, sched_fn, tr_RLCMAC_DL_PACKET_ASS, nr := f_ms_tx_TsTrxBtsNum(ms));
	if (f_dltbf_num_slots(ms.dl_tbf) != 8) {
		setverdict(fail, "Expected 8 PDCH slots allocated but got ", f_dltbf_num_slots(ms.dl_tbf));
		f_shutdown(__BFILE__, __LINE__);
	}
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn, nr := f_ms_tx_TsTrxBtsNum(ms));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_dl_multislot_tbf_ms_class_from_2phase() runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind := valueof(ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var PollFnCtx pollctx;
	var uint32_t sched_fn;
	var GprsMS ms;

	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '10010'B, /* MS class 18, supports 8 DL and 8 UL */
		gprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, omit)) };


	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Only 1 TRX with 8 PDCH */
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '11111111'B, 0);
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000000'B, (1 .. 7));

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);

	/* Send PACKET RESOURCE REQUEST to notify the MultiSlot Class */
	pollctx := f_ms_establish_ul_tbf_2phase_access(ms, ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, ms_racap));

	/* Pkt Uplink Assignment above sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), pollctx.fn, nr := pollctx.tstrxbts);

	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));
	dl_block := f_ms_rx_pkt_ass_pacch(ms, sched_fn, tr_RLCMAC_DL_PACKET_ASS, nr := f_ms_tx_TsTrxBtsNum(ms));
	if (f_dltbf_num_slots(ms.dl_tbf) != 8) {
		setverdict(fail, "Expected 8 PDCH slot allocated but got ", f_dltbf_num_slots(ms.dl_tbf));
		f_shutdown(__BFILE__, __LINE__);
	}
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn, nr := f_ms_tx_TsTrxBtsNum(ms));

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_ul_multislot_tbf_ms_class_from_2phase() runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind := valueof(ts_PCUIF_INFO_default);
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var PollFnCtx pollctx;
	var uint32_t sched_fn;
	var GprsMS ms;

	var MultislotCap_GPRS mscap_gprs := {
		gprsmultislotclass := '10010'B, /* MS class 18, supports 8 DL and 8 UL */
		gprsextendeddynalloccap := '0'B
	};
	var MSRadioAccessCapabilityV ms_racap := { valueof(ts_RaCapRec('0001'B /* E-GSM */, mscap_gprs, omit)) };


	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Only 1 TRX with 8 PDCH */
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '11111111'B, 0);
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000000'B, (1 .. 7));

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign(TLLI_UNUSED, ms.tlli);

	/* Send PACKET RESOURCE REQUEST to notify the MultiSlot Class */
	pollctx := f_ms_establish_ul_tbf_2phase_access(ms, ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, ms_racap));

	if (f_ultbf_num_slots(ms.ul_tbf) != 8) {
		setverdict(fail, "Expected 8 PDCH slot allocated but got ", f_ultbf_num_slots(ms.ul_tbf));
		f_shutdown(__BFILE__, __LINE__);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test scenario where MS wants to request a new TBF once the current one is
 * ending, by means of sending a Packet Resource Request on ul slot provided by
 * last Pkt Ul ACK's RRBP.
 * See 3GPP TS 44.060 sec 9.3.2.4.2 "Non-extended uplink TBF mode" */
testcase TC_ul_tbf_reestablish_with_pkt_resource_req() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var template RlcmacDlBlock acknack_tmpl;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send one UL block (with TLLI since we are in One-Phase Access
	   contention resoultion) and make sure it is ACKED fine */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true);

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	acknack_tmpl := tr_RLCMAC_UL_ACK_NACK_GPRS(ms.ul_tbf.tfi,
						   tr_UlAckNackGprs(ms.tlli,
								    tr_AckNackDescription(final_ack := '1'B),
								    tr_UlAckNackGprsAdditionsRel99(tbf_est := true)))
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn, acknack_tmpl);

	/* TODO: verify TBF_EST and FinalACK are both '1' above */

	/* Send PACKET RESOURCE REQUEST to request a new UL TBF */
	f_ms_tx_ul_block(ms, ts_RLC_UL_CTRL_ACK(ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, omit)), sched_fn);
	f_ms_rx_pkt_ass_pacch(ms, sched_fn, tr_RLCMAC_UL_PACKET_ASS);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* Send one UL block (without TLLI since we are in Second-Phase Access)
	   and make sure it is ACKED fine */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := false);  /* TODO: send using cs_mcs */

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn, acknack_tmpl);
	/* ACK the ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test CS paging over the BTS<->PCU socket.
 * When a (class B or C, not A) MS has an active TBF (or is on the PDCH), the MS can not react on CS paging over CCCH.
 * Paging should be send on the PACCH.
 *
 * 1. Send a Paging Request over PCU socket.
 * 2. Send a Ready-To-Send message over PCU socket
 * 3. Expect a Paging Frame
 */
testcase TC_paging_cs_from_bts() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var MobileIdentityLV mi;
	var octetstring mi_enc_lv;
	var hexstring imsi := f_gen_imsi(42);
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* build mobile Identity */
	mi := valueof(ts_MI_IMSI_LV(imsi));
	mi_enc_lv := enc_MobileIdentityLV(mi);
	/* Send paging request */
	BTS.send(ts_PCUIF_PAG_REQ(bts_nr := 0, id_lv := mi_enc_lv, chan_needed := 0,
				sapi :=PCU_IF_SAPI_PDTCH));

	/* Receive it on BTS side towards MS */
	f_rx_rlcmac_dl_block_exp_pkt_pag_req(dl_block);

	/* Make sure that Packet Paging Request contains the same IMSI */
	var PacketPagingReq req := dl_block.ctrl.payload.u.paging;
	if (not f_pkt_paging_match_imsi(req, imsi, ps_domain := false)) {
		setverdict(fail, "Failed to match IMSI ", imsi, " in ", req);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Test CS paging over Gb (SGSN->PCU->BTS[PDCH]).
 */
private function f_tc_paging_cs_from_sgsn(Nsvci bvci, boolean use_ptmsi := false)
runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var hexstring imsi := f_gen_imsi(42);
	var GsmTmsi tmsi;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send paging request with or without TMSI */
	if (use_ptmsi) {
		tmsi := oct2int(f_rnd_octstring(4)); /* Random P-TMSI */
		BSSGP[0].send(ts_BSSGP_CS_PAGING_PTMSI(bvci, imsi, tmsi));
	} else {
		BSSGP[0].send(ts_BSSGP_CS_PAGING_IMSI(bvci, imsi));
	}

	/* Receive it on BTS side towards MS */
	f_rx_rlcmac_dl_block_exp_pkt_pag_req(dl_block);

	/* Make sure that Packet Paging Request contains the same P-TMSI/IMSI */
	var PacketPagingReq req := dl_block.ctrl.payload.u.paging;
	if (use_ptmsi) {
		if (not f_pkt_paging_match_tmsi(req, tmsi, ps_domain := false)) {
			setverdict(fail, "Failed to match P-TMSI ", tmsi, " in ", req);
		}
	} else {
		if (not f_pkt_paging_match_imsi(req, imsi, ps_domain := false)) {
			setverdict(fail, "Failed to match IMSI ", imsi, " in ", req);
		}
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_paging_cs_from_sgsn_sign_ptmsi() runs on RAW_PCU_Test_CT {
	f_tc_paging_cs_from_sgsn(0, true);
}

testcase TC_paging_cs_from_sgsn_sign() runs on RAW_PCU_Test_CT {
	f_tc_paging_cs_from_sgsn(0);
}

testcase TC_paging_cs_from_sgsn_ptp() runs on RAW_PCU_Test_CT {
	f_tc_paging_cs_from_sgsn(mp_gb_cfg.bvc[0].bvci);
}

/* Test PS paging over Gb (SGSN->PCU->BTS[CCCH]).
 */
private function f_tc_paging_ps_from_sgsn(Nsvci bvci, boolean use_ptmsi := false)
runs on RAW_PCU_Test_CT {
	var integer imsi_suff_tx := 423;
	var hexstring imsi := f_gen_imsi(imsi_suff_tx);
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Send BSSGP PAGING-PS (with or without TMSI), wait for RR Paging Request Type 1.
	 * Make sure that both paging group (IMSI suffix) and Mobile Identity match. */
	if (use_ptmsi) {
		var OCT4 tmsi := f_rnd_octstring(4); /* Random P-TMSI */
		BSSGP[0].send(ts_BSSGP_PS_PAGING_PTMSI(bvci, imsi, oct2int(tmsi)));
		f_pcuif_rx_pch_pag_req1(t_MI_TMSI(tmsi), imsi_suff_tx);
	} else {
		BSSGP[0].send(ts_BSSGP_PS_PAGING_IMSI(bvci, imsi));
		f_pcuif_rx_pch_pag_req1(tr_MI_IMSI(imsi), imsi_suff_tx);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_paging_ps_from_sgsn_sign_ptmsi() runs on RAW_PCU_Test_CT {
	f_tc_paging_ps_from_sgsn(0, true);
}

testcase TC_paging_ps_from_sgsn_sign() runs on RAW_PCU_Test_CT {
	f_tc_paging_ps_from_sgsn(0);
}

testcase TC_paging_ps_from_sgsn_ptp() runs on RAW_PCU_Test_CT {
	f_tc_paging_ps_from_sgsn(mp_gb_cfg.bvc[0].bvci);
}

/* Verify osmo-pcu handles DL UNIT_DATA from SGSN with IMSI IE correctly. See OS#4729 */
testcase TC_bssgp_dl_unitdata_with_valid_imsi() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	f_statsd_reset();

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Fake GMM GPRS Attach or similar, PCU doesn't care about upper layers here */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true);
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	/* Now SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data, imsi := ts_BSSGP_IMSI(ms.imsi)));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	/* Wait timer X2002 and DL block is available after CCCH IMM ASS: */
	f_sleep(X2002);
	f_rx_rlcmac_dl_block_exp_data(dl_block, dl_fn, data, 0);

	/* ACK the DL block */
	f_acknackdesc_ack_block(ms.dl_tbf.acknack_desc, dl_block, '1'B);
	f_ms_tx_ul_block(ms, ts_RLCMAC_DL_ACK_NACK(ms.dl_tbf.tfi, ms.dl_tbf.acknack_desc),
			     f_dl_block_ack_fn(dl_block, dl_fn));

	var StatsDExpects expect := {
		{ name := "TTCN3.bts.0.rach.requests", mtype := "c", min := 1, max := 1},
		{ name := "TTCN3.bts.0.immediate.assignment_DL", mtype := "c", min := 1, max := 1},
		{ name := "TTCN3.bts.0.tbf.dl.alloc", mtype := "c", min := 1, max := 1},
		{ name := "TTCN3.bts.0.tbf.ul.alloc", mtype := "c", min := 1, max := 1},
		{ name := "TTCN3.bts.0.rlc.dl_payload_bytes", mtype := "c", min := 28, max := 28},
		{ name := "TTCN3.bts.0.rlc.ul_payload_bytes", mtype := "c", min := 26, max := 26}
	};
	f_statsd_expect(expect);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify osmo-pcu acts on incorrect IMSI IE content in DL UNIT_DATA from SGSN. See OS#4729 */
testcase TC_bssgp_dl_unitdata_with_invalid_imsi() runs on RAW_PCU_Test_CT {
	var RlcmacDlBlock dl_block;
	var octetstring data := f_rnd_octstring(10);
	var uint32_t sched_fn;
	var uint32_t dl_fn;
	var GprsMS ms;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms();
	ms := g_ms[0]; /* We only use first MS in this test */

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Fake GMM GPRS Attach or similar, PCU doesn't care about upper layers here */
	f_ms_tx_ul_data_block_multi(ms, 1, with_tlli := true);
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, sched_fn);
	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), sched_fn);

	/* UL block should be received in SGSN */
	BSSGP[0].receive(tr_BSSGP_UL_UD(ms.tlli, mp_gb_cfg.bvc[0].cell_id));

	/* Now SGSN sends some DL data with an invalid IMSI */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data, imsi := ts_BSSGP_IMSI('1122'H)));

	BSSGP_SIG[0].receive(tr_BSSGP_STATUS(omit, BSSGP_CAUSE_CONDITIONAL_IE_ERROR, ?));

	/* TODO: make sure no data is sent over PCU -> MS */

	f_shutdown(__BFILE__, __LINE__, final := true);
}

private function f_TC_egprs_pkt_chan_req(in EGPRSPktChRequest req,
					 template GsmRrMessage t_imm_ass := ?,
					 PCUIF_BurstType bt := BURST_TYPE_1)
runs on RAW_PCU_Test_CT {
	var GsmRrMessage rr_msg;
	var uint16_t ra11;

	ra11 := enc_EGPRSPktChRequest2uint(req);
	log("Sending EGPRS Packet Channel Request (", ra11, "): ", req);

	rr_msg := f_pcuif_tx_rach_rx_imm_ass(ra := ra11, is_11bit := 1, burst_type := bt);
	if (not match(rr_msg, t_imm_ass)) {
		setverdict(fail, "Immediate Assignment does not match");
		f_shutdown(__BFILE__, __LINE__);
	}

	setverdict(pass);
}

testcase TC_egprs_pkt_chan_req_signalling() runs on RAW_PCU_Test_CT {
	var template GsmRrMessage imm_ass;
	var template IaRestOctets rest;
	var template EgprsUlAss ul_ass;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	var EGPRSPktChRequest req := {
		/* NOTE: other fields are set in the loop */
		signalling := { tag := '110011'B }
	};

	for (var integer i := 0; i < 6; i := i + 1) {
		var BIT5 ext_ra := int2bit(f_rnd_int(32), 5);
		req.signalling.random_bits := ext_ra;

		/* For signalling, do we expect Multiblock UL TBF Assignment? */
		ul_ass  := tr_EgprsUlAssMultiblock(ext_ra := ext_ra);
		rest    := tr_IaRestOctets_EGPRSULAss(ul_ass);
		imm_ass := tr_IMM_TBF_ASS(dl := false, rest := rest);

		f_TC_egprs_pkt_chan_req(req, imm_ass);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_egprs_pkt_chan_req_one_phase() runs on RAW_PCU_Test_CT {
	var template GsmRrMessage imm_ass;
	var template IaRestOctets rest;
	var template EgprsUlAss ul_ass;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	var EGPRSPktChRequest req := {
		/* NOTE: other fields are set in the loop */
		one_phase := { tag := '0'B }
	};

	for (var integer i := 0; i < 6; i := i + 1) {
		var BIT5 ext_ra := int2bit(f_rnd_int(32), 5);
		var BIT5 mslot_class := int2bit(f_rnd_int(32), 5);
		var BIT2 priority := substr(ext_ra, 0, 2);
		var BIT3 rand := substr(ext_ra, 2, 3);

		req.one_phase.multislot_class := mslot_class;
		req.one_phase.priority := priority;
		req.one_phase.random_bits := rand;

		/* For one phase access, do we expect Dynamic UL TBF Assignment? */
		ul_ass  := tr_EgprsUlAssDynamic(ext_ra := ext_ra);
		rest    := tr_IaRestOctets_EGPRSULAss(ul_ass);
		imm_ass := tr_IMM_TBF_ASS(dl := false, rest := rest);

		f_TC_egprs_pkt_chan_req(req, imm_ass);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_egprs_pkt_chan_req_two_phase() runs on RAW_PCU_Test_CT {
	var template GsmRrMessage imm_ass;
	var template IaRestOctets rest;
	var template EgprsUlAss ul_ass;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	var EGPRSPktChRequest req := {
		/* NOTE: other fields are set in the loop */
		two_phase := { tag := '110000'B }
	};

	for (var integer i := 0; i < 6; i := i + 1) {
		var BIT5 ext_ra := int2bit(f_rnd_int(32), 5);
		var BIT2 priority := substr(ext_ra, 0, 2);
		var BIT3 rand := substr(ext_ra, 2, 3);

		req.two_phase.priority := priority;
		req.two_phase.random_bits := rand;

		/* For two phase access, do we expect Multiblock UL TBF Assignment? */
		ul_ass  := tr_EgprsUlAssMultiblock(ext_ra := ext_ra);
		rest    := tr_IaRestOctets_EGPRSULAss(ul_ass);
		imm_ass := tr_IMM_TBF_ASS(dl := false, rest := rest);

		f_TC_egprs_pkt_chan_req(req, imm_ass);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

private function f_TC_egprs_pkt_chan_req_reject(bitstring ra11, uint32_t fn,
						template IARRestOctets rest := ?,
						PCUIF_BurstType bt := BURST_TYPE_1)
runs on RAW_PCU_Test_CT {
	var template ReqRefWaitInd tr_ref;
	var GsmRrMessage rr_msg;

	/* Send RACH.ind with malformed EGPRS Packet Channel Request */
	BTS.send(ts_PCUIF_RACH_IND(bts_nr := 0, trx_nr := 0, ts_nr := 0,
				   ra := bit2int(ra11), is_11bit := 1,
				   burst_type := bt, fn := fn,
				   arfcn := 871));

	/* Abuse f_pcuif_rx_imm_ass(): wait for Immediate Assignment Reject */
	rr_msg := f_pcuif_rx_imm_ass(t_imm_ass := tr_IMM_ASS_REJ);

	/* Just to have a short-name reference to the actual message */
	var ImmediateAssignmentReject iar := rr_msg.payload.imm_ass_rej;

	/* Make sure that Request Reference list contains at least one entry
	 * with our TDMA frame number, and RA is set to 'reserved' value 127. */
	tr_ref := tr_ReqRefWaitInd(f_compute_ReqRef(127, fn));
	if (not match(iar.payload, { *, tr_ref, * })) {
		setverdict(fail, "Request Reference list does not match");
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Match Feature Indicator (must indicate PS domain) */
	if (not match(iar.feature_ind, FeatureIndicator:{?, false, true})) {
		setverdict(fail, "Feature Indicator does not match");
		f_shutdown(__BFILE__, __LINE__);
	}

	/* Match IAR Rest Octets */
	if (not match(iar.rest_octets, rest)) {
		setverdict(fail, "IAR Rest Octets does not match: ",
			   iar.rest_octets, " vs expected ", rest);
		f_shutdown(__BFILE__, __LINE__);
	}

	setverdict(pass);
}

/* Verify the contents of RR Immediate Assignment Reject message and its
 * Rest Octets sent in response to EGPRS Packet Channel Request (11 bit). */
testcase TC_egprs_pkt_chan_req_reject_content() runs on RAW_PCU_Test_CT {
	var template IARRestOctets rest;
	var BIT5 ext_ra;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	for (var integer i := 0; i < 6; i := i + 1) {
		ext_ra := int2bit(f_rnd_int(32), 5); /* 5 LSB's of RA11 */
		rest := tr_IARRestOctets({ *, tr_ExtRAOpt(ext_ra), * });

		/* Intentionally incorrect message (see table 11.2.5a.2) */
		f_TC_egprs_pkt_chan_req_reject('111111'B & ext_ra, 1337 + i, rest);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* At the moment, the IUT does not support any emergency services. Make sure
 * that EGPRS Packet Channel Request for an emergency call is properly rejected. */
testcase TC_egprs_pkt_chan_req_reject_emergency() runs on RAW_PCU_Test_CT {
	var template IARRestOctets rest;
	var BIT5 ext_ra;
	var BIT11 ra11;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename());

	var EGPRSPktChRequest req := {
		/* NOTE: other fields are set in the loop */
		emergency := { tag := '110111'B }
	};

	for (var integer i := 0; i < 6; i := i + 1) {
		ext_ra := int2bit(f_rnd_int(32), 5); /* 5 LSB's of RA11 */
		rest := tr_IARRestOctets({ *, tr_ExtRAOpt(ext_ra), * });

		req.emergency.random_bits := ext_ra;
		ra11 := enc_EGPRSPktChRequest2bits(req);

		/* Intentionally incorrect message (see table 11.2.5a.2) */
		f_TC_egprs_pkt_chan_req_reject(ra11, 1337 + i, rest);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Make sure that IUT responds with RR Immediate Assignment Reject due to exhaustion. */
testcase TC_egprs_pkt_chan_req_reject_exhaustion() runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind;
	var template IARRestOctets rest;
	var BIT11 ra11;

	info_ind := valueof(ts_PCUIF_INFO_default);

	/* Only the first TRX is enabled. */
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000000'B, (1 .. 7));
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000001'B, 0);

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	var EGPRSPktChRequest req := {
		one_phase := {
			tag := '0'B,
			multislot_class := '10101'B,
			priority := '01'B,
			random_bits := '101'B
		}
	};

	/* We send 7 requests, the IUT gives us all available USFs (0..6).
	 * TODO: make it configurable: usf_max := mp_pdch_ts_num * 7. */
	for (var integer i := 0; i < 7; i := i + 1) {
		req.one_phase.random_bits := int2bit(f_rnd_int(8), 3);
		f_TC_egprs_pkt_chan_req(req, tr_IMM_TBF_ASS);
	}

	ra11 := enc_EGPRSPktChRequest2bits(req);
	rest := tr_IARRestOctets({ *, tr_ExtRAOpt(substr(ra11, 6, 5)), * });

	/* At this point, the IUT should run out of free USFs */
	f_TC_egprs_pkt_chan_req_reject(ra11, 1870, rest);

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Randomly generate a set of hopping parameters for one timeslot */
private function f_TC_pcuif_fh_params_gen(integer max_ma_len)
return template (value) PCUIF_InfoTrxTs {
	/* Pick a random MA length in range 2 .. max_ma_len */
	var integer ma_len := 2 + f_rnd_int(max_ma_len - 2);

	return ts_PCUIF_InfoTrxTsH1(tsc := f_rnd_int(7),
				    hsn := f_rnd_int(63),
				    maio := f_rnd_int(63),
				    ma := f_rnd_bitstring(ma_len));
}

private function f_TC_pcuif_fh_check_imm_ass(in PCUIF_info_ind info_ind,
					     in GsmRrMessage rr_msg)
{
	var ImmediateAssignment ia := rr_msg.payload.imm_ass;
	var PCUIF_InfoTrxTs ts := info_ind.trx.v10[0].ts[ia.pkt_chan_desc.tn];

	var template PacketChannelDescription tr_pkt_chan_desc := {
		channel_Type_spare := ?,
		tn := ?,
		tsc := ts.tsc,
		presence := '1'B,
		zero := omit,
		one := {
			maio := ts.maio,
			hsn := ts.hsn
		}
	};

	if (not match(ia.pkt_chan_desc, tr_pkt_chan_desc)) {
		setverdict(fail, "Packet Channel Description does not match: ",
			   ia.pkt_chan_desc, " vs ", tr_pkt_chan_desc);
	}

	/* Mobile Allocation is expected to be octet-aligned */
	var uint8_t ma_oct_len := (ts.ma_bit_len + 8 - 1) / 8;
	var template MobileAllocationLV tr_ma := {
		len := ma_oct_len, /* in bytes */
		ma := substr(ts.ma, 0, ma_oct_len * 8)
	};

	if (not match(ia.mobile_allocation, tr_ma)) {
		setverdict(fail, "Mobile Allocation does not match: ",
			   ia.mobile_allocation, " vs ", tr_ma);
	}

	setverdict(pass);
}

/* Make sure that Immediate (UL EGPRS TBF) Assignment contains hopping parameters */
testcase TC_pcuif_fh_imm_ass_ul_egprs() runs on RAW_PCU_Test_CT {
	var template PCUIF_info_ind info_ind := ts_PCUIF_INFO_default;
	var GprsMS ms := valueof(t_GprsMS_def);

	/* Enable frequency hopping on TRX0/TS7 */
	info_ind.trx.v10[0].ts[7] := f_TC_pcuif_fh_params_gen(32);

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* EGPRS Packet Channel Request (cause=Signalling) */
	f_ms_use_ra(ms, bit2int('11001101010'B), ra_is_11bit := 1);

	/* Establish an Uplink EGPRS TBF */
	f_ms_establish_ul_tbf(ms);

	f_TC_pcuif_fh_check_imm_ass(valueof(info_ind), ms.ul_tbf.rr_imm_ass);
	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Make sure that Immediate (UL TBF) Assignment contains hopping parameters */
testcase TC_pcuif_fh_imm_ass_ul() runs on RAW_PCU_Test_CT {
	var template PCUIF_info_ind info_ind := ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS);
	var GprsMS ms := valueof(t_GprsMS_def);

	/* Enable frequency hopping on TRX0/TS7 */
	info_ind.trx.v10[0].ts[7] := f_TC_pcuif_fh_params_gen(32);

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	f_TC_pcuif_fh_check_imm_ass(valueof(info_ind), ms.ul_tbf.rr_imm_ass);
	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Make sure that Immediate (DL TBF) Assignment contains hopping parameters */
testcase TC_pcuif_fh_imm_ass_dl() runs on RAW_PCU_Test_CT {
	var template PCUIF_info_ind info_ind := ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS)
	var GprsMS ms := valueof(t_GprsMS_def);

	/* Enable frequency hopping on TRX0/TS7 */
	info_ind.trx.v10[0].ts[7] := f_TC_pcuif_fh_params_gen(16);

	/* Initialize NS/BSSGP side */
	f_init_bssgp();

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* SGSN sends some DL data, PCU will page on CCCH (PCH) */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, f_rnd_octstring(12)));
	f_ms_exp_dl_tbf_ass_ccch(ms, PCU_IF_SAPI_PCH);

	f_TC_pcuif_fh_check_imm_ass(valueof(info_ind), ms.dl_tbf.rr_imm_ass);
	f_shutdown(__BFILE__, __LINE__, final := true);
}

private function f_TC_pcuif_fh_check_pkt_ass(in PCUIF_info_ind info_ind,
					     in FrequencyParameters fp)
{
	/* FIXME: TRX0/TS7 is a hard-coded expectation, make it configurable */
	var PCUIF_InfoTrxTs ts := info_ind.trx.v10[0].ts[7];

	/* Table 12.8.1: Frequency Parameters information elements */
	var template FrequencyParameters tr_fp := {
		tsc := ts.tsc,
		presence := '10'B, /* Direct encoding 1 */
		arfcn := omit,
		indirect := omit,
		direct1 := {
			maio := ts.maio,
			/* Table 12.10a.1: GPRS Mobile Allocation information elements */
			mobile_allocation := {
				hsn := ts.hsn,
				rfl_number_list_present := '0'B,
				rfl_number_list := omit,
				ma_present := '0'B, /* inverted logic */
				ma_length := ts.ma_bit_len,
				ma_bitmap := substr(ts.ma, 0, ts.ma_bit_len)
			}
		},
		direct2 := omit
	};

	if (not match(fp, tr_fp)) {
		setverdict(fail, "Frequency Parameters IE does not match: ",
			   fp, " vs ", tr_fp);
	}

	setverdict(pass);
}

/* Make sure that Packet Uplink Assignment contains hopping parameters */
testcase TC_pcuif_fh_pkt_ass_ul() runs on RAW_PCU_Test_CT {
	var template PCUIF_info_ind info_ind := ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS);
	var GprsMS ms := valueof(t_GprsMS_def);
	var uint32_t poll_fn;

	/* Enable frequency hopping on TRX0/TS7 */
	info_ind.trx.v10[0].ts[7] := f_TC_pcuif_fh_params_gen(33);

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send Packet Resource Request, so the network will allocate an Uplink resource */
	f_ms_tx_ul_block(ms, ts_RLC_UL_CTRL_ACK(ts_RlcMacUlCtrl_PKT_RES_REQ(ms.tlli, omit)));

	/* Expect an RLC/MAC block with Packet Uplink Assignment on PACCH (see 11.2.29) */
	var RlcmacDlBlock blk := f_ms_rx_pkt_ass_pacch(ms, poll_fn, tr_RLCMAC_UL_PACKET_ASS);
	var PacketUlAssignment ua := blk.ctrl.payload.u.ul_assignment;

	/* 3GPP TS 44.060, section 12.8 "Frequency Parameters" */
	var template (omit) FrequencyParameters fp;
	if (ua.is_egprs == '1'B) {
		fp := ua.egprs.freq_par;
	} else {
		fp := ua.gprs.freq_par;
	}

	/* This is an optional IE, so it's worth to check its presence */
	if (istemplatekind(fp, "omit")) {
		setverdict(fail, "Frequency Parameters IE is not present");
		f_shutdown(__BFILE__, __LINE__);
	}

	f_TC_pcuif_fh_check_pkt_ass(valueof(info_ind), valueof(fp));
	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Make sure that Packet Downlink Assignment contains hopping parameters */
testcase TC_pcuif_fh_pkt_ass_dl() runs on RAW_PCU_Test_CT {
	var template PCUIF_info_ind info_ind := ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS);
	var octetstring data := f_rnd_octstring(10);
	var GprsMS ms := valueof(t_GprsMS_def);
	var RlcmacDlBlock dl_block;
	var uint32_t poll_fn;

	/* Enable frequency hopping on TRX0/TS7 */
	info_ind.trx.v10[0].ts[7] := f_TC_pcuif_fh_params_gen(33);

	/* Initialize NS/BSSGP side */
	f_init_bssgp();

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_bssgp_client_llgmm_assign('FFFFFFFF'O, ms.tlli);

	/* Establish an Uplink TBF */
	f_ms_establish_ul_tbf(ms);

	/* Send an Uplink block, so this TBF becomes "active" */
	f_ms_tx_ul_data_block(ms, data, with_tlli := true);

	/* DL ACK/NACK sets poll+rrbp requesting PACKET CONTROL ACK */
	f_rx_rlcmac_dl_block_exp_ack_nack(dl_block, poll_fn);
	f_ms_tx_ul_block(ms, ts_RLCMAC_CTRL_ACK(ms.tlli), poll_fn);

	/* SGSN sends some DL data, PCU will assign Downlink resource on PACCH */
	BSSGP[0].send(ts_BSSGP_DL_UD(ms.tlli, data));

	/* Expect an RLC/MAC block with Packet Downlink Assignment on PACCH (see 11.2.29) */
	dl_block := f_ms_rx_pkt_ass_pacch(ms, poll_fn, tr_RLCMAC_DL_PACKET_ASS);
	var PacketDlAssignment da := dl_block.ctrl.payload.u.dl_assignment;

	/* This is an optional IE, so it's worth to check its presence */
	if (not ispresent(da.freq_par)) {
		setverdict(fail, "Frequency Parameters IE is not present");
		f_shutdown(__BFILE__, __LINE__);
	}

	f_TC_pcuif_fh_check_pkt_ass(valueof(info_ind), da.freq_par);
	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Check if the IUT handles subsequent INFO.ind messages */
testcase TC_pcuif_info_ind_subsequent() runs on RAW_PCU_Test_CT {
	var template PCUIF_info_ind info_ind := ts_PCUIF_INFO_default;
	var PCUIF_Message pcu_msg;

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Send 16 conseqtive INFO.ind messages and check that the IUT stays alive */
	for (var integer i := 0; i < 16; i := i + 1) {
		BTS.send(ts_PCUIF_INFO_IND(0, info_ind));
		f_pcuif_rx_data_req(pcu_msg);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

/* Verify allocation of several MS along PDCH ts of several TRX. See OS#1775, SYS#5030 */
testcase TC_multitrx_multims_alloc() runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind;
	var integer i;
	const integer num_ms := 8;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();
	/* Initialize GPRS MS side */
	f_init_gprs_ms(num_ms);

	info_ind := valueof(ts_PCUIF_INFO_default(c_PCUIF_Flags_noMCS));
	/* Only the 3 first TRX are enabled. The enabled ones all have same
	   amount of resources, hence same amount of initial resources. */
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000000'B, (3 .. 7));
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00000011'B, 0);
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '00001100'B, 1);
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, '11000000'B, 2);

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_multi_ms_bssgp_register();

	/* Establish an Uplink TBF for each GprsMS instance */
	f_multi_ms_establish_tbf(do_activate := false);

	/* Check if all TBFs are allocated on different TRX in an uniform way */
	for (i := 0; i < num_ms; i := i + 1) {
		if (g_ms[i].ul_tbf.arfcn != info_ind.trx.v10[i mod 3].arfcn) {
			setverdict(fail, "Got assigned ARFCN ", g_ms[i].ul_tbf.arfcn,
				   " vs exp ", info_ind.trx.v10[i mod 3].arfcn);
			f_shutdown(__BFILE__, __LINE__);
		}
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

private function f_TC_paging_cs_multi_ms(template (value) TsTrxBtsNum nr,
					 boolean exp_imsi, boolean exp_tmsi)
runs on RAW_PCU_Test_CT {
	var bitstring mask := f_pad_bit(''B, lengthof(g_ms), '0'B);
	var integer pending := lengthof(g_ms);
	var RlcmacDlBlock dl_block;
	var boolean f1, f2;

	while (pending > 0) {
		var uint32_t poll_fn;

		/* Obtain a Downlink block and make sure it is a paging request */
		f_rx_rlcmac_dl_block(dl_block, poll_fn, nr := nr);
		if (not match(dl_block, tr_RLCMAC_PACKET_PAG_REQ)) {
			setverdict(fail, "Rx unexpected DL block: ", dl_block);
			break;
		}

		/* This should not happen in general, but who knows... */
		var PacketPagingReq req := dl_block.ctrl.payload.u.paging;
		if (not ispresent(req.repeated_pageinfo)) {
			setverdict(fail, "Repeated Page Info IE is absent?!?");
			break;
		}

		/* A single message may contain several MIs depending on their type */
		for (var integer i := 0; i < lengthof(g_ms); i := i + 1) {
			f1 := exp_imsi and f_pkt_paging_match_imsi(req, g_ms[i].imsi,
								   ps_domain := false);
			f2 := exp_tmsi and f_pkt_paging_match_tmsi(req, oct2int(g_ms[i].tlli),
								   ps_domain := false);
			if (not f1 and not f2)
				{ continue; }

			/* Detect duplicate MIs */
			if (mask[i] == '1'B) {
				setverdict(fail, "MS is paged twice: ", g_ms[i].imsi);
				continue;
			}

			mask[i] := '1'B;
		}

		pending := pending - lengthof(req.repeated_pageinfo);
	}

	for (var integer i := 0; i < lengthof(mask); i := i + 1) {
		if (mask[i] != '1'B) {
			setverdict(fail, "MS was not paged at all: ", g_ms[i].imsi);
			log("===== mask := ", mask);
		}
	}

	/* All messages must have been received by now, expect a dummy block */
	f_rx_rlcmac_dl_block_exp_dummy(dl_block, nr := nr);
}

private function f_TC_paging_cs_multi_ms_init(BIT8 pdch_mask)
runs on RAW_PCU_Test_CT {
	var PCUIF_info_ind info_ind := valueof(ts_PCUIF_INFO_default);
	const BssgpBvci bvci := mp_gb_cfg.bvc[0].bvci;

	/* Initialize NS/BSSGP side */
	f_init_bssgp();

	/* Explicitly set the given PDCH slot-mask to all transceivers */
	f_PCUIF_ver_INFO_PDCHMask_set(info_ind, pdch_mask);

	/* Allocate 56 GprsMS instances (maximum for 8 PDCH slots) */
	f_init_gprs_ms(7 * 8);

	/* Initialize the PCU interface abstraction */
	f_init_raw(testcasename(), info_ind);

	/* Establish BSSGP connection to the PCU */
	f_bssgp_establish();
	f_multi_ms_bssgp_register();

	/* Establish an Uplink TBF for each GprsMS instance */
	f_multi_ms_establish_tbf(do_activate := true);
}

testcase TC_paging_cs_multi_ms_imsi() runs on RAW_PCU_Test_CT {
	const BssgpBvci bvci := mp_gb_cfg.bvc[0].bvci;

	/* Common part: send INFO.ind, establish TBFs... */
	f_TC_paging_cs_multi_ms_init(pdch_mask := '00000001'B);

	/* Enqueue multiple CS PAGING requests at a time (IMSI only) */
	for (var integer i := 0; i < lengthof(g_ms); i := i + 1) {
		BSSGP[0].send(ts_BSSGP_CS_PAGING_IMSI(bvci, g_ms[i].imsi));
	}

	/* FIXME: work around a race condition between PCUIF and BSSGP */
	f_sleep(0.2); /* i.e. give the IUT some time to process everything */

	/* Check what the IUT sends on PACCH, all GprsMS instances must be paged.
	 * The IUT is expected to page on all PDCH slots of all transceivers. */
	for (var integer trx_nr := 0; trx_nr < 8; trx_nr := trx_nr + 1) {
		var template (value) TsTrxBtsNum nr := ts_TsTrxBtsNum(7, trx_nr);
		f_TC_paging_cs_multi_ms(nr, exp_imsi := true, exp_tmsi := false);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_paging_cs_multi_ms_tmsi() runs on RAW_PCU_Test_CT {
	const BssgpBvci bvci := mp_gb_cfg.bvc[0].bvci;

	/* Common part: send INFO.ind, establish TBFs... */
	f_TC_paging_cs_multi_ms_init(pdch_mask := '00000001'B);

	/* Enqueue multiple CS PAGING requests at a time (P-TMSI only) */
	for (var integer i := 0; i < lengthof(g_ms); i := i + 1) {
		var GsmTmsi tmsi := oct2int(g_ms[i].tlli); /* P-TMSI == TLLI */
		BSSGP[0].send(ts_BSSGP_CS_PAGING_PTMSI(bvci, g_ms[i].imsi, tmsi));
	}

	/* FIXME: work around a race condition between PCUIF and BSSGP */
	f_sleep(0.2); /* i.e. give the IUT some time to process everything */

	/* Check what the IUT sends on PACCH, all GprsMS instances must be paged.
	 * The IUT is expected to page on all PDCH slots of all transceivers. */
	for (var integer trx_nr := 0; trx_nr < 8; trx_nr := trx_nr + 1) {
		var template (value) TsTrxBtsNum nr := ts_TsTrxBtsNum(7, trx_nr);
		f_TC_paging_cs_multi_ms(nr, exp_imsi := false, exp_tmsi := true);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

testcase TC_paging_cs_multi_ms_imsi_tmsi() runs on RAW_PCU_Test_CT {
	const BssgpBvci bvci := mp_gb_cfg.bvc[0].bvci;

	/* Common part: send INFO.ind, establish TBFs... */
	f_TC_paging_cs_multi_ms_init(pdch_mask := '00000001'B);

	/* Enqueue multiple CS PAGING requests at a time (IMSI & P-TMSI) */
	for (var integer i := 0; i < lengthof(g_ms); i := i + 1) {
		var GsmTmsi tmsi := oct2int(g_ms[i].tlli); /* P-TMSI == TLLI */
		if (i mod 3 == 0) { /* One PDU fits: 1 IMSI and 2 P-TMSI MIs */
			BSSGP[0].send(ts_BSSGP_CS_PAGING_PTMSI(bvci, g_ms[i].imsi, tmsi));
		} else {
			BSSGP[0].send(ts_BSSGP_CS_PAGING_IMSI(bvci, g_ms[i].imsi));
		}
	}

	/* FIXME: work around a race condition between PCUIF and BSSGP */
	f_sleep(0.2); /* i.e. give the IUT some time to process everything */

	/* Check what the IUT sends on PACCH, all GprsMS instances must be paged.
	 * The IUT is expected to page on all PDCH slots of all transceivers. */
	for (var integer trx_nr := 0; trx_nr < 8; trx_nr := trx_nr + 1) {
		var template (value) TsTrxBtsNum nr := ts_TsTrxBtsNum(7, trx_nr);
		f_TC_paging_cs_multi_ms(nr, exp_imsi := true, exp_tmsi := true);
	}

	f_shutdown(__BFILE__, __LINE__, final := true);
}

control {
	execute( TC_pcuif_suspend() );
	execute( TC_ta_ptcch_idle() );
	execute( TC_ta_rach_imm_ass() );
	execute( TC_ta_idle_dl_tbf_ass() );
	execute( TC_ta_ptcch_ul_multi_tbf() );
	execute( TC_cs_lqual_ul_tbf() );
	execute( TC_cs_initial_ul() );
	execute( TC_cs_max_ul() );
	execute( TC_cs_initial_dl() );
	execute( TC_cs_max_dl() );
	execute( TC_dl_cs1_to_cs4() );
	execute( TC_mcs_initial_ul() );
	execute( TC_mcs_max_ul() );
	execute( TC_mcs_initial_dl() );
	execute( TC_t3169() );
	execute( TC_t3193() );
	execute( TC_countdown_procedure() );
	execute( TC_ul_all_sizes() );
	execute( TC_ul_data_toolong_fills_padding() );
	execute( TC_mo_ping_pong() );
	execute( TC_mo_ping_pong_with_ul_racap() );
	execute( TC_force_two_phase_access() );
	execute( TC_mt_ping_pong() );
	execute( TC_mt_ping_pong_with_dl_racap() );
	execute( TC_ul_intermediate_retrans() );
	execute( TC_imm_ass_dl_block_retrans() );
	execute( TC_dl_flow_more_blocks() );
	execute( TC_ul_flow_multiple_llc_blocks() );
	execute( TC_paging_cs_from_bts() );
	execute( TC_paging_cs_from_sgsn_sign_ptmsi() );
	execute( TC_paging_cs_from_sgsn_sign() );
	execute( TC_paging_cs_from_sgsn_ptp() );
	execute( TC_paging_ps_from_sgsn_sign_ptmsi() );
	execute( TC_paging_ps_from_sgsn_sign() );
	execute( TC_paging_ps_from_sgsn_ptp() );
	execute( TC_paging_cs_multi_ms_imsi_tmsi() );
	execute( TC_paging_cs_multi_ms_imsi() );
	execute( TC_paging_cs_multi_ms_tmsi() );
	execute( TC_bssgp_dl_unitdata_with_valid_imsi() );
	execute( TC_bssgp_dl_unitdata_with_invalid_imsi() );

	/* EGPRS specific test cases */
	execute( TC_egprs_pkt_chan_req_signalling() );
	execute( TC_egprs_pkt_chan_req_one_phase() );
	execute( TC_egprs_pkt_chan_req_two_phase() );
	execute( TC_egprs_pkt_chan_req_reject_content() );
	execute( TC_egprs_pkt_chan_req_reject_emergency() );
	execute( TC_egprs_pkt_chan_req_reject_exhaustion() );

	execute( TC_mo_ping_pong_with_ul_racap_egprs_only() );

	/* Frequency hopping specific test cases */
	if (PCUIF_Types.mp_pcuif_version >= 10) {
		/* Immediate Assignment on AGCH/PCH */
		execute( TC_pcuif_fh_imm_ass_ul_egprs() );
		execute( TC_pcuif_fh_imm_ass_ul() );
		execute( TC_pcuif_fh_imm_ass_dl() );
		/* Packet Uplink/Downlink Assignment on PACCH */
		execute( TC_pcuif_fh_pkt_ass_ul() );
		execute( TC_pcuif_fh_pkt_ass_dl() );
		execute( TC_multitrx_multims_alloc() );
		execute( TC_dl_multislot_tbf_ms_class_from_sgsn() );
		execute( TC_dl_multislot_tbf_ms_class_from_2phase() );
		execute( TC_ul_multislot_tbf_ms_class_from_2phase() );
	}

	execute( TC_pcuif_info_ind_subsequent() );
}

}