mesh: update subnet based on network keys

This commit is contained in:
Matthias Ringwald 2019-07-02 17:09:36 +02:00
parent ac7c0c0957
commit a2484a8470
6 changed files with 71 additions and 2 deletions

View File

@ -1054,7 +1054,7 @@ void mesh_subnet_remove(mesh_subnet_t * subnet){
btstack_linked_list_remove(&subnets, (btstack_linked_item_t *) subnet);
}
mesh_subnet_t * mesh_subnet_list_get(uint16_t netkey_index){
mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &subnets);
while (btstack_linked_list_iterator_has_next(&it)){
@ -1080,3 +1080,56 @@ int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it){
mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it){
return (mesh_subnet_t *) btstack_linked_list_iterator_next(&it->it);
}
/**
* @brief Update subnet for given netkey index
*/
void mesh_subnet_update_for_netkey_index(uint16_t netkey_index){
// find old / new keys
mesh_network_key_t * old_key = NULL;
mesh_network_key_t * new_key = NULL;
mesh_network_key_iterator_t it;
mesh_network_key_iterator_init(&it);
while (mesh_network_key_iterator_has_more(&it)){
mesh_network_key_t * network_key = mesh_network_key_iterator_get_next(&it);
if (network_key->netkey_index != netkey_index) continue;
if (old_key == NULL){
old_key = network_key;
continue;
}
// assign current key depending on key version
if (((int8_t) (network_key->version - new_key->version)) > 0) {
new_key = network_key;
} else {
new_key = old_key;
old_key = network_key;
}
}
// no keys?
if (old_key == NULL) return;
// get/create subnet for netkey index
mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index);
if (subnet == NULL){
subnet = btstack_memory_mesh_subnet_get();
if (subnet == NULL) return;
subnet->netkey_index = netkey_index;
}
// set keys
subnet->old_key = old_key;
subnet->new_key = new_key;
// key refresh
if (new_key != NULL){
if (subnet->key_refresh == MESH_KEY_REFRESH_NOT_ACTIVE){
// approximation: at least phase 1
subnet->key_refresh = MESH_KEY_REFRESH_FIRST_PHASE;
}
}
// TODO: advertisement using node id active
// TODO: advertisement using network id (used by proxy)
// TODO: secure network beacons
}

View File

@ -329,6 +329,10 @@ int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it);
*/
mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it);
/**
* @brief Update subnet for given netkey index
*/
void mesh_subnet_update_for_netkey_index(uint16_t netkey_index);
// buffer pool

View File

@ -402,10 +402,13 @@ static void mesh_provisioning_message_handler (uint8_t packet_type, uint16_t cha
// get primary netkey
primary_network_key = provisioning_device_data_get_network_key();
mesh_network_key_dump(primary_network_key);
// add to network keys
mesh_network_key_add(primary_network_key);
// setup primary network
mesh_subnet_update_for_netkey_index(primary_network_key->netkey_index);
// store provisioning data and primary network key in TLV
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, 'PROV', (uint8_t *) &provisioning_data, sizeof(mesh_provisioning_data_t));
mesh_store_network_key(primary_network_key);

View File

@ -977,6 +977,9 @@ void mesh_load_network_keys(void){
#endif
mesh_network_key_add(network_key);
mesh_subnet_update_for_netkey_index(network_key->netkey_index);
printf("- internal index 0x%x, NetKey Index 0x%06x, NID %02x: ", network_key->internal_index, network_key->netkey_index, network_key->nid);
printf_hexdump(network_key->net_key, 16);
}

View File

@ -805,6 +805,9 @@ static void config_netkey_add_or_update_derived(void * arg){
// add key to NetKey List
mesh_network_key_add(network_key);
// update subnet
mesh_subnet_update_for_netkey_index(network_key->netkey_index);
#ifdef ENABLE_MESH_PROXY_SERVER
mesh_proxy_start_advertising_with_network_id();
#endif

View File

@ -161,6 +161,7 @@ static void load_network_key_nid_68(void){
btstack_parse_hex("0953fa93e7caac9638f58820220a398e", 16, network_key->encryption_key);
btstack_parse_hex("8b84eedec100067d670971dd2aa700cf", 16, network_key->privacy_key);
mesh_network_key_add(network_key);
mesh_subnet_update_for_netkey_index(network_key->netkey_index);
}
static void load_network_key_nid_5e(void){
@ -169,6 +170,7 @@ static void load_network_key_nid_5e(void){
btstack_parse_hex("be635105434859f484fc798e043ce40e", 16, network_key->encryption_key);
btstack_parse_hex("5d396d4b54d3cbafe943e051fe9a4eb8", 16, network_key->privacy_key);
mesh_network_key_add(network_key);
mesh_subnet_update_for_netkey_index(network_key->netkey_index);
}
static void load_network_key_nid_10(void){
@ -177,6 +179,7 @@ static void load_network_key_nid_10(void){
btstack_parse_hex("3a4fe84a6cc2c6a766ea93f1084d4039", 16, network_key->encryption_key);
btstack_parse_hex("f695fcce709ccface4d8b7a1e6e39d25", 16, network_key->privacy_key);
mesh_network_key_add(network_key);
mesh_subnet_update_for_netkey_index(network_key->netkey_index);
}
static void load_provisioning_data_test_message(void){