mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-03-01 10:13:29 +00:00
mesh: move netkey laod/store to mesh from access
This commit is contained in:
parent
9d29a5e252
commit
663808079b
109
src/mesh/mesh.c
109
src/mesh/mesh.c
@ -45,6 +45,8 @@
|
|||||||
#include "btstack_util.h"
|
#include "btstack_util.h"
|
||||||
#include "btstack_config.h"
|
#include "btstack_config.h"
|
||||||
#include "btstack_event.h"
|
#include "btstack_event.h"
|
||||||
|
#include "btstack_tlv.h"
|
||||||
|
#include "btstack_memory.h"
|
||||||
|
|
||||||
#include "mesh/adv_bearer.h"
|
#include "mesh/adv_bearer.h"
|
||||||
#include "mesh/beacon.h"
|
#include "mesh/beacon.h"
|
||||||
@ -65,6 +67,32 @@
|
|||||||
#include "mesh/provisioning.h"
|
#include "mesh/provisioning.h"
|
||||||
#include "mesh/provisioning_device.h"
|
#include "mesh/provisioning_device.h"
|
||||||
|
|
||||||
|
// Persistent storage structures
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t netkey_index;
|
||||||
|
|
||||||
|
uint8_t version;
|
||||||
|
|
||||||
|
// net_key from provisioner or Config Model Client
|
||||||
|
uint8_t net_key[16];
|
||||||
|
|
||||||
|
// derived data
|
||||||
|
|
||||||
|
// k1
|
||||||
|
uint8_t identity_key[16];
|
||||||
|
uint8_t beacon_key[16];
|
||||||
|
|
||||||
|
// k3
|
||||||
|
uint8_t network_id[8];
|
||||||
|
|
||||||
|
// k2
|
||||||
|
uint8_t nid;
|
||||||
|
uint8_t encryption_key[16];
|
||||||
|
uint8_t privacy_key[16];
|
||||||
|
} mesh_persistent_net_key_t;
|
||||||
|
|
||||||
|
|
||||||
static btstack_packet_handler_t provisioning_device_packet_handler;
|
static btstack_packet_handler_t provisioning_device_packet_handler;
|
||||||
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
static btstack_packet_callback_registration_t hci_event_callback_registration;
|
||||||
static int provisioned;
|
static int provisioned;
|
||||||
@ -80,6 +108,10 @@ static mesh_configuration_server_model_context_t mesh_configuration_server_model
|
|||||||
static btstack_crypto_random_t mesh_access_crypto_random;
|
static btstack_crypto_random_t mesh_access_crypto_random;
|
||||||
static uint8_t random_device_uuid[16];
|
static uint8_t random_device_uuid[16];
|
||||||
|
|
||||||
|
// TLV
|
||||||
|
static const btstack_tlv_t * btstack_tlv_singleton_impl;
|
||||||
|
static void * btstack_tlv_singleton_context;
|
||||||
|
|
||||||
void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){
|
void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){
|
||||||
|
|
||||||
// set iv_index and iv index update active
|
// set iv_index and iv index update active
|
||||||
@ -180,6 +212,9 @@ static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *
|
|||||||
switch (hci_event_packet_get_type(packet)) {
|
switch (hci_event_packet_get_type(packet)) {
|
||||||
case BTSTACK_EVENT_STATE:
|
case BTSTACK_EVENT_STATE:
|
||||||
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
|
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
|
||||||
|
// get TLV instance
|
||||||
|
btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context);
|
||||||
|
|
||||||
// startup from provisioning data stored in TLV
|
// startup from provisioning data stored in TLV
|
||||||
provisioned = mesh_node_startup_from_tlv();
|
provisioned = mesh_node_startup_from_tlv();
|
||||||
break;
|
break;
|
||||||
@ -207,6 +242,80 @@ static void hci_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Mesh Network Keys
|
||||||
|
static uint32_t mesh_network_key_tag_for_internal_index(uint16_t internal_index){
|
||||||
|
return ((uint32_t) 'M' << 24) | ((uint32_t) 'N' << 16) | ((uint32_t) internal_index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mesh_store_network_key(mesh_network_key_t * network_key){
|
||||||
|
mesh_persistent_net_key_t data;
|
||||||
|
printf("Store NetKey: 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);
|
||||||
|
uint32_t tag = mesh_network_key_tag_for_internal_index(network_key->internal_index);
|
||||||
|
data.netkey_index = network_key->netkey_index;
|
||||||
|
memcpy(data.net_key, network_key->net_key, 16);
|
||||||
|
memcpy(data.identity_key, network_key->identity_key, 16);
|
||||||
|
memcpy(data.beacon_key, network_key->beacon_key, 16);
|
||||||
|
memcpy(data.network_id, network_key->network_id, 8);
|
||||||
|
data.nid = network_key->nid;
|
||||||
|
data.version = network_key->version;
|
||||||
|
memcpy(data.encryption_key, network_key->encryption_key, 16);
|
||||||
|
memcpy(data.privacy_key, network_key->privacy_key, 16);
|
||||||
|
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(mesh_persistent_net_key_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
void mesh_delete_network_key(uint16_t internal_index){
|
||||||
|
uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index);
|
||||||
|
btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void mesh_load_network_keys(void){
|
||||||
|
printf("Load Network Keys\n");
|
||||||
|
uint16_t internal_index;
|
||||||
|
for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){
|
||||||
|
mesh_persistent_net_key_t data;
|
||||||
|
uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index);
|
||||||
|
int netkey_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
|
||||||
|
if (netkey_len != sizeof(mesh_persistent_net_key_t)) continue;
|
||||||
|
|
||||||
|
mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get();
|
||||||
|
if (network_key == NULL) return;
|
||||||
|
|
||||||
|
network_key->netkey_index = data.netkey_index;
|
||||||
|
memcpy(network_key->net_key, data.net_key, 16);
|
||||||
|
memcpy(network_key->identity_key, data.identity_key, 16);
|
||||||
|
memcpy(network_key->beacon_key, data.beacon_key, 16);
|
||||||
|
memcpy(network_key->network_id, data.network_id, 8);
|
||||||
|
network_key->nid = data.nid;
|
||||||
|
network_key->version = data.version;
|
||||||
|
memcpy(network_key->encryption_key, data.encryption_key, 16);
|
||||||
|
memcpy(network_key->privacy_key, data.privacy_key, 16);
|
||||||
|
|
||||||
|
#ifdef ENABLE_GATT_BEARER
|
||||||
|
// setup advertisement with network id
|
||||||
|
network_key->advertisement_with_network_id.adv_length = mesh_proxy_setup_advertising_with_network_id(network_key->advertisement_with_network_id.adv_data, network_key->network_id);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
mesh_network_key_add(network_key);
|
||||||
|
|
||||||
|
mesh_subnet_setup_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void mesh_delete_network_keys(void){
|
||||||
|
printf("Delete Network Keys\n");
|
||||||
|
|
||||||
|
uint16_t internal_index;
|
||||||
|
for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){
|
||||||
|
mesh_delete_network_key(internal_index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void mesh_node_setup_default_models(void){
|
static void mesh_node_setup_default_models(void){
|
||||||
// configure Config Server
|
// configure Config Server
|
||||||
mesh_configuration_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER);
|
mesh_configuration_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER);
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
|
|
||||||
#include "btstack_defines.h"
|
#include "btstack_defines.h"
|
||||||
#include "mesh/provisioning.h"
|
#include "mesh/provisioning.h"
|
||||||
|
#include "mesh/mesh_keys.h"
|
||||||
|
|
||||||
#if defined __cplusplus
|
#if defined __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -56,6 +57,12 @@ void mesh_init(void);
|
|||||||
*/
|
*/
|
||||||
void mesh_register_provisioning_device_packet_handler(btstack_packet_handler_t packet_handler);
|
void mesh_register_provisioning_device_packet_handler(btstack_packet_handler_t packet_handler);
|
||||||
|
|
||||||
|
// Mesh NetKey List
|
||||||
|
void mesh_store_network_key(mesh_network_key_t * network_key);
|
||||||
|
void mesh_delete_network_key(uint16_t internal_index);
|
||||||
|
void mesh_delete_network_keys(void);
|
||||||
|
void mesh_load_network_keys(void);
|
||||||
|
|
||||||
// temp
|
// temp
|
||||||
void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data);
|
void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data);
|
||||||
void mesh_access_setup_without_provisiong_data(void);
|
void mesh_access_setup_without_provisiong_data(void);
|
||||||
|
@ -53,6 +53,7 @@
|
|||||||
#include "mesh/mesh_node.h"
|
#include "mesh/mesh_node.h"
|
||||||
#include "mesh/mesh_proxy.h"
|
#include "mesh/mesh_proxy.h"
|
||||||
#include "mesh/mesh_upper_transport.h"
|
#include "mesh/mesh_upper_transport.h"
|
||||||
|
#include "mesh/mesh.h"
|
||||||
|
|
||||||
#define MEST_TRANSACTION_TIMEOUT_MS 6000
|
#define MEST_TRANSACTION_TIMEOUT_MS 6000
|
||||||
|
|
||||||
@ -995,10 +996,6 @@ int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t mesh_network_key_tag_for_internal_index(uint16_t internal_index){
|
|
||||||
return ((uint32_t) 'M' << 24) | ((uint32_t) 'N' << 16) | ((uint32_t) internal_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Foundation state
|
// Foundation state
|
||||||
|
|
||||||
static const uint32_t mesh_foundation_state_tag = ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'N' << 8) | ((uint32_t) 'D' << 8);
|
static const uint32_t mesh_foundation_state_tag = ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'N' << 8) | ((uint32_t) 'D' << 8);
|
||||||
@ -1042,102 +1039,6 @@ void mesh_foundation_state_store(void){
|
|||||||
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, mesh_foundation_state_tag, (uint8_t *) &data, sizeof(data));
|
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, mesh_foundation_state_tag, (uint8_t *) &data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mesh Network Keys
|
|
||||||
typedef struct {
|
|
||||||
uint16_t netkey_index;
|
|
||||||
|
|
||||||
uint8_t version;
|
|
||||||
|
|
||||||
// net_key from provisioner or Config Model Client
|
|
||||||
uint8_t net_key[16];
|
|
||||||
|
|
||||||
// derived data
|
|
||||||
|
|
||||||
// k1
|
|
||||||
uint8_t identity_key[16];
|
|
||||||
uint8_t beacon_key[16];
|
|
||||||
|
|
||||||
// k3
|
|
||||||
uint8_t network_id[8];
|
|
||||||
|
|
||||||
// k2
|
|
||||||
uint8_t nid;
|
|
||||||
uint8_t encryption_key[16];
|
|
||||||
uint8_t privacy_key[16];
|
|
||||||
} mesh_persistent_net_key_t;
|
|
||||||
|
|
||||||
void mesh_store_network_key(mesh_network_key_t * network_key){
|
|
||||||
mesh_access_setup_tlv();
|
|
||||||
mesh_persistent_net_key_t data;
|
|
||||||
printf("Store NetKey: 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);
|
|
||||||
uint32_t tag = mesh_network_key_tag_for_internal_index(network_key->internal_index);
|
|
||||||
data.netkey_index = network_key->netkey_index;
|
|
||||||
memcpy(data.net_key, network_key->net_key, 16);
|
|
||||||
memcpy(data.identity_key, network_key->identity_key, 16);
|
|
||||||
memcpy(data.beacon_key, network_key->beacon_key, 16);
|
|
||||||
memcpy(data.network_id, network_key->network_id, 8);
|
|
||||||
data.nid = network_key->nid;
|
|
||||||
data.version = network_key->version;
|
|
||||||
memcpy(data.encryption_key, network_key->encryption_key, 16);
|
|
||||||
memcpy(data.privacy_key, network_key->privacy_key, 16);
|
|
||||||
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(mesh_persistent_net_key_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
void mesh_delete_network_key(uint16_t internal_index){
|
|
||||||
mesh_access_setup_tlv();
|
|
||||||
uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index);
|
|
||||||
btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void mesh_load_network_keys(void){
|
|
||||||
mesh_access_setup_tlv();
|
|
||||||
printf("Load Network Keys\n");
|
|
||||||
uint16_t internal_index;
|
|
||||||
for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){
|
|
||||||
mesh_persistent_net_key_t data;
|
|
||||||
uint32_t tag = mesh_network_key_tag_for_internal_index(internal_index);
|
|
||||||
int netkey_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
|
|
||||||
if (netkey_len != sizeof(mesh_persistent_net_key_t)) continue;
|
|
||||||
|
|
||||||
mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get();
|
|
||||||
if (network_key == NULL) return;
|
|
||||||
|
|
||||||
network_key->netkey_index = data.netkey_index;
|
|
||||||
memcpy(network_key->net_key, data.net_key, 16);
|
|
||||||
memcpy(network_key->identity_key, data.identity_key, 16);
|
|
||||||
memcpy(network_key->beacon_key, data.beacon_key, 16);
|
|
||||||
memcpy(network_key->network_id, data.network_id, 8);
|
|
||||||
network_key->nid = data.nid;
|
|
||||||
network_key->version = data.version;
|
|
||||||
memcpy(network_key->encryption_key, data.encryption_key, 16);
|
|
||||||
memcpy(network_key->privacy_key, data.privacy_key, 16);
|
|
||||||
|
|
||||||
#ifdef ENABLE_GATT_BEARER
|
|
||||||
// setup advertisement with network id
|
|
||||||
network_key->advertisement_with_network_id.adv_length = mesh_proxy_setup_advertising_with_network_id(network_key->advertisement_with_network_id.adv_data, network_key->network_id);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mesh_network_key_add(network_key);
|
|
||||||
|
|
||||||
mesh_subnet_setup_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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void mesh_delete_network_keys(void){
|
|
||||||
printf("Delete Network Keys\n");
|
|
||||||
|
|
||||||
uint16_t internal_index;
|
|
||||||
for (internal_index = 0; internal_index < MAX_NR_MESH_NETWORK_KEYS; internal_index++){
|
|
||||||
mesh_delete_network_key(internal_index);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Mesh App Keys
|
// Mesh App Keys
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -350,12 +350,6 @@ uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu);
|
|||||||
uint16_t mesh_pdu_len(mesh_pdu_t * pdu);
|
uint16_t mesh_pdu_len(mesh_pdu_t * pdu);
|
||||||
uint8_t * mesh_pdu_data(mesh_pdu_t * pdu);
|
uint8_t * mesh_pdu_data(mesh_pdu_t * pdu);
|
||||||
|
|
||||||
// Mesh NetKey List
|
|
||||||
void mesh_store_network_key(mesh_network_key_t * network_key);
|
|
||||||
void mesh_delete_network_key(uint16_t internal_index);
|
|
||||||
void mesh_delete_network_keys(void);
|
|
||||||
void mesh_load_network_keys(void);
|
|
||||||
|
|
||||||
void mesh_access_netkey_finalize(mesh_network_key_t * network_key);
|
void mesh_access_netkey_finalize(mesh_network_key_t * network_key);
|
||||||
|
|
||||||
// Mesh Appkeys
|
// Mesh Appkeys
|
||||||
|
Loading…
x
Reference in New Issue
Block a user