1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <string.h>
8 #include <zephyr/sys/util.h>
9 
10 #define BT_MESH_BLOB_OP_XFER_GET BT_MESH_MODEL_OP_2(0x83, 0x00)
11 #define BT_MESH_BLOB_OP_XFER_START BT_MESH_MODEL_OP_2(0x83, 0x01)
12 #define BT_MESH_BLOB_OP_XFER_CANCEL BT_MESH_MODEL_OP_2(0x83, 0x02)
13 #define BT_MESH_BLOB_OP_XFER_STATUS BT_MESH_MODEL_OP_2(0x83, 0x03)
14 #define BT_MESH_BLOB_OP_BLOCK_GET BT_MESH_MODEL_OP_2(0x83, 0x05)
15 #define BT_MESH_BLOB_OP_BLOCK_START BT_MESH_MODEL_OP_2(0x83, 0x04)
16 #define BT_MESH_BLOB_OP_CHUNK BT_MESH_MODEL_OP_1(0x66)
17 #define BT_MESH_BLOB_OP_BLOCK_STATUS BT_MESH_MODEL_OP_1(0x67)
18 #define BT_MESH_BLOB_OP_BLOCK_REPORT BT_MESH_MODEL_OP_1(0x68)
19 #define BT_MESH_BLOB_OP_INFO_GET BT_MESH_MODEL_OP_2(0x83, 0x06)
20 #define BT_MESH_BLOB_OP_INFO_STATUS BT_MESH_MODEL_OP_2(0x83, 0x07)
21 
22 #define BLOB_BLOCK_NOT_SET 0xffff
23 
24 #define BLOB_CHUNK_SDU_OVERHEAD                                                \
25 	(BT_MESH_MODEL_OP_LEN(BT_MESH_BLOB_OP_CHUNK) + 2 + BT_MESH_MIC_SHORT)
26 
27 #define BLOB_CHUNK_SIZE_MAX(sdu_max) ((sdu_max) - BLOB_CHUNK_SDU_OVERHEAD)
28 #define BLOB_CHUNK_SDU_LEN(chunk_size) (BLOB_CHUNK_SDU_OVERHEAD + (chunk_size))
29 
30 #if CONFIG_BT_MESH_ALIGN_CHUNK_SIZE_TO_MAX_SEGMENT ||                                              \
31 	CONFIG_BT_MESH_RX_BLOB_CHUNK_SIZE > BLOB_CHUNK_SIZE_MAX(BT_MESH_RX_SDU_MAX)
32 #define BLOB_RX_CHUNK_SIZE BLOB_CHUNK_SIZE_MAX(BT_MESH_RX_SDU_MAX)
33 #else
34 #define BLOB_RX_CHUNK_SIZE CONFIG_BT_MESH_RX_BLOB_CHUNK_SIZE
35 #endif
36 
37 #if CONFIG_BT_MESH_ALIGN_CHUNK_SIZE_TO_MAX_SEGMENT ||                                              \
38 	CONFIG_BT_MESH_TX_BLOB_CHUNK_SIZE > BLOB_CHUNK_SIZE_MAX(BT_MESH_TX_SDU_MAX)
39 #define BLOB_TX_CHUNK_SIZE BLOB_CHUNK_SIZE_MAX(BT_MESH_TX_SDU_MAX)
40 #else
41 #define BLOB_TX_CHUNK_SIZE CONFIG_BT_MESH_TX_BLOB_CHUNK_SIZE
42 #endif
43 
44 /* Utility macros for calculating log2 of a number at compile time.
45  * Used to determine the log2 representation of the block size, which
46  * is configured as a raw number, but encoded as log2.
47  *
48  * The macros expand to a series of ternary expressions, effectively
49  * searching through power of twos until a match is found.
50  * According to MshMBTv1.0, the block size cannot be larger than 2^20,
51  * so we'll stop the search at 20.
52  */
53 #define _BLOB_LOG_2_CEIL(l, x) ((x) <= (1U << l)) ? l :
54 #define _BLOB_LOG_2_FLOOR(l, x) ((x) < (1U << (l + 1))) ? l :
55 
56 #define BLOB_BLOCK_SIZE_LOG_CEIL(x) (LISTIFY(20, _BLOB_LOG_2_CEIL, (), x) 20)
57 #define BLOB_BLOCK_SIZE_LOG_FLOOR(x) (LISTIFY(20, _BLOB_LOG_2_FLOOR, (), x) 20)
58 
59 /* Log2 representation of the minimum block size */
60 #define BLOB_BLOCK_SIZE_LOG_MIN BLOB_BLOCK_SIZE_LOG_CEIL(CONFIG_BT_MESH_BLOB_BLOCK_SIZE_MIN)
61 /* Log2 representation of the maximum block size */
62 #define BLOB_BLOCK_SIZE_LOG_MAX BLOB_BLOCK_SIZE_LOG_FLOOR(CONFIG_BT_MESH_BLOB_BLOCK_SIZE_MAX)
63 
64 #if defined(CONFIG_BT_MESH_BLOB_SRV)
65 #define BLOB_BLOCK_REPORT_STATUS_MSG_MAXLEN ( \
66 					MAX(sizeof(((struct bt_mesh_blob_block *)0)->missing), \
67 					    CONFIG_BT_MESH_BLOB_SRV_PULL_REQ_COUNT * 3))
68 #define BLOB_BLOCK_STATUS_MSG_MAXLEN (5 + \
69 				      MAX(sizeof(((struct bt_mesh_blob_block *)0)->missing), \
70 					  CONFIG_BT_MESH_BLOB_SRV_PULL_REQ_COUNT * 3))
71 #else
72 #define BLOB_BLOCK_REPORT_STATUS_MSG_MAXLEN sizeof(((struct bt_mesh_blob_srv *)0)->block.missing)
73 #define BLOB_BLOCK_STATUS_MSG_MAXLEN (5 + sizeof(((struct bt_mesh_blob_srv *)0)->block.missing))
74 #endif
75 
76 #define BLOB_XFER_STATUS_MSG_MAXLEN (17 + sizeof(((struct bt_mesh_blob_srv *)0)->state.blocks))
77 
78 enum bt_mesh_blob_chunks_missing {
79 	BT_MESH_BLOB_CHUNKS_MISSING_ALL,
80 	BT_MESH_BLOB_CHUNKS_MISSING_NONE,
81 	BT_MESH_BLOB_CHUNKS_MISSING_SOME,
82 	BT_MESH_BLOB_CHUNKS_MISSING_ENCODED,
83 };
84 
blob_block_size(size_t xfer_size,uint8_t block_size_log,uint32_t idx)85 static inline size_t blob_block_size(size_t xfer_size, uint8_t block_size_log,
86 				     uint32_t idx)
87 {
88 	if (((idx + 1U) << block_size_log) <= xfer_size) {
89 		return (1U << block_size_log);
90 	}
91 
92 	return xfer_size & BIT_MASK(block_size_log);
93 }
94 
blob_chunk_missing_set(uint8_t * missing_chunks,int idx,bool missing)95 static inline void blob_chunk_missing_set(uint8_t *missing_chunks,
96 					  int idx, bool missing)
97 {
98 	WRITE_BIT(missing_chunks[idx / 8], idx % 8, missing);
99 }
100 
101 static inline bool
blob_chunk_missing_get(const uint8_t * missing_chunks,int idx)102 blob_chunk_missing_get(const uint8_t *missing_chunks, int idx)
103 {
104 	return !!(missing_chunks[idx / 8] & BIT(idx % 8));
105 }
106 
blob_chunk_missing_set_all(struct bt_mesh_blob_block * block)107 static inline void blob_chunk_missing_set_all(struct bt_mesh_blob_block *block)
108 {
109 	size_t bytes = block->chunk_count / 8;
110 
111 	memset(block->missing, 0xff, bytes);
112 	if (block->chunk_count % 8) {
113 		block->missing[bytes] = BIT_MASK(block->chunk_count % 8);
114 	}
115 }
116 
blob_chunk_missing_set_none(struct bt_mesh_blob_block * block)117 static inline void blob_chunk_missing_set_none(struct bt_mesh_blob_block *block)
118 {
119 	memset(block->missing, 0, sizeof(block->missing));
120 }
121 
122 /** @brief Perform a message broadcast to all BLOB Transfer Client Target nodes.
123  *
124  *  Will send to a group or each Target node individually, repeating until
125  *  all Target nodes have responded or the retry time has run out.
126  *
127  *  @param cli BLOB Transfer Client instance
128  *  @param ctx Broadcast context
129  */
130 void blob_cli_broadcast(struct bt_mesh_blob_cli *cli,
131 			const struct blob_cli_broadcast_ctx *ctx);
132 
133 /** @brief Register that a Target node responded to a broadcast.
134  *
135  *  @param cli    BLOB Transfer Client instance
136  *  @param target Target node that responded.
137  */
138 void blob_cli_broadcast_rsp(struct bt_mesh_blob_cli *cli,
139 			    struct bt_mesh_blob_target *target);
140 
141 /** @brief Notify the BLOB Transfer Client that the requested transmission is complete.
142  *
143  *  Should be called once for each call to the @ref blob_cli_broadcast_ctx.send
144  *  callback.
145  *
146  *  @param cli BLOB Transfer Client instance.
147  */
148 void blob_cli_broadcast_tx_complete(struct bt_mesh_blob_cli *cli);
149 
150 /** @brief Aborts any ongoing BLOB Transfer Client operations.
151  *
152  * @param cli BLOB Transfer Client instance.
153  */
154 void blob_cli_broadcast_abort(struct bt_mesh_blob_cli *cli);
155