ble_sc_ctrlpt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /**
  2. * Copyright (c) 2013 - 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_sc_ctrlpt.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 SC_CTRLPT_NACK_PROC_ALREADY_IN_PROGRESS (BLE_GATT_STATUS_ATTERR_APP_BEGIN + 0)
  52. #define SC_CTRLPT_NACK_CCCD_IMPROPERLY_CONFIGURED (BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1)
  53. uint32_t ble_sc_ctrlpt_init(ble_sc_ctrlpt_t * p_sc_ctrlpt,
  54. const ble_cs_ctrlpt_init_t * p_sc_ctrlpt_init)
  55. {
  56. if (p_sc_ctrlpt == NULL || p_sc_ctrlpt_init == NULL)
  57. {
  58. return NRF_ERROR_NULL;
  59. }
  60. ble_add_char_params_t add_char_params;
  61. p_sc_ctrlpt->conn_handle = BLE_CONN_HANDLE_INVALID;
  62. p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS;
  63. p_sc_ctrlpt->size_list_supported_locations = p_sc_ctrlpt_init->size_list_supported_locations;
  64. if ((p_sc_ctrlpt_init->size_list_supported_locations != 0) &&
  65. (p_sc_ctrlpt_init->list_supported_locations != NULL))
  66. {
  67. memcpy(p_sc_ctrlpt->list_supported_locations,
  68. p_sc_ctrlpt_init->list_supported_locations,
  69. p_sc_ctrlpt->size_list_supported_locations * sizeof(ble_sensor_location_t));
  70. }
  71. p_sc_ctrlpt->service_handle = p_sc_ctrlpt_init->service_handle;
  72. p_sc_ctrlpt->evt_handler = p_sc_ctrlpt_init->evt_handler;
  73. p_sc_ctrlpt->supported_functions = p_sc_ctrlpt_init->supported_functions;
  74. p_sc_ctrlpt->sensor_location_handle = p_sc_ctrlpt_init->sensor_location_handle;
  75. p_sc_ctrlpt->error_handler = p_sc_ctrlpt_init->error_handler;
  76. // Add speed and cadence control point characteristic
  77. memset(&add_char_params, 0, sizeof(add_char_params));
  78. add_char_params.uuid = BLE_UUID_SC_CTRLPT_CHAR;
  79. add_char_params.max_len = BLE_SC_CTRLPT_MAX_LEN;
  80. add_char_params.is_var_len = true;
  81. add_char_params.char_props.indicate = 1;
  82. add_char_params.char_props.write = 1;
  83. add_char_params.cccd_write_access = p_sc_ctrlpt_init->sc_ctrlpt_cccd_wr_sec;
  84. add_char_params.write_access = p_sc_ctrlpt_init->sc_ctrlpt_wr_sec;
  85. add_char_params.is_defered_write = true;
  86. return characteristic_add(p_sc_ctrlpt->service_handle,
  87. &add_char_params,
  88. &p_sc_ctrlpt->sc_ctrlpt_handles);
  89. }
  90. /**@brief Decode an incoming control point write.
  91. *
  92. * @param[in] rcvd_val received write value
  93. * @param[in] len value length
  94. * @param[out] decoded_ctrlpt decoded control point structure
  95. */
  96. static uint32_t sc_ctrlpt_decode(uint8_t const * p_rcvd_val,
  97. uint8_t len,
  98. ble_sc_ctrlpt_val_t * p_write_val)
  99. {
  100. int pos = 0;
  101. if (len < BLE_SC_CTRLPT_MIN_LEN)
  102. {
  103. return NRF_ERROR_INVALID_PARAM;
  104. }
  105. p_write_val->opcode = (ble_scpt_operator_t) p_rcvd_val[pos++];
  106. switch (p_write_val->opcode)
  107. {
  108. case BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS:
  109. break;
  110. case BLE_SCPT_START_AUTOMATIC_CALIBRATION:
  111. break;
  112. case BLE_SCPT_UPDATE_SENSOR_LOCATION:
  113. p_write_val->location = (ble_sensor_location_t)p_rcvd_val[pos];
  114. break;
  115. case BLE_SCPT_SET_CUMULATIVE_VALUE:
  116. p_write_val->cumulative_value = uint32_decode(&(p_rcvd_val[pos]));
  117. break;
  118. default:
  119. return NRF_ERROR_INVALID_PARAM;
  120. }
  121. return NRF_SUCCESS;
  122. }
  123. /**@brief encode a control point response indication.
  124. *
  125. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  126. * @param[in] p_ctrlpt_rsp structure containing response data to be encoded
  127. * @param[out] p_data pointer where data needs to be written
  128. * @return size of encoded data
  129. */
  130. static int ctrlpt_rsp_encode(ble_sc_ctrlpt_t * p_sc_ctrlpt,
  131. ble_sc_ctrlpt_rsp_t * p_ctrlpt_rsp,
  132. uint8_t * p_data)
  133. {
  134. int len = 0;
  135. p_data[len++] = BLE_SCPT_RESPONSE_CODE;
  136. p_data[len++] = p_ctrlpt_rsp->opcode;
  137. p_data[len++] = p_ctrlpt_rsp->status;
  138. if (p_ctrlpt_rsp->status == BLE_SCPT_SUCCESS)
  139. {
  140. switch (p_ctrlpt_rsp->opcode)
  141. {
  142. case BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS:
  143. {
  144. int i;
  145. for (i = 0; i < p_sc_ctrlpt->size_list_supported_locations; i++)
  146. {
  147. p_data[len++] = p_sc_ctrlpt->list_supported_locations[i];
  148. }
  149. break;
  150. }
  151. default:
  152. // No implementation needed.
  153. break;
  154. }
  155. }
  156. return len;
  157. }
  158. /**@brief check if a given sensor location is supported or not.
  159. *
  160. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  161. * @param[in] location sensor location to check.
  162. * @return true if the given location is found in the list of supported locations, false otherwise.
  163. */
  164. static bool is_location_supported(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_sensor_location_t location)
  165. {
  166. int i;
  167. for (i = 0; i < p_sc_ctrlpt->size_list_supported_locations; i++)
  168. {
  169. if (p_sc_ctrlpt->list_supported_locations[i] == location)
  170. {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. /**@brief check if the cccd is configured
  177. *
  178. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  179. * @return true if the sc_control point's cccd is correctly configured, false otherwise.
  180. */
  181. static bool is_cccd_configured(ble_sc_ctrlpt_t * p_sc_ctrlpt)
  182. {
  183. uint32_t err_code;
  184. uint8_t cccd_value_buf[BLE_CCCD_VALUE_LEN];
  185. bool is_sccp_indic_enabled = false;
  186. ble_gatts_value_t gatts_value;
  187. // Initialize value struct.
  188. memset(&gatts_value, 0, sizeof(gatts_value));
  189. gatts_value.len = BLE_CCCD_VALUE_LEN;
  190. gatts_value.offset = 0;
  191. gatts_value.p_value = cccd_value_buf;
  192. err_code = sd_ble_gatts_value_get(p_sc_ctrlpt->conn_handle,
  193. p_sc_ctrlpt->sc_ctrlpt_handles.cccd_handle,
  194. &gatts_value);
  195. if (err_code != NRF_SUCCESS)
  196. {
  197. // Report error to application
  198. if (p_sc_ctrlpt->error_handler != NULL)
  199. {
  200. p_sc_ctrlpt->error_handler(err_code);
  201. }
  202. }
  203. is_sccp_indic_enabled = ble_srv_is_indication_enabled(cccd_value_buf);
  204. return is_sccp_indic_enabled;
  205. }
  206. /**@brief sends a control point indication.
  207. *
  208. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  209. */
  210. static void sc_ctrlpt_resp_send(ble_sc_ctrlpt_t * p_sc_ctrlpt)
  211. {
  212. uint16_t hvx_len;
  213. ble_gatts_hvx_params_t hvx_params;
  214. uint32_t err_code;
  215. if (p_sc_ctrlpt->procedure_status == BLE_SCPT_INDICATION_PENDING)
  216. {
  217. hvx_len = p_sc_ctrlpt->response.len;
  218. memset(&hvx_params, 0, sizeof(hvx_params));
  219. hvx_params.handle = p_sc_ctrlpt->sc_ctrlpt_handles.value_handle;
  220. hvx_params.type = BLE_GATT_HVX_INDICATION;
  221. hvx_params.offset = 0;
  222. hvx_params.p_len = &hvx_len;
  223. hvx_params.p_data = p_sc_ctrlpt->response.encoded_ctrl_rsp;
  224. err_code = sd_ble_gatts_hvx(p_sc_ctrlpt->conn_handle, &hvx_params);
  225. // Error handling
  226. if ((err_code == NRF_SUCCESS) && (hvx_len != p_sc_ctrlpt->response.len))
  227. {
  228. err_code = NRF_ERROR_DATA_SIZE;
  229. }
  230. switch (err_code)
  231. {
  232. case NRF_SUCCESS:
  233. p_sc_ctrlpt->procedure_status = BLE_SCPT_IND_CONFIRM_PENDING;
  234. // Wait for HVC event
  235. break;
  236. case NRF_ERROR_RESOURCES:
  237. // Wait for TX_COMPLETE event to retry transmission.
  238. p_sc_ctrlpt->procedure_status = BLE_SCPT_INDICATION_PENDING;
  239. break;
  240. default:
  241. // Report error to application.
  242. p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS;
  243. if (p_sc_ctrlpt->error_handler != NULL)
  244. {
  245. p_sc_ctrlpt->error_handler(err_code);
  246. }
  247. break;
  248. }
  249. }
  250. }
  251. /**@brief Handle a write event to the Speed and Cadence Control Point.
  252. *
  253. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  254. * @param[in] p_evt_write WRITE event to be handled.
  255. */
  256. static void on_ctrlpt_write(ble_sc_ctrlpt_t * p_sc_ctrlpt,
  257. ble_gatts_evt_write_t const * p_evt_write)
  258. {
  259. ble_sc_ctrlpt_val_t rcvd_ctrlpt =
  260. { BLE_SCPT_RESPONSE_CODE , 0, BLE_SENSOR_LOCATION_OTHER };
  261. ble_sc_ctrlpt_rsp_t rsp;
  262. uint32_t err_code;
  263. ble_gatts_rw_authorize_reply_params_t auth_reply;
  264. ble_sc_ctrlpt_evt_t evt;
  265. auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  266. auth_reply.params.write.offset = 0;
  267. auth_reply.params.write.len = 0;
  268. auth_reply.params.write.p_data = NULL;
  269. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  270. auth_reply.params.write.update = 1;
  271. if (is_cccd_configured(p_sc_ctrlpt))
  272. {
  273. if (p_sc_ctrlpt->procedure_status == BLE_SCPT_NO_PROC_IN_PROGRESS)
  274. {
  275. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  276. }
  277. else
  278. {
  279. auth_reply.params.write.gatt_status = SC_CTRLPT_NACK_PROC_ALREADY_IN_PROGRESS;
  280. }
  281. }
  282. else
  283. {
  284. auth_reply.params.write.gatt_status = SC_CTRLPT_NACK_CCCD_IMPROPERLY_CONFIGURED;
  285. }
  286. err_code = sd_ble_gatts_rw_authorize_reply(p_sc_ctrlpt->conn_handle, &auth_reply);
  287. if (err_code != NRF_SUCCESS)
  288. {
  289. // Report error to application.
  290. if (p_sc_ctrlpt->error_handler != NULL)
  291. {
  292. p_sc_ctrlpt->error_handler(err_code);
  293. }
  294. }
  295. if (auth_reply.params.write.gatt_status != BLE_GATT_STATUS_SUCCESS)
  296. {
  297. return;
  298. }
  299. p_sc_ctrlpt->procedure_status = BLE_SCPT_INDICATION_PENDING;
  300. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  301. err_code = sc_ctrlpt_decode(p_evt_write->data, p_evt_write->len, &rcvd_ctrlpt);
  302. if (err_code != NRF_SUCCESS)
  303. {
  304. rsp.opcode = rcvd_ctrlpt.opcode;
  305. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  306. }
  307. else
  308. {
  309. rsp.opcode = rcvd_ctrlpt.opcode;
  310. switch (rcvd_ctrlpt.opcode)
  311. {
  312. case BLE_SCPT_REQUEST_SUPPORTED_SENSOR_LOCATIONS:
  313. if ((p_sc_ctrlpt->supported_functions &
  314. BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED) ==
  315. BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED)
  316. {
  317. rsp.status = BLE_SCPT_SUCCESS;
  318. }
  319. else
  320. {
  321. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  322. }
  323. break;
  324. case BLE_SCPT_UPDATE_SENSOR_LOCATION:
  325. if ((p_sc_ctrlpt->supported_functions &
  326. BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED) ==
  327. BLE_SRV_SC_CTRLPT_SENSOR_LOCATIONS_OP_SUPPORTED)
  328. {
  329. if (is_location_supported(p_sc_ctrlpt, rcvd_ctrlpt.location))
  330. {
  331. ble_gatts_value_t gatts_value;
  332. uint8_t rcvd_location = (uint8_t)rcvd_ctrlpt.location;
  333. rsp.status = BLE_SCPT_SUCCESS;
  334. // Initialize value struct.
  335. memset(&gatts_value, 0, sizeof(gatts_value));
  336. gatts_value.len = sizeof(uint8_t);
  337. gatts_value.offset = 0;
  338. gatts_value.p_value = &rcvd_location;
  339. evt.evt_type = BLE_SC_CTRLPT_EVT_UPDATE_LOCATION;
  340. evt.params.update_location = rcvd_ctrlpt.location;
  341. if (p_sc_ctrlpt->evt_handler != NULL)
  342. {
  343. rsp.status = p_sc_ctrlpt->evt_handler(p_sc_ctrlpt, &evt);
  344. }
  345. if (rsp.status == BLE_SCPT_SUCCESS)
  346. {
  347. err_code = sd_ble_gatts_value_set(p_sc_ctrlpt->conn_handle,
  348. p_sc_ctrlpt->sensor_location_handle,
  349. &gatts_value);
  350. if (err_code != NRF_SUCCESS)
  351. {
  352. // Report error to application
  353. if (p_sc_ctrlpt->error_handler != NULL)
  354. {
  355. p_sc_ctrlpt->error_handler(err_code);
  356. }
  357. rsp.status = BLE_SCPT_OPERATION_FAILED;
  358. }
  359. }
  360. }
  361. else
  362. {
  363. rsp.status = BLE_SCPT_INVALID_PARAMETER;
  364. }
  365. }
  366. else
  367. {
  368. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  369. }
  370. break;
  371. case BLE_SCPT_SET_CUMULATIVE_VALUE:
  372. if ((p_sc_ctrlpt->supported_functions &
  373. BLE_SRV_SC_CTRLPT_CUM_VAL_OP_SUPPORTED) ==
  374. BLE_SRV_SC_CTRLPT_CUM_VAL_OP_SUPPORTED)
  375. {
  376. rsp.status = BLE_SCPT_SUCCESS;
  377. evt.evt_type = BLE_SC_CTRLPT_EVT_SET_CUMUL_VALUE;
  378. evt.params.cumulative_value = rcvd_ctrlpt.cumulative_value;
  379. if (p_sc_ctrlpt->evt_handler != NULL)
  380. {
  381. rsp.status = p_sc_ctrlpt->evt_handler(p_sc_ctrlpt, &evt);
  382. }
  383. }
  384. else
  385. {
  386. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  387. }
  388. break;
  389. case BLE_SCPT_START_AUTOMATIC_CALIBRATION:
  390. if ((p_sc_ctrlpt->supported_functions &
  391. BLE_SRV_SC_CTRLPT_START_CALIB_OP_SUPPORTED) ==
  392. BLE_SRV_SC_CTRLPT_START_CALIB_OP_SUPPORTED)
  393. {
  394. p_sc_ctrlpt->procedure_status = BLE_SCPT_AUTOMATIC_CALIB_IN_PROGRESS;
  395. evt.evt_type = BLE_SC_CTRLPT_EVT_START_CALIBRATION;
  396. if (p_sc_ctrlpt->evt_handler != NULL)
  397. {
  398. rsp.status = p_sc_ctrlpt->evt_handler(p_sc_ctrlpt, &evt);
  399. if (rsp.status != BLE_SCPT_SUCCESS)
  400. {
  401. // If the application returns an error, the response is to be sent
  402. // right away and the calibration is considered as not started.
  403. p_sc_ctrlpt->procedure_status = BLE_SCPT_INDICATION_PENDING;
  404. }
  405. }
  406. }
  407. else
  408. {
  409. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  410. }
  411. break;
  412. default:
  413. rsp.status = BLE_SCPT_OP_CODE_NOT_SUPPORTED;
  414. break;
  415. }
  416. }
  417. p_sc_ctrlpt->response.len = ctrlpt_rsp_encode(p_sc_ctrlpt, &rsp,
  418. p_sc_ctrlpt->response.encoded_ctrl_rsp);
  419. if (p_sc_ctrlpt->procedure_status == BLE_SCPT_INDICATION_PENDING)
  420. {
  421. sc_ctrlpt_resp_send(p_sc_ctrlpt);
  422. }
  423. }
  424. /**@brief Authorize WRITE request event handler.
  425. *
  426. * @details Handles WRITE events from the BLE stack.
  427. *
  428. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  429. * @param[in] p_gatts_evt GATTS Event received from the BLE stack.
  430. *
  431. */
  432. static void on_rw_authorize_request(ble_sc_ctrlpt_t * p_sc_ctrlpt,
  433. ble_gatts_evt_t const * p_gatts_evt)
  434. {
  435. ble_gatts_evt_rw_authorize_request_t const * p_auth_req =
  436. &p_gatts_evt->params.authorize_request;
  437. if (p_auth_req->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
  438. {
  439. if ( (p_gatts_evt->params.authorize_request.request.write.op
  440. != BLE_GATTS_OP_PREP_WRITE_REQ)
  441. && (p_gatts_evt->params.authorize_request.request.write.op
  442. != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
  443. && (p_gatts_evt->params.authorize_request.request.write.op
  444. != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
  445. )
  446. {
  447. if (p_auth_req->request.write.handle == p_sc_ctrlpt->sc_ctrlpt_handles.value_handle)
  448. {
  449. on_ctrlpt_write(p_sc_ctrlpt, &p_auth_req->request.write);
  450. }
  451. }
  452. }
  453. }
  454. /**@brief Tx Complete event handler.
  455. *
  456. * @details Tx Complete event handler.
  457. * Handles WRITE events from the BLE stack and if an indication was pending try sending it
  458. * again.
  459. *
  460. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  461. *
  462. */
  463. static void on_tx_complete(ble_sc_ctrlpt_t * p_sc_ctrlpt)
  464. {
  465. if (p_sc_ctrlpt->procedure_status == BLE_SCPT_INDICATION_PENDING)
  466. {
  467. sc_ctrlpt_resp_send(p_sc_ctrlpt);
  468. }
  469. }
  470. /**@brief Function for handling the Connect event.
  471. *
  472. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  473. * @param[in] p_ble_evt Event received from the BLE stack.
  474. */
  475. static void on_connect(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt)
  476. {
  477. p_sc_ctrlpt->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  478. p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS;
  479. }
  480. /**@brief Function for handling the Disconnect event.
  481. *
  482. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  483. * @param[in] p_ble_evt Event received from the BLE stack.
  484. */
  485. static void on_disconnect(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt)
  486. {
  487. UNUSED_PARAMETER(p_ble_evt);
  488. p_sc_ctrlpt->conn_handle = BLE_CONN_HANDLE_INVALID;
  489. p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS;
  490. }
  491. /**@brief Function for handling the BLE_GATTS_EVT_HVC event.
  492. *
  493. * @param[in] p_sc_ctrlpt SC Ctrlpt structure.
  494. * @param[in] p_ble_evt Event received from the BLE stack.
  495. */
  496. static void on_sc_hvc_confirm(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt)
  497. {
  498. if (p_ble_evt->evt.gatts_evt.params.hvc.handle == p_sc_ctrlpt->sc_ctrlpt_handles.value_handle)
  499. {
  500. if (p_sc_ctrlpt->procedure_status == BLE_SCPT_IND_CONFIRM_PENDING)
  501. {
  502. p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS;
  503. }
  504. }
  505. }
  506. void ble_sc_ctrlpt_on_ble_evt(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_evt_t const * p_ble_evt)
  507. {
  508. if (p_sc_ctrlpt == NULL || p_ble_evt == NULL)
  509. {
  510. return;
  511. }
  512. switch (p_ble_evt->header.evt_id)
  513. {
  514. case BLE_GAP_EVT_CONNECTED:
  515. on_connect(p_sc_ctrlpt, p_ble_evt);
  516. break;
  517. case BLE_GAP_EVT_DISCONNECTED:
  518. on_disconnect(p_sc_ctrlpt, p_ble_evt);
  519. break;
  520. case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
  521. on_rw_authorize_request(p_sc_ctrlpt, &p_ble_evt->evt.gatts_evt);
  522. break;
  523. case BLE_GATTS_EVT_HVC:
  524. on_sc_hvc_confirm(p_sc_ctrlpt, p_ble_evt);
  525. break;
  526. case BLE_GATTS_EVT_HVN_TX_COMPLETE:
  527. on_tx_complete(p_sc_ctrlpt);
  528. break;
  529. default:
  530. break;
  531. }
  532. }
  533. uint32_t ble_sc_ctrlpt_rsp_send(ble_sc_ctrlpt_t * p_sc_ctrlpt, ble_scpt_response_t response_status)
  534. {
  535. if (p_sc_ctrlpt == NULL)
  536. {
  537. return NRF_ERROR_NULL;
  538. }
  539. uint32_t err_code = NRF_SUCCESS;
  540. ble_sc_ctrlpt_rsp_t rsp;
  541. uint8_t encoded_ctrl_rsp[BLE_SC_CTRLPT_MAX_LEN];
  542. uint16_t hvx_len;
  543. ble_gatts_hvx_params_t hvx_params;
  544. if (p_sc_ctrlpt->procedure_status != BLE_SCPT_AUTOMATIC_CALIB_IN_PROGRESS)
  545. {
  546. return NRF_ERROR_INVALID_STATE;
  547. }
  548. rsp.status = response_status;
  549. rsp.opcode = BLE_SCPT_START_AUTOMATIC_CALIBRATION;
  550. hvx_len = ctrlpt_rsp_encode(p_sc_ctrlpt, &rsp, encoded_ctrl_rsp);
  551. // Send indication
  552. memset(&hvx_params, 0, sizeof(hvx_params));
  553. hvx_params.handle = p_sc_ctrlpt->sc_ctrlpt_handles.value_handle;
  554. hvx_params.type = BLE_GATT_HVX_INDICATION;
  555. hvx_params.offset = 0;
  556. hvx_params.p_len = &hvx_len;
  557. hvx_params.p_data = encoded_ctrl_rsp;
  558. err_code = sd_ble_gatts_hvx(p_sc_ctrlpt->conn_handle, &hvx_params);
  559. if (err_code == NRF_SUCCESS)
  560. {
  561. p_sc_ctrlpt->procedure_status = BLE_SCPT_NO_PROC_IN_PROGRESS;
  562. }
  563. return err_code;
  564. }