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 78 // access acknowledged message 79 uint16_t retransmit_count; 80 uint32_t retransmit_timeout_ms; 81 uint32_t ack_opcode; 82 83 } mesh_pdu_t; 84 85 // 86 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1 87 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER 2 88 #define MESH_NETWORK_PDU_FLAGS_RELAY 4 89 90 typedef struct mesh_network_pdu { 91 mesh_pdu_t pdu_header; 92 93 // callback 94 void (*callback)(struct mesh_network_pdu * network_pdu); 95 96 // meta data network layer 97 uint16_t netkey_index; 98 // meta data transport layer 99 uint16_t appkey_index; 100 // flags: bit 0 indicates Proxy PDU 101 uint16_t flags; 102 103 // pdu 104 uint16_t len; 105 uint8_t data[MESH_NETWORK_PAYLOAD_MAX]; 106 } mesh_network_pdu_t; 107 108 typedef struct { 109 mesh_pdu_t pdu_header; 110 111 // rx/tx: acknowledgement timer / segment transmission timer 112 btstack_timer_source_t acknowledgement_timer; 113 // rx: incomplete timer / tx: resend timer 114 btstack_timer_source_t incomplete_timer; 115 // block access 116 uint32_t block_ack; 117 // meta data network layer 118 uint16_t netkey_index; 119 // meta data transport layer 120 uint16_t appkey_index; 121 // transmic size 122 uint8_t transmic_len; 123 // akf - aid 124 uint8_t akf_aid; 125 // network pdu header 126 uint8_t network_header[9]; 127 // acknowledgement timer active 128 uint8_t acknowledgement_timer_active; 129 // incomplete timer active 130 uint8_t incomplete_timer_active; 131 // message complete 132 uint8_t message_complete; 133 // seq_zero for segmented messages 134 uint16_t seq_zero; 135 // pdu 136 uint16_t len; 137 uint8_t data[MESH_ACCESS_PAYLOAD_MAX]; 138 } mesh_transport_pdu_t; 139 140 typedef enum { 141 MESH_KEY_REFRESH_NOT_ACTIVE = 0, 142 MESH_KEY_REFRESH_FIRST_PHASE, 143 MESH_KEY_REFRESH_SECOND_PHASE 144 } mesh_key_refresh_state_t; 145 146 typedef enum { 147 MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE, 148 MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE, 149 MESH_SECURE_NETWORK_BEACON_AUTH_VALUE, 150 MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV, 151 MESH_SECURE_NETWORK_BEACON_ADV_SENT, 152 MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT, 153 MESH_SECURE_NETWORK_BEACON_GATT_SENT, 154 MESH_SECURE_NETWORK_BEACON_W4_INTERVAL 155 } mesh_secure_network_beacon_state_t; 156 157 typedef struct { 158 btstack_linked_item_t item; 159 160 // netkey index 161 uint16_t netkey_index; 162 163 // current / old key 164 mesh_network_key_t * old_key; 165 166 // new key (only set during key refresh) 167 mesh_network_key_t * new_key; 168 169 // key refresh state 170 mesh_key_refresh_state_t key_refresh; 171 172 // advertisement using node id active 173 uint8_t node_id_advertisement_running; 174 175 // advertisement using network id (used by proxy) 176 adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id; 177 178 // secure network beacons 179 mesh_secure_network_beacon_state_t beacon_state; 180 uint32_t beacon_interval_ms; 181 uint32_t beacon_observation_start_ms; 182 uint16_t beacon_observation_counter; 183 184 } mesh_subnet_t; 185 186 typedef struct { 187 btstack_linked_list_iterator_t it; 188 } mesh_subnet_iterator_t; 189 190 /** 191 * @brief Init Mesh Network Layer 192 */ 193 void mesh_network_init(void); 194 195 /** 196 * @brief Set higher layer Network PDU handler 197 * @param packet_handler 198 */ 199 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 200 201 /** 202 * @brief Set higher layer Proxy PDU handler 203 * @param packet_handler 204 */ 205 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 206 207 /** 208 * @brief Mark packet as processed 209 * @param newtork_pdu received via call packet_handler 210 */ 211 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu); 212 213 /** 214 * @brief Configure address filter 215 */ 216 void mesh_network_set_primary_element_address(uint16_t addr); 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_pdu_data(mesh_network_pdu_t * network_pdu); 379 uint8_t mesh_network_pdu_len(mesh_network_pdu_t * network_pdu); 380 381 void mesh_set_iv_index(uint32_t iv_index); 382 uint32_t mesh_get_iv_index(void); 383 384 uint32_t mesh_get_iv_index_for_tx(void); 385 386 int mesh_iv_update_active(void); 387 void mesh_trigger_iv_update(void); 388 void mesh_iv_update_completed(void); 389 390 // Testing only 391 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags); 392 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len); 393 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback)); 394 void mesh_network_dump(void); 395 void mesh_network_reset(void); 396 397 #if defined __cplusplus 398 } 399 #endif 400 401 #endif 402