mesh: move iv index + seq number load/store to mesh from access

This commit is contained in:
Matthias Ringwald 2019-07-15 13:18:07 +02:00
parent a55e84a9c0
commit a0253209c2
4 changed files with 97 additions and 109 deletions

View File

@ -110,6 +110,11 @@ typedef struct {
uint8_t friend;
} mesh_persistent_foundation_t;
typedef struct {
uint32_t iv_index;
uint32_t seq_number;
} iv_index_and_sequence_number_t;
static btstack_packet_handler_t provisioning_device_packet_handler;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static int provisioned;
@ -129,6 +134,11 @@ static uint8_t random_device_uuid[16];
static const btstack_tlv_t * btstack_tlv_singleton_impl;
static void * btstack_tlv_singleton_context;
// IV Index persistence
static uint32_t sequence_number_last_stored;
static uint32_t sequence_number_storage_trigger;
void mesh_access_setup_from_provisioning_data(const mesh_provisioning_data_t * provisioning_data){
// set iv_index and iv index update active
@ -514,6 +524,81 @@ int mesh_model_contains_appkey(mesh_model_t * mesh_model, uint16_t appkey_index)
return 0;
}
void mesh_access_netkey_finalize(mesh_network_key_t * network_key){
mesh_network_key_remove(network_key);
mesh_delete_network_key(network_key->internal_index);
btstack_memory_mesh_network_key_free(network_key);
}
void mesh_access_appkey_finalize(mesh_transport_key_t * transport_key){
mesh_transport_key_remove(transport_key);
mesh_delete_app_key(transport_key->appkey_index);
btstack_memory_mesh_transport_key_free(transport_key);
}
void mesh_access_key_refresh_revoke_keys(mesh_subnet_t * subnet){
// delete old netkey index
mesh_access_netkey_finalize(subnet->old_key);
subnet->old_key = subnet->new_key;
subnet->new_key = NULL;
// delete old appkeys, if any
mesh_transport_key_iterator_t it;
mesh_transport_key_iterator_init(&it, subnet->netkey_index);
while (mesh_transport_key_iterator_has_more(&it)){
mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it);
if (transport_key->old_key == 0) continue;
mesh_access_appkey_finalize(transport_key);
}
}
// Mesh IV Index
static uint32_t mesh_tag_for_iv_index_and_seq_number(void){
return ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'I' << 9) | ((uint32_t) 'S');
}
void mesh_store_iv_index_after_provisioning(uint32_t iv_index){
iv_index_and_sequence_number_t data;
uint32_t tag = mesh_tag_for_iv_index_and_seq_number();
data.iv_index = iv_index;
data.seq_number = 0;
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
sequence_number_last_stored = data.seq_number;
sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL;
}
void mesh_store_iv_index_and_sequence_number(void){
iv_index_and_sequence_number_t data;
uint32_t tag = mesh_tag_for_iv_index_and_seq_number();
data.iv_index = mesh_get_iv_index();
data.seq_number = mesh_sequence_number_peek();
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
sequence_number_last_stored = data.seq_number;
sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL;
}
int mesh_load_iv_index_and_sequence_number(uint32_t * iv_index, uint32_t * sequence_number){
iv_index_and_sequence_number_t data;
uint32_t tag = mesh_tag_for_iv_index_and_seq_number();
uint32_t len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
if (len == sizeof(iv_index_and_sequence_number_t)){
*iv_index = data.iv_index;
*sequence_number = data.seq_number;
return 1;
}
return 0;
}
// higher layer
static void mesh_persist_iv_index_and_sequence_number(void){
if (mesh_sequence_number_peek() >= sequence_number_storage_trigger){
mesh_store_iv_index_and_sequence_number();
}
}
static void mesh_node_setup_default_models(void){
// configure Config Server
mesh_configuration_server_model.model_identifier = mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER);
@ -560,7 +645,11 @@ void mesh_init(void){
// Access layer
mesh_access_init();
// Add mandatory models: Config Server and Health Server
mesh_node_setup_default_models();
// register for seq number updates
mesh_sequence_number_set_update_callback(&mesh_persist_iv_index_and_sequence_number);
}
/**

View File

@ -62,6 +62,11 @@ void mesh_register_provisioning_device_packet_handler(btstack_packet_handler_t p
void mesh_foundation_state_load(void);
void mesh_foundation_state_store(void);
// Mesh IV Index and sequence number
void mesh_store_iv_index_after_provisioning(uint32_t iv_index);
void mesh_store_iv_index_and_sequence_number(void);
int mesh_load_iv_index_and_sequence_number(uint32_t * iv_index, uint32_t * sequence_number);
// Mesh NetKey List
void mesh_store_network_key(mesh_network_key_t * network_key);
void mesh_delete_network_key(uint16_t internal_index);
@ -85,6 +90,9 @@ int mesh_model_contains_appkey(mesh_model_t * mesh_model, uint16_t appkey_index)
// temp
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_key_refresh_revoke_keys(mesh_subnet_t * subnet);
void mesh_access_netkey_finalize(mesh_network_key_t * network_key);
void mesh_access_appkey_finalize(mesh_transport_key_t * transport_key);
#if defined __cplusplus
}

View File

@ -61,7 +61,6 @@ static void mesh_access_message_process_handler(mesh_pdu_t * pdu);
static void mesh_access_secure_network_beacon_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size);
static void mesh_access_upper_transport_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu);
static const mesh_operation_t * mesh_model_lookup_operation_by_opcode(mesh_model_t * model, uint32_t opcode);
static void mesh_persist_iv_index_and_sequence_number(void);
// acknowledged messages
static btstack_linked_list_t mesh_access_acknowledged_messages;
@ -69,20 +68,12 @@ static btstack_timer_source_t mesh_access_acknowledged_timer;
static uint16_t mid_counter;
static const btstack_tlv_t * btstack_tlv_singleton_impl;
static void * btstack_tlv_singleton_context;
// Transitions
static btstack_linked_list_t transitions;
static btstack_timer_source_t transitions_timer;
static uint32_t transition_step_min_ms;
static uint8_t mesh_transaction_id_counter = 0;
static void mesh_access_setup_tlv(void){
if (btstack_tlv_singleton_impl) return;
btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context);
}
void mesh_access_init(void){
// register with upper transport
mesh_upper_transport_register_access_message_handler(&mesh_access_message_process_handler);
@ -90,9 +81,6 @@ void mesh_access_init(void){
// register for secure network beacons
beacon_register_for_secure_network_beacons(&mesh_access_secure_network_beacon_handler);
// register for seq number updates
mesh_sequence_number_set_update_callback(&mesh_persist_iv_index_and_sequence_number);
}
void mesh_access_emit_state_update_bool(btstack_packet_handler_t event_handler, uint8_t element_index, uint32_t model_identifier,
@ -996,64 +984,6 @@ int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address
return 0;
}
// Mesh IV Index
static uint32_t mesh_tag_for_iv_index_and_seq_number(void){
return ((uint32_t) 'M' << 24) | ((uint32_t) 'F' << 16) | ((uint32_t) 'I' << 9) | ((uint32_t) 'S');
}
typedef struct {
uint32_t iv_index;
uint32_t seq_number;
} iv_index_and_sequence_number_t;
static uint32_t sequence_number_last_stored;
static uint32_t sequence_number_storage_trigger;
void mesh_store_iv_index_after_provisioning(uint32_t iv_index){
iv_index_and_sequence_number_t data;
mesh_access_setup_tlv();
uint32_t tag = mesh_tag_for_iv_index_and_seq_number();
data.iv_index = iv_index;
data.seq_number = 0;
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
sequence_number_last_stored = data.seq_number;
sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL;
}
void mesh_store_iv_index_and_sequence_number(void){
iv_index_and_sequence_number_t data;
mesh_access_setup_tlv();
uint32_t tag = mesh_tag_for_iv_index_and_seq_number();
data.iv_index = mesh_get_iv_index();
data.seq_number = mesh_sequence_number_peek();
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
sequence_number_last_stored = data.seq_number;
sequence_number_storage_trigger = sequence_number_last_stored + MESH_SEQUENCE_NUMBER_STORAGE_INTERVAL;
}
int mesh_load_iv_index_and_sequence_number(uint32_t * iv_index, uint32_t * sequence_number){
iv_index_and_sequence_number_t data;
mesh_access_setup_tlv();
uint32_t tag = mesh_tag_for_iv_index_and_seq_number();
uint32_t len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
if (len == sizeof(iv_index_and_sequence_number_t)){
*iv_index = data.iv_index;
*sequence_number = data.seq_number;
return 1;
}
return 0;
}
// higher layer
static void mesh_persist_iv_index_and_sequence_number(void){
if (mesh_sequence_number_peek() >= sequence_number_storage_trigger){
mesh_store_iv_index_and_sequence_number();
}
}
// Mesh Model Publication
static btstack_timer_source_t mesh_access_publication_timer;
@ -1234,34 +1164,6 @@ void mesh_access_state_changed(mesh_model_t * mesh_model){
mesh_model_publication_run(NULL);
}
void mesh_access_netkey_finalize(mesh_network_key_t * network_key){
mesh_network_key_remove(network_key);
mesh_delete_network_key(network_key->internal_index);
btstack_memory_mesh_network_key_free(network_key);
}
void mesh_access_appkey_finalize(mesh_transport_key_t * transport_key){
mesh_transport_key_remove(transport_key);
mesh_delete_app_key(transport_key->appkey_index);
btstack_memory_mesh_transport_key_free(transport_key);
}
void mesh_access_key_refresh_revoke_keys(mesh_subnet_t * subnet){
// delete old netkey index
mesh_access_netkey_finalize(subnet->old_key);
subnet->old_key = subnet->new_key;
subnet->new_key = NULL;
// delete old appkeys, if any
mesh_transport_key_iterator_t it;
mesh_transport_key_iterator_init(&it, subnet->netkey_index);
while (mesh_transport_key_iterator_has_more(&it)){
mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it);
if (transport_key->old_key == 0) continue;
mesh_access_appkey_finalize(transport_key);
}
}
static void mesh_access_secure_network_beacon_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t size){
UNUSED(channel);
UNUSED(size);

View File

@ -319,9 +319,6 @@ void mesh_access_transitions_add(mesh_transition_t * transition);
void mesh_access_transitions_remove(mesh_transition_t * transition);
uint8_t mesh_access_transactions_get_next_transaction_id(void);
// Key Refresh
void mesh_access_key_refresh_revoke_keys(mesh_subnet_t * subnet);
// Mesh Model Publicaation
/**
@ -350,17 +347,9 @@ uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu);
uint16_t mesh_pdu_len(mesh_pdu_t * pdu);
uint8_t * mesh_pdu_data(mesh_pdu_t * pdu);
void mesh_access_netkey_finalize(mesh_network_key_t * network_key);
void mesh_access_appkey_finalize(mesh_transport_key_t * transport_key);
// Mesh Model Subscriptions
int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address);
// Mesh IV Index and sequence number
void mesh_store_iv_index_after_provisioning(uint32_t iv_index);
void mesh_store_iv_index_and_sequence_number(void);
int mesh_load_iv_index_and_sequence_number(uint32_t * iv_index, uint32_t * sequence_number);
// Mesh Access Parser
int mesh_access_pdu_get_opcode(mesh_pdu_t * pdu, uint32_t * opcode, uint16_t * opcode_size);
int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu);