1 /**
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "bstests.h"
8 #include "bs_tracing.h"
9 
10 #include <zephyr/bluetooth/conn.h>
11 #include <zephyr/bluetooth/uuid.h>
12 
13 extern enum bst_result_t bst_result;
14 
15 #define DECLARE_FLAG(flag) extern atomic_t flag
16 #define DEFINE_FLAG(flag)  atomic_t flag = (atomic_t) false
17 #define SET_FLAG(flag)     (void)atomic_set(&flag, (atomic_t) true)
18 #define UNSET_FLAG(flag)   (void)atomic_set(&flag, (atomic_t) false)
19 #define WAIT_FOR_FLAG(flag)                                                                        \
20 	while (!(bool)atomic_get(&flag)) {                                                         \
21 		(void)k_sleep(K_MSEC(1));                                                          \
22 	}
23 #define WAIT_FOR_FLAG_UNSET(flag)                                                                  \
24 	while ((bool)atomic_get(&flag)) {                                                          \
25 		(void)k_sleep(K_MSEC(1));                                                          \
26 	}
27 #define TAKE_FLAG(flag)                                                                            \
28 	while (!(bool)atomic_cas(&flag, true, false)) {                                            \
29 		(void)k_sleep(K_MSEC(1));                                                          \
30 	}
31 #define GET_FLAG(flag) (bool)atomic_get(&flag)
32 
33 #define BSIM_ASSERT(expr, ...)                                                                     \
34 	do {                                                                                       \
35 		if (!(expr)) {                                                                     \
36 			FAIL(__VA_ARGS__);                                                         \
37 		}                                                                                  \
38 	} while (0)
39 
40 #define FAIL(...)                                                                                  \
41 	do {                                                                                       \
42 		bst_result = Failed;                                                               \
43 		bs_trace_error_time_line(__VA_ARGS__);                                             \
44 	} while (0)
45 
46 #define PASS(...)                                                                                  \
47 	do {                                                                                       \
48 		bst_result = Passed;                                                               \
49 		bs_trace_info_time(1, __VA_ARGS__);                                                \
50 	} while (0)
51 
52 DECLARE_FLAG(flag_pairing_complete);
53 DECLARE_FLAG(flag_bonded);
54 DECLARE_FLAG(flag_not_bonded);
55 
56 void scan_connect_to_first_result(void);
57 struct bt_conn *get_g_conn(void);
58 void clear_g_conn(void);
59 void disconnect(void);
60 void wait_connected(void);
61 void wait_disconnected(void);
62 void create_adv(struct bt_le_ext_adv **adv);
63 void start_adv(struct bt_le_ext_adv *adv);
64 void stop_adv(struct bt_le_ext_adv *adv);
65 void set_security(bt_security_t sec);
66 void pairing_complete(struct bt_conn *conn, bool bonded);
67 void pairing_failed(struct bt_conn *conn, enum bt_security_err err);
68