1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/net/buf.h>
8 #include <zephyr/bluetooth/bluetooth.h>
9 #include <zephyr/bluetooth/conn.h>
10 #include <zephyr/bluetooth/gatt.h>
11 #include <zephyr/bluetooth/mesh.h>
12 #include <zephyr/types.h>
13 
14 #include "net.h"
15 #include "sar_cfg_internal.h"
16 
bt_mesh_sar_tx_encode(struct net_buf_simple * buf,const struct bt_mesh_sar_tx * tx)17 void bt_mesh_sar_tx_encode(struct net_buf_simple *buf,
18 			   const struct bt_mesh_sar_tx *tx)
19 {
20 	net_buf_simple_add_u8(buf, (tx->seg_int_step & 0xf) |
21 					   (tx->unicast_retrans_count << 4));
22 	net_buf_simple_add_u8(buf, (tx->unicast_retrans_without_prog_count &
23 				    0xf) | (tx->unicast_retrans_int_step << 4));
24 	net_buf_simple_add_u8(buf, (tx->unicast_retrans_int_inc & 0xf) |
25 					   (tx->multicast_retrans_count << 4));
26 	net_buf_simple_add_u8(buf, tx->multicast_retrans_int & 0xf);
27 }
28 
bt_mesh_sar_rx_encode(struct net_buf_simple * buf,const struct bt_mesh_sar_rx * rx)29 void bt_mesh_sar_rx_encode(struct net_buf_simple *buf,
30 			   const struct bt_mesh_sar_rx *rx)
31 {
32 	net_buf_simple_add_u8(buf, (rx->seg_thresh & 0x1f) |
33 					   (rx->ack_delay_inc << 5));
34 	net_buf_simple_add_u8(buf, (rx->discard_timeout & 0xf) |
35 					   ((rx->rx_seg_int_step & 0xf) << 4));
36 	net_buf_simple_add_u8(buf, (rx->ack_retrans_count & 0x3));
37 }
38 
bt_mesh_sar_tx_decode(struct net_buf_simple * buf,struct bt_mesh_sar_tx * tx)39 void bt_mesh_sar_tx_decode(struct net_buf_simple *buf,
40 			   struct bt_mesh_sar_tx *tx)
41 {
42 	uint8_t val;
43 
44 	val = net_buf_simple_pull_u8(buf);
45 	tx->seg_int_step = (val & 0xf);
46 	tx->unicast_retrans_count = (val >> 4);
47 	val = net_buf_simple_pull_u8(buf);
48 	tx->unicast_retrans_without_prog_count = (val & 0xf);
49 	tx->unicast_retrans_int_step = (val >> 4);
50 	val = net_buf_simple_pull_u8(buf);
51 	tx->unicast_retrans_int_inc = (val & 0xf);
52 	tx->multicast_retrans_count = (val >> 4);
53 	val = net_buf_simple_pull_u8(buf);
54 	tx->multicast_retrans_int = (val & 0xf);
55 }
56 
bt_mesh_sar_rx_decode(struct net_buf_simple * buf,struct bt_mesh_sar_rx * rx)57 void bt_mesh_sar_rx_decode(struct net_buf_simple *buf,
58 			   struct bt_mesh_sar_rx *rx)
59 {
60 	uint8_t val;
61 
62 	val = net_buf_simple_pull_u8(buf);
63 	rx->seg_thresh = (val & 0x1f);
64 	rx->ack_delay_inc = (val >> 5);
65 	val = net_buf_simple_pull_u8(buf);
66 	rx->discard_timeout = (val & 0xf);
67 	rx->rx_seg_int_step = (val >> 4);
68 	val = net_buf_simple_pull_u8(buf);
69 	rx->ack_retrans_count = (val & 0x3);
70 }
71