diff options
55 files changed, 3739 insertions, 6 deletions
diff --git a/config/Makefile.am b/config/Makefile.am index 4dc7216f1..959b8f014 100644 --- a/config/Makefile.am +++ b/config/Makefile.am @@ -70,6 +70,7 @@ m4macros = \ grc_gr_video_sdl.m4 \ grc_gr_wxgui.m4 \ grc_gruel.m4 \ + grc_vrt.m4 \ gr_check_createfilemapping.m4 \ gr_check_mc4020.m4 \ gr_check_shm_open.m4 \ diff --git a/config/grc_gr_uhd.m4 b/config/grc_gr_uhd.m4 new file mode 100644 index 000000000..870a8abc6 --- /dev/null +++ b/config/grc_gr_uhd.m4 @@ -0,0 +1,47 @@ +dnl Copyright 2010 Free Software Foundation, Inc. +dnl +dnl This file is part of GNU Radio +dnl +dnl GNU Radio is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 3, or (at your option) +dnl any later version. +dnl +dnl GNU Radio is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +dnl GNU General Public License for more details. +dnl +dnl You should have received a copy of the GNU General Public License +dnl along with GNU Radio; see the file COPYING. If not, write to +dnl the Free Software Foundation, Inc., 51 Franklin Street, +dnl Boston, MA 02110-1301, USA. + +AC_DEFUN([GRC_GR_UHD],[ + GRC_ENABLE(gr-uhd) + + dnl Dont do gr-uhd if gnuradio-core skipped + GRC_CHECK_DEPENDENCY(gr-uhd, gnuradio-core) + + if test $passed = yes; then + #by default, cmake likes to install the uhd.pc file here + #so lets make life easier for everybody and add this path + export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig + PKG_CHECK_MODULES( + [UHD], [uhd], [], + [passed=no; AC_MSG_RESULT([gr-uhd requires libuhd])] + ) + AC_SUBST(UHD_CFLAGS) + AC_SUBST(UHD_LIBS) + fi + + AC_CONFIG_FILES([ \ + gr-uhd/Makefile \ + gr-uhd/apps/Makefile \ + gr-uhd/grc/Makefile \ + gr-uhd/lib/Makefile \ + gr-uhd/swig/Makefile \ + ]) + + GRC_BUILD_CONDITIONAL(gr-uhd) +]) diff --git a/config/grc_vrt.m4 b/config/grc_vrt.m4 new file mode 100644 index 000000000..982c8863a --- /dev/null +++ b/config/grc_vrt.m4 @@ -0,0 +1,64 @@ +dnl Copyright 2008,2009 Free Software Foundation, Inc. +dnl +dnl This file is part of GNU Radio +dnl +dnl GNU Radio is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 3, or (at your option) +dnl any later version. +dnl +dnl GNU Radio is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +dnl GNU General Public License for more details. +dnl +dnl You should have received a copy of the GNU General Public License +dnl along with GNU Radio; see the file COPYING. If not, write to +dnl the Free Software Foundation, Inc., 51 Franklin Street, +dnl Boston, MA 02110-1301, USA. + +AC_DEFUN([GRC_VRT],[ + GRC_ENABLE(vrt) + + dnl If execution gets to here, $passed will be: + dnl with : if the --with code didn't error out + dnl yes : if the --enable code passed muster and all dependencies are met + dnl no : otherwise + if test $passed = yes; then + dnl Needed for vrt_socket_opener + AC_CHECK_HEADERS(arpa/inet.h byteswap.h linux/if_packet.h sys/socket.h sys/un.h) + fi + if test $passed != with; then + dnl how and where to find INCLUDES and LA + VRT_INCLUDES="-I\${abs_top_srcdir}/vrt/include" + VRT_LA="\${abs_top_builddir}/vrt/lib/libvrt.la" + fi + + # Test host OS compatibility + AC_MSG_CHECKING([whether host_os is linux*]) + case "$host_os" in + linux*) + AC_MSG_RESULT([yes]) + ;; + *) + AC_MSG_RESULT([no]) + AC_MSG_NOTICE([libvrt currently requires Linux host OS, not found]) + passed="no" + ;; + esac + + dnl Include the vrt INCLUDES and LA + AC_SUBST(VRT_INCLUDES) + AC_SUBST(VRT_LA) + + AC_CONFIG_FILES([ + vrt/Makefile + vrt/vrt.pc + vrt/include/Makefile + vrt/include/vrt/Makefile + vrt/lib/Makefile + vrt/apps/Makefile + ]) + + GRC_BUILD_CONDITIONAL(vrt) +]) diff --git a/configure.ac b/configure.ac index 19d193172..4ec2644ee 100644 --- a/configure.ac +++ b/configure.ac @@ -340,6 +340,7 @@ GRC_GCELL GRC_GNURADIO_CORE GRC_USRP GRC_USRP2 +GRC_VRT GRC_GR_USRP dnl this must come after GRC_USRP GRC_GR_USRP2 GRC_GR_GCELL dnl this must come after GRC_GCELL and GRC_GNURADIO_CORE @@ -367,6 +368,7 @@ GRC_GR_SOUNDER dnl this must come after GRC_USRP GRC_GR_UTILS dnl this must come after GRC_GR_WXGUI GRC_GNURADIO_EXAMPLES dnl must come after all GRC_GR_* GRC_GRC +GRC_GR_UHD GRC_DOCS dnl must be last # Each component is now either to be built, was skipped, will be diff --git a/gr-howto-write-a-block/version.sh b/gr-howto-write-a-block/version.sh index c18f46416..0d6cc96f0 100644 --- a/gr-howto-write-a-block/version.sh +++ b/gr-howto-write-a-block/version.sh @@ -1,4 +1,4 @@ MAJOR_VERSION=3 -API_COMPAT=3 -MINOR_VERSION=0-rc3 +API_COMPAT=4 +MINOR_VERSION=git MAINT_VERSION=0 diff --git a/gr-uhd/.gitignore b/gr-uhd/.gitignore new file mode 100644 index 000000000..b336cc7ce --- /dev/null +++ b/gr-uhd/.gitignore @@ -0,0 +1,2 @@ +/Makefile +/Makefile.in diff --git a/gr-uhd/Makefile.am b/gr-uhd/Makefile.am new file mode 100644 index 000000000..83190a1d0 --- /dev/null +++ b/gr-uhd/Makefile.am @@ -0,0 +1,29 @@ +# +# Copyright 2010 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. +# + +include $(top_srcdir)/Makefile.common + +SUBDIRS = lib apps + +if PYTHON +SUBDIRS += swig grc +endif + diff --git a/gr-uhd/apps/.gitignore b/gr-uhd/apps/.gitignore new file mode 100644 index 000000000..b336cc7ce --- /dev/null +++ b/gr-uhd/apps/.gitignore @@ -0,0 +1,2 @@ +/Makefile +/Makefile.in diff --git a/gr-uhd/apps/Makefile.am b/gr-uhd/apps/Makefile.am new file mode 100644 index 000000000..b78d07ee8 --- /dev/null +++ b/gr-uhd/apps/Makefile.am @@ -0,0 +1,22 @@ +# +# Copyright 2010 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. +# + +include $(top_srcdir)/Makefile.common diff --git a/gr-uhd/grc/.gitignore b/gr-uhd/grc/.gitignore new file mode 100644 index 000000000..b336cc7ce --- /dev/null +++ b/gr-uhd/grc/.gitignore @@ -0,0 +1,2 @@ +/Makefile +/Makefile.in diff --git a/gr-uhd/grc/Makefile.am b/gr-uhd/grc/Makefile.am new file mode 100644 index 000000000..61eb32770 --- /dev/null +++ b/gr-uhd/grc/Makefile.am @@ -0,0 +1,28 @@ +# +# Copyright 2010 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. +# + +include $(top_srcdir)/Makefile.common + +grcblocksdir = $(grc_blocksdir) + +dist_grcblocks_DATA = \ + uhd_simple_source.xml \ + uhd_simple_sink.xml diff --git a/gr-uhd/grc/uhd_simple_sink.xml b/gr-uhd/grc/uhd_simple_sink.xml new file mode 100644 index 000000000..2992f650e --- /dev/null +++ b/gr-uhd/grc/uhd_simple_sink.xml @@ -0,0 +1,93 @@ +<?xml version="1.0"?> +<!-- +################################################### +## UHD Simple Sink +################################################### + --> +<block> + <name>UHD Simple Sink</name> + <key>uhd_simple_sink</key> + <category>UHD</category> + <import>from gnuradio import uhd</import> + <make>uhd.simple_sink($args, uhd.io_type_t.$type.type) +self.$(id).set_samp_rate($samp_rate) +self.$(id).set_center_freq($center_freq) +self.$(id).set_gain($gain) +#if $ant() +self.$(id).set_antenna($ant) +#end if</make> + <callback>set_samp_rate($samp_rate)</callback> + <callback>set_center_freq($center_freq)</callback> + <callback>set_gain($gain)</callback> + <callback>set_antenna($ant)</callback> + <param> + <name>Input Type</name> + <key>type</key> + <type>enum</type> + <option> + <name>Complex</name> + <key>complex</key> + <opt>type:COMPLEX_FLOAT32</opt> + <opt>vlen:1</opt> + </option> + <option> + <name>Short</name> + <key>short</key> + <opt>type:COMPLEX_INT16</opt> + <opt>vlen:2</opt> + </option> + </param> + <param> + <name>Args</name> + <key>args</key> + <value></value> + <type>string</type> + </param> + <param> + <name>Samp Rate (Sps)</name> + <key>samp_rate</key> + <value>samp_rate</value> + <type>real</type> + </param> + <param> + <name>Center Freq (Hz)</name> + <key>center_freq</key> + <type>real</type> + </param> + <param> + <name>Gain (dB)</name> + <key>gain</key> + <value>0</value> + <type>real</type> + </param> + <param> + <name>Antenna</name> + <key>ant</key> + <type>string</type> + <hide>#if $ant() == '' then 'part' else 'none'#</hide> + </param> + <sink> + <name>out</name> + <type>$type</type> + <vlen>$type.vlen</vlen> + </sink> + <doc> +The UHD Sink Block: + +Args: +Args is a delimited string used to locate UHD devices on your system. \ +If left blank, the first UHD device found will be used. \ +Used args to specify a specfic device. \ +USRP2 Example: addr=192.168.10.2 + +Sample rate: +The sample rate is the number of samples per second input by this block. \ +The UHD device driver will try its best to match the requested sample rate. \ +If the requested rate is not possible, the UHD block will print an error at runtime. + +Antenna: +For subdevices/daughterboards with only one antenna, this may be left blank. \ +Otherwise, the user should specify one of the possible antenna choices. \ +See the daughterboard application notes for the possible antenna choices. + </doc> +</block> diff --git a/gr-uhd/grc/uhd_simple_source.xml b/gr-uhd/grc/uhd_simple_source.xml new file mode 100644 index 000000000..ecd963928 --- /dev/null +++ b/gr-uhd/grc/uhd_simple_source.xml @@ -0,0 +1,93 @@ +<?xml version="1.0"?> +<!-- +################################################### +## UHD Simple Source +################################################### + --> +<block> + <name>UHD Simple Source</name> + <key>uhd_simple_source</key> + <category>UHD</category> + <import>from gnuradio import uhd</import> + <make>uhd.simple_source($args, uhd.io_type_t.$type.type) +self.$(id).set_samp_rate($samp_rate) +self.$(id).set_center_freq($center_freq) +self.$(id).set_gain($gain) +#if $ant() +self.$(id).set_antenna($ant) +#end if</make> + <callback>set_samp_rate($samp_rate)</callback> + <callback>set_center_freq($center_freq)</callback> + <callback>set_gain($gain)</callback> + <callback>set_antenna($ant)</callback> + <param> + <name>Output Type</name> + <key>type</key> + <type>enum</type> + <option> + <name>Complex</name> + <key>complex</key> + <opt>type:COMPLEX_FLOAT32</opt> + <opt>vlen:1</opt> + </option> + <option> + <name>Short</name> + <key>short</key> + <opt>type:COMPLEX_INT16</opt> + <opt>vlen:2</opt> + </option> + </param> + <param> + <name>Args</name> + <key>args</key> + <value></value> + <type>string</type> + </param> + <param> + <name>Samp Rate (Sps)</name> + <key>samp_rate</key> + <value>samp_rate</value> + <type>real</type> + </param> + <param> + <name>Center Freq (Hz)</name> + <key>center_freq</key> + <type>real</type> + </param> + <param> + <name>Gain (dB)</name> + <key>gain</key> + <value>0</value> + <type>real</type> + </param> + <param> + <name>Antenna</name> + <key>ant</key> + <type>string</type> + <hide>#if $ant() == '' then 'part' else 'none'#</hide> + </param> + <source> + <name>out</name> + <type>$type</type> + <vlen>$type.vlen</vlen> + </source> + <doc> +The UHD Source Block: + +Args: +Args is a delimited string used to locate UHD devices on your system. \ +If left blank, the first UHD device found will be used. \ +Used args to specify a specfic device. \ +USRP2 Example: addr=192.168.10.2 + +Sample rate: +The sample rate is the number of samples per second output by this block. \ +The UHD device driver will try its best to match the requested sample rate. \ +If the requested rate is not possible, the UHD block will print an error at runtime. + +Antenna: +For subdevices/daughterboards with only one antenna, this may be left blank. \ +Otherwise, the user should specify one of the possible antenna choices. \ +See the daughterboard application notes for the possible antenna choices. + </doc> +</block> diff --git a/gr-uhd/lib/.gitignore b/gr-uhd/lib/.gitignore new file mode 100644 index 000000000..b336cc7ce --- /dev/null +++ b/gr-uhd/lib/.gitignore @@ -0,0 +1,2 @@ +/Makefile +/Makefile.in diff --git a/gr-uhd/lib/Makefile.am b/gr-uhd/lib/Makefile.am new file mode 100644 index 000000000..2ac4cc2e6 --- /dev/null +++ b/gr-uhd/lib/Makefile.am @@ -0,0 +1,47 @@ +# +# Copyright 2010 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. +# + +include $(top_srcdir)/Makefile.common + +AM_CPPFLAGS = \ + $(STD_DEFINES_AND_INCLUDES) \ + $(WITH_INCLUDES) \ + $(UHD_CFLAGS) + +lib_LTLIBRARIES = libgnuradio-uhd.la + +libgnuradio_uhd_la_SOURCES = \ + utils.cc \ + uhd_simple_source.cc \ + uhd_simple_sink.cc + +libgnuradio_uhd_la_LIBADD = \ + $(GNURADIO_CORE_LA) \ + $(UHD_LIBS) + +libgnuradio_uhd_la_LDFLAGS = $(LTVERSIONFLAGS) + +grinclude_HEADERS = \ + uhd_simple_source.h \ + uhd_simple_sink.h + +noinst_HEADERS = \ + utils.h diff --git a/gr-uhd/lib/uhd_simple_sink.cc b/gr-uhd/lib/uhd_simple_sink.cc new file mode 100644 index 000000000..ccec0e29c --- /dev/null +++ b/gr-uhd/lib/uhd_simple_sink.cc @@ -0,0 +1,130 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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. + */ + +#include <uhd_simple_sink.h> +#include <gr_io_signature.h> +#include <stdexcept> +#include "utils.h" + +/*********************************************************************** + * UHD Sink + **********************************************************************/ +uhd_simple_sink::uhd_simple_sink(gr_io_signature_sptr sig) +:gr_sync_block("uhd sink", sig, gr_make_io_signature(0, 0, 0)){ + /* NOP */ +} + +/*********************************************************************** + * UHD Sink Impl + **********************************************************************/ +class uhd_simple_sink_impl : public uhd_simple_sink{ +public: + uhd_simple_sink_impl( + const std::string &args, + const uhd::io_type_t &type + ) : uhd_simple_sink(gr_make_io_signature(1, 1, type.size)), _type(type) + { + _dev = uhd::usrp::simple_usrp::make(args); + } + + ~uhd_simple_sink_impl(void){ + //NOP + } + + void set_samp_rate(double rate){ + _dev->set_tx_rate(rate); + do_samp_rate_error_message(rate, get_samp_rate()); + } + + double get_samp_rate(void){ + return _dev->get_tx_rate(); + } + + uhd::tune_result_t set_center_freq(double freq){ + return _dev->set_tx_freq(freq); + } + + uhd::freq_range_t get_freq_range(void){ + return _dev->get_tx_freq_range(); + } + + void set_gain(float gain){ + return _dev->set_tx_gain(gain); + } + + float get_gain(void){ + return _dev->get_tx_gain(); + } + + uhd::gain_range_t get_gain_range(void){ + return _dev->get_tx_gain_range(); + } + + void set_antenna(const std::string &ant){ + return _dev->set_tx_antenna(ant); + } + + std::string get_antenna(void){ + return _dev->get_tx_antenna(); + } + + std::vector<std::string> get_antennas(void){ + return _dev->get_tx_antennas(); + } + + uhd::usrp::simple_usrp::sptr get_device(void){ + return _dev; + } + +/*********************************************************************** + * Work + **********************************************************************/ + int work( + int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items + ){ + uhd::tx_metadata_t metadata; + metadata.start_of_burst = true; + + return _dev->get_device()->send( + boost::asio::buffer(input_items[0], noutput_items*_type.size), + metadata, _type, uhd::device::SEND_MODE_FULL_BUFF + ); + } + +protected: + uhd::usrp::simple_usrp::sptr _dev; + const uhd::io_type_t _type; +}; + +/*********************************************************************** + * Make UHD Sink + **********************************************************************/ +boost::shared_ptr<uhd_simple_sink> uhd_make_simple_sink( + const std::string &args, + const uhd::io_type_t::tid_t &type +){ + return boost::shared_ptr<uhd_simple_sink>( + new uhd_simple_sink_impl(args, type) + ); +} diff --git a/gr-uhd/lib/uhd_simple_sink.h b/gr-uhd/lib/uhd_simple_sink.h new file mode 100644 index 000000000..9ecf51aea --- /dev/null +++ b/gr-uhd/lib/uhd_simple_sink.h @@ -0,0 +1,114 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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_UHD_SIMPLE_SINK_H +#define INCLUDED_UHD_SIMPLE_SINK_H + +#include <gr_sync_block.h> +#include <uhd/usrp/simple_usrp.hpp> + +class uhd_simple_sink; + +boost::shared_ptr<uhd_simple_sink> uhd_make_simple_sink( + const std::string &args, + const uhd::io_type_t::tid_t &type +); + +class uhd_simple_sink : public gr_sync_block{ +public: + + /*! + * Set the IO signature for this block. + * \param sig the input signature + */ + uhd_simple_sink(gr_io_signature_sptr sig); + + /*! + * Set the sample rate for the usrp device. + * \param rate a new rate in Sps + */ + virtual void set_samp_rate(double rate) = 0; + + /*! + * Get the sample rate for the usrp device. + * This is the actual sample rate and may differ from the rate set. + * \return the actual rate in Sps + */ + virtual double get_samp_rate(void) = 0; + + /*! + * Tune the usrp device to the desired center frequency. + * \param freq the desired frequency in Hz + * \return a tune result with the actual frequencies + */ + virtual uhd::tune_result_t set_center_freq(double freq) = 0; + + /*! + * Get the tunable frequency range. + * \return the frequency range in Hz + */ + virtual uhd::freq_range_t get_freq_range(void) = 0; + + /*! + * Set the gain for the dboard. + * \param gain the gain in dB + */ + virtual void set_gain(float gain) = 0; + + /*! + * Get the actual dboard gain setting. + * \return the actual gain in dB + */ + virtual float get_gain(void) = 0; + + /*! + * Get the settable gain range. + * \return the gain range in dB + */ + virtual uhd::gain_range_t get_gain_range(void) = 0; + + /*! + * Set the antenna to use. + * \param ant the antenna string + */ + virtual void set_antenna(const std::string &ant) = 0; + + /*! + * Get the antenna in use. + * \return the antenna string + */ + virtual std::string get_antenna(void) = 0; + + /*! + * Get a list of possible antennas. + * \return a vector of antenna strings + */ + virtual std::vector<std::string> get_antennas(void) = 0; + + /*! + * Get access to the underlying uhd device object. + * \return the simple usrp device object + */ + virtual uhd::usrp::simple_usrp::sptr get_device(void) = 0; +}; + +#endif /* INCLUDED_UHD_SIMPLE_SINK_H */ diff --git a/gr-uhd/lib/uhd_simple_source.cc b/gr-uhd/lib/uhd_simple_source.cc new file mode 100644 index 000000000..76179c326 --- /dev/null +++ b/gr-uhd/lib/uhd_simple_source.cc @@ -0,0 +1,144 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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. + */ + +#include <uhd_simple_source.h> +#include <gr_io_signature.h> +#include <stdexcept> +#include "utils.h" + +/*********************************************************************** + * UHD Source + **********************************************************************/ +uhd_simple_source::uhd_simple_source(gr_io_signature_sptr sig) +:gr_sync_block("uhd source", gr_make_io_signature(0, 0, 0), sig){ + /* NOP */ +} + +/*********************************************************************** + * UHD Source Impl + **********************************************************************/ +class uhd_simple_source_impl : public uhd_simple_source{ +public: + uhd_simple_source_impl( + const std::string &args, + const uhd::io_type_t &type + ) : uhd_simple_source(gr_make_io_signature(1, 1, type.size)), _type(type) + { + _dev = uhd::usrp::simple_usrp::make(args); + set_streaming(false); + } + + ~uhd_simple_source_impl(void){ + set_streaming(false); + } + + void set_samp_rate(double rate){ + _dev->set_rx_rate(rate); + do_samp_rate_error_message(rate, get_samp_rate()); + } + + double get_samp_rate(void){ + return _dev->get_rx_rate(); + } + + uhd::tune_result_t set_center_freq(double freq){ + return _dev->set_rx_freq(freq); + } + + uhd::freq_range_t get_freq_range(void){ + return _dev->get_rx_freq_range(); + } + + void set_gain(float gain){ + return _dev->set_rx_gain(gain); + } + + float get_gain(void){ + return _dev->get_rx_gain(); + } + + uhd::gain_range_t get_gain_range(void){ + return _dev->get_rx_gain_range(); + } + + void set_antenna(const std::string &ant){ + return _dev->set_rx_antenna(ant); + } + + std::string get_antenna(void){ + return _dev->get_rx_antenna(); + } + + std::vector<std::string> get_antennas(void){ + return _dev->get_rx_antennas(); + } + + uhd::usrp::simple_usrp::sptr get_device(void){ + return _dev; + } + +/*********************************************************************** + * Work + **********************************************************************/ + int work( + int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items + ){ + //conditionally start streaming in the work call + //this prevents streaming before the runtime is ready + if (not _is_streaming) set_streaming(true); + + uhd::rx_metadata_t metadata; //not passed out of this block + + return _dev->get_device()->recv( + boost::asio::buffer(output_items[0], noutput_items*_type.size), + metadata, _type, uhd::device::RECV_MODE_FULL_BUFF + ); + } + +private: + uhd::usrp::simple_usrp::sptr _dev; + const uhd::io_type_t _type; + bool _is_streaming; + + void set_streaming(bool enb){ + if (enb) + _dev->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_START_CONTINUOUS); + else + _dev->issue_stream_cmd(uhd::stream_cmd_t::STREAM_MODE_STOP_CONTINUOUS); + _is_streaming = enb; + } +}; + + +/*********************************************************************** + * Make UHD Source + **********************************************************************/ +boost::shared_ptr<uhd_simple_source> uhd_make_simple_source( + const std::string &args, + const uhd::io_type_t::tid_t &type +){ + return boost::shared_ptr<uhd_simple_source>( + new uhd_simple_source_impl(args, type) + ); +} diff --git a/gr-uhd/lib/uhd_simple_source.h b/gr-uhd/lib/uhd_simple_source.h new file mode 100644 index 000000000..480bb4d3f --- /dev/null +++ b/gr-uhd/lib/uhd_simple_source.h @@ -0,0 +1,114 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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_UHD_SIMPLE_SOURCE_H +#define INCLUDED_UHD_SIMPLE_SOURCE_H + +#include <gr_sync_block.h> +#include <uhd/usrp/simple_usrp.hpp> + +class uhd_simple_source; + +boost::shared_ptr<uhd_simple_source> uhd_make_simple_source( + const std::string &args, + const uhd::io_type_t::tid_t &type +); + +class uhd_simple_source : public gr_sync_block{ +public: + + /*! + * Set the IO signature for this block. + * \param sig the output signature + */ + uhd_simple_source(gr_io_signature_sptr sig); + + /*! + * Set the sample rate for the usrp device. + * \param rate a new rate in Sps + */ + virtual void set_samp_rate(double rate) = 0; + + /*! + * Get the sample rate for the usrp device. + * This is the actual sample rate and may differ from the rate set. + * \return the actual rate in Sps + */ + virtual double get_samp_rate(void) = 0; + + /*! + * Tune the usrp device to the desired center frequency. + * \param freq the desired frequency in Hz + * \return a tune result with the actual frequencies + */ + virtual uhd::tune_result_t set_center_freq(double freq) = 0; + + /*! + * Get the tunable frequency range. + * \return the frequency range in Hz + */ + virtual uhd::freq_range_t get_freq_range(void) = 0; + + /*! + * Set the gain for the dboard. + * \param gain the gain in dB + */ + virtual void set_gain(float gain) = 0; + + /*! + * Get the actual dboard gain setting. + * \return the actual gain in dB + */ + virtual float get_gain(void) = 0; + + /*! + * Get the settable gain range. + * \return the gain range in dB + */ + virtual uhd::gain_range_t get_gain_range(void) = 0; + + /*! + * Set the antenna to use. + * \param ant the antenna string + */ + virtual void set_antenna(const std::string &ant) = 0; + + /*! + * Get the antenna in use. + * \return the antenna string + */ + virtual std::string get_antenna(void) = 0; + + /*! + * Get a list of possible antennas. + * \return a vector of antenna strings + */ + virtual std::vector<std::string> get_antennas(void) = 0; + + /*! + * Get access to the underlying uhd device object. + * \return the simple usrp device object + */ + virtual uhd::usrp::simple_usrp::sptr get_device(void) = 0; +}; + +#endif /* INCLUDED_UHD_SIMPLE_SOURCE_H */ diff --git a/gr-uhd/lib/utils.cc b/gr-uhd/lib/utils.cc new file mode 100644 index 000000000..ddc36eb99 --- /dev/null +++ b/gr-uhd/lib/utils.cc @@ -0,0 +1,37 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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. + */ + +#include "utils.h" //local include +#include <boost/format.hpp> +#include <iostream> +#include <cmath> + +void do_samp_rate_error_message(double target_rate, double actual_rate){ + static const double max_allowed_error = 1.0; //Sps + if (std::abs(target_rate - actual_rate) > max_allowed_error){ + std::cerr << boost::format( + "The hardware does not support the requested sample rate:\n" + " Target sample rate: %f MSps\n" + " Actual sample rate: %f MSps\n" + ) % (target_rate/1e6) % (actual_rate/1e6) << std::endl; + } +} diff --git a/gr-uhd/lib/utils.h b/gr-uhd/lib/utils.h new file mode 100644 index 000000000..cf349e5aa --- /dev/null +++ b/gr-uhd/lib/utils.h @@ -0,0 +1,28 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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_NOINST_UTILS_H +#define INCLUDED_NOINST_UTILS_H + +void do_samp_rate_error_message(double target_rate, double actual_rate); + +#endif /* INCLUDED_NOINST_UTILS_H */ diff --git a/gr-uhd/swig/.gitignore b/gr-uhd/swig/.gitignore new file mode 100644 index 000000000..02d25cc00 --- /dev/null +++ b/gr-uhd/swig/.gitignore @@ -0,0 +1,4 @@ +/uhd_swig.cc +/uhd_swig.py +/Makefile +/Makefile.in diff --git a/gr-uhd/swig/Makefile.am b/gr-uhd/swig/Makefile.am new file mode 100644 index 000000000..183e6b587 --- /dev/null +++ b/gr-uhd/swig/Makefile.am @@ -0,0 +1,69 @@ +# +# Copyright 2010 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. +# + +include $(top_srcdir)/Makefile.common + +AM_CPPFLAGS = \ + $(STD_DEFINES_AND_INCLUDES) \ + $(PYTHON_CPPFLAGS) \ + $(WITH_INCLUDES) \ + $(UHD_CFLAGS) \ + -I$(top_srcdir)/gr-uhd/lib + +if PYTHON +# ---------------------------------------------------------------- +# The SWIG library +# TESTS = run_tests + +TOP_SWIG_IFILES = \ + uhd_swig.i + +# Install so that they end up available as: +# import gnuradio.uhd +# This ends up at: +# ${prefix}/lib/python${python_version}/site-packages/gnuradio/uhd +uhd_swig_pythondir_category = \ + gnuradio/uhd + +# additional libraries for linking with the SWIG-generated library +uhd_swig_la_swig_libadd = \ + $(top_builddir)/gr-uhd/lib/libgnuradio-uhd.la + +# additional Python files to be installed along with the SWIG-generated one +uhd_swig_python = \ + __init__.py + +# additional SWIG files to be installed +uhd_swig_swiginclude_headers = + +include $(top_srcdir)/Makefile.swig + +# FIXME better way to set this? +STD_SWIG_PYTHON_ARGS += \ + $(UHD_CFLAGS) \ + -I$(top_srcdir)/gr-uhd/lib + +# add some of the variables generated inside the Makefile.swig.gen +BUILT_SOURCES = $(swig_built_sources) + +# Do not distribute the output of SWIG +no_dist_files = $(swig_built_sources) +endif diff --git a/gr-uhd/swig/Makefile.swig.gen b/gr-uhd/swig/Makefile.swig.gen new file mode 100644 index 000000000..3804461a4 --- /dev/null +++ b/gr-uhd/swig/Makefile.swig.gen @@ -0,0 +1,259 @@ +# -*- Makefile -*- +# +# Copyright 2009 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. +# + +# Makefile.swig.gen for uhd_swig.i + +## Default install locations for these files: +## +## Default location for the Python directory is: +## ${prefix}/lib/python${python_version}/site-packages/[category]/uhd_swig +## Default location for the Python exec directory is: +## ${exec_prefix}/lib/python${python_version}/site-packages/[category]/uhd_swig +## +## The following can be overloaded to change the install location, but +## this has to be done in the including Makefile.am -before- +## Makefile.swig is included. + +uhd_swig_pythondir_category ?= gnuradio/uhd_swig +uhd_swig_pylibdir_category ?= $(uhd_swig_pythondir_category) +uhd_swig_pythondir = $(pythondir)/$(uhd_swig_pythondir_category) +uhd_swig_pylibdir = $(pyexecdir)/$(uhd_swig_pylibdir_category) + +## SWIG headers are always installed into the same directory. + +uhd_swig_swigincludedir = $(swigincludedir) + +## This is a template file for a "generated" Makefile addition (in +## this case, "Makefile.swig.gen"). By including the top-level +## Makefile.swig, this file will be used to generate the SWIG +## dependencies. Assign the variable TOP_SWIG_FILES to be the list of +## SWIG .i files to generated wrappings for; there can be more than 1 +## so long as the names are unique (no sorting is done on the +## TOP_SWIG_FILES list). This file explicitly assumes that a SWIG .i +## file will generate .cc, .py, and possibly .h files -- meaning that +## all of these files will have the same base name (that provided for +## the SWIG .i file). +## +## This code is setup to ensure parallel MAKE ("-j" or "-jN") does the +## right thing. For more info, see < +## http://sources.redhat.com/automake/automake.html#Multiple-Outputs > + +## Stamps used to ensure parallel make does the right thing. These +## are removed by "make clean", but otherwise unused except during the +## parallel built. These will not be included in a tarball, because +## the SWIG-generated files will be removed from the distribution. + +STAMPS += $(DEPDIR)/uhd_swig-generate-* + +## Other cleaned files: dependency files generated by SWIG or this Makefile + +MOSTLYCLEANFILES += $(DEPDIR)/*.S* + +## Add the .py and .cc files to the list of SWIG built sources. The +## .h file is sometimes built, but not always ... so that one has to +## be added manually by the including Makefile.am . + +swig_built_sources += uhd_swig.py uhd_swig.cc + +## Various SWIG variables. These can be overloaded in the including +## Makefile.am by setting the variable value there, then including +## Makefile.swig . + +uhd_swig_swiginclude_HEADERS = \ + uhd_swig.i \ + $(uhd_swig_swiginclude_headers) + +uhd_swig_pylib_LTLIBRARIES = \ + _uhd_swig.la + +_uhd_swig_la_SOURCES = \ + uhd_swig.cc \ + $(uhd_swig_la_swig_sources) + +_uhd_swig_la_LIBADD = \ + $(STD_SWIG_LA_LIB_ADD) \ + $(uhd_swig_la_swig_libadd) + +_uhd_swig_la_LDFLAGS = \ + $(STD_SWIG_LA_LD_FLAGS) \ + $(uhd_swig_la_swig_ldflags) + +_uhd_swig_la_CXXFLAGS = \ + $(STD_SWIG_CXX_FLAGS) \ + $(uhd_swig_la_swig_cxxflags) + +uhd_swig_python_PYTHON = \ + uhd_swig.py \ + $(uhd_swig_python) + +## Entry rule for running SWIG + +uhd_swig.h uhd_swig.py uhd_swig.cc: uhd_swig.i +## This rule will get called only when MAKE decides that one of the +## targets needs to be created or re-created, because: +## +## * The .i file is newer than any or all of the generated files; +## +## * Any or all of the .cc, .h, or .py files does not exist and is +## needed (in the case this file is not needed, the rule for it is +## ignored); or +## +## * Some SWIG-based dependecy of the .cc file isn't met and hence the +## .cc file needs be be regenerated. Explanation: Because MAKE +## knows how to handle dependencies for .cc files (regardless of +## their name or extension), then the .cc file is used as a target +## instead of the .i file -- but with the dependencies of the .i +## file. It is this last reason why the line: +## +## if test -f $@; then :; else +## +## cannot be used in this case: If a .i file dependecy is not met, +## then the .cc file needs to be rebuilt. But if the stamp is newer +## than the .cc file, and the .cc file exists, then in the original +## version (with the 'test' above) the internal MAKE call will not +## be issued and hence the .cc file will not be rebuilt. +## +## Once execution gets to here, it should always proceed no matter the +## state of a stamp (as discussed in link above). The +## $(DEPDIR)/uhd_swig-generate stuff is used to allow for parallel +## builds to "do the right thing". The stamp has no relationship with +## either the target files or dependency file; it is used solely for +## the protection of multiple builds during a given call to MAKE. +## +## Catch signals SIGHUP (1), SIGINT (2), SIGPIPE (13), and SIGTERM +## (15). At a caught signal, the quoted command will be issued before +## exiting. In this case, remove any stamp, whether temporary of not. +## The trap is valid until the process exits; the process includes all +## commands appended via "\"s. +## + trap 'rm -rf $(DEPDIR)/uhd_swig-generate-*' 1 2 13 15; \ +## +## Create a temporary directory, which acts as a lock. The first +## process to create the directory will succeed and issue the MAKE +## command to do the actual work, while all subsequent processes will +## fail -- leading them to wait for the first process to finish. +## + if mkdir $(DEPDIR)/uhd_swig-generate-lock 2>/dev/null; then \ +## +## This code is being executed by the first process to succeed in +## creating the directory lock. +## +## Remove the stamp associated with this filename. +## + rm -f $(DEPDIR)/uhd_swig-generate-stamp; \ +## +## Tell MAKE to run the rule for creating this stamp. +## + $(MAKE) $(AM_MAKEFLAGS) $(DEPDIR)/uhd_swig-generate-stamp WHAT=$<; \ +## +## Now that the .cc, .h, and .py files have been (re)created from the +## .i file, future checking of this rule during the same MAKE +## execution will come back that the rule doesn't need to be executed +## because none of the conditions mentioned at the start of this rule +## will be positive. Remove the the directory lock, which frees up +## any waiting process(es) to continue. +## + rmdir $(DEPDIR)/uhd_swig-generate-lock; \ + else \ +## +## This code is being executed by any follower processes while the +## directory lock is in place. +## +## Wait until the first process is done, testing once per second. +## + while test -d $(DEPDIR)/uhd_swig-generate-lock; do \ + sleep 1; \ + done; \ +## +## Succeed if and only if the first process succeeded; exit this +## process returning the status of the generated stamp. +## + test -f $(DEPDIR)/uhd_swig-generate-stamp; \ + exit $$?; \ + fi; + +$(DEPDIR)/uhd_swig-generate-stamp: +## This rule will be called only by the first process issuing the +## above rule to succeed in creating the lock directory, after +## removing the actual stamp file in order to guarantee that MAKE will +## execute this rule. +## +## Call SWIG to generate the various output files; special +## post-processing on 'mingw32' host OS for the dependency file. +## + if $(SWIG) $(STD_SWIG_PYTHON_ARGS) $(uhd_swig_swig_args) \ + -MD -MF $(DEPDIR)/uhd_swig.Std \ + -module uhd_swig -o uhd_swig.cc $(WHAT); then \ + if test $(host_os) = mingw32; then \ + $(RM) $(DEPDIR)/uhd_swig.Sd; \ + $(SED) 's,\\\\,/,g' < $(DEPDIR)/uhd_swig.Std \ + > $(DEPDIR)/uhd_swig.Sd; \ + $(RM) $(DEPDIR)/uhd_swig.Std; \ + $(MV) $(DEPDIR)/uhd_swig.Sd $(DEPDIR)/uhd_swig.Std; \ + fi; \ + else \ + $(RM) $(DEPDIR)/uhd_swig.S*; exit 1; \ + fi; +## +## Mess with the SWIG output .Std dependency file, to create a +## dependecy file valid for the input .i file: Basically, simulate the +## dependency file created for libraries by GNU's libtool for C++, +## where all of the dependencies for the target are first listed, then +## each individual dependency is listed as a target with no further +## dependencies. +## +## (1) remove the current dependency file +## + $(RM) $(DEPDIR)/uhd_swig.d +## +## (2) Copy the whole SWIG file: +## + cp $(DEPDIR)/uhd_swig.Std $(DEPDIR)/uhd_swig.d +## +## (3) all a carriage return to the end of the dependency file. +## + echo "" >> $(DEPDIR)/uhd_swig.d +## +## (4) from the SWIG file, remove the first line (the target); remove +## trailing " \" and " " from each line. Append ":" to each line, +## followed by 2 carriage returns, then append this to the end of +## the dependency file. +## + $(SED) -e '1d;s, \\,,g;s, ,,g' < $(DEPDIR)/uhd_swig.Std | \ + awk '{ printf "%s:\n\n", $$0 }' >> $(DEPDIR)/uhd_swig.d +## +## (5) remove the SWIG-generated file +## + $(RM) $(DEPDIR)/uhd_swig.Std +## +## Create the stamp for this filename generation, to signal success in +## executing this rule; allows other threads waiting on this process +## to continue. +## + touch $(DEPDIR)/uhd_swig-generate-stamp + +# KLUDGE: Force runtime include of a SWIG dependency file. This is +# not guaranteed to be portable, but will probably work. If it works, +# we have accurate dependencies for our swig stuff, which is good. + +@am__include@ @am__quote@./$(DEPDIR)/uhd_swig.d@am__quote@ + diff --git a/gr-uhd/swig/__init__.py b/gr-uhd/swig/__init__.py new file mode 100644 index 000000000..2fed17e57 --- /dev/null +++ b/gr-uhd/swig/__init__.py @@ -0,0 +1,28 @@ +# +# Copyright 2010 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. +# + +# The presence of this file turns this directory into a Python package + +# Add SWIG generated code to this namespace +from uhd_swig import * + +# Add other content from pure-Python modules here + diff --git a/gr-uhd/swig/uhd_swig.i b/gr-uhd/swig/uhd_swig.i new file mode 100644 index 000000000..2642f9450 --- /dev/null +++ b/gr-uhd/swig/uhd_swig.i @@ -0,0 +1,39 @@ +/* -*- c++ -*- */ +/* + * Copyright 2010 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. + */ + +%include "gnuradio.i" + +%{ +#include <uhd_simple_source.h> +#include <uhd_simple_sink.h> +%} + +%include <uhd/config.hpp> +%include <uhd/types/ranges.hpp> +%include <uhd/types/tune_result.hpp> +%include <uhd/types/io_type.hpp> + +GR_SWIG_BLOCK_MAGIC(uhd,simple_source) +%include <uhd_simple_source.h> + +GR_SWIG_BLOCK_MAGIC(uhd,simple_sink) +%include <uhd_simple_sink.h> diff --git a/grc/python/Generator.py b/grc/python/Generator.py index acd98ef84..d53802bef 100644 --- a/grc/python/Generator.py +++ b/grc/python/Generator.py @@ -58,7 +58,7 @@ class Generator(object): def write(self): #do throttle warning all_keys = ' '.join(map(lambda b: b.get_key(), self._flow_graph.get_enabled_blocks())) - if ('usrp' not in all_keys) and ('audio' not in all_keys) and ('throttle' not in all_keys) and self._generate_options != 'hb': + if ('usrp' not in all_keys) and ('uhd' not in all_keys) and ('audio' not in all_keys) and ('throttle' not in all_keys) and self._generate_options != 'hb': Messages.send_warning('''\ This flow graph may not have flow control: no audio or usrp blocks found. \ Add a Misc->Throttle block to your flow graph to avoid CPU congestion.''') diff --git a/version.sh b/version.sh index c18f46416..0ae040982 100644 --- a/version.sh +++ b/version.sh @@ -1,4 +1,4 @@ MAJOR_VERSION=3 -API_COMPAT=3 -MINOR_VERSION=0-rc3 -MAINT_VERSION=0 +API_COMPAT=4 +MINOR_VERSION=git +MAINT_VERSION= diff --git a/vrt/.gitignore b/vrt/.gitignore new file mode 100644 index 000000000..8d802d8ff --- /dev/null +++ b/vrt/.gitignore @@ -0,0 +1,3 @@ +Makefile +Makefile.in +vrt.pc diff --git a/vrt/Makefile.am b/vrt/Makefile.am new file mode 100644 index 000000000..791b8d65c --- /dev/null +++ b/vrt/Makefile.am @@ -0,0 +1,29 @@ +# +# Copyright 2008,2009 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +include $(top_srcdir)/Makefile.common + +pkgconfigdir = $(libdir)/pkgconfig +pkgconfig_DATA = vrt.pc + +EXTRA_DIST = \ + vrt.pc.in + +SUBDIRS = include lib apps diff --git a/vrt/apps/.gitignore b/vrt/apps/.gitignore new file mode 100644 index 000000000..dab7a7fd6 --- /dev/null +++ b/vrt/apps/.gitignore @@ -0,0 +1,5 @@ +Makefile +Makefile.in +.deps +.libs + diff --git a/vrt/apps/Makefile.am b/vrt/apps/Makefile.am new file mode 100644 index 000000000..45772055b --- /dev/null +++ b/vrt/apps/Makefile.am @@ -0,0 +1,18 @@ +# +# Copyright 2009 Free Software Foundation, Inc. +# +# 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 diff --git a/vrt/include/Makefile.am b/vrt/include/Makefile.am new file mode 100644 index 000000000..3ce6a8f32 --- /dev/null +++ b/vrt/include/Makefile.am @@ -0,0 +1,23 @@ +# +# Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +include $(top_srcdir)/Makefile.common + +SUBDIRS = vrt diff --git a/vrt/include/vrt/Makefile.am b/vrt/include/vrt/Makefile.am new file mode 100644 index 000000000..183eaf9ce --- /dev/null +++ b/vrt/include/vrt/Makefile.am @@ -0,0 +1,30 @@ +# +# Copyright 2008,2009 Free Software Foundation, Inc. +# +# 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 + +INCLUDES = + +vrtincludedir = $(includedir)/vrt + +vrtinclude_HEADERS = \ + bits.h \ + copiers.h \ + expanded_header.h \ + rx.h \ + rx_packet_handler.h \ + types.h diff --git a/vrt/include/vrt/bits.h b/vrt/include/vrt/bits.h new file mode 100644 index 000000000..54eeec7b4 --- /dev/null +++ b/vrt/include/vrt/bits.h @@ -0,0 +1,92 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 Free Software Foundation, Inc. + * + * 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_VRT_BITS_H +#define INCLUDED_VRT_BITS_H + +#include <stdint.h> + + +/* VRT Header bits */ + +#define VRTH_PT_MASK (0xf << 28) +#define VRTH_PT_IF_DATA_NO_SID (0x0 << 28) // IF-Data, no stream id +#define VRTH_PT_IF_DATA_WITH_SID (0x1 << 28) // IF-Data, w/ stream id +#define VRTH_PT_EXT_DATA_NO_SID (0x2 << 28) +#define VRTH_PT_EXT_DATA_WITH_SID (0x3 << 28) +#define VRTH_PT_IF_CONTEXT (0x4 << 28) +#define VRTH_PT_EXT_CONTEXT (0x5 << 28) + +#define VRTH_HAS_CLASSID (1 << 27) +#define VRTH_HAS_TRAILER (1 << 26) // Data pkts only +#define VRTH_START_OF_BURST (1 << 25) // Data (Tx) pkts only +#define VRTH_END_OF_BURST (1 << 24) // Data (Tx) pkts only +#define VRTH_TSM (1 << 24) // Context pkts only + +#define VRTH_TSI_MASK (0x3 << 22) +#define VRTH_TSI_NONE (0x0 << 22) +#define VRTH_TSI_UTC (0x1 << 22) +#define VRTH_TSI_GPS (0x2 << 22) +#define VRTH_TSI_OTHER (0x3 << 22) + +#define VRTH_TSF_MASK (0x3 << 20) +#define VRTH_TSF_NONE (0x0 << 20) +#define VRTH_TSF_SAMPLE_CNT (0x1 << 20) +#define VRTH_TSF_REAL_TIME_PS (0x2 << 20) +#define VRTH_TSF_FREE_RUNNING (0x3 << 20) + +#define VRTH_PKT_CNT_SHIFT 16 +#define VRTH_PKT_CNT_MASK (0xf << 16) + +#define VRTH_PKT_SIZE_MASK 0xffff + + +static inline int +vrth_pkt_cnt(uint32_t h) +{ + return (h & VRTH_PKT_CNT_MASK) >> 16; +} + +static inline int +vrth_pkt_size(uint32_t h) +{ + return h & VRTH_PKT_SIZE_MASK; +} + +/* + * Trailer bits + */ +#define TR_E (1 << 8) + +#define TR_ENABLE(x) ((x) << 20) +#define TR_STATE(x) ((x) << 8) + +// Use these with TR_ENABLE and TR_STATE +#define TR_CAL_TIME (1 << 11) +#define TR_VALID_DATA (1 << 10) +#define TR_REF_LOCK (1 << 9) +#define TR_AGC (1 << 8) +#define TR_DETECTED_SIG (1 << 7) +#define TR_SPECTRAL_INVERSION (1 << 6) +#define TR_OVER_RANGE (1 << 5) +#define TR_SAMPLE_LOSS (1 << 4) +#define TR_USER_3 (1 << 3) +#define TR_USER_2 (1 << 2) +#define TR_USER_1 (1 << 1) +#define TR_USER_0 (1 << 0) + +#endif /* INCLUDED_VRT_BITS_H */ diff --git a/vrt/include/vrt/copiers.h b/vrt/include/vrt/copiers.h new file mode 100644 index 000000000..990538c42 --- /dev/null +++ b/vrt/include/vrt/copiers.h @@ -0,0 +1,49 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef INCLUDED_VRT_COPIERS_H +#define INCLUDED_VRT_COPIERS_H + +#include <stdint.h> +#include <stddef.h> +#include <complex> + +namespace vrt { + + /*! + * \brief Copy and convert from net format to host format + */ + void + copy_net_16sc_to_host_16sc(size_t nitems, + const uint32_t *items, + std::complex<int16_t> *host_items); + + + /*! + * \brief Copy and convert from net format to host format mapping [-32768, 32767] -> [1.0, +1.0) + */ + void + copy_net_16sc_to_host_32fc(size_t nitems, + const uint32_t *items, + std::complex<float> *host_items); +}; + +#endif /* INCLUDED_VRT_COPIERS_H */ diff --git a/vrt/include/vrt/expanded_header.h b/vrt/include/vrt/expanded_header.h new file mode 100644 index 000000000..b3333a72e --- /dev/null +++ b/vrt/include/vrt/expanded_header.h @@ -0,0 +1,112 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef INCLUDED_VRT_EXPANDED_HEADER_H +#define INCLUDED_VRT_EXPANDED_HEADER_H + +#include <stdint.h> +#include <stddef.h> +#include <vrt/bits.h> + +namespace vrt { + + static const size_t HEADER_MAX_N32_BIT_WORDS = 7; + static const size_t TRAILER_MAX_N32_BIT_WORDS = 1; + + /*! + * \brief All headers and trailer for VRT IF-Data, Extension-Data, + * IF-Context and Extension-Context packets. + * + * There are fields allocated for each possible header. Their content may + * or may not be valid. Check the header field to confirm their validity. + * All values are in host-endian format. + */ + struct expanded_header { + uint32_t header; // first word of all packets + uint32_t stream_id; // optional stream identifier + uint64_t class_id; // optional class identifier + uint32_t integer_secs; // optional integer seconds timestamp + uint64_t fractional_secs; // optional fractional seconds timestamp + uint32_t trailer; // optional trailer (only possible in data pkts) + + expanded_header() + : header(0) /*, stream_id(0), class_id(0), + integer_secs(0), fractional_secs(0), trailer(0)*/ {} + + + int pkt_type() const { + return (header & VRTH_PT_MASK) >> 28; + } + + int pkt_cnt() const { return vrth_pkt_cnt(header); } + size_t pkt_size() const { return vrth_pkt_size(header); } + + + // packet type predicates + bool if_data_p() const { return s_if_data[pkt_type()]; } + bool ext_data_p() const { return s_ext_data[pkt_type()]; } + bool if_context_p() const { return (header & VRTH_PT_MASK) == VRTH_PT_IF_CONTEXT; } + bool ext_context_p() const { return (header & VRTH_PT_MASK) == VRTH_PT_EXT_CONTEXT; } + + bool data_p() const { return s_data[pkt_type()]; } // if_data_p() || ext_data_p() + bool context_p() const { return s_context[pkt_type()]; } // if_context_p() || ext_context_p() + + // optional info predicates + bool stream_id_p() const { return s_stream_id[pkt_type()]; } + bool class_id_p() const { return (header & VRTH_HAS_CLASSID) != 0; } + bool integer_secs_p() const { return (header & VRTH_TSI_MASK) != 0; } + bool fractional_secs_p() const { return (header & VRTH_TSF_MASK) != 0; } + bool trailer_p() const { return (header & VRTH_HAS_TRAILER) != 0 && data_p(); } + + + /*! + * \brief unparse expanded header, fill-in the words of a vrt packet header and trailer + * This method is only intended to fill the buffers with header and trailer information. + * The actual handling of the separate header, payload, trailer buffers is up to the caller. + */ + static void unparse(const expanded_header *hdr, // in + size_t n32_bit_words_payload, // in + uint32_t *header, // out + size_t *n32_bit_words_header, // out + uint32_t *trailer, // out + size_t *n32_bit_words_trailer);// out + + /*! + * \brief parse packet, fill-in expanded header, start of payload and len of payload + */ + static bool parse(const uint32_t *packet, // in + size_t n32_bit_words_packet, // in + expanded_header *hdr, // out + const uint32_t **payload, // out + size_t *n32_bit_words_payload); // out + + private: + static unsigned char s_if_data[16]; + static unsigned char s_ext_data[16]; + static unsigned char s_data[16]; + static unsigned char s_context[16]; + static unsigned char s_stream_id[16]; + + }; + +}; // vrt + + +#endif /* INCLUDED_VRT_EXPANDED_HEADER_H */ diff --git a/vrt/include/vrt/rx.h b/vrt/include/vrt/rx.h new file mode 100644 index 000000000..ff3ce85fb --- /dev/null +++ b/vrt/include/vrt/rx.h @@ -0,0 +1,93 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef INCLUDED_VRT_RX_H +#define INCLUDED_VRT_RX_H + +#include <boost/shared_ptr.hpp> +#include <boost/utility.hpp> +#include <vrt/rx_packet_handler.h> + +namespace vrt { + + class socket_rx_buffer; + + /*! + * Relatively low-level interface to receive VRT packets over a datagram socket. + * + * (We'll refactor this if/when we use a non-UDP transport.) + * No VRT control issues are addressed here. + */ + class rx : boost::noncopyable + { + int d_socket_fd; + socket_rx_buffer *d_srb; + + public: + /*! + * Shared pointer to this class + */ + typedef boost::shared_ptr<rx> sptr; + + /*! + * \brief Static function to return an instance of rx as a shared pointer. + * + * \param socket_fd file descriptor that data grams will be received from. + * It is assumed that some higher-level control software + * opened the appropriate UDP socket for us. This object + * assumes management of the socket's lifetime. The + * socket will be closed when our destructor fires. + * + * \param rx_bufsize is a hint as to the number of bytes of memory + * to allocate for received ethernet frames (0 -> reasonable default) + */ + static sptr make(int socket_fd, size_t rx_bufsize = 0); + + /*! + * \param socket_fd file descriptor that data grams will be received from. + * It is assumed that some higher-level control software + * opened the appropriate UDP socket for us. This object + * assumes management of the socket's lifetime. The + * socket will be closed when our destructor fires. + * + * \param rx_bufsize is a hint as to the number of bytes of memory + * to allocate for received ethernet frames (0 -> reasonable default) + */ + rx(int socket_fd, size_t rx_bufsize = 0); + ~rx(); + + /*! + * \brief Receive packets from the given socket file descriptor. + * + * \p handler will be invoked for all available packets. + * Unless \p dont_wait is true, this function blocks until at + * least one packet has been processed. + */ + bool rx_packets(rx_packet_handler *handler, bool dont_wait = false); + + /* + * \returns the socket_fd. Useful for select or poll. + */ + int socket_fd() const { return d_socket_fd; } + }; + +} + +#endif /* INCLUDED_VRT_RX_H */ diff --git a/vrt/include/vrt/rx_packet_handler.h b/vrt/include/vrt/rx_packet_handler.h new file mode 100644 index 000000000..ad3407813 --- /dev/null +++ b/vrt/include/vrt/rx_packet_handler.h @@ -0,0 +1,62 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef INCLUDED_VRT_RX_PACKET_HANDLER_H +#define INCLUDED_VRT_RX_PACKET_HANDLER_H + +#include <vrt/expanded_header.h> +#include <stddef.h> + +namespace vrt { + + /*! + * \brief Abstract function object called to handle received VRT packets. + * + * An object derived from this class is passed to vrt_rx_udp::rx_packets + * to process the received packets. + */ + class rx_packet_handler { + public: + virtual ~rx_packet_handler(); + + /*! + * \param payload points to the first 32-bit word of the payload field. + * \param n32_bit_words is the number of 32-bit words in the payload field. + * \param hdr is the expanded version of the mandatory and optional header fields (& trailer). + * + * \p payload points to the raw payload section of the packet received off + * the wire. The data is network-endian (aka big-endian) 32-bit integers. + * + * This is the general purpose, low level interface and relies on other + * functions to handle all required endian-swapping and format conversion + * of the payload. \sa FIXME. + * + * \returns true if the object wants to be called again with new data; + * false if no additional data is wanted. + */ + virtual bool operator()(const uint32_t *payload, + size_t n32_bit_words, + const expanded_header *hdr); + }; + +}; // vrt + + +#endif /* INCLUDED_VRT_RX_PACKET_HANDLER_H */ diff --git a/vrt/include/vrt/types.h b/vrt/include/vrt/types.h new file mode 100644 index 000000000..edfa4ec37 --- /dev/null +++ b/vrt/include/vrt/types.h @@ -0,0 +1,138 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 Free Software Foundation, Inc. + * + * 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_VRT_TYPES_H +#define INCLUDED_VRT_TYPES_H + +#include <stdint.h> + +/* macros for dealing with fixed point numbers */ +#define _FXPT_C(_type, _x, _rp) ((_type)((_x)*(1ll << _rp))) +#define _FXPT_TO_INT(_x, _one) (((_x) + ((_one)/2))/(_one)) +#define _FXPT_TO_DOUBLE(_x, _one) ((double)(_x) * (1.0/(_one))) + +/*********************************************************************** + * The VRT Altitude Type (meters) + **********************************************************************/ +typedef int32_t vrt_altitude_t; +#define VRT_ALTITUDE_RP 5 +#define VRT_ALTITUDE_C(_x) _FXPT_C(vrt_altitude_t, _x, VRT_ALTITUDE_RP) + +static inline vrt_altitude_t +double_to_vrt_altitude(double num){ + return VRT_ALTITUDE_C(num); +} + +static inline int32_t +vrt_altitude_round_to_int(vrt_altitude_t fx){ + return _FXPT_TO_INT(fx, VRT_ALTITUDE_C(1)); +} + +static inline double +vrt_altitude_to_double(vrt_altitude_t fx){ + return _FXPT_TO_DOUBLE(fx, VRT_ALTITUDE_C(1)); +} + +/*********************************************************************** + * The VRT Geolocation Angle Type (degrees) + **********************************************************************/ +typedef int32_t vrt_geo_angle_t; +#define VRT_GEO_ANGLE_RP 22 +#define VRT_GEO_ANGLE_C(_x) _FXPT_C(vrt_geo_angle_t, _x, VRT_GEO_ANGLE_RP) + +static inline vrt_geo_angle_t +double_to_vrt_geo_angle(double num){ + return VRT_GEO_ANGLE_C(num); +} + +static inline int16_t +vrt_geo_angle_round_to_int(vrt_geo_angle_t fx){ + return _FXPT_TO_INT(fx, VRT_GEO_ANGLE_C(1)); +} + +static inline double +vrt_geo_angle_to_double(vrt_geo_angle_t fx){ + return _FXPT_TO_DOUBLE(fx, VRT_GEO_ANGLE_C(1)); +} + +/*********************************************************************** + * The VRT Frequency Type (Hz) + **********************************************************************/ +typedef int64_t vrt_freq_t; +#define VRT_FREQ_RP 20 +#define VRT_FREQ_C(_x) _FXPT_C(vrt_freq_t, _x, VRT_FREQ_RP) + +static inline vrt_freq_t +double_to_vrt_freq(double num){ + return VRT_FREQ_C(num); +} + +static inline int64_t +vrt_freq_round_to_int(vrt_freq_t fx){ + return _FXPT_TO_INT(fx, VRT_FREQ_C(1)); +} + +static inline double +vrt_freq_to_double(vrt_freq_t fx){ + return _FXPT_TO_DOUBLE(fx, VRT_FREQ_C(1)); +} + +/*********************************************************************** + * The VRT Gain Type (dB) + **********************************************************************/ +typedef int16_t vrt_gain_t; +#define VRT_GAIN_RP 7 +#define VRT_GAIN_C(_x) _FXPT_C(vrt_gain_t, _x, VRT_GAIN_RP) + +static inline vrt_gain_t +double_to_vrt_gain(double num){ + return VRT_GAIN_C(num); +} + +static inline int16_t +vrt_gain_round_to_int(vrt_gain_t fx){ + return _FXPT_TO_INT(fx, VRT_GAIN_C(1)); +} + +static inline double +vrt_gain_to_double(vrt_gain_t fx){ + return _FXPT_TO_DOUBLE(fx, VRT_GAIN_C(1)); +} + +/*********************************************************************** + * The VRT Temperature Type (Celcius) + **********************************************************************/ +typedef int16_t vrt_temp_t; +#define VRT_TEMP_RP 6 +#define VRT_TEMP_C(_x) _FXPT_C(vrt_temp_t, _x, VRT_TEMP_RP) + +static inline vrt_temp_t +double_to_vrt_temp(double num){ + return VRT_TEMP_C(num); +} + +static inline int16_t +vrt_temp_round_to_int(vrt_temp_t fx){ + return _FXPT_TO_INT(fx, VRT_TEMP_C(1)); +} + +static inline double +vrt_temp_to_double(vrt_temp_t fx){ + return _FXPT_TO_DOUBLE(fx, VRT_TEMP_C(1)); +} + +#endif /* INCLUDED_VRT_TYPES_H */ diff --git a/vrt/lib/.gitignore b/vrt/lib/.gitignore new file mode 100644 index 000000000..02b052397 --- /dev/null +++ b/vrt/lib/.gitignore @@ -0,0 +1,4 @@ +Makefile +Makefile.in +.deps +.libs diff --git a/vrt/lib/Makefile.am b/vrt/lib/Makefile.am new file mode 100644 index 000000000..4873c979d --- /dev/null +++ b/vrt/lib/Makefile.am @@ -0,0 +1,52 @@ +# +# Copyright 2007,2008,2009 Free Software Foundation, Inc. +# +# 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 + +AM_CPPFLAGS = \ + $(VRT_INCLUDES) \ + $(BOOST_CPPFLAGS) \ + $(CPPUNIT_INCLUDES) \ + $(GRUEL_INCLUDES) + +bin_PROGRAMS = + +lib_LTLIBRARIES = \ + libvrt.la + +libvrt_la_SOURCES = \ + copiers.cc \ + data_handler.cc \ + expanded_header.cc \ + rx.cc \ + rx_packet_handler.cc \ + socket_rx_buffer.cc + +libvrt_la_LIBADD = + +libvrt_la_LDFLAGS = $(LTVERSIONFLAGS) + +# Private headers not needed for above the API development +noinst_HEADERS = \ + data_handler.h \ + expanded_header_parse_switch_body.h \ + expanded_header_unparse_switch_body.h \ + socket_rx_buffer.h + +EXTRA_DIST = \ + gen_parse_switch_body.py \ + gen_unparse_switch_body.py diff --git a/vrt/lib/copiers.cc b/vrt/lib/copiers.cc new file mode 100644 index 000000000..ddf82abcf --- /dev/null +++ b/vrt/lib/copiers.cc @@ -0,0 +1,71 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <vrt/copiers.h> +#include <arpa/inet.h> +#include <assert.h> +#include <string.h> + +namespace vrt { + + void + copy_net_16sc_to_host_16sc(size_t nitems, + const uint32_t *items, + std::complex<int16_t> *host_items) + { +#ifdef WORDS_BIGENDIAN + + assert(sizeof(items[0]) == sizeof(host_items[0])); + memcpy(host_items, items, nitems * sizeof(items[0])); + +#else + + // FIXME SIMD welcome here + + for (size_t i = 0; i < nitems; i++){ + uint32_t t = ntohl(items[i]); + //printf("%9d\n", items[i]); + host_items[i] = std::complex<int16_t>((t >> 16), t & 0xffff); + } + +#endif + } + + void + copy_net_16sc_to_host_32fc(size_t nitems, + const uint32_t *items, + std::complex<float> *host_items) + { + // FIXME SIMD welcome here + + for (size_t i = 0; i < nitems; i++){ + uint32_t t = ntohl(items[i]); + int16_t re = (t >> 16) & 0xffff; + int16_t im = (t & 0xffff); + host_items[i] = std::complex<float>(re * 1.0/32768, im * 1.0/32768); + } + } + +}; + diff --git a/vrt/lib/data_handler.cc b/vrt/lib/data_handler.cc new file mode 100644 index 000000000..7d1f73a9a --- /dev/null +++ b/vrt/lib/data_handler.cc @@ -0,0 +1,32 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "data_handler.h" + +namespace vrt { + + data_handler::~data_handler() + { + // default nop destructor + } + +} + diff --git a/vrt/lib/data_handler.h b/vrt/lib/data_handler.h new file mode 100644 index 000000000..c041e48be --- /dev/null +++ b/vrt/lib/data_handler.h @@ -0,0 +1,53 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef INCLUDED_VRT_DATA_HANDLER_H +#define INCLUDED_VRT_DATA_HANDLER_H + +#include <stdint.h> +#include <stddef.h> + +namespace vrt { + + /*! + * \brief Abstract function object called to handle received data blocks. + */ + class data_handler + { + public: + + enum result_bits { + DONE = 0x0002, //< do not call this object again + }; + + typedef int result; //< bitmask of result_bits + + /*! + * \param base points to the beginning of the data + * \param len is the length in bytes of the data + * \returns bitmask composed of DONE + */ + virtual result operator()(const void *base, size_t len) = 0; + virtual ~data_handler(); + }; + +} // namespace vrt + +#endif /* INCLUDED_VRT_DATA_HANDLER_H */ diff --git a/vrt/lib/expanded_header.cc b/vrt/lib/expanded_header.cc new file mode 100644 index 000000000..8b22fb925 --- /dev/null +++ b/vrt/lib/expanded_header.cc @@ -0,0 +1,135 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <vrt/expanded_header.h> +#include <gruel/inet.h> +//#include <stdio.h> + +namespace vrt { + + // lookup tables indexed by packet type + unsigned char expanded_header::s_if_data[16] = { + 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + unsigned char expanded_header::s_ext_data[16] = { + 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + unsigned char expanded_header::s_data[16] = { + 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + unsigned char expanded_header::s_context[16] = { + 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + unsigned char expanded_header::s_stream_id[16] = { + 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + }; + + + // dispatch codeword bits + static const int HAS_STREAM_ID = 1 << 0; + static const int HAS_CLASS_ID = 1 << 1; + static const int HAS_INTEGER_SECS = 1 << 2; + static const int HAS_FRACTIONAL_SECS = 1 << 3; + static const int HAS_TRAILER = 1 << 4; + + static int + compute_codeword(const expanded_header &h) + { + int cw = 0; + if (h.stream_id_p()) cw |= HAS_STREAM_ID; + if (h.class_id_p()) cw |= HAS_CLASS_ID; + if (h.integer_secs_p()) cw |= HAS_INTEGER_SECS; + if (h.fractional_secs_p()) cw |= HAS_FRACTIONAL_SECS; + if (h.trailer_p()) cw |= HAS_TRAILER; + return cw; + } + + void expanded_header::unparse(const expanded_header *h, // in + size_t n32_bit_words_payload, // in + uint32_t *header, // out + size_t *n32_bit_words_header, // out + uint32_t *trailer, // out + size_t *n32_bit_words_trailer){// out + int cw = compute_codeword(*h); + //fills in the header (except word0), header length, trailer, trailer length + switch (cw & 0x1f){ +#include "expanded_header_unparse_switch_body.h" + } + //fill in the header word 0 with the calculated length + size_t n32_bit_words_packet = *n32_bit_words_header + n32_bit_words_payload + *n32_bit_words_trailer; + header[0] = htonl((h->header & ~VRTH_PKT_SIZE_MASK) | (n32_bit_words_packet & VRTH_PKT_SIZE_MASK)); + } + + bool + expanded_header::parse(const uint32_t *packet, // in + size_t n32_bit_words_packet, // in + expanded_header *h, // out + const uint32_t **payload, // out + size_t *n32_bit_words_payload) // out + { + size_t n32_bit_words_header = 0; + size_t n32_bit_words_trailer = 0; + size_t len = n32_bit_words_packet; + const uint32_t *p = packet; + + *payload = 0; + *n32_bit_words_payload = 0; + + // printf("parse: n32_bit_words_packet = %zd\n", n32_bit_words_packet); + + if (len < 1){ // must have at least the header word + h->header = 0; + return false; + } + + h->header = ntohl(p[0]); + + if (h->pkt_size() > len) + return false; // VRT header says packet is bigger than what we've got + + len = h->pkt_size(); // valid length of packet + + int cw = compute_codeword(*h); + switch (cw & 0x1f){ +#include "expanded_header_parse_switch_body.h" + } + + if (n32_bit_words_header + n32_bit_words_trailer > len) + return false; // negative payload len + + *payload = p + n32_bit_words_header; + *n32_bit_words_payload = len - (n32_bit_words_header + n32_bit_words_trailer); + + // printf("parse: hdr = 0x%08x, cw = 0x%02x, n32_bit_words_header = %d, n32_bit_words_trailer = %d\n", + // h->header, cw, n32_bit_words_header, n32_bit_words_trailer); + + return true; + } + + +}; // vrt diff --git a/vrt/lib/expanded_header_parse_switch_body.h b/vrt/lib/expanded_header_parse_switch_body.h new file mode 100644 index 000000000..6bfaf3799 --- /dev/null +++ b/vrt/lib/expanded_header_parse_switch_body.h @@ -0,0 +1,320 @@ + case 0: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 1; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 1: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 2; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 2: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 3; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 3: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 4; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 4: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = ntohl(p[1]); + h->fractional_secs = 0; + n32_bit_words_header = 2; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 5: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = ntohl(p[2]); + h->fractional_secs = 0; + n32_bit_words_header = 3; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 6: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = ntohl(p[3]); + h->fractional_secs = 0; + n32_bit_words_header = 4; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 7: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = ntohl(p[4]); + h->fractional_secs = 0; + n32_bit_words_header = 5; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 8: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + n32_bit_words_header = 3; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 9: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + n32_bit_words_header = 4; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 10: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[3])) << 32) | ntohl(p[4]); + n32_bit_words_header = 5; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 11: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[4])) << 32) | ntohl(p[5]); + n32_bit_words_header = 6; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 12: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = ntohl(p[1]); + h->fractional_secs = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + n32_bit_words_header = 4; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 13: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = ntohl(p[2]); + h->fractional_secs = ((uint64_t)(ntohl(p[3])) << 32) | ntohl(p[4]); + n32_bit_words_header = 5; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 14: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = ntohl(p[3]); + h->fractional_secs = ((uint64_t)(ntohl(p[4])) << 32) | ntohl(p[5]); + n32_bit_words_header = 6; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 15: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = ntohl(p[4]); + h->fractional_secs = ((uint64_t)(ntohl(p[5])) << 32) | ntohl(p[6]); + n32_bit_words_header = 7; + h->trailer = 0; + n32_bit_words_trailer = 0; + break; + + case 16: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 1; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 17: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 2; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 18: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 3; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 19: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = 0; + h->fractional_secs = 0; + n32_bit_words_header = 4; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 20: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = ntohl(p[1]); + h->fractional_secs = 0; + n32_bit_words_header = 2; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 21: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = ntohl(p[2]); + h->fractional_secs = 0; + n32_bit_words_header = 3; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 22: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = ntohl(p[3]); + h->fractional_secs = 0; + n32_bit_words_header = 4; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 23: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = ntohl(p[4]); + h->fractional_secs = 0; + n32_bit_words_header = 5; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 24: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + n32_bit_words_header = 3; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 25: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + n32_bit_words_header = 4; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 26: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[3])) << 32) | ntohl(p[4]); + n32_bit_words_header = 5; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 27: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = 0; + h->fractional_secs = ((uint64_t)(ntohl(p[4])) << 32) | ntohl(p[5]); + n32_bit_words_header = 6; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 28: + h->stream_id = 0; + h->class_id = 0; + h->integer_secs = ntohl(p[1]); + h->fractional_secs = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + n32_bit_words_header = 4; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 29: + h->stream_id = ntohl(p[1]); + h->class_id = 0; + h->integer_secs = ntohl(p[2]); + h->fractional_secs = ((uint64_t)(ntohl(p[3])) << 32) | ntohl(p[4]); + n32_bit_words_header = 5; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 30: + h->stream_id = 0; + h->class_id = ((uint64_t)(ntohl(p[1])) << 32) | ntohl(p[2]); + h->integer_secs = ntohl(p[3]); + h->fractional_secs = ((uint64_t)(ntohl(p[4])) << 32) | ntohl(p[5]); + n32_bit_words_header = 6; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + + case 31: + h->stream_id = ntohl(p[1]); + h->class_id = ((uint64_t)(ntohl(p[2])) << 32) | ntohl(p[3]); + h->integer_secs = ntohl(p[4]); + h->fractional_secs = ((uint64_t)(ntohl(p[5])) << 32) | ntohl(p[6]); + n32_bit_words_header = 7; + h->trailer = ntohl(p[len-1]); + n32_bit_words_trailer = 1; + break; + diff --git a/vrt/lib/expanded_header_unparse_switch_body.h b/vrt/lib/expanded_header_unparse_switch_body.h new file mode 100644 index 000000000..ca6e14989 --- /dev/null +++ b/vrt/lib/expanded_header_unparse_switch_body.h @@ -0,0 +1,272 @@ + case 0: + *n32_bit_words_header = 1; + *n32_bit_words_trailer = 0; + break; + + case 1: + header[1] = htonl(h->stream_id); + *n32_bit_words_header = 2; + *n32_bit_words_trailer = 0; + break; + + case 2: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + *n32_bit_words_header = 3; + *n32_bit_words_trailer = 0; + break; + + case 3: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + *n32_bit_words_header = 4; + *n32_bit_words_trailer = 0; + break; + + case 4: + header[1] = htonl(h->integer_secs); + *n32_bit_words_header = 2; + *n32_bit_words_trailer = 0; + break; + + case 5: + header[1] = htonl(h->stream_id); + header[2] = htonl(h->integer_secs); + *n32_bit_words_header = 3; + *n32_bit_words_trailer = 0; + break; + + case 6: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[3] = htonl(h->integer_secs); + *n32_bit_words_header = 4; + *n32_bit_words_trailer = 0; + break; + + case 7: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[4] = htonl(h->integer_secs); + *n32_bit_words_header = 5; + *n32_bit_words_trailer = 0; + break; + + case 8: + header[1] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 3; + *n32_bit_words_trailer = 0; + break; + + case 9: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 4; + *n32_bit_words_trailer = 0; + break; + + case 10: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[4] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 5; + *n32_bit_words_trailer = 0; + break; + + case 11: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[4] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[5] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 6; + *n32_bit_words_trailer = 0; + break; + + case 12: + header[1] = htonl(h->integer_secs); + header[2] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 4; + *n32_bit_words_trailer = 0; + break; + + case 13: + header[1] = htonl(h->stream_id); + header[2] = htonl(h->integer_secs); + header[3] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[4] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 5; + *n32_bit_words_trailer = 0; + break; + + case 14: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[3] = htonl(h->integer_secs); + header[4] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[5] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 6; + *n32_bit_words_trailer = 0; + break; + + case 15: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[4] = htonl(h->integer_secs); + header[5] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[6] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 7; + *n32_bit_words_trailer = 0; + break; + + case 16: + *n32_bit_words_header = 1; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 17: + header[1] = htonl(h->stream_id); + *n32_bit_words_header = 2; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 18: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + *n32_bit_words_header = 3; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 19: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + *n32_bit_words_header = 4; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 20: + header[1] = htonl(h->integer_secs); + *n32_bit_words_header = 2; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 21: + header[1] = htonl(h->stream_id); + header[2] = htonl(h->integer_secs); + *n32_bit_words_header = 3; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 22: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[3] = htonl(h->integer_secs); + *n32_bit_words_header = 4; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 23: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[4] = htonl(h->integer_secs); + *n32_bit_words_header = 5; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 24: + header[1] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 3; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 25: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 4; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 26: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[4] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 5; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 27: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[4] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[5] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 6; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 28: + header[1] = htonl(h->integer_secs); + header[2] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 4; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 29: + header[1] = htonl(h->stream_id); + header[2] = htonl(h->integer_secs); + header[3] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[4] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 5; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 30: + header[1] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[2] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[3] = htonl(h->integer_secs); + header[4] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[5] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 6; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + + case 31: + header[1] = htonl(h->stream_id); + header[2] = htonl((uint32_t)((h->class_id >> 32) & 0xffffffff)); + header[3] = htonl((uint32_t)((h->class_id >> 0) & 0xffffffff)); + header[4] = htonl(h->integer_secs); + header[5] = htonl((uint32_t)((h->fractional_secs >> 32) & 0xffffffff)); + header[6] = htonl((uint32_t)((h->fractional_secs >> 0) & 0xffffffff)); + *n32_bit_words_header = 7; + trailer[0] = htonl(h->trailer); + *n32_bit_words_trailer = 1; + break; + diff --git a/vrt/lib/gen_parse_switch_body.py b/vrt/lib/gen_parse_switch_body.py new file mode 100755 index 000000000..d13e490b7 --- /dev/null +++ b/vrt/lib/gen_parse_switch_body.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# +# Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +import sys + +# dispatch codeword bits +HAS_STREAM_ID = 1 << 0; +HAS_CLASS_ID = 1 << 1; +HAS_INTEGER_SECS = 1 << 2; +HAS_FRACTIONAL_SECS = 1 << 3; +HAS_TRAILER = 1 << 4; + +def do_case(f, cw): + + def do32(name, mask, index): + f.write(" ") + if cw & mask: + f.write("h->%s = ntohl(p[%d]);\n" % (name, index)) + return 1 + else: + f.write("h->%s = 0;\n" % (name,)) + return 0 + + def do64(name, mask, index): + f.write(" ") + if cw & mask: + f.write("h->%s = ((uint64_t)(ntohl(p[%d])) << 32) | ntohl(p[%d]);\n" % (name, index, index+1)) + return 2 + else: + f.write("h->%s = 0;\n" % (name,)) + return 0 + + def dolength(index): + f.write(" n32_bit_words_header = %d;\n"%index) + + def dotrailer(name, mask): + if cw & mask: + f.write(" h->%s = ntohl(p[len-1]);\n" % (name,)) + f.write(" n32_bit_words_trailer = 1;\n") + return 1 + else: + f.write(" h->%s = 0;\n" % (name,)) + f.write(" n32_bit_words_trailer = 0;\n") + return 0 + + f.write(" case %d:\n" % (cw,)) + + index = 1 + index += do32("stream_id", HAS_STREAM_ID, index) + index += do64("class_id", HAS_CLASS_ID, index) + index += do32("integer_secs", HAS_INTEGER_SECS, index) + index += do64("fractional_secs", HAS_FRACTIONAL_SECS, index) + dolength(index) + dotrailer("trailer", HAS_TRAILER) + + f.write(" break;\n\n") + + +def main(): + f = sys.stdout + + for cw in range(32): + do_case(f, cw) + + +if __name__ == '__main__': + main() diff --git a/vrt/lib/gen_unparse_switch_body.py b/vrt/lib/gen_unparse_switch_body.py new file mode 100755 index 000000000..6c7cd01b3 --- /dev/null +++ b/vrt/lib/gen_unparse_switch_body.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# +# Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# + +import sys + +# dispatch codeword bits +HAS_STREAM_ID = 1 << 0; +HAS_CLASS_ID = 1 << 1; +HAS_INTEGER_SECS = 1 << 2; +HAS_FRACTIONAL_SECS = 1 << 3; +HAS_TRAILER = 1 << 4; + +def do_case(f, cw): + + def do32(name, mask, index): + if cw & mask: + f.write(" header[%d] = htonl(h->%s);\n" % (index, name)) + return 1 + return 0 + + def do64(name, mask, index): + if cw & mask: + f.write(" header[%d] = htonl((uint32_t)((h->%s >> 32) & 0xffffffff));\n" % (index, name)) + f.write(" header[%d] = htonl((uint32_t)((h->%s >> 0) & 0xffffffff));\n" % (index+1, name)) + return 2 + return 0 + + def dolength(index): + f.write(" *n32_bit_words_header = %d;\n"%index) + + def dotrailer(name, mask): + if cw & mask: + f.write(" trailer[%d] = htonl(h->%s);\n" % (0, name)) + f.write(" *n32_bit_words_trailer = 1;\n") + return 1 + else: + f.write(" *n32_bit_words_trailer = 0;\n") + return 0 + + f.write(" case %d:\n" % (cw,)) + + index = 1 + index += do32("stream_id", HAS_STREAM_ID, index) + index += do64("class_id", HAS_CLASS_ID, index) + index += do32("integer_secs", HAS_INTEGER_SECS, index) + index += do64("fractional_secs", HAS_FRACTIONAL_SECS, index) + dolength(index) + dotrailer("trailer", HAS_TRAILER) + + f.write(" break;\n\n") + + +def main(): + f = sys.stdout + + for cw in range(32): + do_case(f, cw) + + +if __name__ == '__main__': + main() diff --git a/vrt/lib/rx.cc b/vrt/lib/rx.cc new file mode 100644 index 000000000..2d741e908 --- /dev/null +++ b/vrt/lib/rx.cc @@ -0,0 +1,124 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <vrt/rx.h> +#include <vrt/expanded_header.h> +#include "socket_rx_buffer.h" +#include "data_handler.h" +#include <unistd.h> +#include <stdio.h> +#include <stdexcept> + +static void +print_words(FILE *fp, size_t offset, const uint32_t *buf, size_t n) +{ + size_t i; + for (i = 0; i < n; i++){ + if (i % 4 == 0){ + fprintf(fp, "%04zx:", i); + } + + putc(' ', fp); + fprintf(fp, "%08x", buf[i]); + if (i % 4 == 3) + putc('\n', fp); + } + + putc('\n', fp); +} + + + +namespace vrt { + + rx::sptr + rx::make(int socket_fd, size_t rx_bufsize) + { + return sptr(new rx(socket_fd, rx_bufsize)); + } + + rx::rx(int socket_fd, size_t rx_bufsize) + : d_socket_fd(socket_fd), + d_srb(new socket_rx_buffer(socket_fd, rx_bufsize)) + { + } + + rx::~rx() + { + delete d_srb; + ::close(d_socket_fd); + } + + + class vrt_data_handler : public data_handler + { + rx_packet_handler *d_handler; + + public: + vrt_data_handler(rx_packet_handler *handler) + : d_handler(handler){} + + ~vrt_data_handler(); + + result operator()(const void *base, size_t len); + }; + + vrt_data_handler::~vrt_data_handler(){} + + // N.B., There may be more than 1 VRT packet in a frame (usually IF-Context packets) + data_handler::result + vrt_data_handler::operator()(const void *base, size_t len) + { + const uint32_t *word_base = (const uint32_t *) base; + size_t word_len = len/(sizeof(uint32_t)); + + bool want_more = true; + while (word_len > 0 && want_more){ + const uint32_t *payload; + size_t n32_bit_words; + expanded_header hdr; + if (!expanded_header::parse(word_base, word_len, + &hdr, &payload, &n32_bit_words)){ + if (1){ + fprintf(stderr, "vrt_data_handler: malformed VRT packet!\n"); + print_words(stderr, 0, word_base, word_len); + } + return 0; + } + want_more = (*d_handler)(payload, n32_bit_words, &hdr); + word_base += hdr.pkt_size(); + word_len -= hdr.pkt_size(); + } + return !want_more ? data_handler::DONE : 0; + } + + + bool + rx::rx_packets(rx_packet_handler *handler, bool dont_wait) + { + vrt_data_handler h(handler); + socket_rx_buffer::result r = d_srb->rx_frames(&h, dont_wait ? 0 : -1); + return r == socket_rx_buffer::EB_OK || r == socket_rx_buffer::EB_WOULD_BLOCK; + } + +}; // vrt diff --git a/vrt/lib/rx_packet_handler.cc b/vrt/lib/rx_packet_handler.cc new file mode 100644 index 000000000..11f90278d --- /dev/null +++ b/vrt/lib/rx_packet_handler.cc @@ -0,0 +1,41 @@ +/* -*- c++ -*- */ +/* + * Copyright 2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif +#include <vrt/rx_packet_handler.h> + +namespace vrt { + + rx_packet_handler::~rx_packet_handler(){} + + // default operator is a NOP + bool + rx_packet_handler::operator()(const uint32_t *payload, + size_t n32_bit_words, + const expanded_header *hdr) + { + return true; + } + + +}; // vrt diff --git a/vrt/lib/socket_rx_buffer.cc b/vrt/lib/socket_rx_buffer.cc new file mode 100644 index 000000000..6ed211b9a --- /dev/null +++ b/vrt/lib/socket_rx_buffer.cc @@ -0,0 +1,278 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include "socket_rx_buffer.h" +#include "data_handler.h" +#include <linux/if_packet.h> +#include <sys/socket.h> +#include <sys/mman.h> +#include <sys/poll.h> +#include <iostream> +#include <cmath> +#include <errno.h> +#include <stdexcept> +#include <string.h> +#include <fcntl.h> +#include <cstdio> + + +#define SOCKET_RX_BUFFER_DEBUG 1 // define to 0 or 1 +#if SOCKET_RX_BUFFER_DEBUG +#define DEBUG_LOG(x) ::write(2, (x), 1) +#else +#define DEBUG_LOG(X) +#endif + +#define DEFAULT_MEM_SIZE 62.5e6 // ~0.5s @ 125 MB/s +#define MAX_MEM_SIZE 1000e6 // ~10.00s @ 100 MB/s. +#define MAX_SLAB_SIZE 131072 // 128 KB (FIXME fish out of /proc/slabinfo) + + +namespace vrt { + + const unsigned int socket_rx_buffer::MAX_PKTLEN = 8192; + const unsigned int socket_rx_buffer::MIN_PKTLEN = 64; + + socket_rx_buffer::socket_rx_buffer(int socket_fd, size_t rx_bufsize) + : d_fd(socket_fd), d_using_tpring(false), d_buflen(0), d_buf(0), d_frame_nr(0), + d_frame_size(0), d_head(0), d_ring(0) + { + if (rx_bufsize == 0) + d_buflen = (size_t)DEFAULT_MEM_SIZE; + else + d_buflen = std::min((size_t)MAX_MEM_SIZE, rx_bufsize); + + if (!open()){ + throw std::runtime_error("socket_rx_buffer::open failed"); + } + } + + socket_rx_buffer::~socket_rx_buffer() + { + close(); + } + + bool + socket_rx_buffer::open() + { + if (try_packet_ring()){ + d_using_tpring = true; + // fprintf(stderr, "socket_rx_buffer: using memory mapped interface\n"); + } + else { + d_using_tpring = false; + // fprintf(stderr, "socket_rx_buffer: NOT using memory mapped interface\n"); + + // Increase socket buffer if possible + + int rcvbuf_size = d_buflen; +#if defined(SO_RCVBUFFORCE) + if (setsockopt(d_fd, SOL_SOCKET, SO_RCVBUFFORCE, &rcvbuf_size, sizeof(rcvbuf_size)) != 0){ + perror("setsockopt(SO_RCVBUFFORCE)"); + fprintf(stderr, "Are you running as root? If not, please do.\n"); + } + else { + fprintf(stderr, "SO_RCVBUFFORCE = %zd\n", d_buflen); + } +#endif + } + + return true; + } + + bool + socket_rx_buffer::try_packet_ring() + { + struct tpacket_req req; + size_t page_size = getpagesize(); + + // Calculate minimum power-of-two aligned size for frames + req.tp_frame_size = + (unsigned int)rint(pow(2, ceil(log2(TPACKET_ALIGN(TPACKET_HDRLEN)+TPACKET_ALIGN(MAX_PKTLEN))))); + d_frame_size = req.tp_frame_size; + + // Calculate minimum contiguous pages needed to enclose a frame + int npages = (page_size > req.tp_frame_size) ? 1 : ((req.tp_frame_size+page_size-1)/page_size); + req.tp_block_size = page_size << (int)ceil(log2(npages)); + + // Calculate number of blocks + req.tp_block_nr = (int)(d_buflen/req.tp_block_size); + + + // Recalculate buffer length + d_buflen = req.tp_block_nr*req.tp_block_size; + + // Finally, calculate total number of frames. Since frames, blocks, + // and pages are all power-of-two aligned, frames are contiguous + req.tp_frame_nr = d_buflen/req.tp_frame_size; + d_frame_nr = req.tp_frame_nr; + +#if 0 + if (SOCKET_RX_BUFFER_DEBUG) + std::cerr << "socket_rx_buffer:" + << " frame_size=" << req.tp_frame_size + << " block_size=" << req.tp_block_size + << " block_nr=" << req.tp_block_nr + << " frame_nr=" << req.tp_frame_nr + << " buflen=" << d_buflen + << std::endl; +#endif + + // Try to get kernel shared memory buffer + if (setsockopt(d_fd, SOL_PACKET, PACKET_RX_RING, (void *)&req, sizeof(req)) != 0){ + // perror("socket_rx_buffer: setsockopt"); + return false; + } + + void *p = mmap(0, d_buflen, PROT_READ|PROT_WRITE, MAP_SHARED, d_fd, 0); + if (p == MAP_FAILED){ + perror("socket_rx_buffer: mmap"); + return false; + } + d_buf = (uint8_t *) p; + + // Initialize our pointers into the packet ring + d_ring.resize(req.tp_frame_nr); + for (unsigned int i=0; i < req.tp_frame_nr; i++) + d_ring[i] = (uint8_t *)(d_buf+i*req.tp_frame_size); + + return true; + } + + bool + socket_rx_buffer::close() + { + return true; + } + + inline bool + socket_rx_buffer::frame_available() + { + return (((tpacket_hdr *)d_ring[d_head])->tp_status != TP_STATUS_KERNEL); + } + + socket_rx_buffer::result + socket_rx_buffer::rx_frames(data_handler *f, int timeout_in_ms) + { + if (!d_using_tpring){ + + // ---------------------------------------------------------------- + // Use recv instead of kernel Rx packet ring + // ---------------------------------------------------------------- + + unsigned char buf[MAX_PKTLEN]; + bool dont_wait = timeout_in_ms == 0; // FIXME treating timeout as 0 or inf + int flags = dont_wait ? MSG_DONTWAIT : 0; + + ssize_t rr = recv(d_fd, buf, sizeof(buf), flags); + if (rr == -1){ // error? + if (errno == EAGAIN){ // non-blocking, nothing there + return EB_WOULD_BLOCK; + } + perror("rx_frames: recv"); + return EB_ERROR; + } + + // Got first packet. Call handler + + data_handler::result r = (*f)(buf, rr); + if (r & data_handler::DONE) + return EB_OK; + + // Now do as many as we can without blocking + + while (1){ + rr = recv(d_fd, buf, sizeof(buf), MSG_DONTWAIT); + if (rr == -1){ // error? + if (errno == EAGAIN) // non-blocking, nothing there + return EB_OK; // return OK; we've processed >= 1 packets + perror("rx_frames: recv"); + return EB_ERROR; + } + + r = (*f)(buf, rr); + if (r & data_handler::DONE) + break; + } + return EB_OK; + } + + // ---------------------------------------------------------------- + // Use kernel Rx packet ring + // ---------------------------------------------------------------- + + DEBUG_LOG("\n"); + + while (!frame_available()) { + if (timeout_in_ms == 0) { + DEBUG_LOG("w"); + return EB_WOULD_BLOCK; + } + + struct pollfd pfd; + pfd.fd = d_fd; + pfd.revents = 0; + pfd.events = POLLIN; + + // DEBUG_LOG("P"); + + int pres = poll(&pfd, 1, timeout_in_ms); + if (pres == -1) { + perror("poll"); + return EB_ERROR; + } + + if (pres == 0) { + DEBUG_LOG("t"); + return EB_TIMED_OUT; + } + } + + // Iterate through available packets + while (frame_available()) { + // Get start of ethernet frame and length + tpacket_hdr *hdr = (tpacket_hdr *)d_ring[d_head]; + void *base = (uint8_t *)hdr+hdr->tp_mac; + size_t len = hdr->tp_len; + + if (1) + fprintf(stderr, "socket_rx_buffer: base = %p tp_mac = %3d tp_net = %3d\n", + base, hdr->tp_mac, hdr->tp_net); + + // Invoke data handler + data_handler::result r = (*f)(base, len); + hdr->tp_status = TP_STATUS_KERNEL; // mark it free + + inc_head(); + + if (r & data_handler::DONE) + break; + } + + DEBUG_LOG("|"); + return EB_OK; + } + +} // namespace vrt diff --git a/vrt/lib/socket_rx_buffer.h b/vrt/lib/socket_rx_buffer.h new file mode 100644 index 000000000..053c30c12 --- /dev/null +++ b/vrt/lib/socket_rx_buffer.h @@ -0,0 +1,122 @@ +/* -*- c++ -*- */ +/* + * Copyright 2008,2009 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 this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef INCLUDED_VRT_SOCKET_RX_BUFFER_H +#define INCLUDED_VRT_SOCKET_RX_BUFFER_H + +#include <boost/utility.hpp> +#include <vector> +#include <memory> +#include <stdint.h> + +namespace vrt { + + class data_handler; + + /*! + * \brief high-performance interface to receive datagrams + * + * On many systems it should be possible to implement this on top of libpcap + * + * \internal + */ + class socket_rx_buffer : boost::noncopyable + { + + int d_fd; // socket file descriptor + bool d_using_tpring; // using kernel mapped packet ring + size_t d_buflen; // length of our buffer + uint8_t *d_buf; // packet ring + unsigned int d_frame_nr; // max frames on ring + size_t d_frame_size; // frame storage size + unsigned int d_head; // pointer to next frame + + std::vector<uint8_t *> d_ring; // pointers into buffer + + bool frame_available(); + + void inc_head() + { + if (d_head + 1 >= d_frame_nr) + d_head = 0; + else + d_head = d_head + 1; + } + + bool open(); + bool close(); + bool try_packet_ring(); + + public: + + enum result { + EB_OK, //< everything's fine + EB_ERROR, //< A non-recoverable error occurred + EB_WOULD_BLOCK, //< A timeout of 0 was specified and nothing was ready + EB_TIMED_OUT, //< The timeout expired before anything was ready + }; + + static const unsigned int MAX_PKTLEN; + static const unsigned int MIN_PKTLEN; + + /*! + * \param socket_fd file descriptor that corresponds to a socket + * \param rx_bufsize is a hint as to the number of bytes of memory + * to allocate for received ethernet frames (0 -> reasonable default) + */ + socket_rx_buffer(int socket_fd, size_t rx_bufsize = 0); + ~socket_rx_buffer(); + + /*! + * \brief Call \p f for each frame in the receive buffer. + * \param f is the frame data handler + * \param timeout (in ms) controls behavior when there are no frames to read + * + * If \p timeout is 0, rx_frames will not wait for frames if none are + * available, and f will not be invoked. If \p timeout is -1 (the + * default), rx_frames will block indefinitely until frames are + * available. If \p timeout is positive, it indicates the number of + * milliseconds to wait for a frame to become available. Once the + * timeout has expired, rx_frames will return, f never having been + * invoked. + * + * \p f will be called on each frame that is available. + * \p f returns a bit mask with one of the following set or cleared: + * + * data_handler::DONE - return from rx_frames now even though more frames + * might be available; otherwise continue if more + * frames are ready. + * + * \returns EB_OK if at least one frame was received + * \returns EB_WOULD_BLOCK if \p timeout is 0 and the call would have blocked + * \returns EB_TIMED_OUT if timeout occurred + * \returns EB_ERROR if there was an unrecoverable error. + */ + result rx_frames(data_handler *f, int timeout=-1); + + /* + * \brief Returns maximum possible number of frames in buffer + */ + unsigned int max_frames() const { return d_using_tpring ? d_frame_nr : 0; } + }; + +}; // namespace vrt + +#endif /* INCLUDED_VRT_SOCKET_RX_BUFFER_H */ diff --git a/vrt/vrt.pc.in b/vrt/vrt.pc.in new file mode 100644 index 000000000..af9892943 --- /dev/null +++ b/vrt/vrt.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: vrt +Description: Host implementation of Virtual Radio Transport (VITA-49) +Requires: +Version: @LIBVER@ +Libs: -L${libdir} -lvrt +Cflags: -I${includedir} |