summaryrefslogtreecommitdiff
path: root/android/tester-main.h
blob: aa56cd2b060d96ee3cccfdd4fd7e18395d6faf92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2014  Intel Corporation. All rights reserved.
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>

#include <glib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <libgen.h>
#include <sys/signalfd.h>

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

#include "src/shared/tester.h"
#include "src/shared/mgmt.h"
#include "src/shared/queue.h"
#include "emulator/hciemu.h"

#include <hardware/hardware.h>
#include <hardware/audio.h>
#include <hardware/bluetooth.h>
#include <hardware/bt_sock.h>
#include <hardware/bt_hh.h>
#include <hardware/bt_pan.h>
#include <hardware/bt_hl.h>
#include <hardware/bt_av.h>
#include <hardware/bt_rc.h>
#include <hardware/bt_gatt.h>
#include <hardware/bt_gatt_client.h>
#include <hardware/bt_gatt_server.h>

struct pdu_set {
	struct iovec req;
	struct iovec rsp;
};

#define raw_data(args...) ((unsigned char[]) { args })

#define raw_pdu(args...)					\
	{							\
		.iov_base = raw_data(args),			\
		.iov_len = sizeof(raw_data(args)),		\
	}

#define end_pdu { .iov_base = NULL }

#define TEST_CASE_BREDR(text, ...) { \
		HCIEMU_TYPE_BREDR, \
		text, \
		sizeof((struct step[]) {__VA_ARGS__}) / sizeof(struct step), \
		(struct step[]) {__VA_ARGS__}, \
	}

#define TEST_CASE_BREDRLE(text, ...) { \
		HCIEMU_TYPE_BREDRLE, \
		text, \
		sizeof((struct step[]) {__VA_ARGS__}) / sizeof(struct step), \
		(struct step[]) {__VA_ARGS__}, \
	}

#define ACTION(status, act_fun, data_set) { \
		.action_status = status, \
		.action = act_fun, \
		.set_data = data_set, \
	}

#define ACTION_FAIL(act_fun, data_set) \
		ACTION(BT_STATUS_FAIL, act_fun, data_set)

#define ACTION_SUCCESS(act_fun, data_set) \
		ACTION(BT_STATUS_SUCCESS, act_fun, data_set)

#define CALLBACK(cb) { \
		.callback = cb, \
	}

#define CALLBACK_STATE(cb, cb_res) { \
		.callback = cb, \
		.callback_result.state = cb_res, \
	}

#define CALLBACK_STATUS(cb, cb_res) { \
		.callback = cb, \
		.callback_result.status = cb_res, \
	}

#define CALLBACK_ADAPTER_PROPS(props, prop_cnt) { \
		.callback = CB_BT_ADAPTER_PROPERTIES, \
		.callback_result.properties = props, \
		.callback_result.num_properties = prop_cnt, \
	}

#define CALLBACK_PROPS(cb, props, prop_cnt) { \
		.callback = cb, \
		.callback_result.properties = props, \
		.callback_result.num_properties = prop_cnt, \
	}

#define CALLBACK_HH_MODE(cb, cb_res, cb_mode) { \
		.callback = cb, \
		.callback_result.status = cb_res, \
		.callback_result.mode = cb_mode, \
	}

#define CALLBACK_HHREPORT(cb, cb_res, cb_rep_size) { \
		.callback = cb, \
		.callback_result.status = cb_res, \
		.callback_result.report_size = cb_rep_size, \
	}

#define CLLBACK_GATTC_SCAN_RES(props, prop_cnt, cb_adv_data) {\
		.callback = CB_GATTC_SCAN_RESULT, \
		.callback_result.properties = props, \
		.callback_result.num_properties = prop_cnt, \
		.callback_result.adv_data = cb_adv_data, \
	}

#define CALLBACK_GATTC_CONNECT(cb_res, cb_prop, cb_conn_id, cb_client_id) { \
		.callback = CB_GATTC_OPEN, \
		.callback_result.status = cb_res, \
		.callback_result.properties = cb_prop, \
		.callback_result.num_properties = 1, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.gatt_app_id = cb_client_id, \
	}

#define CALLBACK_GATTC_SEARCH_RESULT(cb_conn_id, cb_service) { \
		.callback = CB_GATTC_SEARCH_RESULT, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.service = cb_service \
	}

#define CALLBACK_GATTC_SEARCH_COMPLETE(cb_res, cb_conn_id) { \
		.callback = CB_GATTC_SEARCH_COMPLETE, \
		.callback_result.conn_id = cb_conn_id \
	}
#define CALLBACK_GATTC_GET_CHARACTERISTIC_CB(cb_res, cb_conn_id, cb_service, \
						cb_char, cb_char_prop) { \
		.callback = CB_GATTC_GET_CHARACTERISTIC, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.service = cb_service, \
		.callback_result.characteristic = cb_char, \
		.callback_result.char_prop = cb_char_prop \
	}

#define CALLBACK_GATTC_GET_DESCRIPTOR(cb_res, cb_conn_id, cb_service, \
						cb_char, cb_desc) { \
		.callback = CB_GATTC_GET_DESCRIPTOR, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.service = cb_service, \
		.callback_result.characteristic = cb_char, \
		.callback_result.descriptor = cb_desc \
	}

#define CALLBACK_GATTC_GET_INCLUDED(cb_res, cb_conn_id, cb_service, \
							cb_incl) { \
		.callback = CB_GATTC_GET_INCLUDED_SERVICE, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.service = cb_service, \
		.callback_result.included = cb_incl, \
	}

#define CALLBACK_GATTC_READ_CHARACTERISTIC(cb_res, cb_conn_id, cb_read_data) { \
		.callback = CB_GATTC_READ_CHARACTERISTIC, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.read_params = cb_read_data, \
	}

#define CALLBACK_GATTC_READ_DESCRIPTOR(cb_res, cb_conn_id, cb_read_data) { \
		.callback = CB_GATTC_READ_DESCRIPTOR, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.read_params = cb_read_data, \
	}

#define CALLBACK_GATTC_WRITE_DESCRIPTOR(cb_res, cb_conn_id, cb_write_data) { \
		.callback = CB_GATTC_WRITE_DESCRIPTOR, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.write_params = cb_write_data, \
	}

#define CALLBACK_GATTC_WRITE_CHARACTERISTIC(cb_res, cb_conn_id, \
							cb_write_data) { \
		.callback = CB_GATTC_WRITE_CHARACTERISTIC, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.status = cb_res, \
		.callback_result.write_params = cb_write_data, \
	}

#define CALLBACK_GATTC_DISCONNECT(cb_res, cb_prop, cb_conn_id, cb_client_id) { \
		.callback = CB_GATTC_CLOSE, \
		.callback_result.status = cb_res, \
		.callback_result.properties = cb_prop, \
		.callback_result.num_properties = 1, \
		.callback_result.conn_id = cb_conn_id, \
		.callback_result.gatt_app_id = cb_client_id, \
	}

#define CALLBACK_PAN_CTRL_STATE(cb, cb_res, cb_state, cb_local_role) { \
		.callback = cb, \
		.callback_result.status = cb_res, \
		.callback_result.ctrl_state = cb_state, \
		.callback_result.local_role = cb_local_role, \
	}

#define CALLBACK_PAN_CONN_STATE(cb, cb_res, cb_state, cb_local_role, \
							cb_remote_role) { \
		.callback = cb, \
		.callback_result.status = cb_res, \
		.callback_result.conn_state = cb_state, \
		.callback_result.local_role = cb_local_role, \
		.callback_result.remote_role = cb_remote_role, \
	}

#define CALLBACK_HDP_APP_REG_STATE(cb, cb_app_id, cb_state) { \
		.callback = cb, \
		.callback_result.app_id = cb_app_id, \
		.callback_result.app_state = cb_state, \
	}

#define CALLBACK_HDP_CHANNEL_STATE(cb, cb_app_id, cb_channel_id, \
					cb_mdep_cfg_index, cb_state) { \
		.callback = cb, \
		.callback_result.app_id = cb_app_id, \
		.callback_result.channel_id = cb_channel_id, \
		.callback_result.mdep_cfg_index = cb_mdep_cfg_index, \
		.callback_result.channel_state = cb_state, \
	}

#define CALLBACK_AV_CONN_STATE(cb, cb_state) { \
		.callback = cb, \
		.callback_result.state = cb_state, \
	}

#define CALLBACK_AV_AUDIO_STATE(cb, cb_state) { \
		.callback = cb, \
		.callback_result.state = cb_state, \
	}

#define CALLBACK_DEVICE_PROPS(props, prop_cnt) \
	CALLBACK_PROPS(CB_BT_REMOTE_DEVICE_PROPERTIES, props, prop_cnt)

#define CALLBACK_DEVICE_FOUND(props, prop_cnt) \
	CALLBACK_PROPS(CB_BT_DEVICE_FOUND, props, prop_cnt)

#define CALLBACK_BOND_STATE(cb_res, props, prop_cnt) { \
		.callback = CB_BT_BOND_STATE_CHANGED, \
		.callback_result.state = cb_res, \
		.callback_result.properties = props, \
		.callback_result.num_properties = prop_cnt, \
	}

#define CALLBACK_BOND_STATE_FAILED(cb_res, props, prop_cnt, reason) { \
		.callback = CB_BT_BOND_STATE_CHANGED, \
		.callback_result.state = cb_res, \
		.callback_result.status = reason, \
		.callback_result.properties = props, \
		.callback_result.num_properties = prop_cnt, \
	}

#define CALLBACK_SSP_REQ(pair_var, props, prop_cnt) { \
		.callback = CB_BT_SSP_REQUEST, \
		.callback_result.pairing_variant = pair_var, \
		.callback_result.properties = props, \
		.callback_result.num_properties = prop_cnt, \
	}

#define DBG_CB(cb) { cb, #cb }

/*
 * NOTICE:
 * Callback enum sections should be
 * updated while adding new HAL to tester.
 */
typedef enum {
	CB_BT_ADAPTER_STATE_CHANGED = 1,
	CB_BT_ADAPTER_PROPERTIES,
	CB_BT_REMOTE_DEVICE_PROPERTIES,
	CB_BT_DEVICE_FOUND,
	CB_BT_DISCOVERY_STATE_CHANGED,
	CB_BT_PIN_REQUEST,
	CB_BT_SSP_REQUEST,
	CB_BT_BOND_STATE_CHANGED,
	CB_BT_ACL_STATE_CHANGED,
	CB_BT_THREAD_EVT,
	CB_BT_DUT_MODE_RECV,
	CB_BT_LE_TEST_MODE,

	/* Hidhost cb */
	CB_HH_CONNECTION_STATE,
	CB_HH_HID_INFO,
	CB_HH_PROTOCOL_MODE,
	CB_HH_IDLE_TIME,
	CB_HH_GET_REPORT,
	CB_HH_VIRTUAL_UNPLUG,

	/* PAN cb */
	CB_PAN_CONTROL_STATE,
	CB_PAN_CONNECTION_STATE,

	/* HDP cb */
	CB_HDP_APP_REG_STATE,
	CB_HDP_CHANNEL_STATE,

	/* A2DP cb */
	CB_A2DP_CONN_STATE,
	CB_A2DP_AUDIO_STATE,

	/* Gatt client */
	CB_GATTC_REGISTER_CLIENT,
	CB_GATTC_SCAN_RESULT,
	CB_GATTC_OPEN,
	CB_GATTC_CLOSE,
	CB_GATTC_SEARCH_COMPLETE,
	CB_GATTC_SEARCH_RESULT,
	CB_GATTC_GET_CHARACTERISTIC,
	CB_GATTC_GET_DESCRIPTOR,
	CB_GATTC_GET_INCLUDED_SERVICE,
	CB_GATTC_REGISTER_FOR_NOTIFICATION,
	CB_GATTC_NOTIFY,
	CB_GATTC_READ_CHARACTERISTIC,
	CB_GATTC_WRITE_CHARACTERISTIC,
	CB_GATTC_READ_DESCRIPTOR,
	CB_GATTC_WRITE_DESCRIPTOR,
	CB_GATTC_EXECUTE_WRITE,
	CB_GATTC_READ_REMOTE_RSSI,
	CB_GATTC_LISTEN,

	/* Gatt server */
	CB_GATTS_REGISTER_SERVER,
	CB_GATTS_CONNECTION,
	CB_GATTS_SERVICE_ADDED,
	CB_GATTS_INCLUDED_SERVICE_ADDED,
	CB_GATTS_CHARACTERISTIC_ADDED,
	CB_GATTS_DESCRIPTOR_ADDED,
	CB_GATTS_SERVICE_STARTED,
	CB_GATTS_SERVICE_STOPPED,
	CB_GATTS_SERVICE_DELETED,
	CB_GATTS_REQUEST_READ,
	CB_GATTS_REQUEST_WRITE,
	CB_GATTS_REQUEST_EXEC_WRITE,
	CB_GATTS_RESPONSE_CONFIRMATION,
} expected_bt_callback_t;

struct test_data {
	struct mgmt *mgmt;
	audio_hw_device_t *audio;
	struct hw_device_t *device;
	struct hciemu *hciemu;
	enum hciemu_type hciemu_type;

	const bt_interface_t *if_bluetooth;
	const btsock_interface_t *if_sock;
	const bthh_interface_t *if_hid;
	const btpan_interface_t *if_pan;
	const bthl_interface_t *if_hdp;
	const btav_interface_t *if_a2dp;
	struct audio_stream_out *if_stream;
	const btrc_interface_t *if_avrcp;
	const btgatt_interface_t *if_gatt;

	const void *test_data;
	struct queue *steps;

	guint signalfd;
	uint16_t mgmt_index;
	pid_t bluetoothd_pid;

	struct queue *pdus;
};

/*
 * Struct holding bluetooth HAL action parameters
 */
struct bt_action_data {
	bt_bdaddr_t *addr;

	/* Remote props action arguments */
	const int prop_type;
	const bt_property_t *prop;

	/* Bonding requests parameters */
	bt_pin_code_t *pin;
	const uint8_t pin_len;
	const uint8_t ssp_variant;
	const bool accept;
	const uint16_t io_cap;

	/* Socket HAL specific params */
	const btsock_type_t sock_type;
	const int channel;
	const uint8_t *service_uuid;
	const char *service_name;
	const int flags;
	int *fd;

	/* HidHost params */
	const int report_size;

	/*Connection params*/
	const uint8_t bearer_type;
};

/* bthost's l2cap server setup parameters */
struct emu_set_l2cap_data {
	const uint16_t psm;
	const bthost_l2cap_connect_cb func;
	void *user_data;
};

struct emu_l2cap_cid_data {
	const struct pdu_set *pdu;

	uint16_t handle;
	uint16_t cid;
	bool is_sdp;
};

/*
 * Callback data structure should be enhanced with data
 * returned by callbacks. It's used for test case step
 * matching with expected step data.
 */
struct bt_callback_data {
	bt_state_t state;
	bt_status_t status;
	int num_properties;
	bt_property_t *properties;

	bt_ssp_variant_t pairing_variant;

	bthh_protocol_mode_t mode;
	int report_size;

	bool adv_data;

	int gatt_app_id;
	int conn_id;
	btgatt_srvc_id_t *service;
	btgatt_gatt_id_t *characteristic;
	btgatt_gatt_id_t *descriptor;
	btgatt_srvc_id_t *included;
	btgatt_read_params_t *read_params;
	btgatt_write_params_t *write_params;
	int char_prop;

	btpan_control_state_t ctrl_state;
	btpan_connection_state_t conn_state;
	int local_role;
	int remote_role;

	int app_id;
	int channel_id;
	int mdep_cfg_index;
	bthl_app_reg_state_t app_state;
	bthl_channel_state_t channel_state;
};

/*
 * Step structure contains expected step data and step
 * action, which should be performed before step check.
 */
struct step {
	void (*action)(void);
	int action_status;

	expected_bt_callback_t callback;
	struct bt_callback_data callback_result;

	void *set_data;
	int set_data_len;
};

struct test_case {
	const uint8_t emu_type;
	const char *title;
	const uint16_t step_num;
	const struct step *step;
};

void tester_handle_l2cap_data_exchange(struct emu_l2cap_cid_data *cid_data);

/* Get, remove test cases API */
struct queue *get_bluetooth_tests(void);
void remove_bluetooth_tests(void);
struct queue *get_socket_tests(void);
void remove_socket_tests(void);
struct queue *get_hidhost_tests(void);
void remove_hidhost_tests(void);
struct queue *get_pan_tests(void);
void remove_pan_tests(void);
struct queue *get_hdp_tests(void);
void remove_hdp_tests(void);
struct queue *get_a2dp_tests(void);
void remove_a2dp_tests(void);
struct queue *get_avrcp_tests(void);
void remove_avrcp_tests(void);
struct queue *get_gatt_tests(void);
void remove_gatt_tests(void);

/* Generic tester API */
void schedule_action_verification(struct step *step);

/* Emulator actions */
void emu_setup_powered_remote_action(void);
void emu_set_pin_code_action(void);
void emu_set_ssp_mode_action(void);
void emu_set_connect_cb_action(void);
void emu_remote_connect_hci_action(void);
void emu_remote_disconnect_hci_action(void);
void emu_set_io_cap(void);
void emu_add_l2cap_server_action(void);
void emu_add_rfcomm_server_action(void);

/* Actions */
void dummy_action(void);
void bluetooth_enable_action(void);
void bluetooth_disable_action(void);
void bt_set_property_action(void);
void bt_get_property_action(void);
void bt_start_discovery_action(void);
void bt_cancel_discovery_action(void);
void bt_get_device_props_action(void);
void bt_get_device_prop_action(void);
void bt_set_device_prop_action(void);
void bt_create_bond_action(void);
void bt_pin_reply_accept_action(void);
void bt_ssp_reply_accept_action(void);
void bt_cancel_bond_action(void);
void bt_remove_bond_action(void);
void set_default_ssp_request_handler(void);