ble_rscs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "sdk_common.h"
  45. #if NRF_MODULE_ENABLED(BLE_RSCS)
  46. #include "ble_rscs.h"
  47. #include <string.h>
  48. #include "ble.h"
  49. #include "ble_srv_common.h"
  50. #define OPCODE_LENGTH 1 /**< Length of opcode inside Running Speed and Cadence Measurement packet. */
  51. #define HANDLE_LENGTH 2 /**< Length of handle inside Running Speed and Cadence Measurement packet. */
  52. #define MAX_RSCM_LEN (BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Running Speed and Cadence Measurement. */
  53. // Running Speed and Cadence Measurement flag bits
  54. #define RSC_MEAS_FLAG_INSTANT_STRIDE_LEN_PRESENT (0x01 << 0) /**< Instantaneous Stride Length Present flag bit. */
  55. #define RSC_MEAS_FLAG_TOTAL_DISTANCE_PRESENT (0x01 << 1) /**< Total Distance Present flag bit. */
  56. #define RSC_MEAS_FLAG_WALKING_OR_RUNNING_BIT (0x01 << 2) /**< Walking or Running Status flag bit. */
  57. /**@brief Function for handling the Connect event.
  58. *
  59. * @param[in] p_rscs Running Speed and Cadence Service structure.
  60. * @param[in] p_ble_evt Event received from the BLE stack.
  61. */
  62. static void on_connect(ble_rscs_t * p_rscs, ble_evt_t const * p_ble_evt)
  63. {
  64. p_rscs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  65. }
  66. /**@brief Function for handling the Disconnect event.
  67. *
  68. * @param[in] p_rscs Running Speed and Cadence Service structure.
  69. * @param[in] p_ble_evt Event received from the BLE stack.
  70. */
  71. static void on_disconnect(ble_rscs_t * p_rscs, ble_evt_t const * p_ble_evt)
  72. {
  73. UNUSED_PARAMETER(p_ble_evt);
  74. p_rscs->conn_handle = BLE_CONN_HANDLE_INVALID;
  75. }
  76. /**@brief Function for handling the write events to the RSCS Measurement characteristic.
  77. *
  78. * @param[in] p_rscs Running Speed and Cadence Service structure.
  79. * @param[in] p_evt_write Write event received from the BLE stack.
  80. */
  81. static void on_meas_cccd_write(ble_rscs_t * p_rscs, ble_gatts_evt_write_t const * p_evt_write)
  82. {
  83. if (p_evt_write->len == 2)
  84. {
  85. // CCCD written, update notification state
  86. if (p_rscs->evt_handler != NULL)
  87. {
  88. ble_rscs_evt_t evt;
  89. if (ble_srv_is_notification_enabled(p_evt_write->data))
  90. {
  91. evt.evt_type = BLE_RSCS_EVT_NOTIFICATION_ENABLED;
  92. }
  93. else
  94. {
  95. evt.evt_type = BLE_RSCS_EVT_NOTIFICATION_DISABLED;
  96. }
  97. p_rscs->evt_handler(p_rscs, &evt);
  98. }
  99. }
  100. }
  101. /**@brief Function for handling the Write event.
  102. *
  103. * @param[in] p_rscs Running Speed and Cadence Service structure.
  104. * @param[in] p_ble_evt Event received from the BLE stack.
  105. */
  106. static void on_write(ble_rscs_t * p_rscs, ble_evt_t const * p_ble_evt)
  107. {
  108. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  109. if (p_evt_write->handle == p_rscs->meas_handles.cccd_handle)
  110. {
  111. on_meas_cccd_write(p_rscs, p_evt_write);
  112. }
  113. }
  114. void ble_rscs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  115. {
  116. if ((p_context == NULL) || (p_ble_evt == NULL))
  117. {
  118. return;
  119. }
  120. ble_rscs_t * p_rscs = (ble_rscs_t *)p_context;
  121. switch (p_ble_evt->header.evt_id)
  122. {
  123. case BLE_GAP_EVT_CONNECTED:
  124. on_connect(p_rscs, p_ble_evt);
  125. break;
  126. case BLE_GAP_EVT_DISCONNECTED:
  127. on_disconnect(p_rscs, p_ble_evt);
  128. break;
  129. case BLE_GATTS_EVT_WRITE:
  130. on_write(p_rscs, p_ble_evt);
  131. break;
  132. default:
  133. // No implementation needed.
  134. break;
  135. }
  136. }
  137. /**@brief Function for encoding a RSCS Measurement.
  138. *
  139. * @param[in] p_rscs Running Speed and Cadence Service structure.
  140. * @param[in] p_rsc_measurement Measurement to be encoded.
  141. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  142. *
  143. * @return Size of encoded data.
  144. */
  145. static uint8_t rsc_measurement_encode(const ble_rscs_t * p_rscs,
  146. const ble_rscs_meas_t * p_rsc_measurement,
  147. uint8_t * p_encoded_buffer)
  148. {
  149. uint8_t flags = 0;
  150. uint8_t len = 1;
  151. // Instantaneous speed field
  152. len += uint16_encode(p_rsc_measurement->inst_speed, &p_encoded_buffer[len]);
  153. // Instantaneous cadence field
  154. p_encoded_buffer[len++] = p_rsc_measurement->inst_cadence;
  155. // Instantaneous stride length field
  156. if (p_rscs->feature & BLE_RSCS_FEATURE_INSTANT_STRIDE_LEN_BIT)
  157. {
  158. if (p_rsc_measurement->is_inst_stride_len_present)
  159. {
  160. flags |= RSC_MEAS_FLAG_INSTANT_STRIDE_LEN_PRESENT;
  161. len += uint16_encode(p_rsc_measurement->inst_stride_length,
  162. &p_encoded_buffer[len]);
  163. }
  164. }
  165. // Total distance field
  166. if (p_rscs->feature & BLE_RSCS_FEATURE_TOTAL_DISTANCE_BIT)
  167. {
  168. if (p_rsc_measurement->is_total_distance_present)
  169. {
  170. flags |= RSC_MEAS_FLAG_TOTAL_DISTANCE_PRESENT;
  171. len += uint32_encode(p_rsc_measurement->total_distance, &p_encoded_buffer[len]);
  172. }
  173. }
  174. // Flags field
  175. if (p_rscs->feature & BLE_RSCS_FEATURE_WALKING_OR_RUNNING_STATUS_BIT)
  176. {
  177. if (p_rsc_measurement->is_running)
  178. {
  179. flags |= RSC_MEAS_FLAG_WALKING_OR_RUNNING_BIT;
  180. }
  181. }
  182. p_encoded_buffer[0] = flags;
  183. return len;
  184. }
  185. uint32_t ble_rscs_init(ble_rscs_t * p_rscs, const ble_rscs_init_t * p_rscs_init)
  186. {
  187. if (p_rscs == NULL || p_rscs_init == NULL)
  188. {
  189. return NRF_ERROR_NULL;
  190. }
  191. uint32_t err_code;
  192. uint8_t init_value_encoded[MAX(MAX_RSCM_LEN, sizeof(uint16_t))];
  193. ble_uuid_t ble_uuid;
  194. ble_add_char_params_t add_char_params;
  195. // Initialize service structure
  196. p_rscs->evt_handler = p_rscs_init->evt_handler;
  197. p_rscs->conn_handle = BLE_CONN_HANDLE_INVALID;
  198. p_rscs->feature = p_rscs_init->feature;
  199. // Add service
  200. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_RUNNING_SPEED_AND_CADENCE);
  201. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  202. &ble_uuid,
  203. &p_rscs->service_handle);
  204. if (err_code != NRF_SUCCESS)
  205. {
  206. return err_code;
  207. }
  208. // Add measurement characteristic
  209. memset(&add_char_params, 0, sizeof(add_char_params));
  210. add_char_params.uuid = BLE_UUID_RSC_MEASUREMENT_CHAR;
  211. add_char_params.max_len = MAX_RSCM_LEN;
  212. add_char_params.is_var_len = true;
  213. add_char_params.char_props.notify = 1;
  214. add_char_params.cccd_write_access = p_rscs_init->rsc_meas_cccd_wr_sec;
  215. add_char_params.p_init_value = init_value_encoded;
  216. add_char_params.init_len = rsc_measurement_encode(p_rscs,
  217. &p_rscs_init->initial_rcm,
  218. init_value_encoded);
  219. err_code = characteristic_add(p_rscs->service_handle, &add_char_params, &p_rscs->meas_handles);
  220. if (err_code != NRF_SUCCESS)
  221. {
  222. return err_code;
  223. }
  224. // Add feature characteristic
  225. memset(&add_char_params, 0, sizeof(add_char_params));
  226. add_char_params.uuid = BLE_UUID_RSC_FEATURE_CHAR;
  227. add_char_params.max_len = sizeof(uint16_t);
  228. add_char_params.init_len = uint16_encode(p_rscs_init->feature, init_value_encoded);
  229. add_char_params.p_init_value = init_value_encoded;
  230. add_char_params.char_props.read = 1;
  231. add_char_params.read_access = p_rscs_init->rsc_feature_rd_sec;
  232. err_code = characteristic_add(p_rscs->service_handle,
  233. &add_char_params,
  234. &p_rscs->feature_handles);
  235. if (err_code != NRF_SUCCESS)
  236. {
  237. return err_code;
  238. }
  239. return NRF_SUCCESS;
  240. }
  241. uint32_t ble_rscs_measurement_send(ble_rscs_t * p_rscs, ble_rscs_meas_t * p_measurement)
  242. {
  243. if (p_rscs == NULL || p_measurement == NULL)
  244. {
  245. return NRF_ERROR_NULL;
  246. }
  247. uint32_t err_code;
  248. // Send value if connected and notifying
  249. if (p_rscs->conn_handle != BLE_CONN_HANDLE_INVALID)
  250. {
  251. uint8_t encoded_rsc_meas[MAX_RSCM_LEN];
  252. uint16_t len;
  253. uint16_t hvx_len;
  254. ble_gatts_hvx_params_t hvx_params;
  255. len = rsc_measurement_encode(p_rscs, p_measurement, encoded_rsc_meas);
  256. hvx_len = len;
  257. memset(&hvx_params, 0, sizeof(hvx_params));
  258. hvx_params.handle = p_rscs->meas_handles.value_handle;
  259. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  260. hvx_params.offset = 0;
  261. hvx_params.p_len = &hvx_len;
  262. hvx_params.p_data = encoded_rsc_meas;
  263. err_code = sd_ble_gatts_hvx(p_rscs->conn_handle, &hvx_params);
  264. if ((err_code == NRF_SUCCESS) && (hvx_len != len))
  265. {
  266. err_code = NRF_ERROR_DATA_SIZE;
  267. }
  268. }
  269. else
  270. {
  271. err_code = NRF_ERROR_INVALID_STATE;
  272. }
  273. return err_code;
  274. }
  275. #endif // NRF_MODULE_ENABLED(BLE_RSCS)