aboutsummaryrefslogtreecommitdiffstats
path: root/lib/usrp/mboard/test.cpp
blob: a12560e9a7300fa84c082ec4b77ab019412517b3 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Copyright 2010 Ettus Research LLC
//

#include <usrp_uhd/usrp/mboard/test.hpp>
#include <usrp_uhd/utils.hpp>
#include <usrp_uhd/props.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <stdexcept>

using namespace usrp_uhd;
using namespace usrp_uhd::usrp;
using namespace usrp_uhd::usrp::mboard;

/***********************************************************************
 * dummy interface for dboards
 **********************************************************************/
class dummy_interface : public usrp_uhd::usrp::dboard::interface{
public:
    dummy_interface(void){}
    ~dummy_interface(void){}
    void write_aux_dac(int, int){}
    int read_aux_adc(int){return 0;}
    void set_atr_reg(gpio_bank_t, uint16_t, uint16_t, uint16_t){}
    void set_gpio_ddr(gpio_bank_t, uint16_t, uint16_t){}
    void write_gpio(gpio_bank_t, uint16_t, uint16_t){}
    uint16_t read_gpio(gpio_bank_t){return 0;}
    void write_i2c (int, const std::string &){}
    std::string read_i2c (int, size_t){return "";}
    void write_spi (spi_dev_t, spi_push_t, const std::string &){}
    std::string read_spi (spi_dev_t, spi_latch_t, size_t){return "";}
    double get_rx_clock_rate(void){return 0.0;}
    double get_tx_clock_rate(void){return 0.0;}
};

/***********************************************************************
 * shell class to act as a dboard
 **********************************************************************/
class shell_dboard : public wax::obj{
public:
    enum type_t {TYPE_RX, TYPE_TX};
    shell_dboard(dboard::manager::sptr mgr, type_t type){
        _mgr = mgr;
        _type = type;
    }
    ~shell_dboard(void){}
private:
    void get(const wax::type &key_, wax::type &val){
        //extract the index if key is a named prop
        wax::type key = key_; std::string name = "";
        if (key.type() == typeid(named_prop_t)){
            boost::tie(key, name) = wax::cast<named_prop_t>(key);
        }

        //handle the get request conditioned on the key
        switch(wax::cast<dboard_prop_t>(key)){
        case DBOARD_PROP_NAME:
            val = std::string("dboard test mboard");
            return;

        case DBOARD_PROP_SUBDEV:
            switch(_type){
            case TYPE_RX:
                val = _mgr->get_rx_subdev(name);
                return;

            case TYPE_TX:
                val = _mgr->get_tx_subdev(name);
                return;
            }

        case DBOARD_PROP_SUBDEV_NAMES:
            switch(_type){
            case TYPE_RX:
                val = _mgr->get_rx_subdev_names();
                return;

            case TYPE_TX:
                val = _mgr->get_tx_subdev_names();
                return;
            }

        case DBOARD_PROP_CODEC:
            val = NULL; //TODO
            return;
        }
    }

    void set(const wax::type &, const wax::type &){
        throw std::runtime_error("Cannot set on usrp test dboard");
    }

    type_t                _type;
    dboard::manager::sptr _mgr;
};

/***********************************************************************
 * test usrp mboard class
 **********************************************************************/
test::test(const device_addr_t &device_addr){
    //create a manager for each dboard
    for (size_t i = 0; i < device_addr.virtual_args.num_dboards; i++){
        dboard::interface::sptr ifc(new dummy_interface());
        _dboard_managers[boost::lexical_cast<std::string>(i)] = dboard::manager::sptr(
            new dboard::manager(0x0001, 0x0000, ifc)
        );
    }
}

test::~test(void){
    /* NOP */
}

void test::get(const wax::type &key_, wax::type &val){
    //extract the index if key is a named prop
    wax::type key = key_; std::string name = "";
    if (key.type() == typeid(named_prop_t)){
        boost::tie(key, name) = wax::cast<named_prop_t>(key);
    }

    //handle the get request conditioned on the key
    switch(wax::cast<mboard_prop_t>(key)){
    case MBOARD_PROP_NAME:
        val = std::string("usrp test mboard");
        return;

    case MBOARD_PROP_OTHERS:
        val = prop_names_t(); //empty other props
        return;

    case MBOARD_PROP_RX_DBOARD:
        if (_dboard_managers.count(name) == 0) throw std::invalid_argument(
            str(boost::format("Unknown rx dboard name %s") % name)
        );
        val = wax::obj::sptr(
            new shell_dboard(_dboard_managers[name], shell_dboard::TYPE_RX)
        );
        return;

    case MBOARD_PROP_RX_DBOARD_NAMES:
        val = prop_names_t(get_map_keys(_dboard_managers));
        return;

    case MBOARD_PROP_TX_DBOARD:
        if (_dboard_managers.count(name) == 0) throw std::invalid_argument(
            str(boost::format("Unknown tx dboard name %s") % name)
        );
        val = wax::obj::sptr(
            new shell_dboard(_dboard_managers[name], shell_dboard::TYPE_TX)
        );
        return;

    case MBOARD_PROP_TX_DBOARD_NAMES:
        val = prop_names_t(get_map_keys(_dboard_managers));
        return;

    case MBOARD_PROP_MTU:
    case MBOARD_PROP_CLOCK_RATE:
    case MBOARD_PROP_RX_DSP:
    case MBOARD_PROP_RX_DSP_NAMES:
    case MBOARD_PROP_TX_DSP:
    case MBOARD_PROP_TX_DSP_NAMES:
    case MBOARD_PROP_PPS_SOURCE:
    case MBOARD_PROP_PPS_SOURCE_NAMES:
    case MBOARD_PROP_PPS_POLARITY:
    case MBOARD_PROP_REF_SOURCE:
    case MBOARD_PROP_REF_SOURCE_NAMES:
    case MBOARD_PROP_TIME_NOW:
    case MBOARD_PROP_TIME_NEXT_PPS:
        throw std::runtime_error("unhandled prop is usrp test mboard");
    }
}

void test::set(const wax::type &, const wax::type &){
    throw std::runtime_error("Cannot set on usrp test mboard");
}