Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / android / tester-hidhost.c
1 /*
2  * Copyright (C) 2014 Intel Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdbool.h>
19
20 #include "emulator/bthost.h"
21 #include "src/shared/tester.h"
22 #include "src/shared/queue.h"
23 #include "lib/bluetooth.h"
24 #include "android/utils.h"
25 #include "tester-main.h"
26
27 #define HID_GET_REPORT_PROTOCOL         0x60
28 #define HID_GET_BOOT_PROTOCOL           0x61
29 #define HID_SET_REPORT_PROTOCOL         0x70
30 #define HID_SET_BOOT_PROTOCOL           0x71
31
32 #define HID_SET_INPUT_REPORT            0x51
33 #define HID_SET_OUTPUT_REPORT           0x52
34 #define HID_SET_FEATURE_REPORT          0x53
35
36 #define HID_SEND_DATA                   0xa2
37
38 #define HID_GET_INPUT_REPORT            0x49
39 #define HID_GET_OUTPUT_REPORT           0x4a
40 #define HID_GET_FEATURE_REPORT          0x4b
41
42 #define HID_MODE_DEFAULT                0x00
43 #define HID_MODE_BREDR                  0x01
44 #define HID_MODE_LE                     0x02
45
46 #define HID_EXPECTED_REPORT_SIZE        0x02
47
48 #define HID_VIRTUAL_CABLE_UNPLUG        0x15
49
50 static struct queue *list; /* List of hidhost test cases */
51
52 #define did_req_pdu     0x06, \
53                         0x00, 0x00, \
54                         0x00, 0x0f, \
55                         0x35, 0x03, \
56                         0x19, 0x12, 0x00, 0xff, 0xff, 0x35, 0x05, 0x0a, 0x00, \
57                         0x00, 0xff, 0xff, 0x00
58
59 #define did_rsp_pdu     0x07, \
60                         0x00, 0x00, \
61                         0x00, 0x4f, \
62                         0x00, 0x4c, \
63                         0x35, 0x4a, 0x35, 0x48, 0x09, 0x00, 0x00, 0x0a, 0x00, \
64                         0x01, 0x00, 0x00, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, \
65                         0x12, 0x00, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, \
66                         0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, \
67                         0x12, 0x00, 0x09, 0x01, 0x03, 0x09, 0x02, 0x00, 0x09, \
68                         0x01, 0x03, 0x09, 0x02, 0x01, 0x09, 0x1d, 0x6b, 0x09, \
69                         0x02, 0x02, 0x09, 0x02, 0x46, 0x09, 0x02, 0x03, 0x09, \
70                         0x05, 0x0e, 0x09, 0x02, 0x04, 0x28, 0x01, 0x09, 0x02, \
71                         0x05, 0x09, 0x00, 0x02, \
72                         0x00
73
74 #define hid_req_pdu     0x06, \
75                         0x00, 0x01, \
76                         0x00, 0x0f, \
77                         0x35, 0x03, \
78                         0x19, 0x11, 0x24, 0xff, 0xff, 0x35, 0x05, 0x0a, 0x00, \
79                         0x00, 0xff, 0xff, 0x00
80
81 #define hid_rsp_pdu     0x07, \
82                         0x00, 0x01, \
83                         0x01, 0x71, \
84                         0x01, 0x6E, \
85                         0x36, 0x01, 0x6b, 0x36, 0x01, 0x68, 0x09, 0x00, 0x00, \
86                         0x0a, 0x00, 0x01, 0x00, 0x00, 0x09, 0x00, 0x01, 0x35, \
87                         0x03, 0x19, 0x11, 0x24, 0x09, 0x00, 0x04, 0x35, 0x0d, \
88                         0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x11, 0x35, \
89                         0x03, 0x19, 0x00, 0x11, 0x09, 0x00, 0x05, 0x35, 0x03, \
90                         0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, \
91                         0x65, 0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01, 0x00, 0x09, \
92                         0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x24, \
93                         0x09, 0x01, 0x00, 0x09, 0x00, 0x0d, 0x35, 0x0f, 0x35, \
94                         0x0d, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x13, \
95                         0x35, 0x03, 0x19, 0x00, 0x11, 0x09, 0x01, 0x00, 0x25, \
96                         0x1e, 0x4c, 0x6f, 0x67, 0x69, 0x74, 0x65, 0x63, 0x68, \
97                         0x20, 0x42, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74, \
98                         0x68, 0x20, 0x4d, 0x6f, 0x75, 0x73, 0x65, 0x20, 0x4d, \
99                         0x35, 0x35, 0x35, 0x62, 0x09, 0x01, 0x01, 0x25, 0x0f, \
100                         0x42, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74, 0x68, \
101                         0x20, 0x4d, 0x6f, 0x75, 0x73, 0x65, 0x09, 0x01, 0x02, \
102                         0x25, 0x08, 0x4c, 0x6f, 0x67, 0x69, 0x74, 0x65, 0x63, \
103                         0x68, 0x09, 0x02, 0x00, 0x09, 0x01, 0x00, 0x09, 0x02, \
104                         0x01, 0x09, 0x01, 0x11, 0x09, 0x02, 0x02, 0x08, 0x80, \
105                         0x09, 0x02, 0x03, 0x08, 0x21, 0x09, 0x02, 0x04, 0x28, \
106                         0x01, 0x09, 0x02, 0x05, 0x28, 0x01, 0x09, 0x02, 0x06, \
107                         0x35, 0x74, 0x35, 0x72, 0x08, 0x22, 0x25, 0x6e, 0x05, \
108                         0x01, 0x09, 0x02, 0xa1, 0x01, 0x85, 0x02, 0x09, 0x01, \
109                         0xa1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x08, 0x15, \
110                         0x00, 0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, \
111                         0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x16, 0x01, 0xf8, \
112                         0x26, 0xff, 0x07, 0x75, 0x0c, 0x95, 0x02, 0x81, 0x06, \
113                         0x09, 0x38, 0x15, 0x81, 0x25, 0x7f, 0x75, 0x08, 0x95, \
114                         0x01, 0x81, 0x06, 0x05, 0x0c, 0x0a, 0x38, 0x02, 0x81, \
115                         0x06, 0x05, 0x09, 0x19, 0x09, 0x29, 0x10, 0x15, 0x00, \
116                         0x25, 0x01, 0x95, 0x08, 0x75, 0x01, 0x81, 0x02, 0xc0, \
117                         0xc0, 0x06, 0x00, 0xff, 0x09, 0x01, 0xa1, 0x01, 0x85, \
118                         0x10, 0x75, 0x08, 0x95, 0x06, 0x15, 0x00, 0x26, 0xff, \
119                         0x00, 0x09, 0x01, 0x81, 0x00, 0x09, 0x01, 0x91, 0x00, \
120                         0xc0, 0x09, 0x02, 0x07, 0x35, 0x08, 0x35, 0x06, 0x09, \
121                         0x04, 0x09, 0x09, 0x01, 0x00, 0x09, 0x02, 0x08, 0x28, \
122                         0x00, 0x09, 0x02, 0x09, 0x28, 0x01, 0x09, 0x02, 0x0a, \
123                         0x28, 0x01, 0x09, 0x02, 0x0b, 0x09, 0x01, 0x00, 0x09, \
124                         0x02, 0x0c, 0x09, 0x0c, 0x80, 0x09, 0x02, 0x0d, 0x28, \
125                         0x00, 0x09, 0x02, 0x0e, 0x28, 0x01, \
126                         0x00
127
128 static const struct pdu_set sdp_pdus[] = {
129         { raw_pdu(did_req_pdu), raw_pdu(did_rsp_pdu) },
130         { raw_pdu(hid_req_pdu), raw_pdu(hid_rsp_pdu) },
131         { end_pdu, end_pdu },
132 };
133
134 static struct emu_l2cap_cid_data sdp_cid_data = {
135         .pdu = sdp_pdus,
136         .is_sdp = TRUE,
137 };
138
139 #define hid_keyboard_rsp_pdu    0x07, \
140                         0x00, 0x01, \
141                         0x02, 0x04, \
142                         0x02, 0x01, \
143                         0x36, 0x01, 0xfe, 0x36, 0x01, 0x93, 0x09, 0x00, 0x00, \
144                         0x0a, 0x00, 0x01, 0x00, 0x00, 0x09, 0x00, 0x01, 0x35, \
145                         0x03, 0x19, 0x11, 0x24, 0x09, 0x00, 0x04, 0x35, 0x0d, \
146                         0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x11, 0x35, \
147                         0x03, 0x19, 0x00, 0x11, 0x09, 0x00, 0x06, 0x35, 0x09, \
148                         0x09, 0x65, 0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01, 0x00, \
149                         0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, \
150                         0x24, 0x09, 0x01, 0x00, 0x09, 0x00, 0x0d, 0x35, 0x0f, \
151                         0x35, 0x0d, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, \
152                         0x13, 0x35, 0x03, 0x19, 0x00, 0x11, 0x09, 0x01, 0x00, \
153                         0x25, 0x10, 0x53, 0x41, 0x4d, 0x53, 0x55, 0x4e, 0x47, \
154                         0x20, 0x4b, 0x65, 0x79, 0x62, 0x6f, 0x61, 0x72, 0x64, \
155                         0x09, 0x01, 0x01, 0x25, 0x08, 0x4b, 0x65, 0x79, 0x62, \
156                         0x6f, 0x61, 0x72, 0x64, 0x09, 0x01, 0x02, 0x25, 0x0d, \
157                         0x43, 0x53, 0x52, 0x20, 0x48, 0x49, 0x44, 0x45, 0x6e, \
158                         0x67, 0x69, 0x6e, 0x65, 0x09, 0x02, 0x00, 0x09, 0x01, \
159                         0x00, 0x09, 0x02, 0x01, 0x09, 0x01, 0x11, 0x09, 0x02, \
160                         0x02, 0x08, 0x40, 0x09, 0x02, 0x03, 0x08, 0x23, 0x09, \
161                         0x02, 0x04, 0x28, 0x01, 0x09, 0x02, 0x05, 0x28, 0x01, \
162                         0x09, 0x02, 0x06, 0x35, 0xb7, 0x35, 0xb5, 0x08, 0x22, \
163                         0x25, 0xb1, 0x05, 0x01, 0x09, 0x06, 0xa1, 0x01, 0x05, \
164                         0x07, 0x85, 0x01, 0x19, 0xe0, 0x29, 0xe7, 0x15, 0x00, \
165                         0x25, 0x01, 0x75, 0x01, 0x95, 0x08, 0x81, 0x02, 0x95, \
166                         0x01, 0x75, 0x08, 0x81, 0x01, 0x95, 0x05, 0x75, 0x01, \
167                         0x05, 0x08, 0x85, 0x01, 0x19, 0x01, 0x29, 0x05, 0x91, \
168                         0x02, 0x95, 0x01, 0x75, 0x03, 0x91, 0x03, 0x95, 0x06, \
169                         0x75, 0x08, 0x15, 0x00, 0x25, 0x65, 0x05, 0x07, 0x19, \
170                         0x00, 0x29, 0x6f, 0x81, 0x00, 0xc0, 0x05, 0x0c, 0x09, \
171                         0x01, 0xa1, 0x01, 0x85, 0x02, 0x05, 0x0c, 0x15, 0x00, \
172                         0x25, 0x01, 0x75, 0x01, 0x95, 0x18, 0x09, 0xe2, 0x09, \
173                         0xea, 0x09, 0xe9, 0x09, 0xb7, 0x09, 0xcd, 0x0a, 0x23, \
174                         0x02, 0x0a, 0x8a, 0x01, 0x0a, 0x21, 0x02, 0x75, 0x01, \
175                         0x95, 0x03, 0x81, 0x02, 0x75, 0x01, 0x95, 0x05, 0x81, \
176                         0x01, 0x05, 0x08, 0x85, 0xff, 0x95, 0x01, 0x75, 0x02, \
177                         0x09, 0x24, 0x09, 0x26, 0x81, 0x02, 0x75, 0x06, 0x81, \
178                         0x01, 0xc0, 0x06, 0x7f, 0xff, 0x09, 0x01, 0xa1, 0x01, \
179                         0x85, 0x03, 0x15, 0x00, 0x25, 0x01, 0x09, 0xb9, 0x09, \
180                         0xb5, 0x09, 0xba, 0x09, 0xbb, 0x09, 0xbc, 0x09, 0xbd, \
181                         0x09, 0xb6, 0x09, 0xb7, 0x75, 0x01, 0x95, 0x06, 0x81, \
182                         0x02, 0x75, 0x01, 0x95, 0x02, 0x81, 0x01, 0xc0, 0x09, \
183                         0x02, 0x07, 0x35, 0x08, 0x35, 0x06, 0x09, 0x04, 0x09, \
184                         0x09, 0x01, 0x00, 0x09, 0x02, 0x08, 0x28, 0x00, 0x09, \
185                         0x02, 0x09, 0x28, 0x01, 0x09, 0x02, 0x0a, 0x28, 0x01, \
186                         0x09, 0x02, 0x0b, 0x09, 0x01, 0x00, 0x09, 0x02, 0x0c, \
187                         0x09, 0x1f, 0x40, 0x09, 0x02, 0x0d, 0x28, 0x00, 0x09, \
188                         0x02, 0x0e, 0x28, 0x01, 0x36, 0x00, 0x65, 0x09, 0x00, \
189                         0x00, 0x0a, 0x00, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, \
190                         0x35, 0x03, 0x19, 0x12, 0x00, 0x09, 0x00, 0x04, 0x35, \
191                         0x0d, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x01, \
192                         0x35, 0x03, 0x19, 0x00, 0x01, 0x09, 0x00, 0x06, 0x35, \
193                         0x09, 0x09, 0x65, 0x6e, 0x09, 0x00, 0x6a, 0x09, 0x01, \
194                         0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, \
195                         0x12, 0x00, 0x09, 0x01, 0x00, 0x09, 0x01, 0x01, 0x25, \
196                         0x00, 0x09, 0x02, 0x00, 0x09, 0x01, 0x00, 0x09, 0x02, \
197                         0x01, 0x09, 0x23, 0x3d, 0x09, 0x02, 0x02, 0x09, 0x01, \
198                         0x3d, 0x09, 0x02, 0x03, 0x09, 0x00, 0x00, 0x09, 0x02, \
199                         0x04, 0x28, 0x01, 0x09, 0x02, 0x05, 0x09, 0x00, 0x02, \
200                         0x00
201
202 static const struct pdu_set sdp_kb_pdus[] = {
203         { raw_pdu(did_req_pdu), raw_pdu(did_rsp_pdu) },
204         { raw_pdu(hid_req_pdu), raw_pdu(hid_keyboard_rsp_pdu) },
205         { end_pdu, end_pdu },
206 };
207
208 static struct emu_l2cap_cid_data sdp_kb_cid_data = {
209         .pdu = sdp_kb_pdus,
210         .is_sdp = TRUE,
211 };
212
213 static struct emu_l2cap_cid_data ctrl_cid_data;
214 static struct emu_l2cap_cid_data intr_cid_data;
215
216 static void hid_prepare_reply_protocol_mode(struct emu_l2cap_cid_data *cid_data)
217 {
218         struct test_data *t_data = tester_get_data();
219         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
220         const struct iovec pdu = raw_pdu(0xa0, 0x00);
221
222         bthost_send_cid_v(bthost, cid_data->handle, cid_data->cid, &pdu, 1);
223 }
224
225 static void hid_prepare_reply_report(struct emu_l2cap_cid_data *cid_data)
226 {
227         struct test_data *t_data = tester_get_data();
228         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
229         const struct iovec pdu = raw_pdu(0xa2, 0x01, 0x00);
230
231         bthost_send_cid_v(bthost, cid_data->handle, cid_data->cid, &pdu, 1);
232 }
233
234 static void hid_ctrl_cid_hook_cb(const void *data, uint16_t len,
235                                                         void *user_data)
236 {
237         struct emu_l2cap_cid_data *cid_data = user_data;
238         uint8_t header = ((uint8_t *) data)[0];
239         struct step *step;
240
241         switch (header) {
242         case HID_GET_REPORT_PROTOCOL:
243         case HID_GET_BOOT_PROTOCOL:
244         case HID_SET_REPORT_PROTOCOL:
245         case HID_SET_BOOT_PROTOCOL:
246                 hid_prepare_reply_protocol_mode(cid_data);
247                 break;
248         case HID_GET_INPUT_REPORT:
249         case HID_GET_OUTPUT_REPORT:
250         case HID_GET_FEATURE_REPORT:
251                 hid_prepare_reply_report(cid_data);
252                 break;
253         /*
254          * HID device doesnot reply for this commads, so reaching pdu's
255          * to hid device means assuming test passed
256          */
257         case HID_SET_INPUT_REPORT:
258         case HID_SET_OUTPUT_REPORT:
259         case HID_SET_FEATURE_REPORT:
260         case HID_SEND_DATA:
261                 /* Successfully verify sending data step */
262                 step = g_new0(struct step, 1);
263
264                 step->callback = CB_EMU_CONFIRM_SEND_DATA;
265
266                 schedule_callback_verification(step);
267                 break;
268         case HID_VIRTUAL_CABLE_UNPLUG:
269                 step = g_new0(struct step, 1);
270
271                 step->callback = CB_EMU_CONNECTION_REJECTED;
272
273                 schedule_callback_verification(step);
274                 break;
275         }
276 }
277 static void hid_ctrl_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
278 {
279         struct test_data *data = tester_get_data();
280         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
281         struct emu_l2cap_cid_data *cid_data = user_data;
282
283         cid_data->handle = handle;
284         cid_data->cid = cid;
285
286         bthost_add_cid_hook(bthost, handle, cid, hid_ctrl_cid_hook_cb,
287                                                                 cid_data);
288 }
289
290 static void hid_intr_cid_hook_cb(const void *data, uint16_t len,
291                                                         void *user_data)
292 {
293         uint8_t header = ((uint8_t *) data)[0];
294         struct step *step;
295
296         switch (header) {
297         case HID_SEND_DATA:
298                 /* Successfully verify sending data step */
299                 step = g_new0(struct step, 1);
300
301                 step->callback = CB_EMU_CONFIRM_SEND_DATA;
302
303                 schedule_callback_verification(step);
304                 break;
305         }
306 }
307 static void hid_intr_connect_cb(uint16_t handle, uint16_t cid, void *user_data)
308 {
309         struct test_data *data = tester_get_data();
310         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
311         struct emu_l2cap_cid_data *cid_data = user_data;
312
313         cid_data->handle = handle;
314         cid_data->cid = cid;
315
316         bthost_add_cid_hook(bthost, handle, cid, hid_intr_cid_hook_cb,
317                                                                 cid_data);
318 }
319
320 static bt_scan_mode_t setprop_scan_mode_conn_val = BT_SCAN_MODE_CONNECTABLE;
321
322 static bt_property_t prop_test_scan_mode_conn = {
323         .type = BT_PROPERTY_ADAPTER_SCAN_MODE,
324         .val = &setprop_scan_mode_conn_val,
325         .len = sizeof(setprop_scan_mode_conn_val),
326 };
327
328 /* Emulate SDP (PSM = 1) */
329 static struct emu_set_l2cap_data l2cap_setup_sdp_data = {
330         .psm = 1,
331         .func = tester_generic_connect_cb,
332         .user_data = &sdp_cid_data,
333 };
334
335 static struct emu_set_l2cap_data l2cap_setup_kb_sdp_data = {
336         .psm = 1,
337         .func = tester_generic_connect_cb,
338         .user_data = &sdp_kb_cid_data,
339 };
340
341 /* Emulate Control Channel (PSM = 17) */
342 static struct emu_set_l2cap_data l2cap_setup_cc_data = {
343         .psm = 17,
344         .func = hid_ctrl_connect_cb,
345         .user_data = &ctrl_cid_data,
346 };
347
348 /* Emulate Interrupt Channel (PSM = 19) */
349 static struct emu_set_l2cap_data l2cap_setup_ic_data = {
350         .psm = 19,
351         .func = hid_intr_connect_cb,
352         .user_data = &intr_cid_data,
353 };
354
355 static void hidhost_connect_action(void)
356 {
357         struct test_data *data = tester_get_data();
358         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
359         struct step *step = g_new0(struct step, 1);
360         bt_bdaddr_t bdaddr;
361
362         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
363
364         step->action_status = data->if_hid->connect(&bdaddr);
365
366         schedule_action_verification(step);
367 }
368
369 static void hidhost_disconnect_action(void)
370 {
371         struct test_data *data = tester_get_data();
372         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
373         struct step *step = g_new0(struct step, 1);
374         bt_bdaddr_t bdaddr;
375
376         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
377
378         step->action_status = data->if_hid->disconnect(&bdaddr);
379
380         schedule_action_verification(step);
381 }
382
383 static void hidhost_virtual_unplug_action(void)
384 {
385         struct test_data *data = tester_get_data();
386         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
387         struct step *step = g_new0(struct step, 1);
388         bt_bdaddr_t bdaddr;
389
390         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
391
392         step->action_status = data->if_hid->virtual_unplug(&bdaddr);
393
394         schedule_action_verification(step);
395 }
396
397 static void hidhost_get_protocol_action(void)
398 {
399         struct test_data *data = tester_get_data();
400         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
401         struct step *step = g_new0(struct step, 1);
402         bt_bdaddr_t bdaddr;
403
404         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
405
406         step->action_status = data->if_hid->get_protocol(&bdaddr,
407                                                         BTHH_REPORT_MODE);
408
409         schedule_action_verification(step);
410 }
411
412 static void hidhost_set_protocol_action(void)
413 {
414         struct test_data *data = tester_get_data();
415         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
416         struct step *step = g_new0(struct step, 1);
417         bt_bdaddr_t bdaddr;
418
419         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
420
421         step->action_status = data->if_hid->set_protocol(&bdaddr,
422                                                         BTHH_REPORT_MODE);
423
424         schedule_action_verification(step);
425 }
426
427 static void hidhost_get_report_action(void)
428 {
429         struct test_data *data = tester_get_data();
430         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
431         struct step *step = g_new0(struct step, 1);
432         bt_bdaddr_t bdaddr;
433
434         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
435
436         step->action_status = data->if_hid->get_report(&bdaddr,
437                                                         BTHH_INPUT_REPORT, 1,
438                                                         20);
439
440         schedule_action_verification(step);
441 }
442
443 static void hidhost_set_report_action(void)
444 {
445         struct test_data *data = tester_get_data();
446         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
447         struct step *step = g_new0(struct step, 1);
448         char *buf = "fe0201";
449         bt_bdaddr_t bdaddr;
450
451         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
452
453         step->action_status = data->if_hid->send_data(&bdaddr, buf);
454         schedule_action_verification(step);
455 }
456
457 static void hidhost_send_data_action(void)
458 {
459         struct test_data *data = tester_get_data();
460         const uint8_t *hid_addr = hciemu_get_client_bdaddr(data->hciemu);
461         struct step *step = g_new0(struct step, 1);
462         char *buf = "010101";
463         bt_bdaddr_t bdaddr;
464
465         bdaddr2android((const bdaddr_t *) hid_addr, &bdaddr);
466
467         step->action_status = data->if_hid->set_report(&bdaddr,
468                                                         BTHH_INPUT_REPORT, buf);
469         schedule_action_verification(step);
470 }
471
472 static void client_l2cap_rsp(uint8_t code, const void *data, uint16_t len,
473                                                         void *user_data)
474 {
475         struct test_data *t_data = tester_get_data();
476         struct bthost *bthost = hciemu_client_get_host(t_data->hciemu);
477         struct emu_l2cap_cid_data *cid_data = user_data;
478         const uint16_t *psm = data;
479         const struct iovec con_req = raw_pdu(0x13, 0x00,        /* PSM */
480                                                 0x41, 0x00);    /* Source CID */
481
482         if (len < sizeof(*psm)) {
483                 tester_warn("Invalid l2cap response.");
484                 return;
485         }
486
487         switch (*psm) {
488         case 0x40:
489                 bthost_add_cid_hook(bthost, cid_data->handle, 0x40,
490                                                 hid_ctrl_cid_hook_cb, cid_data);
491
492                 bthost_l2cap_req(bthost, cid_data->handle, 0x02,
493                                         con_req.iov_base, con_req.iov_len,
494                                         client_l2cap_rsp, cid_data);
495                 break;
496         case 0x41:
497                 bthost_add_cid_hook(bthost, cid_data->handle, 0x41,
498                                                 hid_intr_cid_hook_cb, cid_data);
499                 break;
500         default:
501                 break;
502         }
503 }
504
505 static void hidhost_conn_cb(uint16_t handle, void *user_data)
506 {
507         const struct iovec con_req = raw_pdu(0x11, 0x00,        /* PSM */
508                                                 0x40, 0x00);    /* Source CID */
509
510         struct test_data *data = tester_get_data();
511         struct bthost *bthost = hciemu_client_get_host(data->hciemu);
512
513         if (data->hciemu_type == HCIEMU_TYPE_BREDR) {
514                 tester_warn("Not handled device type.");
515                 return;
516         }
517
518         ctrl_cid_data.cid = 0x40;
519         ctrl_cid_data.handle = handle;
520
521         tester_print("Sending L2CAP Request from remote");
522
523         bthost_l2cap_req(bthost, handle, 0x02, con_req.iov_base,
524                                         con_req.iov_len, client_l2cap_rsp,
525                                         &ctrl_cid_data);
526 }
527
528 static struct test_case test_cases[] = {
529         TEST_CASE_BREDRLE("HidHost Init",
530                 ACTION_SUCCESS(dummy_action, NULL),
531         ),
532         TEST_CASE_BREDRLE("HidHost Connect Success",
533                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
534                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
535                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
536                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
537                 ACTION_SUCCESS(emu_add_l2cap_server_action,
538                                                         &l2cap_setup_sdp_data),
539                 ACTION_SUCCESS(emu_add_l2cap_server_action,
540                                                         &l2cap_setup_cc_data),
541                 ACTION_SUCCESS(emu_add_l2cap_server_action,
542                                                         &l2cap_setup_ic_data),
543                 ACTION_SUCCESS(hidhost_connect_action, NULL),
544                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
545                                                 BTHH_CONN_STATE_CONNECTED),
546                 ACTION_SUCCESS(bluetooth_disable_action, NULL),
547                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
548                                                 BTHH_CONN_STATE_DISCONNECTED),
549                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
550         ),
551         TEST_CASE_BREDRLE("HidHost Disconnect Success",
552                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
553                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
554                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
555                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
556                 ACTION_SUCCESS(emu_add_l2cap_server_action,
557                                                         &l2cap_setup_sdp_data),
558                 ACTION_SUCCESS(emu_add_l2cap_server_action,
559                                                         &l2cap_setup_cc_data),
560                 ACTION_SUCCESS(emu_add_l2cap_server_action,
561                                                         &l2cap_setup_ic_data),
562                 ACTION_SUCCESS(hidhost_connect_action, NULL),
563                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
564                                                 BTHH_CONN_STATE_CONNECTED),
565                 ACTION_SUCCESS(hidhost_disconnect_action, NULL),
566                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
567                                                 BTHH_CONN_STATE_DISCONNECTED),
568         ),
569         TEST_CASE_BREDRLE("HidHost VirtualUnplug Success",
570                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
571                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
572                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
573                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
574                 ACTION_SUCCESS(emu_add_l2cap_server_action,
575                                                         &l2cap_setup_sdp_data),
576                 ACTION_SUCCESS(emu_add_l2cap_server_action,
577                                                         &l2cap_setup_cc_data),
578                 ACTION_SUCCESS(emu_add_l2cap_server_action,
579                                                         &l2cap_setup_ic_data),
580                 ACTION_SUCCESS(hidhost_connect_action, NULL),
581                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
582                                                 BTHH_CONN_STATE_CONNECTED),
583                 ACTION_SUCCESS(hidhost_virtual_unplug_action, NULL),
584                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
585                                                 BTHH_CONN_STATE_DISCONNECTED),
586         ),
587         TEST_CASE_BREDRLE("HidHost GetProtocol Success",
588                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
589                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
590                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
591                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
592                 ACTION_SUCCESS(emu_add_l2cap_server_action,
593                                                         &l2cap_setup_sdp_data),
594                 ACTION_SUCCESS(emu_add_l2cap_server_action,
595                                                         &l2cap_setup_cc_data),
596                 ACTION_SUCCESS(emu_add_l2cap_server_action,
597                                                         &l2cap_setup_ic_data),
598                 ACTION_SUCCESS(hidhost_connect_action, NULL),
599                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
600                                                 BTHH_CONN_STATE_CONNECTED),
601                 ACTION_SUCCESS(hidhost_get_protocol_action, NULL),
602                 CALLBACK_HH_MODE(CB_HH_PROTOCOL_MODE, BTHH_OK, HID_MODE_BREDR),
603         ),
604         TEST_CASE_BREDRLE("HidHost SetProtocol Success",
605                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
606                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
607                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
608                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
609                 ACTION_SUCCESS(emu_add_l2cap_server_action,
610                                                         &l2cap_setup_sdp_data),
611                 ACTION_SUCCESS(emu_add_l2cap_server_action,
612                                                         &l2cap_setup_cc_data),
613                 ACTION_SUCCESS(emu_add_l2cap_server_action,
614                                                         &l2cap_setup_ic_data),
615                 ACTION_SUCCESS(hidhost_connect_action, NULL),
616                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
617                                                 BTHH_CONN_STATE_CONNECTED),
618                 ACTION_SUCCESS(hidhost_set_protocol_action, NULL),
619                 CALLBACK_HH_MODE(CB_HH_PROTOCOL_MODE, BTHH_OK, HID_MODE_BREDR),
620         ),
621         TEST_CASE_BREDRLE("HidHost GetReport Success",
622                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
623                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
624                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
625                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
626                 ACTION_SUCCESS(emu_add_l2cap_server_action,
627                                                         &l2cap_setup_sdp_data),
628                 ACTION_SUCCESS(emu_add_l2cap_server_action,
629                                                         &l2cap_setup_cc_data),
630                 ACTION_SUCCESS(emu_add_l2cap_server_action,
631                                                         &l2cap_setup_ic_data),
632                 ACTION_SUCCESS(hidhost_connect_action, NULL),
633                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
634                                                 BTHH_CONN_STATE_CONNECTED),
635                 ACTION_SUCCESS(hidhost_get_report_action, NULL),
636                 CALLBACK_HHREPORT(CB_HH_GET_REPORT, BTHH_OK,
637                                                 HID_EXPECTED_REPORT_SIZE),
638         ),
639         TEST_CASE_BREDRLE("HidHost SetReport Success",
640                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
641                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
642                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
643                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
644                 ACTION_SUCCESS(emu_add_l2cap_server_action,
645                                                         &l2cap_setup_sdp_data),
646                 ACTION_SUCCESS(emu_add_l2cap_server_action,
647                                                         &l2cap_setup_cc_data),
648                 ACTION_SUCCESS(emu_add_l2cap_server_action,
649                                                         &l2cap_setup_ic_data),
650                 ACTION_SUCCESS(hidhost_connect_action, NULL),
651                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
652                                                 BTHH_CONN_STATE_CONNECTING),
653                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
654                                                 BTHH_CONN_STATE_CONNECTED),
655                 ACTION_SUCCESS(hidhost_set_report_action, NULL),
656                 CALLBACK(CB_EMU_CONFIRM_SEND_DATA),
657         ),
658         TEST_CASE_BREDRLE("HidHost SendData Success",
659                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
660                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
661                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
662                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
663                 ACTION_SUCCESS(emu_add_l2cap_server_action,
664                                                         &l2cap_setup_sdp_data),
665                 ACTION_SUCCESS(emu_add_l2cap_server_action,
666                                                         &l2cap_setup_cc_data),
667                 ACTION_SUCCESS(emu_add_l2cap_server_action,
668                                                         &l2cap_setup_ic_data),
669                 ACTION_SUCCESS(hidhost_connect_action, NULL),
670                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
671                                                 BTHH_CONN_STATE_CONNECTED),
672                 ACTION_SUCCESS(hidhost_send_data_action, NULL),
673                 CALLBACK(CB_EMU_CONFIRM_SEND_DATA),
674         ),
675         TEST_CASE_BREDRLE("HidHost Connect Encrypted Success",
676                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
677                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
678                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
679                 ACTION_SUCCESS(emu_set_ssp_mode_action, NULL),
680                 ACTION_SUCCESS(set_default_ssp_request_handler, NULL),
681                 ACTION_SUCCESS(emu_add_l2cap_server_action,
682                                                 &l2cap_setup_kb_sdp_data),
683                 ACTION_SUCCESS(emu_add_l2cap_server_action,
684                                                         &l2cap_setup_cc_data),
685                 ACTION_SUCCESS(emu_add_l2cap_server_action,
686                                                         &l2cap_setup_ic_data),
687                 ACTION_SUCCESS(hidhost_connect_action, NULL),
688                 CALLBACK(CB_EMU_ENCRYPTION_ENABLED),
689                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
690                                                 BTHH_CONN_STATE_CONNECTED),
691                 ACTION_SUCCESS(hidhost_send_data_action, NULL),
692                 ACTION_SUCCESS(bluetooth_disable_action, NULL),
693                 CALLBACK_STATE(CB_HH_CONNECTION_STATE,
694                                                 BTHH_CONN_STATE_DISCONNECTED),
695                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_OFF),
696         ),
697         TEST_CASE_BREDRLE("HidHost Reject Unknown Remote Connection",
698                 ACTION_SUCCESS(bluetooth_enable_action, NULL),
699                 CALLBACK_STATE(CB_BT_ADAPTER_STATE_CHANGED, BT_STATE_ON),
700                 ACTION_SUCCESS(bt_set_property_action,
701                                                 &prop_test_scan_mode_conn),
702                 CALLBACK_ADAPTER_PROPS(&prop_test_scan_mode_conn, 1),
703                 ACTION_SUCCESS(emu_setup_powered_remote_action, NULL),
704                 ACTION_SUCCESS(emu_add_l2cap_server_action,
705                                                 &l2cap_setup_kb_sdp_data),
706                 ACTION_SUCCESS(emu_add_l2cap_server_action,
707                                                         &l2cap_setup_cc_data),
708                 ACTION_SUCCESS(emu_add_l2cap_server_action,
709                                                         &l2cap_setup_ic_data),
710                 /* Trigger incoming connection */
711                 ACTION_SUCCESS(emu_set_connect_cb_action, hidhost_conn_cb),
712                 ACTION_SUCCESS(emu_remote_connect_hci_action, NULL),
713                 CALLBACK(CB_EMU_CONNECTION_REJECTED),
714         ),
715 };
716
717 struct queue *get_hidhost_tests(void)
718 {
719         uint16_t i = 0;
720
721         list = queue_new();
722
723         for (; i < sizeof(test_cases) / sizeof(test_cases[0]); ++i)
724                 queue_push_tail(list, &test_cases[i]);
725
726         return list;
727 }
728
729 void remove_hidhost_tests(void)
730 {
731         queue_destroy(list, NULL);
732 }