aboutsummaryrefslogtreecommitdiffstats
path: root/bsc/BSC_Tests_CBSP.ttcn
blob: 3f1eff026b8c39ac758b9b6a511ac178670f0e3a (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
module BSC_Tests_CBSP {

/* CBSP Integration Tests for OsmoBSC
 * (C) 2019 by Harald Welte <laforge@gnumonks.org>
 * 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
 *
 * This test suite tests OsmoBSC while emulating the CBC (Cell Broadcast Centre)
 */

import from BSC_Tests all;

import from General_Types all;
import from Osmocom_Types all;
import from GSM_Types all;
import from IPL4asp_Types all;
import from BSSAP_Types all;
import from BSSMAP_Templates all;

import from IPA_Emulation all;
import from IPA_CodecPort all;
import from IPA_Types all;

import from MobileL3_Types all;
import from MobileL3_RRM_Types all;
import from L3_Templates all;

import from RSL_Types all;
import from RSL_Emulation all;

import from CBSP_Types all;
import from CBSP_Templates all;
import from CBSP_Adapter all;
import from CBSP_CodecPort all;

import from Osmocom_VTY_Functions all;
import from TELNETasp_PortType all;

import from Osmocom_CTRL_Functions all;
import from Osmocom_CTRL_Types all;
import from Osmocom_CTRL_Adapter all;

modulepar {
	charstring mp_cbc_ip := "127.0.0.1";
	charstring mp_cbc_ip6 := "::1";
	integer mp_cbc_port := 48049;
	charstring mp_bsc_cbsp_ip := "127.0.0.1";
	charstring mp_bsc_cbsp_ip6 := "::1";
	integer mp_bsc_cbsp_port := 48050;
	/* port number to which to establish the IPA CTRL connection */
	integer mp_bsc_ctrl_port := 4249;

	/* BTS 0: 001-01-1-0 with CBCH
	 * BTS 1: 001-01-1-1 with CBCH
	 * BTS 2: 001-01-2-1 with CBCH
	 * BTS 3: 001-01-2-3 without CBCH */
	GsmCgiAbstract mp_cgi_bts0 := { '001'H, '01'H, 1, 0 };
	GsmCgiAbstract mp_cgi_bts1 := { '001'H, '01'H, 1, 1 };
	GsmCgiAbstract mp_cgi_bts2 := { '001'H, '01'H, 2, 1 };
	GsmCgiAbstract mp_cgi_bts3 := { '001'H, '01'H, 2, 3 };
}

type record CBSP_Pars {
	boolean tcp_client,
	charstring local_ip,
	integer local_port,
	charstring remote_ip,
	integer remote_port,
	uint16_t cbsp_msg_id optional, /*server mode only */
	uint16_t cbsp_ser_no optional  /*server mode only */
};

private template (value) CBSP_Pars ts_CBSP_Pars_default(boolean tcp_client := true,
							template (omit) uint16_t cbsp_msg_id := omit,
							template (omit) uint16_t cbsp_ser_no := omit) := {
	tcp_client := tcp_client,
	local_ip := mp_cbc_ip,
	local_port := mp_cbc_port,
	remote_ip := mp_bsc_cbsp_ip,
	remote_port := mp_bsc_cbsp_port,
	cbsp_msg_id := cbsp_msg_id,
	cbsp_ser_no := cbsp_ser_no
};

private type record GsmCgiAbstract {
	GsmMcc mcc,
	GsmMnc mnc,
	GsmLac lac,
	GsmCellId ci
};
private template (value) BSSMAP_FIELD_CellIdentification_CGI bssmap_cgi(GsmCgiAbstract cgi) :=
	ts_BSSMAP_CI_CGI(cgi.mcc, cgi.mnc, cgi.lac, cgi.ci);
private template (value) BSSMAP_FIELD_CellIdentification_LAC_CI bssmap_lac_ci(GsmCgiAbstract cgi) :=
	ts_BSSMAP_CI_LAC_CI(cgi.lac, cgi.ci);
private template (value) BSSMAP_FIELD_CellIdentification_LAI bssmap_lai(GsmCgiAbstract cgi) :=
	ts_BSSMAP_CI_LAI(cgi.mcc, cgi.mnc, cgi.lac);
private template (value) OCT2 bssmap_lac(GsmCgiAbstract cgi) := ts_BSSMAP_CI_LAC(cgi.lac);
private template (value) OCT2 bssmap_ci(GsmCgiAbstract cgi) := ts_BSSMAP_CI_CI(cgi.ci);

type component cbsp_test_CT extends test_CT, CBSP_Adapter_CT {
	var CBSP_Pars g_pars;
	var uint16_t g_cbsp_msg_id := 0;
	var uint16_t g_cbsp_ser_no := 0;
}

private function f_g_cbsp_next_msg_id_ser_no() runs on cbsp_test_CT
{
	g_cbsp_msg_id := g_cbsp_msg_id + 1;
	g_cbsp_ser_no := g_cbsp_ser_no + 1;
	log("g_cbsp_msg_id=", g_cbsp_msg_id, "  g_cbsp_ser_no=", g_cbsp_ser_no);
}

private altstep as_IgnRSL(template (present) RSL_Message tr) runs on cbsp_test_CT {
[] IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr)) { repeat; }
[] IPA_RSL[1][0].receive(tr_ASP_RSL_UD(tr)) { repeat; }
[] IPA_RSL[2][0].receive(tr_ASP_RSL_UD(tr)) { repeat; }
}

private altstep as_FailRSL() runs on cbsp_test_CT {
var template (present) RSL_Message tr := (tr_RSL_SMSCB_CMD);
var ASP_RSL_Unitdata rx;
[] IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr)) -> value rx {
	setverdict(fail, "Received unexpected RSL ", rx);
	mtc.stop;
	}
[] IPA_RSL[1][0].receive(tr_ASP_RSL_UD(tr)) -> value rx {
	setverdict(fail, "Received unexpected RSL ", rx);
	mtc.stop;
	}
[] IPA_RSL[2][0].receive(tr_ASP_RSL_UD(tr)) -> value rx {
	setverdict(fail, "Received unexpected RSL ", rx);
	mtc.stop;
	}
}

private function f_vty_configure_cbsp() runs on cbsp_test_CT {
	f_vty_enter_config(BSCVTY);
	f_vty_transceive(BSCVTY, "cbc");
	/* Move to disabled in order to force socker recreating later */
	f_vty_transceive(BSCVTY, "mode disabled");

	/* Configure IP addresses */

	if (g_pars.tcp_client) {
		f_vty_transceive(BSCVTY, "server");
		f_vty_transceive(BSCVTY, "local-ip " & g_pars.remote_ip);
		f_vty_transceive(BSCVTY, "exit");
		f_vty_transceive(BSCVTY, "mode server");
	} else {
		f_vty_transceive(BSCVTY, "client");
		f_vty_transceive(BSCVTY, "local-ip " & g_pars.remote_ip);
		f_vty_transceive(BSCVTY, "remote-ip " & g_pars.local_ip);
		f_vty_transceive(BSCVTY, "exit");
		f_vty_transceive(BSCVTY, "mode client");
	}

	f_vty_transceive(BSCVTY, "exit");
	f_vty_transceive(BSCVTY, "exit");
}

private function f_init(float guard_timeout := 30.0) runs on cbsp_test_CT {
	BSC_Tests.f_init(guard_timeout := guard_timeout);
	activate(as_IgnRSL((tr_RSL_BCCH_INFO, tr_RSL_SACCH_FILL,
			    tr_RSL_NO_BCCH_INFO, tr_RSL_NO_SACCH_FILL,
			    tr_RSL_MsgTypeD(?))));
	activate(as_FailRSL());

	f_vty_configure_cbsp();

	if (g_pars.tcp_client) {
		f_cbsp_init_client();
	} else {
		f_cbsp_init_server();
	}

}
private function f_cbsp_init_client() runs on cbsp_test_CT {
	CBSP_Adapter.f_connect(g_pars.remote_ip, g_pars.remote_port, g_pars.local_ip, 0);
	CBSP[0].receive(tr_CBSP_Recv(?, tr_CBSP_RESTART(?, CBSP_BC_MSGT_CBS, ?)));
	setverdict(pass);
}

private function f_cbsp_init_server(float guard_timeout := 30.0) runs on cbsp_test_CT {
	var ASP_Event asp_evt;
	timer T := 10.0;

	CBSP_Adapter.f_bind(g_pars.local_ip, g_pars.local_port);

	T.start;
	alt {
	[] CBSP[0].receive(ASP_Event:{connOpened:=?}) -> value asp_evt {
		g_cbsp_conn_id[0] := asp_evt.connOpened.connId;
		}
	[] T.timeout {
		setverdict(fail, "Timeout waiting for incoming connection to CBSP Port");
		mtc.stop;
		}
	}
	f_expect_cbsp_restart();

	g_cbsp_msg_id := g_pars.cbsp_msg_id;
	g_cbsp_ser_no := g_pars.cbsp_ser_no;
	log("g_cbsp_msg_id=", g_cbsp_msg_id, "  g_cbsp_ser_no=", g_cbsp_ser_no);

	f_cbsp_reset_bss(0);
}
private function f_expect_cbsp_restart() runs on cbsp_test_CT {
	interleave {
	[] CBSP[0].receive(tr_CBSP_Recv(?, tr_CBSP_RESTART(?, CBSP_BC_MSGT_CBS, CBSP_RI_DATA_LOST)));
	/* should we also expect a restart for emergency related messages? */
	//[] CBSP[0].receive(tr_CBSP_Recv(?, tr_CBSP_RESTART(?, CBSP_BC_MSGT_EMERG, CBSP_RI_DATA_LOST)));
	}
}

/* Generate a CBSP payload: random size for payload_len == 0, or specific fixed size for payload_len > 0. */
function f_gen_page(integer payload_len := 0) return CBSP_IE {
	if (payload_len < 1) {
		/* The maximum CBSP page payload space is 88, but 6 bytes of payload header are added in the first page:
		 * the maximum length generated here thus is 82. The minimum generated length is 1 (avoiding zero
		 * length). Note, f_rnd_int(82) returns [0..81], so this results in a len ranging [1..82]: */
		payload_len := 1 + f_rnd_int(82);
	}
	log("Generating CBSP payload: ", payload_len, " octets");
	var octetstring payload := f_rnd_octstring(payload_len);
	return valueof(ts_CbspMsgContent(payload, payload_len));
}

function f_cbsp_reset_bss(integer idx) runs on cbsp_test_CT {
	/* Make sure no CBSP ETWS commands from a previous CBSP test remain in the RSL queue */
	IPA_RSL[0][0].clear;
	IPA_RSL[1][0].clear;
	IPA_RSL[2][0].clear;

	var template (value) CBSP_PDU tx;
	timer T := 3.0;
	tx := ts_CBSP_RESET(cell_list := ts_BSSMAP_CIL_BSS);
	CBSP[idx].clear;
	CBSP[idx].send(ts_CBSP_Send(g_cbsp_conn_id[idx], tx));
	T.start;
	alt {
	[] CBSP[idx].receive(tr_CBSP_Recv(?, tr_CBSP_RESET_COMPL(cell_list := ts_BSSMAP_CIL_BSS)));
	[] CBSP[idx].receive {
		setverdict(fail, "received unexpected CBSP");
		mtc.stop;
		}
	[] T.timeout {
		setverdict(fail, "timeout waiting for RESET COMPLETE");
		mtc.stop;
		}
	}

	f_cbsp_expect_disable_etws_pn_broadcast();
}

function f_cbsp_expect_disable_etws_pn_broadcast() runs on cbsp_test_CT
{
	var template ASP_RSL_Unitdata zero_payload := tr_ASP_RSL_UD(tr_RSL_OSMO_ETWS_CMD(t_RslChanNr_PCH_AGCH(0), ''O));
	interleave {
	[] IPA_RSL[0][0].receive(zero_payload) { log("CBSP: disabled ETWS PN broadcast on bts 0"); }
	[] IPA_RSL[1][0].receive(zero_payload) { log("CBSP: disabled ETWS PN broadcast on bts 1"); }
	[] IPA_RSL[2][0].receive(zero_payload) { log("CBSP: disabled ETWS PN broadcast on bts 2"); }
	}
}

/* send a WRITE CBS to the BSC; expect either COMPLETE or FAILURE in response*/
function f_cbsp_write_emerg(uint16_t msg_id, uint16_t ser_no,
			template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_BSS,
			template (value) uint8_t emerg_ind := 1,
			template (value) uint16_t warn_type := oct2int('0780'O),
			template (value) uint16_t warn_per := 5,
			template BSSMAP_FIELD_CellIdentificationList success_list := ?,
			template CBSP_FailureListItems fail_list := omit) runs on cbsp_test_CT {
	var template (value) CBSP_PDU tx;
	var template CBSP_PDU rx;
	var CBSP_IEs pages := {f_gen_page()};

	tx := ts_CBSP_WRITE_EMERG(msg_id, ser_no, cell_list, emerg_ind, warn_type, warn_per);
	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	if (istemplatekind(fail_list, "omit")) {
		rx := tr_CBSP_WRITE_EMERG_COMPL(msg_id, ser_no, success_list);
	} else {
		rx := tr_CBSP_WRITE_EMERG_FAIL(msg_id, ser_no, fail_list, *, success_list);
	}
	alt {
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx)) {
		setverdict(pass);
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
}

/* send a WRITE CBS to the BSC; expect either COMPLETE or FAILURE in response*/
function f_cbsp_write(uint16_t msg_id, uint16_t ser_no,
		      template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_BSS,
		      template (value) CBSP_Category category := CBSP_CATEG_NORMAL,
		      uint16_t rep_period := 10, uint16_t num_bcast_req := 1,
		      uint8_t dcs := 0, uint8_t channel_ind := 0, CBSP_IEs content,
		      template BSSMAP_FIELD_CellIdentificationList success_list := ?,
		      template CBSP_FailureListItems fail_list := omit) runs on cbsp_test_CT {
	var template (value) CBSP_PDU tx;
	var template CBSP_PDU rx;

	tx := ts_CBSP_WRITE_CBS(msg_id, ser_no, cell_list, channel_ind, category,
				rep_period, num_bcast_req, dcs, content);
	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	if (istemplatekind(fail_list, "omit")) {
		rx := tr_CBSP_WRITE_CBS_COMPL(msg_id, ser_no, success_list, channel_ind);
	} else {
		rx := tr_CBSP_WRITE_CBS_FAIL(msg_id, ser_no, fail_list, *, success_list, channel_ind);
	}
	alt {
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx)) {
		setverdict(pass);
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
}

/* send a REPLACE emergency to the BSC; expect either COMPLETE or FAILURE in response*/
function f_cbsp_replace_emerg(uint16_t msg_id, uint16_t new_ser_no, uint16_t old_ser_no,
			template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_BSS,
			template (value) uint8_t emerg_ind := 1,
			template (value) uint16_t warn_type := oct2int('0780'O),
			template (value) uint16_t warn_per := 5,
		        template BSSMAP_FIELD_CellIdentificationList success_list := ?,
			template CBSP_FailureListItems fail_list := omit) runs on cbsp_test_CT {
	var template (value) CBSP_PDU tx;
	var template CBSP_PDU rx;
	var CBSP_IEs pages := {f_gen_page()};

	tx := ts_CBSP_REPLACE_EMERG(msg_id, new_ser_no, old_ser_no, cell_list, emerg_ind, warn_type, warn_per);
	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	if (istemplatekind(fail_list, "omit")) {
		rx := tr_CBSP_REPLACE_EMERG_COMPL(msg_id, new_ser_no, old_ser_no, success_list);
	} else {
		rx := tr_CBSP_REPLACE_EMERG_FAIL(msg_id, new_ser_no, old_ser_no, fail_list, omit, success_list);
	}
	alt {
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx)) {
		setverdict(pass);
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
}

/* send a REPLACE CBS to the BSC; expect either COMPLETE or FAILURE in response*/
function f_cbsp_replace(uint16_t msg_id, uint16_t new_ser_no, uint16_t old_ser_no,
		      template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_BSS,
		      template (value) CBSP_Category category := CBSP_CATEG_NORMAL,
		      uint16_t rep_period := 10, uint16_t num_bcast_req := 1,
		      uint8_t dcs := 0, uint8_t channel_ind := 0, CBSP_IEs content,
		      template BSSMAP_FIELD_CellIdentificationList success_list := ?,
		      template CBSP_FailureListItems fail_list := omit) runs on cbsp_test_CT {
	var template (value) CBSP_PDU tx;
	var template CBSP_PDU rx;

	tx := ts_CBSP_REPLACE_CBS(msg_id, new_ser_no, old_ser_no, cell_list, channel_ind, category,
				rep_period, num_bcast_req, dcs, content);
	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	if (istemplatekind(fail_list, "omit")) {
		rx := tr_CBSP_REPLACE_CBS_COMPL(msg_id, new_ser_no, old_ser_no, ?, success_list,
						channel_ind);
	} else {
		rx := tr_CBSP_REPLACE_CBS_FAIL(msg_id, new_ser_no, old_ser_no, fail_list, *, success_list,
					       channel_ind);
	}
	alt {
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx)) {
		setverdict(pass);
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
}
/* send a KILL CBS to the BSC; expect either COMPLETE or FAILURE in response*/
function f_cbsp_kill(uint16_t msg_id, uint16_t ser_no, template (omit) uint8_t channel_ind := 0,
		     template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_BSS,
		     template BSSMAP_FIELD_CellIdentificationList success_list := ?,
		     template CBSP_IE_NumBcastComplList compl_list := *,
		     template CBSP_FailureListItems fail_list := omit) runs on cbsp_test_CT
{
	var template (value) CBSP_PDU tx;
	var template CBSP_PDU rx;

	tx := ts_CBSP_KILL(msg_id, ser_no, cell_list, channel_ind);
	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	if (istemplatekind(fail_list, "omit")) {
		rx := tr_CBSP_KILL_COMPL(msg_id, ser_no, compl_list:=compl_list, cell_list:=success_list,
					 channel_ind:=channel_ind);
	} else {
		rx := tr_CBSP_KILL_FAIL(msg_id, ser_no, fail_list, compl_list:=compl_list, cell_list:=success_list,
					channel_ind:=channel_ind);
	}
	alt {
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx)) {
		setverdict(pass);
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
}

/* send a KILL CBS to the BSC; expect either COMPLETE or FAILURE in response*/
function f_cbsp_msg_status_query(uint16_t msg_id, uint16_t ser_no, template (value) uint8_t channel_ind := 0,
		     template (value) BSSMAP_FIELD_CellIdentificationList cell_list := ts_BSSMAP_CIL_BSS,
		     template CBSP_IE_NumBcastComplList compl_list := ?,
		     template CBSP_FailureListItems fail_list := omit) runs on cbsp_test_CT
{
	var template (value) CBSP_PDU tx;
	var template CBSP_PDU rx;

	tx := ts_CBSP_MSG_STATUS_QUERY(msg_id, ser_no, cell_list, channel_ind);
	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	if (istemplatekind(fail_list, "omit")) {
		rx := tr_CBSP_MSG_STATUS_QUERY_COMPL(msg_id, ser_no, compl_list:=compl_list, channel_ind:=channel_ind);
	} else {
		rx := tr_CBSP_MSG_STATUS_QUERY_FAIL(msg_id, ser_no, fail_list, channel_ind:=channel_ind,
						    compl_list:=compl_list);
	}
	alt {
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx)) {
		setverdict(pass);
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
}

template (present) RSL_IE_CbCommandType
tr_RslCbCmdType(template (present) uint2_t lblock := ?, template (present) RSL_CbCommand cmd := ?) := {
	command := cmd,
	default_bcast_null := ?,
	spare := ?,
	last_block := lblock
}

/* translate blocks count to RSL_CB_CMD_LASTBLOCK_1..4 values */
private function f_cbsp_block_count_enc(integer num_blocks) return integer
{
	if (num_blocks < 1 or num_blocks > 4) {
		setverdict(fail, "Invalid num_blocks: ", num_blocks);
		mtc.stop;
	}
	if (num_blocks == 4) {
		return 0;
	}
	return num_blocks;
}

/* build a RSL_Message receive template from a CBSP page */
private function f_page2rsl(CBSP_IE page, uint16_t msg_id, uint16_t ser_no, boolean ext_cbch := false,
                            template (present) integer expect_blocks := ?)
return template (present) RSL_Message
{
	var template (present) RSL_Message tr;
	var integer len;
	var integer num_blocks;
	var octetstring payload;

	payload := int2oct(ser_no, 2) & int2oct(msg_id, 2) & '0011'O & page.body.msg_content.val;
	len := lengthof(payload);
	num_blocks := len / 22;
	if (len mod 22 > 0) {
		num_blocks := num_blocks + 1;
	}

	if (not istemplatekind(expect_blocks, "omit") and not match(num_blocks, expect_blocks)) {
		setverdict(fail, "mismatch: CBSP page expect_blocks == ", expect_blocks, ", but generated num_blocks == ", num_blocks);
		mtc.stop;
	}

	var integer lblock := f_cbsp_block_count_enc(num_blocks);

	tr := tr_RSL_SMSCB_CMD(tr_RslCbCmdType(lblock), f_pad_oct(payload, 88, '00'O));
	if (ext_cbch) {
		tr.ies[3] := tr_RSL_IE(RSL_IE_Body:{smscb_chan_ind := 1});
		tr.ies[4] := *;
	}
	return tr;
}

/***********************************************************************
 * Test Cases
 ***********************************************************************/

/* Test if BSC (server) accepts connections from CBC (client, IPv4) */
testcase TC_cbsp_bsc_server() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(tcp_client := true));
	f_init();
	f_shutdown_helper();
}

/* Test if BSC (server) accepts connections from CBC (client, IPv6) */
testcase TC_cbsp_bsc_server_ipv6() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(tcp_client := true));
	g_pars.local_ip := mp_cbc_ip6;
	g_pars.remote_ip := mp_bsc_cbsp_ip6;
	f_init();
	f_shutdown_helper();
}

/* Test if BSC (client) is connecting to CBC (server, IPv4) */
testcase TC_cbsp_bsc_client() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(false, 0, 0));
	f_init();
	f_shutdown_helper();
}

/* Test if BSC (client) is connecting to CBC (server, IPv6) */
testcase TC_cbsp_bsc_client_ipv6() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(false, 0, 0));
	g_pars.local_ip := mp_cbc_ip6;
	g_pars.remote_ip := mp_bsc_cbsp_ip6;
	f_init();
	f_shutdown_helper();
}

/* Test if a BSS-global RESET is executed successfully */
testcase TC_cbsp_reset_bss() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(false, 0, 0));
	f_init();
	f_cbsp_reset_bss(0);
	f_shutdown_helper();
}

/* Test if a LAC_CI FAILURE Ind is sent when TRX holding the CBCH is locked
 * (becomes unavailable). LAC_CI RESTART Ind should be sent when the TRX becomes
 * unlocked again. */
testcase TC_cbsp_cell_rflock_failure_restart() runs on cbsp_test_CT {
	var template CBSP_PDU rx_cbs;
	var template CBSP_PDU rx_emerg;
	var boolean received_cbs := false;
	var boolean received_emerg := false;
	var template (present) CBSP_FailureListItem fli_exp;
	var template (present) BSSMAP_FIELD_CellIdentificationList cil_exp;

	g_pars := valueof(ts_CBSP_Pars_default(false, 0, 0));
	f_init();

	/* Lock the TRX, CBCH should become unavailable and BSC send FAILURE */
	f_ctrl_set(IPA_CTRL, "bts.0.trx.0.rf_locked", "1");

	/* Expect receiving either CGI or LAC+CI: */
	fli_exp := (CBSP_FailureListItem_CGI(bssmap_cgi(mp_cgi_bts0),
					     CBSP_CAUSE_CB_NOT_OPERATIONAL),
		    CBSP_FailureListItem_LAC_CI(bssmap_lac_ci(mp_cgi_bts0),
						CBSP_CAUSE_CB_NOT_OPERATIONAL));
	rx_cbs := tr_CBSP_FAILURE({fli_exp}, CBSP_BC_MSGT_CBS);
	rx_emerg := tr_CBSP_FAILURE({fli_exp}, CBSP_BC_MSGT_EMERG);
	alt {
	[not received_cbs] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx_cbs)) {
		received_cbs := true;
		if (not received_cbs or not received_emerg) {
			repeat;
		}
		}
	[not received_emerg] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx_emerg)) {
		received_emerg := true;
		if (not received_cbs or not received_emerg) {
			repeat;
		}
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}
	setverdict(pass);

	/* Unlock the TRX, CBCH should become available and BSC send RESTART */
	f_ctrl_set(IPA_CTRL, "bts.0.trx.0.rf_locked", "0");
	/* Expect receiving either CGI or LAC+CI: */
	cil_exp := (ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)}),
		    ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)}));
	rx_cbs := tr_CBSP_RESTART(cil_exp, CBSP_BC_MSGT_CBS, ?);
	rx_emerg := tr_CBSP_RESTART(cil_exp, CBSP_BC_MSGT_EMERG, ?);
	received_cbs := false;
	received_emerg := false;
	alt {
	[not received_cbs] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx_cbs)) {
		received_cbs := true;
		if (not received_cbs or not received_emerg) {
			repeat;
		}
		}
	[not received_emerg] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx_emerg)) {
		received_emerg := true;
		if (not received_cbs or not received_emerg) {
			repeat;
		}
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}

	f_shutdown_helper();
}

/* Test if a LAC_CI FAILURE Ind is sent when conn of TRX holding the CBCH goes down. */
testcase TC_cbsp_cell_rsl_down_failure() runs on cbsp_test_CT {
	var template CBSP_PDU rx_cbs;
	var template CBSP_PDU rx_emerg;
	var boolean received_cbs := false;
	var boolean received_emerg := false;
	var template (present) CBSP_FailureListItem fli_exp;

	g_pars := valueof(ts_CBSP_Pars_default(false, 0, 0));
	f_init();

	/* Drop RSL link of the TRX holding the CBCH: */
	f_ipa_rsl_stop(bts[0][0].rsl);

	/* Expect receiving either CGI or LAC+CI: */
	fli_exp := (CBSP_FailureListItem_CGI(bssmap_cgi(mp_cgi_bts0),
					     CBSP_CAUSE_CB_NOT_OPERATIONAL),
		    CBSP_FailureListItem_LAC_CI(bssmap_lac_ci(mp_cgi_bts0),
						CBSP_CAUSE_CB_NOT_OPERATIONAL));
	rx_cbs := tr_CBSP_FAILURE({fli_exp}, CBSP_BC_MSGT_CBS);
	rx_emerg := tr_CBSP_FAILURE({fli_exp}, CBSP_BC_MSGT_EMERG);
	alt {
	[not received_cbs] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx_cbs)) {
		received_cbs := true;
		if (not received_cbs or not received_emerg) {
			repeat;
		}
		}
	[not received_emerg] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], rx_emerg)) {
		received_emerg := true;
		if (not received_cbs or not received_emerg) {
			repeat;
		}
		}
	[] CBSP[0].receive(tr_CBSP_Recv(g_cbsp_conn_id[0], ?)) {
		setverdict(fail, "Received unexpected CBSP");
		mtc.stop;
		}
	}

	/* TODO: call f_init() again to reconnect? */
	f_shutdown_helper();
}

testcase TC_cbsp_write() runs on cbsp_test_CT {
	var template (value) CBSP_PDU tx;
	var CBSP_IEs pages := {f_gen_page()};
	g_pars := valueof(ts_CBSP_Pars_default(false, 0, 0));
	f_init();

	tx := ts_CBSP_WRITE_CBS(msg_id:=23,  new_ser_nr:=42, cell_list:=ts_BSSMAP_CIL_BSS,
				channel_ind:=0, category:=CBSP_CATEG_NORMAL,
				rep_period:=10, num_bcast_req:=1, dcs := 0,
				content:=pages);

	CBSP[0].send(ts_CBSP_Send(g_cbsp_conn_id[0], tx));
	f_sleep(10.0);
	f_shutdown_helper();
}

/* Write to entire BSS; three cells succeed; one fails (no CBCH) */
function f_tc_cbsp_write_bss(integer payload_len := -1, template (present) integer expect_blocks) runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page(payload_len := payload_len)};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	cell_list := ts_BSSMAP_CIL_BSS;
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=tr_BSSMAP_CIL_CGI({?,?,?}), fail_list:={?});

	var template (present) RSL_Message tr := f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no, expect_blocks := expect_blocks);
	log("RSL[0,1,2] EXPECTING ", tr_ASP_RSL_UD(tr));
	interleave {
	[] IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr)) { log("Got SMSCB CMD on RSL[0]"); }
	[] IPA_RSL[1][0].receive(tr_ASP_RSL_UD(tr)) { log("Got SMSCB CMD on RSL[1]"); }
	[] IPA_RSL[2][0].receive(tr_ASP_RSL_UD(tr)) { log("Got SMSCB CMD on RSL[2]"); }
	}
	setverdict(pass);

	/* Make the next test run (if any) use different msg_id and ser_no */
	f_g_cbsp_next_msg_id_ser_no();
}
testcase TC_cbsp_write_bss() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(false, 1001, 1501));
	f_init(guard_timeout := 60.0);
	/* In the SMSCB message, there is a head followed by payload,
	 * and the resulting data is segmented in blocks of 22 octets (<= 4 blocks).
	 *
	 * [head][...payload....]|[....................]|[....................]|[....................]
	 *       0               |16                    |38                    |60                   |82
	 * 0    5                |22                    |44                    |66                   |88
	 *
	 *         blocks count:   1     | 2      | 3      | 4
	 * payload octets count:   1..16 | 17..38 | 39..60 | 61..82
	 */
	f_tc_cbsp_write_bss(payload_len := 1, expect_blocks := 1);
	f_tc_cbsp_write_bss(payload_len := 2, expect_blocks := 1);
	f_tc_cbsp_write_bss(payload_len := 16, expect_blocks := 1);
	f_tc_cbsp_write_bss(payload_len := 17, expect_blocks := 2);
	f_tc_cbsp_write_bss(payload_len := 23, expect_blocks := 2);
	f_tc_cbsp_write_bss(payload_len := 38, expect_blocks := 2);
	f_tc_cbsp_write_bss(payload_len := 39, expect_blocks := 3);
	f_tc_cbsp_write_bss(payload_len := 42, expect_blocks := 3);
	f_tc_cbsp_write_bss(payload_len := 60, expect_blocks := 3);
	f_tc_cbsp_write_bss(payload_len := 61, expect_blocks := 4);
	f_tc_cbsp_write_bss(payload_len := 77, expect_blocks := 4);
	f_tc_cbsp_write_bss(payload_len := 82, expect_blocks := 4);
	f_shutdown_helper();
}

/* Write to single BTS supporting CBCH: success */
testcase TC_cbsp_write_bts_cgi() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 2001, 2501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=cell_list, fail_list:=omit);
	var template (present) RSL_Message tr := f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no);
	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr));
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write to single BTS not supporting CBCH: failure */
testcase TC_cbsp_write_bts_no_cbch() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 3001, 3501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts3)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=omit, fail_list:={?});
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write to single non-existant BTS */
testcase TC_cbsp_write_unknown_bts() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 4001, 4501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({ts_BSSMAP_CI_CGI(mp_cgi_bts0.mcc, mp_cgi_bts1.mnc, 22222, 33333)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=omit, fail_list:={?});
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write to single BTS using LAC+CI */
testcase TC_cbsp_write_lac_ci() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 5001, 5501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=?, fail_list:=omit);
	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no)));
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write to single BTS using CI */
testcase TC_cbsp_write_ci() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 6001, 6501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CI({bssmap_ci(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=?, fail_list:=omit);
	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no)));
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write to single BTS using LAI */
testcase TC_cbsp_write_lai() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 7001, 7501));
	f_init();

	/* bts0 and bts1 have the same LAI (only differ in cell identity).
	 * bts2 and bts3 also have the same LAI, but only bts2 has a CBCH.
	 * Target only bts2.
	 */
	cell_list := ts_BSSMAP_CIL_LAI({bssmap_lai(mp_cgi_bts2)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=?, fail_list:=omit);
	IPA_RSL[2][0].receive(tr_ASP_RSL_UD(f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no)));
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write to two BTS using LAC */
testcase TC_cbsp_write_lac() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 8001, 8501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC({bssmap_lac(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages,
		     success_list:=?, fail_list:=omit);
	var template (present) RSL_Message tr := f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no);
	interleave {
	[] IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr));
	[] IPA_RSL[1][0].receive(tr_ASP_RSL_UD(tr));
	}
	f_sleep(5.0);
	f_shutdown_helper();
}

/* Write a message, then replace it */
testcase TC_cbsp_write_then_replace() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 9001, 9501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, num_bcast_req:=10, content:=pages,
		     success_list:=?, fail_list:=omit);

	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no)));

	/* Replace: keep the same msg_id, use a new ser_no */
	var uint16_t old_ser_no := g_cbsp_ser_no;
	g_cbsp_ser_no := g_cbsp_ser_no + 1;
	f_cbsp_replace(g_cbsp_msg_id, g_cbsp_ser_no, old_ser_no, cell_list, content:=pages,
		       success_list:=?, fail_list:=omit);

	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no)));
	f_sleep(1.0);
	f_shutdown_helper();
}

/* Verify handling of the Repetition Period and the Number of Broadcasts */
testcase TC_cbsp_write_rep_period_num() runs on cbsp_test_CT {
	var CBSP_IE page := f_gen_page();
	const integer rep_period := 2; /* units of 1.883s */
	const integer rep_number := 5;
	var integer msg_count := 0;
	var float last_time := 0.0;
	timer T;

	g_pars := valueof(ts_CBSP_Pars_default(false, 9001, 9501));
	f_init(guard_timeout := 60.0);

	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no,
		     cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)}),
		     rep_period := rep_period, num_bcast_req := rep_number,
		     content := { page }, success_list := ?, fail_list := omit);

	/* Count SMSCB messages during N=rep_number+2 repetition periods */
	T.start(int2float(rep_period * (rep_number + 2)) * 1.883);
	alt {
	[] IPA_RSL[0][0].receive(tr_ASP_RSL_UD(f_page2rsl(page, g_cbsp_msg_id, g_cbsp_ser_no))) {
		var float exp_period := int2float(rep_period) * 1.883;
		var float calc_period := T.read - last_time;

		log("Rx SMSCB message: count := ", msg_count + 1, ", ",
		    "elapsed := ", T.read, "s, diff := ", calc_period, "s");

		if (msg_count > 0) {
			/* Check the actual repetition period (+/- 0.5s) */
			var template float tr_exp_period := (exp_period - 0.5 .. exp_period + 0.5);
			if (not match(calc_period, tr_exp_period)) {
				setverdict(fail, "Repetition period mismatch: ",
					   "calculated := ", calc_period, "s vs ",
					   "expected := ", tr_exp_period, "s");
			}
		}

		msg_count := msg_count + 1;
		last_time := T.read;
		repeat;
		}
	[] T.timeout {
		log("Received ", msg_count, " messages during ", last_time, "s");
		}
	}

	if (msg_count != rep_number) {
		setverdict(fail, "Received ", msg_count, " messages, ",
			   "while we expected ", rep_number);
	}

	f_sleep(1.0);
	f_shutdown_helper();
}

/* Replace a message that doesn't exist: failure */
testcase TC_cbsp_replace_nonexist() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 10001, 10501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_replace(10, 10023, 10042, cell_list, content:=pages,
		       success_list:=omit, fail_list:=?);
	f_shutdown_helper();
}

/* Write more messages than can be scheduled */
testcase TC_cbsp_write_too_many() runs on cbsp_test_CT {
	/* repeating three pages at an interval of 1 is impossible */
	var CBSP_IEs pages := {f_gen_page(), f_gen_page(), f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 11001, 11501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, rep_period:=1, content:=pages,
		     success_list:=omit, fail_list:=?);
	f_shutdown_helper();
}

/* Kill message that doesn't exist: failure */
testcase TC_cbsp_kill_nonexist() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 12001, 12501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_kill(g_cbsp_msg_id, g_cbsp_ser_no, 0, cell_list, success_list:=omit, compl_list:=omit, fail_list:=?);
	f_shutdown_helper();
}
/* Write a message, then kill it */
testcase TC_cbsp_write_then_kill() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 13001, 13501));
	f_init();

	/* write message, request more than one transmission on BTS0 */
	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, num_bcast_req := 5, content:=pages, success_list:=?, fail_list:=omit);
	/* expect to receive it once on the BTS */
	var template (present) RSL_Message tr := f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no);
	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr));
	/* kill it, expecting non-empty completion list; success must be empty in case of CBS! */
	f_cbsp_kill(g_cbsp_msg_id, g_cbsp_ser_no, 0, cell_list, success_list:=omit, compl_list:=?, fail_list:=omit);
	f_shutdown_helper();
}

/* Write a message, then reset all messages */
testcase TC_cbsp_write_then_reset() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	g_pars := valueof(ts_CBSP_Pars_default(false, 14001, 14501));
	f_init();

	cell_list := ts_BSSMAP_CIL_LAC_CI({bssmap_lac_ci(mp_cgi_bts0)});
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, content:=pages, success_list:=?, fail_list:=omit);
	f_cbsp_reset_bss(0);
	f_shutdown_helper();
}

private const octetstring c_ETWS_sec_default :=
	'00000000000000000000000000000000000000000000000000'O &
	'00000000000000000000000000000000000000000000000000'O;
function f_gen_etws_pn(uint16_t ser_nr, uint16_t msg_id, OCT2 msg_type := '0780'O,
			octetstring sec_inf := c_ETWS_sec_default) return octetstring {
	return int2oct(ser_nr, 2) & int2oct(msg_id, 2) & msg_type & sec_inf;
}

/* Write ETWS PN to single BTS; verify it arrives on DCHAN */
testcase TC_cbsp_emerg_write_bts_cgi_dchan() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	var ASP_RSL_Unitdata rx_rsl_ud;

	g_pars := valueof(ts_CBSP_Pars_default(false, 15001, 15501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});

	/* first establish a dedicated channel */
	var DchanTuple dt := f_est_dchan('23'O, 23, gen_l3_valid_payload());

	/* then send ETWS PN */
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list);
	var template (present) octetstring tr_apdu := f_gen_etws_pn(g_cbsp_msg_id, g_cbsp_ser_no);
	timer T := 5.0;
	T.start;
	alt {
	[] IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr_RSL_DATA_REQ(dt.rsl_chan_nr, ?, ?))) -> value rx_rsl_ud {
		var RSL_IE_Body l3_ie;
		if (f_rsl_find_ie(rx_rsl_ud.rsl, RSL_IE_L3_INFO, l3_ie) == false) {
			setverdict(fail, "RSL DATA REQ without L3?");
			mtc.stop;
		}
		var PDU_ML3_NW_MS l3 := dec_PDU_ML3_NW_MS(l3_ie.l3_info.payload);
		var template (present) APDU_Flags_V tr_flags := {
			lastSeg := '0'B,
			firstSeg := '0'B,
			cR := '0'B,
			spare := '0'B
		};
		if (match(l3, tr_RR_APP_INFO('0001'B, tr_apdu, tr_flags))) {
			setverdict(pass);
			}
		}
	[] IPA_RSL[0][0].receive { repeat; }
	[] T.timeout {
		setverdict(fail, "Waiting for APP INFO");
		}
	}

	f_perform_clear_test_ct(dt);
	f_shutdown_helper();
}

private function f_exp_rsl_etws(integer rsl_idx := 0, boolean enabled) runs on cbsp_test_CT {
	var template (present) octetstring tr_apdu;
	timer T := 5.0 + 0.5; /* +0.5s guard */

	if (enabled) {
		tr_apdu := f_gen_etws_pn(g_cbsp_ser_no, g_cbsp_msg_id);
	} else {
		tr_apdu := ''O;
	}

	T.start;
	alt {
	[] IPA_RSL[rsl_idx][0].receive(tr_ASP_RSL_UD(tr_RSL_OSMO_ETWS_CMD(t_RslChanNr_PCH_AGCH(0), tr_apdu))) {
		setverdict(pass);
		}
	[] IPA_RSL[rsl_idx][0].receive(tr_ASP_RSL_UD(tr_RSL_OSMO_ETWS_CMD(?,?))) {
		setverdict(fail, "Received unexpected OSMO_ETWS_CMD");
		mtc.stop;
		}
	[] IPA_RSL[rsl_idx][0].receive { repeat; }
	[] T.timeout {
		if (enabled) {
			setverdict(fail, "Timeout waiting for RSL_OSMO_ETWS_CMD (enable)");
		} else {
			setverdict(fail, "Timeout waiting for RSL_OSMO_ETWS_CMD (disable)");
		}
		mtc.stop;
		}
	}
}

/* Write ETWS PN to single BTS; verify it arrives on CCHAN */
testcase TC_cbsp_emerg_write_bts_cgi_cchan() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	var ASP_RSL_Unitdata rx_rsl_ud;

	g_pars := valueof(ts_CBSP_Pars_default(false, 16001, 16501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list);

	f_exp_rsl_etws(0, true);
	f_shutdown_helper();
}

/* Write ETWS PN to single BTS; verify it arrives on CCHAN */
testcase TC_cbsp_emerg_write_bts_cgi_cchan_disable() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	var ASP_RSL_Unitdata rx_rsl_ud;

	g_pars := valueof(ts_CBSP_Pars_default(false, 17001, 17501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list);

	/* first expect the PN to be enabled */
	f_exp_rsl_etws(0, true);

	/* then expect it to be disabled after the warning period (5s) */
	f_exp_rsl_etws(0, false);
	f_shutdown_helper();
}

/* Write (!replace) ETWS PN to a single BTS which already has an ongoing PN; expect failure (OS#5539) */
testcase TC_cbsp_emerg_write_bts_cgi_noreplace() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	var ASP_RSL_Unitdata rx_rsl_ud;

	g_pars := valueof(ts_CBSP_Pars_default(false, 18001, 18501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list);

	/* first expect the PN to be enabled */
	f_exp_rsl_etws(0, true);

	/* write another emergency while first one is still ongoing; expect it to fail */
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no+1, cell_list, success_list:=omit, fail_list:=?);

	/* then expect first one to be disabled after the warning period (5s) */
	f_exp_rsl_etws(0, false);

	/* write another emergency after the first one has expired; expect it to succeed */
	g_cbsp_ser_no := g_cbsp_ser_no + 2;
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, success_list:=?, fail_list:=omit);
	f_exp_rsl_etws(0, true);
	f_shutdown_helper();
}

/* Replace ETWS PN to a single BTS which already has an ongoing PN; expect success */
testcase TC_cbsp_emerg_write_bts_cgi_replace() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	var ASP_RSL_Unitdata rx_rsl_ud;

	g_pars := valueof(ts_CBSP_Pars_default(false, 19001, 19501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list);

	/* first expect the PN to be enabled */
	f_exp_rsl_etws(0, true);

	/* write another emergency while first one is still ongoing; expect it to fail */
	f_cbsp_replace_emerg(g_cbsp_msg_id, g_cbsp_ser_no+1, g_cbsp_ser_no, cell_list);
	f_shutdown_helper();
}

/* Write ETWS PN to a single BTS, then kill it during its lifetime (OS#5540) */
testcase TC_cbsp_emerg_write_bts_cgi_kill() runs on cbsp_test_CT {
	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	var ASP_RSL_Unitdata rx_rsl_ud;

	g_pars := valueof(ts_CBSP_Pars_default(false, 20001, 20501));
	f_init();

	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	f_cbsp_write_emerg(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, warn_per := 10);

	/* first expect the PN to be enabled */
	f_exp_rsl_etws(0, true);

	/* kill it; expect non-zero success list, and no completion or failure lists */
	f_cbsp_kill(g_cbsp_msg_id, g_cbsp_ser_no, omit, cell_list, success_list:=cell_list,
		    compl_list:=omit, fail_list:=omit);

	/* then expect it to be disabled */
	f_exp_rsl_etws(0, false);
	f_shutdown_helper();
}



/* Send a MSG STATUS QUERY for an unknown message; expect no completion list and present failure list */
testcase TC_cbsp_status_q_empty() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(false, 21001, 21501));
	f_init();

	f_cbsp_msg_status_query(g_cbsp_msg_id, g_cbsp_ser_no, compl_list := omit, fail_list := ?);
	f_shutdown_helper();
}

/* Send a SMSCB to entire BSS followed by MSG_STATUS_QUERY; expect completion list and no failure list */
testcase TC_cbsp_status_q_bts_cgi() runs on cbsp_test_CT {
	g_pars := valueof(ts_CBSP_Pars_default(false, 22001, 22501));
	f_init();

	var CBSP_IEs pages := {f_gen_page()};
	var template (value) BSSMAP_FIELD_CellIdentificationList cell_list;
	cell_list := ts_BSSMAP_CIL_CGI({bssmap_cgi(mp_cgi_bts0)});
	/* request 5 transmissions */
	f_cbsp_write(g_cbsp_msg_id, g_cbsp_ser_no, cell_list, num_bcast_req := 5, content:=pages,
		     success_list:=cell_list, fail_list:=omit);
	var template (present) RSL_Message tr := f_page2rsl(pages[0], g_cbsp_msg_id, g_cbsp_ser_no);
	/* wait for first transmission */
	IPA_RSL[0][0].receive(tr_ASP_RSL_UD(tr));
	var template (present) CBSP_IE_NumBcastComplList compl_list := {
		len := ?,
		spare1_4 := ?,
		cell_id_discr := ?,
		list := {
			cI_CGI := {
				ci := bssmap_cgi(mp_cgi_bts0),
				num_bcast_compl := (1 .. 5),
				num_bcast_info := CBSP_NUM_BCAST_INFO_VALID,
				spare1_4 := '0000'B
			}
		}
	};
	f_cbsp_msg_status_query(g_cbsp_msg_id, g_cbsp_ser_no, cell_list:=cell_list, compl_list := compl_list, fail_list := omit);
	f_shutdown_helper();
}




control {
	execute( TC_cbsp_bsc_server() );
	execute( TC_cbsp_bsc_server_ipv6() );
	execute( TC_cbsp_bsc_client() );
	execute( TC_cbsp_bsc_client_ipv6() );
	execute( TC_cbsp_reset_bss() );
	execute( TC_cbsp_cell_rflock_failure_restart() );
	execute( TC_cbsp_cell_rsl_down_failure() );

	/* test various different types of Cell Identities */
	execute( TC_cbsp_write_bss() );
	execute( TC_cbsp_write_bts_cgi() );
	execute( TC_cbsp_write_bts_no_cbch() );
	execute( TC_cbsp_write_unknown_bts() );
	execute( TC_cbsp_write_lac_ci() );
	execute( TC_cbsp_write_ci() );
	execute( TC_cbsp_write_lai() );
	execute( TC_cbsp_write_lac() );

	execute( TC_cbsp_write_then_replace() );
	execute( TC_cbsp_write_rep_period_num() );
	execute( TC_cbsp_replace_nonexist() );
	execute( TC_cbsp_write_too_many() );
	execute( TC_cbsp_kill_nonexist() );
	execute( TC_cbsp_write_then_kill() );
	execute( TC_cbsp_write_then_reset() );

	execute( TC_cbsp_status_q_empty() );
	execute( TC_cbsp_status_q_bts_cgi() );

	execute( TC_cbsp_emerg_write_bts_cgi_dchan() );
	execute( TC_cbsp_emerg_write_bts_cgi_cchan() );
	execute( TC_cbsp_emerg_write_bts_cgi_cchan_disable() );
	execute( TC_cbsp_emerg_write_bts_cgi_noreplace() );
	execute( TC_cbsp_emerg_write_bts_cgi_replace() );
	execute( TC_cbsp_emerg_write_bts_cgi_kill() );
}


}