xref: /btstack/src/mesh/mesh_network.h (revision 2f6af885c0155311b66a331b5f3076e8033cd9a5)
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 "btstack_run_loop.h"
43 
44 #include "mesh/provisioning.h"
45 #include "mesh/mesh_keys.h"
46 
47 #if defined __cplusplus
48 extern "C" {
49 #endif
50 
51 #define MESH_DEVICE_KEY_INDEX     0xffff
52 
53 #define MESH_NETWORK_PAYLOAD_MAX      29
54 #define MESH_ACCESS_PAYLOAD_MAX      384
55 
56 #define MESH_ADDRESS_UNSASSIGNED     0x0000u
57 #define MESH_ADDRESS_ALL_PROXIES     0xFFFCu
58 #define MESH_ADDRESS_ALL_FRIENDS     0xFFFDu
59 #define MESH_ADDRESS_ALL_RELAYS      0xFFFEu
60 #define MESH_ADDRESS_ALL_NODES       0xFFFFu
61 
62 typedef enum {
63     MESH_NETWORK_PDU_RECEIVED,
64     MESH_NETWORK_PDU_SENT,
65     MESH_NETWORK_CAN_SEND_NOW,
66 } mesh_network_callback_type_t;
67 
68 typedef enum {
69     MESH_PDU_TYPE_NETWORK = 0,
70     MESH_PDU_TYPE_TRANSPORT,
71     MESH_PDU_TYPE_MESSAGE,
72 } mesh_pdu_type_t;
73 
74 typedef struct mesh_pdu {
75     // allow for linked lists
76     btstack_linked_item_t item;
77     // type
78     mesh_pdu_type_t pdu_type;
79 
80 } mesh_pdu_t;
81 
82 //
83 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1
84 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER         2
85 #define MESH_NETWORK_PDU_FLAGS_RELAY               4
86 
87 typedef struct mesh_network_pdu {
88     mesh_pdu_t pdu_header;
89 
90     // callback
91     void (*callback)(struct mesh_network_pdu * network_pdu);
92 
93     // meta data network layer
94     uint16_t              netkey_index;
95     // meta data transport layer
96     uint16_t              appkey_index;
97     // MESH_NETWORK_PDU_FLAGS
98     uint16_t              flags;
99 
100     // pdu
101     uint16_t              len;
102     uint8_t               data[MESH_NETWORK_PAYLOAD_MAX];
103 } mesh_network_pdu_t;
104 
105 #define MESH_TRANSPORT_FLAG_SEQ_RESERVED    1
106 
107 typedef struct {
108     mesh_pdu_t pdu_header;
109 
110     // access acknowledged message
111     uint16_t retransmit_count;
112     uint32_t retransmit_timeout_ms;
113     uint32_t ack_opcode;
114 
115     // rx/tx: acknowledgement timer / segment transmission timer
116     btstack_timer_source_t acknowledgement_timer;
117     // rx: incomplete timer / tx: resend timer
118     btstack_timer_source_t incomplete_timer;
119     // block access
120     uint32_t              block_ack;
121     // meta data network layer
122     uint16_t              netkey_index;
123     // meta data transport layer
124     uint16_t              appkey_index;
125     // transmic size
126     uint8_t               transmic_len;
127     // akf - aid for access, opcode for control
128     uint8_t               akf_aid_control;
129     // network pdu header
130     uint8_t               network_header[9];
131     // MESH_TRANSPORT_FLAG
132     uint16_t              flags;
133     // acknowledgement timer active
134     uint8_t               acknowledgement_timer_active;
135     // incomplete timer active
136     uint8_t               incomplete_timer_active;
137     // message complete
138     uint8_t               message_complete;
139     // seq_zero for segmented messages
140     uint16_t              seq_zero;
141     // pdu
142     uint16_t              len;
143     uint8_t               data[MESH_ACCESS_PAYLOAD_MAX];
144 } mesh_transport_pdu_t;
145 
146 typedef struct {
147     mesh_pdu_t pdu_header;
148 
149     // access acknowledged message
150     uint16_t retransmit_count;
151     uint32_t retransmit_timeout_ms;
152     uint32_t ack_opcode;
153 
154     // rx/tx: acknowledgement timer / segment transmission timer
155     btstack_timer_source_t acknowledgement_timer;
156     // rx: incomplete timer / tx: resend timer
157     btstack_timer_source_t incomplete_timer;
158     // block access
159     uint32_t              block_ack;
160     // meta data network layer
161     uint16_t              netkey_index;
162     // meta data transport layer
163     uint16_t              appkey_index;
164     // transmic size
165     uint8_t               transmic_len;
166     // akf - aid for access, opcode for control
167     uint8_t               akf_aid_control;
168     // network pdu header
169     uint8_t               network_header[9];
170     // MESH_TRANSPORT_FLAG
171     uint16_t              flags;
172     // acknowledgement timer active
173     uint8_t               acknowledgement_timer_active;
174     // incomplete timer active
175     uint8_t               incomplete_timer_active;
176     // message complete
177     uint8_t               message_complete;
178     // seq_zero for segmented messages
179     uint16_t              seq_zero;
180     // pdu segments
181     uint16_t              len;
182     btstack_linked_list_t segments;
183 } mesh_message_pdu_t;
184 
185 typedef enum {
186     MESH_KEY_REFRESH_NOT_ACTIVE = 0,
187     MESH_KEY_REFRESH_FIRST_PHASE,
188     MESH_KEY_REFRESH_SECOND_PHASE
189 } mesh_key_refresh_state_t;
190 
191 typedef enum {
192     MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE,
193     MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE,
194     MESH_SECURE_NETWORK_BEACON_AUTH_VALUE,
195     MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV,
196     MESH_SECURE_NETWORK_BEACON_ADV_SENT,
197     MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT,
198     MESH_SECURE_NETWORK_BEACON_GATT_SENT,
199     MESH_SECURE_NETWORK_BEACON_W4_INTERVAL
200 } mesh_secure_network_beacon_state_t;
201 
202 typedef struct {
203     btstack_linked_item_t item;
204 
205     // netkey index
206     uint16_t              netkey_index;
207 
208     // current / old key
209     mesh_network_key_t * old_key;
210 
211     // new key (only set during key refresh)
212     mesh_network_key_t * new_key;
213 
214     // key refresh state
215     mesh_key_refresh_state_t key_refresh;
216 
217     // advertisement using node id active
218     uint8_t node_id_advertisement_running;
219 
220 
221     // advertisement using network id (used by proxy)
222     adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id;
223 
224     // advertising using node id (used by proxy)
225     adv_bearer_connectable_advertisement_data_item_t advertisement_with_node_id;
226 
227     // secure network beacons
228     mesh_secure_network_beacon_state_t beacon_state;
229     uint32_t                           beacon_interval_ms;
230     uint32_t                           beacon_observation_start_ms;
231     uint16_t                           beacon_observation_counter;
232 
233 } mesh_subnet_t;
234 
235 typedef struct {
236     btstack_linked_list_iterator_t it;
237 } mesh_subnet_iterator_t;
238 
239 /**
240  * @brief Init Mesh Network Layer
241  */
242 void mesh_network_init(void);
243 
244 /**
245  * @brief Set higher layer Network PDU handler
246  * @param packet_handler
247  */
248 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
249 
250 /**
251  * @brief Set higher layer Proxy PDU handler
252  * @param packet_handler
253  */
254 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
255 
256 /**
257  * @brief Mark packet as processed
258  * @param newtork_pdu received via call packet_handler
259  */
260 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu);
261 
262 /**
263  * @brief Send network_pdu after encryption
264  * @param network_pdu
265  */
266 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu);
267 
268 /*
269  * @brief Setup network pdu header
270  * @param netkey_index
271  * @param nid
272  * @param ctl
273  * @param ttl
274  * @param seq
275  * @param dst
276  * @param transport_pdu_data
277  * @param transport_pdu_len
278  */
279 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);
280 
281 /**
282  * Setup network pdu header without modifying len or payload
283  * @param network_pdu
284  * @param netkey_index
285  * @param nid
286  * @param ctl
287  * @param ttl
288  * @param seq
289  * @param src
290  * @param dest
291  */
292 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);
293 
294 /**
295  * @brief Validate network addresses
296  * @param ctl
297  * @param src
298  * @param dst
299  * @returns 1 if valid,
300  */
301 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst);
302 
303 /**
304  * @brief Check if Unicast address
305  * @param addr
306  * @returns 1 if unicast
307  */
308 int mesh_network_address_unicast(uint16_t addr);
309 
310 /**
311  * @brief Check if Unicast address
312  * @param addr
313  * @returns 1 if unicast
314  */
315 int mesh_network_address_group(uint16_t addr);
316 
317 /**
318  * @brief Check if All Proxies address
319  * @param addr
320  * @returns 1 if all proxies
321  */
322 int mesh_network_address_all_proxies(uint16_t addr);
323 
324 /**
325  * @brief Check if All Nodes address
326  * @param addr
327  * @returns 1 if all nodes
328  */
329 int mesh_network_address_all_nodes(uint16_t addr);
330 
331 /**
332  * @brief Check if All Friends address
333  * @param addr
334  * @returns 1 if all friends
335  */
336 int mesh_network_address_all_friends(uint16_t addr);
337 
338 /**
339  * @brief Check if All Relays address
340  * @param addr
341  * @returns 1 if all relays
342  */
343 int mesh_network_address_all_relays(uint16_t addr);
344 
345 
346 /**
347  * @brief Check if Virtual address
348  * @param addr
349  * @returns 1 if virtual
350  */
351 int mesh_network_address_virtual(uint16_t addr);
352 
353 
354 /**
355  * @brief Add subnet to list
356  * @param subnet
357  */
358 void mesh_subnet_add(mesh_subnet_t * subnet);
359 
360 /**
361  * @brief Remove subnet from list
362  * @param subnet
363  */
364 void mesh_subnet_remove(mesh_subnet_t * subnet);
365 
366 /**
367  * @brief Get subnet for netkey_index
368  * @param netkey_index
369  * @returns mesh_subnet_t or NULL
370  */
371 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index);
372 
373 /**
374  * @brief Get number of stored subnets
375  * @returns count
376  */
377 int mesh_subnet_list_count(void);
378 
379 /**
380  * @brief Iterate over all subnets
381  * @param it
382  */
383 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it);
384 
385 /**
386  * @brief Check if another subnet is available
387  * @param it
388  * @return
389  */
390 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it);
391 
392 /**
393  * @brief Get next subnet
394  * @param it
395  * @return
396  */
397 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it);
398 
399 /**
400  * @brief Setup subnet for given netkey index
401  */
402 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index);
403 
404 
405 /**
406  * @brief Get outgoing network key for subnet based on key refresh phase
407  */
408 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet);
409 
410 // buffer pool
411 mesh_network_pdu_t * mesh_network_pdu_get(void);
412 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
413 
414 // Mesh Network PDU Getter
415 uint16_t  mesh_network_control(mesh_network_pdu_t * network_pdu);
416 uint8_t   mesh_network_nid(mesh_network_pdu_t * network_pdu);
417 uint8_t   mesh_network_ttl(mesh_network_pdu_t * network_pdu);
418 uint32_t  mesh_network_seq(mesh_network_pdu_t * network_pdu);
419 uint16_t  mesh_network_src(mesh_network_pdu_t * network_pdu);
420 uint16_t  mesh_network_dst(mesh_network_pdu_t * network_pdu);
421 int       mesh_network_segmented(mesh_network_pdu_t * network_pdu);
422 uint8_t   mesh_network_control_opcode(mesh_network_pdu_t * network_pdu);
423 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu);
424 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu);
425 
426 // Mesh Network PDU Setter
427 void mesh_network_pdu_set_seq(mesh_network_pdu_t * network_pdu, uint32_t seq);
428 
429 // Testing only
430 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags);
431 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len);
432 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback));
433 void mesh_network_dump(void);
434 void mesh_network_reset(void);
435 
436 #if defined __cplusplus
437 }
438 #endif
439 
440 #endif
441