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 #include <mesh/adv.h>
27 
28 #define TEST_MOD_ID 0x8888
29 #define TEST_MSG_OP_1  BT_MESH_MODEL_OP_1(0x0f)
30 #define TEST_MSG_OP_2  BT_MESH_MODEL_OP_1(0x10)
31 
32 #define TEST_VND_COMPANY_ID 0x1234
33 #define TEST_VND_MOD_ID   0x5678
34 
35 #define MODEL_LIST(...) ((const struct bt_mesh_model[]){ __VA_ARGS__ })
36 
37 #define FAIL(msg, ...)                                                         \
38 	do {                                                                   \
39 		bst_result = Failed;                                           \
40 		bs_trace_error_time_line(msg "\n", ##__VA_ARGS__);             \
41 	} while (0)
42 
43 #define PASS()                                                                 \
44 	do {                                                                   \
45 		bst_result = Passed;                                           \
46 		bs_trace_info_time(1, "%s PASSED\n", __func__);                \
47 	} while (0)
48 
49 #define ASSERT_OK(cond)                                                        \
50 	do {                                                                   \
51 		int _err = (cond);                                             \
52 		if (_err) {                                                    \
53 			bst_result = Failed;                                   \
54 			bs_trace_error_time_line(                              \
55 				#cond " failed with error %d\n", _err);        \
56 		}                                                              \
57 	} while (0)
58 
59 #define ASSERT_OK_MSG(cond, fmt, ...)                                          \
60 	do {                                                                   \
61 		int _err = (cond);                                             \
62 		if (_err) {                                                    \
63 			bst_result = Failed;                                   \
64 			bs_trace_error_time_line(                              \
65 				#cond " failed with error %d\n" fmt, _err,     \
66 				##__VA_ARGS__);                                \
67 		}                                                              \
68 	} while (0)
69 
70 #define ASSERT_TRUE(cond)                                                      \
71 	do {                                                                   \
72 		if (!(cond)) {                                                 \
73 			bst_result = Failed;                                   \
74 			bs_trace_error_time_line(                              \
75 				#cond " is false.\n");                         \
76 		}                                                              \
77 	} while (0)
78 
79 #define ASSERT_TRUE_MSG(cond, fmt, ...)                                        \
80 	do {                                                                   \
81 		if (!(cond)) {                                                 \
82 			bst_result = Failed;                                   \
83 			bs_trace_error_time_line(                              \
84 				#cond " is false. " fmt, ##__VA_ARGS__);       \
85 		}                                                              \
86 	} while (0)
87 
88 
89 #define ASSERT_FALSE(cond)                                                     \
90 	do {                                                                   \
91 		if (cond) {                                                    \
92 			bst_result = Failed;                                   \
93 			bs_trace_error_time_line(                              \
94 				#cond " is true.\n");                          \
95 		}                                                              \
96 	} while (0)
97 
98 #define ASSERT_FALSE_MSG(cond, fmt, ...)                                       \
99 	do {                                                                   \
100 		if (cond) {                                                    \
101 			bst_result = Failed;                                   \
102 			bs_trace_error_time_line(                              \
103 				#cond " is true. " fmt, ##__VA_ARGS__);        \
104 		}                                                              \
105 	} while (0)
106 
107 #define ASSERT_EQUAL(expected, got)                                            \
108 	do {                                                                   \
109 		if ((expected) != (got)) {                                     \
110 			bst_result = Failed;                                   \
111 			bs_trace_error_time_line(                              \
112 				#expected " not equal to " #got ": %d != %d\n",\
113 				(expected), (got));                            \
114 		}                                                              \
115 	} while (0)
116 
117 #define ASSERT_IN_RANGE(got, min, max)                                                             \
118 	do {                                                                                       \
119 		if (!IN_RANGE(got, min, max)) {                                            \
120 			bst_result = Failed;                                                       \
121 			bs_trace_error_time_line(#got " not in range %d <-> %d, " #got " = %d\n",  \
122 						 (min), (max), (got));                             \
123 		}                                                                                  \
124 	} while (0)
125 
126 #define WAIT_FOR_COND(cond, wait)                                                                  \
127 	do {                                                                                       \
128 		bool _err = false;                                                                 \
129 		for (uint8_t sec = (wait); !(cond); sec--) {                                       \
130 			if (!sec) {                                                                \
131 				_err = true;                                                       \
132 				break;                                                             \
133 			}                                                                          \
134 			k_sleep(K_SECONDS(1));                                                     \
135 		}                                                                                  \
136                                                                                                    \
137 		if (_err) {                                                                        \
138 			bst_result = Failed;                                                       \
139 			bs_trace_error_time_line("Waiting for " #cond " timed out\n");             \
140 		}                                                                                  \
141 	} while (0)
142 
143 struct bt_mesh_test_cfg {
144 	uint16_t addr;
145 	uint8_t dev_key[16];
146 };
147 
148 enum bt_mesh_test_send_flags {
149 	FORCE_SEGMENTATION = BIT(0),
150 	LONG_MIC = BIT(1),
151 };
152 
153 struct bt_mesh_test_stats {
154 	uint32_t received;
155 	uint32_t sent;
156 	uint32_t recv_overflow;
157 };
158 
159 struct bt_mesh_test_msg {
160 	sys_snode_t _node;
161 	size_t len;
162 	uint8_t seq;
163 	struct bt_mesh_msg_ctx ctx;
164 };
165 
166 struct bt_mesh_test_sync_ctx {
167 	uint32_t *dev_nmbr;
168 	uint32_t *chan_nmbr;
169 	uint32_t *chan_id;
170 	uint16_t cnt;
171 };
172 
173 extern enum bst_result_t bst_result;
174 extern const struct bt_mesh_test_cfg *cfg;
175 extern const struct bt_mesh_model *test_model;
176 extern const struct bt_mesh_model *test_vnd_model;
177 extern const uint8_t test_net_key[16];
178 extern const uint8_t test_app_key[16];
179 extern const uint8_t test_va_uuid[16];
180 extern struct bt_mesh_test_stats test_stats;
181 extern struct bt_mesh_msg_ctx test_send_ctx;
182 
183 void bt_mesh_test_cfg_set(const struct bt_mesh_test_cfg *cfg, int wait_time);
184 void bt_mesh_test_setup(void);
185 void bt_mesh_test_timeout(bs_time_t HW_device_time);
186 
187 void bt_mesh_device_setup(const struct bt_mesh_prov *prov, const struct bt_mesh_comp *comp);
188 
189 int bt_mesh_test_recv(uint16_t len, uint16_t dst, const uint8_t *uuid, k_timeout_t timeout);
190 int bt_mesh_test_recv_msg(struct bt_mesh_test_msg *msg, k_timeout_t timeout);
191 int bt_mesh_test_recv_clear(void);
192 
193 int bt_mesh_test_send(uint16_t addr, const uint8_t *uuid, size_t len,
194 		      enum bt_mesh_test_send_flags flags, k_timeout_t timeout);
195 int bt_mesh_test_send_async(uint16_t addr, const uint8_t *uuid, size_t len,
196 			    enum bt_mesh_test_send_flags flags,
197 			    const struct bt_mesh_send_cb *send_cb,
198 			    void *cb_data);
199 int bt_mesh_test_send_ra(uint16_t addr, uint8_t *data, size_t len,
200 			 const struct bt_mesh_send_cb *send_cb,
201 			 void *cb_data);
202 void bt_mesh_test_ra_cb_setup(void (*cb)(uint8_t *, size_t));
203 
204 uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr);
205 
206 void bt_mesh_test_send_over_adv(void *data, size_t len);
207 /* Wait for a packet (i. e. an advertisement or a GATT frame) sent by a device.
208  * `scan_cb` is triggered if the packet is received, and must release `observer_sem` when finished.
209  */
210 int bt_mesh_test_wait_for_packet(bt_le_scan_cb_t scan_cb, struct k_sem *observer_sem,
211 				 uint16_t wait);
212 
213 #if defined(CONFIG_BT_MESH_SAR_CFG)
214 void bt_mesh_test_sar_conf_set(struct bt_mesh_sar_tx *tx_set, struct bt_mesh_sar_rx *rx_set);
215 #endif
216 
217 #endif /* ZEPHYR_TESTS_BLUETOOTH_BSIM_BT_BSIM_TEST_MESH_MESH_TEST_H_ */
218