Implemented TactilityKernel and DevicetreeCompiler, updated licenses & copyrights (#452)
**New features** - Created a devicetree DTS and YAML parser in Python - Created new modules: - TactilityKernel (LGPL v3.0 license) - Platforms/PlatformEsp32 (LGPL v3.0 license) - Platforms/PlatformPosix (LGPL v3.0 license) - Tests/TactilityKernelTests Most boards have a placeholder DTS file, while T-Lora Pager has a few devices attached. **Licenses** Clarified licenses and copyrights better. - Add explanation about the intent behind them. - Added explanation about licenses for past and future subprojects - Added more details explanations with regards to the logo usage - Copied licenses to subprojects to make it more explicit
This commit is contained in:
committed by
GitHub
parent
0d16eb606f
commit
4b6ed871a9
@@ -0,0 +1,225 @@
|
||||
#include <Tactility/Device.h>
|
||||
#include <Tactility/Error.h>
|
||||
#include <Tactility/Driver.h>
|
||||
#include <Tactility/Log.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <sys/errno.h>
|
||||
#include <vector>
|
||||
|
||||
#define TAG LOG_TAG(device)
|
||||
|
||||
struct DeviceData {
|
||||
std::vector<Device*> children;
|
||||
};
|
||||
|
||||
struct DeviceLedger {
|
||||
std::vector<Device*> devices;
|
||||
Mutex mutex { 0 };
|
||||
|
||||
DeviceLedger() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~DeviceLedger() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
static DeviceLedger& get_ledger() {
|
||||
static DeviceLedger ledger;
|
||||
return ledger;
|
||||
}
|
||||
|
||||
#define ledger get_ledger()
|
||||
|
||||
extern "C" {
|
||||
|
||||
#define ledger_lock() mutex_lock(&ledger.mutex)
|
||||
#define ledger_unlock() mutex_unlock(&ledger.mutex)
|
||||
|
||||
#define get_device_data(device) static_cast<DeviceData*>(device->internal.data)
|
||||
|
||||
int device_construct(Device* device) {
|
||||
device->internal.data = new(std::nothrow) DeviceData;
|
||||
if (device->internal.data == nullptr) {
|
||||
return ENOMEM;
|
||||
}
|
||||
LOG_I(TAG, "construct %s", device->name);
|
||||
mutex_construct(&device->internal.mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int device_destruct(Device* device) {
|
||||
if (device->internal.state.started || device->internal.state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
if (!get_device_data(device)->children.empty()) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
LOG_I(TAG, "destruct %s", device->name);
|
||||
mutex_destruct(&device->internal.mutex);
|
||||
delete get_device_data(device);
|
||||
device->internal.data = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Add a child to the list of children */
|
||||
static void device_add_child(struct Device* device, struct Device* child) {
|
||||
device_lock(device);
|
||||
assert(device->internal.state.added);
|
||||
get_device_data(device)->children.push_back(child);
|
||||
device_unlock(device);
|
||||
}
|
||||
|
||||
/** Remove a child from the list of children */
|
||||
static void device_remove_child(struct Device* device, struct Device* child) {
|
||||
device_lock(device);
|
||||
auto* parent_data = get_device_data(device);
|
||||
const auto iterator = std::ranges::find(parent_data->children, child);
|
||||
if (iterator != parent_data->children.end()) {
|
||||
parent_data->children.erase(iterator);
|
||||
}
|
||||
device_unlock(device);
|
||||
}
|
||||
|
||||
int device_add(Device* device) {
|
||||
LOG_I(TAG, "add %s", device->name);
|
||||
|
||||
// Already added
|
||||
if (device->internal.state.started || device->internal.state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Add to ledger
|
||||
ledger_lock();
|
||||
ledger.devices.push_back(device);
|
||||
ledger_unlock();
|
||||
|
||||
// Add self to parent's children list
|
||||
auto* parent = device->parent;
|
||||
if (parent != nullptr) {
|
||||
device_add_child(parent, device);
|
||||
}
|
||||
|
||||
device->internal.state.added = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int device_remove(Device* device) {
|
||||
LOG_I(TAG, "remove %s", device->name);
|
||||
|
||||
if (device->internal.state.started || !device->internal.state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Remove self from parent's children list
|
||||
auto* parent = device->parent;
|
||||
if (parent != nullptr) {
|
||||
device_remove_child(parent, device);
|
||||
}
|
||||
|
||||
ledger_lock();
|
||||
const auto iterator = std::ranges::find(ledger.devices, device);
|
||||
if (iterator == ledger.devices.end()) {
|
||||
ledger_unlock();
|
||||
goto failed_ledger_lookup;
|
||||
}
|
||||
ledger.devices.erase(iterator);
|
||||
ledger_unlock();
|
||||
|
||||
device->internal.state.added = false;
|
||||
return 0;
|
||||
|
||||
failed_ledger_lookup:
|
||||
|
||||
// Re-add to parent
|
||||
if (parent != nullptr) {
|
||||
device_add_child(parent, device);
|
||||
}
|
||||
|
||||
return ERROR_NOT_FOUND;
|
||||
}
|
||||
|
||||
int device_start(Device* device) {
|
||||
if (!device->internal.state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
if (device->internal.driver == nullptr) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Already started
|
||||
if (device->internal.state.started) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int result = driver_bind(device->internal.driver, device);
|
||||
device->internal.state.started = (result == 0);
|
||||
device->internal.state.start_result = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
int device_stop(struct Device* device) {
|
||||
if (!device->internal.state.added) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
// Not started
|
||||
if (!device->internal.state.started) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int result = driver_unbind(device->internal.driver, device);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
device->internal.state.started = false;
|
||||
device->internal.state.start_result = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void device_set_parent(Device* device, Device* parent) {
|
||||
assert(!device->internal.state.started);
|
||||
device->parent = parent;
|
||||
}
|
||||
|
||||
void for_each_device(void* callback_context, bool(*on_device)(Device* device, void* context)) {
|
||||
ledger_lock();
|
||||
for (auto* device : ledger.devices) {
|
||||
if (!on_device(device, callback_context)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ledger_unlock();
|
||||
}
|
||||
|
||||
void for_each_device_child(Device* device, void* callback_context, bool(*on_device)(struct Device* device, void* context)) {
|
||||
auto* data = get_device_data(device);
|
||||
for (auto* child_device : data->children) {
|
||||
if (!on_device(child_device, callback_context)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void for_each_device_of_type(const DeviceType* type, void* callback_context, bool(*on_device)(Device* device, void* context)) {
|
||||
ledger_lock();
|
||||
for (auto* device : ledger.devices) {
|
||||
auto* driver = device->internal.driver;
|
||||
if (driver != nullptr) {
|
||||
if (driver->device_type == type) {
|
||||
if (!on_device(device, callback_context)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ledger_unlock();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,188 @@
|
||||
#include <cstring>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
#include <Tactility/concurrent/Mutex.h>
|
||||
#include <Tactility/Device.h>
|
||||
#include <Tactility/Driver.h>
|
||||
#include <Tactility/Error.h>
|
||||
#include <Tactility/Log.h>
|
||||
|
||||
#define TAG LOG_TAG(driver)
|
||||
|
||||
struct DriverInternalData {
|
||||
Mutex mutex { 0 };
|
||||
int use_count = 0;
|
||||
|
||||
DriverInternalData() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~DriverInternalData() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
struct DriverLedger {
|
||||
std::vector<Driver*> drivers;
|
||||
Mutex mutex { 0 };
|
||||
|
||||
DriverLedger() {
|
||||
mutex_construct(&mutex);
|
||||
}
|
||||
|
||||
~DriverLedger() {
|
||||
mutex_destruct(&mutex);
|
||||
}
|
||||
|
||||
void lock() {
|
||||
mutex_lock(&mutex);
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
mutex_unlock(&mutex);
|
||||
}
|
||||
};
|
||||
|
||||
static DriverLedger& get_ledger() {
|
||||
static DriverLedger ledger;
|
||||
return ledger;
|
||||
}
|
||||
|
||||
#define ledger get_ledger()
|
||||
|
||||
#define driver_internal_data(driver) static_cast<DriverInternalData*>(driver->internal.data)
|
||||
#define driver_lock(driver) mutex_lock(&driver_internal_data(driver)->mutex);
|
||||
#define driver_unlock(driver) mutex_unlock(&driver_internal_data(driver)->mutex);
|
||||
|
||||
static void driver_add(Driver* driver) {
|
||||
LOG_I(TAG, "add %s", driver->name);
|
||||
ledger.lock();
|
||||
ledger.drivers.push_back(driver);
|
||||
ledger.unlock();
|
||||
}
|
||||
|
||||
static bool driver_remove(Driver* driver) {
|
||||
LOG_I(TAG, "remove %s", driver->name);
|
||||
|
||||
ledger.lock();
|
||||
const auto iterator = std::ranges::find(ledger.drivers, driver);
|
||||
// check that there actually is a 3 in our vector
|
||||
if (iterator == ledger.drivers.end()) {
|
||||
ledger.unlock();
|
||||
return false;
|
||||
}
|
||||
ledger.drivers.erase(iterator);
|
||||
ledger.unlock();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int driver_construct(Driver* driver) {
|
||||
driver->internal.data = new(std::nothrow) DriverInternalData;
|
||||
if (driver->internal.data == nullptr) {
|
||||
return ENOMEM;
|
||||
}
|
||||
driver_add(driver);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int driver_destruct(Driver* driver) {
|
||||
// Check if in use
|
||||
if (driver_internal_data(driver)->use_count != 0) {
|
||||
return ERROR_INVALID_STATE;
|
||||
}
|
||||
|
||||
driver_remove(driver);
|
||||
delete driver_internal_data(driver);
|
||||
driver->internal.data = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool driver_is_compatible(Driver* driver, const char* compatible) {
|
||||
if (compatible == nullptr || driver->compatible == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const char** compatible_iterator = driver->compatible;
|
||||
while (*compatible_iterator != nullptr) {
|
||||
if (strcmp(*compatible_iterator, compatible) == 0) {
|
||||
return true;
|
||||
}
|
||||
compatible_iterator++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Driver* driver_find_compatible(const char* compatible) {
|
||||
ledger.lock();
|
||||
Driver* result = nullptr;
|
||||
for (auto* driver : ledger.drivers) {
|
||||
if (driver_is_compatible(driver, compatible)) {
|
||||
result = driver;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ledger.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
int driver_bind(Driver* driver, Device* device) {
|
||||
driver_lock(driver);
|
||||
|
||||
int err = 0;
|
||||
if (!device_is_added(device)) {
|
||||
err = ERROR_INVALID_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (driver->start_device != nullptr) {
|
||||
err = driver->start_device(device);
|
||||
if (err != 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
driver_internal_data(driver)->use_count++;
|
||||
driver_unlock(driver);
|
||||
|
||||
LOG_I(TAG, "bound %s to %s", driver->name, device->name);
|
||||
return 0;
|
||||
|
||||
error:
|
||||
|
||||
driver_unlock(driver);
|
||||
return err;
|
||||
}
|
||||
|
||||
int driver_unbind(Driver* driver, Device* device) {
|
||||
driver_lock(driver);
|
||||
|
||||
int err = 0;
|
||||
if (!device_is_added(device)) {
|
||||
err = ERROR_INVALID_STATE;
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (driver->stop_device != nullptr) {
|
||||
err = driver->stop_device(device);
|
||||
if (err != 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
driver_internal_data(driver)->use_count--;
|
||||
driver_unlock(driver);
|
||||
|
||||
LOG_I(TAG, "unbound %s to %s", driver->name, device->name);
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
|
||||
driver_unlock(driver);
|
||||
return err;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef ESP_PLATFORM
|
||||
|
||||
#include <Tactility/Log.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
void log_generic(const char* tag, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
printf("%s ", tag);
|
||||
vprintf(format, args);
|
||||
printf("\n");
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <Tactility/drivers/GpioController.h>
|
||||
#include <Tactility/Driver.h>
|
||||
|
||||
#define GPIO_DRIVER_API(driver) ((struct GpioControllerApi*)driver->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
bool gpio_controller_set_level(Device* device, gpio_pin_t pin, bool high) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return GPIO_DRIVER_API(driver)->set_level(device, pin, high);
|
||||
}
|
||||
|
||||
bool gpio_controller_get_level(Device* device, gpio_pin_t pin, bool* high) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return GPIO_DRIVER_API(driver)->get_level(device, pin, high);
|
||||
}
|
||||
|
||||
bool gpio_controller_set_options(Device* device, gpio_pin_t pin, gpio_flags_t options) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return GPIO_DRIVER_API(driver)->set_options(device, pin, options);
|
||||
}
|
||||
|
||||
bool gpio_controller_get_options(Device* device, gpio_pin_t pin, gpio_flags_t* options) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return GPIO_DRIVER_API(driver)->get_options(device, pin, options);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <Tactility/drivers/I2cController.h>
|
||||
#include <Tactility/Driver.h>
|
||||
|
||||
#define I2C_DRIVER_API(driver) ((struct I2cControllerApi*)driver->api)
|
||||
|
||||
extern "C" {
|
||||
|
||||
bool i2c_controller_read(Device* device, uint8_t address, uint8_t* data, size_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->read(device, address, data, dataSize, timeout);
|
||||
}
|
||||
|
||||
bool i2c_controller_write(Device* device, uint8_t address, const uint8_t* data, uint16_t dataSize, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->write(device, address, data, dataSize, timeout);
|
||||
}
|
||||
|
||||
bool i2c_controller_write_read(Device* device, uint8_t address, const uint8_t* write_data, size_t write_data_size, uint8_t* read_data, size_t read_data_size, TickType_t timeout) {
|
||||
const auto* driver = device_get_driver(device);
|
||||
return I2C_DRIVER_API(driver)->write_read(device, address, write_data, write_data_size, read_data, read_data_size, timeout);
|
||||
}
|
||||
|
||||
const struct DeviceType I2C_CONTROLLER_TYPE { 0 };
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <Tactility/Driver.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern void register_kernel_drivers() {
|
||||
extern Driver root_driver;
|
||||
driver_construct(&root_driver);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <Tactility/drivers/Root.h>
|
||||
#include <Tactility/Driver.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
Driver root_driver = {
|
||||
.name = "root",
|
||||
.compatible = (const char*[]) { "root", nullptr },
|
||||
.start_device = nullptr,
|
||||
.stop_device = nullptr,
|
||||
.api = nullptr,
|
||||
.device_type = nullptr,
|
||||
.internal = { 0 }
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user