Implement device management (#199)
- Added `tt::hal::Device` and functions (de)register devices and search for them. - Refactored apps: `Power` and `Display` settings apps now use the device API to find devices. - Implemented the new API for all existing drivers for all devices, including the simulator. - Updated HAL Configuration to return `std::shared_ptr` instead of raw pointers. - Added test project for headless tests and implemented tests for the new code.
This commit is contained in:
committed by
GitHub
parent
c87200a80d
commit
cff0605b0a
@@ -0,0 +1,97 @@
|
||||
#include "Tactility/hal/Device.h"
|
||||
|
||||
#include <ranges>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
std::vector<std::shared_ptr<Device>> devices;
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
static Device::Id nextId = 0;
|
||||
|
||||
#define TAG "devices"
|
||||
|
||||
Device::Device() : id(nextId++) {}
|
||||
|
||||
template <std::ranges::range RangeType>
|
||||
auto toVector(RangeType&& range) {
|
||||
auto view = range | std::views::common;
|
||||
return std::vector(view.begin(), view.end());
|
||||
}
|
||||
|
||||
void registerDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
if (scoped_mutex->lock()) {
|
||||
if (findDevice(device->getId()) == nullptr) {
|
||||
devices.push_back(device);
|
||||
TT_LOG_I(TAG, "Registered %s with id %lu", device->getName().c_str(), device->getId());
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Device %s with id %lu was already registered", device->getName().c_str(), device->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void deregisterDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
auto id_to_remove = device->getId();
|
||||
if (scoped_mutex->lock()) {
|
||||
auto remove_iterator = std::remove_if(devices.begin(), devices.end(), [id_to_remove](const auto& device) {
|
||||
return device->getId() == id_to_remove;
|
||||
});
|
||||
if (remove_iterator != devices.end()) {
|
||||
TT_LOG_I(TAG, "Deregistering %s with id %lu", device->getName().c_str(), device->getId());
|
||||
devices.erase(remove_iterator);
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Deregistering %s with id %lu failed: not found", device->getName().c_str(), device->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> findDevices(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
if (scoped_mutex->lock()) {
|
||||
auto devices_view = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
return toVector(devices_view);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::shared_ptr<Device> _Nullable findDevice(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.scoped();
|
||||
if (scoped_mutex->lock()) {
|
||||
auto result_set = devices | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
if (!result_set.empty()) {
|
||||
return result_set.front();
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(std::string name) {
|
||||
return findDevice([&name](auto& device){
|
||||
return device->getName() == name;
|
||||
});
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> _Nullable findDevice(Device::Id id) {
|
||||
return findDevice([id](auto& device){
|
||||
return device->getId() == id;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> findDevices(Device::Type type) {
|
||||
return findDevices([type](auto& device) {
|
||||
return device->getType() == type;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> getDevices() {
|
||||
return devices;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user