2020-02-21 11:05:07 +02:00
# include "TransceiverBase.hpp"
# include <exception>
# include <QString>
# include <QTimer>
# include <QThread>
# include <QDebug>
# include "moc_TransceiverBase.cpp"
namespace
{
auto const unexpected = TransceiverBase : : tr ( " Unexpected rig error " ) ;
}
2021-02-22 17:35:24 +02:00
void TransceiverBase : : start ( unsigned sequence_number , JTDXDateTime * jtdxdatetime ) noexcept
2020-02-21 11:05:07 +02:00
{
QString message ;
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
FILE * pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-12-12 12:29:33 +02:00
fprintf ( pFile , " Transceiver start #:%d \n " , sequence_number ) ;
2021-03-12 17:43:55 +02:00
else
2021-12-12 12:29:33 +02:00
fprintf ( pFile , " %s Transceiver start #:%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
try
{
2020-04-13 19:53:20 +03:00
last_sequence_number_ = sequence_number ;
2021-02-22 17:35:24 +02:00
jtdxtime_ = jtdxdatetime ;
2020-02-21 11:05:07 +02:00
may_update u { this , true } ;
shutdown ( ) ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver start shutdown done \n " ) ;
2021-02-28 12:10:31 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver start shutdown done \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
startup ( ) ;
}
catch ( std : : exception const & e )
{
message = e . what ( ) ;
}
catch ( . . . )
{
message = unexpected ;
}
if ( ! message . isEmpty ( ) )
{
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver start message %s \n " , message . toStdString ( ) . c_str ( ) ) ;
2021-03-12 17:43:55 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver start message %s \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , message . toStdString ( ) . c_str ( ) ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
offline ( message ) ;
}
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver start done \n " ) ;
2021-03-12 17:43:55 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver start done \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
}
void TransceiverBase : : set ( TransceiverState const & s ,
unsigned sequence_number ) noexcept
{
TRACE_CAT ( " TransceiverBase " , " #: " < < sequence_number < < s ) ;
2020-04-21 19:52:35 +03:00
# if JTDX_DEBUG_TO_FILE
2021-02-28 12:10:31 +02:00
long long ms = 0 ;
2020-04-21 19:52:35 +03:00
FILE * pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver set state %d #:%d \n " , s . online ( ) , sequence_number ) ;
2021-02-28 12:10:31 +02:00
else {
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver set state %d #:%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , s . online ( ) , sequence_number ) ;
2021-02-28 12:10:31 +02:00
ms = jtdxtime_ - > currentMSecsSinceEpoch2 ( ) ;
}
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
QString message ;
try
{
2020-04-13 19:53:20 +03:00
last_sequence_number_ = sequence_number ;
2020-02-21 11:05:07 +02:00
may_update u { this , true } ;
bool was_online { requested_ . online ( ) } ;
if ( ! s . online ( ) & & was_online )
{
2020-04-21 19:52:35 +03:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " Transceiver shutdown #:%d " , sequence_number ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Transceiver shutdown #:%d " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
shutdown ( ) ;
}
else if ( s . online ( ) & & ! was_online )
{
2020-04-21 19:52:35 +03:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " restart shutdown #:%d " , sequence_number ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s restart shutdown #:%d " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
shutdown ( ) ;
2020-04-21 19:52:35 +03:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " start #:%d " , sequence_number ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s start #:%d " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
startup ( ) ;
}
if ( requested_ . online ( ) )
{
2021-03-12 17:43:55 +02:00
bool audio_cmd { false } ;
2021-02-22 17:35:24 +02:00
if ( requested_ . blocksize ( ) ! = s . blocksize ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d blocksize=%d \n " , sequence_number , s . blocksize ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d blocksize=%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . blocksize ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_blocksize ( s . blocksize ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . blocksize ( s . blocksize ( ) ) ;
}
if ( requested_ . period ( ) ! = s . period ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d period=%0.1f \n " , sequence_number , s . period ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d period=%0.1f \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . period ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_period ( s . period ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . period ( s . period ( ) ) ;
}
if ( requested_ . spread ( ) ! = s . spread ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d spread=%0.1f \n " , sequence_number , s . spread ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d spread=%0.1f \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . spread ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_spread ( s . spread ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . spread ( s . spread ( ) ) ;
}
if ( requested_ . nsym ( ) ! = s . nsym ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d nsym=%d \n " , sequence_number , s . nsym ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d nsym=%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . nsym ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_nsym ( s . nsym ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . nsym ( s . nsym ( ) ) ;
}
if ( requested_ . trfrequency ( ) ! = s . trfrequency ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d trfrequency=%0.1f \n " , sequence_number , s . trfrequency ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d trfrequency=%0.1f \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . trfrequency ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_trfrequency ( s . trfrequency ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . trfrequency ( s . trfrequency ( ) ) ;
}
if ( requested_ . volume ( ) ! = s . volume ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d volume=%0.1f \n " , sequence_number , s . volume ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d volume=%0.1f \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . volume ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_txvolume ( s . volume ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . volume ( s . volume ( ) ) ;
}
if ( requested_ . audio ( ) ! = s . audio ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d audio=%d \n " , sequence_number , s . audio ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d audio=%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . audio ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_audio ( s . audio ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . audio ( s . audio ( ) ) ;
}
if ( requested_ . tx_audio ( ) ! = s . tx_audio ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d tx_audio=%d \n " , sequence_number , s . tx_audio ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d tx_audio=%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . tx_audio ( ) ) ;
fclose ( pFile ) ;
2021-02-22 17:35:24 +02:00
# endif
if ( s . tx_audio ( ) ) {
do_modulator_start ( s . symbolslength ( ) , s . framespersymbol ( ) , s . trfrequency ( ) , s . tonespacing ( ) , s . synchronize ( ) , s . dbsnr ( ) , s . trperiod ( ) ) ;
requested_ . symbolslength ( s . symbolslength ( ) ) ;
requested_ . framespersymbol ( s . framespersymbol ( ) ) ;
requested_ . trfrequency ( s . trfrequency ( ) ) ;
requested_ . tonespacing ( s . tonespacing ( ) ) ;
requested_ . synchronize ( s . synchronize ( ) ) ;
requested_ . dbsnr ( s . dbsnr ( ) ) ;
requested_ . trperiod ( s . trperiod ( ) ) ;
} else {
do_modulator_stop ( s . quick ( ) ) ;
requested_ . quick ( s . quick ( ) ) ;
}
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . tx_audio ( s . tx_audio ( ) ) ;
}
if ( requested_ . tune ( ) ! = s . tune ( ) ) {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d tune=%d \n " , sequence_number , s . tune ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d tune=%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . tune ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_tune ( s . tune ( ) ) ;
2021-03-12 17:43:55 +02:00
audio_cmd = true ;
2021-02-22 17:35:24 +02:00
requested_ . tune ( s . tune ( ) ) ;
}
2021-03-12 17:43:55 +02:00
if ( ! audio_cmd ) {
bool ptt_on { false } ;
bool ptt_off { false } ;
if ( requested_ . ft4_mode ( ) ! = s . ft4_mode ( ) ) {
# if JTDX_DEBUG_TO_FILE
2020-04-21 19:52:35 +03:00
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d ft4_mode=%d \n " , sequence_number , s . ft4_mode ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s Timing #:%d ft4_mode=%d \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . ft4_mode ( ) ) ;
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
2021-03-12 17:43:55 +02:00
# endif
do_post_ft4_mode ( s . ft4_mode ( ) ) ;
requested_ . ft4_mode ( s . ft4_mode ( ) ) ;
2020-02-21 11:05:07 +02:00
}
2021-03-12 17:43:55 +02:00
if ( s . ptt ( ) ! = requested_ . ptt ( ) )
{
ptt_on = s . ptt ( ) ;
ptt_off = ! s . ptt ( ) ;
}
if ( ptt_off )
{
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " #:%d ptt_off \n " , sequence_number ) ;
else
fprintf ( pFile , " %s Timing #:%d ptt_off \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
fclose ( pFile ) ;
# endif
do_ptt ( false ) ;
do_post_ptt ( false ) ;
2021-08-01 13:43:54 +03:00
if ( ! requested_ . audio ( ) ) QThread : : msleep ( 100 ) ; // some rigs cannot process CAT
2021-05-11 08:00:30 +03:00
// commands while switching from
// Tx to Rx
2021-03-12 17:43:55 +02:00
}
if ( s . frequency ( ) // ignore bogus zero frequencies
& & ( ( s . frequency ( ) ! = requested_ . frequency ( ) // and QSY
| | ( s . mode ( ) ! = UNK & & s . mode ( ) ! = requested_ . mode ( ) ) ) // or mode change
| | ptt_off ) ) // or just returned to rx
{
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " #:%d do_frequency %lld \n " , sequence_number , s . frequency ( ) ) ;
else
fprintf ( pFile , " %s Timing #:%d do_frequency %lld \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . frequency ( ) ) ;
fclose ( pFile ) ;
# endif
2021-03-16 10:28:32 +02:00
requested_ . frequency ( s . frequency ( ) ) ;
requested_ . mode ( s . mode ( ) ) ;
2021-03-12 17:43:55 +02:00
do_frequency ( s . frequency ( ) , s . mode ( ) , ptt_off ) ;
do_post_frequency ( s . frequency ( ) , s . mode ( ) ) ;
2020-02-21 11:05:07 +02:00
2021-03-12 17:43:55 +02:00
// record what actually changed
requested_ . frequency ( actual_ . frequency ( ) ) ;
requested_ . mode ( actual_ . mode ( ) ) ;
}
2021-03-16 10:28:32 +02:00
else if ( ! s . tx_frequency ( )
2021-03-12 17:43:55 +02:00
| | ( s . tx_frequency ( ) > 10000 // ignore bogus startup values
& & s . tx_frequency ( ) < std : : numeric_limits < Frequency > : : max ( ) - 10000 ) )
{
if ( ( s . tx_frequency ( ) ! = requested_ . tx_frequency ( ) // and QSY
| | ( s . mode ( ) ! = UNK & & s . mode ( ) ! = requested_ . mode ( ) ) ) // or mode change
// || s.split () != requested_.split ())) // or split change
| | ( s . tx_frequency ( ) & & ptt_on ) ) // or about to tx split
{
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " #:%d do_tx_frequency %lld \n " , sequence_number , s . tx_frequency ( ) ) ;
else
fprintf ( pFile , " %s Timing #:%d do_tx_frequency %lld \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , s . tx_frequency ( ) ) ;
fclose ( pFile ) ;
# endif
2021-03-16 10:28:32 +02:00
requested_ . tx_frequency ( s . tx_frequency ( ) ) ;
requested_ . split ( s . tx_frequency ( ) ! = 0 ) ;
2021-03-12 17:43:55 +02:00
do_tx_frequency ( s . tx_frequency ( ) , s . mode ( ) , ptt_on ) ;
do_post_tx_frequency ( s . tx_frequency ( ) , s . mode ( ) ) ;
2020-02-21 11:05:07 +02:00
2021-03-12 17:43:55 +02:00
// record what actually changed
requested_ . tx_frequency ( actual_ . tx_frequency ( ) ) ;
requested_ . split ( actual_ . split ( ) ) ;
}
}
if ( ptt_on )
{
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " #:%d ptt_on \n " , sequence_number ) ;
else
fprintf ( pFile , " %s Timing #:%d ptt_on \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
fclose ( pFile ) ;
# endif
do_ptt ( true ) ;
do_post_ptt ( true ) ;
// QThread::msleep (100 + ms); // some rigs cannot process CAT
// commands while switching from
// Rx to Tx
}
2020-02-21 11:05:07 +02:00
2021-03-12 17:43:55 +02:00
// record what actually changed
if ( ptt_on | | ptt_off ) requested_ . ptt ( actual_ . ptt ( ) ) ;
}
2020-04-21 19:52:35 +03:00
} else {
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d NOT ONLINE \n " , sequence_number ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s #:%d NOT ONLINE \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number ) ;
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
# endif
}
2020-02-21 11:05:07 +02:00
}
catch ( std : : exception const & e )
{
message = e . what ( ) ;
}
catch ( . . . )
{
message = unexpected ;
}
2020-04-18 10:51:54 +03:00
2020-02-21 11:05:07 +02:00
if ( ! message . isEmpty ( ) )
{
2020-04-21 19:52:35 +03:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " #:%d set message %s \n " , sequence_number , message . toStdString ( ) . c_str ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-03-12 17:43:55 +02:00
fprintf ( pFile , " %s #:%d set message %s \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , sequence_number , message . toStdString ( ) . c_str ( ) ) ;
2020-04-21 19:52:35 +03:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
offline ( message ) ;
}
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver set %d #:%d end \n " , s . online ( ) , sequence_number ) ;
2021-03-12 17:43:55 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver set %d #:%d end %lld ms. \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , s . online ( ) , sequence_number , jtdxtime_ - > currentMSecsSinceEpoch2 ( ) - ms ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
}
void TransceiverBase : : startup ( )
{
2020-04-13 19:53:20 +03:00
QString message ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
2021-02-28 12:10:31 +02:00
long long ms = 0 ;
2021-02-22 17:35:24 +02:00
FILE * pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver startup \n " ) ;
2021-02-28 12:10:31 +02:00
else {
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver startup \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-28 12:10:31 +02:00
ms = jtdxtime_ - > currentMSecsSinceEpoch2 ( ) ;
}
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-04-13 19:53:20 +03:00
try
{
actual_ . online ( true ) ;
requested_ . online ( true ) ;
2021-02-22 17:35:24 +02:00
auto res = do_start ( jtdxtime_ ) ;
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " do_start \n " ) ;
else
fprintf ( pFile , " %s Timing do_start \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
do_post_start ( jtdxtime_ ) ;
2020-04-13 19:53:20 +03:00
Q_EMIT resolution ( res ) ;
}
catch ( std : : exception const & e )
{
message = e . what ( ) ;
}
catch ( . . . )
{
message = unexpected ;
}
if ( ! message . isEmpty ( ) )
{
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver startup message %s \n " , message . toStdString ( ) . c_str ( ) ) ;
2021-02-28 12:10:31 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver startup message %s \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , message . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-04-13 19:53:20 +03:00
offline ( message ) ;
}
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver startup end \n " ) ;
2021-03-12 17:43:55 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver startup end %lld ms. \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , jtdxtime_ - > currentMSecsSinceEpoch2 ( ) - ms ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
}
void TransceiverBase : : shutdown ( )
{
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
2021-02-28 12:10:31 +02:00
long long ms = 0 ;
2021-02-22 17:35:24 +02:00
FILE * pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr ) {
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver shutdown \n " ) ;
2021-02-28 12:10:31 +02:00
}
else {
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver shutdown \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-28 12:10:31 +02:00
ms = jtdxtime_ - > currentMSecsSinceEpoch2 ( ) ;
}
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
2021-03-29 22:44:25 +03:00
# endif
may_update u { this } ;
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " may_update \n " ) ;
else
fprintf ( pFile , " %s Timing may_update \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
fclose ( pFile ) ;
2021-02-22 17:35:24 +02:00
# endif
2020-02-21 11:05:07 +02:00
if ( requested_ . online ( ) )
{
try
{
// try and ensure PTT isn't left set
do_ptt ( false ) ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " do_ptt:0 \n " ) ;
else
fprintf ( pFile , " %s Timing do_ptt:0 \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
do_post_ptt ( false ) ;
if ( requested_ . split ( ) )
{
// try and reset split mode
do_tx_frequency ( 0 , UNK , true ) ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " do_tx_freq:0 \n " ) ;
else
fprintf ( pFile , " %s Timing do_tx_freq:0 \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
do_post_tx_frequency ( 0 , UNK ) ;
}
}
catch ( . . . )
{
// don't care about exceptions
}
}
2021-04-08 09:14:54 +03:00
// printf("Transceiver shutdow b4 dostop()\n");
2020-02-21 11:05:07 +02:00
do_stop ( ) ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
fprintf ( pFile , " do_stop \n " ) ;
else
fprintf ( pFile , " %s Timing do_stop \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
do_post_stop ( ) ;
2020-04-13 19:53:20 +03:00
actual_ = TransceiverState { } ;
requested_ = TransceiverState { } ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver shutdown end \n " ) ;
2021-02-28 12:10:31 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver shutdown end %lld ms. \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , jtdxtime_ - > currentMSecsSinceEpoch2 ( ) - ms ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2021-02-28 12:10:31 +02:00
// jtdxtime_ = nullptr;
2020-02-21 11:05:07 +02:00
}
void TransceiverBase : : stop ( ) noexcept
{
QString message ;
2021-02-22 17:35:24 +02:00
# if JTDX_DEBUG_TO_FILE
FILE * pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
2021-02-28 12:10:31 +02:00
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver stop \n " ) ;
2021-02-28 12:10:31 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver stop \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-02-22 17:35:24 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
try
{
shutdown ( ) ;
}
catch ( std : : exception const & e )
{
message = e . what ( ) ;
}
catch ( . . . )
{
message = unexpected ;
}
if ( ! message . isEmpty ( ) )
{
offline ( message ) ;
}
else
{
Q_EMIT finished ( ) ;
}
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver stop end \n " ) ;
2021-03-12 17:43:55 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver stop end \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
}
void TransceiverBase : : update_rx_frequency ( Frequency rx )
{
2020-04-13 19:53:20 +03:00
if ( rx )
{
actual_ . frequency ( rx ) ;
requested_ . frequency ( rx ) ; // track rig changes
}
2020-02-21 11:05:07 +02:00
}
void TransceiverBase : : update_other_frequency ( Frequency tx )
{
actual_ . tx_frequency ( tx ) ;
}
void TransceiverBase : : update_split ( bool state )
{
actual_ . split ( state ) ;
}
void TransceiverBase : : update_mode ( MODE m )
{
actual_ . mode ( m ) ;
requested_ . mode ( m ) ; // track rig changes
}
void TransceiverBase : : update_PTT ( bool state )
{
actual_ . ptt ( state ) ;
}
void TransceiverBase : : update_level ( int l )
{
actual_ . level ( l ) ;
// requested_.level (l); // track rig changes
}
void TransceiverBase : : update_power ( unsigned int p )
{
actual_ . power ( p ) ;
// requested_.power (p); // track rig changes
}
2021-11-17 19:59:24 +02:00
void TransceiverBase : : update_swr ( unsigned int p )
{
actual_ . swr ( p ) ;
// requested_.swr (p); // track rig changes
}
2020-02-21 11:05:07 +02:00
void TransceiverBase : : update_complete ( bool force_signal )
{
if ( ( do_pre_update ( ) & & actual_ ! = last_ ) | | force_signal )
{
Q_EMIT update ( actual_ , last_sequence_number_ ) ;
last_ = actual_ ;
}
}
void TransceiverBase : : offline ( QString const & reason )
{
2021-03-12 17:43:55 +02:00
# if JTDX_DEBUG_TO_FILE
FILE * pFile = fopen ( debug_file_ . c_str ( ) , " a " ) ;
if ( jtdxtime_ = = nullptr )
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " Transceiver offline %s \n " , reason . toStdString ( ) . c_str ( ) ) ;
2021-03-12 17:43:55 +02:00
else
2021-04-08 09:14:54 +03:00
fprintf ( pFile , " %s Transceiver offline %s \n " , jtdxtime_ - > currentDateTimeUtc2 ( ) . toString ( " hh:mm:ss.zzz " ) . toStdString ( ) . c_str ( ) , reason . toStdString ( ) . c_str ( ) ) ;
2021-03-12 17:43:55 +02:00
fclose ( pFile ) ;
# endif
2020-02-21 11:05:07 +02:00
Q_EMIT failure ( reason ) ;
try
{
shutdown ( ) ;
}
catch ( . . . )
{
// don't care
}
}