* Socket refactor and SSL * esp-idf temp * Fixes * Echo component and noise * Add noise API transport support * Updates * ESP-IDF * Complete * Fixes * Fixes * Versions update * New i2c APIs * Complete i2c refactor * SPI migration * Revert ESP Preferences migration, too complex for now * OTA support * Remove echo again * Remove ssl again * GPIOFlags updates * Rename esphal and ICACHE_RAM_ATTR * Make ESP32 arduino compilable again * Fix GPIO flags * Complete pin registry refactor and fixes * Fixes to make test1 compile * Remove sdkconfig file * Ignore sdkconfig file * Fixes in reviewing * Make test2 compile * Make test4 compile * Make test5 compile * Run clang-format * Fix lint errors * Use esp-idf APIs instead of btStart * Another round of fixes * Start implementing ESP8266 * Make test3 compile * Guard esp8266 code * Lint * Reformat * Fixes * Fixes v2 * more fixes * ESP-IDF tidy target * Convert ARDUINO_ARCH_ESPxx * Update WiFiSignalSensor * Update time ifdefs * OTA needs millis from hal * RestartSwitch needs delay from hal * ESP-IDF Uart * Fix OTA blank password * Allow setting sdkconfig * Fix idf partitions and allow setting sdkconfig from yaml * Re-add read/write compat APIs and fix esp8266 uart * Fix esp8266 store log strings in flash * Fix ESP32 arduino preferences not initialized * Update ifdefs * Change how sdkconfig change is detected * Add checks to ci-custom and fix them * Run clang-format * Add esp-idf clang-tidy target and fix errors * Fixes from clang-tidy idf round 2 * Fixes from compiling tests with esp-idf * Run clang-format * Switch test5.yaml to esp-idf * Implement ESP8266 Preferences * Lint * Re-do PIO package version selection a bit * Fix arduinoespressif32 package version * Fix unit tests * Lint * Lint fixes * Fix readv/writev not defined * Fix graphing component * Re-add all old options from core/config.py Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
139 lines
4.8 KiB
C++
139 lines
4.8 KiB
C++
#include "ble_sensor.h"
|
|
#include "esphome/core/log.h"
|
|
#include "esphome/core/application.h"
|
|
#include "esphome/core/helpers.h"
|
|
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
|
|
|
|
#ifdef USE_ESP32
|
|
|
|
namespace esphome {
|
|
namespace ble_client {
|
|
|
|
static const char *const TAG = "ble_sensor";
|
|
|
|
uint32_t BLESensor::hash_base() { return 343459825UL; }
|
|
|
|
void BLESensor::loop() {}
|
|
|
|
void BLESensor::dump_config() {
|
|
LOG_SENSOR("", "BLE Sensor", this);
|
|
ESP_LOGCONFIG(TAG, " MAC address : %s", this->parent()->address_str().c_str());
|
|
ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
|
|
ESP_LOGCONFIG(TAG, " Characteristic UUID: %s", this->char_uuid_.to_string().c_str());
|
|
ESP_LOGCONFIG(TAG, " Descriptor UUID : %s", this->descr_uuid_.to_string().c_str());
|
|
ESP_LOGCONFIG(TAG, " Notifications : %s", YESNO(this->notify_));
|
|
LOG_UPDATE_INTERVAL(this);
|
|
}
|
|
|
|
void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
|
|
esp_ble_gattc_cb_param_t *param) {
|
|
switch (event) {
|
|
case ESP_GATTC_OPEN_EVT: {
|
|
if (param->open.status == ESP_GATT_OK) {
|
|
ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
case ESP_GATTC_DISCONNECT_EVT: {
|
|
ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
|
|
this->status_set_warning();
|
|
this->publish_state(NAN);
|
|
break;
|
|
}
|
|
case ESP_GATTC_SEARCH_CMPL_EVT: {
|
|
this->handle = 0;
|
|
auto chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
|
|
if (chr == nullptr) {
|
|
this->status_set_warning();
|
|
this->publish_state(NAN);
|
|
ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
|
|
this->char_uuid_.to_string().c_str());
|
|
break;
|
|
}
|
|
this->handle = chr->handle;
|
|
if (this->descr_uuid_.get_uuid().len > 0) {
|
|
auto descr = chr->get_descriptor(this->descr_uuid_);
|
|
if (descr == nullptr) {
|
|
this->status_set_warning();
|
|
this->publish_state(NAN);
|
|
ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
|
|
this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
|
|
this->descr_uuid_.to_string().c_str());
|
|
break;
|
|
}
|
|
this->handle = descr->handle;
|
|
}
|
|
if (this->notify_) {
|
|
auto status =
|
|
esp_ble_gattc_register_for_notify(this->parent()->gattc_if, this->parent()->remote_bda, chr->handle);
|
|
if (status) {
|
|
ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
|
|
}
|
|
} else {
|
|
this->node_state = espbt::ClientState::Established;
|
|
}
|
|
break;
|
|
}
|
|
case ESP_GATTC_READ_CHAR_EVT: {
|
|
if (param->read.conn_id != this->parent()->conn_id)
|
|
break;
|
|
if (param->read.status != ESP_GATT_OK) {
|
|
ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
|
|
break;
|
|
}
|
|
if (param->read.handle == this->handle) {
|
|
this->status_clear_warning();
|
|
this->publish_state(this->parse_data(param->read.value, param->read.value_len));
|
|
}
|
|
break;
|
|
}
|
|
case ESP_GATTC_NOTIFY_EVT: {
|
|
if (param->notify.conn_id != this->parent()->conn_id || param->notify.handle != this->handle)
|
|
break;
|
|
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]);
|
|
this->publish_state(this->parse_data(param->notify.value, param->notify.value_len));
|
|
break;
|
|
}
|
|
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
|
|
this->node_state = espbt::ClientState::Established;
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
float BLESensor::parse_data(uint8_t *value, uint16_t value_len) {
|
|
if (this->data_to_value_func_.has_value()) {
|
|
std::vector<uint8_t> data(value, value + value_len);
|
|
return (*this->data_to_value_func_)(data);
|
|
} else {
|
|
return value[0];
|
|
}
|
|
}
|
|
|
|
void BLESensor::update() {
|
|
if (this->node_state != espbt::ClientState::Established) {
|
|
ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
|
|
return;
|
|
}
|
|
if (this->handle == 0) {
|
|
ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
|
|
return;
|
|
}
|
|
|
|
auto status =
|
|
esp_ble_gattc_read_char(this->parent()->gattc_if, this->parent()->conn_id, this->handle, ESP_GATT_AUTH_REQ_NONE);
|
|
if (status) {
|
|
this->status_set_warning();
|
|
this->publish_state(NAN);
|
|
ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
|
|
}
|
|
}
|
|
|
|
} // namespace ble_client
|
|
} // namespace esphome
|
|
#endif
|