cfg.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /** @file
  2. * @brief Runtime configuration APIs.
  3. */
  4. /*
  5. * Copyright (c) 2020 Nordic Semiconductor
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. */
  9. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_MESH_CFG_H_
  10. #define ZEPHYR_INCLUDE_BLUETOOTH_MESH_CFG_H_
  11. #include <stdbool.h>
  12. #include <stddef.h>
  13. #include <sys/types.h>
  14. /**
  15. * @brief Runtime Configuration
  16. * @defgroup bt_mesh_cfg Runtime Configuration
  17. * @ingroup bt_mesh
  18. * @{
  19. */
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /** Bluetooth mesh feature states */
  24. enum bt_mesh_feat_state {
  25. /** Feature is supported, but disabled. */
  26. BT_MESH_FEATURE_DISABLED,
  27. /** Feature is supported and enabled. */
  28. BT_MESH_FEATURE_ENABLED,
  29. /** Feature is not supported, and cannot be enabled. */
  30. BT_MESH_FEATURE_NOT_SUPPORTED,
  31. };
  32. /* Key Refresh Phase */
  33. #define BT_MESH_KR_NORMAL 0x00
  34. #define BT_MESH_KR_PHASE_1 0x01
  35. #define BT_MESH_KR_PHASE_2 0x02
  36. #define BT_MESH_KR_PHASE_3 0x03
  37. /* Legacy feature defines */
  38. #define BT_MESH_RELAY_DISABLED BT_MESH_FEATURE_DISABLED
  39. #define BT_MESH_RELAY_ENABLED BT_MESH_FEATURE_ENABLED
  40. #define BT_MESH_RELAY_NOT_SUPPORTED BT_MESH_FEATURE_NOT_SUPPORTED
  41. #define BT_MESH_BEACON_DISABLED BT_MESH_FEATURE_DISABLED
  42. #define BT_MESH_BEACON_ENABLED BT_MESH_FEATURE_ENABLED
  43. #define BT_MESH_GATT_PROXY_DISABLED BT_MESH_FEATURE_DISABLED
  44. #define BT_MESH_GATT_PROXY_ENABLED BT_MESH_FEATURE_ENABLED
  45. #define BT_MESH_GATT_PROXY_NOT_SUPPORTED BT_MESH_FEATURE_NOT_SUPPORTED
  46. #define BT_MESH_FRIEND_DISABLED BT_MESH_FEATURE_DISABLED
  47. #define BT_MESH_FRIEND_ENABLED BT_MESH_FEATURE_ENABLED
  48. #define BT_MESH_FRIEND_NOT_SUPPORTED BT_MESH_FEATURE_NOT_SUPPORTED
  49. #define BT_MESH_NODE_IDENTITY_STOPPED BT_MESH_FEATURE_DISABLED
  50. #define BT_MESH_NODE_IDENTITY_RUNNING BT_MESH_FEATURE_ENABLED
  51. #define BT_MESH_NODE_IDENTITY_NOT_SUPPORTED BT_MESH_FEATURE_NOT_SUPPORTED
  52. /** @brief Enable or disable sending of the Secure Network Beacon.
  53. *
  54. * @param beacon New Secure Network Beacon state.
  55. */
  56. void bt_mesh_beacon_set(bool beacon);
  57. /** @brief Get the current Secure Network Beacon state.
  58. *
  59. * @returns Whether the Secure Network Beacon feature is enabled.
  60. */
  61. bool bt_mesh_beacon_enabled(void);
  62. /** @brief Set the default TTL value.
  63. *
  64. * The default TTL value is used when no explicit TTL value is set. Models will
  65. * use the default TTL value when @ref bt_mesh_msg_ctx::send_ttl is
  66. * @ref BT_MESH_TTL_DEFAULT.
  67. *
  68. * @param default_ttl The new default TTL value. Valid values are 0x00 and 0x02
  69. * to @ref BT_MESH_TTL_MAX.
  70. *
  71. * @retval 0 Successfully set the default TTL value.
  72. * @retval -EINVAL Invalid TTL value.
  73. */
  74. int bt_mesh_default_ttl_set(uint8_t default_ttl);
  75. /** @brief Get the current default TTL value.
  76. *
  77. * @return The current default TTL value.
  78. */
  79. uint8_t bt_mesh_default_ttl_get(void);
  80. /** @brief Set the Network Transmit parameters.
  81. *
  82. * The Network Transmit parameters determine the parameters local messages are
  83. * transmitted with.
  84. *
  85. * @see BT_MESH_TRANSMIT
  86. *
  87. * @param xmit New Network Transmit parameters. Use @ref BT_MESH_TRANSMIT for
  88. * encoding.
  89. */
  90. void bt_mesh_net_transmit_set(uint8_t xmit);
  91. /** @brief Get the current Network Transmit parameters.
  92. *
  93. * The @ref BT_MESH_TRANSMIT_COUNT and @ref BT_MESH_TRANSMIT_INT macros can be
  94. * used to decode the Network Transmit parameters.
  95. *
  96. * @return The current Network Transmit parameters.
  97. */
  98. uint8_t bt_mesh_net_transmit_get(void);
  99. /** @brief Configure the Relay feature.
  100. *
  101. * Enable or disable the Relay feature, and configure the parameters to
  102. * transmit relayed messages with.
  103. *
  104. * Support for the Relay feature must be enabled through the
  105. * @c CONFIG_BT_MESH_RELAY configuration option.
  106. *
  107. * @see BT_MESH_TRANSMIT
  108. *
  109. * @param relay New Relay feature state. Must be one of
  110. * @ref BT_MESH_FEATURE_ENABLED and
  111. * @ref BT_MESH_FEATURE_DISABLED.
  112. * @param xmit New Relay retransmit parameters. Use @ref BT_MESH_TRANSMIT for
  113. * encoding.
  114. *
  115. * @retval 0 Successfully changed the Relay configuration.
  116. * @retval -ENOTSUP The Relay feature is not supported.
  117. * @retval -EINVAL Invalid parameter.
  118. * @retval -EALREADY Already using the given parameters.
  119. */
  120. int bt_mesh_relay_set(enum bt_mesh_feat_state relay, uint8_t xmit);
  121. /** @brief Get the current Relay feature state.
  122. *
  123. * @returns The Relay feature state.
  124. */
  125. enum bt_mesh_feat_state bt_mesh_relay_get(void);
  126. /** @brief Get the current Relay Retransmit parameters.
  127. *
  128. * The @ref BT_MESH_TRANSMIT_COUNT and @ref BT_MESH_TRANSMIT_INT macros can be
  129. * used to decode the Relay Retransmit parameters.
  130. *
  131. * @return The current Relay Retransmit parameters, or 0 if relay is not
  132. * supported.
  133. */
  134. uint8_t bt_mesh_relay_retransmit_get(void);
  135. /** @brief Enable or disable the GATT Proxy feature.
  136. *
  137. * Support for the GATT Proxy feature must be enabled through the
  138. * @c CONFIG_BT_MESH_GATT_PROXY configuration option.
  139. *
  140. * @note The GATT Proxy feature only controls a Proxy node's ability to relay
  141. * messages to the mesh network. A node that supports GATT Proxy will
  142. * still advertise Connectable Proxy beacons, even if the feature is
  143. * disabled. The Proxy feature can only be fully disabled through compile
  144. * time configuration.
  145. *
  146. * @param gatt_proxy New GATT Proxy state. Must be one of
  147. * @ref BT_MESH_FEATURE_ENABLED and
  148. * @ref BT_MESH_FEATURE_DISABLED.
  149. *
  150. * @retval 0 Successfully changed the GATT Proxy feature state.
  151. * @retval -ENOTSUP The GATT Proxy feature is not supported.
  152. * @retval -EINVAL Invalid parameter.
  153. * @retval -EALREADY Already in the given state.
  154. */
  155. int bt_mesh_gatt_proxy_set(enum bt_mesh_feat_state gatt_proxy);
  156. /** @brief Get the current GATT Proxy state.
  157. *
  158. * @returns The GATT Proxy feature state.
  159. */
  160. enum bt_mesh_feat_state bt_mesh_gatt_proxy_get(void);
  161. /** @brief Enable or disable the Friend feature.
  162. *
  163. * Any active friendships will be terminated immediately if the Friend feature
  164. * is disabled.
  165. *
  166. * Support for the Friend feature must be enabled through the
  167. * @c CONFIG_BT_MESH_FRIEND configuration option.
  168. *
  169. * @param friendship New Friend feature state. Must be one of
  170. * @ref BT_MESH_FEATURE_ENABLED and
  171. * @ref BT_MESH_FEATURE_DISABLED.
  172. *
  173. * @retval 0 Successfully changed the Friend feature state.
  174. * @retval -ENOTSUP The Friend feature is not supported.
  175. * @retval -EINVAL Invalid parameter.
  176. * @retval -EALREADY Already in the given state.
  177. */
  178. int bt_mesh_friend_set(enum bt_mesh_feat_state friendship);
  179. /** @brief Get the current Friend state.
  180. *
  181. * @returns The Friend feature state.
  182. */
  183. enum bt_mesh_feat_state bt_mesh_friend_get(void);
  184. /**
  185. * @brief Subnet Configuration
  186. * @defgroup bt_mesh_cfg_subnet Subnet Configuration
  187. * @{
  188. */
  189. /** @brief Add a Subnet.
  190. *
  191. * Adds a subnet with the given network index and network key to the list of
  192. * known Subnets. All messages sent on the given Subnet will be processed by
  193. * this node, and the node may send and receive Network Beacons on the given
  194. * Subnet.
  195. *
  196. * @param net_idx Network index.
  197. * @param key Root network key of the Subnet. All other keys are derived
  198. * from this.
  199. *
  200. * @retval STATUS_SUCCESS The Subnet was successfully added.
  201. * @retval STATUS_INSUFF_RESOURCES No room for this Subnet.
  202. * @retval STATUS_UNSPECIFIED The Subnet couldn't be created for some reason.
  203. */
  204. uint8_t bt_mesh_subnet_add(uint16_t net_idx, const uint8_t key[16]);
  205. /** @brief Update the given Subnet.
  206. *
  207. * Starts the Key Refresh procedure for this Subnet by adding a second set of
  208. * encryption keys. The Subnet will continue sending with the old key (but
  209. * receiving messages using both) until the Subnet enters Key Refresh phase 2.
  210. *
  211. * This allows a network configurator to replace old network and application
  212. * keys for the entire network, effectively removing access for all nodes that
  213. * aren't given the new keys.
  214. *
  215. * @param net_idx Network index.
  216. * @param key New root network key of the Subnet.
  217. *
  218. * @retval STATUS_SUCCESS The Subnet was updated with a second key.
  219. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  220. * @retval STATUS_IDX_ALREADY_STORED The @c key value is the same as the
  221. * current key.
  222. * @retval STATUS_CANNOT_UPDATE The Subnet cannot be updated for some reason.
  223. */
  224. uint8_t bt_mesh_subnet_update(uint16_t net_idx, const uint8_t key[16]);
  225. /** @brief Delete a Subnet.
  226. *
  227. * Removes the Subnet with the given network index from the node. The node will
  228. * stop sending Network Beacons with the given Subnet, and can no longer
  229. * process messages on this Subnet.
  230. *
  231. * All Applications bound to this Subnet are also deleted.
  232. *
  233. * @param net_idx Network index.
  234. *
  235. * @retval STATUS_SUCCESS The Subnet was deleted.
  236. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  237. */
  238. uint8_t bt_mesh_subnet_del(uint16_t net_idx);
  239. /** @brief Check whether a Subnet is known.
  240. *
  241. * @param net_idx Network index
  242. *
  243. * @return true if a Subnet with the given index exists, false otherwise.
  244. */
  245. bool bt_mesh_subnet_exists(uint16_t net_idx);
  246. /** @brief Set the Subnet's Key Refresh phase.
  247. *
  248. * The Key Refresh procedure is started by updating the Subnet keys through
  249. * @ref bt_mesh_subnet_update. This puts the Subnet in Key Refresh Phase 1.
  250. * Once all nodes have received the new Subnet key, Key Refresh Phase 2 can be
  251. * activated through this function to start transmitting with the new network
  252. * key. Finally, to revoke the old key, set the Key Refresh Phase to 3. This
  253. * removes the old keys from the node, and returns the Subnet back to normal
  254. * single-key operation with the new key set.
  255. *
  256. * @param net_idx Network index.
  257. * @param phase Pointer to the new Key Refresh phase. Will return the actual
  258. * Key Refresh phase after updating.
  259. *
  260. * @retval STATUS_SUCCESS The Key Refresh phase of the Subnet was successfully
  261. * changed.
  262. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  263. * @retval STATUS_CANNOT_UPDATE The given phase change is invalid.
  264. */
  265. uint8_t bt_mesh_subnet_kr_phase_set(uint16_t net_idx, uint8_t *phase);
  266. /** @brief Get the Subnet's Key Refresh phase.
  267. *
  268. * @param net_idx Network index.
  269. * @param phase Pointer to the Key Refresh variable to fill.
  270. *
  271. * @retval STATUS_SUCCESS Successfully populated the @c phase variable.
  272. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  273. */
  274. uint8_t bt_mesh_subnet_kr_phase_get(uint16_t net_idx, uint8_t *phase);
  275. /** @brief Set the Node Identity state of the Subnet.
  276. *
  277. * The Node Identity state of a Subnet determines whether the Subnet advertises
  278. * connectable Node Identity beacons for Proxy Clients to connect to.
  279. * Once started, the Node Identity beacon runs for 60 seconds, or until it is
  280. * stopped.
  281. *
  282. * This function serves the same purpose as @ref bt_mesh_proxy_identity_enable,
  283. * but only acts on a single Subnet.
  284. *
  285. * GATT Proxy support must be enabled through
  286. * @kconfig{CONFIG_BT_MESH_GATT_PROXY}.
  287. *
  288. * @param net_idx Network index.
  289. * @param node_id New Node Identity state, must be either @ref
  290. * BT_MESH_FEATURE_ENABLED or @ref BT_MESH_FEATURE_DISABLED.
  291. *
  292. * @retval STATUS_SUCCESS Successfully set the Node Identity state of the
  293. * Subnet.
  294. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  295. * @retval STATUS_FEAT_NOT_SUPP The Node Identity feature is not supported.
  296. * @retval STATUS_CANNOT_SET Couldn't set the Node Identity state.
  297. */
  298. uint8_t bt_mesh_subnet_node_id_set(uint16_t net_idx,
  299. enum bt_mesh_feat_state node_id);
  300. /** @brief Get the Node Identity state of the Subnet.
  301. *
  302. * @param net_idx Network index.
  303. * @param node_id Node Identity variable to fill.
  304. *
  305. * @retval STATUS_SUCCESS Successfully populated the @c node_id variable.
  306. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  307. */
  308. uint8_t bt_mesh_subnet_node_id_get(uint16_t net_idx,
  309. enum bt_mesh_feat_state *node_id);
  310. /** @brief Get a list of all known Subnet indexes.
  311. *
  312. * Builds a list of all known Subnet indexes in the @c net_idxs array.
  313. * If the @c net_idxs array is smaller than the list of known Subnets, this
  314. * function fills all available entries and returns @c -ENOMEM. In this
  315. * case, the next @c max entries of the list can be read out by calling
  316. * @code
  317. * bt_mesh_subnets_get(list, max, max);
  318. * @endcode
  319. *
  320. * Note that any changes to the Subnet list between calls to this function
  321. * could change the order and number of entries in the list.
  322. *
  323. * @param net_idxs Array to fill.
  324. * @param max Max number of indexes to return.
  325. * @param skip Number of indexes to skip. Enables batched processing of the
  326. * list.
  327. *
  328. * @return The number of indexes added to the @c net_idxs array, or @c -ENOMEM
  329. * if the number of known Subnets exceeds the @c max parameter.
  330. */
  331. ssize_t bt_mesh_subnets_get(uint16_t net_idxs[], size_t max, off_t skip);
  332. /**
  333. * @}
  334. */
  335. /**
  336. * @brief Application Configuration
  337. * @defgroup bt_mesh_cfg_app Application Configuration
  338. * @{
  339. */
  340. /** @brief Add an Application key.
  341. *
  342. * Adds the Application with the given index to the list of known applications.
  343. * Allows the node to send and receive model messages encrypted with this
  344. * Application key.
  345. *
  346. * Every Application is bound to a specific Subnet. The node must know the
  347. * Subnet the Application is bound to before it can add the Application.
  348. *
  349. * @param app_idx Application index.
  350. * @param net_idx Network index the Application is bound to.
  351. * @param key Application key value.
  352. *
  353. * @retval STATUS_SUCCESS The Application was successfully added.
  354. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  355. * @retval STATUS_INSUFF_RESOURCES There's no room for storing this
  356. * Application.
  357. * @retval STATUS_INVALID_BINDING This AppIdx is already bound to another
  358. * Subnet.
  359. * @retval STATUS_IDX_ALREADY_STORED This AppIdx is already stored with a
  360. * different key value.
  361. * @retval STATUS_CANNOT_SET Cannot set the Application key for some reason.
  362. */
  363. uint8_t bt_mesh_app_key_add(uint16_t app_idx, uint16_t net_idx,
  364. const uint8_t key[16]);
  365. /** @brief Update an Application key.
  366. *
  367. * Update an Application with a second Application key, as part of the
  368. * Key Refresh procedure of the bound Subnet. The node will continue
  369. * transmitting with the old application key (but receiving on both) until the
  370. * Subnet enters Key Refresh phase 2. Once the Subnet enters Key Refresh phase
  371. * 3, the old application key will be deleted.
  372. *
  373. * @note The Application key can only be updated if the bound Subnet is in Key
  374. * Refresh phase 1.
  375. *
  376. * @param app_idx Application index.
  377. * @param net_idx Network index the Application is bound to, or
  378. * @ref BT_MESH_KEY_ANY to skip the binding check.
  379. * @param key New key value.
  380. *
  381. * @retval STATUS_SUCCESS The Application key was successfully updated.
  382. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  383. * @retval STATUS_INVALID_BINDING This AppIdx is not bound to the given NetIdx.
  384. * @retval STATUS_CANNOT_UPDATE The Application key cannot be updated for some
  385. * reason.
  386. * @retval STATUS_IDX_ALREADY_STORED This AppIdx is already updated with a
  387. * different key value.
  388. */
  389. uint8_t bt_mesh_app_key_update(uint16_t app_idx, uint16_t net_idx,
  390. const uint8_t key[16]);
  391. /** @brief Delete an Application key.
  392. *
  393. * All models bound to this application will remove this binding.
  394. * All models publishing with this application will stop publishing.
  395. *
  396. * @param app_idx Application index.
  397. * @param net_idx Network index.
  398. *
  399. * @retval STATUS_SUCCESS The Application key was successfully deleted.
  400. * @retval STATUS_INVALID_NETKEY The NetIdx is unknown.
  401. * @retval STATUS_INVALID_BINDING This AppIdx is not bound to the given NetIdx.
  402. */
  403. uint8_t bt_mesh_app_key_del(uint16_t app_idx, uint16_t net_idx);
  404. /** @brief Check if an Application key is known.
  405. *
  406. * @param app_idx Application index.
  407. *
  408. * @return true if the Application is known, false otherwise.
  409. */
  410. bool bt_mesh_app_key_exists(uint16_t app_idx);
  411. /** @brief Get a list of all known Application key indexes.
  412. *
  413. * Builds a list of all Application indexes for the given network index in the
  414. * @c app_idxs array. If the @c app_idxs array cannot fit all bound
  415. * Applications, this function fills all available entries and returns @c
  416. * -ENOMEM. In this case, the next @c max entries of the list can be read out
  417. * by calling
  418. * @code
  419. * bt_mesh_app_keys_get(net_idx, list, max, max);
  420. * @endcode
  421. *
  422. * Note that any changes to the Application key list between calls to this
  423. * function could change the order and number of entries in the list.
  424. *
  425. * @param net_idx Network Index to get the Applications of, or @ref
  426. * BT_MESH_KEY_ANY to get all Applications.
  427. * @param app_idxs Array to fill.
  428. * @param max Max number of indexes to return.
  429. * @param skip Number of indexes to skip. Enables batched processing of the
  430. * list.
  431. *
  432. * @return The number of indexes added to the @c app_idxs array, or @c -ENOMEM
  433. * if the number of known Applications exceeds the @c max parameter.
  434. */
  435. ssize_t bt_mesh_app_keys_get(uint16_t net_idx, uint16_t app_idxs[], size_t max,
  436. off_t skip);
  437. /**
  438. * @}
  439. */
  440. #ifdef __cplusplus
  441. }
  442. #endif
  443. /**
  444. * @}
  445. */
  446. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_MESH_CFG_H_ */