summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gr-blocks/grc/blocks_udp_source.xml79
-rw-r--r--gr-blocks/include/blocks/udp_source.h84
-rw-r--r--gr-blocks/lib/udp_source_impl.cc199
-rw-r--r--gr-blocks/lib/udp_source_impl.h84
4 files changed, 446 insertions, 0 deletions
diff --git a/gr-blocks/grc/blocks_udp_source.xml b/gr-blocks/grc/blocks_udp_source.xml
new file mode 100644
index 0000000000..bca1dff8b8
--- /dev/null
+++ b/gr-blocks/grc/blocks_udp_source.xml
@@ -0,0 +1,79 @@
+<?xml version="1.0"?>
+<!--
+###################################################
+##UDP Source
+###################################################
+ -->
+<block>
+ <name>UDP Source</name>
+ <key>blocks_udp_source</key>
+ <import>from gnuradio import blocks</import>
+ <make>blocks.udp_source($type.size*$vlen, $ipaddr, $port, $psize, $eof)</make>
+ <callback>set_mtu($mtu)</callback>
+ <param>
+ <name>Output Type</name>
+ <key>type</key>
+ <type>enum</type>
+ <option>
+ <name>Complex</name>
+ <key>complex</key>
+ <opt>size:gr.sizeof_gr_complex</opt>
+ </option>
+ <option>
+ <name>Float</name>
+ <key>float</key>
+ <opt>size:gr.sizeof_float</opt>
+ </option>
+ <option>
+ <name>Int</name>
+ <key>int</key>
+ <opt>size:gr.sizeof_int</opt>
+ </option>
+ <option>
+ <name>Short</name>
+ <key>short</key>
+ <opt>size:gr.sizeof_short</opt>
+ </option>
+ <option>
+ <name>Byte</name>
+ <key>byte</key>
+ <opt>size:gr.sizeof_char</opt>
+ </option>
+ </param>
+ <param>
+ <name>IP Address</name>
+ <key>ipaddr</key>
+ <value>127.0.0.1</value>
+ <type>string</type>
+ </param>
+ <param>
+ <name>Port</name>
+ <key>port</key>
+ <value>1234</value>
+ <type>int</type>
+ </param>
+ <param>
+ <name>Payload Size</name>
+ <key>psize</key>
+ <value>1472</value>
+ <type>int</type>
+ </param>
+ <param>
+ <name>Null Pkt is EOF</name>
+ <key>eof</key>
+ <value>True</value>
+ <type>bool</type>
+ </param>
+ <param>
+ <name>Vec Length</name>
+ <key>vlen</key>
+ <value>1</value>
+ <type>int</type>
+ </param>
+ <check>$vlen &gt; 0</check>
+ <source>
+ <name>out</name>
+ <type>$type</type>
+ <vlen>$vlen</vlen>
+ </source>
+</block>
diff --git a/gr-blocks/include/blocks/udp_source.h b/gr-blocks/include/blocks/udp_source.h
new file mode 100644
index 0000000000..b72db30db0
--- /dev/null
+++ b/gr-blocks/include/blocks/udp_source.h
@@ -0,0 +1,84 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007-2010,2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_GR_UDP_SOURCE_H
+#define INCLUDED_GR_UDP_SOURCE_H
+
+#include <blocks/api.h>
+#include <gr_sync_block.h>
+
+namespace gr {
+ namespace blocks {
+
+ /*!
+ * \brief Read stream from an UDP socket.
+ * \ingroup source_blk
+ */
+ class BLOCKS_API udp_source : virtual public gr_sync_block
+ {
+ public:
+ // gr::blocks::udp_source::sptr
+ typedef boost::shared_ptr<udp_source> sptr;
+
+ /*!
+ * \brief UDP Source Constructor
+ *
+ * \param itemsize The size (in bytes) of the item datatype
+ * \param host The name or IP address of the receiving host; can be
+ * NULL, None, or "0.0.0.0" to allow reading from any
+ * interface on the host
+ * \param port The port number on which to receive data; use 0 to
+ * have the system assign an unused port number
+ * \param payload_size UDP payload size by default set to 1472 =
+ * (1500 MTU - (8 byte UDP header) - (20 byte IP header))
+ * \param eof Interpret zero-length packet as EOF (default: true)
+ */
+ static sptr make(size_t itemsize,
+ const std::string &host, int port,
+ int payload_size=1472,
+ bool eof=true);
+
+ /*! \brief Change the connection to a new destination
+ *
+ * \param host The name or IP address of the receiving host; use
+ * NULL or None to break the connection without closing
+ * \param port Destination port to connect to on receiving host
+ *
+ * Calls disconnect() to terminate any current connection first.
+ */
+ virtual void connect(const std::string &host, int port) = 0;
+
+ /*! \brief Cut the connection if we have one set up.
+ */
+ virtual void disconnect() = 0;
+
+ /*! \brief return the PAYLOAD_SIZE of the socket */
+ virtual int payload_size() = 0;
+
+ /*! \brief return the port number of the socket */
+ virtual int get_port() = 0;
+ };
+
+ } /* namespace blocks */
+} /* namespace gr */
+
+#endif /* INCLUDED_GR_UDP_SOURCE_H */
diff --git a/gr-blocks/lib/udp_source_impl.cc b/gr-blocks/lib/udp_source_impl.cc
new file mode 100644
index 0000000000..b8847586be
--- /dev/null
+++ b/gr-blocks/lib/udp_source_impl.cc
@@ -0,0 +1,199 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007-2010,2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "udp_source_impl.h"
+#include <gr_io_signature.h>
+#include <gr_math.h>
+#include <stdexcept>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+namespace gr {
+ namespace blocks {
+
+ udp_source::sptr
+ udp_source::make(size_t itemsize,
+ const std::string &ipaddr, int port,
+ int payload_size, bool eof)
+ {
+ return gnuradio::get_initial_sptr
+ (new udp_source_impl(itemsize, ipaddr, port,
+ payload_size, eof));
+ }
+
+ udp_source_impl::udp_source_impl(size_t itemsize,
+ const std::string &host, int port,
+ int payload_size, bool eof)
+ : gr_sync_block("udp_source",
+ gr_make_io_signature(0, 0, 0),
+ gr_make_io_signature(1, 1, itemsize)),
+ d_itemsize(itemsize), d_payload_size(payload_size),
+ d_eof(eof), d_connected(false), d_residual(0), d_sent(0), d_offset(0)
+ {
+ // Give us some more room to play.
+ d_rxbuf = new char[4*d_payload_size];
+ d_residbuf = new char[50*d_payload_size];
+
+ connect(host, port);
+ }
+
+ udp_source_impl::~udp_source_impl()
+ {
+ if(d_connected)
+ disconnect();
+
+ delete [] d_rxbuf;
+ delete [] d_residbuf;
+ }
+
+ void
+ udp_source_impl::connect(const std::string &host, int port)
+ {
+ if(d_connected)
+ disconnect();
+
+ d_host = host;
+ d_port = static_cast<unsigned short>(port);
+
+ if(host.size() > 0) {
+ d_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), d_port);
+ d_socket = new boost::asio::ip::udp::socket(d_io_service, d_endpoint);
+
+ boost::asio::socket_base::linger loption(true, 0);
+ d_socket->set_option(loption);
+
+ boost::asio::socket_base::reuse_address roption(true);
+ d_socket->set_option(roption);
+
+ start_receive();
+ d_udp_thread = gruel::thread(boost::bind(&udp_source_impl::run_io_service, this));
+ d_connected = true;
+ }
+ }
+
+ void
+ udp_source_impl::disconnect()
+ {
+ gruel::scoped_lock lock(d_setlock);
+
+ if(!d_connected)
+ return;
+
+ d_io_service.stop();
+
+ d_socket->close();
+ delete d_socket;
+
+ d_connected = false;
+ }
+
+ // Return port number of d_socket
+ int
+ udp_source_impl::get_port(void)
+ {
+ return d_endpoint.port();
+ }
+
+ void
+ udp_source_impl::start_receive()
+ {
+ d_socket->async_receive_from(boost::asio::buffer((void*)d_rxbuf, d_payload_size), d_endpoint_rcvd,
+ boost::bind(&udp_source_impl::handle_read, this,
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
+
+ void
+ udp_source_impl::handle_read(const boost::system::error_code& error,
+ size_t bytes_transferred)
+ {
+ if(!error) {
+ {
+ boost::lock_guard<gruel::mutex> lock(d_udp_mutex);
+ if(d_eof && (bytes_transferred == 1) && (d_rxbuf[0] == 0x00)) {
+ // If we are using EOF notification, test for it and don't
+ // add anything to the output.
+ d_residual = 0;
+ }
+ else {
+ // Make sure we never go beyond the boundary of the
+ // residual buffer. This will just drop the last bit of
+ // data in the buffer if we've run out of room.
+ if(d_residual + bytes_transferred > 50*d_payload_size) {
+ GR_LOG_WARN(d_logger, "Too much data; dropping packet.");
+ }
+ else {
+ // otherwise, copy receid data into local buffer for
+ // copying later.
+ memcpy(d_residbuf+d_residual, d_rxbuf, bytes_transferred);
+ d_residual += bytes_transferred;
+ }
+ }
+ d_cond_wait.notify_one();
+ }
+ }
+ start_receive();
+ }
+
+ int
+ udp_source_impl::work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items)
+ {
+ gruel::scoped_lock l(d_setlock);
+
+ char *out = (char*)output_items[0];
+
+ // Use async receive_from to get data from UDP buffer and wait
+ // on a conditional signal before proceeding. We use this
+ // because the conditional wait is interruptable while a
+ // synchronous receive_from is not.
+ boost::unique_lock<boost::mutex> lock(d_udp_mutex);
+ d_cond_wait.wait(lock);
+
+ int to_be_sent = (int)(d_residual - d_sent);
+ int to_send = std::min(noutput_items, to_be_sent);
+
+ // Copy the received data in the residual buffer to the output stream
+ memcpy(out, d_residbuf+d_sent, to_send);
+ int nitems = to_send/d_itemsize;
+
+ // Keep track of where we are if we don't have enough output
+ // space to send all the data in the residbuf.
+ if(to_send == to_be_sent) {
+ d_residual = 0;
+ d_sent = 0;
+ }
+ else {
+ d_sent += to_send;
+ }
+
+ return nitems;
+ }
+
+ } /* namespace blocks */
+} /* namespace gr */
diff --git a/gr-blocks/lib/udp_source_impl.h b/gr-blocks/lib/udp_source_impl.h
new file mode 100644
index 0000000000..8927f5f933
--- /dev/null
+++ b/gr-blocks/lib/udp_source_impl.h
@@ -0,0 +1,84 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007-2010,2013 Free Software Foundation, Inc.
+ *
+ * This file is part of GNU Radio
+ *
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ *
+ * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_GR_UDP_SOURCE_IMPL_H
+#define INCLUDED_GR_UDP_SOURCE_IMPL_H
+
+#include <blocks/udp_source.h>
+#include <boost/asio.hpp>
+#include <boost/format.hpp>
+#include <gruel/thread.h>
+
+namespace gr {
+ namespace blocks {
+
+ class udp_source_impl : public udp_source
+ {
+ private:
+ size_t d_itemsize;
+ int d_payload_size; // maximum transmission unit (packet length)
+ bool d_eof; // look for an EOF signal
+ bool d_connected; // are we connected?
+ char *d_rxbuf; // get UDP buffer items
+ char *d_residbuf; // hold buffer between calls
+ ssize_t d_residual; // hold information about number of bytes stored in residbuf
+ ssize_t d_sent; // track how much of d_residbuf we've outputted
+ size_t d_offset; // point to residbuf location offset
+
+ std::string d_host;
+ unsigned short d_port;
+
+ boost::asio::ip::udp::socket *d_socket;
+ boost::asio::ip::udp::endpoint d_endpoint;
+ boost::asio::ip::udp::endpoint d_endpoint_rcvd;
+ boost::asio::io_service d_io_service;
+
+ gruel::condition_variable d_cond_wait;
+ gruel::mutex d_udp_mutex;
+ gruel::thread d_udp_thread;
+
+ void start_receive();
+ void handle_read(const boost::system::error_code& error,
+ size_t bytes_transferred);
+ void run_io_service() { d_io_service.run(); }
+
+ public:
+ udp_source_impl(size_t itemsize,
+ const std::string &host, int port,
+ int payload_size, bool eof);
+ ~udp_source_impl();
+
+ void connect(const std::string &host, int port);
+ void disconnect();
+
+ int payload_size() { return d_payload_size; }
+ int get_port();
+
+ int work(int noutput_items,
+ gr_vector_const_void_star &input_items,
+ gr_vector_void_star &output_items);
+ };
+
+ } /* namespace blocks */
+} /* namespace gr */
+
+#endif /* INCLUDED_GR_UDP_SOURCE_H */