From de7d11c030262e8a919c187ba7bf11e93a266bb8 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Tue, 2 Jul 2019 15:22:20 +0200 Subject: [PATCH] mesh: dd mesh_subnet_iterator, add mesh_subnet_for_netkey_index --- src/mesh/mesh_network.c | 40 +++++++++++++++++++++++++++++ src/mesh/mesh_network.h | 56 +++++++++++++++++++++++++++++++++++++++++ test/mesh/mesh_keys.h | 1 - 3 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/mesh/mesh_network.c b/src/mesh/mesh_network.c index 05831dc71..4e4ba5011 100644 --- a/src/mesh/mesh_network.c +++ b/src/mesh/mesh_network.c @@ -94,6 +94,9 @@ static const mesh_network_key_t * current_network_key; static uint8_t encryption_block[16]; static uint8_t obfuscation_block[16]; +// Subnets +static btstack_linked_list_t subnets; + // Network Nonce static uint8_t network_nonce[13]; @@ -1040,3 +1043,40 @@ mesh_network_pdu_t * mesh_network_pdu_get(void){ void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu){ btstack_memory_mesh_network_pdu_free(network_pdu); } + +// Mesh Subnet Management + +void mesh_subnet_add(mesh_subnet_t * subnet){ + btstack_linked_list_add_tail(&subnets, (btstack_linked_item_t *) subnet); +} + +void mesh_subnet_remove(mesh_subnet_t * subnet){ + btstack_linked_list_remove(&subnets, (btstack_linked_item_t *) subnet); +} + +mesh_subnet_t * mesh_subnet_list_get(uint16_t netkey_index){ + btstack_linked_list_iterator_t it; + btstack_linked_list_iterator_init(&it, &subnets); + while (btstack_linked_list_iterator_has_next(&it)){ + mesh_subnet_t * item = (mesh_subnet_t *) btstack_linked_list_iterator_next(&it); + if (item->netkey_index == netkey_index) return item; + } + return NULL; +} + +int mesh_subnet_list_count(void){ + return btstack_linked_list_count(&subnets); +} + +// mesh network key iterator over all keys +void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it){ + btstack_linked_list_iterator_init(&it->it, &subnets); +} + +int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it){ + return btstack_linked_list_iterator_has_next(&it->it); +} + +mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it){ + return (mesh_subnet_t *) btstack_linked_list_iterator_next(&it->it); +} diff --git a/src/mesh/mesh_network.h b/src/mesh/mesh_network.h index e8b57ea50..74888135f 100644 --- a/src/mesh/mesh_network.h +++ b/src/mesh/mesh_network.h @@ -41,6 +41,7 @@ #include "btstack_linked_list.h" #include "provisioning.h" #include "btstack_run_loop.h" +#include "mesh_keys.h" #if defined __cplusplus extern "C" { @@ -133,6 +134,9 @@ typedef struct { typedef struct { btstack_linked_item_t item; + // netkey index + uint16_t netkey_index; + // current / old key mesh_network_key_t * old_key; @@ -156,6 +160,10 @@ typedef struct { } mesh_subnet_t; +typedef struct { + btstack_linked_list_iterator_t it; +} mesh_subnet_iterator_t; + /** * @brief Init Mesh Network Layer */ @@ -275,6 +283,54 @@ int mesh_network_address_all_relays(uint16_t addr); */ int mesh_network_address_virtual(uint16_t addr); + +/** + * @brief Add subnet to list + * @param subnet + */ +void mesh_subnet_add(mesh_subnet_t * subnet); + +/** + * @brief Remove subnet from list + * @param subnet + */ +void mesh_subnet_remove(mesh_subnet_t * subnet); + +/** + * @brief Get subnet for netkey_index + * @param netkey_index + * @returns mesh_subnet_t or NULL + */ +mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index); + +/** + * @brief Get number of stored subnets + * @returns count + */ +int mesh_subnet_list_count(void); + +/** + * @brief Iterate over all subnets + * @param it + */ +void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it); + +/** + * @brief Check if another subnet is available + * @param it + * @return + */ +int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it); + +/** + * @brief Get next subnet + * @param it + * @return + */ +mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it); + + + // buffer pool mesh_network_pdu_t * mesh_network_pdu_get(void); void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu); diff --git a/test/mesh/mesh_keys.h b/test/mesh/mesh_keys.h index 26b8e404a..8744c360d 100644 --- a/test/mesh/mesh_keys.h +++ b/test/mesh/mesh_keys.h @@ -47,7 +47,6 @@ extern "C" #include -#include "mesh/mesh_network.h" #include "mesh/adv_bearer.h"