mesh: move node->element management to mesh_node

This commit is contained in:
Matthias Ringwald 2019-07-11 16:17:35 +02:00
parent 683cf298e3
commit e8625ff1d4
4 changed files with 120 additions and 110 deletions

View File

@ -37,10 +37,18 @@
#define __BTSTACK_FILE__ "mesh_node.c"
#include <stddef.h>
#include "mesh/mesh_node.h"
static uint16_t primary_element_address;
static mesh_element_t primary_element;
static uint16_t mesh_element_index_next;
static btstack_linked_list_t mesh_elements;
void mesh_node_primary_element_address_set(uint16_t unicast_address){
primary_element_address = unicast_address;
}
@ -48,3 +56,49 @@ void mesh_node_primary_element_address_set(uint16_t unicast_address){
uint16_t mesh_node_primary_element_address_get(void){
return primary_element_address;
}
void mesh_node_init(void){
// dd Primary Element to list of elements
mesh_element_add(&primary_element);
}
void mesh_element_add(mesh_element_t * element){
element->element_index = mesh_element_index_next++;
btstack_linked_list_add_tail(&mesh_elements, (void*) element);
}
mesh_element_t * mesh_primary_element(void){
return &primary_element;
}
void mesh_access_set_primary_element_location(uint16_t location){
primary_element.loc = location;
}
mesh_element_t * mesh_element_for_index(uint16_t element_index){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &mesh_elements);
while (btstack_linked_list_iterator_has_next(&it)){
mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it);
if (element->element_index != element_index) continue;
return element;
}
return NULL;
}
mesh_element_t * mesh_element_for_unicast_address(uint16_t unicast_address){
uint16_t element_index = unicast_address - mesh_node_primary_element_address_get();
return mesh_element_for_index(element_index);
}
void mesh_element_iterator_init(mesh_element_iterator_t * iterator){
btstack_linked_list_iterator_init(&iterator->it, &mesh_elements);
}
int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){
return btstack_linked_list_iterator_has_next(&iterator->it);
}
mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){
return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it);
}

View File

@ -43,6 +43,31 @@ extern "C" {
#endif
#include <stdint.h>
#include "btstack_linked_list.h"
typedef struct mesh_element {
// linked list item
btstack_linked_item_t item;
// element index
uint16_t element_index;
// LOC
uint16_t loc;
// models
btstack_linked_list_t models;
uint16_t models_count_sig;
uint16_t models_count_vendor;
} mesh_element_t;
typedef struct {
btstack_linked_list_iterator_t it;
} mesh_element_iterator_t;
void mesh_node_init(void);
/**
* @brief Set unicast address of primary element
@ -50,11 +75,50 @@ extern "C" {
*/
void mesh_node_primary_element_address_set(uint16_t unicast_address);
/**
* @brief Set location of primary element
* @note Returned by Configuration Server Composite Data
* @param location
*/
void mesh_access_set_primary_element_location(uint16_t location);
/**
* @brief Get unicast address of primary element
*/
uint16_t mesh_node_primary_element_address_get(void);
/**
* @brief Get Primary Element of this node
*/
mesh_element_t * mesh_primary_element(void);
/**
* @brief Add secondary element
* @param element
*/
void mesh_element_add(mesh_element_t * element);
/**
* @brief Get element for given unicast address
* @param unicast_address
*/
mesh_element_t * mesh_element_for_unicast_address(uint16_t unicast_address);
/**
* @brief Get element by index
* @param element_index
*/
mesh_element_t * mesh_element_for_index(uint16_t element_index);
// Mesh Element Iterator
void mesh_element_iterator_init(mesh_element_iterator_t * iterator);
int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator);
mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator);
#if defined __cplusplus
}
#endif

View File

@ -59,11 +59,6 @@ static void mesh_access_upper_transport_handler(mesh_transport_callback_type_t c
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);
static mesh_element_t primary_element;
static uint16_t mesh_element_index_next;
static btstack_linked_list_t mesh_elements;
// acknowledged messages
static btstack_linked_list_t mesh_access_acknowledged_messages;
static btstack_timer_source_t mesh_access_acknowledged_timer;
@ -85,9 +80,6 @@ static void mesh_access_setup_tlv(void){
}
void mesh_access_init(void){
// Access layer - add Primary Element to list of elements
mesh_element_add(&primary_element);
// register with upper transport
mesh_upper_transport_register_access_message_handler(&mesh_access_message_process_handler);
mesh_upper_transport_set_higher_layer_handler(&mesh_access_upper_transport_handler);
@ -401,39 +393,10 @@ uint8_t mesh_access_transactions_get_next_transaction_id(void){
}
// Mesh Node Element functions
mesh_element_t * mesh_primary_element(void){
return &primary_element;
}
uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model){
return mesh_model->element->element_index;
}
void mesh_access_set_primary_element_location(uint16_t location){
primary_element.loc = location;
}
void mesh_element_add(mesh_element_t * element){
element->element_index = mesh_element_index_next++;
btstack_linked_list_add_tail(&mesh_elements, (void*) element);
}
mesh_element_t * mesh_element_for_unicast_address(uint16_t unicast_address){
uint16_t element_index = unicast_address - mesh_node_primary_element_address_get();
return mesh_element_for_index(element_index);
}
mesh_element_t * mesh_element_for_index(uint16_t element_index){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &mesh_elements);
while (btstack_linked_list_iterator_has_next(&it)){
mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it);
if (element->element_index != element_index) continue;
return element;
}
return NULL;
}
uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model){
return mesh_node_primary_element_address_get() + mesh_model->element->element_index;
}
@ -461,7 +424,7 @@ int mesh_model_is_bluetooth_sig(uint32_t model_identifier){
}
mesh_model_t * mesh_model_get_configuration_server(void){
return mesh_model_get_by_identifier(&primary_element, mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER));
return mesh_model_get_by_identifier(mesh_primary_element(), mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER));
}
void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model){
@ -487,18 +450,6 @@ mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator){
return (mesh_model_t *) btstack_linked_list_iterator_next(&iterator->it);
}
void mesh_element_iterator_init(mesh_element_iterator_t * iterator){
btstack_linked_list_iterator_init(&iterator->it, &mesh_elements);
}
int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){
return btstack_linked_list_iterator_has_next(&iterator->it);
}
mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){
return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it);
}
mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier){
mesh_model_iterator_t it;
mesh_model_iterator_init(&it, element);
@ -993,7 +944,7 @@ static void mesh_access_message_process_handler(mesh_pdu_t * pdu){
}
if (deliver_to_primary_element){
mesh_model_iterator_t model_it;
mesh_model_iterator_init(&model_it, &primary_element);
mesh_model_iterator_init(&model_it, mesh_primary_element());
while (mesh_model_iterator_has_next(&model_it)){
mesh_model_t * model = mesh_model_iterator_next(&model_it);
// find opcode in table

View File

@ -163,10 +163,6 @@ typedef struct {
btstack_linked_list_iterator_t it;
} mesh_model_iterator_t;
typedef struct {
btstack_linked_list_iterator_t it;
} mesh_element_iterator_t;
#define MESH_MAX_NUM_FAULTS 3
typedef struct {
@ -178,23 +174,6 @@ typedef struct {
uint8_t faults[MESH_MAX_NUM_FAULTS];
} mesh_fault_t;
typedef struct mesh_element {
// linked list item
btstack_linked_item_t item;
// element index
uint16_t element_index;
// LOC
uint16_t loc;
// models
btstack_linked_list_t models;
uint16_t models_count_sig;
uint16_t models_count_vendor;
} mesh_element_t;
typedef struct {
uint32_t opcode;
uint8_t * data;
@ -268,24 +247,6 @@ void mesh_access_send_unacknowledged_pdu(mesh_pdu_t * pdu);
*/
void mesh_access_send_acknowledged_pdu(mesh_pdu_t * pdu, uint8_t retransmissions, uint32_t ack_opcode);
/**
* @brief Get Primary Element of this node
*/
mesh_element_t * mesh_primary_element(void);
/**
* @brief Set location of primary element
* @note Returned by Configuration Server Composite Data
* @param location
*/
void mesh_access_set_primary_element_location(uint16_t location);
/**
* @brief Add secondary element
* @param element
*/
void mesh_element_add(mesh_element_t * element);
/**
* @brief Get element index for give model
* @param mesh_model
@ -298,18 +259,6 @@ uint8_t mesh_access_get_element_index(mesh_model_t * mesh_model);
*/
uint16_t mesh_access_get_element_address(mesh_model_t * mesh_model);
/**
* @brief Get element for given unicast address
* @param unicast_address
*/
mesh_element_t * mesh_element_for_unicast_address(uint16_t unicast_address);
/**
* @brief Get element by index
* @param element_index
*/
mesh_element_t * mesh_element_for_index(uint16_t element_index);
/**
* @brief Add model to element
* @param element
@ -317,14 +266,6 @@ mesh_element_t * mesh_element_for_index(uint16_t element_index);
*/
void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model);
// Mesh Element Iterator
void mesh_element_iterator_init(mesh_element_iterator_t * iterator);
int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator);
mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator);
// Mesh Model Iterator
void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element);