1 /** @file
2  *  @brief Common functionality for Bluetooth mesh BabbleSim tests.
3  */
4 
5 /*
6  * Copyright (c) 2021 Nordic Semiconductor ASA
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 #ifndef ZEPHYR_TESTS_BLUETOOTH_BSIM_BT_BSIM_TEST_MESH_MESH_TEST_H_
11 #define ZEPHYR_TESTS_BLUETOOTH_BSIM_BT_BSIM_TEST_MESH_MESH_TEST_H_
12 #include <zephyr/kernel.h>
13 
14 #include "bs_types.h"
15 #include "bs_tracing.h"
16 #include "time_machine.h"
17 #include "bstests.h"
18 
19 #include <zephyr/types.h>
20 #include <stddef.h>
21 #include <errno.h>
22 #include <zephyr/sys/printk.h>
23 
24 #include <zephyr/bluetooth/bluetooth.h>
25 #include <zephyr/bluetooth/mesh.h>
26 
27 #define TEST_MOD_ID 0x8888
28 #define TEST_MSG_OP_1  BT_MESH_MODEL_OP_1(0x0f)
29 #define TEST_MSG_OP_2  BT_MESH_MODEL_OP_1(0x10)
30 
31 #define TEST_VND_COMPANY_ID 0x1234
32 #define TEST_VND_MOD_ID   0x5678
33 
34 #define MODEL_LIST(...) ((struct bt_mesh_model[]){ __VA_ARGS__ })
35 
36 #define FAIL(msg, ...)                                                         \
37 	do {                                                                   \
38 		bst_result = Failed;                                           \
39 		bs_trace_error_time_line(msg "\n", ##__VA_ARGS__);             \
40 	} while (0)
41 
42 #define PASS()                                                                 \
43 	do {                                                                   \
44 		bst_result = Passed;                                           \
45 		bs_trace_info_time(1, "%s PASSED\n", __func__);                \
46 	} while (0)
47 
48 #define ASSERT_OK(cond)                                                        \
49 	do {                                                                   \
50 		int _err = (cond);                                             \
51 		if (_err) {                                                    \
52 			bst_result = Failed;                                   \
53 			bs_trace_error_time_line(                              \
54 				#cond " failed with error %d\n", _err);        \
55 		}                                                              \
56 	} while (0)
57 
58 #define ASSERT_OK_MSG(cond, fmt, ...)                                          \
59 	do {                                                                   \
60 		int _err = (cond);                                             \
61 		if (_err) {                                                    \
62 			bst_result = Failed;                                   \
63 			bs_trace_error_time_line(                              \
64 				#cond " failed with error %d\n" fmt, _err,     \
65 				##__VA_ARGS__);                                \
66 		}                                                              \
67 	} while (0)
68 
69 #define ASSERT_TRUE(cond)                                                      \
70 	do {                                                                   \
71 		if (!(cond)) {                                                 \
72 			bst_result = Failed;                                   \
73 			bs_trace_error_time_line(                              \
74 				#cond " is false.\n");                         \
75 		}                                                              \
76 	} while (0)
77 
78 #define ASSERT_TRUE_MSG(cond, fmt, ...)                                        \
79 	do {                                                                   \
80 		if (!(cond)) {                                                 \
81 			bst_result = Failed;                                   \
82 			bs_trace_error_time_line(                              \
83 				#cond " is false. " fmt, ##__VA_ARGS__);       \
84 		}                                                              \
85 	} while (0)
86 
87 
88 #define ASSERT_FALSE(cond)                                                     \
89 	do {                                                                   \
90 		if (cond) {                                                    \
91 			bst_result = Failed;                                   \
92 			bs_trace_error_time_line(                              \
93 				#cond " is true.\n");                          \
94 		}                                                              \
95 	} while (0)
96 
97 #define ASSERT_FALSE_MSG(cond, fmt, ...)                                       \
98 	do {                                                                   \
99 		if (cond) {                                                    \
100 			bst_result = Failed;                                   \
101 			bs_trace_error_time_line(                              \
102 				#cond " is true. " fmt, ##__VA_ARGS__);        \
103 		}                                                              \
104 	} while (0)
105 
106 #define ASSERT_EQUAL(expected, got)                                            \
107 	do {                                                                   \
108 		if ((expected) != (got)) {                                     \
109 			bst_result = Failed;                                   \
110 			bs_trace_error_time_line(                              \
111 				#expected " not equal to " #got ": %d != %d\n",\
112 				(expected), (got));                            \
113 		}                                                              \
114 	} while (0)
115 
116 #define ASSERT_IN_RANGE(got, min, max)                                                             \
117 	do {                                                                                       \
118 		if (!IN_RANGE(got, min, max)) {                                            \
119 			bst_result = Failed;                                                       \
120 			bs_trace_error_time_line(#got " not in range %d <-> %d, " #got " = %d\n",  \
121 						 (min), (max), (got));                             \
122 		}                                                                                  \
123 	} while (0)
124 
125 struct bt_mesh_test_cfg {
126 	uint16_t addr;
127 	uint8_t dev_key[16];
128 };
129 
130 enum bt_mesh_test_send_flags {
131 	FORCE_SEGMENTATION = BIT(0),
132 	LONG_MIC = BIT(1),
133 };
134 
135 struct bt_mesh_test_stats {
136 	uint32_t received;
137 	uint32_t sent;
138 	uint32_t recv_overflow;
139 };
140 
141 struct bt_mesh_test_msg {
142 	sys_snode_t _node;
143 	size_t len;
144 	uint8_t seq;
145 	struct bt_mesh_msg_ctx ctx;
146 };
147 
148 struct bt_mesh_test_sync_ctx {
149 	uint32_t *dev_nmbr;
150 	uint32_t *chan_nmbr;
151 	uint32_t *chan_id;
152 	uint16_t cnt;
153 };
154 
155 extern enum bst_result_t bst_result;
156 extern const struct bt_mesh_test_cfg *cfg;
157 extern struct bt_mesh_model *test_model;
158 extern struct bt_mesh_model *test_vnd_model;
159 extern const uint8_t test_net_key[16];
160 extern const uint8_t test_app_key[16];
161 extern const uint8_t test_va_uuid[16];
162 extern struct bt_mesh_test_stats test_stats;
163 extern struct bt_mesh_msg_ctx test_send_ctx;
164 
165 void bt_mesh_test_cfg_set(const struct bt_mesh_test_cfg *cfg, int wait_time);
166 void bt_mesh_test_setup(void);
167 void bt_mesh_test_timeout(bs_time_t HW_device_time);
168 
169 void bt_mesh_device_setup(const struct bt_mesh_prov *prov, const struct bt_mesh_comp *comp);
170 
171 int bt_mesh_test_recv(uint16_t len, uint16_t dst, const uint8_t *uuid, k_timeout_t timeout);
172 int bt_mesh_test_recv_msg(struct bt_mesh_test_msg *msg, k_timeout_t timeout);
173 int bt_mesh_test_recv_clear(void);
174 
175 int bt_mesh_test_send(uint16_t addr, const uint8_t *uuid, size_t len,
176 		      enum bt_mesh_test_send_flags flags, k_timeout_t timeout);
177 int bt_mesh_test_send_async(uint16_t addr, const uint8_t *uuid, size_t len,
178 			    enum bt_mesh_test_send_flags flags,
179 			    const struct bt_mesh_send_cb *send_cb,
180 			    void *cb_data);
181 int bt_mesh_test_send_ra(uint16_t addr, uint8_t *data, size_t len,
182 			 const struct bt_mesh_send_cb *send_cb,
183 			 void *cb_data);
184 void bt_mesh_test_ra_cb_setup(void (*cb)(uint8_t *, size_t));
185 
186 uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr);
187 
188 #if defined(CONFIG_BT_MESH_SAR_CFG)
189 void bt_mesh_test_sar_conf_set(struct bt_mesh_sar_tx *tx_set, struct bt_mesh_sar_rx *rx_set);
190 #endif
191 
192 #endif /* ZEPHYR_TESTS_BLUETOOTH_BSIM_BT_BSIM_TEST_MESH_MESH_TEST_H_ */
193