Skip to content

Commit 46a95f1

Browse files
tsvehagenjhedberg
authored andcommitted
Bluetooth: Mesh: Add database for managing nodes and keys
Refactor the handling of network nodes and their keys into a separate Mesh Configuration Database (CDB). This, not only creates a separation of the local node and the other nodes, but also makes it possible to implement functions to manage the whole, or at least parts of the mesh network. Signed-off-by: Tobias Svehagen <[email protected]>
1 parent 8df133e commit 46a95f1

File tree

16 files changed

+1689
-274
lines changed

16 files changed

+1689
-274
lines changed

include/bluetooth/mesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@
2121
#include <bluetooth/mesh/cfg_cli.h>
2222
#include <bluetooth/mesh/health_cli.h>
2323
#include <bluetooth/mesh/proxy.h>
24+
#include <bluetooth/mesh/cdb.h>
2425

2526
#endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_H_ */

include/bluetooth/mesh/cdb.h

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
/*
2+
* Copyright (c) 2019 Tobias Svehagen
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef ZEPHYR_INCLUDE_BLUETOOTH_MESH_CDB_H_
7+
#define ZEPHYR_INCLUDE_BLUETOOTH_MESH_CDB_H_
8+
9+
#if defined(CONFIG_BT_MESH_CDB)
10+
#define NODE_COUNT CONFIG_BT_MESH_CDB_NODE_COUNT
11+
#define SUBNET_COUNT CONFIG_BT_MESH_CDB_SUBNET_COUNT
12+
#define APP_KEY_COUNT CONFIG_BT_MESH_CDB_APP_KEY_COUNT
13+
#else
14+
#define NODE_COUNT 0
15+
#define SUBNET_COUNT 0
16+
#define APP_KEY_COUNT 0
17+
#endif
18+
19+
enum {
20+
BT_MESH_CDB_NODE_CONFIGURED,
21+
BT_MESH_CDB_NODE_BLACKLISTED,
22+
23+
BT_MESH_CDB_NODE_FLAG_COUNT
24+
};
25+
26+
struct bt_mesh_cdb_node {
27+
u8_t uuid[16];
28+
u16_t addr;
29+
u16_t net_idx;
30+
u8_t num_elem;
31+
u8_t dev_key[16];
32+
33+
ATOMIC_DEFINE(flags, BT_MESH_CDB_NODE_FLAG_COUNT);
34+
};
35+
36+
struct bt_mesh_cdb_subnet {
37+
u16_t net_idx;
38+
39+
bool kr_flag;
40+
u8_t kr_phase;
41+
42+
struct {
43+
u8_t net_key[16];
44+
} keys[2];
45+
};
46+
47+
struct bt_mesh_cdb_app_key {
48+
u16_t net_idx;
49+
u16_t app_idx;
50+
51+
struct {
52+
u8_t app_key[16];
53+
} keys[2];
54+
};
55+
56+
enum {
57+
BT_MESH_CDB_VALID,
58+
BT_MESH_CDB_SUBNET_PENDING,
59+
BT_MESH_CDB_KEYS_PENDING,
60+
BT_MESH_CDB_NODES_PENDING,
61+
BT_MESH_CDB_IVU_IN_PROGRESS,
62+
63+
BT_MESH_CDB_FLAG_COUNT,
64+
};
65+
66+
struct bt_mesh_cdb {
67+
u32_t iv_index;
68+
69+
ATOMIC_DEFINE(flags, BT_MESH_CDB_FLAG_COUNT);
70+
71+
struct bt_mesh_cdb_node nodes[NODE_COUNT];
72+
struct bt_mesh_cdb_subnet subnets[SUBNET_COUNT];
73+
struct bt_mesh_cdb_app_key app_keys[APP_KEY_COUNT];
74+
};
75+
76+
extern struct bt_mesh_cdb bt_mesh_cdb;
77+
78+
/** @brief Create the Mesh Configuration Database.
79+
*
80+
* Create and initialize the Mesh Configuration Database. A primary subnet,
81+
* ie one with NetIdx 0, will be added and the provided key will be used as
82+
* NetKey for that subnet.
83+
*
84+
* @param key The NetKey to be used for the primary subnet.
85+
*
86+
* @return 0 on success or negative error code on failure.
87+
*/
88+
int bt_mesh_cdb_create(const u8_t key[16]);
89+
90+
/** @brief Clear the Mesh Configuration Database.
91+
*
92+
* Remove all nodes, subnets and app-keys stored in the database and mark
93+
* the database as invalid. The data will be cleared from persistent storage
94+
* if CONFIG_BT_SETTINGS is enabled.
95+
*/
96+
void bt_mesh_cdb_clear(void);
97+
98+
/** @brief Set and store the IV Index and IV Update flag.
99+
*
100+
* The IV Index stored in the CDB will be the one used during provisioning
101+
* of new nodes. This function is generally only used from inside the stack.
102+
*
103+
* This function will store the data to persistent storage if
104+
* CONFIG_BT_SETTINGS is enabled.
105+
*
106+
* @param iv_index The new IV Index to use.
107+
* @param iv_update True if there is an ongoing IV Update procedure.
108+
*/
109+
void bt_mesh_cdb_iv_update(u32_t iv_index, bool iv_update);
110+
111+
/** @brief Allocate a node.
112+
*
113+
* Allocate a new node in the CDB.
114+
*
115+
* @param uuid UUID of the node.
116+
* @param addr Address of the node's primary element. If 0, the lowest
117+
* possible address available will be assigned to the node.
118+
* @param num_elem Number of elements that the node has.
119+
* @param net_idx NetIdx that the node was provisioned to.
120+
*
121+
* @return The new node or NULL if it cannot be allocated.
122+
*/
123+
struct bt_mesh_cdb_node *bt_mesh_cdb_node_alloc(const u8_t uuid[16], u16_t addr,
124+
u8_t num_elem, u16_t net_idx);
125+
126+
/** @brief Delete a node.
127+
*
128+
* Delete a node from the CDB.
129+
*
130+
* @param node The node to be deleted.
131+
* @param store If true, the node will be cleared from persistent storage.
132+
*/
133+
void bt_mesh_cdb_node_del(struct bt_mesh_cdb_node *node, bool store);
134+
135+
/** @brief Get a node by address.
136+
*
137+
* Try to find the node that has the provided address assigned to one of its
138+
* elements.
139+
*
140+
* @param addr Address of the element to look for.
141+
*
142+
* @return The node that has an element with address addr or NULL if no such
143+
* node exists.
144+
*/
145+
struct bt_mesh_cdb_node *bt_mesh_cdb_node_get(u16_t addr);
146+
147+
/** @brief Store node to persistent storage.
148+
*
149+
* @param node Node to be stored.
150+
*/
151+
void bt_mesh_cdb_node_store(const struct bt_mesh_cdb_node *node);
152+
153+
enum {
154+
BT_MESH_CDB_ITER_STOP = 0,
155+
BT_MESH_CDB_ITER_CONTINUE,
156+
};
157+
158+
/** @typedef bt_mesh_cdb_node_func_t
159+
* @brief Node iterator callback.
160+
*
161+
* @param node Node found.
162+
* @param user_data Data given.
163+
*
164+
* @return BT_MESH_CDB_ITER_CONTINUE to continue to iterate through the nodes
165+
* or BT_MESH_CDB_ITER_STOP to stop.
166+
*/
167+
typedef u8_t (*bt_mesh_cdb_node_func_t)(struct bt_mesh_cdb_node *node,
168+
void *user_data);
169+
170+
/** @brief Node iterator.
171+
*
172+
* Iterate nodes in the Mesh Configuration Database. The callback function
173+
* will only be called for valid, ie allocated, nodes.
174+
*
175+
* @param func Callback function.
176+
* @param user_data Data to pass to the callback.
177+
*/
178+
void bt_mesh_cdb_node_foreach(bt_mesh_cdb_node_func_t func, void *user_data);
179+
180+
/** @brief Allocate a subnet.
181+
*
182+
* Allocate a new subnet in the CDB.
183+
*
184+
* @param net_idx NetIdx of the subnet.
185+
*
186+
* @return The new subnet or NULL if it cannot be allocated.
187+
*/
188+
struct bt_mesh_cdb_subnet *bt_mesh_cdb_subnet_alloc(u16_t net_idx);
189+
190+
/** @brief Delete a subnet.
191+
*
192+
* Delete a subnet from the CDB.
193+
*
194+
* @param sub The subnet to be deleted.
195+
* @param store If true, the subnet will be cleared from persistent storage.
196+
*/
197+
void bt_mesh_cdb_subnet_del(struct bt_mesh_cdb_subnet *sub, bool store);
198+
199+
/** @brief Get a subnet by NetIdx
200+
*
201+
* Try to find the subnet with the specified NetIdx.
202+
*
203+
* @param net_idx NetIdx of the subnet to look for.
204+
*
205+
* @return The subnet with the specified NetIdx or NULL if no such subnet
206+
* exists.
207+
*/
208+
struct bt_mesh_cdb_subnet *bt_mesh_cdb_subnet_get(u16_t net_idx);
209+
210+
/** @brief Store subnet to persistent storage.
211+
*
212+
* @param sub Subnet to be stored.
213+
*/
214+
void bt_mesh_cdb_subnet_store(const struct bt_mesh_cdb_subnet *sub);
215+
216+
/** @brief Get the flags for a subnet
217+
*
218+
* @param sub The subnet to get flags for.
219+
*
220+
* @return The flags for the subnet.
221+
*/
222+
u8_t bt_mesh_cdb_subnet_flags(const struct bt_mesh_cdb_subnet *sub);
223+
224+
225+
/** @brief Allocate an application key.
226+
*
227+
* Allocate a new application key in the CDB.
228+
*
229+
* @param net_idx NetIdx of NetKey that the application key is bound to.
230+
* @param app_idx AppIdx of the application key.
231+
*
232+
* @return The new application key or NULL if it cannot be allocated.
233+
*/
234+
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_alloc(u16_t net_idx,
235+
u16_t app_idx);
236+
237+
/** @brief Delete an application key.
238+
*
239+
* Delete an application key from the CDB.
240+
*
241+
* @param key The application key to be deleted.
242+
* @param store If true, the key will be cleared from persistent storage.
243+
*/
244+
void bt_mesh_cdb_app_key_del(struct bt_mesh_cdb_app_key *key, bool store);
245+
246+
/** @brief Get an application key by AppIdx
247+
*
248+
* Try to find the application key with the specified AppIdx.
249+
*
250+
* @param app_idx AppIdx of the application key to look for.
251+
*
252+
* @return The application key with the specified AppIdx or NULL if no such key
253+
* exists.
254+
*/
255+
struct bt_mesh_cdb_app_key *bt_mesh_cdb_app_key_get(u16_t app_idx);
256+
257+
/** @brief Store application key to persistent storage.
258+
*
259+
* @param key Application key to be stored.
260+
*/
261+
void bt_mesh_cdb_app_key_store(const struct bt_mesh_cdb_app_key *key);
262+
263+
#endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_CDB_H_ */

include/bluetooth/mesh/main.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,12 @@ struct bt_mesh_prov {
190190
* the specified NetKeyIndex and primary element address.
191191
*
192192
* @param net_idx NetKeyIndex given during provisioning.
193+
* @param uuid UUID of the added node
193194
* @param addr Primary element address.
194195
* @param num_elem Number of elements that this node has.
195196
*/
196-
void (*node_added)(u16_t net_idx, u16_t addr, u8_t num_elem);
197+
void (*node_added)(u16_t net_idx, u8_t uuid[16], u16_t addr,
198+
u8_t num_elem);
197199

198200
/** @brief Node has been reset.
199201
*

subsys/bluetooth/mesh/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@ zephyr_library_sources_ifdef(CONFIG_BT_MESH_SELF_TEST test.c)
3333

3434
zephyr_library_sources_ifdef(CONFIG_BT_MESH_SHELL shell.c)
3535

36-
zephyr_library_sources_ifdef(CONFIG_BT_MESH_PROVISIONER nodes.c)
37-
36+
zephyr_library_sources_ifdef(CONFIG_BT_MESH_CDB cdb.c)

subsys/bluetooth/mesh/Kconfig

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,36 @@ config BT_MESH_PROVISIONER
3737
help
3838
Enable this option to have support for provisioning remote devices.
3939

40-
if BT_MESH_PROVISIONER
40+
config BT_MESH_CDB
41+
bool "Mesh Configuration Database [EXPERIMENTAL]"
4142

42-
config BT_MESH_NODE_COUNT
43-
int "Maximum number of saved nodes per network"
43+
if BT_MESH_CDB
44+
45+
config BT_MESH_CDB_NODE_COUNT
46+
int "Maximum number of nodes in the database"
4447
default 1
4548
range 1 4096
4649
help
4750
This option specifies how many nodes each network can at most
48-
save in the provisioning database.
51+
save in the configuration database.
4952

50-
endif # BT_MESH_PROVISIONER
53+
config BT_MESH_CDB_SUBNET_COUNT
54+
int "Maximum number of subnets in the database"
55+
default 1
56+
range 1 4096
57+
help
58+
This option specifies how many subnets that can at most be
59+
saved in the configuration database.
60+
61+
config BT_MESH_CDB_APP_KEY_COUNT
62+
int "Maximum number of application keys in the database"
63+
default 1
64+
range 1 4096
65+
help
66+
This option specifies how many application keys that can at most
67+
be saved in the configuration database.
68+
69+
endif # BT_MESH_CDB
5170

5271
if BT_CONN
5372

@@ -591,6 +610,12 @@ config BT_MESH_DEBUG_SETTINGS
591610
help
592611
Use this option to enable persistent settings debug logs.
593612

613+
config BT_MESH_DEBUG_CDB
614+
bool "Configuration database debug"
615+
depends on BT_MESH_CDB
616+
help
617+
Use this option to enable configuration database debug logs.
618+
594619
endif # BT_MESH_DEBUG
595620

596621
endif # BT_MESH

0 commit comments

Comments
 (0)