Wifi from new settings

This commit is contained in:
2021-12-29 00:40:05 +01:00
parent 32d57e4f6a
commit 67249cfb2f
25 changed files with 188 additions and 205 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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<typename T>
@ -39,8 +40,16 @@ struct FieldWeakMaxAccessor : public RefAccessorSaveSettings<int16_t> { int16_t
struct PhaseAdvMaxAccessor : public RefAccessorSaveSettings<int16_t> { int16_t &getRef() const override { return settings.limits.phaseAdvMax; } };
// WiFi
struct WifiStaEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.wifiSettings.wifiStaEnabled; } };
struct WifiApEnabledAccessor : public RefAccessorSaveSettings<bool> { bool &getRef() const override { return settings.wifiSettings.wifiApEnabled; } };
struct WifiStaEnabledAccessor : public virtual espgui::AccessorInterface<bool>
{
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>
{
bool getValue() const override { return configs.wifiApEnabled.value; }
void setValue(bool value) override { configs.write_config(configs.wifiApEnabled, value); }
};
// Bluetooth
#ifdef FEATURE_BLUETOOTH

View File

@ -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

View File

@ -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

View File

@ -3,11 +3,46 @@
// esp-idf includes
#include <esp_log.h>
namespace {
constexpr const char * const TAG = "BOBBYBLE";
} // namespace
// 3rdparty lib includes
#include <ArduinoJson.h>
#include <wifi_bobbycar.h>
#include <futurecpp.h>
// 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<int16_t>();
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<int>();
ESP_LOGI(TAG, "[ble_config]: Set wifi%i: WiFi-SSID: %s, WiFi-Password: ***", doc["wifi_index"].as<int>(), doc["wifi_ssid"].as<const char*>());
stringSettings.wifis[index].ssid = doc["wifi_ssid"].as<std::string>();
stringSettings.wifis[index].key = doc["wifi_pass"].as<std::string>();
configs.write_config(configs.wifi_configs[index].ssid, doc["wifi_ssid"].as<std::string>());
configs.write_config(configs.wifi_configs[index].key, doc["wifi_pass"].as<std::string>());
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

View File

@ -1,24 +1,10 @@
#pragma once
// 3rdparty lib includes
#include <ArduinoJson.h>
#ifdef FEATURE_BLE
// esp-idf includes
#include <NimBLEDevice.h>
#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();

View File

@ -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++) {

View File

@ -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) : "-");
}

View File

@ -16,8 +16,6 @@ float avgSpeedKmh{};
float sumCurrent{};
bool isLocked{};
char deviceName[32] = STRING(DEVICE_PREFIX) "_ERR";
bool simplified =
#if defined(HAS_SIMPLIFIED)
true

View File

@ -42,7 +42,6 @@ extern float avgSpeed;
extern float avgSpeedKmh;
extern float sumCurrent;
extern char deviceName[32];
extern bool isLocked;
#ifdef GLOBALS_PLUGIN

View File

@ -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());

View File

@ -1,11 +1,24 @@
#include "newsettings.h"
// esp-idf includes
#include <esp_log.h>
// 3rdparty lib includes
#include <configmanager_priv.h>
#include <fmt/core.h>
#include <espwifistack.h>
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<ConfigContainer> configs;

View File

@ -73,13 +73,13 @@ public:
ConfigWrapper<int8_t> wifiStaMinRssi {-90, DoReset, {}, "wifiStaMinRssi" };
ConfigWrapper<bool> wifiApEnabled {true, DoReset, {}, "wifiApEnabled" };
ConfigWrapper<std::string> factoryWifiApName {defaultHostname, NoReset, StringMinMaxSize<4, 32>, "facWifiApName" };
ConfigWrapper<std::string> wifiApName { factoryWifiApName, StringMinMaxSize<4, 32>, "wifiApName" };
ConfigWrapper<std::string> factoryWifiApKey {"Passwort_123", NoReset, StringOr<StringEmpty, StringMinMaxSize<8, 64>>, "factWifiApKey" };
ConfigWrapper<std::string> wifiApKey { factoryWifiApKey, StringOr<StringEmpty, StringMinMaxSize<8, 64>>, "wifiApKey" };
ConfigWrapper<std::string> wifiApName {defaultHostname, DoReset, StringMinMaxSize<4, 32>, "wifiApName" };
ConfigWrapper<std::string> wifiApKey {"Passwort_123", DoReset, StringOr<StringEmpty, StringMinMaxSize<8, 64>>, "wifiApKey" };
ConfigWrapper<uint8_t> wifiApChannel {1, DoReset, {}, "wifiApChannel" };
ConfigWrapper<wifi_auth_mode_t> wifiApAuthmode{WIFI_AUTH_WPA2_PSK, DoReset, {}, "wifiApAuthmode" };
ConfigWrapper<std::string> 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<typename T>
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<ConfigWrapperInterface>(x),
NEW_SETTINGS(HELPER)
#undef HELPER
std::ref<ConfigWrapperInterface>(wifiApAuthmode)
std::ref<ConfigWrapperInterface>(bluetoothName)
);
}
};

View File

@ -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, 10> {
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

View File

@ -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

View File

@ -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

View File

@ -6,13 +6,6 @@
struct StringSettings
{
struct ConfiguredWifi {
std::string ssid;
std::string key;
};
std::array<ConfiguredWifi, 10> 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<typename T>
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

View File

@ -10,27 +10,28 @@
#include <espwifistack.h>
// local includes
#include "globals.h"
#include "newsettings.h"
namespace {
wifi_stack::config wifi_create_config();
std::optional<wifi_stack::sta_config> wifi_create_sta_config();
std::optional<wifi_stack::ap_config> wifi_create_ap_config();
}
wifi_stack::config createConfig();
std::optional<wifi_stack::sta_config> createStaConfig();
wifi_stack::wifi_entry createWifiEntry(const WiFiConfig &wifi_config);
std::optional<wifi_stack::ap_config> 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_stack::sta_config> wifi_create_sta_config()
std::optional<wifi_stack::sta_config> 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, 10> {
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_stack::ap_config> wifi_create_ap_config()
wifi_stack::wifi_entry createWifiEntry(const WiFiConfig &wifi_config)
{
if (!settings.wifiSettings.wifiApEnabled)
std::optional<wifi_stack::static_ip_config> 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<wifi_stack::ap_config> 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

View File

@ -3,6 +3,12 @@
// esp-idf includes
#include <esp_err.h>
// 3rdparty lib includes
#include <espwifiutils.h>
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();