1 /* 2 * Copyright (C) 2019 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 #define __BTSTACK_FILE__ "mesh.c" 39 40 #include <string.h> 41 #include <stdio.h> 42 43 #include "mesh/mesh.h" 44 45 #include "btstack_util.h" 46 #include "btstack_config.h" 47 #include "btstack_event.h" 48 #include "btstack_tlv.h" 49 #include "btstack_memory.h" 50 #include "btstack_debug.h" 51 52 #include "mesh/adv_bearer.h" 53 #include "mesh/beacon.h" 54 #include "mesh/gatt_bearer.h" 55 #include "mesh/mesh_access.h" 56 #include "mesh/mesh_configuration_server.h" 57 #include "mesh/mesh_foundation.h" 58 #include "mesh/mesh_generic_model.h" 59 #include "mesh/mesh_generic_server.h" 60 #include "mesh/mesh_iv_index_seq_number.h" 61 #include "mesh/mesh_lower_transport.h" 62 #include "mesh/mesh_peer.h" 63 #include "mesh/mesh_proxy.h" 64 #include "mesh/mesh_upper_transport.h" 65 #include "mesh/mesh_virtual_addresses.h" 66 #include "mesh/pb_adv.h" 67 #include "mesh/pb_gatt.h" 68 #include "mesh/provisioning.h" 69 #include "mesh/provisioning_device.h" 70 71 static void mesh_node_store_provisioning_data(mesh_provisioning_data_t * provisioning_data); 72 static int mesh_node_startup_from_tlv(void); 73 74 // Persistent storage structures 75 76 typedef struct { 77 uint16_t hash; 78 uint8_t label_uuid[16]; 79 } mesh_persistent_virtual_address_t; 80 81 typedef struct { 82 uint16_t netkey_index; 83 84 uint8_t version; 85 86 // net_key from provisioner or Config Model Client 87 uint8_t net_key[16]; 88 89 // derived data 90 91 // k1 92 uint8_t identity_key[16]; 93 uint8_t beacon_key[16]; 94 95 // k3 96 uint8_t network_id[8]; 97 98 // k2 99 uint8_t nid; 100 uint8_t encryption_key[16]; 101 uint8_t privacy_key[16]; 102 } mesh_persistent_net_key_t; 103 104 typedef struct { 105 uint16_t netkey_index; 106 uint16_t appkey_index; 107 uint8_t aid; 108 uint8_t version; 109 uint8_t key[16]; 110 } mesh_persistent_app_key_t; 111 112 typedef struct { 113 uint8_t gatt_proxy; 114 uint8_t beacon; 115 uint8_t default_ttl; 116 uint8_t network_transmit; 117 uint8_t relay; 118 uint8_t relay_retransmit; 119 uint8_t friend; 120 } mesh_persistent_foundation_t; 121 122 typedef struct { 123 uint32_t iv_index; 124 uint32_t seq_number; 125 } iv_index_and_sequence_number_t; 126 127 static btstack_packet_handler_t provisioning_device_packet_handler; 128 static btstack_packet_callback_registration_t hci_event_callback_registration; 129 static int provisioned; 130 131 // Mandatory Confiuration Server 132 static mesh_model_t mesh_configuration_server_model; 133 134 // Mandatory Health Server 135 static mesh_model_t mesh_health_server_model; 136 static mesh_configuration_server_model_context_t mesh_configuration_server_model_context; 137 138 // Random UUID on start 139 static btstack_crypto_random_t mesh_access_crypto_random; 140 static uint8_t random_device_uuid[16]; 141 142 // TLV 143 static const btstack_tlv_t * btstack_tlv_singleton_impl; 144 static void * btstack_tlv_singleton_context; 145 146 // IV Index persistence 147 static uint32_t sequence_number_last_stored; 148 static uint32_t sequence_number_storage_trigger; 149 150 151 static void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){ 152 153 // set iv_index and iv index update active 154 int iv_index_update_active = (provisioning_data->flags & 2) >> 1; 155 mesh_iv_index_recovered(iv_index_update_active, provisioning_data->iv_index); 156 157 // set unicast address 158 mesh_node_primary_element_address_set(provisioning_data->unicast_address); 159 160 // set device_key 161 mesh_transport_set_device_key(provisioning_data->device_key); 162 163 if (provisioning_data->network_key){ 164 165 // setup primary network with provisioned netkey 166 mesh_network_key_add(provisioning_data->network_key); 167 168 // setup primary network 169 mesh_subnet_setup_for_netkey_index(provisioning_data->network_key->netkey_index); 170 171 // start sending Secure Network Beacons 172 mesh_subnet_t * provisioned_subnet = mesh_subnet_get_by_netkey_index(provisioning_data->network_key->netkey_index); 173 beacon_secure_network_start(provisioned_subnet); 174 } 175 176 // Mesh Proxy 177 #ifdef ENABLE_MESH_PROXY_SERVER 178 // Setup Proxy 179 mesh_proxy_init(provisioning_data->unicast_address); 180 mesh_proxy_start_advertising_with_network_id(); 181 #endif 182 } 183 184 static void mesh_access_setup_unprovisioned_device(void * arg){ 185 // set random value 186 if (arg == NULL){ 187 mesh_node_set_device_uuid(random_device_uuid); 188 } 189 190 #ifdef ENABLE_MESH_PB_ADV 191 // PB-ADV 192 beacon_unprovisioned_device_start(mesh_node_get_device_uuid(), 0); 193 #endif 194 #ifdef ENABLE_MESH_PB_GATT 195 mesh_proxy_start_advertising_unprovisioned_device(); 196 #endif 197 } 198 199 static void mesh_access_setup_without_provisiong_data(void){ 200 const uint8_t * device_uuid = mesh_node_get_device_uuid(); 201 if (device_uuid){ 202 mesh_access_setup_unprovisioned_device((void *)device_uuid); 203 } else{ 204 btstack_crypto_random_generate(&mesh_access_crypto_random, random_device_uuid, 16, &mesh_access_setup_unprovisioned_device, NULL); 205 } 206 } 207 208 static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 209 mesh_provisioning_data_t provisioning_data; 210 211 switch(packet[0]){ 212 case HCI_EVENT_MESH_META: 213 switch(packet[2]){ 214 case MESH_SUBEVENT_PB_PROV_COMPLETE: 215 // get provisioning data 216 provisioning_device_data_get(&provisioning_data); 217 218 // and store in TLV 219 mesh_node_store_provisioning_data(&provisioning_data); 220 221 // setup node after provisioned 222 mesh_access_setup_from_provisioning_data(&provisioning_data); 223 224 // start advertising with node id after provisioning 225 mesh_proxy_set_advertising_with_node_id(provisioning_data.network_key->netkey_index, MESH_NODE_IDENTITY_STATE_ADVERTISING_RUNNING); 226 227 provisioned = 1; 228 break; 229 default: 230 break; 231 } 232 break; 233 default: 234 break; 235 } 236 if (provisioning_device_packet_handler == NULL) return; 237 238 // forward 239 (*provisioning_device_packet_handler)(packet_type, channel, packet, size); 240 } 241 242 static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 243 UNUSED(channel); 244 UNUSED(size); 245 246 switch (packet_type) { 247 case HCI_EVENT_PACKET: 248 switch (hci_event_packet_get_type(packet)) { 249 case BTSTACK_EVENT_STATE: 250 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 251 // get TLV instance 252 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 253 254 // startup fromstatic provisioning data stored in TLV 255 provisioned = mesh_node_startup_from_tlv(); 256 break; 257 258 case HCI_EVENT_DISCONNECTION_COMPLETE: 259 // enable PB_GATT 260 if (provisioned == 0){ 261 mesh_proxy_start_advertising_unprovisioned_device(); 262 } else { 263 #ifdef ENABLE_MESH_PROXY_SERVER 264 mesh_proxy_start_advertising_with_network_id(); 265 #endif 266 } 267 break; 268 269 case HCI_EVENT_LE_META: 270 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 271 // disable PB_GATT 272 mesh_proxy_stop_advertising_unprovisioned_device(); 273 break; 274 default: 275 break; 276 } 277 break; 278 } 279 } 280 281 // Foundation state 282 static const uint32_t mesh_foundation_state_tag = ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'N' << 8) | ((uint32_t) 'D' << 8); 283 284 void mesh_foundation_state_load(void){ 285 mesh_persistent_foundation_t data; 286 287 int foundation_state_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, mesh_foundation_state_tag, (uint8_t *) &data, sizeof(data)); 288 if (foundation_state_len != sizeof(data)) return; 289 290 mesh_foundation_gatt_proxy_set(data.gatt_proxy); 291 mesh_foundation_beacon_set(data.gatt_proxy); 292 mesh_foundation_default_ttl_set(data.default_ttl); 293 mesh_foundation_friend_set(data.friend); 294 mesh_foundation_network_transmit_set(data.network_transmit); 295 mesh_foundation_relay_set(data.relay); 296 mesh_foundation_relay_retransmit_set(data.relay_retransmit); 297 } 298 299 void mesh_foundation_state_store(void){ 300 mesh_persistent_foundation_t data; 301 data.gatt_proxy = mesh_foundation_gatt_proxy_get(); 302 data.gatt_proxy = mesh_foundation_beacon_get(); 303 data.default_ttl = mesh_foundation_default_ttl_get(); 304 data.friend = mesh_foundation_friend_get(); 305 data.network_transmit = mesh_foundation_network_transmit_get(); 306 data.relay = mesh_foundation_relay_get(); 307 data.relay_retransmit = mesh_foundation_relay_retransmit_get(); 308 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, mesh_foundation_state_tag, (uint8_t *) &data, sizeof(data)); 309 } 310 311 // Mesh Virtual Address Management 312 static uint32_t mesh_virtual_address_tag_for_pseudo_dst(uint16_t pseudo_dst){ 313 return ((uint32_t) 'M' << 24) | ((uint32_t) 'V' << 16) | ((uint32_t) pseudo_dst); 314 } 315 316 static void mesh_store_virtual_address(uint16_t pseudo_dest, uint16_t hash, const uint8_t * label_uuid){ 317 mesh_persistent_virtual_address_t data; 318 uint32_t tag = mesh_virtual_address_tag_for_pseudo_dst(pseudo_dest); 319 data.hash = hash; 320 memcpy(data.label_uuid, label_uuid, 16); 321 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 322 } 323 324 static void mesh_delete_virtual_address(uint16_t pseudo_dest){ 325 uint32_t tag = mesh_virtual_address_tag_for_pseudo_dst(pseudo_dest); 326 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 327 } 328 329 void mesh_load_virtual_addresses(void){ 330 uint16_t pseudo_dst; 331 for (pseudo_dst = 0x8000; pseudo_dst < (0x8000 + MAX_NR_MESH_VIRTUAL_ADDRESSES); pseudo_dst++){ 332 mesh_virtual_address_tag_for_pseudo_dst(pseudo_dst); 333 mesh_persistent_virtual_address_t data; 334 uint32_t tag = mesh_virtual_address_tag_for_pseudo_dst(pseudo_dst); 335 int virtual_address_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 336 if (virtual_address_len == 0) return; 337 338 mesh_virtual_address_t * virtual_address = btstack_memory_mesh_virtual_address_get(); 339 if (virtual_address == NULL) return; 340 341 virtual_address->pseudo_dst = pseudo_dst; 342 virtual_address->hash = data.hash; 343 memcpy(virtual_address->label_uuid, data.label_uuid, 16); 344 mesh_virtual_address_add(virtual_address); 345 } 346 } 347 348 void mesh_delete_virtual_addresses(void){ 349 uint16_t pseudo_dest; 350 for (pseudo_dest = 0x8000; pseudo_dest < (0x8000 + MAX_NR_MESH_VIRTUAL_ADDRESSES); pseudo_dest++){ 351 mesh_delete_virtual_address(pseudo_dest); 352 } 353 } 354 355 void mesh_virtual_address_decrease_refcount(mesh_virtual_address_t * virtual_address){ 356 if (virtual_address == NULL){ 357 log_error("virtual_address == NULL"); 358 } 359 // decrease refcount 360 virtual_address->ref_count--; 361 // Free virtual address if ref count reaches zero 362 if (virtual_address->ref_count > 0) return; 363 // delete from TLV 364 mesh_delete_virtual_address(virtual_address->pseudo_dst); 365 // remove from list 366 mesh_virtual_address_remove(virtual_address); 367 // free memory 368 btstack_memory_mesh_virtual_address_free(virtual_address); 369 } 370 371 void mesh_virtual_address_increase_refcount(mesh_virtual_address_t * virtual_address){ 372 if (virtual_address == NULL){ 373 log_error("virtual_address == NULL"); 374 } 375 virtual_address->ref_count++; 376 if (virtual_address->ref_count > 1) return; 377 // store in TLV 378 mesh_store_virtual_address(virtual_address->pseudo_dst, virtual_address->hash, virtual_address->label_uuid); 379 } 380 381 // Mesh Subscriptions 382 static uint32_t mesh_model_subscription_tag_for_index(uint16_t internal_model_id){ 383 return ((uint32_t) 'M' << 24) | ((uint32_t) 'S' << 16) | ((uint32_t) internal_model_id); 384 } 385 386 static void mesh_model_load_subscriptions(mesh_model_t * mesh_model){ 387 uint32_t tag = mesh_model_subscription_tag_for_index(mesh_model->mid); 388 btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &mesh_model->subscriptions, sizeof(mesh_model->subscriptions)); 389 // update ref count 390 391 // increase ref counts for virtual subscriptions 392 uint16_t i; 393 for (i = 0; i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL ; i++){ 394 uint16_t src = mesh_model->subscriptions[i]; 395 if (mesh_network_address_virtual(src)){ 396 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(src); 397 mesh_virtual_address_increase_refcount(virtual_address); 398 } 399 } 400 } 401 402 void mesh_model_store_subscriptions(mesh_model_t * model){ 403 uint32_t tag = mesh_model_subscription_tag_for_index(model->mid); 404 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->subscriptions, sizeof(model->subscriptions)); 405 } 406 407 static void mesh_model_delete_subscriptions(mesh_model_t * model){ 408 uint32_t tag = mesh_model_subscription_tag_for_index(model->mid); 409 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 410 } 411 412 void mesh_load_subscriptions(void){ 413 printf("Load Model Subscription Lists\n"); 414 // iterate over elements and models 415 mesh_element_iterator_t element_it; 416 mesh_element_iterator_init(&element_it); 417 while (mesh_element_iterator_has_next(&element_it)){ 418 mesh_element_t * element = mesh_element_iterator_next(&element_it); 419 mesh_model_iterator_t model_it; 420 mesh_model_iterator_init(&model_it, element); 421 while (mesh_model_iterator_has_next(&model_it)){ 422 mesh_model_t * model = mesh_model_iterator_next(&model_it); 423 mesh_model_load_subscriptions(model); 424 } 425 } 426 } 427 428 void mesh_delete_subscriptions(void){ 429 printf("Delete Model Subscription Lists\n"); 430 // iterate over elements and models 431 mesh_element_iterator_t element_it; 432 mesh_element_iterator_init(&element_it); 433 while (mesh_element_iterator_has_next(&element_it)){ 434 mesh_element_t * element = mesh_element_iterator_next(&element_it); 435 mesh_model_iterator_t model_it; 436 mesh_model_iterator_init(&model_it, element); 437 while (mesh_model_iterator_has_next(&model_it)){ 438 mesh_model_t * model = mesh_model_iterator_next(&model_it); 439 mesh_model_delete_subscriptions(model); 440 } 441 } 442 } 443 444 // Model Publication 445 446 static uint32_t mesh_model_publication_tag_for_index(uint16_t internal_model_id){ 447 return ((uint32_t) 'M' << 24) | ((uint32_t) 'P' << 16) | ((uint32_t) internal_model_id); 448 } 449 450 static void mesh_model_load_publication(mesh_model_t * mesh_model){ 451 if (mesh_model->publication_model == NULL) return; 452 453 uint32_t tag = mesh_model_publication_tag_for_index(mesh_model->mid); 454 btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &mesh_model->publication_model, sizeof(mesh_publication_model_t)); 455 456 // increase ref counts for current virtual publicataion address 457 uint16_t src = mesh_model->publication_model->address; 458 if (mesh_network_address_virtual(src)){ 459 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(src); 460 mesh_virtual_address_increase_refcount(virtual_address); 461 } 462 463 mesh_model_publication_start(mesh_model); 464 } 465 466 void mesh_model_store_publication(mesh_model_t * mesh_model){ 467 if (mesh_model->publication_model == NULL) return; 468 uint32_t tag = mesh_model_publication_tag_for_index(mesh_model->mid); 469 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &mesh_model->subscriptions, sizeof(mesh_publication_model_t)); 470 } 471 472 static void mesh_model_delete_publication(mesh_model_t * mesh_model){ 473 if (mesh_model->publication_model == NULL) return; 474 uint32_t tag = mesh_model_publication_tag_for_index(mesh_model->mid); 475 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 476 } 477 478 void mesh_load_publications(void){ 479 printf("Load Model Publications\n"); 480 // iterate over elements and models 481 mesh_element_iterator_t element_it; 482 mesh_element_iterator_init(&element_it); 483 while (mesh_element_iterator_has_next(&element_it)){ 484 mesh_element_t * element = mesh_element_iterator_next(&element_it); 485 mesh_model_iterator_t model_it; 486 mesh_model_iterator_init(&model_it, element); 487 while (mesh_model_iterator_has_next(&model_it)){ 488 mesh_model_t * model = mesh_model_iterator_next(&model_it); 489 mesh_model_load_publication(model); 490 } 491 } 492 } 493 494 void mesh_delete_publications(void){ 495 printf("Delete Model Publications\n"); 496 // iterate over elements and models 497 mesh_element_iterator_t element_it; 498 mesh_element_iterator_init(&element_it); 499 while (mesh_element_iterator_has_next(&element_it)){ 500 mesh_element_t * element = mesh_element_iterator_next(&element_it); 501 mesh_model_iterator_t model_it; 502 mesh_model_iterator_init(&model_it, element); 503 while (mesh_model_iterator_has_next(&model_it)){ 504 mesh_model_t * model = mesh_model_iterator_next(&model_it); 505 mesh_model_delete_publication(model); 506 } 507 } 508 } 509 510 // Mesh Network Keys 511 static uint32_t mesh_network_key_tag_for_internal_index(uint16_t internal_index){ 512 return ((uint32_t) 'M' << 24) | ((uint32_t) 'N' << 16) | ((uint32_t) internal_index); 513 } 514 515 void mesh_store_network_key(mesh_network_key_t * network_key){ 516 mesh_persistent_net_key_t data; 517 printf("Store NetKey: internal index 0x%x, NetKey Index 0x%06x, NID %02x: ", network_key->internal_index, network_key->netkey_index, network_key->nid); 518 printf_hexdump(network_key->net_key, 16); 519 uint32_t tag = mesh_network_key_tag_for_internal_index(network_key->internal_index); 520 data.netkey_index = network_key->netkey_index; 521 memcpy(data.net_key, network_key->net_key, 16); 522 memcpy(data.identity_key, network_key->identity_key, 16); 523 memcpy(data.beacon_key, network_key->beacon_key, 16); 524 memcpy(data.network_id, network_key->network_id, 8); 525 data.nid = network_key->nid; 526 data.version = network_key->version; 527 memcpy(data.encryption_key, network_key->encryption_key, 16); 528 memcpy(data.privacy_key, network_key->privacy_key, 16); 529 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(mesh_persistent_net_key_t)); 530 } 531 532 void mesh_delete_network_key(uint16_t internal_index){ 533 uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index); 534 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 535 } 536 537 538 void mesh_load_network_keys(void){ 539 printf("Load Network Keys\n"); 540 uint16_t internal_index; 541 for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){ 542 mesh_persistent_net_key_t data; 543 uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index); 544 int netkey_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 545 if (netkey_len != sizeof(mesh_persistent_net_key_t)) continue; 546 547 mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get(); 548 if (network_key == NULL) return; 549 550 network_key->netkey_index = data.netkey_index; 551 memcpy(network_key->net_key, data.net_key, 16); 552 memcpy(network_key->identity_key, data.identity_key, 16); 553 memcpy(network_key->beacon_key, data.beacon_key, 16); 554 memcpy(network_key->network_id, data.network_id, 8); 555 network_key->nid = data.nid; 556 network_key->version = data.version; 557 memcpy(network_key->encryption_key, data.encryption_key, 16); 558 memcpy(network_key->privacy_key, data.privacy_key, 16); 559 560 #ifdef ENABLE_GATT_BEARER 561 // setup advertisement with network id 562 network_key->advertisement_with_network_id.adv_length = mesh_proxy_setup_advertising_with_network_id(network_key->advertisement_with_network_id.adv_data, network_key->network_id); 563 #endif 564 565 mesh_network_key_add(network_key); 566 567 mesh_subnet_setup_for_netkey_index(network_key->netkey_index); 568 569 printf("- internal index 0x%x, NetKey Index 0x%06x, NID %02x: ", network_key->internal_index, network_key->netkey_index, network_key->nid); 570 printf_hexdump(network_key->net_key, 16); 571 } 572 } 573 574 void mesh_delete_network_keys(void){ 575 printf("Delete Network Keys\n"); 576 577 uint16_t internal_index; 578 for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){ 579 mesh_delete_network_key(internal_index); 580 } 581 } 582 583 // Mesh App Keys 584 585 static uint32_t mesh_transport_key_tag_for_internal_index(uint16_t internal_index){ 586 return ((uint32_t) 'M' << 24) | ((uint32_t) 'A' << 16) | ((uint32_t) internal_index); 587 } 588 589 void mesh_store_app_key(mesh_transport_key_t * app_key){ 590 mesh_persistent_app_key_t data; 591 printf("Store AppKey: internal index 0x%x, AppKey Index 0x%06x, AID %02x: ", app_key->internal_index, app_key->appkey_index, app_key->aid); 592 printf_hexdump(app_key->key, 16); 593 uint32_t tag = mesh_transport_key_tag_for_internal_index(app_key->internal_index); 594 data.netkey_index = app_key->netkey_index; 595 data.appkey_index = app_key->appkey_index; 596 data.aid = app_key->aid; 597 data.version = app_key->version; 598 memcpy(data.key, app_key->key, 16); 599 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 600 } 601 602 void mesh_delete_app_key(uint16_t internal_index){ 603 uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index); 604 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 605 } 606 607 void mesh_load_app_keys(void){ 608 printf("Load App Keys\n"); 609 uint16_t internal_index; 610 for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){ 611 mesh_persistent_app_key_t data; 612 uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index); 613 int app_key_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 614 if (app_key_len == 0) continue; 615 616 mesh_transport_key_t * key = btstack_memory_mesh_transport_key_get(); 617 if (key == NULL) return; 618 619 key->internal_index = internal_index; 620 key->appkey_index = data.appkey_index; 621 key->netkey_index = data.netkey_index; 622 key->aid = data.aid; 623 key->akf = 1; 624 key->version = data.version; 625 memcpy(key->key, data.key, 16); 626 mesh_transport_key_add(key); 627 printf("- internal index 0x%x, AppKey Index 0x%06x, AID %02x: ", key->internal_index, key->appkey_index, key->aid); 628 printf_hexdump(key->key, 16); 629 } 630 } 631 632 void mesh_delete_app_keys(void){ 633 printf("Delete App Keys\n"); 634 635 uint16_t internal_index; 636 for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){ 637 mesh_delete_app_key(internal_index); 638 } 639 } 640 641 642 // Model to Appkey List 643 644 static uint32_t mesh_model_tag_for_index(uint16_t internal_model_id){ 645 return ((uint32_t) 'M' << 24) | ((uint32_t) 'B' << 16) | ((uint32_t) internal_model_id); 646 } 647 648 static void mesh_load_appkey_list(mesh_model_t * model){ 649 uint32_t tag = mesh_model_tag_for_index(model->mid); 650 btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->appkey_indices, sizeof(model->appkey_indices)); 651 } 652 653 static void mesh_store_appkey_list(mesh_model_t * model){ 654 uint32_t tag = mesh_model_tag_for_index(model->mid); 655 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->appkey_indices, sizeof(model->appkey_indices)); 656 } 657 658 static void mesh_delete_appkey_list(mesh_model_t * model){ 659 uint32_t tag = mesh_model_tag_for_index(model->mid); 660 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 661 } 662 663 void mesh_load_appkey_lists(void){ 664 printf("Load Appkey Lists\n"); 665 // iterate over elements and models 666 mesh_element_iterator_t element_it; 667 mesh_element_iterator_init(&element_it); 668 while (mesh_element_iterator_has_next(&element_it)){ 669 mesh_element_t * element = mesh_element_iterator_next(&element_it); 670 mesh_model_iterator_t model_it; 671 mesh_model_iterator_init(&model_it, element); 672 while (mesh_model_iterator_has_next(&model_it)){ 673 mesh_model_t * model = mesh_model_iterator_next(&model_it); 674 mesh_load_appkey_list(model); 675 } 676 } 677 } 678 679 void mesh_delete_appkey_lists(void){ 680 printf("Delete Appkey Lists\n"); 681 // iterate over elements and models 682 mesh_element_iterator_t element_it; 683 mesh_element_iterator_init(&element_it); 684 while (mesh_element_iterator_has_next(&element_it)){ 685 mesh_element_t * element = mesh_element_iterator_next(&element_it); 686 mesh_model_iterator_t model_it; 687 mesh_model_iterator_init(&model_it, element); 688 while (mesh_model_iterator_has_next(&model_it)){ 689 mesh_model_t * model = mesh_model_iterator_next(&model_it); 690 mesh_delete_appkey_list(model); 691 } 692 } 693 } 694 695 uint8_t mesh_model_bind_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 696 uint16_t i; 697 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 698 if (mesh_model->appkey_indices[i] == appkey_index) return MESH_FOUNDATION_STATUS_SUCCESS; 699 } 700 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 701 if (mesh_model->appkey_indices[i] == MESH_APPKEY_INVALID) { 702 mesh_model->appkey_indices[i] = appkey_index; 703 mesh_store_appkey_list(mesh_model); 704 return MESH_FOUNDATION_STATUS_SUCCESS; 705 } 706 } 707 return MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 708 } 709 710 void mesh_model_unbind_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 711 uint16_t i; 712 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 713 if (mesh_model->appkey_indices[i] == appkey_index) { 714 mesh_model->appkey_indices[i] = MESH_APPKEY_INVALID; 715 mesh_store_appkey_list(mesh_model); 716 } 717 } 718 } 719 720 int mesh_model_contains_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 721 uint16_t i; 722 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 723 if (mesh_model->appkey_indices[i] == appkey_index) return 1; 724 } 725 return 0; 726 } 727 728 void mesh_access_netkey_finalize(mesh_network_key_t * network_key){ 729 mesh_network_key_remove(network_key); 730 mesh_delete_network_key(network_key->internal_index); 731 btstack_memory_mesh_network_key_free(network_key); 732 } 733 734 void mesh_access_appkey_finalize(mesh_transport_key_t * transport_key){ 735 mesh_transport_key_remove(transport_key); 736 mesh_delete_app_key(transport_key->appkey_index); 737 btstack_memory_mesh_transport_key_free(transport_key); 738 } 739 740 void mesh_access_key_refresh_revoke_keys(mesh_subnet_t * subnet){ 741 // delete old netkey index 742 mesh_access_netkey_finalize(subnet->old_key); 743 subnet->old_key = subnet->new_key; 744 subnet->new_key = NULL; 745 746 // delete old appkeys, if any 747 mesh_transport_key_iterator_t it; 748 mesh_transport_key_iterator_init(&it, subnet->netkey_index); 749 while (mesh_transport_key_iterator_has_more(&it)){ 750 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 751 if (transport_key->old_key == 0) continue; 752 mesh_access_appkey_finalize(transport_key); 753 } 754 } 755 756 // Mesh IV Index 757 static uint32_t mesh_tag_for_iv_index_and_seq_number(void){ 758 return ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'I' << 9) | ((uint32_t) 'S'); 759 } 760 761 void mesh_store_iv_index_after_provisioning(uint32_t iv_index){ 762 iv_index_and_sequence_number_t data; 763 uint32_t tag = mesh_tag_for_iv_index_and_seq_number(); 764 data.iv_index = iv_index; 765 data.seq_number = 0; 766 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 767 768 sequence_number_last_stored = data.seq_number; 769 sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL; 770 } 771 772 void mesh_store_iv_index_and_sequence_number(void){ 773 iv_index_and_sequence_number_t data; 774 uint32_t tag = mesh_tag_for_iv_index_and_seq_number(); 775 data.iv_index = mesh_get_iv_index(); 776 data.seq_number = mesh_sequence_number_peek(); 777 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 778 779 sequence_number_last_stored = data.seq_number; 780 sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL; 781 } 782 783 int mesh_load_iv_index_and_sequence_number(uint32_t * iv_index, uint32_t * sequence_number){ 784 iv_index_and_sequence_number_t data; 785 uint32_t tag = mesh_tag_for_iv_index_and_seq_number(); 786 uint32_t len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 787 if (len == sizeof(iv_index_and_sequence_number_t)){ 788 *iv_index = data.iv_index; 789 *sequence_number = data.seq_number; 790 return 1; 791 } 792 return 0; 793 } 794 795 // higher layer 796 static void mesh_persist_iv_index_and_sequence_number(void){ 797 if (mesh_sequence_number_peek() >= sequence_number_storage_trigger){ 798 mesh_store_iv_index_and_sequence_number(); 799 } 800 } 801 802 803 static uint32_t mesh_tag_for_prov_data(void){ 804 return ((uint32_t) 'P' << 24) | ((uint32_t) 'R' << 16) | ((uint32_t) 'O' << 8) | (uint32_t)'V'; 805 } 806 807 void mesh_node_reset(void){ 808 // PROV 809 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, mesh_tag_for_prov_data()); 810 // everything else 811 mesh_delete_network_keys(); 812 mesh_delete_app_keys(); 813 mesh_delete_appkey_lists(); 814 mesh_delete_virtual_addresses(); 815 mesh_delete_subscriptions(); 816 mesh_delete_publications(); 817 } 818 819 typedef struct { 820 uint16_t unicast_address; 821 uint8_t flags; 822 uint8_t device_key[16]; 823 824 } mesh_persistent_provisioning_data_t; 825 826 static void mesh_node_store_provisioning_data(mesh_provisioning_data_t * provisioning_data){ 827 828 // fill persistent prov data 829 mesh_persistent_provisioning_data_t persistent_provisioning_data; 830 831 persistent_provisioning_data.unicast_address = provisioning_data->unicast_address; 832 memcpy(persistent_provisioning_data.device_key, provisioning_data->device_key, 16); 833 834 // store in tlv 835 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 836 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, mesh_tag_for_prov_data(), (uint8_t *) &persistent_provisioning_data, sizeof(mesh_persistent_provisioning_data_t)); 837 838 // store IV Index and sequence number 839 mesh_store_iv_index_after_provisioning(provisioning_data->iv_index); 840 841 // store primary network key 842 mesh_store_network_key(provisioning_data->network_key); 843 } 844 845 static int mesh_node_startup_from_tlv(void){ 846 847 mesh_persistent_provisioning_data_t persistent_provisioning_data; 848 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 849 850 // load provisioning data 851 uint32_t prov_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, mesh_tag_for_prov_data(), (uint8_t *) &persistent_provisioning_data, sizeof(mesh_persistent_provisioning_data_t)); 852 printf("Provisioning data available: %u\n", prov_len ? 1 : 0); 853 if (prov_len){ 854 855 // copy into mesh_provisioning_data 856 mesh_provisioning_data_t provisioning_data; 857 memcpy(provisioning_data.device_key, persistent_provisioning_data.device_key, 16); 858 provisioning_data.unicast_address = persistent_provisioning_data.unicast_address; 859 provisioning_data.flags = persistent_provisioning_data.flags; 860 provisioning_data.network_key = NULL; 861 862 // load iv index 863 uint32_t iv_index; 864 uint32_t sequence_number; 865 int ok = mesh_load_iv_index_and_sequence_number(&iv_index, &sequence_number); 866 if (ok){ 867 // bump sequence number to account for interval updates 868 sequence_number += MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL; 869 mesh_sequence_number_set(sequence_number); 870 mesh_store_iv_index_and_sequence_number(); 871 log_info("IV Index: %08x, Sequence Number %08x", (int) iv_index, (int) sequence_number); 872 provisioning_data.iv_index = iv_index; 873 } 874 875 // load network keys 876 mesh_load_network_keys(); 877 // load app keys 878 mesh_load_app_keys(); 879 // load model to appkey bindings 880 mesh_load_appkey_lists(); 881 // load virtual addresses 882 mesh_load_virtual_addresses(); 883 // load model subscriptions 884 mesh_load_subscriptions(); 885 // load model publications 886 mesh_load_publications(); 887 // load foundation state 888 mesh_foundation_state_load(); 889 890 mesh_access_setup_from_provisioning_data(&provisioning_data); 891 892 #if defined(ENABLE_MESH_ADV_BEARER) || defined(ENABLE_MESH_PB_ADV) 893 // start sending Secure Network Beacon 894 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(0); 895 if (subnet){ 896 beacon_secure_network_start(subnet); 897 } 898 #endif 899 return 1; 900 } else { 901 mesh_access_setup_without_provisiong_data(); 902 return 0; 903 } 904 } 905 906 907 static void mesh_node_setup_default_models(void){ 908 // configure Config Server 909 mesh_configuration_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER); 910 mesh_configuration_server_model.model_data = &mesh_configuration_server_model_context; 911 mesh_configuration_server_model.operations = mesh_configuration_server_get_operations(); 912 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_configuration_server_model); 913 914 // Config Health Server 915 mesh_health_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_HEALTH_SERVER); 916 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_health_server_model); 917 } 918 919 void mesh_init(void){ 920 921 // register for HCI events 922 hci_event_callback_registration.callback = &hci_packet_handler; 923 hci_add_event_handler(&hci_event_callback_registration); 924 925 // ADV Bearer also used for GATT Proxy Advertisements and PB-GATT 926 adv_bearer_init(); 927 928 #ifdef ENABLE_MESH_GATT_BEARER 929 // Setup GATT bearer 930 gatt_bearer_init(); 931 #endif 932 933 #ifdef ENABLE_MESH_ADV_BEARER 934 // Setup Unprovisioned Device Beacon 935 beacon_init(); 936 #endif 937 938 provisioning_device_init(); 939 940 // Node Configuration 941 mesh_node_init(); 942 943 // Network layer 944 mesh_network_init(); 945 946 // Transport layers (lower + upper)) 947 mesh_lower_transport_init(); 948 mesh_upper_transport_init(); 949 950 // Access layer 951 mesh_access_init(); 952 953 // Add mandatory models: Config Server and Health Server 954 mesh_node_setup_default_models(); 955 956 // register for seq number updates 957 mesh_sequence_number_set_update_callback(&mesh_persist_iv_index_and_sequence_number); 958 } 959 960 /** 961 * Register for Mesh Provisioning Device events 962 * @param packet_handler 963 */ 964 void mesh_register_provisioning_device_packet_handler(btstack_packet_handler_t packet_handler){ 965 provisioning_device_packet_handler = packet_handler; 966 provisioning_device_register_packet_handler(&mesh_provisioning_message_handler); 967 } 968