Various services improved (#110)

This commit is contained in:
Ken Van Hoeylandt
2024-12-06 23:16:29 +01:00
committed by GitHub
parent 36bb25deba
commit d52fe52d96
32 changed files with 771 additions and 636 deletions
@@ -1,3 +1,4 @@
#include <Dispatcher.h>
#include "TactilityHeadless.h"
#include "hal/Configuration.h"
#include "hal/Hal_i.h"
@@ -15,6 +16,8 @@ namespace tt {
namespace service::wifi { extern const ServiceManifest manifest; }
namespace service::sdcard { extern const ServiceManifest manifest; }
static Dispatcher mainDispatcher;
static const service::ServiceManifest* const system_services[] = {
&service::sdcard::manifest,
&service::wifi::manifest
@@ -40,6 +43,11 @@ void initHeadless(const hal::Configuration& config) {
register_and_start_system_services();
}
Dispatcher& getMainDispatcher() {
return mainDispatcher;
}
namespace hal {
const Configuration& getConfiguration() {
@@ -2,11 +2,14 @@
#include "hal/Configuration.h"
#include "TactilityHeadlessConfig.h"
#include "Dispatcher.h"
namespace tt {
void initHeadless(const hal::Configuration& config);
Dispatcher& getMainDispatcher();
} // namespace
namespace tt::hal {
@@ -1,11 +1,13 @@
#include <cstdlib>
#include "Mutex.h"
#include "Timer.h"
#include "service/ServiceContext.h"
#include "TactilityCore.h"
#include "TactilityHeadless.h"
#include "service/ServiceRegistry.h"
#include <cstdlib>
#define TAG "sdcard_service"
namespace tt::service::sdcard {
@@ -15,21 +17,11 @@ extern const ServiceManifest manifest;
struct ServiceData {
Mutex mutex;
Thread thread = Thread(
"sdcard",
3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard
&sdcard_task,
nullptr
);
std::unique_ptr<Timer> updateTimer;
hal::sdcard::State lastState = hal::sdcard::StateUnmounted;
bool interrupted = false;
ServiceData() {
thread.setPriority(Thread::PriorityLow);
}
void lock() const {
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
bool lock(TickType_t timeout) const {
return mutex.acquire(timeout) == TtStatusOk;
}
void unlock() const {
@@ -38,46 +30,36 @@ struct ServiceData {
};
static int32_t sdcard_task(TT_UNUSED void* context) {
delay_ms(20); // TODO: Make service instance findable earlier on (but expose "starting" state?)
auto service = findServiceById(manifest.id);
if (service == nullptr) {
TT_LOG_E(TAG, "Service not found");
return -1;
static void onUpdate(std::shared_ptr<void> context) {
auto data = std::static_pointer_cast<ServiceData>(context);
if (!data->lock(50)) {
TT_LOG_W(TAG, "Failed to acquire lock");
return;
}
auto data = std::static_pointer_cast<ServiceData>(service->getData());
hal::sdcard::State new_state = hal::sdcard::getState();
bool interrupted = false;
if (new_state == hal::sdcard::StateError) {
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
hal::sdcard::unmount(ms_to_ticks(1000));
}
do {
data->lock();
if (new_state != data->lastState) {
data->lastState = new_state;
}
interrupted = data->interrupted;
hal::sdcard::State new_state = hal::sdcard::getState();
if (new_state == hal::sdcard::StateError) {
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
hal::sdcard::unmount(ms_to_ticks(1000));
}
if (new_state != data->lastState) {
data->lastState = new_state;
}
data->lock();
delay_ms(2000);
} while (!interrupted);
return 0;
data->unlock();
}
static void onStart(ServiceContext& service) {
if (hal::getConfiguration().sdcard != nullptr) {
auto data = std::make_shared<ServiceData>();
service.setData(data);
data->thread.start();
data->updateTimer = std::make_unique<Timer>(Timer::TypePeriodic, onUpdate, data);
// We want to try and scan more often in case of startup or scan lock failure
data->updateTimer->start(1000);
} else {
TT_LOG_I(TAG, "task not started due to config");
}
@@ -85,12 +67,10 @@ static void onStart(ServiceContext& service) {
static void onStop(ServiceContext& service) {
auto data = std::static_pointer_cast<ServiceData>(service.getData());
if (data != nullptr) {
data->lock();
data->interrupted = true;
data->unlock();
data->thread.join();
if (data->updateTimer != nullptr) {
// Stop thread
data->updateTimer->stop();
data->updateTimer = nullptr;
}
}
+3 -3
View File
@@ -59,7 +59,7 @@ enum WifiRadioState {
WIFI_RADIO_CONNECTION_PENDING,
WIFI_RADIO_CONNECTION_ACTIVE,
WIFI_RADIO_OFF_PENDING,
WIFI_RADIO_OFF
WIFI_RADIO_OFF,
};
struct WifiEvent {
@@ -74,9 +74,9 @@ struct WifiApRecord {
/**
* @brief Get wifi pubsub
* @return PubSub*
* @return PubSub
*/
PubSub* getPubsub();
std::shared_ptr<PubSub> getPubsub();
WifiRadioState getRadioState();
/**
File diff suppressed because it is too large Load Diff
@@ -21,7 +21,7 @@ typedef struct {
/** @brief Locking mechanism for modifying the Wifi instance */
Mutex* mutex;
/** @brief The public event bus */
PubSub* pubsub;
std::shared_ptr<PubSub> pubsub;
/** @brief The internal message queue */
MessageQueue queue;
bool scan_active;
@@ -50,7 +50,7 @@ static void publish_event_simple(Wifi* wifi, WifiEventType type) {
static Wifi* wifi_alloc() {
auto* instance = static_cast<Wifi*>(malloc(sizeof(Wifi)));
instance->mutex = tt_mutex_alloc(MutexTypeRecursive);
instance->pubsub = tt_pubsub_alloc();
instance->pubsub = std::make_shared<PubSub>();
instance->scan_active = false;
instance->radio_state = WIFI_RADIO_CONNECTION_ACTIVE;
instance->secure_connection = false;
@@ -59,7 +59,6 @@ static Wifi* wifi_alloc() {
static void wifi_free(Wifi* instance) {
tt_mutex_free(instance->mutex);
tt_pubsub_free(instance->pubsub);
free(instance);
}
@@ -67,7 +66,7 @@ static void wifi_free(Wifi* instance) {
// region Public functions
PubSub* getPubsub() {
std::shared_ptr<PubSub> getPubsub() {
tt_assert(wifi);
return wifi->pubsub;
}