TactilityCore improvements (#187)
FreeRTOS handles were stored plainly and they were deleted in the destructor of classes. This meant that if a class were to be copied, the destructor would be called twice on the same handles and lead to double-free. Seha on Discord suggested to fix this by using `std::unique_ptr` with a custom deletion function. The changes affect: - Thread - Semaphore - Mutex - StreamBuffer - Timer - MessageQueue - EventFlag Thread changes: - Removal of the hack with the `Data` struct - Thread's main body is now just a private static function inside the class. - The C functions were relocated to static class members PubSub changes: - Refactored pubsub into class - Renamed files to `PubSub` instead of `Pubsub` - `PubSubSubscription` is now a private inner struct and `PubSub` only exposes `SubscriptionHandle` Lockable, ScopedLockable, Mutex: - Added `lock()` method that locks indefinitely - Remove deprecated `acquire()` and `release()` methods - Removed `TtWaitForever` in favour of `portMAX_DELAY`
This commit is contained in:
committed by
GitHub
parent
c2edbad0fb
commit
686f7cce83
@@ -6,15 +6,15 @@ namespace tt::app {
|
||||
#define TAG "app"
|
||||
|
||||
void AppInstance::setState(State newState) {
|
||||
mutex.acquire(TtWaitForever);
|
||||
mutex.lock();
|
||||
state = newState;
|
||||
mutex.release();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
State AppInstance::getState() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
mutex.lock();
|
||||
auto result = state;
|
||||
mutex.release();
|
||||
mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -30,22 +30,22 @@ const AppManifest& AppInstance::getManifest() const {
|
||||
}
|
||||
|
||||
Flags AppInstance::getFlags() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
mutex.lock();
|
||||
auto result = flags;
|
||||
mutex.release();
|
||||
mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
void AppInstance::setFlags(Flags newFlags) {
|
||||
mutex.acquire(TtWaitForever);
|
||||
mutex.lock();
|
||||
flags = newFlags;
|
||||
mutex.release();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
std::shared_ptr<const Bundle> AppInstance::getParameters() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
mutex.lock();
|
||||
std::shared_ptr<const Bundle> result = parameters;
|
||||
mutex.release();
|
||||
mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ static Mutex hash_mutex(Mutex::Type::Normal);
|
||||
void addApp(const AppManifest& manifest) {
|
||||
TT_LOG_I(TAG, "Registering manifest %s", manifest.id.c_str());
|
||||
|
||||
hash_mutex.acquire(TtWaitForever);
|
||||
hash_mutex.lock();
|
||||
|
||||
if (!app_manifest_map.contains(manifest.id)) {
|
||||
app_manifest_map[manifest.id] = std::make_shared<AppManifest>(manifest);
|
||||
@@ -23,13 +23,13 @@ void addApp(const AppManifest& manifest) {
|
||||
TT_LOG_E(TAG, "App id in use: %s", manifest.id.c_str());
|
||||
}
|
||||
|
||||
hash_mutex.release();
|
||||
hash_mutex.unlock();
|
||||
}
|
||||
|
||||
_Nullable std::shared_ptr<AppManifest> findAppById(const std::string& id) {
|
||||
hash_mutex.acquire(TtWaitForever);
|
||||
hash_mutex.lock();
|
||||
auto result = app_manifest_map.find(id);
|
||||
hash_mutex.release();
|
||||
hash_mutex.unlock();
|
||||
if (result != app_manifest_map.end()) {
|
||||
return result->second;
|
||||
} else {
|
||||
@@ -39,11 +39,11 @@ _Nullable std::shared_ptr<AppManifest> findAppById(const std::string& id) {
|
||||
|
||||
std::vector<std::shared_ptr<AppManifest>> getApps() {
|
||||
std::vector<std::shared_ptr<AppManifest>> manifests;
|
||||
hash_mutex.acquire(TtWaitForever);
|
||||
hash_mutex.lock();
|
||||
for (const auto& item: app_manifest_map) {
|
||||
manifests.push_back(item.second);
|
||||
}
|
||||
hash_mutex.release();
|
||||
hash_mutex.unlock();
|
||||
return manifests;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,11 @@ private:
|
||||
public:
|
||||
|
||||
void lock() const {
|
||||
tt_check(mutex.acquire(1000) == TtStatusOk);
|
||||
tt_check(mutex.lock(1000));
|
||||
}
|
||||
|
||||
void unlock() const {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
tt_check(mutex.unlock());
|
||||
}
|
||||
|
||||
void onShow(AppContext& app, lv_obj_t* parent) override;
|
||||
|
||||
@@ -115,12 +115,12 @@ void I2cScannerApp::onShow(AppContext& app, lv_obj_t* parent) {
|
||||
|
||||
void I2cScannerApp::onHide(AppContext& app) {
|
||||
bool isRunning = false;
|
||||
if (mutex.acquire(250 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(250 / portTICK_PERIOD_MS)) {
|
||||
auto* timer = scanTimer.get();
|
||||
if (timer != nullptr) {
|
||||
isRunning = timer->isRunning();
|
||||
}
|
||||
mutex.release();
|
||||
mutex.unlock();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
@@ -158,9 +158,9 @@ void I2cScannerApp::onScanTimerCallback(TT_UNUSED std::shared_ptr<void> context)
|
||||
// endregion Callbacks
|
||||
|
||||
bool I2cScannerApp::getPort(i2c_port_t* outPort) {
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
*outPort = this->port;
|
||||
assert(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
return true;
|
||||
} else {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "getPort");
|
||||
@@ -169,9 +169,9 @@ bool I2cScannerApp::getPort(i2c_port_t* outPort) {
|
||||
}
|
||||
|
||||
bool I2cScannerApp::addAddressToList(uint8_t address) {
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
scannedAddresses.push_back(address);
|
||||
assert(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
return true;
|
||||
} else {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "addAddressToList");
|
||||
@@ -180,9 +180,9 @@ bool I2cScannerApp::addAddressToList(uint8_t address) {
|
||||
}
|
||||
|
||||
bool I2cScannerApp::shouldStopScanTimer() {
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
bool is_scanning = scanState == ScanStateScanning;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
return !is_scanning;
|
||||
} else {
|
||||
return true;
|
||||
@@ -222,9 +222,9 @@ void I2cScannerApp::onScanTimer() {
|
||||
|
||||
bool I2cScannerApp::hasScanThread() {
|
||||
bool has_thread;
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
has_thread = scanTimer != nullptr;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
return has_thread;
|
||||
} else {
|
||||
// Unsafe way
|
||||
@@ -238,7 +238,7 @@ void I2cScannerApp::startScanning() {
|
||||
stopScanning();
|
||||
}
|
||||
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
scannedAddresses.clear();
|
||||
|
||||
lv_obj_add_flag(scanListWidget, LV_OBJ_FLAG_HIDDEN);
|
||||
@@ -250,16 +250,16 @@ void I2cScannerApp::startScanning() {
|
||||
onScanTimerCallback
|
||||
);
|
||||
scanTimer->start(10);
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
} else {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "startScanning");
|
||||
}
|
||||
}
|
||||
void I2cScannerApp::stopScanning() {
|
||||
if (mutex.acquire(250 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(250 / portTICK_PERIOD_MS)) {
|
||||
assert(scanTimer != nullptr);
|
||||
scanState = ScanStateStopped;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
}
|
||||
@@ -271,11 +271,11 @@ void I2cScannerApp::onSelectBus(lv_event_t* event) {
|
||||
auto i2c_devices = tt::getConfiguration()->hardware->i2c;
|
||||
assert(selected < i2c_devices.size());
|
||||
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
scannedAddresses.clear();
|
||||
port = i2c_devices[selected].port;
|
||||
scanState = ScanStateInitial;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
|
||||
updateViews();
|
||||
}
|
||||
@@ -293,7 +293,7 @@ void I2cScannerApp::onPressScan(TT_UNUSED lv_event_t* event) {
|
||||
}
|
||||
|
||||
void I2cScannerApp::updateViews() {
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
if (scanState == ScanStateScanning) {
|
||||
lv_label_set_text(scanButtonLabelWidget, STOP_SCAN_TEXT);
|
||||
lv_obj_remove_flag(portDropdownWidget, LV_OBJ_FLAG_CLICKABLE);
|
||||
@@ -317,7 +317,7 @@ void I2cScannerApp::updateViews() {
|
||||
lv_obj_add_flag(scanListWidget, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
} else {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "updateViews");
|
||||
}
|
||||
@@ -333,12 +333,12 @@ void I2cScannerApp::updateViewsSafely() {
|
||||
}
|
||||
|
||||
void I2cScannerApp::onScanTimerFinished() {
|
||||
if (mutex.acquire(100 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
if (mutex.lock(100 / portTICK_PERIOD_MS)) {
|
||||
if (scanState == ScanStateScanning) {
|
||||
scanState = ScanStateStopped;
|
||||
updateViewsSafely();
|
||||
}
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
} else {
|
||||
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "onScanTimerFinished");
|
||||
}
|
||||
|
||||
@@ -1,47 +1,46 @@
|
||||
#include "app/wificonnect/State.h"
|
||||
#include "Check.h"
|
||||
#include <cstring>
|
||||
|
||||
namespace tt::app::wificonnect {
|
||||
|
||||
void State::setConnectionError(bool error) {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
lock.lock();
|
||||
connectionError = error;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
bool State::hasConnectionError() const {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
lock.lock();
|
||||
auto result = connectionError;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
lock.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
void State::setApSettings(const service::wifi::settings::WifiApSettings* newSettings) {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
lock.lock();
|
||||
memcpy(&this->apSettings, newSettings, sizeof(service::wifi::settings::WifiApSettings));
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
const service::wifi::settings::WifiApSettings& State::lockApSettings() {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
lock.lock();
|
||||
return apSettings;
|
||||
}
|
||||
|
||||
void State::unlockApSettings() {
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
void State::setConnecting(bool isConnecting) {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
lock.lock();
|
||||
connecting = isConnecting;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
bool State::isConnecting() const {
|
||||
tt_check(lock.acquire(TtWaitForever) == TtStatusOk);
|
||||
lock.lock();
|
||||
auto result = connecting;
|
||||
tt_check(lock.release() == TtStatusOk);
|
||||
lock.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,7 @@ static void onConnect(const service::wifi::settings::WifiApSettings* ap_settings
|
||||
}
|
||||
|
||||
WifiConnect::WifiConnect() {
|
||||
auto wifi_pubsub = service::wifi::getPubsub();
|
||||
wifiSubscription = tt_pubsub_subscribe(wifi_pubsub, &eventCallback, this);
|
||||
wifiSubscription = service::wifi::getPubsub()->subscribe(&eventCallback, this);
|
||||
bindings = (Bindings) {
|
||||
.onConnectSsid = onConnect,
|
||||
.onConnectSsidContext = this,
|
||||
@@ -65,16 +64,15 @@ WifiConnect::WifiConnect() {
|
||||
}
|
||||
|
||||
WifiConnect::~WifiConnect() {
|
||||
auto pubsub = service::wifi::getPubsub();
|
||||
tt_pubsub_unsubscribe(pubsub, wifiSubscription);
|
||||
service::wifi::getPubsub()->unsubscribe(wifiSubscription);
|
||||
}
|
||||
|
||||
void WifiConnect::lock() {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
}
|
||||
|
||||
void WifiConnect::unlock() {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void WifiConnect::requestViewUpdate() {
|
||||
|
||||
@@ -1,63 +1,61 @@
|
||||
#include <Check.h>
|
||||
#include "app/wifimanage/WifiManagePrivate.h"
|
||||
|
||||
namespace tt::app::wifimanage {
|
||||
|
||||
void State::setScanning(bool isScanning) {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
scanning = isScanning;
|
||||
scannedAfterRadioOn |= isScanning;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void State::setRadioState(service::wifi::RadioState state) {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
radioState = state;
|
||||
if (radioState == service::wifi::RadioState::Off) {
|
||||
scannedAfterRadioOn = false;
|
||||
}
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
service::wifi::RadioState State::getRadioState() const {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
auto result = radioState;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
bool State::isScanning() const {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
bool result = scanning;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
const std::vector<service::wifi::ApRecord>& State::lockApRecords() const {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
return apRecords;
|
||||
}
|
||||
|
||||
void State::unlockApRecords() const {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void State::updateApRecords() {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
apRecords = service::wifi::getScanResults();
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void State::setConnectSsid(const std::string& ssid) {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
connectSsid = ssid;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
std::string State::getConnectSsid() const {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
auto result = connectSsid;
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,11 +64,11 @@ WifiManage::WifiManage() {
|
||||
}
|
||||
|
||||
void WifiManage::lock() {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
mutex.lock();
|
||||
}
|
||||
|
||||
void WifiManage::unlock() {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void WifiManage::requestViewUpdate() {
|
||||
@@ -111,8 +111,7 @@ static void wifiManageEventCallback(const void* message, void* context) {
|
||||
}
|
||||
|
||||
void WifiManage::onShow(AppContext& app, lv_obj_t* parent) {
|
||||
auto wifi_pubsub = service::wifi::getPubsub();
|
||||
wifiSubscription = tt_pubsub_subscribe(wifi_pubsub, &wifiManageEventCallback, this);
|
||||
wifiSubscription = service::wifi::getPubsub()->subscribe(&wifiManageEventCallback, this);
|
||||
|
||||
// State update (it has its own locking)
|
||||
state.setRadioState(service::wifi::getRadioState());
|
||||
@@ -139,8 +138,7 @@ void WifiManage::onShow(AppContext& app, lv_obj_t* parent) {
|
||||
|
||||
void WifiManage::onHide(TT_UNUSED AppContext& app) {
|
||||
lock();
|
||||
auto wifi_pubsub = service::wifi::getPubsub();
|
||||
tt_pubsub_unsubscribe(wifi_pubsub, wifiSubscription);
|
||||
service::wifi::getPubsub()->unsubscribe(wifiSubscription);
|
||||
wifiSubscription = nullptr;
|
||||
isViewEnabled = false;
|
||||
unlock();
|
||||
|
||||
Reference in New Issue
Block a user