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