Various fixes and improvements (#435)
- Fix for `sdkconfig` generation: the keys that contained the MCU type in its name weren't properly upper-cased. - Moved WiFi configuration property files to the user data path of the app instead of a fixed location. - Moved more properties from `device.py` to `sdkconfig/default.properties` - Fix for `device.cmake` device id parsing: separate basic property parsing from device id validation - Created internal `tt::service::wifi::findServiceContext()` - Changed Wi-Fi service id to lowercase (will change it for other services later)
This commit is contained in:
committed by
GitHub
parent
261796068e
commit
f48654d3dc
@@ -1,5 +1,8 @@
|
||||
#include "Tactility/service/wifi/Wifi.h"
|
||||
|
||||
#include <Tactility/service/ServiceManifest.h>
|
||||
#include <Tactility/service/ServiceRegistration.h>
|
||||
|
||||
namespace tt::service::wifi {
|
||||
|
||||
const char* radioStateToString(RadioState state) {
|
||||
@@ -21,4 +24,10 @@ const char* radioStateToString(RadioState state) {
|
||||
tt_crash("not implemented");
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest;
|
||||
|
||||
std::shared_ptr<ServiceContext> findServiceContext() {
|
||||
return findServiceContextById(manifest.id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#include "Tactility/service/wifi/WifiApSettings.h"
|
||||
#include "Tactility/file/PropertiesFile.h"
|
||||
#include <Tactility/service/wifi/WifiPrivate.h>
|
||||
#include <Tactility/service/wifi/WifiApSettings.h>
|
||||
#include <Tactility/file/PropertiesFile.h>
|
||||
|
||||
#include <Tactility/crypt/Crypt.h>
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <Tactility/service/ServicePaths.h>
|
||||
#include <format>
|
||||
#include <iomanip>
|
||||
#include <ranges>
|
||||
@@ -15,14 +16,13 @@ namespace tt::service::wifi::settings {
|
||||
|
||||
constexpr auto* TAG = "WifiApSettings";
|
||||
|
||||
constexpr auto* AP_SETTINGS_FORMAT = "/data/settings/{}.ap.properties";
|
||||
constexpr auto* AP_SETTINGS_FORMAT = "{}/{}.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;
|
||||
@@ -48,8 +48,9 @@ bool readHex(const std::string& input, uint8_t* buffer, int length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static std::string getApPropertiesFilePath(const std::string& ssid) {
|
||||
return std::format(AP_SETTINGS_FORMAT, ssid);
|
||||
// TODO: The SSID could contain invalid filename characters (e.g. "/", "\" and more) so we have to refactor this.
|
||||
static std::string getApPropertiesFilePath(std::shared_ptr<ServicePaths> paths, const std::string& ssid) {
|
||||
return std::format(AP_SETTINGS_FORMAT, paths->getUserDataDirectory(), ssid);
|
||||
}
|
||||
|
||||
static bool encrypt(const std::string& ssidInput, std::string& ssidOutput) {
|
||||
@@ -111,12 +112,20 @@ static bool decrypt(const std::string& ssidInput, std::string& ssidOutput) {
|
||||
}
|
||||
|
||||
bool contains(const std::string& ssid) {
|
||||
const auto file_path = getApPropertiesFilePath(ssid);
|
||||
auto service_context = findServiceContext();
|
||||
if (service_context == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const auto file_path = getApPropertiesFilePath(service_context->getPaths(), ssid);
|
||||
return file::isFile(file_path);
|
||||
}
|
||||
|
||||
bool load(const std::string& ssid, WifiApSettings& apSettings) {
|
||||
const auto file_path = getApPropertiesFilePath(ssid);
|
||||
auto service_context = findServiceContext();
|
||||
if (service_context == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const auto file_path = getApPropertiesFilePath(service_context->getPaths(), ssid);
|
||||
std::map<std::string, std::string> map;
|
||||
if (!file::loadPropertiesFile(file_path, map)) {
|
||||
return false;
|
||||
@@ -165,7 +174,12 @@ bool save(const WifiApSettings& apSettings) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto file_path = getApPropertiesFilePath(apSettings.ssid);
|
||||
auto service_context = findServiceContext();
|
||||
if (service_context == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto file_path = getApPropertiesFilePath(service_context->getPaths(), apSettings.ssid);
|
||||
|
||||
std::map<std::string, std::string> map;
|
||||
|
||||
@@ -187,7 +201,12 @@ bool save(const WifiApSettings& apSettings) {
|
||||
}
|
||||
|
||||
bool remove(const std::string& ssid) {
|
||||
const auto path = getApPropertiesFilePath(ssid);
|
||||
auto service_context = findServiceContext();
|
||||
if (service_context == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto path = getApPropertiesFilePath(service_context->getPaths(), ssid);
|
||||
if (!file::isFile(path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -959,7 +959,7 @@ public:
|
||||
};
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Wifi",
|
||||
.id = "wifi",
|
||||
.createService = create<WifiService>
|
||||
};
|
||||
|
||||
|
||||
@@ -153,7 +153,7 @@ public:
|
||||
};
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Wifi",
|
||||
.id = "wifi",
|
||||
.createService = create<WifiService>
|
||||
};
|
||||
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#include <Tactility/service/wifi/WifiSettings.h>
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/file/File.h>
|
||||
#include <Tactility/file/PropertiesFile.h>
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/service/ServicePaths.h>
|
||||
#include <Tactility/service/wifi/WifiPrivate.h>
|
||||
|
||||
namespace tt::service::wifi::settings {
|
||||
|
||||
constexpr auto* TAG = "WifiSettings";
|
||||
constexpr auto* SETTINGS_FILE = "/data/settings/wifi.properties";
|
||||
constexpr auto* SETTINGS_KEY_ENABLE_ON_BOOT = "enableOnBoot";
|
||||
|
||||
struct WifiSettings {
|
||||
@@ -21,8 +22,14 @@ static WifiSettings cachedSettings {
|
||||
static bool cached = false;
|
||||
|
||||
static bool load(WifiSettings& settings) {
|
||||
auto service_context = findServiceContext();
|
||||
if (service_context == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> map;
|
||||
if (!file::loadPropertiesFile(SETTINGS_FILE, map)) {
|
||||
std::string settings_path = service_context->getPaths()->getUserDataPath("settings.properties");
|
||||
if (!file::loadPropertiesFile(settings_path, map)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -36,15 +43,20 @@ static bool load(WifiSettings& settings) {
|
||||
}
|
||||
|
||||
static bool save(const WifiSettings& settings) {
|
||||
auto service_context = findServiceContext();
|
||||
if (service_context == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::map<std::string, std::string> map;
|
||||
map[SETTINGS_KEY_ENABLE_ON_BOOT] = settings.enableOnBoot ? "true" : "false";
|
||||
return file::savePropertiesFile(SETTINGS_FILE, map);
|
||||
std::string settings_path = service_context->getPaths()->getUserDataPath("settings.properties");
|
||||
return file::savePropertiesFile(settings_path, map);
|
||||
}
|
||||
|
||||
WifiSettings getCachedOrLoad() {
|
||||
if (!cached) {
|
||||
if (!load(cachedSettings)) {
|
||||
TT_LOG_E(TAG, "Failed to load %s", SETTINGS_FILE);
|
||||
TT_LOG_E(TAG, "Failed to load");
|
||||
} else {
|
||||
cached = true;
|
||||
}
|
||||
@@ -56,7 +68,7 @@ WifiSettings getCachedOrLoad() {
|
||||
void setEnableOnBoot(bool enable) {
|
||||
cachedSettings.enableOnBoot = enable;
|
||||
if (!save(cachedSettings)) {
|
||||
TT_LOG_E(TAG, "Failed to save %s", SETTINGS_FILE);
|
||||
TT_LOG_E(TAG, "Failed to save");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user