ble_rscs_c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /**@cond To Make Doxygen skip documentation generation for this file.
  41. * @{
  42. */
  43. #include "sdk_common.h"
  44. #if NRF_MODULE_ENABLED(BLE_RSCS_C)
  45. #include "ble_rscs_c.h"
  46. #include "ble_db_discovery.h"
  47. #include "ble_types.h"
  48. #include "ble_gattc.h"
  49. #define NRF_LOG_MODULE_NAME ble_rscs_c
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. #define WRITE_MESSAGE_LENGTH BLE_CCCD_VALUE_LEN /**< Length of the write message for CCCD. */
  53. /**@brief Function for intercepting the errors of GATTC and the BLE GATT Queue.
  54. *
  55. * @param[in] nrf_error Error code.
  56. * @param[in] p_ctx Parameter from the event handler.
  57. * @param[in] conn_handle Connection handle.
  58. */
  59. static void gatt_error_handler(uint32_t nrf_error,
  60. void * p_ctx,
  61. uint16_t conn_handle)
  62. {
  63. ble_rscs_c_t * p_ble_rscs_c = (ble_rscs_c_t *)p_ctx;
  64. NRF_LOG_DEBUG("A GATT Client error has occurred on conn_handle: 0X%X", conn_handle);
  65. if (p_ble_rscs_c->error_handler != NULL)
  66. {
  67. p_ble_rscs_c->error_handler(nrf_error);
  68. }
  69. }
  70. /**@brief Function for handling Handle Value Notification received from the SoftDevice.
  71. *
  72. * @details This function uses the Handle Value Notification received from the SoftDevice
  73. * and checks whether it is a notification of the Running Speed and Cadence measurement from
  74. * the peer. If it is, this function decodes the Running Speed measurement and sends it
  75. * to the application.
  76. *
  77. * @param[in] p_ble_rscs_c Pointer to the Running Speed and Cadence Client structure.
  78. * @param[in] p_ble_evt Pointer to the BLE event received.
  79. */
  80. static void on_hvx(ble_rscs_c_t * p_ble_rscs_c, const ble_evt_t * p_ble_evt)
  81. {
  82. const ble_gattc_evt_hvx_t * p_notif = &p_ble_evt->evt.gattc_evt.params.hvx;
  83. // Check if the event is on the link for this instance
  84. if (p_ble_rscs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
  85. {
  86. return;
  87. }
  88. // Check if this is a Running Speed and Cadence notification.
  89. if (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_rscs_c->peer_db.rsc_handle)
  90. {
  91. uint32_t index = 0;
  92. ble_rscs_c_evt_t ble_rscs_c_evt;
  93. ble_rscs_c_evt.evt_type = BLE_RSCS_C_EVT_RSC_NOTIFICATION;
  94. ble_rscs_c_evt.conn_handle = p_ble_evt->evt.gattc_evt.conn_handle;
  95. //lint -save -e415 -e416 -e662 "Access of out of bounds pointer" "Creation of out of bounds pointer"
  96. // Flags field
  97. ble_rscs_c_evt.params.rsc.is_inst_stride_len_present = p_notif->data[index] >> BLE_RSCS_INSTANT_STRIDE_LEN_PRESENT & 0x01;
  98. ble_rscs_c_evt.params.rsc.is_total_distance_present = p_notif->data[index] >> BLE_RSCS_TOTAL_DISTANCE_PRESENT & 0x01;
  99. ble_rscs_c_evt.params.rsc.is_running = p_notif->data[index] >> BLE_RSCS_WALKING_OR_RUNNING_STATUS_BIT & 0x01;
  100. index++;
  101. // Instantaneous speed
  102. ble_rscs_c_evt.params.rsc.inst_speed = uint16_decode(&p_notif->data[index]);
  103. index += sizeof(uint16_t);
  104. // Instantaneous cadence
  105. ble_rscs_c_evt.params.rsc.inst_cadence = p_notif->data[index];
  106. index++;
  107. // Instantaneous stride length
  108. if (ble_rscs_c_evt.params.rsc.is_inst_stride_len_present == true)
  109. {
  110. ble_rscs_c_evt.params.rsc.inst_stride_length = uint16_decode(&p_notif->data[index]);
  111. index += sizeof(uint16_t);
  112. }
  113. // Total distance field
  114. if (ble_rscs_c_evt.params.rsc.is_total_distance_present == true)
  115. {
  116. ble_rscs_c_evt.params.rsc.total_distance = uint32_decode(&p_notif->data[index]);
  117. //index += sizeof(uint32_t);
  118. }
  119. p_ble_rscs_c->evt_handler(p_ble_rscs_c, &ble_rscs_c_evt);
  120. //lint -restore
  121. }
  122. }
  123. /**@brief Function for handling events from the Database Discovery module.
  124. *
  125. * @details This function handles an event from the Database Discovery module, and determines
  126. * whether it relates to the discovery of Running Speed and Cadence service at the peer. If it does, the function
  127. * calls the application's event handler to indicate that the Running Speed and Cadence
  128. * service was discovered at the peer. The function also populates the event with service-related
  129. * information before providing it to the application.
  130. *
  131. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  132. *
  133. */
  134. void ble_rscs_on_db_disc_evt(ble_rscs_c_t * p_ble_rscs_c, const ble_db_discovery_evt_t * p_evt)
  135. {
  136. // Check if the Heart Rate Service was discovered.
  137. if (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE &&
  138. p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_RUNNING_SPEED_AND_CADENCE &&
  139. p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE)
  140. {
  141. ble_rscs_c_evt_t evt;
  142. evt.conn_handle = p_evt->conn_handle;
  143. // Find the CCCD Handle of the Running Speed and Cadence characteristic.
  144. for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
  145. {
  146. if (p_evt->params.discovered_db.charateristics[i].characteristic.uuid.uuid ==
  147. BLE_UUID_RSC_MEASUREMENT_CHAR)
  148. {
  149. // Found Running Speed and Cadence characteristic. Store CCCD handle and break.
  150. evt.params.rscs_db.rsc_cccd_handle =
  151. p_evt->params.discovered_db.charateristics[i].cccd_handle;
  152. evt.params.rscs_db.rsc_handle =
  153. p_evt->params.discovered_db.charateristics[i].characteristic.handle_value;
  154. break;
  155. }
  156. }
  157. NRF_LOG_DEBUG("Running Speed and Cadence Service discovered at peer.");
  158. // If the instance has been assigned prior to db_discovery, assign the db_handles.
  159. if (p_ble_rscs_c->conn_handle != BLE_CONN_HANDLE_INVALID)
  160. {
  161. if ((p_ble_rscs_c->peer_db.rsc_cccd_handle == BLE_GATT_HANDLE_INVALID)&&
  162. (p_ble_rscs_c->peer_db.rsc_handle == BLE_GATT_HANDLE_INVALID))
  163. {
  164. p_ble_rscs_c->peer_db = evt.params.rscs_db;
  165. }
  166. }
  167. evt.evt_type = BLE_RSCS_C_EVT_DISCOVERY_COMPLETE;
  168. p_ble_rscs_c->evt_handler(p_ble_rscs_c, &evt);
  169. }
  170. }
  171. uint32_t ble_rscs_c_init(ble_rscs_c_t * p_ble_rscs_c, ble_rscs_c_init_t * p_ble_rscs_c_init)
  172. {
  173. VERIFY_PARAM_NOT_NULL(p_ble_rscs_c);
  174. VERIFY_PARAM_NOT_NULL(p_ble_rscs_c_init);
  175. ble_uuid_t rscs_uuid;
  176. rscs_uuid.type = BLE_UUID_TYPE_BLE;
  177. rscs_uuid.uuid = BLE_UUID_RUNNING_SPEED_AND_CADENCE;
  178. p_ble_rscs_c->evt_handler = p_ble_rscs_c_init->evt_handler;
  179. p_ble_rscs_c->error_handler = p_ble_rscs_c_init->error_handler;
  180. p_ble_rscs_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  181. p_ble_rscs_c->peer_db.rsc_cccd_handle = BLE_GATT_HANDLE_INVALID;
  182. p_ble_rscs_c->peer_db.rsc_handle = BLE_GATT_HANDLE_INVALID;
  183. p_ble_rscs_c->p_gatt_queue = p_ble_rscs_c_init->p_gatt_queue;
  184. return ble_db_discovery_evt_register(&rscs_uuid);
  185. }
  186. uint32_t ble_rscs_c_handles_assign(ble_rscs_c_t * p_ble_rscs_c,
  187. uint16_t conn_handle,
  188. ble_rscs_c_db_t * p_peer_handles)
  189. {
  190. VERIFY_PARAM_NOT_NULL(p_ble_rscs_c);
  191. p_ble_rscs_c->conn_handle = conn_handle;
  192. if (p_peer_handles != NULL)
  193. {
  194. p_ble_rscs_c->peer_db = *p_peer_handles;
  195. }
  196. return nrf_ble_gq_conn_handle_register(p_ble_rscs_c->p_gatt_queue, conn_handle);
  197. }
  198. /**@brief Function for handling Disconnected event received from the SoftDevice.
  199. *
  200. * @details This function check whether the disconnect event is happening on the link
  201. * associated with the current instance of the module. If the event is happening, the function sets the instance's
  202. * conn_handle to invalid.
  203. *
  204. * @param[in] p_ble_rscs_c Pointer to the RSC Client structure.
  205. * @param[in] p_ble_evt Pointer to the BLE event received.
  206. */
  207. static void on_disconnected(ble_rscs_c_t * p_ble_rscs_c, const ble_evt_t * p_ble_evt)
  208. {
  209. if (p_ble_rscs_c->conn_handle == p_ble_evt->evt.gap_evt.conn_handle)
  210. {
  211. p_ble_rscs_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  212. p_ble_rscs_c->peer_db.rsc_cccd_handle = BLE_GATT_HANDLE_INVALID;
  213. p_ble_rscs_c->peer_db.rsc_handle = BLE_GATT_HANDLE_INVALID;
  214. }
  215. }
  216. void ble_rscs_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  217. {
  218. if ((p_context == NULL) || (p_ble_evt == NULL))
  219. {
  220. return;
  221. }
  222. ble_rscs_c_t * p_ble_rscs_c = (ble_rscs_c_t *)p_context;
  223. switch (p_ble_evt->header.evt_id)
  224. {
  225. case BLE_GATTC_EVT_HVX:
  226. on_hvx(p_ble_rscs_c, p_ble_evt);
  227. break;
  228. case BLE_GAP_EVT_DISCONNECTED:
  229. on_disconnected(p_ble_rscs_c, p_ble_evt);
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. /**@brief Function for creating a message for writing to the CCCD.
  236. */
  237. static uint32_t cccd_configure(ble_rscs_c_t * p_ble_rscs_c, bool enable)
  238. {
  239. NRF_LOG_DEBUG("Configuring CCCD. CCCD Handle = %d, Connection Handle = %d",
  240. p_ble_rscs_c->peer_db.rsc_cccd_handle,
  241. p_ble_rscs_c->conn_handle);
  242. uint8_t cccd[WRITE_MESSAGE_LENGTH];
  243. uint16_t cccd_val = enable ? BLE_GATT_HVX_NOTIFICATION : 0;
  244. nrf_ble_gq_req_t rscs_c_req;
  245. cccd[0] = LSB_16(cccd_val);
  246. cccd[1] = MSB_16(cccd_val);
  247. memset(&rscs_c_req, 0, sizeof(rscs_c_req));
  248. rscs_c_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  249. rscs_c_req.error_handler.cb = gatt_error_handler;
  250. rscs_c_req.error_handler.p_ctx = p_ble_rscs_c;
  251. rscs_c_req.params.gattc_write.handle = p_ble_rscs_c->peer_db.rsc_cccd_handle;
  252. rscs_c_req.params.gattc_write.len = WRITE_MESSAGE_LENGTH;
  253. rscs_c_req.params.gattc_write.p_value = cccd;
  254. rscs_c_req.params.gattc_write.offset = 0;
  255. rscs_c_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_REQ;
  256. return nrf_ble_gq_item_add(p_ble_rscs_c->p_gatt_queue, &rscs_c_req, p_ble_rscs_c->conn_handle);
  257. }
  258. uint32_t ble_rscs_c_rsc_notif_enable(ble_rscs_c_t * p_ble_rscs_c)
  259. {
  260. VERIFY_PARAM_NOT_NULL(p_ble_rscs_c);
  261. if (p_ble_rscs_c->conn_handle == BLE_CONN_HANDLE_INVALID)
  262. {
  263. return NRF_ERROR_INVALID_STATE;
  264. }
  265. return cccd_configure(p_ble_rscs_c, true);
  266. }
  267. /** @}
  268. * @endcond
  269. */
  270. #endif // NRF_MODULE_ENABLED(BLE_RSCS_C)