mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
**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
184 lines
5.3 KiB
C
184 lines
5.3 KiB
C
#pragma once
|
|
|
|
#include "Driver.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <Tactility/concurrent/Mutex.h>
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
struct Driver;
|
|
|
|
/** Enables discovering devices of the same type */
|
|
struct DeviceType {
|
|
/* Placeholder because empty structs have a different size with C vs C++ compilers */
|
|
uint8_t _;
|
|
};
|
|
|
|
/** Represents a piece of hardware */
|
|
struct Device {
|
|
/** The name of the device. Valid characters: a-z a-Z 0-9 - _ . */
|
|
const char* name;
|
|
/** The configuration data for the device's driver */
|
|
const void* config;
|
|
/** The parent device that this device belongs to. Can be NULL, but only the root device should have a NULL parent. */
|
|
struct Device* parent;
|
|
/** Internal data */
|
|
struct {
|
|
/** Address of the API exposed by the device instance. */
|
|
struct Driver* driver;
|
|
/** The driver data for this device (e.g. a mutex) */
|
|
void* driver_data;
|
|
/** The mutex for device operations */
|
|
struct Mutex mutex;
|
|
/** The device state */
|
|
struct {
|
|
int start_result;
|
|
bool started : 1;
|
|
bool added : 1;
|
|
} state;
|
|
/** Private data */
|
|
void* data;
|
|
} internal;
|
|
};
|
|
|
|
/**
|
|
* Initialize the properties of a device.
|
|
*
|
|
* @param[in] dev a device with all non-internal properties set
|
|
* @return the result code (0 for success)
|
|
*/
|
|
int device_construct(struct Device* device);
|
|
|
|
/**
|
|
* Deinitialize the properties of a device.
|
|
* This fails when a device is busy or has children.
|
|
*
|
|
* @param[in] dev
|
|
* @return the result code (0 for success)
|
|
*/
|
|
int device_destruct(struct Device* device);
|
|
|
|
/**
|
|
* Indicates whether the device is in a state where its API is available
|
|
*
|
|
* @param[in] dev non-null device pointer
|
|
* @return true if the device is ready for use
|
|
*/
|
|
static inline bool device_is_ready(const struct Device* device) {
|
|
return device->internal.state.started;
|
|
}
|
|
|
|
/**
|
|
* Register a device to all relevant systems:
|
|
* - the global ledger
|
|
* - its parent (if any)
|
|
* - a bus (if any)
|
|
*
|
|
* @param[in] device non-null device pointer
|
|
* @return 0 on success
|
|
*/
|
|
int device_add(struct Device* device);
|
|
|
|
/**
|
|
* Deregister a device. Remove it from all relevant systems:
|
|
* - the global ledger
|
|
* - its parent (if any)
|
|
* - a bus (if any)
|
|
*
|
|
* @param[in] device non-null device pointer
|
|
* @return 0 on success
|
|
*/
|
|
int device_remove(struct Device* device);
|
|
|
|
/**
|
|
* Attach the driver.
|
|
*
|
|
* @warning must call device_construct() and device_add() first
|
|
* @param device
|
|
* @return ERROR_INVALID_STATE or otherwise the value of the driver binding result (0 on success)
|
|
*/
|
|
int device_start(struct Device* device);
|
|
|
|
/**
|
|
* Detach the driver.
|
|
*
|
|
* @param device
|
|
* @return ERROR_INVALID_STATE or otherwise the value of the driver unbinding result (0 on success)
|
|
*/
|
|
int device_stop(struct Device* device);
|
|
|
|
/**
|
|
* Set or unset a parent.
|
|
* @warning must call before device_add()
|
|
* @param device non-NULL device
|
|
* @param parent nullable parent device
|
|
*/
|
|
void device_set_parent(struct Device* device, struct Device* parent);
|
|
|
|
static inline void device_set_driver(struct Device* device, struct Driver* driver) {
|
|
device->internal.driver = driver;
|
|
}
|
|
|
|
static inline struct Driver* device_get_driver(struct Device* device) {
|
|
return device->internal.driver;
|
|
}
|
|
|
|
static inline void device_set_driver_data(struct Device* device, void* driver_data) {
|
|
device->internal.driver_data = driver_data;
|
|
}
|
|
|
|
static inline void* device_get_driver_data(struct Device* device) {
|
|
return device->internal.driver_data;
|
|
}
|
|
|
|
static inline bool device_is_added(const struct Device* device) {
|
|
return device->internal.state.added;
|
|
}
|
|
|
|
static inline void device_lock(struct Device* device) {
|
|
mutex_lock(&device->internal.mutex);
|
|
}
|
|
|
|
static inline int device_try_lock(struct Device* device) {
|
|
return mutex_try_lock(&device->internal.mutex);
|
|
}
|
|
|
|
static inline void device_unlock(struct Device* device) {
|
|
mutex_unlock(&device->internal.mutex);
|
|
}
|
|
|
|
static inline const struct DeviceType* device_get_type(struct Device* device) {
|
|
return device->internal.driver ? device->internal.driver->device_type : NULL;
|
|
}
|
|
/**
|
|
* Iterate through all the known devices
|
|
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
|
* @param on_device the function to call for each filtered device. return true to continue iterating or false to stop.
|
|
*/
|
|
void for_each_device(void* callback_context, bool(*on_device)(struct Device* device, void* context));
|
|
|
|
/**
|
|
* Iterate through all the child devices of the specified device
|
|
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
|
* @param on_device the function to call for each filtered device. return true to continue iterating or false to stop.
|
|
*/
|
|
void for_each_device_child(struct Device* device, void* callback_context, bool(*on_device)(struct Device* device, void* context));
|
|
|
|
/**
|
|
* Iterate through all the known devices of a specific type
|
|
* @param type the type to filter
|
|
* @param callback_context the parameter to pass to the callback. NULL is valid.
|
|
* @param on_device the function to call for each filtered device. return true to continue iterating or false to stop.
|
|
*/
|
|
void for_each_device_of_type(const struct DeviceType* type, void* callback_context, bool(*on_device)(struct Device* device, void* context));
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|