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_provisioning_message_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 185 mesh_provisioning_data_t provisioning_data; 186 187 switch(packet[0]){ 188 case HCI_EVENT_MESH_META: 189 switch(packet[2]){ 190 case MESH_SUBEVENT_PB_PROV_COMPLETE: 191 // get provisioning data 192 provisioning_device_data_get(&provisioning_data); 193 194 // and store in TLV 195 mesh_node_store_provisioning_data(&provisioning_data); 196 197 // setup node after provisioned 198 mesh_access_setup_from_provisioning_data(&provisioning_data); 199 200 // start advertising with node id after provisioning 201 mesh_proxy_set_advertising_with_node_id(provisioning_data.network_key->netkey_index, MESH_NODE_IDENTITY_STATE_ADVERTISING_RUNNING); 202 203 provisioned = 1; 204 break; 205 default: 206 break; 207 } 208 break; 209 default: 210 break; 211 } 212 if (provisioning_device_packet_handler == NULL) return; 213 214 // forward 215 (*provisioning_device_packet_handler)(packet_type, channel, packet, size); 216 } 217 218 static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 219 UNUSED(channel); 220 UNUSED(size); 221 222 switch (packet_type) { 223 case HCI_EVENT_PACKET: 224 switch (hci_event_packet_get_type(packet)) { 225 case BTSTACK_EVENT_STATE: 226 if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break; 227 // get TLV instance 228 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 229 230 // startup from static provisioning data stored in TLV 231 provisioned = mesh_node_startup_from_tlv(); 232 break; 233 234 case HCI_EVENT_DISCONNECTION_COMPLETE: 235 // enable PB_GATT 236 if (provisioned == 0){ 237 mesh_proxy_start_advertising_unprovisioned_device(); 238 } else { 239 #ifdef ENABLE_MESH_PROXY_SERVER 240 mesh_proxy_start_advertising_with_network_id(); 241 #endif 242 } 243 break; 244 245 case HCI_EVENT_LE_META: 246 if (hci_event_le_meta_get_subevent_code(packet) != HCI_SUBEVENT_LE_CONNECTION_COMPLETE) break; 247 // disable PB_GATT 248 mesh_proxy_stop_advertising_unprovisioned_device(); 249 break; 250 default: 251 break; 252 } 253 break; 254 } 255 } 256 257 // Foundation state 258 static const uint32_t mesh_foundation_state_tag = ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'N' << 8) | ((uint32_t) 'D' << 8); 259 260 void mesh_foundation_state_load(void){ 261 mesh_persistent_foundation_t data; 262 263 int foundation_state_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, mesh_foundation_state_tag, (uint8_t *) &data, sizeof(data)); 264 if (foundation_state_len != sizeof(data)) return; 265 266 mesh_foundation_gatt_proxy_set(data.gatt_proxy); 267 mesh_foundation_beacon_set(data.gatt_proxy); 268 mesh_foundation_default_ttl_set(data.default_ttl); 269 mesh_foundation_friend_set(data.friend); 270 mesh_foundation_network_transmit_set(data.network_transmit); 271 mesh_foundation_relay_set(data.relay); 272 mesh_foundation_relay_retransmit_set(data.relay_retransmit); 273 } 274 275 void mesh_foundation_state_store(void){ 276 mesh_persistent_foundation_t data; 277 data.gatt_proxy = mesh_foundation_gatt_proxy_get(); 278 data.gatt_proxy = mesh_foundation_beacon_get(); 279 data.default_ttl = mesh_foundation_default_ttl_get(); 280 data.friend = mesh_foundation_friend_get(); 281 data.network_transmit = mesh_foundation_network_transmit_get(); 282 data.relay = mesh_foundation_relay_get(); 283 data.relay_retransmit = mesh_foundation_relay_retransmit_get(); 284 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, mesh_foundation_state_tag, (uint8_t *) &data, sizeof(data)); 285 } 286 287 // Mesh Virtual Address Management 288 static uint32_t mesh_virtual_address_tag_for_pseudo_dst(uint16_t pseudo_dst){ 289 return ((uint32_t) 'M' << 24) | ((uint32_t) 'V' << 16) | ((uint32_t) pseudo_dst); 290 } 291 292 static void mesh_store_virtual_address(uint16_t pseudo_dest, uint16_t hash, const uint8_t * label_uuid){ 293 mesh_persistent_virtual_address_t data; 294 uint32_t tag = mesh_virtual_address_tag_for_pseudo_dst(pseudo_dest); 295 data.hash = hash; 296 memcpy(data.label_uuid, label_uuid, 16); 297 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 298 } 299 300 static void mesh_delete_virtual_address(uint16_t pseudo_dest){ 301 uint32_t tag = mesh_virtual_address_tag_for_pseudo_dst(pseudo_dest); 302 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 303 } 304 305 void mesh_load_virtual_addresses(void){ 306 uint16_t pseudo_dst; 307 for (pseudo_dst = 0x8000; pseudo_dst < (0x8000 + MAX_NR_MESH_VIRTUAL_ADDRESSES); pseudo_dst++){ 308 mesh_virtual_address_tag_for_pseudo_dst(pseudo_dst); 309 mesh_persistent_virtual_address_t data; 310 uint32_t tag = mesh_virtual_address_tag_for_pseudo_dst(pseudo_dst); 311 int virtual_address_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 312 if (virtual_address_len == 0) return; 313 314 mesh_virtual_address_t * virtual_address = btstack_memory_mesh_virtual_address_get(); 315 if (virtual_address == NULL) return; 316 317 virtual_address->pseudo_dst = pseudo_dst; 318 virtual_address->hash = data.hash; 319 memcpy(virtual_address->label_uuid, data.label_uuid, 16); 320 mesh_virtual_address_add(virtual_address); 321 } 322 } 323 324 void mesh_delete_virtual_addresses(void){ 325 uint16_t pseudo_dest; 326 for (pseudo_dest = 0x8000; pseudo_dest < (0x8000 + MAX_NR_MESH_VIRTUAL_ADDRESSES); pseudo_dest++){ 327 mesh_delete_virtual_address(pseudo_dest); 328 } 329 } 330 331 void mesh_virtual_address_decrease_refcount(mesh_virtual_address_t * virtual_address){ 332 if (virtual_address == NULL){ 333 log_error("virtual_address == NULL"); 334 } 335 // decrease refcount 336 virtual_address->ref_count--; 337 // Free virtual address if ref count reaches zero 338 if (virtual_address->ref_count > 0) return; 339 // delete from TLV 340 mesh_delete_virtual_address(virtual_address->pseudo_dst); 341 // remove from list 342 mesh_virtual_address_remove(virtual_address); 343 // free memory 344 btstack_memory_mesh_virtual_address_free(virtual_address); 345 } 346 347 void mesh_virtual_address_increase_refcount(mesh_virtual_address_t * virtual_address){ 348 if (virtual_address == NULL){ 349 log_error("virtual_address == NULL"); 350 } 351 virtual_address->ref_count++; 352 if (virtual_address->ref_count > 1) return; 353 // store in TLV 354 mesh_store_virtual_address(virtual_address->pseudo_dst, virtual_address->hash, virtual_address->label_uuid); 355 } 356 357 // Mesh Subscriptions 358 static uint32_t mesh_model_subscription_tag_for_index(uint16_t internal_model_id){ 359 return ((uint32_t) 'M' << 24) | ((uint32_t) 'S' << 16) | ((uint32_t) internal_model_id); 360 } 361 362 static void mesh_model_load_subscriptions(mesh_model_t * mesh_model){ 363 uint32_t tag = mesh_model_subscription_tag_for_index(mesh_model->mid); 364 btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &mesh_model->subscriptions, sizeof(mesh_model->subscriptions)); 365 // update ref count 366 367 // increase ref counts for virtual subscriptions 368 uint16_t i; 369 for (i = 0; i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL ; i++){ 370 uint16_t src = mesh_model->subscriptions[i]; 371 if (mesh_network_address_virtual(src)){ 372 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(src); 373 mesh_virtual_address_increase_refcount(virtual_address); 374 } 375 } 376 } 377 378 void mesh_model_store_subscriptions(mesh_model_t * model){ 379 uint32_t tag = mesh_model_subscription_tag_for_index(model->mid); 380 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->subscriptions, sizeof(model->subscriptions)); 381 } 382 383 static void mesh_model_delete_subscriptions(mesh_model_t * model){ 384 uint32_t tag = mesh_model_subscription_tag_for_index(model->mid); 385 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 386 } 387 388 void mesh_load_subscriptions(void){ 389 printf("Load Model Subscription Lists\n"); 390 // iterate over elements and models 391 mesh_element_iterator_t element_it; 392 mesh_element_iterator_init(&element_it); 393 while (mesh_element_iterator_has_next(&element_it)){ 394 mesh_element_t * element = mesh_element_iterator_next(&element_it); 395 mesh_model_iterator_t model_it; 396 mesh_model_iterator_init(&model_it, element); 397 while (mesh_model_iterator_has_next(&model_it)){ 398 mesh_model_t * model = mesh_model_iterator_next(&model_it); 399 mesh_model_load_subscriptions(model); 400 } 401 } 402 } 403 404 void mesh_delete_subscriptions(void){ 405 printf("Delete Model Subscription Lists\n"); 406 // iterate over elements and models 407 mesh_element_iterator_t element_it; 408 mesh_element_iterator_init(&element_it); 409 while (mesh_element_iterator_has_next(&element_it)){ 410 mesh_element_t * element = mesh_element_iterator_next(&element_it); 411 mesh_model_iterator_t model_it; 412 mesh_model_iterator_init(&model_it, element); 413 while (mesh_model_iterator_has_next(&model_it)){ 414 mesh_model_t * model = mesh_model_iterator_next(&model_it); 415 mesh_model_delete_subscriptions(model); 416 } 417 } 418 } 419 420 // Model Publication 421 422 static uint32_t mesh_model_publication_tag_for_index(uint16_t internal_model_id){ 423 return ((uint32_t) 'M' << 24) | ((uint32_t) 'P' << 16) | ((uint32_t) internal_model_id); 424 } 425 426 static void mesh_model_load_publication(mesh_model_t * mesh_model){ 427 if (mesh_model->publication_model == NULL) return; 428 429 uint32_t tag = mesh_model_publication_tag_for_index(mesh_model->mid); 430 btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &mesh_model->publication_model, sizeof(mesh_publication_model_t)); 431 432 // increase ref counts for current virtual publicataion address 433 uint16_t src = mesh_model->publication_model->address; 434 if (mesh_network_address_virtual(src)){ 435 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(src); 436 mesh_virtual_address_increase_refcount(virtual_address); 437 } 438 439 mesh_model_publication_start(mesh_model); 440 } 441 442 void mesh_model_store_publication(mesh_model_t * mesh_model){ 443 if (mesh_model->publication_model == NULL) return; 444 uint32_t tag = mesh_model_publication_tag_for_index(mesh_model->mid); 445 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &mesh_model->subscriptions, sizeof(mesh_publication_model_t)); 446 } 447 448 static void mesh_model_delete_publication(mesh_model_t * mesh_model){ 449 if (mesh_model->publication_model == NULL) return; 450 uint32_t tag = mesh_model_publication_tag_for_index(mesh_model->mid); 451 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 452 } 453 454 void mesh_load_publications(void){ 455 printf("Load Model Publications\n"); 456 // iterate over elements and models 457 mesh_element_iterator_t element_it; 458 mesh_element_iterator_init(&element_it); 459 while (mesh_element_iterator_has_next(&element_it)){ 460 mesh_element_t * element = mesh_element_iterator_next(&element_it); 461 mesh_model_iterator_t model_it; 462 mesh_model_iterator_init(&model_it, element); 463 while (mesh_model_iterator_has_next(&model_it)){ 464 mesh_model_t * model = mesh_model_iterator_next(&model_it); 465 mesh_model_load_publication(model); 466 } 467 } 468 } 469 470 void mesh_delete_publications(void){ 471 printf("Delete Model Publications\n"); 472 // iterate over elements and models 473 mesh_element_iterator_t element_it; 474 mesh_element_iterator_init(&element_it); 475 while (mesh_element_iterator_has_next(&element_it)){ 476 mesh_element_t * element = mesh_element_iterator_next(&element_it); 477 mesh_model_iterator_t model_it; 478 mesh_model_iterator_init(&model_it, element); 479 while (mesh_model_iterator_has_next(&model_it)){ 480 mesh_model_t * model = mesh_model_iterator_next(&model_it); 481 mesh_model_delete_publication(model); 482 } 483 } 484 } 485 486 // Mesh Network Keys 487 static uint32_t mesh_network_key_tag_for_internal_index(uint16_t internal_index){ 488 return ((uint32_t) 'M' << 24) | ((uint32_t) 'N' << 16) | ((uint32_t) internal_index); 489 } 490 491 void mesh_store_network_key(mesh_network_key_t * network_key){ 492 mesh_persistent_net_key_t data; 493 printf("Store NetKey: internal index 0x%x, NetKey Index 0x%06x, NID %02x: ", network_key->internal_index, network_key->netkey_index, network_key->nid); 494 printf_hexdump(network_key->net_key, 16); 495 uint32_t tag = mesh_network_key_tag_for_internal_index(network_key->internal_index); 496 data.netkey_index = network_key->netkey_index; 497 memcpy(data.net_key, network_key->net_key, 16); 498 memcpy(data.identity_key, network_key->identity_key, 16); 499 memcpy(data.beacon_key, network_key->beacon_key, 16); 500 memcpy(data.network_id, network_key->network_id, 8); 501 data.nid = network_key->nid; 502 data.version = network_key->version; 503 memcpy(data.encryption_key, network_key->encryption_key, 16); 504 memcpy(data.privacy_key, network_key->privacy_key, 16); 505 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(mesh_persistent_net_key_t)); 506 } 507 508 void mesh_delete_network_key(uint16_t internal_index){ 509 uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index); 510 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 511 } 512 513 514 void mesh_load_network_keys(void){ 515 printf("Load Network Keys\n"); 516 uint16_t internal_index; 517 for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){ 518 mesh_persistent_net_key_t data; 519 uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index); 520 int netkey_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 521 if (netkey_len != sizeof(mesh_persistent_net_key_t)) continue; 522 523 mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get(); 524 if (network_key == NULL) return; 525 526 network_key->netkey_index = data.netkey_index; 527 memcpy(network_key->net_key, data.net_key, 16); 528 memcpy(network_key->identity_key, data.identity_key, 16); 529 memcpy(network_key->beacon_key, data.beacon_key, 16); 530 memcpy(network_key->network_id, data.network_id, 8); 531 network_key->nid = data.nid; 532 network_key->version = data.version; 533 memcpy(network_key->encryption_key, data.encryption_key, 16); 534 memcpy(network_key->privacy_key, data.privacy_key, 16); 535 536 #ifdef ENABLE_GATT_BEARER 537 // setup advertisement with network id 538 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); 539 #endif 540 541 mesh_network_key_add(network_key); 542 543 mesh_subnet_setup_for_netkey_index(network_key->netkey_index); 544 545 printf("- internal index 0x%x, NetKey Index 0x%06x, NID %02x: ", network_key->internal_index, network_key->netkey_index, network_key->nid); 546 printf_hexdump(network_key->net_key, 16); 547 } 548 } 549 550 void mesh_delete_network_keys(void){ 551 printf("Delete Network Keys\n"); 552 553 uint16_t internal_index; 554 for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){ 555 mesh_delete_network_key(internal_index); 556 } 557 } 558 559 // Mesh App Keys 560 561 static uint32_t mesh_transport_key_tag_for_internal_index(uint16_t internal_index){ 562 return ((uint32_t) 'M' << 24) | ((uint32_t) 'A' << 16) | ((uint32_t) internal_index); 563 } 564 565 void mesh_store_app_key(mesh_transport_key_t * app_key){ 566 mesh_persistent_app_key_t data; 567 printf("Store AppKey: internal index 0x%x, AppKey Index 0x%06x, AID %02x: ", app_key->internal_index, app_key->appkey_index, app_key->aid); 568 printf_hexdump(app_key->key, 16); 569 uint32_t tag = mesh_transport_key_tag_for_internal_index(app_key->internal_index); 570 data.netkey_index = app_key->netkey_index; 571 data.appkey_index = app_key->appkey_index; 572 data.aid = app_key->aid; 573 data.version = app_key->version; 574 memcpy(data.key, app_key->key, 16); 575 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 576 } 577 578 void mesh_delete_app_key(uint16_t internal_index){ 579 uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index); 580 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 581 } 582 583 void mesh_load_app_keys(void){ 584 printf("Load App Keys\n"); 585 uint16_t internal_index; 586 for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){ 587 mesh_persistent_app_key_t data; 588 uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index); 589 int app_key_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 590 if (app_key_len == 0) continue; 591 592 mesh_transport_key_t * key = btstack_memory_mesh_transport_key_get(); 593 if (key == NULL) return; 594 595 key->internal_index = internal_index; 596 key->appkey_index = data.appkey_index; 597 key->netkey_index = data.netkey_index; 598 key->aid = data.aid; 599 key->akf = 1; 600 key->version = data.version; 601 memcpy(key->key, data.key, 16); 602 mesh_transport_key_add(key); 603 printf("- internal index 0x%x, AppKey Index 0x%06x, AID %02x: ", key->internal_index, key->appkey_index, key->aid); 604 printf_hexdump(key->key, 16); 605 } 606 } 607 608 void mesh_delete_app_keys(void){ 609 printf("Delete App Keys\n"); 610 611 uint16_t internal_index; 612 for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){ 613 mesh_delete_app_key(internal_index); 614 } 615 } 616 617 618 // Model to Appkey List 619 620 static uint32_t mesh_model_tag_for_index(uint16_t internal_model_id){ 621 return ((uint32_t) 'M' << 24) | ((uint32_t) 'B' << 16) | ((uint32_t) internal_model_id); 622 } 623 624 static void mesh_load_appkey_list(mesh_model_t * model){ 625 uint32_t tag = mesh_model_tag_for_index(model->mid); 626 btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->appkey_indices, sizeof(model->appkey_indices)); 627 } 628 629 static void mesh_store_appkey_list(mesh_model_t * model){ 630 uint32_t tag = mesh_model_tag_for_index(model->mid); 631 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->appkey_indices, sizeof(model->appkey_indices)); 632 } 633 634 static void mesh_delete_appkey_list(mesh_model_t * model){ 635 uint32_t tag = mesh_model_tag_for_index(model->mid); 636 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag); 637 } 638 639 void mesh_load_appkey_lists(void){ 640 printf("Load Appkey Lists\n"); 641 // iterate over elements and models 642 mesh_element_iterator_t element_it; 643 mesh_element_iterator_init(&element_it); 644 while (mesh_element_iterator_has_next(&element_it)){ 645 mesh_element_t * element = mesh_element_iterator_next(&element_it); 646 mesh_model_iterator_t model_it; 647 mesh_model_iterator_init(&model_it, element); 648 while (mesh_model_iterator_has_next(&model_it)){ 649 mesh_model_t * model = mesh_model_iterator_next(&model_it); 650 mesh_load_appkey_list(model); 651 } 652 } 653 } 654 655 void mesh_delete_appkey_lists(void){ 656 printf("Delete Appkey Lists\n"); 657 // iterate over elements and models 658 mesh_element_iterator_t element_it; 659 mesh_element_iterator_init(&element_it); 660 while (mesh_element_iterator_has_next(&element_it)){ 661 mesh_element_t * element = mesh_element_iterator_next(&element_it); 662 mesh_model_iterator_t model_it; 663 mesh_model_iterator_init(&model_it, element); 664 while (mesh_model_iterator_has_next(&model_it)){ 665 mesh_model_t * model = mesh_model_iterator_next(&model_it); 666 mesh_delete_appkey_list(model); 667 } 668 } 669 } 670 671 uint8_t mesh_model_bind_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 672 uint16_t i; 673 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 674 if (mesh_model->appkey_indices[i] == appkey_index) return MESH_FOUNDATION_STATUS_SUCCESS; 675 } 676 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 677 if (mesh_model->appkey_indices[i] == MESH_APPKEY_INVALID) { 678 mesh_model->appkey_indices[i] = appkey_index; 679 mesh_store_appkey_list(mesh_model); 680 return MESH_FOUNDATION_STATUS_SUCCESS; 681 } 682 } 683 return MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 684 } 685 686 void mesh_model_unbind_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 687 uint16_t i; 688 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 689 if (mesh_model->appkey_indices[i] == appkey_index) { 690 mesh_model->appkey_indices[i] = MESH_APPKEY_INVALID; 691 mesh_store_appkey_list(mesh_model); 692 } 693 } 694 } 695 696 int mesh_model_contains_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 697 uint16_t i; 698 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 699 if (mesh_model->appkey_indices[i] == appkey_index) return 1; 700 } 701 return 0; 702 } 703 704 void mesh_access_netkey_finalize(mesh_network_key_t * network_key){ 705 mesh_network_key_remove(network_key); 706 mesh_delete_network_key(network_key->internal_index); 707 btstack_memory_mesh_network_key_free(network_key); 708 } 709 710 void mesh_access_appkey_finalize(mesh_transport_key_t * transport_key){ 711 mesh_transport_key_remove(transport_key); 712 mesh_delete_app_key(transport_key->appkey_index); 713 btstack_memory_mesh_transport_key_free(transport_key); 714 } 715 716 void mesh_access_key_refresh_revoke_keys(mesh_subnet_t * subnet){ 717 // delete old netkey index 718 mesh_access_netkey_finalize(subnet->old_key); 719 subnet->old_key = subnet->new_key; 720 subnet->new_key = NULL; 721 722 // delete old appkeys, if any 723 mesh_transport_key_iterator_t it; 724 mesh_transport_key_iterator_init(&it, subnet->netkey_index); 725 while (mesh_transport_key_iterator_has_more(&it)){ 726 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 727 if (transport_key->old_key == 0) continue; 728 mesh_access_appkey_finalize(transport_key); 729 } 730 } 731 732 // Mesh IV Index 733 static uint32_t mesh_tag_for_iv_index_and_seq_number(void){ 734 return ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'I' << 9) | ((uint32_t) 'S'); 735 } 736 737 void mesh_store_iv_index_after_provisioning(uint32_t iv_index){ 738 iv_index_and_sequence_number_t data; 739 uint32_t tag = mesh_tag_for_iv_index_and_seq_number(); 740 data.iv_index = iv_index; 741 data.seq_number = 0; 742 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 743 744 sequence_number_last_stored = data.seq_number; 745 sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL; 746 } 747 748 void mesh_store_iv_index_and_sequence_number(void){ 749 iv_index_and_sequence_number_t data; 750 uint32_t tag = mesh_tag_for_iv_index_and_seq_number(); 751 data.iv_index = mesh_get_iv_index(); 752 data.seq_number = mesh_sequence_number_peek(); 753 btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 754 755 sequence_number_last_stored = data.seq_number; 756 sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL; 757 } 758 759 int mesh_load_iv_index_and_sequence_number(uint32_t * iv_index, uint32_t * sequence_number){ 760 iv_index_and_sequence_number_t data; 761 uint32_t tag = mesh_tag_for_iv_index_and_seq_number(); 762 uint32_t len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data)); 763 if (len == sizeof(iv_index_and_sequence_number_t)){ 764 *iv_index = data.iv_index; 765 *sequence_number = data.seq_number; 766 return 1; 767 } 768 return 0; 769 } 770 771 // higher layer 772 static void mesh_persist_iv_index_and_sequence_number(void){ 773 if (mesh_sequence_number_peek() >= sequence_number_storage_trigger){ 774 mesh_store_iv_index_and_sequence_number(); 775 } 776 } 777 778 779 static uint32_t mesh_tag_for_prov_data(void){ 780 return ((uint32_t) 'P' << 24) | ((uint32_t) 'R' << 16) | ((uint32_t) 'O' << 8) | (uint32_t)'V'; 781 } 782 783 void mesh_node_reset(void){ 784 // PROV 785 btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, mesh_tag_for_prov_data()); 786 // everything else 787 mesh_delete_network_keys(); 788 mesh_delete_app_keys(); 789 mesh_delete_appkey_lists(); 790 mesh_delete_virtual_addresses(); 791 mesh_delete_subscriptions(); 792 mesh_delete_publications(); 793 } 794 795 typedef struct { 796 uint16_t unicast_address; 797 uint8_t flags; 798 uint8_t device_key[16]; 799 800 } mesh_persistent_provisioning_data_t; 801 802 static void mesh_node_store_provisioning_data(mesh_provisioning_data_t * provisioning_data){ 803 804 // fill persistent prov data 805 mesh_persistent_provisioning_data_t persistent_provisioning_data; 806 807 persistent_provisioning_data.unicast_address = provisioning_data->unicast_address; 808 memcpy(persistent_provisioning_data.device_key, provisioning_data->device_key, 16); 809 810 // store in tlv 811 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 812 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)); 813 814 // store IV Index and sequence number 815 mesh_store_iv_index_after_provisioning(provisioning_data->iv_index); 816 817 // store primary network key 818 mesh_store_network_key(provisioning_data->network_key); 819 } 820 821 static void mesh_access_setup_unprovisioned_device(const uint8_t * device_uuid){ 822 #ifdef ENABLE_MESH_PB_ADV 823 // PB-ADV 824 beacon_unprovisioned_device_start(mesh_node_get_device_uuid(), 0); 825 #endif 826 #ifdef ENABLE_MESH_PB_GATT 827 mesh_proxy_start_advertising_unprovisioned_device(); 828 #endif 829 } 830 831 static void mesh_access_setup_without_provisiong_data_random(void * arg){ 832 UNUSED(arg); 833 // set random value 834 mesh_node_set_device_uuid(random_device_uuid); 835 mesh_access_setup_unprovisioned_device(random_device_uuid); 836 } 837 838 static void mesh_access_setup_with_provisiong_data_random(void * arg){ 839 UNUSED(arg); 840 // set random value 841 mesh_node_set_device_uuid(random_device_uuid); 842 } 843 844 static int mesh_node_startup_from_tlv(void){ 845 846 mesh_persistent_provisioning_data_t persistent_provisioning_data; 847 btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context); 848 849 // load provisioning data 850 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)); 851 printf("Provisioning data available: %u\n", prov_len ? 1 : 0); 852 int prov_data_valid = prov_len == sizeof(mesh_persistent_provisioning_data_t); 853 if (prov_data_valid){ 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 // create random uuid if not already set 900 if (mesh_node_get_device_uuid() == NULL){ 901 btstack_crypto_random_generate(&mesh_access_crypto_random, random_device_uuid, 16, &mesh_access_setup_with_provisiong_data_random, NULL); 902 } 903 904 } else { 905 906 const uint8_t * device_uuid = mesh_node_get_device_uuid(); 907 if (device_uuid){ 908 mesh_access_setup_unprovisioned_device(device_uuid); 909 } else{ 910 btstack_crypto_random_generate(&mesh_access_crypto_random, random_device_uuid, 16, &mesh_access_setup_without_provisiong_data_random, NULL); 911 } 912 913 } 914 915 return prov_data_valid; 916 } 917 918 919 static void mesh_node_setup_default_models(void){ 920 // configure Config Server 921 mesh_configuration_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER); 922 mesh_configuration_server_model.model_data = &mesh_configuration_server_model_context; 923 mesh_configuration_server_model.operations = mesh_configuration_server_get_operations(); 924 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_configuration_server_model); 925 926 // Config Health Server 927 mesh_health_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_HEALTH_SERVER); 928 mesh_element_add_model(mesh_node_get_primary_element(), &mesh_health_server_model); 929 } 930 931 void mesh_init(void){ 932 933 // register for HCI events 934 hci_event_callback_registration.callback = &hci_packet_handler; 935 hci_add_event_handler(&hci_event_callback_registration); 936 937 // ADV Bearer also used for GATT Proxy Advertisements and PB-GATT 938 adv_bearer_init(); 939 940 #ifdef ENABLE_MESH_GATT_BEARER 941 // Setup GATT bearer 942 gatt_bearer_init(); 943 #endif 944 945 #ifdef ENABLE_MESH_ADV_BEARER 946 // Setup Unprovisioned Device Beacon 947 beacon_init(); 948 #endif 949 950 provisioning_device_init(); 951 952 // Node Configuration 953 mesh_node_init(); 954 955 // Network layer 956 mesh_network_init(); 957 958 // Transport layers (lower + upper)) 959 mesh_lower_transport_init(); 960 mesh_upper_transport_init(); 961 962 // Access layer 963 mesh_access_init(); 964 965 // Add mandatory models: Config Server and Health Server 966 mesh_node_setup_default_models(); 967 968 // register for seq number updates 969 mesh_sequence_number_set_update_callback(&mesh_persist_iv_index_and_sequence_number); 970 } 971 972 /** 973 * Register for Mesh Provisioning Device events 974 * @param packet_handler 975 */ 976 void mesh_register_provisioning_device_packet_handler(btstack_packet_handler_t packet_handler){ 977 provisioning_device_packet_handler = packet_handler; 978 provisioning_device_register_packet_handler(&mesh_provisioning_message_handler); 979 } 980