2022-01-17 17:23:55 +00:00
# include "servermain.h"
# include "commhandler.h"
# include "rigidentities.h"
# include "logcategories.h"
2022-01-18 00:11:15 +00:00
# include <iostream>
2022-01-17 17:23:55 +00:00
// This code is copyright 2017-2020 Elliott H. Liggett
// All rights reserved
servermain : : servermain ( const QString serialPortCL , const QString hostCL , const QString settingsFile )
{
this - > serialPortCL = serialPortCL ;
this - > hostCL = hostCL ;
2022-01-26 09:49:52 +00:00
qRegisterMetaType < udpPreferences > ( ) ; // Needs to be registered early.
qRegisterMetaType < rigCapabilities > ( ) ;
qRegisterMetaType < duplexMode > ( ) ;
qRegisterMetaType < rptAccessTxRx > ( ) ;
qRegisterMetaType < rigInput > ( ) ;
qRegisterMetaType < meterKind > ( ) ;
qRegisterMetaType < spectrumMode > ( ) ;
qRegisterMetaType < freqt > ( ) ;
qRegisterMetaType < mode_info > ( ) ;
qRegisterMetaType < audioPacket > ( ) ;
2022-01-17 17:23:55 +00:00
qRegisterMetaType < audioSetup > ( ) ;
qRegisterMetaType < SERVERCONFIG > ( ) ;
qRegisterMetaType < timekind > ( ) ;
qRegisterMetaType < datekind > ( ) ;
qRegisterMetaType < rigstate * > ( ) ;
2022-01-22 15:32:53 +00:00
qRegisterMetaType < QList < radio_cap_packet > > ( ) ;
2022-02-02 16:26:40 +00:00
qRegisterMetaType < networkStatus > ( ) ;
2022-01-17 17:23:55 +00:00
setDefPrefs ( ) ;
getSettingsFilePath ( settingsFile ) ;
loadSettings ( ) ; // Look for saved preferences
setInitialTiming ( ) ;
openRig ( ) ;
setServerToPrefs ( ) ;
amTransmitting = false ;
}
servermain : : ~ servermain ( )
{
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-26 09:49:52 +00:00
{
2022-01-27 19:11:16 +00:00
if ( radio - > rigThread ! = Q_NULLPTR )
2022-01-26 09:49:52 +00:00
{
2022-01-27 19:11:16 +00:00
radio - > rigThread - > quit ( ) ;
radio - > rigThread - > wait ( ) ;
2022-01-26 09:49:52 +00:00
}
2022-01-27 19:11:16 +00:00
delete radio ; // This has been created by new in loadSettings();
2022-01-26 09:49:52 +00:00
}
2022-01-27 19:11:16 +00:00
serverConfig . rigs . clear ( ) ;
2022-01-17 17:23:55 +00:00
if ( serverThread ! = Q_NULLPTR ) {
serverThread - > quit ( ) ;
serverThread - > wait ( ) ;
}
2022-01-26 09:49:52 +00:00
2022-01-17 17:23:55 +00:00
delete settings ;
# if defined(PORTAUDIO)
Pa_Terminate ( ) ;
# endif
}
void servermain : : openRig ( )
{
// This function is intended to handle opening a connection to the rig.
// the connection can be either serial or network,
// and this function is also responsible for initiating the search for a rig model and capabilities.
// Any errors, such as unable to open connection or unable to open port, are to be reported to the user.
makeRig ( ) ;
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-26 09:49:52 +00:00
{
//qInfo(logSystem()) << "Opening rig";
2022-01-27 19:11:16 +00:00
if ( radio - > rigThread ! = Q_NULLPTR )
2022-01-26 09:49:52 +00:00
{
//qInfo(logSystem()) << "Got rig";
2022-01-27 19:11:16 +00:00
QMetaObject : : invokeMethod ( radio - > rig , [ = ] ( ) {
radio - > rig - > commSetup ( radio - > civAddr , radio - > serialPort , radio - > baudRate , QString ( " none " ) ) ;
2022-01-26 09:49:52 +00:00
} , Qt : : QueuedConnection ) ;
2022-01-17 17:23:55 +00:00
}
}
}
void servermain : : makeRig ( )
{
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-17 17:23:55 +00:00
{
2022-01-27 19:11:16 +00:00
if ( radio - > rigThread = = Q_NULLPTR )
2022-01-26 09:49:52 +00:00
{
qInfo ( logSystem ( ) ) < < " Creating new rigThread() " ;
2022-01-27 19:11:16 +00:00
radio - > rig = new rigCommander ( radio - > guid ) ;
radio - > rigThread = new QThread ( this ) ;
2022-01-26 09:49:52 +00:00
// Thread:
2022-01-27 19:11:16 +00:00
radio - > rig - > moveToThread ( radio - > rigThread ) ;
connect ( radio - > rigThread , SIGNAL ( started ( ) ) , radio - > rig , SLOT ( process ( ) ) ) ;
connect ( radio - > rigThread , SIGNAL ( finished ( ) ) , radio - > rig , SLOT ( deleteLater ( ) ) ) ;
radio - > rigThread - > start ( ) ;
2022-01-26 09:49:52 +00:00
// Rig status and Errors:
2022-01-27 19:11:16 +00:00
connect ( radio - > rig , SIGNAL ( haveSerialPortError ( QString , QString ) ) , this , SLOT ( receiveSerialPortError ( QString , QString ) ) ) ;
connect ( radio - > rig , SIGNAL ( haveStatusUpdate ( networkStatus ) ) , this , SLOT ( receiveStatusUpdate ( networkStatus ) ) ) ;
2022-01-26 09:49:52 +00:00
// Rig comm setup:
2022-01-27 19:11:16 +00:00
//connect(this, SIGNAL(sendCommSetup(unsigned char, udpPreferences, audioSetup, audioSetup, QString)), radio->rig, SLOT(commSetup(unsigned char, udpPreferences, audioSetup, audioSetup, QString)));
//connect(this, SIGNAL(sendCommSetup(unsigned char, QString, quint32, QString)), radio->rig, SLOT(commSetup(unsigned char, QString, quint32, QString)));
connect ( this , SIGNAL ( setRTSforPTT ( bool ) ) , radio - > rig , SLOT ( setRTSforPTT ( bool ) ) ) ;
2022-01-26 09:49:52 +00:00
2022-01-27 19:11:16 +00:00
connect ( radio - > rig , SIGNAL ( haveBaudRate ( quint32 ) ) , this , SLOT ( receiveBaudRate ( quint32 ) ) ) ;
2022-01-26 09:49:52 +00:00
2022-01-27 19:11:16 +00:00
connect ( this , SIGNAL ( sendCloseComm ( ) ) , radio - > rig , SLOT ( closeComm ( ) ) ) ;
connect ( this , SIGNAL ( sendChangeLatency ( quint16 ) ) , radio - > rig , SLOT ( changeLatency ( quint16 ) ) ) ;
//connect(this, SIGNAL(getRigCIV()), radio->rig, SLOT(findRigs()));
//connect(this, SIGNAL(setRigID(unsigned char)), radio->rig, SLOT(setRigID(unsigned char)));
connect ( radio - > rig , SIGNAL ( discoveredRigID ( rigCapabilities ) ) , this , SLOT ( receiveFoundRigID ( rigCapabilities ) ) ) ;
connect ( radio - > rig , SIGNAL ( commReady ( ) ) , this , SLOT ( receiveCommReady ( ) ) ) ;
2022-01-26 09:49:52 +00:00
2022-01-27 19:11:16 +00:00
connect ( this , SIGNAL ( requestRigState ( ) ) , radio - > rig , SLOT ( sendState ( ) ) ) ;
connect ( this , SIGNAL ( stateUpdated ( ) ) , radio - > rig , SLOT ( stateUpdated ( ) ) ) ;
connect ( radio - > rig , SIGNAL ( stateInfo ( rigstate * ) ) , this , SLOT ( receiveStateInfo ( rigstate * ) ) ) ;
2022-01-26 09:49:52 +00:00
//Other connections
2022-01-27 19:11:16 +00:00
connect ( this , SIGNAL ( setCIVAddr ( unsigned char ) ) , radio - > rig , SLOT ( setCIVAddr ( unsigned char ) ) ) ;
2022-01-26 09:49:52 +00:00
2022-01-27 19:11:16 +00:00
connect ( radio - > rig , SIGNAL ( havePTTStatus ( bool ) ) , this , SLOT ( receivePTTstatus ( bool ) ) ) ;
connect ( this , SIGNAL ( setPTT ( bool ) ) , radio - > rig , SLOT ( setPTT ( bool ) ) ) ;
connect ( this , SIGNAL ( getPTT ( ) ) , radio - > rig , SLOT ( getPTT ( ) ) ) ;
connect ( this , SIGNAL ( getDebug ( ) ) , radio - > rig , SLOT ( getDebug ( ) ) ) ;
if ( radio - > rigThread - > isRunning ( ) ) {
2022-01-26 09:49:52 +00:00
qInfo ( logSystem ( ) ) < < " Rig thread is running " ;
}
else {
qInfo ( logSystem ( ) ) < < " Rig thread is not running " ;
}
2022-01-17 17:23:55 +00:00
}
2022-01-26 09:49:52 +00:00
2022-01-17 17:23:55 +00:00
}
2022-01-26 09:49:52 +00:00
2022-01-17 17:23:55 +00:00
}
void servermain : : removeRig ( )
{
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-17 17:23:55 +00:00
{
2022-01-27 19:11:16 +00:00
if ( radio - > rigThread ! = Q_NULLPTR )
2022-01-26 09:49:52 +00:00
{
2022-01-27 19:11:16 +00:00
radio - > rigThread - > disconnect ( ) ;
radio - > rig - > disconnect ( ) ;
delete radio - > rigThread ;
delete radio - > rig ;
radio - > rig = Q_NULLPTR ;
2022-01-17 17:23:55 +00:00
}
}
}
void servermain : : findSerialPort ( )
{
// Find the ICOM radio connected, or, if none, fall back to OS default.
// qInfo(logSystem()) << "Searching for serial port...";
QDirIterator it73 ( " /dev/serial/by-id " , QStringList ( ) < < " *IC-7300* " , QDir : : Files , QDirIterator : : Subdirectories ) ;
QDirIterator it97 ( " /dev/serial " , QStringList ( ) < < " *IC-9700*A* " , QDir : : Files , QDirIterator : : Subdirectories ) ;
QDirIterator it785x ( " /dev/serial " , QStringList ( ) < < " *IC-785*A* " , QDir : : Files , QDirIterator : : Subdirectories ) ;
QDirIterator it705 ( " /dev/serial " , QStringList ( ) < < " *IC-705*A " , QDir : : Files , QDirIterator : : Subdirectories ) ;
QDirIterator it7610 ( " /dev/serial " , QStringList ( ) < < " *IC-7610*A " , QDir : : Files , QDirIterator : : Subdirectories ) ;
QDirIterator itR8600 ( " /dev/serial " , QStringList ( ) < < " *IC-R8600*A " , QDir : : Files , QDirIterator : : Subdirectories ) ;
if ( ! it73 . filePath ( ) . isEmpty ( ) )
{
// IC-7300
serialPortRig = it73 . filePath ( ) ; // first
} else if ( ! it97 . filePath ( ) . isEmpty ( ) )
{
// IC-9700
serialPortRig = it97 . filePath ( ) ;
} else if ( ! it785x . filePath ( ) . isEmpty ( ) )
{
// IC-785x
serialPortRig = it785x . filePath ( ) ;
} else if ( ! it705 . filePath ( ) . isEmpty ( ) )
{
// IC-705
serialPortRig = it705 . filePath ( ) ;
} else if ( ! it7610 . filePath ( ) . isEmpty ( ) )
{
// IC-7610
serialPortRig = it7610 . filePath ( ) ;
} else if ( ! itR8600 . filePath ( ) . isEmpty ( ) )
{
// IC-R8600
serialPortRig = itR8600 . filePath ( ) ;
} else {
//fall back:
qInfo ( logSystem ( ) ) < < " Could not find Icom serial port. Falling back to OS default. Use --port to specify, or modify preferences. " ;
# ifdef Q_OS_MAC
serialPortRig = QString ( " /dev/tty.SLAB_USBtoUART " ) ;
# endif
# ifdef Q_OS_LINUX
serialPortRig = QString ( " /dev/ttyUSB0 " ) ;
# endif
# ifdef Q_OS_WIN
serialPortRig = QString ( " COM1 " ) ;
# endif
}
}
2022-01-26 09:49:52 +00:00
void servermain : : receiveStatusUpdate ( networkStatus status )
2022-01-18 00:11:15 +00:00
{
2022-01-26 09:49:52 +00:00
if ( status . message ! = lastMessage ) {
std : : cout < < status . message . toLocal8Bit ( ) . toStdString ( ) < < " \n " ;
lastMessage = status . message ;
2022-01-18 00:14:37 +00:00
}
2022-01-18 00:11:15 +00:00
}
2022-01-17 17:23:55 +00:00
void servermain : : receiveCommReady ( )
{
2022-01-26 09:49:52 +00:00
rigCommander * sender = qobject_cast < rigCommander * > ( QObject : : sender ( ) ) ;
// Use the GUID to determine which radio the response is from
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-17 17:23:55 +00:00
{
2022-01-29 22:50:58 +00:00
if ( sender ! = Q_NULLPTR & & radio - > rig ! = Q_NULLPTR & & ! memcmp ( sender - > getGUID ( ) , radio - > guid , GUIDLEN ) )
2022-01-17 17:23:55 +00:00
{
2022-01-26 09:49:52 +00:00
qInfo ( logSystem ( ) ) < < " Received CommReady!! " ;
2022-01-27 19:11:16 +00:00
if ( radio - > civAddr = = 0 )
2022-01-26 09:49:52 +00:00
{
// tell rigCommander to broadcast a request for all rig IDs.
// qInfo(logSystem()) << "Beginning search from wfview for rigCIV (auto-detection broadcast)";
2022-01-27 19:11:16 +00:00
if ( ! radio - > rigAvailable ) {
2022-01-29 22:50:58 +00:00
if ( radio - > connectTimer = = Q_NULLPTR ) {
radio - > connectTimer = new QTimer ( ) ;
connect ( radio - > connectTimer , & QTimer : : timeout , this , std : : bind ( & servermain : : connectToRig , this , radio ) ) ;
}
radio - > connectTimer - > start ( 500 ) ;
2022-01-26 09:49:52 +00:00
}
}
else {
// don't bother, they told us the CIV they want, stick with it.
// We still query the rigID to find the model, but at least we know the CIV.
2022-01-27 19:11:16 +00:00
qInfo ( logSystem ( ) ) < < " Skipping automatic CIV, using user-supplied value of " < < radio - > civAddr ;
QMetaObject : : invokeMethod ( radio - > rig , [ = ] ( ) {
radio - > rig - > setRigID ( radio - > civAddr ) ;
2022-01-26 09:49:52 +00:00
} , Qt : : QueuedConnection ) ;
}
2022-01-17 17:23:55 +00:00
}
}
}
2022-01-29 22:50:58 +00:00
void servermain : : connectToRig ( RIGCONFIG * rig )
{
if ( ! rig - > rigAvailable ) {
//qDebug(logSystem()) << "Searching for rig on" << rig->serialPort;
QMetaObject : : invokeMethod ( rig - > rig , [ = ] ( ) {
rig - > rig - > findRigs ( ) ;
} , Qt : : QueuedConnection ) ;
}
else {
rig - > connectTimer - > stop ( ) ;
}
}
2022-01-17 17:23:55 +00:00
void servermain : : receiveFoundRigID ( rigCapabilities rigCaps )
{
// Entry point for unknown rig being identified at the start of the program.
//now we know what the rig ID is:
2022-01-26 09:49:52 +00:00
rigCommander * sender = qobject_cast < rigCommander * > ( QObject : : sender ( ) ) ;
// Use the GUID to determine which radio the response is from
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-17 17:23:55 +00:00
{
2022-01-29 22:50:58 +00:00
if ( sender ! = Q_NULLPTR & & radio - > rig ! = Q_NULLPTR & & ! radio - > rigAvailable & & ! memcmp ( sender - > getGUID ( ) , radio - > guid , GUIDLEN ) )
2022-01-26 09:49:52 +00:00
{
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
qDebug ( logSystem ( ) ) < < " Rig name: " < < rigCaps . modelName ;
qDebug ( logSystem ( ) ) < < " Has LAN capabilities: " < < rigCaps . hasLan ;
qDebug ( logSystem ( ) ) < < " Rig ID received into servermain: spectLenMax: " < < rigCaps . spectLenMax ;
qDebug ( logSystem ( ) ) < < " Rig ID received into servermain: spectAmpMax: " < < rigCaps . spectAmpMax ;
qDebug ( logSystem ( ) ) < < " Rig ID received into servermain: spectSeqMax: " < < rigCaps . spectSeqMax ;
qDebug ( logSystem ( ) ) < < " Rig ID received into servermain: hasSpectrum: " < < rigCaps . hasSpectrum ;
qDebug ( logSystem ( ) ) . noquote ( ) < < QString ( " Rig ID received into servermain: GUID: {%1%2%3%4-%5%6-%7%8-%9%10-%11%12%13%14%15%16} " )
. arg ( rigCaps . guid [ 0 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 1 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 2 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 3 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 4 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 5 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 6 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 7 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 8 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 9 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 10 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 11 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 12 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 13 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 14 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
. arg ( rigCaps . guid [ 15 ] , 2 , 16 , QLatin1Char ( ' 0 ' ) )
;
2022-01-27 19:11:16 +00:00
radio - > rigCaps = rigCaps ;
2022-01-26 09:49:52 +00:00
// Added so that server receives rig capabilities.
emit sendRigCaps ( rigCaps ) ;
}
}
2022-01-17 17:23:55 +00:00
return ;
}
void servermain : : receiveSerialPortError ( QString port , QString errorText )
{
qInfo ( logSystem ( ) ) < < " servermain: received serial port error for port: " < < port < < " with message: " < < errorText ;
// TODO: Dialog box, exit, etc
}
void servermain : : getSettingsFilePath ( QString settingsFile )
{
if ( settingsFile . isNull ( ) ) {
settings = new QSettings ( ) ;
}
else
{
QString file = settingsFile ;
QFile info ( settingsFile ) ;
QString path = " " ;
if ( ! QFileInfo ( info ) . isAbsolute ( ) )
{
path = QStandardPaths : : writableLocation ( QStandardPaths : : AppDataLocation ) ;
if ( path . isEmpty ( ) )
{
path = QDir : : homePath ( ) ;
}
path = path + " / " ;
file = info . fileName ( ) ;
}
qInfo ( logSystem ( ) ) < < " Loading settings from: " < < path + file ;
settings = new QSettings ( path + file , QSettings : : Format : : IniFormat ) ;
}
}
void servermain : : setInitialTiming ( )
{
loopTickCounter = 0 ;
pttTimer = new QTimer ( this ) ;
pttTimer - > setInterval ( 180 * 1000 ) ; // 3 minute max transmit time in ms
pttTimer - > setSingleShot ( true ) ;
connect ( pttTimer , SIGNAL ( timeout ( ) ) , this , SLOT ( handlePttLimit ( ) ) ) ;
}
void servermain : : setServerToPrefs ( )
{
// Start server if enabled in config
if ( serverThread ! = Q_NULLPTR ) {
serverThread - > quit ( ) ;
serverThread - > wait ( ) ;
serverThread = Q_NULLPTR ;
udp = Q_NULLPTR ;
}
2022-01-27 19:11:16 +00:00
udp = new udpServer ( serverConfig , serverTxSetup , serverRxSetup ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
serverThread = new QThread ( this ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
udp - > moveToThread ( serverThread ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
connect ( this , SIGNAL ( initServer ( ) ) , udp , SLOT ( init ( ) ) ) ;
connect ( serverThread , SIGNAL ( finished ( ) ) , udp , SLOT ( deleteLater ( ) ) ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
// Step through all radios and connect them to the server,
// server will then use GUID to determine which actual radio it belongs to.
2022-01-17 17:23:55 +00:00
2022-01-27 19:11:16 +00:00
for ( RIGCONFIG * radio : serverConfig . rigs )
2022-01-26 09:49:52 +00:00
{
2022-01-27 19:11:16 +00:00
if ( radio - > rigThread ! = Q_NULLPTR )
2022-01-26 09:49:52 +00:00
{
2022-01-17 17:23:55 +00:00
2022-01-27 19:11:16 +00:00
if ( radio - > rig ! = Q_NULLPTR ) {
connect ( radio - > rig , SIGNAL ( haveAudioData ( audioPacket ) ) , udp , SLOT ( receiveAudioData ( audioPacket ) ) ) ;
connect ( radio - > rig , SIGNAL ( haveDataForServer ( QByteArray ) ) , udp , SLOT ( dataForServer ( QByteArray ) ) ) ;
//connect(udp, SIGNAL(haveDataFromServer(QByteArray)), radio->rig, SLOT(dataFromServer(QByteArray)));
2022-01-26 09:49:52 +00:00
connect ( this , SIGNAL ( sendRigCaps ( rigCapabilities ) ) , udp , SLOT ( receiveRigCaps ( rigCapabilities ) ) ) ;
}
}
}
2022-01-17 17:23:55 +00:00
2022-02-02 16:02:40 +00:00
connect ( udp , SIGNAL ( haveNetworkStatus ( networkStatus ) ) , this , SLOT ( receiveStatusUpdate ( networkStatus ) ) ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
serverThread - > start ( ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
emit initServer ( ) ;
2022-01-17 17:23:55 +00:00
}
void servermain : : setDefPrefs ( )
{
defPrefs . radioCIVAddr = 0x00 ; // previously was 0x94 for 7300.
defPrefs . CIVisRadioModel = false ;
defPrefs . forceRTSasPTT = false ;
defPrefs . serialPortRadio = QString ( " auto " ) ;
defPrefs . serialPortBaud = 115200 ;
defPrefs . localAFgain = 255 ;
udpDefPrefs . ipAddress = QString ( " " ) ;
udpDefPrefs . controlLANPort = 50001 ;
udpDefPrefs . serialLANPort = 50002 ;
udpDefPrefs . audioLANPort = 50003 ;
udpDefPrefs . username = QString ( " " ) ;
udpDefPrefs . password = QString ( " " ) ;
udpDefPrefs . clientName = QHostInfo : : localHostName ( ) ;
}
void servermain : : loadSettings ( )
{
qInfo ( logSystem ( ) ) < < " Loading settings from " < < settings - > fileName ( ) ;
2022-01-26 09:49:52 +00:00
int numRadios = settings - > beginReadArray ( " Radios " ) ;
int tempNum = numRadios ;
if ( numRadios = = 0 ) {
2022-01-23 15:06:31 +00:00
settings - > endArray ( ) ;
2022-01-26 09:49:52 +00:00
numRadios = 1 ;
2022-01-17 17:23:55 +00:00
}
# if defined(RTAUDIO)
# if defined(Q_OS_LINUX)
RtAudio * audio = new RtAudio ( RtAudio : : Api : : LINUX_ALSA ) ;
# elif defined(Q_OS_WIN)
RtAudio * audio = new RtAudio ( RtAudio : : Api : : WINDOWS_WASAPI ) ;
# elif defined(Q_OS_MACX)
RtAudio * audio = new RtAudio ( RtAudio : : Api : : MACOSX_CORE ) ;
# endif
// Enumerate audio devices, need to do before settings are loaded.
std : : map < int , std : : string > apiMap ;
apiMap [ RtAudio : : MACOSX_CORE ] = " OS-X Core Audio " ;
apiMap [ RtAudio : : WINDOWS_ASIO ] = " Windows ASIO " ;
apiMap [ RtAudio : : WINDOWS_DS ] = " Windows DirectSound " ;
apiMap [ RtAudio : : WINDOWS_WASAPI ] = " Windows WASAPI " ;
apiMap [ RtAudio : : UNIX_JACK ] = " Jack Client " ;
apiMap [ RtAudio : : LINUX_ALSA ] = " Linux ALSA " ;
apiMap [ RtAudio : : LINUX_PULSE ] = " Linux PulseAudio " ;
apiMap [ RtAudio : : LINUX_OSS ] = " Linux OSS " ;
apiMap [ RtAudio : : RTAUDIO_DUMMY ] = " RtAudio Dummy " ;
std : : vector < RtAudio : : Api > apis ;
RtAudio : : getCompiledApi ( apis ) ;
qInfo ( logAudio ( ) ) < < " RtAudio Version " < < QString : : fromStdString ( RtAudio : : getVersion ( ) ) ;
qInfo ( logAudio ( ) ) < < " Compiled APIs: " ;
for ( unsigned int i = 0 ; i < apis . size ( ) ; i + + ) {
qInfo ( logAudio ( ) ) < < " " < < QString : : fromStdString ( apiMap [ apis [ i ] ] ) ;
}
RtAudio : : DeviceInfo info ;
qInfo ( logAudio ( ) ) < < " Current API: " < < QString : : fromStdString ( apiMap [ audio - > getCurrentApi ( ) ] ) ;
unsigned int devices = audio - > getDeviceCount ( ) ;
qInfo ( logAudio ( ) ) < < " Found " < < devices < < " audio device(s) *=default " ;
# elif defined(PORTAUDIO)
// Use PortAudio device enumeration
PaError err ;
err = Pa_Initialize ( ) ;
if ( err ! = paNoError )
{
qInfo ( logAudio ( ) ) < < " ERROR: Cannot initialize Portaudio " ;
}
qInfo ( logAudio ( ) ) < < " PortAudio version: " < < Pa_GetVersionInfo ( ) - > versionText ;
int numDevices ;
numDevices = Pa_GetDeviceCount ( ) ;
qInfo ( logAudio ( ) ) < < " Pa_CountDevices returned " < < numDevices ;
const PaDeviceInfo * info ;
2022-01-26 09:49:52 +00:00
# else
2022-01-17 17:23:55 +00:00
const auto audioOutputs = QAudioDeviceInfo : : availableDevices ( QAudio : : AudioOutput ) ;
const auto audioInputs = QAudioDeviceInfo : : availableDevices ( QAudio : : AudioInput ) ;
# endif
2022-01-26 09:49:52 +00:00
for ( int i = 0 ; i < numRadios ; i + + ) {
if ( tempNum = = 0 ) {
settings - > beginGroup ( " Radio " ) ;
2022-01-17 17:23:55 +00:00
}
2022-01-26 09:49:52 +00:00
else {
settings - > setArrayIndex ( i ) ;
2022-01-17 17:23:55 +00:00
}
2022-01-27 19:11:16 +00:00
RIGCONFIG * tempPrefs = new RIGCONFIG ( ) ;
tempPrefs - > civAddr = ( unsigned char ) settings - > value ( " RigCIVuInt " , defPrefs . radioCIVAddr ) . toInt ( ) ;
tempPrefs - > forceRTSasPTT = ( bool ) settings - > value ( " ForceRTSasPTT " , defPrefs . forceRTSasPTT ) . toBool ( ) ;
tempPrefs - > serialPort = settings - > value ( " SerialPortRadio " , defPrefs . serialPortRadio ) . toString ( ) ;
tempPrefs - > rigName = settings - > value ( " RigName " , " <NONE> " ) . toString ( ) ;
tempPrefs - > baudRate = ( quint32 ) settings - > value ( " SerialPortBaud " , defPrefs . serialPortBaud ) . toInt ( ) ;
if ( tempPrefs - > rigName = = " <NONE> " )
2022-01-17 17:23:55 +00:00
{
2022-01-26 09:49:52 +00:00
foreach ( const QSerialPortInfo & serialPortInfo , QSerialPortInfo : : availablePorts ( ) )
2022-01-17 17:23:55 +00:00
{
2022-02-02 16:07:03 +00:00
qDebug ( logSystem ( ) ) < < " Serial Port found: " < < serialPortInfo . portName ( ) < < " Manufacturer: " < < serialPortInfo . manufacturer ( ) < < " Product ID " < < serialPortInfo . description ( ) < < " S/N " < < serialPortInfo . serialNumber ( ) ;
2022-01-27 19:11:16 +00:00
if ( serialPortInfo . portName ( ) = = tempPrefs - > serialPort & & ! serialPortInfo . serialNumber ( ) . isEmpty ( ) )
2022-01-26 09:49:52 +00:00
{
2022-01-27 19:11:16 +00:00
tempPrefs - > rigName = serialPortInfo . serialNumber ( ) ;
2022-01-26 09:49:52 +00:00
}
2022-01-17 17:23:55 +00:00
}
}
2022-01-26 09:49:52 +00:00
QString guid = settings - > value ( " GUID " , " " ) . toString ( ) ;
if ( guid . isEmpty ( ) ) {
guid = QUuid : : createUuid ( ) . toString ( ) ;
settings - > setValue ( " GUID " , guid ) ;
2022-01-17 17:23:55 +00:00
}
2022-01-30 10:55:12 +00:00
# if (QT_VERSION >= QT_VERSION_CHECK(5,10,0))
2022-01-29 22:50:58 +00:00
memcpy ( tempPrefs - > guid , QUuid : : fromString ( guid ) . toRfc4122 ( ) . constData ( ) , GUIDLEN ) ;
2022-01-30 10:55:12 +00:00
# endif
2022-01-27 19:11:16 +00:00
tempPrefs - > rxAudioSetup . isinput = true ;
tempPrefs - > txAudioSetup . isinput = false ;
tempPrefs - > rxAudioSetup . localAFgain = 255 ;
tempPrefs - > txAudioSetup . localAFgain = 255 ;
tempPrefs - > rxAudioSetup . resampleQuality = 4 ;
tempPrefs - > txAudioSetup . resampleQuality = 4 ;
tempPrefs - > rxAudioSetup . name = settings - > value ( " AudioInput " , " " ) . toString ( ) ;
tempPrefs - > txAudioSetup . name = settings - > value ( " AudioOutput " , " " ) . toString ( ) ;
bool rxDeviceFound = false ;
bool txDeviceFound = false ;
2022-01-26 09:49:52 +00:00
// Find the actual audio devices
# if defined(RTAUDIO)
for ( unsigned int i = 1 ; i < devices ; i + + ) {
info = audio - > getDeviceInfo ( i ) ;
if ( info . outputChannels > 0 ) {
2022-02-02 16:02:40 +00:00
if ( tempPrefs - > txAudioSetup . name = = info - > name ) {
tempPrefs - > txAudioSetup . port = i ;
2022-01-27 19:11:16 +00:00
txDeviceFound = true ;
2022-01-26 09:49:52 +00:00
}
}
if ( info . inputChannels > 0 ) {
2022-02-02 16:02:40 +00:00
if ( tempPrefs - > rxAudioSetup . name = = info - > name ) {
tempPrefs - > rxAudioSetup . port = i ;
2022-01-27 19:11:16 +00:00
rxDeviceFound = true ;
2022-01-26 09:49:52 +00:00
}
}
2022-01-18 00:11:15 +00:00
}
2022-01-26 09:49:52 +00:00
# elif defined(PORTAUDIO)
for ( int i = 0 ; i < numDevices ; i + + )
2022-01-18 00:11:15 +00:00
{
2022-01-26 09:49:52 +00:00
info = Pa_GetDeviceInfo ( i ) ;
if ( info - > maxInputChannels > 0 ) {
2022-02-02 16:02:40 +00:00
if ( tempPrefs - > txAudioSetup . name = = info - > name ) {
tempPrefs - > txAudioSetup . port = i ;
2022-01-27 19:11:16 +00:00
txDeviceFound = true ;
2022-01-26 09:49:52 +00:00
}
}
if ( info - > maxOutputChannels > 0 ) {
2022-02-02 16:02:40 +00:00
if ( tempPrefs - > rxAudioSetup . name = = info - > name ) {
tempPrefs - > rxAudioSetup . port = i ;
2022-01-27 19:11:16 +00:00
rxDeviceFound = true ;
}
2022-01-26 09:49:52 +00:00
}
2022-01-18 00:11:15 +00:00
}
2022-01-26 09:49:52 +00:00
# else
2022-01-18 00:11:15 +00:00
2022-01-26 09:49:52 +00:00
/* If no external library is configured, use QTMultimedia
// Set these to default audio devices initially.
*/
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
//qInfo(logAudio()) << "Looking for audio output devices";
for ( const QAudioDeviceInfo & deviceInfo : audioOutputs ) {
2022-02-02 16:07:03 +00:00
qDebug ( logSystem ( ) ) < < " Found Audio output: " < < deviceInfo . deviceName ( ) ;
2022-01-29 22:50:58 +00:00
if ( deviceInfo . deviceName ( ) = = tempPrefs - > txAudioSetup . name
# ifdef Q_OS_WIN
& & deviceInfo . realm ( ) = = " wasapi "
# endif
) {
2022-01-30 10:29:23 +00:00
qDebug ( logSystem ( ) ) < < " Audio output: " < < deviceInfo . deviceName ( ) ;
2022-01-27 19:11:16 +00:00
tempPrefs - > txAudioSetup . port = deviceInfo ;
txDeviceFound = true ;
2022-01-26 09:49:52 +00:00
}
}
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
//qInfo(logAudio()) << "Looking for audio input devices";
for ( const QAudioDeviceInfo & deviceInfo : audioInputs ) {
2022-02-02 16:07:03 +00:00
qDebug ( logSystem ( ) ) < < " Found Audio input: " < < deviceInfo . deviceName ( ) ;
2022-01-29 22:50:58 +00:00
if ( deviceInfo . deviceName ( ) = = tempPrefs - > rxAudioSetup . name
# ifdef Q_OS_WIN
& & deviceInfo . realm ( ) = = " wasapi "
# endif
) {
2022-01-30 10:29:23 +00:00
qDebug ( logSystem ( ) ) < < " Audio input: " < < deviceInfo . deviceName ( ) ;
2022-01-27 19:11:16 +00:00
tempPrefs - > rxAudioSetup . port = deviceInfo ;
rxDeviceFound = true ;
2022-01-17 17:23:55 +00:00
}
}
2022-01-26 09:49:52 +00:00
# endif
2022-01-27 19:11:16 +00:00
if ( ! txDeviceFound ) {
qInfo ( ) < < " Cannot find txAudioDevice " < < tempPrefs - > txAudioSetup . name ;
}
if ( ! rxDeviceFound ) {
qInfo ( ) < < " Cannot find rxAudioDevice " < < tempPrefs - > rxAudioSetup . name ;
}
tempPrefs - > rig = Q_NULLPTR ;
tempPrefs - > rigThread = Q_NULLPTR ;
2022-01-26 09:49:52 +00:00
serverConfig . rigs . append ( tempPrefs ) ;
if ( tempNum = = 0 ) {
settings - > endGroup ( ) ;
2022-01-17 17:23:55 +00:00
}
}
2022-01-26 09:49:52 +00:00
if ( tempNum > 0 ) {
settings - > endArray ( ) ;
}
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
settings - > beginGroup ( " Server " ) ;
serverConfig . enabled = settings - > value ( " ServerEnabled " , false ) . toBool ( ) ;
serverConfig . controlPort = settings - > value ( " ServerControlPort " , 50001 ) . toInt ( ) ;
serverConfig . civPort = settings - > value ( " ServerCivPort " , 50002 ) . toInt ( ) ;
serverConfig . audioPort = settings - > value ( " ServerAudioPort " , 50003 ) . toInt ( ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
serverConfig . users . clear ( ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
int numUsers = settings - > beginReadArray ( " Users " ) ;
if ( numUsers > 0 ) {
2022-01-17 17:23:55 +00:00
{
2022-01-26 09:49:52 +00:00
for ( int f = 0 ; f < numUsers ; f + + )
{
settings - > setArrayIndex ( f ) ;
SERVERUSER user ;
user . username = settings - > value ( " Username " , " " ) . toString ( ) ;
user . password = settings - > value ( " Password " , " " ) . toString ( ) ;
user . userType = settings - > value ( " UserType " , 0 ) . toInt ( ) ;
serverConfig . users . append ( user ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
}
}
settings - > endArray ( ) ;
2022-01-17 17:23:55 +00:00
}
2022-01-26 09:49:52 +00:00
else {
/* Support old way of storing users just to get them loaded*/
settings - > endArray ( ) ;
numUsers = settings - > value ( " ServerNumUsers " , 2 ) . toInt ( ) ;
for ( int f = 0 ; f < numUsers ; f + + )
2022-01-17 17:23:55 +00:00
{
2022-01-26 09:49:52 +00:00
SERVERUSER user ;
user . username = settings - > value ( " ServerUsername_ " + QString : : number ( f ) , " " ) . toString ( ) ;
user . password = settings - > value ( " ServerPassword_ " + QString : : number ( f ) , " " ) . toString ( ) ;
user . userType = settings - > value ( " ServerUserType_ " + QString : : number ( f ) , 0 ) . toInt ( ) ;
serverConfig . users . append ( user ) ;
2022-01-17 17:23:55 +00:00
}
}
2022-01-26 09:49:52 +00:00
settings - > endGroup ( ) ;
settings - > sync ( ) ;
2022-01-17 17:23:55 +00:00
2022-01-26 09:49:52 +00:00
# if defined(RTAUDIO)
delete audio ;
# endif
2022-01-17 17:23:55 +00:00
}
2022-01-26 09:49:52 +00:00
void servermain : : receivePTTstatus ( bool pttOn )
2022-01-17 17:23:55 +00:00
{
2022-01-26 09:49:52 +00:00
// This is the only place where amTransmitting and the transmit button text should be changed:
//qInfo(logSystem()) << "PTT status: " << pttOn;
amTransmitting = pttOn ;
2022-01-17 17:23:55 +00:00
}
2022-01-18 00:11:15 +00:00
void servermain : : handlePttLimit ( )
2022-01-17 17:23:55 +00:00
{
2022-01-26 09:49:52 +00:00
// ptt time exceeded!
2022-01-18 00:11:15 +00:00
std : : cout < < " Transmit timeout at 3 minutes. Sending PTT OFF command now. \n " ;
2022-01-26 09:49:52 +00:00
emit setPTT ( false ) ;
emit getPTT ( ) ;
2022-01-17 17:23:55 +00:00
}
void servermain : : receiveBaudRate ( quint32 baud )
{
qInfo ( ) < < " Received serial port baud rate from remote server: " < < baud ;
prefs . serialPortBaud = baud ;
}
void servermain : : powerRigOn ( )
{
emit sendPowerOn ( ) ;
}
void servermain : : powerRigOff ( )
{
emit sendPowerOff ( ) ;
}
2022-01-18 00:18:52 +00:00
void servermain : : receiveStateInfo ( rigstate * state )
{
2022-01-26 09:49:52 +00:00
qInfo ( " Received rig state for wfmain " ) ;
2022-01-30 10:29:23 +00:00
Q_UNUSED ( state ) ;
2022-01-26 09:49:52 +00:00
//rigState = state;
2022-01-17 17:23:55 +00:00
}