Cleanup and improvements (#194)

- Lots of changes for migrating C code to C++
- Improved `Lockable` in several ways like adding `withLock()` (+ tests)
- Improved `Semaphore` a bit for improved readability, and also added some tests
- Upgrade Linux machine in GitHub Actions so that we can compile with a newer GCC
- Simplification of WiFi connection
- Updated funding options
- (and more)
This commit is contained in:
Ken Van Hoeylandt 2025-01-28 17:39:58 +01:00 committed by GitHub
parent 1bb1260ea0
commit 6c67845645
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
54 changed files with 518 additions and 531 deletions

2
.github/FUNDING.yml vendored
View File

@ -10,6 +10,6 @@ liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username issuehunt: # Replace with a single IssueHunt username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
polar: # Replace with a single Polar username polar: # Replace with a single Polar username
buy_me_a_coffee: # Replace with a single Buy Me a Coffee username buy_me_a_coffee: bytewelder
thanks_dev: # Replace with a single thanks.dev username thanks_dev: # Replace with a single thanks.dev username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

View File

@ -8,7 +8,7 @@ on:
types: [opened, synchronize, reopened] types: [opened, synchronize, reopened]
jobs: jobs:
Build-Simulator-Linux: Build-Simulator-Linux:
runs-on: ubuntu-22.04 runs-on: ubuntu-24.04
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: "Build" - name: "Build"

View File

@ -26,9 +26,9 @@ static adc_oneshot_chan_cfg_t adcChannelConfig = {
}; };
static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) { static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
float volts = TT_MIN((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX); float volts = std::min((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN); float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN);
float voltage_factor = TT_MIN(1.0f, voltage_percentage); float voltage_factor = std::min(1.0f, voltage_percentage);
auto charge_level = (uint8_t) (voltage_factor * 100.f); auto charge_level = (uint8_t) (voltage_factor * 100.f);
TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level); TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level);
return charge_level; return charge_level;
@ -56,11 +56,11 @@ TdeckPower::~TdeckPower() {
bool TdeckPower::supportsMetric(MetricType type) const { bool TdeckPower::supportsMetric(MetricType type) const {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: using enum MetricType;
case MetricType::ChargeLevel: case BatteryVoltage:
case ChargeLevel:
return true; return true;
case MetricType::IsCharging: default:
case MetricType::Current:
return false; return false;
} }
@ -69,17 +69,17 @@ bool TdeckPower::supportsMetric(MetricType type) const {
bool TdeckPower::getMetric(Power::MetricType type, Power::MetricData& data) { bool TdeckPower::getMetric(Power::MetricType type, Power::MetricData& data) {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: using enum MetricType;
case BatteryVoltage:
return readBatteryVoltageSampled(data.valueAsUint32); return readBatteryVoltageSampled(data.valueAsUint32);
case MetricType::ChargeLevel: case ChargeLevel:
if (readBatteryVoltageSampled(data.valueAsUint32)) { if (readBatteryVoltageSampled(data.valueAsUint32)) {
data.valueAsUint32 = estimateChargeLevelFromVoltage(data.valueAsUint32); data.valueAsUint32 = estimateChargeLevelFromVoltage(data.valueAsUint32);
return true; return true;
} else { } else {
return false; return false;
} }
case MetricType::IsCharging: default:
case MetricType::Current:
return false; return false;
} }

View File

@ -8,11 +8,12 @@ extern axp192_t axpDevice;
bool Core2Power::supportsMetric(MetricType type) const { bool Core2Power::supportsMetric(MetricType type) const {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: using enum MetricType;
case MetricType::ChargeLevel: case BatteryVoltage:
case MetricType::IsCharging: case ChargeLevel:
case IsCharging:
return true; return true;
case MetricType::Current: default:
return false; return false;
} }
@ -21,16 +22,17 @@ bool Core2Power::supportsMetric(MetricType type) const {
bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) { bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: { using enum MetricType;
case BatteryVoltage: {
float voltage; float voltage;
if (axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &voltage) == ESP_OK) { if (axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &voltage) == ESP_OK) {
data.valueAsUint32 = (uint32_t)TT_MAX((voltage * 1000.f), 0.0f); data.valueAsUint32 = (uint32_t)std::max((voltage * 1000.f), 0.0f);
return true; return true;
} else { } else {
return false; return false;
} }
} }
case MetricType::ChargeLevel: { case ChargeLevel: {
float vbat, charge_current; float vbat, charge_current;
if ( if (
axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &vbat) == ESP_OK && axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &vbat) == ESP_OK &&
@ -51,7 +53,7 @@ bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
return false; return false;
} }
} }
case MetricType::IsCharging: { case IsCharging: {
float charge_current; float charge_current;
if (axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK) { if (axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK) {
data.valueAsBool = charge_current > 0.001f; data.valueAsBool = charge_current > 0.001f;
@ -60,7 +62,7 @@ bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
return false; return false;
} }
} }
case MetricType::Current: { case Current: {
float charge_current, discharge_current; float charge_current, discharge_current;
if ( if (
axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK && axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK &&
@ -76,9 +78,9 @@ bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
return false; return false;
} }
} }
default:
return false;
} }
return false; // Safety guard for when new enum values are introduced
} }
bool Core2Power::isAllowedToCharge() const { bool Core2Power::isAllowedToCharge() const {

View File

@ -5,11 +5,12 @@
bool CoreS3Power::supportsMetric(MetricType type) const { bool CoreS3Power::supportsMetric(MetricType type) const {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: using enum MetricType;
case MetricType::IsCharging: case BatteryVoltage:
case MetricType::ChargeLevel: case IsCharging:
case ChargeLevel:
return true; return true;
case MetricType::Current: default:
return false; return false;
} }
@ -18,7 +19,8 @@ bool CoreS3Power::supportsMetric(MetricType type) const {
bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) { bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: { using enum MetricType;
case BatteryVoltage: {
float milliVolt; float milliVolt;
if (axpDevice.getBatteryVoltage(milliVolt)) { if (axpDevice.getBatteryVoltage(milliVolt)) {
data.valueAsUint32 = (uint32_t)milliVolt; data.valueAsUint32 = (uint32_t)milliVolt;
@ -27,7 +29,7 @@ bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
return false; return false;
} }
} }
case MetricType::ChargeLevel: { case ChargeLevel: {
float vbatMillis; float vbatMillis;
if (axpDevice.getBatteryVoltage(vbatMillis)) { if (axpDevice.getBatteryVoltage(vbatMillis)) {
float vbat = vbatMillis / 1000.f; float vbat = vbatMillis / 1000.f;
@ -44,7 +46,7 @@ bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
return false; return false;
} }
} }
case MetricType::IsCharging: { case IsCharging: {
Axp2101::ChargeStatus status; Axp2101::ChargeStatus status;
if (axpDevice.getChargeStatus(status)) { if (axpDevice.getChargeStatus(status)) {
data.valueAsBool = (status == Axp2101::CHARGE_STATUS_CHARGING); data.valueAsBool = (status == Axp2101::CHARGE_STATUS_CHARGING);
@ -53,11 +55,9 @@ bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
return false; return false;
} }
} }
case MetricType::Current: default:
return false; return false;
} }
return false; // Safety guard for when new enum values are introduced
} }
bool CoreS3Power::isAllowedToCharge() const { bool CoreS3Power::isAllowedToCharge() const {

View File

@ -4,10 +4,11 @@
bool SimulatorPower::supportsMetric(MetricType type) const { bool SimulatorPower::supportsMetric(MetricType type) const {
switch (type) { switch (type) {
case MetricType::IsCharging: using enum MetricType;
case MetricType::Current: case IsCharging:
case MetricType::BatteryVoltage: case Current:
case MetricType::ChargeLevel: case BatteryVoltage:
case ChargeLevel:
return true; return true;
} }
@ -16,16 +17,17 @@ bool SimulatorPower::supportsMetric(MetricType type) const {
bool SimulatorPower::getMetric(Power::MetricType type, Power::MetricData& data) { bool SimulatorPower::getMetric(Power::MetricType type, Power::MetricData& data) {
switch (type) { switch (type) {
case MetricType::IsCharging: using enum MetricType;
case IsCharging:
data.valueAsBool = true; data.valueAsBool = true;
return true; return true;
case MetricType::Current: case Current:
data.valueAsInt32 = 42; data.valueAsInt32 = 42;
return true; return true;
case MetricType::BatteryVoltage: case BatteryVoltage:
data.valueAsUint32 = 4032; data.valueAsUint32 = 4032;
return true; return true;
case MetricType::ChargeLevel: case ChargeLevel:
data.valueAsUint8 = 100; data.valueAsUint8 = 100;
return true; return true;
} }

View File

@ -8,9 +8,9 @@
#define BATTERY_VOLTAGE_MAX 4.2f #define BATTERY_VOLTAGE_MAX 4.2f
static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) { static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
float volts = TT_MIN((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX); float volts = std::min((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN); float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN);
float voltage_factor = TT_MIN(1.0f, voltage_percentage); float voltage_factor = std::min(1.0f, voltage_percentage);
auto charge_level = (uint8_t) (voltage_factor * 100.f); auto charge_level = (uint8_t) (voltage_factor * 100.f);
TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level); TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level);
return charge_level; return charge_level;
@ -18,8 +18,9 @@ static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
bool UnPhonePower::supportsMetric(MetricType type) const { bool UnPhonePower::supportsMetric(MetricType type) const {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: using enum MetricType;
case MetricType::ChargeLevel: case BatteryVoltage:
case ChargeLevel:
return true; return true;
default: default:
return false; return false;
@ -28,9 +29,10 @@ bool UnPhonePower::supportsMetric(MetricType type) const {
bool UnPhonePower::getMetric(Power::MetricType type, Power::MetricData& data) { bool UnPhonePower::getMetric(Power::MetricType type, Power::MetricData& data) {
switch (type) { switch (type) {
case MetricType::BatteryVoltage: using enum MetricType;
case BatteryVoltage:
return readBatteryVoltageSampled(data.valueAsUint32); return readBatteryVoltageSampled(data.valueAsUint32);
case MetricType::ChargeLevel: { case ChargeLevel: {
uint32_t milli_volt; uint32_t milli_volt;
if (readBatteryVoltageSampled(milli_volt)) { if (readBatteryVoltageSampled(milli_volt)) {
data.valueAsUint8 = estimateChargeLevelFromVoltage(milli_volt); data.valueAsUint8 = estimateChargeLevelFromVoltage(milli_volt);

View File

@ -1,4 +1,5 @@
# TODOs # TODOs
- Use std::span or string_view in StringUtils https://youtu.be/FRkJCvHWdwQ?t=2754
- Fix bug in T-Deck/etc: esp_lvgl_port settings has a large stack size (~9kB) to fix an issue where the T-Deck would get a stackoverflow. This sometimes happens when WiFi is auto-enabled and you open the app while it is still connecting. - Fix bug in T-Deck/etc: esp_lvgl_port settings has a large stack size (~9kB) to fix an issue where the T-Deck would get a stackoverflow. This sometimes happens when WiFi is auto-enabled and you open the app while it is still connecting.
- Clean up static_cast when casting to base class. - Clean up static_cast when casting to base class.
- Mutex: Implement give/take from ISR support (works only for non-recursive ones) - Mutex: Implement give/take from ISR support (works only for non-recursive ones)

View File

@ -10,13 +10,13 @@
namespace tt::app { namespace tt::app {
typedef enum { enum class State {
StateInitial, // App is being activated in loader Initial, // App is being activated in loader
StateStarted, // App is in memory Started, // App is in memory
StateShowing, // App view is created Showing, // App view is created
StateHiding, // App view is destroyed Hiding, // App view is destroyed
StateStopped // App is not in memory Stopped // App is not in memory
} State; };
/** /**
* Thread-safe app instance. * Thread-safe app instance.
@ -27,7 +27,7 @@ private:
Mutex mutex = Mutex(Mutex::Type::Normal); Mutex mutex = Mutex(Mutex::Type::Normal);
const std::shared_ptr<AppManifest> manifest; const std::shared_ptr<AppManifest> manifest;
State state = StateInitial; State state = State::Initial;
Flags flags = { .showStatusbar = true }; Flags flags = { .showStatusbar = true };
/** @brief Optional parameters to start the app with /** @brief Optional parameters to start the app with
* When these are stored in the app struct, the struct takes ownership. * When these are stored in the app struct, the struct takes ownership.

View File

@ -41,13 +41,11 @@ public:
bool setEntriesForChildPath(const std::string& child_path); bool setEntriesForChildPath(const std::string& child_path);
bool setEntriesForPath(const std::string& path); bool setEntriesForPath(const std::string& path);
const std::vector<dirent>& lockEntries() const { template <std::invocable<const std::vector<dirent> &> Func>
mutex.lock(); void withEntries(Func&& onEntries) const {
return dir_entries; mutex.withLock([&]() {
} std::invoke(std::forward<Func>(onEntries), dir_entries);
});
void unlockEntries() {
mutex.unlock();
} }
bool getDirent(uint32_t index, dirent& dirent); bool getDirent(uint32_t index, dirent& dirent);

View File

@ -20,9 +20,6 @@ public:
void setConnectionError(bool error); void setConnectionError(bool error);
bool hasConnectionError() const; bool hasConnectionError() const;
const service::wifi::settings::WifiApSettings& lockApSettings();
void unlockApSettings();
void setApSettings(const service::wifi::settings::WifiApSettings* newSettings); void setApSettings(const service::wifi::settings::WifiApSettings* newSettings);
void setConnecting(bool isConnecting); void setConnecting(bool isConnecting);

View File

@ -30,8 +30,12 @@ public:
void updateApRecords(); void updateApRecords();
const std::vector<service::wifi::ApRecord>& lockApRecords() const; template <std::invocable<const std::vector<service::wifi::ApRecord>&> Func>
void unlockApRecords() const; void withApRecords(Func&& onApRecords) const {
mutex.withLock([&]() {
std::invoke(std::forward<Func>(onApRecords), apRecords);
});
}
void setConnectSsid(const std::string& ssid); void setConnectSsid(const std::string& ssid);
std::string getConnectSsid() const; std::string getConnectSsid() const;

View File

@ -62,51 +62,46 @@ namespace app {
#ifndef ESP_PLATFORM #ifndef ESP_PLATFORM
#endif #endif
static const std::vector<const app::AppManifest*> system_apps = {
&app::alertdialog::manifest,
&app::applist::manifest,
&app::boot::manifest,
&app::display::manifest,
&app::files::manifest,
&app::gpio::manifest,
&app::i2cscanner::manifest,
&app::i2csettings::manifest,
&app::imageviewer::manifest,
&app::inputdialog::manifest,
&app::launcher::manifest,
&app::log::manifest,
&app::settings::manifest,
&app::selectiondialog::manifest,
&app::systeminfo::manifest,
&app::textviewer::manifest,
&app::timedatesettings::manifest,
&app::timezone::manifest,
&app::usbsettings::manifest,
&app::wifiapsettings::manifest,
&app::wificonnect::manifest,
&app::wifimanage::manifest,
#if TT_FEATURE_SCREENSHOT_ENABLED
&app::screenshot::manifest,
#endif
#ifdef ESP_PLATFORM
&app::crashdiagnostics::manifest
#endif
};
// endregion // endregion
static void register_system_apps() { static void registerSystemApps() {
TT_LOG_I(TAG, "Registering default apps"); addApp(app::alertdialog::manifest);
for (const auto* app_manifest: system_apps) { addApp(app::applist::manifest);
addApp(*app_manifest); addApp(app::boot::manifest);
} addApp(app::display::manifest);
addApp(app::files::manifest);
addApp(app::gpio::manifest);
addApp(app::i2cscanner::manifest);
addApp(app::i2csettings::manifest);
addApp(app::imageviewer::manifest);
addApp(app::inputdialog::manifest);
addApp(app::launcher::manifest);
addApp(app::log::manifest);
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);
addApp(app::wifiapsettings::manifest);
addApp(app::wificonnect::manifest);
addApp(app::wifimanage::manifest);
#if TT_FEATURE_SCREENSHOT_ENABLED
addApp(app::screenshot::manifest);
#endif
#ifdef ESP_PLATFORM
addApp(app::crashdiagnostics::manifest);
#endif
if (getConfiguration()->hardware->power != nullptr) { if (getConfiguration()->hardware->power != nullptr) {
addApp(app::power::manifest); addApp(app::power::manifest);
} }
} }
static void register_user_apps(const std::vector<const app::AppManifest*>& apps) { static void registerUserApps(const std::vector<const app::AppManifest*>& apps) {
TT_LOG_I(TAG, "Registering user apps"); TT_LOG_I(TAG, "Registering user apps");
for (auto* manifest : apps) { for (auto* manifest : apps) {
assert(manifest != nullptr); assert(manifest != nullptr);
@ -114,7 +109,7 @@ static void register_user_apps(const std::vector<const app::AppManifest*>& apps)
} }
} }
static void register_and_start_system_services() { static void registerAndStartSystemServices() {
TT_LOG_I(TAG, "Registering and starting system services"); TT_LOG_I(TAG, "Registering and starting system services");
addService(service::loader::manifest); addService(service::loader::manifest);
addService(service::gui::manifest); addService(service::gui::manifest);
@ -124,7 +119,7 @@ static void register_and_start_system_services() {
#endif #endif
} }
static void register_and_start_user_services(const std::vector<const service::ServiceManifest*>& manifests) { static void registerAndStartUserServices(const std::vector<const service::ServiceManifest*>& manifests) {
TT_LOG_I(TAG, "Registering and starting user services"); TT_LOG_I(TAG, "Registering and starting user services");
for (auto* manifest : manifests) { for (auto* manifest : manifests) {
assert(manifest != nullptr); assert(manifest != nullptr);
@ -147,14 +142,14 @@ void run(const Configuration& config) {
// Note: the order of starting apps and services is critical! // Note: the order of starting apps and services is critical!
// System services are registered first so the apps below can find them if needed // System services are registered first so the apps below can find them if needed
register_and_start_system_services(); registerAndStartSystemServices();
// Then we register system apps. They are not used/started yet. // Then we register system apps. They are not used/started yet.
register_system_apps(); registerSystemApps();
// Then we register and start user services. They are started after system app // Then we register and start user services. They are started after system app
// registration just in case they want to figure out which system apps are installed. // registration just in case they want to figure out which system apps are installed.
register_and_start_user_services(config.services); registerAndStartUserServices(config.services);
// Now we register the user apps, as they might rely on the user services. // Now we register the user apps, as they might rely on the user services.
register_user_apps(config.apps); registerUserApps(config.apps);
TT_LOG_I(TAG, "init starting desktop app"); TT_LOG_I(TAG, "init starting desktop app");
service::loader::startApp(app::boot::manifest.id); service::loader::startApp(app::boot::manifest.id);

View File

@ -7,18 +7,22 @@
namespace tt { namespace tt {
namespace app::launcher { extern const app::AppManifest manifest; }
/** @brief The configuration for the operating system /** @brief The configuration for the operating system
* It contains the hardware configuration, apps and services * It contains the hardware configuration, apps and services
*/ */
struct Configuration { struct Configuration {
/** HAL configuration (drivers) */ /** HAL configuration (drivers) */
const hal::Configuration* hardware; const hal::Configuration* hardware = nullptr;
/** List of user applications */ /** List of user applications */
const std::vector<const app::AppManifest*> apps = {}; const std::vector<const app::AppManifest*> apps = {};
/** List of user services */ /** List of user services */
const std::vector<const service::ServiceManifest*> services = {}; const std::vector<const service::ServiceManifest*> services = {};
/** Optional app to start automatically after the splash screen. */ /** Optional app to start automatically after the splash screen. */
const char* _Nullable autoStartAppId = nullptr; const std::string launcherAppId = app::launcher::manifest.id;
/** Optional app to start automatically after the splash screen. */
const std::string autoStartAppId = {};
}; };
/** /**

View File

@ -61,10 +61,10 @@ typedef std::shared_ptr<App>(*CreateApp)();
struct AppManifest { struct AppManifest {
/** The identifier by which the app is launched by the system and other apps. */ /** The identifier by which the app is launched by the system and other apps. */
std::string id; std::string id = {};
/** The user-readable name of the app. Used in UI. */ /** The user-readable name of the app. Used in UI. */
std::string name; std::string name = {};
/** Optional icon. */ /** Optional icon. */
std::string icon = {}; std::string icon = {};

View File

@ -73,12 +73,8 @@ private:
#endif #endif
auto* config = tt::getConfiguration(); auto* config = tt::getConfiguration();
if (config->autoStartAppId) { assert(!config->launcherAppId.empty());
TT_LOG_I(TAG, "init auto-starting %s", config->autoStartAppId); tt::service::loader::startApp(config->launcherAppId);
tt::service::loader::startApp(config->autoStartAppId);
} else {
app::launcher::start();
}
} }
public: public:

View File

@ -69,7 +69,7 @@ public:
TT_LOG_I(TAG, "Create canvas"); TT_LOG_I(TAG, "Create canvas");
int32_t available_height = parent_height - top_label_height - bottom_label_height; int32_t available_height = parent_height - top_label_height - bottom_label_height;
int32_t available_width = lv_display_get_horizontal_resolution(display); int32_t available_width = lv_display_get_horizontal_resolution(display);
int32_t smallest_size = TT_MIN(available_height, available_width); int32_t smallest_size = std::min(available_height, available_width);
int32_t pixel_size; int32_t pixel_size;
if (qrcode.size * 2 <= smallest_size) { if (qrcode.size * 2 <= smallest_size) {
pixel_size = 2; pixel_size = 2;

View File

@ -229,12 +229,13 @@ void View::update() {
auto scoped_lockable = lvgl::getLvglSyncLockable()->scoped(); auto scoped_lockable = lvgl::getLvglSyncLockable()->scoped();
if (scoped_lockable->lock(100 / portTICK_PERIOD_MS)) { if (scoped_lockable->lock(100 / portTICK_PERIOD_MS)) {
lv_obj_clean(dir_entry_list); lv_obj_clean(dir_entry_list);
auto entries = state->lockEntries();
state->withEntries([this](const std::vector<dirent>& entries) {
for (auto entry : entries) { for (auto entry : entries) {
TT_LOG_D(TAG, "Entry: %s %d", entry.d_name, entry.d_type); TT_LOG_D(TAG, "Entry: %s %d", entry.d_name, entry.d_type);
createDirEntryWidget(dir_entry_list, entry); createDirEntryWidget(dir_entry_list, entry);
} }
state->unlockEntries(); });
if (state->getCurrentPath() == "/") { if (state->getCurrentPath() == "/") {
lv_obj_add_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN); lv_obj_add_flag(navigate_up_button, LV_OBJ_FLAG_HIDDEN);

View File

@ -1,9 +1,12 @@
#include "Check.h"
#include "Tactility.h"
#include "app/AppContext.h" #include "app/AppContext.h"
#include "app/ManifestRegistry.h" #include "app/ManifestRegistry.h"
#include "Check.h"
#include "lvgl.h" #include "lvgl.h"
#include "service/loader/Loader.h" #include "service/loader/Loader.h"
#define TAG "launcher"
namespace tt::app::launcher { namespace tt::app::launcher {
static void onAppPressed(TT_UNUSED lv_event_t* e) { static void onAppPressed(TT_UNUSED lv_event_t* e) {
@ -44,6 +47,14 @@ static lv_obj_t* createAppButton(lv_obj_t* parent, const char* title, const char
class LauncherApp : public App { class LauncherApp : public App {
void onStart(TT_UNUSED AppContext& app) override {
auto* config = tt::getConfiguration();
if (!config->autoStartAppId.empty()) {
TT_LOG_I(TAG, "auto-starting %s", config->autoStartAppId.c_str());
tt::service::loader::startApp(config->autoStartAppId);
}
}
void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) override { void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) override {
auto* wrapper = lv_obj_create(parent); auto* wrapper = lv_obj_create(parent);
@ -64,7 +75,7 @@ class LauncherApp : public App {
} }
int32_t available_width = lv_display_get_horizontal_resolution(display) - (3 * 80); int32_t available_width = lv_display_get_horizontal_resolution(display) - (3 * 80);
int32_t padding = is_landscape_display ? TT_MIN(available_width / 4, 64) : 0; int32_t padding = is_landscape_display ? std::min(available_width / 4, (int32_t)64) : 0;
auto paths = app.getPaths(); auto paths = app.getPaths();
auto apps_icon_path = paths->getSystemPathLvgl("icon_apps.png"); auto apps_icon_path = paths->getSystemPathLvgl("icon_apps.png");

View File

@ -1,5 +1,3 @@
#include <sstream>
#include <vector>
#include "lvgl.h" #include "lvgl.h"
#include "lvgl/Style.h" #include "lvgl/Style.h"
#include "lvgl/Toolbar.h" #include "lvgl/Toolbar.h"
@ -7,6 +5,10 @@
#include "service/loader/Loader.h" #include "service/loader/Loader.h"
#include "lvgl/LvglSync.h" #include "lvgl/LvglSync.h"
#include <sstream>
#include <vector>
#include <ranges>
#define TAG "text_viewer" #define TAG "text_viewer"
namespace tt::app::log { namespace tt::app::log {
@ -18,40 +20,37 @@ private:
LogLevel filterLevel = LogLevel::Info; LogLevel filterLevel = LogLevel::Info;
lv_obj_t* labelWidget = nullptr; lv_obj_t* labelWidget = nullptr;
static bool shouldShowLog(LogLevel filterLevel, LogLevel logLevel) { static inline bool shouldShowLog(LogLevel filterLevel, LogLevel logLevel) {
if (filterLevel == LogLevel::None || logLevel == LogLevel::None) { return (filterLevel != LogLevel::None) &&
return false; (logLevel != LogLevel::None) &&
} else { filterLevel >= logLevel;
return filterLevel >= logLevel;
}
} }
void updateLogEntries() { void updateLogEntries() {
unsigned int index; std::size_t next_log_index;
auto* entries = copyLogEntries(index); auto entries = copyLogEntries(next_log_index);
std::stringstream buffer; std::stringstream buffer;
if (entries != nullptr) {
for (unsigned int i = index; i < TT_LOG_ENTRY_COUNT; ++i) { if (next_log_index != 0) {
if (shouldShowLog(filterLevel, entries[i].level)) { long to_drop = TT_LOG_ENTRY_COUNT - next_log_index;
buffer << entries[i].message; for (auto entry : std::views::drop(*entries, (long)next_log_index)) {
} if (shouldShowLog(filterLevel, entry.level) && entry.message[0] != 0x00) {
} buffer << entry.message;
if (index != 0) {
for (unsigned int i = 0; i < index; ++i) {
if (shouldShowLog(filterLevel, entries[i].level)) {
buffer << entries[i].message;
} }
} }
} }
delete entries;
for (auto entry : std::views::take(*entries, (long)next_log_index)) {
if (shouldShowLog(filterLevel, entry.level) && entry.message[0] != 0x00) {
buffer << entry.message;
}
}
if (!buffer.str().empty()) { if (!buffer.str().empty()) {
lv_label_set_text(labelWidget, buffer.str().c_str()); lv_label_set_text(labelWidget, buffer.str().c_str());
} else { } else {
lv_label_set_text(labelWidget, "No logs for the selected log level"); lv_label_set_text(labelWidget, "No logs for the selected log level");
} }
} else {
lv_label_set_text(labelWidget, "Failed to load log");
}
} }
void updateViews() { void updateViews() {

View File

@ -22,15 +22,6 @@ void State::setApSettings(const service::wifi::settings::WifiApSettings* newSett
lock.unlock(); lock.unlock();
} }
const service::wifi::settings::WifiApSettings& State::lockApSettings() {
lock.lock();
return apSettings;
}
void State::unlockApSettings() {
lock.unlock();
}
void State::setConnecting(bool isConnecting) { void State::setConnecting(bool isConnecting) {
lock.lock(); lock.lock();
connecting = isConnecting; connecting = isConnecting;

View File

@ -55,6 +55,7 @@ static void onConnect(TT_UNUSED lv_event_t* event) {
service::wifi::settings::WifiApSettings settings; service::wifi::settings::WifiApSettings settings;
strcpy((char*)settings.password, password); strcpy((char*)settings.password, password);
strcpy((char*)settings.ssid, ssid); strcpy((char*)settings.ssid, ssid);
settings.channel = 0;
settings.auto_connect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w settings.auto_connect = TT_WIFI_AUTO_CONNECT; // No UI yet, so use global setting:w
auto* bindings = &wifi->getBindings(); auto* bindings = &wifi->getBindings();

View File

@ -32,15 +32,6 @@ bool State::isScanning() const {
return result; return result;
} }
const std::vector<service::wifi::ApRecord>& State::lockApRecords() const {
mutex.lock();
return apRecords;
}
void State::unlockApRecords() const {
mutex.unlock();
}
void State::updateApRecords() { void State::updateApRecords() {
mutex.lock(); mutex.lock();
apRecords = service::wifi::getScanResults(); apRecords = service::wifi::getScanResults();

View File

@ -130,16 +130,17 @@ void View::createSsidListItem(const service::wifi::ApRecord& record, bool isConn
} }
void View::updateConnectToHidden() { void View::updateConnectToHidden() {
using enum service::wifi::RadioState;
switch (state->getRadioState()) { switch (state->getRadioState()) {
case service::wifi::RadioState::On: case On:
case service::wifi::RadioState::ConnectionPending: case ConnectionPending:
case service::wifi::RadioState::ConnectionActive: case ConnectionActive:
lv_obj_remove_flag(connect_to_hidden, LV_OBJ_FLAG_HIDDEN); lv_obj_remove_flag(connect_to_hidden, LV_OBJ_FLAG_HIDDEN);
break; break;
case service::wifi::RadioState::OnPending: case OnPending:
case service::wifi::RadioState::OffPending: case OffPending:
case service::wifi::RadioState::Off: case Off:
lv_obj_add_flag(connect_to_hidden, LV_OBJ_FLAG_HIDDEN); lv_obj_add_flag(connect_to_hidden, LV_OBJ_FLAG_HIDDEN);
break; break;
} }
@ -149,20 +150,20 @@ void View::updateNetworkList() {
lv_obj_clean(networks_list); lv_obj_clean(networks_list);
switch (state->getRadioState()) { switch (state->getRadioState()) {
case service::wifi::RadioState::OnPending: using enum service::wifi::RadioState;
case service::wifi::RadioState::On: case OnPending:
case service::wifi::RadioState::ConnectionPending: case On:
case service::wifi::RadioState::ConnectionActive: { case ConnectionPending:
case ConnectionActive: {
std::string connection_target = service::wifi::getConnectionTarget(); std::string connection_target = service::wifi::getConnectionTarget();
auto& ap_records = state->lockApRecords();
state->withApRecords([this, &connection_target](const std::vector<service::wifi::ApRecord>& apRecords){
bool is_connected = !connection_target.empty() && bool is_connected = !connection_target.empty() &&
state->getRadioState() == service::wifi::RadioState::ConnectionActive; state->getRadioState() == service::wifi::RadioState::ConnectionActive;
bool added_connected = false; bool added_connected = false;
if (is_connected) { if (is_connected && !apRecords.empty()) {
if (!ap_records.empty()) { for (auto &record : apRecords) {
for (auto &record : ap_records) {
if (record.ssid == connection_target) { if (record.ssid == connection_target) {
lv_list_add_text(networks_list, "Connected"); lv_list_add_text(networks_list, "Connected");
createSsidListItem(record, false); createSsidListItem(record, false);
@ -171,12 +172,11 @@ void View::updateNetworkList() {
} }
} }
} }
}
lv_list_add_text(networks_list, "Other networks"); lv_list_add_text(networks_list, "Other networks");
std::set<std::string> used_ssids; std::set<std::string> used_ssids;
if (!ap_records.empty()) { if (!apRecords.empty()) {
for (auto& record : ap_records) { for (auto& record : apRecords) {
if (used_ssids.find(record.ssid) == used_ssids.end()) { if (used_ssids.find(record.ssid) == used_ssids.end()) {
bool connection_target_match = (record.ssid == connection_target); bool connection_target_match = (record.ssid == connection_target);
bool is_connecting = connection_target_match bool is_connecting = connection_target_match
@ -198,11 +198,12 @@ void View::updateNetworkList() {
lv_obj_t* label = lv_label_create(networks_list); lv_obj_t* label = lv_label_create(networks_list);
lv_label_set_text(label, "No networks found."); lv_label_set_text(label, "No networks found.");
} }
state->unlockApRecords(); });
break; break;
} }
case service::wifi::RadioState::OffPending: case OffPending:
case service::wifi::RadioState::Off: { case Off: {
lv_obj_add_flag(networks_list, LV_OBJ_FLAG_HIDDEN); lv_obj_add_flag(networks_list, LV_OBJ_FLAG_HIDDEN);
break; break;
} }
@ -220,18 +221,19 @@ void View::updateScanning() {
void View::updateWifiToggle() { void View::updateWifiToggle() {
lv_obj_clear_state(enable_switch, LV_STATE_ANY); lv_obj_clear_state(enable_switch, LV_STATE_ANY);
switch (state->getRadioState()) { switch (state->getRadioState()) {
case service::wifi::RadioState::On: using enum service::wifi::RadioState;
case service::wifi::RadioState::ConnectionPending: case On:
case service::wifi::RadioState::ConnectionActive: case ConnectionPending:
case ConnectionActive:
lv_obj_add_state(enable_switch, LV_STATE_CHECKED); lv_obj_add_state(enable_switch, LV_STATE_CHECKED);
break; break;
case service::wifi::RadioState::OnPending: case OnPending:
lv_obj_add_state(enable_switch, LV_STATE_CHECKED | LV_STATE_DISABLED); lv_obj_add_state(enable_switch, LV_STATE_CHECKED | LV_STATE_DISABLED);
break; break;
case service::wifi::RadioState::Off: case Off:
lv_obj_remove_state(enable_switch, LV_STATE_CHECKED | LV_STATE_DISABLED); lv_obj_remove_state(enable_switch, LV_STATE_CHECKED | LV_STATE_DISABLED);
break; break;
case service::wifi::RadioState::OffPending: case OffPending:
lv_obj_remove_state(enable_switch, LV_STATE_CHECKED); lv_obj_remove_state(enable_switch, LV_STATE_CHECKED);
lv_obj_add_state(enable_switch, LV_STATE_DISABLED); lv_obj_add_state(enable_switch, LV_STATE_DISABLED);
break; break;

View File

@ -91,14 +91,15 @@ static void wifiManageEventCallback(const void* message, void* context) {
TT_LOG_I(TAG, "Update with state %s", service::wifi::radioStateToString(radio_state)); TT_LOG_I(TAG, "Update with state %s", service::wifi::radioStateToString(radio_state));
wifi->getState().setRadioState(radio_state); wifi->getState().setRadioState(radio_state);
switch (event->type) { switch (event->type) {
case tt::service::wifi::EventType::ScanStarted: using enum tt::service::wifi::EventType;
case ScanStarted:
wifi->getState().setScanning(true); wifi->getState().setScanning(true);
break; break;
case tt::service::wifi::EventType::ScanFinished: case ScanFinished:
wifi->getState().setScanning(false); wifi->getState().setScanning(false);
wifi->getState().updateApRecords(); wifi->getState().updateApRecords();
break; break;
case tt::service::wifi::EventType::RadioStateOn: case RadioStateOn:
if (!service::wifi::isScanning()) { if (!service::wifi::isScanning()) {
service::wifi::scan(); service::wifi::scan();
} }

View File

@ -85,15 +85,16 @@ std::shared_ptr<PubSub> getPubsub() {
static const char* appStateToString(app::State state) { static const char* appStateToString(app::State state) {
switch (state) { switch (state) {
case app::StateInitial: using enum app::State;
case Initial:
return "initial"; return "initial";
case app::StateStarted: case Started:
return "started"; return "started";
case app::StateShowing: case Showing:
return "showing"; return "showing";
case app::StateHiding: case Hiding:
return "hiding"; return "hiding";
case app::StateStopped: case Stopped:
return "stopped"; return "stopped";
default: default:
return "?"; return "?";
@ -113,31 +114,29 @@ static void transitionAppToState(std::shared_ptr<app::AppInstance> app, app::Sta
); );
switch (state) { switch (state) {
case app::StateInitial: using enum app::State;
app->setState(app::StateInitial); case Initial:
break; break;
case app::StateStarted: case Started:
app->getApp()->onStart(*app); app->getApp()->onStart(*app);
app->setState(app::StateStarted);
break; break;
case app::StateShowing: { case Showing: {
LoaderEvent event_showing = { .type = LoaderEventTypeApplicationShowing }; LoaderEvent event_showing = { .type = LoaderEventTypeApplicationShowing };
loader_singleton->pubsubExternal->publish(&event_showing); loader_singleton->pubsubExternal->publish(&event_showing);
app->setState(app::StateShowing);
break; break;
} }
case app::StateHiding: { case Hiding: {
LoaderEvent event_hiding = { .type = LoaderEventTypeApplicationHiding }; LoaderEvent event_hiding = { .type = LoaderEventTypeApplicationHiding };
loader_singleton->pubsubExternal->publish(&event_hiding); loader_singleton->pubsubExternal->publish(&event_hiding);
app->setState(app::StateHiding);
break; break;
} }
case app::StateStopped: case Stopped:
// TODO: Verify manifest // TODO: Verify manifest
app->getApp()->onStop(*app); app->getApp()->onStop(*app);
app->setState(app::StateStopped);
break; break;
} }
app->setState(state);
} }
static LoaderStatus startAppWithManifestInternal( static LoaderStatus startAppWithManifestInternal(
@ -160,15 +159,15 @@ static LoaderStatus startAppWithManifestInternal(
new_app->mutableFlags().showStatusbar = (manifest->type != app::Type::Boot); new_app->mutableFlags().showStatusbar = (manifest->type != app::Type::Boot);
loader_singleton->appStack.push(new_app); loader_singleton->appStack.push(new_app);
transitionAppToState(new_app, app::StateInitial); transitionAppToState(new_app, app::State::Initial);
transitionAppToState(new_app, app::StateStarted); transitionAppToState(new_app, app::State::Started);
// We might have to hide the previous app first // We might have to hide the previous app first
if (previous_app != nullptr) { if (previous_app != nullptr) {
transitionAppToState(previous_app, app::StateHiding); transitionAppToState(previous_app, app::State::Hiding);
} }
transitionAppToState(new_app, app::StateShowing); transitionAppToState(new_app, app::State::Showing);
LoaderEvent event_external = { .type = LoaderEventTypeApplicationStarted }; LoaderEvent event_external = { .type = LoaderEventTypeApplicationStarted };
loader_singleton->pubsubExternal->publish(&event_external); loader_singleton->pubsubExternal->publish(&event_external);
@ -230,8 +229,8 @@ static void stopAppInternal() {
result_set = true; result_set = true;
} }
transitionAppToState(app_to_stop, app::StateHiding); transitionAppToState(app_to_stop, app::State::Hiding);
transitionAppToState(app_to_stop, app::StateStopped); transitionAppToState(app_to_stop, app::State::Stopped);
loader_singleton->appStack.pop(); loader_singleton->appStack.pop();
@ -254,7 +253,7 @@ static void stopAppInternal() {
if (!loader_singleton->appStack.empty()) { if (!loader_singleton->appStack.empty()) {
instance_to_resume = loader_singleton->appStack.top(); instance_to_resume = loader_singleton->appStack.top();
assert(instance_to_resume); assert(instance_to_resume);
transitionAppToState(instance_to_resume, app::StateShowing); transitionAppToState(instance_to_resume, app::State::Showing);
} }
// Unlock so that we can send results to app and they can also start/stop new apps while processing these results // Unlock so that we can send results to app and they can also start/stop new apps while processing these results

View File

@ -18,7 +18,7 @@ std::shared_ptr<ScreenshotService> _Nullable optScreenshotService() {
return service::findServiceById<ScreenshotService>(manifest.id); return service::findServiceById<ScreenshotService>(manifest.id);
} }
void ScreenshotService::startApps(const char* path) { void ScreenshotService::startApps(const std::string& path) {
auto scoped_lockable = mutex.scoped(); auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
@ -34,7 +34,7 @@ void ScreenshotService::startApps(const char* path) {
} }
} }
void ScreenshotService::startTimed(const char* path, uint8_t delayInSeconds, uint8_t amount) { void ScreenshotService::startTimed(const std::string& path, uint8_t delayInSeconds, uint8_t amount) {
auto scoped_lockable = mutex.scoped(); auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);

View File

@ -35,14 +35,14 @@ public:
/** @brief Start taking screenshot whenever an app is started /** @brief Start taking screenshot whenever an app is started
* @param[in] path the path to store the screenshots at * @param[in] path the path to store the screenshots at
*/ */
void startApps(const char* path); void startApps(const std::string& path);
/** @brief Start taking screenshots after a certain delay /** @brief Start taking screenshots after a certain delay
* @param[in] path the path to store the screenshots at * @param[in] path the path to store the screenshots at
* @param[in] delayInSeconds the delay before starting (and between successive screenshots) * @param[in] delayInSeconds the delay before starting (and between successive screenshots)
* @param[in] amount 0 = indefinite, >0 for a specific * @param[in] amount 0 = indefinite, >0 for a specific
*/ */
void startTimed(const char* path, uint8_t delayInSeconds, uint8_t amount); void startTimed(const std::string& path, uint8_t delayInSeconds, uint8_t amount);
/** @brief Stop taking screenshots */ /** @brief Stop taking screenshots */
void stop(); void stop();

View File

@ -2,11 +2,10 @@
#if TT_FEATURE_SCREENSHOT_ENABLED #if TT_FEATURE_SCREENSHOT_ENABLED
#include <cstring>
#include "ScreenshotTask.h" #include "ScreenshotTask.h"
#include "lv_screenshot.h" #include "lv_screenshot.h"
#include <format>
#include "app/AppContext.h"
#include "TactilityCore.h" #include "TactilityCore.h"
#include "service/loader/Loader.h" #include "service/loader/Loader.h"
#include "lvgl/LvglSync.h" #include "lvgl/LvglSync.h"
@ -45,12 +44,12 @@ void ScreenshotTask::setFinished() {
finished = true; finished = true;
} }
static void makeScreenshot(const char* filename) { static void makeScreenshot(const std::string& filename) {
if (lvgl::lock(50 / portTICK_PERIOD_MS)) { if (lvgl::lock(50 / portTICK_PERIOD_MS)) {
if (lv_screenshot_create(lv_scr_act(), LV_100ASK_SCREENSHOT_SV_PNG, filename)) { if (lv_screenshot_create(lv_scr_act(), LV_100ASK_SCREENSHOT_SV_PNG, filename.c_str())) {
TT_LOG_I(TAG, "Screenshot saved to %s", filename); TT_LOG_I(TAG, "Screenshot saved to %s", filename.c_str());
} else { } else {
TT_LOG_E(TAG, "Screenshot not saved to %s", filename); TT_LOG_E(TAG, "Screenshot not saved to %s", filename.c_str());
} }
lvgl::unlock(); lvgl::unlock();
} else { } else {
@ -78,8 +77,7 @@ void ScreenshotTask::taskMain() {
if (!isInterrupted()) { if (!isInterrupted()) {
screenshots_taken++; screenshots_taken++;
char filename[SCREENSHOT_PATH_LIMIT + 32]; std::string filename = std::format("{}/screenshot-{}.png", work.path, screenshots_taken);
sprintf(filename, "%s/screenshot-%d.png", work.path, screenshots_taken);
makeScreenshot(filename); makeScreenshot(filename);
if (work.amount > 0 && screenshots_taken >= work.amount) { if (work.amount > 0 && screenshots_taken >= work.amount) {
@ -93,8 +91,7 @@ void ScreenshotTask::taskMain() {
if (manifest.id != last_app_id) { if (manifest.id != last_app_id) {
kernel::delayMillis(100); kernel::delayMillis(100);
last_app_id = manifest.id; last_app_id = manifest.id;
char filename[SCREENSHOT_PATH_LIMIT + 32]; auto filename = std::format("{}/screenshot-{}.png", work.path, manifest.id);
sprintf(filename, "%s/screenshot-%s.png", work.path, manifest.id.c_str());
makeScreenshot(filename); makeScreenshot(filename);
} }
} }
@ -123,9 +120,7 @@ void ScreenshotTask::taskStart() {
thread->start(); thread->start();
} }
void ScreenshotTask::startApps(const char* path) { void ScreenshotTask::startApps(const std::string& path) {
tt_check(strlen(path) < (SCREENSHOT_PATH_LIMIT - 1));
auto scoped_lockable = mutex.scoped(); auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
@ -135,15 +130,14 @@ void ScreenshotTask::startApps(const char* path) {
if (thread == nullptr) { if (thread == nullptr) {
interrupted = false; interrupted = false;
work.type = TASK_WORK_TYPE_APPS; work.type = TASK_WORK_TYPE_APPS;
strcpy(work.path, path); work.path = path;
taskStart(); taskStart();
} else { } else {
TT_LOG_E(TAG, "Task was already running"); TT_LOG_E(TAG, "Task was already running");
} }
} }
void ScreenshotTask::startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) { void ScreenshotTask::startTimed(const std::string& path, uint8_t delay_in_seconds, uint8_t amount) {
tt_check(strlen(path) < (SCREENSHOT_PATH_LIMIT - 1));
auto scoped_lockable = mutex.scoped(); auto scoped_lockable = mutex.scoped();
if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) { if (!scoped_lockable->lock(50 / portTICK_PERIOD_MS)) {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
@ -155,7 +149,7 @@ void ScreenshotTask::startTimed(const char* path, uint8_t delay_in_seconds, uint
work.type = TASK_WORK_TYPE_DELAY; work.type = TASK_WORK_TYPE_DELAY;
work.delay_in_seconds = delay_in_seconds; work.delay_in_seconds = delay_in_seconds;
work.amount = amount; work.amount = amount;
strcpy(work.path, path); work.path = path;
taskStart(); taskStart();
} else { } else {
TT_LOG_E(TAG, "Task was already running"); TT_LOG_E(TAG, "Task was already running");

View File

@ -13,15 +13,13 @@ namespace tt::service::screenshot {
#define TASK_WORK_TYPE_DELAY 1 #define TASK_WORK_TYPE_DELAY 1
#define TASK_WORK_TYPE_APPS 2 #define TASK_WORK_TYPE_APPS 2
#define SCREENSHOT_PATH_LIMIT 128
class ScreenshotTask { class ScreenshotTask {
struct ScreenshotTaskWork { struct ScreenshotTaskWork {
int type = TASK_WORK_TYPE_DELAY ; int type = TASK_WORK_TYPE_DELAY ;
uint8_t delay_in_seconds = 0; uint8_t delay_in_seconds = 0;
uint8_t amount = 0; uint8_t amount = 0;
char path[SCREENSHOT_PATH_LIMIT] = { 0 }; std::string path;
}; };
Thread* thread = nullptr; Thread* thread = nullptr;
@ -39,12 +37,12 @@ public:
* @param[in] delayInSeconds the delay before starting (and between successive screenshots) * @param[in] delayInSeconds the delay before starting (and between successive screenshots)
* @param[in] amount 0 = indefinite, >0 for a specific * @param[in] amount 0 = indefinite, >0 for a specific
*/ */
void startTimed(const char* path, uint8_t delayInSeconds, uint8_t amount); void startTimed(const std::string& path, uint8_t delayInSeconds, uint8_t amount);
/** @brief Start taking screenshot whenever an app is started /** @brief Start taking screenshot whenever an app is started
* @param[in] path the path to store the screenshots at * @param[in] path the path to store the screenshots at
*/ */
void startApps(const char* path); void startApps(const std::string& path);
/** @brief Stop taking screenshots */ /** @brief Stop taking screenshots */
void stop(); void stop();

View File

@ -54,14 +54,15 @@ const char* getWifiStatusIconForRssi(int rssi) {
static const char* getWifiStatusIcon(wifi::RadioState state, bool secure) { static const char* getWifiStatusIcon(wifi::RadioState state, bool secure) {
int rssi; int rssi;
switch (state) { switch (state) {
case wifi::RadioState::On: using enum wifi::RadioState;
case wifi::RadioState::OnPending: case On:
case wifi::RadioState::ConnectionPending: case OnPending:
case ConnectionPending:
return STATUSBAR_ICON_WIFI_SCAN_WHITE; return STATUSBAR_ICON_WIFI_SCAN_WHITE;
case wifi::RadioState::OffPending: case OffPending:
case wifi::RadioState::Off: case Off:
return STATUSBAR_ICON_WIFI_OFF_WHITE; return STATUSBAR_ICON_WIFI_OFF_WHITE;
case wifi::RadioState::ConnectionActive: case ConnectionActive:
rssi = wifi::getRssi(); rssi = wifi::getRssi();
return getWifiStatusIconForRssi(rssi); return getWifiStatusIconForRssi(rssi);
default: default:
@ -71,11 +72,12 @@ static const char* getWifiStatusIcon(wifi::RadioState state, bool secure) {
static const char* getSdCardStatusIcon(hal::SdCard::State state) { static const char* getSdCardStatusIcon(hal::SdCard::State state) {
switch (state) { switch (state) {
case hal::SdCard::State::Mounted: using enum hal::SdCard::State;
case Mounted:
return STATUSBAR_ICON_SDCARD; return STATUSBAR_ICON_SDCARD;
case hal::SdCard::State::Error: case Error:
case hal::SdCard::State::Unmounted: case Unmounted:
case hal::SdCard::State::Unknown: case Unknown:
return STATUSBAR_ICON_SDCARD_ALERT; return STATUSBAR_ICON_SDCARD_ALERT;
default: default:
tt_crash("Unhandled SdCard state"); tt_crash("Unhandled SdCard state");

View File

@ -22,7 +22,7 @@ bool tt_semaphore_release(SemaphoreHandle handle) {
} }
uint32_t tt_semaphore_get_count(SemaphoreHandle handle) { uint32_t tt_semaphore_get_count(SemaphoreHandle handle) {
return HANDLE_AS_SEMAPHORE(handle)->getCount(); return HANDLE_AS_SEMAPHORE(handle)->getAvailable();
} }
} }

View File

@ -1,37 +1,3 @@
#pragma once #pragma once
/** Find the largest value
* @param[in] a first value to compare
* @param[in] b second value to compare
* @return the largest value of a and b
*/
#define TT_MAX(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
/** Find the smallest value
* @param[in] a first value to compare
* @param[in] b second value to compare
* @return the smallest value of a and b
*/
#define TT_MIN(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
/** @return the absolute value of the input */
#define TT_ABS(a) ({ (a) < 0 ? -(a) : (a); })
/** Clamp a value between a min and a max.
* @param[in] x value to clamp
* @param[in] upper upper bounds for x
* @param[in] lower lower bounds for x
*/
#define TT_CLAMP(x, upper, lower) (TT_MIN(upper, TT_MAX(x, lower)))
#define TT_STRINGIFY(x) #x #define TT_STRINGIFY(x) #x

View File

@ -13,7 +13,6 @@
namespace tt { namespace tt {
/** /**
* A thread-safe way to defer code execution. * A thread-safe way to defer code execution.
* Generally, one task would dispatch the execution, * Generally, one task would dispatch the execution,

View File

@ -12,18 +12,41 @@ class ScopedLockableUsage;
/** Represents a lock/mutex */ /** Represents a lock/mutex */
class Lockable { class Lockable {
public: public:
virtual ~Lockable() = default; virtual ~Lockable() = default;
virtual bool lock(TickType_t timeoutTicks) const = 0; virtual bool lock(TickType_t timeout) const = 0;
virtual bool lock() const { return lock(portMAX_DELAY); } bool lock() const { return lock(portMAX_DELAY); }
virtual bool unlock() const = 0; virtual bool unlock() const = 0;
void withLock(TickType_t timeout, const std::function<void()>& onLockAcquired) const {
if (lock(timeout)) {
onLockAcquired();
unlock();
}
}
void withLock(TickType_t timeout, const std::function<void()>& onLockAcquired, const std::function<void()>& onLockFailure) const {
if (lock(timeout)) {
onLockAcquired();
unlock();
} else {
onLockFailure();
}
}
void withLock(const std::function<void()>& onLockAcquired) const { withLock(portMAX_DELAY, onLockAcquired); }
void withLock(const std::function<void()>& onLockAcquired, const std::function<void()>& onLockFailed) const { withLock(portMAX_DELAY, onLockAcquired, onLockFailed); }
std::unique_ptr<ScopedLockableUsage> scoped() const; std::unique_ptr<ScopedLockableUsage> scoped() const;
}; };
/** /**
* Represents a lockable instance that is scoped to a specific lifecycle. * Represents a lockable instance that is scoped to a specific lifecycle.
* Once the ScopedLockableUsage is destroyed, unlock() is called automatically. * Once the ScopedLockableUsage is destroyed, unlock() is called automatically.
@ -37,6 +60,8 @@ class ScopedLockableUsage final : public Lockable {
public: public:
using Lockable::lock;
explicit ScopedLockableUsage(const Lockable& lockable) : lockable(lockable) {} explicit ScopedLockableUsage(const Lockable& lockable) : lockable(lockable) {}
~ScopedLockableUsage() final { ~ScopedLockableUsage() final {
@ -47,8 +72,6 @@ public:
return lockable.lock(timeout); return lockable.lock(timeout);
} }
bool lock() const override { return lock(portMAX_DELAY); }
bool unlock() const override { bool unlock() const override {
return lockable.unlock(); return lockable.unlock();
} }

View File

@ -4,8 +4,8 @@
namespace tt { namespace tt {
static LogEntry* logEntries = nullptr; static std::array<LogEntry, TT_LOG_ENTRY_COUNT> logEntries;
static unsigned int nextLogEntryIndex; static size_t nextLogEntryIndex;
/** /**
* This used to be a simple static value, but that crashes on device boot where early logging happens. * This used to be a simple static value, but that crashes on device boot where early logging happens.
@ -20,18 +20,8 @@ Mutex& getLogMutex() {
return *logMutex; return *logMutex;
} }
static void ensureLogEntriesExist() {
if (logEntries == nullptr) {
logEntries = new LogEntry[TT_LOG_ENTRY_COUNT];
assert(logEntries != nullptr);
nextLogEntryIndex = 0;
}
}
static void storeLog(LogLevel level, const char* format, va_list args) { static void storeLog(LogLevel level, const char* format, va_list args) {
if (getLogMutex().lock(5 / portTICK_PERIOD_MS)) { if (getLogMutex().lock(5 / portTICK_PERIOD_MS)) {
ensureLogEntriesExist();
logEntries[nextLogEntryIndex].level = level; logEntries[nextLogEntryIndex].level = level;
vsnprintf(logEntries[nextLogEntryIndex].message, TT_LOG_MESSAGE_SIZE, format, args); vsnprintf(logEntries[nextLogEntryIndex].message, TT_LOG_MESSAGE_SIZE, format, args);
@ -44,16 +34,12 @@ static void storeLog(LogLevel level, const char* format, va_list args) {
} }
} }
LogEntry* copyLogEntries(unsigned int& outIndex) { std::unique_ptr<std::array<LogEntry, TT_LOG_ENTRY_COUNT>> copyLogEntries(std::size_t& outIndex) {
if (getLogMutex().lock(5 / portTICK_PERIOD_MS)) { if (getLogMutex().lock(5 / portTICK_PERIOD_MS)) {
auto* newEntries = new LogEntry[TT_LOG_ENTRY_COUNT]; auto copy = std::make_unique<std::array<LogEntry, TT_LOG_ENTRY_COUNT>>(logEntries);
assert(newEntries != nullptr);
for (int i = 0; i < TT_LOG_ENTRY_COUNT; ++i) {
memcpy(&newEntries[i], &logEntries[i], sizeof(LogEntry));
}
outIndex = nextLogEntryIndex;
getLogMutex().unlock(); getLogMutex().unlock();
return newEntries; outIndex = nextLogEntryIndex;
return copy;
} else { } else {
return nullptr; return nullptr;
} }

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "LogMessages.h" #include "LogMessages.h"
#include <array>
#include <memory>
#ifdef ESP_PLATFORM #ifdef ESP_PLATFORM
#include <esp_log.h> #include <esp_log.h>
@ -38,7 +40,7 @@ struct LogEntry {
* The array size is TT_LOG_ENTRY_COUNT * The array size is TT_LOG_ENTRY_COUNT
* @param[out] outIndex the write index for the next log entry. * @param[out] outIndex the write index for the next log entry.
*/ */
LogEntry* copyLogEntries(unsigned int& outIndex); std::unique_ptr<std::array<LogEntry, TT_LOG_ENTRY_COUNT>> copyLogEntries(std::size_t& outIndex);
} // namespace tt } // namespace tt

View File

@ -39,24 +39,21 @@ private:
public: public:
using Lockable::lock;
explicit Mutex(Type type = Type::Normal); explicit Mutex(Type type = Type::Normal);
~Mutex() override = default; ~Mutex() final = default;
/** Attempt to lock the mutex. Blocks until timeout passes or lock is acquired. /** Attempt to lock the mutex. Blocks until timeout passes or lock is acquired.
* @param[in] timeout * @param[in] timeout
* @return success result * @return success result
*/ */
bool lock(TickType_t timeout) const override; bool lock(TickType_t timeout) const final;
/** Attempt to lock the mutex. Blocks until lock is acquired, without timeout.
* @return success result
*/
bool lock() const override { return lock(portMAX_DELAY); }
/** Attempt to unlock the mutex. /** Attempt to unlock the mutex.
* @return success result * @return success result
*/ */
bool unlock() const override; bool unlock() const final;
/** @return the owner of the thread */ /** @return the owner of the thread */
ThreadId getOwner() const; ThreadId getOwner() const;

View File

@ -4,7 +4,7 @@
namespace tt { namespace tt {
static inline struct QueueDefinition* createHandle(uint32_t maxCount, uint32_t initialCount) { static inline QueueHandle_t createHandle(uint32_t maxCount, uint32_t initialCount) {
assert((maxCount > 0U) && (initialCount <= maxCount)); assert((maxCount > 0U) && (initialCount <= maxCount));
if (maxCount == 1U) { if (maxCount == 1U) {
@ -21,7 +21,7 @@ static inline struct QueueDefinition* createHandle(uint32_t maxCount, uint32_t i
} }
} }
Semaphore::Semaphore(uint32_t maxCount, uint32_t initialCount) : handle(createHandle(maxCount, initialCount)){ Semaphore::Semaphore(uint32_t maxAvailable, uint32_t initialAvailable) : handle(createHandle(maxAvailable, initialAvailable)) {
assert(!TT_IS_IRQ_MODE()); assert(!TT_IS_IRQ_MODE());
tt_check(handle != nullptr); tt_check(handle != nullptr);
} }
@ -30,7 +30,7 @@ Semaphore::~Semaphore() {
assert(!TT_IS_IRQ_MODE()); assert(!TT_IS_IRQ_MODE());
} }
bool Semaphore::acquire(uint32_t timeout) const { bool Semaphore::acquire(TickType_t timeout) const {
if (TT_IS_IRQ_MODE()) { if (TT_IS_IRQ_MODE()) {
if (timeout != 0U) { if (timeout != 0U) {
return false; return false;
@ -45,7 +45,7 @@ bool Semaphore::acquire(uint32_t timeout) const {
} }
} }
} else { } else {
return xSemaphoreTake(handle.get(), (TickType_t)timeout) == pdPASS; return xSemaphoreTake(handle.get(), timeout) == pdPASS;
} }
} }
@ -63,13 +63,13 @@ bool Semaphore::release() const {
} }
} }
uint32_t Semaphore::getCount() const { uint32_t Semaphore::getAvailable() const {
if (TT_IS_IRQ_MODE()) { if (TT_IS_IRQ_MODE()) {
// TODO: uxSemaphoreGetCountFromISR is not supported on esp-idf 5.1.2 - perhaps later on? // TODO: uxSemaphoreGetCountFromISR is not supported on esp-idf 5.1.2 - perhaps later on?
#ifdef uxSemaphoreGetCountFromISR #ifdef uxSemaphoreGetCountFromISR
return uxSemaphoreGetCountFromISR(handle.get()); return uxSemaphoreGetCountFromISR(handle.get());
#else #else
return uxQueueMessagesWaitingFromISR((QueueHandle_t)hSemaphore); return uxQueueMessagesWaitingFromISR(handle.get());
#endif #endif
} else { } else {
return uxSemaphoreGetCount(handle.get()); return uxSemaphoreGetCount(handle.get());

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Thread.h" #include "Lockable.h"
#include <cassert> #include <cassert>
#include <memory> #include <memory>
@ -18,7 +18,7 @@ namespace tt {
* Wrapper for xSemaphoreCreateBinary (max count == 1) and xSemaphoreCreateCounting (max count > 1) * Wrapper for xSemaphoreCreateBinary (max count == 1) and xSemaphoreCreateCounting (max count > 1)
* Can be used from IRQ/ISR mode, but cannot be created/destroyed from such a context. * Can be used from IRQ/ISR mode, but cannot be created/destroyed from such a context.
*/ */
class Semaphore { class Semaphore final : public Lockable {
private: private:
@ -32,24 +32,39 @@ private:
std::unique_ptr<std::remove_pointer_t<QueueHandle_t>, SemaphoreHandleDeleter> handle; std::unique_ptr<std::remove_pointer_t<QueueHandle_t>, SemaphoreHandleDeleter> handle;
public: public:
using Lockable::lock;
/** /**
* Cannot be called from IRQ/ISR mode. * Cannot be called from IRQ/ISR mode.
* @param[in] maxCount The maximum count * @param[in] maxAvailable The maximum count
* @param[in] initialCount The initial count * @param[in] initialAvailable The initial count
*/ */
Semaphore(uint32_t maxCount, uint32_t initialCount); Semaphore(uint32_t maxAvailable, uint32_t initialAvailable);
/**
* Cannot be called from IRQ/ISR mode.
* @param[in] maxAvailable The maximum count
*/
explicit Semaphore(uint32_t maxAvailable) : Semaphore(maxAvailable, maxAvailable) {};
/** Cannot be called from IRQ/ISR mode. */ /** Cannot be called from IRQ/ISR mode. */
~Semaphore(); ~Semaphore() override;
Semaphore(Semaphore& other) : handle(std::move(other.handle)) {}
/** Acquire semaphore */ /** Acquire semaphore */
bool acquire(uint32_t timeout) const; bool acquire(TickType_t timeout) const;
/** Release semaphore */ /** Release semaphore */
bool release() const; bool release() const;
/** @return semaphore count */ bool lock(TickType_t timeout) const override { return acquire(timeout); }
uint32_t getCount() const;
bool unlock() const override { return release(); }
/** @return return the amount of times this semaphore can be acquired/locked */
uint32_t getAvailable() const;
}; };
} // namespace } // namespace

View File

@ -1,21 +1,13 @@
#include "StringUtils.h" #include "StringUtils.h"
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include <string>
namespace tt::string { namespace tt::string {
int findLastIndex(const char* text, size_t from_index, char find) {
for (int i = (int)from_index; i >= 0; i--) {
if (text[i] == find) {
return (int)i;
}
}
return -1;
}
bool getPathParent(const std::string& path, std::string& output) { bool getPathParent(const std::string& path, std::string& output) {
int index = findLastIndex(path.c_str(), path.length() - 1, '/'); auto index = path.find_last_of('/');
if (index == -1) { if (index == std::string::npos) {
return false; return false;
} else if (index == 0) { } else if (index == 0) {
output = "/"; output = "/";

View File

@ -7,15 +7,6 @@
namespace tt::string { namespace tt::string {
/**
* Find the last occurrence of a character.
* @param[in] text the text to search in
* @param[in] from_index the index to search from (searching from right to left)
* @param[in] find the character to search for
* @return the index of the found character, or -1 if none found
*/
int findLastIndex(const char* text, size_t from_index, char find);
/** /**
* Given a filesystem path as input, try and get the parent path. * Given a filesystem path as input, try and get the parent path.
* @param[in] path input path * @param[in] path input path

View File

@ -11,8 +11,8 @@ static portMUX_TYPE critical_mutex;
namespace tt::kernel::critical { namespace tt::kernel::critical {
TtCriticalInfo enter() { CriticalInfo enter() {
TtCriticalInfo info = { CriticalInfo info = {
.isrm = 0, .isrm = 0,
.fromIsr = TT_IS_ISR(), .fromIsr = TT_IS_ISR(),
.kernelRunning = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) .kernelRunning = (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)
@ -29,7 +29,7 @@ TtCriticalInfo enter() {
return info; return info;
} }
void exit(TtCriticalInfo info) { void exit(CriticalInfo info) {
if (info.fromIsr) { if (info.fromIsr) {
taskEXIT_CRITICAL_FROM_ISR(info.isrm); taskEXIT_CRITICAL_FROM_ISR(info.isrm);
} else if (info.kernelRunning) { } else if (info.kernelRunning) {

View File

@ -4,21 +4,21 @@
namespace tt::kernel::critical { namespace tt::kernel::critical {
typedef struct { struct CriticalInfo {
uint32_t isrm; uint32_t isrm;
bool fromIsr; bool fromIsr;
bool kernelRunning; bool kernelRunning;
} TtCriticalInfo; };
/** Enter a critical section /** Enter a critical section
* @return info on the status * @return info on the status
*/ */
TtCriticalInfo enter(); CriticalInfo enter();
/** /**
* Exit a critical section * Exit a critical section
* @param[in] info the info from when the critical section was started * @param[in] info the info from when the critical section was started
*/ */
void exit(TtCriticalInfo info); void exit(CriticalInfo info);
} // namespace } // namespace

View File

@ -23,7 +23,7 @@ static Dispatcher mainDispatcher;
static const hal::Configuration* hardwareConfig = nullptr; static const hal::Configuration* hardwareConfig = nullptr;
static void register_and_start_system_services() { static void registerAndStartSystemServices() {
TT_LOG_I(TAG, "Registering and starting system services"); TT_LOG_I(TAG, "Registering and starting system services");
addService(service::sdcard::manifest); addService(service::sdcard::manifest);
addService(service::wifi::manifest); addService(service::wifi::manifest);
@ -38,7 +38,7 @@ void initHeadless(const hal::Configuration& config) {
time::init(); time::init();
hal::init(config); hal::init(config);
network::ntp::init(); network::ntp::init();
register_and_start_system_services(); registerAndStartSystemServices();
} }

View File

@ -23,11 +23,12 @@ static Data dataArray[I2C_NUM_MAX];
const char* initModeToString(InitMode mode) { const char* initModeToString(InitMode mode) {
switch (mode) { switch (mode) {
case InitMode::ByTactility: using enum InitMode;
case ByTactility:
return TT_STRINGIFY(InitMode::ByTactility); return TT_STRINGIFY(InitMode::ByTactility);
case InitMode::ByExternal: case ByExternal:
return TT_STRINGIFY(InitMode::ByExternal); return TT_STRINGIFY(InitMode::ByExternal);
case InitMode::Disabled: case Disabled:
return TT_STRINGIFY(InitMode::Disabled); return TT_STRINGIFY(InitMode::Disabled);
} }
tt_crash("not implemented"); tt_crash("not implemented");

View File

@ -19,26 +19,27 @@ static std::list<SubscriptionData> subscriptions;
static const char* getEventName(SystemEvent event) { static const char* getEventName(SystemEvent event) {
switch (event) { switch (event) {
case SystemEvent::BootInitHalBegin: using enum SystemEvent;
return TT_STRINGIFY(SystemEvent::BootInitHalBegin); case BootInitHalBegin:
case SystemEvent::BootInitHalEnd: return TT_STRINGIFY(BootInitHalBegin);
return TT_STRINGIFY(SystemEvent::BootInitHalEnd); case BootInitHalEnd:
case SystemEvent::BootInitI2cBegin: return TT_STRINGIFY(BootInitHalEnd);
return TT_STRINGIFY(SystemEvent::BootInitI2cBegin); case BootInitI2cBegin:
case SystemEvent::BootInitI2cEnd: return TT_STRINGIFY(BootInitI2cBegin);
return TT_STRINGIFY(SystemEvent::BootInitI2cEnd); case BootInitI2cEnd:
case SystemEvent::BootInitLvglBegin: return TT_STRINGIFY(BootInitI2cEnd);
return TT_STRINGIFY(SystemEvent::BootInitLvglBegin); case BootInitLvglBegin:
case SystemEvent::BootInitLvglEnd: return TT_STRINGIFY(BootInitLvglBegin);
return TT_STRINGIFY(SystemEvent::BootInitLvglEnd); case BootInitLvglEnd:
case SystemEvent::BootSplash: return TT_STRINGIFY(BootInitLvglEnd);
return TT_STRINGIFY(SystemEvent::BootSplash); case BootSplash:
case SystemEvent::NetworkConnected: return TT_STRINGIFY(BootSplash);
return TT_STRINGIFY(SystemEvent::NetworkConnected); case NetworkConnected:
case SystemEvent::NetworkDisconnected: return TT_STRINGIFY(NetworkConnected);
return TT_STRINGIFY(SystemEvent::NetworkDisconnected); case NetworkDisconnected:
case SystemEvent::Time: return TT_STRINGIFY(NetworkDisconnected);
return TT_STRINGIFY(SystemEvent::Time); case Time:
return TT_STRINGIFY(Time);
} }
tt_crash(); // Missing case above tt_crash(); // Missing case above

View File

@ -4,18 +4,19 @@ namespace tt::service::wifi {
const char* radioStateToString(RadioState state) { const char* radioStateToString(RadioState state) {
switch (state) { switch (state) {
case RadioState::OnPending: using enum RadioState;
return TT_STRINGIFY(RadioState::OnPending); case OnPending:
case RadioState::On: return TT_STRINGIFY(OnPending);
return TT_STRINGIFY(RadioState::On); case On:
case RadioState::ConnectionPending: return TT_STRINGIFY(On);
return TT_STRINGIFY(RadioState::ConnectionPending); case ConnectionPending:
case RadioState::ConnectionActive: return TT_STRINGIFY(ConnectionPending);
return TT_STRINGIFY(RadioState::ConnectionActive); case ConnectionActive:
case RadioState::OffPending: return TT_STRINGIFY(ConnectionActive);
return TT_STRINGIFY(RadioState::OnPending); case OffPending:
case RadioState::Off: return TT_STRINGIFY(OnPending);
return TT_STRINGIFY(RadioState::Off); case Off:
return TT_STRINGIFY(Off);
} }
tt_crash("not implemented"); tt_crash("not implemented");
} }

View File

@ -69,6 +69,7 @@ struct Event {
struct ApRecord { struct ApRecord {
std::string ssid; std::string ssid;
int8_t rssi; int8_t rssi;
int32_t channel;
wifi_auth_mode_t auth_mode; wifi_auth_mode_t auth_mode;
}; };

View File

@ -268,6 +268,7 @@ std::vector<ApRecord> getScanResults() {
records.push_back((ApRecord) { records.push_back((ApRecord) {
.ssid = (const char*)wifi->scan_list[i].ssid, .ssid = (const char*)wifi->scan_list[i].ssid,
.rssi = wifi->scan_list[i].rssi, .rssi = wifi->scan_list[i].rssi,
.channel = wifi->scan_list[i].primary,
.auth_mode = wifi->scan_list[i].authmode .auth_mode = wifi->scan_list[i].authmode
}); });
} }
@ -389,7 +390,7 @@ static bool copy_scan_list(std::shared_ptr<Wifi> wifi) {
uint16_t record_count = wifi->scan_list_limit; uint16_t record_count = wifi->scan_list_limit;
esp_err_t scan_result = esp_wifi_scan_get_ap_records(&record_count, wifi->scan_list); esp_err_t scan_result = esp_wifi_scan_get_ap_records(&record_count, wifi->scan_list);
if (scan_result == ESP_OK) { if (scan_result == ESP_OK) {
uint16_t safe_record_count = TT_MIN(wifi->scan_list_limit, record_count); uint16_t safe_record_count = std::min(wifi->scan_list_limit, record_count);
wifi->scan_list_count = safe_record_count; wifi->scan_list_count = safe_record_count;
TT_LOG_I(TAG, "Scanned %u APs. Showing %u:", record_count, safe_record_count); TT_LOG_I(TAG, "Scanned %u APs. Showing %u:", record_count, safe_record_count);
for (uint16_t i = 0; i < safe_record_count; i++) { for (uint16_t i = 0; i < safe_record_count; i++) {
@ -720,65 +721,24 @@ static void dispatchConnect(std::shared_ptr<void> context) {
publish_event_simple(wifi, EventType::ConnectionPending); publish_event_simple(wifi, EventType::ConnectionPending);
wifi_config_t wifi_config = { wifi_config_t config;
.sta = { memset(&config, 0, sizeof(wifi_config_t));
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8). config.sta.channel = wifi_singleton->connection_target.channel;
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value config.sta.scan_method = WIFI_FAST_SCAN;
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards. config.sta.threshold.rssi = -127;
*/ config.sta.pmf_cfg.capable = true;
.ssid = {0},
.password = {0},
.scan_method = WIFI_ALL_CHANNEL_SCAN,
.bssid_set = false,
.bssid = { 0 },
.channel = 0,
.listen_interval = 0,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
.threshold = {
.rssi = 0,
.authmode = WIFI_AUTH_OPEN,
.rssi_5g_adjustment = 0
},
.pmf_cfg = {
.capable = false,
.required = false
},
.rm_enabled = 0,
.btm_enabled = 0,
.mbo_enabled = 0,
.ft_enabled = 0,
.owe_enabled = 0,
.transition_disable = 0,
.reserved = 0,
.sae_pwe_h2e = WPA3_SAE_PWE_BOTH,
.sae_pk_mode = WPA3_SAE_PK_MODE_AUTOMATIC,
.failure_retry_cnt = 1,
.he_dcm_set = 0,
.he_dcm_max_constellation_tx = 0,
.he_dcm_max_constellation_rx = 0,
.he_mcs9_enabled = 0,
.he_su_beamformee_disabled = 0,
.he_trig_su_bmforming_feedback_disabled = 0,
.he_trig_mu_bmforming_partial_feedback_disabled = 0,
.he_trig_cqi_feedback_disabled = 0,
.he_reserved = 0,
.sae_h2e_identifier = {0},
}
};
static_assert(sizeof(wifi_config.sta.ssid) == (sizeof(wifi_singleton->connection_target.ssid)-1), "SSID size mismatch"); static_assert(sizeof(config.sta.ssid) == (sizeof(wifi_singleton->connection_target.ssid)-1), "SSID size mismatch");
memcpy(wifi_config.sta.ssid, wifi_singleton->connection_target.ssid, sizeof(wifi_config.sta.ssid)); memcpy(config.sta.ssid, wifi_singleton->connection_target.ssid, sizeof(config.sta.ssid));
memcpy(wifi_config.sta.password, wifi_singleton->connection_target.password, sizeof(wifi_config.sta.password));
if (wifi_singleton->connection_target.password[0] != 0x00U) { if (wifi_singleton->connection_target.password[0] != 0x00) {
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK; memcpy(config.sta.password, wifi_singleton->connection_target.password, sizeof(config.sta.password));
} else { config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
} }
TT_LOG_I(TAG, "esp_wifi_set_config()"); TT_LOG_I(TAG, "esp_wifi_set_config()");
esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &config);
if (set_config_result != ESP_OK) { if (set_config_result != ESP_OK) {
wifi->setRadioState(RadioState::On); wifi->setRadioState(RadioState::On);
TT_LOG_E(TAG, "Failed to set wifi config (%s)", esp_err_to_name(set_config_result)); TT_LOG_E(TAG, "Failed to set wifi config (%s)", esp_err_to_name(set_config_result));
@ -802,7 +762,7 @@ static void dispatchConnect(std::shared_ptr<void> context) {
TT_LOG_I(TAG, "Waiting for EventFlag by event_handler()"); TT_LOG_I(TAG, "Waiting for EventFlag by event_handler()");
if (bits & WIFI_CONNECTED_BIT) { if (bits & WIFI_CONNECTED_BIT) {
wifi->setSecureConnection(wifi_config.sta.password[0] != 0x00U); wifi->setSecureConnection(config.sta.password[0] != 0x00U);
wifi->setRadioState(RadioState::ConnectionActive); wifi->setRadioState(RadioState::ConnectionActive);
publish_event_simple(wifi, EventType::ConnectionSuccess); 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);
@ -842,49 +802,15 @@ static void dispatchDisconnectButKeepActive(std::shared_ptr<void> context) {
return; return;
} }
wifi_config_t wifi_config = { wifi_config_t config;
.sta = { memset(&config, 0, sizeof(wifi_config_t));
.ssid = {0}, config.sta.channel = wifi_singleton->connection_target.channel;
.password = {0}, config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
.scan_method = WIFI_ALL_CHANNEL_SCAN, config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
.bssid_set = false, config.sta.threshold.rssi = -127;
.bssid = { 0 }, config.sta.pmf_cfg.capable = true;
.channel = 0,
.listen_interval = 0,
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
.threshold = {
.rssi = 0,
.authmode = WIFI_AUTH_OPEN,
.rssi_5g_adjustment = 0
},
.pmf_cfg = {
.capable = false,
.required = false,
},
.rm_enabled = false,
.btm_enabled = false,
.mbo_enabled = false,
.ft_enabled = false,
.owe_enabled = false,
.transition_disable = false,
.reserved = 0,
.sae_pwe_h2e = WPA3_SAE_PWE_UNSPECIFIED,
.sae_pk_mode = WPA3_SAE_PK_MODE_AUTOMATIC,
.failure_retry_cnt = 0,
.he_dcm_set = false,
.he_dcm_max_constellation_tx = false,
.he_dcm_max_constellation_rx = false,
.he_mcs9_enabled = false,
.he_su_beamformee_disabled = false,
.he_trig_su_bmforming_feedback_disabled = false,
.he_trig_mu_bmforming_partial_feedback_disabled = false,
.he_trig_cqi_feedback_disabled = false,
.he_reserved = 0,
.sae_h2e_identifier = {0},
},
};
esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &wifi_config); esp_err_t set_config_result = esp_wifi_set_config(WIFI_IF_STA, &config);
if (set_config_result != ESP_OK) { if (set_config_result != ESP_OK) {
// TODO: disable radio, because radio state is in limbo between off and on // TODO: disable radio, because radio state is in limbo between off and on
wifi->setRadioState(RadioState::Off); wifi->setRadioState(RadioState::Off);
@ -948,7 +874,7 @@ public:
wifi_singleton->autoConnectTimer = std::make_unique<Timer>(Timer::Type::Periodic, onAutoConnectTimer, wifi_singleton); wifi_singleton->autoConnectTimer = std::make_unique<Timer>(Timer::Type::Periodic, onAutoConnectTimer, wifi_singleton);
// We want to try and scan more often in case of startup or scan lock failure // We want to try and scan more often in case of startup or scan lock failure
wifi_singleton->autoConnectTimer->start(TT_MIN(2000, AUTO_SCAN_INTERVAL)); wifi_singleton->autoConnectTimer->start(std::min(2000, AUTO_SCAN_INTERVAL));
if (settings::shouldEnableOnBoot()) { if (settings::shouldEnableOnBoot()) {
TT_LOG_I(TAG, "Auto-enabling due to setting"); TT_LOG_I(TAG, "Auto-enabling due to setting");

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "WifiGlobals.h" #include "WifiGlobals.h"
#include <cstdint>
namespace tt::service::wifi::settings { namespace tt::service::wifi::settings {
@ -13,6 +14,7 @@ namespace tt::service::wifi::settings {
struct WifiApSettings { struct WifiApSettings {
char ssid[TT_WIFI_SSID_LIMIT + 1] = { 0 }; char ssid[TT_WIFI_SSID_LIMIT + 1] = { 0 };
char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT + 1] = { 0 }; char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT + 1] = { 0 };
int32_t channel = 0;
bool auto_connect = true; bool auto_connect = true;
}; };

View File

@ -0,0 +1,39 @@
#include "Lockable.h"
#include "Semaphore.h"
#include "doctest.h"
#include <Mutex.h>
using namespace tt;
TEST_CASE("withLock() locks correctly on Semaphore") {
auto semaphore = std::make_shared<Semaphore>(2U);
semaphore->withLock([semaphore](){
CHECK_EQ(semaphore->getAvailable(), 1);
});
}
TEST_CASE("withLock() unlocks correctly on Semaphore") {
auto semaphore = std::make_shared<Semaphore>(2U);
semaphore->withLock([=](){
// NO-OP
});
CHECK_EQ(semaphore->getAvailable(), 2);
}
TEST_CASE("withLock() locks correctly on Mutex") {
auto mutex = std::make_shared<Mutex>();
mutex->withLock([mutex](){
CHECK_EQ(mutex->lock(1), false);
});
}
TEST_CASE("withLock() unlocks correctly on Mutex") {
auto mutex = std::make_shared<Mutex>();
mutex->withLock([=](){
// NO-OP
});
CHECK_EQ(mutex->lock(1), true);
CHECK_EQ(mutex->unlock(), true);
}

View File

@ -32,3 +32,22 @@ TEST_CASE("a mutex can block a thread") {
thread.join(); thread.join();
} }
TEST_CASE("a Mutex can be locked exactly once") {
auto mutex = Mutex(Mutex::Type::Normal);
CHECK_EQ(mutex.lock(0), true);
CHECK_EQ(mutex.lock(0), false);
CHECK_EQ(mutex.unlock(), true);
}
TEST_CASE("unlocking a Mutex without locking returns false") {
auto mutex = Mutex(Mutex::Type::Normal);
CHECK_EQ(mutex.unlock(), false);
}
TEST_CASE("unlocking a Mutex twice returns false on the second attempt") {
auto mutex = Mutex(Mutex::Type::Normal);
CHECK_EQ(mutex.lock(0), true);
CHECK_EQ(mutex.unlock(), true);
CHECK_EQ(mutex.unlock(), false);
}

View File

@ -0,0 +1,35 @@
#include "doctest.h"
#include "Semaphore.h"
using namespace tt;
// We want a distinct test for 1 item, because it creates the Semaphore differently
TEST_CASE("a Semaphore with max count of 1 can be acquired exactly once") {
auto semaphore = Semaphore(1);
CHECK_EQ(semaphore.acquire(0), true);
CHECK_EQ(semaphore.getAvailable(), 0);
CHECK_EQ(semaphore.acquire(0), false);
CHECK_EQ(semaphore.release(), true);
CHECK_EQ(semaphore.getAvailable(), 1);
}
TEST_CASE("a Semaphore with max count of 2 can be acquired exactly twice") {
auto semaphore = Semaphore(2);
CHECK_EQ(semaphore.acquire(0), true);
CHECK_EQ(semaphore.getAvailable(), 1);
CHECK_EQ(semaphore.acquire(0), true);
CHECK_EQ(semaphore.getAvailable(), 0);
CHECK_EQ(semaphore.acquire(0), false);
CHECK_EQ(semaphore.release(), true);
CHECK_EQ(semaphore.getAvailable(), 1);
CHECK_EQ(semaphore.release(), true);
CHECK_EQ(semaphore.getAvailable(), 2);
}
TEST_CASE("the semaphore count should be correct initially") {
auto semaphore_a = Semaphore(2);
CHECK_EQ(semaphore_a.getAvailable(), 2);
auto semaphore_b = Semaphore(2, 0);
CHECK_EQ(semaphore_b.getAvailable(), 0);
}