Compare commits

..

No commits in common. "a2a802eaa2c0e64c2519ce09d6b1ac49517852b3" and "d4793596137dae64259c3bf411e6fa34012a8412" have entirely different histories.

17 changed files with 197 additions and 149 deletions

View File

@ -1,12 +1,15 @@
#pragma once #pragma once
#include <tactility/service/service_instance.h>
#include <memory> #include <memory>
namespace tt::service { namespace tt::service {
using State = ::ServiceState; enum class State {
Starting,
Started,
Stopping,
Stopped
};
// Forward declaration // Forward declaration
class ServiceContext; class ServiceContext;

View File

@ -6,7 +6,6 @@
#include <tactility/error.h> #include <tactility/error.h>
#include <tactility/service/service_paths.h> #include <tactility/service/service_paths.h>
#include <cassert>
#include <string> #include <string>
#include <memory> #include <memory>

View File

@ -102,7 +102,7 @@ void attachDevices() {
// We search for the manifest first, because during the initial start() during boot // We search for the manifest first, because during the initial start() during boot
// the service won't be registered yet. // the service won't be registered yet.
if (service::findManifestById("Gui") != nullptr) { if (service::findManifestById("Gui") != nullptr) {
if (service::getState("Gui") == SERVICE_STATE_STOPPED) { if (service::getState("Gui") == service::State::Stopped) {
service::startService("Gui"); service::startService("Gui");
} else { } else {
LOG_E(TAG, "Gui service is not in Stopped state"); LOG_E(TAG, "Gui service is not in Stopped state");
@ -112,7 +112,7 @@ void attachDevices() {
// We search for the manifest first, because during the initial start() during boot // We search for the manifest first, because during the initial start() during boot
// the service won't be registered yet. // the service won't be registered yet.
if (service::findManifestById("Statusbar") != nullptr) { if (service::findManifestById("Statusbar") != nullptr) {
if (service::getState("Statusbar") == SERVICE_STATE_STOPPED) { if (service::getState("Statusbar") == service::State::Stopped) {
service::startService("Statusbar"); service::startService("Statusbar");
} else { } else {
LOG_E(TAG, "Statusbar service is not in Stopped state"); LOG_E(TAG, "Statusbar service is not in Stopped state");

View File

@ -3,9 +3,9 @@
#include <Tactility/service/ServiceInstance.h> #include <Tactility/service/ServiceInstance.h>
#include <Tactility/service/ServiceManifest.h> #include <Tactility/service/ServiceManifest.h>
#include <tactility/service/service_registration.h>
#include <tactility/error.h> #include <tactility/error.h>
#include <tactility/log.h> #include <tactility/log.h>
#include <tactility/service/service_manager.h>
#include <cassert> #include <cassert>
@ -13,32 +13,45 @@ namespace tt::service {
constexpr auto* TAG = "ServiceRegistration"; constexpr auto* TAG = "ServiceRegistration";
static State toCppState(ServiceState state) {
switch (state) {
case SERVICE_STATE_STARTING: return State::Starting;
case SERVICE_STATE_STARTED: return State::Started;
case SERVICE_STATE_STOPPING: return State::Stopping;
case SERVICE_STATE_STOPPED:
default: return State::Stopped;
}
}
// Bridges the kernel's context-free C ServiceManifest/Service callbacks to the // Bridges the kernel's context-free C ServiceManifest/Service callbacks to the
// C++ Service instances they wrap. Declared extern "C" to match the linkage of // C++ Service instances they wrap. Declared extern "C" to match the linkage of
// the C function-pointer types they're assigned to (see e.g. gpio_controller.cpp). // the C function-pointer types they're assigned to (see e.g. gpio_controller.cpp).
extern "C" { extern "C" {
static error_t cppOnStartTrampoline(::ServiceInstance* cContext) { static error_t cppOnStartTrampoline(::Service* cService, ::ServiceInstance* cContext) {
auto& servicePtr = *static_cast<std::shared_ptr<Service>*>(cContext->data); auto& servicePtr = *static_cast<std::shared_ptr<Service>*>(cService->data);
ServiceInstance context(cContext); ServiceInstance context(cContext);
return servicePtr->onStart(context) ? ERROR_NONE : ERROR_RESOURCE; return servicePtr->onStart(context) ? ERROR_NONE : ERROR_RESOURCE;
} }
static void cppOnStopTrampoline(::ServiceInstance* cContext) { static void cppOnStopTrampoline(::Service* cService, ::ServiceInstance* cContext) {
auto& servicePtr = *static_cast<std::shared_ptr<Service>*>(cContext->data); auto& servicePtr = *static_cast<std::shared_ptr<Service>*>(cService->data);
ServiceInstance context(cContext); ServiceInstance context(cContext);
servicePtr->onStop(context); servicePtr->onStop(context);
} }
static void cppCreateServiceTrampoline(::ServiceInstance* cContext, void* context) { static ::Service* cppCreateServiceTrampoline(void* context) {
auto& cppManifest = *static_cast<std::shared_ptr<const ServiceManifest>*>(context); auto& cppManifest = *static_cast<std::shared_ptr<const ServiceManifest>*>(context);
cContext->data = new std::shared_ptr(cppManifest->createService()); auto* cService = new ::Service();
cContext->on_start = cppOnStartTrampoline; cService->data = new std::shared_ptr(cppManifest->createService());
cContext->on_stop = cppOnStopTrampoline; cService->on_start = cppOnStartTrampoline;
cService->on_stop = cppOnStopTrampoline;
return cService;
} }
static void cppDestroyServiceTrampoline(::ServiceInstance* cContext, void* /*context*/) { static void cppDestroyServiceTrampoline(::Service* cService, void* /*context*/) {
delete static_cast<std::shared_ptr<Service>*>(cContext->data); delete static_cast<std::shared_ptr<Service>*>(cService->data);
delete cService;
} }
} // extern "C" } // extern "C"
@ -49,7 +62,7 @@ void addService(std::shared_ptr<const ServiceManifest> manifest, bool autoStart)
LOG_I(TAG, "Adding %s", id.c_str()); LOG_I(TAG, "Adding %s", id.c_str());
if (service_manager_find_manifest(id.c_str()) != nullptr) { if (service_registration_find_manifest(id.c_str()) != nullptr) {
LOG_E(TAG, "Service id in use: %s", id.c_str()); LOG_E(TAG, "Service id in use: %s", id.c_str());
return; return;
} }
@ -66,7 +79,7 @@ void addService(std::shared_ptr<const ServiceManifest> manifest, bool autoStart)
.context = cppManifestPtr .context = cppManifestPtr
}; };
error_t error = service_manager_add(cManifest, autoStart); error_t error = service_registration_add(cManifest, autoStart);
if (error != ERROR_NONE) { if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to add service %s: %s", id.c_str(), error_to_string(error)); LOG_E(TAG, "Failed to add service %s: %s", id.c_str(), error_to_string(error));
} }
@ -77,7 +90,7 @@ void addService(const ServiceManifest& manifest, bool autoStart) {
} }
std::shared_ptr<const ServiceManifest> findManifestById(const std::string& id) { std::shared_ptr<const ServiceManifest> findManifestById(const std::string& id) {
const auto* cManifest = service_manager_find_manifest(id.c_str()); const auto* cManifest = service_registration_find_manifest(id.c_str());
if (cManifest == nullptr) { if (cManifest == nullptr) {
return nullptr; return nullptr;
} }
@ -86,7 +99,7 @@ std::shared_ptr<const ServiceManifest> findManifestById(const std::string& id) {
bool startService(const std::string& id) { bool startService(const std::string& id) {
LOG_I(TAG, "Starting %s", id.c_str()); LOG_I(TAG, "Starting %s", id.c_str());
error_t error = service_manager_start(id.c_str()); error_t error = service_registration_start(id.c_str());
if (error != ERROR_NONE) { if (error != ERROR_NONE) {
LOG_E(TAG, "Starting %s failed: %s", id.c_str(), error_to_string(error)); LOG_E(TAG, "Starting %s failed: %s", id.c_str(), error_to_string(error));
return false; return false;
@ -96,7 +109,7 @@ bool startService(const std::string& id) {
} }
std::shared_ptr<ServiceContext> findServiceContextById(const std::string& id) { std::shared_ptr<ServiceContext> findServiceContextById(const std::string& id) {
auto* cContext = service_manager_find_context(id.c_str()); auto* cContext = service_registration_find_context(id.c_str());
if (cContext == nullptr) { if (cContext == nullptr) {
return nullptr; return nullptr;
} }
@ -104,16 +117,16 @@ std::shared_ptr<ServiceContext> findServiceContextById(const std::string& id) {
} }
std::shared_ptr<Service> findServiceById(const std::string& id) { std::shared_ptr<Service> findServiceById(const std::string& id) {
auto* cContext = service_manager_find_context(id.c_str()); auto* cService = service_registration_find_service(id.c_str());
if (cContext == nullptr) { if (cService == nullptr) {
return nullptr; return nullptr;
} }
return *static_cast<std::shared_ptr<Service>*>(cContext->data); return *static_cast<std::shared_ptr<Service>*>(cService->data);
} }
bool stopService(const std::string& id) { bool stopService(const std::string& id) {
LOG_I(TAG, "Stopping %s", id.c_str()); LOG_I(TAG, "Stopping %s", id.c_str());
error_t error = service_manager_stop(id.c_str()); error_t error = service_registration_stop(id.c_str());
if (error != ERROR_NONE) { if (error != ERROR_NONE) {
LOG_W(TAG, "Service not running: %s", id.c_str()); LOG_W(TAG, "Service not running: %s", id.c_str());
return false; return false;
@ -123,7 +136,7 @@ bool stopService(const std::string& id) {
} }
State getState(const std::string& id) { State getState(const std::string& id) {
return service_manager_get_state(id.c_str()); return toCppState(service_registration_get_state(id.c_str()));
} }
} // namespace } // namespace

View File

@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/error.h>
#ifdef __cplusplus
extern "C" {
#endif
// ServiceContext (declared in service_context.h) is a typedef alias of ServiceInstance,
// so callbacks below are declared directly in terms of ServiceInstance to avoid a
// conflicting forward-declaration of an unrelated "ServiceContext" struct tag.
struct ServiceInstance;
/**
* A service is a long-running background process (e.g. Wi-Fi, GPS, GUI).
* Concrete services keep their own state behind the `data` pointer.
*/
struct Service {
/** Service-specific data, owned by the service implementation. Can be NULL. */
void* data;
/**
* Called when the service is starting.
* Can be NULL, in which case starting always succeeds.
* @param[in,out] service this service
* @param[in,out] context the context (a ServiceInstance) for this running service
* @return ERROR_NONE if the service started successfully
*/
error_t (*on_start)(struct Service* service, struct ServiceInstance* context);
/**
* Called when the service is stopping.
* Can be NULL, in which case stopping is a no-op.
* @param[in,out] service this service
* @param[in,out] context the context (a ServiceInstance) for this running service
*/
void (*on_stop)(struct Service* service, struct ServiceInstance* context);
};
#ifdef __cplusplus
}
#endif

View File

@ -23,21 +23,8 @@ typedef enum {
struct ServiceInstance { struct ServiceInstance {
/** The manifest that spawned this instance. */ /** The manifest that spawned this instance. */
const struct ServiceManifest* manifest; const struct ServiceManifest* manifest;
/** Service-specific data, owned by the service implementation. Can be NULL. */ /** The service created via manifest->create_service(). */
void* data; struct Service* service;
/**
* Called when the service is starting.
* Can be NULL, in which case starting always succeeds.
* @param[in,out] instance this service instance (also its own ServiceContext)
* @return ERROR_NONE if the service started successfully
*/
error_t (*on_start)(struct ServiceInstance* instance);
/**
* Called when the service is stopping.
* Can be NULL, in which case stopping is a no-op.
* @param[in,out] instance this service instance (also its own ServiceContext)
*/
void (*on_stop)(struct ServiceInstance* instance);
/** /**
* Internal state managed by the kernel. * Internal state managed by the kernel.
* ServiceInstance implementers should initialize this to NULL. * ServiceInstance implementers should initialize this to NULL.
@ -73,11 +60,11 @@ error_t service_instance_destruct(struct ServiceInstance* instance);
const struct ServiceManifest* service_instance_get_manifest(struct ServiceInstance* instance); const struct ServiceManifest* service_instance_get_manifest(struct ServiceInstance* instance);
/** /**
* @brief Get the data of a service instance. * @brief Get the service of a service instance.
* @param[in] instance non-null service instance pointer * @param[in] instance non-null service instance pointer
* @return the data (can be NULL) * @return the service
*/ */
void* service_instance_get_data(struct ServiceInstance* instance); struct Service* service_instance_get_service(struct ServiceInstance* instance);
/** /**
* @brief Get the state of a service instance. * @brief Get the state of a service instance.

View File

@ -2,35 +2,25 @@
#pragma once #pragma once
#include <tactility/error.h> #include <tactility/service/service.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// ServiceContext (declared in service_context.h) is a typedef alias of ServiceInstance, /**
// so ServiceCreate/ServiceDestroy below are declared directly in terms of ServiceInstance * Allocates and initializes a new Service instance.
// to avoid a conflicting forward-declaration of an unrelated "ServiceContext" struct tag. * @param[in] context the manifest's context (ServiceManifest::context)
struct ServiceInstance; * @return the new service, never NULL
*/
typedef struct Service* (*ServiceCreate)(void* context);
/** /**
* Initializes a newly-registered service instance in place. * Frees a Service instance that was created by the matching ServiceCreate function.
* Implementations should set instance->data, instance->on_start and instance->on_stop * @param[in] service the service to free
* as needed; all three may be left at their zeroed defaults (NULL) if the service has
* no state or lifecycle callbacks.
* @param[in,out] instance the instance to populate (manifest and internal are already set)
* @param[in] context the manifest's context (ServiceManifest::context) * @param[in] context the manifest's context (ServiceManifest::context)
*/ */
typedef void (*ServiceCreate)(struct ServiceInstance* instance, void* context); typedef void (*ServiceDestroy)(struct Service* service, void* context);
/**
* Tears down state that was set up by the matching ServiceCreate function
* (e.g. frees instance->data). Should not clear instance->data/on_start/on_stop;
* the caller does that.
* @param[in,out] instance the instance to tear down
* @param[in] context the manifest's context (ServiceManifest::context)
*/
typedef void (*ServiceDestroy)(struct ServiceInstance* instance, void* context);
/** /**
* Describes a registrable service type. * Describes a registrable service type.

View File

@ -20,7 +20,7 @@ extern "C" {
* @retval ERROR_RESOURCE if auto_start is true and starting the service failed * @retval ERROR_RESOURCE if auto_start is true and starting the service failed
* @retval ERROR_NONE on success * @retval ERROR_NONE on success
*/ */
error_t service_manager_add(const struct ServiceManifest* manifest, bool auto_start); error_t service_registration_add(const struct ServiceManifest* manifest, bool auto_start);
/** /**
* @brief Unregister a previously-added manifest. * @brief Unregister a previously-added manifest.
@ -29,14 +29,7 @@ extern "C" {
* @retval ERROR_NOT_FOUND if no manifest with this id is registered * @retval ERROR_NOT_FOUND if no manifest with this id is registered
* @retval ERROR_NONE on success * @retval ERROR_NONE on success
*/ */
error_t service_manager_remove(const char* id); error_t service_registration_remove(const char* id);
/**
* @brief Find a registered manifest by id.
* @param[in] id non-null service id
* @return the manifest, or NULL if not found
*/
const struct ServiceManifest* service_manager_find_manifest(const char* id);
/** /**
* @brief Start a registered service by id. * @brief Start a registered service by id.
@ -46,7 +39,7 @@ const struct ServiceManifest* service_manager_find_manifest(const char* id);
* @retval ERROR_RESOURCE if the service's on_start callback failed * @retval ERROR_RESOURCE if the service's on_start callback failed
* @retval ERROR_NONE on success * @retval ERROR_NONE on success
*/ */
error_t service_manager_start(const char* id); error_t service_registration_start(const char* id);
/** /**
* @brief Stop a running service by id. * @brief Stop a running service by id.
@ -54,21 +47,35 @@ error_t service_manager_start(const char* id);
* @retval ERROR_NOT_FOUND if no service with this id is running * @retval ERROR_NOT_FOUND if no service with this id is running
* @retval ERROR_NONE on success * @retval ERROR_NONE on success
*/ */
error_t service_manager_stop(const char* id); error_t service_registration_stop(const char* id);
/** /**
* @brief Get the state of a service by id. * @brief Get the state of a service by id.
* @param[in] id non-null service id * @param[in] id non-null service id
* @return the current state, or SERVICE_STATE_STOPPED if the id is unknown * @return the current state, or SERVICE_STATE_STOPPED if the id is unknown
*/ */
ServiceState service_manager_get_state(const char* id); ServiceState service_registration_get_state(const char* id);
/**
* @brief Find a registered manifest by id.
* @param[in] id non-null service id
* @return the manifest, or NULL if not found
*/
const struct ServiceManifest* service_registration_find_manifest(const char* id);
/** /**
* @brief Find the context of a running service by id. * @brief Find the context of a running service by id.
* @param[in] id non-null service id * @param[in] id non-null service id
* @return the context, or NULL if the service isn't running * @return the context, or NULL if the service isn't running
*/ */
ServiceContext* service_manager_find_context(const char* id); ServiceContext* service_registration_find_context(const char* id);
/**
* @brief Find the service instance of a running service by id.
* @param[in] id non-null service id
* @return the service, or NULL if not running
*/
struct Service* service_registration_find_service(const char* id);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -24,10 +24,7 @@ error_t service_instance_construct(ServiceInstance* instance, const ServiceManif
mutex_construct(&instance->internal->mutex); mutex_construct(&instance->internal->mutex);
instance->manifest = manifest; instance->manifest = manifest;
instance->data = nullptr; instance->service = manifest->create_service(manifest->context);
instance->on_start = nullptr;
instance->on_stop = nullptr;
manifest->create_service(instance, manifest->context);
LOG_D(TAG, "construct %s", manifest->id); LOG_D(TAG, "construct %s", manifest->id);
return ERROR_NONE; return ERROR_NONE;
@ -45,10 +42,8 @@ error_t service_instance_destruct(ServiceInstance* instance) {
LOG_D(TAG, "destruct %s", instance->manifest->id); LOG_D(TAG, "destruct %s", instance->manifest->id);
instance->manifest->destroy_service(instance, instance->manifest->context); instance->manifest->destroy_service(instance->service, instance->manifest->context);
instance->data = nullptr; instance->service = nullptr;
instance->on_start = nullptr;
instance->on_stop = nullptr;
instance->internal = nullptr; instance->internal = nullptr;
mutex_destruct(&internal->mutex); mutex_destruct(&internal->mutex);
@ -61,8 +56,8 @@ const ServiceManifest* service_instance_get_manifest(ServiceInstance* instance)
return instance->manifest; return instance->manifest;
} }
void* service_instance_get_data(ServiceInstance* instance) { Service* service_instance_get_service(ServiceInstance* instance) {
return instance->data; return instance->service;
} }
ServiceState service_instance_get_state(ServiceInstance* instance) { ServiceState service_instance_get_state(ServiceInstance* instance) {

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
#include <tactility/service/service_manager.h> #include <tactility/service/service_registration.h>
#include <tactility/concurrent/mutex.h> #include <tactility/concurrent/mutex.h>
#include <tactility/log.h> #include <tactility/log.h>
@ -45,7 +45,7 @@ static InstanceLedger& get_instance_ledger() {
extern "C" { extern "C" {
error_t service_manager_add(const ServiceManifest* manifest, bool auto_start) { error_t service_registration_add(const ServiceManifest* manifest, bool auto_start) {
mutex_lock(&manifest_ledger.mutex); mutex_lock(&manifest_ledger.mutex);
if (manifest_ledger.manifests.contains(manifest->id)) { if (manifest_ledger.manifests.contains(manifest->id)) {
mutex_unlock(&manifest_ledger.mutex); mutex_unlock(&manifest_ledger.mutex);
@ -58,14 +58,14 @@ error_t service_manager_add(const ServiceManifest* manifest, bool auto_start) {
LOG_I(TAG, "add %s", manifest->id); LOG_I(TAG, "add %s", manifest->id);
if (auto_start) { if (auto_start) {
return service_manager_start(manifest->id); return service_registration_start(manifest->id);
} }
return ERROR_NONE; return ERROR_NONE;
} }
error_t service_manager_remove(const char* id) { error_t service_registration_remove(const char* id) {
if (service_manager_find_context(id) != nullptr) { if (service_registration_find_context(id) != nullptr) {
return ERROR_INVALID_STATE; return ERROR_INVALID_STATE;
} }
@ -82,7 +82,7 @@ error_t service_manager_remove(const char* id) {
return ERROR_NONE; return ERROR_NONE;
} }
error_t service_manager_start(const char* id) { error_t service_registration_start(const char* id) {
mutex_lock(&manifest_ledger.mutex); mutex_lock(&manifest_ledger.mutex);
const auto manifest_iterator = manifest_ledger.manifests.find(id); const auto manifest_iterator = manifest_ledger.manifests.find(id);
if (manifest_iterator == manifest_ledger.manifests.end()) { if (manifest_iterator == manifest_ledger.manifests.end()) {
@ -98,7 +98,7 @@ error_t service_manager_start(const char* id) {
return ERROR_INVALID_STATE; return ERROR_INVALID_STATE;
} }
auto* instance = new(std::nothrow) ServiceInstance { .manifest = nullptr, .data = nullptr, .on_start = nullptr, .on_stop = nullptr, .internal = nullptr }; auto* instance = new(std::nothrow) ServiceInstance { .manifest = nullptr, .service = nullptr, .internal = nullptr };
if (instance == nullptr) { if (instance == nullptr) {
mutex_unlock(&instance_ledger.mutex); mutex_unlock(&instance_ledger.mutex);
return ERROR_OUT_OF_MEMORY; return ERROR_OUT_OF_MEMORY;
@ -118,7 +118,8 @@ error_t service_manager_start(const char* id) {
service_instance_set_state(instance, SERVICE_STATE_STARTING); service_instance_set_state(instance, SERVICE_STATE_STARTING);
LOG_I(TAG, "start %s", id); LOG_I(TAG, "start %s", id);
error = (instance->on_start != nullptr) ? instance->on_start(instance) : ERROR_NONE; Service* service = instance->service;
error = (service->on_start != nullptr) ? service->on_start(service, instance) : ERROR_NONE;
if (error == ERROR_NONE) { if (error == ERROR_NONE) {
service_instance_set_state(instance, SERVICE_STATE_STARTED); service_instance_set_state(instance, SERVICE_STATE_STARTED);
@ -138,7 +139,7 @@ error_t service_manager_start(const char* id) {
return ERROR_RESOURCE; return ERROR_RESOURCE;
} }
error_t service_manager_stop(const char* id) { error_t service_registration_stop(const char* id) {
mutex_lock(&instance_ledger.mutex); mutex_lock(&instance_ledger.mutex);
const auto iterator = instance_ledger.instances.find(id); const auto iterator = instance_ledger.instances.find(id);
if (iterator == instance_ledger.instances.end()) { if (iterator == instance_ledger.instances.end()) {
@ -152,8 +153,9 @@ error_t service_manager_stop(const char* id) {
service_instance_set_state(instance, SERVICE_STATE_STOPPING); service_instance_set_state(instance, SERVICE_STATE_STOPPING);
if (instance->on_stop != nullptr) { Service* service = instance->service;
instance->on_stop(instance); if (service->on_stop != nullptr) {
service->on_stop(service, instance);
} }
service_instance_set_state(instance, SERVICE_STATE_STOPPED); service_instance_set_state(instance, SERVICE_STATE_STOPPED);
@ -168,7 +170,7 @@ error_t service_manager_stop(const char* id) {
return ERROR_NONE; return ERROR_NONE;
} }
ServiceState service_manager_get_state(const char* id) { ServiceState service_registration_get_state(const char* id) {
mutex_lock(&instance_ledger.mutex); mutex_lock(&instance_ledger.mutex);
const auto iterator = instance_ledger.instances.find(id); const auto iterator = instance_ledger.instances.find(id);
if (iterator == instance_ledger.instances.end()) { if (iterator == instance_ledger.instances.end()) {
@ -181,7 +183,7 @@ ServiceState service_manager_get_state(const char* id) {
return service_instance_get_state(instance); return service_instance_get_state(instance);
} }
const ServiceManifest* service_manager_find_manifest(const char* id) { const ServiceManifest* service_registration_find_manifest(const char* id) {
mutex_lock(&manifest_ledger.mutex); mutex_lock(&manifest_ledger.mutex);
const auto iterator = manifest_ledger.manifests.find(id); const auto iterator = manifest_ledger.manifests.find(id);
const ServiceManifest* manifest = (iterator != manifest_ledger.manifests.end()) ? iterator->second : nullptr; const ServiceManifest* manifest = (iterator != manifest_ledger.manifests.end()) ? iterator->second : nullptr;
@ -189,7 +191,7 @@ const ServiceManifest* service_manager_find_manifest(const char* id) {
return manifest; return manifest;
} }
ServiceContext* service_manager_find_context(const char* id) { ServiceContext* service_registration_find_context(const char* id) {
mutex_lock(&instance_ledger.mutex); mutex_lock(&instance_ledger.mutex);
const auto iterator = instance_ledger.instances.find(id); const auto iterator = instance_ledger.instances.find(id);
ServiceInstance* instance = (iterator != instance_ledger.instances.end()) ? iterator->second : nullptr; ServiceInstance* instance = (iterator != instance_ledger.instances.end()) ? iterator->second : nullptr;
@ -197,4 +199,9 @@ ServiceContext* service_manager_find_context(const char* id) {
return instance; return instance;
} }
Service* service_registration_find_service(const char* id) {
ServiceInstance* instance = service_registration_find_context(id);
return (instance != nullptr) ? instance->service : nullptr;
}
} // extern "C" } // extern "C"

View File

@ -2,6 +2,7 @@ project(TactilityTests)
enable_language(C CXX ASM) enable_language(C CXX ASM)
set(CMAKE_CXX_COMPILER g++)
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp) file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
add_executable(TactilityTests EXCLUDE_FROM_ALL ${TEST_SOURCES}) add_executable(TactilityTests EXCLUDE_FROM_ALL ${TEST_SOURCES})

View File

@ -1,4 +1,4 @@
#include "TestFile.h" #include "../../TactilityCore/Source/TestFile.h"
#include "../../Tactility/Private/Tactility/app/AppManifestParsing.h" #include "../../Tactility/Private/Tactility/app/AppManifestParsing.h"
#include "../../Tactility/Private/Tactility/app/AppManifestParsingInternal.h" #include "../../Tactility/Private/Tactility/app/AppManifestParsingInternal.h"

View File

@ -1,4 +1,4 @@
#include "TestFile.h" #include "../../TactilityCore/Source/TestFile.h"
#include "../../Tactility/Include/Tactility/file/PropertiesFile.h" #include "../../Tactility/Include/Tactility/file/PropertiesFile.h"
#include "doctest.h" #include "doctest.h"

View File

@ -2,6 +2,7 @@ project(TactilityFreeRtosTests)
enable_language(C CXX ASM) enable_language(C CXX ASM)
set(CMAKE_CXX_COMPILER g++)
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp) file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
add_executable(TactilityFreeRtosTests EXCLUDE_FROM_ALL ${TEST_SOURCES}) add_executable(TactilityFreeRtosTests EXCLUDE_FROM_ALL ${TEST_SOURCES})

View File

@ -2,6 +2,7 @@ project(TactilityKernelTests)
enable_language(C CXX ASM) enable_language(C CXX ASM)
set(CMAKE_CXX_COMPILER g++)
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp) file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
add_executable(TactilityKernelTests EXCLUDE_FROM_ALL ${TEST_SOURCES}) add_executable(TactilityKernelTests EXCLUDE_FROM_ALL ${TEST_SOURCES})

View File

@ -1,8 +1,5 @@
#include "doctest.h" #include "doctest.h"
#include <tactility/service/service_manager.h> #include <tactility/service/service_registration.h>
// Defined in service_instance.cpp. Internal-only, exposed here to test try_get/put gating.
extern "C" void service_instance_set_state(ServiceInstance* instance, ServiceState state);
static int create_called = 0; static int create_called = 0;
static int destroy_called = 0; static int destroy_called = 0;
@ -12,20 +9,22 @@ static error_t on_start_result = ERROR_NONE;
static void* last_create_context = nullptr; static void* last_create_context = nullptr;
static void* last_destroy_context = nullptr; static void* last_destroy_context = nullptr;
static void test_create_service(ServiceInstance* instance, void* context) { static Service* test_create_service(void* context) {
create_called++; create_called++;
last_create_context = context; last_create_context = context;
instance->data = nullptr; static Service service;
instance->on_start = [](ServiceInstance*) -> error_t { service.data = nullptr;
service.on_start = [](Service*, ServiceContext*) -> error_t {
on_start_called++; on_start_called++;
return on_start_result; return on_start_result;
}; };
instance->on_stop = [](ServiceInstance*) { service.on_stop = [](Service*, ServiceContext*) {
on_stop_called++; on_stop_called++;
}; };
return &service;
} }
static void test_destroy_service(ServiceInstance*, void* context) { static void test_destroy_service(Service*, void* context) {
destroy_called++; destroy_called++;
last_destroy_context = context; last_destroy_context = context;
} }
@ -51,13 +50,12 @@ TEST_CASE("ServiceInstance construction and destruction") {
.context = &context_marker .context = &context_marker
}; };
ServiceInstance instance = { .manifest = nullptr, .data = nullptr, .on_start = nullptr, .on_stop = nullptr, .internal = nullptr }; ServiceInstance instance = { .manifest = nullptr, .service = nullptr, .internal = nullptr };
CHECK_EQ(service_instance_construct(&instance, &manifest), ERROR_NONE); CHECK_EQ(service_instance_construct(&instance, &manifest), ERROR_NONE);
CHECK_NE(instance.internal, nullptr); CHECK_NE(instance.internal, nullptr);
CHECK_EQ(instance.manifest, &manifest); CHECK_EQ(instance.manifest, &manifest);
CHECK_NE(instance.on_start, nullptr); CHECK_NE(instance.service, nullptr);
CHECK_NE(instance.on_stop, nullptr);
CHECK_EQ(create_called, 1); CHECK_EQ(create_called, 1);
CHECK_EQ(last_create_context, &context_marker); CHECK_EQ(last_create_context, &context_marker);
CHECK_EQ(service_instance_get_state(&instance), SERVICE_STATE_STOPPED); CHECK_EQ(service_instance_get_state(&instance), SERVICE_STATE_STOPPED);
@ -68,7 +66,7 @@ TEST_CASE("ServiceInstance construction and destruction") {
CHECK_EQ(last_destroy_context, &context_marker); CHECK_EQ(last_destroy_context, &context_marker);
} }
TEST_CASE("service_manager_add rejects duplicate ids") { TEST_CASE("service_registration_add rejects duplicate ids") {
reset_counters(); reset_counters();
static const ServiceManifest manifest = { static const ServiceManifest manifest = {
@ -77,10 +75,10 @@ TEST_CASE("service_manager_add rejects duplicate ids") {
.destroy_service = test_destroy_service .destroy_service = test_destroy_service
}; };
CHECK_EQ(service_manager_add(&manifest, false), ERROR_NONE); CHECK_EQ(service_registration_add(&manifest, false), ERROR_NONE);
CHECK_EQ(service_manager_add(&manifest, false), ERROR_INVALID_ARGUMENT); CHECK_EQ(service_registration_add(&manifest, false), ERROR_INVALID_ARGUMENT);
CHECK_EQ(service_manager_remove("duplicate-test"), ERROR_NONE); CHECK_EQ(service_registration_remove("duplicate-test"), ERROR_NONE);
} }
TEST_CASE("service_registration start/stop lifecycle") { TEST_CASE("service_registration start/stop lifecycle") {
@ -92,32 +90,33 @@ TEST_CASE("service_registration start/stop lifecycle") {
.destroy_service = test_destroy_service .destroy_service = test_destroy_service
}; };
CHECK_EQ(service_manager_add(&manifest, false), ERROR_NONE); CHECK_EQ(service_registration_add(&manifest, false), ERROR_NONE);
CHECK_EQ(service_manager_get_state("lifecycle-test"), SERVICE_STATE_STOPPED); CHECK_EQ(service_registration_get_state("lifecycle-test"), SERVICE_STATE_STOPPED);
CHECK_EQ(service_manager_start("lifecycle-test"), ERROR_NONE); CHECK_EQ(service_registration_start("lifecycle-test"), ERROR_NONE);
CHECK_EQ(on_start_called, 1); CHECK_EQ(on_start_called, 1);
CHECK_EQ(service_manager_get_state("lifecycle-test"), SERVICE_STATE_STARTED); CHECK_EQ(service_registration_get_state("lifecycle-test"), SERVICE_STATE_STARTED);
CHECK_NE(service_manager_find_context("lifecycle-test"), nullptr); CHECK_NE(service_registration_find_context("lifecycle-test"), nullptr);
CHECK_NE(service_registration_find_service("lifecycle-test"), nullptr);
// Starting again while already started should fail // Starting again while already started should fail
CHECK_EQ(service_manager_start("lifecycle-test"), ERROR_INVALID_STATE); CHECK_EQ(service_registration_start("lifecycle-test"), ERROR_INVALID_STATE);
// Removing while running should fail // Removing while running should fail
CHECK_EQ(service_manager_remove("lifecycle-test"), ERROR_INVALID_STATE); CHECK_EQ(service_registration_remove("lifecycle-test"), ERROR_INVALID_STATE);
CHECK_EQ(service_manager_stop("lifecycle-test"), ERROR_NONE); CHECK_EQ(service_registration_stop("lifecycle-test"), ERROR_NONE);
CHECK_EQ(on_stop_called, 1); CHECK_EQ(on_stop_called, 1);
CHECK_EQ(service_manager_get_state("lifecycle-test"), SERVICE_STATE_STOPPED); CHECK_EQ(service_registration_get_state("lifecycle-test"), SERVICE_STATE_STOPPED);
CHECK_EQ(service_manager_find_context("lifecycle-test"), nullptr); CHECK_EQ(service_registration_find_context("lifecycle-test"), nullptr);
// Stopping again while already stopped should fail // Stopping again while already stopped should fail
CHECK_EQ(service_manager_stop("lifecycle-test"), ERROR_NOT_FOUND); CHECK_EQ(service_registration_stop("lifecycle-test"), ERROR_NOT_FOUND);
CHECK_EQ(service_manager_remove("lifecycle-test"), ERROR_NONE); CHECK_EQ(service_registration_remove("lifecycle-test"), ERROR_NONE);
} }
TEST_CASE("service_manager_add with auto_start") { TEST_CASE("service_registration_add with auto_start") {
reset_counters(); reset_counters();
static const ServiceManifest manifest = { static const ServiceManifest manifest = {
@ -126,15 +125,15 @@ TEST_CASE("service_manager_add with auto_start") {
.destroy_service = test_destroy_service .destroy_service = test_destroy_service
}; };
CHECK_EQ(service_manager_add(&manifest, true), ERROR_NONE); CHECK_EQ(service_registration_add(&manifest, true), ERROR_NONE);
CHECK_EQ(on_start_called, 1); CHECK_EQ(on_start_called, 1);
CHECK_EQ(service_manager_get_state("auto-start-test"), SERVICE_STATE_STARTED); CHECK_EQ(service_registration_get_state("auto-start-test"), SERVICE_STATE_STARTED);
CHECK_EQ(service_manager_stop("auto-start-test"), ERROR_NONE); CHECK_EQ(service_registration_stop("auto-start-test"), ERROR_NONE);
CHECK_EQ(service_manager_remove("auto-start-test"), ERROR_NONE); CHECK_EQ(service_registration_remove("auto-start-test"), ERROR_NONE);
} }
TEST_CASE("service_manager_start failure leaves service stopped") { TEST_CASE("service_registration_start failure leaves service stopped") {
reset_counters(); reset_counters();
on_start_result = ERROR_RESOURCE; on_start_result = ERROR_RESOURCE;
@ -144,19 +143,20 @@ TEST_CASE("service_manager_start failure leaves service stopped") {
.destroy_service = test_destroy_service .destroy_service = test_destroy_service
}; };
CHECK_EQ(service_manager_add(&manifest, false), ERROR_NONE); CHECK_EQ(service_registration_add(&manifest, false), ERROR_NONE);
CHECK_EQ(service_manager_start("failing-start-test"), ERROR_RESOURCE); CHECK_EQ(service_registration_start("failing-start-test"), ERROR_RESOURCE);
CHECK_EQ(service_manager_get_state("failing-start-test"), SERVICE_STATE_STOPPED); CHECK_EQ(service_registration_get_state("failing-start-test"), SERVICE_STATE_STOPPED);
CHECK_EQ(service_manager_find_context("failing-start-test"), nullptr); CHECK_EQ(service_registration_find_context("failing-start-test"), nullptr);
CHECK_EQ(service_manager_remove("failing-start-test"), ERROR_NONE); CHECK_EQ(service_registration_remove("failing-start-test"), ERROR_NONE);
} }
TEST_CASE("service_registration lookup functions with unknown id") { TEST_CASE("service_registration lookup functions with unknown id") {
CHECK_EQ(service_manager_get_state("unknown-service-id"), SERVICE_STATE_STOPPED); CHECK_EQ(service_registration_get_state("unknown-service-id"), SERVICE_STATE_STOPPED);
CHECK_EQ(service_manager_find_manifest("unknown-service-id"), nullptr); CHECK_EQ(service_registration_find_manifest("unknown-service-id"), nullptr);
CHECK_EQ(service_manager_find_context("unknown-service-id"), nullptr); CHECK_EQ(service_registration_find_context("unknown-service-id"), nullptr);
CHECK_EQ(service_manager_start("unknown-service-id"), ERROR_NOT_FOUND); CHECK_EQ(service_registration_find_service("unknown-service-id"), nullptr);
CHECK_EQ(service_manager_stop("unknown-service-id"), ERROR_NOT_FOUND); CHECK_EQ(service_registration_start("unknown-service-id"), ERROR_NOT_FOUND);
CHECK_EQ(service_manager_remove("unknown-service-id"), ERROR_NOT_FOUND); CHECK_EQ(service_registration_stop("unknown-service-id"), ERROR_NOT_FOUND);
CHECK_EQ(service_registration_remove("unknown-service-id"), ERROR_NOT_FOUND);
} }

View File

@ -2,6 +2,8 @@ project(CryptModuleTests)
enable_language(C CXX ASM) enable_language(C CXX ASM)
set(CMAKE_CXX_COMPILER g++)
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp) file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/Source/*.cpp)
add_executable(CryptModuleTests EXCLUDE_FROM_ALL ${TEST_SOURCES}) add_executable(CryptModuleTests EXCLUDE_FROM_ALL ${TEST_SOURCES})