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 enum { 135 MESH_KEY_REFRESH_NOT_ACTIVE = 0, 136 MESH_KEY_REFRESH_FIRST_PHASE, 137 MESH_KEY_REFRESH_SECOND_PHASE 138 } mesh_key_refresh_state_t; 139 140 typedef enum { 141 MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE, 142 MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE, 143 MESH_SECURE_NETWORK_BEACON_AUTH_VALUE, 144 MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV, 145 MESH_SECURE_NETWORK_BEACON_ADV_SENT, 146 MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT, 147 MESH_SECURE_NETWORK_BEACON_GATT_SENT, 148 MESH_SECURE_NETWORK_BEACON_W4_INTERVAL 149 } mesh_secure_network_beacon_state_t; 150 151 typedef struct { 152 btstack_linked_item_t item; 153 154 // netkey index 155 uint16_t netkey_index; 156 157 // current / old key 158 mesh_network_key_t * old_key; 159 160 // new key (only set during key refresh) 161 mesh_network_key_t * new_key; 162 163 // key refresh state 164 mesh_key_refresh_state_t key_refresh; 165 166 // advertisement using node id active 167 uint8_t node_id_advertisement_running; 168 169 // advertisement using network id (used by proxy) 170 adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id; 171 172 // secure network beacons 173 mesh_secure_network_beacon_state_t beacon_state; 174 uint32_t beacon_interval_ms; 175 uint32_t beacon_observation_start_ms; 176 uint16_t beacon_observation_counter; 177 178 } mesh_subnet_t; 179 180 typedef struct { 181 btstack_linked_list_iterator_t it; 182 } mesh_subnet_iterator_t; 183 184 /** 185 * @brief Init Mesh Network Layer 186 */ 187 void mesh_network_init(void); 188 189 /** 190 * @brief Set higher layer Network PDU handler 191 * @param packet_handler 192 */ 193 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 194 195 /** 196 * @brief Set higher layer Proxy PDU handler 197 * @param packet_handler 198 */ 199 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 200 201 /** 202 * @brief Mark packet as processed 203 * @param newtork_pdu received via call packet_handler 204 */ 205 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu); 206 207 /** 208 * @brief Configure address filter 209 */ 210 void mesh_network_set_primary_element_address(uint16_t addr); 211 212 /** 213 * @brief Send network_pdu after encryption 214 * @param network_pdu 215 */ 216 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu); 217 218 /* 219 * @brief Setup network pdu header 220 * @param netkey_index 221 * @param nid 222 * @param ctl 223 * @param ttl 224 * @param seq 225 * @param dst 226 * @param transport_pdu_data 227 * @param transport_pdu_len 228 */ 229 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); 230 231 /** 232 * Setup network pdu header without modifying len or payload 233 * @param network_pdu 234 * @param netkey_index 235 * @param nid 236 * @param ctl 237 * @param ttl 238 * @param seq 239 * @param src 240 * @param dest 241 */ 242 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); 243 244 /** 245 * @brief Validate network addresses 246 * @param ctl 247 * @param src 248 * @param dst 249 * @returns 1 if valid, 250 */ 251 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst); 252 253 /** 254 * @brief Check if Unicast address 255 * @param addr 256 * @returns 1 if unicast 257 */ 258 int mesh_network_address_unicast(uint16_t addr); 259 260 /** 261 * @brief Check if Unicast address 262 * @param addr 263 * @returns 1 if unicast 264 */ 265 int mesh_network_address_group(uint16_t addr); 266 267 /** 268 * @brief Check if All Proxies address 269 * @param addr 270 * @returns 1 if all proxies 271 */ 272 int mesh_network_address_all_proxies(uint16_t addr); 273 274 /** 275 * @brief Check if All Nodes address 276 * @param addr 277 * @returns 1 if all nodes 278 */ 279 int mesh_network_address_all_nodes(uint16_t addr); 280 281 /** 282 * @brief Check if All Friends address 283 * @param addr 284 * @returns 1 if all friends 285 */ 286 int mesh_network_address_all_friends(uint16_t addr); 287 288 /** 289 * @brief Check if All Relays address 290 * @param addr 291 * @returns 1 if all relays 292 */ 293 int mesh_network_address_all_relays(uint16_t addr); 294 295 296 /** 297 * @brief Check if Virtual address 298 * @param addr 299 * @returns 1 if virtual 300 */ 301 int mesh_network_address_virtual(uint16_t addr); 302 303 304 /** 305 * @brief Add subnet to list 306 * @param subnet 307 */ 308 void mesh_subnet_add(mesh_subnet_t * subnet); 309 310 /** 311 * @brief Remove subnet from list 312 * @param subnet 313 */ 314 void mesh_subnet_remove(mesh_subnet_t * subnet); 315 316 /** 317 * @brief Get subnet for netkey_index 318 * @param netkey_index 319 * @returns mesh_subnet_t or NULL 320 */ 321 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index); 322 323 /** 324 * @brief Get number of stored subnets 325 * @returns count 326 */ 327 int mesh_subnet_list_count(void); 328 329 /** 330 * @brief Iterate over all subnets 331 * @param it 332 */ 333 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it); 334 335 /** 336 * @brief Check if another subnet is available 337 * @param it 338 * @return 339 */ 340 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it); 341 342 /** 343 * @brief Get next subnet 344 * @param it 345 * @return 346 */ 347 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it); 348 349 /** 350 * @brief Setup subnet for given netkey index 351 */ 352 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index); 353 354 355 /** 356 * @brief Get outgoing network key for subnet based on key refresh phase 357 */ 358 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet); 359 360 // buffer pool 361 mesh_network_pdu_t * mesh_network_pdu_get(void); 362 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu); 363 364 // Mesh Network PDU Getter 365 uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu); 366 uint8_t mesh_network_nid(mesh_network_pdu_t * network_pdu); 367 uint8_t mesh_network_ttl(mesh_network_pdu_t * network_pdu); 368 uint32_t mesh_network_seq(mesh_network_pdu_t * network_pdu); 369 uint16_t mesh_network_src(mesh_network_pdu_t * network_pdu); 370 uint16_t mesh_network_dst(mesh_network_pdu_t * network_pdu); 371 int mesh_network_segmented(mesh_network_pdu_t * network_pdu); 372 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu); 373 uint8_t mesh_network_pdu_len(mesh_network_pdu_t * network_pdu); 374 375 void mesh_set_iv_index(uint32_t iv_index); 376 uint32_t mesh_get_iv_index(void); 377 378 int mesh_iv_update_active(void); 379 void mesh_trigger_iv_update(void); 380 381 // Testing only 382 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags); 383 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len); 384 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback)); 385 void mesh_network_dump(void); 386 void mesh_network_reset(void); 387 388 #if defined __cplusplus 389 } 390 #endif 391 392 #endif 393