cdb.h 7.1 KB

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