mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
add generic driver
Signed-off-by: Pascal Bauer <pascal.bauer@rwth-aachen.de>
This commit is contained in:
parent
e3125dbdff
commit
8d53e0c139
2 changed files with 92 additions and 0 deletions
52
common/include/villas/kernel/devices/generic_driver.hpp
Normal file
52
common/include/villas/kernel/devices/generic_driver.hpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/* GenericDriver - Works for standard Linux drivers
|
||||
*
|
||||
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <villas/kernel/devices/driver.hpp>
|
||||
|
||||
namespace villas {
|
||||
namespace kernel {
|
||||
namespace devices {
|
||||
|
||||
class GenericDriver : public Driver {
|
||||
private:
|
||||
static constexpr char BIND_DEFAULT[] = "bind";
|
||||
static constexpr char UNBIND_DEFAULT[] = "unbind";
|
||||
|
||||
public:
|
||||
const std::filesystem::path path;
|
||||
|
||||
private:
|
||||
const std::filesystem::path bind_path;
|
||||
const std::filesystem::path unbind_path;
|
||||
|
||||
public:
|
||||
GenericDriver(const std::filesystem::path path)
|
||||
: GenericDriver(path, path / std::filesystem::path(BIND_DEFAULT),
|
||||
path / std::filesystem::path(UNBIND_DEFAULT)){};
|
||||
|
||||
GenericDriver(const std::filesystem::path path,
|
||||
const std::filesystem::path bind_path,
|
||||
const std::filesystem::path unbind_path)
|
||||
: path(path), bind_path(bind_path), unbind_path(unbind_path){};
|
||||
|
||||
public:
|
||||
virtual void attach(const Device &device) const = 0;
|
||||
virtual void bind(const Device &device) const = 0;
|
||||
virtual std::string name() const = 0;
|
||||
virtual void override(const Device &device) const = 0;
|
||||
virtual void unbind(const Device &device) const = 0;
|
||||
};
|
||||
|
||||
} // namespace devices
|
||||
} // namespace kernel
|
||||
} // namespace villas
|
40
common/lib/kernel/devices/generic_driver.cpp
Normal file
40
common/lib/kernel/devices/generic_driver.cpp
Normal file
|
@ -0,0 +1,40 @@
|
|||
/* GenericDriver
|
||||
*
|
||||
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <villas/kernel/devices/generic_driver.hpp>
|
||||
|
||||
#include <villas/kernel/devices/device.hpp>
|
||||
#include <villas/kernel/devices/utils.hpp>
|
||||
|
||||
using villas::kernel::devices::Device, villas::kernel::devices::GenericDriver;
|
||||
using villas::kernel::devices::utils::write_to_file;
|
||||
|
||||
void GenericDriver::attach(const Device &device) const {
|
||||
if (device.driver().has_value()) {
|
||||
device.driver().value()->unbind(device);
|
||||
}
|
||||
this->override(device);
|
||||
device.probe();
|
||||
}
|
||||
|
||||
void GenericDriver::bind(const Device &device) const {
|
||||
write_to_file(device.name(), this->bind_path);
|
||||
}
|
||||
|
||||
std::string GenericDriver::name() const {
|
||||
size_t pos = path.u8string().rfind('/');
|
||||
return path.u8string().substr(pos + 1);
|
||||
}
|
||||
|
||||
void GenericDriver::override(const Device &device) const {
|
||||
write_to_file(this->name(), device.override_path());
|
||||
}
|
||||
|
||||
void GenericDriver::unbind(const Device &device) const {
|
||||
write_to_file(device.name(), this->unbind_path);
|
||||
}
|
Loading…
Add table
Reference in a new issue