10 namespace ble_client {
12 static const char *
const TAG =
"ble_sensor";
17 LOG_SENSOR(
"",
"BLE Sensor",
this);
18 ESP_LOGCONFIG(TAG,
" MAC address : %s", this->
parent()->address_str().c_str());
22 ESP_LOGCONFIG(TAG,
" Notifications : %s", YESNO(this->
notify_));
23 LOG_UPDATE_INTERVAL(
this);
27 esp_ble_gattc_cb_param_t *param) {
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());
36 case ESP_GATTC_CLOSE_EVT: {
37 ESP_LOGW(TAG,
"[%s] Disconnected!", this->
get_name().c_str());
42 case ESP_GATTC_SEARCH_CMPL_EVT: {
48 ESP_LOGW(TAG,
"No sensor characteristic found at service %s char %s", this->
service_uuid_.
to_string().c_str(),
52 this->
handle = chr->handle;
54 auto *descr = chr->get_descriptor(this->
descr_uuid_);
55 if (descr ==
nullptr) {
58 ESP_LOGW(TAG,
"No sensor descriptor found at service %s char %s descr %s",
63 this->
handle = descr->handle;
66 auto status = esp_ble_gattc_register_for_notify(this->
parent()->get_gattc_if(),
67 this->
parent()->get_remote_bda(), chr->handle);
69 ESP_LOGW(TAG,
"esp_ble_gattc_register_for_notify failed, status=%d",
status);
72 this->
node_state = espbt::ClientState::ESTABLISHED;
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);
81 if (param->read.handle == this->handle) {
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)
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);
102 this->
node_state = espbt::ClientState::ESTABLISHED;
114 std::vector<uint8_t> data(value, value + value_len);
122 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
123 ESP_LOGW(TAG,
"[%s] Cannot poll, not connected", this->
get_name().c_str());
127 ESP_LOGW(TAG,
"[%s] Cannot poll, no service or characteristic found", this->
get_name().c_str());
132 ESP_GATT_AUTH_REQ_NONE);
136 ESP_LOGW(TAG,
"[%s] Error sending read request for sensor, status=%d", this->
get_name().c_str(),
status);
void dump_config() override
void status_set_warning(const char *message="unspecified")
espbt::ESPBTUUID descr_uuid_
optional< data_to_value_t > data_to_value_func_
float parse_data_(uint8_t *value, uint16_t value_len)
espbt::ESPBTUUID char_uuid_
void status_clear_warning()
void publish_state(float state)
Publish a new state to the front-end.
std::string to_string() const
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
espbt::ESPBTUUID service_uuid_
Implementation of SPI Controller mode.
const StringRef & get_name() const
esp_bt_uuid_t get_uuid() const
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