From c0a711d937540431008924bfa62bfa8a050c681c Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Fri, 2 Nov 2018 14:40:19 +0100 Subject: [PATCH] mesh: define mesh_network_key and add provide memory pool for it --- src/ble/mesh/mesh_network.h | 32 ++++++++++++++++++++++--- src/btstack_memory.c | 41 ++++++++++++++++++++++++++++++++ src/btstack_memory.h | 4 +++- tool/btstack_memory_generator.py | 7 +++--- 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/ble/mesh/mesh_network.h b/src/ble/mesh/mesh_network.h index 2ecc6f9c5..4c1235850 100644 --- a/src/ble/mesh/mesh_network.h +++ b/src/ble/mesh/mesh_network.h @@ -45,11 +45,37 @@ extern "C" { #endif typedef struct { - btstack_linked_item_t item; - uint8_t len; - uint8_t data[29]; + btstack_linked_item_t item; + uint8_t len; + uint8_t data[29]; } mesh_network_pdu_t; +typedef struct { + btstack_linked_item_t item; + + // index into shared global key list + uint16_t netkey_index; + + // random net_key + uint8_t net_key[16]; + + // derivative data + + // k1 + uint8_t identity_key[16]; + uint8_t beacon_key[16]; + + // k2 + uint8_t nid; + uint8_t encryption_key[16]; + uint8_t privacy_key[16]; + + // k3 + uint8_t network_id[8]; + +} mesh_network_key_t; + + #if defined __cplusplus } #endif diff --git a/src/btstack_memory.c b/src/btstack_memory.c index b706927f8..bedbf44ca 100644 --- a/src/btstack_memory.c +++ b/src/btstack_memory.c @@ -934,6 +934,44 @@ void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu){ #endif +// MARK: mesh_network_key_t +#if !defined(HAVE_MALLOC) && !defined(MAX_NR_MESH_NETWORK_KEYS) + #if defined(MAX_NO_MESH_NETWORK_KEYS) + #error "Deprecated MAX_NO_MESH_NETWORK_KEYS defined instead of MAX_NR_MESH_NETWORK_KEYS. Please update your btstack_config.h to use MAX_NR_MESH_NETWORK_KEYS." + #else + #define MAX_NR_MESH_NETWORK_KEYS 0 + #endif +#endif + +#ifdef MAX_NR_MESH_NETWORK_KEYS +#if MAX_NR_MESH_NETWORK_KEYS > 0 +static mesh_network_key_t mesh_network_key_storage[MAX_NR_MESH_NETWORK_KEYS]; +static btstack_memory_pool_t mesh_network_key_pool; +mesh_network_key_t * btstack_memory_mesh_network_key_get(void){ + return (mesh_network_key_t *) btstack_memory_pool_get(&mesh_network_key_pool); +} +void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){ + btstack_memory_pool_free(&mesh_network_key_pool, mesh_network_key); +} +#else +mesh_network_key_t * btstack_memory_mesh_network_key_get(void){ + return NULL; +} +void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){ + // silence compiler warning about unused parameter in a portable way + (void) mesh_network_key; +}; +#endif +#elif defined(HAVE_MALLOC) +mesh_network_key_t * btstack_memory_mesh_network_key_get(void){ + return (mesh_network_key_t*) malloc(sizeof(mesh_network_key_t)); +} +void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key){ + free(mesh_network_key); +} +#endif + + #endif // init void btstack_memory_init(void){ @@ -995,5 +1033,8 @@ void btstack_memory_init(void){ #if MAX_NR_MESH_NETWORK_PDUS > 0 btstack_memory_pool_create(&mesh_network_pdu_pool, mesh_network_pdu_storage, MAX_NR_MESH_NETWORK_PDUS, sizeof(mesh_network_pdu_t)); #endif +#if MAX_NR_MESH_NETWORK_KEYS > 0 + btstack_memory_pool_create(&mesh_network_key_pool, mesh_network_key_storage, MAX_NR_MESH_NETWORK_KEYS, sizeof(mesh_network_key_t)); +#endif #endif } diff --git a/src/btstack_memory.h b/src/btstack_memory.h index 85a5043ea..e49710112 100644 --- a/src/btstack_memory.h +++ b/src/btstack_memory.h @@ -144,9 +144,11 @@ whitelist_entry_t * btstack_memory_whitelist_entry_get(void); void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry); sm_lookup_entry_t * btstack_memory_sm_lookup_entry_get(void); void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry); -// mesh_network_pdu +// mesh_network_pdu, mesh_network_key mesh_network_pdu_t * btstack_memory_mesh_network_pdu_get(void); void btstack_memory_mesh_network_pdu_free(mesh_network_pdu_t *mesh_network_pdu); +mesh_network_key_t * btstack_memory_mesh_network_key_get(void); +void btstack_memory_mesh_network_key_free(mesh_network_key_t *mesh_network_key); #endif #if defined __cplusplus diff --git a/tool/btstack_memory_generator.py b/tool/btstack_memory_generator.py index b9ebf2830..6afe084e5 100755 --- a/tool/btstack_memory_generator.py +++ b/tool/btstack_memory_generator.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +import os +import sys import os import sys @@ -199,12 +201,9 @@ list_of_structs = [ ] list_of_le_structs = [ ["gatt_client", "whitelist_entry", "sm_lookup_entry"], - ['mesh_network_pdu'] + ['mesh_network_pdu', 'mesh_network_key'] ] -""" -""" - btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') file_name = btstack_root + "/src/btstack_memory" print ('Generating %s.[h|c]' % file_name)