ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
improv_ble_component.h
Go to the documentation of this file.
1#pragma once
2
7
10
11#ifdef USE_IMPROV_BLE_STATE_CALLBACK
13#endif
14
15#ifdef USE_BINARY_SENSOR
17#endif
18
19#ifdef USE_OUTPUT
21#endif
22
23#include <span>
24#include <vector>
25
26// ESP-IDF is currently the only target platform with a BLE GATT server, so it is
27// the only backend this component has. The Python side keeps the platform table
28// (BLE_SERVER_BACKENDS in __init__.py); a second backend adds another arm here.
29#ifdef USE_ESP32
30
33
34#include <improv.h>
35
36namespace esphome::improv_ble {
37
38using namespace esp32_ble_server;
39
41#ifdef USE_BINARY_SENSOR
42 SUB_BINARY_SENSOR(authorizer)
43#endif
44
45 public:
47 void dump_config() override;
48 void loop() override;
49 void setup() override;
50 void setup_characteristics();
51
52 float get_setup_priority() const override;
53 void start();
54 void stop();
55 bool is_active() const { return this->state_ != improv::STATE_STOPPED; }
56 bool should_start() const { return this->should_start_; }
57
58#ifdef USE_IMPROV_BLE_STATE_CALLBACK
59 template<typename F> void add_on_state_callback(F &&callback) {
60 this->state_callback_.add(std::forward<F>(callback));
61 }
62#endif
63#ifdef USE_OUTPUT
64 void set_status_indicator(output::BinaryOutput *status_indicator) { this->status_indicator_ = status_indicator; }
65#endif
66 void set_identify_duration(uint32_t identify_duration) { this->identify_duration_ = identify_duration; }
67 void set_authorized_duration(uint32_t authorized_duration) { this->authorized_duration_ = authorized_duration; }
68
69 void set_wifi_timeout(uint32_t wifi_timeout) { this->wifi_timeout_ = wifi_timeout; }
70 uint32_t get_wifi_timeout() const { return this->wifi_timeout_; }
71
72 improv::State get_improv_state() const { return this->state_; }
73 improv::Error get_improv_error_state() const { return this->error_state_; }
74
75 protected:
80
82
83 std::vector<uint8_t> incoming_data_;
85 // Single byte fields here fill the alignment gap between connecting_sta_ and the next pointer
86 improv::State state_{improv::STATE_STOPPED};
87 improv::Error error_state_{improv::ERROR_NONE};
88 bool should_start_{false};
89 bool setup_complete_{false};
93
100
101#ifdef USE_OUTPUT
103#endif
104
105#ifdef USE_IMPROV_BLE_STATE_CALLBACK
106 CallbackManager<void(improv::State, improv::Error)> state_callback_{};
107#endif
108
114
115 void set_state_(improv::State state, bool update_advertising = true);
116 void set_error_(improv::Error error);
117 improv::State get_initial_state_() const;
118 void send_response_(std::span<const uint8_t> response);
122 bool check_identify_();
124#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
125 const char *state_to_string_(improv::State state);
126#endif
127};
128
129// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
130extern ImprovBLEComponent *global_improv_component;
131
132} // namespace esphome::improv_ble
133
134#endif
void set_state_(improv::State state, bool update_advertising=true)
const char * state_to_string_(improv::State state)
CallbackManager< void(improv::State, improv::Error)> state_callback_
void send_response_(std::span< const uint8_t > response)
bool state
Definition fan.h:2
ImprovBLEComponent * global_improv_component
static void uint32_t