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