xref: /btstack/src/mesh/mesh_network.h (revision 01122b73eef352aeca7441c8b29c15365f0d0177)
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 
45 #if defined __cplusplus
46 extern "C" {
47 #endif
48 
49 #define MESH_DEVICE_KEY_INDEX     0xffff
50 
51 #define MESH_NETWORK_PAYLOAD_MAX      29
52 #define MESH_ACCESS_PAYLOAD_MAX      384
53 
54 #define MESH_ADDRESS_UNSASSIGNED     0x0000u
55 #define MESH_ADDRESS_ALL_PROXIES     0xFFFCu
56 #define MESH_ADDRESS_ALL_FRIENDS     0xFFFDu
57 #define MESH_ADDRESS_ALL_RELAYS      0xFFFEu
58 #define MESH_ADDRESS_ALL_NODES       0xFFFFu
59 
60 typedef enum {
61     MESH_NETWORK_PDU_RECEIVED,
62     MESH_NETWORK_PDU_SENT,
63     MESH_NETWORK_CAN_SEND_NOW,
64 } mesh_network_callback_type_t;
65 
66 typedef enum {
67     MESH_PDU_TYPE_NETWORK = 0,
68     MESH_PDU_TYPE_TRANSPORT,
69 } mesh_pdu_type_t;
70 
71 typedef struct mesh_pdu {
72     // allow for linked lists
73     btstack_linked_item_t item;
74     // type
75     mesh_pdu_type_t pdu_type;
76 } mesh_pdu_t;
77 
78 //
79 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1
80 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER         2
81 #define MESH_NETWORK_PDU_FLAGS_RELAY               4
82 
83 typedef struct mesh_network_pdu {
84     mesh_pdu_t pdu_header;
85 
86     // callback
87     void (*callback)(struct mesh_network_pdu * network_pdu);
88 
89     // meta data network layer
90     uint16_t              netkey_index;
91     // meta data transport layer
92     uint16_t              appkey_index;
93     // flags: bit 0 indicates Proxy PDU
94     uint16_t              flags;
95 
96     // pdu
97     uint16_t              len;
98     uint8_t               data[MESH_NETWORK_PAYLOAD_MAX];
99 } mesh_network_pdu_t;
100 
101 typedef struct {
102     mesh_pdu_t pdu_header;
103 
104     // rx/tx: acknowledgement timer / segment transmission timer
105     btstack_timer_source_t acknowledgement_timer;
106     // rx: incomplete timer / tx: resend timer
107     btstack_timer_source_t incomplete_timer;
108     // block access
109     uint32_t              block_ack;
110     // meta data network layer
111     uint16_t              netkey_index;
112     // meta data transport layer
113     uint16_t              appkey_index;
114     // transmic size
115     uint8_t               transmic_len;
116     // akf - aid
117     uint8_t               akf_aid;
118     // network pdu header
119     uint8_t               network_header[9];
120     // acknowledgement timer active
121     uint8_t               acknowledgement_timer_active;
122     // incomplete timer active
123     uint8_t               incomplete_timer_active;
124     // message complete
125     uint8_t               message_complete;
126     // seq_zero for segmented messages
127     uint16_t              seq_zero;
128     // pdu
129     uint16_t              len;
130     uint8_t               data[MESH_ACCESS_PAYLOAD_MAX];
131 } mesh_transport_pdu_t;
132 
133 typedef struct {
134     btstack_linked_item_t item;
135 
136     // current / old key
137     mesh_network_key_t * old_key;
138 
139     // new key (only set during key refresh)
140     mesh_network_key_t * new_key;
141 
142     // key refresh state
143     mesh_key_refresh_state_t key_refresh;
144 
145     // advertisement using node id active
146     uint8_t node_id_advertisement_running;
147 
148     // advertisement using network id (used by proxy)
149     adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id;
150 
151     // secure network beacons
152     mesh_secure_network_beacon_state_t beacon_state;
153     uint32_t                           beacon_interval_ms;
154     uint32_t                           beacon_observation_start_ms;
155     uint16_t                           beacon_observation_counter;
156 
157 } mesh_subnet_t;
158 
159 /**
160  * @brief Init Mesh Network Layer
161  */
162 void mesh_network_init(void);
163 
164 /**
165  * @brief Set higher layer Network PDU handler
166  * @param packet_handler
167  */
168 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
169 
170 /**
171  * @brief Set higher layer Proxy PDU handler
172  * @param packet_handler
173  */
174 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
175 
176 /**
177  * @brief Mark packet as processed
178  * @param newtork_pdu received via call packet_handler
179  */
180 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu);
181 
182 /**
183  * @brief Configure address filter
184  */
185 void mesh_network_set_primary_element_address(uint16_t addr);
186 
187 /**
188  * @brief Send network_pdu after encryption
189  * @param network_pdu
190  */
191 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu);
192 
193 /*
194  * @brief Setup network pdu header
195  * @param netkey_index
196  * @param nid
197  * @param ctl
198  * @param ttl
199  * @param seq
200  * @param dst
201  * @param transport_pdu_data
202  * @param transport_pdu_len
203  */
204 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);
205 
206 /**
207  * Setup network pdu header without modifying len or payload
208  * @param network_pdu
209  * @param netkey_index
210  * @param nid
211  * @param ctl
212  * @param ttl
213  * @param seq
214  * @param src
215  * @param dest
216  */
217 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);
218 
219 /**
220  * @brief Validate network addresses
221  * @param ctl
222  * @param src
223  * @param dst
224  * @returns 1 if valid,
225  */
226 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst);
227 
228 /**
229  * @brief Check if Unicast address
230  * @param addr
231  * @returns 1 if unicast
232  */
233 int mesh_network_address_unicast(uint16_t addr);
234 
235 /**
236  * @brief Check if Unicast address
237  * @param addr
238  * @returns 1 if unicast
239  */
240 int mesh_network_address_group(uint16_t addr);
241 
242 /**
243  * @brief Check if All Proxies address
244  * @param addr
245  * @returns 1 if all proxies
246  */
247 int mesh_network_address_all_proxies(uint16_t addr);
248 
249 /**
250  * @brief Check if All Nodes address
251  * @param addr
252  * @returns 1 if all nodes
253  */
254 int mesh_network_address_all_nodes(uint16_t addr);
255 
256 /**
257  * @brief Check if All Friends address
258  * @param addr
259  * @returns 1 if all friends
260  */
261 int mesh_network_address_all_friends(uint16_t addr);
262 
263 /**
264  * @brief Check if All Relays address
265  * @param addr
266  * @returns 1 if all relays
267  */
268 int mesh_network_address_all_relays(uint16_t addr);
269 
270 
271 /**
272  * @brief Check if Virtual address
273  * @param addr
274  * @returns 1 if virtual
275  */
276 int mesh_network_address_virtual(uint16_t addr);
277 
278 // buffer pool
279 mesh_network_pdu_t * mesh_network_pdu_get(void);
280 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
281 
282 // Mesh Network PDU Getter
283 uint16_t  mesh_network_control(mesh_network_pdu_t * network_pdu);
284 uint8_t   mesh_network_nid(mesh_network_pdu_t * network_pdu);
285 uint8_t   mesh_network_ttl(mesh_network_pdu_t * network_pdu);
286 uint32_t  mesh_network_seq(mesh_network_pdu_t * network_pdu);
287 uint16_t  mesh_network_src(mesh_network_pdu_t * network_pdu);
288 uint16_t  mesh_network_dst(mesh_network_pdu_t * network_pdu);
289 int       mesh_network_segmented(mesh_network_pdu_t * network_pdu);
290 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu);
291 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu);
292 
293 void     mesh_set_iv_index(uint32_t iv_index);
294 uint32_t mesh_get_iv_index(void);
295 
296 // Testing only
297 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags);
298 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len);
299 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback));
300 void mesh_network_dump(void);
301 void mesh_network_reset(void);
302 
303 #if defined __cplusplus
304 }
305 #endif
306 
307 #endif
308