mirror of
https://github.com/esphome/esphome.git
synced 2025-10-04 02:52:22 +01:00
[esp32_ble_server] Create custom services, characteristics and descriptors (#7009)
Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com>
This commit is contained in:
@@ -12,11 +12,19 @@ namespace esp32_ble_server {
|
||||
|
||||
static const char *const TAG = "esp32_ble_server.descriptor";
|
||||
|
||||
BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len) {
|
||||
static RAMAllocator<uint8_t> descriptor_allocator{}; // NOLINT
|
||||
|
||||
BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len, bool read, bool write) {
|
||||
this->uuid_ = uuid;
|
||||
this->value_.attr_len = 0;
|
||||
this->value_.attr_max_len = max_len;
|
||||
this->value_.attr_value = (uint8_t *) malloc(max_len); // NOLINT
|
||||
this->value_.attr_value = descriptor_allocator.allocate(max_len);
|
||||
if (read) {
|
||||
this->permissions_ |= ESP_GATT_PERM_READ;
|
||||
}
|
||||
if (write) {
|
||||
this->permissions_ |= ESP_GATT_PERM_WRITE;
|
||||
}
|
||||
}
|
||||
|
||||
BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
|
||||
@@ -38,14 +46,15 @@ void BLEDescriptor::do_create(BLECharacteristic *characteristic) {
|
||||
this->state_ = CREATING;
|
||||
}
|
||||
|
||||
void BLEDescriptor::set_value(const std::string &value) { this->set_value((uint8_t *) value.data(), value.length()); }
|
||||
void BLEDescriptor::set_value(const uint8_t *data, size_t length) {
|
||||
void BLEDescriptor::set_value(std::vector<uint8_t> buffer) {
|
||||
size_t length = buffer.size();
|
||||
|
||||
if (length > this->value_.attr_max_len) {
|
||||
ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
|
||||
return;
|
||||
}
|
||||
this->value_.attr_len = length;
|
||||
memcpy(this->value_.attr_value, data, length);
|
||||
memcpy(this->value_.attr_value, buffer.data(), length);
|
||||
}
|
||||
|
||||
void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
|
||||
@@ -61,10 +70,13 @@ void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
|
||||
break;
|
||||
}
|
||||
case ESP_GATTS_WRITE_EVT: {
|
||||
if (this->handle_ == param->write.handle) {
|
||||
this->value_.attr_len = param->write.len;
|
||||
memcpy(this->value_.attr_value, param->write.value, param->write.len);
|
||||
}
|
||||
if (this->handle_ != param->write.handle)
|
||||
break;
|
||||
this->value_.attr_len = param->write.len;
|
||||
memcpy(this->value_.attr_value, param->write.value, param->write.len);
|
||||
this->emit_(BLEDescriptorEvt::VectorEvt::ON_WRITE,
|
||||
std::vector<uint8_t>(param->write.value, param->write.value + param->write.len),
|
||||
param->write.conn_id);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
Reference in New Issue
Block a user