Refactor LVGL code into kernel module (#472)
* **New Features** * Added a standalone LVGL module and enabled LVGL support in the simulator for richer local UI testing. * **Refactor** * HAL and LVGL split into distinct modules; startup and device attach/detach flows reorganized for clearer lifecycle management. * Public APIs tightened with clearer nullability/documentation. * **Bug Fixes** * More consistent LVGL start/stop and device attach/detach behavior for improved stability.
This commit is contained in:
committed by
GitHub
parent
3fe1dc0312
commit
9f721e6655
@@ -0,0 +1,126 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/drivers/hal_device.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
|
||||
#include <tactility/log.h>
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#define TAG LOG_TAG(HalDevice)
|
||||
|
||||
struct HalDevicePrivate {
|
||||
std::shared_ptr<tt::hal::Device> halDevice;
|
||||
};
|
||||
|
||||
#define GET_DATA(device) ((struct HalDevicePrivate*)device->internal.driver_data)
|
||||
|
||||
static enum HalDeviceType getHalDeviceType(tt::hal::Device::Type type) {
|
||||
switch (type) {
|
||||
case tt::hal::Device::Type::I2c:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_I2C;
|
||||
case tt::hal::Device::Type::Display:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_DISPLAY;
|
||||
case tt::hal::Device::Type::Touch:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_TOUCH;
|
||||
case tt::hal::Device::Type::SdCard:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_SDCARD;
|
||||
case tt::hal::Device::Type::Keyboard:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_KEYBOARD;
|
||||
case tt::hal::Device::Type::Encoder:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_ENCODER;
|
||||
case tt::hal::Device::Type::Power:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_POWER;
|
||||
case tt::hal::Device::Type::Gps:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_GPS;
|
||||
case tt::hal::Device::Type::Other:
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_OTHER;
|
||||
default:
|
||||
LOG_W(TAG, "Device type %d is not implemented", static_cast<int>(type));
|
||||
return HalDeviceType::HAL_DEVICE_TYPE_OTHER;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
HalDeviceType hal_device_get_type(struct Device* device) {
|
||||
auto type = GET_DATA(device)->halDevice->getType();
|
||||
return getHalDeviceType(type);
|
||||
}
|
||||
|
||||
void hal_device_for_each_of_type(HalDeviceType type, void* context, bool(*onDevice)(struct Device* device, void* context)) {
|
||||
struct InternalContext {
|
||||
HalDeviceType typeParam;
|
||||
void* contextParam;
|
||||
bool(*onDeviceParam)(struct Device* device, void* context);
|
||||
};
|
||||
|
||||
InternalContext internal_context = {
|
||||
.typeParam = type,
|
||||
.contextParam = context,
|
||||
.onDeviceParam = onDevice
|
||||
};
|
||||
|
||||
for_each_device_of_type(&HAL_DEVICE_TYPE, &internal_context, [](Device* device, void* context){
|
||||
auto* hal_device_private = GET_DATA(device);
|
||||
auto* internal_context = static_cast<InternalContext*>(context);
|
||||
auto hal_device_type = getHalDeviceType(hal_device_private->halDevice->getType());
|
||||
if (hal_device_type == internal_context->typeParam) {
|
||||
if (!internal_context->onDeviceParam(device, internal_context->contextParam)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
std::shared_ptr<Device> hal_device_get_device(::Device* device) {
|
||||
auto* hal_device_private = GET_DATA(device);
|
||||
return hal_device_private->halDevice;
|
||||
}
|
||||
|
||||
void hal_device_set_device(::Device* kernelDevice, std::shared_ptr<Device> halDevice) {
|
||||
GET_DATA(kernelDevice)->halDevice = std::move(halDevice);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#pragma region Lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
LOG_I(TAG, "start %s", device->name);
|
||||
device->internal.driver_data = new HalDevicePrivate();
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
LOG_I(TAG, "stop %s", device->name);
|
||||
delete GET_DATA(device);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
extern "C" {
|
||||
|
||||
const struct DeviceType HAL_DEVICE_TYPE {0};
|
||||
|
||||
extern struct Module hal_device_module;
|
||||
|
||||
Driver hal_device_driver = {
|
||||
.name = "hal-device",
|
||||
.compatible = (const char*[]) {"hal-device", nullptr},
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = nullptr,
|
||||
.device_type = &HAL_DEVICE_TYPE,
|
||||
.owner = &hal_device_module,
|
||||
.driver_private = nullptr
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/hal/Device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/hal_device.hpp>
|
||||
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/RecursiveMutex.h>
|
||||
#include <algorithm>
|
||||
|
||||
namespace tt::hal {
|
||||
|
||||
RecursiveMutex mutex;
|
||||
static Device::Id nextId = 0;
|
||||
|
||||
static const auto LOGGER = Logger("Devices");
|
||||
|
||||
Device::Device() : id(nextId++) {}
|
||||
|
||||
static std::shared_ptr<Device::KernelDeviceHolder> createKernelDeviceHolder(const std::shared_ptr<Device>& device) {
|
||||
auto kernel_device_name = std::format("hal-device-{}", device->getId());
|
||||
LOGGER.info("Registering {} with id {} as kernel device {}", device->getName(), device->getId(), kernel_device_name);
|
||||
auto kernel_device_holder = std::make_shared<Device::KernelDeviceHolder>(kernel_device_name);
|
||||
auto* kernel_device = kernel_device_holder->device.get();
|
||||
check(device_construct(kernel_device) == ERROR_NONE);
|
||||
check(device_add(kernel_device) == ERROR_NONE);
|
||||
auto* driver = driver_find_compatible("hal-device");
|
||||
check(driver);
|
||||
device_set_driver(kernel_device, driver);
|
||||
check(device_start(kernel_device) == ERROR_NONE);
|
||||
hal_device_set_device(kernel_device, device);
|
||||
return kernel_device_holder;
|
||||
}
|
||||
|
||||
static void destroyKernelDeviceHolder(std::shared_ptr<Device::KernelDeviceHolder>& holder) {
|
||||
auto kernel_device = holder->device.get();
|
||||
hal_device_set_device(kernel_device, nullptr);
|
||||
check(device_stop(kernel_device) == ERROR_NONE);
|
||||
check(device_remove(kernel_device) == ERROR_NONE);
|
||||
check(device_destruct(kernel_device) == ERROR_NONE);
|
||||
holder->device = nullptr;
|
||||
}
|
||||
|
||||
void registerDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
if (device->getKernelDeviceHolder() == nullptr) {
|
||||
// Kernel device
|
||||
auto kernel_device_holder = createKernelDeviceHolder(device);
|
||||
device->setKernelDeviceHolder(kernel_device_holder);
|
||||
} else {
|
||||
LOGGER.warn("Device {} with id {} was already registered", device->getName(), device->getId());
|
||||
}
|
||||
}
|
||||
|
||||
void deregisterDevice(const std::shared_ptr<Device>& device) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
// Kernel device
|
||||
auto kernel_device_holder = device->getKernelDeviceHolder();
|
||||
if (kernel_device_holder) {
|
||||
destroyKernelDeviceHolder(kernel_device_holder);
|
||||
device->setKernelDeviceHolder(nullptr);
|
||||
} else {
|
||||
LOGGER.warn("Device {} with id {} was not registered", device->getName(), device->getId());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
auto toVector(R&& range) {
|
||||
using T = std::ranges::range_value_t<R>;
|
||||
std::vector<T> result;
|
||||
if constexpr (std::ranges::common_range<R>) {
|
||||
result.reserve(std::ranges::distance(range));
|
||||
}
|
||||
std::ranges::copy(range, std::back_inserter(result));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<Device>> findDevices(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto devices_view = getDevices() | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
return toVector(devices_view);
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> findDevice(const std::function<bool(const std::shared_ptr<Device>&)>& filterFunction) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
|
||||
auto result_set = getDevices() | std::views::filter([&filterFunction](auto& device) {
|
||||
return filterFunction(device);
|
||||
});
|
||||
if (!result_set.empty()) {
|
||||
return result_set.front();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> findDevice(std::string name) {
|
||||
return findDevice([&name](auto& device){
|
||||
return device->getName() == name;
|
||||
});
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> 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() {
|
||||
std::vector<std::shared_ptr<Device>> devices;
|
||||
for_each_device_of_type(&HAL_DEVICE_TYPE, &devices ,[](auto* kernelDevice, auto* context) {
|
||||
auto devices_ptr = static_cast<std::vector<std::shared_ptr<Device>>*>(context);
|
||||
auto hal_device = hal_device_get_device(kernelDevice);
|
||||
(*devices_ptr).push_back(hal_device);
|
||||
return true;
|
||||
});
|
||||
return devices;
|
||||
}
|
||||
|
||||
bool hasDevice(Device::Type type) {
|
||||
auto scoped_mutex = mutex.asScopedLock();
|
||||
scoped_mutex.lock();
|
||||
auto result_set = getDevices() | std::views::filter([&type](auto& device) {
|
||||
return device->getType() == type;
|
||||
});
|
||||
return !result_set.empty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver hal_device_driver;
|
||||
|
||||
static error_t start() {
|
||||
/* We crash when construct fails, because if a single driver fails to construct,
|
||||
* there is no guarantee that the previously constructed drivers can be destroyed */
|
||||
check(driver_construct_add(&hal_device_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
/* We crash when destruct fails, because if a single driver fails to destruct,
|
||||
* there is no guarantee that the previously destroyed drivers can be recovered */
|
||||
check(driver_remove(&hal_device_driver) == ERROR_NONE);
|
||||
check(driver_destruct(&hal_device_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
struct Module hal_device_module = {
|
||||
.name = "hal-device",
|
||||
.start = start,
|
||||
.stop = stop
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user