Skip to content

Commit 71163be

Browse files
refactor Interface
1 parent bc99a3b commit 71163be

File tree

5 files changed

+82
-54
lines changed

5 files changed

+82
-54
lines changed

common/include/villas/kernel/devices/device.hpp

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,12 @@
1111
#include <filesystem>
1212
#include <optional>
1313
#include <villas/kernel/devices/driver.hpp>
14-
#include <villas/kernel/devices/utils.hpp>
1514

1615
class Device {
17-
private:
18-
static constexpr char PROBE_DEFAULT[] = "/sys/bus/platform/drivers_probe";
19-
static constexpr char OVERRIDE_DEFAULT[] = "driver_override";
20-
21-
public:
22-
const std::filesystem::path path;
23-
const std::filesystem::path probe_path;
24-
const std::filesystem::path override_path;
25-
2616
public:
27-
Device(const std::filesystem::path path)
28-
: Device(path, std::filesystem::path(PROBE_DEFAULT),
29-
path / std::filesystem::path(OVERRIDE_DEFAULT)){};
30-
Device(const std::filesystem::path path,
31-
const std::filesystem::path probe_path,
32-
const std::filesystem::path override_path)
33-
: path(path), probe_path(probe_path), override_path(override_path){};
34-
35-
std::string name() const {
36-
size_t pos = path.u8string().rfind('/');
37-
return path.u8string().substr(pos + 1);
38-
}
39-
40-
std::optional<Driver> driver() const {
41-
std::filesystem::path driver_symlink =
42-
this->path / std::filesystem::path("driver");
43-
44-
if (!std::filesystem::is_symlink(driver_symlink))
45-
return std::nullopt;
46-
47-
std::filesystem::path driver_path =
48-
std::filesystem::canonical(driver_symlink);
49-
return Driver(driver_path);
50-
};
51-
52-
void probe() const { write_to_file(this->name(), this->probe_path); };
17+
virtual std::string name() const = 0;
18+
virtual std::optional<Driver> driver() const = 0;
19+
virtual int iommu_group() const = 0;
20+
virtual void probe() const = 0;
21+
virtual std::filesystem::path get_override_path() const = 0;
5322
};

common/include/villas/kernel/devices/ip_device.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
#pragma once
1010

1111
#include <filesystem>
12-
#include <villas/kernel/devices/device.hpp>
12+
#include <villas/kernel/devices/platform_device.hpp>
1313

14-
class IpDevice : public Device {
14+
class IpDevice : public PlatformDevice {
15+
public:
1516
public:
1617
static IpDevice from(const std::filesystem::path unsafe_path);
1718
static bool is_path_valid(const std::filesystem::path unsafe_path);
1819

1920
private:
20-
IpDevice(const std::filesystem::path valid_path) : Device(valid_path){};
21+
IpDevice(const std::filesystem::path valid_path) : PlatformDevice(path){};
2122

2223
public:
2324
std::string ip_name() const;
2425
size_t addr() const;
25-
int iommu_group() const;
2626
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* Platform Device
2+
*
3+
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
4+
*
5+
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#pragma once
10+
11+
#include <filesystem>
12+
#include <villas/kernel/devices/device.hpp>
13+
#include <villas/kernel/devices/utils.hpp>
14+
15+
class PlatformDevice : public Device {
16+
private:
17+
static constexpr char PROBE_DEFAULT[] = "/sys/bus/platform/drivers_probe";
18+
static constexpr char OVERRIDE_DEFAULT[] = "driver_override";
19+
20+
public:
21+
const std::filesystem::path path;
22+
const std::filesystem::path probe_path;
23+
const std::filesystem::path override_path;
24+
25+
public:
26+
PlatformDevice(const std::filesystem::path path)
27+
: PlatformDevice(path, std::filesystem::path(PROBE_DEFAULT),
28+
path / std::filesystem::path(OVERRIDE_DEFAULT)){};
29+
PlatformDevice(const std::filesystem::path path,
30+
const std::filesystem::path probe_path,
31+
const std::filesystem::path override_path)
32+
: path(path), probe_path(probe_path), override_path(override_path){};
33+
34+
std::string name() const override {
35+
size_t pos = path.u8string().rfind('/');
36+
return path.u8string().substr(pos + 1);
37+
}
38+
39+
std::optional<Driver> driver() const override {
40+
std::filesystem::path driver_symlink =
41+
this->path / std::filesystem::path("driver");
42+
43+
if (!std::filesystem::is_symlink(driver_symlink))
44+
return std::nullopt;
45+
46+
std::filesystem::path driver_path =
47+
std::filesystem::canonical(driver_symlink);
48+
return Driver(driver_path);
49+
};
50+
51+
int iommu_group() const override {
52+
std::filesystem::path symlink =
53+
std::filesystem::path(this->path.u8string() + "/iommu_group");
54+
55+
std::filesystem::path link;
56+
link = std::filesystem::read_symlink(symlink);
57+
58+
std::string delimiter = "iommu_groups/";
59+
int pos = link.u8string().find(delimiter);
60+
int iommu_group =
61+
std::stoi(link.u8string().substr(pos + delimiter.length()));
62+
return iommu_group;
63+
}
64+
65+
void probe() const override {
66+
write_to_file(this->name(), this->probe_path);
67+
};
68+
69+
virtual std::filesystem::path get_override_path() const override {
70+
return this->override_path;
71+
};
72+
};

common/lib/kernel/devices/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void Driver::unbind(const Device &device) const {
2323
};
2424

2525
void Driver::override(const Device &device) const {
26-
write_to_file(this->name(), device.override_path);
26+
write_to_file(this->name(), device.get_override_path());
2727
};
2828

2929
void Driver::attach(const Device &device) const {

common/lib/kernel/devices/ip_device.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,3 @@ size_t IpDevice::addr() const {
5151

5252
return addr;
5353
}
54-
55-
int IpDevice::iommu_group() const {
56-
std::filesystem::path symlink =
57-
std::filesystem::path(this->path.u8string() + "/iommu_group");
58-
59-
std::filesystem::path link;
60-
link = std::filesystem::read_symlink(symlink);
61-
62-
std::string delimiter = "iommu_groups/";
63-
int pos = link.u8string().find(delimiter);
64-
int iommu_group = std::stoi(link.u8string().substr(pos + delimiter.length()));
65-
return iommu_group;
66-
}

0 commit comments

Comments
 (0)