-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathip_device.cpp
More file actions
69 lines (56 loc) · 1.77 KB
/
ip_device.cpp
File metadata and controls
69 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Linux/Unix device which represents an IP component of a FPGA.
*
* 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 <regex>
#include <stdexcept>
#include <villas/exceptions.hpp>
#include <villas/fs.hpp>
#include <villas/kernel/devices/ip_device.hpp>
#include <villas/utils.hpp>
using villas::kernel::devices::IpDevice;
IpDevice IpDevice::from(const fs::path unsafe_path) {
if (!is_path_valid(unsafe_path))
throw RuntimeError(
"Path {} failed validation as IpDevicePath [adress in hex].[name] ",
unsafe_path.string());
return IpDevice(unsafe_path);
}
std::string IpDevice::ip_name() const {
int pos = name().find('.');
return name().substr(pos + 1);
}
size_t IpDevice::addr() const {
size_t pos = name().find('.');
std::string addr_hex = name().substr(0, pos);
// Convert from hex-string to number
std::stringstream ss;
ss << std::hex << addr_hex;
size_t addr = 0;
ss >> addr;
return addr;
}
bool IpDevice::is_path_valid(const fs::path unsafe_path) {
std::string assumed_device_name = unsafe_path.filename();
// Match format of hexaddr.devicename
if (!std::regex_match(assumed_device_name,
std::regex(R"([0-9A-Fa-f]+\..*)"))) {
return false;
}
return true;
}
std::vector<villas::kernel::devices::IpDevice>
IpDevice::from_directory(fs::path devices_directory) {
std::vector<villas::kernel::devices::IpDevice> devices;
for (auto devicetree : fs::directory_iterator{devices_directory}) {
try {
auto device = villas::kernel::devices::IpDevice::from(devicetree.path());
devices.push_back(device);
} catch (std::runtime_error &e) {
}
}
return devices;
}