xref: /btstack/src/mesh/mesh_network.h (revision de7d11c030262e8a919c187ba7bf11e93a266bb8)
1 /*
2  * Copyright (C) 2018 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #ifndef __MESH_NETWORK
39 #define __MESH_NETWORK
40 
41 #include "btstack_linked_list.h"
42 #include "provisioning.h"
43 #include "btstack_run_loop.h"
44 #include "mesh_keys.h"
45 
46 #if defined __cplusplus
47 extern "C" {
48 #endif
49 
50 #define MESH_DEVICE_KEY_INDEX     0xffff
51 
52 #define MESH_NETWORK_PAYLOAD_MAX      29
53 #define MESH_ACCESS_PAYLOAD_MAX      384
54 
55 #define MESH_ADDRESS_UNSASSIGNED     0x0000u
56 #define MESH_ADDRESS_ALL_PROXIES     0xFFFCu
57 #define MESH_ADDRESS_ALL_FRIENDS     0xFFFDu
58 #define MESH_ADDRESS_ALL_RELAYS      0xFFFEu
59 #define MESH_ADDRESS_ALL_NODES       0xFFFFu
60 
61 typedef enum {
62     MESH_NETWORK_PDU_RECEIVED,
63     MESH_NETWORK_PDU_SENT,
64     MESH_NETWORK_CAN_SEND_NOW,
65 } mesh_network_callback_type_t;
66 
67 typedef enum {
68     MESH_PDU_TYPE_NETWORK = 0,
69     MESH_PDU_TYPE_TRANSPORT,
70 } mesh_pdu_type_t;
71 
72 typedef struct mesh_pdu {
73     // allow for linked lists
74     btstack_linked_item_t item;
75     // type
76     mesh_pdu_type_t pdu_type;
77 } mesh_pdu_t;
78 
79 //
80 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1
81 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER         2
82 #define MESH_NETWORK_PDU_FLAGS_RELAY               4
83 
84 typedef struct mesh_network_pdu {
85     mesh_pdu_t pdu_header;
86 
87     // callback
88     void (*callback)(struct mesh_network_pdu * network_pdu);
89 
90     // meta data network layer
91     uint16_t              netkey_index;
92     // meta data transport layer
93     uint16_t              appkey_index;
94     // flags: bit 0 indicates Proxy PDU
95     uint16_t              flags;
96 
97     // pdu
98     uint16_t              len;
99     uint8_t               data[MESH_NETWORK_PAYLOAD_MAX];
100 } mesh_network_pdu_t;
101 
102 typedef struct {
103     mesh_pdu_t pdu_header;
104 
105     // rx/tx: acknowledgement timer / segment transmission timer
106     btstack_timer_source_t acknowledgement_timer;
107     // rx: incomplete timer / tx: resend timer
108     btstack_timer_source_t incomplete_timer;
109     // block access
110     uint32_t              block_ack;
111     // meta data network layer
112     uint16_t              netkey_index;
113     // meta data transport layer
114     uint16_t              appkey_index;
115     // transmic size
116     uint8_t               transmic_len;
117     // akf - aid
118     uint8_t               akf_aid;
119     // network pdu header
120     uint8_t               network_header[9];
121     // acknowledgement timer active
122     uint8_t               acknowledgement_timer_active;
123     // incomplete timer active
124     uint8_t               incomplete_timer_active;
125     // message complete
126     uint8_t               message_complete;
127     // seq_zero for segmented messages
128     uint16_t              seq_zero;
129     // pdu
130     uint16_t              len;
131     uint8_t               data[MESH_ACCESS_PAYLOAD_MAX];
132 } mesh_transport_pdu_t;
133 
134 typedef struct {
135     btstack_linked_item_t item;
136 
137     // netkey index
138     uint16_t              netkey_index;
139 
140     // current / old key
141     mesh_network_key_t * old_key;
142 
143     // new key (only set during key refresh)
144     mesh_network_key_t * new_key;
145 
146     // key refresh state
147     mesh_key_refresh_state_t key_refresh;
148 
149     // advertisement using node id active
150     uint8_t node_id_advertisement_running;
151 
152     // advertisement using network id (used by proxy)
153     adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id;
154 
155     // secure network beacons
156     mesh_secure_network_beacon_state_t beacon_state;
157     uint32_t                           beacon_interval_ms;
158     uint32_t                           beacon_observation_start_ms;
159     uint16_t                           beacon_observation_counter;
160 
161 } mesh_subnet_t;
162 
163 typedef struct {
164     btstack_linked_list_iterator_t it;
165 } mesh_subnet_iterator_t;
166 
167 /**
168  * @brief Init Mesh Network Layer
169  */
170 void mesh_network_init(void);
171 
172 /**
173  * @brief Set higher layer Network PDU handler
174  * @param packet_handler
175  */
176 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
177 
178 /**
179  * @brief Set higher layer Proxy PDU handler
180  * @param packet_handler
181  */
182 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
183 
184 /**
185  * @brief Mark packet as processed
186  * @param newtork_pdu received via call packet_handler
187  */
188 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu);
189 
190 /**
191  * @brief Configure address filter
192  */
193 void mesh_network_set_primary_element_address(uint16_t addr);
194 
195 /**
196  * @brief Send network_pdu after encryption
197  * @param network_pdu
198  */
199 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu);
200 
201 /*
202  * @brief Setup network pdu header
203  * @param netkey_index
204  * @param nid
205  * @param ctl
206  * @param ttl
207  * @param seq
208  * @param dst
209  * @param transport_pdu_data
210  * @param transport_pdu_len
211  */
212 void mesh_network_setup_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dst, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len);
213 
214 /**
215  * Setup network pdu header without modifying len or payload
216  * @param network_pdu
217  * @param netkey_index
218  * @param nid
219  * @param ctl
220  * @param ttl
221  * @param seq
222  * @param src
223  * @param dest
224  */
225 void mesh_network_setup_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dest);
226 
227 /**
228  * @brief Validate network addresses
229  * @param ctl
230  * @param src
231  * @param dst
232  * @returns 1 if valid,
233  */
234 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst);
235 
236 /**
237  * @brief Check if Unicast address
238  * @param addr
239  * @returns 1 if unicast
240  */
241 int mesh_network_address_unicast(uint16_t addr);
242 
243 /**
244  * @brief Check if Unicast address
245  * @param addr
246  * @returns 1 if unicast
247  */
248 int mesh_network_address_group(uint16_t addr);
249 
250 /**
251  * @brief Check if All Proxies address
252  * @param addr
253  * @returns 1 if all proxies
254  */
255 int mesh_network_address_all_proxies(uint16_t addr);
256 
257 /**
258  * @brief Check if All Nodes address
259  * @param addr
260  * @returns 1 if all nodes
261  */
262 int mesh_network_address_all_nodes(uint16_t addr);
263 
264 /**
265  * @brief Check if All Friends address
266  * @param addr
267  * @returns 1 if all friends
268  */
269 int mesh_network_address_all_friends(uint16_t addr);
270 
271 /**
272  * @brief Check if All Relays address
273  * @param addr
274  * @returns 1 if all relays
275  */
276 int mesh_network_address_all_relays(uint16_t addr);
277 
278 
279 /**
280  * @brief Check if Virtual address
281  * @param addr
282  * @returns 1 if virtual
283  */
284 int mesh_network_address_virtual(uint16_t addr);
285 
286 
287 /**
288  * @brief Add subnet to list
289  * @param subnet
290  */
291 void mesh_subnet_add(mesh_subnet_t * subnet);
292 
293 /**
294  * @brief Remove subnet from list
295  * @param subnet
296  */
297 void mesh_subnet_remove(mesh_subnet_t * subnet);
298 
299 /**
300  * @brief Get subnet for netkey_index
301  * @param netkey_index
302  * @returns mesh_subnet_t or NULL
303  */
304 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index);
305 
306 /**
307  * @brief Get number of stored subnets
308  * @returns count
309  */
310 int mesh_subnet_list_count(void);
311 
312 /**
313  * @brief Iterate over all subnets
314  * @param it
315  */
316 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it);
317 
318 /**
319  * @brief Check if another subnet is available
320  * @param it
321  * @return
322  */
323 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it);
324 
325 /**
326  * @brief Get next subnet
327  * @param it
328  * @return
329  */
330 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it);
331 
332 
333 
334 // buffer pool
335 mesh_network_pdu_t * mesh_network_pdu_get(void);
336 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
337 
338 // Mesh Network PDU Getter
339 uint16_t  mesh_network_control(mesh_network_pdu_t * network_pdu);
340 uint8_t   mesh_network_nid(mesh_network_pdu_t * network_pdu);
341 uint8_t   mesh_network_ttl(mesh_network_pdu_t * network_pdu);
342 uint32_t  mesh_network_seq(mesh_network_pdu_t * network_pdu);
343 uint16_t  mesh_network_src(mesh_network_pdu_t * network_pdu);
344 uint16_t  mesh_network_dst(mesh_network_pdu_t * network_pdu);
345 int       mesh_network_segmented(mesh_network_pdu_t * network_pdu);
346 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu);
347 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu);
348 
349 void     mesh_set_iv_index(uint32_t iv_index);
350 uint32_t mesh_get_iv_index(void);
351 
352 // Testing only
353 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags);
354 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len);
355 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback));
356 void mesh_network_dump(void);
357 void mesh_network_reset(void);
358 
359 #if defined __cplusplus
360 }
361 #endif
362 
363 #endif
364