rtl_tcp: add initial component files

For use with the rtl_tcp utility acting as a spectrum server.

The "empty" rtl_tcp= device hint might be used to connect to rtl_tcp
running on local machine.
gr3.6
Dimitri Stolnikov 11 years ago
parent 3a066ff258
commit f94d8da48e
  1. 3
      grc/gen_osmosdr_blocks.py
  2. 10
      lib/CMakeLists.txt
  3. 1
      lib/config.h.in
  4. 13
      lib/osmosdr_source_c_impl.cc
  5. 36
      lib/rtl_tcp/CMakeLists.txt
  6. 230
      lib/rtl_tcp/rtl_tcp_source_c.cc
  7. 82
      lib/rtl_tcp/rtl_tcp_source_c.h
  8. 259
      lib/rtl_tcp/rtl_tcp_source_f.cc
  9. 99
      lib/rtl_tcp/rtl_tcp_source_f.h

@ -112,9 +112,10 @@ The device argument is a delimited string used to locate devices on your system.
Use the device id or name (if applicable) to specify a certain device or list
of devices. If left blank, the first device found will be used.
Examples:
Examples (some arguments may be optional):
fcd=0
rtl=0,rtl_xtal=28.80001e6,tuner_xtal=26e6,buffers=64 ...
rtl_tcp=host=127.0.0.1,port=1234,eof=false,wait=true,psize=16384
uhd=0|name,mcr=52e6,nchan=2,subdev='\\\\'B:0 A:0'\\\\' ...
osmosdr=0|name,mcr=64e6,nchan=5,port=/dev/ttyUSB0 ...
file=/path/to/file.ext,freq=428e6,rate=1e6,repeat=true,throttle=true ...

@ -86,6 +86,16 @@ if(ENABLE_RTL)
GR_INCLUDE_SUBDIRECTORY(rtl)
endif(ENABLE_RTL)
########################################################################
# Setup RTL_TCP component
########################################################################
include(GrComponent)
GR_REGISTER_COMPONENT("Osmocom RTLSDR TCP Client" ENABLE_RTL_TCP GNURADIO_CORE_FOUND)
if(ENABLE_RTL_TCP)
GR_INCLUDE_SUBDIRECTORY(rtl_tcp)
endif(ENABLE_RTL_TCP)
########################################################################
# Setup UHD component
########################################################################

@ -5,6 +5,7 @@
#cmakedefine ENABLE_FCD
#cmakedefine ENABLE_FILE
#cmakedefine ENABLE_RTL
#cmakedefine ENABLE_RTL_TCP
#cmakedefine ENABLE_UHD
#endif // CONFIG_H_IN

@ -46,6 +46,10 @@
#include <rtl_source_c.h>
#endif
#ifdef ENABLE_RTL_TCP
#include <rtl_tcp_source_c.h>
#endif
#ifdef ENABLE_UHD
#include <uhd_source_c.h>
#endif
@ -83,6 +87,7 @@ osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args)
dict.count("fcd") |
dict.count("file") |
dict.count("rtl") |
dict.count("rtl_tcp") |
dict.count("uhd") )
{
device_specified = true;
@ -159,6 +164,14 @@ osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args)
}
#endif
#ifdef ENABLE_RTL_TCP
if ( dict.count("rtl_tcp") ) {
rtl_tcp_source_c_sptr src = make_rtl_tcp_source_c( arg );
connect(src, 0, self(), channel++);
_devs.push_back( src.get() );
}
#endif
#ifdef ENABLE_UHD
if ( dict.count("uhd") ) {
uhd_source_c_sptr src = make_uhd_source_c( arg );

@ -0,0 +1,36 @@
# 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(rtl_tcp_srcs
${CMAKE_CURRENT_SOURCE_DIR}/rtl_tcp_source_f.cc
${CMAKE_CURRENT_SOURCE_DIR}/rtl_tcp_source_c.cc
)
########################################################################
# Append gnuradio-osmosdr library sources
########################################################################
list(APPEND gr_osmosdr_srcs ${rtl_tcp_srcs})
list(APPEND gr_osmosdr_libs ${GNURADIO_CORE_LIBRARIES})

@ -0,0 +1,230 @@
/* -*- c++ -*- */
/*
* Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
*
* 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 <fstream>
#include <string>
#include <sstream>
#include <boost/assign.hpp>
#include <gr_io_signature.h>
#include <gr_deinterleave.h>
#include <gr_float_to_complex.h>
#include "rtl_tcp_source_c.h"
#include <osmosdr_arg_helpers.h>
using namespace boost::assign;
rtl_tcp_source_c_sptr make_rtl_tcp_source_c(const std::string &args)
{
return gnuradio::get_initial_sptr(new rtl_tcp_source_c(args));
}
rtl_tcp_source_c::rtl_tcp_source_c(const std::string &args) :
gr_hier_block2("rtl_tcp_source_c",
gr_make_io_signature (0, 0, 0),
gr_make_io_signature (1, 1, sizeof (gr_complex)))
{
std::string host = "127.0.0.1";
unsigned short port = 1234;
bool eof = false;
bool wait = true;
int payload_size = 16384;
_freq = 0;
_rate = 0;
dict_t dict = params_to_dict(args);
if (dict.count("host"))
host = dict["host"];
if (dict.count("port"))
port = boost::lexical_cast< unsigned short >( dict["port"] );
if (dict.count("eof"))
eof = "true" == dict["eof"] ? true : false;
if (dict.count("wait"))
wait = "true" == dict["wait"] ? true : false;
if (dict.count("psize"))
payload_size = boost::lexical_cast< int >( dict["psize"] );
if (!host.length())
host = "127.0.0.1";
if (0 == port)
port = 1234;
if (payload_size <= 0)
payload_size = 16384;
_src = make_rtl_tcp_source_f(sizeof(float), host.c_str(), port, payload_size,
eof, wait);
/* rtl tcp source provides a stream of interleaved IQ floats */
gr_deinterleave_sptr deinterleave = gr_make_deinterleave(sizeof(float));
/* block to convert deinterleaved floats to a complex stream */
gr_float_to_complex_sptr f2c = gr_make_float_to_complex(1);
connect(_src, 0, deinterleave, 0);
connect(deinterleave, 0, f2c, 0); /* I */
connect(deinterleave, 1, f2c, 1); /* Q */
connect(f2c, 0, self(), 0);
}
rtl_tcp_source_c::~rtl_tcp_source_c()
{
}
gr_basic_block_sptr rtl_tcp_source_c::self()
{
return gr_hier_block2::self();
}
std::string rtl_tcp_source_c::name()
{
return "RTL TCP Client Source";
}
size_t rtl_tcp_source_c::get_num_channels( void )
{
return 1;
}
osmosdr::meta_range_t rtl_tcp_source_c::get_sample_rates( void )
{
osmosdr::meta_range_t range;
range += osmosdr::range_t( get_sample_rate() );
return range;
}
double rtl_tcp_source_c::set_sample_rate( double rate )
{
return get_sample_rate();
}
double rtl_tcp_source_c::get_sample_rate( void )
{
return _rate;
}
osmosdr::freq_range_t rtl_tcp_source_c::get_freq_range( size_t chan )
{
osmosdr::freq_range_t range(50e6, 2.2e6, 100);
return range;
}
double rtl_tcp_source_c::set_center_freq( double freq, size_t chan )
{
_src->set_freq( int(freq) );
_freq = freq;
return get_center_freq(chan);
}
double rtl_tcp_source_c::get_center_freq( size_t chan )
{
return _freq;
}
double rtl_tcp_source_c::set_freq_corr( double ppm, size_t chan )
{
return get_freq_corr( chan );
}
double rtl_tcp_source_c::get_freq_corr( size_t chan )
{
return 0;
}
std::vector<std::string> rtl_tcp_source_c::get_gain_names( size_t chan )
{
return std::vector< std::string >();
}
osmosdr::gain_range_t rtl_tcp_source_c::get_gain_range( size_t chan )
{
osmosdr::gain_range_t range(0, 0);
return range;
}
osmosdr::gain_range_t rtl_tcp_source_c::get_gain_range( const std::string & name, size_t chan )
{
return get_gain_range( chan );
}
bool rtl_tcp_source_c::set_gain_mode( bool mode, size_t chan )
{
return get_gain_mode(chan);
}
bool rtl_tcp_source_c::get_gain_mode( size_t chan )
{
return true;
}
double rtl_tcp_source_c::set_gain( double gain, size_t chan )
{
return get_gain(chan);
}
double rtl_tcp_source_c::set_gain( double gain, const std::string & name, size_t chan )
{
return set_gain(chan);
}
double rtl_tcp_source_c::get_gain( size_t chan )
{
return 0;
}
double rtl_tcp_source_c::get_gain( const std::string & name, size_t chan )
{
return get_gain(chan);
}
std::vector< std::string > rtl_tcp_source_c::get_antennas( size_t chan )
{
std::vector< std::string > antennas;
antennas += get_antenna(chan);
return antennas;
}
std::string rtl_tcp_source_c::set_antenna( const std::string & antenna, size_t chan )
{
return get_antenna(chan);
}
std::string rtl_tcp_source_c::get_antenna( size_t chan )
{
return "ANT";
}

@ -0,0 +1,82 @@
/* -*- c++ -*- */
/*
* Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
*
* 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 RTL_TCP_SOURCE_C_H
#define RTL_TCP_SOURCE_C_H
#include <gr_hier_block2.h>
#include "osmosdr_src_iface.h"
#include "rtl_tcp_source_f.h"
class rtl_tcp_source_c;
typedef boost::shared_ptr< rtl_tcp_source_c > rtl_tcp_source_c_sptr;
rtl_tcp_source_c_sptr make_rtl_tcp_source_c( const std::string & args = "" );
class rtl_tcp_source_c :
public gr_hier_block2,
public osmosdr_src_iface
{
private:
friend rtl_tcp_source_c_sptr make_rtl_tcp_source_c(const std::string &args);
rtl_tcp_source_c(const std::string &args);
public:
~rtl_tcp_source_c();
gr_basic_block_sptr self();
std::string name();
size_t get_num_channels( void );
osmosdr::meta_range_t get_sample_rates( void );
double set_sample_rate( double rate );
double get_sample_rate( void );
osmosdr::freq_range_t get_freq_range( size_t chan = 0 );
double set_center_freq( double freq, size_t chan = 0 );
double get_center_freq( size_t chan = 0 );
double set_freq_corr( double ppm, size_t chan = 0 );
double get_freq_corr( size_t chan = 0 );
std::vector<std::string> get_gain_names( size_t chan = 0 );
osmosdr::gain_range_t get_gain_range( size_t chan = 0 );
osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 );
bool set_gain_mode( bool mode, size_t chan = 0 );
bool get_gain_mode( size_t chan = 0 );
double set_gain( double gain, size_t chan = 0 );
double set_gain( double gain, const std::string & name, size_t chan = 0 );
double get_gain( size_t chan = 0 );
double get_gain( const std::string & name, size_t chan = 0 );
std::vector< std::string > get_antennas( size_t chan = 0 );
std::string set_antenna( const std::string & antenna, size_t chan = 0 );
std::string get_antenna( size_t chan = 0 );
private:
double _freq, _rate;
rtl_tcp_source_f_sptr _src;
};
#endif // RTL_TCP_SOURCE_C_H

@ -0,0 +1,259 @@
/* -*- c++ -*- */
/*
* Copyright 2012 Hoernchen <la@tfc-server.de>
*
* 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.
*/
//#define HAVE_WINDOWS_H
#include <rtl_tcp_source_f.h>
#include <gr_io_signature.h>
#include <stdexcept>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define USE_SELECT 1 // non-blocking receive on all platforms
#define USE_RCV_TIMEO 0 // non-blocking receive on all but Cygwin
#define SRC_VERBOSE 0
#define SNK_VERBOSE 0
static int is_error( int perr )
{
// Compare error to posix error code; return nonzero if match.
#if defined(USING_WINSOCK)
#define ENOPROTOOPT 109
// All codes to be checked for must be defined below
int werr = WSAGetLastError();
switch( werr ) {
case WSAETIMEDOUT:
return( perr == EAGAIN );
case WSAENOPROTOOPT:
return( perr == ENOPROTOOPT );
default:
fprintf(stderr,"rtl_tcp_source_f: unknown error %d WS err %d \n", perr, werr );
throw std::runtime_error("internal error");
}
return 0;
#else
return( perr == errno );
#endif
}
static void report_error( const char *msg1, const char *msg2 )
{
// Deal with errors, both posix and winsock
#if defined(USING_WINSOCK)
int werr = WSAGetLastError();
fprintf(stderr, "%s: winsock error %d\n", msg1, werr );
#else
perror(msg1);
#endif
if( msg2 != NULL )
throw std::runtime_error(msg2);
return;
}
rtl_tcp_source_f::rtl_tcp_source_f(size_t itemsize,
const char *host,
unsigned short port,
int payload_size,
bool eof,
bool wait)
: gr_sync_block ("rtl_tcp_source_f",
gr_make_io_signature(0, 0, 0),
gr_make_io_signature(1, 1, sizeof(float))),
d_itemsize(itemsize),
d_payload_size(payload_size),
d_eof(eof),
d_wait(wait),
d_socket(-1),
d_residual(0),
d_temp_offset(0),
curr_freq(0)
{
int ret = 0;
struct sockaddr_in cliaddr;
socklen_t clilen;
#if defined(USING_WINSOCK) // for Windows (with MinGW)
// initialize winsock DLL
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if( iResult != NO_ERROR ) {
report_error( "rtl_tcp_source_f WSAStartup", "can't open socket" );
}
#endif
// Set up the address stucture for the source address and port numbers
// Get the source IP address from the host name
struct addrinfo *ip_src; // store the source IP address to use
struct addrinfo hints;
memset( (void*)&hints, 0, sizeof(hints) );
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
char port_str[12];
sprintf( port_str, "%d", port );
// FIXME leaks if report_error throws below
ret = getaddrinfo( host, port_str, &hints, &ip_src );
if( ret != 0 )
report_error("rtl_tcp_source_f/getaddrinfo",
"can't initialize source socket" );
// FIXME leaks if report_error throws below
d_temp_buff = new unsigned char[d_payload_size]; // allow it to hold up to payload_size bytes
d_LUT= new float[0xff+1];
for(int i=0; i <=(0xff);++i){
d_LUT[i] = (((float)(i&0xff))-127.5f)*(1.0f/128.0f);
}
// create socket
d_socket = socket(ip_src->ai_family, ip_src->ai_socktype,
ip_src->ai_protocol);
if(d_socket == -1) {
report_error("socket open","can't open socket");
}
// Turn on reuse address
int opt_val = 1;
if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -1) {
report_error("SO_REUSEADDR","can't set socket option SO_REUSEADDR");
}
// Don't wait when shutting down
linger lngr;
lngr.l_onoff = 1;
lngr.l_linger = 0;
if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) {
if( !is_error(ENOPROTOOPT) ) { // no SO_LINGER for SOCK_DGRAM on Windows
report_error("SO_LINGER","can't set socket option SO_LINGER");
}
}
#if USE_RCV_TIMEO
// Set a timeout on the receive function to not block indefinitely
// This value can (and probably should) be changed
// Ignored on Cygwin
#if defined(USING_WINSOCK)
DWORD timeout = 1000; // milliseconds
#else
timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
#endif
if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (optval_t)&timeout, sizeof(timeout)) == -1) {
report_error("SO_RCVTIMEO","can't set socket option SO_RCVTIMEO");
}
#endif // USE_RCV_TIMEO
while(connect(d_socket, ip_src->ai_addr, ip_src->ai_addrlen) != 0);
freeaddrinfo(ip_src);
int flag = 1;
setsockopt(d_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,sizeof(flag));
}
rtl_tcp_source_f_sptr make_rtl_tcp_source_f (size_t itemsize,
const char *ipaddr,
unsigned short port,
int payload_size,
bool eof,
bool wait)
{
return gnuradio::get_initial_sptr(new rtl_tcp_source_f (
itemsize,
ipaddr,
port,
payload_size,
eof,
wait));
}
rtl_tcp_source_f::~rtl_tcp_source_f ()
{
delete [] d_temp_buff;
if (d_socket != -1){
shutdown(d_socket, SHUT_RDWR);
#if defined(USING_WINSOCK)
closesocket(d_socket);
#else
::close(d_socket);
#endif
d_socket = -1;
}
#if defined(USING_WINSOCK) // for Windows (with MinGW)
// free winsock resources
WSACleanup();
#endif
}
int rtl_tcp_source_f::work (int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items)
{
float *out = (float *) output_items[0];
ssize_t r=0, nbytes=0, bytes_received=0;
ssize_t total_bytes = (ssize_t)(d_itemsize*noutput_items);
int bytesleft = noutput_items;
int index = 0;
int receivedbytes = 0;
while(bytesleft > 0) {
receivedbytes = recv(d_socket, (char*)&d_temp_buff[index], bytesleft, 0);
if(receivedbytes == -1 && !is_error(EAGAIN)){
fprintf(stderr, "socket error\n");
return -1;
}
bytesleft -= receivedbytes;
index += receivedbytes;
}
r = noutput_items;
for(int i=0; i<r; ++i)
out[i]=d_LUT[*(d_temp_buff+d_temp_offset+i)];
return r;
}
#ifdef _WIN32
#define __attribute__(x)
#pragma pack(push, 1)
#endif
struct command{
unsigned char cmd;
unsigned int param;
}__attribute__((packed));
#ifdef _WIN32
#pragma pack(pop)
#endif
void rtl_tcp_source_f::set_freq(int freq)
{
struct command cmd;
if (freq != curr_freq) {
cmd.cmd = 0x01;
cmd.param = freq;
send(d_socket, (const char*)&cmd, sizeof(cmd), 0);
curr_freq = freq;
}
}

@ -0,0 +1,99 @@
/* -*- c++ -*- */
/*
* Copyright 2012 Hoernchen <la@tfc-server.de>
*
* 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 RTL_TCP_SOURCE_F_H
#define RTL_TCP_SOURCE_F_H
#include <osmosdr_api.h>
#include <gr_sync_block.h>
#include <gruel/thread.h>
#if defined(_WIN32)
// if not posix, assume winsock
#pragma comment(lib, "ws2_32.lib")
#define USING_WINSOCK
#include <winsock2.h>
#include <ws2tcpip.h>
#define SHUT_RDWR 2
typedef char* optval_t;
#else
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
typedef void* optval_t;
#endif
#define ssize_t int
class rtl_tcp_source_f;
typedef boost::shared_ptr<rtl_tcp_source_f> rtl_tcp_source_f_sptr;
OSMOSDR_API rtl_tcp_source_f_sptr make_rtl_tcp_source_f (
size_t itemsize,
const char *host,
unsigned short port,
int payload_size,
bool eof,
bool wait);
class OSMOSDR_API rtl_tcp_source_f : public gr_sync_block
{
private:
size_t d_itemsize;
int d_payload_size; // maximum transmission unit (packet length)
bool d_eof; // zero-length packet is EOF
bool d_wait; // wait if data if not immediately available
int d_socket; // handle to socket
unsigned char *d_temp_buff; // hold buffer between calls
ssize_t d_residual; // hold information about number of bytes stored in the temp buffer
size_t d_temp_offset; // point to temp buffer location offset
int curr_freq;
float *d_LUT;
private:
rtl_tcp_source_f(size_t itemsize, const char *host,
unsigned short port, int payload_size, bool eof, bool wait);
// The friend declaration allows make_source_c to
// access the private constructor.
friend OSMOSDR_API rtl_tcp_source_f_sptr make_rtl_tcp_source_f (
size_t itemsize,
const char *host,
unsigned short port,
int payload_size,
bool eof,
bool wait);
public:
~rtl_tcp_source_f();
int work(int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items);
void set_freq(int freq);
};
#endif /* RTL_TCP_SOURCE_F_H */
Loading…
Cancel
Save