aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/rfnoc/block_id.cpp
blob: 55bd5e6f70d03d2e87b6d832f060035f97fd81e0 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Copyright 2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

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

#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_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 boost::regex_match(block_name, boost::regex(VALID_BLOCKNAME_REGEX));
}

bool block_id_t::is_valid_block_id(const std::string &block_name)
{
    return boost::regex_match(block_name, boost::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)
{
    boost::cmatch matches;
    if (not boost::regex_match(block_str.c_str(), matches, boost::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)
{
    boost::cmatch matches;
    if (not boost::regex_match(new_name.c_str(), matches, boost::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;
}