1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

add IpDevice

Signed-off-by: Pascal Bauer <pascal.bauer@rwth-aachen.de>
This commit is contained in:
Pascal Bauer 2024-08-24 12:37:06 +02:00
parent 9d6b81b15a
commit d65910a14a
2 changed files with 89 additions and 0 deletions

View file

@ -0,0 +1,34 @@
/* IpDevice
*
* 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 <villas/kernel/devices/platform_device.hpp>
namespace villas {
namespace kernel {
namespace devices {
class IpDevice : public PlatformDevice {
public:
public:
static IpDevice from(const std::filesystem::path unsafe_path);
static bool is_path_valid(const std::filesystem::path unsafe_path);
private:
IpDevice(const std::filesystem::path valid_path) : PlatformDevice(valid_path){};
public:
size_t addr() const;
std::string ip_name() const;
};
} // namespace devices
} // namespace kernel
} // namespace villas

View file

@ -0,0 +1,55 @@
/* IpDevice
*
* 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/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 std::runtime_error(
"Path \"" + unsafe_path.u8string() +
"\" failed validation as IpDevicePath \"[adress in hex].[name]\". ");
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) {
// Split the string at last slash
int pos = unsafe_path.u8string().rfind('/');
std::string assumed_device_name = unsafe_path.u8string().substr(pos + 1);
// Match format of hexaddr.devicename
if (!std::regex_match(assumed_device_name,
std::regex(R"([0-9A-Fa-f]+\..*)"))) {
return false;
}
return true;
}