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: {
199 uint32_t time = now % 1000;
206 ESP_LOGV(TAG,
"Setting state: %d",
state);
210 if (
state != improv::STATE_STOPPED)
213 std::vector<uint8_t> service_data(8, 0);
214 service_data[0] = 0x77;
215 service_data[1] = 0x46;
216 service_data[2] =
static_cast<uint8_t
>(
state);
218 uint8_t capabilities = 0x00;
221 capabilities |= improv::CAPABILITY_IDENTIFY;
224 service_data[3] = capabilities;
225 service_data[4] = 0x00;
226 service_data[5] = 0x00;
227 service_data[6] = 0x00;
228 service_data[7] = 0x00;
231#ifdef USE_ESP32_IMPROV_STATE_CALLBACK
237 if (error != improv::ERROR_NONE) {
238 ESP_LOGE(TAG,
"Error: %d", error);
240 if (this->
error_->
get_value().empty() || this->error_->get_value()[0] != error) {
242 if (this->
state_ != improv::STATE_STOPPED)
249 if (this->
state_ != improv::STATE_STOPPED)
257 ESP_LOGD(TAG,
"Setting Improv to start");
265 if (this->
state_ == improv::STATE_STOPPED || this->
service_ ==
nullptr)
275 ESP_LOGCONFIG(TAG,
"ESP32 Improv:");
276#ifdef USE_BINARY_SENSOR
277 LOG_BINARY_SENSOR(
" ",
"Authorizer", this->
authorizer_);
280 ESP_LOGCONFIG(TAG,
" Status Indicator: '%s'", YESNO(this->
status_indicator_ !=
nullptr));
290 improv::ImprovCommand command = improv::parse_improv_data(this->
incoming_data_);
291 switch (command.command) {
292 case improv::BAD_CHECKSUM:
293 ESP_LOGW(TAG,
"Error decoding Improv payload");
297 case improv::WIFI_SETTINGS: {
298 if (this->
state_ != improv::STATE_AUTHORIZED) {
299 ESP_LOGW(TAG,
"Settings received, but not authorized");
300 this->
set_error_(improv::ERROR_NOT_AUTHORIZED);
306 sta.set_password(command.password);
312 ESP_LOGD(TAG,
"Received Improv Wi-Fi settings ssid=%s, password=" LOG_SECRET(
"%s"), command.ssid.c_str(),
313 command.password.c_str());
316 this->
set_timeout(
"wifi-connect-timeout", 30000, f);
320 case improv::IDENTIFY:
325 ESP_LOGW(TAG,
"Unknown Improv payload");
330 ESP_LOGV(TAG,
"Too much data received or data malformed; resetting buffer");
333 ESP_LOGV(TAG,
"Waiting for split data packets");
338 this->
set_error_(improv::ERROR_UNABLE_TO_CONNECT);
340#ifdef USE_BINARY_SENSOR
344 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 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(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 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.