Refactor services into classes (#183)

This commit is contained in:
Ken Van Hoeylandt
2025-01-24 18:21:47 +01:00
committed by GitHub
parent bb7e79886f
commit 3be251d8fb
20 changed files with 382 additions and 373 deletions
@@ -11,7 +11,10 @@ namespace tt::service::sdcard {
extern const ServiceManifest manifest;
struct ServiceData {
class SdCardService final : public Service {
private:
Mutex mutex;
std::unique_ptr<Timer> updateTimer;
hal::SdCard::State lastState = hal::SdCard::State::Unmounted;
@@ -23,61 +26,59 @@ struct ServiceData {
void unlock() const {
tt_check(mutex.release() == TtStatusOk);
}
void update() {
auto sdcard = tt::hal::getConfiguration()->sdcard;
tt_assert(sdcard);
if (lock(50)) {
auto new_state = sdcard->getState();
if (new_state == hal::SdCard::State::Error) {
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
sdcard->unmount();
}
if (new_state != lastState) {
lastState = new_state;
}
unlock();
} else {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
}
}
static void onUpdate(std::shared_ptr<void> context) {
auto service = std::static_pointer_cast<SdCardService>(context);
service->update();
}
public:
void onStart(ServiceContext& serviceContext) final {
if (hal::getConfiguration()->sdcard != nullptr) {
auto service = findServiceById(manifest.id);
updateTimer = std::make_unique<Timer>(Timer::Type::Periodic, onUpdate, service);
// We want to try and scan more often in case of startup or scan lock failure
updateTimer->start(1000);
} else {
TT_LOG_I(TAG, "Timer not started: no SD card config");
}
}
void onStop(ServiceContext& serviceContext) final {
if (updateTimer != nullptr) {
// Stop thread
updateTimer->stop();
updateTimer = nullptr;
}
}
};
static void onUpdate(std::shared_ptr<void> context) {
auto sdcard = tt::hal::getConfiguration()->sdcard;
if (sdcard == nullptr) {
return;
}
auto data = std::static_pointer_cast<ServiceData>(context);
if (!data->lock(50)) {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return;
}
auto new_state = sdcard->getState();
if (new_state == hal::SdCard::State::Error) {
TT_LOG_W(TAG, "Sdcard error - unmounting. Did you eject the card in an unsafe manner?");
sdcard->unmount();
}
if (new_state != data->lastState) {
data->lastState = new_state;
}
data->unlock();
}
static void onStart(ServiceContext& service) {
if (hal::getConfiguration()->sdcard != nullptr) {
auto data = std::make_shared<ServiceData>();
service.setData(data);
data->updateTimer = std::make_unique<Timer>(Timer::Type::Periodic, 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");
}
}
static void onStop(ServiceContext& service) {
auto data = std::static_pointer_cast<ServiceData>(service.getData());
if (data->updateTimer != nullptr) {
// Stop thread
data->updateTimer->stop();
data->updateTimer = nullptr;
}
}
extern const ServiceManifest manifest = {
.id = "sdcard",
.onStart = onStart,
.onStop = onStop
.createService = create<SdCardService>
};
} // namespace