|
| 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_ */ |
0 commit comments