aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/rfnoc/node_ctrl_base.cpp
blob: fd8a13aa9352adf6b5eae999f4fce5fc002ff311 (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
//
// Copyright 2014 Ettus Research LLC
//
// SPDX-License-Identifier: GPL-3.0
//

#include <uhd/rfnoc/node_ctrl_base.hpp>
#include <uhd/utils/log.hpp>
#include <boost/range/adaptor/map.hpp>

using namespace uhd::rfnoc;

std::string node_ctrl_base::unique_id() const
{
    // Most instantiations will override this, so we don't need anything
    // more elegant here.
    return str(boost::format("%08X") % size_t(this));
}

void node_ctrl_base::clear()
{
    UHD_RFNOC_BLOCK_TRACE() << "node_ctrl_base::clear() " ;
    // Reset connections:
    _upstream_nodes.clear();
    _downstream_nodes.clear();
}

void node_ctrl_base::_register_downstream_node(
    node_ctrl_base::sptr,
    size_t
) {
    throw uhd::runtime_error("Attempting to register a downstream block on a non-source node.");
}

void node_ctrl_base::_register_upstream_node(
    node_ctrl_base::sptr,
    size_t
) {
    throw uhd::runtime_error("Attempting to register an upstream block on a non-sink node.");
}

void node_ctrl_base::set_downstream_port(
        const size_t this_port,
        const size_t remote_port
) {
    if (not _downstream_nodes.count(this_port) and remote_port != ANY_PORT) {
        throw uhd::value_error(str(
            boost::format("[%s] Cannot set remote downstream port: Port %d not connected.")
            % unique_id() % this_port
        ));
    }
    _downstream_ports[this_port] = remote_port;
}

size_t node_ctrl_base::get_downstream_port(const size_t this_port)
{
    if (not _downstream_ports.count(this_port)
        or not _downstream_nodes.count(this_port)
        or _downstream_ports[this_port] == ANY_PORT) {
        throw uhd::value_error(str(
            boost::format("[%s] Cannot retrieve remote downstream port: Port %d not connected.")
            % unique_id() % this_port
        ));
    }
    return _downstream_ports[this_port];
}

void node_ctrl_base::set_upstream_port(
        const size_t this_port,
        const size_t remote_port
) {
    if (not _upstream_nodes.count(this_port) and remote_port != ANY_PORT) {
        throw uhd::value_error(str(
            boost::format("[%s] Cannot set remote upstream port: Port %d not connected.")
            % unique_id() % this_port
        ));
    }
    _upstream_ports[this_port] = remote_port;
}

size_t node_ctrl_base::get_upstream_port(const size_t this_port)
{
    if (not _upstream_ports.count(this_port)
        or not _upstream_nodes.count(this_port)
        or _upstream_ports[this_port] == ANY_PORT) {
        throw uhd::value_error(str(
            boost::format("[%s] Cannot retrieve remote upstream port: Port %d not connected.")
            % unique_id() % this_port
        ));
    }
    return _upstream_ports[this_port];
}

void node_ctrl_base::disconnect()
{
    // Notify neighbours:
    for (node_map_t::iterator i = _downstream_nodes.begin(); i != _downstream_nodes.end(); ++i) {
        sptr downstream_node = i->second.lock();
        if (not downstream_node) {
            // Actually this is not OK
            continue;
        }
        downstream_node->disconnect_input_port(_downstream_ports[i->first]);
    }
    for (node_map_t::iterator i = _upstream_nodes.begin(); i != _upstream_nodes.end(); ++i) {
        sptr upstream_node = i->second.lock();
        if (not upstream_node) {
            // Actually this is not OK
            continue;
        }
        upstream_node->disconnect_output_port(_upstream_ports[i->first]);
    }
    // Clear own maps:
    _downstream_nodes.clear();
    _downstream_ports.clear();
    _upstream_nodes.clear();
    _upstream_ports.clear();
}

void node_ctrl_base::disconnect_output_port(const size_t output_port)
{
    if (_downstream_nodes.count(output_port) == 0 or
        _downstream_ports.count(output_port) == 0) {
        throw uhd::assertion_error(str(boost::format("[%s] Attempting to disconnect output port %u, which is not registered as connected!") % unique_id() % output_port));
    }
    _downstream_nodes.erase(output_port);
    _downstream_ports.erase(output_port);
}

void node_ctrl_base::disconnect_input_port(const size_t input_port)
{
    if (_upstream_nodes.count(input_port) == 0 or
        _upstream_ports.count(input_port) == 0) {
        throw uhd::assertion_error(str(boost::format("[%s] Attempting to disconnect input port %u, which is not registered as connected!") % unique_id() % input_port));
    }
    _upstream_nodes.erase(input_port);
    _upstream_ports.erase(input_port);
}