ble_cscs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 "ble_cscs.h"
  45. #include <string.h>
  46. #include "nordic_common.h"
  47. #include "ble.h"
  48. #include "ble_err.h"
  49. #include "ble_srv_common.h"
  50. #include "app_util.h"
  51. #define OPCODE_LENGTH 1 /**< Length of opcode inside Cycling Speed and Cadence Measurement packet. */
  52. #define HANDLE_LENGTH 2 /**< Length of handle inside Cycling Speed and Cadence Measurement packet. */
  53. #define MAX_CSCM_LEN (BLE_GATT_ATT_MTU_DEFAULT - OPCODE_LENGTH - HANDLE_LENGTH) /**< Maximum size of a transmitted Cycling Speed and Cadence Measurement. */
  54. // Cycling Speed and Cadence Measurement flag bits
  55. #define CSC_MEAS_FLAG_MASK_WHEEL_REV_DATA_PRESENT (0x01 << 0) /**< Wheel revolution data present flag bit. */
  56. #define CSC_MEAS_FLAG_MASK_CRANK_REV_DATA_PRESENT (0x01 << 1) /**< Crank revolution data present flag bit. */
  57. /**@brief Function for handling the Connect event.
  58. *
  59. * @param[in] p_cscs Cycling Speed and Cadence Service structure.
  60. * @param[in] p_ble_evt Event received from the BLE stack.
  61. */
  62. static void on_connect(ble_cscs_t * p_cscs, ble_evt_t const * p_ble_evt)
  63. {
  64. p_cscs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  65. }
  66. /**@brief Function for handling the Disconnect event.
  67. *
  68. * @param[in] p_cscs Cycling Speed and Cadence Service structure.
  69. * @param[in] p_ble_evt Event received from the BLE stack.
  70. */
  71. static void on_disconnect(ble_cscs_t * p_cscs, ble_evt_t const * p_ble_evt)
  72. {
  73. UNUSED_PARAMETER(p_ble_evt);
  74. p_cscs->conn_handle = BLE_CONN_HANDLE_INVALID;
  75. }
  76. /**@brief Function for handling write events to the CSCS Measurement characteristic.
  77. *
  78. * @param[in] p_cscs Cycling 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_cscs_t * p_cscs, 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_cscs->evt_handler != NULL)
  87. {
  88. ble_cscs_evt_t evt;
  89. if (ble_srv_is_notification_enabled(p_evt_write->data))
  90. {
  91. evt.evt_type = BLE_CSCS_EVT_NOTIFICATION_ENABLED;
  92. }
  93. else
  94. {
  95. evt.evt_type = BLE_CSCS_EVT_NOTIFICATION_DISABLED;
  96. }
  97. p_cscs->evt_handler(p_cscs, &evt);
  98. }
  99. }
  100. }
  101. /**@brief Function for handling the Write event.
  102. *
  103. * @param[in] p_cscs Cycling Speed and Cadence Service structure.
  104. * @param[in] p_ble_evt Event received from the BLE stack.
  105. */
  106. static void on_write(ble_cscs_t * p_cscs, 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_cscs->meas_handles.cccd_handle)
  110. {
  111. on_meas_cccd_write(p_cscs, p_evt_write);
  112. }
  113. }
  114. void ble_cscs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  115. {
  116. ble_cscs_t * p_cscs = (ble_cscs_t *)p_context;
  117. if (p_cscs == NULL || p_ble_evt == NULL)
  118. {
  119. return;
  120. }
  121. ble_sc_ctrlpt_on_ble_evt(&(p_cscs->ctrl_pt), p_ble_evt);
  122. switch (p_ble_evt->header.evt_id)
  123. {
  124. case BLE_GAP_EVT_CONNECTED:
  125. on_connect(p_cscs, p_ble_evt);
  126. break;
  127. case BLE_GAP_EVT_DISCONNECTED:
  128. on_disconnect(p_cscs, p_ble_evt);
  129. break;
  130. case BLE_GATTS_EVT_WRITE:
  131. on_write(p_cscs, p_ble_evt);
  132. break;
  133. default:
  134. // No implementation needed.
  135. break;
  136. }
  137. }
  138. /**@brief Function for encoding a CSCS Measurement.
  139. *
  140. * @param[in] p_cscs Cycling Speed and Cadence Service structure.
  141. * @param[in] p_csc_measurement Measurement to be encoded.
  142. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  143. *
  144. * @return Size of encoded data.
  145. */
  146. static uint8_t csc_measurement_encode(ble_cscs_t * p_cscs,
  147. ble_cscs_meas_t * p_csc_measurement,
  148. uint8_t * p_encoded_buffer)
  149. {
  150. uint8_t flags = 0;
  151. uint8_t len = 1;
  152. // Cumulative Wheel Revolutions and Last Wheel Event Time Fields
  153. if (p_cscs->feature & BLE_CSCS_FEATURE_WHEEL_REV_BIT)
  154. {
  155. if (p_csc_measurement->is_wheel_rev_data_present)
  156. {
  157. flags |= CSC_MEAS_FLAG_MASK_WHEEL_REV_DATA_PRESENT;
  158. len += uint32_encode(p_csc_measurement->cumulative_wheel_revs, &p_encoded_buffer[len]);
  159. len += uint16_encode(p_csc_measurement->last_wheel_event_time, &p_encoded_buffer[len]);
  160. }
  161. }
  162. // Cumulative Crank Revolutions and Last Crank Event Time Fields
  163. if (p_cscs->feature & BLE_CSCS_FEATURE_CRANK_REV_BIT)
  164. {
  165. if (p_csc_measurement->is_crank_rev_data_present)
  166. {
  167. flags |= CSC_MEAS_FLAG_MASK_CRANK_REV_DATA_PRESENT;
  168. len += uint16_encode(p_csc_measurement->cumulative_crank_revs, &p_encoded_buffer[len]);
  169. len += uint16_encode(p_csc_measurement->last_crank_event_time, &p_encoded_buffer[len]);
  170. }
  171. }
  172. // Flags Field
  173. p_encoded_buffer[0] = flags;
  174. return len;
  175. }
  176. uint32_t ble_cscs_init(ble_cscs_t * p_cscs, ble_cscs_init_t const * p_cscs_init)
  177. {
  178. if (p_cscs == NULL || p_cscs_init == NULL)
  179. {
  180. return NRF_ERROR_NULL;
  181. }
  182. uint32_t err_code;
  183. uint8_t init_value_encoded[MAX_CSCM_LEN];
  184. ble_cscs_meas_t initial_scm = {0};
  185. ble_add_char_params_t add_char_params;
  186. ble_uuid_t ble_uuid;
  187. ble_cs_ctrlpt_init_t sc_ctrlpt_init;
  188. // Initialize service structure
  189. p_cscs->evt_handler = p_cscs_init->evt_handler;
  190. p_cscs->conn_handle = BLE_CONN_HANDLE_INVALID;
  191. p_cscs->feature = p_cscs_init->feature;
  192. // Add service
  193. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_CYCLING_SPEED_AND_CADENCE);
  194. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  195. &ble_uuid,
  196. &p_cscs->service_handle);
  197. if (err_code != NRF_SUCCESS)
  198. {
  199. return err_code;
  200. }
  201. // Add cycling speed and cadence measurement characteristic
  202. memset(&add_char_params, 0, sizeof(add_char_params));
  203. add_char_params.uuid = BLE_UUID_CSC_MEASUREMENT_CHAR;
  204. add_char_params.max_len = MAX_CSCM_LEN;
  205. add_char_params.is_var_len = true;
  206. add_char_params.init_len = csc_measurement_encode(p_cscs, &initial_scm, init_value_encoded);
  207. add_char_params.p_init_value = init_value_encoded;
  208. add_char_params.char_props.notify = 1;
  209. add_char_params.cccd_write_access = p_cscs_init->csc_meas_cccd_wr_sec;
  210. err_code = characteristic_add(p_cscs->service_handle, &add_char_params, &p_cscs->meas_handles);
  211. if (err_code != NRF_SUCCESS)
  212. {
  213. return err_code;
  214. }
  215. // Add cycling speed and cadence feature characteristic
  216. memset(&add_char_params, 0, sizeof(add_char_params));
  217. add_char_params.uuid = BLE_UUID_CSC_FEATURE_CHAR;
  218. add_char_params.max_len = sizeof(uint16_t);
  219. add_char_params.init_len = uint16_encode(p_cscs_init->feature, &init_value_encoded[0]);
  220. add_char_params.p_init_value = init_value_encoded;
  221. add_char_params.char_props.read = 1;
  222. add_char_params.read_access = p_cscs_init->csc_feature_rd_sec;
  223. err_code = characteristic_add(p_cscs->service_handle,
  224. &add_char_params,
  225. &p_cscs->feature_handles);
  226. if (err_code != NRF_SUCCESS)
  227. {
  228. return err_code;
  229. }
  230. // Add Sensor Location characteristic (optional)
  231. if (p_cscs_init->sensor_location != NULL)
  232. {
  233. memset(&add_char_params, 0, sizeof(add_char_params));
  234. add_char_params.uuid = BLE_UUID_SENSOR_LOCATION_CHAR;
  235. add_char_params.max_len = sizeof(uint8_t);
  236. add_char_params.init_len = sizeof(uint8_t);
  237. add_char_params.p_init_value = (uint8_t *)p_cscs_init->sensor_location;
  238. add_char_params.char_props.read = 1;
  239. add_char_params.read_access = p_cscs_init->csc_location_rd_sec;
  240. err_code = characteristic_add(p_cscs->service_handle, &add_char_params, &p_cscs->sensor_loc_handles);
  241. if (err_code != NRF_SUCCESS)
  242. {
  243. return err_code;
  244. }
  245. }
  246. // Add speed and cadence control point characteristic
  247. sc_ctrlpt_init.error_handler = p_cscs_init->error_handler;
  248. sc_ctrlpt_init.size_list_supported_locations = p_cscs_init->size_list_supported_locations;
  249. sc_ctrlpt_init.supported_functions = p_cscs_init->ctrplt_supported_functions;
  250. sc_ctrlpt_init.evt_handler = p_cscs_init->ctrlpt_evt_handler;
  251. sc_ctrlpt_init.list_supported_locations = p_cscs_init->list_supported_locations;
  252. sc_ctrlpt_init.sc_ctrlpt_wr_sec = p_cscs_init->sc_ctrlpt_wr_sec;
  253. sc_ctrlpt_init.sc_ctrlpt_cccd_wr_sec = p_cscs_init->sc_ctrlpt_cccd_wr_sec;
  254. sc_ctrlpt_init.sensor_location_handle = p_cscs->sensor_loc_handles.value_handle;
  255. sc_ctrlpt_init.service_handle = p_cscs->service_handle;
  256. return ble_sc_ctrlpt_init(&p_cscs->ctrl_pt, &sc_ctrlpt_init);
  257. }
  258. uint32_t ble_cscs_measurement_send(ble_cscs_t * p_cscs, ble_cscs_meas_t * p_measurement)
  259. {
  260. if (p_cscs == NULL || p_measurement == NULL)
  261. {
  262. return NRF_ERROR_NULL;
  263. }
  264. uint32_t err_code;
  265. // Send value if connected and notifying
  266. if (p_cscs->conn_handle != BLE_CONN_HANDLE_INVALID)
  267. {
  268. uint8_t encoded_csc_meas[MAX_CSCM_LEN];
  269. uint16_t len;
  270. uint16_t hvx_len;
  271. ble_gatts_hvx_params_t hvx_params;
  272. len = csc_measurement_encode(p_cscs, p_measurement, encoded_csc_meas);
  273. hvx_len = len;
  274. memset(&hvx_params, 0, sizeof(hvx_params));
  275. hvx_params.handle = p_cscs->meas_handles.value_handle;
  276. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  277. hvx_params.offset = 0;
  278. hvx_params.p_len = &hvx_len;
  279. hvx_params.p_data = encoded_csc_meas;
  280. err_code = sd_ble_gatts_hvx(p_cscs->conn_handle, &hvx_params);
  281. if ((err_code == NRF_SUCCESS) && (hvx_len != len))
  282. {
  283. err_code = NRF_ERROR_DATA_SIZE;
  284. }
  285. }
  286. else
  287. {
  288. err_code = NRF_ERROR_INVALID_STATE;
  289. }
  290. return err_code;
  291. }