Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Tactility/Include/Tactility/service/Service.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#pragma once

#include <tactility/service/service_instance.h>

#include <memory>

namespace tt::service {

enum class State {
Starting,
Started,
Stopping,
Stopped
};
using State = ::ServiceState;

// Forward declaration
class ServiceContext;
Expand All @@ -26,6 +23,6 @@ class Service {
};

template<typename T>
std::shared_ptr<Service> create() { return std::shared_ptr<T>(new T); }
void* create(const ::ServiceManifest* /*manifest*/) { return new std::shared_ptr<Service>(std::shared_ptr<T>(new T)); }

}
19 changes: 11 additions & 8 deletions Tactility/Include/Tactility/service/ServiceContext.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
#pragma once

#include <memory>
#include <tactility/service/service_manifest.h>

struct ServiceInstance;

namespace tt::service {

struct ServiceManifest;
class ServicePaths;

/**
* The public representation of a service instance.
* Thin, non-owning wrapper around the running TactilityKernel service instance
* (ServiceInstance, which the kernel API also calls ServiceContext).
* @warning Do not store references or pointers to these! You can retrieve them via the Loader service.
*/
class ServiceContext {

protected:
class ServiceContext final {

virtual ~ServiceContext() = default;
::ServiceInstance* service_instance;

public:

explicit ServiceContext(::ServiceInstance* instance) : service_instance(instance) {}

/** @return a reference to the service's manifest */
virtual const ServiceManifest& getManifest() const = 0;
const ::ServiceManifest* getManifest() const;
Comment thread
KenVanHoeylandt marked this conversation as resolved.

/** Retrieve the paths that are relevant to this service */
virtual std::unique_ptr<ServicePaths> getPaths() const = 0;
std::unique_ptr<ServicePaths> getPaths() const;
};


} // namespace
2 changes: 1 addition & 1 deletion Tactility/Include/Tactility/service/ServiceManifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace tt::service {
// Forward declarations
class ServiceContext;

typedef std::shared_ptr<Service>(*CreateService)();
using CreateService = ::ServiceCreate;

/** A ledger that describes the main parts of a service. */
struct ServiceManifest {
Expand Down
42 changes: 32 additions & 10 deletions Tactility/Include/Tactility/service/ServicePaths.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,67 @@
#pragma once

#include <tactility/check.h>
#include <tactility/error.h>
#include <tactility/service/service_manifest.h>
#include <tactility/service/service_paths.h>

#include <cassert>
#include <string>
#include <memory>

namespace tt::service {

// Forward declarations
class ServiceManifest;
constexpr size_t PATH_BUFFER_SIZE = 255;

class ServicePaths {

std::shared_ptr<const ServiceManifest> manifest;
const ::ServiceManifest* manifest;

public:

explicit ServicePaths(std::shared_ptr<const ServiceManifest> manifest) : manifest(std::move(manifest)) {}
explicit ServicePaths(const ::ServiceManifest* manifest) : manifest(manifest) {}

/**
* The user data directory is intended to survive OS upgrades.
* The path will not end with a "/".
*/
std::string getUserDataDirectory() const;
std::string getUserDataDirectory() const {
char buffer[PATH_BUFFER_SIZE];
check(service_paths_get_user_data_directory(manifest->id, buffer, sizeof(buffer)) == ERROR_NONE);
return buffer;
}

/**
* The user data directory is intended to survive OS upgrades.
* Configuration data should be stored here.
* @param[in] childPath the path without a "/" prefix
*/
std::string getUserDataPath(const std::string& childPath) const;
std::string getUserDataPath(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
char buffer[PATH_BUFFER_SIZE];
check(service_paths_get_user_data_path(manifest->id, childPath.c_str(), buffer, sizeof(buffer)) == ERROR_NONE);
return buffer;
}

/**
* You should not store configuration data here.
* The path will not end with a "/".
*/
std::string getAssetsDirectory() const;
std::string getAssetsDirectory() const {
char buffer[PATH_BUFFER_SIZE];
check(service_paths_get_assets_directory(manifest->id, buffer, sizeof(buffer)) == ERROR_NONE);
return buffer;
}

/**
* You should not store configuration data here.
* @param[in] childPath the path without a "/" prefix
*/
std::string getAssetsPath(const std::string& childPath) const;
std::string getAssetsPath(const std::string& childPath) const {
assert(!childPath.starts_with('/'));
char buffer[PATH_BUFFER_SIZE];
check(service_paths_get_assets_path(manifest->id, childPath.c_str(), buffer, sizeof(buffer)) == ERROR_NONE);
return buffer;
}
};

}
}
2 changes: 1 addition & 1 deletion Tactility/Include/Tactility/service/ServiceRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ State getState(const std::string& id);
* @param[in] id the id as defined in the manifest
* @return the matching manifest or nullptr when it wasn't found
*/
std::shared_ptr<const ServiceManifest> findManifestById(const std::string& id);
const ::ServiceManifest* findManifestById(const std::string& id);

/** Find a ServiceContext by its manifest id.
* @param[in] id the id as defined in the manifest
Expand Down
36 changes: 0 additions & 36 deletions Tactility/Private/Tactility/service/ServiceInstance.h

This file was deleted.

14 changes: 11 additions & 3 deletions Tactility/Source/bluetooth/BluetoothPairedDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <Tactility/bluetooth/BluetoothPairedDevice.h>

#include "Tactility/Paths.h"

#include <Tactility/file/File.h>
#include <Tactility/file/PropertiesFile.h>
#include <tactility/log.h>
Expand All @@ -16,13 +18,16 @@ namespace tt::bluetooth::settings {
constexpr auto* TAG = "BluetoothPairedDevice";

// Use the same directory as the old service for backward compatibility.
constexpr auto* DATA_DIR = "/data/service/bluetooth";
constexpr auto* DEVICE_SETTINGS_FORMAT = "{}/{}.device.properties";
constexpr auto* KEY_NAME = "name";
constexpr auto* KEY_ADDR = "addr";
constexpr auto* KEY_AUTO_CONNECT = "autoConnect";
constexpr auto* KEY_PROFILE_ID = "profileId";

static std::string getSettingsFilePath() {
return getUserDataPath() + "/service/bluetooth";
}

Comment thread
KenVanHoeylandt marked this conversation as resolved.
std::string addrToHex(const std::array<uint8_t, 6>& addr) {
std::stringstream stream;
stream << std::hex;
Expand Down Expand Up @@ -52,7 +57,7 @@ static bool hexToAddr(const std::string& hex, std::array<uint8_t, 6>& addr) {
}

static std::string getFilePath(const std::string& addr_hex) {
return std::format(DEVICE_SETTINGS_FORMAT, DATA_DIR, addr_hex);
return std::format(DEVICE_SETTINGS_FORMAT, getSettingsFilePath(), addr_hex);
}

bool hasFileForDevice(const std::string& addr_hex) {
Expand Down Expand Up @@ -102,7 +107,10 @@ bool remove(const std::string& addr_hex) {

std::vector<PairedDevice> loadAll() {
std::vector<dirent> entries;
file::scandir(DATA_DIR, entries, [](const dirent* entry) -> int {
if (!file::isDirectory(getSettingsFilePath())) {
return {};
}
file::scandir(getSettingsFilePath(), entries, [](const dirent* entry) -> int {
if (entry->d_type != file::TT_DT_REG && entry->d_type != file::TT_DT_UNKNOWN) return -1;
std::string name = entry->d_name;
return name.ends_with(".device.properties") ? 0 : -1;
Expand Down
4 changes: 2 additions & 2 deletions Tactility/Source/lvgl/Lvgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void attachDevices() {
// We search for the manifest first, because during the initial start() during boot
// the service won't be registered yet.
if (service::findManifestById("Gui") != nullptr) {
if (service::getState("Gui") == service::State::Stopped) {
if (service::getState("Gui") == SERVICE_STATE_STOPPED) {
service::startService("Gui");
} else {
LOG_E(TAG, "Gui service is not in Stopped state");
Expand All @@ -112,7 +112,7 @@ void attachDevices() {
// We search for the manifest first, because during the initial start() during boot
// the service won't be registered yet.
if (service::findManifestById("Statusbar") != nullptr) {
if (service::getState("Statusbar") == service::State::Stopped) {
if (service::getState("Statusbar") == SERVICE_STATE_STOPPED) {
service::startService("Statusbar");
} else {
LOG_E(TAG, "Statusbar service is not in Stopped state");
Expand Down
17 changes: 17 additions & 0 deletions Tactility/Source/service/ServiceContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <Tactility/service/ServiceContext.h>

#include <Tactility/service/ServicePaths.h>

#include <tactility/service/service_instance.h>

namespace tt::service {

const ::ServiceManifest* ServiceContext::getManifest() const {
return service_instance_get_manifest(service_instance);
}

std::unique_ptr<ServicePaths> ServiceContext::getPaths() const {
return std::make_unique<ServicePaths>(getManifest());
}

} // namespace
19 changes: 0 additions & 19 deletions Tactility/Source/service/ServiceInstance.cpp

This file was deleted.

29 changes: 0 additions & 29 deletions Tactility/Source/service/ServicePaths.cpp

This file was deleted.

Loading
Loading