diff --git a/config_allfeatures.cmake b/config_allfeatures.cmake index 6501142..de85f5c 100644 --- a/config_allfeatures.cmake +++ b/config_allfeatures.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=7 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=bobbyquad - -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER -DFEATURE_OTA -DOTA_USERNAME="allfeatures" diff --git a/config_comred.cmake b/config_comred.cmake index ea8bd84..db3740d 100644 --- a/config_comred.cmake +++ b/config_comred.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=7 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=comr_bobbyquad - -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER -DFEATURE_OTA -DOTA_USERNAME="comred" diff --git a/config_feedc0de.cmake b/config_feedc0de.cmake index e4bae9f..f64676b 100644 --- a/config_feedc0de.cmake +++ b/config_feedc0de.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=17 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=bobbyquad - -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER -DFEATURE_OTA -DOTA_USERNAME="feedc0de" diff --git a/config_greyhash.cmake b/config_greyhash.cmake index 854abbf..962d2a4 100644 --- a/config_greyhash.cmake +++ b/config_greyhash.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=7 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=bobbyquad -# -DAP_PASSWORD=Passwort_123 # -DFEATURE_WEBSERVER # -DFEATURE_OTA # -DOTA_USERNAME="greyhash" diff --git a/config_mick.cmake b/config_mick.cmake index b537942..aa97e23 100644 --- a/config_mick.cmake +++ b/config_mick.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=7 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=bobbyquad - -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER # -DFEATURE_OTA -DOTA_USERNAME="mick" diff --git a/config_nofeatures.cmake b/config_nofeatures.cmake index 07da662..708278a 100644 --- a/config_nofeatures.cmake +++ b/config_nofeatures.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=7 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=bobbyquad - -DAP_PASSWORD=Passwort_123 # -DFEATURE_WEBSERVER # -DFEATURE_OTA -DOTA_USERNAME="nofeatures" diff --git a/config_peter.cmake b/config_peter.cmake index b92556b..83bde44 100644 --- a/config_peter.cmake +++ b/config_peter.cmake @@ -40,8 +40,6 @@ set(BOBBYCAR_BUILDFLAGS -DDEFAULT_NMOTMAX=2000 -DDEFAULT_FIELDWEAKMAX=7 -DDEFAULT_FIELDADVMAX=40 - -DDEVICE_PREFIX=peter_bobbyquad - -DAP_PASSWORD=Passwort_123 -DFEATURE_WEBSERVER -DFEATURE_OTA -DOTA_USERNAME="peter" diff --git a/main/accessors/settingsaccessors.h b/main/accessors/settingsaccessors.h index de687be..190d53d 100644 --- a/main/accessors/settingsaccessors.h +++ b/main/accessors/settingsaccessors.h @@ -6,6 +6,7 @@ // local includes #include "globals.h" #include "utils.h" +#include "newsettings.h" //! Special type of RefAccessor that also saves settings after setValue() template @@ -39,8 +40,16 @@ struct FieldWeakMaxAccessor : public RefAccessorSaveSettings { int16_t struct PhaseAdvMaxAccessor : public RefAccessorSaveSettings { int16_t &getRef() const override { return settings.limits.phaseAdvMax; } }; // WiFi -struct WifiStaEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.wifiSettings.wifiStaEnabled; } }; -struct WifiApEnabledAccessor : public RefAccessorSaveSettings { bool &getRef() const override { return settings.wifiSettings.wifiApEnabled; } }; +struct WifiStaEnabledAccessor : public virtual espgui::AccessorInterface +{ + bool getValue() const override { return configs.wifiStaEnabled.value; } + void setValue(bool value) override { configs.write_config(configs.wifiStaEnabled, value); } +}; +struct WifiApEnabledAccessor : public virtual espgui::AccessorInterface +{ + bool getValue() const override { return configs.wifiApEnabled.value; } + void setValue(bool value) override { configs.write_config(configs.wifiApEnabled, value); } +}; // Bluetooth #ifdef FEATURE_BLUETOOTH diff --git a/main/actions/bluetoothbeginaction.cpp b/main/actions/bluetoothbeginaction.cpp index 6e894ee..6705a48 100644 --- a/main/actions/bluetoothbeginaction.cpp +++ b/main/actions/bluetoothbeginaction.cpp @@ -2,11 +2,12 @@ // local includes #include "globals.h" +#include "newsettings.h" #ifdef FEATURE_BLUETOOTH void BluetoothBeginAction::triggered() { - if (!bluetoothSerial.begin(deviceName)) + if (!bluetoothSerial.begin(configs.bluetoothName.value.c_str())) { //Serial.println("Could not begin bluetooth"); // TODO: better error handling diff --git a/main/actions/bluetoothbeginmasteraction.h b/main/actions/bluetoothbeginmasteraction.h index a63c9aa..5d50824 100644 --- a/main/actions/bluetoothbeginmasteraction.h +++ b/main/actions/bluetoothbeginmasteraction.h @@ -2,6 +2,7 @@ #include "actioninterface.h" #include "globals.h" +#include "newsettings.h" using namespace espgui; @@ -12,7 +13,7 @@ class BluetoothBeginMasterAction : public virtual ActionInterface public: void triggered() override { - if (!bluetoothSerial.begin(deviceName, true)) + if (!bluetoothSerial.begin(configs.bluetoothName.value.c_str(), true)) { //Serial.println("Could not begin bluetooth master"); // TODO: better error handling diff --git a/main/ble_bobby.cpp b/main/ble_bobby.cpp index 85cfc6b..3e9ebd3 100644 --- a/main/ble_bobby.cpp +++ b/main/ble_bobby.cpp @@ -3,11 +3,46 @@ // esp-idf includes #include -namespace { -constexpr const char * const TAG = "BOBBYBLE"; -} // namespace +// 3rdparty lib includes +#include +#include +#include + +// local includes +#ifdef FEATURE_LEDSTRIP +#include "ledstrip.h" +#endif +#include "globals.h" +#include "modes/remotecontrolmode.h" +#include "utils.h" +#include "newsettings.h" #ifdef FEATURE_BLE +namespace { +constexpr const char * const TAG = "BOBBYBLE"; + +class RemoteControlCallbacks : public NimBLECharacteristicCallbacks +{ +public: + void onWrite(NimBLECharacteristic* pCharacteristic) override; +}; + +#ifdef FEATURE_WIRELESS_CONFIG +class WirelessSettingsCallbacks : public NimBLECharacteristicCallbacks +{ +public: + void onWrite(NimBLECharacteristic* pCharacteristic) override; +}; + +class WiFiListCallbacks : public NimBLECharacteristicCallbacks +{ +public: + void onRead(NimBLECharacteristic* pCharacteristic) override; +}; +#endif // FEATURE_WIRELESS_CONFIG + +} // namespace + BLEServer *pServer{}; BLEService *pService{}; BLECharacteristic *livestatsCharacteristic{}; @@ -15,21 +50,21 @@ BLECharacteristic *remotecontrolCharacteristic{}; #ifdef FEATURE_WIRELESS_CONFIG BLECharacteristic *wirelessConfig{}; BLECharacteristic *getwifilist{}; -#endif +#endif // FEATURE_WIRELESS_CONFIG +namespace { RemoteControlCallbacks bleRemoteCallbacks; #ifdef FEATURE_WIRELESS_CONFIG WirelessSettingsCallbacks bleWirelessSettingsCallbacks; WiFiListCallbacks bleWiFiListCallbacks; -#endif +#endif // FEATURE_WIRELESS_CONFIG -namespace { void createBle() { ESP_LOGI("BOBBY", "called"); - BLEDevice::init(deviceName); + BLEDevice::init(configs.bluetoothName.value); const auto serviceUuid{"0335e46c-f355-4ce6-8076-017de08cee98"}; @@ -198,6 +233,8 @@ void handleBle() } } +namespace { + void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) { const auto &val = pCharacteristic->getValue(); @@ -212,7 +249,7 @@ void RemoteControlCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) #ifdef FEATURE_LEDSTRIP auto newBlinkAnimation = doc["anim"].as(); if (blinkAnimation != newBlinkAnimation) blinkAnimation = newBlinkAnimation; -#endif +#endif // FEATURE_LEDSTRIP const bool isInverted = (settings.controllerHardware.invertFrontLeft && !settings.controllerHardware.invertFrontRight); @@ -244,8 +281,8 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) if (write_type == "wifi") { const int index = doc["wifi_index"].as(); ESP_LOGI(TAG, "[ble_config]: Set wifi%i: WiFi-SSID: %s, WiFi-Password: ***", doc["wifi_index"].as(), doc["wifi_ssid"].as()); - stringSettings.wifis[index].ssid = doc["wifi_ssid"].as(); - stringSettings.wifis[index].key = doc["wifi_pass"].as(); + configs.write_config(configs.wifi_configs[index].ssid, doc["wifi_ssid"].as()); + configs.write_config(configs.wifi_configs[index].key, doc["wifi_pass"].as()); saveSettings(); } else { const auto deserialized = deserializeJson(doc, val); @@ -253,19 +290,22 @@ void WirelessSettingsCallbacks::onWrite(NimBLECharacteristic* pCharacteristic) } } -void WiFiListCallbacks::onRead(NimBLECharacteristic *pCharacteristic) { +void WiFiListCallbacks::onRead(NimBLECharacteristic *pCharacteristic) +{ StaticJsonDocument<768> responseDoc; - auto wifis = stringSettings.wifis; auto wifiArray = responseDoc.createNestedArray("wifis"); ESP_LOGI(TAG, "[ble_wifilist] Got request for listing wifi ssids."); - for (unsigned int index = 0; index < wifis.size(); index++) { - wifiArray.add(wifis[index].ssid); + for (const auto &wifi : configs.wifi_configs) + { + wifiArray.add(wifi.ssid.value); } - responseDoc["wifi_count"] = wifis.size(); + responseDoc["wifi_count"] = configs.wifi_configs.size(); std::string json; serializeJson(responseDoc, json); pCharacteristic->setValue(json); } -#endif +#endif // FEATURE_WIRELESS_CONFIG + +} // namespace #endif diff --git a/main/ble_bobby.h b/main/ble_bobby.h index 905ee2d..1ffb091 100644 --- a/main/ble_bobby.h +++ b/main/ble_bobby.h @@ -1,24 +1,10 @@ #pragma once -// 3rdparty lib includes -#include #ifdef FEATURE_BLE + +// esp-idf includes #include -#endif -#ifdef FEATURE_LEDSTRIP -#include "ledstrip.h" -#endif -// local includes -#include "globals.h" -#include "futurecpp.h" -#include "modes/remotecontrolmode.h" -#include "utils.h" - -//wifistack -#include "wifi_bobbycar.h" - -#ifdef FEATURE_BLE extern BLEServer *pServer; extern BLEService *pService; extern BLECharacteristic *livestatsCharacteristic; @@ -26,34 +12,7 @@ extern BLECharacteristic *remotecontrolCharacteristic; #ifdef FEATURE_WIRELESS_CONFIG extern BLECharacteristic *wirelessConfig; extern BLECharacteristic *getwifilist; -#endif - -class RemoteControlCallbacks : public NimBLECharacteristicCallbacks -{ -public: - void onWrite(NimBLECharacteristic* pCharacteristic) override; -}; - -#ifdef FEATURE_WIRELESS_CONFIG -class WirelessSettingsCallbacks : public NimBLECharacteristicCallbacks -{ -public: - void onWrite(NimBLECharacteristic* pCharacteristic) override; -}; - -class WiFiListCallbacks : public NimBLECharacteristicCallbacks -{ -public: - void onRead(NimBLECharacteristic* pCharacteristic) override; -}; -#endif - -extern RemoteControlCallbacks bleRemoteCallbacks; - -#ifdef FEATURE_WIRELESS_CONFIG -extern WirelessSettingsCallbacks bleWirelessSettingsCallbacks; -extern WiFiListCallbacks bleWiFiListCallbacks; -#endif +#endif // FEATURE_WIRELESS_CONFIG void initBle(); diff --git a/main/displays/qrcodedebug.cpp b/main/displays/qrcodedebug.cpp index cf9f0fa..1bb80ab 100644 --- a/main/displays/qrcodedebug.cpp +++ b/main/displays/qrcodedebug.cpp @@ -6,6 +6,7 @@ #include "displays/menus/debugmenu.h" #include "globals.h" #include "screenmanager.h" +#include "newsettings.h" using namespace espgui; @@ -31,7 +32,7 @@ void QrCodeDebugDisplay::buttonPressed(espgui::Button button) case Button::Right: { uint8_t qrcodeBytes[qrcode_getBufferSize(7)]; - qrcode_initText(&m_qrcode, qrcodeBytes, 7, ECC_MEDIUM, fmt::format("WIFI:T:WPA;S:{};P:{};", deviceName, stringSettings.ap_password).c_str()); + qrcode_initText(&m_qrcode, qrcodeBytes, 7, ECC_MEDIUM, fmt::format("WIFI:T:WPA;S:{};P:{};", configs.wifiApName.value, configs.wifiApKey.value).c_str()); for (uint8_t y = 0; y < m_qrcode.size; y++) { for (uint8_t x = 0; x < m_qrcode.size; x++) { diff --git a/main/displays/statusdisplay.cpp b/main/displays/statusdisplay.cpp index 91ae27b..a2f69c0 100644 --- a/main/displays/statusdisplay.cpp +++ b/main/displays/statusdisplay.cpp @@ -17,6 +17,7 @@ #include "udpcloud.h" #include "modes/defaultmode.h" #include "taskmanager.h" +#include "newsettings.h" using namespace std::chrono_literals; using namespace espgui; @@ -176,7 +177,7 @@ clearIp: } m_labelMode.redraw(currentMode->displayName()); - m_labelName.redraw(deviceName); + m_labelName.redraw(configs.wifiApName.value); const auto profile = settingsPersister.currentlyOpenProfileIndex(); m_labelProfile.redraw(profile ? std::to_string(*profile) : "-"); } diff --git a/main/globals.cpp b/main/globals.cpp index e11e925..a90cf92 100644 --- a/main/globals.cpp +++ b/main/globals.cpp @@ -16,8 +16,6 @@ float avgSpeedKmh{}; float sumCurrent{}; bool isLocked{}; -char deviceName[32] = STRING(DEVICE_PREFIX) "_ERR"; - bool simplified = #if defined(HAS_SIMPLIFIED) true diff --git a/main/globals.h b/main/globals.h index 12758ef..12dd799 100644 --- a/main/globals.h +++ b/main/globals.h @@ -42,7 +42,6 @@ extern float avgSpeed; extern float avgSpeedKmh; extern float sumCurrent; -extern char deviceName[32]; extern bool isLocked; #ifdef GLOBALS_PLUGIN diff --git a/main/main.cpp b/main/main.cpp index bffd7b2..9bd989a 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -66,12 +66,6 @@ extern "C" void app_main() else ESP_LOGE("BOBBY", "init() failed"); - bootLabel.redraw("deviceName"); - if (const auto result = wifi_stack::get_default_mac_addr()) - std::sprintf(deviceName, STRING(DEVICE_PREFIX) "_%02hhx%02hhx%02hhx", result->at(3), result->at(4), result->at(5)); - else - ESP_LOGE("MAIN", "get_default_mac_addr() failed: %.*s", result.error().size(), result.error().data()); - for (const auto &task : schedulerTasks) { bootLabel.redraw(task.name()); diff --git a/main/newsettings.cpp b/main/newsettings.cpp index ea029a2..a8e9e76 100644 --- a/main/newsettings.cpp +++ b/main/newsettings.cpp @@ -1,11 +1,24 @@ #include "newsettings.h" +// esp-idf includes +#include + // 3rdparty lib includes #include +#include +#include + +namespace { +//constexpr const char * const TAG = "CONFIG"; +} std::string defaultHostname() { - return "hallo"; + if (const auto result = wifi_stack::get_default_mac_addr()) + return fmt::format("bobby_{:02x}{:02x}{:02x}", result->at(3), result->at(4), result->at(5)); + else + ESP_LOGE(TAG, "get_default_mac_addr() failed: %.*s", result.error().size(), result.error().data()); + return "bobby"; } ConfigManager configs; diff --git a/main/newsettings.h b/main/newsettings.h index 0187466..c993995 100644 --- a/main/newsettings.h +++ b/main/newsettings.h @@ -73,13 +73,13 @@ public: ConfigWrapper wifiStaMinRssi {-90, DoReset, {}, "wifiStaMinRssi" }; ConfigWrapper wifiApEnabled {true, DoReset, {}, "wifiApEnabled" }; - ConfigWrapper factoryWifiApName {defaultHostname, NoReset, StringMinMaxSize<4, 32>, "facWifiApName" }; - ConfigWrapper wifiApName { factoryWifiApName, StringMinMaxSize<4, 32>, "wifiApName" }; - ConfigWrapper factoryWifiApKey {"Passwort_123", NoReset, StringOr>, "factWifiApKey" }; - ConfigWrapper wifiApKey { factoryWifiApKey, StringOr>, "wifiApKey" }; + ConfigWrapper wifiApName {defaultHostname, DoReset, StringMinMaxSize<4, 32>, "wifiApName" }; + ConfigWrapper wifiApKey {"Passwort_123", DoReset, StringOr>, "wifiApKey" }; ConfigWrapper wifiApChannel {1, DoReset, {}, "wifiApChannel" }; ConfigWrapper wifiApAuthmode{WIFI_AUTH_WPA2_PSK, DoReset, {}, "wifiApAuthmode" }; + ConfigWrapper bluetoothName {defaultHostname, DoReset, StringMinMaxSize<4, 32>, "bluetoothName" }; + #define NEW_SETTINGS(x) \ x(baseMacAddressOverride) \ x(hostname) \ @@ -189,12 +189,12 @@ public: x(wifiStaMinRssi) \ \ x(wifiApEnabled) \ - x(factoryWifiApName) \ x(wifiApName) \ - x(factoryWifiApKey) \ x(wifiApKey) \ x(wifiApChannel) \ - //x(wifiApAuthmode) + x(wifiApAuthmode) \ + \ + //x(bluetoothName) template void callForEveryConfig(T &&callback) @@ -202,7 +202,7 @@ public: #define HELPER(x) callback(x); NEW_SETTINGS(HELPER) #undef HELPER - callback(wifiApAuthmode); + callback(bluetoothName); } auto getAllConfigParams() @@ -211,7 +211,7 @@ public: #define HELPER(x) std::ref(x), NEW_SETTINGS(HELPER) #undef HELPER - std::ref(wifiApAuthmode) + std::ref(bluetoothName) ); } }; diff --git a/main/presets.cpp b/main/presets.cpp index 41ce45c..63b7ea3 100644 --- a/main/presets.cpp +++ b/main/presets.cpp @@ -4,24 +4,11 @@ namespace presets { StringSettings makeDefaultStringSettings() { - using ConfiguredWifi = StringSettings::ConfiguredWifi; #ifdef FEATURE_OTA using ConfiguredOtaServer = StringSettings::ConfiguredOtaServer; #endif return { - .wifis = std::array { - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} }, - ConfiguredWifi { .ssid = {}, .key = {} } - }, #ifdef FEATURE_CLOUD .cloudUrl = {}, #endif @@ -52,11 +39,6 @@ StringSettings makeDefaultStringSettings() }, .otaServerUrl = {}, #endif -#ifdef AP_PASSWORD - .ap_password = STRING(AP_PASSWORD), -#else - .ap_password = "Bobbycar_123", -#endif #ifdef FEATURE_OTA .otaServerBranch = {}, #endif diff --git a/main/presets.h b/main/presets.h index 4a2318c..377aa38 100644 --- a/main/presets.h +++ b/main/presets.h @@ -96,11 +96,6 @@ constexpr Settings::ControllerHardware mosfetsOffControllerHardware { #endif }; -constexpr Settings::WifiSettings defaultWifiSettings { - .wifiStaEnabled = true, - .wifiApEnabled = true -}; - #ifdef FEATURE_BLUETOOTH constexpr Settings::BluetoothSettings defaultBluetoothSettings { .autoBluetoothMode = BluetoothMode::Master @@ -321,7 +316,6 @@ constexpr Settings defaultSettings { #endif .buzzer = defaultBuzzer, .limits = defaultLimits, - .wifiSettings = defaultWifiSettings, #ifdef FEATURE_BLUETOOTH .bluetoothSettings = defaultBluetoothSettings, #endif diff --git a/main/settings.h b/main/settings.h index 7949a3c..8a46138 100644 --- a/main/settings.h +++ b/main/settings.h @@ -47,11 +47,6 @@ struct Settings int16_t phaseAdvMax; // [deg] Maximum Phase Advance angle (only for SIN). Higher angle results in higher maximum speed. } limits; - struct WifiSettings { - bool wifiStaEnabled; - bool wifiApEnabled; - } wifiSettings; - #ifdef FEATURE_BLUETOOTH struct BluetoothSettings { BluetoothMode autoBluetoothMode; @@ -254,9 +249,6 @@ void Settings::executeForEveryCommonSetting(T &&callable) callable("revBeepDur0", buzzer.reverseBeepDuration0); callable("revBeepDur1", buzzer.reverseBeepDuration1); - callable("wifiStaEnabled", wifiSettings.wifiStaEnabled); - callable("wifiApEnabled", wifiSettings.wifiApEnabled); - #ifdef FEATURE_BLUETOOTH callable("autoBluetoothMo", bluetoothSettings.autoBluetoothMode); #endif diff --git a/main/stringsettings.h b/main/stringsettings.h index e4da7f3..c2ceb65 100644 --- a/main/stringsettings.h +++ b/main/stringsettings.h @@ -6,13 +6,6 @@ struct StringSettings { - struct ConfiguredWifi { - std::string ssid; - std::string key; - }; - - std::array wifis; - #ifdef FEATURE_CLOUD std::string cloudUrl; #endif @@ -51,7 +44,6 @@ struct StringSettings #ifdef FEATURE_DNS_NS std::string dns_key; #endif - std::string ap_password; #ifdef FEATURE_OTA std::string otaServerBranch; #endif @@ -70,27 +62,6 @@ struct StringSettings template void StringSettings::executeForEveryCommonSetting(T &&callable) { - callable("ssid0", wifis[0].ssid); - callable("key0", wifis[0].key); - callable("ssid1", wifis[1].ssid); - callable("key1", wifis[1].key); - callable("ssid2", wifis[2].ssid); - callable("key2", wifis[2].key); - callable("ssid3", wifis[3].ssid); - callable("key3", wifis[3].key); - callable("ssid4", wifis[4].ssid); - callable("key4", wifis[4].key); - callable("ssid5", wifis[5].ssid); - callable("key5", wifis[5].key); - callable("ssid6", wifis[6].ssid); - callable("key6", wifis[6].key); - callable("ssid7", wifis[7].ssid); - callable("key7", wifis[7].key); - callable("ssid8", wifis[8].ssid); - callable("key8", wifis[8].key); - callable("ssid9", wifis[9].ssid); - callable("key9", wifis[9].key); - #ifdef FEATURE_CLOUD callable("cloudUrl", cloudUrl); #endif @@ -137,7 +108,6 @@ void StringSettings::executeForEveryCommonSetting(T &&callable) #ifdef FEATURE_DNS_NS callable("dnskey", dns_key); #endif - callable("ap_pw", ap_password); callable("webpw", webserver_password); #ifdef FEATURE_ESPNOW diff --git a/main/wifi_bobbycar.cpp b/main/wifi_bobbycar.cpp index 5cfa8b6..1e81d60 100644 --- a/main/wifi_bobbycar.cpp +++ b/main/wifi_bobbycar.cpp @@ -10,27 +10,28 @@ #include // local includes -#include "globals.h" +#include "newsettings.h" namespace { -wifi_stack::config wifi_create_config(); -std::optional wifi_create_sta_config(); -std::optional wifi_create_ap_config(); -} +wifi_stack::config createConfig(); +std::optional createStaConfig(); +wifi_stack::wifi_entry createWifiEntry(const WiFiConfig &wifi_config); +std::optional createApConfig(); +} // namespace void wifi_begin() { - wifi_stack::init(wifi_create_config()); + wifi_stack::init(createConfig()); } void wifi_update() { - wifi_stack::update(wifi_create_config()); + wifi_stack::update(createConfig()); } esp_err_t wifi_scan() { - const auto &sta_config = wifi_create_sta_config(); + const auto &sta_config = createStaConfig(); if (!sta_config) { ESP_LOGE("BOBBY", "no sta enabled"); @@ -47,63 +48,99 @@ esp_err_t wifi_scan() } namespace { -wifi_stack::config wifi_create_config() +wifi_stack::config createConfig() { return wifi_stack::config { - .sta = wifi_create_sta_config(), - .ap = wifi_create_ap_config(), - .country = wifi_country_t { - .cc = { 'A', 'T', '\0' }, - .schan = 1, - .nchan = 13, - .max_tx_power = 30, - .policy = WIFI_COUNTRY_POLICY_MANUAL - } + .base_mac_override = configs.baseMacAddressOverride.value, + //.dual_ant = dual_ant_config{}, + .sta = createStaConfig(), + .ap = createApConfig(), +#ifdef CONFIG_ETH_ENABLED + .eth = createEthConfig() +#endif }; } -std::optional wifi_create_sta_config() +std::optional createStaConfig() { - if (!settings.wifiSettings.wifiStaEnabled) + if (!configs.wifiStaEnabled.value) return std::nullopt; - return wifi_stack::sta_config { - .hostname = deviceName, + return wifi_stack::sta_config{ + .hostname = configs.hostname.value, .wifis = std::array { - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[0].ssid, .key = stringSettings.wifis[0].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[1].ssid, .key = stringSettings.wifis[1].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[2].ssid, .key = stringSettings.wifis[2].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[3].ssid, .key = stringSettings.wifis[3].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[4].ssid, .key = stringSettings.wifis[4].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[5].ssid, .key = stringSettings.wifis[5].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[6].ssid, .key = stringSettings.wifis[6].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[7].ssid, .key = stringSettings.wifis[7].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[8].ssid, .key = stringSettings.wifis[8].key }, - wifi_stack::wifi_entry { .ssid = stringSettings.wifis[9].ssid, .key = stringSettings.wifis[9].key } + createWifiEntry(configs.wifi_configs[0]), + createWifiEntry(configs.wifi_configs[1]), + createWifiEntry(configs.wifi_configs[2]), + createWifiEntry(configs.wifi_configs[3]), + createWifiEntry(configs.wifi_configs[4]), + createWifiEntry(configs.wifi_configs[5]), + createWifiEntry(configs.wifi_configs[6]), + createWifiEntry(configs.wifi_configs[7]), + createWifiEntry(configs.wifi_configs[8]), + createWifiEntry(configs.wifi_configs[9]) }, - .min_rssi = -90 + .min_rssi = configs.wifiStaMinRssi.value, + .long_range = false }; } -std::optional wifi_create_ap_config() +wifi_stack::wifi_entry createWifiEntry(const WiFiConfig &wifi_config) { - if (!settings.wifiSettings.wifiApEnabled) + std::optional static_ip; + if (wifi_config.useStaticIp.value) + static_ip = wifi_stack::static_ip_config { + .ip = wifi_config.staticIp.value, + .subnet = wifi_config.staticSubnet.value, + .gateway = wifi_config.staticGateway.value + }; + + wifi_stack::static_dns_config static_dns; + if (wifi_config.useStaticDns.value) + { + if (wifi_config.staticDns0.value.value()) + static_dns.main = wifi_config.staticDns0.value; + if (wifi_config.staticDns1.value.value()) + static_dns.backup = wifi_config.staticDns1.value; + if (wifi_config.staticDns2.value.value()) + static_dns.fallback = wifi_config.staticDns2.value; + } + + return wifi_stack::wifi_entry { + .ssid = wifi_config.ssid.value, + .key = wifi_config.key.value, + .static_ip = static_ip, + .static_dns = static_dns + }; +} + +std::optional createApConfig() +{ + if (!configs.wifiApEnabled.value) return std::nullopt; +// if (configs.wifiDisableApWhenOnline.value && +// cloudStarted && +// cloudConnected && +// lastCloudConnectedToggled && +// espchrono::ago(*lastCloudConnectedToggled) >= 30s) +// return std::nullopt; + return wifi_stack::ap_config { - .hostname = deviceName, - .ssid = deviceName, - .key = stringSettings.ap_password, + .hostname = configs.hostname.value, + .ssid = configs.wifiApName.value, + .key = configs.wifiApKey.value, .static_ip = { - .ip = {10, 0, 0, 1}, - .subnet = {255, 255, 255, 0}, - .gateway = {10, 0, 0, 1}, + .ip = apIP, + .subnet = netMsk, + .gateway = apIP }, - .channel = 1, - .authmode = WIFI_AUTH_WPA2_PSK, + .channel = configs.wifiApChannel.value, + .authmode = configs.wifiApAuthmode.value, .ssid_hidden = false, .max_connection = 4, - .beacon_interval = 100 + .beacon_interval = 100, + .long_range = false }; } } // namespace diff --git a/main/wifi_bobbycar.h b/main/wifi_bobbycar.h index c754338..9bad512 100644 --- a/main/wifi_bobbycar.h +++ b/main/wifi_bobbycar.h @@ -3,6 +3,12 @@ // esp-idf includes #include +// 3rdparty lib includes +#include + +constexpr const wifi_stack::ip_address_t apIP{10, 0, 0, 1}; +constexpr const wifi_stack::ip_address_t netMsk{255, 255, 255, 0}; + void wifi_begin(); void wifi_update();