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

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

#include <stdlib.h>
#include <string.h>

#include "monitor/bt.h"
#include "src/shared/hci.h"
#include "src/shared/util.h"
#include "src/shared/ecc.h"
#include "src/shared/tester.h"

struct user_data {
	const void *test_data;
	uint16_t index_ut;
	uint16_t index_lt;
	struct bt_hci *hci_ut;		/* Upper Tester / IUT */
	struct bt_hci *hci_lt;		/* Lower Tester / Reference */

	uint8_t bdaddr_ut[6];
	uint8_t bdaddr_lt[6];
	uint16_t handle_ut;
};

struct le_keys {
	uint8_t remote_sk[32];
	uint8_t local_pk[64];
} key_test_data;

static void swap_buf(const uint8_t *src, uint8_t *dst, uint16_t len)
{
	int i;

	for (i = 0; i < len; i++)
		dst[len - 1 - i] = src[i];
}

static void test_debug(const char *str, void *user_data)
{
	tester_debug("%s", str);
}

static void test_pre_setup_lt_address(const void *data, uint8_t size,
							void *user_data)
{
	struct user_data *user = tester_get_data();
	const struct bt_hci_rsp_read_bd_addr *rsp = data;

	if (rsp->status) {
		tester_warn("Read lower tester address failed (0x%02x)",
								rsp->status);
		tester_pre_setup_failed();
		return;
	}

	memcpy(user->bdaddr_lt, rsp->bdaddr, 6);

	tester_pre_setup_complete();
}

static void test_pre_setup_lt_complete(const void *data, uint8_t size,
							void *user_data)
{
	struct user_data *user = tester_get_data();
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("Reset lower tester failed (0x%02x)", status);
		tester_pre_setup_failed();
		return;
	}

	if (!bt_hci_send(user->hci_lt, BT_HCI_CMD_READ_BD_ADDR, NULL, 0,
				test_pre_setup_lt_address, NULL, NULL)) {
		tester_warn("Failed to read lower tester address");
		tester_pre_setup_failed();
		return;
	}
}

static void test_pre_setup_ut_address(const void *data, uint8_t size,
							void *user_data)
{
	struct user_data *user = tester_get_data();
	const struct bt_hci_rsp_read_bd_addr *rsp = data;

	if (rsp->status) {
		tester_warn("Read upper tester address failed (0x%02x)",
								rsp->status);
		tester_pre_setup_failed();
		return;
	}

	memcpy(user->bdaddr_ut, rsp->bdaddr, 6);

	user->hci_lt = bt_hci_new_user_channel(user->index_lt);
	if (!user->hci_lt) {
		tester_warn("Failed to setup lower tester user channel");
		tester_pre_setup_failed();
		return;
	}

	if (!bt_hci_send(user->hci_lt, BT_HCI_CMD_RESET, NULL, 0,
				test_pre_setup_lt_complete, NULL, NULL)) {
		tester_warn("Failed to reset lower tester");
		tester_pre_setup_failed();
		return;
	}
}

static void test_pre_setup_ut_complete(const void *data, uint8_t size,
							void *user_data)
{
	struct user_data *user = tester_get_data();
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("Reset upper tester failed (0x%02x)", status);
		tester_pre_setup_failed();
		return;
	}

	if (user->index_lt == 0xffff) {
		tester_pre_setup_complete();
		return;
	}

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_READ_BD_ADDR, NULL, 0,
				test_pre_setup_ut_address, NULL, NULL)) {
		tester_warn("Failed to read upper tester address");
		tester_pre_setup_failed();
		return;
	}
}

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

	user->hci_ut = bt_hci_new_user_channel(user->index_ut);
	if (!user->hci_ut) {
		tester_warn("Failed to setup upper tester user channel");
		tester_pre_setup_failed();
		return;
	}

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_RESET, NULL, 0,
				test_pre_setup_ut_complete, NULL, NULL)) {
		tester_warn("Failed to reset upper tester");
		tester_pre_setup_failed();
		return;
	}
}

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

	bt_hci_unref(user->hci_lt);
	user->hci_lt = NULL;

	bt_hci_unref(user->hci_ut);
	user->hci_ut = NULL;

	tester_post_teardown_complete();
}

static void user_data_free(void *data)
{
	struct user_data *user = data;

	free(user);
}

#define test_hci(name, data, setup, func, teardown) \
	do { \
		struct user_data *user; \
		user = calloc(1, sizeof(struct user_data)); \
		if (!user) \
			break; \
		user->test_data = data; \
		user->index_ut = 0; \
		user->index_lt = 1; \
		tester_add_full(name, data, \
				test_pre_setup, setup, func, teardown, \
				test_post_teardown, 30, user, user_data_free); \
	} while (0)

#define test_hci_local(name, data, setup, func) \
	do { \
		struct user_data *user; \
		user = calloc(1, sizeof(struct user_data)); \
		if (!user) \
			break; \
		user->test_data = data; \
		user->index_ut = 0; \
		user->index_lt = 0xffff; \
		tester_add_full(name, data, \
				test_pre_setup, setup, func, NULL, \
				test_post_teardown, 30, user, user_data_free); \
	} while (0)

static void setup_features_complete(const void *data, uint8_t size,
							void *user_data)
{
	const struct bt_hci_rsp_read_local_features *rsp = data;

	if (rsp->status) {
		tester_warn("Failed to get HCI features (0x%02x)", rsp->status);
		tester_setup_failed();
		return;
	}

	tester_setup_complete();
}

static void setup_features(const void *test_data)
{
	struct user_data *user = tester_get_data();

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_READ_LOCAL_FEATURES, NULL, 0,
					setup_features_complete, NULL, NULL)) {
		tester_warn("Failed to send HCI features command");
		tester_setup_failed();
		return;
	}
}

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

static void test_command_complete(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("HCI command failed (0x%02x)", status);
		tester_test_failed();
		return;
	}

	tester_test_passed();
}

static void test_command(uint16_t opcode)
{
	struct user_data *user = tester_get_data();

	if (!bt_hci_send(user->hci_ut, opcode, NULL, 0,
					test_command_complete, NULL, NULL)) {
		tester_warn("Failed to send HCI command 0x%04x", opcode);
		tester_test_failed();
		return;
	}
}

static void test_read_local_version_information(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_LOCAL_VERSION);
}

static void test_read_local_supported_commands(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_LOCAL_COMMANDS);
}

static void test_read_local_supported_features(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_LOCAL_FEATURES);
}

static void test_local_extended_features_complete(const void *data,
						uint8_t size, void *user_data)
{
	const struct bt_hci_rsp_read_local_ext_features *rsp = data;

	if (rsp->status) {
		tester_warn("Failed to get HCI extended features (0x%02x)",
								rsp->status);
		tester_test_failed();
		return;
	}

	tester_test_passed();
}

static void test_read_local_extended_features(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_read_local_ext_features cmd;

	cmd.page = 0x00;

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_READ_LOCAL_EXT_FEATURES,
					&cmd, sizeof(cmd),
					test_local_extended_features_complete,
								NULL, NULL)) {
		tester_warn("Failed to send HCI extended features command");
		tester_test_failed();
		return;
	}
}

static void test_read_buffer_size(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_BUFFER_SIZE);
}

static void test_read_country_code(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_COUNTRY_CODE);
}

static void test_read_bd_addr(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_BD_ADDR);
}

static void test_read_local_supported_codecs(const void *test_data)
{
	test_command(BT_HCI_CMD_READ_LOCAL_CODECS);
}

static void test_le_read_accept_list_size(const void *test_data)
{
	test_command(BT_HCI_CMD_LE_READ_ACCEPT_LIST_SIZE);
}

static void test_le_clear_accept_list(const void *test_data)
{
	test_command(BT_HCI_CMD_LE_CLEAR_ACCEPT_LIST);
}

static void test_le_encrypt_complete(const void *data, uint8_t size,
								void *user_data)
{
	const struct bt_hci_rsp_le_encrypt *rsp = data;
	uint8_t sample[16] = {
		0x7d, 0xf7, 0x6b, 0x0c, 0x1a, 0xb8, 0x99, 0xb3,
		0x3e, 0x42, 0xf0, 0x47, 0xb9, 0x1b, 0x54, 0x6f
	};
	uint8_t enc_data[16];

	if (rsp->status) {
		tester_warn("Failed HCI LE Encrypt (0x%02x)", rsp->status);
		tester_test_failed();
		return;
	}

	swap_buf(rsp->data, enc_data, 16);
	util_hexdump('>', enc_data, 16, test_debug, NULL);

	if (!memcmp(sample, enc_data, 16))
		tester_test_passed();
	else
		tester_test_failed();
}

/* Data are taken from RFC 4493 Test Vectors */
static void test_le_encrypt(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_le_encrypt cmd;
	uint8_t key[16] = {
		0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
		0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
	};
	uint8_t plaintext[16] = { 0 };

	/* Swap bytes since our interface has LE interface, opposed to
	 * common crypto interface
	 */
	swap_buf(key, cmd.key, 16);
	swap_buf(plaintext, cmd.plaintext, 16);

	util_hexdump('<', cmd.key, 16, test_debug, NULL);
	util_hexdump('<', cmd.plaintext, 16, test_debug, NULL);

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_ENCRYPT, &cmd, sizeof(cmd),
					test_le_encrypt_complete, NULL, NULL)) {
		tester_warn("Failed to send HCI LE Encrypt command");
		tester_test_failed();
		return;
	}

}

static void test_le_rand(const void *test_data)
{
	test_command(BT_HCI_CMD_LE_RAND);
}

static void test_le_read_local_pk_complete(const void *data, uint8_t size,
								void *user_data)
{
	const uint8_t *event = data;
	const struct bt_hci_evt_le_read_local_pk256_complete *evt;
	struct le_keys *keys = user_data;

	if (*event != BT_HCI_EVT_LE_READ_LOCAL_PK256_COMPLETE) {
		tester_warn("Failed Read Local PK256 command");
		tester_test_failed();
		return;
	}

	evt = (void *)(event + 1);
	if (evt->status) {
		tester_warn("HCI Read Local PK complete failed (0x%02x)",
								evt->status);
		tester_test_failed();
		return;
	}

	memcpy(keys->local_pk, evt->local_pk256, 64);

	util_hexdump('>', evt->local_pk256, 64, test_debug, NULL);

	tester_test_passed();
}

static void test_le_read_local_pk_status(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("Failed to send Read Local PK256 cmd (0x%02x)", status);
		tester_test_failed();
		return;
	}
}

static void test_le_read_local_pk(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_set_event_mask sem;
	struct bt_hci_cmd_le_set_event_mask lsem;

	bt_hci_register(user->hci_ut, BT_HCI_EVT_LE_META_EVENT,
				test_le_read_local_pk_complete,
				(void *)test_data, NULL);

	memset(sem.mask, 0, 8);
	sem.mask[1] |= 0x20;	/* Command Complete */
	sem.mask[1] |= 0x40;	/* Command Status */
	sem.mask[7] |= 0x20;	/* LE Meta */

	bt_hci_send(user->hci_ut, BT_HCI_CMD_SET_EVENT_MASK,
					&sem, sizeof(sem), NULL, NULL, NULL);

	memset(lsem.mask, 0, 8);
	lsem.mask[0] |= 0x80;	/* LE Read Local P-256 Public Key Complete */

	bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_SET_EVENT_MASK,
					&lsem, sizeof(lsem), NULL, NULL, NULL);

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_READ_LOCAL_PK256, NULL,
				0, test_le_read_local_pk_status,
				NULL, NULL)) {
		tester_warn("Failed to send HCI LE Read Local PK256 command");
		tester_test_failed();
		return;
	}
}

static void setup_le_read_local_pk_complete(const void *data, uint8_t size,
								void *user_data)
{
	const uint8_t *event = data;
	const struct bt_hci_evt_le_read_local_pk256_complete *evt;
	struct le_keys *keys = user_data;

	if (*event != BT_HCI_EVT_LE_READ_LOCAL_PK256_COMPLETE) {
		tester_warn("Failed Read Local PK256 command");
		tester_setup_failed();
		return;
	}

	evt = (void *)(event + 1);
	if (evt->status) {
		tester_warn("HCI Read Local PK complete failed (0x%02x)",
								evt->status);
		tester_setup_failed();
		return;
	}

	memcpy(keys->local_pk, evt->local_pk256, 64);

	util_hexdump('>', evt->local_pk256, 64, test_debug, NULL);

	tester_setup_complete();
}

static void setup_le_read_local_pk_status(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("Failed to send DHKey gen cmd (0x%02x)", status);
		tester_setup_failed();
		return;
	}
}

static void setup_le_generate_dhkey(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_set_event_mask sem;
	struct bt_hci_cmd_le_set_event_mask lsem;

	bt_hci_register(user->hci_ut, BT_HCI_EVT_LE_META_EVENT,
				setup_le_read_local_pk_complete,
				(void *)test_data, NULL);

	memset(sem.mask, 0, 8);
	sem.mask[1] |= 0x20;	/* Command Complete */
	sem.mask[1] |= 0x40;	/* Command Status */
	sem.mask[7] |= 0x20;	/* LE Meta */

	bt_hci_send(user->hci_ut, BT_HCI_CMD_SET_EVENT_MASK,
					&sem, sizeof(sem), NULL, NULL, NULL);

	memset(lsem.mask, 0, 8);
	lsem.mask[0] |= 0x80;	/* LE Read Local P-256 Public Key Complete */
	lsem.mask[1] |= 0x01;	/* LE Generate DHKey Complete */

	bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_SET_EVENT_MASK,
					&lsem, sizeof(lsem), NULL, NULL, NULL);

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_READ_LOCAL_PK256, NULL,
				0, setup_le_read_local_pk_status,
				NULL, NULL)) {
		tester_warn("Failed to send HCI LE Read Local PK256 command");
		tester_setup_failed();
		return;
	}
}

static void test_le_generate_dhkey_complete(const void *data, uint8_t size,
								void *user_data)
{
	const uint8_t *event = data;
	const struct bt_hci_evt_le_generate_dhkey_complete *evt;
	struct le_keys *keys = user_data;
	uint8_t dhkey[32];

	if (*event != BT_HCI_EVT_LE_GENERATE_DHKEY_COMPLETE) {
		tester_warn("Failed DHKey generation command");
		tester_test_failed();
		return;
	}

	evt = (void *)(event + 1);
	if (evt->status) {
		tester_warn("HCI Generate DHKey complete failed (0x%02x)",
								evt->status);
		tester_test_failed();
		return;
	}

	util_hexdump('>', evt->dhkey, 32, test_debug, NULL);


	util_hexdump('S', keys->remote_sk, 32, test_debug, NULL);
	util_hexdump('P', keys->local_pk, 64, test_debug, NULL);

	/* Generate DHKey ourself with local public key and remote
	 * private key we got when generated public / private key
	 * pair for BT_HCI_CMD_LE_GENERATE_DHKEY argument.
	 */
	ecdh_shared_secret(keys->local_pk, keys->remote_sk, dhkey);

	util_hexdump('D', dhkey, 32, test_debug, NULL);

	if (!memcmp(dhkey, evt->dhkey, 32))
		tester_test_passed();
	else
		tester_test_failed();
}

static void test_le_generate_dhkey_status(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("Failed to send DHKey gen cmd (0x%02x)", status);
		tester_test_failed();
		return;
	}
}

static void test_le_generate_dhkey(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_le_generate_dhkey cmd;
	struct le_keys *keys = (void *)test_data;

	ecc_make_key(cmd.remote_pk256, keys->remote_sk);

	/* Unregister handler for META event */
	bt_hci_unregister(user->hci_ut, 1);

	bt_hci_register(user->hci_ut, BT_HCI_EVT_LE_META_EVENT,
				test_le_generate_dhkey_complete, keys,
				NULL);

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_GENERATE_DHKEY, &cmd,
				sizeof(cmd), test_le_generate_dhkey_status,
				NULL, NULL)) {
		tester_warn("Failed to send HCI LE Encrypt command");
		tester_test_failed();
		return;
	}

}

static void test_inquiry_complete(const void *data, uint8_t size,
							void *user_data)
{
	const struct bt_hci_evt_inquiry_complete *evt = data;

	if (evt->status) {
		tester_warn("HCI inquiry complete failed (0x%02x)",
							evt->status);
		tester_test_failed();
		return;
	}

	tester_test_passed();
}

static void test_inquiry_status(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("HCI inquiry command failed (0x%02x)", status);
		tester_test_failed();
		return;
	}
}

static void test_inquiry_liac(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_inquiry cmd;

	bt_hci_register(user->hci_ut, BT_HCI_EVT_INQUIRY_COMPLETE,
					test_inquiry_complete, NULL, NULL);

	cmd.lap[0] = 0x00;
	cmd.lap[1] = 0x8b;
	cmd.lap[2] = 0x9e;
	cmd.length = 0x08;
	cmd.num_resp = 0x00;

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_INQUIRY, &cmd, sizeof(cmd),
					test_inquiry_status, NULL, NULL)) {
		tester_warn("Failed to send HCI inquiry command");
		tester_test_failed();
		return;
	}
}

static void setup_lt_connectable_complete(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("Failed to set HCI scan enable (0x%02x)", status);
		tester_setup_failed();
		return;
	}

	tester_setup_complete();
}

static void setup_lt_connect_request_accept(const void *data, uint8_t size,
							void *user_data)
{
	struct user_data *user = tester_get_data();
	const struct bt_hci_evt_conn_request *evt = data;
	struct bt_hci_cmd_accept_conn_request cmd;

	memcpy(cmd.bdaddr, evt->bdaddr, 6);
	cmd.role = 0x01;

	if (!bt_hci_send(user->hci_lt, BT_HCI_CMD_ACCEPT_CONN_REQUEST,
					&cmd, sizeof(cmd), NULL, NULL, NULL)) {
		tester_warn("Failed to send HCI accept connection command");
		return;
	}
}

static void setup_lt_connectable(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_write_scan_enable cmd;

	bt_hci_register(user->hci_lt, BT_HCI_EVT_CONN_REQUEST,
				setup_lt_connect_request_accept, NULL, NULL);

	cmd.enable = 0x02;

	if (!bt_hci_send(user->hci_lt, BT_HCI_CMD_WRITE_SCAN_ENABLE,
				&cmd, sizeof(cmd),
				setup_lt_connectable_complete, NULL, NULL)) {
		tester_warn("Failed to send HCI scan enable command");
		tester_setup_failed();
		return;
	}
}

static void test_create_connection_disconnect(void *user_data)
{
	tester_test_passed();
}

static void test_create_connection_complete(const void *data, uint8_t size,
							void *user_data)
{
	struct user_data *user = tester_get_data();
	const struct bt_hci_evt_conn_complete *evt = data;

	if (evt->status) {
		tester_warn("HCI create connection complete failed (0x%02x)",
								evt->status);
		tester_test_failed();
		return;
	}

	user->handle_ut = le16_to_cpu(evt->handle);

	tester_wait(2, test_create_connection_disconnect, NULL);
}

static void test_create_connection_status(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("HCI create connection command failed (0x%02x)",
								status);
		tester_test_failed();
		return;
	}
}

static void test_create_connection(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_create_conn cmd;

	bt_hci_register(user->hci_ut, BT_HCI_EVT_CONN_COMPLETE,
				test_create_connection_complete, NULL, NULL);

	memcpy(cmd.bdaddr, user->bdaddr_lt, 6);
	cmd.pkt_type = cpu_to_le16(0x0008);
	cmd.pscan_rep_mode = 0x02;
	cmd.pscan_mode = 0x00;
	cmd.clock_offset = cpu_to_le16(0x0000);
	cmd.role_switch = 0x01;

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_CREATE_CONN,
						&cmd, sizeof(cmd),
						test_create_connection_status,
								NULL, NULL)) {
		tester_warn("Failed to send HCI create connection command");
		tester_test_failed();
		return;
	}
}

static void teardown_timeout(void *user_data)
{
	tester_teardown_complete();
}

static void teardown_disconnect_status(const void *data, uint8_t size,
							void *user_data)
{
	uint8_t status = *((uint8_t *) data);

	if (status) {
		tester_warn("HCI disconnect failed (0x%02x)", status);
		tester_teardown_failed();
		return;
	}

	tester_wait(1, teardown_timeout, NULL);
}

static void teardown_connection(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_disconnect cmd;

	cmd.handle = cpu_to_le16(user->handle_ut);
	cmd.reason = 0x13;

	if (!bt_hci_send(user->hci_ut, BT_HCI_CMD_DISCONNECT,
						&cmd, sizeof(cmd),
						teardown_disconnect_status,
								NULL, NULL)) {
		tester_warn("Failed to send HCI disconnect command");
		tester_test_failed();
		return;
	}
}

static void test_adv_report(const void *data, uint8_t size, void *user_data)
{
	struct user_data *user = tester_get_data();
	uint8_t subevent = *((uint8_t *) data);
	const struct bt_hci_evt_le_adv_report *lar = data + 1;

	switch (subevent) {
	case BT_HCI_EVT_LE_ADV_REPORT:
		if (!memcmp(lar->addr, user->bdaddr_ut, 6))
			tester_setup_complete();
		break;
	}
}

static void setup_advertising_initiated(const void *test_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_set_event_mask sem;
	struct bt_hci_cmd_le_set_event_mask lsem;
	struct bt_hci_cmd_le_set_scan_enable lsse;
	struct bt_hci_cmd_le_set_adv_parameters lsap;
	struct bt_hci_cmd_le_set_adv_enable lsae;

	bt_hci_register(user->hci_lt, BT_HCI_EVT_LE_META_EVENT,
					test_adv_report, NULL, NULL);

	memset(sem.mask, 0, 8);
	sem.mask[1] |= 0x20;	/* Command Complete */
	sem.mask[1] |= 0x40;	/* Command Status */
	sem.mask[7] |= 0x20;	/* LE Meta */

	bt_hci_send(user->hci_lt, BT_HCI_CMD_SET_EVENT_MASK,
					&sem, sizeof(sem), NULL, NULL, NULL);

	memset(lsem.mask, 0, 8);
	lsem.mask[0] |= 0x02;	/* LE Advertising Report */

	bt_hci_send(user->hci_lt, BT_HCI_CMD_LE_SET_EVENT_MASK,
					&lsem, sizeof(lsem), NULL, NULL, NULL);

	lsse.enable = 0x01;
	lsse.filter_dup = 0x00;

	bt_hci_send(user->hci_lt, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
					&lsse, sizeof(lsse), NULL, NULL, NULL);

	lsap.min_interval = cpu_to_le16(0x0800);
	lsap.max_interval = cpu_to_le16(0x0800);
	lsap.type = 0x03;
	lsap.own_addr_type = 0x00;
	lsap.direct_addr_type = 0x00;
	memset(lsap.direct_addr, 0, 6);
	lsap.channel_map = 0x07;
	lsap.filter_policy = 0x00;

	bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_SET_ADV_PARAMETERS,
					&lsap, sizeof(lsap), NULL, NULL, NULL);

	lsae.enable = 0x01;

	bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_SET_ADV_ENABLE,
					&lsae, sizeof(lsae), NULL, NULL, NULL);
}

static void test_reset_in_advertising_state_timeout(void *user_data)
{
	struct user_data *user = tester_get_data();
	struct bt_hci_cmd_le_set_adv_enable lsae;
	struct bt_hci_cmd_le_set_scan_enable lsse;

	lsae.enable = 0x00;

	bt_hci_send(user->hci_ut, BT_HCI_CMD_LE_SET_ADV_ENABLE,
					&lsae, sizeof(lsae), NULL, NULL, NULL);

	lsse.enable = 0x00;
	lsse.filter_dup = 0x00;

	bt_hci_send(user->hci_lt, BT_HCI_CMD_LE_SET_SCAN_ENABLE,
					&lsse, sizeof(lsse), NULL, NULL, NULL);

	tester_test_passed();
}

static void test_reset_in_advertising_state(const void *test_data)
{
	struct user_data *user = tester_get_data();

	bt_hci_send(user->hci_ut, BT_HCI_CMD_RESET, NULL, 0, NULL, NULL, NULL);

	tester_wait(5, test_reset_in_advertising_state_timeout, NULL);
}

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

	test_hci_local("Reset", NULL, NULL, test_reset);

	test_hci_local("Read Local Version Information", NULL, NULL,
				test_read_local_version_information);
	test_hci_local("Read Local Supported Commands", NULL, NULL,
				test_read_local_supported_commands);
	test_hci_local("Read Local Supported Features", NULL, NULL,
				test_read_local_supported_features);
	test_hci_local("Read Local Extended Features", NULL,
				setup_features,
				test_read_local_extended_features);
	test_hci_local("Read Buffer Size", NULL, NULL,
				test_read_buffer_size);
	test_hci_local("Read Country Code", NULL, NULL,
				test_read_country_code);
	test_hci_local("Read BD_ADDR", NULL, NULL,
				test_read_bd_addr);
	test_hci_local("Read Local Supported Codecs", NULL, NULL,
				test_read_local_supported_codecs);

	test_hci_local("LE Read Accept List Size", NULL, NULL,
				test_le_read_accept_list_size);
	test_hci_local("LE Clear Accept List", NULL, NULL,
				test_le_clear_accept_list);
	test_hci_local("LE Encrypt", NULL, NULL,
				test_le_encrypt);
	test_hci_local("LE Rand", NULL, NULL,
				test_le_rand);
	test_hci_local("LE Read Local PK", &key_test_data, NULL,
				test_le_read_local_pk);
	test_hci_local("LE Generate DHKey", &key_test_data,
				setup_le_generate_dhkey,
				test_le_generate_dhkey);

	test_hci_local("Inquiry (LIAC)", NULL, NULL, test_inquiry_liac);

	test_hci("Create Connection", NULL,
				setup_lt_connectable,
				test_create_connection,
				teardown_connection);

	test_hci("TP/DSU/BV-02-C Reset in Advertising State", NULL,
				setup_advertising_initiated,
				test_reset_in_advertising_state, NULL);

	return tester_run();
}