From 0f0f44ede9ed9746d2427bc6010bd373b8793d84 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 4 Jul 2019 11:44:20 +0200 Subject: [PATCH] mesh: delete subnet on netkey delete in config server / remove from mesh_subnet_update_for_netkey_index helper --- src/mesh/mesh_network.c | 9 +++----- test/mesh/mesh_configuration_server.c | 30 +++++++++++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/mesh/mesh_network.c b/src/mesh/mesh_network.c index 055c70cad..8827ad05d 100644 --- a/src/mesh/mesh_network.c +++ b/src/mesh/mesh_network.c @@ -1123,12 +1123,9 @@ void mesh_subnet_update_for_netkey_index(uint16_t netkey_index){ // get subnet mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); - // no keys -> no subnet - if (old_key == NULL) { - if (subnet == NULL) return; - mesh_subnet_remove(subnet); - return; - } + // ignore if no key or no subnet + if (old_key == NULL) return; + if (subnet == NULL) return; // create subnet for netkey index if needed if (subnet == NULL){ diff --git a/test/mesh/mesh_configuration_server.c b/test/mesh/mesh_configuration_server.c index f1d41e0b1..42ba78988 100644 --- a/test/mesh/mesh_configuration_server.c +++ b/test/mesh/mesh_configuration_server.c @@ -910,14 +910,11 @@ static void config_netkey_delete_handler(mesh_model_t * mesh_model, mesh_pdu_t * // get existing network_key uint8_t status = MESH_FOUNDATION_STATUS_SUCCESS; - mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); - if (network_key){ - if (mesh_network_key_list_count() > 1){ - // delete - mesh_delete_network_key(network_key->internal_index); - // remove netkey - mesh_network_key_remove(network_key); + // remove subnet + mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); + if (subnet != NULL){ + if (mesh_subnet_list_count() > 1){ // remove all appkeys for this netkey mesh_transport_key_iterator_t it; @@ -927,8 +924,23 @@ static void config_netkey_delete_handler(mesh_model_t * mesh_model, mesh_pdu_t * mesh_configuration_server_delete_appkey(transport_key); } - // update subnet - mesh_subnet_update_for_netkey_index(netkey_index); + // delete old/current key + mesh_network_key_remove(subnet->old_key); + mesh_delete_network_key(subnet->old_key->internal_index); + btstack_memory_mesh_network_key_free(subnet->old_key); + subnet->old_key = NULL; + + // delete new key + if (subnet->new_key != NULL){ + mesh_network_key_remove(subnet->new_key); + mesh_delete_network_key(subnet->new_key->internal_index); + btstack_memory_mesh_network_key_free(subnet->new_key); + subnet->new_key = NULL; + } + + // remove subnet + mesh_subnet_remove(subnet); + btstack_memory_mesh_subnet_free(subnet); } else { // we cannot remove the last network key