12namespace esp32_improv {
14using namespace bytebuffer;
16static const char *
const TAG =
"esp32_improv.component";
17static const char *
const ESPHOME_MY_LINK =
"https://my.home-assistant.io/redirect/config_flow_start?domain=esphome";
22#ifdef USE_BINARY_SENSOR
33 [
this](uint16_t conn_id) { this->
set_error_(improv::ERROR_NONE); });
49 BLECharacteristicEvt::VectorEvt::ON_WRITE, [
this](
const std::vector<uint8_t> &data, uint16_t
id) {
66 uint8_t capabilities = 0x00;
69 capabilities |= improv::CAPABILITY_IDENTIFY;
77 if (this->
state_ != improv::STATE_STOPPED) {
78 this->
state_ = improv::STATE_STOPPED;
79#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
88 ESP_LOGD(TAG,
"Creating Improv service");
98 case improv::STATE_STOPPED:
107 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
109 ESP_LOGD(TAG,
"Service started!");
113 case improv::STATE_AWAITING_AUTHORIZATION: {
114#ifdef USE_BINARY_SENSOR
120 { this->
set_state_(improv::STATE_AUTHORIZED); }
128 case improv::STATE_AUTHORIZED: {
129#ifdef USE_BINARY_SENSOR
132 ESP_LOGD(TAG,
"Authorization timeout");
133 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
143 case improv::STATE_PROVISIONING: {
147 this->connecting_sta_.get_password());
152 std::vector<std::string> urls = {ESPHOME_MY_LINK};
156 std::string webserver_url =
"http://" + ip.str() +
":" +
to_string(USE_WEBSERVER_PORT);
157 urls.push_back(webserver_url);
162 std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls);
168 case improv::STATE_PROVISIONED: {
197 uint32_t time = now % 1000;
204 ESP_LOGV(TAG,
"Setting state: %d",
state);
208 if (
state != improv::STATE_STOPPED)
211 std::vector<uint8_t> service_data(8, 0);
212 service_data[0] = 0x77;
213 service_data[1] = 0x46;
214 service_data[2] =
static_cast<uint8_t
>(
state);
216 uint8_t capabilities = 0x00;
219 capabilities |= improv::CAPABILITY_IDENTIFY;
222 service_data[3] = capabilities;
223 service_data[4] = 0x00;
224 service_data[5] = 0x00;
225 service_data[6] = 0x00;
226 service_data[7] = 0x00;
229#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
235 if (error != improv::ERROR_NONE) {
236 ESP_LOGE(TAG,
"Error: %d", error);
238 if (this->
error_->
get_value().empty() || this->error_->get_value()[0] != error) {
240 if (this->
state_ != improv::STATE_STOPPED)
247 if (this->
state_ != improv::STATE_STOPPED)
255 ESP_LOGD(TAG,
"Setting Improv to start");
262 if (this->
state_ == improv::STATE_STOPPED || this->
service_ ==
nullptr)
272 ESP_LOGCONFIG(TAG,
"ESP32 Improv:");
273#ifdef USE_BINARY_SENSOR
274 LOG_BINARY_SENSOR(
" ",
"Authorizer", this->
authorizer_);
277 ESP_LOGCONFIG(TAG,
" Status Indicator: '%s'", YESNO(this->
status_indicator_ !=
nullptr));
287 improv::ImprovCommand command = improv::parse_improv_data(this->
incoming_data_);
288 switch (command.command) {
289 case improv::BAD_CHECKSUM:
290 ESP_LOGW(TAG,
"Error decoding Improv payload");
294 case improv::WIFI_SETTINGS: {
295 if (this->
state_ != improv::STATE_AUTHORIZED) {
296 ESP_LOGW(TAG,
"Settings received, but not authorized");
297 this->
set_error_(improv::ERROR_NOT_AUTHORIZED);
303 sta.set_password(command.password);
309 ESP_LOGD(TAG,
"Received Improv Wi-Fi settings ssid=%s, password=" LOG_SECRET(
"%s"), command.ssid.c_str(),
310 command.password.c_str());
313 this->
set_timeout(
"wifi-connect-timeout", 30000, f);
317 case improv::IDENTIFY:
322 ESP_LOGW(TAG,
"Unknown Improv payload");
327 ESP_LOGV(TAG,
"Too much data received or data malformed; resetting buffer...");
330 ESP_LOGV(TAG,
"Waiting for split data packets...");
335 this->
set_error_(improv::ERROR_UNABLE_TO_CONNECT);
337#ifdef USE_BINARY_SENSOR
341 ESP_LOGW(TAG,
"Timed out while connecting to Wi-Fi network");
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
bool cancel_timeout(const std::string &name)
Cancel a timeout function.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void add_on_state_callback(std::function< void(bool)> &&callback)
Add a callback to be notified of state changes.
static ByteBuffer wrap(T value, Endian endianness=LITTLE)
void advertising_set_service_data(const std::vector< uint8_t > &data)
static ESPBTUUID from_raw(const uint8_t *data)
void set_value(ByteBuffer buffer)
static const uint32_t PROPERTY_NOTIFY
static const uint32_t PROPERTY_WRITE
void add_descriptor(BLEDescriptor *descriptor)
static const uint32_t PROPERTY_READ
std::vector< uint8_t > & get_value()
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties)
void on_wifi_connect_timeout_()
void send_response_(std::vector< uint8_t > &response)
binary_sensor::BinarySensor * authorizer_
bool status_indicator_state_
improv::Error error_state_
BLECharacteristic * status_
uint32_t authorized_duration_
void set_state_(improv::State state)
float get_setup_priority() const override
void set_status_indicator_state_(bool state)
uint32_t identify_duration_
uint32_t authorized_start_
CallbackManager< void(improv::State, improv::Error)> state_callback_
void dump_config() override
std::vector< uint8_t > incoming_data_
void set_error_(improv::Error error)
BLECharacteristic * rpc_response_
void process_incoming_data_()
void setup_characteristics()
BLECharacteristic * capabilities_
BLECharacteristic * error_
wifi::WiFiAP connecting_sta_
output::BinaryOutput * status_indicator_
EventEmitterListenerID on(EvtType event, std::function< void(Args...)> listener)
virtual void turn_off()
Disable this binary output.
virtual void turn_on()
Enable this binary output.
const std::string & get_ssid() const
void set_ssid(const std::string &ssid)
void set_sta(const WiFiAP &ap)
void save_wifi_sta(const std::string &ssid, const std::string &password)
void start_connecting(const WiFiAP &ap, bool two)
BLEServer * global_ble_server
ESP32ImprovComponent * global_improv_component
const float AFTER_BLUETOOTH
WiFiComponent * global_wifi_component
Providing packet encoding functions for exchanging data with a remote host.
std::string to_string(int value)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.