mesh: simplify network key index management

This commit is contained in:
Matthias Ringwald 2019-07-19 11:22:27 +02:00
parent bc588d8011
commit 0500a9341c
5 changed files with 20 additions and 8 deletions

View File

@ -623,7 +623,7 @@ static void config_netkey_add_handler(mesh_model_t * mesh_model, mesh_pdu_t * pd
// check limit for pts
uint16_t internal_index = mesh_network_key_get_free_index();
if (internal_index == 0 || (config_netkey_list_max && mesh_network_key_list_count() >= config_netkey_list_max)){
if (internal_index == MESH_KEYS_INVALID_INDEX || (config_netkey_list_max && mesh_network_key_list_count() >= config_netkey_list_max)){
status = MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES;
} else {
@ -709,10 +709,17 @@ static void config_netkey_update_handler(mesh_model_t * mesh_model, mesh_pdu_t *
return;
}
// setup new key
// get index for new key
uint16_t internal_index = mesh_network_key_get_free_index();
if (internal_index == MESH_KEYS_INVALID_INDEX){
config_netkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES, netkey_index);
mesh_access_message_processed(access_pdu_in_process);
return;
}
// get new key
mesh_network_key_t * new_network_key = btstack_memory_mesh_network_key_get();
if (internal_index == 0 || new_network_key == NULL){
if (new_network_key == NULL){
config_netkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES, netkey_index);
mesh_access_message_processed(access_pdu_in_process);
return;

View File

@ -55,14 +55,13 @@ void mesh_network_key_init(void){
}
uint16_t mesh_network_key_get_free_index(void){
// find empty slot, skip slot #0 reserved for primary network key
uint16_t i;
for (i=1;i < MAX_NR_MESH_NETWORK_KEYS ; i++){
for (i=0;i < MAX_NR_MESH_NETWORK_KEYS ; i++){
if (mesh_network_key_used[i] == 0){
return i;
}
}
return 0;
return MESH_KEYS_INVALID_INDEX;
}
void mesh_network_key_add(mesh_network_key_t * network_key){

View File

@ -49,6 +49,8 @@ extern "C"
{
#endif
#define MESH_KEYS_INVALID_INDEX 0xffff
typedef struct {
btstack_linked_item_t item;
@ -134,7 +136,7 @@ void mesh_network_key_init(void);
/**
* @brief Get internal index of free network key storage entry
* @note index 0 is reserved for primary network key
* @returns index or 0u if none found
* @returns index or MESH_KEYS_INVALID_INDEX if none found
*/
uint16_t mesh_network_key_get_free_index(void);

View File

@ -704,7 +704,8 @@ static void provisioning_handle_data_ccm(void * arg){
// sort provisoning data
memcpy(network_key->net_key, provisioning_data, 16);
network_key->netkey_index = big_endian_read_16(provisioning_data, 16);
network_key->internal_index = 0;
// assume free index available for very first network key
network_key->internal_index = mesh_network_key_get_free_index();
flags = provisioning_data[18];
iv_index = big_endian_read_32(provisioning_data, 19);
unicast_address = big_endian_read_16(provisioning_data, 23);

View File

@ -140,6 +140,9 @@ void pb_adv_send_pdu(uint16_t pb_transport_cid, const uint8_t * pdu, uint16_t si
}
void pb_gatt_send_pdu(uint16_t con_handle, const uint8_t * pdu, uint16_t pdu_size){}
uint16_t mesh_network_key_get_free_index(void){
return 0;
}
static mesh_network_key_t network_key;
extern "C" mesh_network_key_t * btstack_memory_mesh_network_key_get(void){