nrf_ble_escs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef NRF_BLE_ESCS_H__
  41. #define NRF_BLE_ESCS_H__
  42. #include "ble.h"
  43. #include "ble_srv_common.h"
  44. #include "app_util_platform.h"
  45. #include "sdk_common.h"
  46. #include "escs_defs.h"
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. /**
  50. * @file
  51. * @defgroup nrf_ble_escs Eddystone Configuration Service
  52. * @brief Eddystone Configuration Service module.
  53. * @ingroup ble_sdk_srv
  54. * @{
  55. */
  56. #define BLE_ESCS_NUMBER_OF_CHARACTERISTICS 13 //!< Number of characteristics contained in the Eddystone Configuration Service.
  57. #define BLE_UUID_ESCS_SERVICE 0x7500 //!< UUID of the Eddystone Configuration Service.
  58. // ECS UUIDs
  59. #define BLE_UUID_ESCS_BROADCAST_CAP_CHAR 0x7501
  60. #define BLE_UUID_ESCS_ACTIVE_SLOT_CHAR 0x7502
  61. #define BLE_UUID_ESCS_ADV_INTERVAL_CHAR 0x7503
  62. #define BLE_UUID_ESCS_RADIO_TX_PWR_CHAR 0x7504
  63. #define BLE_UUID_ESCS_ADV_TX_PWR_CHAR 0x7505
  64. #define BLE_UUID_ESCS_LOCK_STATE_CHAR 0x7506
  65. #define BLE_UUID_ESCS_UNLOCK_CHAR 0x7507
  66. #define BLE_UUID_ESCS_PUBLIC_ECDH_KEY_CHAR 0x7508
  67. #define BLE_UUID_ESCS_EID_ID_KEY_CHAR 0x7509
  68. #define BLE_UUID_ESCS_RW_ADV_SLOT_CHAR 0x750A
  69. #define BLE_UUID_ESCS_FACTORY_RESET_CHAR 0x750B
  70. #define BLE_UUID_ESCS_REMAIN_CONNECTABLE_CHAR 0x750C
  71. #define ESCS_BASE_UUID \
  72. {{0x95, 0xE2, 0xED, 0xEB, 0x1B, 0xA0, 0x39, 0x8A, 0xDF, 0x4B, 0xD3, 0x8E, 0x00, 0x00, 0xC8, \
  73. 0xA3}}
  74. // A3C8XXXX-8ED3-4BDF-8A39-A01BEBEDE295
  75. #define NRF_BLE_ESCS_BROADCAST_CAP_LEN (ESCS_NUM_OF_SUPPORTED_TX_POWER + 6) // According to the eddystone spec, there are 6 bytes of data in addition to the supported_radio_tx_power array
  76. /**@brief Data fields in the Broadcast Capabilities characteristic.
  77. * @note This is a packed structure. Therefore, you should not change it.
  78. */
  79. typedef PACKED_STRUCT
  80. {
  81. int8_t vers_byte;
  82. int8_t max_supp_total_slots;
  83. int8_t max_supp_eid_slots;
  84. int8_t cap_bitfield;
  85. int16_t supp_frame_types;
  86. int8_t supp_radio_tx_power[ESCS_NUM_OF_SUPPORTED_TX_POWER];
  87. } nrf_ble_escs_broadcast_cap_t;
  88. typedef uint8_t nrf_ble_escs_active_slot_t;
  89. typedef uint16_t nrf_ble_escs_adv_interval_t;
  90. typedef int8_t nrf_ble_escs_radio_tx_pwr_t;
  91. typedef int8_t nrf_ble_escs_adv_tx_pwr_t;
  92. /**@brief Read states of the Lock State characteristic. */
  93. typedef enum
  94. {
  95. NRF_BLE_ESCS_LOCK_STATE_LOCKED = ESCS_LOCK_STATE_LOCKED,
  96. NRF_BLE_ESCS_LOCK_STATE_UNLOCKED = ESCS_LOCK_STATE_UNLOCKED,
  97. NRF_BLE_ESCS_LOCK_STATE_UNLOCKED_AUTO_RELOCK_DISABLED =
  98. ESCS_LOCK_STATE_UNLOCKED_AUTO_RELOCK_DISABLED
  99. } nrf_ble_escs_lock_state_read_t;
  100. /**@brief Write bytes of the Lock State characteristic. */
  101. typedef enum
  102. {
  103. NRF_BLE_ESCS_LOCK_BYTE_LOCK = ESCS_LOCK_BYTE_LOCK,
  104. NRF_BLE_ESCS_LOCK_BYTE_DISABLE_AUTO_RELOCK = ESCS_LOCK_BYTE_DISABLE_AUTO_RELOCK
  105. } nrf_ble_escs_lock_byte_t;
  106. /**@brief Write data fields of the Lock State characteristic.
  107. * @note This is a packed structure. Therefore, you should not change it.
  108. */
  109. typedef PACKED_STRUCT
  110. {
  111. nrf_ble_escs_lock_byte_t lock_byte;
  112. int8_t encrypted_key[ESCS_AES_KEY_SIZE];
  113. } nrf_ble_escs_lock_state_write_t;
  114. /**@brief Lock State characteristic. */
  115. typedef union
  116. {
  117. nrf_ble_escs_lock_state_read_t read;
  118. nrf_ble_escs_lock_state_write_t write;
  119. } nrf_ble_escs_lock_state_t;
  120. /**@brief Unlock characteristic (read/write). */
  121. typedef union
  122. {
  123. int8_t r_challenge[ESCS_AES_KEY_SIZE];
  124. int8_t w_unlock_token[ESCS_AES_KEY_SIZE];
  125. } nrf_ble_escs_unlock_t;
  126. /**@brief Public ECDH Key characteristic.
  127. * @note This is a packed structure. Therefore, you should not change it.
  128. */
  129. typedef PACKED_STRUCT
  130. {
  131. int8_t key[ESCS_ECDH_KEY_SIZE];
  132. } nrf_ble_escs_public_ecdh_key_t;
  133. /**@brief EID Identity Key characteristic.
  134. * @note This is a packed structure. Therefore, you should not change it.
  135. */
  136. typedef PACKED_STRUCT
  137. {
  138. int8_t key[ESCS_AES_KEY_SIZE];
  139. } nrf_ble_escs_eid_id_key_t;
  140. typedef uint8_t nrf_ble_escs_factory_reset_t;
  141. /**@brief Unlock characteristic (read/write). */
  142. typedef union
  143. {
  144. uint8_t r_is_non_connectable_supported;
  145. uint8_t w_remain_connectable_boolean;
  146. } nrf_ble_escs_remain_conntbl_t;
  147. /**@brief Eddystone Configuration Service initialization parameters (corresponding to required characteristics). */
  148. typedef struct
  149. {
  150. nrf_ble_escs_broadcast_cap_t broadcast_cap;
  151. nrf_ble_escs_adv_interval_t adv_interval;
  152. nrf_ble_escs_radio_tx_pwr_t radio_tx_pwr;
  153. nrf_ble_escs_adv_tx_pwr_t adv_tx_pwr;
  154. nrf_ble_escs_factory_reset_t factory_reset;
  155. nrf_ble_escs_remain_conntbl_t remain_connectable;
  156. } nrf_ble_escs_init_params_t;
  157. // Forward Declaration of nrf_ble_escs_t type.
  158. typedef struct nrf_ble_escs_s nrf_ble_escs_t;
  159. typedef void (*nrf_ble_escs_write_evt_handler_t)(nrf_ble_escs_t * p_escs,
  160. uint16_t uuid,
  161. uint16_t value_handle,
  162. uint8_t const * p_data,
  163. uint16_t length);
  164. typedef void (*nrf_ble_escs_read_evt_handler_t)(nrf_ble_escs_t * p_escs,
  165. uint16_t uuid,
  166. uint16_t value_handle);
  167. /**@brief Eddystone Configuration Service initialization structure.
  168. *
  169. * @details This structure contains the initialization information for the service. The application
  170. * must fill this structure and pass it to the service using the @ref nrf_ble_escs_init
  171. * function.
  172. */
  173. typedef struct
  174. {
  175. nrf_ble_escs_init_params_t * p_init_vals; //!< Initialization parameters for the service.
  176. nrf_ble_escs_write_evt_handler_t write_evt_handler; //!< Event handler to be called for authorizing write requests.
  177. nrf_ble_escs_read_evt_handler_t read_evt_handler; //!< Event handler to be called for authorizing read requests.
  178. } nrf_ble_escs_init_t;
  179. struct nrf_ble_escs_s
  180. {
  181. uint8_t uuid_type; //!< UUID type for the Eddystone Configuration Service Base UUID.
  182. uint16_t service_handle; //!< Handle of the Eddystone Configuration Service (as provided by the SoftDevice).
  183. ble_gatts_char_handles_t broadcast_cap_handles; //!< Handles related to the Capabilities characteristic (as provided by the SoftDevice).
  184. ble_gatts_char_handles_t active_slot_handles; //!< Handles related to the Active Slot characteristic (as provided by the SoftDevice).
  185. ble_gatts_char_handles_t adv_interval_handles; //!< Handles related to the Advertising Interval characteristic (as provided by the SoftDevice).
  186. ble_gatts_char_handles_t radio_tx_pwr_handles; //!< Handles related to the Radio Tx Power characteristic (as provided by the SoftDevice).
  187. ble_gatts_char_handles_t adv_tx_pwr_handles; //!< Handles related to the (Advanced) Advertised Tx Power characteristic (as provided by the SoftDevice).
  188. ble_gatts_char_handles_t lock_state_handles; //!< Handles related to the Lock State characteristic (as provided by the SoftDevice).
  189. ble_gatts_char_handles_t unlock_handles; //!< Handles related to the Unlock characteristic (as provided by the SoftDevice).
  190. ble_gatts_char_handles_t pub_ecdh_key_handles; //!< Handles related to the Public ECDH Key characteristic (as provided by the SoftDevice).
  191. ble_gatts_char_handles_t eid_id_key_handles; //!< Handles related to the EID Identity Key characteristic (as provided by the SoftDevice).
  192. ble_gatts_char_handles_t rw_adv_slot_handles; //!< Handles related to the ADV Slot Data characteristic (as provided by the SoftDevice).
  193. ble_gatts_char_handles_t factory_reset_handles; //!< Handles related to the (Advanced) Factory reset characteristic (as provided by the SoftDevice).
  194. ble_gatts_char_handles_t remain_connectable_handles; //!< Handles related to the (Advanced) Remain Connectable characteristic (as provided by the SoftDevice).
  195. uint16_t conn_handle; //!< Handle of the current connection (as provided by the SoftDevice). @ref BLE_CONN_HANDLE_INVALID if not in a connection.
  196. nrf_ble_escs_write_evt_handler_t write_evt_handler; //!< Event handler to be called for handling write attempts.
  197. nrf_ble_escs_read_evt_handler_t read_evt_handler; //!< Event handler to be called for handling read attempts.
  198. uint8_t * p_active_slot;
  199. uint8_t lock_state;
  200. };
  201. /**@brief Function for initializing the Eddystone Configuration Service.
  202. *
  203. * @param[out] p_escs Eddystone Configuration Service structure. This structure must be supplied
  204. * by the application. It is initialized by this function and will
  205. * later be used to identify this particular service instance.
  206. * @param[in] p_ecs_init Information needed to initialize the service.
  207. *
  208. * @retval NRF_SUCCESS If the service was successfully initialized. Otherwise, an error code is returned.
  209. * @retval NRF_ERROR_NULL If either of the pointers @p p_escs or @p p_ecs_init is NULL.
  210. */
  211. ret_code_t nrf_ble_escs_init(nrf_ble_escs_t * p_escs, const nrf_ble_escs_init_t * p_ecs_init);
  212. /**@brief Function for handling the Eddystone Configuration Service's BLE events.
  213. *
  214. * @details The Eddystone Configuration Service expects the application to call this function each time an
  215. * event is received from the SoftDevice. This function processes the event if it
  216. * is relevant and calls the Eddystone Configuration Service event handler of the
  217. * application if necessary.
  218. *
  219. * @param[in] p_escs Eddystone Configuration Service structure.
  220. * @param[in] p_ble_evt Event received from the SoftDevice.
  221. *
  222. * @retval NRF_ERROR_NULL If any of the arguments given are NULL.
  223. * @retval NRF_SUCCESS otherwise.
  224. */
  225. ret_code_t nrf_ble_escs_on_ble_evt(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt);
  226. /** @} */
  227. #endif // NRF_BLE_ESCS_H__