diff options
Diffstat (limited to 'mpm/lib')
-rw-r--r-- | mpm/lib/CMakeLists.txt | 6 | ||||
-rw-r--r-- | mpm/lib/lmk04828/CMakeLists.txt | 4 | ||||
-rw-r--r-- | mpm/lib/mykonos/ad937x_ctrl.hpp | 29 | ||||
-rw-r--r-- | mpm/lib/net_helper.cpp | 118 | ||||
-rw-r--r-- | mpm/lib/net_helper.hpp | 69 | ||||
-rw-r--r-- | mpm/lib/udev_helper.cpp | 93 | ||||
-rw-r--r-- | mpm/lib/udev_helper.hpp | 62 | ||||
-rw-r--r-- | mpm/lib/xbar_iface.cpp | 56 | ||||
-rw-r--r-- | mpm/lib/xbar_iface.hpp | 69 |
9 files changed, 504 insertions, 2 deletions
diff --git a/mpm/lib/CMakeLists.txt b/mpm/lib/CMakeLists.txt index 19dbf6dce..647697c84 100644 --- a/mpm/lib/CMakeLists.txt +++ b/mpm/lib/CMakeLists.txt @@ -25,4 +25,8 @@ ADD_SUBDIRECTORY(mykonos) ADD_SUBDIRECTORY(lmk04828) USRP_PERIPHS_ADD_OBJECT(periphs - print_foo.cpp) + net_helper.cpp + udev_helper.cpp + xbar_iface.cpp + print_foo.cpp + ) diff --git a/mpm/lib/lmk04828/CMakeLists.txt b/mpm/lib/lmk04828/CMakeLists.txt index 4235cb34a..b3621034a 100644 --- a/mpm/lib/lmk04828/CMakeLists.txt +++ b/mpm/lib/lmk04828/CMakeLists.txt @@ -2,7 +2,9 @@ MACRO(ETTUS_PYTHON_GEN_SOURCE pyfile outfile) #ensure that the directory exists for outfile GET_FILENAME_COMPONENT(outfile_dir ${outfile} PATH) FILE(MAKE_DIRECTORY ${outfile_dir}) - + IF(NOT PYTHON_EXECUTABLE) + MESSAGE( FATAL_ERROR "No python executable found to generate ic_regmaps!" ) + ENDIF(NOT PYTHON_EXECUTABLE) #make the outfile depend on the python script ADD_CUSTOM_COMMAND( OUTPUT ${outfile} DEPENDS ${pyfile} ${ETTUS_PYTHON_GEN_SOURCE_DEPS} diff --git a/mpm/lib/mykonos/ad937x_ctrl.hpp b/mpm/lib/mykonos/ad937x_ctrl.hpp index defb57fa2..12c165dab 100644 --- a/mpm/lib/mykonos/ad937x_ctrl.hpp +++ b/mpm/lib/mykonos/ad937x_ctrl.hpp @@ -70,3 +70,32 @@ protected: static std::set<size_t> _get_valid_fir_lengths(const std::string& which); }; + +#ifdef LIBMPM_PYTHON +void export_mykonos(){ + LIBMPM_BOOST_PREAMBLE("ad937x") + + bp::class_<ad937x_ctrl, boost::noncopyable, std::shared_ptr<ad937x_ctrl> >("ad937x_ctrl", bp::no_init) + .def("make", &ad937x_ctrl::make) + .def("get_rf_freq_range", &ad937x_ctrl::get_rf_freq_range) + .def("get_bw_filter_range", &ad937x_ctrl::get_bw_filter_range) + .def("get_clock_rates", &ad937x_ctrl::get_clock_rates) + .def("get_gain_range", &ad937x_ctrl::get_gain_range) + .def("get_product_id", &ad937x_ctrl::get_product_id) + .def("get_device_rev", &ad937x_ctrl::get_device_rev) + .def("get_api_version", &ad937x_ctrl::get_api_version) + .def("get_arm_version", &ad937x_ctrl::get_arm_version) + .def("set_bw_filter", &ad937x_ctrl::set_bw_filter) + .def("set_gain", &ad937x_ctrl::set_gain) + .def("set_agc_mode", &ad937x_ctrl::set_agc_mode) + .def("set_clock_rate", &ad937x_ctrl::set_clock_rate) + .def("enable_channel", &ad937x_ctrl::enable_channel) + .def("set_freq", &ad937x_ctrl::set_freq) + .def("get_freq", &ad937x_ctrl::get_freq) + .def("set_fir", &ad937x_ctrl::set_fir) + .def("get_fir", &ad937x_ctrl::get_fir) + .def("get_temperature", &ad937x_ctrl::get_temperature) + ; +} +#endif + diff --git a/mpm/lib/net_helper.cpp b/mpm/lib/net_helper.cpp new file mode 100644 index 000000000..0da8de3cf --- /dev/null +++ b/mpm/lib/net_helper.cpp @@ -0,0 +1,118 @@ +// +// Copyright 2017 Ettus Research (National Instruments) +// +// 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 "net_helper.hpp" +#include <uhd/exception.hpp> +#include <boost/format.hpp> +#include <netdb.h> +#include <ifaddrs.h> +#include <linux/if_link.h> +#include <linux/if_packet.h> +#include <iomanip> +#include <iostream> +#include <string> +#include <sstream> +#include <cstring> +#include <cstdint> + +namespace mpm { +namespace network{ + +template <typename ArrayType> +std::string bytearray_to_string(const ArrayType array[], size_t elements) { + std::stringstream result; + for (size_t i = 0; i < elements; i++) { + result << std::uppercase << std::setfill('0') + << std::setw(sizeof(array[i]) * + 2) // always produce 2 hex values for each byte + << std::hex << +array[i]; // Implicit integer promotion + } + return result.str(); +} + +void print_net_ifaces(net_ifaces my_ifaces) { + /* take in a net_ifaces and pretty print information + about all detected network interfaces */ + for (const auto& iface : my_ifaces) { + std::cout << "interface: " << iface.first << std::endl; + std::cout << "\tMAC: " << iface.second.mac_addr << std::endl; + for (const auto& addr : iface.second.ip_addr) { + std::cout << "\tip address: " << addr << std::endl; + } + } +} + +net_ifaces get_net_map() { + /* Get a map containing a string and a net_iface struct + to describe all adresses assigned to a interface */ + struct ifaddrs *ifaddr, *ifa; + int family, s; + char host[NI_MAXHOST]; + net_ifaces net_map; + + if (getifaddrs(&ifaddr) == -1) { + throw uhd::system_error(str(boost::format("Error: %s") % strerror(errno))); + } + + /* Walk through linked list, maintaining head pointer so we + can free list later */ + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) + continue; + + /* Put the interaface name into the map, if it already exists + we get an iterator to the existing element */ + auto result = net_map.emplace( + std::make_pair(std::string(ifa->ifa_name), net_iface())); + auto current_iface = result.first; + + family = ifa->ifa_addr->sa_family; + if (family == AF_INET || family == AF_INET6) { + s = getnameinfo(ifa->ifa_addr, + (family == AF_INET) ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6), + host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + if (s != 0) { + printf("getnameinfo() failed: %s\n", gai_strerror(s)); + return net_map; + } + current_iface->second.ip_addr.push_back(std::string(host)); + } else if (family == AF_PACKET && ifa->ifa_data != NULL) { + struct sockaddr_ll* s = (struct sockaddr_ll*)ifa->ifa_addr; + uint8_t mac_addr[6]; + memcpy(&mac_addr, s->sll_addr, 6); + current_iface->second.mac_addr = bytearray_to_string(mac_addr, 6); + } + } + freeifaddrs(ifaddr); + return net_map; +} + +std::vector<std::string> get_if_addrs(const std::string& mac_addr) { + /* Convenience wrapper to return all adresses associated with one + mac address */ + net_ifaces my_map = get_net_map(); + for (const auto& iface : my_map) { // find + if (iface.second.mac_addr == mac_addr) { + return iface.second.ip_addr; + } + } + return std::vector<std::string>(); +} +} +} diff --git a/mpm/lib/net_helper.hpp b/mpm/lib/net_helper.hpp new file mode 100644 index 000000000..b07e43ccc --- /dev/null +++ b/mpm/lib/net_helper.hpp @@ -0,0 +1,69 @@ +// +// Copyright 2017 Ettus Research (National Instruments) +// +// 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 <map> +#include <vector> +#include <string> + +namespace mpm { +namespace network { + +/*! + * A struct describing a single network interface + */ +using net_iface = struct net_iface { + /*! MAC address of the interface in the form AABBCCDDEEFF */ + std::string mac_addr; + /*! vector of associated IP addresses, contains both IPv4 and IPv6 */ + std::vector<std::string> ip_addr; +}; + +/*! + * net_ifaces contains a <interfaces name, net_iface> pair + * describing mac address and associated ip addresses for + * each interface + */ +using net_ifaces = std::map<std::string, net_iface>; + +/*! + * Convenience function to get all ip addresses of one MAC address + * \param MAC address in the form AABBCCDDEEFF + * \return vector of strings containing all IP addresses with this MAC address + */ +std::vector<std::string> get_if_addrs(const std::string& mac_addr); + +/*! + * Get information about all interfaces on this system + * \return a map with interface names as keys and the interfaces information as value + */ +net_ifaces get_net_map(); + +/*! + * Pretty print net_ifaces in the style of `ip addr` + * \param interface map net_ifaces to print + */ +void print_net_ifaces(net_ifaces my_ifaces); +} +} + +#ifdef LIBMPM_PYTHON +void export_net_iface(){ + LIBMPM_BOOST_PREAMBLE("network") + bp::def("get_if_addrs", &mpm::network::get_if_addrs); +} +#endif + diff --git a/mpm/lib/udev_helper.cpp b/mpm/lib/udev_helper.cpp new file mode 100644 index 000000000..ba12f6667 --- /dev/null +++ b/mpm/lib/udev_helper.cpp @@ -0,0 +1,93 @@ +// +// Copyright 2017 Ettus Research (National Instruments) +// +// 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 "udev_helper.hpp" +#include <uhd/exception.hpp> +#include <boost/format.hpp> +#include <boost/make_shared.hpp> +#include <boost/crc.hpp> +#include <utility> +#include <iostream> +#include <string> +#include <cstring> +#include <fstream> + +using namespace mpm; + +udev_helper::udev_helper(){ + _udev = udev_new(); + if (!_udev) { + throw uhd::os_error("Failed to create udev!"); + } + _enumerate = udev_enumerate_new(_udev); + +} + +udev_helper::~udev_helper(){ + udev_enumerate_unref(_enumerate); + udev_unref(_udev); +} +std::string udev_helper::get_eeprom(const std::string &address){ + udev_list_entry *devices, *dev_list_entry; + udev_device *dev, *parent; + + parent = udev_device_new_from_subsystem_sysname(_udev, "platform", address.c_str()); + if (parent == NULL){ + return std::string(); + } + udev_enumerate_add_match_parent(_enumerate, parent); + udev_enumerate_add_match_subsystem(_enumerate, "nvmem"); + udev_enumerate_scan_devices(_enumerate); + + devices = udev_enumerate_get_list_entry(_enumerate); + if (devices == NULL){ + return std::string(); + } + udev_list_entry_foreach(dev_list_entry, devices) { + const char *path = NULL, *sys_path = NULL; + path = udev_list_entry_get_name(dev_list_entry); + dev = udev_device_new_from_syspath(_udev, path); + sys_path = udev_device_get_syspath(dev); + udev_device_unref(dev); + return "/sys" + std::string(sys_path) + "/nvmem"; + } + return std::string(); +} + +std::vector<std::string> udev_helper::get_spidev_nodes(const std::string &spi_master){ + udev_list_entry *devices, *dev_list_entry; + udev_device *dev, *parent; + + parent = udev_device_new_from_subsystem_sysname(_udev, "platform", spi_master.c_str()); + udev_enumerate_add_match_parent(_enumerate, parent); + udev_enumerate_add_match_subsystem(_enumerate, "spidev"); + udev_enumerate_scan_devices(_enumerate); + + devices = udev_enumerate_get_list_entry(_enumerate); + std::vector<std::string> found_dev_nodes; + if (devices != NULL){ + udev_list_entry_foreach(dev_list_entry, devices){ + const char *path, *dev_node; + path = udev_list_entry_get_name(dev_list_entry); + dev = udev_device_new_from_syspath(_udev, path); + dev_node = udev_device_get_devnode(dev); + found_dev_nodes.push_back(std::string(dev_node)); + udev_device_unref(dev); + } + } + return found_dev_nodes; +} diff --git a/mpm/lib/udev_helper.hpp b/mpm/lib/udev_helper.hpp new file mode 100644 index 000000000..055a93cee --- /dev/null +++ b/mpm/lib/udev_helper.hpp @@ -0,0 +1,62 @@ +// +// Copyright 2017 Ettus Research (National Instruments) +// +// 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 <libudev.h> +#include <string> +#include <vector> + +namespace mpm { + /*! + * The udev_helper class: + * + * talks to libudev and holds a udev context. Device enumeration is done + * once during initialization. + * On destruction the udev context is unreferenced again. + */ + class udev_helper{ + public: + udev_helper(); + ~udev_helper(); + /*! + * Return the nvmem device associated with the parent address + * \param address of the parent platform driver + * \return a string containing the name of file of the device in /sys + */ + std::string get_eeprom(const std::string &address); + /*! + * Find spidevices associated with the spi_master + * \param address of the parent platform driver + * \return a vector of string containing the device paths is /dev + */ + std::vector<std::string> get_spidev_nodes(const std::string &spi_master); + + private: + udev *_udev; + udev_enumerate *_enumerate; + }; +} + +#ifdef LIBMPM_PYTHON +void export_udev_helper(){ + LIBMPM_BOOST_PREAMBLE("udev") + bp::class_<mpm::udev_helper>("udev_helper", bp::init<>()) + .def("get_eeprom", &mpm::udev_helper::get_eeprom) + .def("get_spidev_nodes", &mpm::udev_helper::get_spidev_nodes) + ; +} +#endif + diff --git a/mpm/lib/xbar_iface.cpp b/mpm/lib/xbar_iface.cpp new file mode 100644 index 000000000..21b60d131 --- /dev/null +++ b/mpm/lib/xbar_iface.cpp @@ -0,0 +1,56 @@ +// +// Copyright 2017 Ettus Research (National Instruments) +// +// 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 "xbar_iface.hpp" +#include <uhd/exception.hpp> +#include <boost/format.hpp> +#include <sys/ioctl.h> +#include <fcntl.h> + +using namespace mpm; + +std::mutex xbar_iface::_lock; // Initialize lock for all objects + +xbar_iface::xbar_iface(const std::string &device){ + _fd = open(device.c_str(), O_RDWR); +} + +xbar_iface::~xbar_iface(){ + close(_fd); +} + +void xbar_iface::set_route(uint8_t dst_addr, uint8_t dst_port) { + std::lock_guard<std::mutex> lock(_lock); + rfnoc_crossbar_cmd cmd = {.dest_addr = dst_addr, .dest_port = dst_port}; + int err = ioctl(_fd, RFNCBWROUTIOC, &cmd); + if (err < 0) { + throw uhd::os_error(str(boost::format("setting crossbar route failed! Error: %d") % err)); + } +} + +void xbar_iface::del_route(uint8_t dst_addr, uint8_t dst_port){ + std::lock_guard<std::mutex> lock(_lock); + rfnoc_crossbar_cmd cmd = {.dest_addr = dst_addr, .dest_port = dst_port}; + int err = ioctl(_fd, RFNCDELROUTIOC, &cmd); + if (err < 0){ + throw uhd::os_error(str(boost::format("deleting crossbar route failed! Error: %d") % err)); + } +} + +xbar_iface::sptr xbar_iface::make(const std::string &device){ + return std::make_shared<xbar_iface>(device); +} diff --git a/mpm/lib/xbar_iface.hpp b/mpm/lib/xbar_iface.hpp new file mode 100644 index 000000000..9b17ed97b --- /dev/null +++ b/mpm/lib/xbar_iface.hpp @@ -0,0 +1,69 @@ +// +// Copyright 2017 Ettus Research (National Instruments) +// +// 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/>. +// +#pragma once +#include <boost/noncopyable.hpp> +#include <memory> +#include <mutex> +#include <cstdint> + +namespace mpm{ + +/*! + * Crossbar route command + */ +using rfnoc_crossbar_cmd = struct rfnoc_crossbar_cmd { + /*! destination address */ + uint8_t dest_addr; + /*! destination port */ + uint8_t dest_port; +}; + +#define RFNCBWROUTIOC _IOW('R', 1, struct rfnoc_crossbar_cmd) +#define RFNCDELROUTIOC _IOW('D', 1, struct rfnoc_crossbar_cmd) + +/*! + * Crossbar interface class holding a crossbar context + */ +class xbar_iface: boost::noncopyable{ +public: + // use static mutex! lock_guard + using sptr = std::shared_ptr<xbar_iface>; + static sptr make(const std::string &device); + void set_route(uint8_t dst_addr, uint8_t dst_port); + void del_route(uint8_t dst_addr, uint8_t dst_port); + ~xbar_iface(); + xbar_iface(const std::string &device); + +private: + static std::mutex _lock; + int _fd; +}; +} + + +#ifdef LIBMPM_PYTHON +void export_xbar(){ + LIBMPM_BOOST_PREAMBLE("xbar") + bp::class_<mpm::xbar_iface, boost::noncopyable, std::shared_ptr<mpm::xbar_iface> >("xbar", bp::no_init) + .def("make", &mpm::xbar_iface::make) + .staticmethod("make") + .def("set_route", &mpm::xbar_iface::set_route) + .def("del_route", &mpm::xbar_iface::del_route) + ; +} +#endif + |