Remove uuid lookups from BLE read/write/notify characteristics (#4102)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
J. Nick Koston 2022-11-28 18:30:19 -10:00 committed by GitHub
parent 120327866f
commit ccef7c322f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -160,18 +160,11 @@ esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) {
this->address_str_.c_str());
return ESP_GATT_NOT_CONNECTED;
}
auto *characteristic = this->get_characteristic(handle);
if (characteristic == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT characteristic, not found.", this->connection_index_,
this->address_str_.c_str());
return ESP_GATT_INVALID_HANDLE;
}
ESP_LOGV(TAG, "[%d] [%s] Reading GATT characteristic %s", this->connection_index_, this->address_str_.c_str(),
characteristic->uuid.to_string().c_str());
ESP_LOGV(TAG, "[%d] [%s] Reading GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
handle);
esp_err_t err =
esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, characteristic->handle, ESP_GATT_AUTH_REQ_NONE);
esp_err_t err = esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
if (err != ERR_OK) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char error, err=%d", this->connection_index_,
this->address_str_.c_str(), err);
@ -186,18 +179,12 @@ esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::
this->address_str_.c_str());
return ESP_GATT_NOT_CONNECTED;
}
auto *characteristic = this->get_characteristic(handle);
if (characteristic == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT characteristic, not found.", this->connection_index_,
this->address_str_.c_str());
return ESP_GATT_INVALID_HANDLE;
}
ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
handle);
ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic %s", this->connection_index_, this->address_str_.c_str(),
characteristic->uuid.to_string().c_str());
auto err = characteristic->write_value((uint8_t *) data.data(), data.size(),
response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP);
esp_err_t err =
esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
if (err != ERR_OK) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->connection_index_,
this->address_str_.c_str(), err);
@ -212,18 +199,10 @@ esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) {
this->address_str_.c_str());
return ESP_GATT_NOT_CONNECTED;
}
auto *descriptor = this->get_descriptor(handle);
if (descriptor == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT descriptor, not found.", this->connection_index_,
this->address_str_.c_str());
return ESP_GATT_INVALID_HANDLE;
}
ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
handle);
ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor %s", this->connection_index_, this->address_str_.c_str(),
descriptor->uuid.to_string().c_str());
esp_err_t err =
esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, descriptor->handle, ESP_GATT_AUTH_REQ_NONE);
esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
if (err != ERR_OK) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->connection_index_,
this->address_str_.c_str(), err);
@ -238,15 +217,8 @@ esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::stri
this->address_str_.c_str());
return ESP_GATT_NOT_CONNECTED;
}
auto *descriptor = this->get_descriptor(handle);
if (descriptor == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT descriptor, not found.", this->connection_index_,
this->address_str_.c_str());
return ESP_GATT_INVALID_HANDLE;
}
ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor %s", this->connection_index_, this->address_str_.c_str(),
descriptor->uuid.to_string().c_str());
ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
handle);
esp_err_t err = esp_ble_gattc_write_char_descr(
this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
@ -265,26 +237,20 @@ esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enabl
this->address_str_.c_str());
return ESP_GATT_NOT_CONNECTED;
}
auto *characteristic = this->get_characteristic(handle);
if (characteristic == nullptr) {
ESP_LOGW(TAG, "[%d] [%s] Cannot notify GATT characteristic, not found.", this->connection_index_,
this->address_str_.c_str());
return ESP_GATT_INVALID_HANDLE;
}
if (enable) {
ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications %s", this->connection_index_,
this->address_str_.c_str(), characteristic->uuid.to_string().c_str());
esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, characteristic->handle);
ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications handle %d", this->connection_index_,
this->address_str_.c_str(), handle);
esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle);
if (err != ESP_OK) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->connection_index_,
this->address_str_.c_str(), err);
return err;
}
} else {
ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications %s", this->connection_index_,
this->address_str_.c_str(), characteristic->uuid.to_string().c_str());
esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, characteristic->handle);
ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
this->address_str_.c_str(), handle);
esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
if (err != ESP_OK) {
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_unregister_for_notify failed, err=%d", this->connection_index_,
this->address_str_.c_str(), err);