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";
18static constexpr uint16_t STOP_ADVERTISING_DELAY =
20static constexpr uint16_t NAME_ADVERTISING_INTERVAL = 60000;
21static constexpr uint16_t NAME_ADVERTISING_DURATION = 1000;
24static constexpr uint8_t IMPROV_SERVICE_DATA_SIZE = 8;
25static constexpr uint8_t IMPROV_PROTOCOL_ID_1 = 0x77;
26static constexpr uint8_t IMPROV_PROTOCOL_ID_2 = 0x46;
31#ifdef USE_BINARY_SENSOR
42 [
this](uint16_t conn_id) { this->
set_error_(improv::ERROR_NONE); });
61 BLECharacteristicEvt::VectorEvt::ON_WRITE, [
this](
const std::vector<uint8_t> &data, uint16_t
id) {
78 uint8_t capabilities = 0x00;
81 capabilities |= improv::CAPABILITY_IDENTIFY;
89 if (this->
state_ != improv::STATE_STOPPED) {
90 this->
state_ = improv::STATE_STOPPED;
91#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
100 ESP_LOGD(TAG,
"Creating Improv service");
110 if (this->
state_ != improv::STATE_STOPPED && this->
state_ != improv::STATE_PROVISIONED) {
115 case improv::STATE_STOPPED:
123 ESP_LOGV(TAG,
"Starting with device name advertising");
132 ESP_LOGD(TAG,
"Service started!");
136 case improv::STATE_AWAITING_AUTHORIZATION: {
137#ifdef USE_BINARY_SENSOR
150 case improv::STATE_AUTHORIZED: {
151#ifdef USE_BINARY_SENSOR
153 ESP_LOGD(TAG,
"Authorization timeout");
154 this->
set_state_(improv::STATE_AWAITING_AUTHORIZATION);
163 case improv::STATE_PROVISIONING: {
167 this->connecting_sta_.get_password());
172 std::vector<std::string> urls = {ESPHOME_MY_LINK};
176 std::string webserver_url =
"http://" + ip.str() +
":" + to_string(USE_WEBSERVER_PORT);
177 urls.push_back(webserver_url);
182 std::vector<uint8_t> data = improv::build_rpc_response(improv::WIFI_SETTINGS, urls);
188 case improv::STATE_PROVISIONED: {
213#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
216 case improv::STATE_STOPPED:
218 case improv::STATE_AWAITING_AUTHORIZATION:
219 return "AWAITING_AUTHORIZATION";
220 case improv::STATE_AUTHORIZED:
222 case improv::STATE_PROVISIONING:
223 return "PROVISIONING";
224 case improv::STATE_PROVISIONED:
225 return "PROVISIONED";
238 uint32_t time = now % 1000;
246 if (this->
state_ == state) {
250#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
257 if (
state != improv::STATE_STOPPED)
264 if (state != improv::STATE_STOPPED && update_advertising) {
272#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
278 if (error != improv::ERROR_NONE) {
279 ESP_LOGE(TAG,
"Error: %d", error);
286 if (this->
error_ !=
nullptr && (this->
error_->
get_value().empty() || this->error_->get_value()[0] != error)) {
288 if (this->
state_ != improv::STATE_STOPPED)
295 if (this->
state_ != improv::STATE_STOPPED)
303 ESP_LOGD(TAG,
"Setting Improv to start");
313 this->
set_timeout(
"end-service", STOP_ADVERTISING_DELAY, [
this] {
314 if (this->
state_ == improv::STATE_STOPPED || this->
service_ ==
nullptr)
324 ESP_LOGCONFIG(TAG,
"ESP32 Improv:");
325#ifdef USE_BINARY_SENSOR
326 LOG_BINARY_SENSOR(
" ",
"Authorizer", this->
authorizer_);
329 ESP_LOGCONFIG(TAG,
" Status Indicator: '%s'", YESNO(this->
status_indicator_ !=
nullptr));
339 improv::ImprovCommand command = improv::parse_improv_data(this->
incoming_data_);
340 switch (command.command) {
341 case improv::BAD_CHECKSUM:
342 ESP_LOGW(TAG,
"Error decoding Improv payload");
346 case improv::WIFI_SETTINGS: {
347 if (this->
state_ != improv::STATE_AUTHORIZED) {
348 ESP_LOGW(TAG,
"Settings received, but not authorized");
349 this->
set_error_(improv::ERROR_NOT_AUTHORIZED);
355 sta.set_password(command.password);
361 ESP_LOGD(TAG,
"Received Improv Wi-Fi settings ssid=%s, password=" LOG_SECRET(
"%s"), command.ssid.c_str(),
362 command.password.c_str());
365 this->
set_timeout(
"wifi-connect-timeout", 30000, f);
369 case improv::IDENTIFY:
374 ESP_LOGW(TAG,
"Unknown Improv payload");
379 ESP_LOGV(TAG,
"Too much data received or data malformed; resetting buffer");
382 ESP_LOGV(TAG,
"Waiting for split data packets");
387 this->
set_error_(improv::ERROR_UNABLE_TO_CONNECT);
389#ifdef USE_BINARY_SENSOR
393 ESP_LOGW(TAG,
"Timed out while connecting to Wi-Fi network");
398 uint8_t service_data[IMPROV_SERVICE_DATA_SIZE] = {};
399 service_data[0] = IMPROV_PROTOCOL_ID_1;
400 service_data[1] = IMPROV_PROTOCOL_ID_2;
401 service_data[2] =
static_cast<uint8_t
>(this->
state_);
403 uint8_t capabilities = 0x00;
406 capabilities |= improv::CAPABILITY_IDENTIFY;
409 service_data[3] = capabilities;
422 ESP_LOGV(TAG,
"Switching back to service data advertising");
432 ESP_LOGV(TAG,
"Switching to device name advertising");
442#ifdef USE_BINARY_SENSOR
444 return this->
authorizer_ ==
nullptr ? improv::STATE_AUTHORIZED : improv::STATE_AWAITING_AUTHORIZATION;
447 return improv::STATE_AUTHORIZED;
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 enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
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(T)> &&callback)
static ByteBuffer wrap(T value, Endian endianness=LITTLE)
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
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_
float get_setup_priority() const override
void set_status_indicator_state_(bool state)
void advertise_service_data_()
uint32_t identify_duration_
uint32_t authorized_start_
CallbackManager< void(improv::State, improv::Error)> state_callback_
void dump_config() override
bool advertising_device_name_
std::vector< uint8_t > incoming_data_
void set_error_(improv::Error error)
BLECharacteristic * rpc_response_
void process_incoming_data_()
void setup_characteristics()
BLECharacteristic * capabilities_
void set_state_(improv::State state, bool update_advertising=true)
improv::State get_initial_state_() const
BLECharacteristic * error_
wifi::WiFiAP connecting_sta_
output::BinaryOutput * status_indicator_
uint32_t last_name_adv_time_
const char * state_to_string_(improv::State state)
void update_advertising_type_()
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 format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.