From a55b5a097da01606f23209713bf1ce754be5b7d3 Mon Sep 17 00:00:00 2001
From: Julian Arnold <julian.arnold@ettus.com>
Date: Fri, 30 Jan 2015 15:11:15 -0800
Subject: uhd: Introduced filter API.

This is a unified API to access filters on USRP devices. Filters
can be accessed through the property tree, or multi_usrp.
---
 host/include/uhd/types/CMakeLists.txt |   1 +
 host/include/uhd/types/filters.hpp    | 286 ++++++++++++++++++++++++++++++++++
 host/include/uhd/usrp/multi_usrp.hpp  |  33 ++++
 host/lib/types/CMakeLists.txt         |   1 +
 host/lib/types/filters.cpp            |  74 +++++++++
 host/lib/usrp/multi_usrp.cpp          |  83 ++++++++++
 6 files changed, 478 insertions(+)
 create mode 100644 host/include/uhd/types/filters.hpp
 create mode 100644 host/lib/types/filters.cpp

(limited to 'host')

diff --git a/host/include/uhd/types/CMakeLists.txt b/host/include/uhd/types/CMakeLists.txt
index f5a92a805..b82c2b7f2 100644
--- a/host/include/uhd/types/CMakeLists.txt
+++ b/host/include/uhd/types/CMakeLists.txt
@@ -37,6 +37,7 @@ UHD_INSTALL(FILES
     tune_request.hpp
     tune_result.hpp
     wb_iface.hpp
+    filters.hpp
     DESTINATION ${INCLUDE_DIR}/uhd/types
     COMPONENT headers
 )
diff --git a/host/include/uhd/types/filters.hpp b/host/include/uhd/types/filters.hpp
new file mode 100644
index 000000000..0cb23b294
--- /dev/null
+++ b/host/include/uhd/types/filters.hpp
@@ -0,0 +1,286 @@
+//
+// Copyright 2015 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/>.
+//
+
+#ifndef INCLUDED_UHD_TYPES_FILTERS_HPP
+#define INCLUDED_UHD_TYPES_FILTERS_HPP
+
+#include <uhd/config.hpp>
+#include <uhd/utils/log.hpp>
+#include <uhd/utils/msg.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/scoped_array.hpp>
+#include <string>
+#include <vector>
+#include <iostream>
+#include <ostream>
+#include <sstream>
+
+namespace uhd{
+
+    class UHD_API filter_info_base
+    {
+    public:
+        typedef boost::shared_ptr<filter_info_base> sptr;
+        enum filter_type
+        {
+            ANALOG_LOW_PASS,
+            ANALOG_BAND_PASS,
+            DIGITAL_I16,
+            DIGITAL_FIR_I16
+        };
+
+        filter_info_base(
+            filter_type type,
+            bool bypass,
+            size_t position_index
+        ):
+            _type(type), _bypass(bypass),
+            _position_index(position_index)
+        {
+            //NOP
+        }
+
+        inline virtual bool is_bypassed()
+        {
+            return _bypass;
+        }
+
+        inline filter_type get_type()
+        {
+            return _type;
+        }
+
+        virtual ~filter_info_base()
+        {
+            //NOP
+        }
+
+        virtual std::string to_pp_string();
+
+    protected:
+        filter_type _type;
+        bool _bypass;
+        size_t _position_index;
+
+    };
+
+    UHD_API std::ostream& operator<<(std::ostream& os, filter_info_base& f);
+
+    class UHD_API analog_filter_base : public filter_info_base
+    {
+        std::string _analog_type;
+        public:
+            typedef boost::shared_ptr<analog_filter_base> sptr;
+            analog_filter_base(
+                filter_type type,
+                bool bypass,
+                size_t position_index,
+                const std::string& analog_type
+            ):
+                filter_info_base(type, bypass, position_index),
+                _analog_type(analog_type)
+            {
+                //NOP
+            }
+
+            inline const std::string& get_analog_type()
+            {
+                return _analog_type;
+            }
+
+            virtual std::string to_pp_string();
+    };
+
+    class UHD_API analog_filter_lp : public analog_filter_base
+    {
+        double _cutoff;
+        double _rolloff;
+
+    public:
+        typedef boost::shared_ptr<analog_filter_lp> sptr;
+        analog_filter_lp(
+            filter_type type,
+            bool bypass,
+            size_t position_index,
+            const std::string& analog_type,
+            double cutoff,
+            double rolloff
+        ):
+            analog_filter_base(type, bypass, position_index, analog_type),
+            _cutoff(cutoff),
+            _rolloff(rolloff)
+        {
+            //NOP
+        }
+
+        inline double get_cutoff()
+        {
+            return _cutoff;
+        }
+
+        inline double get_rolloff()
+        {
+            return _cutoff;
+        }
+
+        inline void set_cutoff(const double cutoff)
+        {
+            _cutoff = cutoff;
+        }
+
+        virtual std::string to_pp_string();
+    };
+
+    template<typename tap_t>
+    class UHD_API digital_filter_base : public filter_info_base
+    {
+    protected:
+        double _rate;
+        boost::uint32_t _interpolation;
+        boost::uint32_t _decimation;
+        tap_t _tap_full_scale;
+        boost::uint32_t _max_num_taps;
+        std::vector<tap_t> _taps;
+
+    public:
+        typedef boost::shared_ptr<digital_filter_base> sptr;
+        digital_filter_base(
+            filter_type type,
+            bool bypass,
+            size_t position_index,
+            double rate,
+            size_t interpolation,
+            size_t decimation,
+            double tap_full_scale,
+            size_t max_num_taps,
+            const std::vector<tap_t>& taps
+        ):
+            filter_info_base(type, bypass, position_index),
+            _rate(rate),
+            _interpolation(interpolation),
+            _decimation(decimation),
+            _tap_full_scale(tap_full_scale),
+            _max_num_taps(max_num_taps),
+            _taps(taps)
+        {
+            //NOP
+        }
+
+        inline double get_output_rate()
+        {
+            return (_bypass ? _rate : (_rate / _decimation * _interpolation));
+        }
+
+        inline double get_input_rate()
+        {
+            return _rate;
+        }
+
+        inline double get_interpolation()
+        {
+            return _interpolation;
+        }
+
+        inline double get_decimation()
+        {
+            return _decimation;
+        }
+
+        inline double get_tap_full_scale()
+        {
+            return _tap_full_scale;
+        }
+
+        inline std::vector<tap_t>& get_taps()
+        {
+            return _taps;
+        }
+
+        virtual std::string to_pp_string()
+        {
+            std::ostringstream os;
+            os<<filter_info_base::to_pp_string()<<
+            "\t[digital_filter_base]"<<std::endl<<
+            "\tinput rate: "<<_rate<<std::endl<<
+            "\tinterpolation: "<<_interpolation<<std::endl<<
+            "\tdecimation: "<<_decimation<<std::endl<<
+            "\tfull-scale: "<<_tap_full_scale<<std::endl<<
+            "\tmax num taps: "<<_max_num_taps<<std::endl<<
+            "\ttaps: "<<std::endl;
+
+            os<<"\t\t";
+            for(int i = 0; i < _taps.size(); i++)
+            {
+                os<<"(tap "<<i<<": "<<_taps[i]<<")";
+                if( ((i%10) == 0) && (i != 0))
+                {
+                    os<<std::endl<<"\t\t";
+                }
+            }
+            os<<std::endl;
+            return std::string(os.str());
+        }
+
+    };
+
+    template<typename tap_t>
+    class UHD_API digital_filter_fir : public digital_filter_base<tap_t>
+    {
+    public:
+        typedef boost::shared_ptr<digital_filter_fir<tap_t> > sptr;
+
+        digital_filter_fir(
+            filter_info_base::filter_type type,
+            bool bypass, size_t position_index,
+            double rate,
+            size_t interpolation,
+            size_t decimation,
+            size_t tap_bit_width,
+            size_t max_num_taps,
+            const std::vector<tap_t>& taps
+        ):
+            digital_filter_base<tap_t>(type, bypass, position_index, rate, interpolation, decimation, tap_bit_width, max_num_taps, taps)
+        {
+            //NOP
+        }
+
+        void set_taps(const std::vector<tap_t>& taps)
+        {
+            std::size_t num_taps = taps.size();
+            if(num_taps < this->_max_num_taps){
+                UHD_MSG(warning) << "digital_filter_fir::set_taps not enough coefficients. Appending zeros";
+                std::vector<tap_t> coeffs;
+                for (size_t i = 0; i < this->_max_num_taps; i++)
+                {
+                    if(i < num_taps)
+                    {
+                        coeffs.push_back(taps[i]);
+                    } else {
+                        coeffs.push_back(0);
+                    }
+                }
+                this->_taps = coeffs;
+            } else {
+                this->_taps = taps;
+            }
+        }
+    };
+
+} //namespace uhd
+
+#endif /* INCLUDED_UHD_TYPES_FILTERS_HPP */
diff --git a/host/include/uhd/usrp/multi_usrp.hpp b/host/include/uhd/usrp/multi_usrp.hpp
index e8fcdb4b6..1c408d56e 100644
--- a/host/include/uhd/usrp/multi_usrp.hpp
+++ b/host/include/uhd/usrp/multi_usrp.hpp
@@ -36,6 +36,7 @@
 #include <uhd/types/tune_request.hpp>
 #include <uhd/types/tune_result.hpp>
 #include <uhd/types/sensors.hpp>
+#include <uhd/types/filters.hpp>
 #include <uhd/usrp/subdev_spec.hpp>
 #include <uhd/usrp/dboard_iface.hpp>
 #include <boost/shared_ptr.hpp>
@@ -973,6 +974,38 @@ public:
      */
     virtual boost::uint32_t get_gpio_attr(const std::string &bank, const std::string &attr, const size_t mboard = 0) = 0;
 
+    /*******************************************************************
+     * Filter API methods
+     ******************************************************************/
+
+    /*!
+     * Enumerate the available filters in the signal path.
+     * \param search_mask
+     * \parblock
+     * Select only certain filter names by specifying this search mask.
+     *
+     * E.g. if search mask is set to "rx_frontends/A" only filter names including that string will be returned.
+     * \endparblock
+     * \return a vector of strings representing the selected filter names.
+     */
+    virtual std::vector<std::string> get_filter_names(const std::string &search_mask = "") = 0;
+
+    /*!
+     * Return the filter object for the given name.
+     * \param path the name of the filter as returned from get_filter_names().
+     * \return a filter_info_base::sptr.
+     */
+    virtual filter_info_base::sptr get_filter(const std::string &path) = 0;
+
+    /*!
+     * Write back a filter obtained by get_filter() to the signal path.
+     * This filter can be a modified version of the originally returned one.
+     * The information about Rx or Tx is contained in the path parameter.
+     * \param path the name of the filter as returned from get_filter_names().
+     * \param filter the filter_info_base::sptr of the filter object to be written
+     */
+    virtual void set_filter(const std::string &path, filter_info_base::sptr filter) = 0;
+
 };
 
 }}
diff --git a/host/lib/types/CMakeLists.txt b/host/lib/types/CMakeLists.txt
index 853da3fe2..821754386 100644
--- a/host/lib/types/CMakeLists.txt
+++ b/host/lib/types/CMakeLists.txt
@@ -91,4 +91,5 @@ LIBUHD_APPEND_SOURCES(
     ${CMAKE_CURRENT_SOURCE_DIR}/tune.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/types.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/wb_iface.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/filters.cpp
 )
diff --git a/host/lib/types/filters.cpp b/host/lib/types/filters.cpp
new file mode 100644
index 000000000..4ee06491f
--- /dev/null
+++ b/host/lib/types/filters.cpp
@@ -0,0 +1,74 @@
+//
+// Copyright 2015 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 <uhd/types/filters.hpp>
+
+using namespace uhd;
+
+std::ostream& uhd::operator<<(std::ostream& os, filter_info_base& f)
+{
+    return os << f.to_pp_string();
+}
+
+std::string filter_info_base::to_pp_string()
+{
+    std::ostringstream os;
+    os << "[filter_info_base]" << std::endl;
+    switch(_type){
+        case ANALOG_LOW_PASS:
+            os << "type: " << "Analog Low-pass" << std::endl;
+            break;
+        case ANALOG_BAND_PASS:
+            os << "type: " << "Analog Band-pass" << std::endl;
+            break;
+        case DIGITAL_I16:
+            os << "type: " << "Digital (i16)" << std::endl;
+            break;
+        case DIGITAL_FIR_I16:
+            os << "type: " << "Digital FIR (i16)" << std::endl;
+            break;
+        default:
+            os << "type: " << "Unknown type!" << std::endl;
+            break;
+        }
+
+    os << "bypass enable: " << _bypass << std::endl
+        <<"position index: " << _position_index << std::endl;
+
+    std::string str =  os.str();
+    return str;
+}
+
+std::string analog_filter_base::to_pp_string()
+{
+    std::ostringstream os;
+    os << filter_info_base::to_pp_string() <<
+        "\t[analog_filter_base]" << std::endl <<
+        "\tdesc: " << _analog_type << std::endl;
+    return std::string(os.str());
+
+}
+
+std::string analog_filter_lp::to_pp_string()
+{
+    std::ostringstream os;
+    os << analog_filter_base::to_pp_string() <<
+        "\t\t[analog_filter_lp]" << std::endl <<
+        "\t\tcutoff: " << _cutoff << std::endl <<
+        "\t\trolloff: " << _rolloff << std::endl;
+    return std::string(os.str());
+}
diff --git a/host/lib/usrp/multi_usrp.cpp b/host/lib/usrp/multi_usrp.cpp
index bc6e121d0..570c67875 100644
--- a/host/lib/usrp/multi_usrp.cpp
+++ b/host/lib/usrp/multi_usrp.cpp
@@ -30,6 +30,8 @@
 #include <boost/thread.hpp>
 #include <boost/foreach.hpp>
 #include <boost/format.hpp>
+#include <boost/algorithm/string.hpp>
+#include <algorithm>
 #include <cmath>
 
 using namespace uhd;
@@ -1007,6 +1009,87 @@ public:
         }
     }
 
+    std::vector<std::string> get_filter_names(const std::string &search_mask)
+    {
+        std::vector<std::string> ret;
+
+        for (size_t chan = 0; chan < get_rx_num_channels(); chan++){
+
+            if (_tree->exists(rx_rf_fe_root(chan) / "filters")) {
+                std::vector<std::string> names = _tree->list(rx_rf_fe_root(chan) / "filters");
+                for(size_t i = 0; i < names.size(); i++)
+                {
+                    std::string name = rx_rf_fe_root(chan) / "filters" / names[i];
+                    if((search_mask.empty()) or boost::contains(name, search_mask)) {
+                        ret.push_back(name);
+                    }
+                }
+            }
+            if (_tree->exists(rx_dsp_root(chan) / "filters")) {
+                std::vector<std::string> names = _tree->list(rx_dsp_root(chan) / "filters");
+                for(size_t i = 0; i < names.size(); i++)
+                {
+                    std::string name = rx_dsp_root(chan) / "filters" / names[i];
+                    if((search_mask.empty()) or (boost::contains(name, search_mask))) {
+                        ret.push_back(name);
+                    }
+                }
+            }
+
+        }
+
+        for (size_t chan = 0; chan < get_tx_num_channels(); chan++){
+
+            if (_tree->exists(tx_rf_fe_root(chan) / "filters")) {
+                std::vector<std::string> names = _tree->list(tx_rf_fe_root(chan) / "filters");
+                for(size_t i = 0; i < names.size(); i++)
+                {
+                    std::string name = tx_rf_fe_root(chan) / "filters" / names[i];
+                    if((search_mask.empty()) or (boost::contains(name, search_mask))) {
+                        ret.push_back(name);
+                    }
+                }
+            }
+            if (_tree->exists(rx_dsp_root(chan) / "filters")) {
+                std::vector<std::string> names = _tree->list(tx_dsp_root(chan) / "filters");
+                for(size_t i = 0; i < names.size(); i++)
+                {
+                    std::string name = tx_dsp_root(chan) / "filters" / names[i];
+                    if((search_mask.empty()) or (boost::contains(name, search_mask))) {
+                        ret.push_back(name);
+                    }
+                }
+            }
+
+        }
+
+        return ret;
+    }
+
+    filter_info_base::sptr get_filter(const std::string &path)
+    {
+        std::vector<std::string> possible_names = get_filter_names("");
+        std::vector<std::string>::iterator it;
+        it = find(possible_names.begin(), possible_names.end(), path);
+        if (it == possible_names.end()) {
+            throw uhd::runtime_error("Attempting to get non-existing filter: "+path);
+        }
+
+        return _tree->access<filter_info_base::sptr>(path / "value").get();
+    }
+
+    void set_filter(const std::string &path, filter_info_base::sptr filter)
+    {
+        std::vector<std::string> possible_names = get_filter_names("");
+        std::vector<std::string>::iterator it;
+        it = find(possible_names.begin(), possible_names.end(), path);
+        if (it == possible_names.end()) {
+            throw uhd::runtime_error("Attempting to set non-existing filter: "+path);
+        }
+
+        _tree->access<filter_info_base::sptr>(path / "value").set(filter);
+    }
+
     /*******************************************************************
      * TX methods
      ******************************************************************/
-- 
cgit v1.2.3