xref: /btstack/src/mesh/mesh_network.h (revision f276878605b4be50ed0b7879c5ca0a1e40c1cc30)
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 
134 /**
135  * @brief Init Mesh Network Layer
136  */
137 void mesh_network_init(void);
138 
139 /**
140  * @brief Set higher layer Network PDU handler
141  * @param packet_handler
142  */
143 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
144 
145 /**
146  * @brief Set higher layer Proxy PDU handler
147  * @param packet_handler
148  */
149 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
150 
151 /**
152  * @brief Mark packet as processed
153  * @param newtork_pdu received via call packet_handler
154  */
155 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu);
156 
157 /**
158  * @brief Configure address filter
159  */
160 void mesh_network_set_primary_element_address(uint16_t addr);
161 
162 /**
163  * @brief Send network_pdu after encryption
164  * @param network_pdu
165  */
166 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu);
167 
168 /*
169  * @brief Setup network pdu header
170  * @param netkey_index
171  * @param nid
172  * @param ctl
173  * @param ttl
174  * @param seq
175  * @param dst
176  * @param transport_pdu_data
177  * @param transport_pdu_len
178  */
179 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);
180 
181 /**
182  * Setup network pdu header without modifying len or payload
183  * @param network_pdu
184  * @param netkey_index
185  * @param nid
186  * @param ctl
187  * @param ttl
188  * @param seq
189  * @param src
190  * @param dest
191  */
192 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);
193 
194 /**
195  * @brief Validate network addresses
196  * @param ctl
197  * @param src
198  * @param dst
199  * @returns 1 if valid,
200  */
201 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst);
202 
203 /**
204  * @brief Check if Unicast address
205  * @param addr
206  * @returns 1 if unicast
207  */
208 int mesh_network_address_unicast(uint16_t addr);
209 
210 /**
211  * @brief Check if Unicast address
212  * @param addr
213  * @returns 1 if unicast
214  */
215 int mesh_network_address_group(uint16_t addr);
216 
217 /**
218  * @brief Check if All Proxies address
219  * @param addr
220  * @returns 1 if all proxies
221  */
222 int mesh_network_address_all_proxies(uint16_t addr);
223 
224 /**
225  * @brief Check if All Nodes address
226  * @param addr
227  * @returns 1 if all nodes
228  */
229 int mesh_network_address_all_nodes(uint16_t addr);
230 
231 /**
232  * @brief Check if All Friends address
233  * @param addr
234  * @returns 1 if all friends
235  */
236 int mesh_network_address_all_friends(uint16_t addr);
237 
238 /**
239  * @brief Check if All Relays address
240  * @param addr
241  * @returns 1 if all relays
242  */
243 int mesh_network_address_all_relays(uint16_t addr);
244 
245 
246 /**
247  * @brief Check if Virtual address
248  * @param addr
249  * @returns 1 if virtual
250  */
251 int mesh_network_address_virtual(uint16_t addr);
252 
253 // buffer pool
254 mesh_network_pdu_t * mesh_network_pdu_get(void);
255 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
256 
257 // Mesh Network PDU Getter
258 uint16_t  mesh_network_control(mesh_network_pdu_t * network_pdu);
259 uint8_t   mesh_network_nid(mesh_network_pdu_t * network_pdu);
260 uint8_t   mesh_network_ttl(mesh_network_pdu_t * network_pdu);
261 uint32_t  mesh_network_seq(mesh_network_pdu_t * network_pdu);
262 uint16_t  mesh_network_src(mesh_network_pdu_t * network_pdu);
263 uint16_t  mesh_network_dst(mesh_network_pdu_t * network_pdu);
264 int       mesh_network_segmented(mesh_network_pdu_t * network_pdu);
265 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu);
266 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu);
267 
268 void     mesh_set_iv_index(uint32_t iv_index);
269 uint32_t mesh_get_iv_index(void);
270 
271 // Testing only
272 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags);
273 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len);
274 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback));
275 void mesh_network_dump(void);
276 void mesh_network_reset(void);
277 
278 #if defined __cplusplus
279 }
280 #endif
281 
282 #endif
283