-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathip_device.cpp
More file actions
54 lines (43 loc) · 1.36 KB
/
ip_device.cpp
File metadata and controls
54 lines (43 loc) · 1.36 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
/* 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 <filesystem>
#include <regex>
#include <stdexcept>
#include <villas/exceptions.hpp>
#include <villas/kernel/devices/ip_device.hpp>
using villas::kernel::devices::IpDevice;
IpDevice IpDevice::from(const std::filesystem::path unsafe_path) {
if (!is_path_valid(unsafe_path))
throw RuntimeError(
"Path {} failed validation as IpDevicePath [adress in hex].[name] ",
unsafe_path.u8string());
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 std::filesystem::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;
}