ble_cts_c.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * Copyright (c) 2014 - 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_cts_c Current Time Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Current Time Service Client module.
  46. *
  47. * @details This module implements the Current Time Service (CTS) client-peripheral role of
  48. * the Time Profile. After security is established, the module tries to discover the
  49. * Current Time Service and its characteristic on the central side. If this succeeds,
  50. * the application can trigger a read of the current time from the connected server.
  51. *
  52. * The module informs the application about the successful discovery with the
  53. * @ref BLE_CTS_C_EVT_DISCOVERY_COMPLETE event. The handles for the CTS server are now
  54. * available in the @ref ble_cts_c_evt_t structure. These handles must be assigned to an
  55. * instance of CTS_C with @ref ble_cts_c_handles_assign. For more information about the
  56. * service discovery, see the ble_discovery module documentation: @ref lib_ble_db_discovery.
  57. *
  58. * After assigning the handles to an instance of CTS_C, the application can use the function
  59. * @ref ble_cts_c_current_time_read to read the current time. If the read succeeds, it triggers either
  60. * a @ref BLE_CTS_C_EVT_CURRENT_TIME event or a @ref BLE_CTS_C_EVT_INVALID_TIME event
  61. * (depending whether the data that was read was actually a valid time). Then the read result is sent
  62. * to the application. The current time is then available in the params field of the
  63. * passed @ref ble_cts_c_evt_t structure.
  64. *
  65. * @note The application must register this module as the BLE event observer by using the
  66. * NRF_SDH_BLE_OBSERVER macro. Example:
  67. * @code
  68. * ble_cts_c_t instance;
  69. * NRF_SDH_BLE_OBSERVER(anything, BLE_CTS_C_BLE_OBSERVER_PRIO,
  70. * ble_cts_c_on_ble_evt, &instance);
  71. * @endcode
  72. */
  73. #ifndef BLE_CTS_C_H__
  74. #define BLE_CTS_C_H__
  75. #include "ble_srv_common.h"
  76. #include "ble_gattc.h"
  77. #include "ble.h"
  78. #include "ble_date_time.h"
  79. #include "ble_db_discovery.h"
  80. #include "nrf_ble_gq.h"
  81. #include "nrf_sdh_ble.h"
  82. #include <stdint.h>
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /**@brief Macro for defining a ble_bps instance.
  87. *
  88. * @param _name Name of the instance.
  89. * @hideinitializer
  90. */
  91. #define BLE_CTS_C_DEF(_name) \
  92. static ble_cts_c_t _name; \
  93. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  94. BLE_CTS_C_BLE_OBSERVER_PRIO, \
  95. ble_cts_c_on_ble_evt, &_name)
  96. /** @brief Macro for defining multiple ble_cts_c instances.
  97. *
  98. * @param _name Name of the array of instances.
  99. * @param _cnt Number of instances to define.
  100. * @hideinitializer
  101. */
  102. #define BLE_CTS_C_ARRAY_DEF(_name, _cnt) \
  103. static ble_cts_c_t _name[_cnt]; \
  104. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  105. BLE_CTS_C_BLE_OBSERVER_PRIO, \
  106. ble_cts_c_on_ble_evt, &_name, _cnt)
  107. /**@brief "Day Date Time" field of the "Exact Time 256" field of the Current Time characteristic. */
  108. typedef struct
  109. {
  110. ble_date_time_t date_time;
  111. uint8_t day_of_week;
  112. } day_date_time_t;
  113. /**@brief "Exact Time 256" field of the Current Time characteristic. */
  114. typedef struct
  115. {
  116. day_date_time_t day_date_time;
  117. uint8_t fractions256;
  118. } exact_time_256_t;
  119. /**@brief "Adjust Reason" field of the Current Time characteristic. */
  120. typedef struct
  121. {
  122. uint8_t manual_time_update : 1;
  123. uint8_t external_reference_time_update : 1;
  124. uint8_t change_of_time_zone : 1;
  125. uint8_t change_of_daylight_savings_time : 1;
  126. } adjust_reason_t;
  127. /**@brief Data structure for the Current Time characteristic. */
  128. typedef struct
  129. {
  130. exact_time_256_t exact_time_256;
  131. adjust_reason_t adjust_reason;
  132. } current_time_char_t;
  133. // Forward declaration of the ble_cts_c_t type.
  134. typedef struct ble_cts_c_s ble_cts_c_t;
  135. /**@brief Current Time Service client event type. */
  136. typedef enum
  137. {
  138. BLE_CTS_C_EVT_DISCOVERY_COMPLETE, /**< The Current Time Service was found at the peer. */
  139. BLE_CTS_C_EVT_DISCOVERY_FAILED, /**< The Current Time Service was not found at the peer. */
  140. BLE_CTS_C_EVT_DISCONN_COMPLETE, /**< Event indicating that the Current Time Service Client module finished processing the BLE_GAP_EVT_DISCONNECTED event. This event is triggered only if a valid instance of the Current Time Service was found at the server. The application can use this event to do a cleanup related to the Current Time Service client.*/
  141. BLE_CTS_C_EVT_CURRENT_TIME, /**< A new Current Time reading has been received. */
  142. BLE_CTS_C_EVT_INVALID_TIME /**< The Current Time value received from the peer is invalid.*/
  143. } ble_cts_c_evt_type_t;
  144. /**@brief Structure containing the handles related to the Heart Rate Service found on the peer. */
  145. typedef struct
  146. {
  147. uint16_t cts_handle; /**< Handle of the Current Time characteristic, as provided by the SoftDevice. */
  148. uint16_t cts_cccd_handle; /**< Handle of the CCCD of the Current Time characteristic. */
  149. } ble_cts_c_handles_t;
  150. /**@brief Current Time Service client event. */
  151. typedef struct
  152. {
  153. ble_cts_c_evt_type_t evt_type; /**< Type of event. */
  154. uint16_t conn_handle; /**< Connection handle on which the CTS service was discovered on the peer device. This is filled if the evt_type is @ref BLE_CTS_C_EVT_DISCOVERY_COMPLETE.*/
  155. union
  156. {
  157. current_time_char_t current_time; /**< Current Time characteristic data. This is filled when the evt_type is @ref BLE_CTS_C_EVT_CURRENT_TIME. */
  158. ble_cts_c_handles_t char_handles; /**< Handles related to Current Time, found on the peer device. This is filled when the evt_type is @ref BLE_HRS_C_EVT_DISCOVERY_COMPLETE.*/
  159. } params;
  160. } ble_cts_c_evt_t;
  161. /**@brief Current Time Service client event handler type. */
  162. typedef void (* ble_cts_c_evt_handler_t) (ble_cts_c_t * p_cts, ble_cts_c_evt_t * p_evt);
  163. /**@brief Current Time Service client structure. This structure contains status information for the client. */
  164. struct ble_cts_c_s
  165. {
  166. ble_cts_c_evt_handler_t evt_handler; /**< Event handler to be called for handling events from the Current Time Service client. */
  167. ble_srv_error_handler_t error_handler; /**< Function to be called if an error occurs. */
  168. ble_cts_c_handles_t char_handles; /**< Handles of Current Time characteristic at the peer. (Handles are provided by the BLE stack through the Database Discovery module.) */
  169. uint16_t conn_handle; /**< Handle of the current connection. BLE_CONN_HANDLE_INVALID if not in a connection. */
  170. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  171. };
  172. /**@brief Current Time Service client init structure. This structure contains all options and data needed for the initialization of the client.*/
  173. typedef struct
  174. {
  175. ble_cts_c_evt_handler_t evt_handler; /**< Event handler to be called for handling events from the Current Time Service client. */
  176. ble_srv_error_handler_t error_handler; /**< Function to be called if an error occurs. */
  177. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  178. } ble_cts_c_init_t;
  179. /**@brief Function for initializing the Current Time Service client.
  180. *
  181. * @details This function must be used by the application to initialize the Current Time Service client.
  182. *
  183. * @param[out] p_cts Current Time Service client structure. This structure must
  184. * be supplied by the application. It is initialized by this
  185. * function and can later be used to identify this particular client
  186. * instance.
  187. * @param[in] p_cts_init Information needed to initialize the Current Time Service client.
  188. *
  189. * @retval NRF_SUCCESS If the service was initialized successfully.
  190. */
  191. uint32_t ble_cts_c_init(ble_cts_c_t * p_cts, const ble_cts_c_init_t * p_cts_init);
  192. /**@brief Function for handling events from the Database Discovery module.
  193. *
  194. * @details This function handles an event from the Database Discovery module, and determines
  195. * whether it relates to the discovery of CTS at the peer. If it does, the function
  196. * calls the application's event handler to indicate that CTS was
  197. * discovered. The function also populates the event with service-related
  198. * information before providing it to the application.
  199. *
  200. * @param[in] p_cts Pointer to the CTS client structure.
  201. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  202. */
  203. void ble_cts_c_on_db_disc_evt(ble_cts_c_t * p_cts, ble_db_discovery_evt_t * p_evt);
  204. /**@brief Function for handling the application's BLE stack events.
  205. *
  206. * @details This function handles all events from the BLE stack that are of interest to the
  207. * Current Time Service client. This is a callback function that must be dispatched
  208. * from the main application context.
  209. *
  210. * @param[in] p_ble_evt Event received from the BLE stack.
  211. * @param[in] p_context Current Time Service client structure.
  212. */
  213. void ble_cts_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  214. /**@brief Function for checking whether the peer's Current Time Service instance and the Current Time
  215. * Characteristic have been discovered.
  216. *
  217. * @param[in] p_cts Current Time Service client structure.
  218. */
  219. static __INLINE bool ble_cts_c_is_cts_discovered(const ble_cts_c_t * p_cts)
  220. {
  221. return (p_cts->char_handles.cts_handle != BLE_GATT_HANDLE_INVALID);
  222. }
  223. /**@brief Function for reading the peer's Current Time Service Current Time characteristic.
  224. *
  225. * @param[in] p_cts Current Time Service client structure.
  226. *
  227. * @retval NRF_SUCCESS If the operation is successful.
  228. * @retval err_code Otherwise, this API propagates the error code returned by function
  229. * @ref nrf_ble_gq_item_add.
  230. */
  231. uint32_t ble_cts_c_current_time_read(ble_cts_c_t const * p_cts);
  232. /**@brief Function for assigning handles to this instance of cts_c.
  233. *
  234. * @details Call this function when a link has been established with a peer to
  235. * associate the link to this instance of the module. This association makes it
  236. * possible to handle several links and associate each link to a particular
  237. * instance of this module. The connection handle and attribute handles are
  238. * provided from the discovery event @ref BLE_CTS_C_EVT_DISCOVERY_COMPLETE.
  239. *
  240. * @param[in] p_cts Pointer to the CTS client structure instance for associating the link.
  241. * @param[in] conn_handle Connection handle to associate with the given CTS instance.
  242. * @param[in] p_peer_handles Attribute handles for the CTS server you want this CTS client to
  243. * interact with.
  244. *
  245. * @retval NRF_SUCCESS If the operation was successful.
  246. * @retval NRF_ERROR_NULL If a p_cts was a NULL pointer.
  247. * @retval err_code Otherwise, this API propagates the error code returned by function
  248. * @ref nrf_ble_gq_conn_handle_register.
  249. */
  250. uint32_t ble_cts_c_handles_assign(ble_cts_c_t * p_cts,
  251. const uint16_t conn_handle,
  252. const ble_cts_c_handles_t * p_peer_handles);
  253. #ifdef __cplusplus
  254. }
  255. #endif
  256. #endif // BLE_CTS_C_H__
  257. /** @} */