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";
37 this->
value_ = std::move(value);
41 this->
set_value(std::vector<uint8_t>(value.begin(), value.end()));
44 this->
set_value(std::vector<uint8_t>(data, data + length));
89 ESP_LOGW(TAG,
"notification=false is not yet supported");
100 ESP_LOGE(TAG,
"esp_ble_gatts_send_indicate failed %d", err);
115 esp_attr_control_t control;
116 control.auto_rsp = ESP_GATT_RSP_BY_APP;
118 ESP_LOGV(TAG,
"Creating characteristic - %s", this->
uuid_.
to_string().c_str());
121 esp_err_t err = esp_ble_gatts_add_char(service->
get_handle(), &uuid,
static_cast<esp_gatt_perm_t
>(this->
permissions_),
125 ESP_LOGE(TAG,
"esp_ble_gatts_add_char failed: %d", err);
141 created &= descriptor->is_created();
145 return this->state_ ==
CREATED;
149 if (this->state_ ==
FAILED)
154 failed |= descriptor->is_failed();
158 return this->state_ ==
FAILED;
205 esp_ble_gatts_cb_param_t *param) {
207 case ESP_GATTS_ADD_CHAR_EVT: {
209 this->
handle_ = param->add_char.attr_handle;
212 descriptor->do_create(
this);
219 case ESP_GATTS_READ_EVT: {
220 if (param->read.handle != this->handle_)
223 if (!param->read.need_rsp)
226 uint16_t max_offset = 22;
228 esp_gatt_rsp_t response;
229 if (param->read.is_long) {
234 memcpy(response.attr_value.value, this->value_.data() + response.attr_value.offset, response.attr_value.len);
237 response.attr_value.len = max_offset;
239 memcpy(response.attr_value.value, this->value_.data() + response.attr_value.offset, response.attr_value.len);
243 response.attr_value.offset = 0;
244 if (this->
value_.size() + 1 > max_offset) {
245 response.attr_value.len = max_offset;
248 response.attr_value.len = this->
value_.size();
250 memcpy(response.attr_value.value, this->value_.data(), response.attr_value.len);
253 response.attr_value.handle = this->
handle_;
254 response.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
257 esp_ble_gatts_send_response(gatts_if, param->read.conn_id, param->read.trans_id, ESP_GATT_OK, &response);
259 ESP_LOGE(TAG,
"esp_ble_gatts_send_response failed: %d", err);
263 case ESP_GATTS_WRITE_EVT: {
264 if (this->
handle_ != param->write.handle)
267 if (param->write.is_prep) {
268 this->
value_.insert(this->
value_.end(), param->write.value, param->write.value + param->write.len);
271 this->
set_value(param->write.value, param->write.len);
274 if (param->write.need_rsp) {
275 esp_gatt_rsp_t response;
277 response.attr_value.len = param->write.len;
278 response.attr_value.handle = this->
handle_;
279 response.attr_value.offset = param->write.offset;
280 response.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
281 memcpy(response.attr_value.value, param->write.value, param->write.len);
284 esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, &response);
287 ESP_LOGE(TAG,
"esp_ble_gatts_send_response failed: %d", err);
291 if (!param->write.is_prep) {
298 case ESP_GATTS_EXEC_WRITE_EVT: {
302 if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
306 esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK,
nullptr);
308 ESP_LOGE(TAG,
"esp_ble_gatts_send_response failed: %d", err);
317 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)
const std::unordered_map< uint16_t, void * > & get_clients()
void set_notify_property(bool value)
static const uint32_t PROPERTY_BROADCAST
void set_value(const uint8_t *data, size_t length)
void set_broadcast_property(bool value)
void remove_descriptor(BLEDescriptor *descriptor)
esp_gatt_if_t get_gatts_if()
std::vector< BLEDescriptor * > descriptors_
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
void set_write_property(bool value)
esp_gatt_char_prop_t properties_
void notify(bool notification=true)
std::vector< uint8_t > value_
uint16_t value_read_offset_
SemaphoreHandle_t set_value_lock_
void set_indicate_property(bool value)
BLECharacteristic(ESPBTUUID uuid, uint32_t properties)
esp_gatt_perm_t permissions_
uint32_t get_connected_client_count()
std::function< void(const std::vector< uint8_t > &)> on_write_
std::string to_string() const
void add_descriptor(BLEDescriptor *descriptor)
Implementation of SPI Controller mode.
static const uint32_t PROPERTY_WRITE_NR
static const uint32_t PROPERTY_READ
esp_bt_uuid_t get_uuid() const
static const uint32_t PROPERTY_NOTIFY