1 /**
2  * Common functions and helpers for BSIM audio tests
3  *
4  * Copyright (c) 2019 Bose Corporation
5  * Copyright (c) 2020-2022 Nordic Semiconductor ASA
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #ifndef ZEPHYR_TEST_BSIM_BT_AUDIO_TEST_
11 #define ZEPHYR_TEST_BSIM_BT_AUDIO_TEST_
12 
13 #include <zephyr/kernel.h>
14 
15 #include "bs_types.h"
16 #include "bs_tracing.h"
17 #include "time_machine.h"
18 #include "bstests.h"
19 
20 #include <zephyr/types.h>
21 #include <stddef.h>
22 #include <errno.h>
23 #include <zephyr/sys_clock.h>
24 
25 #include <zephyr/bluetooth/bluetooth.h>
26 #include <zephyr/bluetooth/hci.h>
27 #include <zephyr/bluetooth/conn.h>
28 #include <zephyr/bluetooth/uuid.h>
29 #include <zephyr/bluetooth/gatt.h>
30 
31 #define WAIT_SECONDS 60 /* seconds */
32 #define WAIT_TIME (WAIT_SECONDS * USEC_PER_SEC) /* microseconds*/
33 
34 #define WAIT_FOR_COND(cond) while (!(cond)) { k_sleep(K_MSEC(1)); }
35 
36 #define CREATE_FLAG(flag) static atomic_t flag = (atomic_t)false
37 #define SET_FLAG(flag) (void)atomic_set(&flag, (atomic_t)true)
38 #define UNSET_FLAG(flag) (void)atomic_clear(&flag)
39 #define TEST_FLAG(flag) (atomic_get(&flag) == (atomic_t)true)
40 #define WAIT_FOR_FLAG(flag) \
41 	while (!(bool)atomic_get(&flag)) { \
42 		(void)k_sleep(K_MSEC(1)); \
43 	}
44 #define WAIT_FOR_UNSET_FLAG(flag) \
45 	while (atomic_get(&flag) != (atomic_t)false) { \
46 		(void)k_sleep(K_MSEC(1)); \
47 	}
48 
49 
50 #define FAIL(...) \
51 	do { \
52 		bst_result = Failed; \
53 		bs_trace_error_time_line(__VA_ARGS__); \
54 	} while (0)
55 
56 #define PASS(...) \
57 	do { \
58 		bst_result = Passed; \
59 		bs_trace_info_time(1, "PASSED: " __VA_ARGS__); \
60 	} while (0)
61 
62 #define AD_SIZE 1
63 
64 #define INVALID_BROADCAST_ID (BT_AUDIO_BROADCAST_ID_MAX + 1)
65 #define SYNC_RETRY_COUNT     6 /* similar to retries for connections */
66 #define PA_SYNC_SKIP         5
67 
68 extern struct bt_le_scan_cb common_scan_cb;
69 extern const struct bt_data ad[AD_SIZE];
70 extern struct bt_conn *default_conn;
71 extern atomic_t flag_connected;
72 extern atomic_t flag_conn_updated;
73 
74 void disconnected(struct bt_conn *conn, uint8_t reason);
75 void test_tick(bs_time_t HW_device_time);
76 void test_init(void);
77 
78 #endif /* ZEPHYR_TEST_BSIM_BT_AUDIO_TEST_ */
79