redpitaya: change base class to gr::sync_block

This commit is contained in:
Pavel Demin 2015-12-21 18:28:54 +01:00 committed by Dimitri Stolnikov
parent 55fe961987
commit 810a981d0c
5 changed files with 87 additions and 37 deletions

View File

@ -37,12 +37,22 @@
#include "redpitaya_common.h" #include "redpitaya_common.h"
#if defined(__APPLE__) || defined(__MACH__)
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif
#endif
void redpitaya_send_command( int socket, uint32_t command ) void redpitaya_send_command( int socket, uint32_t command )
{ {
ssize_t size; ssize_t size;
std::stringstream message; std::stringstream message;
size = send( socket, &command, sizeof(command), MSG_NOSIGNAL ); #if defined(_WIN32)
size = ::send( socket, (char *)&command, sizeof(command), 0 );
#else
size = ::send( socket, &command, sizeof(command), MSG_NOSIGNAL );
#endif
if ( size != sizeof(command) ) if ( size != sizeof(command) )
{ {

View File

@ -24,7 +24,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#ifdef _WIN32 #if defined(_WIN32)
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <windows.h> #include <windows.h>
@ -54,9 +54,9 @@ redpitaya_sink_c_sptr make_redpitaya_sink_c(const std::string &args)
} }
redpitaya_sink_c::redpitaya_sink_c(const std::string &args) : redpitaya_sink_c::redpitaya_sink_c(const std::string &args) :
gr::hier_block2("redpitaya_sink_c", gr::sync_block("redpitaya_sink_c",
gr::io_signature::make(1, 1, sizeof(gr_complex)), gr::io_signature::make(1, 1, sizeof(gr_complex)),
gr::io_signature::make(0, 0, 0)) gr::io_signature::make(0, 0, 0))
{ {
std::string host = "192.168.1.100"; std::string host = "192.168.1.100";
std::stringstream message; std::stringstream message;
@ -64,7 +64,7 @@ redpitaya_sink_c::redpitaya_sink_c(const std::string &args) :
struct sockaddr_in addr; struct sockaddr_in addr;
uint32_t command; uint32_t command;
#ifdef _WIN32 #if defined(_WIN32)
WSADATA wsaData; WSADATA wsaData;
WSAStartup( MAKEWORD(2, 2), &wsaData ); WSAStartup( MAKEWORD(2, 2), &wsaData );
#endif #endif
@ -120,22 +120,42 @@ redpitaya_sink_c::redpitaya_sink_c(const std::string &args) :
command = ptt ? 2<<28 : 3<<28; command = ptt ? 2<<28 : 3<<28;
redpitaya_send_command( _sockets[0], command ); redpitaya_send_command( _sockets[0], command );
_sink = gr::blocks::file_descriptor_sink::make( sizeof(gr_complex), _sockets[1] );
connect( self(), 0, _sink, 0 );
} }
redpitaya_sink_c::~redpitaya_sink_c() redpitaya_sink_c::~redpitaya_sink_c()
{ {
close( _sockets[1] ); #if defined(_WIN32)
close( _sockets[0] ); ::closesocket( _sockets[1] );
::closesocket( _sockets[0] );
#ifdef _WIN32
WSACleanup(); WSACleanup();
#else
::close( _sockets[1] );
::close( _sockets[0] );
#endif #endif
} }
int redpitaya_sink_c::work( int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items )
{
ssize_t size;
ssize_t total = sizeof(gr_complex) * noutput_items;
const gr_complex *in = (const gr_complex *)input_items[0];
#if defined(_WIN32)
size = ::send( _sockets[1], (char *)in, total, 0 );
#else
size = ::send( _sockets[1], in, total, MSG_NOSIGNAL );
#endif
if ( size != total )
throw std::runtime_error( "Sending samples failed." );
consume(0, noutput_items);
return 0;
}
std::string redpitaya_sink_c::name() std::string redpitaya_sink_c::name()
{ {
return "Red Pitaya Sink"; return "Red Pitaya Sink";

View File

@ -21,8 +21,7 @@
#ifndef REDPITAYA_SINK_C_H #ifndef REDPITAYA_SINK_C_H
#define REDPITAYA_SINK_C_H #define REDPITAYA_SINK_C_H
#include <gnuradio/hier_block2.h> #include <gnuradio/sync_block.h>
#include <gnuradio/blocks/file_descriptor_sink.h>
#include "sink_iface.h" #include "sink_iface.h"
@ -33,7 +32,7 @@ typedef boost::shared_ptr< redpitaya_sink_c > redpitaya_sink_c_sptr;
redpitaya_sink_c_sptr make_redpitaya_sink_c( const std::string & args = "" ); redpitaya_sink_c_sptr make_redpitaya_sink_c( const std::string & args = "" );
class redpitaya_sink_c : class redpitaya_sink_c :
public gr::hier_block2, public gr::sync_block,
public sink_iface public sink_iface
{ {
private: private:
@ -44,6 +43,10 @@ private:
public: public:
~redpitaya_sink_c(); ~redpitaya_sink_c();
int work( int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items );
std::string name(); std::string name();
static std::vector< std::string > get_devices( bool fake = false ); static std::vector< std::string > get_devices( bool fake = false );
@ -73,7 +76,6 @@ public:
std::string get_antenna( size_t chan = 0 ); std::string get_antenna( size_t chan = 0 );
private: private:
gr::blocks::file_descriptor_sink::sptr _sink;
double _freq, _rate, _corr; double _freq, _rate, _corr;
int _sockets[2]; int _sockets[2];
}; };

View File

@ -24,7 +24,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#ifdef _WIN32 #if defined(_WIN32)
#include <winsock2.h> #include <winsock2.h>
#include <ws2tcpip.h> #include <ws2tcpip.h>
#include <windows.h> #include <windows.h>
@ -54,9 +54,9 @@ redpitaya_source_c_sptr make_redpitaya_source_c(const std::string &args)
} }
redpitaya_source_c::redpitaya_source_c(const std::string &args) : redpitaya_source_c::redpitaya_source_c(const std::string &args) :
gr::hier_block2("redpitaya_source_c", gr::sync_block("redpitaya_source_c",
gr::io_signature::make(0, 0, 0), gr::io_signature::make(0, 0, 0),
gr::io_signature::make(1, 1, sizeof(gr_complex))) gr::io_signature::make(1, 1, sizeof(gr_complex)))
{ {
std::string host = "192.168.1.100"; std::string host = "192.168.1.100";
std::stringstream message; std::stringstream message;
@ -64,7 +64,7 @@ redpitaya_source_c::redpitaya_source_c(const std::string &args) :
struct sockaddr_in addr; struct sockaddr_in addr;
uint32_t command; uint32_t command;
#ifdef _WIN32 #if defined(_WIN32)
WSADATA wsaData; WSADATA wsaData;
WSAStartup( MAKEWORD(2, 2), &wsaData ); WSAStartup( MAKEWORD(2, 2), &wsaData );
#endif #endif
@ -96,9 +96,7 @@ redpitaya_source_c::redpitaya_source_c(const std::string &args) :
for ( size_t i = 0; i < 2; ++i ) for ( size_t i = 0; i < 2; ++i )
{ {
if ( ( _sockets[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) if ( ( _sockets[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
{
throw std::runtime_error( "Could not create TCP socket." ); throw std::runtime_error( "Could not create TCP socket." );
}
memset( &addr, 0, sizeof(addr) ); memset( &addr, 0, sizeof(addr) );
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -114,22 +112,40 @@ redpitaya_source_c::redpitaya_source_c(const std::string &args) :
command = i; command = i;
redpitaya_send_command( _sockets[i], command ); redpitaya_send_command( _sockets[i], command );
} }
_source = gr::blocks::file_descriptor_source::make( sizeof(gr_complex), _sockets[1] );
connect( _source, 0, self(), 0 );
} }
redpitaya_source_c::~redpitaya_source_c() redpitaya_source_c::~redpitaya_source_c()
{ {
close( _sockets[1] ); #if defined(_WIN32)
close( _sockets[0] ); ::closesocket( _sockets[1] );
::closesocket( _sockets[0] );
#ifdef _WIN32
WSACleanup(); WSACleanup();
#else
::close( _sockets[1] );
::close( _sockets[0] );
#endif #endif
} }
int redpitaya_source_c::work( int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items )
{
ssize_t size;
ssize_t total = sizeof(gr_complex) * noutput_items;
gr_complex *out = (gr_complex *)output_items[0];
#if defined(_WIN32)
size = ::recv( _sockets[1], (char *)out, total, MSG_WAITALL );
#else
size = ::recv( _sockets[1], out, total, MSG_WAITALL );
#endif
if ( size != total )
throw std::runtime_error( "Receiving samples failed." );
return noutput_items;
}
std::string redpitaya_source_c::name() std::string redpitaya_source_c::name()
{ {
return "Red Pitaya Source"; return "Red Pitaya Source";

View File

@ -21,8 +21,7 @@
#ifndef REDPITAYA_SOURCE_C_H #ifndef REDPITAYA_SOURCE_C_H
#define REDPITAYA_SOURCE_C_H #define REDPITAYA_SOURCE_C_H
#include <gnuradio/hier_block2.h> #include <gnuradio/sync_block.h>
#include <gnuradio/blocks/file_descriptor_source.h>
#include "source_iface.h" #include "source_iface.h"
@ -33,7 +32,7 @@ typedef boost::shared_ptr< redpitaya_source_c > redpitaya_source_c_sptr;
redpitaya_source_c_sptr make_redpitaya_source_c( const std::string & args = "" ); redpitaya_source_c_sptr make_redpitaya_source_c( const std::string & args = "" );
class redpitaya_source_c : class redpitaya_source_c :
public gr::hier_block2, public gr::sync_block,
public source_iface public source_iface
{ {
private: private:
@ -44,6 +43,10 @@ private:
public: public:
~redpitaya_source_c(); ~redpitaya_source_c();
int work( int noutput_items,
gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items );
std::string name(); std::string name();
static std::vector< std::string > get_devices( bool fake = false ); static std::vector< std::string > get_devices( bool fake = false );
@ -73,7 +76,6 @@ public:
std::string get_antenna( size_t chan = 0 ); std::string get_antenna( size_t chan = 0 );
private: private:
gr::blocks::file_descriptor_source::sptr _source;
double _freq, _rate, _corr; double _freq, _rate, _corr;
int _sockets[2]; int _sockets[2];
}; };