iso-tester: add test for ACL disconnect before ISO created
[platform/upstream/bluez.git] / tools / iso-tester.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  *
4  *  BlueZ - Bluetooth protocol stack for Linux
5  *
6  *  Copyright (C) 2022  Intel Corporation.
7  *  Copyright 2023 NXP
8  *
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <poll.h>
19 #include <stdbool.h>
20
21 #include <glib.h>
22
23 #include "lib/bluetooth.h"
24 #include "lib/iso.h"
25 #include "lib/mgmt.h"
26
27 #include "monitor/bt.h"
28 #include "emulator/vhci.h"
29 #include "emulator/bthost.h"
30 #include "emulator/hciemu.h"
31
32 #include "src/shared/tester.h"
33 #include "src/shared/mgmt.h"
34 #include "src/shared/util.h"
35
36 #define QOS_IO(_interval, _latency, _sdu, _phy, _rtn) \
37 { \
38         .interval = _interval, \
39         .latency = _latency, \
40         .sdu = _sdu, \
41         .phy = _phy, \
42         .rtn = _rtn, \
43 }
44
45 #define QOS_FULL(_cig, _cis, _in, _out) \
46 { \
47         .ucast = { \
48                 .cig = _cig, \
49                 .cis = _cis, \
50                 .sca = 0x07, \
51                 .packing = 0x00, \
52                 .framing = 0x00, \
53                 .in = _in, \
54                 .out = _out, \
55         },\
56 }
57
58 #define QOS(_interval, _latency, _sdu, _phy, _rtn) \
59         QOS_FULL(BT_ISO_QOS_CIG_UNSET, BT_ISO_QOS_CIS_UNSET, \
60                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), \
61                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
62
63 #define QOS_1(_interval, _latency, _sdu, _phy, _rtn) \
64         QOS_FULL(0x01, BT_ISO_QOS_CIS_UNSET, \
65                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), \
66                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
67
68 #define QOS_2(_interval, _latency, _sdu, _phy, _rtn) \
69         QOS_FULL(0x02, BT_ISO_QOS_CIS_UNSET, \
70                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), \
71                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
72
73 #define QOS_1_1(_interval, _latency, _sdu, _phy, _rtn) \
74         QOS_FULL(0x01, 0x01, \
75                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), \
76                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
77
78 #define QOS_1_2(_interval, _latency, _sdu, _phy, _rtn) \
79         QOS_FULL(0x01, 0x02, \
80                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), \
81                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
82
83 #define QOS_OUT(_interval, _latency, _sdu, _phy, _rtn) \
84         QOS_FULL(BT_ISO_QOS_CIG_UNSET, BT_ISO_QOS_CIS_UNSET, \
85                 {}, QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
86
87 #define QOS_OUT_1(_interval, _latency, _sdu, _phy, _rtn) \
88         QOS_FULL(0x01, BT_ISO_QOS_CIS_UNSET, \
89                 {}, QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
90
91 #define QOS_OUT_1_1(_interval, _latency, _sdu, _phy, _rtn) \
92         QOS_FULL(0x01, 0x01, \
93                 {}, QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
94
95 #define QOS_OUT_1_2(_interval, _latency, _sdu, _phy, _rtn) \
96         QOS_FULL(0x01, 0x02, \
97                 {}, QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
98
99 #define QOS_OUT_1_EF(_interval, _latency, _sdu, _phy, _rtn) \
100         QOS_FULL(0x01, 0xEF, \
101                 {}, QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
102
103 #define QOS_IN(_interval, _latency, _sdu, _phy, _rtn) \
104         QOS_FULL(BT_ISO_QOS_CIG_UNSET, BT_ISO_QOS_CIS_UNSET, \
105                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
106 #define QOS_IN_1(_interval, _latency, _sdu, _phy, _rtn) \
107         QOS_FULL(0x01, BT_ISO_QOS_CIS_UNSET, \
108                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
109 #define QOS_IN_2(_interval, _latency, _sdu, _phy, _rtn) \
110         QOS_FULL(0x02, BT_ISO_QOS_CIS_UNSET, \
111                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
112 #define QOS_IN_1_1(_interval, _latency, _sdu, _phy, _rtn) \
113         QOS_FULL(0x01, 0x01, \
114                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
115 #define QOS_IN_1_2(_interval, _latency, _sdu, _phy, _rtn) \
116         QOS_FULL(0x01, 0x02, \
117                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
118
119 /* QoS Configuration settings for low latency audio data */
120 #define QOS_8_1_1 QOS(7500, 8, 26, 0x02, 2)
121 #define QOS_8_2_1 QOS(10000, 10, 30, 0x02, 2)
122 #define QOS_16_1_1 QOS(7500, 8, 30, 0x02, 2)
123 #define QOS_16_2_1 QOS(10000, 10, 40, 0x02, 2)
124 #define QOS_1_16_2_1 QOS_1(10000, 10, 40, 0x02, 2)
125 #define QOS_2_16_2_1 QOS_2(10000, 10, 40, 0x02, 2)
126 #define QOS_1_1_16_2_1 QOS_1_1(10000, 10, 40, 0x02, 2)
127 #define QOS_24_1_1 QOS(7500, 8, 45, 0x02, 2)
128 #define QOS_24_2_1 QOS(10000, 10, 60, 0x02, 2)
129 #define QOS_32_1_1 QOS(7500, 8, 60, 0x02, 2)
130 #define QOS_32_2_1 QOS(10000, 10, 80, 0x02, 2)
131 #define QOS_44_1_1 QOS_OUT(8163, 24, 98, 0x02, 5)
132 #define QOS_44_2_1 QOS_OUT(10884, 31, 130, 0x02, 5)
133 #define QOS_48_1_1 QOS_OUT(7500, 15, 75, 0x02, 5)
134 #define QOS_48_2_1 QOS_OUT(10000, 20, 100, 0x02, 5)
135 #define QOS_48_3_1 QOS_OUT(7500, 15, 90, 0x02, 5)
136 #define QOS_48_4_1 QOS_OUT(10000, 20, 120, 0x02, 5)
137 #define QOS_48_5_1 QOS_OUT(7500, 15, 117, 0x02, 5)
138 #define QOS_48_6_1 QOS_OUT(10000, 20, 155, 0x02, 5)
139 /* QoS Configuration settings for high reliability audio data */
140 #define QOS_8_1_2 QOS(7500, 75, 26, 0x02, 13)
141 #define QOS_8_2_2 QOS(10000, 95, 30, 0x02, 13)
142 #define QOS_16_1_2 QOS(7500, 75, 30, 0x02, 13)
143 #define QOS_16_2_2 QOS(10000, 95, 40, 0x02, 13)
144 #define QOS_24_1_2 QOS(7500, 75, 45, 0x02, 13)
145 #define QOS_24_2_2 QOS(10000, 95, 60, 0x02, 13)
146 #define QOS_32_1_2 QOS(7500, 65, 60, 0x02, 13)
147 #define QOS_32_2_2 QOS(10000, 95, 80, 0x02, 13)
148 #define QOS_44_1_2 QOS_OUT(8163, 80, 98, 0x02, 13)
149 #define QOS_44_2_2 QOS_OUT(10884, 85, 130, 0x02, 13)
150 #define QOS_48_1_2 QOS_OUT(7500, 75, 75, 0x02, 13)
151 #define QOS_48_2_2 QOS_OUT(10000, 95, 100, 0x02, 13)
152 #define QOS_48_3_2 QOS_OUT(7500, 75, 90, 0x02, 13)
153 #define QOS_48_4_2 QOS_OUT(10000, 100, 120, 0x02, 13)
154 #define QOS_48_5_2 QOS_OUT(7500, 75, 117, 0x02, 13)
155 #define QOS_48_6_2 QOS_OUT(10000, 100, 155, 0x02, 13)
156
157 /* One unidirectional CIS. Unicast Server is Audio Sink */
158 #define AC_1_4 QOS_OUT(10000, 10, 40, 0x02, 2)
159 /* One unidirectional CIS. Unicast Server is Audio Sink CIG 0x01 */
160 #define AC_1_4_1 QOS_OUT_1(10000, 10, 40, 0x02, 2)
161 /* One unidirectional CIS. Unicast Server is Audio Source. */
162 #define AC_2_10 QOS_IN(10000, 10, 40, 0x02, 2)
163 /* One unidirectional CIS. Unicast Server is Audio Source CIG 0x02 */
164 #define AC_2_10_2 QOS_IN_2(10000, 10, 40, 0x02, 2)
165 /* One bidirectional CIS. Unicast Server is Audio Sink and Audio Source. */
166 #define AC_3_5 QOS(10000, 10, 40, 0x02, 2)
167 /* Two unidirectional CISes. Unicast Server is Audio Sink.
168  * #1 - CIG 1 CIS 1 (output)
169  * #2 - CIG 1 CIS 2 (output)
170  */
171 #define AC_6i_1 QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
172 #define AC_6i_2 QOS_OUT_1_2(10000, 10, 40, 0x02, 2)
173 /* Two Unicast Servers. Unicast Server 1 is Audio Sink. Unicast Server 2 is
174  * Audio Sink.
175  * #1 - CIG 1 CIS auto (output)
176  * #2 - CIG 1 CIS auto (output)
177  */
178 #define AC_6ii_1 QOS_OUT_1(10000, 10, 40, 0x02, 2)
179 #define AC_6ii_2 QOS_OUT_1(10000, 10, 40, 0x02, 2)
180 #define AC_6ii_1_EF QOS_OUT_1_EF(10000, 10, 40, 0x02, 2)  /* different CIS ID */
181 /* Two unidirectional CISes. Unicast Server is Audio Sink and Audio Source.
182  * #1 - CIG 1 CIS 1 (input)
183  * #2 - CIG 1 CIS 2 (output)
184  */
185 #define AC_7i_1 QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
186 #define AC_7i_2 QOS_IN_1_2(10000, 10, 40, 0x02, 2)
187 /* Two Unidirectional CISes. Two Unicast Servers. Unicast Server 1 is Audio
188  * Sink. Unicast Server 2 is Audio Source.
189  * #1 - CIG 1 CIS auto (output)
190  * #2 - CIG 1 CIS auto (output)
191  */
192 #define AC_7ii_1 QOS_OUT_1(10000, 10, 40, 0x02, 2)
193 #define AC_7ii_2 QOS_IN_1(10000, 10, 40, 0x02, 2)
194 /* One bidirectional CIS and one unidirectional CIS. Unicast Server is Audio
195  * Sink and Audio Source.
196  * #1 - CIG 1 CIS 1 (output)
197  * #2 - CIG 1 CIS 2 (input/output)
198  */
199 #define AC_8i_1 QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
200 #define AC_8i_2 QOS_1_2(10000, 10, 40, 0x02, 2)
201 /* One bidirectional CIS and one unidirectional CIS. Two Unicast Servers.
202  * Unicast Server 1 is Audio Sink and Audio Source. Unicast Server 2 is
203  * Audio Sink.
204  * #1 - CIG 1 CIS auto (input/output)
205  * #2 - CIG 1 CIS auto (output)
206  */
207 #define AC_8ii_1 QOS_1(10000, 10, 40, 0x02, 2)
208 #define AC_8ii_2 QOS_OUT_1(10000, 10, 40, 0x02, 2)
209 /* Two unidirectional CISes. Unicast Server is Audio Source.
210  * #1 - CIG 1 CIS 1 (input)
211  * #2 - CIG 1 CIS 2 (input)
212  */
213 #define AC_9i_1 QOS_IN_1_1(10000, 10, 40, 0x02, 2)
214 #define AC_9i_2 QOS_IN_1_2(10000, 10, 40, 0x02, 2)
215 /* Two unidirectional CISes. Two Unicast Servers. Unicast Server 1 is Audio
216  * Source. Unicast Server 2 is Audio Source.
217  * #1 - CIG 1 CIS auto (input)
218  * #2 - CIG 1 CIS auto (input)
219  */
220 #define AC_9ii_1 QOS_IN_1(10000, 10, 40, 0x02, 2)
221 #define AC_9ii_2 QOS_IN_1(10000, 10, 40, 0x02, 2)
222 /* Two bidirectional CISes. Unicast Server is Audio Sink and Audio Source.
223  * #1 - CIG 1 CIS 1 (input/output)
224  * #2 - CIG 1 CIS 2 (input/output)
225  */
226 #define AC_11i_1 QOS_1_1(10000, 10, 40, 0x02, 2)
227 #define AC_11i_2 QOS_1_2(10000, 10, 40, 0x02, 2)
228 /* Two bidirectional CISes. Two Unicast Servers. Unicast Server 1 is Audio Sink
229  * and Audio Source. Unicast Server 2 is Audio Sink and Audio Source.
230  * #1 - CIG 1 CIS auto (input/output)
231  * #2 - CIG 1 CIS auto (input/output)
232  */
233 #define AC_11ii_1 QOS_1(10000, 10, 40, 0x02, 2)
234 #define AC_11ii_2 QOS_1(10000, 10, 40, 0x02, 2)
235
236 #define BCODE {0x01, 0x02, 0x68, 0x05, 0x53, 0xf1, 0x41, 0x5a, \
237                                 0xa2, 0x65, 0xbb, 0xaf, 0xc6, 0xea, 0x03, 0xb8}
238
239 #define QOS_BCAST_FULL(_big, _bis, _encryption, _bcode, _in, _out) \
240 { \
241         .bcast = { \
242                 .big = _big, \
243                 .bis = _bis, \
244                 .sync_interval = 0x07, \
245                 .packing = 0x00, \
246                 .framing = 0x00, \
247                 .in = _in, \
248                 .out = _out, \
249                 .encryption = _encryption, \
250                 .bcode = _bcode, \
251                 .options = 0x00, \
252                 .skip = 0x0000, \
253                 .sync_timeout = 0x4000, \
254                 .sync_cte_type = 0x00, \
255                 .mse = 0x00, \
256                 .timeout = 0x4000, \
257         }, \
258 }
259
260 #define BCAST_QOS_OUT(_interval, _latency, _sdu, _phy, _rtn) \
261         QOS_BCAST_FULL(BT_ISO_QOS_BIG_UNSET, BT_ISO_QOS_BIS_UNSET, \
262                 0x00, {0x00}, {}, \
263                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
264
265 #define BCAST_QOS_OUT_ENC(_interval, _latency, _sdu, _phy, _rtn) \
266         QOS_BCAST_FULL(BT_ISO_QOS_BIG_UNSET, BT_ISO_QOS_BIS_UNSET, \
267                 0x01, BCODE, {}, \
268                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
269
270 #define BCAST_QOS_OUT_1(_interval, _latency, _sdu, _phy, _rtn) \
271         QOS_BCAST_FULL(0x01, BT_ISO_QOS_BIS_UNSET, \
272                 0x00, {0x00}, {}, \
273                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
274
275 #define BCAST_QOS_OUT_1_1(_interval, _latency, _sdu, _phy, _rtn) \
276         QOS_BCAST_FULL(0x01, 0x01, \
277                 0x00, {0x00}, {}, \
278                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn))
279
280 #define BCAST_QOS_IN(_interval, _latency, _sdu, _phy, _rtn) \
281         QOS_BCAST_FULL(BT_ISO_QOS_BIG_UNSET, BT_ISO_QOS_BIS_UNSET, \
282                 0x00, {0x00}, \
283                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
284
285 #define BCAST_QOS_IN_ENC(_interval, _latency, _sdu, _phy, _rtn) \
286         QOS_BCAST_FULL(BT_ISO_QOS_BIG_UNSET, BT_ISO_QOS_BIS_UNSET, \
287                 0x01, BCODE, \
288                 QOS_IO(_interval, _latency, _sdu, _phy, _rtn), {})
289
290 #define QOS_OUT_16_2_1 BCAST_QOS_OUT(10000, 10, 40, 0x02, 2)
291 #define QOS_OUT_ENC_16_2_1 BCAST_QOS_OUT_ENC(10000, 10, 40, 0x02, 2)
292 #define QOS_OUT_1_16_2_1 BCAST_QOS_OUT_1(10000, 10, 40, 0x02, 2)
293 #define QOS_OUT_1_1_16_2_1 BCAST_QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
294 #define QOS_IN_16_2_1 BCAST_QOS_IN(10000, 10, 40, 0x02, 2)
295 #define QOS_IN_ENC_16_2_1 BCAST_QOS_IN_ENC(10000, 10, 40, 0x02, 2)
296
297 static const uint8_t base_lc3_16_2_1[] = {
298         0x28, 0x00, 0x00, /* Presentation Delay */
299         0x01, /* Number of Subgroups */
300         0x01, /* Number of BIS */
301         0x06, 0x00, 0x00, 0x00, 0x00, /* Code ID = LC3 (0x06) */
302         0x11, /* Codec Specific Configuration */
303         0x02, 0x01, 0x03, /* 16 KHZ */
304         0x02, 0x02, 0x01, /* 10 ms */
305         0x05, 0x03, 0x01, 0x00, 0x00, 0x00,  /* Front Left */
306         0x03, 0x04, 0x28, 0x00, /* Frame Length 40 bytes */
307         0x04, /* Metadata */
308         0x03, 0x02, 0x02, 0x00, /* Audio Context: Convertional */
309         0x01, /* BIS */
310         0x00, /* Codec Specific Configuration */
311 };
312
313 /* Single Audio Channel. One BIS. */
314 #define BCAST_AC_12 BCAST_QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
315
316 static const uint8_t base_lc3_ac_12[] = {
317         0x28, 0x00, 0x00, /* Presentation Delay */
318         0x01, /* Number of Subgroups */
319         0x01, /* Number of BIS */
320         0x06, 0x00, 0x00, 0x00, 0x00, /* Code ID = LC3 (0x06) */
321         0x11, /* Codec Specific Configuration */
322         0x02, 0x01, 0x03, /* 16 KHZ */
323         0x02, 0x02, 0x01, /* 10 ms */
324         0x05, 0x03, 0x01, 0x00, 0x00, 0x00,  /* Front Left */
325         0x03, 0x04, 0x28, 0x00, /* Frame Length 40 bytes */
326         0x04, /* Metadata */
327         0x03, 0x02, 0x02, 0x00, /* Audio Context: Convertional */
328         0x01, /* BIS */
329         0x00, /* Codec Specific Configuration */
330 };
331
332 /* Multiple Audio Channels. Two BISes. */
333 #define BCAST_AC_13 BCAST_QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
334
335 static const uint8_t base_lc3_ac_13[] = {
336         0x28, 0x00, 0x00, /* Presentation Delay */
337         0x01, /* Number of Subgroups */
338         0x02, /* Number of BIS */
339         0x06, 0x00, 0x00, 0x00, 0x00, /* Code ID = LC3 (0x06) */
340         0x11, /* Codec Specific Configuration */
341         0x02, 0x01, 0x03, /* 16 KHZ */
342         0x02, 0x02, 0x01, /* 10 ms */
343         0x05, 0x03, 0x01, 0x00, 0x00, 0x00,  /* Front Left */
344         0x03, 0x04, 0x28, 0x00, /* Frame Length 40 bytes */
345         0x04, /* Metadata */
346         0x03, 0x02, 0x02, 0x00, /* Audio Context: Convertional */
347         0x01, /* BIS 1 */
348         0x06, /* Codec Specific Configuration */
349         0x05, 0x03, 0x01, 0x00, 0x00, 0x00, /* Audio_Channel_Allocation:
350                                              * Front left
351                                              */
352         0x01, /* BIS 2 */
353         0x06, /* Codec Specific Configuration */
354         0x05, 0x03, 0x02, 0x00, 0x00, 0x00, /* Audio_Channel_Allocation:
355                                              * Front right
356                                              */
357 };
358
359 /* Multiple Audio Channels. One BIS. */
360 #define BCAST_AC_14 BCAST_QOS_OUT_1_1(10000, 10, 40, 0x02, 2)
361
362 static const uint8_t base_lc3_ac_14[] = {
363         0x28, 0x00, 0x00, /* Presentation Delay */
364         0x01, /* Number of Subgroups */
365         0x01, /* Number of BIS */
366         0x06, 0x00, 0x00, 0x00, 0x00, /* Code ID = LC3 (0x06) */
367         0x11, /* Codec Specific Configuration */
368         0x02, 0x01, 0x03, /* 16 KHZ */
369         0x02, 0x02, 0x01, /* 10 ms */
370         0x05, 0x03, 0x01, 0x00, 0x00, 0x00,  /* Front Left */
371         0x03, 0x04, 0x28, 0x00, /* Frame Length 40 bytes */
372         0x04, /* Metadata */
373         0x03, 0x02, 0x02, 0x00, /* Audio Context: Convertional */
374         0x01, /* BIS */
375         0x06, /* Codec Specific Configuration */
376         0x05, 0x03, 0x03, 0x00, 0x00, 0x00, /* Audio_Channel_Allocation:
377                                              * Front left, Front right
378                                              */
379 };
380
381 struct test_data {
382         const void *test_data;
383         struct mgmt *mgmt;
384         uint16_t mgmt_index;
385         struct hciemu *hciemu;
386         enum hciemu_type hciemu_type;
387         uint8_t accept_reason;
388         uint16_t handle;
389         uint16_t acl_handle;
390         GIOChannel *io;
391         unsigned int io_id[2];
392         uint8_t client_num;
393         int step;
394         bool reconnect;
395         bool suspending;
396 };
397
398 struct iso_client_data {
399         struct bt_iso_qos qos;
400         struct bt_iso_qos qos_2;
401         int expect_err;
402         const struct iovec *send;
403         const struct iovec *recv;
404         bool server;
405         bool bcast;
406         bool defer;
407         bool disconnect;
408         bool ts;
409         bool mconn;
410         bool suspend;
411         uint8_t pkt_status;
412         const uint8_t *base;
413         size_t base_len;
414 };
415
416 static void mgmt_debug(const char *str, void *user_data)
417 {
418         const char *prefix = user_data;
419
420         tester_print("%s%s", prefix, str);
421 }
422
423 static void read_info_callback(uint8_t status, uint16_t length,
424                                         const void *param, void *user_data)
425 {
426         struct test_data *data = tester_get_data();
427         const struct mgmt_rp_read_info *rp = param;
428         char addr[18];
429         uint16_t manufacturer;
430         uint32_t supported_settings, current_settings;
431
432         tester_print("Read Info callback");
433         tester_print("  Status: 0x%02x", status);
434
435         if (status || !param) {
436                 tester_pre_setup_failed();
437                 return;
438         }
439
440         ba2str(&rp->bdaddr, addr);
441         manufacturer = btohs(rp->manufacturer);
442         supported_settings = btohl(rp->supported_settings);
443         current_settings = btohl(rp->current_settings);
444
445         tester_print("  Address: %s", addr);
446         tester_print("  Version: 0x%02x", rp->version);
447         tester_print("  Manufacturer: 0x%04x", manufacturer);
448         tester_print("  Supported settings: 0x%08x", supported_settings);
449         tester_print("  Current settings: 0x%08x", current_settings);
450         tester_print("  Class: 0x%02x%02x%02x",
451                         rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
452         tester_print("  Name: %s", rp->name);
453         tester_print("  Short name: %s", rp->short_name);
454
455         if (strcmp(hciemu_get_address(data->hciemu), addr)) {
456                 tester_pre_setup_failed();
457                 return;
458         }
459
460         tester_pre_setup_complete();
461 }
462
463 static void index_added_callback(uint16_t index, uint16_t length,
464                                         const void *param, void *user_data)
465 {
466         struct test_data *data = tester_get_data();
467
468         tester_print("Index Added callback");
469         tester_print("  Index: 0x%04x", index);
470
471         data->mgmt_index = index;
472
473         mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
474                                         read_info_callback, NULL, NULL);
475 }
476
477 static void index_removed_callback(uint16_t index, uint16_t length,
478                                         const void *param, void *user_data)
479 {
480         struct test_data *data = tester_get_data();
481
482         tester_print("Index Removed callback");
483         tester_print("  Index: 0x%04x", index);
484
485         if (index != data->mgmt_index)
486                 return;
487
488         mgmt_unregister_index(data->mgmt, data->mgmt_index);
489
490         mgmt_unref(data->mgmt);
491         data->mgmt = NULL;
492
493         tester_post_teardown_complete();
494 }
495
496 static void hciemu_debug(const char *str, void *user_data)
497 {
498         const char *prefix = user_data;
499
500         tester_print("%s%s", prefix, str);
501 }
502
503 static void read_index_list_callback(uint8_t status, uint16_t length,
504                                         const void *param, void *user_data)
505 {
506         struct test_data *data = tester_get_data();
507
508         tester_print("Read Index List callback");
509         tester_print("  Status: 0x%02x", status);
510
511         if (status || !param) {
512                 tester_pre_setup_failed();
513                 return;
514         }
515
516         mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
517                                         index_added_callback, NULL, NULL);
518
519         mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
520                                         index_removed_callback, NULL, NULL);
521
522         data->hciemu = hciemu_new_num(HCIEMU_TYPE_BREDRLE52, data->client_num);
523         if (!data->hciemu) {
524                 tester_warn("Failed to setup HCI emulation");
525                 tester_pre_setup_failed();
526                 return;
527         }
528
529         if (tester_use_debug())
530                 hciemu_set_debug(data->hciemu, hciemu_debug, "hciemu: ", NULL);
531
532         tester_print("New hciemu instance created");
533 }
534
535 static const uint8_t set_iso_socket_param[] = {
536         0x3e, 0xe0, 0xb4, 0xfd, 0xdd, 0xd6, 0x85, 0x98, /* UUID - ISO Socket */
537         0x6a, 0x49, 0xe0, 0x05, 0x88, 0xf1, 0xba, 0x6f,
538         0x01,                                           /* Action - enable */
539 };
540
541 static const uint8_t reset_iso_socket_param[] = {
542         0x3e, 0xe0, 0xb4, 0xfd, 0xdd, 0xd6, 0x85, 0x98, /* UUID - ISO Socket */
543         0x6a, 0x49, 0xe0, 0x05, 0x88, 0xf1, 0xba, 0x6f,
544         0x00,                                           /* Action - disable */
545 };
546
547 static void set_iso_socket_callback(uint8_t status, uint16_t length,
548                                         const void *param, void *user_data)
549 {
550         if (status != MGMT_STATUS_SUCCESS) {
551                 tester_print("ISO socket feature could not be enabled");
552                 return;
553         }
554
555         tester_print("ISO socket feature is enabled");
556 }
557
558 static void test_pre_setup(const void *test_data)
559 {
560         struct test_data *data = tester_get_data();
561
562         data->mgmt = mgmt_new_default();
563         if (!data->mgmt) {
564                 tester_warn("Failed to setup management interface");
565                 tester_pre_setup_failed();
566                 return;
567         }
568
569         if (tester_use_debug())
570                 mgmt_set_debug(data->mgmt, mgmt_debug, "mgmt: ", NULL);
571
572         mgmt_send(data->mgmt, MGMT_OP_SET_EXP_FEATURE, MGMT_INDEX_NONE,
573                   sizeof(set_iso_socket_param), set_iso_socket_param,
574                   set_iso_socket_callback, NULL, NULL);
575
576         mgmt_send(data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
577                                         read_index_list_callback, NULL, NULL);
578 }
579
580 static void test_post_teardown(const void *test_data)
581 {
582         struct test_data *data = tester_get_data();
583
584         mgmt_send(data->mgmt, MGMT_OP_SET_EXP_FEATURE, MGMT_INDEX_NONE,
585                   sizeof(reset_iso_socket_param), reset_iso_socket_param,
586                   NULL, NULL, NULL);
587
588         hciemu_unref(data->hciemu);
589         data->hciemu = NULL;
590 }
591
592 static void test_data_free(void *test_data)
593 {
594         struct test_data *data = test_data;
595
596         if (data->io)
597                 g_io_channel_unref(data->io);
598
599         if (data->io_id[0] > 0)
600                 g_source_remove(data->io_id[0]);
601
602         if (data->io_id[1] > 0)
603                 g_source_remove(data->io_id[1]);
604
605         free(data);
606 }
607
608 #define test_iso_full(name, data, setup, func, num, reason) \
609         do { \
610                 struct test_data *user; \
611                 user = new0(struct test_data, 1); \
612                 if (!user) \
613                         break; \
614                 user->hciemu_type = HCIEMU_TYPE_BREDRLE; \
615                 user->test_data = data; \
616                 user->client_num = num; \
617                 user->accept_reason = reason; \
618                 tester_add_full(name, data, \
619                                 test_pre_setup, setup, func, NULL, \
620                                 test_post_teardown, 2, user, test_data_free); \
621         } while (0)
622
623 #define test_iso(name, data, setup, func) \
624         test_iso_full(name, data, setup, func, 1, 0x00)
625
626 #define test_iso2(name, data, setup, func) \
627         test_iso_full(name, data, setup, func, 2, 0x00)
628
629 #define test_iso_rej(name, data, setup, func, reason) \
630         test_iso_full(name, data, setup, func, 1, reason)
631
632 static const struct iso_client_data connect_8_1_1 = {
633         .qos = QOS_8_1_1,
634         .expect_err = 0
635 };
636
637 static const struct iso_client_data connect_8_2_1 = {
638         .qos = QOS_8_2_1,
639         .expect_err = 0
640 };
641
642 static const struct iso_client_data connect_16_1_1 = {
643         .qos = QOS_16_1_1,
644         .expect_err = 0
645 };
646
647 static const struct iso_client_data connect_16_2_1 = {
648         .qos = QOS_16_2_1,
649         .expect_err = 0
650 };
651
652 static const struct iso_client_data connect_1_16_2_1 = {
653         .qos = QOS_1_16_2_1,
654         .expect_err = 0
655 };
656
657 static const struct iso_client_data connect_1_1_16_2_1 = {
658         .qos = QOS_1_1_16_2_1,
659         .expect_err = 0
660 };
661
662 static const struct iso_client_data connect_24_1_1 = {
663         .qos = QOS_24_1_1,
664         .expect_err = 0
665 };
666
667 static const struct iso_client_data connect_24_2_1 = {
668         .qos = QOS_24_2_1,
669         .expect_err = 0
670 };
671
672 static const struct iso_client_data connect_32_1_1 = {
673         .qos = QOS_32_1_1,
674         .expect_err = 0
675 };
676
677 static const struct iso_client_data connect_32_2_1 = {
678         .qos = QOS_32_2_1,
679         .expect_err = 0
680 };
681
682 static const struct iso_client_data connect_44_1_1 = {
683         .qos = QOS_44_1_1,
684         .expect_err = 0
685 };
686
687 static const struct iso_client_data connect_44_2_1 = {
688         .qos = QOS_44_2_1,
689         .expect_err = 0
690 };
691
692 static const struct iso_client_data connect_48_1_1 = {
693         .qos = QOS_48_1_1,
694         .expect_err = 0
695 };
696
697 static const struct iso_client_data connect_48_2_1 = {
698         .qos = QOS_48_2_1,
699         .expect_err = 0
700 };
701
702 static const struct iso_client_data connect_48_3_1 = {
703         .qos = QOS_48_3_1,
704         .expect_err = 0
705 };
706
707 static const struct iso_client_data connect_48_4_1 = {
708         .qos = QOS_48_4_1,
709         .expect_err = 0
710 };
711
712 static const struct iso_client_data connect_48_5_1 = {
713         .qos = QOS_48_5_1,
714         .expect_err = 0
715 };
716
717 static const struct iso_client_data connect_48_6_1 = {
718         .qos = QOS_48_6_1,
719         .expect_err = 0
720 };
721
722 static const struct iso_client_data connect_8_1_2 = {
723         .qos = QOS_8_1_2,
724         .expect_err = 0
725 };
726
727 static const struct iso_client_data connect_8_2_2 = {
728         .qos = QOS_8_2_2,
729         .expect_err = 0
730 };
731
732 static const struct iso_client_data connect_16_1_2 = {
733         .qos = QOS_16_1_2,
734         .expect_err = 0
735 };
736
737 static const struct iso_client_data connect_16_2_2 = {
738         .qos = QOS_16_2_2,
739         .expect_err = 0
740 };
741
742 static const struct iso_client_data connect_24_1_2 = {
743         .qos = QOS_24_1_2,
744         .expect_err = 0
745 };
746
747 static const struct iso_client_data connect_24_2_2 = {
748         .qos = QOS_24_2_2,
749         .expect_err = 0
750 };
751
752 static const struct iso_client_data connect_32_1_2 = {
753         .qos = QOS_32_1_2,
754         .expect_err = 0
755 };
756
757 static const struct iso_client_data connect_32_2_2 = {
758         .qos = QOS_32_2_2,
759         .expect_err = 0
760 };
761
762 static const struct iso_client_data connect_44_1_2 = {
763         .qos = QOS_44_1_2,
764         .expect_err = 0
765 };
766
767 static const struct iso_client_data connect_44_2_2 = {
768         .qos = QOS_44_2_2,
769         .expect_err = 0
770 };
771
772 static const struct iso_client_data connect_48_1_2 = {
773         .qos = QOS_48_1_2,
774         .expect_err = 0
775 };
776
777 static const struct iso_client_data connect_48_2_2 = {
778         .qos = QOS_48_2_2,
779         .expect_err = 0
780 };
781
782 static const struct iso_client_data connect_48_3_2 = {
783         .qos = QOS_48_3_2,
784         .expect_err = 0
785 };
786
787 static const struct iso_client_data connect_48_4_2 = {
788         .qos = QOS_48_4_2,
789         .expect_err = 0
790 };
791
792 static const struct iso_client_data connect_48_5_2 = {
793         .qos = QOS_48_5_2,
794         .expect_err = 0
795 };
796
797 static const struct iso_client_data connect_48_6_2 = {
798         .qos = QOS_48_6_2,
799         .expect_err = 0
800 };
801
802 static const struct iso_client_data connect_invalid = {
803         .qos = QOS(0, 0, 0, 0, 0),
804         .expect_err = -EINVAL
805 };
806
807 static const struct iso_client_data connect_reject = {
808         .qos = QOS_16_1_2,
809         .expect_err = -ENOSYS
810 };
811
812 static const struct iso_client_data connect_suspend = {
813         .qos = QOS_16_2_1,
814         .expect_err = -ECONNRESET
815 };
816
817 static const struct iso_client_data connect_cig_f0_invalid = {
818         .qos = QOS_FULL(0xF0, 0x00, {}, QOS_IO(10000, 10, 40, 0x02, 2)),
819         .expect_err = -EINVAL
820 };
821
822 static const struct iso_client_data connect_cis_f0_invalid = {
823         .qos = QOS_FULL(0x00, 0xF0, {}, QOS_IO(10000, 10, 40, 0x02, 2)),
824         .expect_err = -EINVAL
825 };
826
827 static const uint8_t data_16_2_1[40] = { [0 ... 39] = 0xff };
828 static const struct iovec send_16_2_1 = {
829         .iov_base = (void *)data_16_2_1,
830         .iov_len = sizeof(data_16_2_1),
831 };
832
833 static const uint8_t data_48_2_1[100] = { [0 ... 99] = 0xff };
834 static const struct iovec send_48_2_1 = {
835         .iov_base = (void *)data_48_2_1,
836         .iov_len = sizeof(data_48_2_1),
837 };
838
839 static const struct iso_client_data connect_16_2_1_send = {
840         .qos = QOS_16_2_1,
841         .expect_err = 0,
842         .send = &send_16_2_1,
843 };
844
845 static const struct iso_client_data listen_16_2_1_recv = {
846         .qos = QOS_16_2_1,
847         .expect_err = 0,
848         .recv = &send_16_2_1,
849         .server = true,
850 };
851
852 static const struct iso_client_data listen_16_2_1_recv_ts = {
853         .qos = QOS_16_2_1,
854         .expect_err = 0,
855         .recv = &send_16_2_1,
856         .server = true,
857         .ts = true,
858 };
859
860 static const struct iso_client_data listen_16_2_1_recv_pkt_status = {
861         .qos = QOS_16_2_1,
862         .expect_err = 0,
863         .recv = &send_16_2_1,
864         .server = true,
865         .pkt_status = 0x02,
866 };
867
868 static const struct iso_client_data defer_16_2_1 = {
869         .qos = QOS_16_2_1,
870         .expect_err = 0,
871         .defer = true,
872 };
873
874 static const struct iso_client_data defer_1_16_2_1 = {
875         .qos = QOS_1_16_2_1,
876         .expect_err = 0,
877         .defer = true,
878 };
879
880 static const struct iso_client_data connect_16_2_1_defer_send = {
881         .qos = QOS_16_2_1,
882         .expect_err = 0,
883         .send = &send_16_2_1,
884         .defer = true,
885 };
886
887 static const struct iso_client_data connect_48_2_1_defer_send = {
888         .qos = QOS_48_2_1,
889         .expect_err = 0,
890         .send = &send_16_2_1,
891         .defer = true,
892 };
893
894 static const struct iso_client_data listen_16_2_1_defer_recv = {
895         .qos = QOS_16_2_1,
896         .expect_err = 0,
897         .recv = &send_16_2_1,
898         .server = true,
899         .defer = true,
900 };
901
902 static const struct iso_client_data listen_48_2_1_defer_recv = {
903         .qos = QOS_48_2_1,
904         .expect_err = 0,
905         .recv = &send_48_2_1,
906         .server = true,
907         .defer = true,
908 };
909
910 static const struct iso_client_data listen_16_2_1_defer_reject = {
911         .qos = QOS_16_2_1,
912         .expect_err = -1,
913         .recv = &send_16_2_1,
914         .server = true,
915         .defer = true,
916 };
917
918 static const struct iso_client_data connect_16_2_1_send_recv = {
919         .qos = QOS_16_2_1,
920         .expect_err = 0,
921         .send = &send_16_2_1,
922         .recv = &send_16_2_1,
923 };
924
925 static const struct iso_client_data disconnect_16_2_1 = {
926         .qos = QOS_16_2_1,
927         .expect_err = 0,
928         .disconnect = true,
929 };
930
931 static const struct iso_client_data suspend_16_2_1 = {
932         .qos = QOS_16_2_1,
933         .suspend = true,
934 };
935
936 static const struct iso_client_data reconnect_16_2_1 = {
937         .qos = QOS_16_2_1,
938         .expect_err = 0,
939         .disconnect = true,
940 };
941
942 static const struct iso_client_data connect_ac_1_4 = {
943         .qos = AC_1_4,
944         .expect_err = 0
945 };
946
947 static const struct iso_client_data connect_ac_2_10 = {
948         .qos = AC_2_10,
949         .expect_err = 0
950 };
951
952 static const struct iso_client_data connect_ac_3_5 = {
953         .qos = AC_3_5,
954         .expect_err = 0
955 };
956
957 static const struct iso_client_data connect_ac_6i = {
958         .qos = AC_6i_1,
959         .qos_2 = AC_6i_2,
960         .expect_err = 0,
961         .mconn = true,
962         .defer = true,
963 };
964
965 static const struct iso_client_data reconnect_ac_6i = {
966         .qos = AC_6i_1,
967         .qos_2 = AC_6i_2,
968         .expect_err = 0,
969         .mconn = true,
970         .defer = true,
971         .disconnect = true,
972 };
973
974 static const struct iso_client_data connect_ac_6ii = {
975         .qos = AC_6ii_1,
976         .qos_2 = AC_6ii_2,
977         .expect_err = 0,
978         .mconn = true,
979         .defer = true,
980 };
981
982 static const struct iso_client_data reconnect_ac_6ii = {
983         .qos = AC_6ii_1,
984         .qos_2 = AC_6ii_2,
985         .expect_err = 0,
986         .mconn = true,
987         .defer = true,
988         .disconnect = true,
989 };
990
991 static const struct iso_client_data connect_ac_6ii_cis_ef_auto = {
992         .qos = AC_6ii_1_EF,
993         .qos_2 = AC_6ii_2,
994         .expect_err = 0,
995         .mconn = true,
996         .defer = true,
997 };
998
999 static const struct iso_client_data connect_ac_6ii_cis_ef_ef = {
1000         .qos = AC_6ii_1_EF,
1001         .qos_2 = AC_6ii_1_EF,
1002         .expect_err = -EINVAL,
1003         .mconn = true,
1004         .defer = true,
1005 };
1006
1007 static const struct iso_client_data connect_ac_7i = {
1008         .qos = AC_7i_1,
1009         .qos_2 = AC_7i_2,
1010         .expect_err = 0,
1011         .mconn = true,
1012         .defer = true,
1013 };
1014
1015 static const struct iso_client_data connect_ac_7ii = {
1016         .qos = AC_7ii_1,
1017         .qos_2 = AC_7ii_2,
1018         .expect_err = 0,
1019         .mconn = true,
1020         .defer = true,
1021 };
1022
1023 static const struct iso_client_data connect_ac_8i = {
1024         .qos = AC_8i_1,
1025         .qos_2 = AC_8i_2,
1026         .expect_err = 0,
1027         .mconn = true,
1028         .defer = true,
1029 };
1030
1031 static const struct iso_client_data connect_ac_8ii = {
1032         .qos = AC_8ii_1,
1033         .qos_2 = AC_8ii_2,
1034         .expect_err = 0,
1035         .mconn = true,
1036         .defer = true,
1037 };
1038
1039 static const struct iso_client_data connect_ac_9i = {
1040         .qos = AC_9i_1,
1041         .qos_2 = AC_9i_2,
1042         .expect_err = 0,
1043         .mconn = true,
1044         .defer = true,
1045 };
1046
1047 static const struct iso_client_data connect_ac_9ii = {
1048         .qos = AC_9ii_1,
1049         .qos_2 = AC_9ii_2,
1050         .expect_err = 0,
1051         .mconn = true,
1052         .defer = true,
1053 };
1054
1055 static const struct iso_client_data connect_ac_11i = {
1056         .qos = AC_11i_1,
1057         .qos_2 = AC_11i_2,
1058         .expect_err = 0,
1059         .mconn = true,
1060         .defer = true,
1061 };
1062
1063 static const struct iso_client_data connect_ac_11ii = {
1064         .qos = AC_11ii_1,
1065         .qos_2 = AC_11ii_2,
1066         .expect_err = 0,
1067         .mconn = true,
1068         .defer = true,
1069 };
1070
1071 static const struct iso_client_data connect_ac_1_2 = {
1072         .qos = AC_1_4,
1073         .qos_2 = AC_2_10,
1074         .expect_err = 0,
1075         .mconn = true,
1076 };
1077
1078 static const struct iso_client_data connect_ac_1_2_cig_1_2 = {
1079         .qos = AC_1_4_1,
1080         .qos_2 = AC_2_10_2,
1081         .expect_err = 0,
1082         .mconn = true,
1083 };
1084
1085 static const struct iso_client_data bcast_16_2_1_send = {
1086         .qos = QOS_OUT_16_2_1,
1087         .expect_err = 0,
1088         .send = &send_16_2_1,
1089         .bcast = true,
1090         .base = base_lc3_16_2_1,
1091         .base_len = sizeof(base_lc3_16_2_1),
1092 };
1093
1094 static const struct iso_client_data bcast_enc_16_2_1_send = {
1095         .qos = QOS_OUT_ENC_16_2_1,
1096         .expect_err = 0,
1097         .send = &send_16_2_1,
1098         .bcast = true,
1099         .base = base_lc3_16_2_1,
1100         .base_len = sizeof(base_lc3_16_2_1),
1101 };
1102
1103 static const struct iso_client_data bcast_1_16_2_1_send = {
1104         .qos = QOS_OUT_1_16_2_1,
1105         .expect_err = 0,
1106         .send = &send_16_2_1,
1107         .bcast = true,
1108         .base = base_lc3_16_2_1,
1109         .base_len = sizeof(base_lc3_16_2_1),
1110 };
1111
1112 static const struct iso_client_data bcast_1_1_16_2_1_send = {
1113         .qos = QOS_OUT_1_1_16_2_1,
1114         .expect_err = 0,
1115         .send = &send_16_2_1,
1116         .bcast = true,
1117         .base = base_lc3_16_2_1,
1118         .base_len = sizeof(base_lc3_16_2_1),
1119 };
1120
1121 static const struct iso_client_data bcast_16_2_1_recv = {
1122         .qos = QOS_IN_16_2_1,
1123         .expect_err = 0,
1124         .recv = &send_16_2_1,
1125         .bcast = true,
1126         .server = true,
1127 };
1128
1129 static const struct iso_client_data bcast_enc_16_2_1_recv = {
1130         .qos = QOS_IN_ENC_16_2_1,
1131         .expect_err = 0,
1132         .recv = &send_16_2_1,
1133         .bcast = true,
1134         .server = true,
1135 };
1136
1137 static const struct iso_client_data bcast_16_2_1_recv_defer = {
1138         .qos = QOS_IN_16_2_1,
1139         .expect_err = 0,
1140         .defer = true,
1141         .recv = &send_16_2_1,
1142         .bcast = true,
1143         .server = true,
1144 };
1145
1146 static const struct iso_client_data bcast_ac_12 = {
1147         .qos = BCAST_AC_12,
1148         .expect_err = 0,
1149         .bcast = true,
1150         .base = base_lc3_ac_12,
1151         .base_len = sizeof(base_lc3_ac_12),
1152 };
1153
1154 static const struct iso_client_data bcast_ac_13 = {
1155         .qos = BCAST_AC_13,
1156         .expect_err = 0,
1157         .bcast = true,
1158         .mconn = true,
1159         .base = base_lc3_ac_13,
1160         .base_len = sizeof(base_lc3_ac_13),
1161 };
1162
1163 static const struct iso_client_data bcast_ac_14 = {
1164         .qos = BCAST_AC_14,
1165         .expect_err = 0,
1166         .bcast = true,
1167         .base = base_lc3_ac_14,
1168         .base_len = sizeof(base_lc3_ac_14),
1169 };
1170
1171 static void client_connectable_complete(uint16_t opcode, uint8_t status,
1172                                         const void *param, uint8_t len,
1173                                         void *user_data)
1174 {
1175         struct test_data *data = user_data;
1176         static uint8_t client_num;
1177
1178         if (opcode != BT_HCI_CMD_LE_SET_EXT_ADV_ENABLE)
1179                 return;
1180
1181         tester_print("Client %u set connectable status 0x%02x", client_num,
1182                                                                 status);
1183
1184         client_num++;
1185
1186         if (status)
1187                 tester_setup_failed();
1188         else if (data->client_num == client_num) {
1189                 tester_setup_complete();
1190                 client_num = 0;
1191         }
1192 }
1193
1194 static void bthost_recv_data(const void *buf, uint16_t len, void *user_data)
1195 {
1196         struct test_data *data = user_data;
1197         const struct iso_client_data *isodata = data->test_data;
1198
1199         tester_print("Client received %u bytes of data", len);
1200
1201         if (isodata->send && (isodata->send->iov_len != len ||
1202                         memcmp(isodata->send->iov_base, buf, len))) {
1203                 if (!isodata->recv->iov_base)
1204                         tester_test_failed();
1205         } else
1206                 tester_test_passed();
1207 }
1208
1209 static void bthost_iso_disconnected(void *user_data)
1210 {
1211         struct test_data *data = user_data;
1212
1213         tester_print("ISO handle 0x%04x disconnected", data->handle);
1214
1215         data->handle = 0x0000;
1216 }
1217
1218 static void iso_new_conn(uint16_t handle, void *user_data)
1219 {
1220         struct test_data *data = user_data;
1221         struct bthost *host;
1222
1223         tester_print("New client connection with handle 0x%04x", handle);
1224
1225         data->handle = handle;
1226
1227         host = hciemu_client_get_host(data->hciemu);
1228         bthost_add_iso_hook(host, data->handle, bthost_recv_data, data,
1229                                 bthost_iso_disconnected);
1230 }
1231
1232 static uint8_t iso_accept_conn(uint16_t handle, void *user_data)
1233 {
1234         struct test_data *data = user_data;
1235
1236         tester_print("Accept client connection with handle 0x%04x: 0x%02x",
1237                      handle, data->accept_reason);
1238
1239         return data->accept_reason;
1240 }
1241
1242 static void acl_new_conn(uint16_t handle, void *user_data)
1243 {
1244         struct test_data *data = user_data;
1245
1246         tester_print("New ACL connection with handle 0x%04x", handle);
1247
1248         data->acl_handle = handle;
1249 }
1250
1251 static void setup_powered_callback(uint8_t status, uint16_t length,
1252                                         const void *param, void *user_data)
1253 {
1254         struct test_data *data = tester_get_data();
1255         const struct iso_client_data *isodata = data->test_data;
1256         uint8_t i;
1257
1258         if (status != MGMT_STATUS_SUCCESS) {
1259                 tester_setup_failed();
1260                 return;
1261         }
1262
1263         tester_print("Controller powered on");
1264
1265         for (i = 0; i < data->client_num; i++) {
1266                 struct hciemu_client *client;
1267                 struct bthost *host;
1268
1269                 client = hciemu_get_client(data->hciemu, i);
1270                 host = hciemu_client_host(client);
1271                 bthost_set_cmd_complete_cb(host, client_connectable_complete,
1272                                                                         data);
1273                 bthost_set_ext_adv_params(host);
1274                 bthost_set_ext_adv_enable(host, 0x01);
1275
1276                 if (!isodata)
1277                         continue;
1278
1279                 if (isodata->send || isodata->recv || isodata->disconnect ||
1280                                 isodata->suspend || data->accept_reason)
1281                         bthost_set_iso_cb(host, iso_accept_conn, iso_new_conn,
1282                                                                         data);
1283
1284                 if (isodata->bcast) {
1285                         bthost_set_pa_params(host);
1286                         bthost_set_pa_enable(host, 0x01);
1287                         bthost_create_big(host, 1,
1288                                         isodata->qos.bcast.encryption,
1289                                         isodata->qos.bcast.bcode);
1290                 } else if (!isodata->send && isodata->recv) {
1291                         const uint8_t *bdaddr;
1292
1293                         bdaddr = hciemu_get_central_bdaddr(data->hciemu);
1294                         bthost_set_connect_cb(host, acl_new_conn, data);
1295                         bthost_hci_connect(host, bdaddr, BDADDR_LE_PUBLIC);
1296                 }
1297         }
1298 }
1299
1300 static void setup_powered(const void *test_data)
1301 {
1302         struct test_data *data = tester_get_data();
1303         const struct iso_client_data *isodata = data->test_data;
1304         unsigned char param[] = { 0x01 };
1305
1306         tester_print("Powering on controller");
1307
1308         if (!isodata || !isodata->bcast)
1309                 mgmt_send(data->mgmt, MGMT_OP_SET_CONNECTABLE, data->mgmt_index,
1310                                         sizeof(param), param,
1311                                         NULL, NULL, NULL);
1312
1313         mgmt_send(data->mgmt, MGMT_OP_SET_SSP, data->mgmt_index,
1314                                 sizeof(param), param, NULL, NULL, NULL);
1315
1316         mgmt_send(data->mgmt, MGMT_OP_SET_LE, data->mgmt_index,
1317                                 sizeof(param), param, NULL, NULL, NULL);
1318
1319         if (isodata && isodata->server && !isodata->bcast)
1320                 mgmt_send(data->mgmt, MGMT_OP_SET_ADVERTISING,
1321                                 data->mgmt_index, sizeof(param), param, NULL,
1322                                 NULL, NULL);
1323
1324         mgmt_send(data->mgmt, MGMT_OP_SET_POWERED, data->mgmt_index,
1325                                         sizeof(param), param,
1326                                         setup_powered_callback, NULL, NULL);
1327 }
1328
1329 static void test_framework(const void *test_data)
1330 {
1331         tester_test_passed();
1332 }
1333
1334 static void test_socket(const void *test_data)
1335 {
1336         int sk;
1337
1338         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_ISO);
1339         if (sk < 0) {
1340                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
1341                                                                         errno);
1342                 tester_test_abort();
1343                 return;
1344         }
1345
1346         close(sk);
1347
1348         tester_test_passed();
1349 }
1350
1351 static void test_getsockopt(const void *test_data)
1352 {
1353         int sk, err;
1354         socklen_t len;
1355         struct bt_iso_qos qos;
1356
1357         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_ISO);
1358         if (sk < 0) {
1359                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
1360                                                                         errno);
1361                 tester_test_abort();
1362                 return;
1363         }
1364
1365         len = sizeof(qos);
1366         memset(&qos, 0, len);
1367
1368         err = getsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos, &len);
1369         if (err < 0) {
1370                 tester_warn("Can't get socket option : %s (%d)",
1371                                                         strerror(errno), errno);
1372                 tester_test_failed();
1373                 goto end;
1374         }
1375
1376         tester_test_passed();
1377
1378 end:
1379         close(sk);
1380 }
1381
1382 static void test_setsockopt(const void *test_data)
1383 {
1384         int sk, err;
1385         socklen_t len;
1386         struct bt_iso_qos qos = QOS_16_1_2;
1387         int pkt_status = 1;
1388
1389         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_ISO);
1390         if (sk < 0) {
1391                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
1392                                                                         errno);
1393                 tester_test_abort();
1394                 goto end;
1395         }
1396
1397         err = setsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos, sizeof(qos));
1398         if (err < 0) {
1399                 tester_warn("Can't set socket option : %s (%d)",
1400                                                         strerror(errno), errno);
1401                 tester_test_failed();
1402                 goto end;
1403         }
1404
1405         len = sizeof(qos);
1406         memset(&qos, 0, len);
1407
1408         err = getsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos, &len);
1409         if (err < 0) {
1410                 tester_warn("Can't get socket option : %s (%d)",
1411                                                         strerror(errno), errno);
1412                 tester_test_failed();
1413                 goto end;
1414         }
1415
1416         err = setsockopt(sk, SOL_BLUETOOTH, BT_PKT_STATUS, &pkt_status,
1417                          sizeof(pkt_status));
1418         if (err < 0) {
1419                 tester_warn("Can't set socket BT_PKT_STATUS option: "
1420                                 "%s (%d)", strerror(errno), errno);
1421                 tester_test_failed();
1422                 goto end;
1423         }
1424
1425         len = sizeof(pkt_status);
1426         memset(&pkt_status, 0, len);
1427
1428         err = getsockopt(sk, SOL_BLUETOOTH, BT_PKT_STATUS, &pkt_status, &len);
1429         if (err < 0) {
1430                 tester_warn("Can't get socket option : %s (%d)",
1431                                                         strerror(errno), errno);
1432                 tester_test_failed();
1433                 goto end;
1434         }
1435
1436         tester_test_passed();
1437
1438 end:
1439         close(sk);
1440 }
1441
1442 static int create_iso_sock(struct test_data *data)
1443 {
1444         const uint8_t *master_bdaddr;
1445         struct sockaddr_iso addr;
1446         int sk, err;
1447
1448         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_NONBLOCK, BTPROTO_ISO);
1449         if (sk < 0) {
1450                 err = -errno;
1451                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
1452                                                                         errno);
1453                 return err;
1454         }
1455
1456         master_bdaddr = hciemu_get_central_bdaddr(data->hciemu);
1457         if (!master_bdaddr) {
1458                 tester_warn("No master bdaddr");
1459                 return -ENODEV;
1460         }
1461
1462         memset(&addr, 0, sizeof(addr));
1463         addr.iso_family = AF_BLUETOOTH;
1464         bacpy(&addr.iso_bdaddr, (void *) master_bdaddr);
1465         addr.iso_bdaddr_type = BDADDR_LE_PUBLIC;
1466
1467         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1468                 err = -errno;
1469                 tester_warn("Can't bind socket: %s (%d)", strerror(errno),
1470                                                                         errno);
1471                 close(sk);
1472                 return err;
1473         }
1474
1475         return sk;
1476 }
1477
1478 static int connect_iso_sock(struct test_data *data, uint8_t num, int sk)
1479 {
1480         const struct iso_client_data *isodata = data->test_data;
1481         struct hciemu_client *client;
1482         const uint8_t *client_bdaddr = NULL;
1483         const struct bt_iso_qos *qos = &isodata->qos;
1484         struct sockaddr_iso addr;
1485         char str[18];
1486         int err;
1487
1488         client = hciemu_get_client(data->hciemu, num);
1489         if (!client) {
1490                 if (!isodata->mconn) {
1491                         tester_warn("No client");
1492                         return -ENODEV;
1493                 }
1494
1495                 client = hciemu_get_client(data->hciemu, 0);
1496                 if (!client) {
1497                         tester_warn("No client");
1498                         return -ENODEV;
1499                 }
1500         }
1501
1502         if (!isodata->bcast && num && isodata->mconn)
1503                 qos = &isodata->qos_2;
1504
1505         if (!isodata->bcast) {
1506                 client_bdaddr = hciemu_client_bdaddr(client);
1507                 if (!client_bdaddr) {
1508                         tester_warn("No client bdaddr");
1509                         return -ENODEV;
1510                 }
1511         } else if (!isodata->server) {
1512                 err = setsockopt(sk, SOL_BLUETOOTH, BT_ISO_BASE,
1513                                 isodata->base, isodata->base_len);
1514                 if (err < 0) {
1515                         tester_warn("Can't set socket BT_ISO_BASE option: "
1516                                         "%s (%d)", strerror(errno), errno);
1517                         tester_test_failed();
1518                         return -EINVAL;
1519                 }
1520         }
1521
1522         err = setsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, qos, sizeof(*qos));
1523         if (err < 0) {
1524                 tester_warn("Can't set socket BT_ISO_QOS option : %s (%d)",
1525                                                         strerror(errno), errno);
1526                 tester_test_failed();
1527                 return -EINVAL;
1528         }
1529
1530         if (isodata->defer || (isodata->bcast && isodata->mconn && !num)) {
1531                 int opt = 1;
1532
1533                 if (setsockopt(sk, SOL_BLUETOOTH, BT_DEFER_SETUP, &opt,
1534                                                         sizeof(opt)) < 0) {
1535                         tester_print("Can't enable deferred setup: %s (%d)",
1536                                                 strerror(errno), errno);
1537                         tester_test_failed();
1538                         return -EINVAL;
1539                 }
1540         }
1541
1542         memset(&addr, 0, sizeof(addr));
1543         addr.iso_family = AF_BLUETOOTH;
1544         bacpy(&addr.iso_bdaddr, client_bdaddr ? (void *) client_bdaddr :
1545                                                         BDADDR_ANY);
1546         addr.iso_bdaddr_type = BDADDR_LE_PUBLIC;
1547
1548         ba2str(&addr.iso_bdaddr, str);
1549
1550         tester_print("Connecting to %s...", str);
1551
1552         err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
1553         if (err < 0 && !(errno == EAGAIN || errno == EINPROGRESS)) {
1554                 err = -errno;
1555                 tester_warn("Can't connect socket: %s (%d)", strerror(errno),
1556                                                                         errno);
1557                 return err;
1558         }
1559
1560         return 0;
1561 }
1562
1563 static bool check_io_qos(const struct bt_iso_io_qos *io1,
1564                                 const struct bt_iso_io_qos *io2)
1565 {
1566         if (io1->interval && io2->interval && io1->interval > io2->interval) {
1567                 tester_warn("Unexpected IO interval: %u > %u",
1568                                 io1->interval, io2->interval);
1569                 return false;
1570         }
1571
1572         if (io1->latency && io2->latency && io1->latency > io2->latency) {
1573                 tester_warn("Unexpected IO latency: %u > %u",
1574                                 io1->latency, io2->latency);
1575                 return false;
1576         }
1577
1578         if (io1->sdu && io2->sdu && io1->sdu != io2->sdu) {
1579                 tester_warn("Unexpected IO SDU: %u != %u", io1->sdu, io2->sdu);
1580                 return false;
1581         }
1582
1583         if (io1->phy && io2->phy && io1->phy != io2->phy) {
1584                 tester_warn("Unexpected IO PHY: 0x%02x != 0x%02x",
1585                                 io1->phy, io2->phy);
1586                 return false;
1587         }
1588
1589         if (io1->rtn && io2->rtn && io1->rtn != io2->rtn) {
1590                 tester_warn("Unexpected IO RTN: %u != %u", io1->rtn, io2->rtn);
1591                 return false;
1592         }
1593
1594         return true;
1595 }
1596
1597 static bool check_ucast_qos(const struct bt_iso_qos *qos1,
1598                                 const struct bt_iso_qos *qos2,
1599                                 const struct bt_iso_qos *qos2_2)
1600 {
1601         if (qos1->ucast.cig != BT_ISO_QOS_CIG_UNSET &&
1602                         qos2->ucast.cig != BT_ISO_QOS_CIG_UNSET &&
1603                         qos1->ucast.cig != qos2->ucast.cig) {
1604                 if (qos2_2)
1605                         return check_ucast_qos(qos1, qos2_2, NULL);
1606
1607                 tester_warn("Unexpected CIG ID: 0x%02x != 0x%02x",
1608                                 qos1->ucast.cig, qos2->ucast.cig);
1609                 return false;
1610         }
1611
1612         if (qos1->ucast.cis != BT_ISO_QOS_CIS_UNSET &&
1613                         qos2->ucast.cis != BT_ISO_QOS_CIS_UNSET &&
1614                         qos1->ucast.cis != qos2->ucast.cis) {
1615                 if (qos2_2)
1616                         return check_ucast_qos(qos1, qos2_2, NULL);
1617
1618                 tester_warn("Unexpected CIS ID: 0x%02x != 0x%02x",
1619                                 qos1->ucast.cis, qos2->ucast.cis);
1620                 return false;
1621         }
1622
1623         if (qos1->ucast.packing != qos2->ucast.packing) {
1624                 if (qos2_2)
1625                         return check_ucast_qos(qos1, qos2_2, NULL);
1626
1627                 tester_warn("Unexpected QoS packing: 0x%02x != 0x%02x",
1628                                 qos1->ucast.packing, qos2->ucast.packing);
1629                 return false;
1630         }
1631
1632         if (qos1->ucast.framing != qos2->ucast.framing) {
1633                 if (qos2_2)
1634                         return check_ucast_qos(qos1, qos2_2, NULL);
1635
1636                 tester_warn("Unexpected QoS framing: 0x%02x != 0x%02x",
1637                                 qos1->ucast.framing, qos2->ucast.framing);
1638                 return false;
1639         }
1640
1641         if (!check_io_qos(&qos1->ucast.in, &qos2->ucast.in)) {
1642                 if (qos2_2)
1643                         return check_ucast_qos(qos1, qos2_2, NULL);
1644
1645                 tester_warn("Unexpected Input QoS");
1646                 return false;
1647         }
1648
1649         if (!check_io_qos(&qos1->ucast.out, &qos2->ucast.out)) {
1650                 if (qos2_2)
1651                         return check_ucast_qos(qos1, qos2_2, NULL);
1652
1653                 tester_warn("Unexpected Output QoS");
1654                 return false;
1655         }
1656
1657         return true;
1658 }
1659
1660 static bool check_bcast_qos(const struct bt_iso_qos *qos1,
1661                                 const struct bt_iso_qos *qos2)
1662 {
1663         if (qos1->bcast.big != BT_ISO_QOS_BIG_UNSET &&
1664                         qos2->bcast.big != BT_ISO_QOS_BIG_UNSET &&
1665                         qos1->bcast.big != qos2->bcast.big) {
1666                 tester_warn("Unexpected BIG ID: 0x%02x != 0x%02x",
1667                                 qos1->bcast.big, qos2->bcast.big);
1668                 return false;
1669         }
1670
1671         if (qos1->bcast.bis != BT_ISO_QOS_BIS_UNSET &&
1672                         qos2->bcast.bis != BT_ISO_QOS_BIS_UNSET &&
1673                         qos1->bcast.bis != qos2->bcast.bis) {
1674                 tester_warn("Unexpected BIS ID: 0x%02x != 0x%02x",
1675                                 qos1->bcast.bis, qos2->bcast.bis);
1676                 return false;
1677         }
1678
1679         if (qos1->bcast.sync_interval != qos2->bcast.sync_interval) {
1680                 tester_warn("Unexpected QoS sync interval: 0x%02x != 0x%02x",
1681                         qos1->bcast.sync_interval, qos2->bcast.sync_interval);
1682                 return false;
1683         }
1684
1685         if (qos1->bcast.packing != qos2->bcast.packing) {
1686                 tester_warn("Unexpected QoS packing: 0x%02x != 0x%02x",
1687                                 qos1->bcast.packing, qos2->bcast.packing);
1688                 return false;
1689         }
1690
1691         if (qos1->bcast.framing != qos2->bcast.framing) {
1692                 tester_warn("Unexpected QoS framing: 0x%02x != 0x%02x",
1693                                 qos1->bcast.framing, qos2->bcast.framing);
1694                 return false;
1695         }
1696
1697         if (!check_io_qos(&qos1->ucast.in, &qos2->ucast.in)) {
1698                 tester_warn("Unexpected Input QoS");
1699                 return false;
1700         }
1701
1702         if (!check_io_qos(&qos1->ucast.out, &qos2->ucast.out)) {
1703                 tester_warn("Unexpected Output QoS");
1704                 return false;
1705         }
1706
1707         if (qos1->bcast.encryption != qos2->bcast.encryption) {
1708                 tester_warn("Unexpected QoS encryption: 0x%02x != 0x%02x",
1709                                 qos1->bcast.encryption, qos2->bcast.encryption);
1710                 return false;
1711         }
1712
1713         if (memcmp(qos1->bcast.bcode, qos2->bcast.bcode,
1714                                 sizeof(qos1->bcast.bcode))) {
1715                 tester_warn("Unexpected QoS Broadcast Code");
1716                 return false;
1717         }
1718
1719         if (qos1->bcast.options != qos2->bcast.options) {
1720                 tester_warn("Unexpected QoS options: 0x%02x != 0x%02x",
1721                                 qos1->bcast.options, qos2->bcast.options);
1722                 return false;
1723         }
1724
1725         if (qos1->bcast.skip != qos2->bcast.skip) {
1726                 tester_warn("Unexpected QoS skip: 0x%04x != 0x%04x",
1727                                 qos1->bcast.skip, qos2->bcast.skip);
1728                 return false;
1729         }
1730
1731         if (qos1->bcast.sync_timeout != qos2->bcast.sync_timeout) {
1732                 tester_warn("Unexpected QoS sync timeout: 0x%04x != 0x%04x",
1733                         qos1->bcast.sync_timeout, qos2->bcast.sync_timeout);
1734                 return false;
1735         }
1736
1737         if (qos1->bcast.sync_cte_type != qos2->bcast.sync_cte_type) {
1738                 tester_warn("Unexpected QoS sync cte type: 0x%02x != 0x%02x",
1739                         qos1->bcast.sync_cte_type, qos2->bcast.sync_cte_type);
1740                 return false;
1741         }
1742
1743         if (qos1->bcast.mse != qos2->bcast.mse) {
1744                 tester_warn("Unexpected QoS MSE: 0x%02x != 0x%02x",
1745                                 qos1->bcast.mse, qos2->bcast.mse);
1746                 return false;
1747         }
1748
1749         if (qos1->bcast.timeout != qos2->bcast.timeout) {
1750                 tester_warn("Unexpected QoS MSE: 0x%04x != 0x%04x",
1751                                 qos1->bcast.timeout, qos2->bcast.timeout);
1752                 return false;
1753         }
1754
1755         return true;
1756 }
1757
1758 static gboolean iso_recv_data(GIOChannel *io, GIOCondition cond,
1759                                                         gpointer user_data)
1760 {
1761         struct test_data *data = user_data;
1762         const struct iso_client_data *isodata = data->test_data;
1763         int sk = g_io_channel_unix_get_fd(io);
1764         unsigned char control[64];
1765         ssize_t ret;
1766         char buf[1024];
1767         struct msghdr msg;
1768         struct iovec iov;
1769
1770         data->io_id[0] = 0;
1771
1772         iov.iov_base = buf;
1773         iov.iov_len = isodata->recv->iov_len;
1774
1775         memset(&msg, 0, sizeof(msg));
1776         msg.msg_iov = &iov;
1777         msg.msg_iovlen = 1;
1778         msg.msg_control = control;
1779         msg.msg_controllen = sizeof(control);
1780
1781         ret = recvmsg(sk, &msg, MSG_DONTWAIT);
1782         if (ret < 0 || isodata->recv->iov_len != (size_t) ret) {
1783                 tester_warn("Failed to read %zu bytes: %s (%d)",
1784                                 isodata->recv->iov_len, strerror(errno), errno);
1785                 tester_test_failed();
1786                 return FALSE;
1787         }
1788
1789         if (isodata->pkt_status) {
1790                 struct cmsghdr *cmsg;
1791                 uint8_t pkt_status = 0;
1792
1793                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
1794                                         cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1795                         if (cmsg->cmsg_level != SOL_BLUETOOTH)
1796                                 continue;
1797
1798                         if (cmsg->cmsg_type == BT_SCM_PKT_STATUS) {
1799                                 memcpy(&pkt_status, CMSG_DATA(cmsg),
1800                                                 sizeof(pkt_status));
1801                                 tester_debug("BT_SCM_PKT_STATUS = 0x%2.2x",
1802                                                         pkt_status);
1803                                 break;
1804                         }
1805                 }
1806
1807                 if (isodata->pkt_status != pkt_status) {
1808                         tester_warn("isodata->pkt_status 0x%2.2x != 0x%2.2x "
1809                                         "pkt_status", isodata->pkt_status,
1810                                         pkt_status);
1811                         tester_test_failed();
1812                 } else
1813                         tester_test_passed();
1814
1815                 return FALSE;
1816         }
1817
1818         if (memcmp(buf, isodata->recv->iov_base, ret))
1819                 tester_test_failed();
1820         else
1821                 tester_test_passed();
1822
1823         return FALSE;
1824 }
1825
1826 static void iso_recv(struct test_data *data, GIOChannel *io)
1827 {
1828         const struct iso_client_data *isodata = data->test_data;
1829         struct bthost *host;
1830         static uint16_t sn;
1831
1832         tester_print("Receive %zu bytes of data", isodata->recv->iov_len);
1833
1834         if (!data->handle) {
1835                 tester_warn("ISO handle not set");
1836                 tester_test_failed();
1837                 return;
1838         }
1839
1840         host = hciemu_client_get_host(data->hciemu);
1841         bthost_send_iso(host, data->handle, isodata->ts, sn++, 0,
1842                                 isodata->pkt_status, isodata->recv, 1);
1843
1844         data->io_id[0] = g_io_add_watch(io, G_IO_IN, iso_recv_data, data);
1845 }
1846
1847 static void iso_send(struct test_data *data, GIOChannel *io)
1848 {
1849         const struct iso_client_data *isodata = data->test_data;
1850         ssize_t ret;
1851         int sk;
1852
1853         sk = g_io_channel_unix_get_fd(io);
1854
1855         tester_print("Writing %zu bytes of data", isodata->send->iov_len);
1856
1857         ret = writev(sk, isodata->send, 1);
1858         if (ret < 0 || isodata->send->iov_len != (size_t) ret) {
1859                 tester_warn("Failed to write %zu bytes: %s (%d)",
1860                                 isodata->send->iov_len, strerror(errno), errno);
1861                 tester_test_failed();
1862                 return;
1863         }
1864
1865         if (isodata->bcast) {
1866                 tester_test_passed();
1867                 return;
1868         }
1869
1870         if (isodata->recv)
1871                 iso_recv(data, io);
1872 }
1873
1874 static void test_connect(const void *test_data);
1875 static gboolean iso_connect_cb(GIOChannel *io, GIOCondition cond,
1876                                                         gpointer user_data);
1877 static gboolean iso_accept_cb(GIOChannel *io, GIOCondition cond,
1878                                                         gpointer user_data);
1879
1880 static gboolean iso_disconnected(GIOChannel *io, GIOCondition cond,
1881                                                         gpointer user_data)
1882 {
1883         struct test_data *data = user_data;
1884
1885         data->io_id[0] = 0;
1886
1887         if ((cond & G_IO_HUP) && !data->handle) {
1888                 tester_print("Successfully disconnected");
1889
1890                 if (data->reconnect) {
1891                         data->reconnect = false;
1892                         test_connect(data->test_data);
1893                         return FALSE;
1894                 }
1895
1896                 tester_test_passed();
1897         } else
1898                 tester_test_failed();
1899
1900         return FALSE;
1901 }
1902
1903 static void iso_shutdown(struct test_data *data, GIOChannel *io)
1904 {
1905         int sk;
1906
1907         sk = g_io_channel_unix_get_fd(io);
1908
1909         data->io_id[0] = g_io_add_watch(io, G_IO_HUP, iso_disconnected, data);
1910
1911         /* Shutdown using SHUT_WR as SHUT_RDWR cause the socket to HUP
1912          * immediately instead of waiting for Disconnect Complete event.
1913          */
1914         shutdown(sk, SHUT_WR);
1915
1916         tester_print("Disconnecting...");
1917 }
1918
1919 static bool hook_set_event_mask(const void *msg, uint16_t len, void *user_data)
1920 {
1921         struct test_data *data = user_data;
1922
1923         tester_print("Set Event Mask");
1924
1925         --data->step;
1926         if (!data->step)
1927                 tester_test_passed();
1928
1929         return true;
1930 }
1931
1932 static void trigger_force_suspend(void *user_data)
1933 {
1934         struct test_data *data = tester_get_data();
1935         struct vhci *vhci = hciemu_get_vhci(data->hciemu);
1936         int err;
1937
1938         /* Make sure suspend is only triggered once */
1939         if (data->suspending)
1940                 return;
1941
1942         data->suspending = true;
1943
1944         /* Triggers the suspend */
1945         tester_print("Set the system into Suspend via force_suspend");
1946         err = vhci_set_force_suspend(vhci, true);
1947         if (err) {
1948                 tester_warn("Unable to enable the force_suspend");
1949                 return;
1950         }
1951
1952         data->step++;
1953
1954         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_CMD,
1955                                         BT_HCI_CMD_SET_EVENT_MASK,
1956                                         hook_set_event_mask, data);
1957 }
1958
1959 static gboolean iso_connect(GIOChannel *io, GIOCondition cond,
1960                                                         gpointer user_data)
1961 {
1962         struct test_data *data = tester_get_data();
1963         const struct iso_client_data *isodata = data->test_data;
1964         int err, sk_err, sk;
1965         socklen_t len;
1966         struct bt_iso_qos qos;
1967         bool ret = true;
1968
1969         sk = g_io_channel_unix_get_fd(io);
1970
1971         len = sizeof(qos);
1972         memset(&qos, 0, len);
1973
1974         err = getsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &qos, &len);
1975         if (err < 0) {
1976                 tester_warn("Can't get socket option : %s (%d)",
1977                                                         strerror(errno), errno);
1978                 tester_test_failed();
1979                 return FALSE;
1980         }
1981
1982         if (!isodata->bcast) {
1983                 ret = check_ucast_qos(&qos, &isodata->qos,
1984                                       isodata->mconn ? &isodata->qos_2 : NULL);
1985         } else if (!isodata->server)
1986                 ret = check_bcast_qos(&qos, &isodata->qos);
1987
1988         if (!ret) {
1989                 tester_warn("Unexpected QoS parameter");
1990                 tester_test_failed();
1991                 return FALSE;
1992         }
1993
1994         len = sizeof(sk_err);
1995
1996         if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
1997                 err = -errno;
1998         else
1999                 err = -sk_err;
2000
2001         if (err < 0)
2002                 tester_warn("Connect failed: %s (%d)", strerror(-err), -err);
2003         else
2004                 tester_print("Successfully connected");
2005
2006         if (err != isodata->expect_err) {
2007                 tester_warn("Expect error: %s (%d) != %s (%d)",
2008                                 strerror(-isodata->expect_err),
2009                                 -isodata->expect_err, strerror(-err), -err);
2010                 tester_test_failed();
2011         } else {
2012                 data->step--;
2013                 if (data->step)
2014                         tester_print("Step %u", data->step);
2015                 else if (isodata->send)
2016                         iso_send(data, io);
2017                 else if (isodata->recv)
2018                         iso_recv(data, io);
2019                 else if (isodata->disconnect)
2020                         iso_shutdown(data, io);
2021                 else if (isodata->suspend)
2022                         trigger_force_suspend(data);
2023                 else
2024                         tester_test_passed();
2025         }
2026
2027         return FALSE;
2028 }
2029
2030 static gboolean iso_connect_cb(GIOChannel *io, GIOCondition cond,
2031                                                         gpointer user_data)
2032 {
2033         struct test_data *data = tester_get_data();
2034
2035         data->io_id[0] = 0;
2036
2037         return iso_connect(io, cond, user_data);
2038 }
2039
2040 static gboolean iso_connect2_cb(GIOChannel *io, GIOCondition cond,
2041                                                         gpointer user_data)
2042 {
2043         struct test_data *data = tester_get_data();
2044
2045         data->io_id[1] = 0;
2046
2047         return iso_connect(io, cond, user_data);
2048 }
2049
2050 static int setup_sock(struct test_data *data, uint8_t num)
2051 {
2052         int sk, err;
2053
2054         sk = create_iso_sock(data);
2055         if (sk < 0) {
2056                 if (sk == -EPROTONOSUPPORT)
2057                         tester_test_abort();
2058                 else
2059                         tester_test_failed();
2060
2061                 return sk;
2062         }
2063
2064         err = connect_iso_sock(data, num, sk);
2065         if (err < 0) {
2066                 const struct iso_client_data *isodata = data->test_data;
2067
2068                 close(sk);
2069
2070                 if (isodata->expect_err == err)
2071                         tester_test_passed();
2072                 else
2073                         tester_test_failed();
2074
2075                 return err;
2076         }
2077
2078         return sk;
2079 }
2080
2081 static int connect_deferred(int sk)
2082 {
2083         int defer;
2084         socklen_t len;
2085         struct pollfd pfd;
2086         char c;
2087
2088         /* Check if socket has DEFER_SETUP set */
2089         len = sizeof(defer);
2090         if (getsockopt(sk, SOL_BLUETOOTH, BT_DEFER_SETUP, &defer,
2091                                         &len) < 0) {
2092                 tester_warn("getsockopt: %s (%d)", strerror(errno),
2093                                 errno);
2094                 tester_test_failed();
2095                 return 0;
2096         }
2097
2098         memset(&pfd, 0, sizeof(pfd));
2099         pfd.fd = sk;
2100         pfd.events = POLLOUT;
2101
2102         if (poll(&pfd, 1, 0) < 0) {
2103                 tester_warn("poll: %s (%d)", strerror(errno), errno);
2104                 tester_test_failed();
2105                 return -EIO;
2106         }
2107
2108         if (!(pfd.revents & POLLOUT)) {
2109                 if (read(sk, &c, 1) < 0) {
2110                         tester_warn("read: %s (%d)", strerror(errno),
2111                                         errno);
2112                         tester_test_failed();
2113                         return -EIO;
2114                 }
2115         }
2116
2117         return 0;
2118 }
2119
2120 static void setup_connect_many(struct test_data *data, uint8_t n, uint8_t *num,
2121                                                                 GIOFunc *func)
2122 {
2123         const struct iso_client_data *isodata = data->test_data;
2124         int sk[256];
2125         GIOChannel *io;
2126         unsigned int i;
2127
2128         for (i = 0; i < n; ++i) {
2129                 sk[i] = setup_sock(data, num[i]);
2130                 if (sk[i] < 0)
2131                         return;
2132         }
2133
2134         if (isodata->defer) {
2135                 for (i = 0; i < n; ++i)
2136                         if (connect_deferred(sk[i]) < 0)
2137                                 return;
2138         }
2139
2140         for (i = 0; i < n; ++i) {
2141                 io = g_io_channel_unix_new(sk[i]);
2142                 g_io_channel_set_close_on_unref(io, TRUE);
2143
2144                 data->io_id[num[i]] = g_io_add_watch(io, G_IO_OUT, func[i],
2145                                                                         NULL);
2146
2147                 g_io_channel_unref(io);
2148
2149                 tester_print("Connect %d in progress", num[i]);
2150
2151                 data->step++;
2152         }
2153 }
2154
2155 static void setup_connect(struct test_data *data, uint8_t num, GIOFunc func)
2156 {
2157         return setup_connect_many(data, 1, &num, &func);
2158 }
2159
2160 static void test_connect(const void *test_data)
2161 {
2162         struct test_data *data = tester_get_data();
2163         const struct iso_client_data *isodata = test_data;
2164         uint8_t n = 0;
2165         GIOFunc func[2];
2166         uint8_t num[2] = {0, 1};
2167
2168         func[n++] = iso_connect_cb;
2169
2170         /* Check if configuration requires multiple CIS setup */
2171         if (!isodata->bcast && isodata->mconn)
2172                 func[n++] = iso_connect2_cb;
2173
2174         setup_connect_many(data, n, num, func);
2175 }
2176
2177 static void test_reconnect(const void *test_data)
2178 {
2179         struct test_data *data = tester_get_data();
2180
2181         data->reconnect = true;
2182         test_connect(test_data);
2183 }
2184
2185 static void test_defer(const void *test_data)
2186 {
2187         struct test_data *data = tester_get_data();
2188         const struct iso_client_data *isodata = data->test_data;
2189         int sk, err;
2190
2191         sk = create_iso_sock(data);
2192         if (sk < 0) {
2193                 if (sk == -EPROTONOSUPPORT)
2194                         tester_test_abort();
2195                 else
2196                         tester_test_failed();
2197                 return;
2198         }
2199
2200         err = connect_iso_sock(data, 0, sk);
2201         if (err < 0) {
2202                 close(sk);
2203
2204                 if (isodata->expect_err == err)
2205                         tester_test_passed();
2206                 else
2207                         tester_test_failed();
2208
2209                 return;
2210         }
2211
2212         err = close(sk);
2213
2214         if (isodata->expect_err == err)
2215                 tester_test_passed();
2216         else
2217                 tester_test_failed();
2218 }
2219
2220 static int listen_iso_sock(struct test_data *data)
2221 {
2222         const struct iso_client_data *isodata = data->test_data;
2223         const uint8_t *src, *dst;
2224         struct sockaddr_iso *addr = NULL;
2225         int sk, err;
2226
2227         sk = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_NONBLOCK, BTPROTO_ISO);
2228         if (sk < 0) {
2229                 err = -errno;
2230                 tester_warn("Can't create socket: %s (%d)", strerror(errno),
2231                                                                         errno);
2232                 return err;
2233         }
2234
2235         src = hciemu_get_central_bdaddr(data->hciemu);
2236         if (!src) {
2237                 tester_warn("No source bdaddr");
2238                 err = -ENODEV;
2239                 goto fail;
2240         }
2241
2242         /* Bind to local address */
2243         addr = malloc(sizeof(*addr) + sizeof(*addr->iso_bc));
2244         memset(addr, 0, sizeof(*addr) + sizeof(*addr->iso_bc));
2245         addr->iso_family = AF_BLUETOOTH;
2246         bacpy(&addr->iso_bdaddr, (void *) src);
2247         addr->iso_bdaddr_type = BDADDR_LE_PUBLIC;
2248
2249         if (isodata->bcast) {
2250                 /* Bind to destination address in case of broadcast */
2251                 dst = hciemu_get_client_bdaddr(data->hciemu);
2252                 if (!dst) {
2253                         tester_warn("No source bdaddr");
2254                         err = -ENODEV;
2255                         goto fail;
2256                 }
2257
2258                 bacpy(&addr->iso_bc->bc_bdaddr, (void *) dst);
2259                 addr->iso_bc->bc_bdaddr_type = BDADDR_LE_PUBLIC;
2260                 addr->iso_bc->bc_num_bis = 1;
2261                 addr->iso_bc->bc_bis[0] = 1;
2262
2263                 err = bind(sk, (struct sockaddr *) addr, sizeof(*addr) +
2264                                                    sizeof(*addr->iso_bc));
2265         } else
2266                 err = bind(sk, (struct sockaddr *) addr, sizeof(*addr));
2267
2268
2269         if (err < 0) {
2270                 err = -errno;
2271                 tester_warn("Can't bind socket: %s (%d)", strerror(errno),
2272                                                                         errno);
2273                 goto fail;
2274         }
2275
2276         if (isodata->defer) {
2277                 int opt = 1;
2278
2279                 if (setsockopt(sk, SOL_BLUETOOTH, BT_DEFER_SETUP, &opt,
2280                                                         sizeof(opt)) < 0) {
2281                         tester_print("Can't enable deferred setup: %s (%d)",
2282                                                 strerror(errno), errno);
2283                         goto fail;
2284                 }
2285         }
2286
2287         if (setsockopt(sk, SOL_BLUETOOTH, BT_ISO_QOS, &isodata->qos,
2288                                                 sizeof(isodata->qos)) < 0) {
2289                 tester_print("Can't set socket BT_ISO_QOS option: %s (%d)",
2290                                         strerror(errno), errno);
2291                 goto fail;
2292         }
2293
2294         if (listen(sk, 10)) {
2295                 err = -errno;
2296                 tester_warn("Can't listen socket: %s (%d)", strerror(errno),
2297                                                                         errno);
2298                 goto fail;
2299         }
2300
2301         free(addr);
2302
2303         return sk;
2304
2305 fail:
2306         free(addr);
2307         close(sk);
2308         return err;
2309 }
2310
2311 static void setup_listen(struct test_data *data, uint8_t num, GIOFunc func)
2312 {
2313         const struct iso_client_data *isodata = data->test_data;
2314         GIOChannel *io;
2315         int sk;
2316
2317         sk = listen_iso_sock(data);
2318         if (sk < 0) {
2319                 if (sk == -EPROTONOSUPPORT)
2320                         tester_test_abort();
2321                 else
2322                         tester_test_failed();
2323                 return;
2324         }
2325
2326         io = g_io_channel_unix_new(sk);
2327         g_io_channel_set_close_on_unref(io, TRUE);
2328
2329         data->io_id[num] = g_io_add_watch(io, G_IO_IN, func, NULL);
2330
2331         g_io_channel_unref(io);
2332
2333         tester_print("Listen in progress");
2334
2335         data->step++;
2336
2337         if (!isodata->bcast) {
2338                 struct hciemu_client *client;
2339                 struct bthost *host;
2340
2341                 if (!data->acl_handle) {
2342                         tester_print("ACL handle not set");
2343                         tester_test_failed();
2344                         return;
2345                 }
2346
2347                 client = hciemu_get_client(data->hciemu, 0);
2348                 host = hciemu_client_host(client);
2349
2350                 bthost_set_cig_params(host, 0x01, 0x01, &isodata->qos);
2351                 bthost_create_cis(host, 257, data->acl_handle);
2352         }
2353 }
2354
2355 static bool iso_defer_accept(struct test_data *data, GIOChannel *io)
2356 {
2357         int sk;
2358         char c;
2359         struct pollfd pfd;
2360         const struct iso_client_data *isodata = data->test_data;
2361
2362         sk = g_io_channel_unix_get_fd(io);
2363
2364         memset(&pfd, 0, sizeof(pfd));
2365         pfd.fd = sk;
2366         pfd.events = POLLOUT;
2367
2368         if (poll(&pfd, 1, 0) < 0) {
2369                 tester_warn("poll: %s (%d)", strerror(errno), errno);
2370                 return false;
2371         }
2372
2373         if (!(pfd.revents & POLLOUT)) {
2374                 if (read(sk, &c, 1) < 0) {
2375                         tester_warn("read: %s (%d)", strerror(errno), errno);
2376                         return false;
2377                 }
2378         }
2379
2380         tester_print("Accept deferred setup");
2381
2382         data->io = io;
2383
2384         if (isodata->bcast) {
2385                 data->io_id[0] = g_io_add_watch(io, G_IO_IN,
2386                                         iso_accept_cb, NULL);
2387                 data->step++;
2388         } else {
2389                 data->io_id[0] = g_io_add_watch(io, G_IO_OUT,
2390                                         iso_connect_cb, NULL);
2391         }
2392
2393         return true;
2394 }
2395
2396 static gboolean iso_accept_cb(GIOChannel *io, GIOCondition cond,
2397                                                         gpointer user_data)
2398 {
2399         struct test_data *data = tester_get_data();
2400         const struct iso_client_data *isodata = data->test_data;
2401         int sk, new_sk;
2402
2403         data->io_id[0] = 0;
2404
2405         sk = g_io_channel_unix_get_fd(io);
2406
2407         new_sk = accept(sk, NULL, NULL);
2408         if (new_sk < 0) {
2409                 tester_test_failed();
2410                 return false;
2411         }
2412
2413         io = g_io_channel_unix_new(new_sk);
2414         g_io_channel_set_close_on_unref(io, TRUE);
2415
2416         if (isodata->defer) {
2417                 if (isodata->expect_err < 0) {
2418                         g_io_channel_unref(io);
2419                         tester_test_passed();
2420                         return false;
2421                 }
2422
2423                 if (isodata->bcast && data->step > 1) {
2424                         data->step--;
2425                         goto connect;
2426                 }
2427
2428                 if (!iso_defer_accept(data, io)) {
2429                         tester_warn("Unable to accept deferred setup");
2430                         tester_test_failed();
2431                 }
2432                 return false;
2433         }
2434
2435         if (isodata->pkt_status) {
2436                 int opt = 1;
2437
2438                 if (setsockopt(new_sk, SOL_BLUETOOTH, BT_PKT_STATUS, &opt,
2439                                                         sizeof(opt)) < 0) {
2440                         tester_print("Can't set socket BT_PKT_STATUS option: "
2441                                         "%s (%d)", strerror(errno), errno);
2442                         tester_test_failed();
2443                         return false;
2444                 }
2445         }
2446
2447 connect:
2448         return iso_connect(io, cond, user_data);
2449 }
2450
2451 static void test_listen(const void *test_data)
2452 {
2453         struct test_data *data = tester_get_data();
2454
2455         setup_listen(data, 0, iso_accept_cb);
2456 }
2457
2458 static void test_connect2(const void *test_data)
2459 {
2460         struct test_data *data = tester_get_data();
2461         uint8_t num[2] = {0, 1};
2462         GIOFunc funcs[2] = {iso_connect_cb, iso_connect2_cb};
2463
2464         setup_connect_many(data, 2, num, funcs);
2465 }
2466
2467 static gboolean iso_connect2_seq_cb(GIOChannel *io, GIOCondition cond,
2468                                                         gpointer user_data)
2469 {
2470         struct test_data *data = tester_get_data();
2471
2472         data->io_id[0] = 0;
2473
2474         setup_connect(data, 1, iso_connect2_cb);
2475
2476         return iso_connect(io, cond, user_data);
2477 }
2478
2479 static void test_connect2_seq(const void *test_data)
2480 {
2481         struct test_data *data = tester_get_data();
2482
2483         setup_connect(data, 0, iso_connect2_seq_cb);
2484 }
2485
2486 static gboolean test_connect2_busy_done(gpointer user_data)
2487 {
2488         struct test_data *data = tester_get_data();
2489
2490         if (data->io_id[0] > 0) {
2491                 /* First connection still exists */
2492                 g_source_remove(data->io_id[0]);
2493                 data->io_id[0] = 0;
2494                 tester_test_passed();
2495         } else {
2496                 tester_test_failed();
2497         }
2498
2499         return FALSE;
2500 }
2501
2502 static gboolean iso_connect_cb_busy_disc(GIOChannel *io, GIOCondition cond,
2503                                                         gpointer user_data)
2504 {
2505         struct test_data *data = tester_get_data();
2506
2507         data->io_id[0] = 0;
2508
2509         tester_print("Disconnected 1");
2510         tester_test_failed();
2511         return FALSE;
2512 }
2513
2514 static gboolean iso_connect_cb_busy_2(GIOChannel *io, GIOCondition cond,
2515                                                         gpointer user_data)
2516 {
2517         struct test_data *data = tester_get_data();
2518         int err, sk_err, sk;
2519         socklen_t len;
2520
2521         data->io_id[1] = 0;
2522
2523         sk = g_io_channel_unix_get_fd(io);
2524
2525         len = sizeof(sk_err);
2526
2527         if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &sk_err, &len) < 0)
2528                 err = -errno;
2529         else
2530                 err = -sk_err;
2531
2532         tester_print("Connected 2: %d", err);
2533
2534         if (err == -EBUSY && data->io_id[0] > 0) {
2535                 /* Wait in case first connection still gets disconnected */
2536                 data->io_id[1] = g_timeout_add(250, test_connect2_busy_done,
2537                                                                         data);
2538         } else {
2539                 tester_test_failed();
2540         }
2541
2542         return FALSE;
2543 }
2544
2545 static gboolean iso_connect_cb_busy(GIOChannel *io, GIOCondition cond,
2546                                                         gpointer user_data)
2547 {
2548         struct test_data *data = tester_get_data();
2549
2550         /* First connection shall not be disconnected */
2551         data->io_id[0] = g_io_add_watch(io, G_IO_ERR | G_IO_HUP,
2552                                                 iso_connect_cb_busy_disc, data);
2553
2554         /* Second connect shall fail since CIG is now busy */
2555         setup_connect(data, 1, iso_connect_cb_busy_2);
2556
2557         return iso_connect(io, cond, user_data);
2558 }
2559
2560 static void test_connect2_busy(const void *test_data)
2561 {
2562         struct test_data *data = tester_get_data();
2563
2564         setup_connect(data, 0, iso_connect_cb_busy);
2565 }
2566
2567 static gboolean iso_connect_close_cb(GIOChannel *io, GIOCondition cond,
2568                                                         gpointer user_data)
2569 {
2570         struct test_data *data = user_data;
2571
2572         data->io_id[0] = 0;
2573
2574         tester_print("Disconnected");
2575
2576         --data->step;
2577         if (!data->step)
2578                 tester_test_passed();
2579
2580         return FALSE;
2581 }
2582
2583 static bool hook_remove_cig(const void *msg, uint16_t len, void *user_data)
2584 {
2585         struct test_data *data = user_data;
2586
2587         tester_print("Remove CIG");
2588
2589         --data->step;
2590         if (!data->step)
2591                 tester_test_passed();
2592
2593         return true;
2594 }
2595
2596 static void test_connect_close(const void *test_data)
2597 {
2598         struct test_data *data = tester_get_data();
2599         int sk;
2600         GIOChannel *io;
2601
2602         data->step = 2;
2603
2604         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_CMD,
2605                                         BT_HCI_CMD_LE_REMOVE_CIG,
2606                                         hook_remove_cig, data);
2607
2608         sk = setup_sock(data, 0);
2609         if (sk < 0)
2610                 return;
2611
2612         io = g_io_channel_unix_new(sk);
2613         g_io_channel_set_close_on_unref(io, TRUE);
2614         data->io_id[0] = g_io_add_watch(io, G_IO_HUP, iso_connect_close_cb,
2615                                                                         data);
2616
2617         shutdown(sk, SHUT_RDWR);
2618 }
2619
2620 static gboolean iso_connect_wait_close_cb(GIOChannel *io, GIOCondition cond,
2621                                                         gpointer user_data)
2622 {
2623         struct test_data *data = tester_get_data();
2624         int sk;
2625
2626         tester_print("Connected");
2627
2628         sk = g_io_channel_unix_get_fd(io);
2629
2630         data->io_id[0] = g_io_add_watch(io, G_IO_HUP, iso_connect_close_cb,
2631                                                                         data);
2632
2633         shutdown(sk, SHUT_RDWR);
2634
2635         return FALSE;
2636 }
2637
2638 static void test_connect_wait_close(const void *test_data)
2639 {
2640         struct test_data *data = tester_get_data();
2641
2642         data->step = 1;
2643
2644         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_PRE_CMD,
2645                                         BT_HCI_CMD_LE_REMOVE_CIG,
2646                                         hook_remove_cig, data);
2647
2648         setup_connect(data, 0, iso_connect_wait_close_cb);
2649 }
2650
2651 static void test_connect_suspend(const void *test_data)
2652 {
2653         test_connect(test_data);
2654         trigger_force_suspend((void *)test_data);
2655 }
2656
2657 static bool hook_acl_disc(const void *msg, uint16_t len, void *user_data)
2658 {
2659         const uint8_t *msg_data = msg;
2660         const struct bt_hci_evt_le_enhanced_conn_complete *ev;
2661         struct test_data *data = tester_get_data();
2662         struct bthost *bthost;
2663
2664         if (msg_data[0] != BT_HCI_EVT_LE_ENHANCED_CONN_COMPLETE)
2665                 return true;
2666
2667         ev = (void *) &msg_data[1];
2668
2669         tester_print("Disconnect ACL");
2670
2671         bthost = hciemu_client_get_host(data->hciemu);
2672         bthost_hci_disconnect(bthost, le16_to_cpu(ev->handle), 0x13);
2673
2674         hciemu_flush_client_events(data->hciemu);
2675
2676         return true;
2677 }
2678
2679 static void test_connect_acl_disc(const void *test_data)
2680 {
2681         struct test_data *data = tester_get_data();
2682
2683         /* ACL disconnected before ISO is created */
2684         hciemu_add_hook(data->hciemu, HCIEMU_HOOK_POST_EVT,
2685                                         BT_HCI_EVT_LE_META_EVENT,
2686                                         hook_acl_disc, NULL);
2687
2688         test_connect(test_data);
2689 }
2690
2691 static void test_bcast(const void *test_data)
2692 {
2693         struct test_data *data = tester_get_data();
2694
2695         setup_connect(data, 0, iso_connect_cb);
2696 }
2697
2698 static void test_bcast2(const void *test_data)
2699 {
2700         struct test_data *data = tester_get_data();
2701         uint8_t num[2] = {0, 1};
2702         GIOFunc funcs[2] = {iso_connect_cb, iso_connect2_cb};
2703
2704         setup_connect_many(data, 2, num, funcs);
2705 }
2706
2707 static void test_bcast_recv(const void *test_data)
2708 {
2709         struct test_data *data = tester_get_data();
2710
2711         setup_listen(data, 0, iso_accept_cb);
2712 }
2713
2714 static void test_connect2_suspend(const void *test_data)
2715 {
2716         test_connect2(test_data);
2717         trigger_force_suspend((void *)test_data);
2718 }
2719
2720 int main(int argc, char *argv[])
2721 {
2722         tester_init(&argc, &argv);
2723
2724         test_iso("Basic Framework - Success", NULL, setup_powered,
2725                                                         test_framework);
2726
2727         test_iso("Basic ISO Socket - Success", NULL, setup_powered,
2728                                                         test_socket);
2729
2730         test_iso("Basic ISO Get Socket Option - Success", NULL, setup_powered,
2731                                                         test_getsockopt);
2732
2733         test_iso("Basic ISO Set Socket Option - Success", NULL, setup_powered,
2734                                                         test_setsockopt);
2735
2736         test_iso("ISO QoS 8_1_1 - Success", &connect_8_1_1, setup_powered,
2737                                                         test_connect);
2738
2739         test_iso("ISO QoS 8_2_1 - Success", &connect_8_2_1, setup_powered,
2740                                                         test_connect);
2741
2742         test_iso("ISO QoS 16_1_1 - Success", &connect_16_1_1, setup_powered,
2743                                                         test_connect);
2744
2745         test_iso("ISO QoS 16_2_1 - Success", &connect_16_2_1, setup_powered,
2746                                                         test_connect);
2747
2748         test_iso("ISO QoS 16_2_1 CIG 0x01 - Success", &connect_1_16_2_1,
2749                                                         setup_powered,
2750                                                         test_connect);
2751
2752         test_iso("ISO QoS 16_2_1 CIG 0x01 CIS 0x01 - Success",
2753                                                         &connect_1_1_16_2_1,
2754                                                         setup_powered,
2755                                                         test_connect);
2756
2757         test_iso("ISO QoS 24_1_1 - Success", &connect_24_1_1, setup_powered,
2758                                                         test_connect);
2759
2760         test_iso("ISO QoS 24_2_1 - Success", &connect_24_2_1, setup_powered,
2761                                                         test_connect);
2762
2763         test_iso("ISO QoS 32_1_1 - Success", &connect_32_1_1, setup_powered,
2764                                                         test_connect);
2765
2766         test_iso("ISO QoS 32_2_1 - Success", &connect_32_2_1, setup_powered,
2767                                                         test_connect);
2768
2769         test_iso("ISO QoS 44_1_1 - Success", &connect_44_1_1, setup_powered,
2770                                                         test_connect);
2771
2772         test_iso("ISO QoS 44_2_1 - Success", &connect_44_2_1, setup_powered,
2773                                                         test_connect);
2774
2775         test_iso("ISO QoS 48_1_1 - Success", &connect_48_1_1, setup_powered,
2776                                                         test_connect);
2777
2778         test_iso("ISO QoS 48_2_1 - Success", &connect_48_2_1, setup_powered,
2779                                                         test_connect);
2780
2781         test_iso("ISO QoS 48_3_1 - Success", &connect_48_3_1, setup_powered,
2782                                                         test_connect);
2783
2784         test_iso("ISO QoS 48_4_1 - Success", &connect_48_4_1, setup_powered,
2785                                                         test_connect);
2786
2787         test_iso("ISO QoS 48_5_1 - Success", &connect_48_5_1, setup_powered,
2788                                                         test_connect);
2789
2790         test_iso("ISO QoS 48_6_1 - Success", &connect_48_6_1, setup_powered,
2791                                                         test_connect);
2792
2793         test_iso("ISO QoS 8_1_2 - Success", &connect_8_1_2, setup_powered,
2794                                                         test_connect);
2795
2796         test_iso("ISO QoS 8_2_2 - Success", &connect_8_2_2, setup_powered,
2797                                                         test_connect);
2798
2799         test_iso("ISO QoS 16_1_2 - Success", &connect_16_1_2, setup_powered,
2800                                                         test_connect);
2801
2802         test_iso("ISO QoS 16_2_2 - Success", &connect_16_2_2, setup_powered,
2803                                                         test_connect);
2804
2805         test_iso("ISO QoS 24_1_2 - Success", &connect_24_1_2, setup_powered,
2806                                                         test_connect);
2807
2808         test_iso("ISO QoS 24_2_2 - Success", &connect_24_2_2, setup_powered,
2809                                                         test_connect);
2810
2811         test_iso("ISO QoS 32_1_2 - Success", &connect_32_1_2, setup_powered,
2812                                                         test_connect);
2813
2814         test_iso("ISO QoS 32_2_2 - Success", &connect_32_2_2, setup_powered,
2815                                                         test_connect);
2816
2817         test_iso("ISO QoS 44_1_2 - Success", &connect_44_1_2, setup_powered,
2818                                                         test_connect);
2819
2820         test_iso("ISO QoS 44_2_2 - Success", &connect_44_2_2, setup_powered,
2821                                                         test_connect);
2822
2823         test_iso("ISO QoS 48_1_2 - Success", &connect_48_1_2, setup_powered,
2824                                                         test_connect);
2825
2826         test_iso("ISO QoS 48_2_2 - Success", &connect_48_2_2, setup_powered,
2827                                                         test_connect);
2828
2829         test_iso("ISO QoS 48_3_2 - Success", &connect_48_3_2, setup_powered,
2830                                                         test_connect);
2831
2832         test_iso("ISO QoS 48_4_2 - Success", &connect_48_4_2, setup_powered,
2833                                                         test_connect);
2834
2835         test_iso("ISO QoS 48_5_2 - Success", &connect_48_5_2, setup_powered,
2836                                                         test_connect);
2837
2838         test_iso("ISO QoS 48_6_2 - Success", &connect_48_6_2, setup_powered,
2839                                                         test_connect);
2840
2841         test_iso("ISO QoS - Invalid", &connect_invalid, setup_powered,
2842                                                         test_connect);
2843
2844         test_iso("ISO QoS CIG 0xF0 - Invalid", &connect_cig_f0_invalid,
2845                         setup_powered, test_connect);
2846
2847         test_iso("ISO QoS CIS 0xF0 - Invalid", &connect_cis_f0_invalid,
2848                         setup_powered, test_connect);
2849
2850         test_iso_rej("ISO Connect - Reject", &connect_reject, setup_powered,
2851                         test_connect, BT_HCI_ERR_CONN_FAILED_TO_ESTABLISH);
2852
2853         test_iso("ISO Send - Success", &connect_16_2_1_send, setup_powered,
2854                                                         test_connect);
2855
2856         test_iso("ISO Receive - Success", &listen_16_2_1_recv, setup_powered,
2857                                                         test_listen);
2858
2859         test_iso("ISO Receive Timestamped - Success", &listen_16_2_1_recv_ts,
2860                                                         setup_powered,
2861                                                         test_listen);
2862
2863         test_iso("ISO Receive Packet Status - Success",
2864                                                 &listen_16_2_1_recv_pkt_status,
2865                                                 setup_powered, test_listen);
2866
2867         test_iso("ISO Defer - Success", &defer_16_2_1, setup_powered,
2868                                                         test_defer);
2869
2870         test_iso("ISO Defer Connect - Success", &defer_16_2_1, setup_powered,
2871                                                         test_connect);
2872
2873         test_iso("ISO Defer Close - Success", &defer_16_2_1, setup_powered,
2874                                                         test_connect_close);
2875
2876         test_iso("ISO Connect Close - Success", &connect_16_2_1, setup_powered,
2877                                                         test_connect_close);
2878
2879         test_iso("ISO Defer Wait Close - Success", &defer_16_2_1,
2880                                         setup_powered, test_connect_wait_close);
2881
2882         test_iso("ISO Connect Wait Close - Success", &connect_16_2_1,
2883                                         setup_powered, test_connect_wait_close);
2884
2885         test_iso("ISO Connect Suspend - Success", &connect_suspend,
2886                                                         setup_powered,
2887                                                         test_connect_suspend);
2888
2889         test_iso("ISO Connected Suspend - Success", &suspend_16_2_1,
2890                                                         setup_powered,
2891                                                         test_connect);
2892
2893         test_iso2("ISO Connect2 CIG 0x01 - Success", &connect_1_16_2_1,
2894                                                         setup_powered,
2895                                                         test_connect2);
2896
2897         test_iso2("ISO Connect2 Busy CIG 0x01 - Success/Invalid",
2898                                         &connect_1_16_2_1, setup_powered,
2899                                         test_connect2_busy);
2900
2901         test_iso2("ISO Defer Connect2 CIG 0x01 - Success", &defer_1_16_2_1,
2902                                                         setup_powered,
2903                                                         test_connect2);
2904
2905         test_iso2("ISO Connect2 Suspend - Success", &connect_suspend,
2906                                                         setup_powered,
2907                                                         test_connect2_suspend);
2908
2909         test_iso2("ISO Connected2 Suspend - Success", &suspend_16_2_1,
2910                                                         setup_powered,
2911                                                         test_connect2);
2912
2913         test_iso("ISO Connect ACL Disconnect - Failure", &connect_suspend,
2914                                                         setup_powered,
2915                                                         test_connect_acl_disc);
2916
2917         test_iso("ISO Defer Send - Success", &connect_16_2_1_defer_send,
2918                                                         setup_powered,
2919                                                         test_connect);
2920
2921         test_iso("ISO 48_2_1 Defer Send - Success", &connect_48_2_1_defer_send,
2922                                                         setup_powered,
2923                                                         test_connect);
2924
2925         test_iso("ISO Defer Receive - Success", &listen_16_2_1_defer_recv,
2926                                                 setup_powered, test_listen);
2927
2928         test_iso("ISO 48_2_1 Defer Receive - Success",
2929                                                 &listen_48_2_1_defer_recv,
2930                                                 setup_powered, test_listen);
2931
2932         test_iso("ISO Defer Reject - Success", &listen_16_2_1_defer_reject,
2933                                                 setup_powered, test_listen);
2934
2935         test_iso("ISO Send and Receive - Success", &connect_16_2_1_send_recv,
2936                                                         setup_powered,
2937                                                         test_connect);
2938
2939         test_iso("ISO Disconnect - Success", &disconnect_16_2_1,
2940                                                         setup_powered,
2941                                                         test_connect);
2942
2943         test_iso("ISO Reconnect - Success", &reconnect_16_2_1,
2944                                                         setup_powered,
2945                                                         test_reconnect);
2946
2947         test_iso("ISO AC 1 & 4 - Success", &connect_ac_1_4, setup_powered,
2948                                                         test_connect);
2949
2950         test_iso("ISO AC 2 & 10 - Success", &connect_ac_2_10, setup_powered,
2951                                                         test_connect);
2952
2953         test_iso("ISO AC 3 & 5 - Success", &connect_ac_3_5, setup_powered,
2954                                                         test_connect);
2955
2956         test_iso("ISO AC 6(i) - Success", &connect_ac_6i, setup_powered,
2957                                                         test_connect);
2958
2959         test_iso2("ISO AC 6(ii) - Success", &connect_ac_6ii, setup_powered,
2960                                                         test_connect2);
2961
2962         test_iso("ISO AC 7(i) - Success", &connect_ac_7i, setup_powered,
2963                                                         test_connect);
2964
2965         test_iso2("ISO AC 7(ii) - Success", &connect_ac_7ii, setup_powered,
2966                                                         test_connect2);
2967
2968         test_iso("ISO AC 8(i) - Success", &connect_ac_8i, setup_powered,
2969                                                         test_connect);
2970
2971         test_iso2("ISO AC 8(ii) - Success", &connect_ac_8ii, setup_powered,
2972                                                         test_connect2);
2973
2974         test_iso("ISO AC 9(i) - Success", &connect_ac_9i, setup_powered,
2975                                                         test_connect);
2976
2977         test_iso2("ISO AC 9(ii) - Success", &connect_ac_9ii, setup_powered,
2978                                                         test_connect2);
2979
2980         test_iso("ISO AC 11(i) - Success", &connect_ac_11i, setup_powered,
2981                                                         test_connect);
2982
2983         test_iso2("ISO AC 11(ii) - Success", &connect_ac_11ii, setup_powered,
2984                                                         test_connect2);
2985
2986         test_iso2("ISO AC 1 + 2 - Success", &connect_ac_1_2, setup_powered,
2987                                                         test_connect2_seq);
2988
2989         test_iso2("ISO AC 1 + 2 CIG 0x01/0x02 - Success",
2990                                                         &connect_ac_1_2_cig_1_2,
2991                                                         setup_powered,
2992                                                         test_connect2_seq);
2993
2994         test_iso2("ISO Reconnect AC 6(i) - Success", &reconnect_ac_6i,
2995                                                         setup_powered,
2996                                                         test_reconnect);
2997
2998         test_iso2("ISO Reconnect AC 6(ii) - Success", &reconnect_ac_6ii,
2999                                                         setup_powered,
3000                                                         test_reconnect);
3001
3002         test_iso2("ISO AC 6(ii) CIS 0xEF/auto - Success",
3003                                                 &connect_ac_6ii_cis_ef_auto,
3004                                                 setup_powered, test_connect);
3005
3006         test_iso2("ISO AC 6(ii) CIS 0xEF/0xEF - Invalid",
3007                                                 &connect_ac_6ii_cis_ef_ef,
3008                                                 setup_powered, test_connect);
3009
3010         test_iso("ISO Broadcaster - Success", &bcast_16_2_1_send, setup_powered,
3011                                                         test_bcast);
3012         test_iso("ISO Broadcaster Encrypted - Success", &bcast_enc_16_2_1_send,
3013                                                         setup_powered,
3014                                                         test_bcast);
3015         test_iso("ISO Broadcaster BIG 0x01 - Success", &bcast_1_16_2_1_send,
3016                                                         setup_powered,
3017                                                         test_bcast);
3018         test_iso("ISO Broadcaster BIG 0x01 BIS 0x01 - Success",
3019                                                         &bcast_1_1_16_2_1_send,
3020                                                         setup_powered,
3021                                                         test_bcast);
3022
3023         test_iso("ISO Broadcaster Receiver - Success", &bcast_16_2_1_recv,
3024                                                         setup_powered,
3025                                                         test_bcast_recv);
3026         test_iso("ISO Broadcaster Receiver Encrypted - Success",
3027                                                         &bcast_enc_16_2_1_recv,
3028                                                         setup_powered,
3029                                                         test_bcast_recv);
3030         test_iso("ISO Broadcaster Receiver Defer - Success",
3031                                                 &bcast_16_2_1_recv_defer,
3032                                                 setup_powered,
3033                                                 test_bcast_recv);
3034
3035         test_iso("ISO Broadcaster AC 12 - Success", &bcast_ac_12, setup_powered,
3036                                                         test_bcast);
3037
3038         test_iso("ISO Broadcaster AC 13 - Success", &bcast_ac_13, setup_powered,
3039                                                         test_bcast2);
3040
3041         test_iso("ISO Broadcaster AC 14 - Success", &bcast_ac_14, setup_powered,
3042                                                         test_bcast);
3043
3044         return tester_run();
3045 }