summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2010-02-12 18:07:55 -0800
committerJosh Blum <josh@joshknows.com>2010-02-12 18:07:55 -0800
commit9fff25f4e5da179ea29ff44278e0415a337870cb (patch)
treecfbee4cf2921fd4bd415e3af1c1d466f79bab3d7 /include
parent350f5c5decca20a54132867283448fd32226bbc2 (diff)
downloaduhd-9fff25f4e5da179ea29ff44278e0415a337870cb.tar.gz
uhd-9fff25f4e5da179ea29ff44278e0415a337870cb.tar.bz2
uhd-9fff25f4e5da179ea29ff44278e0415a337870cb.zip
Added a templated dictionary class because its more useful than map.
Made the device addrs into a string:string dict. If its all strings we dont have to change the top level caller for new product. Created shared_iovec class to manage memory for device recvs. Work on the bro/dude control protocol for usrp2.
Diffstat (limited to 'include')
-rw-r--r--include/uhd/Makefile.am2
-rw-r--r--include/uhd/device.hpp7
-rw-r--r--include/uhd/device_addr.hpp72
-rw-r--r--include/uhd/dict.hpp140
-rw-r--r--include/uhd/shared_iovec.hpp54
-rw-r--r--include/uhd/transport/udp.hpp7
-rw-r--r--include/uhd/usrp/mboard/usrp2.hpp5
-rw-r--r--include/uhd/usrp/usrp.hpp6
8 files changed, 231 insertions, 62 deletions
diff --git a/include/uhd/Makefile.am b/include/uhd/Makefile.am
index 772d1e4b9..6c2034ea8 100644
--- a/include/uhd/Makefile.am
+++ b/include/uhd/Makefile.am
@@ -23,8 +23,10 @@ this_includedir = $(includedir)/uhd
this_include_HEADERS = \
device.hpp \
device_addr.hpp \
+ dict.hpp \
gain_handler.hpp \
props.hpp \
+ shared_iovec.hpp \
time_spec.hpp \
utils.hpp \
wax.hpp
diff --git a/include/uhd/device.hpp b/include/uhd/device.hpp
index 09a2bbb2f..dfbfbd7c0 100644
--- a/include/uhd/device.hpp
+++ b/include/uhd/device.hpp
@@ -25,6 +25,7 @@
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/asio/buffer.hpp>
+#include <uhd/shared_iovec.hpp>
#include <vector>
namespace uhd{
@@ -51,7 +52,7 @@ public:
* \param hint a partially (or fully) filled in device address
* \return a vector of device addresses for all usrps on the system
*/
- static std::vector<device_addr_t> discover(const device_addr_t & hint);
+ static device_addrs_t discover(const device_addr_t &hint);
/*!
* \brief Create a new usrp device from the device address hint.
@@ -64,7 +65,7 @@ public:
* \param which which address to use when multiple are discovered
* \return a shared pointer to a new device instance
*/
- static sptr make(const device_addr_t & hint, size_t which = 0);
+ static sptr make(const device_addr_t &hint, size_t which = 0);
/*!
* Get the device address for this board.
@@ -73,7 +74,7 @@ public:
//the io interface
virtual void send_raw(const std::vector<boost::asio::const_buffer> &) = 0;
- virtual boost::asio::const_buffer recv_raw(void) = 0;
+ virtual uhd::shared_iovec recv_raw(void) = 0;
};
} //namespace uhd
diff --git a/include/uhd/device_addr.hpp b/include/uhd/device_addr.hpp
index 84ae60881..8ea580321 100644
--- a/include/uhd/device_addr.hpp
+++ b/include/uhd/device_addr.hpp
@@ -18,10 +18,12 @@
#ifndef INCLUDED_UHD_DEVICE_ADDR_HPP
#define INCLUDED_UHD_DEVICE_ADDR_HPP
+#include <uhd/dict.hpp>
#include <string>
#include <iostream>
#include <netinet/ether.h>
#include <stdint.h>
+#include <vector>
namespace uhd{
@@ -36,62 +38,30 @@ namespace uhd{
};
/*!
- * Possible usrp device interface types.
- */
- enum device_addr_type_t{
- DEVICE_ADDR_TYPE_AUTO,
- DEVICE_ADDR_TYPE_VIRTUAL,
- DEVICE_ADDR_TYPE_USB,
- DEVICE_ADDR_TYPE_ETH,
- DEVICE_ADDR_TYPE_UDP,
- DEVICE_ADDR_TYPE_GPMC
- };
+ * The device address args are just a mapping of key/value string pairs.
+ * When left empty, the discovery routine will try to find all usrps.
+ * The discovery can be narrowed down by specifying the transport type arguments.
+ *
+ * For example, to access a specific usrp2 one would specify the transport type
+ * ("type", "udp") and the transport args ("addr", "<resolvable_hostname_or_addr>").
+ */
+ typedef dict<std::string, std::string> device_addr_t;
+ typedef std::vector<device_addr_t> device_addrs_t;
/*!
- * Structure to hold properties that identify a usrp device.
- */
- struct device_addr_t{
- device_addr_type_t type;
- struct{
- size_t num_rx_dsps;
- size_t num_tx_dsps;
- size_t num_dboards;
- } virtual_args;
- struct{
- uint16_t vendor_id;
- uint16_t product_id;
- } usb_args;
- struct{
- std::string ifc;
- std::string mac_addr;
- } eth_args;
- struct{
- std::string addr;
- } udp_args;
- struct{
- //TODO unknown for now
- } gpmc_args;
-
- //the discovery args are filled in by the discovery routine
- struct{
- uint16_t mboard_id;
- } discovery_args;
-
- /*!
- * \brief Convert a usrp device_addr_t into a string representation
- */
- std::string to_string(void) const;
-
- /*!
- * \brief Default constructor to initialize the device_addr_t struct
- */
- device_addr_t(device_addr_type_t device_addr_type = DEVICE_ADDR_TYPE_AUTO);
- };
+ * Function to turn a device address into a string.
+ * Just having the operator<< below should be sufficient.
+ * However, boost format seems to complain with the %
+ * and this is just easier because it works.
+ * \param device_addr a device address instance
+ * \return the string representation
+ */
+ std::string device_addr_to_string(const device_addr_t &device_addr);
} //namespace uhd
//ability to use types with stream operators
-std::ostream& operator<<(std::ostream &os, const uhd::device_addr_t &x);
-std::ostream& operator<<(std::ostream &os, const uhd::mac_addr_t &x);
+std::ostream& operator<<(std::ostream &, const uhd::device_addr_t &);
+std::ostream& operator<<(std::ostream &, const uhd::mac_addr_t &);
#endif /* INCLUDED_UHD_DEVICE_ADDR_HPP */
diff --git a/include/uhd/dict.hpp b/include/uhd/dict.hpp
new file mode 100644
index 000000000..3abc4273c
--- /dev/null
+++ b/include/uhd/dict.hpp
@@ -0,0 +1,140 @@
+//
+// Copyright 2010 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_DICT_HPP
+#define INCLUDED_UHD_DICT_HPP
+
+#include <map>
+#include <vector>
+#include <boost/foreach.hpp>
+#include <stdexcept>
+
+namespace uhd{
+
+ /*!
+ * A templated dictionary class with a python-like interface.
+ * Its wraps around a std::map internally.
+ */
+ template <class Key, class Val> class dict{
+ public:
+ /*!
+ * Create a new empty dictionary.
+ */
+ dict(void){
+ /* NOP */
+ }
+
+ /*!
+ * Create a dictionary from a map.
+ * \param map a map with key value pairs
+ */
+ dict(const std::map<Key, Val> &map){
+ _map = map;
+ }
+
+ /*!
+ * Destroy this dict.
+ */
+ ~dict(void){
+ /* NOP */
+ }
+
+ /*!
+ * Get a list of the keys in this dict.
+ * \return vector of keys
+ */
+ std::vector<Key> get_keys(void) const{
+ std::vector<Key> keys;
+ std::pair<Key, Val> p;
+ BOOST_FOREACH(p, _map){
+ keys.push_back(p.first);
+ }
+ return keys;
+ }
+
+ /*!
+ * Get a list of the values in this dict.
+ * \return vector of values
+ */
+ std::vector<Val> get_vals(void) const{
+ std::vector<Val> vals;
+ std::pair<Key, Val> p;
+ BOOST_FOREACH(p, _map){
+ vals.push_back(p.second);
+ }
+ return vals;
+ }
+
+ /*!
+ * Does the dictionary contain this key?
+ * \param key the key to look for
+ * \return true if found
+ */
+ bool has_key(const Key &key) const{
+ std::pair<Key, Val> p;
+ BOOST_FOREACH(p, _map){
+ if (p.first == key) return true;
+ }
+ return false;
+ }
+
+ /*!
+ * Get a value for the given key if it exists.
+ * If the key is not found throw an error.
+ * \param key the key to look for
+ * \return the value at the key
+ * \throw an exception when not found
+ */
+ const Val &operator[](const Key &key) const{
+ if (has_key(key)){
+ return _map.find(key)->second;
+ }
+ throw std::invalid_argument("key not found in dict");
+ }
+
+ /*!
+ * Set a value for the given key, however, in reality
+ * it really returns a reference which can be assigned to.
+ * \param key the key to set to
+ * \return a reference to the value
+ */
+ Val &operator[](const Key &key){
+ return _map[key];
+ }
+
+ /*!
+ * Pop an item out of the dictionary.
+ * \param key the item key
+ * \return the value of the item
+ * \throw an exception when not found
+ */
+ Val pop_key(const Key &key){
+ if (has_key(key)){
+ Val val = _map.find(key)->second;
+ _map.erase(key);
+ return val;
+ }
+ throw std::invalid_argument("key not found in dict");
+ }
+
+ private:
+ std::map<Key, Val> _map; //private container
+ };
+
+} //namespace uhd
+
+#endif /* INCLUDED_UHD_DICT_HPP */
diff --git a/include/uhd/shared_iovec.hpp b/include/uhd/shared_iovec.hpp
new file mode 100644
index 000000000..a120e55d5
--- /dev/null
+++ b/include/uhd/shared_iovec.hpp
@@ -0,0 +1,54 @@
+//
+// Copyright 2010 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_SHARED_IOVEC_HPP
+#define INCLUDED_UHD_SHARED_IOVEC_HPP
+
+#include <boost/shared_array.hpp>
+#include <stdint.h>
+
+namespace uhd{
+
+/*!
+ * A shared iovec contains a shared array and its length.
+ * Creating a new shared iovec allocates new memory.
+ * This memory is freed when all copies are destroyed.
+ */
+class shared_iovec{
+public:
+ /*!
+ * Create a shared iovec and allocate memory.
+ * \param len the length in bytes
+ */
+ shared_iovec(size_t len=0);
+
+ /*!
+ * Destroy a shared iovec.
+ * Will not free the memory unless this is the last copy.
+ */
+ ~shared_iovec(void);
+
+ void *base;
+ size_t len;
+
+private:
+ boost::shared_array<uint8_t> _shared_array;
+};
+
+} //namespace uhd
+
+#endif /* INCLUDED_UHD_SHARED_IOVEC_HPP */
diff --git a/include/uhd/transport/udp.hpp b/include/uhd/transport/udp.hpp
index c9a9dd53b..6db6bd377 100644
--- a/include/uhd/transport/udp.hpp
+++ b/include/uhd/transport/udp.hpp
@@ -18,7 +18,7 @@
#include <boost/asio.hpp>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>
-#include <stdio.h>
+#include <uhd/shared_iovec.hpp>
#ifndef INCLUDED_UHD_TRANSPORT_UDP_HPP
#define INCLUDED_UHD_TRANSPORT_UDP_HPP
@@ -59,16 +59,15 @@ public:
/*!
* Receive a buffer. The memory is managed internally.
* Calling recv will invalidate the buffer of the previous recv.
- * \return an asio const buffer with internal memory
+ * \return a shared iovec with allocated memory
*/
- boost::asio::const_buffer recv(void);
+ uhd::shared_iovec recv(void);
private:
boost::asio::ip::udp::socket *_socket;
boost::asio::ip::udp::endpoint _receiver_endpoint;
boost::asio::ip::udp::endpoint _sender_endpoint;
boost::asio::io_service _io_service;
- uint8_t _recv_buff[1500]; //max mtu
};
}} //namespace
diff --git a/include/uhd/usrp/mboard/usrp2.hpp b/include/uhd/usrp/mboard/usrp2.hpp
index 8c2430dbd..4950f4cd1 100644
--- a/include/uhd/usrp/mboard/usrp2.hpp
+++ b/include/uhd/usrp/mboard/usrp2.hpp
@@ -21,6 +21,7 @@
#include <uhd/usrp/mboard/base.hpp>
#include <uhd/device_addr.hpp>
#include <uhd/usrp/dboard/manager.hpp>
+#include <uhd/transport/udp.hpp>
#include <map>
namespace uhd{ namespace usrp{ namespace mboard{
@@ -36,7 +37,7 @@ public:
* \param hint a device addr with the usrp2 address filled in
* \return a vector of device addresses for all usrp2s found
*/
- static std::vector<device_addr_t> discover(const device_addr_t &hint);
+ static device_addrs_t discover(const device_addr_t &hint);
usrp2(const device_addr_t &);
~usrp2(void);
@@ -46,6 +47,8 @@ private:
void set(const wax::obj &, const wax::obj &);
std::map<std::string, dboard::manager::sptr> _dboard_managers;
+ uhd::transport::udp::sptr _udp_ctrl_transport;
+ uhd::transport::udp::sptr _udp_data_transport;
};
}}} //namespace
diff --git a/include/uhd/usrp/usrp.hpp b/include/uhd/usrp/usrp.hpp
index 975debed1..0dca36f62 100644
--- a/include/uhd/usrp/usrp.hpp
+++ b/include/uhd/usrp/usrp.hpp
@@ -32,12 +32,12 @@ namespace uhd{ namespace usrp{
*/
class usrp : public device{
public:
- usrp(const device_addr_t & device_addr);
+ usrp(const device_addr_t &device_addr);
~usrp(void);
//the io interface
void send_raw(const std::vector<boost::asio::const_buffer> &);
- boost::asio::const_buffer recv_raw(void);
+ uhd::shared_iovec recv_raw(void);
private:
void get(const wax::obj &, wax::obj &);
@@ -45,7 +45,7 @@ private:
std::map<std::string, mboard::base::sptr> _mboards;
boost::function<void(const std::vector<boost::asio::const_buffer> &)> _send_raw_cb;
- boost::function<boost::asio::const_buffer(void)> _recv_raw_cb;
+ boost::function<uhd::shared_iovec(void)> _recv_raw_cb;
};
}} //namespace