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:
Ken Van Hoeylandt
2025-08-23 17:10:18 +02:00
committed by GitHub
parent fbaff8cbac
commit ee5a5a7181
109 changed files with 1396 additions and 744 deletions
+48
View File
@@ -0,0 +1,48 @@
#include "Tactility/BootProperties.h"
#include "Tactility/MountPoints.h"
#include "Tactility/file/PropertiesFile.h"
#include "Tactility/hal/sdcard/SdCardDevice.h"
#include <Tactility/Log.h>
#include <Tactility/file/File.h>
#include <cassert>
#include <format>
#include <string>
#include <vector>
namespace tt {
constexpr auto* TAG = "BootProperties";
constexpr auto* PROPERTIES_FILE_FORMAT = "{}/boot.properties";
constexpr auto* PROPERTIES_KEY_LAUNCHER_APP_ID = "launcherAppId";
constexpr auto* PROPERTIES_KEY_AUTO_START_APP_ID = "autoStartAppId";
static std::string getPropertiesFilePath() {
const auto sdcards = hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
for (auto& sdcard : sdcards) {
std::string path = std::format(PROPERTIES_FILE_FORMAT, sdcard->getMountPath());
if (file::isFile(path)) {
return path;
}
}
return std::format(PROPERTIES_FILE_FORMAT, file::MOUNT_POINT_DATA);
}
bool loadBootProperties(BootProperties& properties) {
const std::string path = getPropertiesFilePath();
if (!file::loadPropertiesFile(path, [&properties](auto& key, auto& value) {
if (key == PROPERTIES_KEY_AUTO_START_APP_ID) {
properties.autoStartAppId = value;
} else if (key == PROPERTIES_KEY_LAUNCHER_APP_ID) {
properties.launcherAppId = value;
}
})) {
TT_LOG_E(TAG, "Failed to load %s", path.c_str());
return false;
}
return !properties.launcherAppId.empty();
}
}
+55
View File
@@ -0,0 +1,55 @@
#include "Tactility/MountPoints.h"
#include "Tactility/hal/Device.h"
#include "Tactility/hal/sdcard/SdCardDevice.h"
#include <Tactility/file/File.h>
#include <cstring>
#include <vector>
#include <dirent.h>
namespace tt::file {
std::vector<dirent> getMountPoints() {
std::vector<dirent> dir_entries;
dir_entries.clear();
// System partition
auto system_dirent = dirent{
.d_ino = 0,
.d_type = TT_DT_DIR,
.d_name = { 0 }
};
strcpy(system_dirent.d_name, SYSTEM_PARTITION_NAME);
dir_entries.push_back(system_dirent);
// Data partition
auto data_dirent = dirent{
.d_ino = 1,
.d_type = TT_DT_DIR,
.d_name = { 0 }
};
strcpy(data_dirent.d_name, DATA_PARTITION_NAME);
dir_entries.push_back(data_dirent);
// SD card partitions
auto sdcards = tt::hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
for (auto& sdcard : sdcards) {
auto state = sdcard->getState();
if (state == hal::sdcard::SdCardDevice::State::Mounted) {
auto mount_name = sdcard->getMountPath().substr(1);
auto dir_entry = dirent {
.d_ino = 2,
.d_type = TT_DT_DIR,
.d_name = { 0 }
};
assert(mount_name.length() < sizeof(dirent::d_name));
strcpy(dir_entry.d_name, mount_name.c_str());
dir_entries.push_back(dir_entry);
}
}
return dir_entries;
}
}
+2 -4
View File
@@ -1,11 +1,11 @@
#include "Tactility/Tactility.h"
#include "Tactility/app/ManifestRegistry.h"
#include "Tactility/app/AppRegistration.h"
#include "Tactility/lvgl/LvglPrivate.h"
#include "Tactility/service/ServiceManifest.h"
#include <Tactility/TactilityHeadless.h>
#include <Tactility/service/ServiceRegistry.h>
#include <Tactility/service/ServiceRegistration.h>
#include <Tactility/service/loader/Loader.h>
namespace tt {
@@ -54,7 +54,6 @@ namespace app {
namespace serialconsole { extern const AppManifest manifest; }
namespace settings { extern const AppManifest manifest; }
namespace systeminfo { extern const AppManifest manifest; }
namespace textviewer { extern const AppManifest manifest; }
namespace timedatesettings { extern const AppManifest manifest; }
namespace timezone { extern const AppManifest manifest; }
namespace usbsettings { extern const AppManifest manifest; }
@@ -96,7 +95,6 @@ static void registerSystemApps() {
addApp(app::settings::manifest);
addApp(app::selectiondialog::manifest);
addApp(app::systeminfo::manifest);
addApp(app::textviewer::manifest);
addApp(app::timedatesettings::manifest);
addApp(app::timezone::manifest);
addApp(app::usbsettings::manifest);
+1 -1
View File
@@ -3,7 +3,7 @@
#include "Tactility/hal/Hal_i.h"
#include "Tactility/network/NtpPrivate.h"
#include "Tactility/service/ServiceManifest.h"
#include "Tactility/service/ServiceRegistry.h"
#include "Tactility/service/ServiceRegistration.h"
#include <Tactility/Dispatcher.h>
#include <Tactility/time/TimePrivate.h>
+9 -9
View File
@@ -1,6 +1,6 @@
#include "Tactility/app/AppInstancePaths.h"
#include <Tactility/Partitions.h>
#include <Tactility/MountPoints.h>
#define LVGL_PATH_PREFIX std::string("A:/")
#ifdef ESP_PLATFORM
@@ -12,38 +12,38 @@
namespace tt::app {
std::string AppInstancePaths::getDataDirectory() const {
return PARTITION_PREFIX + DATA_PARTITION_NAME + "/app/" + manifest.id;
return PARTITION_PREFIX + file::DATA_PARTITION_NAME + "/app/" + manifest.id;
}
std::string AppInstancePaths::getDataDirectoryLvgl() const {
return LVGL_PATH_PREFIX + DATA_PARTITION_NAME + "/app/" + manifest.id;
return LVGL_PATH_PREFIX + file::DATA_PARTITION_NAME + "/app/" + manifest.id;
}
std::string AppInstancePaths::getDataPath(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
return PARTITION_PREFIX + DATA_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
return PARTITION_PREFIX + file::DATA_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
}
std::string AppInstancePaths::getDataPathLvgl(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
return LVGL_PATH_PREFIX + DATA_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
return LVGL_PATH_PREFIX + file::DATA_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
}
std::string AppInstancePaths::getSystemDirectory() const {
return PARTITION_PREFIX + SYSTEM_PARTITION_NAME + "/app/" + manifest.id;
return PARTITION_PREFIX + file::SYSTEM_PARTITION_NAME + "/app/" + manifest.id;
}
std::string AppInstancePaths::getSystemDirectoryLvgl() const {
return LVGL_PATH_PREFIX + SYSTEM_PARTITION_NAME + "/app/" + manifest.id;
return LVGL_PATH_PREFIX + file::SYSTEM_PARTITION_NAME + "/app/" + manifest.id;
}
std::string AppInstancePaths::getSystemPath(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
return PARTITION_PREFIX + SYSTEM_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
return PARTITION_PREFIX + file::SYSTEM_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
}
std::string AppInstancePaths::getSystemPathLvgl(const std::string& childPath) const {
return LVGL_PATH_PREFIX + SYSTEM_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
return LVGL_PATH_PREFIX + file::SYSTEM_PARTITION_NAME + "/app/" + manifest.id + '/' + childPath;
}
}
@@ -1,9 +1,10 @@
#include "Tactility/app/ManifestRegistry.h"
#include "Tactility/app/AppRegistration.h"
#include "Tactility/app/AppManifest.h"
#include <Tactility/Mutex.h>
#include <unordered_map>
#include <Tactility/file/File.h>
#define TAG "app"
+20 -8
View File
@@ -2,9 +2,9 @@
#include "Tactility/app/ElfApp.h"
#include "Tactility/file/File.h"
#include "Tactility/file/FileLock.h"
#include "Tactility/service/loader/Loader.h"
#include "Tactility/hal/sdcard/SdCardDevice.h"
#include <Tactility/Log.h>
#include <Tactility/StringUtils.h>
@@ -15,7 +15,7 @@
namespace tt::app {
#define TAG "elf_app"
constexpr auto* TAG = "ElfApp";
struct ElfManifest {
/** The user-readable name of the app. Used in UI. */
@@ -33,6 +33,7 @@ struct ElfManifest {
static size_t elfManifestSetCount = 0;
static ElfManifest elfManifest;
static std::shared_ptr<Lock> elfManifestLock = std::make_shared<Mutex>();
class ElfApp : public App {
@@ -48,7 +49,7 @@ class ElfApp : public App {
assert(elfFileData == nullptr);
size_t size = 0;
hal::sdcard::withSdCardLock<void>(filePath, [this, &size](){
file::withLock<void>(filePath, [this, &size]{
elfFileData = file::readBinary(filePath, size);
});
@@ -56,25 +57,30 @@ class ElfApp : public App {
return false;
}
if (esp_elf_init(&elf) < 0) {
if (esp_elf_init(&elf) != ESP_OK) {
TT_LOG_E(TAG, "Failed to initialize");
shouldCleanupElf = true;
elfFileData = nullptr;
return false;
}
if (esp_elf_relocate(&elf, elfFileData.get()) < 0) {
if (esp_elf_relocate(&elf, elfFileData.get()) != ESP_OK) {
TT_LOG_E(TAG, "Failed to load executable");
esp_elf_deinit(&elf);
elfFileData = nullptr;
return false;
}
int argc = 0;
char* argv[] = {};
if (esp_elf_request(&elf, 0, argc, argv) < 0) {
if (esp_elf_request(&elf, 0, argc, argv) != ESP_OK) {
TT_LOG_W(TAG, "Executable returned error code");
esp_elf_deinit(&elf);
elfFileData = nullptr;
return false;
}
shouldCleanupElf = true;
return true;
}
@@ -95,10 +101,16 @@ public:
explicit ElfApp(std::string filePath) : filePath(std::move(filePath)) {}
void onCreate(AppContext& appContext) override {
// Because we use global variables, we have to ensure that we are not starting 2 apps in parallel
// We use a ScopedLock so we don't have to safeguard all branches
auto lock = elfManifestLock->asScopedLock();
lock.lock();
auto initial_count = elfManifestSetCount;
if (startElf()) {
if (elfManifestSetCount > initial_count) {
manifest = std::make_unique<ElfManifest>(elfManifest);
lock.unlock();
if (manifest->createData != nullptr) {
data = manifest->createData();
@@ -181,7 +193,7 @@ void registerElfApp(const std::string& filePath) {
if (findAppById(filePath) == nullptr) {
auto manifest = AppManifest {
.id = getElfAppId(filePath),
.name = tt::string::removeFileExtension(tt::string::getLastPathSegment(filePath)),
.name = string::removeFileExtension(string::getLastPathSegment(filePath)),
.type = Type::User,
.location = Location::external(filePath)
};
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Tactility/app/ManifestRegistry.h"
#include "Tactility/app/AppRegistration.h"
#include "Tactility/service/loader/Loader.h"
#include "Tactility/lvgl/Toolbar.h"
+57 -34
View File
@@ -11,6 +11,8 @@
#include <Tactility/kernel/SystemEvents.h>
#include <lvgl.h>
#include <Tactility/BootProperties.h>
#include <Tactility/CpuAffinity.h>
#ifdef ESP_PLATFORM
#include "Tactility/app/crashdiagnostics/CrashDiagnostics.h"
@@ -24,26 +26,25 @@
namespace tt::app::boot {
static std::shared_ptr<tt::hal::display::DisplayDevice> getHalDisplay() {
static std::shared_ptr<hal::display::DisplayDevice> getHalDisplay() {
return hal::findFirstDevice<hal::display::DisplayDevice>(hal::Device::Type::Display);
}
class BootApp : public App {
private:
Thread thread = Thread(
"boot",
4096,
[] { return bootThreadCallback(); },
getCpuAffinityConfiguration().system
);
Thread thread = Thread("boot", 4096, [this]() { return bootThreadCallback(); });
int32_t bootThreadCallback() {
TickType_t start_time = kernel::getTicks();
kernel::publishSystemEvent(kernel::SystemEvent::BootSplash);
auto hal_display = getHalDisplay();
static void setupDisplay() {
const auto hal_display = getHalDisplay();
assert(hal_display != nullptr);
if (hal_display->supportsBacklightDuty()) {
uint8_t backlight_duty = 200;
app::display::getBacklightDuty(backlight_duty);
display::getBacklightDuty(backlight_duty);
TT_LOG_I(TAG, "backlight %du", backlight_duty);
hal_display->setBacklightDuty(backlight_duty);
} else {
@@ -52,27 +53,45 @@ private:
if (hal_display->getGammaCurveCount() > 0) {
uint8_t gamma_curve;
if (app::display::getGammaCurve(gamma_curve)) {
if (display::getGammaCurve(gamma_curve)) {
hal_display->setGammaCurve(gamma_curve);
TT_LOG_I(TAG, "gamma %du", gamma_curve);
}
}
}
if (hal::usb::isUsbBootMode()) {
TT_LOG_I(TAG, "Rebooting into mass storage device mode");
hal::usb::resetUsbBootMode();
hal::usb::startMassStorageWithSdmmc();
} else {
static bool setupUsbBootMode() {
if (!hal::usb::isUsbBootMode()) {
return false;
}
TT_LOG_I(TAG, "Rebooting into mass storage device mode");
hal::usb::resetUsbBootMode();
hal::usb::startMassStorageWithSdmmc();
return true;
}
static void waitForMinimalSplashDuration(TickType_t startTime) {
const auto end_time = kernel::getTicks();
const auto ticks_passed = end_time - startTime;
constexpr auto minimum_ticks = (CONFIG_TT_SPLASH_DURATION / portTICK_PERIOD_MS);
if (minimum_ticks > ticks_passed) {
kernel::delayTicks(minimum_ticks - ticks_passed);
}
}
static int32_t bootThreadCallback() {
const auto start_time = kernel::getTicks();
kernel::publishSystemEvent(kernel::SystemEvent::BootSplash);
setupDisplay();
if (!setupUsbBootMode()) {
initFromBootApp();
TickType_t end_time = tt::kernel::getTicks();
TickType_t ticks_passed = end_time - start_time;
TickType_t minimum_ticks = (CONFIG_TT_SPLASH_DURATION / portTICK_PERIOD_MS);
if (minimum_ticks > ticks_passed) {
kernel::delayTicks(minimum_ticks - ticks_passed);
}
tt::service::loader::stopApp();
waitForMinimalSplashDuration(start_time);
service::loader::stopApp();
startNextApp();
}
@@ -81,16 +100,20 @@ private:
static void startNextApp() {
#ifdef ESP_PLATFORM
esp_reset_reason_t reason = esp_reset_reason();
if (reason == ESP_RST_PANIC) {
app::crashdiagnostics::start();
if (esp_reset_reason() == ESP_RST_PANIC) {
crashdiagnostics::start();
return;
}
#endif
auto* config = tt::getConfiguration();
assert(!config->launcherAppId.empty());
tt::service::loader::startApp(config->launcherAppId);
BootProperties boot_properties;
if (!loadBootProperties(boot_properties) || boot_properties.launcherAppId.empty()) {
TT_LOG_E(TAG, "Launcher not configured");
stop();
return;
}
service::loader::startApp(boot_properties.launcherAppId);
}
public:
@@ -99,9 +122,9 @@ public:
auto* image = lv_image_create(parent);
lv_obj_set_size(image, LV_PCT(100), LV_PCT(100));
auto paths = app.getPaths();
const auto paths = app.getPaths();
const char* logo = hal::usb::isUsbBootMode() ? "logo_usb.png" : "logo.png";
auto logo_path = paths->getSystemPathLvgl(logo);
const auto logo_path = paths->getSystemPathLvgl(logo);
TT_LOG_I(TAG, "%s", logo_path.c_str());
lv_image_set_src(image, logo_path.c_str());
@@ -9,7 +9,7 @@
namespace tt::app::filebrowser {
#define TAG "filebrowser_app"
constexpr auto* TAG = "FileBrowser";
extern const AppManifest manifest;
+9 -35
View File
@@ -3,7 +3,7 @@
#include <Tactility/file/File.h>
#include "Tactility/hal/sdcard/SdCardDevice.h"
#include <Tactility/Log.h>
#include <Tactility/Partitions.h>
#include <Tactility/MountPoints.h>
#include <Tactility/kernel/Kernel.h>
#include <cstring>
@@ -11,10 +11,10 @@
#include <vector>
#include <dirent.h>
#define TAG "filebrowser_app"
namespace tt::app::filebrowser {
constexpr auto* TAG = "FileBrowser";
State::State() {
if (kernel::getPlatform() == kernel::PlatformSimulator) {
char cwd[PATH_MAX];
@@ -43,46 +43,20 @@ bool State::setEntriesForPath(const std::string& path) {
TT_LOG_I(TAG, "Changing path: %s -> %s", current_path.c_str(), path.c_str());
/**
* ESP32 does not have a root directory, so we have to create it manually.
* We'll add the NVS Flash partitions and the binding for the sdcard.
* On PC, the root entry point ("/") is a folder.
* On ESP32, the root entry point contains the various mount points.
*/
bool show_custom_root = (kernel::getPlatform() == kernel::PlatformEsp) && (path == "/");
if (show_custom_root) {
bool get_mount_points = (kernel::getPlatform() == kernel::PlatformEsp) && (path == "/");
if (get_mount_points) {
TT_LOG_I(TAG, "Setting custom root");
dir_entries.clear();
dir_entries.push_back(dirent{
.d_ino = 0,
.d_type = file::TT_DT_DIR,
.d_name = SYSTEM_PARTITION_NAME
});
dir_entries.push_back(dirent{
.d_ino = 1,
.d_type = file::TT_DT_DIR,
.d_name = DATA_PARTITION_NAME
});
auto sdcards = tt::hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
for (auto& sdcard : sdcards) {
auto state = sdcard->getState();
if (state == hal::sdcard::SdCardDevice::State::Mounted) {
auto mount_name = sdcard->getMountPath().substr(1);
auto dir_entry = dirent {
.d_ino = 2,
.d_type = file::TT_DT_DIR,
.d_name = { 0 }
};
assert(mount_name.length() < sizeof(dirent::d_name));
strcpy(dir_entry.d_name, mount_name.c_str());
dir_entries.push_back(dir_entry);
}
}
dir_entries = file::getMountPoints();
current_path = path;
selected_child_entry = "";
action = ActionNone;
return true;
} else {
dir_entries.clear();
// TODO: file Lock
int count = file::scandir(path, dir_entries, &file::direntFilterDotEntries, file::direntSortAlphaAndType);
if (count >= 0) {
TT_LOG_I(TAG, "%s has %u entries", path.c_str(), count);
@@ -3,7 +3,7 @@
namespace tt::app::filebrowser {
#define TAG "filebrowser_app"
constexpr auto* TAG = "FileBrowser";
bool isSupportedExecutableFile(const std::string& filename) {
#ifdef ESP_PLATFORM
+7 -7
View File
@@ -4,13 +4,14 @@
#include "Tactility/app/alertdialog/AlertDialog.h"
#include "Tactility/app/imageviewer/ImageViewer.h"
#include "Tactility/app/inputdialog/InputDialog.h"
#include "Tactility/app/textviewer/TextViewer.h"
#include "Tactility/app/notes/Notes.h"
#include "Tactility/app/ElfApp.h"
#include "Tactility/lvgl/Toolbar.h"
#include "Tactility/lvgl/LvglSync.h"
#include <Tactility/Tactility.h>
#include "Tactility/file/File.h"
#include <Tactility/file/File.h>
#include <Tactility/Log.h>
#include <Tactility/StringUtils.h>
#include <cstring>
@@ -20,10 +21,10 @@
#include "Tactility/service/loader/Loader.h"
#endif
#define TAG "filebrowser_app"
namespace tt::app::filebrowser {
constexpr auto* TAG = "FileBrowser";
// region Callbacks
static void dirEntryListScrollBeginCallback(lv_event_t* event) {
@@ -95,10 +96,10 @@ void View::viewFile(const std::string& path, const std::string& filename) {
imageviewer::start(processed_filepath);
} else if (isSupportedTextFile(filename)) {
if (kernel::getPlatform() == kernel::PlatformEsp) {
textviewer::start(processed_filepath);
notes::start(processed_filepath);
} else {
// Remove forward slash, because we need a relative path
textviewer::start(processed_filepath.substr(1));
notes::start(processed_filepath.substr(1));
}
} else {
TT_LOG_W(TAG, "opening files of this type is not supported");
@@ -163,7 +164,6 @@ void View::onDirEntryLongPressed(int32_t index) {
}
}
void View::createDirEntryWidget(lv_obj_t* list, dirent& dir_entry) {
tt_check(list);
const char* symbol;
@@ -10,7 +10,7 @@
namespace tt::app::fileselection {
#define TAG "fileselection_app"
constexpr auto* TAG = "FileSelection";
extern const AppManifest manifest;
+4 -31
View File
@@ -3,7 +3,7 @@
#include <Tactility/file/File.h>
#include "Tactility/hal/sdcard/SdCardDevice.h"
#include <Tactility/Log.h>
#include <Tactility/Partitions.h>
#include <Tactility/MountPoints.h>
#include <Tactility/kernel/Kernel.h>
#include <cstring>
@@ -11,10 +11,10 @@
#include <vector>
#include <dirent.h>
#define TAG "fileselection_app"
namespace tt::app::fileselection {
constexpr auto* TAG = "FileSelection";
State::State() {
if (kernel::getPlatform() == kernel::PlatformSimulator) {
char cwd[PATH_MAX];
@@ -49,34 +49,7 @@ bool State::setEntriesForPath(const std::string& path) {
bool show_custom_root = (kernel::getPlatform() == kernel::PlatformEsp) && (path == "/");
if (show_custom_root) {
TT_LOG_I(TAG, "Setting custom root");
dir_entries.clear();
dir_entries.push_back(dirent{
.d_ino = 0,
.d_type = file::TT_DT_DIR,
.d_name = SYSTEM_PARTITION_NAME
});
dir_entries.push_back(dirent{
.d_ino = 1,
.d_type = file::TT_DT_DIR,
.d_name = DATA_PARTITION_NAME
});
auto sdcards = tt::hal::findDevices<hal::sdcard::SdCardDevice>(hal::Device::Type::SdCard);
for (auto& sdcard : sdcards) {
auto state = sdcard->getState();
if (state == hal::sdcard::SdCardDevice::State::Mounted) {
auto mount_name = sdcard->getMountPath().substr(1);
auto dir_entry = dirent {
.d_ino = 2,
.d_type = file::TT_DT_DIR,
.d_name = { 0 }
};
assert(mount_name.length() < sizeof(dirent::d_name));
strcpy(dir_entry.d_name, mount_name.c_str());
dir_entries.push_back(dir_entry);
}
}
dir_entries = file::getMountPoints();
current_path = path;
selected_child_entry = "";
return true;
+2 -2
View File
@@ -15,10 +15,10 @@
#include "Tactility/service/loader/Loader.h"
#endif
#define TAG "fileselection_app"
namespace tt::app::fileselection {
constexpr auto* TAG = "FileSelection";
// region Callbacks
static void onDirEntryPressedCallback(lv_event_t* event) {
+1 -1
View File
@@ -148,7 +148,7 @@ void GpioApp::onShow(AppContext& app, lv_obj_t* parent) {
// Add the GPIO number after the last item on a row
auto* postfix = lv_label_create(row_wrapper);
lv_label_set_text_fmt(postfix, "%02d", i);
lv_obj_set_pos(postfix, (int32_t)((column+1) * x_spacing + offset_from_left_label), 0);
lv_obj_set_pos(postfix, (column + 1) * x_spacing + offset_from_left_label, 0);
// Add a new row wrapper underneath the last one
auto* new_row_wrapper = createGpioRowWrapper(wrapper);
@@ -24,8 +24,6 @@ extern const AppManifest manifest;
class I2cScannerApp : public App {
private:
// Core
Mutex mutex = Mutex(Mutex::Type::Recursive);
std::unique_ptr<Timer> scanTimer = nullptr;
@@ -286,7 +284,7 @@ void I2cScannerApp::startScanning() {
lv_obj_clean(scanListWidget);
scanState = ScanStateScanning;
scanTimer = std::make_unique<Timer>(Timer::Type::Once, [](){
scanTimer = std::make_unique<Timer>(Timer::Type::Once, []{
onScanTimerCallback();
});
scanTimer->start(10);
+6 -6
View File
@@ -1,11 +1,11 @@
#include "Tactility/app/AppContext.h"
#include "Tactility/app/ManifestRegistry.h"
#include "Tactility/app/AppRegistration.h"
#include "Tactility/service/loader/Loader.h"
#include <Tactility/Check.h>
#include <Tactility/Tactility.h>
#include <lvgl.h>
#include <Tactility/BootProperties.h>
#define TAG "launcher"
@@ -54,10 +54,10 @@ static lv_obj_t* createAppButton(lv_obj_t* parent, const char* title, const char
class LauncherApp : public App {
void onCreate(TT_UNUSED AppContext& app) override {
auto* config = getConfiguration();
if (!config->autoStartAppId.empty()) {
TT_LOG_I(TAG, "auto-starting %s", config->autoStartAppId.c_str());
service::loader::startApp(config->autoStartAppId);
BootProperties boot_properties;
if (loadBootProperties(boot_properties) && !boot_properties.autoStartAppId.empty()) {
TT_LOG_I(TAG, "Starting %s", boot_properties.autoStartAppId.c_str());
service::loader::startApp(boot_properties.autoStartAppId);
}
}
+31 -16
View File
@@ -1,17 +1,19 @@
#include <Tactility/app/AppManifest.h>
#include <Tactility/file/File.h>
#include <Tactility/lvgl/Keyboard.h>
#include <Tactility/lvgl/Toolbar.h>
#include <Tactility/Assets.h>
#include <lvgl.h>
#include "Tactility/app/AppManifest.h"
#include "Tactility/app/fileselection/FileSelection.h"
#include "Tactility/file/FileLock.h"
#include "Tactility/lvgl/Toolbar.h"
#include "Tactility/lvgl/LvglSync.h"
#include "Tactility/service/loader/Loader.h"
#include "Tactility/Assets.h"
#include <Tactility/app/fileselection/FileSelection.h>
#include <Tactility/hal/sdcard/SdCardDevice.h>
#include <Tactility/lvgl/LvglSync.h>
#include <Tactility/file/File.h>
#include <lvgl.h>
namespace tt::app::notes {
constexpr const char* TAG = "Notes";
constexpr auto* TAG = "Notes";
constexpr auto* NOTES_FILE_ARGUMENT = "file";
class NotesApp : public App {
@@ -81,7 +83,7 @@ class NotesApp : public App {
void openFile(const std::string& path) {
// We might be reading from the SD card, which could share a SPI bus with other devices (display)
hal::sdcard::withSdCardLock<void>(path, [this, path]() {
file::withLock<void>(path, [this, path] {
auto data = file::readString(path);
if (data != nullptr) {
auto lock = lvgl::getSyncLock()->asScopedLock();
@@ -96,7 +98,7 @@ class NotesApp : public App {
bool saveFile(const std::string& path) {
// We might be writing to SD card, which could share a SPI bus with other devices (display)
return hal::sdcard::withSdCardLock<bool>(path, [this, path]() {
return file::withLock<bool>(path, [this, path] {
if (file::writeString(path, saveBuffer.c_str())) {
TT_LOG_I(TAG, "Saved to %s", path.c_str());
filePath = path;
@@ -109,11 +111,20 @@ class NotesApp : public App {
#pragma endregion Open_Events_Functions
void onCreate(AppContext& appContext) override {
auto parameters = appContext.getParameters();
std::string file_path;
if (parameters != nullptr && parameters->optString(NOTES_FILE_ARGUMENT, file_path)) {
if (!file_path.empty()) {
filePath = file_path;
}
}
}
void onShow(AppContext& context, lv_obj_t* parent) override {
lv_obj_remove_flag(parent, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
lv_obj_t* toolbar = tt::lvgl::toolbar_create(parent, context);
lv_obj_t* toolbar = lvgl::toolbar_create(parent, context);
lv_obj_align(toolbar, LV_ALIGN_TOP_MID, 0, 0);
uiDropDownMenu = lv_dropdown_create(toolbar);
@@ -168,9 +179,8 @@ class NotesApp : public App {
lv_label_set_text(uiCurrentFileName, "Untitled");
lv_obj_align(uiCurrentFileName, LV_ALIGN_CENTER, 0, 0);
//TODO: Move this to SD Card?
if (!file::findOrCreateDirectory(context.getPaths()->getDataDirectory(), 0777)) {
TT_LOG_E(TAG, "Failed to find or create path %s", context.getPaths()->getDataDirectory().c_str());
if (!filePath.empty()) {
openFile(filePath);
}
}
@@ -202,4 +212,9 @@ extern const AppManifest manifest = {
.createApp = create<NotesApp>
};
void start(const std::string& filePath) {
auto parameters = std::make_shared<Bundle>();
parameters->putString(NOTES_FILE_ARGUMENT, filePath);
service::loader::startApp(manifest.id, parameters);
}
} // namespace tt::app::notes
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Tactility/app/ManifestRegistry.h"
#include "Tactility/app/AppRegistration.h"
#include "Tactility/lvgl/Toolbar.h"
#include "Tactility/service/loader/Loader.h"
@@ -1,61 +0,0 @@
#include "Tactility/lvgl/LabelUtils.h"
#include "Tactility/lvgl/Style.h"
#include "Tactility/lvgl/Toolbar.h"
#include "Tactility/service/loader/Loader.h"
#include <Tactility/TactilityCore.h>
#include <lvgl.h>
#define TAG "text_viewer"
#define TEXT_VIEWER_FILE_ARGUMENT "file"
namespace tt::app::textviewer {
class TextViewerApp : public App {
void onShow(AppContext& app, lv_obj_t* parent) override {
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
lvgl::toolbar_create(parent, app);
auto* wrapper = lv_obj_create(parent);
lv_obj_set_width(wrapper, LV_PCT(100));
lv_obj_set_flex_grow(wrapper, 1);
lv_obj_set_flex_flow(wrapper, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_all(wrapper, 0, 0);
lvgl::obj_set_style_bg_invisible(wrapper);
auto* label = lv_label_create(wrapper);
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
auto parameters = app.getParameters();
tt_check(parameters != nullptr, "Parameters missing");
bool success = false;
std::string file_argument;
if (parameters->optString(TEXT_VIEWER_FILE_ARGUMENT, file_argument)) {
TT_LOG_I(TAG, "Opening %s", file_argument.c_str());
if (lvgl::label_set_text_file(label, file_argument.c_str())) {
success = true;
}
}
if (!success) {
lv_label_set_text_fmt(label, "Failed to load %s", file_argument.c_str());
}
}
};
extern const AppManifest manifest = {
.id = "TextViewer",
.name = "Text Viewer",
.type = Type::Hidden,
.createApp = create<TextViewerApp>
};
void start(const std::string& file) {
auto parameters = std::make_shared<Bundle>();
parameters->putString(TEXT_VIEWER_FILE_ARGUMENT, file);
service::loader::startApp(manifest.id, parameters);
}
} // namespace
+7 -8
View File
@@ -5,7 +5,7 @@
#include "Tactility/lvgl/LvglSync.h"
#include "Tactility/service/loader/Loader.h"
#include <Tactility/Partitions.h>
#include <Tactility/MountPoints.h>
#include <Tactility/StringUtils.h>
#include <Tactility/Timer.h>
@@ -14,7 +14,7 @@
namespace tt::app::timezone {
#define TAG "timezone_select"
constexpr auto* TAG = "TimeZone";
#define RESULT_BUNDLE_CODE_INDEX "code"
#define RESULT_BUNDLE_NAME_INDEX "name"
@@ -62,8 +62,7 @@ void setResultCode(Bundle& bundle, const std::string& code) {
// endregion
class TimeZoneApp : public App {
class TimeZoneApp final : public App {
Mutex mutex;
std::vector<TimeZoneEntry> entries;
@@ -123,7 +122,7 @@ class TimeZoneApp : public App {
}
void readTimeZones(std::string filter) {
auto path = std::string(MOUNT_POINT_SYSTEM) + "/timezones.csv";
auto path = std::string(file::MOUNT_POINT_SYSTEM) + "/timezones.csv";
auto* file = fopen(path.c_str(), "rb");
if (file == nullptr) {
TT_LOG_E(TAG, "Failed to open %s", path.c_str());
@@ -136,7 +135,7 @@ class TimeZoneApp : public App {
std::vector<TimeZoneEntry> new_entries;
while (fgets(line, 96, file)) {
if (parseEntry(line, name, code)) {
if (tt::string::lowercase(name).find(filter) != std::string::npos) {
if (string::lowercase(name).find(filter) != std::string::npos) {
count++;
new_entries.push_back({.name = name, .code = code});
@@ -165,7 +164,7 @@ class TimeZoneApp : public App {
void updateList() {
if (lvgl::lock(100 / portTICK_PERIOD_MS)) {
std::string filter = tt::string::lowercase(std::string(lv_textarea_get_text(filterTextareaWidget)));
std::string filter = string::lowercase(std::string(lv_textarea_get_text(filterTextareaWidget)));
readTimeZones(filter);
lvgl::unlock();
} else {
@@ -227,7 +226,7 @@ public:
}
void onCreate(AppContext& app) override {
updateTimer = std::make_unique<Timer>(Timer::Type::Once, []() { updateTimerCallback(); });
updateTimer = std::make_unique<Timer>(Timer::Type::Once, [] { updateTimerCallback(); });
}
};
@@ -44,10 +44,10 @@ static void onToggleAutoConnect(lv_event_t* event) {
bool is_on = lv_obj_has_state(enable_switch, LV_STATE_CHECKED);
std::string ssid = parameters->getString("ssid");
service::wifi::settings::WifiApSettings settings {};
if (service::wifi::settings::load(ssid.c_str(), &settings)) {
settings.auto_connect = is_on;
if (!service::wifi::settings::save(&settings)) {
service::wifi::settings::WifiApSettings settings;
if (service::wifi::settings::load(ssid.c_str(), settings)) {
settings.autoConnect = is_on;
if (!service::wifi::settings::save(settings)) {
TT_LOG_E(TAG, "Failed to save settings");
}
} else {
@@ -98,9 +98,9 @@ class WifiApSettings : public App {
lv_obj_align(forget_button_label, LV_ALIGN_CENTER, 0, 0);
lv_label_set_text(forget_button_label, "Forget");
service::wifi::settings::WifiApSettings settings {};
if (service::wifi::settings::load(ssid.c_str(), &settings)) {
if (settings.auto_connect) {
service::wifi::settings::WifiApSettings settings;
if (service::wifi::settings::load(ssid.c_str(), settings)) {
if (settings.autoConnect) {
lv_obj_add_state(auto_connect_switch, LV_STATE_CHECKED);
} else {
lv_obj_remove_state(auto_connect_switch, LV_STATE_CHECKED);
+2 -2
View File
@@ -17,9 +17,9 @@ bool State::hasConnectionError() const {
return result;
}
void State::setApSettings(const service::wifi::settings::WifiApSettings* newSettings) {
void State::setApSettings(const service::wifi::settings::WifiApSettings& newSettings) {
lock.lock();
memcpy(&this->apSettings, newSettings, sizeof(service::wifi::settings::WifiApSettings));
this->apSettings = newSettings;
lock.unlock();
}
+6 -5
View File
@@ -5,10 +5,11 @@
#include "Tactility/lvgl/Spinner.h"
#include <Tactility/TactilityCore.h>
#include <Tactility/service/wifi/WifiSettings.h>
#include <lvgl.h>
#include <cstring>
#include <Tactility/service/wifi/WifiApSettings.h>
#include <Tactility/service/wifi/WifiGlobals.h>
namespace tt::app::wificonnect {
@@ -50,14 +51,14 @@ static void onConnect(TT_UNUSED lv_event_t* event) {
view.setLoading(true);
service::wifi::settings::WifiApSettings settings;
strcpy((char*)settings.password, password);
strcpy((char*)settings.ssid, ssid);
settings.password = password;
settings.ssid = ssid;
settings.channel = 0;
settings.auto_connect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w
settings.autoConnect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w
auto* bindings = &wifi->getBindings();
bindings->onConnectSsid(
&settings,
settings,
store,
bindings->onConnectSsidContext
);
@@ -6,7 +6,6 @@
#include "Tactility/lvgl/LvglSync.h"
namespace tt::app::wificonnect {
#define TAG "wifi_connect"
#define WIFI_CONNECT_PARAM_SSID "ssid" // String
#define WIFI_CONNECT_PARAM_PASSWORD "password" // String
@@ -37,7 +36,7 @@ static void eventCallback(const void* message, void* context) {
wifi->requestViewUpdate();
}
static void onConnect(const service::wifi::settings::WifiApSettings* ap_settings, bool remember, TT_UNUSED void* parameter) {
static void onConnect(const service::wifi::settings::WifiApSettings& ap_settings, bool remember, TT_UNUSED void* parameter) {
auto* wifi = static_cast<WifiConnect*>(parameter);
wifi->getState().setApSettings(ap_settings);
wifi->getState().setConnecting(true);
+1
View File
@@ -11,6 +11,7 @@
#include <format>
#include <string>
#include <set>
#include <Tactility/service/wifi/WifiSettings.h>
namespace tt::app::wifimanage {
@@ -16,9 +16,9 @@ extern const AppManifest manifest;
static void onConnect(const char* ssid) {
service::wifi::settings::WifiApSettings settings;
if (service::wifi::settings::load(ssid, &settings)) {
if (service::wifi::settings::load(ssid, settings)) {
TT_LOG_I(TAG, "Connecting with known credentials");
service::wifi::connect(&settings, false);
service::wifi::connect(settings, false);
} else {
TT_LOG_I(TAG, "Starting connection dialog");
wificonnect::start(ssid);
+24
View File
@@ -0,0 +1,24 @@
#include "Tactility/file/FileLock.h"
#include <Tactility/hal/sdcard/SdCardDevice.h>
#include <Tactility/Mutex.h>
namespace tt::file {
class NoLock : public Lock {
bool lock(TickType_t timeout) const override { return true; }
bool unlock() const override { return true; }
};
static std::shared_ptr<Lock> noLock = std::make_shared<NoLock>();
std::shared_ptr<Lock> getLock(const std::string& path) {
auto sdcard_lock = hal::sdcard::findSdCardLock(path);
if (sdcard_lock != nullptr) {
return sdcard_lock;
} else {
return noLock;
}
}
}
@@ -0,0 +1,78 @@
#include "Tactility/file/ObjectFile.h"
#include "Tactility/file/ObjectFilePrivate.h"
#include <cstring>
#include <Tactility/Log.h>
namespace tt::file {
constexpr auto* TAG = "ObjectFileReader";
bool ObjectFileReader::open() {
auto opening_file = std::unique_ptr<FILE, FileCloser>(fopen(filePath.c_str(), "r"));
if (opening_file == nullptr) {
TT_LOG_E(TAG, "Failed to open file %s", filePath.c_str());
return false;
}
FileHeader file_header;
if (fread(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) {
TT_LOG_E(TAG, "Failed to read file header from %s", filePath.c_str());
return false;
}
if (file_header.identifier != OBJECT_FILE_IDENTIFIER) {
TT_LOG_E(TAG, "Invalid file type for %s", filePath.c_str());
return false;
}
if (file_header.version != OBJECT_FILE_VERSION) {
TT_LOG_E(TAG, "Unknown version for %s: %lu", filePath.c_str(), file_header.identifier);
return false;
}
ContentHeader content_header;
if (fread(&content_header, sizeof(ContentHeader), 1, opening_file.get()) != 1) {
TT_LOG_E(TAG, "Failed to read content header from %s", filePath.c_str());
return false;
}
if (recordSize != content_header.recordSize) {
TT_LOG_E(TAG, "Record size mismatch for %s: expected %lu, got %lu", filePath.c_str(), recordSize, content_header.recordSize);
return false;
}
recordCount = content_header.recordCount;
recordVersion = content_header.recordVersion;
file = std::move(opening_file);
TT_LOG_D(TAG, "File version: %lu", file_header.version);
TT_LOG_D(TAG, "Content: version = %lu, size = %lu bytes, count = %lu", content_header.recordVersion, content_header.recordSize, content_header.recordCount);
return true;
}
void ObjectFileReader::close() {
recordCount = 0;
recordVersion = 0;
recordsRead = 0;
file = nullptr;
}
bool ObjectFileReader::readNext(void* output) {
if (file == nullptr) {
TT_LOG_E(TAG, "File not open");
return false;
}
bool result = fread(output, recordSize, 1, file.get()) == 1;
if (result) {
recordsRead++;
}
return result;
}
}
+125
View File
@@ -0,0 +1,125 @@
#include "../../Include/Tactility/file/ObjectFile.h"
#include "Tactility/file/ObjectFilePrivate.h"
#include <cstring>
#include <Tactility/Log.h>
#include <unistd.h>
namespace tt::file {
constexpr auto* TAG = "ObjectFileWriter";
bool ObjectFileWriter::open() {
bool edit_existing = append && access(filePath.c_str(), F_OK) == 0;
if (append && !edit_existing) {
TT_LOG_W(TAG, "access() to %s failed: %s", filePath.c_str(), strerror(errno));
}
// Edit existing or create a new file
auto* mode = edit_existing ? "r+" : "w";
auto opening_file = std::unique_ptr<FILE, FileCloser>(std::fopen(filePath.c_str(), mode));
if (opening_file == nullptr) {
TT_LOG_E(TAG, "Failed to open file %s in %s mode", filePath.c_str(), mode);
return false;
}
auto file_size = getSize(opening_file.get());
if (file_size > 0 && edit_existing) {
// Read and parse file header
FileHeader file_header;
if (fread(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) {
TT_LOG_E(TAG, "Failed to read file header from %s", filePath.c_str());
return false;
}
if (file_header.identifier != OBJECT_FILE_IDENTIFIER) {
TT_LOG_E(TAG, "Invalid file type for %s", filePath.c_str());
return false;
}
if (file_header.version != OBJECT_FILE_VERSION) {
TT_LOG_E(TAG, "Unknown version for %s: %lu", filePath.c_str(), file_header.identifier);
return false;
}
// Read and parse content header
ContentHeader content_header;
if (fread(&content_header, sizeof(ContentHeader), 1, opening_file.get()) != 1) {
TT_LOG_E(TAG, "Failed to read content header from %s", filePath.c_str());
return false;
}
if (recordSize != content_header.recordSize) {
TT_LOG_E(TAG, "Record size mismatch for %s: expected %lu, got %lu", filePath.c_str(), recordSize, content_header.recordSize);
return false;
}
if (recordVersion != content_header.recordVersion) {
TT_LOG_E(TAG, "Version mismatch for %s: expected %lu, got %lu", filePath.c_str(), recordVersion, content_header.recordVersion);
return false;
}
recordsWritten = content_header.recordCount;
fseek(opening_file.get(), 0, SEEK_END);
} else {
FileHeader file_header;
if (fwrite(&file_header, sizeof(FileHeader), 1, opening_file.get()) != 1) {
TT_LOG_E(TAG, "Failed to write file header for %s", filePath.c_str());
return false;
}
// Seek forward (skip ContentHeader that will be written later)
fseek(opening_file.get(), sizeof(ContentHeader), SEEK_CUR);
}
file = std::move(opening_file);
return true;
}
void ObjectFileWriter::close() {
if (file == nullptr) {
TT_LOG_E(TAG, "File not opened: %s", filePath.c_str());
return;
}
if (fseek(file.get(), sizeof(FileHeader), SEEK_SET) != 0) {
TT_LOG_E(TAG, "File seek failed: %s", filePath.c_str());
return;
}
ContentHeader content_header = {
.recordVersion = this->recordVersion,
.recordSize = this->recordSize,
.recordCount = this->recordsWritten
};
if (fwrite(&content_header, sizeof(ContentHeader), 1, file.get()) != 1) {
TT_LOG_E(TAG, "Failed to write content header to %s", filePath.c_str());
}
file = nullptr;
}
bool ObjectFileWriter::write(void* data) {
if (file == nullptr) {
TT_LOG_E(TAG, "File not opened: %s", filePath.c_str());
return false;
}
if (fwrite(data, recordSize, 1, file.get()) != 1) {
TT_LOG_E(TAG, "Failed to write record to %s", filePath.c_str());
return false;
}
recordsWritten++;
return true;
}
// endregion Writer
}
+74
View File
@@ -0,0 +1,74 @@
#include "Tactility/file/PropertiesFile.h"
#include <Tactility/StringUtils.h>
#include <Tactility/file/File.h>
#include <Tactility/file/FileLock.h>
namespace tt::file {
static auto TAG = "PropertiesFile";
bool getKeyValuePair(const std::string& input, std::string& key, std::string& value) {
auto index = input.find('=');
if (index == std::string::npos) {
return false;
}
key = input.substr(0, index);
value = input.substr(index + 1);
return true;
}
bool loadPropertiesFile(const std::string& filePath, std::function<void(const std::string& key, const std::string& value)> callback) {
return file::withLock<bool>(filePath, [&filePath, &callback] {
TT_LOG_I(TAG, "Reading properties file %s", filePath.c_str());
const auto input = readString(filePath);
if (input == nullptr) {
TT_LOG_E(TAG, "Failed to read file contents of %s", filePath.c_str());
return false;
}
const auto* input_start = reinterpret_cast<const char*>(input.get());
const std::string input_string = input_start;
uint16_t line_count = 0;
string::split(input_string, "\n", [&line_count, &filePath, &callback](auto token) {
line_count++;
std::string key, value;
auto trimmed_token = string::trim(token, " \t");
if (!trimmed_token.starts_with("#")) {
if (getKeyValuePair(token, key, value)) {
std::string trimmed_key = string::trim(key, " \t");
std::string trimmed_value = string::trim(value, " \t");
callback(trimmed_key, trimmed_value);
} else { TT_LOG_E(TAG, "Failed to parse line %d of %s", line_count, filePath.c_str()); }
}
});
return true;
});
}
bool loadPropertiesFile(const std::string& filePath, std::map<std::string, std::string>& outProperties) {
return loadPropertiesFile(filePath, [&outProperties](const std::string& key, const std::string& value) {
outProperties[key] = value;
});
}
bool savePropertiesFile(const std::string& filePath, const std::map<std::string, std::string>& properties) {
return file::withLock<bool>(filePath, [filePath, &properties] {
TT_LOG_I(TAG, "Saving properties file %s", filePath.c_str());
FILE* file = fopen(filePath.c_str(), "w");
if (file == nullptr) {
TT_LOG_E(TAG, "Failed to open %s", filePath.c_str());
return false;
}
for (const auto& [key, value]: properties) { fprintf(file, "%s=%s\n", key.c_str(), value.c_str()); }
fclose(file);
return true;
});
}
}
@@ -1,8 +1,8 @@
#include "Tactility/hal/gps/GpsConfiguration.h"
#include "Tactility/service/gps/GpsService.h"
#include "Tactility/file/ObjectFile.h"
#include <Tactility/TactilityCore.h>
#include <Tactility/file/ObjectFile.h>
namespace tt::hal::gps {
+9
View File
@@ -14,4 +14,13 @@ std::shared_ptr<SdCardDevice> _Nullable find(const std::string& path) {
return nullptr;
}
std::shared_ptr<Lock> findSdCardLock(const std::string& path) {
auto sdcard = find(path);
if (sdcard != nullptr) {
return sdcard->getLock();
}
return nullptr;
}
}
+17 -18
View File
@@ -8,10 +8,10 @@
#include <esp_vfs_fat.h>
#include <sdmmc_cmd.h>
#define TAG "spi_sdcard"
namespace tt::hal::sdcard {
constexpr auto* TAG = "SpiSdCardDevice";
/**
* Before we can initialize the sdcard's SPI communications, we have to set all
* other SPI pins on the board high.
@@ -78,7 +78,7 @@ bool SpiSdCardDevice::mountInternal(const std::string& newMountPath) {
esp_err_t result = esp_vfs_fat_sdspi_mount(newMountPath.c_str(), &host, &slot_config, &mount_config, &card);
if (result != ESP_OK) {
if (result != ESP_OK || card == nullptr) {
if (result == ESP_FAIL) {
TT_LOG_E(TAG, "Mounting failed. Ensure the card is formatted with FAT.");
} else {
@@ -99,6 +99,7 @@ bool SpiSdCardDevice::mount(const std::string& newMountPath) {
}
if (mountInternal(newMountPath)) {
TT_LOG_I(TAG, "Mounted at %s", newMountPath.c_str());
sdmmc_card_print_info(stdout, card);
return true;
} else {
@@ -113,18 +114,19 @@ bool SpiSdCardDevice::unmount() {
return false;
}
if (esp_vfs_fat_sdcard_unmount(mountPath.c_str(), card) == ESP_OK) {
mountPath = "";
card = nullptr;
return true;
} else {
if (esp_vfs_fat_sdcard_unmount(mountPath.c_str(), card) != ESP_OK) {
TT_LOG_E(TAG, "Unmount failed for %s", mountPath.c_str());
return false;
}
TT_LOG_I(TAG, "Unmounted %s", mountPath.c_str());
mountPath = "";
card = nullptr;
return true;
}
// TODO: Refactor to "bool getStatus(Status* status)" method so that it can fail when the lvgl lock fails
SdCardDevice::State SpiSdCardDevice::getState() const {
SdCardDevice::State SpiSdCardDevice::getState(TickType_t timeout) const {
if (card == nullptr) {
return State::Unmounted;
}
@@ -134,20 +136,17 @@ SdCardDevice::State SpiSdCardDevice::getState() const {
* Writing and reading to the bus from 2 devices at the same time causes crashes.
* This work-around ensures that this check is only happening when LVGL isn't rendering.
*/
auto lock = getLock().asScopedLock();
bool locked = lock.lock(50); // TODO: Refactor to a more reliable locking mechanism
auto lock = getLock()->asScopedLock();
bool locked = lock.lock(timeout);
if (!locked) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "LVGL");
return State::Unknown;
return State::Timeout;
}
bool result = sdmmc_get_status(card) == ESP_OK;
if (result) {
return State::Mounted;
} else {
if (sdmmc_get_status(card) != ESP_OK) {
return State::Error;
}
return State::Mounted;
}
}
+4 -4
View File
@@ -2,23 +2,23 @@
#include <Tactility/lvgl/LvglSync.h>
#include <esp_lvgl_port.h>
#include <Tactility/CpuAffinity.h>
#include <Tactility/Mutex.h>
// LVGL
// The minimum task stack seems to be about 3500, but that crashes the wifi app in some scenarios
// At 8192, it sometimes crashes when wifi-auto enables and is busy connecting and then you open WifiManage
#define TDECK_LVGL_TASK_STACK_DEPTH 9216
#define TAG "lvgl"
auto constexpr TAG = "lvgl";
namespace tt::lvgl {
bool initEspLvglPort() {
TT_LOG_D(TAG, "Port init");
static lv_disp_t* display = nullptr;
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = static_cast<UBaseType_t>(Thread::Priority::Critical),
.task_stack = TDECK_LVGL_TASK_STACK_DEPTH,
.task_affinity = 1, // -1 = disabled, 0 = core 1, 1 = core 2
.task_affinity = getCpuAffinityConfiguration().graphics,
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
@@ -28,7 +28,7 @@ bool initEspLvglPort() {
return false;
}
tt::lvgl::syncSet(&lvgl_port_lock, &lvgl_port_unlock);
syncSet(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}
+5 -5
View File
@@ -1,13 +1,13 @@
#include <Tactility/lvgl/LabelUtils.h>
#include <Tactility/file/File.h>
#include <Tactility/hal/sdcard/SdCardDevice.h>
#include "Tactility/lvgl/LabelUtils.h"
#include "Tactility/file/File.h"
#include "Tactility/file/FileLock.h"
namespace tt::lvgl {
#define TAG "tt_lv_label"
constexpr auto* TAG = "LabelUtils";
bool label_set_text_file(lv_obj_t* label, const char* filepath) {
auto text = hal::sdcard::withSdCardLock<std::unique_ptr<uint8_t[]>>(std::string(filepath), [filepath]() {
auto text = file::withLock<std::unique_ptr<uint8_t[]>>(std::string(filepath), [filepath] {
return file::readString(filepath);
});
+1 -2
View File
@@ -13,10 +13,9 @@
#endif
#include <lvgl.h>
#include <Tactility/Tactility.h>
#include <Tactility/TactilityHeadless.h>
#include <Tactility/lvgl/LvglSync.h>
#include <Tactility/service/ServiceRegistry.h>
#include <Tactility/service/ServiceRegistration.h>
namespace tt::lvgl {
@@ -1,6 +1,6 @@
#include "Tactility/service/ServiceInstancePaths.h"
#include "Tactility/Partitions.h"
#include "Tactility/MountPoints.h"
#define LVGL_PATH_PREFIX std::string("A:/")
@@ -13,38 +13,38 @@
namespace tt::service {
std::string ServiceInstancePaths::getDataDirectory() const {
return PARTITION_PREFIX + DATA_PARTITION_NAME + "/service/" + manifest->id;
return PARTITION_PREFIX + file::DATA_PARTITION_NAME + "/service/" + manifest->id;
}
std::string ServiceInstancePaths::getDataDirectoryLvgl() const {
return LVGL_PATH_PREFIX + DATA_PARTITION_NAME + "/service/" + manifest->id;
return LVGL_PATH_PREFIX + file::DATA_PARTITION_NAME + "/service/" + manifest->id;
}
std::string ServiceInstancePaths::getDataPath(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
return PARTITION_PREFIX + DATA_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
return PARTITION_PREFIX + file::DATA_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
}
std::string ServiceInstancePaths::getDataPathLvgl(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
return LVGL_PATH_PREFIX + DATA_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
return LVGL_PATH_PREFIX + file::DATA_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
}
std::string ServiceInstancePaths::getSystemDirectory() const {
return PARTITION_PREFIX + SYSTEM_PARTITION_NAME + "/service/" + manifest->id;
return PARTITION_PREFIX + file::SYSTEM_PARTITION_NAME + "/service/" + manifest->id;
}
std::string ServiceInstancePaths::getSystemDirectoryLvgl() const {
return LVGL_PATH_PREFIX + SYSTEM_PARTITION_NAME + "/service/" + manifest->id;
return LVGL_PATH_PREFIX + file::SYSTEM_PARTITION_NAME + "/service/" + manifest->id;
}
std::string ServiceInstancePaths::getSystemPath(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
return PARTITION_PREFIX + SYSTEM_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
return PARTITION_PREFIX + file::SYSTEM_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
}
std::string ServiceInstancePaths::getSystemPathLvgl(const std::string& childPath) const {
return LVGL_PATH_PREFIX + SYSTEM_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
return LVGL_PATH_PREFIX + file::SYSTEM_PARTITION_NAME + "/service/" + manifest->id + '/' + childPath;
}
}
@@ -1,4 +1,4 @@
#include "Tactility/service/ServiceRegistry.h"
#include "Tactility/service/ServiceRegistration.h"
#include "Tactility/service/ServiceInstance.h"
#include "Tactility/service/ServiceManifest.h"
@@ -6,8 +6,6 @@
#include <Tactility/Mutex.h>
#include <string>
#include <unordered_map>
#include <Tactility/app/AppInstance.h>
namespace tt::service {
@@ -23,8 +21,9 @@ static Mutex manifest_mutex(Mutex::Type::Normal);
static Mutex instance_mutex(Mutex::Type::Normal);
void addService(std::shared_ptr<const ServiceManifest> manifest, bool autoStart) {
assert(manifest != nullptr);
// We'll move the manifest pointer, but we'll need to id later
std::string id = manifest->id;
const auto& id = manifest->id;
TT_LOG_I(TAG, "Adding %s", id.c_str());
@@ -6,7 +6,7 @@
#include "Tactility/network/Url.h"
#include "Tactility/TactilityHeadless.h"
#include "Tactility/service/ServiceManifest.h"
#include "Tactility/service/ServiceRegistry.h"
#include "Tactility/service/ServiceRegistration.h"
#include "Tactility/service/wifi/Wifi.h"
#include <cstring>
@@ -17,7 +17,7 @@
#include <Tactility/StringUtils.h>
#include <Tactility/app/App.h>
#include <Tactility/app/ElfApp.h>
#include <Tactility/app/ManifestRegistry.h>
#include <Tactility/app/AppRegistration.h>
namespace tt::service::development {
@@ -3,7 +3,7 @@
#include "Tactility/service/espnow/EspNowService.h"
#include "Tactility/TactilityHeadless.h"
#include "Tactility/service/ServiceManifest.h"
#include "Tactility/service/ServiceRegistry.h"
#include "Tactility/service/ServiceRegistration.h"
#include "Tactility/service/espnow/EspNowWifi.h"
#include <cstring>
#include <esp_now.h>
@@ -1,6 +1,7 @@
#include "Tactility/service/gps/GpsService.h"
#include <Tactility/file/ObjectFile.h>
#include "Tactility/file/ObjectFile.h"
#include <cstring>
#include <unistd.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "Tactility/service/gps/GpsService.h"
#include "Tactility/service/ServiceManifest.h"
#include "Tactility/service/ServiceRegistry.h"
#include "Tactility/service/ServiceRegistration.h"
#include <Tactility/Log.h>
#include <Tactility/file/File.h>
+1 -1
View File
@@ -6,7 +6,7 @@
#include <Tactility/Tactility.h>
#include <Tactility/app/AppInstance.h>
#include <Tactility/service/ServiceRegistry.h>
#include <Tactility/service/ServiceRegistration.h>
namespace tt::service::gui {
+2 -2
View File
@@ -1,12 +1,12 @@
#include "Tactility/service/loader/Loader.h"
#include "Tactility/app/AppInstance.h"
#include "Tactility/app/AppManifest.h"
#include "Tactility/app/ManifestRegistry.h"
#include "Tactility/app/AppRegistration.h"
#include <Tactility/RtosCompat.h>
#include <Tactility/DispatcherThread.h>
#include <Tactility/service/ServiceManifest.h>
#include <Tactility/service/ServiceRegistry.h>
#include <Tactility/service/ServiceRegistration.h>
#include <stack>
@@ -5,7 +5,7 @@
#include "Tactility/service/screenshot/Screenshot.h"
#include <Tactility/service/ServiceContext.h>
#include <Tactility/service/ServiceRegistry.h>
#include <Tactility/service/ServiceRegistration.h>
#include <memory>
@@ -11,6 +11,7 @@
#include <Tactility/TactilityCore.h>
#include <format>
#include <Tactility/CpuAffinity.h>
namespace tt::service::screenshot {
@@ -109,10 +110,11 @@ void ScreenshotTask::taskStart() {
thread = new Thread(
"screenshot",
8192,
[this]() {
[this] {
this->taskMain();
return 0;
}
},
getCpuAffinityConfiguration().graphics
);
thread->start();
}
+5 -7
View File
@@ -1,6 +1,6 @@
#include "Tactility/service/ServiceContext.h"
#include "Tactility/TactilityHeadless.h"
#include "Tactility/service/ServiceRegistry.h"
#include "Tactility/service/ServiceRegistration.h"
#include <Tactility/Mutex.h>
#include <Tactility/Timer.h>
@@ -13,8 +13,6 @@ extern const ServiceManifest manifest;
class SdCardService final : public Service {
private:
Mutex mutex;
std::unique_ptr<Timer> updateTimer;
hal::sdcard::SdCardDevice::State lastState = hal::sdcard::SdCardDevice::State::Unmounted;
@@ -28,14 +26,14 @@ private:
}
void update() {
auto sdcard = tt::hal::getConfiguration()->sdcard;
auto sdcard = hal::getConfiguration()->sdcard;
assert(sdcard);
if (lock(50)) {
auto new_state = sdcard->getState();
if (new_state == hal::sdcard::SdCardDevice::State::Error) {
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
TT_LOG_E(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
sdcard->unmount();
}
@@ -51,7 +49,7 @@ private:
public:
void onStart(ServiceContext& serviceContext) final {
void onStart(ServiceContext& serviceContext) override {
if (hal::getConfiguration()->sdcard != nullptr) {
auto service = findServiceById<SdCardService>(manifest.id);
updateTimer = std::make_unique<Timer>(Timer::Type::Periodic, [service]() {
@@ -64,7 +62,7 @@ public:
}
}
void onStop(ServiceContext& serviceContext) final {
void onStop(ServiceContext& serviceContext) override {
if (updateTimer != nullptr) {
// Stop thread
updateTimer->stop();
@@ -9,7 +9,7 @@
#include <Tactility/TactilityHeadless.h>
#include <Tactility/Timer.h>
#include <Tactility/service/ServiceContext.h>
#include <Tactility/service/ServiceRegistry.h>
#include <Tactility/service/ServiceRegistration.h>
#include <Tactility/service/wifi/Wifi.h>
namespace tt::service::statusbar {
@@ -81,7 +81,7 @@ static const char* getSdCardStatusIcon(hal::sdcard::SdCardDevice::State state) {
return STATUSBAR_ICON_SDCARD;
case Error:
case Unmounted:
case Unknown:
case Timeout:
return STATUSBAR_ICON_SDCARD_ALERT;
default:
tt_crash("Unhandled SdCard state");
@@ -199,8 +199,8 @@ class StatusbarService final : public Service {
void updateSdCardIcon() {
auto sdcard = hal::getConfiguration()->sdcard;
if (sdcard != nullptr) {
auto state = sdcard->getState();
if (state != hal::sdcard::SdCardDevice::State::Unknown) {
auto state = sdcard->getState(50 / portTICK_PERIOD_MS);
if (state != hal::sdcard::SdCardDevice::State::Timeout) {
auto* desired_icon = getSdCardStatusIcon(state);
if (sdcard_last_icon != desired_icon) {
auto icon_path = paths->getSystemPathLvgl(desired_icon);
@@ -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());
}
}
}
}
+29 -34
View File
@@ -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); });
}
}
+1 -1
View File
@@ -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
}
+42 -6
View File
@@ -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