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 // update model publication state 1440 memcpy(mesh_model->publication_model, new_publication_model, sizeof(mesh_publication_model_t)); 1441 1442 // store 1443 mesh_model_store_publication(mesh_model); 1444 1445 // start publication if address is set (nothing happens if period = 0 and retransmit = 0) 1446 if (new_publication_model->address == MESH_ADDRESS_UNSASSIGNED) return; 1447 1448 // start to publish 1449 mesh_model_publication_start(mesh_model); 1450 } 1451 1452 1453 static void 1454 config_model_publication_status(mesh_model_t *mesh_model, uint16_t netkey_index, uint16_t dest, uint8_t status, 1455 uint16_t element_address, uint32_t model_identifier, mesh_publication_model_t *publication_model) { 1456 UNUSED(mesh_model); 1457 1458 // setup message 1459 uint16_t app_key_index_and_credential_flag = (publication_model->friendship_credential_flag << 12) | publication_model->appkey_index; 1460 uint16_t publish_address = publication_model->address; 1461 1462 if (mesh_network_address_virtual(publish_address)){ 1463 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_pseudo_dst(publish_address); 1464 if (virtual_address){ 1465 publish_address = virtual_address->hash; 1466 } 1467 } 1468 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1469 &mesh_foundation_config_model_publication_status, status, element_address, publish_address, 1470 app_key_index_and_credential_flag, 1471 publication_model->ttl, publication_model->period, publication_model->retransmit, model_identifier); 1472 if (!transport_pdu) return; 1473 1474 // send as segmented access pdu 1475 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1476 } 1477 1478 static void 1479 config_model_publication_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1480 1481 // set defaults 1482 memset(&configuration_server_publication_model, 0, sizeof(mesh_publication_model_t)); 1483 1484 mesh_access_parser_state_t parser; 1485 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1486 1487 // ElementAddress - Address of the element - should be us 1488 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1489 1490 // PublishAddress, 16 bit 1491 configuration_server_publication_model.address = mesh_access_parser_get_u16(&parser); 1492 1493 // AppKeyIndex (12), CredentialFlag (1), RFU (3) 1494 uint16_t temp = mesh_access_parser_get_u16(&parser); 1495 configuration_server_publication_model.appkey_index = temp & 0x0fff; 1496 configuration_server_publication_model.friendship_credential_flag = (temp >> 12) & 1; 1497 1498 // TTL 1499 configuration_server_publication_model.ttl = mesh_access_parser_get_u8(&parser); 1500 1501 // Period 1502 configuration_server_publication_model.period = mesh_access_parser_get_u8(&parser); 1503 1504 // Retransmit 1505 configuration_server_publication_model.retransmit = mesh_access_parser_get_u8(&parser); 1506 1507 // Model Identifier 1508 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1509 uint8_t status; 1510 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1511 1512 // TODO validate params 1513 1514 if (target_model){ 1515 if (target_model->publication_model == NULL){ 1516 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1517 } else { 1518 1519 // decrease ref count if old virtual address 1520 if (mesh_network_address_virtual(configuration_server_publication_model.address)) { 1521 mesh_virtual_address_t * current_virtual_address = mesh_virtual_address_for_pseudo_dst(configuration_server_publication_model.address); 1522 mesh_virtual_address_decrease_refcount(current_virtual_address); 1523 } 1524 1525 // restart publication 1526 config_model_publication_changed(target_model, &configuration_server_publication_model); 1527 } 1528 } 1529 1530 // send status 1531 config_model_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, model_identifier, &configuration_server_publication_model); 1532 mesh_access_message_processed(pdu); 1533 } 1534 1535 static void config_model_publication_virtual_address_set_hash(void *arg){ 1536 mesh_model_t *mesh_model = (mesh_model_t*) arg; 1537 1538 // add if not exist 1539 mesh_virtual_address_t * virtual_address = mesh_virtual_address_for_label_uuid(configuration_server_label_uuid); 1540 if (virtual_address == NULL){ 1541 virtual_address = mesh_virtual_address_register(configuration_server_label_uuid, configuration_server_hash); 1542 } 1543 1544 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1545 if (virtual_address == NULL){ 1546 status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES; 1547 } else { 1548 1549 // increase ref count if new virtual address 1550 mesh_virtual_address_increase_refcount(virtual_address); 1551 1552 // decrease ref count if old virtual address 1553 if (mesh_network_address_virtual(configuration_server_publication_model.address)) { 1554 mesh_virtual_address_t * current_virtual_address = mesh_virtual_address_for_pseudo_dst(configuration_server_publication_model.address); 1555 if (current_virtual_address){ 1556 mesh_virtual_address_decrease_refcount(current_virtual_address); 1557 } 1558 } 1559 1560 configuration_server_publication_model.address = virtual_address->pseudo_dst; 1561 mesh_virtual_address_increase_refcount(virtual_address); 1562 1563 // restart publication 1564 config_model_publication_changed(configuration_server_target_model, &configuration_server_publication_model); 1565 } 1566 1567 // send status 1568 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); 1569 1570 mesh_access_message_processed(access_pdu_in_process); 1571 } 1572 1573 static void 1574 config_model_publication_virtual_address_set_handler(mesh_model_t *mesh_model, 1575 mesh_pdu_t * pdu) { 1576 1577 // set defaults 1578 memset(&configuration_server_publication_model, 0, sizeof(mesh_publication_model_t)); 1579 1580 mesh_access_parser_state_t parser; 1581 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1582 1583 // ElementAddress - Address of the element 1584 configuration_server_element_address = mesh_access_parser_get_u16(&parser); 1585 1586 // store label uuid 1587 mesh_access_parser_get_label_uuid(&parser, configuration_server_label_uuid); 1588 1589 // AppKeyIndex (12), CredentialFlag (1), RFU (3) 1590 uint16_t temp = mesh_access_parser_get_u16(&parser); 1591 configuration_server_publication_model.appkey_index = temp & 0x0fff; 1592 configuration_server_publication_model.friendship_credential_flag = (temp >> 12) & 1; 1593 configuration_server_publication_model.ttl = mesh_access_parser_get_u8(&parser); 1594 configuration_server_publication_model.period = mesh_access_parser_get_u8(&parser); 1595 configuration_server_publication_model.retransmit = mesh_access_parser_get_u8(&parser); 1596 1597 // Model Identifier 1598 configuration_server_model_identifier = mesh_access_parser_get_model_identifier(&parser); 1599 1600 uint8_t status; 1601 configuration_server_target_model = mesh_access_model_for_address_and_model_identifier(configuration_server_element_address, configuration_server_model_identifier, &status); 1602 1603 // model exists, but no publication model 1604 if (configuration_server_target_model != NULL && configuration_server_target_model->publication_model == NULL){ 1605 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1606 } 1607 1608 // on error, no need to calculate virtual address hash 1609 if (status != MESH_FOUNDATION_STATUS_SUCCESS){ 1610 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); 1611 mesh_access_message_processed(pdu); 1612 return; 1613 } 1614 1615 access_pdu_in_process = pdu; 1616 mesh_virtual_address(&configuration_server_cmac_request, configuration_server_label_uuid, &configuration_server_hash, &config_model_publication_virtual_address_set_hash, mesh_model); 1617 } 1618 1619 static void 1620 config_model_publication_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1621 1622 1623 mesh_access_parser_state_t parser; 1624 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1625 1626 // ElementAddress - Address of the element - should be us 1627 uint16_t element_address = mesh_access_parser_get_u16(&parser); 1628 1629 // Model Identifier 1630 uint32_t model_identifier = mesh_access_parser_get_model_identifier(&parser); 1631 1632 uint8_t status; 1633 mesh_model_t * target_model = mesh_access_model_for_address_and_model_identifier(element_address, model_identifier, &status); 1634 1635 mesh_publication_model_t * publication_model; 1636 1637 if (target_model == NULL){ 1638 // use defaults 1639 memset(&configuration_server_publication_model, 0, sizeof(mesh_publication_model_t)); 1640 publication_model = &configuration_server_publication_model; 1641 } else { 1642 publication_model = target_model->publication_model; 1643 } 1644 1645 config_model_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, element_address, model_identifier, publication_model); 1646 mesh_access_message_processed(pdu); 1647 } 1648 1649 // Heartbeat Publication 1650 1651 static void config_heartbeat_publication_emit(mesh_heartbeat_publication_t * mesh_heartbeat_publication){ 1652 1653 printf("CONFIG_SERVER_HEARTBEAT: Emit (dest %04x, count %u, period %u ms)\n", 1654 mesh_heartbeat_publication->destination, 1655 mesh_heartbeat_publication->count, 1656 mesh_heartbeat_publication->period_ms); 1657 1658 // active features 1659 mesh_heartbeat_publication->active_features = mesh_foundation_get_features(); 1660 1661 mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 1662 if (network_pdu){ 1663 uint8_t data[3]; 1664 data[0] = mesh_heartbeat_publication->ttl; 1665 big_endian_store_16(data, 1, mesh_heartbeat_publication->active_features); 1666 mesh_upper_transport_setup_control_pdu((mesh_pdu_t *) network_pdu, mesh_heartbeat_publication->netkey_index, 1667 mesh_heartbeat_publication->ttl, mesh_node_get_primary_element_address(), mesh_heartbeat_publication->destination, 1668 MESH_TRANSPORT_OPCODE_HEARTBEAT, data, sizeof(data)); 1669 mesh_upper_transport_send_control_pdu((mesh_pdu_t *) network_pdu); 1670 } 1671 1672 // forever 1673 if (mesh_heartbeat_publication->count < 0xffffu) { 1674 mesh_heartbeat_publication->count--; 1675 } 1676 } 1677 void mesh_configuration_server_feature_changed(void){ 1678 mesh_model_t * mesh_model = mesh_model_get_configuration_server(); 1679 mesh_heartbeat_publication_t * mesh_heartbeat_publication = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_publication; 1680 1681 // active features 1682 uint16_t active_features = mesh_foundation_get_features(); 1683 if (mesh_heartbeat_publication->active_features == active_features) return; 1684 1685 config_heartbeat_publication_emit(mesh_heartbeat_publication); 1686 } 1687 1688 static void config_heartbeat_publication_timeout_handler(btstack_timer_source_t * ts){ 1689 1690 mesh_heartbeat_publication_t * mesh_heartbeat_publication = (mesh_heartbeat_publication_t*) ts; 1691 1692 // emit beat 1693 config_heartbeat_publication_emit(mesh_heartbeat_publication); 1694 1695 // all sent? 1696 if (mesh_heartbeat_publication->count == 0) return; 1697 1698 // periodic publication? 1699 if (mesh_heartbeat_publication->period_ms == 0) return; 1700 1701 btstack_run_loop_set_timer(ts, mesh_heartbeat_publication->period_ms); 1702 btstack_run_loop_add_timer(ts); 1703 } 1704 1705 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){ 1706 UNUSED(mesh_model); 1707 1708 // setup message 1709 uint8_t count_log = heartbeat_count_log(mesh_heartbeat_publication->count); 1710 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1711 &mesh_foundation_config_heartbeat_publication_status, 1712 status, 1713 mesh_heartbeat_publication->destination, 1714 count_log, 1715 mesh_heartbeat_publication->period_log, 1716 mesh_heartbeat_publication->ttl, 1717 mesh_heartbeat_publication->features, 1718 mesh_heartbeat_publication->netkey_index); 1719 if (!transport_pdu) return; 1720 1721 printf("MESH config_heartbeat_publication_status count = %u => count_log = %u\n", mesh_heartbeat_publication->count, count_log); 1722 1723 // send as segmented access pdu 1724 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1725 } 1726 1727 static void config_heartbeat_publication_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1728 1729 mesh_heartbeat_publication_t requested_publication; 1730 1731 mesh_access_parser_state_t parser; 1732 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1733 1734 // Destination address for Heartbeat messages 1735 requested_publication.destination = mesh_access_parser_get_u16(&parser); 1736 // Number of Heartbeat messages to be sent 1737 requested_publication.count = heartbeat_pwr2(mesh_access_parser_get_u8(&parser)); 1738 // Period for sending Heartbeat messages 1739 requested_publication.period_log = mesh_access_parser_get_u8(&parser); 1740 // TTL to be used when sending Heartbeat messages 1741 requested_publication.ttl = mesh_access_parser_get_u8(&parser); 1742 // Bit field indicating features that trigger Heartbeat messages when changed 1743 requested_publication.features = mesh_access_parser_get_u16(&parser) & MESH_HEARTBEAT_FEATURES_SUPPORTED_MASK; 1744 // NetKey Index 1745 requested_publication.netkey_index = mesh_access_parser_get_u16(&parser); 1746 1747 // store period as ms 1748 requested_publication.period_ms = heartbeat_pwr2(requested_publication.period_log) * 1000; 1749 1750 mesh_heartbeat_publication_t * mesh_heartbeat_publication = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_publication; 1751 1752 // validate fields 1753 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1754 mesh_network_key_t * network_key = mesh_network_key_list_get(requested_publication.netkey_index); 1755 if (network_key == NULL){ 1756 status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 1757 } else { 1758 printf("MESH config_heartbeat_publication_set, destination %x, count = %x, period = %u s\n", 1759 requested_publication.destination, requested_publication.count, requested_publication.period_ms); 1760 1761 // accept update 1762 memcpy(mesh_heartbeat_publication, &requested_publication, sizeof(mesh_heartbeat_publication_t)); 1763 } 1764 1765 config_heartbeat_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, mesh_heartbeat_publication); 1766 1767 mesh_access_message_processed(pdu); 1768 1769 if (status != MESH_FOUNDATION_STATUS_SUCCESS) return; 1770 1771 // check if heartbeats should be disabled 1772 if (mesh_heartbeat_publication->destination == MESH_ADDRESS_UNSASSIGNED || mesh_heartbeat_publication->period_log == 0) { 1773 btstack_run_loop_remove_timer(&mesh_heartbeat_publication->timer); 1774 printf("MESH config_heartbeat_publication_set, disable\n"); 1775 return; 1776 } 1777 1778 // NOTE: defer first heartbeat to allow config status getting sent first 1779 btstack_run_loop_set_timer_handler(&mesh_heartbeat_publication->timer, config_heartbeat_publication_timeout_handler); 1780 btstack_run_loop_set_timer_context(&mesh_heartbeat_publication->timer, mesh_heartbeat_publication); 1781 btstack_run_loop_set_timer(&mesh_heartbeat_publication->timer, 2000); 1782 btstack_run_loop_add_timer(&mesh_heartbeat_publication->timer); 1783 } 1784 1785 static void config_heartbeat_publication_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1786 mesh_heartbeat_publication_t * mesh_heartbeat_publication = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_publication; 1787 config_heartbeat_publication_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_SUCCESS, mesh_heartbeat_publication); 1788 mesh_access_message_processed(pdu); 1789 } 1790 1791 // Heartbeat Subscription 1792 1793 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){ 1794 UNUSED(mesh_model); 1795 1796 // setup message 1797 uint8_t count_log = heartbeat_count_log(mesh_heartbeat_subscription->count_log); 1798 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1799 &mesh_foundation_config_heartbeat_subscription_status, 1800 status, 1801 mesh_heartbeat_subscription->source, 1802 mesh_heartbeat_subscription->destination, 1803 mesh_heartbeat_subscription->period_log, 1804 count_log, 1805 mesh_heartbeat_subscription->min_hops, 1806 mesh_heartbeat_subscription->max_hops); 1807 if (!transport_pdu) return; 1808 printf("MESH config_heartbeat_subscription_status count = %u => count_log = %u\n", mesh_heartbeat_subscription->count_log, count_log); 1809 1810 // send as segmented access pdu 1811 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1812 } 1813 static int config_heartbeat_subscription_enabled(mesh_heartbeat_subscription_t * mesh_heartbeat_subscription){ 1814 return mesh_network_address_unicast(mesh_heartbeat_subscription->source) && mesh_heartbeat_subscription->period_log > 0 && 1815 (mesh_network_address_unicast(mesh_heartbeat_subscription->destination) || mesh_network_address_group(mesh_heartbeat_subscription->destination)); 1816 } 1817 1818 static void config_heartbeat_subscription_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1819 mesh_access_parser_state_t parser; 1820 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1821 1822 mesh_heartbeat_subscription_t requested_subscription; 1823 1824 // Destination address for Heartbeat messages 1825 requested_subscription.source = mesh_access_parser_get_u16(&parser); 1826 // Destination address for Heartbeat messages 1827 requested_subscription.destination = mesh_access_parser_get_u16(&parser); 1828 // Period for sending Heartbeat messages 1829 requested_subscription.period_log = mesh_access_parser_get_u8(&parser); 1830 1831 uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; 1832 if (requested_subscription.period_log > 0x11u){ 1833 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1834 } else if ((requested_subscription.source != MESH_ADDRESS_UNSASSIGNED) && 1835 !mesh_network_address_unicast(requested_subscription.source)){ 1836 status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 1837 } else if ((requested_subscription.destination != MESH_ADDRESS_UNSASSIGNED) && 1838 !mesh_network_address_unicast(requested_subscription.destination) && 1839 !mesh_network_address_group(requested_subscription.destination)){ 1840 status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS; 1841 } 1842 1843 if (status != MESH_FOUNDATION_STATUS_SUCCESS){ 1844 config_heartbeat_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, &requested_subscription); 1845 mesh_access_message_processed(pdu); 1846 return; 1847 } 1848 1849 mesh_heartbeat_subscription_t * mesh_heartbeat_subscription = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_subscription; 1850 1851 if (config_heartbeat_subscription_enabled(mesh_heartbeat_subscription)){ 1852 requested_subscription.count_log = 0u; 1853 requested_subscription.min_hops = 0x7Fu; 1854 requested_subscription.max_hops = 0u; 1855 } else { 1856 requested_subscription.source = MESH_ADDRESS_UNSASSIGNED; 1857 requested_subscription.destination = MESH_ADDRESS_UNSASSIGNED; 1858 requested_subscription.period_log = 0u; 1859 requested_subscription.count_log = 0u; 1860 requested_subscription.min_hops = 0u; 1861 requested_subscription.max_hops = 0u; 1862 } 1863 1864 // accept request 1865 memcpy(mesh_heartbeat_subscription, &requested_subscription, sizeof(mesh_heartbeat_subscription_t)); 1866 1867 printf("MESH config_heartbeat_subscription_set, destination %x, count = %x, period = %u s\n", 1868 mesh_heartbeat_subscription->destination, mesh_heartbeat_subscription->count_log, heartbeat_pwr2(mesh_heartbeat_subscription->period_log)); 1869 1870 config_heartbeat_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, mesh_heartbeat_subscription); 1871 mesh_access_message_processed(pdu); 1872 } 1873 1874 1875 static void config_heartbeat_subscription_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) { 1876 mesh_heartbeat_subscription_t * mesh_heartbeat_subscription = &((mesh_configuration_server_model_context_t*) mesh_model->model_data)->heartbeat_subscription; 1877 config_heartbeat_subscription_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_SUCCESS, mesh_heartbeat_subscription); 1878 mesh_access_message_processed(pdu); 1879 } 1880 1881 // KeyRefresh Phase 1882 1883 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, 1884 mesh_key_refresh_state_t key_refresh_state){ 1885 UNUSED(mesh_model); 1886 1887 // setup message 1888 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1889 &mesh_key_refresh_phase_status, 1890 status, 1891 netkey_index, 1892 key_refresh_state); 1893 if (!transport_pdu) return; 1894 1895 // send as segmented access pdu 1896 config_server_send_message(netkey_index_dest, dest, (mesh_pdu_t *) transport_pdu); 1897 } 1898 1899 static void config_key_refresh_phase_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1900 mesh_access_parser_state_t parser; 1901 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1902 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 1903 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 1904 1905 uint8_t status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 1906 mesh_key_refresh_state_t key_refresh_state = MESH_KEY_REFRESH_NOT_ACTIVE; 1907 1908 if (subnet != NULL){ 1909 status = MESH_FOUNDATION_STATUS_SUCCESS; 1910 key_refresh_state = subnet->key_refresh; 1911 } 1912 1913 config_key_refresh_phase_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, key_refresh_state); 1914 mesh_access_message_processed(pdu); 1915 } 1916 1917 static void config_key_refresh_phase_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1918 mesh_access_parser_state_t parser; 1919 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1920 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 1921 uint8_t key_refresh_phase_transition = mesh_access_parser_get_u8(&parser); 1922 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 1923 1924 uint8_t status = MESH_FOUNDATION_STATUS_INVALID_NETKEY_INDEX; 1925 1926 if (subnet != NULL){ 1927 status = MESH_FOUNDATION_STATUS_SUCCESS; 1928 1929 switch (key_refresh_phase_transition){ 1930 case 0x02: 1931 switch (subnet->key_refresh){ 1932 case MESH_KEY_REFRESH_FIRST_PHASE: 1933 case MESH_KEY_REFRESH_SECOND_PHASE: 1934 subnet->key_refresh = MESH_KEY_REFRESH_SECOND_PHASE; 1935 break; 1936 default: 1937 break; 1938 } 1939 break; 1940 case 0x03: 1941 switch (subnet->key_refresh){ 1942 case MESH_KEY_REFRESH_FIRST_PHASE: 1943 case MESH_KEY_REFRESH_SECOND_PHASE: 1944 // key refresh phase 3 entered 1945 mesh_access_key_refresh_revoke_keys(subnet); 1946 subnet->key_refresh = MESH_KEY_REFRESH_NOT_ACTIVE; 1947 break; 1948 default: 1949 break; 1950 } 1951 break; 1952 default: 1953 status = MESH_FOUNDATION_STATUS_CANNOT_SET; 1954 break; 1955 } 1956 } 1957 1958 config_key_refresh_phase_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, subnet->key_refresh); 1959 mesh_access_message_processed(pdu); 1960 } 1961 1962 1963 static void config_node_reset_status(mesh_model_t *mesh_model, uint16_t netkey_index, uint16_t dest){ 1964 UNUSED(mesh_model); 1965 1966 // setup message 1967 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message(&mesh_foundation_node_reset_status); 1968 if (!transport_pdu) return; 1969 1970 // send as segmented access pdu 1971 config_server_send_message(netkey_index, dest, (mesh_pdu_t *) transport_pdu); 1972 } 1973 1974 static void config_node_reset_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1975 mesh_node_reset(); 1976 config_node_reset_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu)); 1977 mesh_access_message_processed(pdu); 1978 } 1979 1980 static void low_power_node_poll_timeout_status(mesh_model_t *mesh_model, uint16_t netkey_index_dest, uint16_t dest, uint8_t status){ 1981 UNUSED(mesh_model); 1982 1983 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 1984 &mesh_foundation_low_power_node_poll_timeout_status, 1985 status, 1986 0, // The unicast address of the Low Power node 1987 0); // The current value of the PollTimeout timer of the Low Power node 1988 if (!transport_pdu) return; 1989 printf("TODO: send unicast address of the Low Power node and the current value of the PollTimeout timer, instead of 0s\n"); 1990 // send as segmented access pdu 1991 config_server_send_message(netkey_index_dest, dest, (mesh_pdu_t *) transport_pdu); 1992 } 1993 1994 static void config_low_power_node_poll_timeout_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 1995 mesh_access_parser_state_t parser; 1996 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 1997 printf("TODO: implement get the current value of PollTimeout timer of the Low Power node within a Friend node\n"); 1998 low_power_node_poll_timeout_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_SUCCESS); 1999 2000 mesh_access_message_processed(pdu); 2001 } 2002 2003 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, 2004 mesh_node_identity_state_t node_identity_state){ 2005 UNUSED(mesh_model); 2006 2007 // setup message 2008 mesh_transport_pdu_t * transport_pdu = mesh_access_setup_segmented_message( 2009 &mesh_foundation_node_identity_status, 2010 status, 2011 netkey_index, 2012 node_identity_state); 2013 if (!transport_pdu) return; 2014 2015 // send as segmented access pdu 2016 config_server_send_message(netkey_index_dest, dest, (mesh_pdu_t *) transport_pdu); 2017 } 2018 2019 static void config_node_identity_get_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2020 mesh_access_parser_state_t parser; 2021 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2022 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 2023 2024 mesh_node_identity_state_t node_identity_state = MESH_NODE_IDENTITY_STATE_ADVERTISING_NOT_SUPPORTED; 2025 uint8_t status = mesh_proxy_get_advertising_with_node_id_status(netkey_index, &node_identity_state); 2026 2027 config_node_identity_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, node_identity_state); 2028 2029 mesh_access_message_processed(pdu); 2030 } 2031 2032 static void config_node_identity_set_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu){ 2033 mesh_access_parser_state_t parser; 2034 mesh_access_parser_init(&parser, (mesh_pdu_t*) pdu); 2035 uint16_t netkey_index = mesh_access_parser_get_u16(&parser); 2036 mesh_node_identity_state_t node_identity_state = (mesh_node_identity_state_t) mesh_access_parser_get_u8(&parser); 2037 2038 uint8_t status = mesh_proxy_set_advertising_with_node_id(netkey_index, node_identity_state); 2039 2040 config_node_identity_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), status, netkey_index, node_identity_state); 2041 2042 mesh_access_message_processed(pdu); 2043 } 2044 2045 // 2046 2047 const static mesh_operation_t mesh_configuration_server_model_operations[] = { 2048 { MESH_FOUNDATION_OPERATION_APPKEY_ADD, 19, config_appkey_add_handler }, 2049 { MESH_FOUNDATION_OPERATION_APPKEY_DELETE, 3, config_appkey_delete_handler }, 2050 { MESH_FOUNDATION_OPERATION_APPKEY_GET, 2, config_appkey_get_handler }, 2051 { MESH_FOUNDATION_OPERATION_APPKEY_UPDATE, 19, config_appkey_update_handler }, 2052 { MESH_FOUNDATION_OPERATION_NETKEY_ADD, 18, config_netkey_add_handler }, 2053 { MESH_FOUNDATION_OPERATION_NETKEY_UPDATE, 18, config_netkey_update_handler }, 2054 { MESH_FOUNDATION_OPERATION_NETKEY_DELETE, 2, config_netkey_delete_handler }, 2055 { MESH_FOUNDATION_OPERATION_NETKEY_GET, 0, config_netkey_get_handler }, 2056 { MESH_FOUNDATION_OPERATION_COMPOSITION_DATA_GET, 1, config_composition_data_get_handler }, 2057 { MESH_FOUNDATION_OPERATION_BEACON_GET, 0, config_beacon_get_handler }, 2058 { MESH_FOUNDATION_OPERATION_BEACON_SET, 1, config_beacon_set_handler }, 2059 { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_GET, 0, config_default_ttl_get_handler }, 2060 { MESH_FOUNDATION_OPERATION_DEFAULT_TTL_SET, 1, config_default_ttl_set_handler }, 2061 { MESH_FOUNDATION_OPERATION_FRIEND_GET, 0, config_friend_get_handler }, 2062 { MESH_FOUNDATION_OPERATION_FRIEND_SET, 1, config_friend_set_handler }, 2063 { MESH_FOUNDATION_OPERATION_NETWORK_TRANSMIT_GET, 0, config_model_network_transmit_get_handler }, 2064 { MESH_FOUNDATION_OPERATION_NETWORK_TRANSMIT_SET, 1, config_model_network_transmit_set_handler }, 2065 { MESH_FOUNDATION_OPERATION_GATT_PROXY_GET, 0, config_gatt_proxy_get_handler }, 2066 { MESH_FOUNDATION_OPERATION_GATT_PROXY_SET, 1, config_gatt_proxy_set_handler }, 2067 { MESH_FOUNDATION_OPERATION_RELAY_GET, 0, config_relay_get_handler }, 2068 { MESH_FOUNDATION_OPERATION_RELAY_SET, 1, config_relay_set_handler }, 2069 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_ADD, 6, config_model_subscription_add_handler }, 2070 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_ADD, 20, config_model_subscription_virtual_address_add_handler }, 2071 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_DELETE, 6, config_model_subscription_delete_handler }, 2072 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_DELETE, 20, config_model_subscription_virtual_address_delete_handler }, 2073 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_OVERWRITE, 6, config_model_subscription_overwrite_handler }, 2074 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_VIRTUAL_ADDRESS_OVERWRITE,20, config_model_subscription_virtual_address_overwrite_handler }, 2075 { MESH_FOUNDATION_OPERATION_MODEL_SUBSCRIPTION_DELETE_ALL, 4, config_model_subscription_delete_all_handler }, 2076 { MESH_FOUNDATION_OPERATION_SIG_MODEL_SUBSCRIPTION_GET, 4, config_model_subscription_get_handler }, 2077 { MESH_FOUNDATION_OPERATION_VENDOR_MODEL_SUBSCRIPTION_GET, 6, config_model_subscription_get_handler }, 2078 { MESH_FOUNDATION_OPERATION_SIG_MODEL_APP_GET, 4, config_model_app_get_handler }, 2079 { MESH_FOUNDATION_OPERATION_VENDOR_MODEL_APP_GET, 6, config_model_app_get_handler }, 2080 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_SET, 11, config_model_publication_set_handler }, 2081 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_VIRTUAL_ADDRESS_SET, 25, config_model_publication_virtual_address_set_handler }, 2082 { MESH_FOUNDATION_OPERATION_MODEL_PUBLICATION_GET, 4, config_model_publication_get_handler }, 2083 { MESH_FOUNDATION_OPERATION_MODEL_APP_BIND, 6, config_model_app_bind_handler }, 2084 { MESH_FOUNDATION_OPERATION_MODEL_APP_UNBIND, 6, config_model_app_unbind_handler }, 2085 { MESH_FOUNDATION_OPERATION_HEARTBEAT_PUBLICATION_GET, 0, config_heartbeat_publication_get_handler }, 2086 { MESH_FOUNDATION_OPERATION_HEARTBEAT_PUBLICATION_SET, 9, config_heartbeat_publication_set_handler }, 2087 { MESH_FOUNDATION_OPERATION_HEARTBEAT_SUBSCRIPTION_GET, 0, config_heartbeat_subscription_get_handler}, 2088 { MESH_FOUNDATION_OPERATION_HEARTBEAT_SUBSCRIPTION_SET, 5, config_heartbeat_subscription_set_handler}, 2089 { MESH_FOUNDATION_OPERATION_KEY_REFRESH_PHASE_GET, 2, config_key_refresh_phase_get_handler }, 2090 { MESH_FOUNDATION_OPERATION_KEY_REFRESH_PHASE_SET, 3, config_key_refresh_phase_set_handler }, 2091 { MESH_FOUNDATION_OPERATION_NODE_RESET, 0, config_node_reset_handler }, 2092 { MESH_FOUNDATION_OPERATION_LOW_POWER_NODE_POLL_TIMEOUT_GET, 2, config_low_power_node_poll_timeout_get_handler }, 2093 { MESH_FOUNDATION_OPERATION_NODE_IDENTITY_GET, 2, config_node_identity_get_handler }, 2094 { MESH_FOUNDATION_OPERATION_NODE_IDENTITY_SET, 3, config_node_identity_set_handler }, 2095 { 0, 0, NULL } 2096 }; 2097 2098 const mesh_operation_t * mesh_configuration_server_get_operations(void){ 2099 return mesh_configuration_server_model_operations; 2100 } 2101 2102