Merge develop into main (#304)
## New - Read property files with `PropertiesFile` - Support `boot.properties` so the user can specify the launcher app and an optional app to start after the launcher finishes. (see `BootProperties.cpp`) - Create registry for CPU affinity and update code to make use of it - `AppRegistration` and `ServiceRegistration` now also ensure that the `/data` directories always exist for all apps - `Notes` is now the default app for opening text files. `TextViewer` is removed entirely. Created `tt::app::notes::start(path)` function. - WiFi settings moved from NVS to properties file. - Specify `*.ap.properties` file on the SD card for automatic WiFi settings import on start-up. - Added `file::getLock(path)` and `file::withLock(path, function)` to do safe file operations on SD cards ## Improvements - Update TinyUSB to `1.7.6~1` - Improved `Boot.cpp` code. General code quality fixes and some restructuring to improve readability. - `tt::string` functionality improvements - Rename `AppRegistry` to `AppRegistration` - Rename `ServiceRegistry` to `ServiceRegistration` - Cleanup in `Notes.cpp` - `FileTest.cpp` fix for PC - Created `TestFile` helper class for tests, which automatically deletes files after the test. - Renamed `Partitions.h` to `MountPoints.h` - Created `std::string getMountPoints()` function for easy re-use - Other code quality improvements - `SdCardDevice`'s `getState()` and `isMounted()` now have a timeout argument ## Fixes - ELF loading now has a lock so to avoid a bug when 2 ELF apps are loaded in parallel
This commit is contained in:
committed by
GitHub
parent
fbaff8cbac
commit
ee5a5a7181
@@ -0,0 +1,190 @@
|
||||
#include "Tactility/service/wifi/WifiApSettings.h"
|
||||
#include "Tactility/file/PropertiesFile.h"
|
||||
|
||||
#include <Tactility/crypt/Crypt.h>
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <iomanip>
|
||||
#include <ranges>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
constexpr auto* TAG = "WifiApSettings";
|
||||
|
||||
constexpr auto* AP_SETTINGS_FORMAT = "/data/service/Wifi/{}.ap.properties";
|
||||
|
||||
constexpr auto* AP_PROPERTIES_KEY_SSID = "ssid";
|
||||
constexpr auto* AP_PROPERTIES_KEY_PASSWORD = "password";
|
||||
constexpr auto* AP_PROPERTIES_KEY_AUTO_CONNECT = "autoConnect";
|
||||
constexpr auto* AP_PROPERTIES_KEY_CHANNEL = "channel";
|
||||
|
||||
|
||||
std::string toHexString(const uint8_t *data, int length) {
|
||||
std::stringstream stream;
|
||||
stream << std::hex;
|
||||
for( int i(0) ; i < length; ++i )
|
||||
stream << std::setw(2) << std::setfill('0') << static_cast<int>(data[i]);
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
bool readHex(const std::string& input, uint8_t* buffer, int length) {
|
||||
if (input.size() / 2 != length) {
|
||||
TT_LOG_E(TAG, "readHex() length mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
char hex[3] = { 0 };
|
||||
for (int i = 0; i < length; i++) {
|
||||
hex[0] = input[i * 2];
|
||||
hex[1] = input[i * 2 + 1];
|
||||
char* endptr;
|
||||
buffer[i] = static_cast<uint8_t>(strtoul(hex, &endptr, 16));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::string getApPropertiesFilePath(const std::string& ssid) {
|
||||
return std::format(AP_SETTINGS_FORMAT, ssid);
|
||||
}
|
||||
|
||||
static bool encrypt(const std::string& ssidInput, std::string& ssidOutput) {
|
||||
uint8_t iv[16];
|
||||
const auto length = ssidInput.size();
|
||||
constexpr size_t chunk_size = 16;
|
||||
const auto encrypted_length = ((length / chunk_size) + (length % chunk_size ? 1 : 0)) * chunk_size;
|
||||
|
||||
auto* buffer = static_cast<uint8_t*>(malloc(encrypted_length));
|
||||
|
||||
crypt::getIv(ssidInput.c_str(), ssidInput.size(), iv);
|
||||
if (crypt::encrypt(iv, reinterpret_cast<const uint8_t*>(ssidInput.c_str()), buffer, encrypted_length) != 0) {
|
||||
TT_LOG_E(TAG, "Failed to encrypt");
|
||||
free(buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
ssidOutput = toHexString(buffer, encrypted_length);
|
||||
free(buffer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool decrypt(const std::string& ssidInput, std::string& ssidOutput) {
|
||||
assert(!ssidInput.empty());
|
||||
assert(ssidInput.size() % 2 == 0);
|
||||
auto* data = static_cast<uint8_t*>(malloc(ssidInput.size() / 2));
|
||||
if (!readHex(ssidInput, data, ssidInput.size() / 2)) {
|
||||
TT_LOG_E(TAG, "Failed to read hex");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t iv[16];
|
||||
crypt::getIv(ssidInput.c_str(), ssidInput.size(), iv);
|
||||
|
||||
auto result_length = ssidInput.size() / 2;
|
||||
// Allocate correct length plus space for string null terminator
|
||||
auto* result = static_cast<uint8_t*>(malloc(result_length + 1));
|
||||
result[result_length] = 0;
|
||||
|
||||
int decrypt_result = crypt::decrypt(
|
||||
iv,
|
||||
data,
|
||||
result,
|
||||
ssidInput.size() / 2
|
||||
);
|
||||
|
||||
free(data);
|
||||
|
||||
if (decrypt_result != 0) {
|
||||
TT_LOG_E(TAG, "Failed to decrypt credentials for \"%s\": %d", ssidInput.c_str(), decrypt_result);
|
||||
free(result);
|
||||
return false;
|
||||
}
|
||||
|
||||
ssidOutput = reinterpret_cast<char*>(result);
|
||||
free(result);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool contains(const std::string& ssid) {
|
||||
const auto file_path = getApPropertiesFilePath(ssid);
|
||||
return file::isFile(file_path);
|
||||
}
|
||||
|
||||
bool load(const std::string& ssid, WifiApSettings& apSettings) {
|
||||
const auto file_path = getApPropertiesFilePath(ssid);
|
||||
std::map<std::string, std::string> map;
|
||||
if (!file::loadPropertiesFile(file_path, map)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// SSID is required
|
||||
if (!map.contains(AP_PROPERTIES_KEY_SSID)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
apSettings.ssid = map[AP_PROPERTIES_KEY_SSID];
|
||||
assert(ssid == apSettings.ssid);
|
||||
|
||||
if (map.contains(AP_PROPERTIES_KEY_PASSWORD)) {
|
||||
std::string password_decrypted;
|
||||
if (decrypt(map[AP_PROPERTIES_KEY_PASSWORD], password_decrypted)) {
|
||||
apSettings.password = password_decrypted;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
apSettings.password = "";
|
||||
}
|
||||
|
||||
if (map.contains(AP_PROPERTIES_KEY_AUTO_CONNECT)) {
|
||||
apSettings.autoConnect = (map[AP_PROPERTIES_KEY_AUTO_CONNECT] == "true");
|
||||
} else {
|
||||
apSettings.autoConnect = true;
|
||||
}
|
||||
|
||||
if (map.contains(AP_PROPERTIES_KEY_CHANNEL)) {
|
||||
apSettings.channel = std::stoi(map[AP_PROPERTIES_KEY_CHANNEL].c_str());
|
||||
} else {
|
||||
apSettings.channel = 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool save(const WifiApSettings& apSettings) {
|
||||
if (apSettings.ssid.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto file_path = getApPropertiesFilePath(apSettings.ssid);
|
||||
|
||||
std::map<std::string, std::string> map;
|
||||
|
||||
std::string password_encrypted;
|
||||
if (!encrypt(apSettings.password, password_encrypted)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
map[AP_PROPERTIES_KEY_PASSWORD] = password_encrypted;
|
||||
map[AP_PROPERTIES_KEY_SSID] = apSettings.ssid;
|
||||
map[AP_PROPERTIES_KEY_AUTO_CONNECT] = apSettings.autoConnect ? "true" : "false";
|
||||
map[AP_PROPERTIES_KEY_CHANNEL] = std::to_string(apSettings.channel);
|
||||
|
||||
return file::savePropertiesFile(file_path, map);
|
||||
}
|
||||
|
||||
bool remove(const std::string& ssid) {
|
||||
const auto path = getApPropertiesFilePath(ssid);
|
||||
if (!file::isFile(path)) {
|
||||
return false;
|
||||
}
|
||||
return ::remove(path.c_str()) == 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
#include "Tactility/service/wifi/WifiBootSplashInit.h"
|
||||
#include "Tactility/file/PropertiesFile.h"
|
||||
|
||||
#include <Tactility/file/File.h>
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/service/wifi/WifiApSettings.h>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <format>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <Tactility/hal/sdcard/SdCardDevice.h>
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
constexpr auto* TAG = "WifiBootSplashInit";
|
||||
|
||||
constexpr auto* AP_PROPERTIES_KEY_SSID = "ssid";
|
||||
constexpr auto* AP_PROPERTIES_KEY_PASSWORD = "password";
|
||||
constexpr auto* AP_PROPERTIES_KEY_AUTO_CONNECT = "autoConnect";
|
||||
constexpr auto* AP_PROPERTIES_KEY_CHANNEL = "channel";
|
||||
constexpr auto* AP_PROPERTIES_KEY_AUTO_REMOVE = "autoRemovePropertiesFile";
|
||||
|
||||
struct ApProperties {
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
bool autoConnect;
|
||||
int32_t channel;
|
||||
bool autoRemovePropertiesFile;
|
||||
};
|
||||
|
||||
static void importWifiAp(const std::string& filePath) {
|
||||
std::map<std::string, std::string> map;
|
||||
if (!file::loadPropertiesFile(filePath, map)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto ssid_iterator = map.find(AP_PROPERTIES_KEY_SSID);
|
||||
if (ssid_iterator == map.end()) {
|
||||
TT_LOG_E(TAG, "%s is missing ssid", filePath.c_str());
|
||||
return;
|
||||
}
|
||||
const auto ssid = ssid_iterator->second;
|
||||
|
||||
if (!settings::contains(ssid)) {
|
||||
const auto password_iterator = map.find(AP_PROPERTIES_KEY_PASSWORD);
|
||||
const auto password = password_iterator == map.end() ? "" : password_iterator->second;
|
||||
|
||||
const auto auto_connect_iterator = map.find(AP_PROPERTIES_KEY_AUTO_CONNECT);
|
||||
const auto auto_connect = auto_connect_iterator == map.end() ? true : (auto_connect_iterator->second == "true");
|
||||
|
||||
const auto channel_iterator = map.find(AP_PROPERTIES_KEY_CHANNEL);
|
||||
const auto channel = channel_iterator == map.end() ? 0 : std::stoi(channel_iterator->second);
|
||||
|
||||
settings::WifiApSettings settings(
|
||||
ssid,
|
||||
password,
|
||||
auto_connect,
|
||||
channel
|
||||
);
|
||||
|
||||
if (!settings::save(settings)) {
|
||||
TT_LOG_E(TAG, "Failed to save settings for %s", ssid.c_str());
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Imported %s from %s", ssid.c_str(), filePath.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
const auto auto_remove_iterator = map.find(AP_PROPERTIES_KEY_AUTO_REMOVE);
|
||||
if (auto_remove_iterator != map.end() && auto_remove_iterator->second == "true") {
|
||||
if (!remove(filePath.c_str())) {
|
||||
TT_LOG_E(TAG, "Failed to auto-remove %s", filePath.c_str());
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Auto-removed %s", filePath.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void importWifiApSettings(std::shared_ptr<hal::sdcard::SdCardDevice> sdcard) {
|
||||
// auto lock = sdcard->getLock()->asScopedLock();
|
||||
// lock.lock();
|
||||
auto path = sdcard->getMountPath();
|
||||
|
||||
std::vector<dirent> dirent_list;
|
||||
if (file::scandir(path, dirent_list, [](const dirent* entry) {
|
||||
switch (entry->d_type) {
|
||||
case file::TT_DT_DIR:
|
||||
case file::TT_DT_CHR:
|
||||
case file::TT_DT_LNK:
|
||||
return -1;
|
||||
case file::TT_DT_REG:
|
||||
default: {
|
||||
std::string name = entry->d_name;
|
||||
if (name.ends_with(".ap.properties")) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}, nullptr) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (dirent_list.empty()) {
|
||||
TT_LOG_W(TAG, "No AP files found at %s", sdcard->getMountPath().c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto& dirent : dirent_list) {
|
||||
std::string absolute_path = std::format("{}/{}", path, dirent.d_name);
|
||||
importWifiAp(absolute_path);
|
||||
}
|
||||
}
|
||||
|
||||
void bootSplashInit() {
|
||||
const auto sdcards = hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
|
||||
for (auto& sdcard : sdcards) {
|
||||
if (sdcard->isMounted()) {
|
||||
importWifiApSettings(sdcard);
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Skipping unmounted SD card %s", sdcard->getMountPath().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,19 +1,20 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include <lwip/esp_netif_net_stack.h>
|
||||
#include "Tactility/service/wifi/Wifi.h"
|
||||
|
||||
#include "Tactility/TactilityHeadless.h"
|
||||
#include "Tactility/service/ServiceContext.h"
|
||||
#include "Tactility/service/wifi/WifiGlobals.h"
|
||||
#include "Tactility/service/wifi/WifiSettings.h"
|
||||
#include "Tactility/service/wifi/WifiBootSplashInit.h"
|
||||
|
||||
#include <Tactility/kernel/SystemEvents.h>
|
||||
#include <Tactility/Timer.h>
|
||||
|
||||
#include <lwip/esp_netif_net_stack.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
#include <Tactility/kernel/SystemEvents.h>
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
namespace tt::service::wifi {
|
||||
@@ -36,8 +37,6 @@ static void dispatchDisconnectButKeepActive(std::shared_ptr<Wifi> wifi);
|
||||
|
||||
class Wifi {
|
||||
|
||||
private:
|
||||
|
||||
std::atomic<RadioState> radio_state = RadioState::Off;
|
||||
bool scan_active = false;
|
||||
bool secure_connection = false;
|
||||
@@ -66,14 +65,11 @@ public:
|
||||
esp_event_handler_instance_t event_handler_any_id = nullptr;
|
||||
esp_event_handler_instance_t event_handler_got_ip = nullptr;
|
||||
EventFlag connection_wait_flags;
|
||||
settings::WifiApSettings connection_target = {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = false
|
||||
};
|
||||
settings::WifiApSettings connection_target;
|
||||
bool pause_auto_connect = false; // Pause when manually disconnecting until manually connecting again
|
||||
bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not
|
||||
esp_netif_ip_info_t ip_info;
|
||||
kernel::SystemEventSubscription bootEventSubscription = kernel::NoSystemEventSubscription;
|
||||
|
||||
RadioState getRadioState() const {
|
||||
auto lock = dataMutex.asScopedLock();
|
||||
@@ -187,8 +183,8 @@ bool isScanning() {
|
||||
}
|
||||
}
|
||||
|
||||
void connect(const settings::WifiApSettings* ap, bool remember) {
|
||||
TT_LOG_I(TAG, "connect(%s, %d)", ap->ssid, remember);
|
||||
void connect(const settings::WifiApSettings& ap, bool remember) {
|
||||
TT_LOG_I(TAG, "connect(%s, %d)", ap.ssid.c_str(), remember);
|
||||
auto wifi = wifi_singleton;
|
||||
if (wifi == nullptr) {
|
||||
return;
|
||||
@@ -201,14 +197,14 @@ void connect(const settings::WifiApSettings* ap, bool remember) {
|
||||
|
||||
// Manual connect (e.g. via app) should stop auto-connecting until the connection is established
|
||||
wifi->pause_auto_connect = true;
|
||||
memcpy(&wifi->connection_target, ap, sizeof(settings::WifiApSettings));
|
||||
wifi->connection_target = ap;
|
||||
wifi->connection_target_remember = remember;
|
||||
|
||||
if (wifi->getRadioState() == RadioState::Off) {
|
||||
getMainDispatcher().dispatch([wifi]() { dispatchEnable(wifi); });
|
||||
getMainDispatcher().dispatch([wifi] { dispatchEnable(wifi); });
|
||||
}
|
||||
|
||||
getMainDispatcher().dispatch([wifi]() { dispatchConnect(wifi); });
|
||||
getMainDispatcher().dispatch([wifi] { dispatchConnect(wifi); });
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
@@ -223,11 +219,7 @@ void disconnect() {
|
||||
return;
|
||||
}
|
||||
|
||||
wifi->connection_target = (settings::WifiApSettings) {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = false
|
||||
};
|
||||
wifi->connection_target = settings::WifiApSettings("", "");
|
||||
// Manual disconnect (e.g. via app) should stop auto-connecting until a new connection is established
|
||||
wifi->pause_auto_connect = true;
|
||||
getMainDispatcher().dispatch([wifi]() { dispatchDisconnectButKeepActive(wifi); });
|
||||
@@ -307,9 +299,9 @@ void setEnabled(bool enabled) {
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
getMainDispatcher().dispatch([wifi]() { dispatchEnable(wifi); });
|
||||
getMainDispatcher().dispatch([wifi] { dispatchEnable(wifi); });
|
||||
} else {
|
||||
getMainDispatcher().dispatch([wifi]() { dispatchDisable(wifi); });
|
||||
getMainDispatcher().dispatch([wifi] { dispatchDisable(wifi); });
|
||||
}
|
||||
|
||||
wifi->pause_auto_connect = false;
|
||||
@@ -431,8 +423,8 @@ static bool find_auto_connect_ap(std::shared_ptr<Wifi> wifi, settings::WifiApSet
|
||||
auto ssid = reinterpret_cast<const char*>(wifi->scan_list[i].ssid);
|
||||
if (settings::contains(ssid)) {
|
||||
static_assert(sizeof(wifi->scan_list[i].ssid) == (TT_WIFI_SSID_LIMIT + 1), "SSID size mismatch");
|
||||
if (settings::load(ssid, &settings)) {
|
||||
if (settings.auto_connect) {
|
||||
if (settings::load(ssid, settings)) {
|
||||
if (settings.autoConnect) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
@@ -451,8 +443,8 @@ static void dispatchAutoConnect(std::shared_ptr<Wifi> wifi) {
|
||||
|
||||
settings::WifiApSettings settings;
|
||||
if (find_auto_connect_ap(wifi, settings)) {
|
||||
TT_LOG_I(TAG, "Auto-connecting to %s", settings.ssid);
|
||||
connect(&settings, false);
|
||||
TT_LOG_I(TAG, "Auto-connecting to %s", settings.ssid.c_str());
|
||||
connect(settings, false);
|
||||
// TODO: We currently have to manually reset it because connect() sets it.
|
||||
// connect() assumes it's only being called by the user and not internally, so it disables auto-connect
|
||||
wifi->pause_auto_connect = false;
|
||||
@@ -726,7 +718,7 @@ static void dispatchConnect(std::shared_ptr<Wifi> wifi) {
|
||||
return;
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid);
|
||||
TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid.c_str());
|
||||
|
||||
// Stop radio first, if needed
|
||||
RadioState radio_state = wifi->getRadioState();
|
||||
@@ -758,11 +750,10 @@ static void dispatchConnect(std::shared_ptr<Wifi> wifi) {
|
||||
config.sta.threshold.rssi = -127;
|
||||
config.sta.pmf_cfg.capable = true;
|
||||
|
||||
static_assert(sizeof(config.sta.ssid) == (sizeof(wifi_singleton->connection_target.ssid)-1), "SSID size mismatch");
|
||||
memcpy(config.sta.ssid, wifi_singleton->connection_target.ssid, sizeof(config.sta.ssid));
|
||||
memcpy(config.sta.ssid, wifi_singleton->connection_target.ssid.c_str(), wifi_singleton->connection_target.ssid.size());
|
||||
|
||||
if (wifi_singleton->connection_target.password[0] != 0x00) {
|
||||
memcpy(config.sta.password, wifi_singleton->connection_target.password, sizeof(config.sta.password));
|
||||
memcpy(config.sta.password, wifi_singleton->connection_target.password.c_str(), wifi_singleton->connection_target.password.size());
|
||||
config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
|
||||
}
|
||||
|
||||
@@ -794,9 +785,9 @@ static void dispatchConnect(std::shared_ptr<Wifi> wifi) {
|
||||
wifi->setSecureConnection(config.sta.password[0] != 0x00U);
|
||||
wifi->setRadioState(RadioState::ConnectionActive);
|
||||
publish_event_simple(wifi, EventType::ConnectionSuccess);
|
||||
TT_LOG_I(TAG, "Connected to %s", wifi->connection_target.ssid);
|
||||
TT_LOG_I(TAG, "Connected to %s", wifi->connection_target.ssid.c_str());
|
||||
if (wifi->connection_target_remember) {
|
||||
if (!settings::save(&wifi->connection_target)) {
|
||||
if (!settings::save(wifi->connection_target)) {
|
||||
TT_LOG_E(TAG, "Failed to store credentials");
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Stored credentials");
|
||||
@@ -805,7 +796,7 @@ static void dispatchConnect(std::shared_ptr<Wifi> wifi) {
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::ConnectionFailed);
|
||||
TT_LOG_I(TAG, "Failed to connect to %s", wifi->connection_target.ssid);
|
||||
TT_LOG_I(TAG, "Failed to connect to %s", wifi->connection_target.ssid.c_str());
|
||||
} else {
|
||||
wifi->setRadioState(RadioState::On);
|
||||
publish_event_simple(wifi, EventType::ConnectionFailed);
|
||||
@@ -911,13 +902,17 @@ public:
|
||||
assert(wifi_singleton == nullptr);
|
||||
wifi_singleton = std::make_shared<Wifi>();
|
||||
|
||||
wifi_singleton->bootEventSubscription = kernel::subscribeSystemEvent(kernel::SystemEvent::BootSplash, [](auto) {
|
||||
bootSplashInit();
|
||||
});
|
||||
|
||||
wifi_singleton->autoConnectTimer = std::make_unique<Timer>(Timer::Type::Periodic, []() { onAutoConnectTimer(); });
|
||||
// We want to try and scan more often in case of startup or scan lock failure
|
||||
wifi_singleton->autoConnectTimer->start(std::min(2000, AUTO_SCAN_INTERVAL));
|
||||
|
||||
if (settings::shouldEnableOnBoot()) {
|
||||
TT_LOG_I(TAG, "Auto-enabling due to setting");
|
||||
getMainDispatcher().dispatch([]() { dispatchEnable(wifi_singleton); });
|
||||
getMainDispatcher().dispatch([] { dispatchEnable(wifi_singleton); });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ bool isScanning() {
|
||||
return wifi->scan_active;
|
||||
}
|
||||
|
||||
void connect(const settings::WifiApSettings* ap, bool remember) {
|
||||
void connect(const settings::WifiApSettings& ap, bool remember) {
|
||||
assert(wifi);
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
@@ -1,18 +1,54 @@
|
||||
#include "Tactility/service/wifi/WifiSettings.h"
|
||||
#include "Tactility/Preferences.h"
|
||||
#include "Tactility/file/PropertiesFile.h"
|
||||
|
||||
#define WIFI_PREFERENCES_NAMESPACE "wifi"
|
||||
#define WIFI_PREFERENCES_KEY_ENABLE_ON_BOOT "enable_on_boot"
|
||||
#include <Tactility/LogEsp.h>
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
constexpr auto* TAG = "WifiSettings";
|
||||
constexpr auto* SETTINGS_FILE = "/data/service/Wifi/wifi.properties";
|
||||
constexpr auto* SETTINGS_KEY_ENABLE_ON_BOOT = "enableOnBoot";
|
||||
|
||||
struct WifiProperties {
|
||||
bool enableOnBoot;
|
||||
};
|
||||
|
||||
static bool load(WifiProperties& properties) {
|
||||
std::map<std::string, std::string> map;
|
||||
if (!file::loadPropertiesFile(SETTINGS_FILE, map)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!map.contains(SETTINGS_KEY_ENABLE_ON_BOOT)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto enable_on_boot_string = map[SETTINGS_KEY_ENABLE_ON_BOOT];
|
||||
properties.enableOnBoot = (enable_on_boot_string == "true");
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool save(const WifiProperties& properties) {
|
||||
std::map<std::string, std::string> map;
|
||||
map[SETTINGS_KEY_ENABLE_ON_BOOT] = properties.enableOnBoot ? "true" : "false";
|
||||
return file::savePropertiesFile(SETTINGS_FILE, map);
|
||||
}
|
||||
|
||||
void setEnableOnBoot(bool enable) {
|
||||
Preferences(WIFI_PREFERENCES_NAMESPACE).putBool(WIFI_PREFERENCES_KEY_ENABLE_ON_BOOT, enable);
|
||||
WifiProperties properties { .enableOnBoot = enable };
|
||||
if (!save(properties)) {
|
||||
TT_LOG_E(TAG, "Failed to save %s", SETTINGS_FILE);
|
||||
}
|
||||
}
|
||||
|
||||
bool shouldEnableOnBoot() {
|
||||
bool enable = false;
|
||||
Preferences(WIFI_PREFERENCES_NAMESPACE).optBool(WIFI_PREFERENCES_KEY_ENABLE_ON_BOOT, enable);
|
||||
return enable;
|
||||
WifiProperties properties;
|
||||
if (!load(properties)) {
|
||||
return false;
|
||||
}
|
||||
return properties.enableOnBoot;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1,148 +0,0 @@
|
||||
#ifdef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/service/wifi/WifiGlobals.h"
|
||||
#include "Tactility/service/wifi/WifiSettings.h"
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/crypt/Hash.h>
|
||||
#include <Tactility/crypt/Crypt.h>
|
||||
|
||||
#include <nvs_flash.h>
|
||||
#include <cstring>
|
||||
|
||||
#define TAG "wifi_settings"
|
||||
#define TT_NVS_NAMESPACE "wifi_settings" // limited by NVS_KEY_NAME_MAX_SIZE
|
||||
|
||||
// region Wi-Fi Credentials - static
|
||||
|
||||
static esp_err_t credentials_nvs_open(nvs_handle_t* handle, nvs_open_mode_t mode) {
|
||||
return nvs_open(TT_NVS_NAMESPACE, NVS_READWRITE, handle);
|
||||
}
|
||||
|
||||
static void credentials_nvs_close(nvs_handle_t handle) {
|
||||
nvs_close(handle);
|
||||
}
|
||||
|
||||
// endregion Wi-Fi Credentials - static
|
||||
|
||||
// region Wi-Fi Credentials - public
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
bool contains(const char* ssid) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = credentials_nvs_open(&handle, NVS_READONLY);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool key_exists = nvs_find_key(handle, ssid, nullptr) == ESP_OK;
|
||||
credentials_nvs_close(handle);
|
||||
|
||||
return key_exists;
|
||||
}
|
||||
|
||||
bool load(const char* ssid, WifiApSettings* settings) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = credentials_nvs_open(&handle, NVS_READONLY);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
WifiApSettings encrypted_settings;
|
||||
size_t length = sizeof(WifiApSettings);
|
||||
result = nvs_get_blob(handle, ssid, &encrypted_settings, &length);
|
||||
|
||||
uint8_t iv[16];
|
||||
crypt::getIv(ssid, strlen(ssid), iv);
|
||||
int decrypt_result = crypt::decrypt(
|
||||
iv,
|
||||
(uint8_t*)encrypted_settings.password,
|
||||
(uint8_t*)settings->password,
|
||||
TT_WIFI_CREDENTIALS_PASSWORD_LIMIT
|
||||
);
|
||||
// Manually ensure null termination, because encryption must be a multiple of 16 bytes
|
||||
encrypted_settings.password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT] = 0;
|
||||
|
||||
if (decrypt_result != 0) {
|
||||
result = ESP_FAIL;
|
||||
TT_LOG_E(TAG, "Failed to decrypt credentials for \"%s\": %d", ssid, decrypt_result);
|
||||
}
|
||||
|
||||
if (result != ESP_OK && result != ESP_ERR_NVS_NOT_FOUND) {
|
||||
TT_LOG_E(TAG, "Failed to get credentials for \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
}
|
||||
|
||||
credentials_nvs_close(handle);
|
||||
|
||||
settings->auto_connect = encrypted_settings.auto_connect;
|
||||
strcpy((char*)settings->ssid, encrypted_settings.ssid);
|
||||
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool save(const WifiApSettings* settings) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = credentials_nvs_open(&handle, NVS_READWRITE);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
WifiApSettings encrypted_settings = {
|
||||
.ssid = { 0 },
|
||||
.password = { 0 },
|
||||
.auto_connect = settings->auto_connect,
|
||||
};
|
||||
strcpy((char*)encrypted_settings.ssid, settings->ssid);
|
||||
// We only decrypt multiples of 16, so we have to ensure the last byte is set to 0
|
||||
encrypted_settings.password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT] = 0;
|
||||
|
||||
uint8_t iv[16];
|
||||
crypt::getIv(settings->ssid, strlen(settings->ssid), iv);
|
||||
int encrypt_result = crypt::encrypt(
|
||||
iv,
|
||||
(uint8_t*)settings->password,
|
||||
(uint8_t*)encrypted_settings.password,
|
||||
TT_WIFI_CREDENTIALS_PASSWORD_LIMIT
|
||||
);
|
||||
|
||||
if (encrypt_result != 0) {
|
||||
result = ESP_FAIL;
|
||||
TT_LOG_E(TAG, "Failed to encrypt credentials \"%s\": %d", settings->ssid, encrypt_result);
|
||||
}
|
||||
|
||||
if (result == ESP_OK) {
|
||||
result = nvs_set_blob(handle, settings->ssid, &encrypted_settings, sizeof(WifiApSettings));
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to get credentials for \"%s\": %s", settings->ssid, esp_err_to_name(result));
|
||||
}
|
||||
}
|
||||
|
||||
credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
bool remove(const char* ssid) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t result = credentials_nvs_open(&handle, NVS_READWRITE);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to open NVS handle to store \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
|
||||
result = nvs_erase_key(handle, ssid);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to erase credentials for \"%s\": %s", ssid, esp_err_to_name(result));
|
||||
}
|
||||
|
||||
credentials_nvs_close(handle);
|
||||
return result == ESP_OK;
|
||||
}
|
||||
|
||||
// end region Wi-Fi Credentials - public
|
||||
|
||||
} // nemespace
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
@@ -1,27 +0,0 @@
|
||||
#ifndef ESP_PLATFORM
|
||||
|
||||
#include "Tactility/service/wifi/WifiSettings.h"
|
||||
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
#define TAG "wifi_settings_mock"
|
||||
|
||||
bool contains(const char* ssid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool load(const char* ssid, WifiApSettings* settings) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool save(const WifiApSettings* settings) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool remove(const char* ssid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // ESP_PLATFORM
|
||||
Reference in New Issue
Block a user