summaryrefslogtreecommitdiff
path: root/tools/mesh-tester.c
blob: d7b3b6f9b1db57525b77bdb6550b96cc07aea0b0 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdbool.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#include <glib.h>

#include "lib/bluetooth.h"
#include "lib/hci.h"
#include "lib/hci_lib.h"
#include "lib/mgmt.h"
#include "lib/l2cap.h"

#include "monitor/bt.h"
#include "emulator/vhci.h"
#include "emulator/bthost.h"
#include "emulator/hciemu.h"

#include "src/shared/util.h"
#include "src/shared/tester.h"
#include "src/shared/mgmt.h"
#include "src/shared/queue.h"

struct test_data {
	tester_data_func_t test_setup;
	const void *test_data;
	uint8_t expected_version;
	uint16_t expected_manufacturer;
	uint32_t expected_supported_settings;
	uint32_t initial_settings;
	struct mgmt *mgmt;
	struct mgmt *mgmt_alt;
	unsigned int mgmt_settings_id;
	unsigned int mgmt_alt_settings_id;
	unsigned int mgmt_alt_ev_id;
	unsigned int mgmt_discov_ev_id;
	uint8_t mgmt_version;
	uint16_t mgmt_revision;
	uint16_t mgmt_index;
	struct hciemu *hciemu;
	enum hciemu_type hciemu_type;
	bool expect_hci_command_done;
	struct queue *expect_hci_q;
	int unmet_conditions;
	int unmet_setup_conditions;
	int sk;
};

static void print_debug(const char *str, void *user_data)
{
	const char *prefix = user_data;

	tester_print("%s%s", prefix, str);
}

static void test_post_teardown(const void *test_data)
{
	struct test_data *data = tester_get_data();

	if (data->sk >= 0)
		close(data->sk);

	queue_destroy(data->expect_hci_q, NULL);

	hciemu_unref(data->hciemu);
	data->hciemu = NULL;
}

static void test_pre_setup_failed(void)
{
	test_post_teardown(NULL);
	tester_pre_setup_failed();
}

static void read_version_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct mgmt_rp_read_version *rp = param;

	tester_print("Read Version callback");
	tester_print("  Status: %s (0x%02x)", mgmt_errstr(status), status);

	if (status || !param) {
		test_pre_setup_failed();
		return;
	}

	data->mgmt_version = rp->version;
	data->mgmt_revision = btohs(rp->revision);

	tester_print("  Version %u.%u",
			data->mgmt_version, data->mgmt_revision);
}

static void read_commands_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	tester_print("Read Commands callback");
	tester_print("  Status: %s (0x%02x)", mgmt_errstr(status), status);

	if (status || !param) {
		test_pre_setup_failed();
		return;
	}
}

static bool check_settings(uint32_t supported, uint32_t expected)
{
	int i;

	if (supported == expected)
		return true;

	for (i = 0; i < 17; i++) {
		if (supported & BIT(i))
			continue;

		if (expected & BIT(i)) {
			tester_warn("Expected bit %u not supported", i);
			return false;
		}
	}

	return true;
}

static void read_info_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct mgmt_rp_read_info *rp = param;
	char addr[18];
	uint16_t manufacturer;
	uint32_t supported_settings, current_settings;
	struct bthost *bthost;

	tester_print("Read Info callback");
	tester_print("  Status: %s (0x%02x)", mgmt_errstr(status), status);

	if (status || !param) {
		test_pre_setup_failed();
		return;
	}

	ba2str(&rp->bdaddr, addr);
	manufacturer = btohs(rp->manufacturer);
	supported_settings = btohl(rp->supported_settings);
	current_settings = btohl(rp->current_settings);

	tester_print("  Address: %s", addr);
	tester_print("  Version: 0x%02x", rp->version);
	tester_print("  Manufacturer: 0x%04x", manufacturer);
	tester_print("  Supported settings: 0x%08x", supported_settings);
	tester_print("  Current settings: 0x%08x", current_settings);
	tester_print("  Class: 0x%02x%02x%02x",
			rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
	tester_print("  Name: %s", rp->name);
	tester_print("  Short name: %s", rp->short_name);

	if (strcmp(hciemu_get_address(data->hciemu), addr)) {
		test_pre_setup_failed();
		return;
	}

	if (rp->version != data->expected_version) {
		tester_warn("Expected version: 0x%02x != 0x%02x",
				rp->version, data->expected_version);
		test_pre_setup_failed();
		return;
	}

	if (manufacturer != data->expected_manufacturer) {
		tester_warn("Expected manufacturer: 0x%04x != 0x%04x",
				manufacturer, data->expected_manufacturer);
		test_pre_setup_failed();
		return;
	}

	if (!check_settings(supported_settings,
				data->expected_supported_settings)) {
		tester_warn("Expected supported settings: 0x%08x != 0x%08x",
				supported_settings,
				data->expected_supported_settings);
		test_pre_setup_failed();
		return;
	}

	if (!check_settings(current_settings, data->initial_settings)) {
		tester_warn("Initial settings: 0x%08x != 0x%08x",
				current_settings, data->initial_settings);
		test_pre_setup_failed();
		return;
	}

	if (rp->dev_class[0] != 0x00 || rp->dev_class[1] != 0x00 ||
			rp->dev_class[2] != 0x00) {
		test_pre_setup_failed();
		return;
	}

	bthost = hciemu_client_get_host(data->hciemu);
	bthost_notify_ready(bthost, tester_pre_setup_complete);
}

static const uint8_t set_exp_feat_param_mesh[] = {
	0x76, 0x6e, 0xf3, 0xe8, 0x24, 0x5f, 0x05, 0xbf, /* UUID - Mesh */
	0x8d, 0x4d, 0x03, 0x7a, 0xd7, 0x63, 0xe4, 0x2c,
	0x01,						/* Action - enable */
};

static const uint8_t set_exp_feat_rsp_param_mesh[] = {
	0x76, 0x6e, 0xf3, 0xe8, 0x24, 0x5f, 0x05, 0xbf, /* UUID - Mesh */
	0x8d, 0x4d, 0x03, 0x7a, 0xd7, 0x63, 0xe4, 0x2c,
	0x01, 0x00, 0x00, 0x00,			/* Action - enable */
};

static void mesh_exp_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	if (status != MGMT_STATUS_SUCCESS) {
		tester_print("Mesh feature could not be enabled");
		return;
	}

	tester_print("Mesh feature is enabled");
}

static void mesh_exp_feature(struct test_data *data, uint16_t index)
{
	tester_print("Enabling Mesh feature");

	mgmt_send(data->mgmt, MGMT_OP_SET_EXP_FEATURE, index,
			sizeof(set_exp_feat_param_mesh),
			set_exp_feat_param_mesh,
			mesh_exp_callback, NULL, NULL);
}

static void index_added_callback(uint16_t index, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();

	tester_print("Index Added callback");
	tester_print("  Index: 0x%04x", index);

	data->mgmt_index = index;

	mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
			read_info_callback, NULL, NULL);

	tester_warn("Enable management Mesh interface");
	mesh_exp_feature(data, data->mgmt_index);

}

static void index_removed_callback(uint16_t index, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();

	tester_print("Index Removed callback");
	tester_print("  Index: 0x%04x", index);

	if (index != data->mgmt_index)
		return;

	mgmt_unregister_index(data->mgmt, data->mgmt_index);
	mgmt_unregister_index(data->mgmt_alt, data->mgmt_index);

	mgmt_unref(data->mgmt);
	data->mgmt = NULL;

	mgmt_unref(data->mgmt_alt);
	data->mgmt_alt = NULL;

	tester_post_teardown_complete();
}

struct hci_cmd_data {
	uint16_t opcode;
	uint8_t len;
	const void *param;
};

struct hci_entry {
	const struct hci_cmd_data *cmd_data;
};

struct generic_data {
	bool setup_le_states;
	const uint8_t *le_states;
	const uint16_t *setup_settings;
	bool setup_nobredr;
	bool setup_limited_discov;
	const void *setup_exp_feat_param;
	uint16_t setup_expect_hci_command;
	const void *setup_expect_hci_param;
	uint8_t setup_expect_hci_len;
	uint16_t setup_send_opcode;
	const void *setup_send_param;
	uint16_t setup_send_len;
	const struct setup_mgmt_cmd *setup_mgmt_cmd_arr;
	size_t setup_mgmt_cmd_arr_size;
	bool send_index_none;
	const void *setup_discovery_param;
	uint16_t send_opcode;
	const void *send_param;
	uint16_t send_len;
	const void * (*send_func)(uint16_t *len);
	uint8_t expect_status;
	bool expect_ignore_param;
	const void *expect_param;
	uint16_t expect_len;
	const void * (*expect_func)(uint16_t *len);
	uint32_t expect_settings_set;
	uint32_t expect_settings_unset;
	uint16_t expect_alt_ev;
	const void *expect_alt_ev_param;
	bool (*verify_alt_ev_func)(const void *param, uint16_t length);
	uint16_t expect_alt_ev_len;
	uint16_t expect_hci_command;
	const void *expect_hci_param;
	int (*expect_hci_param_check_func)(const void *param, uint16_t length);
	uint8_t expect_hci_len;
	const void * (*expect_hci_func)(uint8_t *len);
	const struct hci_cmd_data *expect_hci_list;
	bool expect_pin;
	uint8_t pin_len;
	const void *pin;
	uint8_t client_pin_len;
	const void *client_pin;
	bool client_enable_ssp;
	uint8_t io_cap;
	uint8_t client_io_cap;
	uint8_t client_auth_req;
	bool reject_confirm;
	bool client_reject_confirm;
	bool just_works;
	bool client_enable_le;
	bool client_enable_sc;
	bool client_enable_adv;
	bool expect_sc_key;
	bool force_power_off;
	bool addr_type_avail;
	bool fail_tolerant;
	uint8_t addr_type;
	bool set_adv;
	const uint8_t *adv_data;
	uint8_t adv_data_len;
};

static const uint8_t set_exp_feat_param_debug[] = {
	0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, /* UUID - Debug */
	0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4,
	0x01,						/* Action - enable */
};

static void debug_exp_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	if (status != MGMT_STATUS_SUCCESS) {
		tester_print("Debug feature could not be enabled");
		return;
	}

	tester_print("Debug feature is enabled");
}

static void debug_exp_feature(struct test_data *data)
{
	tester_print("Enabling Debug feature");

	mgmt_send(data->mgmt, MGMT_OP_SET_EXP_FEATURE, MGMT_INDEX_NONE,
			sizeof(set_exp_feat_param_debug),
			set_exp_feat_param_debug,
			debug_exp_callback, NULL, NULL);
}

static void read_index_list_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;

	tester_print("Read Index List callback");
	tester_print("  Status: %s (0x%02x)", mgmt_errstr(status), status);

	if (status || !param) {
		test_pre_setup_failed();
		return;
	}

	mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
			index_added_callback, NULL, NULL);

	mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
			index_removed_callback, NULL, NULL);

	data->hciemu = hciemu_new(data->hciemu_type);
	if (!data->hciemu) {
		tester_warn("Failed to setup HCI emulation");
		test_pre_setup_failed();
	}

	if (tester_use_debug())
		hciemu_set_debug(data->hciemu, print_debug, "hciemu: ", NULL);

	if (test && test->setup_le_states)
		hciemu_set_central_le_states(data->hciemu, test->le_states);

}

static void test_pre_setup(const void *test_data)
{
	struct test_data *data = tester_get_data();

	data->mgmt = mgmt_new_default();
	if (!data->mgmt) {
		tester_warn("Failed to setup management interface");
		test_pre_setup_failed();
		return;
	}

	data->mgmt_alt = mgmt_new_default();
	if (!data->mgmt_alt) {
		tester_warn("Failed to setup alternate management interface");
		test_pre_setup_failed();

		mgmt_unref(data->mgmt);
		data->mgmt = NULL;
		return;
	}

	if (tester_use_debug()) {
		mgmt_set_debug(data->mgmt, print_debug, "mgmt: ", NULL);
		mgmt_set_debug(data->mgmt_alt, print_debug, "mgmt-alt: ", NULL);
		debug_exp_feature(data);
	}

	mgmt_send(data->mgmt, MGMT_OP_READ_VERSION, MGMT_INDEX_NONE, 0, NULL,
			read_version_callback, NULL, NULL);

	mgmt_send(data->mgmt, MGMT_OP_READ_COMMANDS, MGMT_INDEX_NONE, 0, NULL,
			read_commands_callback, NULL, NULL);

	mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
			read_index_list_callback, NULL, NULL);

	data->sk = -1;
}

#define test_full(name, data, setup, func, timeout, type, version, \
		expected_settings, settings) \
		do { \
			struct test_data *user; \
			user = new0(struct test_data, 1); \
			user->hciemu_type = type; \
			user->test_setup = setup; \
			user->test_data = data; \
			user->expected_version = version; \
			user->expected_manufacturer = 0x05f1; \
			user->expected_supported_settings = expected_settings; \
			user->initial_settings = settings; \
			tester_add_full(name, data, \
					test_pre_setup, test_setup, func, \
					NULL, test_post_teardown, timeout, \
					user, free); \
		} while (0)

#define test_bredrle_full(name, data, setup, func, timeout) \
	test_full(name, data, setup, func, timeout, HCIEMU_TYPE_BREDRLE, \
			0x09, 0x0001beff, 0x00000080)

#define test_bredrle(name, data, setup, func) \
	test_bredrle_full(name, data, setup, func, 2)

#define test_bredr20(name, data, setup, func) \
	test_full(name, data, setup, func, 2, HCIEMU_TYPE_LEGACY, \
			0x03, 0x000110bf, 0x00000080)

#define test_bredr(name, data, setup, func) \
	test_full(name, data, setup, func, 2, HCIEMU_TYPE_BREDR, \
			0x05, 0x000110ff, 0x00000080)

#define test_le_full(name, data, setup, func, timeout) \
	test_full(name, data, setup, func, timeout, HCIEMU_TYPE_LE, \
			0x09, 0x0001be1b, 0x00000200)

#define test_le(name, data, setup, func) \
	test_le_full(name, data, setup, func, 2)

#define test_bredrle50_full(name, data, setup, func, timeout) \
	test_full(name, data, setup, func, timeout, HCIEMU_TYPE_BREDRLE50, \
			0x09, 0x0001beff, 0x00000080)

#define test_bredrle50(name, data, setup, func) \
	test_bredrle50_full(name, data, setup, func, 2)

#define test_hs_full(name, data, setup, func, timeout) \
	test_full(name, data, setup, func, timeout, HCIEMU_TYPE_BREDRLE, \
			0x09, 0x0001bfff, 0x00000080)

#define test_hs(name, data, setup, func) \
	test_hs_full(name, data, setup, func, 2)

static void controller_setup(const void *test_data)
{
	tester_test_passed();
}

struct setup_mgmt_cmd {
	uint8_t send_opcode;
	const void *send_param;
	uint16_t send_len;
};

static bool power_off(uint16_t index)
{
	int sk, err;

	sk = hci_open_dev(index);
	if (sk < 0)
		return false;

	err = ioctl(sk, HCIDEVDOWN, index);

	hci_close_dev(sk);

	if (err < 0)
		return false;

	return true;
}

static void test_condition_complete(struct test_data *data)
{
	data->unmet_conditions--;

	tester_print("Test condition complete, %d left",
			data->unmet_conditions);

	if (data->unmet_conditions > 0)
		return;

	tester_test_passed();
}

static void command_generic_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	const void *expect_param = test->expect_param;
	uint16_t expect_len = test->expect_len;

	tester_print("%s (0x%04x): %s (0x%02x)", mgmt_opstr(test->send_opcode),
			test->send_opcode, mgmt_errstr(status), status);

	if (status != test->expect_status) {
		if (!test->fail_tolerant || !!status != !!test->expect_status) {
			tester_test_abort();
			return;
		}

		tester_warn("Unexpected status got %d expected %d",
				status, test->expect_status);
	}

	if (!test->expect_ignore_param) {
		if (test->expect_func)
			expect_param = test->expect_func(&expect_len);

		if (length != expect_len) {
			tester_warn("Invalid cmd response parameter size %d %d",
					length, expect_len);
			tester_test_failed();
			return;
		}

		if (expect_param && expect_len > 0 &&
				memcmp(param, expect_param, length)) {
			tester_warn("Unexpected cmd response parameter value");
			util_hexdump('>', param, length, print_debug, "");
			util_hexdump('!', expect_param, length, print_debug,
					"");
			tester_test_failed();
			return;
		}
	}

	test_condition_complete(data);
}

static void test_add_condition(struct test_data *data)
{
	data->unmet_conditions++;

	tester_print("Test condition added, total %d", data->unmet_conditions);
}

/* Read HCI commands in the expect_hci_list and add it to the queue
 */
static void add_expect_hci_list(struct test_data *data)
{
	const struct generic_data *test = data->test_data;
	const struct hci_cmd_data *hci_cmd_data;

	/* Initialize the queue */
	data->expect_hci_q = queue_new();

	hci_cmd_data = test->expect_hci_list;
	for (; hci_cmd_data->opcode; hci_cmd_data++) {
		struct hci_entry *entry;

		entry = new0(struct hci_entry, 1);
		entry->cmd_data = hci_cmd_data;
		queue_push_tail(data->expect_hci_q, entry);

		test_add_condition(data);
	}
}

static bool match_hci_cmd_opcode(const void *data, const void *match_data)
{
	const struct hci_entry *entry = data;
	uint16_t opcode = PTR_TO_UINT(match_data);

	return entry->cmd_data->opcode == opcode;
}

static void command_hci_list_callback(uint16_t opcode, const void *param,
		uint8_t length, void *user_data)
{
	struct test_data *data = user_data;
	const struct hci_cmd_data *hci_cmd_data;
	struct hci_entry *entry;
	int ret;

	tester_print("HCI Command 0x%04x length %u", opcode, length);

	entry = queue_find(data->expect_hci_q, match_hci_cmd_opcode,
			UINT_TO_PTR(opcode));
	if (!entry)
		return;

	/* Save the hci cmd data before removing the queue entry */
	hci_cmd_data = entry->cmd_data;

	/* Remove the entry from the queue and free the entry */
	queue_remove(data->expect_hci_q, entry);
	free(entry);

	if (length != hci_cmd_data->len) {
		tester_warn("Invalid parameter size for HCI command");
		tester_test_failed();
		return;
	}

	ret = memcmp(param, hci_cmd_data->param, length);
	if (ret != 0) {
		tester_warn("Unexpected HCI command parameter value:");
		util_hexdump('>', param, length, print_debug, "");
		util_hexdump('!', hci_cmd_data->param, length, print_debug, "");
		tester_test_failed();
		return;
	}

	test_condition_complete(data);
}

static void command_hci_callback(uint16_t opcode, const void *param,
		uint8_t length, void *user_data)
{
	struct test_data *data = user_data;
	const struct generic_data *test = data->test_data;
	const void *expect_hci_param = test->expect_hci_param;
	uint8_t expect_hci_len = test->expect_hci_len;
	int ret;

	tester_print("HCI Command 0x%04x length %u", opcode, length);

	if (opcode != test->expect_hci_command || data->expect_hci_command_done)
		return;

	data->expect_hci_command_done = true;

	if (test->expect_hci_func)
		expect_hci_param = test->expect_hci_func(&expect_hci_len);

	if (length != expect_hci_len) {
		tester_warn("Invalid parameter size for HCI command");
		tester_test_failed();
		return;
	}

	if (test->expect_hci_param_check_func)
		ret = test->expect_hci_param_check_func(param, length);
	else
		ret = memcmp(param, expect_hci_param, length);
	if (ret != 0) {
		tester_warn("Unexpected HCI command parameter value:");
		util_hexdump('>', param, length, print_debug, "");
		util_hexdump('!', expect_hci_param, length, print_debug, "");
		tester_test_failed();
		return;
	}

	test_condition_complete(data);
}

static bool verify_alt_ev(const void *param, uint16_t length)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;

	if (length != test->expect_alt_ev_len) {
		tester_warn("Invalid length %u != %u", length,
				test->expect_alt_ev_len);
		return false;
	}

	if (test->expect_alt_ev_param &&
			memcmp(test->expect_alt_ev_param, param, length)) {
		tester_warn("Event parameters do not match");
		util_hexdump('>', param, length, print_debug, "");
		util_hexdump('!', test->expect_alt_ev_param, length,
				print_debug, "");
		return false;
	}

	return true;
}

static void command_generic_event_alt(uint16_t index, uint16_t length,
		const void *param,
		void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	bool (*verify)(const void *param, uint16_t length);

	tester_print("New %s event received", mgmt_evstr(test->expect_alt_ev));

	mgmt_unregister(data->mgmt_alt, data->mgmt_alt_ev_id);

	if (test->verify_alt_ev_func)
		verify = test->verify_alt_ev_func;
	else
		verify = verify_alt_ev;

	if (!verify(param, length)) {
		tester_warn("Incorrect %s event parameters",
				mgmt_evstr(test->expect_alt_ev));
		tester_test_failed();
		return;
	}

	test_condition_complete(data);
}

static void command_generic_new_settings(uint16_t index, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();

	tester_print("New settings event received");

	mgmt_unregister(data->mgmt, data->mgmt_settings_id);

	tester_test_failed();
}

static void command_generic_new_settings_alt(uint16_t index, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	uint32_t settings;

	if (length != 4) {
		tester_warn("Invalid parameter size for new settings event");
		tester_test_failed();
		return;
	}

	settings = get_le32(param);

	tester_print("New settings 0x%08x received", settings);

	if (test->expect_settings_unset) {
		if ((settings & test->expect_settings_unset) != 0)
			return;
		goto done;
	}

	if (!test->expect_settings_set)
		return;

	if ((settings & test->expect_settings_set) != test->expect_settings_set)
		return;

done:
	tester_print("Unregistering new settings notification");

	mgmt_unregister(data->mgmt_alt, data->mgmt_alt_settings_id);

	test_condition_complete(data);
}

static void test_command_generic(const void *test_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	const void *send_param = test->send_param;
	uint16_t send_len = test->send_len;
	unsigned int id;
	uint16_t index;

	index = test->send_index_none ? MGMT_INDEX_NONE : data->mgmt_index;

	if (test->expect_settings_set || test->expect_settings_unset) {
		tester_print("Registering new settings notification");

		id = mgmt_register(data->mgmt, MGMT_EV_NEW_SETTINGS, index,
				command_generic_new_settings, NULL, NULL);
		data->mgmt_settings_id = id;

		id = mgmt_register(data->mgmt_alt, MGMT_EV_NEW_SETTINGS, index,
				command_generic_new_settings_alt, NULL, NULL);
		data->mgmt_alt_settings_id = id;
		test_add_condition(data);
	}

	if (test->expect_alt_ev) {
		tester_print("Registering %s notification",
				mgmt_evstr(test->expect_alt_ev));
		id = mgmt_register(data->mgmt_alt, test->expect_alt_ev, index,
				command_generic_event_alt, NULL, NULL);
		data->mgmt_alt_ev_id = id;
		test_add_condition(data);
	}

	if (test->expect_hci_command) {
		tester_print("Registering HCI command callback");
		hciemu_add_central_post_command_hook(data->hciemu,
				command_hci_callback, data);
		test_add_condition(data);
	} else if (test->expect_hci_list) {
		/* Use this when it needs to check more than 1 hci command.
		 * However, it cannot be used with expect_hci_command.
		 */
		tester_print("Registering HCI command list callback");
		hciemu_add_central_post_command_hook(data->hciemu,
				command_hci_list_callback, data);
		add_expect_hci_list(data);
	}

	if (test->send_opcode == 0x0000) {
		tester_print("Executing no-op test");
		return;
	}

	tester_print("Sending %s (0x%04x)", mgmt_opstr(test->send_opcode),
			test->send_opcode);

	if (test->send_func)
		send_param = test->send_func(&send_len);

	if (test->force_power_off) {
		mgmt_send_nowait(data->mgmt, test->send_opcode, index,
				send_len, send_param,
				command_generic_callback, NULL, NULL);
		power_off(data->mgmt_index);
	} else {
		mgmt_send(data->mgmt, test->send_opcode, index, send_len,
				send_param, command_generic_callback,
				NULL, NULL);
	}

	test_add_condition(data);
}

static void test_add_setup_condition(struct test_data *data)
{
	data->unmet_setup_conditions++;

	tester_print("Test setup condition added, total %d",
			data->unmet_setup_conditions);
}

static void test_setup_condition_complete(struct test_data *data)
{
	data->unmet_setup_conditions--;

	tester_print("Test setup condition complete, %d left",
			data->unmet_setup_conditions);

	if (data->unmet_setup_conditions > 0)
		return;

	tester_setup_complete();
}

static void client_cmd_complete(uint16_t opcode, uint8_t status,
		const void *param, uint8_t len,
		void *user_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	struct bthost *bthost;

	bthost = hciemu_client_get_host(data->hciemu);

	switch (opcode) {
	case BT_HCI_CMD_WRITE_SCAN_ENABLE:
	case BT_HCI_CMD_LE_SET_ADV_ENABLE:
	case BT_HCI_CMD_LE_SET_EXT_ADV_ENABLE:
		tester_print("Client set connectable: %s (0x%02x)",
				mgmt_errstr(status), status);
		if (!status && test->client_enable_ssp) {
			bthost_write_ssp_mode(bthost, 0x01);
			return;
		}
		break;
	case BT_HCI_CMD_WRITE_SIMPLE_PAIRING_MODE:
		tester_print("Client enable SSP: %s (0x%02x)",
				mgmt_errstr(status), status);
		break;
	default:
		return;
	}

	if (status)
		tester_setup_failed();
	else
		test_setup_condition_complete(data);
}

static void setup_bthost(void)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	struct bthost *bthost;

	bthost = hciemu_client_get_host(data->hciemu);
	bthost_set_cmd_complete_cb(bthost, client_cmd_complete, data);
	test_add_setup_condition(data);

	if (data->hciemu_type == HCIEMU_TYPE_LE ||
			test->client_enable_adv) {
		if (data->hciemu_type >= HCIEMU_TYPE_BREDRLE50) {
			bthost_set_ext_adv_params(bthost);
			bthost_set_ext_adv_enable(bthost, 0x01);
		} else
			bthost_set_adv_enable(bthost, 0x01);
	} else
		bthost_write_scan_enable(bthost, 0x03);
}

static void setup_complete(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	struct test_data *data = tester_get_data();

	if (status != MGMT_STATUS_SUCCESS) {
		tester_setup_failed();
		return;
	}

	tester_print("Initial settings completed");

	if (data->test_setup)
		data->test_setup(data);
	else
		setup_bthost();
}

static void pin_code_request_callback(uint16_t index, uint16_t length,
		const void *param, void *user_data)
{
	const struct mgmt_ev_pin_code_request *ev = param;
	struct test_data *data = user_data;
	const struct generic_data *test = data->test_data;
	struct mgmt_cp_pin_code_reply cp;

	test_condition_complete(data);

	memset(&cp, 0, sizeof(cp));
	memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));

	if (!test->pin) {
		mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_NEG_REPLY,
				data->mgmt_index, sizeof(cp.addr), &cp.addr,
				NULL, NULL, NULL);
		return;
	}

	cp.pin_len = test->pin_len;
	memcpy(cp.pin_code, test->pin, test->pin_len);

	mgmt_reply(data->mgmt, MGMT_OP_PIN_CODE_REPLY, data->mgmt_index,
			sizeof(cp), &cp, NULL, NULL, NULL);
}

static void user_confirm_request_callback(uint16_t index, uint16_t length,
		const void *param,
		void *user_data)
{
	const struct mgmt_ev_user_confirm_request *ev = param;
	struct test_data *data = user_data;
	const struct generic_data *test = data->test_data;
	struct mgmt_cp_user_confirm_reply cp;
	uint16_t opcode;

	memset(&cp, 0, sizeof(cp));
	memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));

	if (test->reject_confirm)
		opcode = MGMT_OP_USER_CONFIRM_NEG_REPLY;
	else
		opcode = MGMT_OP_USER_CONFIRM_REPLY;

	mgmt_reply(data->mgmt, opcode, data->mgmt_index, sizeof(cp), &cp,
			NULL, NULL, NULL);
}

static void user_passkey_request_callback(uint16_t index, uint16_t length,
		const void *param,
		void *user_data)
{
	const struct mgmt_ev_user_passkey_request *ev = param;
	struct test_data *data = user_data;
	const struct generic_data *test = data->test_data;
	struct mgmt_cp_user_passkey_reply cp;

	if (test->just_works) {
		tester_warn("User Passkey Request for just-works case");
		tester_test_failed();
		return;
	}

	memset(&cp, 0, sizeof(cp));
	memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));

	if (test->reject_confirm) {
		mgmt_reply(data->mgmt, MGMT_OP_USER_PASSKEY_NEG_REPLY,
				data->mgmt_index, sizeof(cp.addr), &cp.addr,
				NULL, NULL, NULL);
		return;
	}

	mgmt_reply(data->mgmt, MGMT_OP_USER_PASSKEY_REPLY, data->mgmt_index,
			sizeof(cp), &cp, NULL, NULL, NULL);
}

static void test_setup(const void *test_data)
{
	struct test_data *data = tester_get_data();
	const struct generic_data *test = data->test_data;
	struct bthost *bthost = hciemu_client_get_host(data->hciemu);
	const uint16_t *cmd;

	if (!test)
		goto proceed;

	if (test->pin || test->expect_pin) {
		mgmt_register(data->mgmt, MGMT_EV_PIN_CODE_REQUEST,
				data->mgmt_index, pin_code_request_callback,
				data, NULL);
		test_add_condition(data);
	}

	mgmt_register(data->mgmt, MGMT_EV_USER_CONFIRM_REQUEST,
			data->mgmt_index, user_confirm_request_callback,
			data, NULL);

	mgmt_register(data->mgmt, MGMT_EV_USER_PASSKEY_REQUEST,
			data->mgmt_index, user_passkey_request_callback,
			data, NULL);

	if (test->client_pin)
		bthost_set_pin_code(bthost, test->client_pin,
				test->client_pin_len);

	if (test->client_io_cap)
		bthost_set_io_capability(bthost, test->client_io_cap);

	if (test->client_auth_req)
		bthost_set_auth_req(bthost, test->client_auth_req);
	else if (!test->just_works)
		bthost_set_auth_req(bthost, 0x01);

	if (test->client_reject_confirm)
		bthost_set_reject_user_confirm(bthost, true);

	if (test->client_enable_le)
		bthost_write_le_host_supported(bthost, 0x01);

	if (test->client_enable_sc)
		bthost_set_sc_support(bthost, 0x01);

proceed:
	if (!test || !test->setup_settings) {
		if (data->test_setup)
			data->test_setup(data);
		else
			tester_setup_complete();
		return;
	}

	for (cmd = test->setup_settings; *cmd; cmd++) {
		unsigned char simple_param[] = { 0x01 };
		unsigned char discov_param[] = { 0x01, 0x00, 0x00 };
		unsigned char privacy_param[] = { 0x01,
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
		unsigned char set_exp_feat_param[17] = { 0x00 };
		unsigned char *param = simple_param;
		size_t param_size = sizeof(simple_param);
		mgmt_request_func_t func = NULL;

		/* If this is the last command (next one is 0) request
		 * for a callback.
		 */
		if (!cmd[1])
			func = setup_complete;

		if (*cmd == MGMT_OP_SET_DISCOVERABLE) {
			if (test->setup_limited_discov) {
				discov_param[0] = 0x02;
				discov_param[1] = 0x01;
			}
			param_size = sizeof(discov_param);
			param = discov_param;
		}

		if (*cmd == MGMT_OP_SET_PRIVACY) {
			param_size = sizeof(privacy_param);
			param = privacy_param;
		}

		if (*cmd == MGMT_OP_START_DISCOVERY) {
			if (test->setup_discovery_param)
				memcpy(param, test->setup_discovery_param, 1);
		}

		if (*cmd == MGMT_OP_SET_EXP_FEATURE) {
			if (test->setup_exp_feat_param) {
				memcpy(set_exp_feat_param,
						test->setup_exp_feat_param, 17);
				param_size = sizeof(set_exp_feat_param);
				param = set_exp_feat_param;
			}
		}

		if (*cmd == MGMT_OP_SET_LE && test->setup_nobredr) {
			unsigned char off[] = { 0x00 };

			tester_print("Setup sending %s (0x%04x)",
					mgmt_opstr(*cmd), *cmd);
			mgmt_send(data->mgmt, *cmd, data->mgmt_index,
					param_size, param, NULL, NULL, NULL);
			tester_print("Setup sending %s (0x%04x)",
					mgmt_opstr(MGMT_OP_SET_BREDR),
					MGMT_OP_SET_BREDR);
			mgmt_send(data->mgmt, MGMT_OP_SET_BREDR,
					data->mgmt_index, sizeof(off), off,
					func, data, NULL);
		} else {
			tester_print("Setup sending %s (0x%04x)",
					mgmt_opstr(*cmd), *cmd);
			mgmt_send(data->mgmt, *cmd, data->mgmt_index,
					param_size, param, func, data, NULL);
		}
	}
}

static const struct generic_data enable_mesh_1 = {
	.send_opcode = MGMT_OP_SET_EXP_FEATURE,
	.send_param = set_exp_feat_param_mesh,
	.send_len = sizeof(set_exp_feat_param_mesh),
	.expect_param = set_exp_feat_rsp_param_mesh,
	.expect_len = sizeof(set_exp_feat_rsp_param_mesh),
	.expect_status = MGMT_STATUS_SUCCESS,
};

static const uint8_t set_mesh_receiver_1[] = {
	0x01,
	0x6e, 0x01,
	0xe8, 0x01,
	0x03,
	0x2a, 0x2b, 0x29
};

static const struct generic_data enable_mesh_2 = {
	.send_opcode = MGMT_OP_SET_MESH_RECEIVER,
	.send_param = set_mesh_receiver_1,
	.send_len = sizeof(set_mesh_receiver_1),
	.expect_status = MGMT_STATUS_SUCCESS,
};

static const uint8_t read_mesh_feat_rsp_param_mesh[] = {
	0x00, 0x00,
	0x03,
	0x00
};

static const uint8_t read_mesh_feat_rsp_param_mesh_disabled[] = {
	0x00, 0x00,
	0x00,
	0x00
};

static const struct generic_data read_mesh_features = {
	.send_opcode = MGMT_OP_MESH_READ_FEATURES,
	.expect_param = read_mesh_feat_rsp_param_mesh,
	.expect_len = sizeof(read_mesh_feat_rsp_param_mesh),
	.expect_status = MGMT_STATUS_SUCCESS,
};

static const struct generic_data read_mesh_features_disabled = {
	.send_opcode = MGMT_OP_MESH_READ_FEATURES,
	.expect_param = read_mesh_feat_rsp_param_mesh_disabled,
	.expect_len = sizeof(read_mesh_feat_rsp_param_mesh_disabled),
	.expect_status = MGMT_STATUS_SUCCESS,
};

static const uint8_t send_mesh_1[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	BDADDR_LE_RANDOM,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00,
	0x03,
	0x18,
	0x17, 0x2b, 0x01, 0x00, 0x2d, 0xda, 0x0c, 0x24,
	0x91, 0x53, 0x7a, 0xe2, 0x00, 0x00, 0x00, 0x00,
	0x9d, 0xe2, 0x12, 0x0a, 0x72, 0x50, 0x38, 0xb2
};

static const uint8_t send_mesh_too_long[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	BDADDR_LE_RANDOM,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00,
	0x03,
	0x28,
	0x17, 0x2b, 0x01, 0x00, 0x2d, 0xda, 0x0c, 0x24,
	0x91, 0x53, 0x7a, 0xe2, 0x00, 0x00, 0x00, 0x00,
	0x91, 0x53, 0x7a, 0xe2, 0x00, 0x00, 0x00, 0x00,
	0x91, 0x53, 0x7a, 0xe2, 0x00, 0x00, 0x00, 0x00,
	0x9d, 0xe2, 0x12, 0x0a, 0x72, 0x50, 0x38, 0xb2
};

static const uint8_t mesh_send_rsp_param_mesh[] = {
	0x01
};

static const struct generic_data mesh_send_mesh_1 = {
	.send_opcode = MGMT_OP_MESH_SEND,
	.send_param = send_mesh_1,
	.send_len = sizeof(send_mesh_1),
	.expect_param = mesh_send_rsp_param_mesh,
	.expect_len = sizeof(mesh_send_rsp_param_mesh),
	.expect_status = MGMT_STATUS_SUCCESS,
	.expect_alt_ev = MGMT_EV_MESH_PACKET_CMPLT,
	.expect_alt_ev_param = mesh_send_rsp_param_mesh,
	.expect_alt_ev_len = sizeof(mesh_send_rsp_param_mesh),
	.expect_hci_command = BT_HCI_CMD_LE_SET_ADV_ENABLE,
	.expect_hci_param = mesh_send_rsp_param_mesh,
	.expect_hci_len = sizeof(mesh_send_rsp_param_mesh),
};

static const struct generic_data mesh_send_mesh_too_short = {
	.send_opcode = MGMT_OP_MESH_SEND,
	.send_param = send_mesh_1,
	.send_len = sizeof(send_mesh_1) - 30,
	.expect_status = MGMT_STATUS_INVALID_PARAMS
};

static const struct generic_data mesh_send_mesh_too_long = {
	.send_opcode = MGMT_OP_MESH_SEND,
	.send_param = send_mesh_too_long,
	.send_len = sizeof(send_mesh_too_long),
	.expect_status = MGMT_STATUS_REJECTED
};

static void setup_powered_callback(uint8_t status, uint16_t length,
		const void *param, void *user_data)
{
	if (status != MGMT_STATUS_SUCCESS) {
		tester_setup_failed();
		return;
	}

	tester_print("Controller powered on");

	setup_bthost();
}

static const char set_le_on_param[] = { 0x01 };

static void setup_enable_mesh(const void *test_data)
{
	struct test_data *data = tester_get_data();
	unsigned char param[] = { 0x01 };

	mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
			sizeof(param), param,
			setup_powered_callback, NULL, NULL);

	mgmt_send(data->mgmt, MGMT_OP_SET_LE, data->mgmt_index,
			sizeof(set_le_on_param), set_le_on_param,
			NULL, NULL, NULL);

	mgmt_send(data->mgmt, MGMT_OP_SET_EXP_FEATURE, data->mgmt_index,
			sizeof(set_exp_feat_param_mesh),
			set_exp_feat_param_mesh,
			mesh_exp_callback, NULL, NULL);
}

static const uint8_t send_mesh_cancel_1[] = {
	0x01
};

static const uint8_t send_mesh_cancel_2[] = {
	0x02
};

static const uint8_t mesh_cancel_rsp_param_mesh[] = {
	0x00
};

static const struct generic_data mesh_send_mesh_cancel_1 = {
	.send_opcode = MGMT_OP_MESH_SEND_CANCEL,
	.send_param = send_mesh_cancel_1,
	.send_len = sizeof(send_mesh_cancel_1),
	.expect_status = MGMT_STATUS_SUCCESS,
	.expect_alt_ev = MGMT_EV_MESH_PACKET_CMPLT,
	.expect_alt_ev_param = send_mesh_cancel_1,
	.expect_alt_ev_len = sizeof(send_mesh_cancel_1),

	.expect_hci_command = BT_HCI_CMD_LE_SET_ADV_ENABLE,
	.expect_hci_param = mesh_cancel_rsp_param_mesh,
	.expect_hci_len = sizeof(mesh_cancel_rsp_param_mesh),
};

static const struct generic_data mesh_send_mesh_cancel_2 = {
	.send_opcode = MGMT_OP_MESH_SEND_CANCEL,
	.send_param = send_mesh_cancel_2,
	.send_len = sizeof(send_mesh_cancel_2),
	.expect_status = MGMT_STATUS_SUCCESS,
	.expect_alt_ev = MGMT_EV_MESH_PACKET_CMPLT,
	.expect_alt_ev_param = send_mesh_cancel_2,
	.expect_alt_ev_len = sizeof(send_mesh_cancel_2),

	.expect_hci_command = BT_HCI_CMD_LE_SET_ADV_ENABLE,
	.expect_hci_param = mesh_cancel_rsp_param_mesh,
	.expect_hci_len = sizeof(mesh_cancel_rsp_param_mesh),
};

static void setup_multi_mesh_send(const void *test_data)
{
	struct test_data *data = tester_get_data();

	setup_enable_mesh(test_data);

	mgmt_send(data->mgmt, MGMT_OP_MESH_SEND, data->mgmt_index,
			sizeof(send_mesh_1), send_mesh_1,
			NULL, NULL, NULL);

	mgmt_send(data->mgmt, MGMT_OP_MESH_SEND, data->mgmt_index,
			sizeof(send_mesh_1), send_mesh_1,
			NULL, NULL, NULL);
}

int main(int argc, char *argv[])
{
	tester_init(&argc, &argv);

	test_bredrle("Controller setup",
			NULL, NULL, controller_setup);

	/* LL Mesh Enable
	 * Setup: None
	 * Run: Send Enable Experimental Feature (mesh)
	 * Expect: Mesh feature enable success
	 */
	test_bredrle("Mesh - Enable 1",
			&enable_mesh_1,
			NULL,
			test_command_generic);

	test_bredrle("Mesh - Enable 2",
			&enable_mesh_2,
			setup_enable_mesh,
			test_command_generic);

	test_bredrle("Mesh - Read Mesh Features",
			&read_mesh_features,
			setup_enable_mesh,
			test_command_generic);

	test_bredrle("Mesh - Read Mesh Features - Disabled",
			&read_mesh_features_disabled,
			NULL,
			test_command_generic);

	test_bredrle("Mesh - Send",
			&mesh_send_mesh_1,
			setup_enable_mesh,
			test_command_generic);

	test_bredrle("Mesh - Send - too short",
			&mesh_send_mesh_too_short,
			setup_enable_mesh,
			test_command_generic);

	test_bredrle("Mesh - Send - too long",
			&mesh_send_mesh_too_long,
			setup_enable_mesh,
			test_command_generic);

	test_bredrle("Mesh - Send cancel - 1",
			&mesh_send_mesh_cancel_1,
			setup_multi_mesh_send,
			test_command_generic);

	test_bredrle("Mesh - Send cancel - 2",
			&mesh_send_mesh_cancel_2,
			setup_multi_mesh_send,
			test_command_generic);

	return tester_run();
}