ESPHome  2022.8.0
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_DISCONNECT_EVT: {
40  ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
41  this->status_set_warning();
42  this->publish_state(EMPTY);
43  break;
44  }
45  case ESP_GATTC_SEARCH_CMPL_EVT: {
46  this->handle = 0;
47  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
48  if (chr == nullptr) {
49  this->status_set_warning();
50  this->publish_state(EMPTY);
51  ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
52  this->char_uuid_.to_string().c_str());
53  break;
54  }
55  this->handle = chr->handle;
56  if (this->descr_uuid_.get_uuid().len > 0) {
57  auto *descr = chr->get_descriptor(this->descr_uuid_);
58  if (descr == nullptr) {
59  this->status_set_warning();
60  this->publish_state(EMPTY);
61  ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
62  this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
63  this->descr_uuid_.to_string().c_str());
64  break;
65  }
66  this->handle = descr->handle;
67  }
68  if (this->notify_) {
69  auto status =
70  esp_ble_gattc_register_for_notify(this->parent()->gattc_if, this->parent()->remote_bda, chr->handle);
71  if (status) {
72  ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
73  }
74  } else {
75  this->node_state = espbt::ClientState::ESTABLISHED;
76  }
77  break;
78  }
79  case ESP_GATTC_READ_CHAR_EVT: {
80  if (param->read.conn_id != this->parent()->conn_id)
81  break;
82  if (param->read.status != ESP_GATT_OK) {
83  ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
84  break;
85  }
86  if (param->read.handle == this->handle) {
87  this->status_clear_warning();
88  this->publish_state(this->parse_data(param->read.value, param->read.value_len));
89  }
90  break;
91  }
92  case ESP_GATTC_NOTIFY_EVT: {
93  if (param->notify.conn_id != this->parent()->conn_id || param->notify.handle != this->handle)
94  break;
95  ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
96  param->notify.handle, param->notify.value[0]);
97  this->publish_state(this->parse_data(param->notify.value, param->notify.value_len));
98  break;
99  }
100  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
101  this->node_state = espbt::ClientState::ESTABLISHED;
102  break;
103  }
104  default:
105  break;
106  }
107 }
108 
109 std::string BLETextSensor::parse_data(uint8_t *value, uint16_t value_len) {
110  std::string text(value, value + value_len);
111  return text;
112 }
113 
115  if (this->node_state != espbt::ClientState::ESTABLISHED) {
116  ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
117  return;
118  }
119  if (this->handle == 0) {
120  ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
121  return;
122  }
123 
124  auto status =
125  esp_ble_gattc_read_char(this->parent()->gattc_if, this->parent()->conn_id, this->handle, ESP_GATT_AUTH_REQ_NONE);
126  if (status) {
127  this->status_set_warning();
128  this->publish_state(EMPTY);
129  ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
130  }
131 }
132 
133 } // namespace ble_client
134 } // namespace esphome
135 #endif
std::string parse_data(uint8_t *value, uint16_t value_len)
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Definition: ble_client.cpp:301
const std::string & get_name() const
Definition: entity_base.cpp:11
void publish_state(const std::string &state)
Definition: text_sensor.cpp:12
void status_clear_warning()
Definition: component.cpp:148
void status_set_warning()
Definition: component.cpp:140
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
espbt::ClientState node_state
Definition: ble_client.h:35