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:
parent
5f8dbcfa35
commit
c33550d973
2 changed files with 8 additions and 10 deletions
|
@ -21,10 +21,7 @@ public:
|
|||
static bool is_path_valid(const std::filesystem::path unsafe_path);
|
||||
|
||||
private:
|
||||
IpDevice() = delete;
|
||||
IpDevice(
|
||||
const std::filesystem::path valid_path) //! Dont allow unvalidated paths
|
||||
: PlatformDevice(valid_path) {};
|
||||
IpDevice(const std::filesystem::path valid_path) : PlatformDevice(valid_path){};
|
||||
|
||||
public:
|
||||
size_t addr() const;
|
||||
|
|
|
@ -10,16 +10,15 @@
|
|||
#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());
|
||||
throw std::runtime_error(
|
||||
"Path \"" + unsafe_path.u8string() +
|
||||
"\" failed validation as IpDevicePath \"[adress in hex].[name]\". ");
|
||||
return IpDevice(unsafe_path);
|
||||
}
|
||||
|
||||
|
@ -32,7 +31,7 @@ size_t IpDevice::addr() const {
|
|||
size_t pos = name().find('.');
|
||||
std::string addr_hex = name().substr(0, pos);
|
||||
|
||||
// Convert from hex-string to number
|
||||
// convert from hex string to number
|
||||
std::stringstream ss;
|
||||
ss << std::hex << addr_hex;
|
||||
size_t addr = 0;
|
||||
|
@ -42,7 +41,9 @@ size_t IpDevice::addr() const {
|
|||
}
|
||||
|
||||
bool IpDevice::is_path_valid(const std::filesystem::path unsafe_path) {
|
||||
std::string assumed_device_name = unsafe_path.filename();
|
||||
// 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,
|
||||
|
|
Loading…
Add table
Reference in a new issue