ble_ias_c.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(BLE_IAS_C)
  42. #include "ble_ias_c.h"
  43. #include <string.h>
  44. #include "ble.h"
  45. #include "ble_srv_common.h"
  46. #include "ble_gattc.h"
  47. #include "ble_db_discovery.h"
  48. /**@brief Function for intercepting the errors of GATTC and the BLE GATT Queue.
  49. *
  50. * @param[in] nrf_error Error code.
  51. * @param[in] p_ctx Parameter from the event handler.
  52. * @param[in] conn_handle Connection handle.
  53. */
  54. static void gatt_error_handler(uint32_t nrf_error,
  55. void * p_ctx,
  56. uint16_t conn_handle)
  57. {
  58. UNUSED_PARAMETER(conn_handle);
  59. ble_ias_c_t * p_ias_c = (ble_ias_c_t *)p_ctx;
  60. if (p_ias_c->error_handler != NULL)
  61. {
  62. p_ias_c->error_handler(nrf_error);
  63. }
  64. }
  65. void ble_ias_c_on_db_disc_evt(ble_ias_c_t * p_ias_c, ble_db_discovery_evt_t const * p_evt)
  66. {
  67. ble_ias_c_evt_t evt;
  68. memset(&evt, 0, sizeof(ble_ias_c_evt_t));
  69. evt.conn_handle = p_evt->conn_handle;
  70. ble_gatt_db_char_t const * p_chars = p_evt->params.discovered_db.charateristics;
  71. // Check if the Immediate Alert Service was discovered.
  72. if ( (p_evt->evt_type == BLE_DB_DISCOVERY_COMPLETE)
  73. && (p_evt->params.discovered_db.srv_uuid.uuid == BLE_UUID_IMMEDIATE_ALERT_SERVICE)
  74. && (p_evt->params.discovered_db.srv_uuid.type == BLE_UUID_TYPE_BLE))
  75. {
  76. for (uint32_t i = 0; i < p_evt->params.discovered_db.char_count; i++)
  77. {
  78. // The Alert Level characteristic in the Immediate Alert Service instance is found
  79. // on peer. Check if it has the correct property 'Write without response'.
  80. switch (p_chars[i].characteristic.uuid.uuid)
  81. {
  82. case BLE_UUID_ALERT_LEVEL_CHAR:
  83. if (p_chars[i].characteristic.char_props.write_wo_resp)
  84. {
  85. // Found Alert Level characteristic inside the Immediate Alert Service.
  86. memcpy(&evt.alert_level,
  87. &p_chars[i].characteristic,
  88. sizeof(ble_gattc_char_t));
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. }
  96. else if ((p_evt->evt_type == BLE_DB_DISCOVERY_SRV_NOT_FOUND) ||
  97. (p_evt->evt_type == BLE_DB_DISCOVERY_ERROR))
  98. {
  99. evt.evt_type = BLE_IAS_C_EVT_DISCOVERY_FAILED;
  100. }
  101. else
  102. {
  103. return;
  104. }
  105. if (evt.alert_level.handle_value != BLE_GATT_HANDLE_INVALID)
  106. {
  107. evt.evt_type = BLE_IAS_C_EVT_DISCOVERY_COMPLETE;
  108. }
  109. p_ias_c->evt_handler(p_ias_c, &evt);
  110. }
  111. uint32_t ble_ias_c_init(ble_ias_c_t * p_ias_c, ble_ias_c_init_t const * p_ias_c_init)
  112. {
  113. VERIFY_PARAM_NOT_NULL(p_ias_c);
  114. VERIFY_PARAM_NOT_NULL(p_ias_c_init);
  115. VERIFY_PARAM_NOT_NULL(p_ias_c_init->evt_handler);
  116. VERIFY_PARAM_NOT_NULL(p_ias_c_init->p_gatt_queue);
  117. p_ias_c->evt_handler = p_ias_c_init->evt_handler;
  118. p_ias_c->error_handler = p_ias_c_init->error_handler;
  119. p_ias_c->p_gatt_queue = p_ias_c_init->p_gatt_queue;
  120. p_ias_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  121. p_ias_c->alert_level_char.handle_value = BLE_GATT_HANDLE_INVALID;
  122. BLE_UUID_BLE_ASSIGN(p_ias_c->alert_level_char.uuid, BLE_UUID_ALERT_LEVEL_CHAR);
  123. BLE_UUID_BLE_ASSIGN(p_ias_c->service_uuid, BLE_UUID_IMMEDIATE_ALERT_SERVICE);
  124. return ble_db_discovery_evt_register(&p_ias_c->service_uuid);
  125. }
  126. /**@brief Function for handling the Disconnect event.
  127. *
  128. * @param[in] p_ias_c Immediate Alert Service client structure.
  129. * @param[in] p_ble_evt Event received from the BLE stack.
  130. */
  131. static void on_disconnect(ble_ias_c_t * p_ias_c, ble_evt_t const * p_ble_evt)
  132. {
  133. // The following values will be re-initialized when a new connection is made.
  134. p_ias_c->conn_handle = BLE_CONN_HANDLE_INVALID;
  135. if (ble_ias_c_is_discovered(p_ias_c))
  136. {
  137. // There was a valid instance of IAS on the peer. Send an event to the
  138. // application, so that it can do a cleanup related to this module.
  139. ble_ias_c_evt_t evt;
  140. evt.evt_type = BLE_IAS_C_EVT_DISCONN_COMPLETE;
  141. p_ias_c->evt_handler(p_ias_c, &evt);
  142. p_ias_c->alert_level_char.handle_value = BLE_GATT_HANDLE_INVALID;
  143. }
  144. }
  145. void ble_ias_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  146. {
  147. uint32_t err_code = NRF_SUCCESS;
  148. ble_ias_c_t * p_ias_c = (ble_ias_c_t *)p_context;
  149. switch (p_ble_evt->header.evt_id)
  150. {
  151. case BLE_GAP_EVT_DISCONNECTED:
  152. on_disconnect(p_ias_c, p_ble_evt);
  153. break;
  154. default:
  155. // No implementation needed.
  156. break;
  157. }
  158. if ((err_code != NRF_SUCCESS) && (p_ias_c->error_handler != NULL))
  159. {
  160. p_ias_c->error_handler(err_code);
  161. }
  162. }
  163. /**@brief Function for performing a Write procedure.
  164. *
  165. * @param[in] p_ias_c Pointer to Immediate Alert Service client structure.
  166. * @param[in] length Length of data to be written.
  167. * @param[in] p_value Data to be written.
  168. *
  169. * @return NRF_SUCCESS on success, otherwise an error code.
  170. */
  171. static uint32_t write_characteristic_value(ble_ias_c_t const * const p_ias_c,
  172. uint16_t length,
  173. uint8_t * p_value)
  174. {
  175. nrf_ble_gq_req_t write_req;
  176. memset(&write_req, 0, sizeof(nrf_ble_gq_req_t));
  177. write_req.type = NRF_BLE_GQ_REQ_GATTC_WRITE;
  178. write_req.error_handler.cb = gatt_error_handler;
  179. write_req.error_handler.p_ctx = (ble_ias_c_t *)p_ias_c;
  180. write_req.params.gattc_write.handle = p_ias_c->alert_level_char.handle_value;
  181. write_req.params.gattc_write.write_op = BLE_GATT_OP_WRITE_CMD;
  182. write_req.params.gattc_write.offset = 0;
  183. write_req.params.gattc_write.len = length;
  184. write_req.params.gattc_write.p_value = p_value;
  185. return nrf_ble_gq_item_add(p_ias_c->p_gatt_queue, &write_req, p_ias_c->conn_handle);
  186. }
  187. uint32_t ble_ias_c_send_alert_level(ble_ias_c_t const * p_ias_c, uint8_t alert_level)
  188. {
  189. if (!ble_ias_c_is_discovered(p_ias_c))
  190. {
  191. return NRF_ERROR_NOT_FOUND;
  192. }
  193. return write_characteristic_value(p_ias_c,
  194. sizeof(uint8_t),
  195. &alert_level);
  196. }
  197. uint32_t ble_ias_c_handles_assign(ble_ias_c_t * p_ias_c,
  198. const uint16_t conn_handle,
  199. const uint16_t alert_level_handle)
  200. {
  201. VERIFY_PARAM_NOT_NULL(p_ias_c);
  202. p_ias_c->conn_handle = conn_handle;
  203. p_ias_c->alert_level_char.handle_value = alert_level_handle;
  204. return nrf_ble_gq_conn_handle_register(p_ias_c->p_gatt_queue, conn_handle);
  205. }
  206. #endif //NRF_MODULE_ENABLED(BLE_IAS_C)