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_configuration_server.c" 39 40 #include <string.h> 41 #include <stdio.h> 42 43 #include "mesh/mesh_configuration_server.h" 44 45 #include "bluetooth_company_id.h" 46 #include "btstack_debug.h" 47 #include "btstack_memory.h" 48 #include "btstack_tlv.h" 49 #include "btstack_util.h" 50 51 #include "mesh/beacon.h" 52 #include "mesh/gatt_bearer.h" 53 #include "mesh/mesh.h" 54 #include "mesh/mesh_access.h" 55 #include "mesh/mesh_crypto.h" 56 #include "mesh/mesh_foundation.h" 57 #include "mesh/mesh_iv_index_seq_number.h" 58 #include "mesh/mesh_keys.h" 59 #include "mesh/mesh_network.h" 60 #include "mesh/mesh_node.h" 61 #include "mesh/mesh_proxy.h" 62 #include "mesh/mesh_upper_transport.h" 63 #include "mesh/mesh_virtual_addresses.h" 64 65 #define MESH_HEARTBEAT_FEATURES_SUPPORTED_MASK 0x000f 66 67 // current access pdu 68 static mesh_pdu_t * access_pdu_in_process; 69 70 // data from current pdu 71 static uint16_t configuration_server_element_address; 72 static uint32_t configuration_server_model_identifier; 73 static mesh_model_t * configuration_server_target_model; 74 static mesh_publication_model_t configuration_server_publication_model; 75 76 // cmac for virtual address hash and netkey derive 77 static btstack_crypto_aes128_cmac_t configuration_server_cmac_request; 78 79 // used to setup virtual addresses 80 static uint8_t configuration_server_label_uuid[16]; 81 static uint16_t configuration_server_hash; 82 83 // heartbeat publication and subscription state for all Configuration Server models - there is only one 84 // static mesh_heartbeat_subscription_t mesh_heartbeat_subscription; 85 86 // for PTS testing 87 static int config_netkey_list_max = 0; 88 89 90 // Heartbeat (helper) 91 static uint16_t heartbeat_pwr2(uint8_t value){ 92 if (value == 0 ) return 0x0000; 93 if (value == 0xff || value == 0x11) return 0xffff; 94 return 1 << (value-1); 95 } 96 97 static uint8_t heartbeat_count_log(uint16_t value){ 98 if (value == 0) return 0x00; 99 if (value == 0xffff) return 0xff; 100 // count leading zeros, supported by clang and gcc 101 // note: CountLog(8) == CountLog(7) = 3 102 return 33 - __builtin_clz(value - 1); 103 } 104 105 static uint8_t heartbeat_period_log(uint16_t value){ 106 if (value == 0) return 0x00; 107 // count leading zeros, supported by clang and gcc 108 // note: PeriodLog(8) == PeriodLog(7) = 3 109 return 33 - __builtin_clz(value - 1); 110 } 111 112 // TLV 113 114 static int mesh_model_is_configuration_server(uint32_t model_identifier){ 115 return mesh_model_is_bluetooth_sig(model_identifier) && (mesh_model_get_model_id(model_identifier) == MESH_SIG_MODEL_ID_CONFIGURATION_SERVER); 116 } 117 118 // Configuration Model Subscriptions (helper) 119 120 // Model to Appkey List 121 122 static uint8_t mesh_model_add_subscription(mesh_model_t * mesh_model, uint16_t address){ 123 uint16_t i; 124 for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){ 125 if (mesh_model->subscriptions[i] == address) return MESH_FOUNDATION_STATUS_SUCCESS; 126 } 127 for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){ 128 if (mesh_model->subscriptions[i] == MESH_ADDRESS_UNSASSIGNED) { 129 mesh_model->subscriptions[i] = address; 130 return MESH_FOUNDATION_STATUS_SUCCESS; 131 } 132 } 133 return MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 134 } 135 136 static void mesh_model_delete_subscription(mesh_model_t * mesh_model, uint16_t address){ 137 uint16_t i; 138 for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){ 139 if (mesh_model->subscriptions[i] == address) { 140 mesh_model->subscriptions[i] = MESH_ADDRESS_UNSASSIGNED; 141 } 142 } 143 } 144 145 static void mesh_model_delete_all_subscriptions(mesh_model_t * mesh_model){ 146 uint16_t i; 147 for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){ 148 mesh_model->subscriptions[i] = MESH_ADDRESS_UNSASSIGNED; 149 } 150 } 151 152 static void mesh_subcription_decrease_virtual_address_ref_count(mesh_model_t *mesh_model){ 153 // decrease ref counts for current virtual subscriptions 154 uint16_t i; 155 for (i = 0; i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL ; i++){ 156 uint16_t src = mesh_model->subscriptions[i]; 157 if (mesh_network_address_virtual(src)){ 158 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(src); 159 mesh_virtual_address_decrease_refcount(virtual_address); 160 } 161 } 162 } 163 static void mesh_configuration_server_stop_publishing_using_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){ 164 // stop publishing if this AppKey was used 165 if (mesh_model->publication_model != NULL){ 166 mesh_publication_model_t * publication_model = mesh_model->publication_model; 167 if (publication_model->appkey_index == appkey_index){ 168 publication_model->address = MESH_ADDRESS_UNSASSIGNED; 169 publication_model->appkey_index = 0; 170 publication_model->period = 0; 171 publication_model->ttl = 0; 172 publication_model->retransmit = 0; 173 174 mesh_model_store_publication(mesh_model); 175 } 176 } 177 } 178 179 // AppKeys Helper 180 static void mesh_configuration_server_delete_appkey(mesh_transport_key_t * transport_key){ 181 uint16_t appkey_index = transport_key->appkey_index; 182 183 // iterate over elements and models 184 mesh_element_iterator_t element_it; 185 mesh_element_iterator_init(&element_it); 186 while (mesh_element_iterator_has_next(&element_it)){ 187 mesh_element_t * element = mesh_element_iterator_next(&element_it); 188 mesh_model_iterator_t model_it; 189 mesh_model_iterator_init(&model_it, element); 190 while (mesh_model_iterator_has_next(&model_it)){ 191 mesh_model_t * mesh_model = mesh_model_iterator_next(&model_it); 192 193 // remove from Model to AppKey List 194 mesh_model_unbind_appkey(mesh_model, appkey_index); 195 196 // stop publishing if this appkey is used 197 mesh_configuration_server_stop_publishing_using_appkey(mesh_model, appkey_index); 198 } 199 } 200 201 mesh_access_appkey_finalize(transport_key); 202 } 203 204 // Foundatiopn Message 205 206 const mesh_access_message_t mesh_foundation_config_beacon_status = { 207 MESH_FOUNDATION_OPERATION_BEACON_STATUS, "1" 208 }; 209 const mesh_access_message_t mesh_foundation_config_default_ttl_status = { 210 MESH_FOUNDATION_OPERATION_DEFAULT_TTL_STATUS, "1" 211 }; 212 const mesh_access_message_t mesh_foundation_config_friend_status = { 213 MESH_FOUNDATION_OPERATION_FRIEND_STATUS, "1" 214 }; 215 const mesh_access_message_t mesh_foundation_config_gatt_proxy_status = { 216 MESH_FOUNDATION_OPERATION_GATT_PROXY_STATUS, "1" 217 }; 218 const mesh_access_message_t mesh_foundation_config_relay_status = { 219 MESH_FOUNDATION_OPERATION_RELAY_STATUS, "11" 220 }; 221 const mesh_access_message_t mesh_foundation_config_model_publication_status = { 222 MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_STATUS, "1222111m" 223 }; 224 const mesh_access_message_t mesh_foundation_config_model_subscription_status = { 225 MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_STATUS, "122m" 226 }; 227 const mesh_access_message_t mesh_foundation_config_netkey_status = { 228 MESH_FOUNDATION_OPERATION_NETKEY_STATUS, "12" 229 }; 230 const mesh_access_message_t mesh_foundation_config_appkey_status = { 231 MESH_FOUNDATION_OPERATION_APPKEY_STATUS, "13" 232 }; 233 const mesh_access_message_t mesh_foundation_config_model_app_status = { 234 MESH_FOUNDATION_OPERATION_MODEL_APP_STATUS, "122m" 235 }; 236 const mesh_access_message_t mesh_foundation_node_reset_status = { 237 MESH_FOUNDATION_OPERATION_NODE_RESET_STATUS, "" 238 }; 239 const mesh_access_message_t mesh_foundation_config_heartbeat_publication_status = { 240 MESH_FOUNDATION_OPERATION_HEARTBEAT_PUBLICATION_STATUS, "1211122" 241 }; 242 const mesh_access_message_t mesh_foundation_config_network_transmit_status = { 243 MESH_FOUNDATION_OPERATION_NETWORK_TRANSMIT_STATUS, "1" 244 }; 245 const mesh_access_message_t mesh_foundation_node_identity_status = { 246 MESH_FOUNDATION_OPERATION_NODE_IDENTITY_STATUS, "121" 247 }; 248 const mesh_access_message_t mesh_key_refresh_phase_status = { 249 MESH_FOUNDATION_OPERATION_KEY_REFRESH_PHASE_STATUS, "121" 250 }; 251 const mesh_access_message_t mesh_foundation_low_power_node_poll_timeout_status = { 252 MESH_FOUNDATION_OPERATION_LOW_POWER_NODE_POLL_TIMEOUT_STATUS, "23" 253 }; 254 const mesh_access_message_t mesh_foundation_config_heartbeat_subscription_status = { 255 MESH_FOUNDATION_OPERATION_HEARTBEAT_SUBSCRIPTION_STATUS, "1221111" 256 }; 257 258 static void config_server_send_message(uint16_t netkey_index, uint16_t dest, mesh_pdu_t *pdu){ 259 // Configuration Server is on primary element and can only use DeviceKey 260 uint16_t appkey_index = MESH_DEVICE_KEY_INDEX; 261 uint16_t src = mesh_node_get_primary_element_address(); 262 uint8_t ttl = mesh_foundation_default_ttl_get(); 263 mesh_upper_transport_setup_access_pdu_header(pdu, netkey_index, appkey_index, ttl, src, dest, 0); 264 mesh_access_send_unacknowledged_pdu(pdu); 265 } 266 267 static void config_composition_data_status(uint16_t netkey_index, uint16_t dest){ 268 269 printf("Received Config Composition Data Get -> send Config Composition Data Status\n"); 270 271 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_STATUS); 272 if (!transport_pdu) return; 273 274 // page 0 275 mesh_access_transport_add_uint8(transport_pdu, 0); 276 277 // CID 278 mesh_access_transport_add_uint16(transport_pdu, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH); 279 // PID 280 mesh_access_transport_add_uint16(transport_pdu, 0); 281 // VID 282 mesh_access_transport_add_uint16(transport_pdu, 0); 283 // CRPL - number of protection list entries 284 mesh_access_transport_add_uint16(transport_pdu, 1); 285 // Features - Relay, Proxy, Friend, Lower Power, ... 286 mesh_access_transport_add_uint16(transport_pdu, 0); 287 288 mesh_element_iterator_t element_it; 289 mesh_element_iterator_init(&element_it); 290 while (mesh_element_iterator_has_next(&element_it)){ 291 mesh_element_t * element = mesh_element_iterator_next(&element_it); 292 293 // Loc 294 mesh_access_transport_add_uint16(transport_pdu, element->loc); 295 // NumS 296 mesh_access_transport_add_uint8( transport_pdu, element->models_count_sig); 297 // NumV 298 mesh_access_transport_add_uint8( transport_pdu, element->models_count_vendor); 299 300 mesh_model_iterator_t model_it; 301 302 // SIG Models 303 mesh_model_iterator_init(&model_it, element); 304 while (mesh_model_iterator_has_next(&model_it)){ 305 mesh_model_t * model = mesh_model_iterator_next(&model_it); 306 if (!mesh_model_is_bluetooth_sig(model->model_identifier)) continue; 307 mesh_access_transport_add_uint16(transport_pdu, model->model_identifier); 308 } 309 // Vendor Models 310 mesh_model_iterator_init(&model_it, element); 311 while (mesh_model_iterator_has_next(&model_it)){ 312 mesh_model_t * model = mesh_model_iterator_next(&model_it); 313 if (mesh_model_is_bluetooth_sig(model->model_identifier)) continue; 314 mesh_access_transport_add_uint32(transport_pdu, model->model_identifier); 315 } 316 } 317 318 // send as segmented access pdu 319 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 320 } 321 322 static void config_composition_data_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 323 UNUSED(mesh_model); 324 config_composition_data_status(mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 325 326 mesh_access_message_processed(pdu); 327 } 328 329 static void config_model_beacon_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest){ 330 UNUSED(mesh_model); 331 // setup message 332 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_config_beacon_status, 333 mesh_foundation_beacon_get()); 334 if (!transport_pdu) return; 335 336 // send as segmented access pdu 337 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 338 } 339 340 static void config_beacon_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 341 config_model_beacon_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 342 343 mesh_access_message_processed(pdu); 344 } 345 346 static void config_beacon_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 347 mesh_access_parser_state_t parser; 348 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 349 350 uint8_t beacon_enabled = mesh_access_parser_get_u8(&parser); 351 352 // beacon valid 353 if (beacon_enabled < MESH_FOUNDATION_STATE_NOT_SUPPORTED) { 354 // set and store new value 355 mesh_foundation_beacon_set(beacon_enabled); 356 mesh_foundation_state_store(); 357 358 // send status 359 config_model_beacon_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 360 } 361 362 mesh_access_message_processed(pdu); 363 } 364 365 static void config_model_default_ttl_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest){ 366 UNUSED(mesh_model); 367 // setup message 368 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 369 &mesh_foundation_config_default_ttl_status, mesh_foundation_default_ttl_get()); 370 if (!transport_pdu) return; 371 372 // send as segmented access pdu 373 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 374 } 375 376 static void config_default_ttl_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 377 config_model_default_ttl_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 378 379 mesh_access_message_processed(pdu); 380 } 381 382 static void config_default_ttl_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 383 mesh_access_parser_state_t parser; 384 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 385 386 uint8_t new_ttl = mesh_access_parser_get_u8(&parser); 387 388 // validate (0x01 and > 0x7f are prohibited) 389 if (new_ttl <= 0x7f && new_ttl != 0x01) { 390 391 // set and store 392 mesh_foundation_default_ttl_set(new_ttl); 393 mesh_foundation_state_store(); 394 395 // send status 396 config_model_default_ttl_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 397 } 398 399 mesh_access_message_processed(pdu); 400 } 401 402 static void config_friend_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest){ 403 UNUSED(mesh_model); 404 405 // setup message 406 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 407 &mesh_foundation_config_friend_status, mesh_foundation_friend_get()); 408 if (!transport_pdu) return; 409 410 // send as segmented access pdu 411 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 412 } 413 414 static void config_friend_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 415 config_friend_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 416 417 mesh_access_message_processed(pdu); 418 } 419 420 static void config_friend_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 421 mesh_access_parser_state_t parser; 422 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 423 424 uint8_t new_friend_state = mesh_access_parser_get_u8(&parser); 425 426 // validate 427 if (new_friend_state < MESH_FOUNDATION_STATE_NOT_SUPPORTED) { 428 429 // set and store 430 if (mesh_foundation_friend_get() != MESH_FOUNDATION_STATE_NOT_SUPPORTED){ 431 mesh_foundation_friend_set(new_friend_state); 432 mesh_foundation_state_store(); 433 } 434 435 // send status 436 config_friend_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 437 } 438 439 mesh_access_message_processed(pdu); 440 } 441 442 static void config_model_gatt_proxy_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest){ 443 UNUSED(mesh_model); 444 // setup message 445 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_config_gatt_proxy_status, mesh_foundation_gatt_proxy_get()); 446 if (!transport_pdu) return; 447 448 // send as segmented access pdu 449 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 450 } 451 452 static void config_gatt_proxy_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 453 config_model_gatt_proxy_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 454 455 mesh_access_message_processed(pdu); 456 } 457 458 static void config_gatt_proxy_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 459 mesh_access_parser_state_t parser; 460 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 461 462 uint8_t enabled = mesh_access_parser_get_u8(&parser); 463 464 // validate 465 if (enabled < MESH_FOUNDATION_STATE_NOT_SUPPORTED) { 466 // set and store 467 mesh_foundation_gatt_proxy_set(enabled); 468 mesh_foundation_state_store(); 469 470 // send status 471 config_model_gatt_proxy_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 472 } 473 474 mesh_access_message_processed(pdu); 475 476 // trigger heartbeat emit on change 477 mesh_configuration_server_feature_changed(); 478 } 479 480 static void config_model_relay_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest){ 481 UNUSED(mesh_model); 482 483 // setup message 484 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_config_relay_status, 485 mesh_foundation_relay_get(), 486 mesh_foundation_relay_retransmit_get()); 487 if (!transport_pdu) return; 488 489 // send as segmented access pdu 490 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 491 } 492 493 static void config_relay_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 494 config_model_relay_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 495 496 mesh_access_message_processed(pdu); 497 } 498 499 static void config_relay_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 500 501 mesh_access_parser_state_t parser; 502 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 503 504 // check if valid 505 uint8_t relay = mesh_access_parser_get_u8(&parser); 506 uint8_t relay_retransmit = mesh_access_parser_get_u8(&parser); 507 508 // check if valid 509 if (relay <= 1) { 510 // only update if supported 511 if (mesh_foundation_relay_get() != MESH_FOUNDATION_STATE_NOT_SUPPORTED){ 512 mesh_foundation_relay_set(relay); 513 mesh_foundation_relay_retransmit_set(relay_retransmit); 514 mesh_foundation_state_store(); 515 } 516 517 // send status 518 config_model_relay_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 519 } 520 521 mesh_access_message_processed(pdu); 522 523 // trigger heartbeat emit on change 524 mesh_configuration_server_feature_changed(); 525 } 526 527 static void config_model_network_transmit_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest){ 528 UNUSED(mesh_model); 529 530 // setup message 531 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 532 &mesh_foundation_config_network_transmit_status, mesh_foundation_network_transmit_get()); 533 if (!transport_pdu) return; 534 535 // send as segmented access pdu 536 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 537 } 538 539 static void config_model_network_transmit_get_handler(mesh_model_t * mesh_model, mesh_pdu_t * pdu){ 540 config_model_network_transmit_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 541 542 mesh_access_message_processed(pdu); 543 } 544 545 static void config_model_network_transmit_set_handler(mesh_model_t * mesh_model, mesh_pdu_t * pdu){ 546 mesh_access_parser_state_t parser; 547 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 548 549 uint8_t new_ttl = mesh_access_parser_get_u8(&parser); 550 551 // store 552 mesh_foundation_network_transmit_set(new_ttl); 553 mesh_foundation_state_store(); 554 555 // 556 config_model_network_transmit_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 557 558 mesh_access_message_processed(pdu); 559 } 560 561 // NetKey List 562 563 void config_nekey_list_set_max(uint16_t max){ 564 config_netkey_list_max = max; 565 } 566 567 static void config_netkey_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, uint16_t new_netkey_index){ 568 UNUSED(mesh_model); 569 570 // setup message 571 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 572 &mesh_foundation_config_netkey_status, status, new_netkey_index); 573 if (!transport_pdu) return; 574 575 // send as segmented access pdu 576 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 577 } 578 579 static void config_netkey_list(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest) { 580 UNUSED(mesh_model); 581 582 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(MESH_FOUNDATION_OPERATION_NETKEY_LIST); 583 if (!transport_pdu) return; 584 585 // add list of netkey indexes 586 mesh_network_key_iterator_t it; 587 mesh_network_key_iterator_init(&it); 588 while (mesh_network_key_iterator_has_more(&it)){ 589 mesh_network_key_t * network_key = mesh_network_key_iterator_get_next(&it); 590 mesh_access_transport_add_uint16(transport_pdu, network_key->netkey_index); 591 } 592 593 // send as segmented access pdu 594 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 595 } 596 597 static void config_netkey_add_derived(void * arg){ 598 mesh_subnet_t * subnet = (mesh_subnet_t *) arg; 599 600 // store network key 601 mesh_store_network_key(subnet->old_key); 602 603 // add key to NetKey List 604 mesh_network_key_add(subnet->old_key); 605 606 // add subnet 607 mesh_subnet_add(subnet); 608 609 #ifdef ENABLE_MESH_PROXY_SERVER 610 mesh_proxy_start_advertising_with_network_id(); 611 #endif 612 613 config_netkey_status(mesh_model_get_configuration_server(), mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), MESH_FOUNDATION_STATUS_SUCCESS, subnet->netkey_index); 614 mesh_access_message_processed(access_pdu_in_process); 615 } 616 617 static void config_netkey_add_handler(mesh_model_t * mesh_model, mesh_pdu_t * pdu){ 618 mesh_access_parser_state_t parser; 619 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 620 621 // get params 622 uint8_t new_netkey[16]; 623 uint16_t new_netkey_index = mesh_access_parser_get_u16(&parser); 624 mesh_access_parser_get_key(&parser, new_netkey); 625 626 uint8_t status; 627 628 const mesh_subnet_t * existing_subnet = mesh_subnet_get_by_netkey_index(new_netkey_index); 629 if (existing_subnet == NULL){ 630 631 // check limit for pts 632 uint16_t internal_index = mesh_network_key_get_free_index(); 633 if (internal_index == MESH_KEYS_INVALID_INDEX || (config_netkey_list_max && mesh_network_key_list_count() >= config_netkey_list_max)){ 634 status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 635 } else { 636 637 // allocate new key and subnet 638 mesh_network_key_t * new_network_key = btstack_memory_mesh_network_key_get(); 639 mesh_subnet_t * new_subnet = btstack_memory_mesh_subnet_get(); 640 641 if (new_network_key == NULL || new_subnet == NULL){ 642 if (new_network_key != NULL){ 643 btstack_memory_mesh_network_key_free(new_network_key); 644 } 645 if (new_subnet != NULL){ 646 btstack_memory_mesh_subnet_free(new_subnet); 647 } 648 status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 649 650 } else { 651 652 // setup key 653 new_network_key->internal_index = internal_index; 654 new_network_key->netkey_index = new_netkey_index; 655 memcpy(new_network_key->net_key, new_netkey, 16); 656 657 // setup subnet 658 new_subnet->old_key = new_network_key; 659 new_subnet->netkey_index = new_netkey_index; 660 new_subnet->key_refresh = MESH_KEY_REFRESH_NOT_ACTIVE; 661 662 // derive other keys 663 access_pdu_in_process = pdu; 664 mesh_network_key_derive(&configuration_server_cmac_request, new_network_key, config_netkey_add_derived, new_subnet); 665 return; 666 } 667 } 668 669 } else { 670 // network key for netkey index already exists 671 if (memcmp(existing_subnet->old_key->net_key, new_netkey, 16) == 0){ 672 // same netkey 673 status = MESH_FOUNDATION_STATUS_SUCCESS; 674 } else { 675 // different netkey 676 status = MESH_FOUNDATION_STATUS_KEY_INDEX_ALREADY_STORED; 677 } 678 } 679 680 // report status 681 config_netkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, new_netkey_index); 682 mesh_access_message_processed(pdu); 683 } 684 685 static void config_netkey_update_derived(void * arg){ 686 mesh_subnet_t * subnet = (mesh_subnet_t *) arg; 687 688 // store network key 689 mesh_store_network_key(subnet->new_key); 690 691 // add key to NetKey List 692 mesh_network_key_add(subnet->new_key); 693 694 // update subnet - Key Refresh Phase 1 695 subnet->key_refresh = MESH_KEY_REFRESH_FIRST_PHASE; 696 697 // report status 698 config_netkey_status(mesh_model_get_configuration_server(), mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), MESH_FOUNDATION_STATUS_SUCCESS, subnet->netkey_index); 699 mesh_access_message_processed(access_pdu_in_process); 700 } 701 702 static void config_netkey_update_handler(mesh_model_t * mesh_model, mesh_pdu_t * pdu) { 703 mesh_access_parser_state_t parser; 704 mesh_access_parser_init(&parser, (mesh_pdu_t *) pdu); 705 706 // get params 707 uint8_t new_netkey[16]; 708 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 709 mesh_access_parser_get_key(&parser, new_netkey); 710 711 // get existing subnet 712 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 713 if (subnet == NULL){ 714 config_netkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX, netkey_index); 715 mesh_access_message_processed(access_pdu_in_process); 716 return; 717 } 718 719 // get index for new key 720 uint16_t internal_index = mesh_network_key_get_free_index(); 721 if (internal_index == MESH_KEYS_INVALID_INDEX){ 722 config_netkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES, netkey_index); 723 mesh_access_message_processed(access_pdu_in_process); 724 return; 725 } 726 727 // get new key 728 mesh_network_key_t * new_network_key = btstack_memory_mesh_network_key_get(); 729 if (new_network_key == NULL){ 730 config_netkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES, netkey_index); 731 mesh_access_message_processed(access_pdu_in_process); 732 return; 733 } 734 735 // setup new key 736 new_network_key->internal_index = internal_index; 737 new_network_key->netkey_index = netkey_index; 738 new_network_key->version = (uint8_t)(subnet->old_key->version + 1); 739 memcpy(new_network_key->net_key, new_netkey, 16); 740 741 // store in subnet (not active yet) 742 subnet->new_key = new_network_key; 743 744 // derive other keys 745 access_pdu_in_process = pdu; 746 mesh_network_key_derive(&configuration_server_cmac_request, new_network_key, config_netkey_update_derived, subnet); 747 } 748 749 static void config_netkey_delete_handler(mesh_model_t * mesh_model, mesh_pdu_t * pdu) { 750 mesh_access_parser_state_t parser; 751 mesh_access_parser_init(&parser, (mesh_pdu_t *) pdu); 752 753 // get params 754 uint16_t netkey_index_to_remove = mesh_access_parser_get_u16(&parser); 755 756 // get message netkey 757 uint16_t netkey_index_pdu = mesh_pdu_netkey_index(pdu); 758 759 // remove subnet 760 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 761 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index_to_remove); 762 if (subnet != NULL){ 763 // A NetKey shall not be deleted from the NetKey List using a message secured with this NetKey. 764 // Also prevents deleting the last network key 765 if (netkey_index_to_remove != netkey_index_pdu){ 766 767 // remove all appkeys for this netkey 768 mesh_transport_key_iterator_t it; 769 mesh_transport_key_iterator_init(&it, netkey_index_to_remove); 770 while (mesh_transport_key_iterator_has_more(&it)){ 771 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 772 mesh_configuration_server_delete_appkey(transport_key); 773 } 774 775 // delete old/current key 776 mesh_access_netkey_finalize(subnet->old_key); 777 subnet->old_key = NULL; 778 779 // delete new key 780 if (subnet->new_key != NULL){ 781 mesh_access_netkey_finalize(subnet->new_key); 782 subnet->new_key = NULL; 783 } 784 785 // remove subnet 786 mesh_subnet_remove(subnet); 787 btstack_memory_mesh_subnet_free(subnet); 788 789 } else { 790 status = MESH_FOUNDATION_STATUS_CANNOT_REMOVE; 791 } 792 } 793 config_netkey_status(mesh_model, netkey_index_pdu, mesh_pdu_src(pdu), status, netkey_index_to_remove); 794 mesh_access_message_processed(pdu); 795 } 796 797 static void config_netkey_get_handler(mesh_model_t * mesh_model, mesh_pdu_t * pdu){ 798 config_netkey_list(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 799 mesh_access_message_processed(pdu); 800 } 801 802 // AppKey List 803 804 static void config_appkey_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest, uint32_t netkey_and_appkey_index, uint8_t status){ 805 UNUSED(mesh_model); 806 807 // setup message 808 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_config_appkey_status, 809 status, netkey_and_appkey_index); 810 if (!transport_pdu) return; 811 812 // send as segmented access pdu 813 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 814 } 815 816 static void config_appkey_list(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest, uint32_t netkey_index_of_list){ 817 UNUSED(mesh_model); 818 819 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(MESH_FOUNDATION_OPERATION_APPKEY_LIST); 820 if (!transport_pdu) return; 821 822 // check netkey_index is valid 823 mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index_of_list); 824 uint8_t status; 825 if (network_key == NULL){ 826 status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 827 } else { 828 status = MESH_FOUNDATION_STATUS_SUCCESS; 829 } 830 mesh_access_transport_add_uint8(transport_pdu, status); 831 mesh_access_transport_add_uint16(transport_pdu, netkey_index_of_list); 832 833 // add list of appkey indexes 834 mesh_transport_key_iterator_t it; 835 mesh_transport_key_iterator_init(&it, netkey_index_of_list); 836 while (mesh_transport_key_iterator_has_more(&it)){ 837 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 838 mesh_access_transport_add_uint16(transport_pdu, transport_key->appkey_index); 839 } 840 841 // send as segmented access pdu 842 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 843 } 844 845 static void config_appkey_add_or_update_aid(void *arg){ 846 mesh_transport_key_t * transport_key = (mesh_transport_key_t *) arg; 847 848 printf("Config Appkey Add/Update: NetKey Index 0x%04x, AppKey Index 0x%04x, AID %02x: ", transport_key->netkey_index, transport_key->appkey_index, transport_key->aid); 849 printf_hexdump(transport_key->key, 16); 850 851 // store in TLV 852 mesh_store_app_key(transport_key); 853 854 // add app key 855 mesh_transport_key_add(transport_key); 856 857 uint32_t netkey_and_appkey_index = (transport_key->appkey_index << 12) | transport_key->netkey_index; 858 config_appkey_status(mesh_model_get_configuration_server(), mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_SUCCESS); 859 860 mesh_access_message_processed(access_pdu_in_process); 861 } 862 863 static void config_appkey_add_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 864 865 mesh_access_parser_state_t parser; 866 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 867 868 // netkey and appkey index 869 uint32_t netkey_and_appkey_index = mesh_access_parser_get_u24(&parser); 870 uint16_t netkey_index = netkey_and_appkey_index & 0xfff; 871 uint16_t appkey_index = netkey_and_appkey_index >> 12; 872 873 // actual key 874 uint8_t appkey[16]; 875 mesh_access_parser_get_key(&parser, appkey); 876 877 // check netkey_index is valid 878 mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 879 if (network_key == NULL){ 880 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX); 881 mesh_access_message_processed(pdu); 882 return; 883 } 884 885 // check if appkey already exists 886 mesh_transport_key_t * transport_key = mesh_transport_key_get(appkey_index); 887 if (transport_key){ 888 uint8_t status; 889 if (transport_key->netkey_index != netkey_index){ 890 // already stored but with different netkey 891 status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 892 } else if (memcmp(transport_key->key, appkey, 16) == 0){ 893 // key identical 894 status = MESH_FOUNDATION_STATUS_SUCCESS; 895 } else { 896 // key differs 897 status = MESH_FOUNDATION_STATUS_KEY_INDEX_ALREADY_STORED; 898 } 899 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, status); 900 mesh_access_message_processed(pdu); 901 return; 902 } 903 904 // create app key (first get free slot in transport key table) 905 mesh_transport_key_t * app_key = NULL; 906 uint16_t internal_index = mesh_transport_key_get_free_index(); 907 if (internal_index > 0){ 908 app_key = btstack_memory_mesh_transport_key_get(); 909 } 910 if (app_key == NULL) { 911 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES); 912 mesh_access_message_processed(pdu); 913 return; 914 } 915 916 // store data 917 app_key->internal_index = internal_index; 918 app_key->akf = 1; 919 app_key->appkey_index = appkey_index; 920 app_key->netkey_index = netkey_index; 921 app_key->version = 0; 922 app_key->old_key = 0; 923 924 memcpy(app_key->key, appkey, 16); 925 926 // calculate AID 927 access_pdu_in_process = pdu; 928 mesh_transport_key_calc_aid(&configuration_server_cmac_request, app_key, config_appkey_add_or_update_aid, app_key); 929 } 930 931 static void config_appkey_update_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 932 933 mesh_access_parser_state_t parser; 934 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 935 936 // netkey and appkey index 937 uint32_t netkey_and_appkey_index = mesh_access_parser_get_u24(&parser); 938 uint16_t netkey_index = netkey_and_appkey_index & 0xfff; 939 uint16_t appkey_index = netkey_and_appkey_index >> 12; 940 941 // actual key 942 uint8_t appkey[16]; 943 mesh_access_parser_get_key(&parser, appkey); 944 945 946 // for PTS testing 947 // check netkey_index is valid 948 mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 949 if (network_key == NULL){ 950 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX); 951 mesh_access_message_processed(pdu); 952 return; 953 } 954 955 // check if appkey already exists 956 mesh_transport_key_t * existing_app_key = mesh_transport_key_get(appkey_index); 957 if (!existing_app_key) { 958 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INVALID_APPKEY_INDEX); 959 mesh_access_message_processed(pdu); 960 return; 961 } 962 963 if (existing_app_key->netkey_index != netkey_index){ 964 // already stored but with different netkey 965 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INVALID_BINDING); 966 mesh_access_message_processed(pdu); 967 return; 968 } 969 970 if (memcmp(existing_app_key->key, appkey, 16) == 0){ 971 // key identical 972 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_SUCCESS); 973 mesh_access_message_processed(pdu); 974 return; 975 } 976 977 // create app key 978 mesh_transport_key_t * new_app_key = NULL; 979 uint16_t internal_index = mesh_transport_key_get_free_index(); 980 if (internal_index > 0){ 981 new_app_key = btstack_memory_mesh_transport_key_get(); 982 } 983 if (new_app_key == NULL) { 984 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES); 985 mesh_access_message_processed(pdu); 986 return; 987 } 988 989 // store data 990 new_app_key->internal_index = internal_index; 991 new_app_key->appkey_index = appkey_index; 992 new_app_key->netkey_index = netkey_index; 993 new_app_key->key_refresh = 1; 994 new_app_key->version = (uint8_t)(existing_app_key->version + 1); 995 memcpy(new_app_key->key, appkey, 16); 996 997 // mark old key 998 existing_app_key->old_key = 1; 999 1000 // calculate AID 1001 access_pdu_in_process = pdu; 1002 mesh_transport_key_calc_aid(&configuration_server_cmac_request, new_app_key, config_appkey_add_or_update_aid, new_app_key); 1003 } 1004 1005 static void config_appkey_delete_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1006 mesh_access_parser_state_t parser; 1007 mesh_access_parser_init(&parser, (mesh_pdu_t *) pdu); 1008 1009 // netkey and appkey index 1010 uint32_t netkey_and_appkey_index = mesh_access_parser_get_u24(&parser); 1011 uint16_t netkey_index = netkey_and_appkey_index & 0xfff; 1012 uint16_t appkey_index = netkey_and_appkey_index >> 12; 1013 1014 // check netkey_index is valid 1015 mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 1016 if (network_key == NULL){ 1017 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX); 1018 mesh_access_message_processed(pdu); 1019 return; 1020 } 1021 1022 // check if appkey already exists 1023 mesh_transport_key_t * transport_key = mesh_transport_key_get(appkey_index); 1024 if (transport_key){ 1025 mesh_configuration_server_delete_appkey(transport_key); 1026 } 1027 1028 config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_SUCCESS); 1029 mesh_access_message_processed(pdu); 1030 } 1031 1032 static void config_appkey_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1033 mesh_access_parser_state_t parser; 1034 mesh_access_parser_init(&parser, (mesh_pdu_t *) pdu); 1035 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 1036 1037 config_appkey_list(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_index); 1038 mesh_access_message_processed(pdu); 1039 } 1040 1041 // Configuration Model Subscriptions (messages) 1042 1043 static void config_model_subscription_list(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, uint16_t element_address, uint32_t model_identifier, mesh_model_t * target_model){ 1044 UNUSED(mesh_model); 1045 1046 uint16_t opcode; 1047 if (mesh_model_is_bluetooth_sig(model_identifier)){ 1048 opcode = MESH_FOUNDATION_OPERATION_SIG_MODEL_SUBSCRIPTION_LIST; 1049 } else { 1050 opcode = MESH_FOUNDATION_OPERATION_VENDOR_MODEL_SUBSCRIPTION_LIST; 1051 } 1052 1053 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(opcode); 1054 if (!transport_pdu) return; 1055 1056 // setup segmented message 1057 mesh_access_transport_add_uint8(transport_pdu, status); 1058 mesh_access_transport_add_uint16(transport_pdu, element_address); 1059 mesh_access_transport_add_model_identifier(transport_pdu, model_identifier); 1060 1061 if (target_model != NULL){ 1062 uint16_t i; 1063 for (i = 0; i < MAX_NR_MESH_SUBSCRIPTION_PER_MODEL; i++){ 1064 uint16_t address = target_model->subscriptions[i]; 1065 if (address == MESH_ADDRESS_UNSASSIGNED) continue; 1066 if (mesh_network_address_virtual(address)) { 1067 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(address); 1068 if (virtual_address == NULL) continue; 1069 address = virtual_address->hash; 1070 } 1071 mesh_access_transport_add_uint16(transport_pdu, address); 1072 } 1073 } 1074 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1075 } 1076 1077 static void config_model_subscription_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1078 mesh_access_parser_state_t parser; 1079 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1080 1081 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1082 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1083 1084 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1085 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1086 1087 config_model_subscription_list(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, model_identifier, target_model); 1088 mesh_access_message_processed(pdu); 1089 } 1090 1091 static void config_model_subscription_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, uint16_t element_address, uint16_t address, uint32_t model_identifier){ 1092 UNUSED(mesh_model); 1093 1094 // setup message 1095 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_config_model_subscription_status, 1096 status, element_address, address, model_identifier); 1097 if (!transport_pdu) return; 1098 // send as segmented access pdu 1099 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1100 } 1101 1102 static void config_model_subscription_add_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1103 mesh_access_parser_state_t parser; 1104 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1105 1106 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1107 uint16_t address = mesh_access_parser_get_u16(&parser); 1108 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1109 1110 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1111 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1112 1113 if (target_model != NULL){ 1114 if (mesh_network_address_group(address) && !mesh_network_address_all_nodes(address)){ 1115 status = mesh_model_add_subscription(target_model, address); 1116 if (status == MESH_FOUNDATION_STATUS_SUCCESS){ 1117 mesh_model_store_subscriptions(target_model); 1118 } 1119 } else { 1120 status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 1121 } 1122 } 1123 1124 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, address, model_identifier); 1125 mesh_access_message_processed(pdu); 1126 } 1127 1128 static void config_model_subscription_virtual_address_add_hash(void *arg){ 1129 mesh_model_t * target_model = (mesh_model_t*) arg; 1130 mesh_model_t * mesh_model = mesh_model_get_configuration_server(); 1131 1132 // add if not exists 1133 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_label_uuid(configuration_server_label_uuid); 1134 if (virtual_address == NULL){ 1135 virtual_address = mesh_virtual_address_register(configuration_server_label_uuid, configuration_server_hash); 1136 } 1137 1138 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1139 uint16_t hash_dst = MESH_ADDRESS_UNSASSIGNED; 1140 if (virtual_address == NULL){ 1141 status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 1142 } else { 1143 if (!mesh_model_contains_subscription(target_model, virtual_address->pseudo_dst)){ 1144 status = mesh_model_add_subscription(target_model, virtual_address->pseudo_dst); 1145 if (status == MESH_FOUNDATION_STATUS_SUCCESS){ 1146 hash_dst = virtual_address->hash; 1147 mesh_virtual_address_increase_refcount(virtual_address); 1148 mesh_model_store_subscriptions(target_model); 1149 } 1150 } 1151 } 1152 1153 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), status, configuration_server_element_address, hash_dst, target_model->model_identifier); 1154 mesh_access_message_processed(access_pdu_in_process); 1155 return; 1156 } 1157 1158 static void config_model_subscription_virtual_address_add_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1159 mesh_access_parser_state_t parser; 1160 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1161 1162 // ElementAddress - Address of the element - should be us 1163 configuration_server_element_address = mesh_access_parser_get_u16(&parser); 1164 1165 // store label uuid 1166 mesh_access_parser_get_label_uuid(&parser, configuration_server_label_uuid); 1167 1168 // Model Identifier 1169 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1170 1171 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1172 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(configuration_server_element_address, model_identifier, &status); 1173 1174 if (target_model == NULL){ 1175 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, configuration_server_element_address, MESH_ADDRESS_UNSASSIGNED, model_identifier); 1176 mesh_access_message_processed(pdu); 1177 return; 1178 } 1179 1180 access_pdu_in_process = pdu; 1181 mesh_virtual_address(&configuration_server_cmac_request, configuration_server_label_uuid, &configuration_server_hash, &config_model_subscription_virtual_address_add_hash, target_model); 1182 } 1183 1184 static void config_model_subscription_overwrite_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1185 mesh_access_parser_state_t parser; 1186 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1187 1188 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1189 uint16_t address = mesh_access_parser_get_u16(&parser); 1190 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1191 1192 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1193 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1194 1195 if (target_model != NULL){ 1196 if (mesh_network_address_group(address) && !mesh_network_address_all_nodes(address)){ 1197 mesh_subcription_decrease_virtual_address_ref_count(target_model); 1198 mesh_model_delete_all_subscriptions(target_model); 1199 mesh_model_add_subscription(target_model, address); 1200 mesh_model_store_subscriptions(target_model); 1201 } else { 1202 status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 1203 } 1204 } 1205 1206 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, address, model_identifier); 1207 mesh_access_message_processed(pdu); 1208 } 1209 1210 static void config_model_subscription_virtual_address_overwrite_hash(void *arg){ 1211 mesh_model_t * target_model = (mesh_model_t*) arg; 1212 mesh_model_t * mesh_model = mesh_model_get_configuration_server(); 1213 1214 // add if not exists 1215 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_label_uuid(configuration_server_label_uuid); 1216 if (virtual_address == NULL){ 1217 virtual_address = mesh_virtual_address_register(configuration_server_label_uuid, configuration_server_hash); 1218 } 1219 1220 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1221 uint16_t address = MESH_ADDRESS_UNSASSIGNED;; 1222 if (virtual_address == NULL){ 1223 status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 1224 } else { 1225 address = configuration_server_hash; 1226 1227 // increase refcount first to avoid flash delete + add in a row 1228 mesh_virtual_address_increase_refcount(virtual_address); 1229 1230 // decrease ref counts for virtual addresses in subscription 1231 mesh_subcription_decrease_virtual_address_ref_count(target_model); 1232 1233 // clear subscriptions 1234 mesh_model_delete_all_subscriptions(target_model); 1235 1236 // add new subscription (successfull if MAX_NR_MESH_SUBSCRIPTION_PER_MODEL > 0) 1237 mesh_model_add_subscription(target_model, virtual_address->pseudo_dst); 1238 1239 mesh_model_store_subscriptions(target_model); 1240 } 1241 1242 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), status, configuration_server_element_address, address, target_model->model_identifier); 1243 mesh_access_message_processed(access_pdu_in_process); 1244 return; 1245 } 1246 1247 static void config_model_subscription_virtual_address_overwrite_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1248 mesh_access_parser_state_t parser; 1249 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1250 1251 // ElementAddress - Address of the element - should be us 1252 configuration_server_element_address = mesh_access_parser_get_u16(&parser); 1253 1254 // store label uuid 1255 mesh_access_parser_get_label_uuid(&parser, configuration_server_label_uuid); 1256 1257 // Model Identifier 1258 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1259 1260 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1261 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(configuration_server_element_address, model_identifier, &status); 1262 1263 if (target_model == NULL){ 1264 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, configuration_server_element_address, MESH_ADDRESS_UNSASSIGNED, model_identifier); 1265 mesh_access_message_processed(pdu); 1266 return; 1267 } 1268 access_pdu_in_process = pdu; 1269 mesh_virtual_address(&configuration_server_cmac_request, configuration_server_label_uuid, &configuration_server_hash, &config_model_subscription_virtual_address_overwrite_hash, target_model); 1270 } 1271 1272 static void config_model_subscription_delete_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1273 mesh_access_parser_state_t parser; 1274 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1275 1276 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1277 uint16_t address = mesh_access_parser_get_u16(&parser); 1278 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1279 1280 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1281 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1282 1283 if (target_model != NULL){ 1284 if (mesh_network_address_group(address) && !mesh_network_address_all_nodes(address)){ 1285 mesh_model_delete_subscription(target_model, address); 1286 mesh_model_store_subscriptions(target_model); 1287 } else { 1288 status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 1289 } 1290 } 1291 1292 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, address, model_identifier); 1293 mesh_access_message_processed(pdu); 1294 } 1295 1296 static void config_model_subscription_virtual_address_delete_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1297 mesh_access_parser_state_t parser; 1298 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1299 1300 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1301 mesh_access_parser_get_label_uuid(&parser, configuration_server_label_uuid); 1302 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1303 1304 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_label_uuid(configuration_server_label_uuid); 1305 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1306 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1307 uint16_t address = MESH_ADDRESS_UNSASSIGNED; 1308 1309 if ((target_model != NULL) && (virtual_address != NULL) && mesh_model_contains_subscription(target_model, virtual_address->pseudo_dst)){ 1310 address = virtual_address->hash; 1311 mesh_model_delete_subscription(target_model, virtual_address->pseudo_dst); 1312 mesh_model_store_subscriptions(target_model); 1313 mesh_virtual_address_decrease_refcount(virtual_address); 1314 } 1315 1316 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address,address, model_identifier); 1317 mesh_access_message_processed(pdu); 1318 } 1319 1320 static void config_model_subscription_delete_all_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1321 mesh_access_parser_state_t parser; 1322 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1323 1324 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1325 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1326 1327 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1328 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1329 1330 if (target_model != NULL){ 1331 mesh_subcription_decrease_virtual_address_ref_count(target_model); 1332 mesh_model_delete_all_subscriptions(target_model); 1333 mesh_model_store_subscriptions(target_model); 1334 } 1335 1336 config_model_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, MESH_ADDRESS_UNSASSIGNED, model_identifier); 1337 mesh_access_message_processed(pdu); 1338 } 1339 1340 // Configuration Model to AppKey List 1341 1342 static void config_model_app_status(mesh_model_t * mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, uint16_t element_address, uint16_t appkey_index, uint32_t model_identifier){ 1343 UNUSED(mesh_model); 1344 1345 // setup message 1346 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_config_model_app_status, 1347 status, element_address, appkey_index, model_identifier); 1348 if (!transport_pdu) return; 1349 1350 // send as segmented access pdu 1351 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1352 } 1353 1354 static void config_model_app_list(mesh_model_t * config_server_model, uint16_t netkey_index, uint16_t dest, uint8_t status, uint16_t element_address, uint32_t model_identifier, mesh_model_t * mesh_model){ 1355 UNUSED(config_server_model); 1356 1357 uint16_t opcode; 1358 if (mesh_model_is_bluetooth_sig(model_identifier)){ 1359 opcode = MESH_FOUNDATION_OPERATION_SIG_MODEL_APP_LIST; 1360 } else { 1361 opcode = MESH_FOUNDATION_OPERATION_VENDOR_MODEL_APP_LIST; 1362 } 1363 mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(opcode); 1364 if (!transport_pdu) return; 1365 1366 mesh_access_transport_add_uint8(transport_pdu, status); 1367 mesh_access_transport_add_uint16(transport_pdu, element_address); 1368 if (mesh_model_is_bluetooth_sig(model_identifier)) { 1369 mesh_access_transport_add_uint16(transport_pdu, mesh_model_get_model_id(model_identifier)); 1370 } else { 1371 mesh_access_transport_add_uint32(transport_pdu, model_identifier); 1372 } 1373 1374 // add list of appkey indexes 1375 if (mesh_model){ 1376 uint16_t i; 1377 for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){ 1378 uint16_t appkey_index = mesh_model->appkey_indices[i]; 1379 if (appkey_index == MESH_APPKEY_INVALID) continue; 1380 mesh_access_transport_add_uint16(transport_pdu, appkey_index); 1381 } 1382 } 1383 1384 // send as segmented access pdu 1385 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1386 } 1387 1388 static void config_model_app_bind_handler(mesh_model_t *config_server_model, mesh_pdu_t * pdu) { 1389 mesh_access_parser_state_t parser; 1390 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1391 1392 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1393 uint16_t appkey_index = mesh_access_parser_get_u16(&parser); 1394 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1395 1396 uint8_t status; 1397 do { 1398 // validate address and look up model 1399 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1400 if (target_model == NULL) break; 1401 1402 // validate app key exists 1403 mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); 1404 if (!app_key){ 1405 status = MESH_FOUNDATION_STATUS_INVALID_APPKEY_INDEX; 1406 break; 1407 } 1408 1409 // Configuration Server only allows device keys 1410 if (mesh_model_is_configuration_server(model_identifier)){ 1411 status = MESH_FOUNDATION_STATUS_CANNOT_BIND; 1412 break; 1413 } 1414 status = mesh_model_bind_appkey(target_model, appkey_index); 1415 1416 } while (0); 1417 1418 config_model_app_status(config_server_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, appkey_index, model_identifier); 1419 mesh_access_message_processed(pdu); 1420 } 1421 1422 static void config_model_app_unbind_handler(mesh_model_t *config_server_model, mesh_pdu_t * pdu) { 1423 mesh_access_parser_state_t parser; 1424 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1425 1426 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1427 uint16_t appkey_index = mesh_access_parser_get_u16(&parser); 1428 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1429 1430 uint8_t status; 1431 do { 1432 // validate address and look up model 1433 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1434 if (target_model == NULL) break; 1435 1436 // validate app key exists 1437 mesh_transport_key_t * app_key = mesh_transport_key_get(appkey_index); 1438 if (!app_key){ 1439 status = MESH_FOUNDATION_STATUS_INVALID_APPKEY_INDEX; 1440 break; 1441 } 1442 1443 // stop publishing 1444 mesh_configuration_server_stop_publishing_using_appkey(target_model, appkey_index); 1445 1446 // unbind appkey 1447 mesh_model_unbind_appkey(target_model, appkey_index); 1448 1449 status = MESH_FOUNDATION_STATUS_SUCCESS; 1450 } while (0); 1451 1452 config_model_app_status(config_server_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, appkey_index, model_identifier); 1453 mesh_access_message_processed(pdu); 1454 } 1455 1456 static void config_model_app_get_handler(mesh_model_t *config_server_model, mesh_pdu_t * pdu){ 1457 mesh_access_parser_state_t parser; 1458 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1459 1460 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1461 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1462 1463 uint8_t status; 1464 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1465 config_model_app_list(config_server_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, model_identifier, target_model); 1466 mesh_access_message_processed(pdu); 1467 } 1468 1469 // Model Publication 1470 static void config_model_publication_changed(mesh_model_t *mesh_model, mesh_publication_model_t * new_publication_model){ 1471 1472 // stop publication 1473 mesh_model_publication_stop(mesh_model); 1474 1475 if (new_publication_model->address == MESH_ADDRESS_UNSASSIGNED) { 1476 // "When the PublishAddress is set to the unassigned address, the values of the AppKeyIndex, CredentialFlag, PublishTTL, PublishPeriod, PublishRetransmitCount, and PublishRetransmitIntervalSteps fields shall be set to 0x00. 1477 mesh_model->publication_model->address = MESH_ADDRESS_UNSASSIGNED; 1478 mesh_model->publication_model->appkey_index = 0; 1479 mesh_model->publication_model->friendship_credential_flag = 0; 1480 mesh_model->publication_model->period = 0; 1481 mesh_model->publication_model->ttl = 0; 1482 mesh_model->publication_model->retransmit = 0; 1483 } else { 1484 // update model publication state 1485 mesh_model->publication_model->address = new_publication_model->address; 1486 mesh_model->publication_model->appkey_index = new_publication_model->appkey_index; 1487 mesh_model->publication_model->friendship_credential_flag = new_publication_model->friendship_credential_flag; 1488 mesh_model->publication_model->period = new_publication_model->period; 1489 mesh_model->publication_model->ttl = new_publication_model->ttl; 1490 mesh_model->publication_model->retransmit = new_publication_model->retransmit; 1491 } 1492 1493 // store 1494 mesh_model_store_publication(mesh_model); 1495 1496 // start publication if address is set (nothing happens if period = 0 and retransmit = 0) 1497 if (new_publication_model->address == MESH_ADDRESS_UNSASSIGNED) return; 1498 1499 // start to publish 1500 mesh_model_publication_start(mesh_model); 1501 } 1502 1503 1504 static void 1505 config_model_publication_status(mesh_model_t *mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, 1506 uint16_t element_address, uint32_t model_identifier, mesh_publication_model_t *publication_model) { 1507 UNUSED(mesh_model); 1508 1509 // setup message 1510 uint16_t app_key_index_and_credential_flag = 0; 1511 uint16_t publish_address = 0; 1512 uint8_t ttl = 0; 1513 uint8_t period = 0; 1514 uint8_t retransmit = 0; 1515 if (status == MESH_FOUNDATION_STATUS_SUCCESS){ 1516 app_key_index_and_credential_flag = (publication_model->friendship_credential_flag << 12) | publication_model->appkey_index; 1517 ttl = publication_model->ttl; 1518 period = publication_model->period; 1519 retransmit = publication_model->retransmit; 1520 1521 publish_address = publication_model->address; 1522 if (mesh_network_address_virtual(publish_address)){ 1523 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(publish_address); 1524 if (virtual_address){ 1525 publish_address = virtual_address->hash; 1526 } 1527 } 1528 } 1529 1530 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1531 &mesh_foundation_config_model_publication_status, status, element_address, publish_address, 1532 app_key_index_and_credential_flag, ttl, period, retransmit, model_identifier); 1533 if (!transport_pdu) return; 1534 1535 // send as segmented access pdu 1536 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1537 } 1538 1539 static void 1540 config_model_publication_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1541 1542 // set defaults 1543 memset(&configuration_server_publication_model, 0, sizeof(mesh_publication_model_t)); 1544 1545 mesh_access_parser_state_t parser; 1546 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1547 1548 // ElementAddress 1549 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1550 1551 // PublishAddress, 16 bit 1552 configuration_server_publication_model.address = mesh_access_parser_get_u16(&parser); 1553 1554 // AppKeyIndex (12), CredentialFlag (1), RFU (3) 1555 uint16_t temp = mesh_access_parser_get_u16(&parser); 1556 configuration_server_publication_model.appkey_index = temp & 0x0fff; 1557 configuration_server_publication_model.friendship_credential_flag = (temp >> 12) & 1; 1558 1559 // TTL 1560 configuration_server_publication_model.ttl = mesh_access_parser_get_u8(&parser); 1561 1562 // Period 1563 configuration_server_publication_model.period = mesh_access_parser_get_u8(&parser); 1564 1565 // Retransmit 1566 configuration_server_publication_model.retransmit = mesh_access_parser_get_u8(&parser); 1567 1568 // Model Identifier 1569 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1570 1571 // Get Model for Element + Model Identifier 1572 uint8_t status; 1573 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1574 1575 // Check if publicatation model struct provided 1576 if (status == MESH_FOUNDATION_STATUS_SUCCESS) { 1577 if (target_model->publication_model == NULL){ 1578 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1579 } 1580 } 1581 1582 // Check AppKey 1583 if (status == MESH_FOUNDATION_STATUS_SUCCESS){ 1584 // check if appkey already exists 1585 mesh_transport_key_t * app_key = mesh_transport_key_get(configuration_server_publication_model.appkey_index); 1586 if (app_key == NULL) { 1587 status = MESH_FOUNDATION_STATUS_INVALID_APPKEY_INDEX; 1588 } 1589 } 1590 1591 // Accept set 1592 if (status == MESH_FOUNDATION_STATUS_SUCCESS){ 1593 // decrease ref count if old virtual address 1594 if (mesh_network_address_virtual(configuration_server_publication_model.address)) { 1595 mesh_virtual_address_t * current_virtual_address = mesh_virtual_address_for_pseudo_dst(configuration_server_publication_model.address); 1596 mesh_virtual_address_decrease_refcount(current_virtual_address); 1597 } 1598 1599 // restart publication 1600 config_model_publication_changed(target_model, &configuration_server_publication_model); 1601 } 1602 1603 // send status 1604 config_model_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, model_identifier, &configuration_server_publication_model); 1605 mesh_access_message_processed(pdu); 1606 } 1607 1608 static void config_model_publication_virtual_address_set_hash(void *arg){ 1609 mesh_model_t *mesh_model = (mesh_model_t*) arg; 1610 1611 // add if not exist 1612 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_label_uuid(configuration_server_label_uuid); 1613 if (virtual_address == NULL){ 1614 virtual_address = mesh_virtual_address_register(configuration_server_label_uuid, configuration_server_hash); 1615 } 1616 1617 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1618 if (virtual_address == NULL){ 1619 status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 1620 } else { 1621 1622 // increase ref count if new virtual address 1623 mesh_virtual_address_increase_refcount(virtual_address); 1624 1625 // decrease ref count if old virtual address 1626 if (mesh_network_address_virtual(configuration_server_publication_model.address)) { 1627 mesh_virtual_address_t * current_virtual_address = mesh_virtual_address_for_pseudo_dst(configuration_server_publication_model.address); 1628 if (current_virtual_address){ 1629 mesh_virtual_address_decrease_refcount(current_virtual_address); 1630 } 1631 } 1632 1633 configuration_server_publication_model.address = virtual_address->pseudo_dst; 1634 mesh_virtual_address_increase_refcount(virtual_address); 1635 1636 // restart publication 1637 config_model_publication_changed(configuration_server_target_model, &configuration_server_publication_model); 1638 } 1639 1640 // send status 1641 config_model_publication_status(mesh_model, mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), status, configuration_server_element_address, configuration_server_model_identifier, &configuration_server_publication_model); 1642 1643 mesh_access_message_processed(access_pdu_in_process); 1644 } 1645 1646 static void 1647 config_model_publication_virtual_address_set_handler(mesh_model_t *mesh_model, 1648 mesh_pdu_t * pdu) { 1649 1650 // set defaults 1651 memset(&configuration_server_publication_model, 0, sizeof(mesh_publication_model_t)); 1652 1653 mesh_access_parser_state_t parser; 1654 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1655 1656 // ElementAddress - Address of the element 1657 configuration_server_element_address = mesh_access_parser_get_u16(&parser); 1658 1659 // store label uuid 1660 mesh_access_parser_get_label_uuid(&parser, configuration_server_label_uuid); 1661 1662 // AppKeyIndex (12), CredentialFlag (1), RFU (3) 1663 uint16_t temp = mesh_access_parser_get_u16(&parser); 1664 configuration_server_publication_model.appkey_index = temp & 0x0fff; 1665 configuration_server_publication_model.friendship_credential_flag = (temp >> 12) & 1; 1666 configuration_server_publication_model.ttl = mesh_access_parser_get_u8(&parser); 1667 configuration_server_publication_model.period = mesh_access_parser_get_u8(&parser); 1668 configuration_server_publication_model.retransmit = mesh_access_parser_get_u8(&parser); 1669 1670 // Model Identifier 1671 configuration_server_model_identifier = mesh_access_parser_get_model_identifier(&parser); 1672 1673 // Get Model for Element + Model Identifier 1674 uint8_t status; 1675 configuration_server_target_model = mesh_access_model_for_address_and_model_identifier(configuration_server_element_address, configuration_server_model_identifier, &status); 1676 1677 // Check if publicatation model struct provided 1678 if (status == MESH_FOUNDATION_STATUS_SUCCESS) { 1679 if (configuration_server_target_model->publication_model == NULL){ 1680 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1681 } 1682 } 1683 1684 // Check AppKey 1685 if (status == MESH_FOUNDATION_STATUS_SUCCESS){ 1686 // check if appkey already exists 1687 mesh_transport_key_t * app_key = mesh_transport_key_get(configuration_server_publication_model.appkey_index); 1688 if (app_key == NULL) { 1689 status = MESH_FOUNDATION_STATUS_INVALID_APPKEY_INDEX; 1690 } 1691 } 1692 1693 // on error, no need to calculate virtual address hash 1694 if (status != MESH_FOUNDATION_STATUS_SUCCESS){ 1695 config_model_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, configuration_server_element_address, configuration_server_model_identifier, &configuration_server_publication_model); 1696 mesh_access_message_processed(pdu); 1697 return; 1698 } 1699 1700 access_pdu_in_process = pdu; 1701 mesh_virtual_address(&configuration_server_cmac_request, configuration_server_label_uuid, &configuration_server_hash, &config_model_publication_virtual_address_set_hash, mesh_model); 1702 } 1703 1704 static void 1705 config_model_publication_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1706 1707 1708 mesh_access_parser_state_t parser; 1709 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1710 1711 // ElementAddress - Address of the element - should be us 1712 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1713 1714 // Model Identifier 1715 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1716 1717 // Get Model for Element + Model Identifier 1718 uint8_t status; 1719 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1720 1721 mesh_publication_model_t * publication_model; 1722 if (target_model == NULL){ 1723 // use defaults 1724 memset(&configuration_server_publication_model, 0, sizeof(mesh_publication_model_t)); 1725 publication_model = &configuration_server_publication_model; 1726 } else { 1727 publication_model = target_model->publication_model; 1728 } 1729 1730 config_model_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, model_identifier, publication_model); 1731 mesh_access_message_processed(pdu); 1732 } 1733 1734 // Heartbeat Publication 1735 1736 static void config_heartbeat_publication_emit(mesh_heartbeat_publication_t * mesh_heartbeat_publication){ 1737 1738 printf("CONFIG_SERVER_HEARTBEAT: Emit (dest %04x, count %u, period %u ms)\n", 1739 mesh_heartbeat_publication->destination, 1740 mesh_heartbeat_publication->count, 1741 mesh_heartbeat_publication->period_ms); 1742 1743 // active features 1744 mesh_heartbeat_publication->active_features = mesh_foundation_get_features(); 1745 1746 mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 1747 if (network_pdu){ 1748 uint8_t data[3]; 1749 data[0] = mesh_heartbeat_publication->ttl; 1750 big_endian_store_16(data, 1, mesh_heartbeat_publication->active_features); 1751 uint8_t status = mesh_upper_transport_setup_control_pdu((mesh_pdu_t *) network_pdu, mesh_heartbeat_publication->netkey_index, 1752 mesh_heartbeat_publication->ttl, mesh_node_get_primary_element_address(), mesh_heartbeat_publication->destination, 1753 MESH_TRANSPORT_OPCODE_HEARTBEAT, data, sizeof(data)); 1754 if (status){ 1755 // stop periodic emit on error (netkey got invalid) 1756 mesh_heartbeat_publication->period_ms = 0; 1757 mesh_network_pdu_free(network_pdu); 1758 } else { 1759 mesh_upper_transport_send_control_pdu((mesh_pdu_t *) network_pdu); 1760 } 1761 } 1762 1763 // forever 1764 if (mesh_heartbeat_publication->count > 0 && mesh_heartbeat_publication->count < 0xffffu) { 1765 mesh_heartbeat_publication->count--; 1766 } 1767 } 1768 void mesh_configuration_server_feature_changed(void){ 1769 mesh_model_t * mesh_model = mesh_model_get_configuration_server(); 1770 mesh_heartbeat_publication_t * mesh_heartbeat_publication = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_publication; 1771 1772 // filter features by observed features for heartbeats 1773 uint16_t current_features = mesh_foundation_get_features() & mesh_heartbeat_publication->features; 1774 uint16_t previous_features = mesh_heartbeat_publication->active_features & mesh_heartbeat_publication->features; 1775 1776 // changes? 1777 if (current_features == previous_features) return; 1778 1779 config_heartbeat_publication_emit(mesh_heartbeat_publication); 1780 } 1781 1782 static void config_heartbeat_publication_timeout_handler(btstack_timer_source_t * ts){ 1783 1784 mesh_heartbeat_publication_t * mesh_heartbeat_publication = (mesh_heartbeat_publication_t*) ts; 1785 mesh_heartbeat_publication->timer_active = 0; 1786 1787 // emit beat 1788 config_heartbeat_publication_emit(mesh_heartbeat_publication); 1789 1790 // all sent? 1791 if (mesh_heartbeat_publication->count == 0) return; 1792 1793 // periodic publication? 1794 if (mesh_heartbeat_publication->period_ms == 0) return; 1795 1796 btstack_run_loop_set_timer(ts, mesh_heartbeat_publication->period_ms); 1797 btstack_run_loop_add_timer(ts); 1798 mesh_heartbeat_publication->timer_active = 1; 1799 } 1800 1801 static void config_heartbeat_publication_status(mesh_model_t *mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, mesh_heartbeat_publication_t * mesh_heartbeat_publication){ 1802 UNUSED(mesh_model); 1803 1804 // setup message 1805 uint8_t count_log = heartbeat_count_log(mesh_heartbeat_publication->count); 1806 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1807 &mesh_foundation_config_heartbeat_publication_status, 1808 status, 1809 mesh_heartbeat_publication->destination, 1810 count_log, 1811 mesh_heartbeat_publication->period_log, 1812 mesh_heartbeat_publication->ttl, 1813 mesh_heartbeat_publication->features, 1814 mesh_heartbeat_publication->netkey_index); 1815 if (!transport_pdu) return; 1816 1817 printf("MESH config_heartbeat_publication_status count = %u => count_log = %u\n", mesh_heartbeat_publication->count, count_log); 1818 1819 // send as segmented access pdu 1820 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1821 } 1822 1823 static void config_heartbeat_publication_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1824 1825 mesh_heartbeat_publication_t requested_publication; 1826 memset(&requested_publication, 0, sizeof(requested_publication)); 1827 1828 mesh_access_parser_state_t parser; 1829 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1830 1831 // Destination address for Heartbeat messages 1832 requested_publication.destination = mesh_access_parser_get_u16(&parser); 1833 // Number of Heartbeat messages to be sent 1834 requested_publication.count = heartbeat_pwr2(mesh_access_parser_get_u8(&parser)); 1835 // Period for sending Heartbeat messages 1836 requested_publication.period_log = mesh_access_parser_get_u8(&parser); 1837 // TTL to be used when sending Heartbeat messages 1838 requested_publication.ttl = mesh_access_parser_get_u8(&parser); 1839 // Bit field indicating features that trigger Heartbeat messages when changed 1840 requested_publication.features = mesh_access_parser_get_u16(&parser) & MESH_HEARTBEAT_FEATURES_SUPPORTED_MASK; 1841 // NetKey Index 1842 requested_publication.netkey_index = mesh_access_parser_get_u16(&parser); 1843 1844 // store period as ms 1845 requested_publication.period_ms = heartbeat_pwr2(requested_publication.period_log) * 1000; 1846 1847 // store current features 1848 requested_publication.active_features = mesh_foundation_get_features(); 1849 1850 mesh_heartbeat_publication_t * mesh_heartbeat_publication = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_publication; 1851 1852 // validate fields 1853 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1854 mesh_network_key_t * network_key = mesh_network_key_list_get(requested_publication.netkey_index); 1855 if (network_key == NULL){ 1856 status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 1857 } else { 1858 printf("MESH config_heartbeat_publication_set, destination %x, count = %x, period = %u s\n", 1859 requested_publication.destination, requested_publication.count, requested_publication.period_ms); 1860 1861 // stop timer if active 1862 // note: accept update below using memcpy overwwrite timer_active flag 1863 if (mesh_heartbeat_publication->timer_active){ 1864 btstack_run_loop_remove_timer(&mesh_heartbeat_publication->timer); 1865 mesh_heartbeat_publication->timer_active = 0; 1866 } 1867 1868 // accept update 1869 memcpy(mesh_heartbeat_publication, &requested_publication, sizeof(mesh_heartbeat_publication_t)); 1870 } 1871 1872 config_heartbeat_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, mesh_heartbeat_publication); 1873 1874 mesh_access_message_processed(pdu); 1875 1876 if (status != MESH_FOUNDATION_STATUS_SUCCESS) return; 1877 1878 // check if heartbeats should be disabled 1879 if (mesh_heartbeat_publication->destination == MESH_ADDRESS_UNSASSIGNED || mesh_heartbeat_publication->period_log == 0) { 1880 return; 1881 } 1882 1883 // initial heartbeat after 2000 ms 1884 btstack_run_loop_set_timer_handler(&mesh_heartbeat_publication->timer, config_heartbeat_publication_timeout_handler); 1885 btstack_run_loop_set_timer_context(&mesh_heartbeat_publication->timer, mesh_heartbeat_publication); 1886 btstack_run_loop_set_timer(&mesh_heartbeat_publication->timer, 2000); 1887 btstack_run_loop_add_timer(&mesh_heartbeat_publication->timer); 1888 mesh_heartbeat_publication->timer_active = 1; 1889 } 1890 1891 static void config_heartbeat_publication_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1892 mesh_heartbeat_publication_t * mesh_heartbeat_publication = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_publication; 1893 config_heartbeat_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_SUCCESS, mesh_heartbeat_publication); 1894 mesh_access_message_processed(pdu); 1895 } 1896 1897 // Heartbeat Subscription 1898 1899 static void config_heartbeat_subscription_status(mesh_model_t *mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, mesh_heartbeat_subscription_t * mesh_heartbeat_subscription){ 1900 UNUSED(mesh_model); 1901 1902 // setup message 1903 uint8_t count_log = heartbeat_count_log(mesh_heartbeat_subscription->count); 1904 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1905 &mesh_foundation_config_heartbeat_subscription_status, 1906 status, 1907 mesh_heartbeat_subscription->source, 1908 mesh_heartbeat_subscription->destination, 1909 mesh_heartbeat_subscription->period_log, 1910 count_log, 1911 mesh_heartbeat_subscription->min_hops, 1912 mesh_heartbeat_subscription->max_hops); 1913 if (!transport_pdu) return; 1914 printf("MESH config_heartbeat_subscription_status count = %u => count_log = %u\n", mesh_heartbeat_subscription->count, count_log); 1915 1916 // send as segmented access pdu 1917 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1918 } 1919 static int config_heartbeat_subscription_enabled(mesh_heartbeat_subscription_t * mesh_heartbeat_subscription){ 1920 return mesh_network_address_unicast(mesh_heartbeat_subscription->source) && mesh_heartbeat_subscription->period_log > 0 && 1921 (mesh_network_address_unicast(mesh_heartbeat_subscription->destination) || mesh_network_address_group(mesh_heartbeat_subscription->destination)); 1922 } 1923 1924 static void config_heartbeat_subscription_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1925 mesh_access_parser_state_t parser; 1926 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1927 1928 mesh_heartbeat_subscription_t requested_subscription; 1929 1930 // Destination address for Heartbeat messages 1931 requested_subscription.source = mesh_access_parser_get_u16(&parser); 1932 // Destination address for Heartbeat messages 1933 requested_subscription.destination = mesh_access_parser_get_u16(&parser); 1934 // Period for sending Heartbeat messages 1935 requested_subscription.period_log = mesh_access_parser_get_u8(&parser); 1936 1937 1938 // Hearbeat Subscription Soure must be unassigned or a unicast address 1939 int source_address_valid = 1940 (requested_subscription.source == MESH_ADDRESS_UNSASSIGNED) || 1941 (mesh_network_address_unicast(requested_subscription.source)); 1942 1943 // Heartbeat Subscription Destination must be unassigned, unicast (== our primary address), or a group address) 1944 int destination_address_valid = 1945 (requested_subscription.destination == MESH_ADDRESS_UNSASSIGNED) || 1946 (mesh_network_address_unicast(requested_subscription.destination) && requested_subscription.destination == mesh_node_get_primary_element_address()) || 1947 (mesh_network_address_group(requested_subscription.destination)); 1948 1949 if (!source_address_valid || !destination_address_valid){ 1950 printf("MESH config_heartbeat_subscription_set, source %x or destination %x invalid\n", requested_subscription.source, requested_subscription.destination); 1951 mesh_access_message_processed(pdu); 1952 return; 1953 } 1954 1955 int subscription_enabled = config_heartbeat_subscription_enabled(&requested_subscription); 1956 printf("MESH config_heartbeat_subscription_set, source %x destination %x, period = %u s => enabled %u \n", requested_subscription.source, 1957 requested_subscription.destination, heartbeat_pwr2(requested_subscription.period_log), subscription_enabled); 1958 1959 // ignore messages 1960 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1961 if (requested_subscription.period_log > 0x11u){ 1962 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1963 } else if ((requested_subscription.destination != MESH_ADDRESS_UNSASSIGNED) && 1964 !mesh_network_address_unicast(requested_subscription.destination) && 1965 !mesh_network_address_group(requested_subscription.destination)){ 1966 status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 1967 } 1968 1969 if (status != MESH_FOUNDATION_STATUS_SUCCESS){ 1970 config_heartbeat_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, &requested_subscription); 1971 mesh_access_message_processed(pdu); 1972 return; 1973 } 1974 1975 mesh_heartbeat_subscription_t * mesh_heartbeat_subscription = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_subscription; 1976 1977 if (config_heartbeat_subscription_enabled(&requested_subscription)){ 1978 mesh_heartbeat_subscription->source = requested_subscription.source; 1979 mesh_heartbeat_subscription->destination = requested_subscription.destination; 1980 mesh_heartbeat_subscription->period_log = requested_subscription.period_log; 1981 mesh_heartbeat_subscription->count = 0; 1982 mesh_heartbeat_subscription->min_hops = 0x7Fu; 1983 mesh_heartbeat_subscription->max_hops = 0u; 1984 mesh_heartbeat_subscription->period_start_ms = btstack_run_loop_get_time_ms(); 1985 } else { 1986 #if 0 1987 // code according to Mesh Spec v1.0.1 1988 // "When an element receives a Config Heartbeat Subscription Set message, it shall ... respond with a Config Heartbeat Subscription Status message, setting ... 1989 // If the Source or the Destination field is set to the unassigned address, or the PeriodLog field is set to 0x00, [then] 1990 // - the processing of received Heartbeat messages shall be disabled, 1991 // - the Heartbeat Subscription Source state shall be set to the unassigned address, 1992 // - the Heartbeat Subscription Destination state shall be set to the unassigned address, 1993 // - the Heartbeat Subscription MinHops state shall be unchanged, 1994 // - the Heartbeat Subscription MaxHops state shall be unchanged, 1995 // - and the Heartbeat Subscription Count state shall be unchanged." 1996 // If period_log == 0, then set src + dest to unassigned. If src or dest are unsigned, get triggers status mit count_log == 0 1997 mesh_heartbeat_subscription->source = MESH_ADDRESS_UNSASSIGNED; 1998 mesh_heartbeat_subscription->destination = MESH_ADDRESS_UNSASSIGNED; 1999 #else 2000 // code to satisfy MESH/NODE/CFG/HBS/BV-02-C from PTS 7.4.1 / Mesh TS 1.0.2 2001 if (requested_subscription.source == MESH_ADDRESS_UNSASSIGNED || requested_subscription.destination == MESH_ADDRESS_UNSASSIGNED){ 2002 mesh_heartbeat_subscription->source = MESH_ADDRESS_UNSASSIGNED; 2003 mesh_heartbeat_subscription->destination = MESH_ADDRESS_UNSASSIGNED; 2004 } 2005 #endif 2006 mesh_heartbeat_subscription->period_log = 0u; 2007 } 2008 2009 config_heartbeat_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, mesh_heartbeat_subscription); 2010 mesh_access_message_processed(pdu); 2011 } 2012 2013 static uint32_t config_heartbeat_subscription_get_period_remaining_s(mesh_heartbeat_subscription_t * heartbeat_subscription){ 2014 // calculate period_log 2015 int32_t time_since_start_s = btstack_time_delta(btstack_run_loop_get_time_ms(), heartbeat_subscription->period_start_ms) / 1000; 2016 int32_t period_s = heartbeat_pwr2(heartbeat_subscription->period_log); 2017 uint32_t period_remaining_s = 0; 2018 if (time_since_start_s < period_s){ 2019 period_remaining_s = period_s - time_since_start_s; 2020 } 2021 printf("Heartbeat: time since start %d s, period %u s, period remaining %u s\n", time_since_start_s, period_s, period_remaining_s); 2022 return period_remaining_s; 2023 } 2024 2025 static void config_heartbeat_subscription_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 2026 mesh_heartbeat_subscription_t * mesh_heartbeat_subscription = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_subscription; 2027 mesh_heartbeat_subscription_t subscription; 2028 memcpy(&subscription, mesh_heartbeat_subscription, sizeof(subscription)); 2029 if (mesh_heartbeat_subscription->source == MESH_ADDRESS_UNSASSIGNED || mesh_heartbeat_subscription->destination == MESH_ADDRESS_UNSASSIGNED){ 2030 memset(&subscription, 0, sizeof(subscription)); 2031 } else { 2032 // calculate period_log 2033 uint32_t period_remaining_s = config_heartbeat_subscription_get_period_remaining_s(&subscription); 2034 subscription.period_log = heartbeat_period_log(period_remaining_s); 2035 } 2036 config_heartbeat_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_SUCCESS, &subscription); 2037 mesh_access_message_processed(pdu); 2038 } 2039 2040 // KeyRefresh Phase 2041 2042 static void config_key_refresh_phase_status(mesh_model_t *mesh_model, uint16_t netkey_index_dest, uint16_t dest, uint8_t status, uint16_t netkey_index, 2043 mesh_key_refresh_state_t key_refresh_state){ 2044 UNUSED(mesh_model); 2045 2046 // setup message 2047 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 2048 &mesh_key_refresh_phase_status, 2049 status, 2050 netkey_index, 2051 key_refresh_state); 2052 if (!transport_pdu) return; 2053 2054 // send as segmented access pdu 2055 config_server_send_message(netkey_index_dest, dest, (mesh_pdu_t *) transport_pdu); 2056 } 2057 2058 static void config_key_refresh_phase_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2059 mesh_access_parser_state_t parser; 2060 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2061 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 2062 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 2063 2064 uint8_t status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 2065 mesh_key_refresh_state_t key_refresh_state = MESH_KEY_REFRESH_NOT_ACTIVE; 2066 2067 if (subnet != NULL){ 2068 status = MESH_FOUNDATION_STATUS_SUCCESS; 2069 key_refresh_state = subnet->key_refresh; 2070 } 2071 2072 config_key_refresh_phase_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, key_refresh_state); 2073 mesh_access_message_processed(pdu); 2074 } 2075 2076 static void config_key_refresh_phase_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2077 mesh_access_parser_state_t parser; 2078 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2079 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 2080 uint8_t key_refresh_phase_transition = mesh_access_parser_get_u8(&parser); 2081 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 2082 2083 uint8_t status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 2084 2085 if (subnet != NULL){ 2086 status = MESH_FOUNDATION_STATUS_SUCCESS; 2087 2088 switch (key_refresh_phase_transition){ 2089 case 0x02: 2090 switch (subnet->key_refresh){ 2091 case MESH_KEY_REFRESH_FIRST_PHASE: 2092 case MESH_KEY_REFRESH_SECOND_PHASE: 2093 subnet->key_refresh = MESH_KEY_REFRESH_SECOND_PHASE; 2094 break; 2095 default: 2096 break; 2097 } 2098 break; 2099 case 0x03: 2100 switch (subnet->key_refresh){ 2101 case MESH_KEY_REFRESH_FIRST_PHASE: 2102 case MESH_KEY_REFRESH_SECOND_PHASE: 2103 // key refresh phase 3 entered 2104 mesh_access_key_refresh_revoke_keys(subnet); 2105 subnet->key_refresh = MESH_KEY_REFRESH_NOT_ACTIVE; 2106 break; 2107 default: 2108 break; 2109 } 2110 break; 2111 default: 2112 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 2113 break; 2114 } 2115 } 2116 2117 config_key_refresh_phase_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, subnet->key_refresh); 2118 mesh_access_message_processed(pdu); 2119 } 2120 2121 2122 static void config_node_reset_status(mesh_model_t *mesh_model, uint16_t netkey_index, uint16_t dest){ 2123 UNUSED(mesh_model); 2124 2125 // setup message 2126 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_node_reset_status); 2127 if (!transport_pdu) return; 2128 2129 // send as segmented access pdu 2130 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 2131 } 2132 2133 static void config_node_reset_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2134 mesh_node_reset(); 2135 config_node_reset_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 2136 mesh_access_message_processed(pdu); 2137 } 2138 2139 static void low_power_node_poll_timeout_status(mesh_model_t *mesh_model, uint16_t netkey_index_dest, uint16_t dest, uint8_t status){ 2140 UNUSED(mesh_model); 2141 2142 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 2143 &mesh_foundation_low_power_node_poll_timeout_status, 2144 status, 2145 0, // The unicast address of the Low Power node 2146 0); // The current value of the PollTimeout timer of the Low Power node 2147 if (!transport_pdu) return; 2148 printf("TODO: send unicast address of the Low Power node and the current value of the PollTimeout timer, instead of 0s\n"); 2149 // send as segmented access pdu 2150 config_server_send_message(netkey_index_dest, dest, (mesh_pdu_t *) transport_pdu); 2151 } 2152 2153 static void config_low_power_node_poll_timeout_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2154 mesh_access_parser_state_t parser; 2155 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2156 printf("TODO: implement get the current value of PollTimeout timer of the Low Power node within a Friend node\n"); 2157 low_power_node_poll_timeout_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_SUCCESS); 2158 2159 mesh_access_message_processed(pdu); 2160 } 2161 2162 static void config_node_identity_status(mesh_model_t *mesh_model, uint16_t netkey_index_dest, uint16_t dest, uint8_t status, uint16_t netkey_index, 2163 mesh_node_identity_state_t node_identity_state){ 2164 UNUSED(mesh_model); 2165 2166 // setup message 2167 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 2168 &mesh_foundation_node_identity_status, 2169 status, 2170 netkey_index, 2171 node_identity_state); 2172 if (!transport_pdu) return; 2173 2174 // send as segmented access pdu 2175 config_server_send_message(netkey_index_dest, dest, (mesh_pdu_t *) transport_pdu); 2176 } 2177 2178 static void config_node_identity_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2179 mesh_access_parser_state_t parser; 2180 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2181 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 2182 2183 mesh_node_identity_state_t node_identity_state = MESH_NODE_IDENTITY_STATE_ADVERTISING_NOT_SUPPORTED; 2184 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 2185 #ifdef ENABLE_MESH_PROXY_SERVER 2186 status = mesh_proxy_get_advertising_with_node_id_status(netkey_index, &node_identity_state); 2187 #endif 2188 config_node_identity_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, node_identity_state); 2189 2190 mesh_access_message_processed(pdu); 2191 } 2192 2193 static void config_node_identity_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2194 mesh_access_parser_state_t parser; 2195 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2196 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 2197 mesh_node_identity_state_t node_identity_state = (mesh_node_identity_state_t) mesh_access_parser_get_u8(&parser); 2198 2199 // ignore invalid state 2200 if (node_identity_state >= MESH_NODE_IDENTITY_STATE_ADVERTISING_NOT_SUPPORTED) { 2201 mesh_access_message_processed(pdu); 2202 return; 2203 } 2204 2205 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 2206 #ifdef ENABLE_MESH_PROXY_SERVER 2207 status = mesh_proxy_set_advertising_with_node_id(netkey_index, node_identity_state); 2208 #else 2209 mesh_subnet_t * network_key = mesh_subnet_get_by_netkey_index(netkey_index); 2210 if (network_key == NULL){ 2211 status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 2212 } 2213 #endif 2214 2215 config_node_identity_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, node_identity_state); 2216 2217 mesh_access_message_processed(pdu); 2218 } 2219 2220 // 2221 2222 const static mesh_operation_t mesh_configuration_server_model_operations[] = { 2223 { MESH_FOUNDATION_OPERATION_APPKEY_ADD, 19, config_appkey_add_handler }, 2224 { MESH_FOUNDATION_OPERATION_APPKEY_DELETE, 3, config_appkey_delete_handler }, 2225 { MESH_FOUNDATION_OPERATION_APPKEY_GET, 2, config_appkey_get_handler }, 2226 { MESH_FOUNDATION_OPERATION_APPKEY_UPDATE, 19, config_appkey_update_handler }, 2227 { MESH_FOUNDATION_OPERATION_NETKEY_ADD, 18, config_netkey_add_handler }, 2228 { MESH_FOUNDATION_OPERATION_NETKEY_UPDATE, 18, config_netkey_update_handler }, 2229 { MESH_FOUNDATION_OPERATION_NETKEY_DELETE, 2, config_netkey_delete_handler }, 2230 { MESH_FOUNDATION_OPERATION_NETKEY_GET, 0, config_netkey_get_handler }, 2231 { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET, 1, config_composition_data_get_handler }, 2232 { MESH_FOUNDATION_OPERATION_BEACON_GET, 0, config_beacon_get_handler }, 2233 { MESH_FOUNDATION_OPERATION_BEACON_SET, 1, config_beacon_set_handler }, 2234 { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET, 0, config_default_ttl_get_handler }, 2235 { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET, 1, config_default_ttl_set_handler }, 2236 { MESH_FOUNDATION_OPERATION_FRIEND_GET, 0, config_friend_get_handler }, 2237 { MESH_FOUNDATION_OPERATION_FRIEND_SET, 1, config_friend_set_handler }, 2238 { MESH_FOUNDATION_OPERATION_NETWORK_TRANSMIT_GET, 0, config_model_network_transmit_get_handler }, 2239 { MESH_FOUNDATION_OPERATION_NETWORK_TRANSMIT_SET, 1, config_model_network_transmit_set_handler }, 2240 { MESH_FOUNDATION_OPERATION_GATT_PROXY_GET, 0, config_gatt_proxy_get_handler }, 2241 { MESH_FOUNDATION_OPERATION_GATT_PROXY_SET, 1, config_gatt_proxy_set_handler }, 2242 { MESH_FOUNDATION_OPERATION_RELAY_GET, 0, config_relay_get_handler }, 2243 { MESH_FOUNDATION_OPERATION_RELAY_SET, 1, config_relay_set_handler }, 2244 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_ADD, 6, config_model_subscription_add_handler }, 2245 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_ADD, 20, config_model_subscription_virtual_address_add_handler }, 2246 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_DELETE, 6, config_model_subscription_delete_handler }, 2247 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_DELETE, 20, config_model_subscription_virtual_address_delete_handler }, 2248 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_OVERWRITE, 6, config_model_subscription_overwrite_handler }, 2249 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_OVERWRITE,20, config_model_subscription_virtual_address_overwrite_handler }, 2250 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_DELETE_ALL, 4, config_model_subscription_delete_all_handler }, 2251 { MESH_FOUNDATION_OPERATION_SIG_MODEL_SUBSCRIPTION_GET, 4, config_model_subscription_get_handler }, 2252 { MESH_FOUNDATION_OPERATION_VENDOR_MODEL_SUBSCRIPTION_GET, 6, config_model_subscription_get_handler }, 2253 { MESH_FOUNDATION_OPERATION_SIG_MODEL_APP_GET, 4, config_model_app_get_handler }, 2254 { MESH_FOUNDATION_OPERATION_VENDOR_MODEL_APP_GET, 6, config_model_app_get_handler }, 2255 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, 11, config_model_publication_set_handler }, 2256 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, 25, config_model_publication_virtual_address_set_handler }, 2257 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, 4, config_model_publication_get_handler }, 2258 { MESH_FOUNDATION_OPERATION_MODEL_APP_BIND, 6, config_model_app_bind_handler }, 2259 { MESH_FOUNDATION_OPERATION_MODEL_APP_UNBIND, 6, config_model_app_unbind_handler }, 2260 { MESH_FOUNDATION_OPERATION_HEARTBEAT_PUBLICATION_GET, 0, config_heartbeat_publication_get_handler }, 2261 { MESH_FOUNDATION_OPERATION_HEARTBEAT_PUBLICATION_SET, 9, config_heartbeat_publication_set_handler }, 2262 { MESH_FOUNDATION_OPERATION_HEARTBEAT_SUBSCRIPTION_GET, 0, config_heartbeat_subscription_get_handler}, 2263 { MESH_FOUNDATION_OPERATION_HEARTBEAT_SUBSCRIPTION_SET, 5, config_heartbeat_subscription_set_handler}, 2264 { MESH_FOUNDATION_OPERATION_KEY_REFRESH_PHASE_GET, 2, config_key_refresh_phase_get_handler }, 2265 { MESH_FOUNDATION_OPERATION_KEY_REFRESH_PHASE_SET, 3, config_key_refresh_phase_set_handler }, 2266 { MESH_FOUNDATION_OPERATION_NODE_RESET, 0, config_node_reset_handler }, 2267 { MESH_FOUNDATION_OPERATION_LOW_POWER_NODE_POLL_TIMEOUT_GET, 2, config_low_power_node_poll_timeout_get_handler }, 2268 { MESH_FOUNDATION_OPERATION_NODE_IDENTITY_GET, 2, config_node_identity_get_handler }, 2269 { MESH_FOUNDATION_OPERATION_NODE_IDENTITY_SET, 3, config_node_identity_set_handler }, 2270 { 0, 0, NULL } 2271 }; 2272 2273 const mesh_operation_t * mesh_configuration_server_get_operations(void){ 2274 return mesh_configuration_server_model_operations; 2275 } 2276 2277 void mesh_configuration_server_process_heartbeat(mesh_model_t * configuration_server_model, uint16_t src, uint16_t dest, uint8_t hops, uint16_t features){ 2278 UNUSED(features); 2279 mesh_heartbeat_subscription_t * mesh_heartbeat_subscription = &((mesh_configuration_server_model_context_t*) configuration_server_model->model_data)->heartbeat_subscription; 2280 if (config_heartbeat_subscription_get_period_remaining_s(mesh_heartbeat_subscription) == 0) return; 2281 if (mesh_heartbeat_subscription->source != src) return; 2282 if (mesh_heartbeat_subscription->destination != dest) return; 2283 // update count 2284 if (mesh_heartbeat_subscription->count != 0xffff){ 2285 mesh_heartbeat_subscription->count++; 2286 } 2287 // update min/max hops 2288 mesh_heartbeat_subscription->min_hops = (uint8_t) btstack_min(mesh_heartbeat_subscription->min_hops, hops); 2289 mesh_heartbeat_subscription->max_hops = (uint8_t) btstack_max(mesh_heartbeat_subscription->max_hops, hops); 2290 2291 printf("HEARTBEAT, count %u, min %u, max %u hops\n", mesh_heartbeat_subscription->count, mesh_heartbeat_subscription->min_hops, mesh_heartbeat_subscription->max_hops); 2292 } 2293