root / host / lib / types / device_addr.cpp @ 24c62e97
History | View | Annotate | Download (4.93 KB)
| 1 | ef8d7967 | Josh Blum | //
|
|---|---|---|---|
| 2 | 3d02c074 | Josh Blum | // Copyright 2011 Ettus Research LLC
|
| 3 | ef8d7967 | Josh Blum | //
|
| 4 | // This program is free software: you can redistribute it and/or modify
|
||
| 5 | // it under the terms of the GNU General Public License as published by
|
||
| 6 | // the Free Software Foundation, either version 3 of the License, or
|
||
| 7 | // (at your option) any later version.
|
||
| 8 | //
|
||
| 9 | // This program is distributed in the hope that it will be useful,
|
||
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | // GNU General Public License for more details.
|
||
| 13 | //
|
||
| 14 | // You should have received a copy of the GNU General Public License
|
||
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 16 | //
|
||
| 17 | |||
| 18 | #include <uhd/types/device_addr.hpp> |
||
| 19 | 43638767 | Josh Blum | #include <boost/algorithm/string.hpp> |
| 20 | ef8d7967 | Josh Blum | #include <boost/tokenizer.hpp> |
| 21 | #include <boost/foreach.hpp> |
||
| 22 | #include <boost/format.hpp> |
||
| 23 | 43638767 | Josh Blum | #include <boost/regex.hpp> |
| 24 | ef8d7967 | Josh Blum | #include <stdexcept> |
| 25 | #include <sstream> |
||
| 26 | |||
| 27 | using namespace uhd; |
||
| 28 | |||
| 29 | static const std::string arg_delim = ","; |
||
| 30 | static const std::string pair_delim = "="; |
||
| 31 | |||
| 32 | static std::string trim(const std::string &in){ |
||
| 33 | return boost::algorithm::trim_copy(in);
|
||
| 34 | } |
||
| 35 | |||
| 36 | 771b5ceb | Josh Blum | #define tokenizer(inp, sep) \
|
| 37 | boost::tokenizer<boost::char_separator<char> > \
|
||
| 38 | (inp, boost::char_separator<char>(sep.c_str()))
|
||
| 39 | ef8d7967 | Josh Blum | |
| 40 | device_addr_t::device_addr_t(const std::string &args){ |
||
| 41 | 771b5ceb | Josh Blum | BOOST_FOREACH(const std::string &pair, tokenizer(args, arg_delim)){ |
| 42 | ef8d7967 | Josh Blum | if (trim(pair) == "") continue; |
| 43 | std::string key;
|
||
| 44 | 771b5ceb | Josh Blum | BOOST_FOREACH(const std::string &tok, tokenizer(pair, pair_delim)){ |
| 45 | ef8d7967 | Josh Blum | if (key.empty()) key = tok;
|
| 46 | else{
|
||
| 47 | this->set(trim(key), trim(tok));
|
||
| 48 | goto continue_next_arg;
|
||
| 49 | } |
||
| 50 | } |
||
| 51 | 4357f5d3 | Josh Blum | throw uhd::value_error("invalid args string: "+args); |
| 52 | ef8d7967 | Josh Blum | continue_next_arg: continue; |
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | std::string device_addr_t::to_pp_string(void) const{ |
||
| 57 | if (this->size() == 0) return "Empty Device Address"; |
||
| 58 | |||
| 59 | std::stringstream ss; |
||
| 60 | ss << "Device Address:" << std::endl;
|
||
| 61 | BOOST_FOREACH(std::string key, this->keys()){ |
||
| 62 | ss << boost::format(" %s: %s") % key % this->get(key) << std::endl; |
||
| 63 | } |
||
| 64 | return ss.str();
|
||
| 65 | } |
||
| 66 | |||
| 67 | std::string device_addr_t::to_string(void) const{ |
||
| 68 | std::string args_str;
|
||
| 69 | size_t count = 0;
|
||
| 70 | BOOST_FOREACH(const std::string &key, this->keys()){ |
||
| 71 | args_str += ((count++)? arg_delim : "") + key + pair_delim + this->get(key); |
||
| 72 | } |
||
| 73 | return args_str;
|
||
| 74 | } |
||
| 75 | 43638767 | Josh Blum | |
| 76 | 7f01386f | Josh Blum | #include <uhd/utils/msg.hpp> |
| 77 | 43638767 | Josh Blum | |
| 78 | device_addrs_t uhd::separate_device_addr(const device_addr_t &dev_addr){
|
||
| 79 | //------------ support old deprecated way and print warning --------
|
||
| 80 | if (dev_addr.has_key("addr") and not dev_addr["addr"].empty()){ |
||
| 81 | std::vector<std::string> addrs; boost::split(addrs, dev_addr["addr"], boost::is_any_of(" ")); |
||
| 82 | if (addrs.size() > 1){ |
||
| 83 | device_addr_t fixed_dev_addr = dev_addr; |
||
| 84 | fixed_dev_addr.pop("addr");
|
||
| 85 | for (size_t i = 0; i < addrs.size(); i++){ |
||
| 86 | fixed_dev_addr[str(boost::format("addr%d") % i)] = addrs[i];
|
||
| 87 | } |
||
| 88 | 7f01386f | Josh Blum | UHD_MSG(warning) << |
| 89 | 43638767 | Josh Blum | "addr = <space separated list of ip addresses> is deprecated.\n"
|
| 90 | "To address a multi-device, use multiple <key><index> = <val>.\n"
|
||
| 91 | "See the USRP-NXXX application notes. Two device example:\n"
|
||
| 92 | " addr0 = 192.168.10.2\n"
|
||
| 93 | " addr1 = 192.168.10.3\n"
|
||
| 94 | 7f01386f | Josh Blum | ; |
| 95 | 43638767 | Josh Blum | return separate_device_addr(fixed_dev_addr);
|
| 96 | } |
||
| 97 | } |
||
| 98 | //------------------------------------------------------------------
|
||
| 99 | b20c9fc8 | Josh Blum | device_addrs_t dev_addrs(1); //must be at least one (obviously) |
| 100 | std::vector<std::string> global_keys; //keys that apply to all (no numerical suffix) |
||
| 101 | 43638767 | Josh Blum | BOOST_FOREACH(const std::string &key, dev_addr.keys()){ |
| 102 | boost::cmatch matches; |
||
| 103 | if (not boost::regex_match(key.c_str(), matches, boost::regex("^(\\D+)(\\d*)$"))){ |
||
| 104 | throw std::runtime_error("unknown key format: " + key); |
||
| 105 | } |
||
| 106 | std::string key_part(matches[1].first, matches[1].second); |
||
| 107 | std::string num_part(matches[2].first, matches[2].second); |
||
| 108 | b20c9fc8 | Josh Blum | if (num_part.empty()){ //no number? save it for later |
| 109 | global_keys.push_back(key); |
||
| 110 | continue;
|
||
| 111 | } |
||
| 112 | const size_t num = boost::lexical_cast<size_t>(num_part);
|
||
| 113 | 43638767 | Josh Blum | dev_addrs.resize(std::max(num+1, dev_addrs.size()));
|
| 114 | dev_addrs[num][key_part] = dev_addr[key]; |
||
| 115 | } |
||
| 116 | b20c9fc8 | Josh Blum | |
| 117 | //copy the global settings across all device addresses
|
||
| 118 | BOOST_FOREACH(device_addr_t &my_dev_addr, dev_addrs){
|
||
| 119 | BOOST_FOREACH(const std::string &global_key, global_keys){ |
||
| 120 | my_dev_addr[global_key] = dev_addr[global_key]; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | 43638767 | Josh Blum | return dev_addrs;
|
| 124 | } |
||
| 125 | |||
| 126 | device_addr_t uhd::combine_device_addrs(const device_addrs_t &dev_addrs){
|
||
| 127 | device_addr_t dev_addr; |
||
| 128 | for (size_t i = 0; i < dev_addrs.size(); i++){ |
||
| 129 | BOOST_FOREACH(const std::string &key, dev_addrs[i].keys()){ |
||
| 130 | dev_addr[str(boost::format("%s%d") % key % i)] = dev_addrs[i][key];
|
||
| 131 | } |
||
| 132 | } |
||
| 133 | return dev_addr;
|
||
| 134 | } |