9 namespace airthings_wave_base {
11 static const char *
const TAG =
"airthings_wave_base";
14 esp_ble_gattc_cb_param_t *param) {
16 case ESP_GATTC_OPEN_EVT: {
17 if (param->open.status == ESP_GATT_OK) {
18 ESP_LOGI(TAG,
"Connected successfully!");
23 case ESP_GATTC_DISCONNECT_EVT: {
27 ESP_LOGW(TAG,
"Disconnected!");
31 case ESP_GATTC_SEARCH_CMPL_EVT: {
34 this->
node_state = espbt::ClientState::ESTABLISHED;
47 case ESP_GATTC_READ_CHAR_EVT: {
48 if (param->read.conn_id != this->parent()->get_conn_id())
50 if (param->read.status != ESP_GATT_OK) {
51 ESP_LOGW(TAG,
"Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
54 if (param->read.handle == this->handle_) {
55 this->
read_sensors(param->read.value, param->read.value_len);
60 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
61 this->
node_state = espbt::ClientState::ESTABLISHED;
65 case ESP_GATTC_NOTIFY_EVT: {
66 if (param->notify.conn_id != this->parent()->get_conn_id())
68 if (param->notify.handle == this->acp_handle_) {
69 this->
read_battery_(param->notify.value, param->notify.value_len);
82 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
83 if (!this->
parent()->enabled) {
84 ESP_LOGW(TAG,
"Reconnecting to device");
88 ESP_LOGW(TAG,
"Connection in progress");
96 ESP_LOGW(TAG,
"No sensor characteristic found at service %s char %s", this->
service_uuid_.
to_string().c_str(),
104 ESP_GATT_AUTH_REQ_NONE);
106 ESP_LOGW(TAG,
"Error sending read request for sensor, status=%d",
status);
115 uint8_t battery_command = ACCESS_CONTROL_POINT_COMMAND;
116 uint8_t cccd_value[2] = {1, 0};
119 if (chr ==
nullptr) {
120 ESP_LOGW(TAG,
"No access control point characteristic found at service %s char %s",
127 CLIENT_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR_UUID);
128 if (descr ==
nullptr) {
135 esp_ble_gattc_register_for_notify(this->
parent()->get_gattc_if(), this->
parent()->get_remote_bda(), chr->handle);
137 ESP_LOGW(TAG,
"esp_ble_gattc_register_for_notify failed, status=%d", reg_status);
146 2, cccd_value, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
148 ESP_LOGW(TAG,
"Error sending CCC descriptor write request, status=%d", descr_status);
154 &battery_command, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
156 ESP_LOGW(TAG,
"Error sending read request for battery, status=%d", chr_status);
168 ESP_LOGD(TAG,
"Battery received: %u mV", (
unsigned int) value->battery);
171 float voltage = value->battery / 1000.0f;
202 this->
set_timeout(
"response_timeout", 30 * 1000, [
this]() {
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
uint32_t update_interval_
virtual void read_sensors(uint8_t *raw_value, uint16_t value_len)=0
bool request_read_values_()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void set_response_timeout_()
bool read_battery_next_update_
uint32_t battery_update_interval_
uint8_t responses_pending_
void response_received_()
espbt::ESPBTUUID access_control_point_characteristic_uuid_
bool is_valid_voc_value_(uint16_t voc)
void set_enabled(bool enabled)
void read_battery_(uint8_t *raw_value, uint16_t value_len)
sensor::Sensor * battery_voltage_
void publish_state(float state)
Publish a new state to the front-end.
espbt::ESPBTUUID sensors_data_characteristic_uuid_
espbt::ESPBTUUID service_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
std::string to_string() const
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Implementation of SPI Controller mode.
espbt::ClientState node_state