mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 05:54:56 +01:00
Reduce memory needed to send the services list (#4110)
This commit is contained in:
parent
ad0d6f6337
commit
d5ff8f6117
1 changed files with 45 additions and 17 deletions
|
@ -86,31 +86,59 @@ void BluetoothProxy::loop() {
|
||||||
api::BluetoothGATTService service_resp;
|
api::BluetoothGATTService service_resp;
|
||||||
service_resp.uuid = {service->uuid.get_128bit_high(), service->uuid.get_128bit_low()};
|
service_resp.uuid = {service->uuid.get_128bit_high(), service->uuid.get_128bit_low()};
|
||||||
service_resp.handle = service->start_handle;
|
service_resp.handle = service->start_handle;
|
||||||
if (!service->parsed)
|
uint16_t char_offset = 0;
|
||||||
service->parse_characteristics();
|
esp_gattc_char_elem_t char_result;
|
||||||
for (auto &characteristic : service->characteristics) {
|
while (true) { // characteristics
|
||||||
|
uint16_t char_count = 1;
|
||||||
|
esp_gatt_status_t char_status =
|
||||||
|
esp_ble_gattc_get_all_char(connection->get_gattc_if(), connection->get_conn_id(), service->start_handle,
|
||||||
|
service->end_handle, &char_result, &char_count, char_offset);
|
||||||
|
if (char_status == ESP_GATT_INVALID_OFFSET || char_status == ESP_GATT_NOT_FOUND) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (char_status != ESP_GATT_OK) {
|
||||||
|
ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", connection->get_connection_index(),
|
||||||
|
connection->address_str().c_str(), char_status);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (char_count == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
api::BluetoothGATTCharacteristic characteristic_resp;
|
api::BluetoothGATTCharacteristic characteristic_resp;
|
||||||
characteristic_resp.uuid = {characteristic->uuid.get_128bit_high(), characteristic->uuid.get_128bit_low()};
|
auto char_uuid = espbt::ESPBTUUID::from_uuid(char_result.uuid);
|
||||||
characteristic_resp.handle = characteristic->handle;
|
characteristic_resp.uuid = {char_uuid.get_128bit_high(), char_uuid.get_128bit_low()};
|
||||||
characteristic_resp.properties = characteristic->properties;
|
characteristic_resp.handle = char_result.char_handle;
|
||||||
if (!characteristic->parsed)
|
characteristic_resp.properties = char_result.properties;
|
||||||
characteristic->parse_descriptors();
|
char_offset++;
|
||||||
for (auto &descriptor : characteristic->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(connection->get_gattc_if(), connection->get_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_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", connection->get_connection_index(),
|
||||||
|
connection->address_str().c_str(), desc_status);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (desc_count == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
api::BluetoothGATTDescriptor descriptor_resp;
|
api::BluetoothGATTDescriptor descriptor_resp;
|
||||||
descriptor_resp.uuid = {descriptor->uuid.get_128bit_high(), descriptor->uuid.get_128bit_low()};
|
auto desc_uuid = espbt::ESPBTUUID::from_uuid(desc_result.uuid);
|
||||||
descriptor_resp.handle = descriptor->handle;
|
descriptor_resp.uuid = {desc_uuid.get_128bit_high(), desc_uuid.get_128bit_low()};
|
||||||
|
descriptor_resp.handle = desc_result.handle;
|
||||||
characteristic_resp.descriptors.push_back(std::move(descriptor_resp));
|
characteristic_resp.descriptors.push_back(std::move(descriptor_resp));
|
||||||
|
desc_offset++;
|
||||||
}
|
}
|
||||||
service_resp.characteristics.push_back(std::move(characteristic_resp));
|
service_resp.characteristics.push_back(std::move(characteristic_resp));
|
||||||
}
|
}
|
||||||
resp.services.push_back(std::move(service_resp));
|
resp.services.push_back(std::move(service_resp));
|
||||||
api::global_api_server->send_bluetooth_gatt_services(resp);
|
api::global_api_server->send_bluetooth_gatt_services(resp);
|
||||||
// Descriptors are rarely used and can be quite large so we clear them
|
|
||||||
// after sending them to save memory. If something actually needs them
|
|
||||||
// it can parse them again.
|
|
||||||
for (auto &characteristic : service->characteristics) {
|
|
||||||
characteristic->release_descriptors();
|
|
||||||
}
|
|
||||||
connection->send_service_++;
|
connection->send_service_++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue