ble_ias.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_IAS)
  46. #include "ble_ias.h"
  47. #include <string.h>
  48. #include "ble_srv_common.h"
  49. #define NRF_LOG_MODULE_NAME ble_ias
  50. #if BLE_IAS_CONFIG_LOG_ENABLED
  51. #define NRF_LOG_LEVEL BLE_IAS_CONFIG_LOG_LEVEL
  52. #define NRF_LOG_INFO_COLOR BLE_IAS_CONFIG_INFO_COLOR
  53. #define NRF_LOG_DEBUG_COLOR BLE_IAS_CONFIG_DEBUG_COLOR
  54. #else // BLE_IAS_CONFIG_LOG_ENABLED
  55. #define NRF_LOG_LEVEL 0
  56. #endif // BLE_IAS_CONFIG_LOG_ENABLED
  57. #include "nrf_log.h"
  58. NRF_LOG_MODULE_REGISTER();
  59. #define INITIAL_ALERT_LEVEL BLE_CHAR_ALERT_LEVEL_NO_ALERT
  60. /**@brief Function for handling the Connect event.
  61. *
  62. * @param[in] p_ias Immediate Alert Service structure.
  63. * @param[in] p_ble_evt Event received from the BLE stack.
  64. */
  65. static void on_connect(ble_ias_t * p_ias, ble_evt_t const * p_ble_evt)
  66. {
  67. ret_code_t err_code;
  68. ble_ias_client_context_t * p_client = NULL;
  69. err_code = blcm_link_ctx_get(p_ias->p_link_ctx_storage,
  70. p_ble_evt->evt.gap_evt.conn_handle,
  71. (void *) &p_client);
  72. if (err_code != NRF_SUCCESS)
  73. {
  74. NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
  75. p_ble_evt->evt.gap_evt.conn_handle);
  76. }
  77. else
  78. {
  79. p_client->alert_level = INITIAL_ALERT_LEVEL;
  80. }
  81. }
  82. /**@brief Function for handling the Write event.
  83. *
  84. * @param[in] p_ias Immediate Alert Service structure.
  85. * @param[in] p_ble_evt Event received from the BLE stack.
  86. */
  87. static void on_write(ble_ias_t * p_ias, ble_evt_t const * p_ble_evt)
  88. {
  89. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  90. if ((p_evt_write->handle == p_ias->alert_level_handles.value_handle) && (p_evt_write->len == 1))
  91. {
  92. // Alert level written, call application event handler
  93. ret_code_t err_code;
  94. ble_ias_evt_t evt;
  95. ble_ias_client_context_t * p_client;
  96. err_code = blcm_link_ctx_get(p_ias->p_link_ctx_storage,
  97. p_ble_evt->evt.gatts_evt.conn_handle,
  98. (void *) &p_client);
  99. if (err_code != NRF_SUCCESS)
  100. {
  101. NRF_LOG_ERROR("Link context for 0x%02X connection handle could not be fetched.",
  102. p_ble_evt->evt.gatts_evt.conn_handle);
  103. }
  104. else
  105. {
  106. p_client->alert_level = p_evt_write->data[0];
  107. }
  108. evt.evt_type = BLE_IAS_EVT_ALERT_LEVEL_UPDATED;
  109. evt.conn_handle = p_ble_evt->evt.gatts_evt.conn_handle;
  110. evt.p_link_ctx = p_client;
  111. p_ias->evt_handler(p_ias, &evt);
  112. }
  113. }
  114. void ble_ias_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  115. {
  116. ble_ias_t * p_ias = (ble_ias_t *)p_context;
  117. switch (p_ble_evt->header.evt_id)
  118. {
  119. case BLE_GAP_EVT_CONNECTED:
  120. on_connect(p_ias, p_ble_evt);
  121. break;
  122. case BLE_GATTS_EVT_WRITE:
  123. on_write(p_ias, p_ble_evt);
  124. break;
  125. default:
  126. // No implementation needed.
  127. break;
  128. }
  129. }
  130. uint32_t ble_ias_init(ble_ias_t * p_ias, const ble_ias_init_t * p_ias_init)
  131. {
  132. uint32_t err_code;
  133. uint8_t initial_alert_level;
  134. ble_uuid_t ble_uuid;
  135. ble_add_char_params_t add_char_params;
  136. // Initialize service structure
  137. if (p_ias_init->evt_handler == NULL)
  138. {
  139. return NRF_ERROR_INVALID_PARAM;
  140. }
  141. p_ias->evt_handler = p_ias_init->evt_handler;
  142. // Add service
  143. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_IMMEDIATE_ALERT_SERVICE);
  144. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  145. &ble_uuid,
  146. &p_ias->service_handle);
  147. VERIFY_SUCCESS(err_code);
  148. // Add alert level characteristic
  149. initial_alert_level = INITIAL_ALERT_LEVEL;
  150. memset(&add_char_params, 0, sizeof(add_char_params));
  151. add_char_params.uuid = BLE_UUID_ALERT_LEVEL_CHAR;
  152. add_char_params.max_len = sizeof (uint8_t);
  153. add_char_params.init_len = sizeof (uint8_t);
  154. add_char_params.p_init_value = &initial_alert_level;
  155. add_char_params.char_props.write_wo_resp = 1;
  156. add_char_params.write_access = p_ias_init->alert_wr_sec;
  157. return characteristic_add(p_ias->service_handle,
  158. &add_char_params,
  159. &p_ias->alert_level_handles);
  160. }
  161. uint32_t ble_ias_alert_level_get(ble_ias_t * p_ias, uint16_t conn_handle, uint8_t * p_alert_level)
  162. {
  163. ret_code_t err_code;
  164. ble_ias_client_context_t * p_client;
  165. err_code = blcm_link_ctx_get(p_ias->p_link_ctx_storage, conn_handle, (void *) &p_client);
  166. VERIFY_SUCCESS(err_code);
  167. *p_alert_level = p_client->alert_level;
  168. return NRF_SUCCESS;
  169. }
  170. #endif // NRF_MODULE_ENABLED(BLE_IAS)