ESPHome  2024.12.2
ble_sensor.cpp
Go to the documentation of this file.
1 #include "ble_sensor.h"
2 #include "esphome/core/log.h"
4 #include "esphome/core/helpers.h"
6 
7 #ifdef USE_ESP32
8 
9 namespace esphome {
10 namespace ble_client {
11 
12 static const char *const TAG = "ble_sensor";
13 
14 void BLESensor::loop() {}
15 
17  LOG_SENSOR("", "BLE Sensor", this);
18  ESP_LOGCONFIG(TAG, " MAC address : %s", this->parent()->address_str().c_str());
19  ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
20  ESP_LOGCONFIG(TAG, " Characteristic UUID: %s", this->char_uuid_.to_string().c_str());
21  ESP_LOGCONFIG(TAG, " Descriptor UUID : %s", this->descr_uuid_.to_string().c_str());
22  ESP_LOGCONFIG(TAG, " Notifications : %s", YESNO(this->notify_));
23  LOG_UPDATE_INTERVAL(this);
24 }
25 
26 void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
27  esp_ble_gattc_cb_param_t *param) {
28  switch (event) {
29  case ESP_GATTC_OPEN_EVT: {
30  if (param->open.status == ESP_GATT_OK) {
31  ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
32  break;
33  }
34  break;
35  }
36  case ESP_GATTC_CLOSE_EVT: {
37  ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
38  this->status_set_warning();
39  this->publish_state(NAN);
40  break;
41  }
42  case ESP_GATTC_SEARCH_CMPL_EVT: {
43  this->handle = 0;
44  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
45  if (chr == nullptr) {
46  this->status_set_warning();
47  this->publish_state(NAN);
48  ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
49  this->char_uuid_.to_string().c_str());
50  break;
51  }
52  this->handle = chr->handle;
53  if (this->descr_uuid_.get_uuid().len > 0) {
54  auto *descr = chr->get_descriptor(this->descr_uuid_);
55  if (descr == nullptr) {
56  this->status_set_warning();
57  this->publish_state(NAN);
58  ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
59  this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
60  this->descr_uuid_.to_string().c_str());
61  break;
62  }
63  this->handle = descr->handle;
64  }
65  if (this->notify_) {
66  auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(),
67  this->parent()->get_remote_bda(), chr->handle);
68  if (status) {
69  ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
70  }
71  } else {
72  this->node_state = espbt::ClientState::ESTABLISHED;
73  }
74  break;
75  }
76  case ESP_GATTC_READ_CHAR_EVT: {
77  if (param->read.status != ESP_GATT_OK) {
78  ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
79  break;
80  }
81  if (param->read.handle == this->handle) {
82  this->status_clear_warning();
83  this->publish_state(this->parse_data_(param->read.value, param->read.value_len));
84  }
85  break;
86  }
87  case ESP_GATTC_NOTIFY_EVT: {
88  ESP_LOGD(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
89  param->notify.handle, param->notify.value[0]);
90  if (param->notify.handle != this->handle)
91  break;
92  this->publish_state(this->parse_data_(param->notify.value, param->notify.value_len));
93  break;
94  }
95  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
96  if (param->reg_for_notify.handle == this->handle) {
97  if (param->reg_for_notify.status != ESP_GATT_OK) {
98  ESP_LOGW(TAG, "Error registering for notifications at handle %d, status=%d", param->reg_for_notify.handle,
99  param->reg_for_notify.status);
100  break;
101  }
102  this->node_state = espbt::ClientState::ESTABLISHED;
103  ESP_LOGD(TAG, "Register for notify on %s complete", this->char_uuid_.to_string().c_str());
104  }
105  break;
106  }
107  default:
108  break;
109  }
110 }
111 
112 float BLESensor::parse_data_(uint8_t *value, uint16_t value_len) {
113  if (this->data_to_value_func_.has_value()) {
114  std::vector<uint8_t> data(value, value + value_len);
115  return (*this->data_to_value_func_)(data);
116  } else {
117  return value[0];
118  }
119 }
120 
122  if (this->node_state != espbt::ClientState::ESTABLISHED) {
123  ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
124  return;
125  }
126  if (this->handle == 0) {
127  ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
128  return;
129  }
130 
131  auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle,
132  ESP_GATT_AUTH_REQ_NONE);
133  if (status) {
134  this->status_set_warning();
135  this->publish_state(NAN);
136  ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
137  }
138 }
139 
140 } // namespace ble_client
141 } // namespace esphome
142 #endif
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
espbt::ESPBTUUID descr_uuid_
Definition: ble_sensor.h:47
bool has_value() const
Definition: optional.h:87
optional< data_to_value_t > data_to_value_func_
Definition: ble_sensor.h:43
float parse_data_(uint8_t *value, uint16_t value_len)
Definition: ble_sensor.cpp:112
espbt::ESPBTUUID char_uuid_
Definition: ble_sensor.h:46
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
std::string to_string() const
Definition: ble_uuid.cpp:171
uint8_t status
Definition: bl0942.h:74
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
espbt::ESPBTUUID service_uuid_
Definition: ble_sensor.h:45
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const StringRef & get_name() const
Definition: entity_base.cpp:10
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:170
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition: ble_sensor.cpp:26
espbt::ClientState node_state
Definition: ble_client.h:38