1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @file
8  * @brief Internal Transport SAR Configuration API
9  */
10 
11 #ifndef ZEPHYR_SUBSYS_BLUETOOTH_MESH_SAR_CFG_INTERNAL_H__
12 #define ZEPHYR_SUBSYS_BLUETOOTH_MESH_SAR_CFG_INTERNAL_H__
13 
14 /** SAR Transmitter state encoded length */
15 #define BT_MESH_SAR_TX_LEN 4
16 /** SAR Receiver state encoded length */
17 #define BT_MESH_SAR_RX_LEN 3
18 
19 /** SAR Transmitter Configuration state initializer */
20 #define BT_MESH_SAR_TX_INIT                                                       \
21 	{                                                                         \
22 		CONFIG_BT_MESH_SAR_TX_SEG_INT_STEP,                               \
23 			CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_COUNT,              \
24 			CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_WITHOUT_PROG_COUNT, \
25 			CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_INT_STEP,           \
26 			CONFIG_BT_MESH_SAR_TX_UNICAST_RETRANS_INT_INC,            \
27 			CONFIG_BT_MESH_SAR_TX_MULTICAST_RETRANS_COUNT,            \
28 			CONFIG_BT_MESH_SAR_TX_MULTICAST_RETRANS_INT,              \
29 	}
30 
31 /** SAR Receiver Configuration state initializer */
32 #define BT_MESH_SAR_RX_INIT                                                    \
33 	{                                                                      \
34 		CONFIG_BT_MESH_SAR_RX_SEG_THRESHOLD,                           \
35 			CONFIG_BT_MESH_SAR_RX_ACK_DELAY_INC,                   \
36 			CONFIG_BT_MESH_SAR_RX_DISCARD_TIMEOUT,                 \
37 			CONFIG_BT_MESH_SAR_RX_SEG_INT_STEP,                    \
38 			CONFIG_BT_MESH_SAR_RX_ACK_RETRANS_COUNT,               \
39 	}
40 
41 #define BT_MESH_SAR_TX_SEG_INT_MS ((bt_mesh.sar_tx.seg_int_step + 1) * 10)
42 #define BT_MESH_SAR_TX_RETRANS_COUNT(addr)                                     \
43 	(BT_MESH_ADDR_IS_UNICAST(addr) ?                                       \
44 		 (bt_mesh.sar_tx.unicast_retrans_count + 1) :                  \
45 		 (bt_mesh.sar_tx.multicast_retrans_count + 1))
46 #define BT_MESH_SAR_TX_RETRANS_NO_PROGRESS                                     \
47 	(bt_mesh.sar_tx.unicast_retrans_without_prog_count + 1)
48 #define BT_MESH_SAR_TX_UNICAST_RETRANS_INT_STEP_MS                             \
49 	((bt_mesh.sar_tx.unicast_retrans_int_step + 1) * 25)
50 #define BT_MESH_SAR_TX_UNICAST_RETRANS_INT_INC_MS                              \
51 	((bt_mesh.sar_tx.unicast_retrans_int_inc + 1) * 25)
52 #define BT_MESH_SAR_TX_UNICAST_RETRANS_TIMEOUT_MS(ttl)                         \
53 	((ttl > 0) ? (BT_MESH_SAR_TX_UNICAST_RETRANS_INT_STEP_MS +             \
54 		      BT_MESH_SAR_TX_UNICAST_RETRANS_INT_INC_MS * (ttl - 1)) : \
55 		     BT_MESH_SAR_TX_UNICAST_RETRANS_INT_STEP_MS)
56 #define BT_MESH_SAR_TX_MULTICAST_RETRANS_TIMEOUT_MS                            \
57 	((bt_mesh.sar_tx.multicast_retrans_int + 1) * 25)
58 #define BT_MESH_SAR_TX_RETRANS_TIMEOUT_MS(addr, ttl)                           \
59 	(BT_MESH_ADDR_IS_UNICAST(addr) ?                                       \
60 		 BT_MESH_SAR_TX_UNICAST_RETRANS_TIMEOUT_MS(ttl) :              \
61 		 BT_MESH_SAR_TX_MULTICAST_RETRANS_TIMEOUT_MS)
62 
63 #define BT_MESH_SAR_RX_SEG_THRESHOLD (bt_mesh.sar_rx.seg_thresh)
64 #define BT_MESH_SAR_RX_ACK_DELAY_INC_X2 (bt_mesh.sar_rx.ack_delay_inc * 2 + 3)
65 #define BT_MESH_SAR_RX_ACK_RETRANS_COUNT (bt_mesh.sar_rx.ack_retrans_count + 1)
66 #define BT_MESH_SAR_RX_SEG_INT_MS ((bt_mesh.sar_rx.rx_seg_int_step + 1) * 10)
67 #define BT_MESH_SAR_RX_DISCARD_TIMEOUT_MS                                      \
68 	((bt_mesh.sar_rx.discard_timeout + 1) * 5 * MSEC_PER_SEC)
69 
70 void bt_mesh_sar_tx_encode(struct net_buf_simple *buf,
71 			   const struct bt_mesh_sar_tx *tx);
72 void bt_mesh_sar_rx_encode(struct net_buf_simple *buf,
73 			   const struct bt_mesh_sar_rx *rx);
74 void bt_mesh_sar_tx_decode(struct net_buf_simple *buf,
75 			   struct bt_mesh_sar_tx *tx);
76 void bt_mesh_sar_rx_decode(struct net_buf_simple *buf,
77 			   struct bt_mesh_sar_rx *rx);
78 
79 #endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_SAR_CFG_INTERNAL_H__ */
80