diff options
-rw-r--r-- | host/include/uhd/transport/udp.hpp | 21 | ||||
-rw-r--r-- | host/lib/transport/udp.cpp | 45 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/usrp2_impl.cpp | 34 |
3 files changed, 61 insertions, 39 deletions
diff --git a/host/include/uhd/transport/udp.hpp b/host/include/uhd/transport/udp.hpp index 6db6bd377..554234b43 100644 --- a/host/include/uhd/transport/udp.hpp +++ b/host/include/uhd/transport/udp.hpp @@ -30,44 +30,33 @@ public: typedef boost::shared_ptr<udp> sptr; /*! - * Constructor. + * Make a new udp transport. * 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); + static sptr make(const std::string &addr, const std::string &port, bool bcast = false); /*! * 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); + virtual void send(const std::vector<boost::asio::const_buffer> &buffs) = 0; /*! * Send a single buffer. * \param buff single asio buffer */ - void send(const boost::asio::const_buffer &buff); + virtual void send(const boost::asio::const_buffer &buff) = 0; /*! * Receive a buffer. The memory is managed internally. * Calling recv will invalidate the buffer of the previous recv. * \return a shared iovec with allocated memory */ - 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; + virtual uhd::shared_iovec recv(void) = 0; }; }} //namespace diff --git a/host/lib/transport/udp.cpp b/host/lib/transport/udp.cpp index 06defb107..af60760a5 100644 --- a/host/lib/transport/udp.cpp +++ b/host/lib/transport/udp.cpp @@ -20,7 +20,42 @@ #include <boost/assign/list_of.hpp> #include <iostream> -uhd::transport::udp::udp(const std::string &addr, const std::string &port, bool bcast){ +/*********************************************************************** + * UDP implementation class + **********************************************************************/ +class udp_impl : public uhd::transport::udp{ +public: + //structors + udp_impl(const std::string &addr, const std::string &port, bool bcast); + ~udp_impl(void); + + //send/recv + void send(const std::vector<boost::asio::const_buffer> &buffs); + void send(const boost::asio::const_buffer &buff); + 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; +}; + +/*********************************************************************** + * UDP public make function + **********************************************************************/ +uhd::transport::udp::sptr uhd::transport::udp::make( + const std::string &addr, + const std::string &port, + bool bcast +){ + return uhd::transport::udp::sptr(new udp_impl(addr, port, bcast)); +} + +/*********************************************************************** + * UDP implementation methods + **********************************************************************/ +udp_impl::udp_impl(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 @@ -40,20 +75,20 @@ uhd::transport::udp::udp(const std::string &addr, const std::string &port, bool } -uhd::transport::udp::~udp(void){ +udp_impl::~udp_impl(void){ delete _socket; } -void uhd::transport::udp::send(const std::vector<boost::asio::const_buffer> &buffs){ +void udp_impl::send(const std::vector<boost::asio::const_buffer> &buffs){ _socket->send_to(buffs, _receiver_endpoint); } -void uhd::transport::udp::send(const boost::asio::const_buffer &buff){ +void udp_impl::send(const boost::asio::const_buffer &buff){ std::vector<boost::asio::const_buffer> buffs = boost::assign::list_of(buff); send(buffs); } -uhd::shared_iovec uhd::transport::udp::recv(void){ +uhd::shared_iovec udp_impl::recv(void){ //allocate a buffer for the number of bytes available (could be zero) uhd::shared_iovec iov(_socket->available()); //call recv only if data is available diff --git a/host/lib/usrp/usrp2/usrp2_impl.cpp b/host/lib/usrp/usrp2/usrp2_impl.cpp index 2b4e8fe39..f44964394 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.cpp +++ b/host/lib/usrp/usrp2/usrp2_impl.cpp @@ -33,17 +33,18 @@ uhd::device_addrs_t usrp2::discover(const device_addr_t &hint){ //create a udp transport to communicate //TODO if an addr is not provided, search all interfaces? std::string ctrl_port = boost::lexical_cast<std::string>(USRP2_UDP_CTRL_PORT); - uhd::transport::udp udp_transport(hint["addr"], ctrl_port, true); + transport::udp::sptr udp_transport = \ + transport::udp::make(hint["addr"], ctrl_port, true); //send a hello control packet usrp2_ctrl_data_t ctrl_data_out; ctrl_data_out.id = htonl(USRP2_CTRL_ID_GIVE_ME_YOUR_IP_ADDR_BRO); - udp_transport.send(boost::asio::buffer(&ctrl_data_out, sizeof(ctrl_data_out))); + udp_transport->send(boost::asio::buffer(&ctrl_data_out, sizeof(ctrl_data_out))); //loop and recieve until the time is up size_t num_timeouts = 0; while(true){ - uhd::shared_iovec iov = udp_transport.recv(); + uhd::shared_iovec iov = udp_transport->recv(); //std::cout << boost::asio::buffer_size(buff) << "\n"; if (iov.len < sizeof(usrp2_ctrl_data_t)){ //sleep a little so we dont burn cpu @@ -72,21 +73,17 @@ uhd::device_addrs_t usrp2::discover(const device_addr_t &hint){ /*********************************************************************** * Make **********************************************************************/ +#define num2str(num) (boost::lexical_cast<std::string>(num)) + device::sptr usrp2::make(const device_addr_t &device_addr){ //create a control transport - uhd::transport::udp::sptr ctrl_transport( - new uhd::transport::udp( - device_addr["addr"], - boost::lexical_cast<std::string>(USRP2_UDP_CTRL_PORT) - ) + transport::udp::sptr ctrl_transport = transport::udp::make( + device_addr["addr"], num2str(USRP2_UDP_CTRL_PORT) ); //create a data transport - uhd::transport::udp::sptr data_transport( - new uhd::transport::udp( - device_addr["addr"], - boost::lexical_cast<std::string>(USRP2_UDP_DATA_PORT) - ) + transport::udp::sptr data_transport = transport::udp::make( + device_addr["addr"], num2str(USRP2_UDP_DATA_PORT) ); //create the usrp2 implementation guts @@ -99,8 +96,8 @@ device::sptr usrp2::make(const device_addr_t &device_addr){ * Structors **********************************************************************/ usrp2_impl::usrp2_impl( - uhd::transport::udp::sptr ctrl_transport, - uhd::transport::udp::sptr data_transport + transport::udp::sptr ctrl_transport, + transport::udp::sptr data_transport ){ _ctrl_transport = ctrl_transport; _data_transport = data_transport; @@ -121,9 +118,6 @@ usrp2_impl::usrp2_impl( //init the mboard mboard_init(); - //init the tx and rx dboards - dboard_init(); - //init the ddc init_ddc_config(); @@ -132,6 +126,10 @@ usrp2_impl::usrp2_impl( //initialize the clock configuration init_clock_config(); + + //init the tx and rx dboards (do last) + dboard_init(); + } usrp2_impl::~usrp2_impl(void){ |