nrf_ble_bms.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup ble_bms Bond Management Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Bond Management Service (BMS) module.
  46. *
  47. * @details This module implements the Bond Management Service (BMS).
  48. * By writing to the Bond Management Control Point, the connected peer can request the deletion of
  49. * bond information from the device.
  50. * If authorization is configured, the application must supply an event handler for receiving Bond
  51. * Management Service events. Using this handler, the service requests authorization when
  52. * a procedure is requested by writing to the Bond Management Control Point.
  53. *
  54. * @msc
  55. * hscale = "1.3";
  56. * APP,BMS,PEER;
  57. * |||;
  58. * APP rbox PEER [label="Connection established"];
  59. * |||;
  60. * BMS<=PEER [label="BMCP write request {procedure}"];
  61. * APP<-BMS [label="NRF_BLE_BMS_EVT_AUTH {auth_code}"];
  62. * --- [label="Variant #1: app grants authorization"];
  63. * APP->BMS [label="nrf_ble_bms_auth_reponse (true)"];
  64. * BMS>>APP [label="NRF_SUCCESS"];
  65. * BMS=>PEER [label="BMCP write response"];
  66. * BMS rbox BMS [label="Procedure initiated"];
  67. * --- [label="Variant #2: app denies authorization"];
  68. * APP->BMS [label="nrf_ble_bms_auth_reponse (false)"];
  69. * BMS>>APP [label="NRF_SUCCESS"];
  70. * BMS=>PEER [label="BMCP error response"];
  71. * @endmsc
  72. *
  73. * @note The application must register this module as BLE event observer using the
  74. * NRF_SDH_BLE_OBSERVER macro. Example:
  75. * @code
  76. * nrf_ble_bms_t instance;
  77. * NRF_SDH_BLE_OBSERVER(anything, NRF_BLE_BMS_BLE_OBSERVER_PRIO,
  78. * nrf_ble_bms_on_ble_evt, &instance);
  79. * @endcode
  80. */
  81. #ifndef NRF_BLE_BMS_H__
  82. #define NRF_BLE_BMS_H__
  83. #include <stdint.h>
  84. #include <stdbool.h>
  85. #include "ble.h"
  86. #include "ble_srv_common.h"
  87. #include "nrf_ble_qwr.h"
  88. #include "nrf_sdh_ble.h"
  89. #ifdef __cplusplus
  90. extern "C" {
  91. #endif
  92. /**@brief Macro for defining a nrf_ble_bms instance.
  93. *
  94. * @param _name Name of the instance.
  95. * @hideinitializer
  96. */
  97. #define NRF_BLE_BMS_DEF(_name) \
  98. static nrf_ble_bms_t _name; \
  99. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  100. NRF_BLE_BMS_BLE_OBSERVER_PRIO, \
  101. nrf_ble_bms_on_ble_evt, &_name)
  102. #define NRF_BLE_BMS_FEATURE_LEN 3 //!< Length of the Feature Characteristic (in bytes).
  103. #define NRF_BLE_BMS_CTRLPT_MAX_LEN 128 //!< Maximum length of the Bond Management Control Point Characteristic (in bytes).
  104. #define NRF_BLE_BMS_CTRLPT_MIN_LEN 1 //!< Minimum length of the Bond Management Control Point Characteristic (in bytes).
  105. #define NRF_BLE_BMS_AUTH_CODE_MAX_LEN NRF_BLE_BMS_CTRLPT_MAX_LEN - 1 //!< Maximum length of the Bond Management Control Point Authorization Code (in bytes).
  106. /** @defgroup NRF_BLE_BMS_FEATURES BMS feature bits
  107. * @{ */
  108. #define NRF_BLE_BMS_REQUESTING_DEVICE_BR_LE (0x01 << 0) //!< Delete bond of the requesting device (BR/EDR and LE).
  109. #define NRF_BLE_BMS_REQUESTING_DEVICE_BR_LE_AUTH_CODE (0x01 << 1) //!< Delete bond of the requesting device (BR/EDR and LE) with an authorization code.
  110. #define NRF_BLE_BMS_REQUESTING_DEVICE_BR (0x01 << 2) //!< Delete bond of the requesting device (BR/EDR transport only).
  111. #define NRF_BLE_BMS_REQUESTING_DEVICE_BR_AUTH_CODE (0x01 << 3) //!< Delete bond of the requesting device (BR/EDR transport only) with an authorization code.
  112. #define NRF_BLE_BMS_REQUESTING_DEVICE_LE (0x01 << 4) //!< Delete bond of the requesting device (LE transport only).
  113. #define NRF_BLE_BMS_REQUESTING_DEVICE_LE_AUTH_CODE (0x01 << 5) //!< Delete bond of the requesting device (LE transport only) with an authorization code.
  114. #define NRF_BLE_BMS_ALL_BONDS_BR_LE (0x01 << 6) //!< Delete all bonds on the device (BR/EDR and LE).
  115. #define NRF_BLE_BMS_ALL_BONDS_BR_LE_AUTH_CODE (0x01 << 7) //!< Delete all bonds on the device (BR/EDR and LE) with an authorization code.
  116. #define NRF_BLE_BMS_ALL_BONDS_BR (0x01 << 8) //!< Delete all bonds on the device (BR/EDR transport only).
  117. #define NRF_BLE_BMS_ALL_BONDS_BR_AUTH_CODE (0x01 << 9) //!< Delete all bonds on the device (BR/EDR transport only) with an authorization code.
  118. #define NRF_BLE_BMS_ALL_BONDS_LE (0x01 << 10) //!< Delete all bonds on the device (LE transport only).
  119. #define NRF_BLE_BMS_ALL_BONDS_LE_AUTH_CODE (0x01 << 11) //!< Delete all bonds on the device (LE transport only) with an authorization code.
  120. #define NRF_BLE_BMS_ALL_EXCEPT_REQUESTING_DEVICE_BR_LE (0x01 << 12) //!< Delete all bonds on the device except for the bond of the requesting device (BR/EDR and LE).
  121. #define NRF_BLE_BMS_ALL_EXCEPT_REQUESTING_DEVICE_BR_LE_AUTH_CODE (0x01 << 13) //!< Delete all bonds on the device except for the bond of the requesting device (BR/EDR and LE) with an authorization code.
  122. #define NRF_BLE_BMS_ALL_EXCEPT_REQUESTING_DEVICE_BR (0x01 << 14) //!< Delete all bonds on the device except for the bond of the requesting device (BR/EDR transport only).
  123. #define NRF_BLE_BMS_ALL_EXCEPT_REQUESTING_DEVICE_BR_AUTH_CODE (0x01 << 15) //!< Delete all bonds on the device except for the bond of the requesting device (BR/EDR transport only) with an authorization code.
  124. #define NRF_BLE_BMS_ALL_EXCEPT_REQUESTING_DEVICE_LE (0x01 << 16) //!< Delete all bonds on the device except for the bond of the requesting device (LE transport only).
  125. #define NRF_BLE_BMS_ALL_EXCEPT_REQUESTING_DEVICE_LE_AUTH_CODE (0x01 << 17) //!< Delete all bonds on the device except for the bond of the requesting device (LE transport only) with an authorization code.
  126. /** @} */
  127. #define NRF_BLE_BMS_OPCODE_NOT_SUPPORTED (BLE_GATT_STATUS_ATTERR_APP_BEGIN + 0) //!< Error sent back when receiving a control point write with an unsupported opcode.
  128. #define NRF_BLE_BMS_OPERATION_FAILED (BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1) //!< Error sent back when a control point operation fails.
  129. /**@brief Supported features. */
  130. typedef struct
  131. {
  132. bool delete_all : 1; //!< Indicates whether the application wants to support the operation to delete all bonds.
  133. bool delete_all_auth : 1; //!< Indicates whether the application wants to support the operation to delete all bonds with authorization code.
  134. bool delete_requesting : 1; //!< Indicates whether the application wants to support the operation to delete the bonds of the requesting device.
  135. bool delete_requesting_auth : 1; //!< Indicates whether the application wants to support the operation to delete the bonds of the requesting device with authorization code.
  136. bool delete_all_but_requesting : 1; //!< Indicates whether the application wants to support the operation to delete all bonds except for the bond of the requesting device.
  137. bool delete_all_but_requesting_auth : 1; //!< Indicates whether the application wants to support the operation to delete all bonds except for the bond of the requesting device with authorization code.
  138. } nrf_ble_bms_features_t;
  139. /**@brief BMS Control Point opcodes. */
  140. typedef enum
  141. {
  142. NRF_BLE_BMS_OP_DEL_BOND_REQ_DEVICE_BR_LE = 0x01, //!< Initiates the procedure to delete the bond of the requesting device on BR/EDR and LE transports.
  143. NRF_BLE_BMS_OP_DEL_BOND_REQ_DEVICE_BR_ONLY = 0x02, //!< Initiates the procedure to delete the bond of the requesting device on BR/EDR transport.
  144. NRF_BLE_BMS_OP_DEL_BOND_REQ_DEVICE_LE_ONLY = 0x03, //!< Initiates the procedure to delete the bond of the requesting device on LE transport.
  145. NRF_BLE_BMS_OP_DEL_ALL_BONDS_ON_SERVER_BR_LE = 0x04, //!< Initiates the procedure to delete all bonds on the device on BR/EDR and LE transports.
  146. NRF_BLE_BMS_OP_DEL_ALL_BONDS_ON_SERVER_BR_ONLY = 0x05, //!< Initiates the procedure to delete all bonds on the device on BR/EDR transport.
  147. NRF_BLE_BMS_OP_DEL_ALL_BONDS_ON_SERVER_LE_ONLY = 0x06, //!< Initiates the procedure to delete all bonds on the device on LE transport.
  148. NRF_BLE_BMS_OP_DEL_ALL_BUT_ACTIVE_BOND_BR_LE = 0x07, //!< Initiates the procedure to delete all bonds except for the one of the requesting device on BR/EDR and LE transports.
  149. NRF_BLE_BMS_OP_DEL_ALL_BUT_ACTIVE_BOND_BR_ONLY = 0x08, //!< Initiates the procedure to delete all bonds except for the one of the requesting device on BR/EDR transport.
  150. NRF_BLE_BMS_OP_DEL_ALL_BUT_ACTIVE_BOND_LE_ONLY = 0x09, //!< Initiates the procedure to delete all bonds except for the one of the requesting device on LE transport.
  151. NRF_BLE_BMS_OP_NONE = 0xFF //!< Indicates an invalid opcode or no pending opcode.
  152. } nrf_ble_bms_op_t;
  153. /**@brief Authorization status values. */
  154. typedef enum
  155. {
  156. NRF_BLE_BMS_AUTH_STATUS_ALLOWED, //!< Authorization is granted.
  157. NRF_BLE_BMS_AUTH_STATUS_DENIED, //!< Authorization is denied.
  158. NRF_BLE_BMS_AUTH_STATUS_PENDING //!< Authorization is pending.
  159. } nrf_ble_bms_auth_status_t;
  160. /**@brief Received authorization codes. */
  161. typedef struct
  162. {
  163. uint8_t code[NRF_BLE_BMS_AUTH_CODE_MAX_LEN]; //!< Authorization code.
  164. uint16_t len; //!< Length of the authorization code.
  165. } nrf_ble_bms_auth_code_t;
  166. /**@brief BMS event types. */
  167. typedef enum
  168. {
  169. NRF_BLE_BMS_EVT_AUTH, //!< Event that indicates that the application shall verify the supplied authentication code.
  170. } nrf_ble_bms_evt_type_t;
  171. /**@brief BMS events. */
  172. typedef struct
  173. {
  174. nrf_ble_bms_evt_type_t evt_type; //!< Type of event.
  175. nrf_ble_bms_auth_code_t auth_code; //!< Received authorization code.
  176. } nrf_ble_bms_evt_t;
  177. /**@brief BMS control points. */
  178. typedef struct
  179. {
  180. nrf_ble_bms_op_t op_code; //!< Control Point Op Code.
  181. nrf_ble_bms_auth_code_t auth_code; //!< Control Point Authorization Code.
  182. } nrf_ble_bms_ctrlpt_t;
  183. // Forward declaration of the nrf_ble_bms_t type.
  184. typedef struct nrf_ble_bms_s nrf_ble_bms_t;
  185. /**@brief BMS event handler type. */
  186. typedef void (* nrf_ble_bms_bond_handler_t) (nrf_ble_bms_t const * p_bms);
  187. /**@brief BMS bond management callbacks. */
  188. typedef struct
  189. {
  190. nrf_ble_bms_bond_handler_t delete_requesting; //!< Function to be called to delete the bonding information of the requesting device.
  191. nrf_ble_bms_bond_handler_t delete_all; //!< Function to be called to delete the bonding information of all bonded devices.
  192. nrf_ble_bms_bond_handler_t delete_all_except_requesting; //!< Function to be called to delete the bonding information of all bonded devices except for the requesting device.
  193. } nrf_ble_bms_bond_cbs_t;
  194. /**@brief BMS event handler type. The event handler returns a @ref BLE_GATT_STATUS_CODES "BLE GATT status code". */
  195. typedef void (* ble_bms_evt_handler_t) (nrf_ble_bms_t * p_bms, nrf_ble_bms_evt_t * p_evt);
  196. /**@brief BMS initialization structure that contains all information needed to initialize the service. */
  197. typedef struct
  198. {
  199. ble_bms_evt_handler_t evt_handler; //!< Event handler to be called for handling events in the Bond Management Service.
  200. ble_srv_error_handler_t error_handler; //!< Function to be called if an error occurs.
  201. nrf_ble_bms_features_t feature; //!< Initial value for features of the service.
  202. security_req_t bms_feature_sec_req; //!< Initial security level for the Feature characteristic.
  203. security_req_t bms_ctrlpt_sec_req; //!< Initial security level for the Control Point characteristic.
  204. nrf_ble_qwr_t * p_qwr; //!< Pointer to the initialized Queued Write context.
  205. nrf_ble_bms_bond_cbs_t bond_callbacks; //!< Callback functions for deleting bonds.
  206. } nrf_ble_bms_init_t;
  207. /**@brief Status information for the service. */
  208. struct nrf_ble_bms_s
  209. {
  210. uint16_t service_handle; //!< Handle of the Bond Management Service (as provided by the BLE stack).
  211. uint16_t conn_handle; //!< Handle of the current connection (as provided by the BLE stack). @ref BLE_CONN_HANDLE_INVALID if not in a connection.
  212. ble_bms_evt_handler_t evt_handler; //!< Event handler to be called for handling events in the Bond Management Service.
  213. ble_srv_error_handler_t error_handler; //!< Function to be called if an error occurs.
  214. nrf_ble_bms_features_t feature; //!< Value for features of the service (see @ref NRF_BLE_BMS_FEATURES).
  215. ble_gatts_char_handles_t feature_handles; //!< Handles related to the Bond Management Feature characteristic.
  216. ble_gatts_char_handles_t ctrlpt_handles; //!< Handles related to the Bond Management Control Point characteristic.
  217. nrf_ble_bms_bond_cbs_t bond_callbacks; //!< Callback functions for deleting bonds.
  218. nrf_ble_bms_auth_status_t auth_status; //!< Authorization status.
  219. };
  220. /**@brief Function for responding to an authorization request.
  221. *
  222. * @details This function should be called when receiving the @ref NRF_BLE_BMS_EVT_AUTH event to
  223. * respond to the service with an authorization result.
  224. *
  225. * @param[in] p_bms BMS structure.
  226. * @param[in] authorize Authorization response. True if the authorization is considered successful.
  227. *
  228. * @retval NRF_ERROR_NULL If @p p_bms was NULL.
  229. * @retval NRF_ERROR_INVALID_STATE If no authorization request was pending.
  230. * @retval NRF_SUCCESS If the response was received successfully.
  231. */
  232. ret_code_t nrf_ble_bms_auth_response(nrf_ble_bms_t * p_bms, bool authorize);
  233. /**@brief Function for initializing the Bond Management Service.
  234. *
  235. * @param[out] p_bms BMS structure.
  236. * @param[in] p_bms_init Information needed to initialize the service.
  237. *
  238. * @retval NRF_ERROR_NULL If @p p_bms or @p p_bms_init was NULL.
  239. * @retval NRF_SUCCESS If the service was initialized successfully.
  240. * Otherwise, an error code is returned.
  241. */
  242. ret_code_t nrf_ble_bms_init(nrf_ble_bms_t * p_bms, nrf_ble_bms_init_t * p_bms_init);
  243. /**@brief Function for assigning handles to the Bond Management Service instance.
  244. *
  245. * @details Call this function when a link with a peer has been established to
  246. * associate the link to this instance of the module.
  247. *
  248. * @param[in] p_bms Pointer to the BMS structure instance to associate.
  249. * @param[in] conn_handle Connection handle to be associated with the given BMS instance.
  250. *
  251. * @retval NRF_ERROR_NULL If @p p_bms was NULL.
  252. * @retval NRF_SUCCESS If the operation was successful.
  253. */
  254. ret_code_t nrf_ble_bms_set_conn_handle(nrf_ble_bms_t * p_bms, uint16_t conn_handle);
  255. /**@brief Function for handling Bond Management BLE stack events.
  256. *
  257. * @details This function handles all events from the BLE stack that are of interest to the Bond Management Service.
  258. *
  259. * @param[in] p_ble_evt Event received from the BLE stack.
  260. * @param[in] p_context BMS structure.
  261. */
  262. void nrf_ble_bms_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  263. /**@brief Function for handling events from the @ref nrf_ble_qwr.
  264. *
  265. * @param[in] p_bms BMS structure.
  266. * @param[in] p_qwr Queued Write structure.
  267. * @param[in] p_evt Event received from the Queued Writes module.
  268. *
  269. * @retval BLE_GATT_STATUS_SUCCESS If the received event is accepted.
  270. * @retval NRF_BLE_QWR_REJ_REQUEST_ERR_CODE If the received event is not relevant for any of this module's attributes.
  271. * @retval BLE_BMS_OPCODE_NOT_SUPPORTED If the received opcode is not supported.
  272. * @retval BLE_GATT_STATUS_ATTERR_INSUF_AUTHORIZATION If the application handler returns that the authorization code is not valid.
  273. */
  274. uint16_t nrf_ble_bms_on_qwr_evt(nrf_ble_bms_t * p_bms,
  275. nrf_ble_qwr_t * p_qwr,
  276. nrf_ble_qwr_evt_t * p_evt);
  277. #ifdef __cplusplus
  278. }
  279. #endif
  280. #endif // NRF_BLE_BMS_H__
  281. /** @} */