mirror of
https://github.com/esphome/esphome.git
synced 2025-07-30 15:16:37 +00:00
cleanup
This commit is contained in:
parent
b6e0188c42
commit
8a03e4c2cb
@ -113,72 +113,79 @@ void BluetoothConnection::send_service_for_discovery_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now process characteristics
|
// Now process characteristics
|
||||||
uint16_t char_offset = 0;
|
if (char_count_status == ESP_GATT_OK && total_char_count > 0) {
|
||||||
esp_gattc_char_elem_t char_result;
|
uint16_t char_offset = 0;
|
||||||
while (true) { // characteristics
|
esp_gattc_char_elem_t char_result;
|
||||||
uint16_t char_count = 1;
|
while (true) { // characteristics
|
||||||
esp_gatt_status_t char_status =
|
uint16_t char_count = 1;
|
||||||
esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, service_result.start_handle,
|
esp_gatt_status_t char_status =
|
||||||
service_result.end_handle, &char_result, &char_count, char_offset);
|
esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, service_result.start_handle,
|
||||||
if (char_status == ESP_GATT_INVALID_OFFSET || char_status == ESP_GATT_NOT_FOUND) {
|
service_result.end_handle, &char_result, &char_count, char_offset);
|
||||||
break;
|
if (char_status == ESP_GATT_INVALID_OFFSET || char_status == ESP_GATT_NOT_FOUND) {
|
||||||
}
|
|
||||||
if (char_status != ESP_GATT_OK) {
|
|
||||||
ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_,
|
|
||||||
this->address_str().c_str(), char_status);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (char_count == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
service_resp.characteristics.emplace_back();
|
|
||||||
auto &characteristic_resp = service_resp.characteristics.back();
|
|
||||||
fill_128bit_uuid_array(characteristic_resp.uuid, char_result.uuid);
|
|
||||||
characteristic_resp.handle = char_result.char_handle;
|
|
||||||
characteristic_resp.properties = char_result.properties;
|
|
||||||
char_offset++;
|
|
||||||
|
|
||||||
// Get the number of descriptors directly with one call
|
|
||||||
uint16_t total_desc_count = 0;
|
|
||||||
esp_gatt_status_t desc_count_status =
|
|
||||||
esp_ble_gattc_get_attr_count(this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, char_result.char_handle,
|
|
||||||
service_result.end_handle, 0, &total_desc_count);
|
|
||||||
|
|
||||||
if (desc_count_status == ESP_GATT_OK && total_desc_count > 0) {
|
|
||||||
// Only reserve if we successfully got a count
|
|
||||||
characteristic_resp.descriptors.reserve(total_desc_count);
|
|
||||||
} else if (desc_count_status != ESP_GATT_OK) {
|
|
||||||
ESP_LOGW(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d", this->connection_index_,
|
|
||||||
this->address_str().c_str(), char_result.char_handle, desc_count_status);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now process descriptors
|
|
||||||
uint16_t desc_offset = 0;
|
|
||||||
esp_gattc_descr_elem_t desc_result;
|
|
||||||
while (true) { // descriptors
|
|
||||||
uint16_t desc_count = 1;
|
|
||||||
esp_gatt_status_t desc_status = esp_ble_gattc_get_all_descr(
|
|
||||||
this->gattc_if_, this->conn_id_, char_result.char_handle, &desc_result, &desc_count, desc_offset);
|
|
||||||
if (desc_status == ESP_GATT_INVALID_OFFSET || desc_status == ESP_GATT_NOT_FOUND) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (desc_status != ESP_GATT_OK) {
|
if (char_status != ESP_GATT_OK) {
|
||||||
ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_,
|
ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_,
|
||||||
this->address_str().c_str(), desc_status);
|
this->address_str().c_str(), char_status);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (desc_count == 0) {
|
if (char_count == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
characteristic_resp.descriptors.emplace_back();
|
service_resp.characteristics.emplace_back();
|
||||||
auto &descriptor_resp = characteristic_resp.descriptors.back();
|
auto &characteristic_resp = service_resp.characteristics.back();
|
||||||
fill_128bit_uuid_array(descriptor_resp.uuid, desc_result.uuid);
|
fill_128bit_uuid_array(characteristic_resp.uuid, char_result.uuid);
|
||||||
descriptor_resp.handle = desc_result.handle;
|
characteristic_resp.handle = char_result.char_handle;
|
||||||
desc_offset++;
|
characteristic_resp.properties = char_result.properties;
|
||||||
|
char_offset++;
|
||||||
|
|
||||||
|
// Get the number of descriptors directly with one call
|
||||||
|
uint16_t total_desc_count = 0;
|
||||||
|
esp_gatt_status_t desc_count_status =
|
||||||
|
esp_ble_gattc_get_attr_count(this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, char_result.char_handle,
|
||||||
|
service_result.end_handle, 0, &total_desc_count);
|
||||||
|
|
||||||
|
if (desc_count_status == ESP_GATT_OK && total_desc_count > 0) {
|
||||||
|
// Only reserve if we successfully got a count
|
||||||
|
characteristic_resp.descriptors.reserve(total_desc_count);
|
||||||
|
} else if (desc_count_status != ESP_GATT_OK) {
|
||||||
|
ESP_LOGW(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d", this->connection_index_,
|
||||||
|
this->address_str().c_str(), char_result.char_handle, desc_count_status);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip descriptor processing if there are no descriptors
|
||||||
|
if (desc_count_status != ESP_GATT_OK || total_desc_count == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now process descriptors
|
||||||
|
uint16_t desc_offset = 0;
|
||||||
|
esp_gattc_descr_elem_t desc_result;
|
||||||
|
while (true) { // descriptors
|
||||||
|
uint16_t desc_count = 1;
|
||||||
|
esp_gatt_status_t desc_status = esp_ble_gattc_get_all_descr(
|
||||||
|
this->gattc_if_, this->conn_id_, char_result.char_handle, &desc_result, &desc_count, desc_offset);
|
||||||
|
if (desc_status == ESP_GATT_INVALID_OFFSET || desc_status == ESP_GATT_NOT_FOUND) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (desc_status != ESP_GATT_OK) {
|
||||||
|
ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_,
|
||||||
|
this->address_str().c_str(), desc_status);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (desc_count == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
characteristic_resp.descriptors.emplace_back();
|
||||||
|
auto &descriptor_resp = characteristic_resp.descriptors.back();
|
||||||
|
fill_128bit_uuid_array(descriptor_resp.uuid, desc_result.uuid);
|
||||||
|
descriptor_resp.handle = desc_result.handle;
|
||||||
|
desc_offset++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
} // end if (char_count_status == ESP_GATT_OK && total_char_count > 0)
|
||||||
|
|
||||||
// Send the message (we already checked api_conn is not null at the beginning)
|
// Send the message (we already checked api_conn is not null at the beginning)
|
||||||
api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
|
api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user