1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "dfu_blob_common.h"
8 #include <zephyr/sys/util.h>
9 #include "mesh_test.h"
10 
11 static struct {
12 	uint16_t addrs[6];
13 	int rem_cnt;
14 } lost_targets;
15 
lost_target_find_and_remove(uint16_t addr)16 bool lost_target_find_and_remove(uint16_t addr)
17 {
18 	for (int i = 0; i < ARRAY_SIZE(lost_targets.addrs); i++) {
19 		if (addr == lost_targets.addrs[i]) {
20 			lost_targets.addrs[i] = 0;
21 			ASSERT_TRUE(lost_targets.rem_cnt > 0);
22 			lost_targets.rem_cnt--;
23 			return true;
24 		}
25 	}
26 
27 	return false;
28 }
29 
lost_target_add(uint16_t addr)30 void lost_target_add(uint16_t addr)
31 {
32 	if (lost_targets.rem_cnt >= ARRAY_SIZE(lost_targets.addrs)) {
33 		FAIL("No more room in lost target list");
34 		return;
35 	}
36 
37 	lost_targets.addrs[lost_targets.rem_cnt] = addr;
38 	lost_targets.rem_cnt++;
39 }
40 
lost_targets_rem(void)41 int lost_targets_rem(void)
42 {
43 	return lost_targets.rem_cnt;
44 }
45 
common_sar_conf(uint16_t addr)46 void common_sar_conf(uint16_t addr)
47 {
48 	int err;
49 
50 	/* Reconfigure SAR Transmitter state to change compile-time configuration to default
51 	 * configuration.
52 	 */
53 	struct bt_mesh_sar_tx tx_set = {
54 		.seg_int_step = 1,
55 		.unicast_retrans_count = 3,
56 		.unicast_retrans_without_prog_count = 2,
57 		.unicast_retrans_int_step = 7,
58 		.unicast_retrans_int_inc = 1,
59 		.multicast_retrans_count = 2,
60 		.multicast_retrans_int = 3,
61 	};
62 	struct bt_mesh_sar_tx tx_rsp;
63 
64 	err = bt_mesh_sar_cfg_cli_transmitter_set(0, addr, &tx_set, &tx_rsp);
65 	if (err) {
66 		FAIL("Failed to configure SAR Transmitter state (err %d)", err);
67 	}
68 
69 	/* Reconfigure SAR Receiver state so that the transport layer doesn't generate SegAcks too
70 	 * frequently.
71 	 */
72 	struct bt_mesh_sar_rx rx_set = {
73 		.seg_thresh = 0x1f,
74 		.ack_delay_inc = 7,
75 		.discard_timeout = 1,
76 		.rx_seg_int_step = 0xf,
77 		.ack_retrans_count = 1,
78 	};
79 	struct bt_mesh_sar_rx rx_rsp;
80 
81 	err = bt_mesh_sar_cfg_cli_receiver_set(0, addr, &rx_set, &rx_rsp);
82 	if (err) {
83 		FAIL("Failed to configure SAR Receiver state (err %d)", err);
84 	}
85 }
86