summaryrefslogtreecommitdiff
path: root/tools/ioctl-tester.c
blob: 098b58608500bb5b60c8efc8d4c90fcc380d56d9 (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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2022  Intel Corporation.
 *
 */

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

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <stdbool.h>

#include <sys/ioctl.h>
#include <sys/socket.h>

#include <glib.h>

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

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

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

struct test_data {
	const void *test_data;
	int sock_fd;
	struct hciemu *hciemu;
	enum hciemu_type hciemu_type;
	uint8_t client_num;
	uint16_t hci_dev_id;

	struct mgmt *mgmt;
	uint16_t mgmt_index;
	struct mgmt *mgmt_alt;
	unsigned int mgmt_alt_ev_id;

	uint16_t handle;
	uint16_t acl_handle;
	GIOChannel *io;
	unsigned int io_id[2];
	int step;
	bool reconnect;

	int unmet_conditions;
};

struct ioctl_data {
	uint32_t cmd;
	const uint32_t opt;
	const void *param;
	int (*cmd_param_func)(void *param, uint32_t *length);
	int expected_ioctl_err;
	const void *block_bdaddr;
	const void *expected_data;
	int (*expect_data_check_func)(const void *param, uint32_t length);
};

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

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

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

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

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 int update_hci_dev_id(struct test_data *data)
{
	struct hci_dev_list_req *dl;
	struct hci_dev_req *dr;
	int ret = 0;

	dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(uint16_t));
	if (!dl)
		return -ENOMEM;

	dl->dev_num = HCI_MAX_DEV;
	dr = dl->dev_req;

	if (ioctl(data->sock_fd, HCIGETDEVLIST, (void *) dl) < 0) {
		ret = -EIO;
		goto exit;
	}

	if (dl->dev_num != 1) {
		tester_warn("dev num mismatch returned %d:expected 1",
								dl->dev_num);
		ret = -ENODEV;
		goto exit;
	}

	data->hci_dev_id = dr->dev_id;
	tester_print("HCI device id: %d", data->hci_dev_id);

exit:
	free(dl);
	return ret;
}

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;

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

	if (status || !param) {
		tester_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)) {
		tester_pre_setup_failed();
		return;
	}

	tester_pre_setup_complete();
}

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);
}

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();
}

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();

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

	if (status || !param) {
		tester_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");
		tester_pre_setup_failed();
	}

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

	tester_print("New hciemu instance created");

	data->sock_fd = hci_open_dev(0);
	if (data->sock_fd < 0) {
		tester_warn("Failed to open socket for ioctl");
		tester_pre_setup_failed();
		return;
	}

	update_hci_dev_id(data);
}

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 mgmt interface");
		tester_pre_setup_failed();
		return;
	}

	data->mgmt_alt = mgmt_new_default();
	if (!data->mgmt_alt) {
		tester_warn("Failed to setup alternate management interface");
		tester_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);
	}

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

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

	if (data->sock_fd >= 0) {
		tester_print("Socket closed");
		hci_close_dev(data->sock_fd);
	}

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

static void test_data_free(void *test_data)
{
	struct test_data *data = test_data;

	// TODO: free any data allocated during pre-setup

	free(data);
}

#define test_ioctl_full(name, data, setup, func, num) \
	do { \
		struct test_data *user; \
		user = new0(struct test_data, 1); \
		if (!user) \
			break; \
		user->hciemu_type = HCIEMU_TYPE_BREDRLE; \
		user->test_data = data; \
		user->client_num = num; \
		tester_add_full(name, data, \
				test_pre_setup, setup, func, NULL, \
				test_post_teardown, 2, user, test_data_free); \
	} while (0)

#define test_ioctl(name, data, setup, func) \
	test_ioctl_full(name, data, setup, func, 1)

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");

	tester_setup_complete();
}

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

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

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

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

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

static void setup_add_block_bdaddr(const void *test_data)
{
	struct test_data *data = tester_get_data();
	const struct ioctl_data *ioctl_data = data->test_data;

	if (!ioctl_data->block_bdaddr) {
		tester_warn("Invalid test data: block bdaddr");
		tester_setup_failed();
		return;
	}

	if (ioctl(data->sock_fd, HCIBLOCKADDR, ioctl_data->block_bdaddr) < 0) {
		tester_warn("Failed to add block bdaddr");
		tester_setup_failed();
		return;
	}

	tester_print("Added block BDADDR");

	tester_setup_complete();
}

static int conn_list_empty_check_func(const void *param, uint32_t length)
{
	struct test_data *data = tester_get_data();
	const struct ioctl_data *ioctl_data = data->test_data;
	const struct hci_conn_list_req *cl_input = ioctl_data->expected_data;
	const struct hci_conn_list_req *cl = param;

	if (cl->conn_num != cl_input->conn_num)
		return -1;

	return 0;
}

static int conn_info_cmd_param_func(void *param, uint32_t *length)
{
	struct test_data *data = tester_get_data();
	const struct ioctl_data *ioctl_data = data->test_data;
	const struct hci_conn_info_req *cr_input = ioctl_data->param;
	struct hci_conn_info_req *cr = param;

	memcpy(&cr->bdaddr, &cr_input->bdaddr, sizeof(bdaddr_t));
	cr->type = cr_input->type;

	return 0;
}

static int auth_info_cmd_param_func(void *param, uint32_t *length)
{
	struct test_data *data = tester_get_data();
	const struct ioctl_data *ioctl_data = data->test_data;
	const struct hci_auth_info_req *ar_input = ioctl_data->param;
	struct hci_auth_info_req *ar = param;

	memcpy(&ar->bdaddr, &ar_input->bdaddr, sizeof(bdaddr_t));
	if (ar_input->type)
		ar->type = ar_input->type;

	return 0;
}

static const struct ioctl_data dev_down = {
	.cmd = HCIDEVDOWN,
};

static const struct hci_dev_list_req dev_list_1 = {
	.dev_num = 0x01,
	.dev_req = {{
		.dev_id = 0x00,
		.dev_opt = 0x04,
	}},
};

static const struct ioctl_data dev_list = {
	.cmd = HCIGETDEVLIST,
	.expected_data = (void *)&dev_list_1,
};

static const struct hci_dev_list_req dev_list_invalid_1_param = {
	.dev_num = 0x00,
};

static const struct ioctl_data dev_list_invalid_1 = {
	.cmd = HCIGETDEVLIST,
	.param = (void *)&dev_list_invalid_1_param,
	.expected_ioctl_err = EINVAL,
};

static const struct ioctl_data dev_info = {
	.cmd = HCIGETDEVINFO,
};

static const struct ioctl_data reset_stat = {
	.cmd = HCIDEVRESTAT,
};

static const struct ioctl_data set_link_mode_master = {
	.cmd = HCISETLINKMODE,
	.opt = HCI_LM_MASTER,
};

static const struct ioctl_data set_link_mode_accept = {
	.cmd = HCISETLINKMODE,
	.opt = HCI_LM_ACCEPT,
};

static const struct ioctl_data set_pkt_type_dm = {
	.cmd = HCISETPTYPE,
	.opt = HCI_DM1 | HCI_DM3 | HCI_DM5,
};

static const struct ioctl_data set_pkt_type_dh = {
	.cmd = HCISETPTYPE,
	.opt = HCI_DH1 | HCI_DH3 | HCI_DH5,
};

static const struct ioctl_data set_pkt_type_hv = {
	.cmd = HCISETPTYPE,
	.opt = HCI_HV1 | HCI_HV2 | HCI_HV3,
};

static const struct ioctl_data set_pkt_type_2dh = {
	.cmd = HCISETPTYPE,
	.opt = HCI_2DH1 | HCI_2DH3 | HCI_2DH5,
};

static const struct ioctl_data set_pkt_type_3dh = {
	.cmd = HCISETPTYPE,
	.opt = HCI_3DH1 | HCI_3DH3 | HCI_3DH5,
};

static const struct ioctl_data set_pkt_type_all = {
	.cmd = HCISETPTYPE,
	.opt = HCI_DM1 | HCI_DM3 | HCI_DM5 | HCI_DH1 | HCI_DH3 | HCI_DH5 |
	       HCI_HV1 | HCI_HV2 | HCI_HV3 | HCI_2DH1 | HCI_2DH3 | HCI_2DH5 |
	       HCI_3DH1 | HCI_3DH3 | HCI_3DH5,
};

static const struct ioctl_data set_acl_mtu_1 = {
	.cmd = HCISETACLMTU,
	.opt = 0x1 | (0x3FE << 16),
};

static const struct ioctl_data set_acl_mtu_2 = {
	.cmd = HCISETACLMTU,
	.opt = 0x4 | (0x63 << 16),
};

static const struct ioctl_data set_sco_mtu_1 = {
	.cmd = HCISETSCOMTU,
	.opt = 0x1 | (0x3FE << 16),
};

static const struct ioctl_data set_sco_mtu_2 = {
	.cmd = HCISETSCOMTU,
	.opt = 0x4 | (0x63 << 16),
};

static const uint8_t bdaddr1[] = {
	0x11, 0x22, 0x33, 0x44, 0x55, 0x66
};

static const struct ioctl_data block_bdaddr_success = {
	.cmd = HCIBLOCKADDR,
	.param = bdaddr1,
};

static const struct ioctl_data block_bdaddr_fail = {
	.cmd = HCIBLOCKADDR,
	.param = bdaddr1,
	.expected_ioctl_err = EEXIST,
	.block_bdaddr = bdaddr1,
};

static const struct ioctl_data unblock_bdaddr_success = {
	.cmd = HCIUNBLOCKADDR,
	.param = bdaddr1,
	.block_bdaddr = bdaddr1,
};

static const struct ioctl_data unblock_bdaddr_fail = {
	.cmd = HCIUNBLOCKADDR,
	.param = bdaddr1,
	.expected_ioctl_err = ENOENT,
};

static const struct hci_conn_list_req conn_list_empty = {
	.dev_id = 0x00,
	.conn_num = 0x00,
};

static const struct ioctl_data conn_list_no_conn = {
	.cmd = HCIGETCONNLIST,
	.expected_data = (void *)&conn_list_empty,
	.expect_data_check_func = conn_list_empty_check_func,
};

static const struct hci_conn_list_req conn_list_req_1 = {
	.dev_id = 0x00,
	.conn_num = 0x01,
	.conn_info = {{
		.handle = 0x002a,
		.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
		.type = 0x01,
		.out = 0x00,
		.state = 0x0001,
		.link_mode = 0x00000000,
	}},
};

static const struct ioctl_data conn_list = {
	.cmd = HCIGETCONNLIST,
	.expected_data = (void *)&conn_list_req_1,
};

static const struct hci_conn_info_req conn_info_req = {
	.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
	.type = ACL_LINK,
	.conn_info = {{
		.handle = 0x002a,
		.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
		.type = 0x01,
		.out = 0x00,
		.state = 0x0001,
		.link_mode = 0x00000000,
	}},
};

static const struct hci_conn_info_req conn_info_req_acl = {
	.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
	.type = ACL_LINK,
};

static const struct hci_conn_info_req conn_info_req_sco = {
	.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
	.type = SCO_LINK,
};

static const struct ioctl_data conn_info = {
	.cmd = HCIGETCONNINFO,
	.param = (void *)&conn_info_req_acl,
	.cmd_param_func = conn_info_cmd_param_func,
	.expected_data = (void *)&conn_info_req,
};

static const struct ioctl_data conn_info_no_conn = {
	.cmd = HCIGETCONNINFO,
	.param = (void *)&conn_info_req_acl,
	.expected_ioctl_err = ENOENT,
	.cmd_param_func = conn_info_cmd_param_func,
};

static const struct ioctl_data conn_info_wrong_type = {
	.cmd = HCIGETCONNINFO,
	.param = (void *)&conn_info_req_sco,
	.expected_ioctl_err = ENOENT,
	.cmd_param_func = conn_info_cmd_param_func,
};

static const struct hci_auth_info_req auth_info_req = {
	.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
};

static const struct hci_auth_info_req auth_info_connected = {
	.bdaddr = {{ 0x00, 0x00, 0x01, 0x01, 0xaa, 0x00 }},
	.type = 0x04,
};

static const struct ioctl_data auth_info_no_conn = {
	.cmd = HCIGETAUTHINFO,
	.param = (void *)&auth_info_req,
	.expected_ioctl_err = ENOENT,
	.cmd_param_func = auth_info_cmd_param_func,
};

static const struct ioctl_data auth_info = {
	.cmd = HCIGETAUTHINFO,
	.param = (void *)&auth_info_req,
	.cmd_param_func = auth_info_cmd_param_func,
	.expected_data = (void *)&auth_info_connected,
};

/* Allocate the command request parameters based on the command.
 * returns the allocated request buffer and its length
 */
static int test_alloc_cmd_param(void **req, uint32_t *req_len)
{
	struct test_data *data = tester_get_data();
	const struct ioctl_data *ioctl_data = data->test_data;
	struct hci_dev_req *dr = NULL;
	struct hci_dev_info *di = NULL;
	struct hci_dev_list_req *dl = NULL;
	struct hci_conn_list_req *cl = NULL;
	struct hci_conn_info *ci = NULL;
	struct hci_conn_info_req *cr = NULL;
	struct hci_auth_info_req *ar = NULL;
	bdaddr_t *bdaddr = NULL;
	uint32_t len;

	switch (ioctl_data->cmd) {
	case HCISETAUTH:
	case HCISETENCRYPT:
	case HCISETLINKMODE:
	case HCISETPTYPE:
	case HCISETACLMTU:
	case HCISETSCOMTU:
		len = sizeof(*dr);
		dr = malloc(len);
		if (!dr)
			return -ENOMEM;
		memset(dr, 0, len);
		dr->dev_id = data->hci_dev_id;
		dr->dev_opt = ioctl_data->opt;
		*req = dr;
		*req_len = len;
		break;
	case HCIGETDEVINFO:
		len = sizeof(*di);
		di = malloc(len);
		if (!di)
			return -ENOMEM;
		memset(di, 0, len);
		di->dev_id = data->hci_dev_id;
		*req = di;
		*req_len = len;
		break;
	case HCIGETDEVLIST:
		len = sizeof(*dr) + sizeof(uint16_t);
		dl = malloc(len);
		if (!dl)
			return -ENOMEM;
		memset(dl, 0, len);
		dl->dev_num = 1;
		*req = dl;
		*req_len = len;
		break;
	case HCIGETCONNLIST:
		len = sizeof(*cl) + sizeof(*ci);
		cl = malloc(len);
		if (!cl)
			return -ENOMEM;
		memset(cl, 0, len);
		cl->dev_id = data->hci_dev_id;
		cl->conn_num = 1;
		*req = cl;
		*req_len = len;
		break;
	case HCIGETCONNINFO:
		len = sizeof(*cr) + sizeof(*ci);
		cr = malloc(len);
		if (!cr)
			return -ENOMEM;
		memset(cr, 0, len);
		*req = cr;
		*req_len = len;
		break;
	case HCIGETAUTHINFO:
		len = sizeof(*ar);
		ar = malloc(len);
		if (!ar)
			return -ENOMEM;
		memset(ar, 0, len);
		*req = ar;
		*req_len = len;
		break;
	case HCIBLOCKADDR:
	case HCIUNBLOCKADDR:
		len = sizeof(bdaddr_t);
		bdaddr = malloc(len);
		if (!bdaddr)
			return -ENOMEM;
		memset(bdaddr, 0, len);
		*req = bdaddr;
		*req_len = len;
		break;
	case HCIDEVUP:
	case HCIDEVDOWN:
	case HCIDEVRESET:
	case HCIDEVRESTAT:
		/* These command uses the HCI dev id for param */
		return -ENODATA;
	default:
		return -EINVAL;
	}

	return 0;
}

static void test_ioctl_common(const void *test_data)
{
	struct test_data *data = tester_get_data();
	const struct ioctl_data *ioctl_data = data->test_data;
	bool use_dev_id = false;
	void *req = NULL;
	uint32_t req_len = 0;
	int ret;

	ret = test_alloc_cmd_param(&req, &req_len);
	if (ret < 0) {
		if (ret == -ENODATA)
			use_dev_id = true;
		else {
			tester_warn("Failed to allocate CMD parameter");
			tester_test_failed();
			return;
		}
	}

	if (ioctl_data->expected_ioctl_err)
		test_add_condition(data);

	if (ioctl_data->expected_data)
		test_add_condition(data);

	if (!use_dev_id && ioctl_data->param) {
		test_add_condition(data);
		if (ioctl_data->cmd_param_func) {
			ret = ioctl_data->cmd_param_func(req, &req_len);
			if (ret) {
				tester_warn("Failed to update cmd param");
				tester_test_failed();
				goto exit_free;
			}
		} else
			memcpy(req, ioctl_data->param, req_len);

		tester_print("Command Parameter is updated");
		test_condition_complete(data);
	}

	if (use_dev_id)
		ret = ioctl(data->sock_fd, ioctl_data->cmd, data->hci_dev_id);
	else
		ret = ioctl(data->sock_fd, ioctl_data->cmd, req);

	if (ret < 0) {
		if (ioctl_data->expected_ioctl_err) {
			if (errno != ioctl_data->expected_ioctl_err) {
				tester_warn("Unexpected error: %d expected: %d",
					errno, ioctl_data->expected_ioctl_err);
				tester_test_failed();
				goto exit_free;
			}

			test_condition_complete(data);
			tester_print("Received expected error: %d", errno);
			goto exit_pass;
		}

		tester_warn("IOCTL failed with error: %d", errno);
		tester_test_failed();
		goto exit_free;
	}

	if (ioctl_data->expected_data && req) {
		if (ioctl_data->expect_data_check_func)
			ret = ioctl_data->expect_data_check_func(req, req_len);
		else
			ret = memcmp(req, ioctl_data->expected_data, req_len);

		if (ret != 0) {
			tester_warn("Mismatch expected data");
			util_hexdump('>', req, req_len, print_debug, "");
			util_hexdump('!', ioctl_data->expected_data, req_len,
							print_debug, "");
			tester_test_failed();
			goto exit_free;
		}

		test_condition_complete(data);
	}

exit_pass:
	tester_test_passed();
exit_free:
	if (req)
		free(req);

}

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

	tester_print("Device Connected");

	test_ioctl_common(data);
}

static void test_ioctl_connection(const void *test_data)
{
	struct test_data *data = tester_get_data();
	unsigned int id;
	const uint8_t *central_bdaddr;
	struct bthost *bthost;
	uint8_t addr_type;

	tester_print("Registering %s notification",
					mgmt_evstr(MGMT_EV_DEVICE_CONNECTED));
	id = mgmt_register(data->mgmt_alt, MGMT_EV_DEVICE_CONNECTED,
				data->mgmt_index,
				test_ioctl_connected_event,
				NULL, NULL);
	data->mgmt_alt_ev_id = id;

	central_bdaddr = hciemu_get_central_bdaddr(data->hciemu);
	if (!central_bdaddr) {
		tester_warn("No central bdaddr");
		tester_setup_failed();
		return;
	}

	addr_type = data->hciemu_type == HCIEMU_TYPE_BREDRLE ? BDADDR_BREDR :
							BDADDR_LE_PUBLIC;
	tester_print("ADDR TYPE: %d", addr_type);
	bthost = hciemu_client_get_host(data->hciemu);
	bthost_hci_connect(bthost, central_bdaddr, addr_type);
}

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

	test_ioctl("HCI Down", &dev_down, NULL, test_ioctl_common);

	test_ioctl("Device List", &dev_list,
				NULL, test_ioctl_common);

	test_ioctl("Device List - Invalid Param 1", &dev_list_invalid_1,
				NULL, test_ioctl_common);

	test_ioctl("Device Info", &dev_info,
				NULL, test_ioctl_common);

	test_ioctl("Reset Stat", &reset_stat,
				setup_powered, test_ioctl_common);

	test_ioctl("Set Link Mode - ACCEPT", &set_link_mode_accept,
				NULL, test_ioctl_common);

	test_ioctl("Set Link Mode - MASTER", &set_link_mode_master,
				NULL, test_ioctl_common);

	test_ioctl("Set Pkt Type - DM", &set_pkt_type_dm,
				NULL, test_ioctl_common);

	test_ioctl("Set Pkt Type - DH", &set_pkt_type_dh,
				NULL, test_ioctl_common);

	test_ioctl("Set Pkt Type - HV", &set_pkt_type_hv,
				NULL, test_ioctl_common);

	test_ioctl("Set Pkt Type - 2-DH", &set_pkt_type_2dh,
				NULL, test_ioctl_common);

	test_ioctl("Set Pkt Type - 2-DH", &set_pkt_type_3dh,
				NULL, test_ioctl_common);

	test_ioctl("Set Pkt Type - ALL", &set_pkt_type_all,
				NULL, test_ioctl_common);

	test_ioctl("Set ACL MTU - 1", &set_acl_mtu_1,
				NULL, test_ioctl_common);

	test_ioctl("Set ACL MTU - 2", &set_acl_mtu_2,
				NULL, test_ioctl_common);

	test_ioctl("Set SCO MTU - 1", &set_sco_mtu_1,
				NULL, test_ioctl_common);

	test_ioctl("Set SCO MTU - 2", &set_sco_mtu_2,
				NULL, test_ioctl_common);

	test_ioctl("Block BDADDR - Success", &block_bdaddr_success,
				NULL, test_ioctl_common);

	test_ioctl("Block BDADDR - Fail", &block_bdaddr_fail,
				setup_add_block_bdaddr, test_ioctl_common);

	test_ioctl("Unblock BDADDR - Success", &unblock_bdaddr_success,
				setup_add_block_bdaddr, test_ioctl_common);

	test_ioctl("Unblock BDADDR - Fail", &unblock_bdaddr_fail,
				NULL, test_ioctl_common);

	test_ioctl("Connection List - No Conn", &conn_list_no_conn,
				NULL, test_ioctl_common);

	test_ioctl("Connection List", &conn_list,
				setup_powered, test_ioctl_connection);

	test_ioctl("Connection Info", &conn_info,
				setup_powered, test_ioctl_connection);

	test_ioctl("Connection Info - No Connection", &conn_info_no_conn,
				setup_powered, test_ioctl_common);

	test_ioctl("Connection Info - Wrong Type", &conn_info_wrong_type,
				setup_powered, test_ioctl_common);

	test_ioctl("Authentication Info - No Connection", &auth_info_no_conn,
				setup_powered, test_ioctl_common);

	test_ioctl("Authentication Info", &auth_info,
				setup_powered, test_ioctl_connection);

	return tester_run();
}