123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include "es_gatts.h"
- #include "es_gatts_read.h"
- #include "es_gatts_write.h"
- #include "es_slot.h"
- static uint8_t m_active_slot;
- static bool is_beacon_unlocked(const nrf_ble_escs_t * p_escs)
- {
- return p_escs->lock_state != NRF_BLE_ESCS_LOCK_STATE_LOCKED;
- }
- ret_code_t es_gatts_send_reply(nrf_ble_escs_t * p_escs,
- ble_gatts_rw_authorize_reply_params_t * p_reply)
- {
- VERIFY_PARAM_NOT_NULL(p_escs);
- VERIFY_PARAM_NOT_NULL(p_reply);
- if (p_escs->conn_handle != BLE_CONN_HANDLE_INVALID)
- {
- return sd_ble_gatts_rw_authorize_reply(p_escs->conn_handle, p_reply);
- }
- return NRF_ERROR_INVALID_STATE;
- }
- ret_code_t es_gatts_send_op_not_permitted(nrf_ble_escs_t * p_escs, bool read)
- {
- ble_gatts_rw_authorize_reply_params_t reply = {0};
- VERIFY_PARAM_NOT_NULL(p_escs);
- if (read)
- {
- reply.type = BLE_GATTS_AUTHORIZE_TYPE_READ;
- reply.params.read.gatt_status = BLE_GATT_STATUS_ATTERR_READ_NOT_PERMITTED;
- }
- else
- {
- reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
- reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_WRITE_NOT_PERMITTED;
- }
- return es_gatts_send_reply(p_escs, &reply);
- }
- void es_gatts_handle_write(nrf_ble_escs_t * p_escs,
- uint16_t uuid,
- uint16_t val_handle,
- uint8_t const * p_data,
- uint16_t length)
- {
- ret_code_t err_code;
- if (is_beacon_unlocked(p_escs))
- {
- if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
- {
- err_code = es_gatts_send_op_not_permitted(p_escs, false);
- APP_ERROR_CHECK(err_code);
- }
- else
- {
- err_code = es_gatts_write_handle_unlocked_write(
- p_escs, uuid, val_handle, p_data, length, m_active_slot);
- APP_ERROR_CHECK(err_code);
- }
- }
- else
- {
- if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
- {
- err_code = es_gatts_write_handle_unlock(p_escs, p_data, length, val_handle);
- APP_ERROR_CHECK(err_code);
- }
- else
- {
- err_code = es_gatts_send_op_not_permitted(p_escs, false);
- APP_ERROR_CHECK(err_code);
- }
- }
- }
- void es_gatts_handle_read(nrf_ble_escs_t * p_escs, uint16_t uuid, uint16_t val_handle)
- {
- ret_code_t err_code;
- if (is_beacon_unlocked(p_escs))
- {
- if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
- {
- err_code = es_gatts_send_op_not_permitted(p_escs, true);
- APP_ERROR_CHECK(err_code);
- }
- else
- {
- err_code = es_gatts_read_handle_unlocked_read(p_escs, uuid, val_handle, m_active_slot);
- APP_ERROR_CHECK(err_code);
- }
- }
- else
- {
- if (uuid == BLE_UUID_ESCS_UNLOCK_CHAR)
- {
- err_code = es_gatts_read_handle_unlock(p_escs);
- APP_ERROR_CHECK(err_code);
- }
- else
- {
- err_code = es_gatts_read_handle_locked_read(p_escs, uuid);
- APP_ERROR_CHECK(err_code);
- }
- }
- }
- ret_code_t es_gatts_init(nrf_ble_escs_t * p_ble_escs)
- {
- VERIFY_PARAM_NOT_NULL(p_ble_escs);
- m_active_slot = 0;
-
- p_ble_escs->p_active_slot = &m_active_slot;
- p_ble_escs->lock_state = NRF_BLE_ESCS_LOCK_STATE_LOCKED;
- return NRF_SUCCESS;
- }
|