From 0fd57ef69aed61f2fb95b07436f49f89f03f7c48 Mon Sep 17 00:00:00 2001 From: Anupam Roy Date: Fri, 17 Jul 2020 16:14:01 +0530 Subject: [PATCH] Mesh: Handle Key management operations This patch handles following - Delete NetKey - Unload of network - NetKey Phase Set Change-Id: I6a95631a369c8ef02f8a5dae7d5e93905efd0f36 Signed-off-by: Anupam Roy --- bt-service/services/include/bt-service-mesh-keys.h | 20 ++++- bt-service/services/mesh/bt-service-mesh-keys.c | 95 +++++++++++++++++++++- 2 files changed, 112 insertions(+), 3 deletions(-) diff --git a/bt-service/services/include/bt-service-mesh-keys.h b/bt-service/services/include/bt-service-mesh-keys.h index d6cd042..d0074a2 100644 --- a/bt-service/services/include/bt-service-mesh-keys.h +++ b/bt-service/services/include/bt-service-mesh-keys.h @@ -26,6 +26,7 @@ #include #include "bluetooth-api.h" #include "bluetooth-mesh-api.h" +#include "bt-service-mesh-cdb.h" #include #ifdef __cplusplus @@ -33,25 +34,42 @@ extern "C" { #endif void _bt_mesh_keys_load_net(uint8_t net_uuid[]); + +void _bt_mesh_keys_unload_net(_bt_mesh_cdb_t *cfg); + void _bt_mesh_keys_add_net_key(uint8_t net_uuid[], uint16_t net_idx); + void _bt_mesh_keys_add_app_key(uint8_t net_uuid[], uint16_t net_idx, uint16_t app_idx); + +void _bt_mesh_keys_set_net_key_phase(_bt_mesh_cdb_t *cfg, + uint16_t net_idx, uint8_t phase, bool save); + +void _bt_mesh_keys_del_net_key(uint8_t net_uuid[], + uint16_t net_idx, _bt_mesh_cdb_t *cfg); + void _bt_mesh_keys_del_app_key(uint8_t net_uuid[], uint16_t app_idx); + uint16_t _bt_mesh_keys_get_bound_key(uint8_t net_uuid[], uint16_t app_idx); + bool _bt_mesh_keys_subnet_exists(uint8_t net_uuid[], uint16_t idx); -void _bt_mesh_keys_print_keys(uint8_t net_uuid[]); + bool _bt_mesh_keys_get_new_netkey_index(uint8_t net_uuid[], uint16_t *net_idx); + bool _bt_mesh_keys_get_new_appkey_index(uint8_t net_uuid[], uint16_t *app_idx); + bool _bt_mesh_keys_is_netkey_present(uint8_t net_uuid[], uint16_t net_idx); + bool _bt_mesh_keys_get_netkey_list(uint8_t net_uuid[], GArray **out); + bool _bt_mesh_keys_get_appkey_list(uint8_t net_uuid[], uint16_t net_idx, GArray **out); diff --git a/bt-service/services/mesh/bt-service-mesh-keys.c b/bt-service/services/mesh/bt-service-mesh-keys.c index 0b54fb4..e0980bd 100644 --- a/bt-service/services/mesh/bt-service-mesh-keys.c +++ b/bt-service/services/mesh/bt-service-mesh-keys.c @@ -86,6 +86,45 @@ static bool __mesh_net_idx_match(const void *a, const void *b) return key->idx == idx; } +static bool __mesh_delete_bound_appkey(void *a, void *b) +{ + uint32_t idx = L_PTR_TO_UINT(a); + _bt_mesh_cdb_t *cfg = (_bt_mesh_cdb_t*) b; + + if (!_bt_mesh_conf_delete_application_key(cfg, idx)) + return false; + + l_free(a); + return true; +} +static bool __mesh_remove_netkey_entry(void *a, void *b) +{ + struct mesh_net_key_t *key = (struct mesh_net_key_t*) a; + + /* Only cleanup local keys from the network object: + DONT touch configuration file */ + l_queue_destroy(key->app_keys, l_free); + l_free(key); + return true; +} + +void _bt_mesh_keys_unload_net(_bt_mesh_cdb_t *cfg) +{ + struct mesh_network_t *network; + int numkeys; + BT_INFO("Mesh:Keys: Unload network with all Keys"); + + network = l_queue_find(networks, __mesh_net_uuid_match, cfg->uuid); + if (!network || !network->net_keys) + return; + + numkeys = l_queue_foreach_remove(network->net_keys, + __mesh_remove_netkey_entry, network); + BT_INFO("Mesh:Nodes: Unloadeded [%d] Keys from the network", + numkeys); + l_free(network); +} + void _bt_mesh_keys_load_net(uint8_t net_uuid[]) { struct mesh_network_t *network; @@ -103,14 +142,16 @@ void _bt_mesh_keys_load_net(uint8_t net_uuid[]) l_queue_push_tail(networks, network); } -void _bt_mesh_keys_add_net_key(uint8_t net_uuid[], uint16_t net_idx) +void _bt_mesh_keys_add_net_key(uint8_t net_uuid[], + uint16_t net_idx) { struct mesh_net_key_t *key; struct mesh_network_t *network; BT_INFO("Mesh:Keys: Create new netkey for Network KeyIDx [%u]", net_idx); - network = l_queue_find(networks, __mesh_net_uuid_match, net_uuid); + network = l_queue_find(networks, + __mesh_net_uuid_match, net_uuid); if (!network) return; @@ -257,6 +298,56 @@ bool _bt_mesh_keys_get_net_key_phase(uint8_t net_uuid[], return true; } +void _bt_mesh_keys_set_net_key_phase(_bt_mesh_cdb_t *cfg, + uint16_t net_idx, uint8_t phase, bool save) +{ + struct mesh_net_key_t *key; + struct mesh_network_t *network; + + network = l_queue_find(networks, __mesh_net_uuid_match, cfg->uuid); + if (!network) + return; + + if (!network->net_keys) + return; + + key = l_queue_find(network->net_keys, + __mesh_net_idx_match, L_UINT_TO_PTR(net_idx)); + if (!key) + return; + + key->phase = phase; + + if (save && !_bt_mesh_conf_set_phase_network_key(cfg, + net_idx, phase)) + BT_INFO("Failed to save updated KR phase\n"); +} + +void _bt_mesh_keys_del_net_key(uint8_t net_uuid[], + uint16_t idx, _bt_mesh_cdb_t *cfg) +{ + struct mesh_net_key_t *key; + struct mesh_network_t *network; + BT_INFO("Mesh:Keys: Delete new netkey for Network KeyIDx [%u]", + idx); + + network = l_queue_find(networks, __mesh_net_uuid_match, net_uuid); + if (!network) + return; + + if (!network->net_keys) + return; + + key = l_queue_remove_if(network->net_keys, + __mesh_net_idx_match, L_UINT_TO_PTR(idx)); + if (!key) + return; + + l_queue_foreach_remove(key->app_keys, + __mesh_delete_bound_appkey, cfg); + l_free(key); +} + void _bt_mesh_keys_add_app_key(uint8_t net_uuid[], uint16_t net_idx, uint16_t app_idx) { -- 2.7.4