diff options
author | Josh Blum <josh@joshknows.com> | 2011-09-09 13:39:52 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2011-09-28 10:32:05 -0700 |
commit | 25494489bf8b7c60875ea355d29323bcfffd604b (patch) | |
tree | bbb533238db6fc24d807670c2adf23d59de9160a /host/lib/transport | |
parent | 18a3f03c06c65d79e5c4b7ac1076077c4b9fd8ff (diff) | |
download | uhd-25494489bf8b7c60875ea355d29323bcfffd604b.tar.gz uhd-25494489bf8b7c60875ea355d29323bcfffd604b.tar.bz2 uhd-25494489bf8b7c60875ea355d29323bcfffd604b.zip |
usrp2: uart/udp work in host and fw, working
Diffstat (limited to 'host/lib/transport')
-rw-r--r-- | host/lib/transport/udp_simple.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/host/lib/transport/udp_simple.cpp b/host/lib/transport/udp_simple.cpp index ed778fbf4..bc1bdaf2f 100644 --- a/host/lib/transport/udp_simple.cpp +++ b/host/lib/transport/udp_simple.cpp @@ -81,3 +81,49 @@ udp_simple::sptr udp_simple::make_broadcast( ){ return sptr(new udp_simple_impl(addr, port, true, false /* bcast, no connect */)); } + +/*********************************************************************** + * Simple UART over UDP + **********************************************************************/ +#include <boost/thread/thread.hpp> +class udp_simple_uart_impl : public uhd::uart_iface{ +public: + udp_simple_uart_impl(udp_simple::sptr udp){ + _udp = udp; + _len = 0; + _off = 0; + this->write_uart(""); //send an empty packet to init + } + + void write_uart(const std::string &buf){ + _udp->send(asio::buffer(buf)); + } + + std::string read_uart(double timeout){ + std::string line; + const boost::system_time exit_time = boost::get_system_time() + boost::posix_time::milliseconds(long(timeout*1000)); + do{ + //drain anything in current buffer + while (_off < _len){ + const char ch = _buf[_off]; _off++; + line += std::string(1, ch); + if (ch == '\n' or ch == '\r') return line; + } + + //recv a new packet into the buffer + _len = _udp->recv(asio::buffer(_buf), std::max((exit_time - boost::get_system_time()).total_milliseconds()/1000., 0.0)); + _off = 0; + + } while (_len != 0); + return line; + } + +private: + udp_simple::sptr _udp; + size_t _len, _off; + boost::uint8_t _buf[udp_simple::mtu]; +}; + +uhd::uart_iface::sptr udp_simple::make_uart(sptr udp){ + return uart_iface::sptr(new udp_simple_uart_impl(udp)); +} |