ble_rscs_c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * Copyright (c) 2012 - 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. /**
  41. * @defgroup ble_rscs_c Running Speed and Cadence Service Client
  42. * @{
  43. * @ingroup ble_sdk_srv
  44. *
  45. * @details This module contains the APIs and types exposed by the Running Speed and Cadence
  46. * Service Client module. The application can use these APIs and types to perform
  47. * discovery of Running Speed and Cadence Service at the peer and interact with it.
  48. *
  49. * @note The application must register this module as BLE event observer by using the
  50. * NRF_SDH_BLE_OBSERVER macro. Example:
  51. * @code
  52. * ble_rscs_c_t instance;
  53. * NRF_SDH_BLE_OBSERVER(anything, BLE_RSCS_C_BLE_OBSERVER_PRIO,
  54. * ble_rscs_c_on_ble_evt, &instance);
  55. * @endcode
  56. */
  57. #ifndef BLE_RSCS_C_H__
  58. #define BLE_RSCS_C_H__
  59. #include <stdint.h>
  60. #include <stdbool.h>
  61. #include "ble.h"
  62. #include "ble_db_discovery.h"
  63. #include "ble_srv_common.h"
  64. #include "nrf_ble_gq.h"
  65. #include "nrf_sdh_ble.h"
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. /**@brief Macro for defining a ble_rscs_c instance.
  70. *
  71. * @param _name Name of the instance.
  72. * @hideinitializer
  73. */
  74. #define BLE_RSCS_C_DEF(_name) \
  75. static ble_rscs_c_t _name; \
  76. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  77. BLE_RSCS_C_BLE_OBSERVER_PRIO, \
  78. ble_rscs_c_on_ble_evt, &_name)
  79. /** @brief Macro for defining multiple ble_rscs_c instances.
  80. *
  81. * @param _name Name of the array of instances.
  82. * @param _cnt Number of instances to define.
  83. * @hideinitializer
  84. */
  85. #define BLE_RSCS_C_ARRAY_DEF(_name, _cnt) \
  86. static ble_rscs_c_t _name[_cnt]; \
  87. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  88. BLE_RSCS_C_BLE_OBSERVER_PRIO, \
  89. ble_rscs_c_on_ble_evt, &_name, _cnt)
  90. #define BLE_RSCS_INSTANT_STRIDE_LEN_PRESENT 0x00 /**< Instantaneous Stride Length Measurement Supported bit. */
  91. #define BLE_RSCS_TOTAL_DISTANCE_PRESENT 0x01 /**< Total Distance Measurement Supported bit. */
  92. #define BLE_RSCS_WALKING_OR_RUNNING_STATUS_BIT 0x02 /**< Walking or Running Status Supported bit. */
  93. /**@brief Structure containing the handles related to the Running Speed and Cadence Service found on the peer. */
  94. typedef struct
  95. {
  96. uint16_t rsc_cccd_handle; /**< Handle of the CCCD of the Running Speed and Cadence characteristic. */
  97. uint16_t rsc_handle; /**< Handle of the Running Speed and Cadence characteristic as provided by the SoftDevice. */
  98. } ble_rscs_c_db_t;
  99. /**@brief RSCS Client event type. */
  100. typedef enum
  101. {
  102. BLE_RSCS_C_EVT_DISCOVERY_COMPLETE = 1, /**< Event indicating that the Running Speed and Cadence Service has been discovered at the peer. */
  103. BLE_RSCS_C_EVT_RSC_NOTIFICATION /**< Event indicating that a notification of the Running Speed and Cadence measurement characteristic has been received from the peer. */
  104. } ble_rscs_c_evt_type_t;
  105. /**@brief Structure containing the Running Speed and Cadence measurement received from the peer. */
  106. typedef struct
  107. {
  108. bool is_inst_stride_len_present; /**< True if Instantaneous Stride Length is present in the measurement. */
  109. bool is_total_distance_present; /**< True if Total Distance is present in the measurement. */
  110. bool is_running; /**< True if running, False if walking. */
  111. uint16_t inst_speed; /**< Instantaneous Speed. */
  112. uint8_t inst_cadence; /**< Instantaneous Cadence. */
  113. uint16_t inst_stride_length; /**< Instantaneous Stride Length. */
  114. uint32_t total_distance; /**< Total Distance. */
  115. } ble_rsc_t;
  116. /**@brief Running Speed and Cadence Event structure. */
  117. typedef struct
  118. {
  119. ble_rscs_c_evt_type_t evt_type; /**< Type of the event. */
  120. uint16_t conn_handle; /**< Connection handle on which the rscs_c event occured.*/
  121. union
  122. {
  123. ble_rscs_c_db_t rscs_db; /**< Running Speed and Cadence Service related handles found on the peer device. This is filled if the evt_type is @ref BLE_RSCS_C_EVT_DISCOVERY_COMPLETE.*/
  124. ble_rsc_t rsc; /**< Running Speed and Cadence measurement received. This is filled if the evt_type is @ref BLE_RSCS_C_EVT_RSC_NOTIFICATION. */
  125. } params;
  126. } ble_rscs_c_evt_t;
  127. // Forward declaration of the ble_rscs_c_t type.
  128. typedef struct ble_rscs_c_s ble_rscs_c_t;
  129. /**@brief Event handler type.
  130. *
  131. * @details This is the type of the event handler that is to be provided by the application
  132. * of this module in order to receive events.
  133. */
  134. typedef void (* ble_rscs_c_evt_handler_t) (ble_rscs_c_t * p_ble_rscs_c, ble_rscs_c_evt_t * p_evt);
  135. /**@brief Running Speed and Cadence client structure. */
  136. struct ble_rscs_c_s
  137. {
  138. uint16_t conn_handle; /**< Connection handle as provided by the SoftDevice. */
  139. ble_rscs_c_db_t peer_db; /**< Handles related to RSCS on the peer*/
  140. ble_rscs_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the Running Speed and Cadence service. */
  141. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  142. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  143. };
  144. /**@brief Running Speed and Cadence client initialization structure. */
  145. typedef struct
  146. {
  147. ble_rscs_c_evt_handler_t evt_handler; /**< Event handler to be called by the Running Speed and Cadence Client module whenever there is an event related to the Running Speed and Cadence Service. */
  148. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  149. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  150. } ble_rscs_c_init_t;
  151. /**@brief Function for initializing the Running Speed and Cadence Service Client module.
  152. *
  153. * @details This function will initialize the module and set up Database Discovery to discover
  154. * the Running Speed and Cadence Service. After calling this function, call @ref ble_db_discovery_start
  155. * to start discovery once a link with a peer has been established.
  156. *
  157. * @param[out] p_ble_rscs_c Pointer to the RSC Service Client structure.
  158. * @param[in] p_ble_rscs_c_init Pointer to the RSC Service initialization structure containing
  159. * the initialization information.
  160. *
  161. * @retval NRF_SUCCESS Operation success.
  162. * @retval NRF_ERROR_NULL A parameter is NULL.
  163. * @retval err_code Otherwise, this function propagates the error code returned by @ref ble_db_discovery_evt_register.
  164. */
  165. uint32_t ble_rscs_c_init(ble_rscs_c_t * p_ble_rscs_c, ble_rscs_c_init_t * p_ble_rscs_c_init);
  166. /**@brief Function for handling the Application's BLE Stack events.
  167. *
  168. * @details Handles all events from the BLE stack that are of interest to the Running Speed and Cadence
  169. * Service Client.
  170. *
  171. * @param[in] p_ble_evt Event received from the BLE stack.
  172. * @param[in] p_context Running Speed and Cadence Service Client structure.
  173. */
  174. void ble_rscs_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  175. uint32_t ble_rscs_c_rsc_notif_enable(ble_rscs_c_t * p_ble_rscs_c);
  176. /**@brief Function for handling events from the Database Discovery module.
  177. *
  178. * @details Call this function when you get a callback event from the Database Discovery module.
  179. * This function handles an event from the Database Discovery module, and determines
  180. * whether it relates to the discovery of Running Speed and Cadence Service at the peer.
  181. * If it does, the function calls the application's event handler to indicate that the RSC Service was
  182. * discovered at the peer. The function also populates the event with service-related
  183. * information before providing it to the application.
  184. *
  185. * @param p_ble_rscs_c Pointer to the Runnind Speed and Cadence Service Client structure.
  186. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  187. */
  188. void ble_rscs_on_db_disc_evt(ble_rscs_c_t * p_ble_rscs_c, ble_db_discovery_evt_t const * p_evt);
  189. /**@brief Function for assigning handles to this instance of rscs_c.
  190. *
  191. * @details Call this function when a link has been established with a peer to
  192. * associate the link to this instance of the module. This makes it
  193. * possible to handle several links and associate each link to a particular
  194. * instance of this module. The connection handle and attribute handles are
  195. * provided from the discovery event @ref BLE_RSCS_C_EVT_DISCOVERY_COMPLETE.
  196. *
  197. * @param[in] p_ble_rscs_c Pointer to the RSC client structure instance for associating the link.
  198. * @param[in] conn_handle Connection handle to associated with the given RSCS Client Instance.
  199. * @param[in] p_peer_handles Attribute handles on the RSCS server that you want this RSCS client
  200. * to interact with.
  201. */
  202. uint32_t ble_rscs_c_handles_assign(ble_rscs_c_t * p_ble_rscs_c,
  203. uint16_t conn_handle,
  204. ble_rscs_c_db_t * p_peer_handles);
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #endif // BLE_RSCS_C_H__
  209. /** @} */ // End tag for the file.