rtl_tcp: use gnuradio convention for indent

This commit is contained in:
Dimitri Stolnikov 2013-01-02 00:29:07 +01:00
parent 6ae9c1b4dc
commit 6ad2043130
3 changed files with 205 additions and 206 deletions

View File

@ -37,18 +37,18 @@ using namespace boost::assign;
static std::string get_tuner_name( enum rtlsdr_tuner tuner_type ) static std::string get_tuner_name( enum rtlsdr_tuner tuner_type )
{ {
if ( RTLSDR_TUNER_E4000 == tuner_type ) if ( RTLSDR_TUNER_E4000 == tuner_type )
return "E4000"; return "E4000";
else if ( RTLSDR_TUNER_FC0012 == tuner_type ) else if ( RTLSDR_TUNER_FC0012 == tuner_type )
return "FC0012"; return "FC0012";
else if ( RTLSDR_TUNER_FC0013 == tuner_type ) else if ( RTLSDR_TUNER_FC0013 == tuner_type )
return "FC0013"; return "FC0013";
else if ( RTLSDR_TUNER_FC2580 == tuner_type ) else if ( RTLSDR_TUNER_FC2580 == tuner_type )
return "FC2580"; return "FC2580";
else if ( RTLSDR_TUNER_R820T == tuner_type ) else if ( RTLSDR_TUNER_R820T == tuner_type )
return "R820T"; return "R820T";
else else
return "Unknown"; return "Unknown";
} }
rtl_tcp_source_c_sptr make_rtl_tcp_source_c(const std::string &args) rtl_tcp_source_c_sptr make_rtl_tcp_source_c(const std::string &args)
@ -96,16 +96,15 @@ rtl_tcp_source_c::rtl_tcp_source_c(const std::string &args) :
if (payload_size <= 0) if (payload_size <= 0)
payload_size = 16384; payload_size = 16384;
_src = make_rtl_tcp_source_f(sizeof(float), host.c_str(), port, payload_size, _src = make_rtl_tcp_source_f(sizeof(float), host.c_str(), port, payload_size);
false, false);
if ( _src->get_tuner_type() != RTLSDR_TUNER_UNKNOWN ) if ( _src->get_tuner_type() != RTLSDR_TUNER_UNKNOWN )
{ {
std::cerr << "The RTL TCP server reports a " std::cerr << "The RTL TCP server reports a "
<< get_tuner_name( _src->get_tuner_type() ) << get_tuner_name( _src->get_tuner_type() )
<< " tuner with " << _src->get_tuner_gain_count() << " tuner with " << _src->get_tuner_gain_count()
<< " selectable gains." << " selectable gains."
<< std::endl; << std::endl;
} }
set_gain_mode(false); /* enable manual gain mode by default */ set_gain_mode(false); /* enable manual gain mode by default */
@ -245,16 +244,16 @@ osmosdr::gain_range_t rtl_tcp_source_c::get_gain_range( size_t chan )
/* all gain values are expressed in tenths of a dB */ /* all gain values are expressed in tenths of a dB */
const int e4k_gains[] = { -10, 15, 40, 65, 90, 115, 140, 165, 190, 215, const int e4k_gains[] = { -10, 15, 40, 65, 90, 115, 140, 165, 190, 215,
240, 290, 340, 420 }; 240, 290, 340, 420 };
const int fc0012_gains[] = { -99, -40, 71, 179, 192 }; const int fc0012_gains[] = { -99, -40, 71, 179, 192 };
const int fc0013_gains[] = { -99, -73, -65, -63, -60, -58, -54, 58, 61, const int fc0013_gains[] = { -99, -73, -65, -63, -60, -58, -54, 58, 61,
63, 65, 67, 68, 70, 71, 179, 181, 182, 63, 65, 67, 68, 70, 71, 179, 181, 182,
184, 186, 188, 191, 197 }; 184, 186, 188, 191, 197 };
const int fc2580_gains[] = { 0 /* no gain values */ }; const int fc2580_gains[] = { 0 /* no gain values */ };
const int r820t_gains[] = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157, const int r820t_gains[] = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157,
166, 197, 207, 229, 254, 280, 297, 328, 166, 197, 207, 229, 254, 280, 297, 328,
338, 364, 372, 386, 402, 421, 434, 439, 338, 364, 372, 386, 402, 421, 434, 439,
445, 480, 496 }; 445, 480, 496 };
const int unknown_gains[] = { 0 /* no gain values */ }; const int unknown_gains[] = { 0 /* no gain values */ };
const int *ptr = NULL; const int *ptr = NULL;

View File

@ -1,7 +1,7 @@
/* -*- c++ -*- */ /* -*- c++ -*- */
/* /*
* Copyright 2012 Hoernchen <la@tfc-server.de> * Copyright 2012 Hoernchen <la@tfc-server.de>
* Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net> * Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
* *
* GNU Radio is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -34,13 +34,13 @@
#include <WinSock2.h> #include <WinSock2.h>
#endif #endif
/* copied from rtl sdr code */ /* copied from rtl sdr code */
typedef struct { /* structure size must be multiple of 2 bytes */ typedef struct { /* structure size must be multiple of 2 bytes */
char magic[4]; char magic[4];
uint32_t tuner_type; uint32_t tuner_type;
uint32_t tuner_gain_count; uint32_t tuner_gain_count;
} dongle_info_t; } dongle_info_t;
#define USE_SELECT 1 // non-blocking receive on all platforms #define USE_SELECT 1 // non-blocking receive on all platforms
#define USE_RCV_TIMEO 0 // non-blocking receive on all but Cygwin #define USE_RCV_TIMEO 0 // non-blocking receive on all but Cygwin
#define SRC_VERBOSE 0 #define SRC_VERBOSE 0
@ -48,38 +48,38 @@ typedef struct { /* structure size must be multiple of 2 bytes */
static int is_error( int perr ) static int is_error( int perr )
{ {
// Compare error to posix error code; return nonzero if match. // Compare error to posix error code; return nonzero if match.
#if defined(USING_WINSOCK) #if defined(USING_WINSOCK)
#define ENOPROTOOPT 109 #define ENOPROTOOPT 109
// All codes to be checked for must be defined below // All codes to be checked for must be defined below
int werr = WSAGetLastError(); int werr = WSAGetLastError();
switch( werr ) { switch( werr ) {
case WSAETIMEDOUT: case WSAETIMEDOUT:
return( perr == EAGAIN ); return( perr == EAGAIN );
case WSAENOPROTOOPT: case WSAENOPROTOOPT:
return( perr == ENOPROTOOPT ); return( perr == ENOPROTOOPT );
default: default:
fprintf(stderr,"rtl_tcp_source_f: unknown error %d WS err %d \n", perr, werr ); fprintf(stderr,"rtl_tcp_source_f: unknown error %d WS err %d \n", perr, werr );
throw std::runtime_error("internal error"); throw std::runtime_error("internal error");
} }
return 0; return 0;
#else #else
return( perr == errno ); return( perr == errno );
#endif #endif
} }
static void report_error( const char *msg1, const char *msg2 ) static void report_error( const char *msg1, const char *msg2 )
{ {
// Deal with errors, both posix and winsock // Deal with errors, both posix and winsock
#if defined(USING_WINSOCK) #if defined(USING_WINSOCK)
int werr = WSAGetLastError(); int werr = WSAGetLastError();
fprintf(stderr, "%s: winsock error %d\n", msg1, werr ); fprintf(stderr, "%s: winsock error %d\n", msg1, werr );
#else #else
perror(msg1); perror(msg1);
#endif #endif
if( msg2 != NULL ) if( msg2 != NULL )
throw std::runtime_error(msg2); throw std::runtime_error(msg2);
return; return;
} }
rtl_tcp_source_f::rtl_tcp_source_f(size_t itemsize, rtl_tcp_source_f::rtl_tcp_source_f(size_t itemsize,
@ -88,118 +88,118 @@ rtl_tcp_source_f::rtl_tcp_source_f(size_t itemsize,
int payload_size, int payload_size,
bool eof, bool eof,
bool wait) bool wait)
: gr_sync_block ("rtl_tcp_source_f", : gr_sync_block ("rtl_tcp_source_f",
gr_make_io_signature(0, 0, 0), gr_make_io_signature(0, 0, 0),
gr_make_io_signature(1, 1, sizeof(float))), gr_make_io_signature(1, 1, sizeof(float))),
d_itemsize(itemsize), d_itemsize(itemsize),
d_payload_size(payload_size), d_payload_size(payload_size),
d_eof(eof), d_eof(eof),
d_wait(wait), d_wait(wait),
d_socket(-1), d_socket(-1),
d_temp_offset(0) d_temp_offset(0)
{ {
int ret = 0; int ret = 0;
#if defined(USING_WINSOCK) // for Windows (with MinGW) #if defined(USING_WINSOCK) // for Windows (with MinGW)
// initialize winsock DLL // initialize winsock DLL
WSADATA wsaData; WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if( iResult != NO_ERROR ) { if( iResult != NO_ERROR ) {
report_error( "rtl_tcp_source_f WSAStartup", "can't open socket" ); report_error( "rtl_tcp_source_f WSAStartup", "can't open socket" );
} }
#endif #endif
// Set up the address stucture for the source address and port numbers // Set up the address stucture for the source address and port numbers
// Get the source IP address from the host name // Get the source IP address from the host name
struct addrinfo *ip_src; // store the source IP address to use struct addrinfo *ip_src; // store the source IP address to use
struct addrinfo hints; struct addrinfo hints;
memset( (void*)&hints, 0, sizeof(hints) ); memset( (void*)&hints, 0, sizeof(hints) );
hints.ai_family = AF_INET; hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
char port_str[12]; char port_str[12];
sprintf( port_str, "%d", port ); sprintf( port_str, "%d", port );
// FIXME leaks if report_error throws below // FIXME leaks if report_error throws below
ret = getaddrinfo( host, port_str, &hints, &ip_src ); ret = getaddrinfo( host, port_str, &hints, &ip_src );
if( ret != 0 ) if( ret != 0 )
report_error("rtl_tcp_source_f/getaddrinfo", report_error("rtl_tcp_source_f/getaddrinfo",
"can't initialize source socket" ); "can't initialize source socket" );
// FIXME leaks if report_error throws below // 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_temp_buff = new unsigned char[d_payload_size]; // allow it to hold up to payload_size bytes
d_LUT= new float[0xff+1]; d_LUT= new float[0xff+1];
for(int i=0; i <=(0xff);++i){ for(int i=0; i <=(0xff);++i){
d_LUT[i] = (((float)(i&0xff))-127.5f)*(1.0f/128.0f); d_LUT[i] = (((float)(i&0xff))-127.5f)*(1.0f/128.0f);
} }
// create socket // create socket
d_socket = socket(ip_src->ai_family, ip_src->ai_socktype, d_socket = socket(ip_src->ai_family, ip_src->ai_socktype,
ip_src->ai_protocol); ip_src->ai_protocol);
if(d_socket == -1) { if(d_socket == -1) {
report_error("socket open","can't open socket"); report_error("socket open","can't open socket");
} }
// Turn on reuse address // Turn on reuse address
int opt_val = 1; int opt_val = 1;
if(setsockopt(d_socket, SOL_SOCKET, SO_REUSEADDR, (optval_t)&opt_val, sizeof(int)) == -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"); report_error("SO_REUSEADDR","can't set socket option SO_REUSEADDR");
} }
// Don't wait when shutting down // Don't wait when shutting down
linger lngr; linger lngr;
lngr.l_onoff = 1; lngr.l_onoff = 1;
lngr.l_linger = 0; lngr.l_linger = 0;
if(setsockopt(d_socket, SOL_SOCKET, SO_LINGER, (optval_t)&lngr, sizeof(linger)) == -1) { 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 if( !is_error(ENOPROTOOPT) ) { // no SO_LINGER for SOCK_DGRAM on Windows
report_error("SO_LINGER","can't set socket option SO_LINGER"); report_error("SO_LINGER","can't set socket option SO_LINGER");
} }
} }
#if USE_RCV_TIMEO #if USE_RCV_TIMEO
// Set a timeout on the receive function to not block indefinitely // Set a timeout on the receive function to not block indefinitely
// This value can (and probably should) be changed // This value can (and probably should) be changed
// Ignored on Cygwin // Ignored on Cygwin
#if defined(USING_WINSOCK) #if defined(USING_WINSOCK)
DWORD timeout = 1000; // milliseconds DWORD timeout = 1000; // milliseconds
#else #else
timeval timeout; timeval timeout;
timeout.tv_sec = 1; timeout.tv_sec = 1;
timeout.tv_usec = 0; timeout.tv_usec = 0;
#endif #endif
if(setsockopt(d_socket, SOL_SOCKET, SO_RCVTIMEO, (optval_t)&timeout, sizeof(timeout)) == -1) { 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"); report_error("SO_RCVTIMEO","can't set socket option SO_RCVTIMEO");
} }
#endif // USE_RCV_TIMEO #endif // USE_RCV_TIMEO
while(connect(d_socket, ip_src->ai_addr, ip_src->ai_addrlen) != 0); while(connect(d_socket, ip_src->ai_addr, ip_src->ai_addrlen) != 0);
freeaddrinfo(ip_src); freeaddrinfo(ip_src);
int flag = 1; int flag = 1;
setsockopt(d_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,sizeof(flag)); setsockopt(d_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,sizeof(flag));
dongle_info_t dongle_info; dongle_info_t dongle_info;
ret = recv(d_socket, (char*)&dongle_info, sizeof(dongle_info), 0); ret = recv(d_socket, (char*)&dongle_info, sizeof(dongle_info), 0);
if (sizeof(dongle_info) != ret) if (sizeof(dongle_info) != ret)
fprintf(stderr,"failed to read dongle info\n"); fprintf(stderr,"failed to read dongle info\n");
d_tuner_type = RTLSDR_TUNER_UNKNOWN; d_tuner_type = RTLSDR_TUNER_UNKNOWN;
d_tuner_gain_count = 0; d_tuner_gain_count = 0;
if (memcmp(dongle_info.magic, "RTL0", 4) == 0) if (memcmp(dongle_info.magic, "RTL0", 4) == 0)
{ {
d_tuner_type = ntohl(dongle_info.tuner_type); d_tuner_type = ntohl(dongle_info.tuner_type);
d_tuner_gain_count = ntohl(dongle_info.tuner_gain_count); d_tuner_gain_count = ntohl(dongle_info.tuner_gain_count);
} }
} }
rtl_tcp_source_f_sptr make_rtl_tcp_source_f (size_t itemsize, rtl_tcp_source_f_sptr make_rtl_tcp_source_f (size_t itemsize,
const char *ipaddr, const char *ipaddr,
unsigned short port, unsigned short port,
int payload_size, int payload_size,
bool eof, bool eof,
bool wait) bool wait)
{ {
return gnuradio::get_initial_sptr(new rtl_tcp_source_f ( return gnuradio::get_initial_sptr(new rtl_tcp_source_f (
itemsize, itemsize,
ipaddr, ipaddr,
port, port,
@ -210,21 +210,21 @@ rtl_tcp_source_f_sptr make_rtl_tcp_source_f (size_t itemsize,
rtl_tcp_source_f::~rtl_tcp_source_f () rtl_tcp_source_f::~rtl_tcp_source_f ()
{ {
delete [] d_temp_buff; delete [] d_temp_buff;
if (d_socket != -1){ if (d_socket != -1){
shutdown(d_socket, SHUT_RDWR); shutdown(d_socket, SHUT_RDWR);
#if defined(USING_WINSOCK) #if defined(USING_WINSOCK)
closesocket(d_socket); closesocket(d_socket);
#else #else
::close(d_socket); ::close(d_socket);
#endif #endif
d_socket = -1; d_socket = -1;
} }
#if defined(USING_WINSOCK) // for Windows (with MinGW) #if defined(USING_WINSOCK) // for Windows (with MinGW)
// free winsock resources // free winsock resources
WSACleanup(); WSACleanup();
#endif #endif
} }
@ -232,29 +232,29 @@ int rtl_tcp_source_f::work (int noutput_items,
gr_vector_const_void_star &input_items, gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items) gr_vector_void_star &output_items)
{ {
float *out = (float *) output_items[0]; float *out = (float *) output_items[0];
ssize_t r=0, nbytes=0, bytes_received=0; ssize_t r=0, nbytes=0, bytes_received=0;
ssize_t total_bytes = (ssize_t)(d_itemsize*noutput_items); ssize_t total_bytes = (ssize_t)(d_itemsize*noutput_items);
int bytesleft = noutput_items; int bytesleft = noutput_items;
int index = 0; int index = 0;
int receivedbytes = 0; int receivedbytes = 0;
while(bytesleft > 0) { while(bytesleft > 0) {
receivedbytes = recv(d_socket, (char*)&d_temp_buff[index], bytesleft, 0); receivedbytes = recv(d_socket, (char*)&d_temp_buff[index], bytesleft, 0);
if(receivedbytes == -1 && !is_error(EAGAIN)){ if(receivedbytes == -1 && !is_error(EAGAIN)){
fprintf(stderr, "socket error\n"); fprintf(stderr, "socket error\n");
return -1; return -1;
} }
bytesleft -= receivedbytes; bytesleft -= receivedbytes;
index += receivedbytes; index += receivedbytes;
} }
r = noutput_items; r = noutput_items;
for(int i=0; i<r; ++i)
out[i]=d_LUT[*(d_temp_buff+d_temp_offset+i)];
return r; for(int i=0; i<r; ++i)
out[i]=d_LUT[*(d_temp_buff+d_temp_offset+i)];
return r;
} }
#ifdef _WIN32 #ifdef _WIN32
@ -262,8 +262,8 @@ int rtl_tcp_source_f::work (int noutput_items,
#pragma pack(push, 1) #pragma pack(push, 1)
#endif #endif
struct command{ struct command{
unsigned char cmd; unsigned char cmd;
unsigned int param; unsigned int param;
}__attribute__((packed)); }__attribute__((packed));
#ifdef _WIN32 #ifdef _WIN32
#pragma pack(pop) #pragma pack(pop)

View File

@ -1,17 +1,17 @@
/* -*- c++ -*- */ /* -*- c++ -*- */
/* /*
* Copyright 2012 Hoernchen <la@tfc-server.de> * Copyright 2012 Hoernchen <la@tfc-server.de>
* *
* GNU Radio is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option) * the Free Software Foundation; either version 3, or (at your option)
* any later version. * any later version.
* *
* GNU Radio is distributed in the hope that it will be useful, * GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to * along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, * the Free Software Foundation, Inc., 51 Franklin Street,
@ -44,26 +44,26 @@ typedef void* optval_t;
#define ssize_t int #define ssize_t int
/* copied from rtl sdr */ /* copied from rtl sdr */
enum rtlsdr_tuner { enum rtlsdr_tuner {
RTLSDR_TUNER_UNKNOWN = 0, RTLSDR_TUNER_UNKNOWN = 0,
RTLSDR_TUNER_E4000, RTLSDR_TUNER_E4000,
RTLSDR_TUNER_FC0012, RTLSDR_TUNER_FC0012,
RTLSDR_TUNER_FC0013, RTLSDR_TUNER_FC0013,
RTLSDR_TUNER_FC2580, RTLSDR_TUNER_FC2580,
RTLSDR_TUNER_R820T RTLSDR_TUNER_R820T
}; };
class rtl_tcp_source_f; class rtl_tcp_source_f;
typedef boost::shared_ptr<rtl_tcp_source_f> rtl_tcp_source_f_sptr; typedef boost::shared_ptr<rtl_tcp_source_f> rtl_tcp_source_f_sptr;
rtl_tcp_source_f_sptr make_rtl_tcp_source_f ( rtl_tcp_source_f_sptr make_rtl_tcp_source_f (
size_t itemsize, size_t itemsize,
const char *host, const char *host,
unsigned short port, unsigned short port,
int payload_size, int payload_size,
bool eof, bool eof = false,
bool wait); bool wait = false);
class rtl_tcp_source_f : public gr_sync_block class rtl_tcp_source_f : public gr_sync_block
{ {
@ -77,12 +77,12 @@ private:
size_t d_temp_offset; // point to temp buffer location offset size_t d_temp_offset; // point to temp buffer location offset
float *d_LUT; float *d_LUT;
unsigned int d_tuner_type; unsigned int d_tuner_type;
unsigned int d_tuner_gain_count; unsigned int d_tuner_gain_count;
private: private:
rtl_tcp_source_f(size_t itemsize, const char *host, rtl_tcp_source_f(size_t itemsize, const char *host,
unsigned short port, int payload_size, bool eof, bool wait); unsigned short port, int payload_size, bool eof, bool wait);
// The friend declaration allows make_source_c to // The friend declaration allows make_source_c to
// access the private constructor. // access the private constructor.
@ -94,15 +94,15 @@ private:
bool eof, bool eof,
bool wait); bool wait);
public: public:
~rtl_tcp_source_f(); ~rtl_tcp_source_f();
enum rtlsdr_tuner get_tuner_type() { return (enum rtlsdr_tuner) d_tuner_type; } enum rtlsdr_tuner get_tuner_type() { return (enum rtlsdr_tuner) d_tuner_type; }
unsigned int get_tuner_gain_count() { return d_tuner_gain_count; } unsigned int get_tuner_gain_count() { return d_tuner_gain_count; }
int work(int noutput_items, int work(int noutput_items,
gr_vector_const_void_star &input_items, gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items); gr_vector_void_star &output_items);
void set_freq(int freq); void set_freq(int freq);
void set_sample_rate(int sample_rate); void set_sample_rate(int sample_rate);