mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-29 21:32:38 +00:00
mesh: add internal index to transport keys that is used to persist max MAX_NR_MESH_TRANSPORT_KEYS
This commit is contained in:
parent
fbd63794de
commit
6eb2ec7108
@ -29,6 +29,7 @@
|
||||
#define HCI_INCOMING_PRE_BUFFER_SIZE 4
|
||||
|
||||
#define MAX_NR_LE_DEVICE_DB_ENTRIES 4
|
||||
#define MAX_NR_MESH_TRANSPORT_KEYS 16
|
||||
|
||||
#define NVM_NUM_LINK_KEYS 2
|
||||
|
||||
|
@ -569,6 +569,7 @@ static void load_pts_app_key(void){
|
||||
// PTS app key
|
||||
btstack_parse_hex("3216D1509884B533248541792B877F98", 16, pts_application_key.key);
|
||||
pts_application_key.aid = 0x38;
|
||||
pts_application_key.internal_index = mesh_transport_key_get_free_index();
|
||||
mesh_transport_key_add(&pts_application_key);
|
||||
printf("PTS Application Key (AID %02x): ", 0x38);
|
||||
printf_hexdump(pts_application_key.key, 16);
|
||||
|
@ -657,8 +657,8 @@ int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t mesh_transport_key_tag_for_appkey_index(uint16_t appkey_index){
|
||||
return ((uint32_t) 'M' << 24) | ((uint32_t) 'A' << 16) | ((uint32_t) appkey_index);
|
||||
static uint32_t mesh_transport_key_tag_for_internal_index(uint16_t internal_index){
|
||||
return ((uint32_t) 'M' << 24) | ((uint32_t) 'A' << 16) | ((uint32_t) internal_index);
|
||||
}
|
||||
|
||||
// Mesh App Keys
|
||||
@ -670,18 +670,13 @@ typedef struct {
|
||||
uint8_t key[16];
|
||||
} mesh_persistent_app_key_t;
|
||||
|
||||
#define MESH_APPKEY_INDEX_MAX (16)
|
||||
|
||||
void mesh_store_app_key(uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t * application_key){
|
||||
void mesh_store_app_key(uint16_t internal_index, uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t * application_key){
|
||||
mesh_access_setup_tlv();
|
||||
|
||||
if (appkey_index >= MESH_APPKEY_INDEX_MAX){
|
||||
printf("Warning: AppKey with AppKey Index %x (>= %u) are not persisted\n", appkey_index, MESH_APPKEY_INDEX_MAX);
|
||||
}
|
||||
mesh_persistent_app_key_t data;
|
||||
printf("Store AppKey: AppKey Index 0x%06x, AID %02x: ", appkey_index, aid);
|
||||
printf("Store AppKey: internal index 0x%x, AppKey Index 0x%06x, AID %02x: ", internal_index, appkey_index, aid);
|
||||
printf_hexdump(application_key, 16);
|
||||
uint32_t tag = mesh_transport_key_tag_for_appkey_index(appkey_index);
|
||||
uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index);
|
||||
data.netkey_index = netkey_index;
|
||||
data.appkey_index = appkey_index;
|
||||
data.aid = aid;
|
||||
@ -689,43 +684,44 @@ void mesh_store_app_key(uint16_t netkey_index, uint16_t appkey_index, uint8_t ai
|
||||
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
|
||||
}
|
||||
|
||||
void mesh_delete_app_key(uint16_t appkey_index){
|
||||
void mesh_delete_app_key(uint16_t internal_index){
|
||||
mesh_access_setup_tlv();
|
||||
|
||||
uint32_t tag = mesh_transport_key_tag_for_appkey_index(appkey_index);
|
||||
uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index);
|
||||
btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag);
|
||||
}
|
||||
|
||||
void mesh_load_app_keys(void){
|
||||
mesh_access_setup_tlv();
|
||||
printf("Load App Keys\n");
|
||||
uint16_t appkey_index;
|
||||
for (appkey_index = 0; appkey_index < MESH_APPKEY_INDEX_MAX; appkey_index++){
|
||||
uint16_t internal_index;
|
||||
for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){
|
||||
mesh_persistent_app_key_t data;
|
||||
uint32_t tag = mesh_transport_key_tag_for_appkey_index(appkey_index);
|
||||
uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index);
|
||||
int app_key_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
|
||||
if (app_key_len == 0) return;
|
||||
|
||||
mesh_transport_key_t * key = btstack_memory_mesh_transport_key_get();
|
||||
if (key == NULL) return;
|
||||
|
||||
key->internal_index = internal_index;
|
||||
key->appkey_index = data.appkey_index;
|
||||
key->netkey_index = data.netkey_index;
|
||||
key->aid = data.aid;
|
||||
key->akf = 1;
|
||||
memcpy(key->key, data.key, 16);
|
||||
mesh_transport_key_add(key);
|
||||
printf("Load AppKey: AppKey Index 0x%06x, AID %02x: ", key->appkey_index, key->aid);
|
||||
printf("Load AppKey: internal index 0x%x, AppKey Index 0x%06x, , AID %02x: ", key->internal_index, key->appkey_index, key->aid);
|
||||
printf_hexdump(key->key, 16);
|
||||
}
|
||||
}
|
||||
|
||||
void mesh_delete_app_keys(void){
|
||||
printf("Delete App Keys\n");
|
||||
// TODO: use TLV iterator
|
||||
uint16_t appkey_index;
|
||||
for (appkey_index = 0; appkey_index < MESH_APPKEY_INDEX_MAX; appkey_index++){
|
||||
mesh_delete_app_key(appkey_index);
|
||||
|
||||
uint16_t internal_index;
|
||||
for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){
|
||||
mesh_delete_app_key(internal_index);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,12 +196,12 @@ uint16_t mesh_pdu_len(mesh_pdu_t * pdu);
|
||||
uint8_t * mesh_pdu_data(mesh_pdu_t * pdu);
|
||||
|
||||
// Mesh Appkeys
|
||||
void mesh_store_app_key(uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t * application_key);
|
||||
void mesh_load_app_key(uint16_t appkey_index);
|
||||
void mesh_delete_app_key(uint16_t appkey_index);
|
||||
void mesh_load_app_keys(void);
|
||||
void mesh_store_app_key(uint16_t internal_index, uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t * application_key);
|
||||
void mesh_delete_app_key(uint16_t internal_index);
|
||||
void mesh_delete_app_keys(void);
|
||||
|
||||
void mesh_load_app_keys(void);
|
||||
|
||||
// Mesh Model Subscriptions
|
||||
uint8_t mesh_model_add_subscription(mesh_model_t * mesh_model, uint16_t address);
|
||||
void mesh_model_delete_subscription(mesh_model_t * mesh_model, uint16_t address);
|
||||
|
@ -647,7 +647,7 @@ static void config_appkey_add_or_udpate_aid(void *arg){
|
||||
printf_hexdump(transport_key->key, 16);
|
||||
|
||||
// store in TLV
|
||||
mesh_store_app_key(transport_key->netkey_index, transport_key->appkey_index, transport_key->aid, transport_key->key);
|
||||
mesh_store_app_key(transport_key->internal_index, transport_key->netkey_index, transport_key->appkey_index, transport_key->aid, transport_key->key);
|
||||
|
||||
// add app key
|
||||
mesh_transport_key_add(transport_key);
|
||||
@ -699,8 +699,12 @@ static void config_appkey_add_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu
|
||||
return;
|
||||
}
|
||||
|
||||
// create app key
|
||||
mesh_transport_key_t * app_key = btstack_memory_mesh_transport_key_get();
|
||||
// create app key (first get free slot in transport key table)
|
||||
mesh_transport_key_t * app_key = NULL;
|
||||
uint16_t internal_index = mesh_transport_key_get_free_index();
|
||||
if (internal_index > 0){
|
||||
app_key = btstack_memory_mesh_transport_key_get();
|
||||
}
|
||||
if (app_key == NULL) {
|
||||
config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES);
|
||||
mesh_access_message_processed(pdu);
|
||||
@ -708,6 +712,7 @@ static void config_appkey_add_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu
|
||||
}
|
||||
|
||||
// store data
|
||||
app_key->internal_index = internal_index;
|
||||
app_key->akf = 1;
|
||||
app_key->appkey_index = appkey_index;
|
||||
app_key->netkey_index = netkey_index;
|
||||
@ -718,21 +723,6 @@ static void config_appkey_add_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu
|
||||
mesh_transport_key_calc_aid(&mesh_cmac_request, app_key, config_appkey_add_or_udpate_aid, app_key);
|
||||
}
|
||||
|
||||
static void config_appkey_update_aid(void * arg){
|
||||
mesh_transport_key_t * transport_key = (mesh_transport_key_t *) arg;
|
||||
|
||||
printf("Config Appkey Update: NetKey Index 0x%04x, AppKey Index 0x%04x, AID %02x: ", transport_key->netkey_index, transport_key->appkey_index, transport_key->aid);
|
||||
printf_hexdump(transport_key->key, 16);
|
||||
|
||||
// store in TLV
|
||||
mesh_store_app_key(transport_key->netkey_index, transport_key->appkey_index, transport_key->aid, transport_key->key);
|
||||
|
||||
uint32_t netkey_and_appkey_index = (transport_key->appkey_index << 12) | transport_key->netkey_index;
|
||||
config_appkey_status(NULL, mesh_pdu_netkey_index(access_pdu_in_process), mesh_pdu_src(access_pdu_in_process), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_SUCCESS);
|
||||
|
||||
mesh_access_message_processed(access_pdu_in_process);
|
||||
}
|
||||
|
||||
static void config_appkey_update_handler(mesh_model_t *mesh_model, mesh_pdu_t * pdu) {
|
||||
|
||||
mesh_access_parser_state_t parser;
|
||||
@ -777,8 +767,14 @@ static void config_appkey_update_handler(mesh_model_t *mesh_model, mesh_pdu_t *
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Don't create new key. We create a new app_key becaue key refresh is not implemented yet, by adding a new one, both are available
|
||||
|
||||
// create app key
|
||||
mesh_transport_key_t * app_key = btstack_memory_mesh_transport_key_get();
|
||||
mesh_transport_key_t * app_key = NULL;
|
||||
uint16_t internal_index = mesh_transport_key_get_free_index();
|
||||
if (internal_index > 0){
|
||||
app_key = btstack_memory_mesh_transport_key_get();
|
||||
}
|
||||
if (app_key == NULL) {
|
||||
config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES);
|
||||
mesh_access_message_processed(pdu);
|
||||
@ -786,6 +782,7 @@ static void config_appkey_update_handler(mesh_model_t *mesh_model, mesh_pdu_t *
|
||||
}
|
||||
|
||||
// store data
|
||||
app_key->internal_index = internal_index;
|
||||
app_key->appkey_index = appkey_index;
|
||||
app_key->netkey_index = netkey_index;
|
||||
app_key->key_refresh = 1;
|
||||
@ -816,8 +813,9 @@ static void config_appkey_delete_handler(mesh_model_t *mesh_model, mesh_pdu_t *
|
||||
// check if appkey already exists
|
||||
mesh_transport_key_t * transport_key = mesh_transport_key_get(appkey_index);
|
||||
if (transport_key){
|
||||
mesh_delete_app_key(transport_key->internal_index);
|
||||
mesh_transport_key_remove(transport_key);
|
||||
mesh_delete_app_key(appkey_index);
|
||||
btstack_memory_mesh_transport_key_free(transport_key);
|
||||
}
|
||||
config_appkey_status(mesh_model, mesh_pdu_netkey_index(pdu), mesh_pdu_src(pdu), netkey_and_appkey_index, MESH_FOUNDATION_STATUS_SUCCESS);
|
||||
mesh_access_message_processed(pdu);
|
||||
|
@ -42,7 +42,7 @@
|
||||
#include "mesh_keys.h"
|
||||
#include "btstack_util.h"
|
||||
#include "btstack_memory.h"
|
||||
|
||||
#include "btstack_config.h"
|
||||
|
||||
static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){
|
||||
printf("%-20s ", name);
|
||||
@ -121,19 +121,38 @@ mesh_network_key_t * mesh_network_key_nid_iterator_get_next(mesh_network_key_ite
|
||||
static mesh_transport_key_t mesh_transport_device_key;
|
||||
static btstack_linked_list_t application_keys;
|
||||
|
||||
static uint8_t mesh_transport_key_used[MAX_NR_MESH_TRANSPORT_KEYS];
|
||||
|
||||
void mesh_transport_set_device_key(const uint8_t * device_key){
|
||||
mesh_transport_device_key.appkey_index = MESH_DEVICE_KEY_INDEX;
|
||||
mesh_transport_device_key.aid = 0;
|
||||
mesh_transport_device_key.akf = 0;
|
||||
mesh_transport_device_key.netkey_index = 0; // unused
|
||||
memcpy(mesh_transport_device_key.key, device_key, 16);
|
||||
|
||||
// use internal slot #0
|
||||
mesh_transport_device_key.internal_index = 0;
|
||||
mesh_transport_key_used[0] = 1;
|
||||
}
|
||||
|
||||
uint16_t mesh_transport_key_get_free_index(void){
|
||||
// find empty slot, skip slot #0 reserved for device key
|
||||
uint16_t i;
|
||||
for (i=1;i < MAX_NR_MESH_TRANSPORT_KEYS ; i++){
|
||||
if (mesh_transport_key_used[i] == 0){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mesh_transport_key_add(mesh_transport_key_t * transport_key){
|
||||
mesh_transport_key_used[transport_key->internal_index] = 1;
|
||||
btstack_linked_list_add_tail(&application_keys, (btstack_linked_item_t *) transport_key);
|
||||
}
|
||||
|
||||
int mesh_transport_key_remove(mesh_transport_key_t * transport_key){
|
||||
mesh_transport_key_used[transport_key->internal_index] = 0;
|
||||
return btstack_linked_list_remove(&application_keys, (btstack_linked_item_t *) transport_key);
|
||||
}
|
||||
|
||||
|
@ -100,6 +100,9 @@ typedef struct {
|
||||
typedef struct {
|
||||
btstack_linked_item_t item;
|
||||
|
||||
// internal index [0..MAX_NR_MESH_TRANSPORT_KEYS-1]
|
||||
uint16_t internal_index;
|
||||
|
||||
// netkey_index of subnet this app key is used with
|
||||
uint16_t netkey_index;
|
||||
|
||||
@ -212,6 +215,13 @@ mesh_network_key_t * mesh_network_key_nid_iterator_get_next(mesh_network_key_ite
|
||||
*/
|
||||
void mesh_transport_set_device_key(const uint8_t * device_key);
|
||||
|
||||
/**
|
||||
* @brief Get internal index of free transport key storage entry
|
||||
* @note index 0 is reserved for device key
|
||||
* @returns index or 0u if none found
|
||||
*/
|
||||
uint16_t mesh_transport_key_get_free_index(void);
|
||||
|
||||
/**
|
||||
* @brief Add application key to list
|
||||
* @param application key
|
||||
|
Loading…
x
Reference in New Issue
Block a user