diff --git a/CODEOWNERS b/CODEOWNERS index 18b4564280..80c5dc34c1 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -72,7 +72,6 @@ esphome/components/hitachi_ac424/* @sourabhjaiswal esphome/components/homeassistant/* @OttoWinter esphome/components/hrxl_maxsonar_wr/* @netmikey esphome/components/i2c/* @esphome/core -esphome/components/improv/* @jesserockz esphome/components/improv_serial/* @esphome/core esphome/components/inkbird_ibsth1_mini/* @fkirill esphome/components/inkplate6/* @jesserockz diff --git a/esphome/components/esp32_improv/__init__.py b/esphome/components/esp32_improv/__init__.py index 0b0214c63e..80c53f7c2a 100644 --- a/esphome/components/esp32_improv/__init__.py +++ b/esphome/components/esp32_improv/__init__.py @@ -4,7 +4,7 @@ from esphome.components import binary_sensor, output, esp32_ble_server from esphome.const import CONF_ID -AUTO_LOAD = ["binary_sensor", "output", "improv", "esp32_ble_server"] +AUTO_LOAD = ["binary_sensor", "output", "esp32_ble_server"] CODEOWNERS = ["@jesserockz"] CONFLICTS_WITH = ["esp32_ble_tracker", "esp32_ble_beacon"] DEPENDENCIES = ["wifi", "esp32"] @@ -56,6 +56,7 @@ async def to_code(config): cg.add(ble_server.register_service_component(var)) cg.add_define("USE_IMPROV") + cg.add_library("esphome/Improv", "1.0.0") cg.add(var.set_identify_duration(config[CONF_IDENTIFY_DURATION])) cg.add(var.set_authorized_duration(config[CONF_AUTHORIZED_DURATION])) diff --git a/esphome/components/esp32_improv/esp32_improv_component.h b/esphome/components/esp32_improv/esp32_improv_component.h index 3a5d150fbe..45639f2f63 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.h +++ b/esphome/components/esp32_improv/esp32_improv_component.h @@ -1,9 +1,8 @@ #pragma once #include "esphome/components/binary_sensor/binary_sensor.h" -#include "esphome/components/esp32_ble_server/ble_server.h" #include "esphome/components/esp32_ble_server/ble_characteristic.h" -#include "esphome/components/improv/improv.h" +#include "esphome/components/esp32_ble_server/ble_server.h" #include "esphome/components/output/binary_output.h" #include "esphome/components/wifi/wifi_component.h" #include "esphome/core/component.h" @@ -12,6 +11,8 @@ #ifdef USE_ESP32 +#include + namespace esphome { namespace esp32_improv { diff --git a/esphome/components/improv/__init__.py b/esphome/components/improv/__init__.py deleted file mode 100644 index b1de57df8f..0000000000 --- a/esphome/components/improv/__init__.py +++ /dev/null @@ -1 +0,0 @@ -CODEOWNERS = ["@jesserockz"] diff --git a/esphome/components/improv/improv.cpp b/esphome/components/improv/improv.cpp deleted file mode 100644 index 759962b51a..0000000000 --- a/esphome/components/improv/improv.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include "improv.h" - -namespace improv { - -ImprovCommand parse_improv_data(const std::vector &data, bool check_checksum) { - return parse_improv_data(data.data(), data.size(), check_checksum); -} - -ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum) { - ImprovCommand improv_command; - Command command = (Command) data[0]; - uint8_t data_length = data[1]; - - if (data_length != length - 2 - check_checksum) { - improv_command.command = UNKNOWN; - return improv_command; - } - - if (check_checksum) { - uint8_t checksum = data[length - 1]; - - uint32_t calculated_checksum = 0; - for (uint8_t i = 0; i < length - 1; i++) { - calculated_checksum += data[i]; - } - - if ((uint8_t) calculated_checksum != checksum) { - improv_command.command = BAD_CHECKSUM; - return improv_command; - } - } - - if (command == WIFI_SETTINGS) { - uint8_t ssid_length = data[2]; - uint8_t ssid_start = 3; - size_t ssid_end = ssid_start + ssid_length; - - uint8_t pass_length = data[ssid_end]; - size_t pass_start = ssid_end + 1; - size_t pass_end = pass_start + pass_length; - - std::string ssid(data + ssid_start, data + ssid_end); - std::string password(data + pass_start, data + pass_end); - return {.command = command, .ssid = ssid, .password = password}; - } - - improv_command.command = command; - return improv_command; -} - -std::vector build_rpc_response(Command command, const std::vector &datum, bool add_checksum) { - std::vector out; - uint32_t length = 0; - out.push_back(command); - for (auto str : datum) { - uint8_t len = str.length(); - length += len; - out.push_back(len); - out.insert(out.end(), str.begin(), str.end()); - } - out.insert(out.begin() + 1, length); - - if (add_checksum) { - uint32_t calculated_checksum = 0; - - for (uint8_t byte : out) { - calculated_checksum += byte; - } - out.push_back(calculated_checksum); - } - return out; -} - -#ifdef ARDUINO -std::vector build_rpc_response(Command command, const std::vector &datum, bool add_checksum) { - std::vector out; - uint32_t length = 0; - out.push_back(command); - for (auto str : datum) { - uint8_t len = str.length(); - length += len; - out.push_back(len); - out.insert(out.end(), str.begin(), str.end()); - } - out.insert(out.begin() + 1, length); - - if (add_checksum) { - uint32_t calculated_checksum = 0; - - for (uint8_t byte : out) { - calculated_checksum += byte; - } - out.push_back(calculated_checksum); - } - return out; -} -#endif // ARDUINO - -} // namespace improv diff --git a/esphome/components/improv/improv.h b/esphome/components/improv/improv.h deleted file mode 100644 index 9d1886ddaf..0000000000 --- a/esphome/components/improv/improv.h +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once - -#ifdef ARDUINO -#include "WString.h" -#endif // ARDUINO - -#include -#include -#include - -namespace improv { - -static const char *const SERVICE_UUID = "00467768-6228-2272-4663-277478268000"; -static const char *const STATUS_UUID = "00467768-6228-2272-4663-277478268001"; -static const char *const ERROR_UUID = "00467768-6228-2272-4663-277478268002"; -static const char *const RPC_COMMAND_UUID = "00467768-6228-2272-4663-277478268003"; -static const char *const RPC_RESULT_UUID = "00467768-6228-2272-4663-277478268004"; -static const char *const CAPABILITIES_UUID = "00467768-6228-2272-4663-277478268005"; - -enum Error : uint8_t { - ERROR_NONE = 0x00, - ERROR_INVALID_RPC = 0x01, - ERROR_UNKNOWN_RPC = 0x02, - ERROR_UNABLE_TO_CONNECT = 0x03, - ERROR_NOT_AUTHORIZED = 0x04, - ERROR_UNKNOWN = 0xFF, -}; - -enum State : uint8_t { - STATE_STOPPED = 0x00, - STATE_AWAITING_AUTHORIZATION = 0x01, - STATE_AUTHORIZED = 0x02, - STATE_PROVISIONING = 0x03, - STATE_PROVISIONED = 0x04, -}; - -enum Command : uint8_t { - UNKNOWN = 0x00, - WIFI_SETTINGS = 0x01, - IDENTIFY = 0x02, - GET_CURRENT_STATE = 0x02, - GET_DEVICE_INFO = 0x03, - BAD_CHECKSUM = 0xFF, -}; - -static const uint8_t CAPABILITY_IDENTIFY = 0x01; - -struct ImprovCommand { - Command command; - std::string ssid; - std::string password; -}; - -ImprovCommand parse_improv_data(const std::vector &data, bool check_checksum = true); -ImprovCommand parse_improv_data(const uint8_t *data, size_t length, bool check_checksum = true); - -std::vector build_rpc_response(Command command, const std::vector &datum, - bool add_checksum = true); -#ifdef ARDUINO -std::vector build_rpc_response(Command command, const std::vector &datum, bool add_checksum = true); -#endif // ARDUINO - -} // namespace improv diff --git a/esphome/components/improv_serial/__init__.py b/esphome/components/improv_serial/__init__.py index b1cdc2d93e..ed7c382a2f 100644 --- a/esphome/components/improv_serial/__init__.py +++ b/esphome/components/improv_serial/__init__.py @@ -5,7 +5,6 @@ import esphome.final_validate as fv CODEOWNERS = ["@esphome/core"] DEPENDENCIES = ["logger", "wifi"] -AUTO_LOAD = ["improv"] improv_serial_ns = cg.esphome_ns.namespace("improv_serial") @@ -31,3 +30,4 @@ FINAL_VALIDATE_SCHEMA = validate_logger_baud_rate async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) + cg.add_library("esphome/Improv", "1.0.0") diff --git a/esphome/components/improv_serial/improv_serial_component.h b/esphome/components/improv_serial/improv_serial_component.h index 539674e2d3..304afdaf75 100644 --- a/esphome/components/improv_serial/improv_serial_component.h +++ b/esphome/components/improv_serial/improv_serial_component.h @@ -1,11 +1,12 @@ #pragma once -#include "esphome/components/improv/improv.h" #include "esphome/components/wifi/wifi_component.h" #include "esphome/core/component.h" #include "esphome/core/defines.h" #include "esphome/core/helpers.h" +#include + #ifdef USE_ARDUINO #include #endif diff --git a/platformio.ini b/platformio.ini index 2ac6d0bfc8..b49438b097 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,6 +28,7 @@ build_flags = lib_deps = esphome/noise-c@0.1.4 ; api makuna/NeoPixelBus@2.6.9 ; neopixelbus + esphome/Improv@1.0.0 ; improv_serial / esp32_improv build_flags = -DESPHOME_LOG_LEVEL=ESPHOME_LOG_LEVEL_VERY_VERBOSE src_filter =