1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "mesh_test.h"
7 #include "argparse.h"
8 #include <bs_pc_backchannel.h>
9 #include "mesh/crypto.h"
10 #include <zephyr/bluetooth/hci.h>
11 
12 #define LOG_MODULE_NAME mesh_test
13 
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
16 
17 #include "common/bt_str.h"
18 
19 /* Max number of messages that can be pending on RX at the same time */
20 #define RECV_QUEUE_SIZE 32
21 
22 const struct bt_mesh_test_cfg *cfg;
23 
24 K_MEM_SLAB_DEFINE_STATIC(msg_pool, sizeof(struct bt_mesh_test_msg),
25 			 RECV_QUEUE_SIZE, 4);
26 static K_QUEUE_DEFINE(recv);
27 struct bt_mesh_test_stats test_stats;
28 struct bt_mesh_msg_ctx test_send_ctx;
29 static void (*ra_cb)(uint8_t *, size_t);
30 
msg_rx(const struct bt_mesh_model * mod,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)31 static int msg_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx,
32 		   struct net_buf_simple *buf)
33 {
34 	size_t len = buf->len + BT_MESH_MODEL_OP_LEN(TEST_MSG_OP_1);
35 	static uint8_t prev_seq;
36 	struct bt_mesh_test_msg *msg;
37 	uint8_t seq = 0;
38 
39 	if (buf->len) {
40 		seq = net_buf_simple_pull_u8(buf);
41 		if (prev_seq == seq) {
42 			FAIL("Received same message twice");
43 			return -EINVAL;
44 		}
45 
46 		prev_seq = seq;
47 	}
48 
49 	LOG_INF("Received packet 0x%02x:", seq);
50 	LOG_INF("\tlen: %d bytes", len);
51 	LOG_INF("\tsrc: 0x%04x", ctx->addr);
52 	LOG_INF("\tdst: 0x%04x", ctx->recv_dst);
53 	LOG_INF("\tttl: %u", ctx->recv_ttl);
54 	LOG_INF("\trssi: %d", ctx->recv_rssi);
55 
56 	for (int i = 1; buf->len; i++) {
57 		if (net_buf_simple_pull_u8(buf) != (i & 0xff)) {
58 			FAIL("Invalid message content (byte %u)", i);
59 			return -EINVAL;
60 		}
61 	}
62 
63 	test_stats.received++;
64 
65 	if (k_mem_slab_alloc(&msg_pool, (void **)&msg, K_NO_WAIT)) {
66 		test_stats.recv_overflow++;
67 		return -EOVERFLOW;
68 	}
69 
70 	msg->len = len;
71 	msg->seq = seq;
72 	msg->ctx = *ctx;
73 
74 	k_queue_append(&recv, msg);
75 
76 	return 0;
77 }
78 
ra_rx(const struct bt_mesh_model * mod,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)79 static int ra_rx(const struct bt_mesh_model *mod, struct bt_mesh_msg_ctx *ctx,
80 		 struct net_buf_simple *buf)
81 {
82 	LOG_INF("\tlen: %d bytes", buf->len);
83 	LOG_INF("\tsrc: 0x%04x", ctx->addr);
84 	LOG_INF("\tdst: 0x%04x", ctx->recv_dst);
85 	LOG_INF("\tttl: %u", ctx->recv_ttl);
86 	LOG_INF("\trssi: %d", ctx->recv_rssi);
87 
88 	if (ra_cb) {
89 		ra_cb(net_buf_simple_pull_mem(buf, buf->len), buf->len);
90 	}
91 
92 	return 0;
93 }
94 
95 static const struct bt_mesh_model_op model_op[] = {
96 	{ TEST_MSG_OP_1, 0, msg_rx },
97 	{ TEST_MSG_OP_2, 0, ra_rx },
98 	BT_MESH_MODEL_OP_END
99 };
100 
test_model_pub_update(const struct bt_mesh_model * mod)101 int __weak test_model_pub_update(const struct bt_mesh_model *mod)
102 {
103 	return -1;
104 }
105 
test_model_settings_set(const struct bt_mesh_model * model,const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)106 int __weak test_model_settings_set(const struct bt_mesh_model *model,
107 				   const char *name, size_t len_rd,
108 				   settings_read_cb read_cb, void *cb_arg)
109 {
110 	return -1;
111 }
112 
test_model_reset(const struct bt_mesh_model * model)113 void __weak test_model_reset(const struct bt_mesh_model *model)
114 {
115 	/* No-op. */
116 }
117 
118 static const struct bt_mesh_model_cb test_model_cb = {
119 	.settings_set = test_model_settings_set,
120 	.reset = test_model_reset,
121 };
122 
123 static struct bt_mesh_model_pub pub = {
124 	.msg = NET_BUF_SIMPLE(BT_MESH_TX_SDU_MAX),
125 	.update = test_model_pub_update,
126 };
127 
128 static const struct bt_mesh_model_op vnd_model_op[] = {
129 	BT_MESH_MODEL_OP_END,
130 };
131 
test_vnd_model_pub_update(const struct bt_mesh_model * mod)132 int __weak test_vnd_model_pub_update(const struct bt_mesh_model *mod)
133 {
134 	return -1;
135 }
136 
test_vnd_model_settings_set(const struct bt_mesh_model * model,const char * name,size_t len_rd,settings_read_cb read_cb,void * cb_arg)137 int __weak test_vnd_model_settings_set(const struct bt_mesh_model *model,
138 				       const char *name, size_t len_rd,
139 				       settings_read_cb read_cb, void *cb_arg)
140 {
141 	return -1;
142 }
143 
test_vnd_model_reset(const struct bt_mesh_model * model)144 void __weak test_vnd_model_reset(const struct bt_mesh_model *model)
145 {
146 	/* No-op. */
147 }
148 
149 static const struct bt_mesh_model_cb test_vnd_model_cb = {
150 	.settings_set = test_vnd_model_settings_set,
151 	.reset = test_vnd_model_reset,
152 };
153 
154 static struct bt_mesh_model_pub vnd_pub = {
155 	.msg = NET_BUF_SIMPLE(BT_MESH_TX_SDU_MAX),
156 	.update = test_vnd_model_pub_update,
157 };
158 
159 static struct bt_mesh_cfg_cli cfg_cli;
160 
161 static struct bt_mesh_health_srv health_srv;
162 static struct bt_mesh_model_pub health_pub = {
163 	.msg = NET_BUF_SIMPLE(BT_MESH_TX_SDU_MAX),
164 };
165 
166 #if defined(CONFIG_BT_MESH_SAR_CFG)
167 static struct bt_mesh_sar_cfg_cli sar_cfg_cli;
168 #endif
169 
170 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
171 static struct bt_mesh_priv_beacon_cli priv_beacon_cli;
172 #endif
173 
174 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
175 static struct bt_mesh_od_priv_proxy_cli priv_proxy_cli;
176 #endif
177 
178 static const struct bt_mesh_model models[] = {
179 	BT_MESH_MODEL_CFG_SRV,
180 	BT_MESH_MODEL_CFG_CLI(&cfg_cli),
181 	BT_MESH_MODEL_CB(TEST_MOD_ID, model_op, &pub, NULL, &test_model_cb),
182 	BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub),
183 #if defined(CONFIG_BT_MESH_SAR_CFG)
184 	BT_MESH_MODEL_SAR_CFG_SRV,
185 	BT_MESH_MODEL_SAR_CFG_CLI(&sar_cfg_cli),
186 #endif
187 #if defined(CONFIG_BT_MESH_PRIV_BEACONS)
188 	BT_MESH_MODEL_PRIV_BEACON_SRV,
189 	BT_MESH_MODEL_PRIV_BEACON_CLI(&priv_beacon_cli),
190 #endif
191 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
192 	BT_MESH_MODEL_OD_PRIV_PROXY_SRV,
193 #endif
194 #if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_CLI)
195 	BT_MESH_MODEL_OD_PRIV_PROXY_CLI(&priv_proxy_cli),
196 #endif
197 };
198 
199 const struct bt_mesh_model *test_model = &models[2];
200 
201 static const struct bt_mesh_model vnd_models[] = {
202 	BT_MESH_MODEL_VND_CB(TEST_VND_COMPANY_ID, TEST_VND_MOD_ID, vnd_model_op, &vnd_pub,
203 			     NULL, &test_vnd_model_cb),
204 };
205 
206 const struct bt_mesh_model *test_vnd_model = &vnd_models[0];
207 
208 static const struct bt_mesh_elem elems[] = {
209 	BT_MESH_ELEM(0, models, vnd_models),
210 };
211 
212 const struct bt_mesh_comp comp = {
213 	.elem = elems,
214 	.elem_count = ARRAY_SIZE(elems),
215 };
216 
217 const uint8_t test_net_key[16] = { 1, 2, 3 };
218 const uint8_t test_app_key[16] = { 4, 5, 6 };
219 const uint8_t test_va_uuid[16] = "Mesh Label UUID";
220 
bt_mesh_device_provision_and_configure(void)221 static void bt_mesh_device_provision_and_configure(void)
222 {
223 	uint8_t status;
224 	int err;
225 
226 	err = bt_mesh_provision(test_net_key, 0, 0, 0, cfg->addr, cfg->dev_key);
227 	if (err == -EALREADY) {
228 		LOG_INF("Using stored settings");
229 		return;
230 	} else if (err) {
231 		FAIL("Provisioning failed (err %d)", err);
232 		return;
233 	}
234 
235 	/* Self configure */
236 
237 	err = bt_mesh_cfg_cli_app_key_add(0, cfg->addr, 0, 0, test_app_key, &status);
238 	if (err || status) {
239 		FAIL("AppKey add failed (err %d, status %u)", err, status);
240 		return;
241 	}
242 
243 	err = bt_mesh_cfg_cli_mod_app_bind(0, cfg->addr, cfg->addr, 0, TEST_MOD_ID, &status);
244 	if (err || status) {
245 		FAIL("Mod app bind failed (err %d, status %u)", err, status);
246 		return;
247 	}
248 
249 	err = bt_mesh_cfg_cli_net_transmit_set(0, cfg->addr, BT_MESH_TRANSMIT(2, 20), &status);
250 	if (err || status != BT_MESH_TRANSMIT(2, 20)) {
251 		FAIL("Net transmit set failed (err %d, status %u)", err,
252 		     status);
253 		return;
254 	}
255 }
256 
bt_mesh_device_setup(const struct bt_mesh_prov * prov,const struct bt_mesh_comp * comp)257 void bt_mesh_device_setup(const struct bt_mesh_prov *prov, const struct bt_mesh_comp *comp)
258 {
259 	int err;
260 
261 	err = bt_enable(NULL);
262 	if (err) {
263 		FAIL("Bluetooth init failed (err %d)", err);
264 		return;
265 	}
266 
267 	LOG_INF("Bluetooth initialized");
268 
269 	err = bt_mesh_init(prov, comp);
270 	if (err) {
271 		FAIL("Initializing mesh failed (err %d)", err);
272 		return;
273 	}
274 
275 	if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
276 		LOG_INF("Loading stored settings");
277 		if (IS_ENABLED(CONFIG_BT_MESH_USES_MBEDTLS_PSA)) {
278 			settings_load_subtree("itsemul");
279 		}
280 		settings_load_subtree("bt");
281 	}
282 
283 	LOG_INF("Mesh initialized");
284 }
285 
bt_mesh_test_setup(void)286 void bt_mesh_test_setup(void)
287 {
288 	static struct bt_mesh_prov prov;
289 
290 	net_buf_simple_init(pub.msg, 0);
291 	net_buf_simple_init(vnd_pub.msg, 0);
292 
293 	bt_mesh_device_setup(&prov, &comp);
294 	bt_mesh_device_provision_and_configure();
295 }
296 
bt_mesh_test_timeout(bs_time_t HW_device_time)297 void bt_mesh_test_timeout(bs_time_t HW_device_time)
298 {
299 	if (bst_result != Passed) {
300 		FAIL("Test timeout (not passed after %i seconds)",
301 		     HW_device_time / USEC_PER_SEC);
302 	}
303 
304 	bs_trace_silent_exit(0);
305 }
306 
bt_mesh_test_cfg_set(const struct bt_mesh_test_cfg * my_cfg,int wait_time)307 void bt_mesh_test_cfg_set(const struct bt_mesh_test_cfg *my_cfg, int wait_time)
308 {
309 	bst_ticker_set_next_tick_absolute(wait_time * USEC_PER_SEC);
310 	bst_result = In_progress;
311 	cfg = my_cfg;
312 
313 	/* Ensure those test devices will not drift more than
314 	 * 100ms for each other in emulated time
315 	 */
316 	tm_set_phy_max_resync_offset(100000);
317 }
318 
blocking_recv(k_timeout_t timeout)319 static struct bt_mesh_test_msg *blocking_recv(k_timeout_t timeout)
320 {
321 	if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
322 		return 0;
323 	}
324 
325 	return k_queue_get(&recv, timeout);
326 }
327 
bt_mesh_test_recv(uint16_t len,uint16_t dst,const uint8_t * uuid,k_timeout_t timeout)328 int bt_mesh_test_recv(uint16_t len, uint16_t dst, const uint8_t *uuid, k_timeout_t timeout)
329 {
330 	struct bt_mesh_test_msg *msg = blocking_recv(timeout);
331 
332 	if (!msg) {
333 		return -ETIMEDOUT;
334 	}
335 
336 	if (len != msg->len) {
337 		LOG_ERR("Recv: Invalid message length (%u, expected %u)", msg->len, len);
338 		return -EINVAL;
339 	}
340 
341 	if (dst != BT_MESH_ADDR_UNASSIGNED && dst != msg->ctx.recv_dst) {
342 		LOG_ERR("Recv: Invalid dst 0x%04x, expected 0x%04x", msg->ctx.recv_dst, dst);
343 		return -EINVAL;
344 	}
345 
346 	if (BT_MESH_ADDR_IS_VIRTUAL(msg->ctx.recv_dst) &&
347 	    ((uuid != NULL && msg->ctx.uuid == NULL) ||
348 	     (uuid == NULL && msg->ctx.uuid != NULL) ||
349 	     memcmp(uuid, msg->ctx.uuid, 16))) {
350 		LOG_ERR("Recv: Label UUID mismatch for virtual address 0x%04x");
351 		if (uuid && msg->ctx.uuid) {
352 			LOG_ERR("Got: %s", bt_hex(msg->ctx.uuid, 16));
353 			LOG_ERR("Expected: %s", bt_hex(uuid, 16));
354 		}
355 
356 		return -EINVAL;
357 	}
358 
359 	k_mem_slab_free(&msg_pool, (void *)msg);
360 
361 	return 0;
362 }
363 
bt_mesh_test_recv_msg(struct bt_mesh_test_msg * msg,k_timeout_t timeout)364 int bt_mesh_test_recv_msg(struct bt_mesh_test_msg *msg, k_timeout_t timeout)
365 {
366 	struct bt_mesh_test_msg *queued = blocking_recv(timeout);
367 
368 	if (!queued) {
369 		return -ETIMEDOUT;
370 	}
371 
372 	*msg = *queued;
373 
374 	k_mem_slab_free(&msg_pool, (void *)queued);
375 
376 	return 0;
377 }
378 
bt_mesh_test_recv_clear(void)379 int bt_mesh_test_recv_clear(void)
380 {
381 	struct bt_mesh_test_msg *queued;
382 	int count = 0;
383 
384 	while ((queued = k_queue_get(&recv, K_NO_WAIT))) {
385 		k_mem_slab_free(&msg_pool, (void *)queued);
386 		count++;
387 	}
388 
389 	return count;
390 }
391 
392 struct sync_send_ctx {
393 	struct k_sem sem;
394 	int err;
395 };
396 
tx_started(uint16_t dur,int err,void * data)397 static void tx_started(uint16_t dur, int err, void *data)
398 {
399 	struct sync_send_ctx *send_ctx = data;
400 
401 	if (err) {
402 		LOG_ERR("Couldn't start sending (err: %d)", err);
403 
404 		send_ctx->err = err;
405 		k_sem_give(&send_ctx->sem);
406 
407 		return;
408 	}
409 
410 	LOG_INF("Sending started");
411 }
412 
tx_ended(int err,void * data)413 static void tx_ended(int err, void *data)
414 {
415 	struct sync_send_ctx *send_ctx = data;
416 
417 	send_ctx->err = err;
418 
419 	if (err) {
420 		LOG_ERR("Send failed (%d)", err);
421 	} else {
422 		LOG_INF("Sending ended");
423 	}
424 
425 	k_sem_give(&send_ctx->sem);
426 }
427 
bt_mesh_test_send_async(uint16_t addr,const uint8_t * uuid,size_t len,enum bt_mesh_test_send_flags flags,const struct bt_mesh_send_cb * send_cb,void * cb_data)428 int bt_mesh_test_send_async(uint16_t addr, const uint8_t *uuid, size_t len,
429 			    enum bt_mesh_test_send_flags flags,
430 			    const struct bt_mesh_send_cb *send_cb,
431 			    void *cb_data)
432 {
433 	const size_t mic_len =
434 		(flags & LONG_MIC) ? BT_MESH_MIC_LONG : BT_MESH_MIC_SHORT;
435 	static uint8_t count = 1;
436 	int err;
437 
438 	test_send_ctx.addr = addr;
439 	test_send_ctx.send_rel = (flags & FORCE_SEGMENTATION);
440 	test_send_ctx.send_ttl = BT_MESH_TTL_DEFAULT;
441 	test_send_ctx.uuid = uuid;
442 
443 	BT_MESH_MODEL_BUF_DEFINE(buf, TEST_MSG_OP_1, BT_MESH_TX_SDU_MAX);
444 	bt_mesh_model_msg_init(&buf, TEST_MSG_OP_1);
445 
446 	if (len > BT_MESH_MODEL_OP_LEN(TEST_MSG_OP_1)) {
447 		net_buf_simple_add_u8(&buf, count);
448 	}
449 
450 	/* Subtract the length of the opcode and the sequence ID */
451 	for (int i = 1; i < len - BT_MESH_MODEL_OP_LEN(TEST_MSG_OP_1); i++) {
452 		net_buf_simple_add_u8(&buf, i);
453 	}
454 
455 	if (net_buf_simple_tailroom(&buf) < mic_len) {
456 		LOG_ERR("No room for MIC of len %u in %u byte buffer", mic_len,
457 			buf.len);
458 		return -EINVAL;
459 	}
460 
461 	/* Seal the buffer to prevent accidentally long MICs: */
462 	buf.size = buf.len + mic_len;
463 
464 	LOG_INF("Sending packet 0x%02x: %u %s to 0x%04x force seg: %u...",
465 		count, buf.len, (buf.len == 1 ? "byte" : "bytes"), addr,
466 		(flags & FORCE_SEGMENTATION));
467 
468 	err = bt_mesh_model_send(test_model, &test_send_ctx, &buf, send_cb,
469 				 cb_data);
470 	if (err) {
471 		LOG_ERR("bt_mesh_model_send failed (err: %d)", err);
472 		return err;
473 	}
474 
475 	count++;
476 	test_stats.sent++;
477 	return 0;
478 }
479 
bt_mesh_test_send(uint16_t addr,const uint8_t * uuid,size_t len,enum bt_mesh_test_send_flags flags,k_timeout_t timeout)480 int bt_mesh_test_send(uint16_t addr, const uint8_t *uuid, size_t len,
481 		      enum bt_mesh_test_send_flags flags, k_timeout_t timeout)
482 {
483 	if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
484 		return bt_mesh_test_send_async(addr, uuid, len, flags, NULL, NULL);
485 	}
486 
487 	static const struct bt_mesh_send_cb send_cb = {
488 		.start = tx_started,
489 		.end = tx_ended,
490 	};
491 	int64_t uptime = k_uptime_get();
492 	struct sync_send_ctx send_ctx;
493 	int err;
494 
495 	k_sem_init(&send_ctx.sem, 0, 1);
496 	err = bt_mesh_test_send_async(addr, uuid, len, flags, &send_cb, &send_ctx);
497 	if (err) {
498 		return err;
499 	}
500 
501 	err = k_sem_take(&send_ctx.sem, timeout);
502 	if (err) {
503 		LOG_ERR("Send timed out");
504 		return err;
505 	}
506 
507 	if (send_ctx.err) {
508 		return send_ctx.err;
509 	}
510 
511 	LOG_INF("Sending completed (%lld ms)", k_uptime_delta(&uptime));
512 
513 	return 0;
514 }
515 
bt_mesh_test_send_ra(uint16_t addr,uint8_t * data,size_t len,const struct bt_mesh_send_cb * send_cb,void * cb_data)516 int bt_mesh_test_send_ra(uint16_t addr, uint8_t *data, size_t len,
517 			 const struct bt_mesh_send_cb *send_cb,
518 			 void *cb_data)
519 {
520 	int err;
521 
522 	test_send_ctx.addr = addr;
523 	test_send_ctx.send_rel = 0;
524 	test_send_ctx.send_ttl = BT_MESH_TTL_DEFAULT;
525 
526 	BT_MESH_MODEL_BUF_DEFINE(buf, TEST_MSG_OP_2, BT_MESH_TX_SDU_MAX);
527 	bt_mesh_model_msg_init(&buf, TEST_MSG_OP_2);
528 
529 	net_buf_simple_add_mem(&buf, data, len);
530 
531 	err = bt_mesh_model_send(test_model, &test_send_ctx, &buf, send_cb, cb_data);
532 	if (err) {
533 		LOG_ERR("bt_mesh_model_send failed (err: %d)", err);
534 		return err;
535 	}
536 
537 	return 0;
538 }
539 
bt_mesh_test_ra_cb_setup(void (* cb)(uint8_t *,size_t))540 void bt_mesh_test_ra_cb_setup(void (*cb)(uint8_t *, size_t))
541 {
542 	ra_cb = cb;
543 }
544 
bt_mesh_test_own_addr_get(uint16_t start_addr)545 uint16_t bt_mesh_test_own_addr_get(uint16_t start_addr)
546 {
547 	return start_addr + get_device_nbr();
548 }
549 
bt_mesh_test_send_over_adv(void * data,size_t len)550 void bt_mesh_test_send_over_adv(void *data, size_t len)
551 {
552 	struct bt_mesh_adv *adv = bt_mesh_adv_create(BT_MESH_ADV_DATA, BT_MESH_ADV_TAG_LOCAL,
553 						     BT_MESH_TRANSMIT(0, 20), K_NO_WAIT);
554 	net_buf_simple_add_mem(&adv->b, data, len);
555 	bt_mesh_adv_send(adv, NULL, NULL);
556 }
557 
bt_mesh_test_wait_for_packet(bt_le_scan_cb_t scan_cb,struct k_sem * observer_sem,uint16_t wait)558 int bt_mesh_test_wait_for_packet(bt_le_scan_cb_t scan_cb, struct k_sem *observer_sem, uint16_t wait)
559 {
560 	struct bt_le_scan_param scan_param = {
561 		.type       = BT_HCI_LE_SCAN_PASSIVE,
562 		.options    = BT_LE_SCAN_OPT_NONE,
563 		.interval   = BT_MESH_ADV_SCAN_UNIT(1000),
564 		.window     = BT_MESH_ADV_SCAN_UNIT(1000)
565 	};
566 	int err;
567 	int returned_value = 0;
568 
569 	err = bt_le_scan_start(&scan_param, scan_cb);
570 	if (err && err != -EALREADY) {
571 		LOG_ERR("Starting scan failed (err %d)", err);
572 		return err;
573 	}
574 
575 	err = k_sem_take(observer_sem, K_SECONDS(wait));
576 	if (err == -EAGAIN) {
577 		LOG_WRN("Taking sem timed out (err %d)", err);
578 		returned_value = -ETIMEDOUT;
579 	} else if (err) {
580 		LOG_ERR("Taking sem failed (err %d)", err);
581 		return err;
582 	}
583 
584 	err = bt_le_scan_stop();
585 	if (err && err != -EALREADY) {
586 		LOG_ERR("Stopping scan failed (err %d)", err);
587 		return err;
588 	}
589 
590 	return returned_value;
591 }
592 
593 
594 #if defined(CONFIG_BT_MESH_SAR_CFG)
bt_mesh_test_sar_conf_set(struct bt_mesh_sar_tx * tx_set,struct bt_mesh_sar_rx * rx_set)595 void bt_mesh_test_sar_conf_set(struct bt_mesh_sar_tx *tx_set, struct bt_mesh_sar_rx *rx_set)
596 {
597 	int err;
598 
599 	if (tx_set) {
600 		struct bt_mesh_sar_tx tx_rsp;
601 
602 		err = bt_mesh_sar_cfg_cli_transmitter_set(0, cfg->addr, tx_set, &tx_rsp);
603 		if (err) {
604 			FAIL("Failed to configure SAR Transmitter state (err %d)", err);
605 		}
606 	}
607 
608 	if (rx_set) {
609 		struct bt_mesh_sar_rx rx_rsp;
610 
611 		err = bt_mesh_sar_cfg_cli_receiver_set(0, cfg->addr, rx_set, &rx_rsp);
612 		if (err) {
613 			FAIL("Failed to configure SAR Receiver state (err %d)", err);
614 		}
615 	}
616 }
617 #endif /* defined(CONFIG_BT_MESH_SAR_CFG) */
618