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
54 changed files with 518 additions and 531 deletions
@@ -23,7 +23,7 @@ static Dispatcher mainDispatcher;
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");
addService(service::sdcard::manifest);
addService(service::wifi::manifest);
@@ -38,7 +38,7 @@ void initHeadless(const hal::Configuration& config) {
time::init();
hal::init(config);
network::ntp::init();
register_and_start_system_services();
registerAndStartSystemServices();
}
+4 -3
View File
@@ -23,11 +23,12 @@ static Data dataArray[I2C_NUM_MAX];
const char* initModeToString(InitMode mode) {
switch (mode) {
case InitMode::ByTactility:
using enum InitMode;
case ByTactility:
return TT_STRINGIFY(InitMode::ByTactility);
case InitMode::ByExternal:
case ByExternal:
return TT_STRINGIFY(InitMode::ByExternal);
case InitMode::Disabled:
case Disabled:
return TT_STRINGIFY(InitMode::Disabled);
}
tt_crash("not implemented");
@@ -19,26 +19,27 @@ static std::list<SubscriptionData> subscriptions;
static const char* getEventName(SystemEvent event) {
switch (event) {
case SystemEvent::BootInitHalBegin:
return TT_STRINGIFY(SystemEvent::BootInitHalBegin);
case SystemEvent::BootInitHalEnd:
return TT_STRINGIFY(SystemEvent::BootInitHalEnd);
case SystemEvent::BootInitI2cBegin:
return TT_STRINGIFY(SystemEvent::BootInitI2cBegin);
case SystemEvent::BootInitI2cEnd:
return TT_STRINGIFY(SystemEvent::BootInitI2cEnd);
case SystemEvent::BootInitLvglBegin:
return TT_STRINGIFY(SystemEvent::BootInitLvglBegin);
case SystemEvent::BootInitLvglEnd:
return TT_STRINGIFY(SystemEvent::BootInitLvglEnd);
case SystemEvent::BootSplash:
return TT_STRINGIFY(SystemEvent::BootSplash);
case SystemEvent::NetworkConnected:
return TT_STRINGIFY(SystemEvent::NetworkConnected);
case SystemEvent::NetworkDisconnected:
return TT_STRINGIFY(SystemEvent::NetworkDisconnected);
case SystemEvent::Time:
return TT_STRINGIFY(SystemEvent::Time);
using enum SystemEvent;
case BootInitHalBegin:
return TT_STRINGIFY(BootInitHalBegin);
case BootInitHalEnd:
return TT_STRINGIFY(BootInitHalEnd);
case BootInitI2cBegin:
return TT_STRINGIFY(BootInitI2cBegin);
case BootInitI2cEnd:
return TT_STRINGIFY(BootInitI2cEnd);
case BootInitLvglBegin:
return TT_STRINGIFY(BootInitLvglBegin);
case BootInitLvglEnd:
return TT_STRINGIFY(BootInitLvglEnd);
case BootSplash:
return TT_STRINGIFY(BootSplash);
case NetworkConnected:
return TT_STRINGIFY(NetworkConnected);
case NetworkDisconnected:
return TT_STRINGIFY(NetworkDisconnected);
case Time:
return TT_STRINGIFY(Time);
}
tt_crash(); // Missing case above
+13 -12
View File
@@ -4,18 +4,19 @@ namespace tt::service::wifi {
const char* radioStateToString(RadioState state) {
switch (state) {
case RadioState::OnPending:
return TT_STRINGIFY(RadioState::OnPending);
case RadioState::On:
return TT_STRINGIFY(RadioState::On);
case RadioState::ConnectionPending:
return TT_STRINGIFY(RadioState::ConnectionPending);
case RadioState::ConnectionActive:
return TT_STRINGIFY(RadioState::ConnectionActive);
case RadioState::OffPending:
return TT_STRINGIFY(RadioState::OnPending);
case RadioState::Off:
return TT_STRINGIFY(RadioState::Off);
using enum RadioState;
case OnPending:
return TT_STRINGIFY(OnPending);
case On:
return TT_STRINGIFY(On);
case ConnectionPending:
return TT_STRINGIFY(ConnectionPending);
case ConnectionActive:
return TT_STRINGIFY(ConnectionActive);
case OffPending:
return TT_STRINGIFY(OnPending);
case Off:
return TT_STRINGIFY(Off);
}
tt_crash("not implemented");
}
@@ -69,6 +69,7 @@ struct Event {
struct ApRecord {
std::string ssid;
int8_t rssi;
int32_t channel;
wifi_auth_mode_t auth_mode;
};
@@ -268,6 +268,7 @@ std::vector<ApRecord> getScanResults() {
records.push_back((ApRecord) {
.ssid = (const char*)wifi->scan_list[i].ssid,
.rssi = wifi->scan_list[i].rssi,
.channel = wifi->scan_list[i].primary,
.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;
esp_err_t scan_result = esp_wifi_scan_get_ap_records(&record_count, wifi->scan_list);
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;
TT_LOG_I(TAG, "Scanned %u APs. Showing %u:", record_count, safe_record_count);
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);
wifi_config_t wifi_config = {
.sta = {
/* Authmode threshold resets to WPA2 as default if password matches WPA2 standards (pasword len => 8).
* If you want to connect the device to deprecated WEP/WPA networks, Please set the threshold value
* to WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK and set the password with length and format matching to
* WIFI_AUTH_WEP/WIFI_AUTH_WPA_PSK standards.
*/
.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},
}
};
wifi_config_t config;
memset(&config, 0, sizeof(wifi_config_t));
config.sta.channel = wifi_singleton->connection_target.channel;
config.sta.scan_method = WIFI_FAST_SCAN;
config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
config.sta.threshold.rssi = -127;
config.sta.pmf_cfg.capable = true;
static_assert(sizeof(wifi_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(wifi_config.sta.password, wifi_singleton->connection_target.password, sizeof(wifi_config.sta.password));
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));
if (wifi_singleton->connection_target.password[0] != 0x00U) {
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_WPA3_PSK;
} else {
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
if (wifi_singleton->connection_target.password[0] != 0x00) {
memcpy(config.sta.password, wifi_singleton->connection_target.password, sizeof(config.sta.password));
config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
}
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) {
wifi->setRadioState(RadioState::On);
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()");
if (bits & WIFI_CONNECTED_BIT) {
wifi->setSecureConnection(wifi_config.sta.password[0] != 0x00U);
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);
@@ -842,49 +802,15 @@ static void dispatchDisconnectButKeepActive(std::shared_ptr<void> context) {
return;
}
wifi_config_t wifi_config = {
.sta = {
.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 = 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},
},
};
wifi_config_t config;
memset(&config, 0, sizeof(wifi_config_t));
config.sta.channel = wifi_singleton->connection_target.channel;
config.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
config.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
config.sta.threshold.rssi = -127;
config.sta.pmf_cfg.capable = true;
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) {
// TODO: disable radio, because radio state is in limbo between off and on
wifi->setRadioState(RadioState::Off);
@@ -948,7 +874,7 @@ public:
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
wifi_singleton->autoConnectTimer->start(TT_MIN(2000, AUTO_SCAN_INTERVAL));
wifi_singleton->autoConnectTimer->start(std::min(2000, AUTO_SCAN_INTERVAL));
if (settings::shouldEnableOnBoot()) {
TT_LOG_I(TAG, "Auto-enabling due to setting");
@@ -1,6 +1,7 @@
#pragma once
#include "WifiGlobals.h"
#include <cstdint>
namespace tt::service::wifi::settings {
@@ -13,6 +14,7 @@ namespace tt::service::wifi::settings {
struct WifiApSettings {
char ssid[TT_WIFI_SSID_LIMIT + 1] = { 0 };
char password[TT_WIFI_CREDENTIALS_PASSWORD_LIMIT + 1] = { 0 };
int32_t channel = 0;
bool auto_connect = true;
};