From 755da481d447186ddd5f53a3284293f8c37a2db0 Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Thu, 26 Apr 2012 19:23:55 +0200 Subject: [PATCH] osmosdr: add initial component files --- lib/osmosdr/CMakeLists.txt | 37 ++++++++++++ lib/osmosdr/osmosdr_control.cc | 105 +++++++++++++++++++++++++++++++++ lib/osmosdr/osmosdr_control.h | 67 +++++++++++++++++++++ lib/osmosdr/osmosdr_snk_c.cc | 88 +++++++++++++++++++++++++++ lib/osmosdr/osmosdr_snk_c.h | 76 ++++++++++++++++++++++++ lib/osmosdr/osmosdr_src_c.cc | 84 ++++++++++++++++++++++++++ lib/osmosdr/osmosdr_src_c.h | 76 ++++++++++++++++++++++++ 7 files changed, 533 insertions(+) create mode 100644 lib/osmosdr/CMakeLists.txt create mode 100644 lib/osmosdr/osmosdr_control.cc create mode 100644 lib/osmosdr/osmosdr_control.h create mode 100644 lib/osmosdr/osmosdr_snk_c.cc create mode 100644 lib/osmosdr/osmosdr_snk_c.h create mode 100644 lib/osmosdr/osmosdr_src_c.cc create mode 100644 lib/osmosdr/osmosdr_src_c.h diff --git a/lib/osmosdr/CMakeLists.txt b/lib/osmosdr/CMakeLists.txt new file mode 100644 index 0000000..50275a1 --- /dev/null +++ b/lib/osmosdr/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright 2012 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. + +######################################################################## +# This file included, use CMake directory variables +######################################################################## + +include_directories(APPEND ${CMAKE_CURRENT_SOURCE_DIR}) + +set(osmosdr_srcs + ${CMAKE_CURRENT_SOURCE_DIR}/osmosdr_control.cc + ${CMAKE_CURRENT_SOURCE_DIR}/osmosdr_snk_c.cc + ${CMAKE_CURRENT_SOURCE_DIR}/osmosdr_src_c.cc +) + +######################################################################## +# Append gnuradio-osmosdr library sources +######################################################################## +list(APPEND gr_osmosdr_srcs ${osmosdr_srcs}) +list(APPEND gr_osmosdr_libs ${GNURADIO_AUDIO_LIBRARIES}) + diff --git a/lib/osmosdr/osmosdr_control.cc b/lib/osmosdr/osmosdr_control.cc new file mode 100644 index 0000000..75f5398 --- /dev/null +++ b/lib/osmosdr/osmosdr_control.cc @@ -0,0 +1,105 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Dimitri Stolnikov + * + * 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 +#include +#include + +#include + +#include "osmosdr_control.h" + +using namespace boost::assign; + +osmosdr_control::osmosdr_control(const std::string &args) +{ + /* lookup acm control channel device name for a given alsa device name */ + + /* + if (args.empty()) + pick first available device or throw an exception(); + */ +} + +osmosdr_control::~osmosdr_control() +{ +} + +/* + 1 [OsmoSDR ]: USB-Audio - OsmoSDR + sysmocom OsmoSDR at usb-0000:00:06.1-2, high speed +*/ +std::vector< std::string > osmosdr_control::find_devices() +{ + std::vector< std::string > devices; + + std::string line; + std::ifstream cards( "/proc/asound/cards" ); + if ( cards.is_open() ) + { + while ( cards.good() ) + { + getline (cards, line); + + if ( line.find( "USB-Audio - OsmoSDR" ) != std::string::npos ) + { + int id; + std::istringstream( line ) >> id; + + std::ostringstream hw_id; + hw_id << "hw:" << id; // build alsa identifier + + devices += hw_id.str(); + } + } + + cards.close(); + } + + return devices; +} + +std::string osmosdr_control::audio_dev_name() +{ + return "hw:1"; +} + +std::string osmosdr_control::control_dev_name() +{ + return "/dev/ttyUSB0"; +} + +osmosdr_rx_control::osmosdr_rx_control(const std::string &args) : + osmosdr_control(args) +{ +} + +osmosdr_rx_control::~osmosdr_rx_control() +{ +} + +osmosdr_tx_control::osmosdr_tx_control(const std::string &args) : + osmosdr_control(args) +{ +} + +osmosdr_tx_control::~osmosdr_tx_control() +{ +} diff --git a/lib/osmosdr/osmosdr_control.h b/lib/osmosdr/osmosdr_control.h new file mode 100644 index 0000000..ea9b298 --- /dev/null +++ b/lib/osmosdr/osmosdr_control.h @@ -0,0 +1,67 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Dimitri Stolnikov + * + * 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 OSMOSDR_CONTROL_H +#define OSMOSDR_CONTROL_H + +#include +#include +#include + +/*! + * session object to cdc-acm based control channel of the device + */ +class osmosdr_control +{ +public: + osmosdr_control(const std::string &args); + virtual ~osmosdr_control(); + + /*! + * Discovers all devices connected to the host computer. + * \return a vector of device addresses + */ + static std::vector< std::string > find_devices(); + +protected: + std::string audio_dev_name(); + std::string control_dev_name(); +}; + +/*! + * osmosdr_source class derives from this class to be able to control the device + */ +class osmosdr_rx_control : public osmosdr_control +{ +public: + osmosdr_rx_control(const std::string &args); + virtual ~osmosdr_rx_control(); +}; + +/*! + * osmosdr_sink class derives from this class to be able to control the device + */ +class osmosdr_tx_control : public osmosdr_control +{ +public: + osmosdr_tx_control(const std::string &args); + virtual ~osmosdr_tx_control(); +}; + +#endif // OSMOSDR_CONTROL_H diff --git a/lib/osmosdr/osmosdr_snk_c.cc b/lib/osmosdr/osmosdr_snk_c.cc new file mode 100644 index 0000000..0a61bc1 --- /dev/null +++ b/lib/osmosdr/osmosdr_snk_c.cc @@ -0,0 +1,88 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Dimitri Stolnikov + * + * 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. + */ + +/* + * config.h is generated by configure. It contains the results + * of probing for features, options etc. It should be the first + * file included in your .cc file. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include + +/* + * Create a new instance of osmosdr_snk_c and return + * a boost shared_ptr. This is effectively the public constructor. + */ +osmosdr_snk_c_sptr +osmosdr_make_snk_c (const std::string &args) +{ + return gnuradio::get_initial_sptr(new osmosdr_snk_c (args)); +} + +/* + * Specify constraints on number of input and output streams. + * This info is used to construct the input and output signatures + * (2nd & 3rd args to gr_block's constructor). The input and + * output signatures are used by the runtime system to + * check that a valid number and type of inputs and outputs + * are connected to this block. In this case, we accept + * only 1 input and 1 output. + */ +static const int MIN_IN = 1; // mininum number of input streams +static const int MAX_IN = 1; // maximum number of input streams +static const int MIN_OUT = 0; // minimum number of output streams +static const int MAX_OUT = 0; // maximum number of output streams + +/* + * The private constructor + */ +osmosdr_snk_c::osmosdr_snk_c (const std::string & args) + : gr_hier_block2 ("osmosdr_snk_c", + gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)), + gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex))), + osmosdr_tx_control(args) +{ + throw std::runtime_error("FIXME: OsmoSDR sink is not yet implemented"); + + /* Audio sink; sample rate is 96kHz by default */ + audio_sink::sptr snk = audio_make_sink(96000, audio_dev_name(), true); + + gr_complex_to_real_sptr real_part = gr_make_complex_to_real(1); + gr_complex_to_imag_sptr imag_part = gr_make_complex_to_imag(1); + + connect(self(), 0, real_part, 0); + connect(self(), 0, imag_part, 0); + connect(imag_part, 0, snk, 0); /* Left is I */ + connect(real_part, 0, snk, 1); /* Right is Q */ +} + +/* + * Our virtual destructor. + */ +osmosdr_snk_c::~osmosdr_snk_c () +{ + // nothing else required in this example +} diff --git a/lib/osmosdr/osmosdr_snk_c.h b/lib/osmosdr/osmosdr_snk_c.h new file mode 100644 index 0000000..d85562f --- /dev/null +++ b/lib/osmosdr/osmosdr_snk_c.h @@ -0,0 +1,76 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Dimitri Stolnikov + * + * 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_OSMOSDR_SNK_C_H +#define INCLUDED_OSMOSDR_SNK_C_H + +#include +#include +#include + +class osmosdr_snk_c; + +/* + * We use boost::shared_ptr's instead of raw pointers for all access + * to gr_blocks (and many other data structures). The shared_ptr gets + * us transparent reference counting, which greatly simplifies storage + * management issues. This is especially helpful in our hybrid + * C++ / Python system. + * + * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm + * + * As a convention, the _sptr suffix indicates a boost::shared_ptr + */ +typedef boost::shared_ptr osmosdr_snk_c_sptr; + +/*! + * \brief Return a shared_ptr to a new instance of osmosdr_snk_c. + * + * To avoid accidental use of raw pointers, osmosdr_snk_c's + * constructor is private. osmosdr_make_snk_c is the public + * interface for creating new instances. + */ +OSMOSDR_API osmosdr_snk_c_sptr osmosdr_make_snk_c (const std::string & args = ""); + +/*! + * \brief Takes a stream of complex samples. + * \ingroup block + * + * This uses the preferred technique: subclassing gr_hier_block2. + */ +class OSMOSDR_API osmosdr_snk_c : public gr_hier_block2, + public osmosdr_tx_control +{ +private: + // The friend declaration allows osmosdr_make_snk_c to + // access the private constructor. + + friend OSMOSDR_API osmosdr_snk_c_sptr osmosdr_make_snk_c (const std::string & args); + + /*! + * \brief Takes a stream of complex samples. + */ + osmosdr_snk_c (const std::string & args); // private constructor + + public: + ~osmosdr_snk_c (); // public destructor + +}; + +#endif /* INCLUDED_OSMOSDR_SNK_C_H */ diff --git a/lib/osmosdr/osmosdr_src_c.cc b/lib/osmosdr/osmosdr_src_c.cc new file mode 100644 index 0000000..b239237 --- /dev/null +++ b/lib/osmosdr/osmosdr_src_c.cc @@ -0,0 +1,84 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Dimitri Stolnikov + * + * 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. + */ +/* + * config.h is generated by configure. It contains the results + * of probing for features, options etc. It should be the first + * file included in your .cc file. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "osmosdr_src_c.h" +#include +#include +#include + +/* + * Create a new instance of osmosdr_src_c and return + * a boost shared_ptr. This is effectively the public constructor. + */ +osmosdr_src_c_sptr +osmosdr_make_src_c (const std::string &args) +{ + return gnuradio::get_initial_sptr(new osmosdr_src_c (args)); +} + +/* + * Specify constraints on number of input and output streams. + * This info is used to construct the input and output signatures + * (2nd & 3rd args to gr_block's constructor). The input and + * output signatures are used by the runtime system to + * check that a valid number and type of inputs and outputs + * are connected to this block. In this case, we accept + * only 1 input and 1 output. + */ +static const int MIN_IN = 0; // mininum number of input streams +static const int MAX_IN = 0; // maximum number of input streams +static const int MIN_OUT = 1; // minimum number of output streams +static const int MAX_OUT = 1; // maximum number of output streams + +/* + * The private constructor + */ +osmosdr_src_c::osmosdr_src_c (const std::string &args) + : gr_hier_block2 ("osmosdr_src_c", + gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)), + gr_make_io_signature (MIN_OUT, MAX_OUT, sizeof (gr_complex))), + osmosdr_rx_control(args) +{ + /* Audio source; sample rate is 500kHz by default */ + audio_source::sptr src = audio_make_source(500000, audio_dev_name(), true); + + /* block to convert stereo audio to a complex stream */ + gr_float_to_complex_sptr f2c = gr_make_float_to_complex(1); + + connect(src, 0, f2c, 0); /* Left is I */ + connect(src, 1, f2c, 1); /* Right is Q */ + connect(f2c, 0, self(), 0); +} + +/* + * Our virtual destructor. + */ +osmosdr_src_c::~osmosdr_src_c () +{ + // nothing else required in this example +} diff --git a/lib/osmosdr/osmosdr_src_c.h b/lib/osmosdr/osmosdr_src_c.h new file mode 100644 index 0000000..5b639b2 --- /dev/null +++ b/lib/osmosdr/osmosdr_src_c.h @@ -0,0 +1,76 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 Dimitri Stolnikov + * + * 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_OSMOSDR_SRC_C_H +#define INCLUDED_OSMOSDR_SRC_C_H + +#include +#include +#include + +class osmosdr_src_c; + +/* + * We use boost::shared_ptr's instead of raw pointers for all access + * to gr_blocks (and many other data structures). The shared_ptr gets + * us transparent reference counting, which greatly simplifies storage + * management issues. This is especially helpful in our hybrid + * C++ / Python system. + * + * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm + * + * As a convention, the _sptr suffix indicates a boost::shared_ptr + */ +typedef boost::shared_ptr osmosdr_src_c_sptr; + +/*! + * \brief Return a shared_ptr to a new instance of osmosdr_src_c. + * + * To avoid accidental use of raw pointers, osmosdr_src_c's + * constructor is private. osmosdr_make_src_c is the public + * interface for creating new instances. + */ +OSMOSDR_API osmosdr_src_c_sptr osmosdr_make_src_c (const std::string & args = ""); + +/*! + * \brief Provides a stream of complex samples. + * \ingroup block + * + * \sa osmosdr_sink_c for a version that subclasses gr_hier_block2. + */ +class OSMOSDR_API osmosdr_src_c : public gr_hier_block2, + public osmosdr_rx_control +{ +private: + // The friend declaration allows osmosdr_make_src_c to + // access the private constructor. + + friend OSMOSDR_API osmosdr_src_c_sptr osmosdr_make_src_c (const std::string & args); + + /*! + * \brief Provides a stream of complex samples. + */ + osmosdr_src_c (const std::string & args); // private constructor + + public: + ~osmosdr_src_c (); // public destructor + +}; + +#endif /* INCLUDED_OSMOSDR_SRC_C_H */