ble_rscs.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /** @file
  41. *
  42. * @defgroup ble_rscs Running Speed and Cadence Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Running Speed and Cadence Service module.
  46. *
  47. * @details This module implements the Running Speed and Cadence Service. If enabled, notification
  48. * of the Running Speead and Candence Measurement is performed when the application
  49. * calls ble_rscs_measurement_send().
  50. *
  51. * If an event handler is supplied by the application, the Running Speed and Cadence
  52. * Service will generate Running Speed and Cadence Service events to the application.
  53. *
  54. * @note The application must register this module as BLE event observer using the
  55. * NRF_SDH_BLE_OBSERVER macro. Example:
  56. * @code
  57. * ble_rscs_t instance;
  58. * NRF_SDH_BLE_OBSERVER(anything, BLE_RSCS_BLE_OBSERVER_PRIO,
  59. * ble_rscs_on_ble_evt, &instance);
  60. * @endcode
  61. *
  62. * @note Attention!
  63. * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
  64. * qualification listings, this section of source code must not be modified.
  65. */
  66. #ifndef BLE_RSCS_H__
  67. #define BLE_RSCS_H__
  68. #include <stdint.h>
  69. #include <stdbool.h>
  70. #include "ble_srv_common.h"
  71. #include "nrf_sdh_ble.h"
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75. /**@brief Macro for defining a ble_rscs instance.
  76. *
  77. * @param _name Name of the instance.
  78. * @hideinitializer
  79. */
  80. #define BLE_RSCS_DEF(_name) \
  81. static ble_rscs_t _name; \
  82. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  83. BLE_RSCS_BLE_OBSERVER_PRIO, \
  84. ble_rscs_on_ble_evt, &_name)
  85. /**@brief Running Speed and Cadence Service feature bits. */
  86. #define BLE_RSCS_FEATURE_INSTANT_STRIDE_LEN_BIT (0x01 << 0) /**< Instantaneous Stride Length Measurement Supported bit. */
  87. #define BLE_RSCS_FEATURE_TOTAL_DISTANCE_BIT (0x01 << 1) /**< Total Distance Measurement Supported bit. */
  88. #define BLE_RSCS_FEATURE_WALKING_OR_RUNNING_STATUS_BIT (0x01 << 2) /**< Walking or Running Status Supported bit. */
  89. #define BLE_RSCS_FEATURE_CALIBRATION_PROCEDURE_BIT (0x01 << 3) /**< Calibration Procedure Supported bit. */
  90. #define BLE_RSCS_FEATURE_MULTIPLE_SENSORS_BIT (0x01 << 4) /**< Multiple Sensor Locations Supported bit. */
  91. /**@brief Running Speed and Cadence Service event type. */
  92. typedef enum
  93. {
  94. BLE_RSCS_EVT_NOTIFICATION_ENABLED, /**< Running Speed and Cadence value notification enabled event. */
  95. BLE_RSCS_EVT_NOTIFICATION_DISABLED /**< Running Speed and Cadence value notification disabled event. */
  96. } ble_rscs_evt_type_t;
  97. /**@brief Running Speed and Cadence Service event. */
  98. typedef struct
  99. {
  100. ble_rscs_evt_type_t evt_type; /**< Type of event. */
  101. } ble_rscs_evt_t;
  102. // Forward declaration of the ble_rsc types.
  103. typedef struct ble_rscs_s ble_rscs_t;
  104. typedef struct ble_rscs_meas_s ble_rscs_meas_t;
  105. /**@brief Running Speed and Cadence Service event handler type. */
  106. typedef void (*ble_rscs_evt_handler_t) (ble_rscs_t * p_rscs, ble_rscs_evt_t * p_evt);
  107. /**@brief Running Speed and Cadence Service measurement structure. This contains a Running Speed and
  108. * Cadence measurement.
  109. */
  110. struct ble_rscs_meas_s
  111. {
  112. bool is_inst_stride_len_present; /**< True if Instantaneous Stride Length is present in the measurement. */
  113. bool is_total_distance_present; /**< True if Total Distance is present in the measurement. */
  114. bool is_running; /**< True if running, False if walking. */
  115. uint16_t inst_speed; /**< Instantaneous Speed. */
  116. uint8_t inst_cadence; /**< Instantaneous Cadence. */
  117. uint16_t inst_stride_length; /**< Instantaneous Stride Length. */
  118. uint32_t total_distance; /**< Total Distance. */
  119. };
  120. /**@brief Running Speed and Cadence Service init structure. This contains all options and data
  121. * needed for initialization of the service.
  122. */
  123. typedef struct
  124. {
  125. ble_rscs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Running Speed and Cadence Service. */
  126. security_req_t rsc_meas_cccd_wr_sec; /**< Security requirement for writing running speed and cadence measurement characteristic CCCD. */
  127. security_req_t rsc_feature_rd_sec; /**< Security requirement for reading running speed and cadence feature characteristic. */
  128. uint16_t feature; /**< Initial value for features of sensor. */
  129. ble_rscs_meas_t initial_rcm; /**< Initial Running Speed Cadence Measurement.*/
  130. } ble_rscs_init_t;
  131. /**@brief Running Speed and Cadence Service structure. This contains various status information for
  132. * the service.
  133. */
  134. struct ble_rscs_s
  135. {
  136. ble_rscs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Running Speed and Cadence Service. */
  137. uint16_t service_handle; /**< Handle of Running Speed and Cadence Service (as provided by the BLE stack). */
  138. ble_gatts_char_handles_t meas_handles; /**< Handles related to the Running Speed and Cadence Measurement characteristic. */
  139. ble_gatts_char_handles_t feature_handles; /**< Handles related to the Running Speed and Cadence feature characteristic. */
  140. uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
  141. uint16_t feature; /**< Bit mask of features available on sensor. */
  142. };
  143. /**@brief Function for initializing the Running Speed and Cadence Service.
  144. *
  145. * @param[out] p_rscs Running Speed and Cadence Service structure. This structure will have to
  146. * be supplied by the application. It will be initialized by this function,
  147. * and will later be used to identify this particular service instance.
  148. * @param[in] p_rscs_init Information needed to initialize the service.
  149. *
  150. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  151. */
  152. uint32_t ble_rscs_init(ble_rscs_t * p_rscs, const ble_rscs_init_t * p_rscs_init);
  153. /**@brief Function for handling the Application's BLE Stack events.
  154. *
  155. * @details Handles all events from the BLE stack of interest to the Running Speed and Cadence
  156. * Service.
  157. *
  158. * @param[in] p_ble_evt Event received from the BLE stack.
  159. * @param[in] p_context Running Speed and Cadence Service structure.
  160. */
  161. void ble_rscs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  162. /**@brief Function for sending running speed and cadence measurement if notification has been enabled.
  163. *
  164. * @details The application calls this function after having performed a Running Speed and Cadence
  165. * measurement. If notification has been enabled, the measurement data is encoded and sent
  166. * to the client.
  167. *
  168. * @param[in] p_rscs Running Speed and Cadence Service structure.
  169. * @param[in] p_measurement Pointer to new running speed and cadence measurement.
  170. *
  171. * @return NRF_SUCCESS on success, otherwise an error code.
  172. */
  173. uint32_t ble_rscs_measurement_send(ble_rscs_t * p_rscs, ble_rscs_meas_t * p_measurement);
  174. #ifdef __cplusplus
  175. }
  176. #endif
  177. #endif // BLE_RSCS_H__
  178. /** @} */