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