11 namespace ble_client {
13 static const char *
const TAG =
"ble_text_sensor";
15 static const std::string EMPTY =
"";
20 LOG_TEXT_SENSOR(
"",
"BLE Text Sensor",
this);
21 ESP_LOGCONFIG(TAG,
" MAC address : %s", this->
parent()->address_str().c_str());
25 ESP_LOGCONFIG(TAG,
" Notifications : %s", YESNO(this->
notify_));
26 LOG_UPDATE_INTERVAL(
this);
30 esp_ble_gattc_cb_param_t *param) {
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());
39 case ESP_GATTC_CLOSE_EVT: {
44 case ESP_GATTC_SEARCH_CMPL_EVT: {
50 ESP_LOGW(TAG,
"No sensor characteristic found at service %s char %s", this->
service_uuid_.
to_string().c_str(),
54 this->
handle = chr->handle;
56 auto *descr = chr->get_descriptor(this->
descr_uuid_);
57 if (descr ==
nullptr) {
60 ESP_LOGW(TAG,
"No sensor descriptor found at service %s char %s descr %s",
65 this->
handle = descr->handle;
68 auto status = esp_ble_gattc_register_for_notify(this->
parent()->get_gattc_if(),
69 this->
parent()->get_remote_bda(), chr->handle);
71 ESP_LOGW(TAG,
"esp_ble_gattc_register_for_notify failed, status=%d",
status);
74 this->
node_state = espbt::ClientState::ESTABLISHED;
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);
89 case ESP_GATTC_NOTIFY_EVT: {
90 if (param->notify.handle != this->handle)
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]);
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;
108 std::string text(value, value + value_len);
113 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
114 ESP_LOGW(TAG,
"[%s] Cannot poll, not connected", this->
get_name().c_str());
118 ESP_LOGW(TAG,
"[%s] Cannot poll, no service or characteristic found", this->
get_name().c_str());
123 ESP_GATT_AUTH_REQ_NONE);
127 ESP_LOGW(TAG,
"[%s] Error sending read request for sensor, status=%d", this->
get_name().c_str(),
status);
std::string parse_data(uint8_t *value, uint16_t value_len)
espbt::ESPBTUUID descr_uuid_
void status_set_warning(const char *message="unspecified")
void dump_config() override
void publish_state(const std::string &state)
espbt::ESPBTUUID service_uuid_
void status_clear_warning()
std::string to_string() const
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Implementation of SPI Controller mode.
espbt::ESPBTUUID char_uuid_
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
esp_bt_uuid_t get_uuid() const
espbt::ClientState node_state