📄 fifocmd.cpp
字号:
/*************************************************************************** fifocmd.cpp - Fifo-cmd interface with the sradio-framework ------------------- begin : 2003 authors : Linus Gasser emails : linus.gasser@epfl.ch ***************************************************************************//*************************************************************************** Changes ------- date - name - description 03/08/11 - ineiti- begin 04/05/13 - ineiti - added a profiling-reader **************************************************************************//*************************************************************************** * * * 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 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************//** * The interface to the sradio-framework changed in august 2003 from a * proc-based, directory-like interface to a simple, more rt-friendly * two-fifo system. One fifo is used to send commands to the sradio, * the other fifo is used to read back the results. */#include <iostream>#include <qregexp.h>#include <unistd.h>#include "fifocmd.h"using namespace std;// Real-Time initialisationFifoCmd::FifoCmd( ){ f_cmd = new QFile( "/dev/rtf0" ); f_reply = new QFile( "/dev/rtf1" ); // Is it possible to open them in the correct mode? if ( !f_cmd->open( IO_Raw | IO_WriteOnly ) ){ cout << "Couldn't open fifo_cmd, stale directory\n"; fifo_lives = false; return; } if ( !f_reply->open( IO_Raw | IO_ReadOnly ) ){ cout << "Couldn't open fifo_reply\n"; f_cmd->close(); fifo_lives = false; return; } // Some initialisation fifo_cmd = new QTextStream( f_cmd ); // Final test fifo_lives = isAlive();}FifoCmd::FifoCmd( QString bdir ){ fifo_lives = true; f_cmd = new QFile( QString( bdir ).append( "/fifo_cmd" ) ); f_reply = new QFile( QString( bdir ).append( "/fifo_reply" ) ); // Do the files exist? if ( !f_cmd->exists() || !f_reply->exists() ){ cout << bdir << " doesn't hold a valid sradio-directory\n"; fifo_lives = false; return; } // Is it possible to open them in the correct mode? if ( !f_cmd->open( IO_Raw | IO_WriteOnly | IO_Async ) ){ // If we try to open a writing-fifo with IO_Async and there // is nobody listening on the other end, we get an error. // This is very useful in this case, as the only occurence of // having the fifo_cmd file w/o somebody listening, is when there // is a stale sradio.x directory... cout << "Couldn't open fifo_cmd, stale directory\n"; fifo_lives = false; return; } if ( !f_reply->open( IO_Raw | IO_ReadOnly ) ){ cout << "Couldn't open fifo_reply\n"; f_cmd->close(); fifo_lives = false; return; } // Some initialisation fifo_cmd = new QTextStream( f_cmd ); // Final test fifo_lives = isAlive();}// Checks whether the fifos are actif and sends a pingbool FifoCmd::isAlive(){ // Fifo's can't ressurrect. So, once dead, always dead... if ( !fifo_lives ){ return false; } // Send out a ping if ( !getReplyQStr( "ping" ).compare( "pong" ) ){ return true; } return false;}// Sends a process-messagevoid FifoCmd::processData( int module_id ){ getReplyQStr( QString( "process_data %1" ).arg( module_id ) );}// Sends 'cmd' to the fifo and waits for replyQByteArray FifoCmd::getReply( QString cmd ){ if ( !fifo_lives ){ return QByteArray( 0 ); } cmd.prepend( QString( "%1\n" ).arg( cmd.length(), 2 ) ); *fifo_cmd << cmd; f_cmd->flush(); uint size, pos; char str_size[ 12 ]; for ( pos = 0; pos < 11; ){ pos += f_reply->readBlock( str_size, 11 - pos ); } str_size[11] = 0; size = QString( str_size ).toInt(); QByteArray rep( size ); if ( size ){ for ( pos = 0; pos < size; ){ pos += f_reply->readBlock( rep.data() + pos, size - pos ); } } return rep;}QString FifoCmd::getReplyQStr( QString cmd ){ QByteArray ret = getReply( cmd ); uint size = ret.size(); ret.resize( size + 1 ); ret[ size ] = 0; return QString( ret );}QStringList FifoCmd::getReplyQStrList( QString cmd ){ return QStringList::split( "\n", getReplyQStr( cmd ) );}QStringList FifoCmd::getReplyQStrListCut( QString cmd ){ QStringList ret = getReplyQStrList( cmd ); if ( !ret.count() ){ return ret; } // The first line contains the number of lines ret.remove( ret.begin() ); return ret;}// Returns a list of all available modules. Each string is a comma-// seperated id,name valueQStringList FifoCmd::getModules(){ return getReplyQStrList( "list_modules" );}// Returns a list of all available modules. Each string is a comma-// seperated id,name value// Additionally tags all modules as 'known'QStringList FifoCmd::getTagModules(){ return getReplyQStrList( "list_tag_modules" );}// Returns a list of all not yet seen modules. Each string is a comma-// seperated id,name valueQStringList FifoCmd::getNewModules(){ return getReplyQStrList( "list_new_modules" );}// Returns the name of a module given its idQString FifoCmd::getName( int module_id ){ QString search( QString( "^%1," ).arg( module_id ) ); QStringList entry = getModules().grep( QRegExp( search ) ); switch ( entry.count() ){ case 0: return QString(""); break; case 1: break; default: if ( entry.count() > 1 ){ cout << "More than one ID found... not good" << endl; } break; } return QStringList::split( ",", entry[0] )[1];}// Returns a list of all inputs of a given moduleQStringList FifoCmd::getInputs( int module_id ){ return getReplyQStrListCut( QString( "show_input %1" ).arg( module_id ) );}// Returns a list of all outputs of a given moduleQStringList FifoCmd::getOutputs( int module_id ){ return getReplyQStrListCut( QString( "show_output %1" ).arg( module_id ) );}// Returns a list of all config-params of a given moduleQStringList FifoCmd::getConfig( int module_id ){ return getReplyQStrListCut( QString( "show_config %1" ).arg( module_id ) );}// Returns a list of all stats-params of a given moduleQStringList FifoCmd::getStats( int module_id ){ return getReplyQStrListCut( QString( "show_stats %1" ).arg( module_id ) );}// Gets a memory for a block-statsQByteArray FifoCmd::getBlock( int module_id, int stats_id ){ QString cmd( QString( "get_block %1,%2" ).arg( module_id ).arg( stats_id ) ); return getReply( cmd );}// Gets a memory for an image-statsQByteArray FifoCmd::getImage( int module_id, int stats_id ){ QString cmd( QString( "get_image %1,%2" ).arg( module_id ).arg( stats_id ) ); return getReply( cmd );}// Gets the data from a portQByteArray FifoCmd::getPort( int module_id, int port ){ QString cmd( QString( "get_output %1,%2" ).arg( module_id ).arg( port ) ); return getReply( cmd );}// Sets a new configint FifoCmd::setConfig( int module_id, int config_id, QString new_value ){ QString cmd( QString( "set_config %1,%2,%3" ).arg( module_id ).arg( config_id ). arg( new_value ) ); QString reply = getReplyQStr( cmd ); if ( !reply.compare( "Reconfigured" ) ){ return true; } else { cout << "Got this answer from set_config: " << reply << endl; return false; }}// Sets up a new listint FifoCmd::newList( int m1, int s1, int m2, int s2 ){ QString cmd( QString( "new_list %1,%2,%3,%4" ). arg( m1 ).arg( s1 ).arg( m2 ).arg( s2 ) ); QString reply = getReplyQStr( cmd ); if ( !reply.compare( "error" ) ){ return -1; } else { return reply.toInt(); }}// Returns a list of comma-seperated value-pairsQStringList FifoCmd::readList( int list_id ){ return getReplyQStrListCut( QString( "read_list %1" ).arg( list_id ) );}// Closes down a listvoid FifoCmd::closeList( int list_id ){ getReply( QString( "close_list %1" ).arg( list_id ) );}// Returns the profiling-valuesQValueList<QPair<long long int,long long int> > FifoCmd::getProfiling( int module_id ){ QStringList reply = getReplyQStrList( QString( "get_profiling %1" ). arg( module_id ) ); QValueList<QPair<long long int,long long int> > ret; uint profile; for ( profile=0; profile< reply.count(); profile++ ){ QStringList values( QStringList::split( " ", reply[profile] ) ); ret.append( qMakePair( values[0].toLongLong(), values[1].toLongLong() ) ); } return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -