1 #include "ble_characteristic.h" 3 #include "ble_service.h" 10 namespace esp32_ble_server {
12 static const char *
const TAG =
"esp32_ble_server.characteristic";
43 this->
set_value(std::vector<uint8_t>(buffer.begin(), buffer.end()));
60 ESP_LOGW(TAG,
"INDICATE acknowledgment is not yet supported (i.e. it works as a NOTIFY)");
66 ESP_LOGE(TAG,
"esp_ble_gatts_send_indicate failed %d", err);
76 if (value.size() != 2)
79 bool notify = (cccd & 1) != 0;
80 bool indicate = (cccd & 2) != 0;
81 if (notify || indicate) {
98 esp_attr_control_t control;
99 control.auto_rsp = ESP_GATT_RSP_BY_APP;
101 ESP_LOGV(TAG,
"Creating characteristic - %s", this->
uuid_.
to_string().c_str());
104 esp_err_t err = esp_ble_gatts_add_char(service->
get_handle(), &uuid,
static_cast<esp_gatt_perm_t
>(this->
permissions_),
108 ESP_LOGE(TAG,
"esp_ble_gatts_add_char failed: %d", err);
124 created &= descriptor->is_created();
128 return this->state_ ==
CREATED;
132 if (this->state_ ==
FAILED)
137 failed |= descriptor->is_failed();
141 return this->state_ ==
FAILED;
188 esp_ble_gatts_cb_param_t *param) {
190 case ESP_GATTS_ADD_CHAR_EVT: {
192 this->
handle_ = param->add_char.attr_handle;
195 descriptor->do_create(
this);
202 case ESP_GATTS_READ_EVT: {
203 if (param->read.handle != this->handle_)
206 if (!param->read.need_rsp)
210 param->read.conn_id);
212 uint16_t max_offset = 22;
214 esp_gatt_rsp_t response;
215 if (param->read.is_long) {
220 memcpy(response.attr_value.value, this->value_.data() + response.attr_value.offset, response.attr_value.len);
223 response.attr_value.len = max_offset;
225 memcpy(response.attr_value.value, this->value_.data() + response.attr_value.offset, response.attr_value.len);
229 response.attr_value.offset = 0;
230 if (this->
value_.size() + 1 > max_offset) {
231 response.attr_value.len = max_offset;
234 response.attr_value.len = this->
value_.size();
236 memcpy(response.attr_value.value, this->value_.data(), response.attr_value.len);
239 response.attr_value.handle = this->
handle_;
240 response.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
243 esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &response);
245 ESP_LOGE(TAG,
"esp_ble_gatts_send_response failed: %d", err);
249 case ESP_GATTS_WRITE_EVT: {
250 if (this->
handle_ != param->write.handle)
253 if (param->write.is_prep) {
254 this->
value_.insert(this->
value_.end(), param->write.value, param->write.value + param->write.len);
260 if (param->write.need_rsp) {
261 esp_gatt_rsp_t response;
263 response.attr_value.len = param->write.len;
264 response.attr_value.handle = this->
handle_;
265 response.attr_value.offset = param->write.offset;
266 response.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
267 memcpy(response.attr_value.value, param->write.value, param->write.len);
270 esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, &response);
273 ESP_LOGE(TAG,
"esp_ble_gatts_send_response failed: %d", err);
277 if (!param->write.is_prep) {
285 case ESP_GATTS_EXEC_WRITE_EVT: {
289 if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
294 esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK,
nullptr);
296 ESP_LOGE(TAG,
"esp_ble_gatts_send_response failed: %d", err);
305 descriptor->gatts_event_handler(event, gatts_if, param);
void do_create(BLEService *service)
static const uint32_t PROPERTY_WRITE
void set_write_no_response_property(bool value)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
static const uint32_t PROPERTY_INDICATE
void set_read_property(bool value)
void set_notify_property(bool value)
const std::unordered_set< uint16_t > & get_clients()
EventEmitterListenerID on(EvtType event, std::function< void(Args...)> listener)
static const uint32_t PROPERTY_BROADCAST
void set_value(ByteBuffer buffer)
void set_broadcast_property(bool value)
std::vector< uint8_t > get_data()
void remove_descriptor(BLEDescriptor *descriptor)
esp_gatt_if_t get_gatts_if()
ESPBTUUID get_uuid() const
std::vector< BLEDescriptor * > descriptors_
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
A class modelled on the Java ByteBuffer class.
void set_write_property(bool value)
esp_gatt_char_prop_t properties_
std::vector< uint8_t > value_
uint16_t value_read_offset_
SemaphoreHandle_t set_value_lock_
static ESPBTUUID from_uint16(uint16_t uuid)
void set_indicate_property(bool value)
BLECharacteristic(ESPBTUUID uuid, uint32_t properties)
esp_gatt_perm_t permissions_
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
uint32_t get_connected_client_count()
std::string to_string() const
std::unordered_map< uint16_t, bool > clients_to_notify_
void add_descriptor(BLEDescriptor *descriptor)
void emit_(EvtType event, Args... args)
Implementation of SPI Controller mode.
static const uint32_t PROPERTY_WRITE_NR
static const uint32_t PROPERTY_READ
static ByteBuffer wrap(T value, Endian endianness=LITTLE)
esp_bt_uuid_t get_uuid() const
static const uint32_t PROPERTY_NOTIFY