ESPHome  2022.6.3
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_DISCONNECT_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 =
67  esp_ble_gattc_register_for_notify(this->parent()->gattc_if, this->parent()->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.conn_id != this->parent()->conn_id)
78  break;
79  if (param->read.status != ESP_GATT_OK) {
80  ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
81  break;
82  }
83  if (param->read.handle == this->handle) {
84  this->status_clear_warning();
85  this->publish_state(this->parse_data_(param->read.value, param->read.value_len));
86  }
87  break;
88  }
89  case ESP_GATTC_NOTIFY_EVT: {
90  if (param->notify.conn_id != this->parent()->conn_id || param->notify.handle != this->handle)
91  break;
92  ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
93  param->notify.handle, param->notify.value[0]);
94  this->publish_state(this->parse_data_(param->notify.value, param->notify.value_len));
95  break;
96  }
97  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
98  this->node_state = espbt::ClientState::ESTABLISHED;
99  break;
100  }
101  default:
102  break;
103  }
104 }
105 
106 float BLESensor::parse_data_(uint8_t *value, uint16_t value_len) {
107  if (this->data_to_value_func_.has_value()) {
108  std::vector<uint8_t> data(value, value + value_len);
109  return (*this->data_to_value_func_)(data);
110  } else {
111  return value[0];
112  }
113 }
114 
116  if (this->node_state != espbt::ClientState::ESTABLISHED) {
117  ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
118  return;
119  }
120  if (this->handle == 0) {
121  ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
122  return;
123  }
124 
125  auto status =
126  esp_ble_gattc_read_char(this->parent()->gattc_if, this->parent()->conn_id, this->handle, ESP_GATT_AUTH_REQ_NONE);
127  if (status) {
128  this->status_set_warning();
129  this->publish_state(NAN);
130  ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
131  }
132 }
133 
134 } // namespace ble_client
135 } // namespace esphome
136 #endif
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Definition: ble_client.cpp:300
espbt::ESPBTUUID descr_uuid_
Definition: ble_sensor.h:45
const std::string & get_name() const
Definition: entity_base.cpp:11
bool has_value() const
Definition: optional.h:87
optional< data_to_value_t > data_to_value_func_
Definition: ble_sensor.h:41
float parse_data_(uint8_t *value, uint16_t value_len)
Definition: ble_sensor.cpp:106
espbt::ESPBTUUID char_uuid_
Definition: ble_sensor.h:44
void status_clear_warning()
Definition: component.cpp:148
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:70
void status_set_warning()
Definition: component.cpp:140
espbt::ESPBTUUID service_uuid_
Definition: ble_sensor.h:43
Definition: a4988.cpp:4
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:35