Ble client fixes (#1739)

Co-authored-by: Ben Buxton <bb@cactii.net>
This commit is contained in:
buxtronix 2021-05-04 06:51:03 +10:00 committed by GitHub
parent 07b3327102
commit dd4fb85170
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 17 deletions

View file

@ -102,7 +102,7 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es
switch (event) { switch (event) {
case ESP_GATTC_REG_EVT: { case ESP_GATTC_REG_EVT: {
if (param->reg.status == ESP_GATT_OK) { if (param->reg.status == ESP_GATT_OK) {
ESP_LOGI(TAG, "gattc registered app id %d", this->app_id); ESP_LOGV(TAG, "gattc registered app id %d", this->app_id);
this->gattc_if = esp_gattc_if; this->gattc_if = esp_gattc_if;
} else { } else {
ESP_LOGE(TAG, "gattc app registration failed id=%d code=%d", param->reg.app_id, param->reg.status); ESP_LOGE(TAG, "gattc app registration failed id=%d code=%d", param->reg.app_id, param->reg.status);
@ -110,7 +110,7 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es
break; break;
} }
case ESP_GATTC_OPEN_EVT: { case ESP_GATTC_OPEN_EVT: {
ESP_LOGI(TAG, "[%s] ESP_GATTC_OPEN_EVT", this->address_str().c_str()); ESP_LOGV(TAG, "[%s] ESP_GATTC_OPEN_EVT", this->address_str().c_str());
if (param->open.status != ESP_GATT_OK) { if (param->open.status != ESP_GATT_OK) {
ESP_LOGW(TAG, "connect to %s failed, status=%d", this->address_str().c_str(), param->open.status); ESP_LOGW(TAG, "connect to %s failed, status=%d", this->address_str().c_str(), param->open.status);
this->set_states(espbt::ClientState::Idle); this->set_states(espbt::ClientState::Idle);
@ -137,7 +137,7 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es
if (memcmp(param->disconnect.remote_bda, this->remote_bda, 6) != 0) { if (memcmp(param->disconnect.remote_bda, this->remote_bda, 6) != 0) {
return; return;
} }
ESP_LOGI(TAG, "[%s] ESP_GATTC_DISCONNECT_EVT", this->address_str().c_str()); ESP_LOGV(TAG, "[%s] ESP_GATTC_DISCONNECT_EVT", this->address_str().c_str());
for (auto &svc : this->services_) for (auto &svc : this->services_)
delete svc; delete svc;
this->services_.clear(); this->services_.clear();
@ -216,24 +216,36 @@ float BLEClient::parse_char_value(uint8_t *value, uint16_t length) {
return (float) ((uint8_t) value[1]); return (float) ((uint8_t) value[1]);
case 0x5: // uint12. case 0x5: // uint12.
case 0x6: // uint16. case 0x6: // uint16.
return (float) ((uint16_t)(value[1] << 8) + (uint16_t) value[2]); if (length > 2) {
return (float) ((uint16_t)(value[1] << 8) + (uint16_t) value[2]);
}
case 0x7: // uint24. case 0x7: // uint24.
return (float) ((uint32_t)(value[1] << 16) + (uint32_t)(value[2] << 8) + (uint32_t)(value[3])); if (length > 3) {
return (float) ((uint32_t)(value[1] << 16) + (uint32_t)(value[2] << 8) + (uint32_t)(value[3]));
}
case 0x8: // uint32. case 0x8: // uint32.
return (float) ((uint32_t)(value[1] << 24) + (uint32_t)(value[2] << 16) + (uint32_t)(value[3] << 8) + if (length > 4) {
(uint32_t)(value[4])); return (float) ((uint32_t)(value[1] << 24) + (uint32_t)(value[2] << 16) + (uint32_t)(value[3] << 8) +
(uint32_t)(value[4]));
}
case 0xC: // int8. case 0xC: // int8.
return (float) ((int8_t) value[1]); return (float) ((int8_t) value[1]);
case 0xD: // int12. case 0xD: // int12.
case 0xE: // int16. case 0xE: // int16.
return (float) ((int16_t)(value[1] << 8) + (int16_t) value[2]); if (length > 2) {
return (float) ((int16_t)(value[1] << 8) + (int16_t) value[2]);
}
case 0xF: // int24. case 0xF: // int24.
return (float) ((int32_t)(value[1] << 16) + (int32_t)(value[2] << 8) + (int32_t)(value[3])); if (length > 3) {
return (float) ((int32_t)(value[1] << 16) + (int32_t)(value[2] << 8) + (int32_t)(value[3]));
}
case 0x10: // int32. case 0x10: // int32.
return (float) ((int32_t)(value[1] << 24) + (int32_t)(value[2] << 16) + (int32_t)(value[3] << 8) + if (length > 4) {
(int32_t)(value[4])); return (float) ((int32_t)(value[1] << 24) + (int32_t)(value[2] << 16) + (int32_t)(value[3] << 8) +
(int32_t)(value[4]));
}
} }
ESP_LOGW(TAG, "Cannot parse characteristic value of type 0x%x", value[0]); ESP_LOGW(TAG, "Cannot parse characteristic value of type 0x%x length %d", value[0], length);
return NAN; return NAN;
} }
@ -359,7 +371,7 @@ void BLECharacteristic::parse_descriptors() {
desc->handle = result.handle; desc->handle = result.handle;
desc->characteristic = this; desc->characteristic = this;
this->descriptors.push_back(desc); this->descriptors.push_back(desc);
ESP_LOGI(TAG, " descriptor %s, handle 0x%x", desc->uuid.to_string().c_str(), desc->handle); ESP_LOGV(TAG, " descriptor %s, handle 0x%x", desc->uuid.to_string().c_str(), desc->handle);
offset++; offset++;
} }
} }

View file

@ -12,11 +12,11 @@
#include <esp_gattc_api.h> #include <esp_gattc_api.h>
#include <esp_bt_defs.h> #include <esp_bt_defs.h>
namespace espbt = esphome::esp32_ble_tracker;
namespace esphome { namespace esphome {
namespace ble_client { namespace ble_client {
namespace espbt = esphome::esp32_ble_tracker;
class BLEClient; class BLEClient;
class BLEService; class BLEService;
class BLECharacteristic; class BLECharacteristic;

View file

@ -30,7 +30,7 @@ void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
switch (event) { switch (event) {
case ESP_GATTC_OPEN_EVT: { case ESP_GATTC_OPEN_EVT: {
if (param->open.status == ESP_GATT_OK) { if (param->open.status == ESP_GATT_OK) {
ESP_LOGW(TAG, "[%s] Connected successfully!", this->get_name().c_str()); ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
break; break;
} }
break; break;
@ -91,7 +91,7 @@ void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
case ESP_GATTC_NOTIFY_EVT: { case ESP_GATTC_NOTIFY_EVT: {
if (param->notify.conn_id != this->parent()->conn_id || param->notify.handle != this->handle) if (param->notify.conn_id != this->parent()->conn_id || param->notify.handle != this->handle)
break; break;
ESP_LOGI(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(), ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
param->notify.handle, param->notify.value[0]); param->notify.handle, param->notify.value[0]);
this->publish_state((float) param->notify.value[0]); this->publish_state((float) param->notify.value[0]);
break; break;