es_gatts.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) 2016 - 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 "es_gatts.h"
  41. #include "es_gatts_read.h"
  42. #include "es_gatts_write.h"
  43. #include "es_slot.h"
  44. static uint8_t m_active_slot;
  45. /**@brief Function checking if beacon is unlocked.
  46. *
  47. * @param[in] p_escs Pointer to Eddystone Configuration Service.
  48. *
  49. * @retval true If beacon is unlocked.
  50. * @retval false If beacon is locked.
  51. */
  52. static bool is_beacon_unlocked(const nrf_ble_escs_t * p_escs)
  53. {
  54. return p_escs->lock_state != NRF_BLE_ESCS_LOCK_STATE_LOCKED;
  55. }
  56. ret_code_t es_gatts_send_reply(nrf_ble_escs_t * p_escs,
  57. ble_gatts_rw_authorize_reply_params_t * p_reply)
  58. {
  59. VERIFY_PARAM_NOT_NULL(p_escs);
  60. VERIFY_PARAM_NOT_NULL(p_reply);
  61. if (p_escs->conn_handle != BLE_CONN_HANDLE_INVALID)
  62. {
  63. return sd_ble_gatts_rw_authorize_reply(p_escs->conn_handle, p_reply);
  64. }
  65. return NRF_ERROR_INVALID_STATE;
  66. }
  67. ret_code_t es_gatts_send_op_not_permitted(nrf_ble_escs_t * p_escs, bool read)
  68. {
  69. ble_gatts_rw_authorize_reply_params_t reply = {0};
  70. VERIFY_PARAM_NOT_NULL(p_escs);
  71. if (read)
  72. {
  73. reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
  74. reply.params.read.gatt_status = BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED;
  75. }
  76. else
  77. {
  78. reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  79. reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED;
  80. }
  81. return es_gatts_send_reply(p_escs, &reply);
  82. }
  83. void es_gatts_handle_write(nrf_ble_escs_t * p_escs,
  84. uint16_t uuid,
  85. uint16_t val_handle,
  86. uint8_t const * p_data,
  87. uint16_t length)
  88. {
  89. ret_code_t err_code;
  90. if (is_beacon_unlocked(p_escs))
  91. {
  92. if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
  93. {
  94. err_code = es_gatts_send_op_not_permitted(p_escs, false);
  95. APP_ERROR_CHECK(err_code);
  96. }
  97. else
  98. {
  99. err_code = es_gatts_write_handle_unlocked_write(
  100. p_escs, uuid, val_handle, p_data, length, m_active_slot);
  101. APP_ERROR_CHECK(err_code);
  102. }
  103. }
  104. else
  105. {
  106. if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
  107. {
  108. err_code = es_gatts_write_handle_unlock(p_escs, p_data, length, val_handle);
  109. APP_ERROR_CHECK(err_code);
  110. }
  111. else
  112. {
  113. err_code = es_gatts_send_op_not_permitted(p_escs, false);
  114. APP_ERROR_CHECK(err_code);
  115. }
  116. }
  117. }
  118. void es_gatts_handle_read(nrf_ble_escs_t * p_escs, uint16_t uuid, uint16_t val_handle)
  119. {
  120. ret_code_t err_code;
  121. if (is_beacon_unlocked(p_escs))
  122. {
  123. if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
  124. {
  125. err_code = es_gatts_send_op_not_permitted(p_escs, true);
  126. APP_ERROR_CHECK(err_code);
  127. }
  128. else
  129. {
  130. err_code = es_gatts_read_handle_unlocked_read(p_escs, uuid, val_handle, m_active_slot);
  131. APP_ERROR_CHECK(err_code);
  132. }
  133. }
  134. else // Beacon is locked.
  135. {
  136. if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
  137. {
  138. err_code = es_gatts_read_handle_unlock(p_escs);
  139. APP_ERROR_CHECK(err_code);
  140. }
  141. else
  142. {
  143. err_code = es_gatts_read_handle_locked_read(p_escs, uuid);
  144. APP_ERROR_CHECK(err_code);
  145. }
  146. }
  147. }
  148. ret_code_t es_gatts_init(nrf_ble_escs_t * p_ble_escs)
  149. {
  150. VERIFY_PARAM_NOT_NULL(p_ble_escs);
  151. m_active_slot = 0;
  152. p_ble_escs->p_active_slot = &m_active_slot;
  153. p_ble_escs->lock_state = NRF_BLE_ESCS_LOCK_STATE_LOCKED;
  154. return NRF_SUCCESS;
  155. }