diff options
author | Josh Blum <josh@joshknows.com> | 2010-02-09 17:45:35 -0800 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-02-09 17:45:35 -0800 |
commit | e2044e13ec4ad94e9739402257134abd92cf1521 (patch) | |
tree | e642baa20cb194b38d1b3f0c9d4d0b93e32f868d | |
parent | c5480830c6e8e8e862523b3ebf3117fda8a100df (diff) | |
download | uhd-e2044e13ec4ad94e9739402257134abd92cf1521.tar.gz uhd-e2044e13ec4ad94e9739402257134abd92cf1521.tar.bz2 uhd-e2044e13ec4ad94e9739402257134abd92cf1521.zip |
added transport directory and udp transport
-rw-r--r-- | Makefile.common | 6 | ||||
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | include/uhd/Makefile.am | 2 | ||||
-rw-r--r-- | include/uhd/device.hpp | 13 | ||||
-rw-r--r-- | include/uhd/transport/.gitignore | 2 | ||||
-rw-r--r-- | include/uhd/transport/Makefile.am | 24 | ||||
-rw-r--r-- | include/uhd/transport/udp.hpp | 77 | ||||
-rw-r--r-- | include/uhd/usrp/usrp.hpp | 9 | ||||
-rw-r--r-- | lib/Makefile.am | 3 | ||||
-rw-r--r-- | lib/transport/.gitignore | 2 | ||||
-rw-r--r-- | lib/transport/Makefile.am | 31 | ||||
-rw-r--r-- | lib/transport/udp.cpp | 65 | ||||
-rw-r--r-- | lib/usrp/mboard/usrp2.cpp | 77 | ||||
-rw-r--r-- | lib/usrp/usrp.cpp | 14 | ||||
-rw-r--r-- | uhd.pc.in | 2 |
15 files changed, 230 insertions, 103 deletions
diff --git a/Makefile.common b/Makefile.common index 742a95b31..0a16735af 100644 --- a/Makefile.common +++ b/Makefile.common @@ -16,11 +16,9 @@ # UHD_LA = $(abs_top_builddir)/lib/libuhd.la - -UHD_USRP_LA = $(abs_top_builddir)/lib/usrp/lib.la - +UHD_TRANSPORT_LA = $(abs_top_builddir)/lib/transport/lib.la +UHD_USRP_LA = $(abs_top_builddir)/lib/usrp/lib.la UHD_USRP_DBOARD_LA = $(abs_top_builddir)/lib/usrp/dboard/lib.la - UHD_USRP_MBOARD_LA = $(abs_top_builddir)/lib/usrp/mboard/lib.la GENERAL_CPPFLAGS = \ diff --git a/configure.ac b/configure.ac index 58648ce2d..18598b2b6 100644 --- a/configure.ac +++ b/configure.ac @@ -108,14 +108,16 @@ AC_CONFIG_FILES([ \ include/Makefile \ include/uhd/Makefile \ include/uhd/usrp/Makefile \ + include/uhd/quadradio/Makefile \ + include/uhd/transport/Makefile \ include/uhd/usrp/dboard/Makefile \ include/uhd/usrp/mboard/Makefile \ - include/uhd/quadradio/Makefile \ lib/Makefile \ + lib/quadradio/Makefile \ + lib/transport/Makefile \ lib/usrp/Makefile \ lib/usrp/dboard/Makefile \ lib/usrp/mboard/Makefile \ - lib/quadradio/Makefile \ test/Makefile \ ]) AC_OUTPUT diff --git a/include/uhd/Makefile.am b/include/uhd/Makefile.am index 00691d920..772d1e4b9 100644 --- a/include/uhd/Makefile.am +++ b/include/uhd/Makefile.am @@ -17,7 +17,7 @@ include $(top_srcdir)/Makefile.common -SUBDIRS = usrp quadradio +SUBDIRS = quadradio transport usrp this_includedir = $(includedir)/uhd this_include_HEADERS = \ diff --git a/include/uhd/device.hpp b/include/uhd/device.hpp index 63f897f97..84d1c1bfb 100644 --- a/include/uhd/device.hpp +++ b/include/uhd/device.hpp @@ -38,12 +38,6 @@ class device : boost::noncopyable, public wax::obj{ public: typedef boost::shared_ptr<device> sptr; - //argument types for send and recv raw methods - //the send args is a vector of the boost asio buffers - //the recv args is a callback that takes a boost asio buffer - typedef std::vector<boost::asio::const_buffer> send_args_t; - typedef boost::function<bool(const boost::asio::const_buffer &)> recv_args_t; - //structors device(void); virtual ~device(void); @@ -78,11 +72,8 @@ public: device_addr_t get_device_addr(void); //the io interface - virtual void send_raw(const send_args_t &) = 0; - virtual void recv_raw(const recv_args_t &) = 0; - - //connect dsps and subdevs - void connect(const wax::obj &src, const wax::obj &sink); + virtual void send_raw(const std::vector<boost::asio::const_buffer> &) = 0; + virtual const boost::asio::const_buffer recv_raw(void) = 0; }; } //namespace uhd diff --git a/include/uhd/transport/.gitignore b/include/uhd/transport/.gitignore new file mode 100644 index 000000000..b336cc7ce --- /dev/null +++ b/include/uhd/transport/.gitignore @@ -0,0 +1,2 @@ +/Makefile +/Makefile.in diff --git a/include/uhd/transport/Makefile.am b/include/uhd/transport/Makefile.am new file mode 100644 index 000000000..7a62e3051 --- /dev/null +++ b/include/uhd/transport/Makefile.am @@ -0,0 +1,24 @@ +# +# 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/>. +# + +include $(top_srcdir)/Makefile.common + +SUBDIRS = + +this_includedir = $(includedir)/uhd/transport +this_include_HEADERS = \ + udp.hpp diff --git a/include/uhd/transport/udp.hpp b/include/uhd/transport/udp.hpp new file mode 100644 index 000000000..67979afac --- /dev/null +++ b/include/uhd/transport/udp.hpp @@ -0,0 +1,77 @@ +// +// 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/>. +// + +#include <boost/asio.hpp> +#include <boost/utility.hpp> +#include <boost/shared_ptr.hpp> +#include <stdio.h> + +#ifndef INCLUDED_UHD_TRANSPORT_UDP_HPP +#define INCLUDED_UHD_TRANSPORT_UDP_HPP + +namespace uhd{ namespace transport{ + +class udp : boost::noncopyable{ +public: + typedef boost::shared_ptr<udp> sptr; + + /*! + * Constructor. + * The address will be resolved, it can be a host name or ipv4. + * The port will be resolved, it can be a port type or number. + * \param addr a string representing the destination address + * \param port a string representing the destination port + * \param bcast if true, enable the broadcast option on the socket + */ + udp(const std::string &addr, const std::string &port, bool bcast = false); + + /*! + * Destructor + */ + ~udp(void); + + /*! + * Send a vector of buffer (like send_msg). + * \param buffs a vector of asio buffers + */ + void send(const std::vector<boost::asio::const_buffer> &buffs); + + /*! + * Send a single buffer. + * \param buff a pointer into memory + * \param len the length in bytes + */ + void send(const void *buff, size_t len); + + /*! + * 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 + */ + const boost::asio::const_buffer 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 + +#endif /* INCLUDED_UHD_TRANSPORT_UDP_HPP */ diff --git a/include/uhd/usrp/usrp.hpp b/include/uhd/usrp/usrp.hpp index eda8f5fc1..84890b8fe 100644 --- a/include/uhd/usrp/usrp.hpp +++ b/include/uhd/usrp/usrp.hpp @@ -35,16 +35,17 @@ public: usrp(const device_addr_t & device_addr); ~usrp(void); - void send_raw(const send_args_t &); - void recv_raw(const recv_args_t &); + //the io interface + void send_raw(const std::vector<boost::asio::const_buffer> &); + const boost::asio::const_buffer recv_raw(void); private: void get(const wax::obj &, wax::obj &); void set(const wax::obj &, const wax::obj &); std::map<std::string, mboard::base::sptr> _mboards; - boost::function<void(const device::send_args_t &)> _send_raw_cb; - boost::function<void(const device::recv_args_t &)> _recv_raw_cb; + boost::function<void(const std::vector<boost::asio::const_buffer> &)> _send_raw_cb; + boost::function<const boost::asio::const_buffer(void)> _recv_raw_cb; }; }} //namespace diff --git a/lib/Makefile.am b/lib/Makefile.am index 8ddc0e4be..ff7c6b75d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -17,7 +17,7 @@ include $(top_srcdir)/Makefile.common -SUBDIRS = usrp quadradio +SUBDIRS = quadradio transport usrp AM_CPPFLAGS = $(GENERAL_CPPFLAGS) @@ -32,6 +32,7 @@ libuhd_la_SOURCES = \ libuhd_la_LIBADD = \ $(GENERAL_LDDFLAGS) \ + $(UHD_TRANSPORT_LA) \ $(UHD_USRP_LA) noinst_HEADERS = diff --git a/lib/transport/.gitignore b/lib/transport/.gitignore new file mode 100644 index 000000000..b336cc7ce --- /dev/null +++ b/lib/transport/.gitignore @@ -0,0 +1,2 @@ +/Makefile +/Makefile.in diff --git a/lib/transport/Makefile.am b/lib/transport/Makefile.am new file mode 100644 index 000000000..93e1676bc --- /dev/null +++ b/lib/transport/Makefile.am @@ -0,0 +1,31 @@ +# +# 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/>. +# + +include $(top_srcdir)/Makefile.common + +SUBDIRS = + +AM_CPPFLAGS = $(GENERAL_CPPFLAGS) + +lib_LTLIBRARIES = lib.la + +lib_la_SOURCES = \ + udp.cpp + +lib_la_LIBADD = $(GENERAL_LDDFLAGS) + +noinst_HEADERS = diff --git a/lib/transport/udp.cpp b/lib/transport/udp.cpp new file mode 100644 index 000000000..5b221551b --- /dev/null +++ b/lib/transport/udp.cpp @@ -0,0 +1,65 @@ +// +// 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/>. +// + +#include <uhd/transport/udp.hpp> +#include <boost/format.hpp> +#include <iostream> + +uhd::transport::udp::udp(const std::string &addr, const std::string &port, bool bcast){ + //std::cout << boost::format("Creating udp transport for %s %s") % addr % port << std::endl; + + // resolve the address + boost::asio::ip::udp::resolver resolver(_io_service); + boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), addr, port); + _receiver_endpoint = *resolver.resolve(query); + + // Create and open the socket + _socket = new boost::asio::ip::udp::socket(_io_service); + _socket->open(boost::asio::ip::udp::v4()); + + if (bcast){ + // Allow broadcasting + boost::asio::socket_base::broadcast option(true); + _socket->set_option(option); + } + +} + +uhd::transport::udp::~udp(void){ + delete _socket; +} + +void uhd::transport::udp::send(const std::vector<boost::asio::const_buffer> &buffs){ + _socket->send_to(buffs, _receiver_endpoint); +} + +void uhd::transport::udp::send(const void *buff, size_t len){ + _socket->send_to(boost::asio::buffer(buff, len), _receiver_endpoint); +} + +const boost::asio::const_buffer uhd::transport::udp::recv(void){ + //recv if data is available + if (_socket->available()){ + size_t len = _socket->receive_from( + boost::asio::buffer(_recv_buff, sizeof(_recv_buff)), + _sender_endpoint + ); + return boost::asio::buffer(_recv_buff, len); + } + //return an empty buffer + return boost::asio::buffer(_recv_buff, 0); +} diff --git a/lib/usrp/mboard/usrp2.cpp b/lib/usrp/mboard/usrp2.cpp index ffbfe69fc..d51b37602 100644 --- a/lib/usrp/mboard/usrp2.cpp +++ b/lib/usrp/mboard/usrp2.cpp @@ -16,96 +16,29 @@ // #include <uhd/usrp/mboard/usrp2.hpp> +#include <uhd/transport/udp.hpp> #include "usrp2_fw_common.h" #include <uhd/device.hpp> -#include <boost/asio.hpp> #include <boost/thread.hpp> -#include <boost/format.hpp> -#include <boost/utility.hpp> #include <boost/lexical_cast.hpp> #include <netinet/in.h> -using namespace uhd; using namespace uhd::usrp::mboard; -using boost::asio::ip::udp; - -/*********************************************************************** - * Wrapper for the udp transport - **********************************************************************/ -class udp_transport : boost::noncopyable{ -public: - udp_transport(const std::string &addr, const std::string &port, bool bcast = false){ - //std::cout << boost::format("Creating udp transport for %s %s") % addr % port << std::endl; - - // resolve the address - udp::resolver resolver(_io_service); - udp::resolver::query query(udp::v4(), addr, port); - _receiver_endpoint = *resolver.resolve(query); - - // Create and open the socket - _socket = new udp::socket(_io_service); - _socket->open(udp::v4()); - - if (bcast){ - // Allow broadcasting - boost::asio::socket_base::broadcast option(true); - _socket->set_option(option); - } - - } - - ~udp_transport(void){ - delete _socket; - } - - void send(const device::send_args_t &buffs){ - _socket->send_to(buffs, _receiver_endpoint); - } - - void send(const void *buff, size_t len){ - _socket->send_to(boost::asio::buffer(buff, len), _receiver_endpoint); - } - - void recv(const device::recv_args_t &handler){ - // make sure that bytes are available (crappy timeout 100 ms) - for (size_t i = 0; i < 10; i++){ - if (_socket->available()) break; - boost::this_thread::sleep(boost::posix_time::milliseconds(10)); - } - - // receive the bytes and call the handler - udp::endpoint sender_endpoint; - while (_socket->available()){ - size_t len = _socket->receive_from( - boost::asio::buffer(_recv_buff, sizeof(_recv_buff)), - sender_endpoint - ); - bool done = handler(boost::asio::buffer(_recv_buff, len)); - if (done) return; - } - } - -private: - udp::socket *_socket; - udp::endpoint _receiver_endpoint; - boost::asio::io_service _io_service; - uint8_t _recv_buff[1500]; -}; /*********************************************************************** * Discovery over the udp transport **********************************************************************/ -std::vector<device_addr_t> usrp2::discover(const device_addr_t &hint){ - std::vector<device_addr_t> usrp2_addrs; +std::vector<uhd::device_addr_t> usrp2::discover(const device_addr_t &hint){ + std::vector<uhd::device_addr_t> usrp2_addrs; //create a udp transport to communicate std::string ctrl_port = boost::lexical_cast<std::string>(USRP2_UDP_CTRL_PORT); - udp_transport trans(hint.udp_args.addr, ctrl_port, true); + uhd::transport::udp udp_transport(hint.udp_args.addr, ctrl_port, true); //send a hello control packet usrp2_ctrl_data_t data; data.id = htonl(USRP2_CTRL_ID_HELLO); - trans.send(&data, sizeof(data)); + udp_transport.send(&data, sizeof(data)); //TODO start a thread to listen and sleep for timeout diff --git a/lib/usrp/usrp.cpp b/lib/usrp/usrp.cpp index 59dd49dd1..d188f51a7 100644 --- a/lib/usrp/usrp.cpp +++ b/lib/usrp/usrp.cpp @@ -28,11 +28,11 @@ using namespace uhd::usrp; * default callbacks for the send and recv * these should be replaced with callbacks from the mboard object **********************************************************************/ -static void send_raw_default(const uhd::device::send_args_t &){ +static void send_raw_default(const std::vector<boost::asio::const_buffer> &){ throw std::runtime_error("No callback registered for send raw"); } -static void recv_raw_default(const uhd::device::recv_args_t &){ +static const boost::asio::const_buffer recv_raw_default(void){ throw std::runtime_error("No callback registered for recv raw"); } @@ -42,7 +42,7 @@ static void recv_raw_default(const uhd::device::recv_args_t &){ usrp::usrp(const device_addr_t & device_addr){ //set the default callbacks, the code below should replace them _send_raw_cb = boost::bind(&send_raw_default, _1); - _recv_raw_cb = boost::bind(&recv_raw_default, _1); + _recv_raw_cb = boost::bind(&recv_raw_default); //create mboard based on the device addr if (device_addr.type == DEVICE_ADDR_TYPE_VIRTUAL){ @@ -83,10 +83,10 @@ void usrp::set(const wax::obj &, const wax::obj &){ throw std::runtime_error("Cannot set in usrp device"); } -void usrp::send_raw(const send_args_t &args){ - return _send_raw_cb(args); +void usrp::send_raw(const std::vector<boost::asio::const_buffer> &buffs){ + return _send_raw_cb(buffs); } -void usrp::recv_raw(const recv_args_t &args){ - return _recv_raw_cb(args); +const boost::asio::const_buffer usrp::recv_raw(void){ + return _recv_raw_cb(); } @@ -4,7 +4,7 @@ libdir=@libdir@ includedir=@includedir@ Name: uhd -Description: Universal Software Radio Peripheral - Unified Hardware Driver +Description: Universal Hardware Driver Requires: Version: @VERSION@ Libs: -L${libdir} -luhd |