mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
Service data refactored (#109)
This commit is contained in:
parent
c6f1cd6098
commit
36bb25deba
@ -60,7 +60,7 @@ static void onShow(TT_UNUSED AppContext& app, lv_obj_t* parent) {
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::shared_ptr<Data>(new Data());
|
||||
auto data = std::make_shared<Data>();
|
||||
app.setData(data);
|
||||
}
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
#include "lvgl/Toolbar.h"
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
#include <memory>
|
||||
|
||||
namespace tt::app::files {
|
||||
|
||||
@ -120,11 +121,11 @@ static void viewFile(const char* path, const char* filename) {
|
||||
TT_LOG_I(TAG, "Clicked %s", filepath);
|
||||
|
||||
if (isSupportedImageFile(filename)) {
|
||||
auto bundle = std::shared_ptr<Bundle>(new Bundle());
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
bundle->putString(IMAGE_VIEWER_FILE_ARGUMENT, processed_filepath);
|
||||
service::loader::startApp("ImageViewer", false, bundle);
|
||||
} else if (isSupportedTextFile(filename)) {
|
||||
auto bundle = std::shared_ptr<Bundle>(new Bundle());
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
if (get_platform() == PlatformEsp) {
|
||||
bundle->putString(TEXT_VIEWER_FILE_ARGUMENT, processed_filepath);
|
||||
} else {
|
||||
@ -217,7 +218,7 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::shared_ptr<Data>(new Data());
|
||||
auto data = std::make_shared<Data>();
|
||||
// PC platform is bound to current work directory because of the LVGL file system mapping
|
||||
if (get_platform() == PlatformSimulator) {
|
||||
char cwd[PATH_MAX];
|
||||
|
||||
@ -21,11 +21,11 @@ private:
|
||||
|
||||
public:
|
||||
|
||||
void lock() {
|
||||
void lock() const {
|
||||
tt_check(mutex.acquire(1000) == TtStatusOk);
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
void unlock() const {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ public:
|
||||
|
||||
void startTask();
|
||||
void stopTask();
|
||||
bool shouldInterruptTask() { return interruptTask; };
|
||||
bool shouldInterruptTask() const { return interruptTask; };
|
||||
|
||||
void updatePinStates();
|
||||
void updatePinWidgets();
|
||||
@ -210,7 +210,7 @@ static void onHide(AppContext& app) {
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto gpio = std::shared_ptr<Gpio>(new Gpio());
|
||||
auto gpio = std::make_shared<Gpio>();
|
||||
app.setData(gpio);
|
||||
}
|
||||
|
||||
|
||||
@ -165,7 +165,7 @@ static void onHide(AppContext& app) {
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::shared_ptr<Data>(new Data());
|
||||
auto data = std::make_shared<Data>();
|
||||
app.setData(data);
|
||||
}
|
||||
|
||||
|
||||
@ -119,7 +119,7 @@ static void onHide(TT_UNUSED AppContext& app) {
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto data = std::shared_ptr<Data>(new Data());
|
||||
auto data = std::shared_ptr<Data>();
|
||||
app.setData(data);
|
||||
data->power = getConfiguration()->hardware->power;
|
||||
assert(data->power != nullptr); // The Power app only shows up on supported devices
|
||||
|
||||
@ -50,7 +50,7 @@ static void onListItemSelected(lv_event_t* e) {
|
||||
size_t index = (size_t)(e->user_data);
|
||||
TT_LOG_I(TAG, "Selected item at index %d", index);
|
||||
tt::app::AppContext* app = service::loader::getCurrentApp();
|
||||
auto bundle = std::shared_ptr<Bundle>(new Bundle());
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
setResultIndex(bundle, (int32_t)index);
|
||||
app->setResult(app::ResultOk, bundle);
|
||||
service::loader::stopApp();
|
||||
@ -82,7 +82,7 @@ static void onShow(AppContext& app, lv_obj_t* parent) {
|
||||
app.setResult(ResultError);
|
||||
service::loader::stopApp();
|
||||
} else if (items.size() == 1) {
|
||||
auto result_bundle = std::shared_ptr<Bundle>(new Bundle());
|
||||
auto result_bundle = std::make_shared<Bundle>();
|
||||
setResultIndex(result_bundle, 0);
|
||||
app.setResult(ResultOk, result_bundle);
|
||||
service::loader::stopApp();
|
||||
|
||||
@ -24,7 +24,7 @@ const AppContext* _Nullable optWifiApSettingsApp() {
|
||||
}
|
||||
|
||||
void start(const std::string& ssid) {
|
||||
auto bundle = std::shared_ptr<Bundle>(new Bundle());
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
bundle->putString("ssid", ssid);
|
||||
service::loader::startApp(manifest.id, false, bundle);
|
||||
}
|
||||
|
||||
@ -114,7 +114,7 @@ static void onHide(AppContext& app) {
|
||||
}
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto wifi = std::shared_ptr<WifiConnect>(new WifiConnect());
|
||||
auto wifi = std::make_shared<WifiConnect>();
|
||||
app.setData(wifi);
|
||||
}
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ static void onConnect(const char* ssid) {
|
||||
service::wifi::connect(&settings, false);
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Starting connection dialog");
|
||||
auto bundle = std::shared_ptr<Bundle>(new Bundle());
|
||||
auto bundle = std::make_shared<Bundle>();
|
||||
bundle->putString(WIFI_CONNECT_PARAM_SSID, ssid);
|
||||
bundle->putString(WIFI_CONNECT_PARAM_PASSWORD, "");
|
||||
service::loader::startApp("WifiConnect", false, bundle);
|
||||
@ -145,7 +145,7 @@ void WifiManage::onHide(TT_UNUSED AppContext& app) {
|
||||
// region Manifest methods
|
||||
|
||||
static void onStart(AppContext& app) {
|
||||
auto wifi = std::shared_ptr<WifiManage>(new WifiManage());
|
||||
auto wifi = std::make_shared<WifiManage>();
|
||||
app.setData(wifi);
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "Screenshot.h"
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "ScreenshotTask.h"
|
||||
@ -13,48 +14,25 @@ namespace tt::service::screenshot {
|
||||
|
||||
extern const ServiceManifest manifest;
|
||||
|
||||
typedef struct {
|
||||
Mutex* mutex;
|
||||
task::ScreenshotTask* task;
|
||||
Mode mode;
|
||||
} ServiceData;
|
||||
struct ServiceData {
|
||||
Mutex mutex;
|
||||
task::ScreenshotTask* task = nullptr;
|
||||
Mode mode = ScreenshotModeNone;
|
||||
|
||||
static ServiceData* service_data_alloc() {
|
||||
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
|
||||
*data = (ServiceData) {
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.task = nullptr,
|
||||
.mode = ScreenshotModeNone
|
||||
~ServiceData() {
|
||||
if (task) {
|
||||
task::free(task);
|
||||
}
|
||||
}
|
||||
|
||||
void lock() {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
}
|
||||
};
|
||||
return data;
|
||||
}
|
||||
|
||||
static void service_data_free(ServiceData* data) {
|
||||
tt_mutex_free(data->mutex);
|
||||
}
|
||||
|
||||
static void service_data_lock(ServiceData* data) {
|
||||
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void service_data_unlock(ServiceData* data) {
|
||||
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void on_start(ServiceContext& service) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
service.setData(data);
|
||||
}
|
||||
|
||||
static void on_stop(ServiceContext& service) {
|
||||
auto* data = static_cast<ServiceData*>(service.getData());
|
||||
if (data->task) {
|
||||
task::free(data->task);
|
||||
data->task = nullptr;
|
||||
}
|
||||
tt_mutex_free(data->mutex);
|
||||
service_data_free(data);
|
||||
}
|
||||
|
||||
void startApps(const char* path) {
|
||||
_Nullable auto* service = findServiceById(manifest.id);
|
||||
@ -63,8 +41,8 @@ void startApps(const char* path) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto* data = static_cast<ServiceData*>(service->getData());
|
||||
service_data_lock(data);
|
||||
auto data = std::static_pointer_cast<ServiceData>(service->getData());
|
||||
data->lock();
|
||||
if (data->task == nullptr) {
|
||||
data->task = task::alloc();
|
||||
data->mode = ScreenshotModeApps;
|
||||
@ -72,7 +50,7 @@ void startApps(const char* path) {
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Screenshot task already running");
|
||||
}
|
||||
service_data_unlock(data);
|
||||
data->unlock();
|
||||
}
|
||||
|
||||
void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
|
||||
@ -82,8 +60,8 @@ void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto* data = static_cast<ServiceData*>(service->getData());
|
||||
service_data_lock(data);
|
||||
auto data = std::static_pointer_cast<ServiceData>(service->getData());
|
||||
data->lock();
|
||||
if (data->task == nullptr) {
|
||||
data->task = task::alloc();
|
||||
data->mode = ScreenshotModeTimed;
|
||||
@ -91,7 +69,7 @@ void startTimed(const char* path, uint8_t delay_in_seconds, uint8_t amount) {
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Screenshot task already running");
|
||||
}
|
||||
service_data_unlock(data);
|
||||
data->unlock();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
@ -101,8 +79,8 @@ void stop() {
|
||||
return;
|
||||
}
|
||||
|
||||
auto data = static_cast<ServiceData*>(service->getData());
|
||||
service_data_lock(data);
|
||||
auto data = std::static_pointer_cast<ServiceData>(service->getData());
|
||||
data->lock();
|
||||
if (data->task != nullptr) {
|
||||
task::stop(data->task);
|
||||
task::free(data->task);
|
||||
@ -111,7 +89,7 @@ void stop() {
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Screenshot task not running");
|
||||
}
|
||||
service_data_unlock(data);
|
||||
data->unlock();
|
||||
}
|
||||
|
||||
Mode getMode() {
|
||||
@ -120,10 +98,10 @@ Mode getMode() {
|
||||
TT_LOG_E(TAG, "Service not found");
|
||||
return ScreenshotModeNone;
|
||||
} else {
|
||||
auto* data = static_cast<ServiceData*>(service->getData());
|
||||
service_data_lock(data);
|
||||
auto data = std::static_pointer_cast<ServiceData>(service->getData());
|
||||
data->lock();
|
||||
Mode mode = data->mode;
|
||||
service_data_unlock(data);
|
||||
data->unlock();
|
||||
return mode;
|
||||
}
|
||||
}
|
||||
@ -132,10 +110,14 @@ bool isStarted() {
|
||||
return getMode() != ScreenshotModeNone;
|
||||
}
|
||||
|
||||
static void onStart(ServiceContext& service) {
|
||||
auto data = std::make_shared<ServiceData>();
|
||||
service.setData(data);
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Screenshot",
|
||||
.onStart = &on_start,
|
||||
.onStop = &on_stop
|
||||
.onStart = onStart
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -6,22 +6,39 @@
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "Tactility.h"
|
||||
#include "lvgl/Statusbar.h"
|
||||
#include "service/ServiceRegistry.h"
|
||||
|
||||
namespace tt::service::statusbar {
|
||||
|
||||
#define TAG "statusbar_service"
|
||||
|
||||
typedef struct {
|
||||
Mutex* mutex;
|
||||
Thread* thread;
|
||||
bool service_interrupted;
|
||||
int8_t wifi_icon_id;
|
||||
const char* wifi_last_icon;
|
||||
int8_t sdcard_icon_id;
|
||||
const char* sdcard_last_icon;
|
||||
int8_t power_icon_id;
|
||||
const char* power_last_icon;
|
||||
} ServiceData;
|
||||
extern const ServiceManifest manifest;
|
||||
|
||||
struct ServiceData {
|
||||
Mutex mutex;
|
||||
Thread thread;
|
||||
bool service_interrupted = false;
|
||||
int8_t wifi_icon_id = lvgl::statusbar_icon_add(nullptr);
|
||||
const char* wifi_last_icon = nullptr;
|
||||
int8_t sdcard_icon_id = lvgl::statusbar_icon_add(nullptr);
|
||||
const char* sdcard_last_icon = nullptr;
|
||||
int8_t power_icon_id = lvgl::statusbar_icon_add(nullptr);
|
||||
const char* power_last_icon = nullptr;
|
||||
|
||||
~ServiceData() {
|
||||
lvgl::statusbar_icon_remove(wifi_icon_id);
|
||||
lvgl::statusbar_icon_remove(sdcard_icon_id);
|
||||
lvgl::statusbar_icon_remove(power_icon_id);
|
||||
}
|
||||
|
||||
void lock() const {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
void unlock() const {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
}
|
||||
};
|
||||
|
||||
// region wifi
|
||||
|
||||
@ -59,7 +76,7 @@ static const char* wifi_get_status_icon(wifi::WifiRadioState state, bool secure)
|
||||
}
|
||||
}
|
||||
|
||||
static void update_wifi_icon(ServiceData* data) {
|
||||
static void update_wifi_icon(std::shared_ptr<ServiceData> data) {
|
||||
wifi::WifiRadioState radio_state = wifi::getRadioState();
|
||||
bool is_secure = wifi::isConnectionSecure();
|
||||
const char* desired_icon = wifi_get_status_icon(radio_state, is_secure);
|
||||
@ -85,7 +102,7 @@ static _Nullable const char* sdcard_get_status_icon(hal::sdcard::State state) {
|
||||
}
|
||||
}
|
||||
|
||||
static void update_sdcard_icon(ServiceData* data) {
|
||||
static void update_sdcard_icon(std::shared_ptr<ServiceData> data) {
|
||||
hal::sdcard::State state = hal::sdcard::getState();
|
||||
const char* desired_icon = sdcard_get_status_icon(state);
|
||||
if (data->sdcard_last_icon != desired_icon) {
|
||||
@ -119,7 +136,7 @@ static _Nullable const char* power_get_status_icon() {
|
||||
}
|
||||
}
|
||||
|
||||
static void update_power_icon(ServiceData* data) {
|
||||
static void update_power_icon(std::shared_ptr<ServiceData> data) {
|
||||
const char* desired_icon = power_get_status_icon();
|
||||
if (data->power_last_icon != desired_icon) {
|
||||
lvgl::statusbar_icon_set_image(data->power_icon_id, desired_icon);
|
||||
@ -132,49 +149,21 @@ static void update_power_icon(ServiceData* data) {
|
||||
|
||||
// region service
|
||||
|
||||
static ServiceData* service_data_alloc() {
|
||||
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
|
||||
*data = (ServiceData) {
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.thread = new Thread(),
|
||||
.service_interrupted = false,
|
||||
.wifi_icon_id = lvgl::statusbar_icon_add(nullptr),
|
||||
.wifi_last_icon = nullptr,
|
||||
.sdcard_icon_id = lvgl::statusbar_icon_add(nullptr),
|
||||
.sdcard_last_icon = nullptr,
|
||||
.power_icon_id = lvgl::statusbar_icon_add(nullptr),
|
||||
.power_last_icon = nullptr
|
||||
};
|
||||
|
||||
lvgl::statusbar_icon_set_visibility(data->wifi_icon_id, true);
|
||||
update_wifi_icon(data);
|
||||
update_sdcard_icon(data); // also updates visibility
|
||||
update_power_icon(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static void service_data_free(ServiceData* data) {
|
||||
tt_mutex_free(data->mutex);
|
||||
delete data->thread;
|
||||
lvgl::statusbar_icon_remove(data->wifi_icon_id);
|
||||
lvgl::statusbar_icon_remove(data->sdcard_icon_id);
|
||||
lvgl::statusbar_icon_remove(data->power_icon_id);
|
||||
free(data);
|
||||
}
|
||||
|
||||
static void service_data_lock(ServiceData* data) {
|
||||
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void service_data_unlock(ServiceData* data) {
|
||||
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
|
||||
}
|
||||
|
||||
int32_t service_main(TT_UNUSED void* parameter) {
|
||||
int32_t serviceMain(TT_UNUSED void* parameter) {
|
||||
TT_LOG_I(TAG, "Started main loop");
|
||||
auto* data = (ServiceData*)parameter;
|
||||
tt_assert(data != nullptr);
|
||||
delay_ms(20); // TODO: Make service instance findable earlier on (but expose "starting" state?)
|
||||
auto context = tt::service::findServiceById(manifest.id);
|
||||
if (context == nullptr) {
|
||||
TT_LOG_E(TAG, "Service not found");
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto data = std::static_pointer_cast<ServiceData>(context->getData());
|
||||
|
||||
while (!data->service_interrupted) {
|
||||
update_wifi_icon(data);
|
||||
update_sdcard_icon(data);
|
||||
@ -184,34 +173,37 @@ int32_t service_main(TT_UNUSED void* parameter) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void on_start(ServiceContext& service) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
static void onStart(ServiceContext& service) {
|
||||
auto data = std::make_shared<ServiceData>();
|
||||
service.setData(data);
|
||||
|
||||
data->thread->setCallback(service_main, data);
|
||||
data->thread->setPriority(Thread::PriorityLow);
|
||||
data->thread->setStackSize(3000);
|
||||
data->thread->setName("statusbar");
|
||||
data->thread->start();
|
||||
lvgl::statusbar_icon_set_visibility(data->wifi_icon_id, true);
|
||||
update_wifi_icon(data);
|
||||
update_sdcard_icon(data); // also updates visibility
|
||||
update_power_icon(data);
|
||||
|
||||
|
||||
data->thread.setCallback(serviceMain, nullptr);
|
||||
data->thread.setPriority(Thread::PriorityLow);
|
||||
data->thread.setStackSize(3000);
|
||||
data->thread.setName("statusbar");
|
||||
data->thread.start();
|
||||
}
|
||||
|
||||
static void on_stop(ServiceContext& service) {
|
||||
auto* data = static_cast<ServiceData*>(service.getData());
|
||||
static void onStop(ServiceContext& service) {
|
||||
auto data = std::static_pointer_cast<ServiceData>(service.getData());
|
||||
|
||||
// Stop thread
|
||||
service_data_lock(data);
|
||||
data->lock();
|
||||
data->service_interrupted = true;
|
||||
service_data_unlock(data);
|
||||
tt_mutex_release(data->mutex);
|
||||
data->thread->join();
|
||||
|
||||
service_data_free(data);
|
||||
data->unlock();
|
||||
data->thread.join();
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Statusbar",
|
||||
.onStart = &on_start,
|
||||
.onStop = &on_stop
|
||||
.onStart = onStart,
|
||||
.onStop = onStop
|
||||
};
|
||||
|
||||
// endregion service
|
||||
|
||||
@ -10,7 +10,7 @@ private:
|
||||
|
||||
Mutex mutex = Mutex(MutexTypeNormal);
|
||||
const service::ServiceManifest& manifest;
|
||||
void* data = nullptr;
|
||||
std::shared_ptr<void> data = nullptr;
|
||||
|
||||
public:
|
||||
|
||||
@ -18,8 +18,8 @@ public:
|
||||
~ServiceInstance() override = default;
|
||||
|
||||
const service::ServiceManifest& getManifest() const override;
|
||||
void* getData() const override;
|
||||
void setData(void* newData) override;
|
||||
std::shared_ptr<void> getData() const override;
|
||||
void setData(std::shared_ptr<void> newData) override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "ServiceManifest.h"
|
||||
#include <memory>
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
@ -14,8 +15,8 @@ protected:
|
||||
public:
|
||||
|
||||
virtual const service::ServiceManifest& getManifest() const = 0;
|
||||
virtual void* getData() const = 0;
|
||||
virtual void setData(void* newData) = 0;
|
||||
virtual std::shared_ptr<void> getData() const = 0;
|
||||
virtual void setData(std::shared_ptr<void> newData) = 0;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
#include <utility>
|
||||
|
||||
#include "service/ServiceInstance.h"
|
||||
|
||||
namespace tt::service {
|
||||
@ -6,16 +8,16 @@ ServiceInstance::ServiceInstance(const service::ServiceManifest&manifest) : mani
|
||||
|
||||
const service::ServiceManifest& ServiceInstance::getManifest() const { return manifest; }
|
||||
|
||||
void* ServiceInstance::getData() const {
|
||||
std::shared_ptr<void> ServiceInstance::getData() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
void* data_copy = data;
|
||||
std::shared_ptr<void> result = data;
|
||||
mutex.release();
|
||||
return data_copy;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ServiceInstance::setData(void* newData) {
|
||||
void ServiceInstance::setData(std::shared_ptr<void> newData) {
|
||||
mutex.acquire(TtWaitForever);
|
||||
data = newData;
|
||||
data = std::move(newData);
|
||||
mutex.release();
|
||||
}
|
||||
|
||||
|
||||
@ -4,56 +4,54 @@
|
||||
#include "service/ServiceContext.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "TactilityHeadless.h"
|
||||
#include "service/ServiceRegistry.h"
|
||||
|
||||
#define TAG "sdcard_service"
|
||||
|
||||
namespace tt::service::sdcard {
|
||||
|
||||
static int32_t sdcard_task(void* context);
|
||||
extern const ServiceManifest manifest;
|
||||
|
||||
typedef struct {
|
||||
Mutex* mutex;
|
||||
Thread* thread;
|
||||
hal::sdcard::State lastState;
|
||||
bool interrupted;
|
||||
} ServiceData;
|
||||
|
||||
static ServiceData* service_data_alloc() {
|
||||
auto* data = static_cast<ServiceData*>(malloc(sizeof(ServiceData)));
|
||||
*data = (ServiceData) {
|
||||
.mutex = tt_mutex_alloc(MutexTypeNormal),
|
||||
.thread = new Thread(
|
||||
struct ServiceData {
|
||||
Mutex mutex;
|
||||
Thread thread = Thread(
|
||||
"sdcard",
|
||||
3000, // Minimum is ~2800 @ ESP-IDF 5.1.2 when ejecting sdcard
|
||||
&sdcard_task,
|
||||
data
|
||||
),
|
||||
.lastState = hal::sdcard::StateUnmounted,
|
||||
.interrupted = false
|
||||
nullptr
|
||||
);
|
||||
hal::sdcard::State lastState = hal::sdcard::StateUnmounted;
|
||||
bool interrupted = false;
|
||||
|
||||
ServiceData() {
|
||||
thread.setPriority(Thread::PriorityLow);
|
||||
}
|
||||
|
||||
void lock() const {
|
||||
tt_check(mutex.acquire(TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
void unlock() const {
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
}
|
||||
};
|
||||
data->thread->setPriority(Thread::PriorityLow);
|
||||
return data;
|
||||
|
||||
|
||||
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 service_data_free(ServiceData* data) {
|
||||
tt_mutex_free(data->mutex);
|
||||
delete data->thread;
|
||||
}
|
||||
auto data = std::static_pointer_cast<ServiceData>(service->getData());
|
||||
|
||||
static void service_data_lock(ServiceData* data) {
|
||||
tt_check(tt_mutex_acquire(data->mutex, TtWaitForever) == TtStatusOk);
|
||||
}
|
||||
|
||||
static void service_data_unlock(ServiceData* data) {
|
||||
tt_check(tt_mutex_release(data->mutex) == TtStatusOk);
|
||||
}
|
||||
|
||||
static int32_t sdcard_task(void* context) {
|
||||
auto* data = (ServiceData*)context;
|
||||
bool interrupted = false;
|
||||
|
||||
do {
|
||||
service_data_lock(data);
|
||||
data->lock();
|
||||
|
||||
interrupted = data->interrupted;
|
||||
|
||||
@ -68,40 +66,38 @@ static int32_t sdcard_task(void* context) {
|
||||
data->lastState = new_state;
|
||||
}
|
||||
|
||||
service_data_unlock(data);
|
||||
data->lock();
|
||||
delay_ms(2000);
|
||||
} while (!interrupted);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void on_start(ServiceContext& service) {
|
||||
static void onStart(ServiceContext& service) {
|
||||
if (hal::getConfiguration().sdcard != nullptr) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
auto data = std::make_shared<ServiceData>();
|
||||
service.setData(data);
|
||||
data->thread->start();
|
||||
data->thread.start();
|
||||
} else {
|
||||
TT_LOG_I(TAG, "task not started due to config");
|
||||
}
|
||||
}
|
||||
|
||||
static void on_stop(ServiceContext& service) {
|
||||
auto* data = static_cast<ServiceData*>(service.getData());
|
||||
static void onStop(ServiceContext& service) {
|
||||
auto data = std::static_pointer_cast<ServiceData>(service.getData());
|
||||
if (data != nullptr) {
|
||||
service_data_lock(data);
|
||||
data->lock();
|
||||
data->interrupted = true;
|
||||
service_data_unlock(data);
|
||||
data->unlock();
|
||||
|
||||
data->thread->join();
|
||||
|
||||
service_data_free(data);
|
||||
data->thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "sdcard",
|
||||
.onStart = &on_start,
|
||||
.onStop = &on_stop
|
||||
.onStart = onStart,
|
||||
.onStop = onStop
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -80,13 +80,13 @@ public:
|
||||
bool connection_target_remember = false; // Whether to store the connection_target on successful connection or not
|
||||
};
|
||||
|
||||
static Wifi* wifi_singleton = nullptr;
|
||||
static std::shared_ptr<Wifi> wifi_singleton;
|
||||
|
||||
// Forward declarations
|
||||
static void scan_list_free_safely(Wifi* wifi);
|
||||
static void disconnect_internal_but_keep_active(Wifi* wifi);
|
||||
static void lock(Wifi* wifi);
|
||||
static void unlock(Wifi* wifi);
|
||||
static void scan_list_free_safely(std::shared_ptr<Wifi> wifi);
|
||||
static void disconnect_internal_but_keep_active(std::shared_ptr<Wifi> wifi);
|
||||
static void lock(std::shared_ptr<Wifi> wifi);
|
||||
static void unlock(std::shared_ptr<Wifi> wifi);
|
||||
|
||||
// region Alloc
|
||||
|
||||
@ -253,47 +253,47 @@ int getRssi() {
|
||||
|
||||
// endregion Public functions
|
||||
|
||||
static void lock(Wifi* wifi) {
|
||||
static void lock(std::shared_ptr<Wifi> wifi) {
|
||||
tt_assert(wifi);
|
||||
wifi->mutex.acquire(ms_to_ticks(100));
|
||||
}
|
||||
|
||||
static void unlock(Wifi* wifi) {
|
||||
static void unlock(std::shared_ptr<Wifi> wifi) {
|
||||
tt_assert(wifi);
|
||||
wifi->mutex.release();
|
||||
}
|
||||
|
||||
static void scan_list_alloc(Wifi* wifi) {
|
||||
static void scan_list_alloc(std::shared_ptr<Wifi> wifi) {
|
||||
tt_assert(wifi->scan_list == nullptr);
|
||||
wifi->scan_list = static_cast<wifi_ap_record_t*>(malloc(sizeof(wifi_ap_record_t) * wifi->scan_list_limit));
|
||||
wifi->scan_list_count = 0;
|
||||
}
|
||||
|
||||
static void scan_list_alloc_safely(Wifi* wifi) {
|
||||
static void scan_list_alloc_safely(std::shared_ptr<Wifi> wifi) {
|
||||
if (wifi->scan_list == nullptr) {
|
||||
scan_list_alloc(wifi);
|
||||
}
|
||||
}
|
||||
|
||||
static void scan_list_free(Wifi* wifi) {
|
||||
static void scan_list_free(std::shared_ptr<Wifi> wifi) {
|
||||
tt_assert(wifi->scan_list != nullptr);
|
||||
free(wifi->scan_list);
|
||||
wifi->scan_list = nullptr;
|
||||
wifi->scan_list_count = 0;
|
||||
}
|
||||
|
||||
static void scan_list_free_safely(Wifi* wifi) {
|
||||
static void scan_list_free_safely(std::shared_ptr<Wifi> wifi) {
|
||||
if (wifi->scan_list != nullptr) {
|
||||
scan_list_free(wifi);
|
||||
}
|
||||
}
|
||||
|
||||
static void publish_event_simple(Wifi* wifi, WifiEventType type) {
|
||||
static void publish_event_simple(std::shared_ptr<Wifi> wifi, WifiEventType type) {
|
||||
WifiEvent turning_on_event = {.type = type};
|
||||
tt_pubsub_publish(wifi->pubsub, &turning_on_event);
|
||||
}
|
||||
|
||||
static bool copy_scan_list(Wifi* wifi) {
|
||||
static bool copy_scan_list(std::shared_ptr<Wifi> wifi) {
|
||||
bool can_fetch_results = (wifi->radio_state == WIFI_RADIO_ON || wifi->radio_state == WIFI_RADIO_CONNECTION_ACTIVE) &&
|
||||
wifi->scan_active;
|
||||
|
||||
@ -322,7 +322,7 @@ static bool copy_scan_list(Wifi* wifi) {
|
||||
}
|
||||
}
|
||||
|
||||
static void auto_connect(Wifi* wifi) {
|
||||
static void auto_connect(std::shared_ptr<Wifi> wifi) {
|
||||
TT_LOG_I(TAG, "auto_connect()");
|
||||
for (int i = 0; i < wifi->scan_list_count; ++i) {
|
||||
auto ssid = reinterpret_cast<const char*>(wifi->scan_list[i].ssid);
|
||||
@ -389,7 +389,7 @@ static void event_handler(TT_UNUSED void* arg, esp_event_base_t event_base, int3
|
||||
unlock(wifi_singleton);
|
||||
}
|
||||
|
||||
static void enable(Wifi* wifi) {
|
||||
static void enable(std::shared_ptr<Wifi> wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (
|
||||
state == WIFI_RADIO_ON ||
|
||||
@ -469,7 +469,7 @@ static void enable(Wifi* wifi) {
|
||||
TT_LOG_I(TAG, "Enabled");
|
||||
}
|
||||
|
||||
static void disable(Wifi* wifi) {
|
||||
static void disable(std::shared_ptr<Wifi> wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (
|
||||
state == WIFI_RADIO_OFF ||
|
||||
@ -527,7 +527,7 @@ static void disable(Wifi* wifi) {
|
||||
TT_LOG_I(TAG, "Disabled");
|
||||
}
|
||||
|
||||
static void scan_internal(Wifi* wifi) {
|
||||
static void scan_internal(std::shared_ptr<Wifi> wifi) {
|
||||
WifiRadioState state = wifi->radio_state;
|
||||
if (state != WIFI_RADIO_ON && state != WIFI_RADIO_CONNECTION_ACTIVE && state != WIFI_RADIO_CONNECTION_PENDING) {
|
||||
TT_LOG_W(TAG, "Scan unavailable: wifi not enabled");
|
||||
@ -548,7 +548,7 @@ static void scan_internal(Wifi* wifi) {
|
||||
}
|
||||
}
|
||||
|
||||
static void connect_internal(Wifi* wifi) {
|
||||
static void connect_internal(std::shared_ptr<Wifi> wifi) {
|
||||
TT_LOG_I(TAG, "Connecting to %s", wifi->connection_target.ssid);
|
||||
|
||||
// Stop radio first, if needed
|
||||
@ -668,7 +668,7 @@ static void connect_internal(Wifi* wifi) {
|
||||
wifi_singleton->connection_wait_flags.clear(WIFI_FAIL_BIT | WIFI_CONNECTED_BIT);
|
||||
}
|
||||
|
||||
static void disconnect_internal_but_keep_active(Wifi* wifi) {
|
||||
static void disconnect_internal_but_keep_active(std::shared_ptr<Wifi> wifi) {
|
||||
esp_err_t stop_result = esp_wifi_stop();
|
||||
if (stop_result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "Failed to disconnect (%s)", esp_err_to_name(stop_result));
|
||||
@ -711,7 +711,7 @@ static void disconnect_internal_but_keep_active(Wifi* wifi) {
|
||||
TT_LOG_I(TAG, "Disconnected");
|
||||
}
|
||||
|
||||
static bool shouldScanForAutoConnect(Wifi* wifi) {
|
||||
static bool shouldScanForAutoConnect(std::shared_ptr<Wifi> wifi) {
|
||||
bool is_radio_in_scannable_state = wifi->radio_state == WIFI_RADIO_ON &&
|
||||
!wifi->scan_active &&
|
||||
!wifi->pause_auto_connect;
|
||||
@ -730,7 +730,7 @@ static bool shouldScanForAutoConnect(Wifi* wifi) {
|
||||
_Noreturn int32_t wifi_main(TT_UNUSED void* parameter) {
|
||||
TT_LOG_I(TAG, "Started main loop");
|
||||
tt_assert(wifi_singleton != nullptr);
|
||||
Wifi* wifi = wifi_singleton;
|
||||
auto wifi = wifi_singleton;
|
||||
MessageQueue& queue = wifi->queue;
|
||||
|
||||
if (settings::shouldEnableOnBoot()) {
|
||||
@ -791,7 +791,7 @@ _Noreturn int32_t wifi_main(TT_UNUSED void* parameter) {
|
||||
|
||||
static void service_start(ServiceContext& service) {
|
||||
tt_assert(wifi_singleton == nullptr);
|
||||
wifi_singleton = new Wifi();
|
||||
wifi_singleton = std::make_shared<Wifi>();
|
||||
service.setData(wifi_singleton);
|
||||
}
|
||||
|
||||
@ -803,7 +803,6 @@ static void service_stop(ServiceContext& service) {
|
||||
disable(wifi_singleton);
|
||||
}
|
||||
|
||||
delete wifi_singleton;
|
||||
wifi_singleton = nullptr;
|
||||
|
||||
// wifi_main() cannot be stopped yet as it runs in the main task.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user