ESPHome  2024.12.2
ble_text_sensor.cpp
Go to the documentation of this file.
1 #include "ble_text_sensor.h"
2 
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/log.h"
7 
8 #ifdef USE_ESP32
9 
10 namespace esphome {
11 namespace ble_client {
12 
13 static const char *const TAG = "ble_text_sensor";
14 
15 static const std::string EMPTY = "";
16 
18 
20  LOG_TEXT_SENSOR("", "BLE Text Sensor", this);
21  ESP_LOGCONFIG(TAG, " MAC address : %s", this->parent()->address_str().c_str());
22  ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
23  ESP_LOGCONFIG(TAG, " Characteristic UUID: %s", this->char_uuid_.to_string().c_str());
24  ESP_LOGCONFIG(TAG, " Descriptor UUID : %s", this->descr_uuid_.to_string().c_str());
25  ESP_LOGCONFIG(TAG, " Notifications : %s", YESNO(this->notify_));
26  LOG_UPDATE_INTERVAL(this);
27 }
28 
29 void BLETextSensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
30  esp_ble_gattc_cb_param_t *param) {
31  switch (event) {
32  case ESP_GATTC_OPEN_EVT: {
33  if (param->open.status == ESP_GATT_OK) {
34  ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
35  break;
36  }
37  break;
38  }
39  case ESP_GATTC_CLOSE_EVT: {
40  this->status_set_warning();
41  this->publish_state(EMPTY);
42  break;
43  }
44  case ESP_GATTC_SEARCH_CMPL_EVT: {
45  this->handle = 0;
46  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
47  if (chr == nullptr) {
48  this->status_set_warning();
49  this->publish_state(EMPTY);
50  ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
51  this->char_uuid_.to_string().c_str());
52  break;
53  }
54  this->handle = chr->handle;
55  if (this->descr_uuid_.get_uuid().len > 0) {
56  auto *descr = chr->get_descriptor(this->descr_uuid_);
57  if (descr == nullptr) {
58  this->status_set_warning();
59  this->publish_state(EMPTY);
60  ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
61  this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
62  this->descr_uuid_.to_string().c_str());
63  break;
64  }
65  this->handle = descr->handle;
66  }
67  if (this->notify_) {
68  auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(),
69  this->parent()->get_remote_bda(), chr->handle);
70  if (status) {
71  ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
72  }
73  } else {
74  this->node_state = espbt::ClientState::ESTABLISHED;
75  }
76  break;
77  }
78  case ESP_GATTC_READ_CHAR_EVT: {
79  if (param->read.handle == this->handle) {
80  if (param->read.status != ESP_GATT_OK) {
81  ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
82  break;
83  }
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.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  if (param->reg_for_notify.status == ESP_GATT_OK && param->reg_for_notify.handle == this->handle)
99  this->node_state = espbt::ClientState::ESTABLISHED;
100  break;
101  }
102  default:
103  break;
104  }
105 }
106 
107 std::string BLETextSensor::parse_data(uint8_t *value, uint16_t value_len) {
108  std::string text(value, value + value_len);
109  return text;
110 }
111 
113  if (this->node_state != espbt::ClientState::ESTABLISHED) {
114  ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
115  return;
116  }
117  if (this->handle == 0) {
118  ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
119  return;
120  }
121 
122  auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle,
123  ESP_GATT_AUTH_REQ_NONE);
124  if (status) {
125  this->status_set_warning();
126  this->publish_state(EMPTY);
127  ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
128  }
129 }
130 
131 } // namespace ble_client
132 } // namespace esphome
133 #endif
std::string parse_data(uint8_t *value, uint16_t value_len)
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
void status_clear_warning()
Definition: component.cpp:166
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)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
const StringRef & get_name() const
Definition: entity_base.cpp:10
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:170
espbt::ClientState node_state
Definition: ble_client.h:38