App and Service improvements (#106)
This commit is contained in:
committed by
GitHub
parent
e86a11b7e2
commit
50ee77d572
@@ -1,7 +1,7 @@
|
||||
#include "TactilityHeadless.h"
|
||||
#include "hal/Configuration.h"
|
||||
#include "hal/Hal_i.h"
|
||||
#include "service/Manifest.h"
|
||||
#include "service/ServiceManifest.h"
|
||||
#include "service/ServiceRegistry.h"
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
@@ -12,10 +12,10 @@ namespace tt {
|
||||
|
||||
#define TAG "tactility"
|
||||
|
||||
namespace service::wifi { extern const Manifest manifest; }
|
||||
namespace service::sdcard { extern const Manifest manifest; }
|
||||
namespace service::wifi { extern const ServiceManifest manifest; }
|
||||
namespace service::sdcard { extern const ServiceManifest manifest; }
|
||||
|
||||
static const service::Manifest* const system_services[] = {
|
||||
static const service::ServiceManifest* const system_services[] = {
|
||||
&service::sdcard::manifest,
|
||||
&service::wifi::manifest
|
||||
};
|
||||
@@ -24,7 +24,7 @@ static const hal::Configuration* hardwareConfig = nullptr;
|
||||
|
||||
static void register_and_start_system_services() {
|
||||
TT_LOG_I(TAG, "Registering and starting system services");
|
||||
int app_count = sizeof(system_services) / sizeof(service::Manifest*);
|
||||
int app_count = sizeof(system_services) / sizeof(service::ServiceManifest*);
|
||||
for (int i = 0; i < app_count; ++i) {
|
||||
addService(system_services[i]);
|
||||
tt_check(service::startService(system_services[i]->id));
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
#include "Service.h"
|
||||
#include "Manifest.h"
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
Service::Service(const service::Manifest& manifest) : manifest(manifest) {}
|
||||
|
||||
const service::Manifest& Service::getManifest() const { return manifest; }
|
||||
|
||||
void* Service::getData() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
void* data_copy = data;
|
||||
mutex.release();
|
||||
return data_copy;
|
||||
}
|
||||
|
||||
void Service::setData(void* newData) {
|
||||
mutex.acquire(TtWaitForever);
|
||||
data = newData;
|
||||
mutex.release();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "Manifest.h"
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
class Service {
|
||||
private:
|
||||
Mutex mutex = Mutex(MutexTypeNormal);
|
||||
const service::Manifest& manifest;
|
||||
void* data = nullptr;
|
||||
|
||||
public:
|
||||
Service(const service::Manifest& manifest);
|
||||
|
||||
[[nodiscard]] const service::Manifest& getManifest() const;
|
||||
[[nodiscard]] void* getData() const;
|
||||
void setData(void* newData);
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "ServiceManifest.h"
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
class ServiceContext {
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~ServiceContext() = default;
|
||||
|
||||
public:
|
||||
|
||||
[[nodiscard]] virtual const service::ServiceManifest& getManifest() const = 0;
|
||||
[[nodiscard]] virtual void* getData() const = 0;
|
||||
virtual void setData(void* newData) = 0;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "service/ServiceInstance.h"
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
ServiceInstance::ServiceInstance(const service::ServiceManifest&manifest) : manifest(manifest) {}
|
||||
|
||||
const service::ServiceManifest& ServiceInstance::getManifest() const { return manifest; }
|
||||
|
||||
void* ServiceInstance::getData() const {
|
||||
mutex.acquire(TtWaitForever);
|
||||
void* data_copy = data;
|
||||
mutex.release();
|
||||
return data_copy;
|
||||
}
|
||||
|
||||
void ServiceInstance::setData(void* newData) {
|
||||
mutex.acquire(TtWaitForever);
|
||||
data = newData;
|
||||
mutex.release();
|
||||
}
|
||||
|
||||
}
|
||||
+5
-5
@@ -4,12 +4,12 @@
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
class Service;
|
||||
class ServiceContext;
|
||||
|
||||
typedef void (*ServiceOnStart)(Service& service);
|
||||
typedef void (*ServiceOnStop)(Service& service);
|
||||
typedef void (*ServiceOnStart)(ServiceContext& service);
|
||||
typedef void (*ServiceOnStop)(ServiceContext& service);
|
||||
|
||||
typedef struct Manifest {
|
||||
struct ServiceManifest {
|
||||
/**
|
||||
* The identifier by which the app is launched by the system and other apps.
|
||||
*/
|
||||
@@ -25,6 +25,6 @@ typedef struct Manifest {
|
||||
*/
|
||||
const ServiceOnStop onStop = nullptr;
|
||||
|
||||
} Manifest;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "ServiceRegistry.h"
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "Service.h"
|
||||
#include "Manifest.h"
|
||||
#include "service/ServiceInstance.h"
|
||||
#include "service/ServiceManifest.h"
|
||||
#include "TactilityCore.h"
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -11,8 +11,8 @@ namespace tt::service {
|
||||
|
||||
#define TAG "service_registry"
|
||||
|
||||
typedef std::unordered_map<std::string, const Manifest*> ManifestMap;
|
||||
typedef std::unordered_map<std::string, Service*> ServiceInstanceMap;
|
||||
typedef std::unordered_map<std::string, const ServiceManifest*> ManifestMap;
|
||||
typedef std::unordered_map<std::string, ServiceInstance*> ServiceInstanceMap;
|
||||
|
||||
static ManifestMap service_manifest_map;
|
||||
static ServiceInstanceMap service_instance_map;
|
||||
@@ -20,57 +20,57 @@ static ServiceInstanceMap service_instance_map;
|
||||
static Mutex manifest_mutex(MutexTypeNormal);
|
||||
static Mutex instance_mutex(MutexTypeNormal);
|
||||
|
||||
void addService(const Manifest* manifest) {
|
||||
TT_LOG_I(TAG, "adding %s", manifest->id.c_str());
|
||||
void addService(const ServiceManifest* manifest) {
|
||||
TT_LOG_I(TAG, "Adding %s", manifest->id.c_str());
|
||||
|
||||
manifest_mutex.acquire(TtWaitForever);
|
||||
service_manifest_map[manifest->id] = manifest;
|
||||
manifest_mutex.release();
|
||||
}
|
||||
|
||||
const Manifest* _Nullable findManifestId(const std::string& id) {
|
||||
const ServiceManifest* _Nullable findManifestId(const std::string& id) {
|
||||
manifest_mutex.acquire(TtWaitForever);
|
||||
auto iterator = service_manifest_map.find(id);
|
||||
_Nullable const Manifest * manifest = iterator != service_manifest_map.end() ? iterator->second : nullptr;
|
||||
_Nullable const ServiceManifest * manifest = iterator != service_manifest_map.end() ? iterator->second : nullptr;
|
||||
manifest_mutex.release();
|
||||
return manifest;
|
||||
}
|
||||
|
||||
static Service* _Nullable service_registry_find_instance_by_id(const std::string& id) {
|
||||
static ServiceInstance* _Nullable service_registry_find_instance_by_id(const std::string& id) {
|
||||
manifest_mutex.acquire(TtWaitForever);
|
||||
auto iterator = service_instance_map.find(id);
|
||||
_Nullable Service* service = iterator != service_instance_map.end() ? iterator->second : nullptr;
|
||||
_Nullable ServiceInstance* service = iterator != service_instance_map.end() ? iterator->second : nullptr;
|
||||
manifest_mutex.release();
|
||||
return service;
|
||||
}
|
||||
|
||||
// TODO: return proper error/status instead of BOOL
|
||||
// TODO: Return proper error/status instead of BOOL?
|
||||
bool startService(const std::string& id) {
|
||||
TT_LOG_I(TAG, "starting %s", id.c_str());
|
||||
const Manifest* manifest = findManifestId(id);
|
||||
TT_LOG_I(TAG, "Starting %s", id.c_str());
|
||||
const ServiceManifest* manifest = findManifestId(id);
|
||||
if (manifest == nullptr) {
|
||||
TT_LOG_E(TAG, "manifest not found for service %s", id.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
auto* service = new Service(*manifest);
|
||||
auto* service = new ServiceInstance(*manifest);
|
||||
manifest->onStart(*service);
|
||||
|
||||
instance_mutex.acquire(TtWaitForever);
|
||||
service_instance_map[manifest->id] = service;
|
||||
instance_mutex.release();
|
||||
TT_LOG_I(TAG, "started %s", id.c_str());
|
||||
TT_LOG_I(TAG, "Started %s", id.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
_Nullable Service* findServiceById(const std::string& service_id) {
|
||||
return (Service*)service_registry_find_instance_by_id(service_id);
|
||||
_Nullable ServiceContext* findServiceById(const std::string& service_id) {
|
||||
return static_cast<ServiceInstance*>(service_registry_find_instance_by_id(service_id));
|
||||
}
|
||||
|
||||
bool stopService(const std::string& id) {
|
||||
TT_LOG_I(TAG, "stopping %s", id.c_str());
|
||||
Service* service = service_registry_find_instance_by_id(id);
|
||||
TT_LOG_I(TAG, "Stopping %s", id.c_str());
|
||||
ServiceInstance* service = service_registry_find_instance_by_id(id);
|
||||
if (service == nullptr) {
|
||||
TT_LOG_W(TAG, "service not running: %s", id.c_str());
|
||||
return false;
|
||||
@@ -83,7 +83,7 @@ bool stopService(const std::string& id) {
|
||||
service_instance_map.erase(id);
|
||||
instance_mutex.release();
|
||||
|
||||
TT_LOG_I(TAG, "stopped %s", id.c_str());
|
||||
TT_LOG_I(TAG, "Stopped %s", id.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "Manifest.h"
|
||||
#include "service/ServiceManifest.h"
|
||||
|
||||
namespace tt::service {
|
||||
|
||||
typedef void (*ManifestCallback)(const Manifest*, void* context);
|
||||
typedef void (*ManifestCallback)(const ServiceManifest*, void* context);
|
||||
|
||||
void initRegistry();
|
||||
|
||||
void addService(const Manifest* manifest);
|
||||
void removeService(const Manifest* manifest);
|
||||
void addService(const ServiceManifest* manifest);
|
||||
void removeService(const ServiceManifest* manifest);
|
||||
|
||||
bool startService(const std::string& id);
|
||||
bool stopService(const std::string& id);
|
||||
|
||||
const Manifest* _Nullable findManifestId(const std::string& id);
|
||||
Service* _Nullable findServiceById(const std::string& id);
|
||||
const ServiceManifest* _Nullable findManifestId(const std::string& id);
|
||||
ServiceContext* _Nullable findServiceById(const std::string& id);
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <cstdlib>
|
||||
|
||||
#include "Mutex.h"
|
||||
#include "service/Service.h"
|
||||
#include "service/ServiceContext.h"
|
||||
#include "TactilityCore.h"
|
||||
#include "TactilityHeadless.h"
|
||||
|
||||
@@ -75,7 +75,7 @@ static int32_t sdcard_task(void* context) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void on_start(Service& service) {
|
||||
static void on_start(ServiceContext& service) {
|
||||
if (hal::getConfiguration().sdcard != nullptr) {
|
||||
ServiceData* data = service_data_alloc();
|
||||
service.setData(data);
|
||||
@@ -85,7 +85,7 @@ static void on_start(Service& service) {
|
||||
}
|
||||
}
|
||||
|
||||
static void on_stop(Service& service) {
|
||||
static void on_stop(ServiceContext& service) {
|
||||
auto* data = static_cast<ServiceData*>(service.getData());
|
||||
if (data != nullptr) {
|
||||
service_data_lock(data);
|
||||
@@ -98,7 +98,7 @@ static void on_stop(Service& service) {
|
||||
}
|
||||
}
|
||||
|
||||
extern const Manifest manifest = {
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "sdcard",
|
||||
.onStart = &on_start,
|
||||
.onStop = &on_stop
|
||||
|
||||
@@ -6,10 +6,9 @@
|
||||
#include "Mutex.h"
|
||||
#include "Check.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "Log.h"
|
||||
#include "Pubsub.h"
|
||||
#include "service/Service.h"
|
||||
#include "service/ServiceContext.h"
|
||||
#include "WifiSettings.h"
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
@@ -790,13 +789,13 @@ _Noreturn int32_t wifi_main(TT_UNUSED void* parameter) {
|
||||
}
|
||||
}
|
||||
|
||||
static void service_start(Service& service) {
|
||||
static void service_start(ServiceContext& service) {
|
||||
tt_assert(wifi_singleton == nullptr);
|
||||
wifi_singleton = new Wifi();
|
||||
service.setData(wifi_singleton);
|
||||
}
|
||||
|
||||
static void service_stop(Service& service) {
|
||||
static void service_stop(ServiceContext& service) {
|
||||
tt_assert(wifi_singleton != nullptr);
|
||||
|
||||
WifiRadioState state = wifi_singleton->radio_state;
|
||||
@@ -812,7 +811,7 @@ static void service_stop(Service& service) {
|
||||
tt_crash("not fully implemented");
|
||||
}
|
||||
|
||||
extern const Manifest manifest = {
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Wifi",
|
||||
.onStart = &service_start,
|
||||
.onStop = &service_stop
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "MessageQueue.h"
|
||||
#include "Mutex.h"
|
||||
#include "Pubsub.h"
|
||||
#include "service/Service.h"
|
||||
#include "service/ServiceContext.h"
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
@@ -175,12 +175,12 @@ static void unlock(Wifi* wifi) {
|
||||
}
|
||||
|
||||
|
||||
static void service_start(TT_UNUSED Service& service) {
|
||||
static void service_start(TT_UNUSED ServiceContext& service) {
|
||||
tt_check(wifi == nullptr);
|
||||
wifi = wifi_alloc();
|
||||
}
|
||||
|
||||
static void service_stop(TT_UNUSED Service& service) {
|
||||
static void service_stop(TT_UNUSED ServiceContext& service) {
|
||||
tt_check(wifi != nullptr);
|
||||
|
||||
wifi_free(wifi);
|
||||
@@ -191,7 +191,7 @@ void wifi_main(void*) {
|
||||
// NO-OP
|
||||
}
|
||||
|
||||
extern const Manifest manifest = {
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Wifi",
|
||||
.onStart = &service_start,
|
||||
.onStop = &service_stop
|
||||
|
||||
Reference in New Issue
Block a user