|
| 1 | +/* IpDevice |
| 2 | + * |
| 3 | + * Author: Pascal Bauer <[email protected]> |
| 4 | + * |
| 5 | + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <[email protected]> |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +#include <filesystem> |
| 10 | +#include <regex> |
| 11 | +#include <stdexcept> |
| 12 | + |
| 13 | +#include <villas/fpga/devices/ip_device.hpp> |
| 14 | + |
| 15 | +IpDevice IpDevice::from(const std::filesystem::path unsafe_path) { |
| 16 | + if (!is_path_valid(unsafe_path)) |
| 17 | + throw std::runtime_error( |
| 18 | + "Path \"" + unsafe_path.u8string() + |
| 19 | + "\" failed validation as IpDevicePath \"[adress in hex].[name]\". "); |
| 20 | + return IpDevice(unsafe_path); |
| 21 | +} |
| 22 | + |
| 23 | +bool IpDevice::is_path_valid(const std::filesystem::path unsafe_path) { |
| 24 | + // Split the string at last slash |
| 25 | + int pos = unsafe_path.u8string().rfind('/'); |
| 26 | + std::string assumed_device_name = unsafe_path.u8string().substr(pos + 1); |
| 27 | + |
| 28 | + // Match format of hexaddr.devicename |
| 29 | + if (!std::regex_match(assumed_device_name, |
| 30 | + std::regex(R"([0-9A-Fa-f]+\..*)"))) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +std::string IpDevice::ip_name() const { |
| 38 | + int pos = name().find('.'); |
| 39 | + return name().substr(pos + 1); |
| 40 | +} |
| 41 | + |
| 42 | +size_t IpDevice::addr() const { |
| 43 | + size_t pos = name().find('.'); |
| 44 | + std::string addr_hex = name().substr(0, pos); |
| 45 | + |
| 46 | + // convert from hex string to number |
| 47 | + std::stringstream ss; |
| 48 | + ss << std::hex << addr_hex; |
| 49 | + size_t addr = 0; |
| 50 | + ss >> addr; |
| 51 | + |
| 52 | + return addr; |
| 53 | +} |
| 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