aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/rfnoc/block_id.cpp
blob: db7ddb032041e93c37e993de2198c103570f8fac (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Copyright 2014 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/exception.hpp>
#include <uhd/property_tree.hpp>
#include <uhd/rfnoc/block_id.hpp>
#include <uhd/rfnoc/constants.hpp>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <regex>
#include <iostream>

using namespace uhd::rfnoc;

block_id_t::block_id_t() : _device_no(0), _block_name(""), _block_ctr(0) {}

block_id_t::block_id_t(const std::string& block_str)
    : _device_no(0), _block_name(""), _block_ctr(0)
{
    if (not set(block_str)) {
        throw uhd::value_error(
            "block_id_t: Invalid block ID string: `" + block_str + "'");
    }
}

block_id_t::block_id_t(
    const size_t device_no, const std::string& block_name, const size_t block_ctr)
    : _device_no(device_no), _block_name(block_name), _block_ctr(block_ctr)
{
    if (not is_valid_blockname(block_name)) {
        throw uhd::value_error("block_id_t: Invalid block name.");
    }
}

bool block_id_t::is_valid_blockname(const std::string& block_name)
{
    return std::regex_match(block_name, std::regex(VALID_BLOCKNAME_REGEX));
}

bool block_id_t::is_valid_block_id(const std::string& block_name)
{
    return std::regex_match(block_name, std::regex(VALID_BLOCKID_REGEX));
}

std::string block_id_t::to_string() const
{
    return str(boost::format("%d/%s") % get_device_no() % get_local());
}

std::string block_id_t::get_local() const
{
    return str(boost::format("%s#%d") % get_block_name() % get_block_count());
}

uhd::fs_path block_id_t::get_tree_root() const
{
    return str(boost::format("/mboards/%d/xbar/%s") % get_device_no() % get_local());
}

bool block_id_t::match(const std::string& block_str)
{
    std::cmatch matches;
    if (not std::regex_match(
            block_str.c_str(), matches, std::regex(VALID_BLOCKID_REGEX))) {
        return false;
    }
    try {
        return (matches[1] == "" or boost::lexical_cast<size_t>(matches[1]) == _device_no)
               and (matches[2] == "" or matches[2] == _block_name)
               and (matches[3] == ""
                       or boost::lexical_cast<size_t>(matches[3]) == _block_ctr)
               and not(matches[1] == "" and matches[2] == "" and matches[3] == "");
    } catch (const std::bad_cast& e) {
        return false;
    }
    return false;
}

bool block_id_t::set(const std::string& new_name)
{
    std::cmatch matches;
    if (not std::regex_match(
            new_name.c_str(), matches, std::regex(VALID_BLOCKID_REGEX))) {
        return false;
    }
    if (not(matches[1] == "")) {
        _device_no = boost::lexical_cast<size_t>(matches[1]);
    }
    if (not(matches[2] == "")) {
        _block_name = matches[2];
    }
    if (not(matches[3] == "")) {
        _block_ctr = boost::lexical_cast<size_t>(matches[3]);
    }
    return true;
}

bool block_id_t::set(
    const size_t device_no, const std::string& block_name, const size_t block_ctr)
{
    if (not set_block_name(block_name)) {
        return false;
    }
    set_device_no(device_no);
    set_block_count(block_ctr);
    return true;
}

bool block_id_t::set_block_name(const std::string& block_name)
{
    if (not is_valid_blockname(block_name)) {
        return false;
    }
    _block_name = block_name;
    return true;
}