mirror of
https://github.com/saitohirga/WSJT-X.git
synced 2024-11-24 05:08:38 -05:00
50846fe90a
This allows writable files to be located in the "correct" location for each platform rather than in the directory of the executable which, in general, is not recommended or allowed in some cases. A preprocessor macro WSJT_STANDARD_FILE_LOCATIONS is used to switch be tween old and new functionality, currently it is on by default. It can be turned off by defining it to a false value (0) or more simply with cmake-gui setting the option with the same name. JTAlert can only work with the old non-standard file locations until Laurie VK3AMA chooses to support the new file locations. Even if the above is not enabled; the QSettings file is written to a user specific location so it will be shared by all instances of the program (i.e. across upgrades). See below for multiple concurrent instance support changes. Added a command line parser module for Fortran. Added 'lib/options.f90' to facilitate more complex argument passing to jt9 to cover explicit file locations. Changed the way multiple concurrent instances are handled. This is to allow the program to be run multiple times from the same installation directory. A new wsjtx command line optional argument is available "-r" or "--rig" which enables multiple concurrent instance support. The parameter of the new option is a unique name signifying a rig or equivalent. The name is used as the shared memory segment key and in window titles. The name is also used to access unique settings files and writable data files like ALL.TXT and log files. No attempt has been made to share these files between concurrent instances. If "-r" or "--rig" is used without a parameter it still enables multiple concurrent instance support for that instance. All instances must use a unique parameter, one of which may be empty. The rig name is appended the QCoreApplication::applicationName() for convenient usage like window titles. Set non Qt locale to "C". This ensures that C library functions give consistent results whatever the system locale is set to. QApplication follows the system locale as before. Thus using QApplication and its descendants like widgets and QString for all user visible formating will give correct l10n and using C/C++ library will give consistent formatting across locales. Added top level C++ exception handling to main.cpp. Because the new transceiver framework uses exceptions internally, the main function now handles any exceptions that aren't caught. Retired devsetup, replaced with Configuration. Configuration is a class that encapsulates most of the configuration behavior. Because rig configuration is so closely coupled with rig operation, Configuration serves as a proxy for access to the rig control functions. See Configuration.hpp for more details of the Configuration interface. Menu changes. Various checkable menu actions moved from main menu to the Configuration dialog. The whole settings menu has been retired with the single "Settings..." action moved to the file menu for consistency on Mac where it appears as "Preferences" in line with Mac guidelines. New data models for data used by the application. ADIF amateur band parameters, free text message macros, spot working frequencies and, station information (station descriptions and transverter offsets per band) each implement the QAbstractItemModel interface allowing them to be used directly with Qt view widgets (Bands.hpp, FrequencyList.hpp and, StationList.hpp). Configuration manages maintenance of an instance of all but the former of the above models. The ADIF band model is owned by Configuration but requires no user maintenance as it is immutable. Band combo box gets more functionality. This widget is now an editable QComboBox with some extra input capabilities. The popup list is still the list of spot working frequencies, now showing the actual frequency decorated with the band name. This allows multiple spot frequencies on a band if required. The line edit allows direct frequency entry in mega-Hertz with a completer built in to suggest the available spot working frequencies. It also allows band name entry where the first available spot working frequency is selected. Recognized band names are those that are defined by the ADIF specification and can be found in in the implementation of the ADIF bands model (Bands.cpp). If an out of band frequency is chosen, the line edit shows a warning red background and the text "OOB". Out of band is only defined by the ADIF band limits which in general are wider than any entities regulations. Qt 5.2 now supports default audio i/p and o/p devices. These devices are placeholders for whatever the user defines as the default device. Because of this they need special treatment as the actual device used is chosen at open time behind the scenes. Close-down behavior is simplified. The close-down semantics were broken such that some objects were not being shut down cleanly, this required amendments to facilitate correct close down of threads. User font selection added to Configuration UI. Buttons to set the application font and the font for the band and Rx frequency activity widgets have been added to the Configuration UI to replace the file based font size control. Free text macros now selected directly. The free text line edit widgets are now editable combo boxes that have the current free text macro definitions as their popup list. The old context menu to do this has been retired. Astronomical data window dynamically formatted and has font a chooser. This window is now autonomous, has its own font chooser and, dynamically resizes to cover the contents. Double click to Tx enabled now has its own widget in the status bar. QDir used for portable path and file name handling throughout. The "Monitor", "Decode", "Enable Tx" and, "Tune" buttons are now checkable. Being checkable allows these buttons control their own state and rendering. Calls to PSK Reporter interface simplified. In mainwindow.cpp the calls to this interface are rationalized to just 3 locations. Manipulation of ALL.TXT simplified. Moved, where possible, to common functions. Elevated frequency types to be Qt types. Frequency and FrequencyDelta defined as Qt types in their meta-type system (Radio.hpp). They are integral types for maximum accuracy. Re-factored rig control calls in mainwindow.cpp. The new Configuration proxy access to rig control required many changes (mostly simplifications) to the MainWindow rig control code. Some common code has been gathered in member functions like qsy(), monitor(), band_changed() and auto_tx_mode(). Rig control enhancements. The rig control for clients interface is declared as an abstract interface (See Transceiver.hpp). Concrete implementations of this interface are provided for the Hamlib rig control library, DX Lab Suite Commander via a TCP/IP command channel, Ham Radio Deluxe also via a TCP/IP command channel and, OmniRig via its Windows COM server interface. Concrete Transceiver implementations are expected to be moved to a separate thread after construction since many operations are blocking and not suitable for running in a GUI thread. To facilitate this all instantiation of concrete Transceiver instances are handled by Configuration using a factory class (TransceiverFactory) for configuration parameter based instantiation. Various common functionality shared by different rig interface implementations are factored out into helper base classes that implement or delegate parts of the Transceiver interface. They are TransceiverBase which caches state to minimize expensive rig commands, it also maps the Transceiver interface into a more convenient form for implementation (template methods). PollingTransceiver that provides a state polling mechanism that only reports actual changes. EmulateSplitTransceiver that provides split operation by QSYing on PTT state changes. EmulateSplitTransceiver can be used with any implementation as it follows the GoF Decorator pattern and can wrap any Transceiver implementation. OmniRigTransceiver is derived directly from TransceiverBase since it doesn't require polling due to its asynchronous nature. OmniRigTransceiver is only built on Windows as it is a COM server client. To build it you must first install the OmniRig client on the development machine (http://www.dxatlas.com/omnirig/). DXLabSuiteCommanderTransceiver derives from PollingTransceiver since it is a synchronous communications channel. No third party library is required for this interface. HRDTransceiver also derives from PollingTransceiver. The HRD interface library has been reverse engineered to provide functionality with all available versions of HRD. No third party libraries are required. HamlibTransceiver likewise derives from PollingTransceiver since the Hamlib asynchronous interface is non-functional. Although this class will interface with the release version of Hamlib (1.2.15.3); for correct operation on most rigs it needs to run with the latest master branch code of Hamlib. During development many changes to Hamlib have been submitted and accepted, hence this requirement. Hamlib source can be obtained from git://git.code.sf.net/p/hamlib/code and at the time of writing he master branch was at SHA 6e4432. The Hamlib interface directly calls the "C" interface and the modified rigclass.{h,cpp} files have been retired. There is a rig type selection of "None" which may be used for non-CAT rigs, this is actually a connection to the dummy Hamlib device. PollingTransvceiver derives from TransceiverBase and TransceiverBase derives from the Transceiver interface. Each interface implementation offers some possibility of PTT control via a different serial port than the CAT port. We also support PTT control directly via a second serial port. This is done by delegating to a dummy Hamlib instance which is only used for PTT control. This means that DXLabSuiteCommanderTransceiver, HRDTransceiver and OmniRigTransceiver always wrap a dummy HamlibTransceiver instance. The factory class TransceiverFactory manages all these constructional complexities. Serial port selection combo boxes are now editable with a manually entered value being saved to the settings file. This allows a non-standard port device to be used without having to edit the settings file manually. For TCP/IP network CAT interfaces; the network address and port may be specified allowing the target device to be located on a different machine from the one running wsjtx if required. The default used when the address field is left blank is the correct one for normal usage on the local host. Selecting a polling interval of zero is no longer possible, this is because the rig control capability can no longer support one way connection. This is in line with most other CAT control software. In the Configuration dialog there are options to select split mode control by the software and mode control by the software. For the former "None", "Rig" and "Fake it" are available, for the latter "None", "USB" and, "Data" are available. Because tone generation is implicitly linked to split mode operation; it is no longer possible to have the software in split mode and the rig not or vice versa. This may mean some rigs cannot be used in split mode and therefore not in dual JT65+JT9 until issues with CAT control with that rig are resolved. Single mode with VOX keying and no CAT control are still possible so even the most basic transceiver setup is supported as before. Configuration now supports a frequency offset suitable for transverter operation. The station details model (StationList.hpp) includes a column to store an offset for each band if required. CMake build script improvements. The CMakeLists.txt from the 'lib' directory has been retired with its contents merged into the top level CMakeLists.txt. Install target support has been greatly improved with the Release build configuration now building a fully standalone installation on Mac and Windows. The Debug configuration still builds an installation that has environment dependencies for external libraries, which is desirable for testing and debugging. Package target support is largely complete for Mac, Windows and, Linux, it should be possible to build release installers directly from CMake/CPack. Cmake FindXXXX.cmake modules have been added to improve the location of fftw-3 and Hamlib packages. Version numbers are now stored in Versions.cmake and work in concert with automatic svn revision lookup during build. The version string becomes 'rlocal'± if there are any uncommitted changes in the build source tree. Moved resource like files to Qt resources. Because location of resource files (when they cannot go into the installation directory because of packaging rules) is hard to standardize. I have used the Qt resource system for all ancillary data files. Some like kvasd.dat are dumped out to the temp (working directory) because they are accessed by an external program, others like the audio samples are copied out so they appear in the data directory under the default save directory. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@3929 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
767 lines
22 KiB
C++
767 lines
22 KiB
C++
#include "HRDTransceiver.hpp"
|
|
|
|
#include <QHostAddress>
|
|
#include <QByteArray>
|
|
#include <QRegExp>
|
|
#include <QTcpSocket>
|
|
#include <QThread>
|
|
|
|
#include "NetworkServerLookup.hpp"
|
|
|
|
namespace
|
|
{
|
|
char const * const HRD_transceiver_name = "Ham Radio Deluxe";
|
|
int socket_wait_time {5000};
|
|
}
|
|
|
|
void HRDTransceiver::register_transceivers (TransceiverFactory::Transceivers * registry, int id)
|
|
{
|
|
(*registry)[HRD_transceiver_name] = TransceiverFactory::Capabilities (id, TransceiverFactory::Capabilities::network, "localhost:7809", true);
|
|
}
|
|
|
|
struct HRDMessage
|
|
{
|
|
// placement style new overload for outgoing messages that does the construction too
|
|
static void * operator new (size_t size, QString const& payload)
|
|
{
|
|
size += sizeof (QChar) * (payload.size () + 1); // space for terminator too
|
|
HRDMessage * storage (reinterpret_cast<HRDMessage *> (new char[size]));
|
|
storage->size_ = size ;
|
|
ushort const * pl (payload.utf16 ());
|
|
qCopy (pl, pl + payload.size () + 1, storage->payload_); // copy terminator too
|
|
storage->magic_1_ = magic_1_value_;
|
|
storage->magic_2_ = magic_2_value_;
|
|
storage->checksum_ = 0;
|
|
return storage;
|
|
}
|
|
|
|
// placement style new overload for incoming messages that does the construction too
|
|
//
|
|
// no memory allocation here
|
|
static void * operator new (size_t /* size */, QByteArray const& message)
|
|
{
|
|
// nasty const_cast here to avoid copying the message buffer
|
|
return const_cast<HRDMessage *> (reinterpret_cast<HRDMessage const *> (message.data ()));
|
|
}
|
|
|
|
void operator delete (void * p, size_t)
|
|
{
|
|
delete [] reinterpret_cast<char *> (p); // mirror allocation in operator new above
|
|
}
|
|
|
|
qint32 size_;
|
|
qint32 magic_1_;
|
|
qint32 magic_2_;
|
|
qint32 checksum_; // apparently not used
|
|
QChar payload_[0]; // UTF-16 (which is wchar_t on Windows)
|
|
|
|
static qint32 const magic_1_value_;
|
|
static qint32 const magic_2_value_;
|
|
};
|
|
|
|
qint32 const HRDMessage::magic_1_value_ (0x1234ABCD);
|
|
qint32 const HRDMessage::magic_2_value_ (0xABCD1234);
|
|
|
|
HRDTransceiver::HRDTransceiver (std::unique_ptr<TransceiverBase> wrapped, QString const& server, bool use_for_ptt, int poll_interval)
|
|
: PollingTransceiver {poll_interval}
|
|
, wrapped_ {std::move (wrapped)}
|
|
, use_for_ptt_ {use_for_ptt}
|
|
, server_ {server}
|
|
, hrd_ {0}
|
|
, protocol_ {none}
|
|
, current_radio_ {0}
|
|
, vfo_count_ {0}
|
|
, vfo_A_button_ {-1}
|
|
, vfo_B_button_ {-1}
|
|
, vfo_toggle_button_ {-1}
|
|
, mode_A_dropdown_ {-1}
|
|
, mode_B_dropdown_ {-1}
|
|
, split_mode_button_ {-1}
|
|
, split_mode_dropdown_ {-1}
|
|
, split_mode_dropdown_write_only_ {false}
|
|
, split_mode_dropdown_selection_on_ {-1}
|
|
, split_mode_dropdown_selection_off_ {-1}
|
|
, split_off_button_ {-1}
|
|
, tx_A_button_ {-1}
|
|
, tx_B_button_ {-1}
|
|
, ptt_button_ {-1}
|
|
, reversed_ {false}
|
|
{
|
|
}
|
|
|
|
HRDTransceiver::~HRDTransceiver ()
|
|
{
|
|
}
|
|
|
|
void HRDTransceiver::do_start ()
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::start";
|
|
#endif
|
|
|
|
wrapped_->start ();
|
|
|
|
auto server_details = network_server_lookup (server_, 7809u);
|
|
|
|
if (!hrd_)
|
|
{
|
|
hrd_ = new QTcpSocket {this}; // QObject takes ownership
|
|
}
|
|
|
|
hrd_->connectToHost (std::get<0> (server_details), std::get<1> (server_details));
|
|
if (!hrd_->waitForConnected (socket_wait_time))
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::start failed to connect:" << hrd_->errorString ();
|
|
#endif
|
|
|
|
throw error {"Failed to connect to Ham Radio Deluxe\n" + hrd_->errorString ().toLocal8Bit ()};
|
|
}
|
|
|
|
init_radio ();
|
|
}
|
|
|
|
void HRDTransceiver::do_stop ()
|
|
{
|
|
if (hrd_)
|
|
{
|
|
hrd_->close ();
|
|
}
|
|
|
|
if (wrapped_)
|
|
{
|
|
wrapped_->stop ();
|
|
}
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::stop: state:" << state () << "reversed =" << reversed_;
|
|
#endif
|
|
}
|
|
|
|
void HRDTransceiver::init_radio ()
|
|
{
|
|
Q_ASSERT (hrd_);
|
|
|
|
if (none == protocol_)
|
|
{
|
|
try
|
|
{
|
|
protocol_ = v5; // try this first (works for v6 too)
|
|
send_command ("get context", false, false);
|
|
}
|
|
catch (error const&)
|
|
{
|
|
protocol_ = none;
|
|
}
|
|
}
|
|
|
|
if (none == protocol_)
|
|
{
|
|
hrd_->close ();
|
|
|
|
protocol_ = v4; // try again with older protocol
|
|
hrd_->connectToHost (QHostAddress::LocalHost, 7809);
|
|
if (!hrd_->waitForConnected (socket_wait_time))
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::init_radio failed to connect:" << hrd_->errorString ();
|
|
#endif
|
|
|
|
throw error {"Failed to connect to Ham Radio Deluxe\n" + hrd_->errorString ().toLocal8Bit ()};
|
|
}
|
|
|
|
send_command ("get context", false, false);
|
|
}
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << send_command ("get id", false, false);
|
|
qDebug () << send_command ("get version", false, false);
|
|
#endif
|
|
|
|
auto radios = send_command ("get radios", false, false).trimmed ().split (',', QString::SkipEmptyParts);
|
|
if (radios.isEmpty ())
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::init_radio no rig found";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe: no rig found"};
|
|
}
|
|
|
|
Q_FOREACH (auto const& radio, radios)
|
|
{
|
|
auto entries = radio.trimmed ().split (':', QString::SkipEmptyParts);
|
|
radios_.push_back (std::forward_as_tuple (entries[0].toUInt (), entries[1]));
|
|
}
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "radios:";
|
|
Q_FOREACH (auto const& radio, radios_)
|
|
{
|
|
qDebug () << "\t[" << std::get<0> (radio) << "] " << std::get<1> (radio);
|
|
}
|
|
#endif
|
|
|
|
if (send_command ("get radio", false, false, true).isEmpty ())
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::init_radio no rig found";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe: no rig found"};
|
|
}
|
|
|
|
vfo_count_ = send_command ("get vfo-count").toUInt ();
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "vfo count:" << vfo_count_;
|
|
#endif
|
|
|
|
buttons_ = send_command ("get buttons").trimmed ().split (',', QString::SkipEmptyParts).replaceInStrings (" ", "~");
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRD Buttons: " << buttons_;
|
|
#endif
|
|
|
|
dropdown_names_ = send_command ("get dropdowns").trimmed ().split (',', QString::SkipEmptyParts);
|
|
Q_FOREACH (auto d, dropdown_names_)
|
|
{
|
|
dropdowns_[d] = send_command ("get dropdown-list {" + d + "}").trimmed ().split (',', QString::SkipEmptyParts);
|
|
}
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRD Dropdowns: " << dropdowns_;
|
|
#endif
|
|
|
|
vfo_A_button_ = find_button (QRegExp ("^(VFO~A|Main)$"));
|
|
vfo_B_button_ = find_button (QRegExp ("^(VFO~B|Sub)$"));
|
|
vfo_toggle_button_ = find_button (QRegExp ("^(A~/~B)$"));
|
|
Q_ASSERT (vfo_toggle_button_ >= 0 || (vfo_A_button_ >= 0 && vfo_B_button_ >=0));
|
|
|
|
split_mode_button_ = find_button (QRegExp ("^(Spl~On|Spl_On|Split)$"));
|
|
split_off_button_ = find_button (QRegExp ("^(Spl~Off|Spl_Off)$"));
|
|
|
|
if ((split_mode_dropdown_ = find_dropdown (QRegExp ("^(Split)$"))) >= 0)
|
|
{
|
|
split_mode_dropdown_selection_on_ = find_dropdown_selection (split_mode_dropdown_, QRegExp ("^(On)$"));
|
|
split_mode_dropdown_selection_off_ = find_dropdown_selection (split_mode_dropdown_, QRegExp ("^(Off)$"));
|
|
}
|
|
|
|
tx_A_button_ = find_button (QRegExp ("^(TX~main|TX~-~A)$"));
|
|
tx_B_button_ = find_button (QRegExp ("^(TX~sub|TX~-~B)$"));
|
|
|
|
Q_ASSERT (split_mode_button_ >= 0 || split_mode_dropdown_ >= 0 || (tx_A_button_ >= 0 && tx_B_button_ >= 0));
|
|
|
|
mode_A_dropdown_ = find_dropdown (QRegExp ("^(Main Mode|Mode)$"));
|
|
map_modes (mode_A_dropdown_, &mode_A_map_);
|
|
|
|
if ((mode_B_dropdown_ = find_dropdown (QRegExp ("^(Sub Mode)$"))) >= 0)
|
|
{
|
|
map_modes (mode_B_dropdown_, &mode_B_map_);
|
|
}
|
|
|
|
ptt_button_ = find_button (QRegExp ("^(TX)$"));
|
|
|
|
sync_impl ();
|
|
}
|
|
|
|
void HRDTransceiver::sync_impl ()
|
|
{
|
|
if (vfo_count_ == 1)
|
|
{
|
|
// put the rig into a known state
|
|
auto f = send_command ("get frequency").toUInt ();
|
|
auto m = lookup_mode (get_dropdown (mode_A_dropdown_), mode_A_map_);
|
|
set_button (vfo_B_button_ >= 0 ? vfo_B_button_ : vfo_toggle_button_);
|
|
auto fo = send_command ("get frequency").toUInt ();
|
|
update_other_frequency (fo);
|
|
auto mo = lookup_mode (get_dropdown (mode_A_dropdown_), mode_A_map_);
|
|
set_button (vfo_A_button_ >= 0 ? vfo_A_button_ : vfo_toggle_button_);
|
|
if (f != fo || m != mo)
|
|
{
|
|
// we must have started with A/MAIN
|
|
update_rx_frequency (f);
|
|
update_mode (m);
|
|
}
|
|
else
|
|
{
|
|
update_rx_frequency (send_command ("get frequency").toUInt ());
|
|
update_mode (lookup_mode (get_dropdown (mode_A_dropdown_), mode_A_map_));
|
|
}
|
|
}
|
|
|
|
poll ();
|
|
}
|
|
|
|
int HRDTransceiver::find_button (QRegExp const& re) const
|
|
{
|
|
return buttons_.indexOf (re);
|
|
}
|
|
|
|
int HRDTransceiver::find_dropdown (QRegExp const& re) const
|
|
{
|
|
return dropdown_names_.indexOf (re);
|
|
}
|
|
|
|
int HRDTransceiver::find_dropdown_selection (int dropdown, QRegExp const& re) const
|
|
{
|
|
return dropdowns_.value (dropdown_names_.value (dropdown)).lastIndexOf (re); // backwards because more specialised modes tend to be later in list
|
|
}
|
|
|
|
int HRDTransceiver::lookup_dropdown_selection (int dropdown, QString const& selection) const
|
|
{
|
|
int index {dropdowns_.value (dropdown_names_.value (dropdown)).indexOf (selection)};
|
|
Q_ASSERT (-1 != index);
|
|
return index;
|
|
}
|
|
|
|
void HRDTransceiver::map_modes (int dropdown, ModeMap *map)
|
|
{
|
|
// order matters here (both in the map and in the regexps)
|
|
map->push_back (std::forward_as_tuple (CW, find_dropdown_selection (dropdown, QRegExp ("^(CW|CW\\(N\\))$"))));
|
|
map->push_back (std::forward_as_tuple (CW_R, find_dropdown_selection (dropdown, QRegExp ("^(CW-R|CW)$"))));
|
|
map->push_back (std::forward_as_tuple (LSB, find_dropdown_selection (dropdown, QRegExp ("^(LSB)$"))));
|
|
map->push_back (std::forward_as_tuple (USB, find_dropdown_selection (dropdown, QRegExp ("^(USB)$"))));
|
|
map->push_back (std::forward_as_tuple (DIG_U, find_dropdown_selection (dropdown, QRegExp ("^(DIG|PKT-U|USB)$"))));
|
|
map->push_back (std::forward_as_tuple (DIG_L, find_dropdown_selection (dropdown, QRegExp ("^(DIG|PKT-L|LSB)$"))));
|
|
map->push_back (std::forward_as_tuple (FSK, find_dropdown_selection (dropdown, QRegExp ("^(DIG|FSK|RTTY)$"))));
|
|
map->push_back (std::forward_as_tuple (FSK_R, find_dropdown_selection (dropdown, QRegExp ("^(DIG|FSK-R|RTTY-R|RTTY)$"))));
|
|
map->push_back (std::forward_as_tuple (AM, find_dropdown_selection (dropdown, QRegExp ("^(AM)$"))));
|
|
map->push_back (std::forward_as_tuple (FM, find_dropdown_selection (dropdown, QRegExp ("^(FM|FM\\(N\\)|WFM)$"))));
|
|
map->push_back (std::forward_as_tuple (DIG_FM, find_dropdown_selection (dropdown, QRegExp ("^(PKT-FM|PKT|FM)$"))));
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::map_modes: for dropdown" << dropdown_names_[dropdown];
|
|
|
|
std::for_each (map->begin (), map->end (), [this, dropdown] (ModeMap::value_type const& item)
|
|
{
|
|
auto rhs = std::get<1> (item);
|
|
qDebug () << '\t' << std::get<0> (item) << "<->" << (rhs >= 0 ? dropdowns_[dropdown_names_[dropdown]][rhs] : "None");
|
|
});
|
|
#endif
|
|
}
|
|
|
|
int HRDTransceiver::lookup_mode (MODE mode, ModeMap const& map) const
|
|
{
|
|
auto it = std::find_if (map.begin (), map.end (), [mode] (ModeMap::value_type const& item) {return std::get<0> (item) == mode;});
|
|
if (map.end () == it)
|
|
{
|
|
throw error {"Ham Radio Deluxe: rig doesn't support mode"};
|
|
}
|
|
return std::get<1> (*it);
|
|
}
|
|
|
|
auto HRDTransceiver::lookup_mode (int mode, ModeMap const& map) const -> MODE
|
|
{
|
|
auto it = std::find_if (map.begin (), map.end (), [mode] (ModeMap::value_type const& item) {return std::get<1> (item) == mode;});
|
|
if (map.end () == it)
|
|
{
|
|
throw error {"Ham Radio Deluxe: sent an unrecognised mode"};
|
|
}
|
|
return std::get<0> (*it);
|
|
}
|
|
|
|
int HRDTransceiver::get_dropdown (int dd, bool no_debug)
|
|
{
|
|
auto dd_name = dropdown_names_.value (dd);
|
|
auto reply = send_command ("get dropdown-text {" + dd_name + "}", no_debug);
|
|
auto colon_index = reply.indexOf (':');
|
|
|
|
if (colon_index < 0)
|
|
{
|
|
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::get_dropdown bad response";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe didn't respond as expected"};
|
|
}
|
|
|
|
Q_ASSERT (reply.left (colon_index).trimmed () == dd_name);
|
|
return lookup_dropdown_selection (dd, reply.mid (colon_index + 1).trimmed ());
|
|
}
|
|
|
|
void HRDTransceiver::set_dropdown (int dd, int value)
|
|
{
|
|
auto dd_name = dropdown_names_.value (dd);
|
|
if (value >= 0)
|
|
{
|
|
send_simple_command ("set dropdown " + dd_name.replace (' ', '~') + ' ' + dropdowns_.value (dd_name).value (value) + ' ' + QString::number (value));
|
|
}
|
|
else
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::set_dropdown item" << value << "not found in" << dd_name;
|
|
#endif
|
|
|
|
throw error {("Ham Radio Deluxe: item not found in " + dd_name + " dropdown list").toLocal8Bit ()};
|
|
}
|
|
}
|
|
|
|
void HRDTransceiver::do_ptt (bool on)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::do_ptt:" << on;
|
|
#endif
|
|
|
|
if (use_for_ptt_)
|
|
{
|
|
set_button (ptt_button_, on);
|
|
}
|
|
else
|
|
{
|
|
wrapped_->ptt (on);
|
|
}
|
|
update_PTT (on);
|
|
}
|
|
|
|
void HRDTransceiver::set_button (int button_index, bool checked)
|
|
{
|
|
if (button_index >= 0)
|
|
{
|
|
send_simple_command ("set button-select " + buttons_.value (button_index) + (checked ? " 1" : " 0"));
|
|
}
|
|
else
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::set_button invalid button";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe: button not available"};
|
|
}
|
|
}
|
|
|
|
void HRDTransceiver::do_frequency (Frequency f)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::do_frequency:" << f << "reversed:" << reversed_;
|
|
#endif
|
|
|
|
send_simple_command ("set frequency-hz " + QString::number (f));
|
|
update_rx_frequency (f);
|
|
}
|
|
|
|
void HRDTransceiver::do_tx_frequency (Frequency tx, bool rationalise_mode)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::do_tx_frequency:" << tx << "reversed:" << reversed_;
|
|
#endif
|
|
|
|
bool split {tx != 0};
|
|
|
|
if (vfo_count_ > 1)
|
|
{
|
|
reversed_ = is_button_checked (vfo_B_button_);
|
|
}
|
|
|
|
if (split)
|
|
{
|
|
auto fo_string = QString::number (tx);
|
|
if (reversed_)
|
|
{
|
|
Q_ASSERT (vfo_count_ > 1);
|
|
|
|
auto frequencies = send_command ("get frequencies").trimmed ().split ('-', QString::SkipEmptyParts);
|
|
send_simple_command ("set frequencies-hz " + fo_string + ' ' + QString::number (frequencies[1].toUInt ()));
|
|
}
|
|
else
|
|
{
|
|
if (vfo_count_ > 1)
|
|
{
|
|
auto frequencies = send_command ("get frequencies").trimmed ().split ('-', QString::SkipEmptyParts);
|
|
send_simple_command ("set frequencies-hz " + QString::number (frequencies[0].toUInt ()) + ' ' + fo_string);
|
|
}
|
|
else
|
|
{
|
|
// we rationalise the modes and VFOs here as well as the frequencies
|
|
set_button (vfo_B_button_ >= 0 ? vfo_B_button_ : vfo_toggle_button_);
|
|
send_simple_command ("set frequency-hz " + fo_string);
|
|
if (rationalise_mode)
|
|
{
|
|
set_dropdown (mode_B_dropdown_ >= 0 ? mode_B_dropdown_ : mode_A_dropdown_, lookup_mode (state ().mode (), mode_B_dropdown_ >= 0 ? mode_B_map_ : mode_A_map_));
|
|
}
|
|
set_button (vfo_A_button_ >= 0 ? vfo_A_button_ : vfo_toggle_button_);
|
|
}
|
|
}
|
|
if (rationalise_mode)
|
|
{
|
|
set_dropdown (mode_B_dropdown_ >= 0 ? mode_B_dropdown_ : mode_A_dropdown_, lookup_mode (state ().mode (), mode_B_dropdown_ >= 0 ? mode_B_map_ : mode_A_map_));
|
|
}
|
|
}
|
|
update_other_frequency (tx);
|
|
|
|
if (split_mode_button_ >= 0)
|
|
{
|
|
if (split_off_button_ >= 0 && !split)
|
|
{
|
|
set_button (split_off_button_);
|
|
}
|
|
else
|
|
{
|
|
set_button (split_mode_button_, split);
|
|
}
|
|
}
|
|
else if (split_mode_dropdown_ >= 0)
|
|
{
|
|
set_dropdown (split_mode_dropdown_, split ? split_mode_dropdown_selection_on_ : split_mode_dropdown_selection_off_);
|
|
}
|
|
else if (vfo_A_button_ >= 0 && vfo_B_button_ >= 0 && tx_A_button_ >= 0 && tx_B_button_ >= 0)
|
|
{
|
|
if (split)
|
|
{
|
|
if (reversed_ != is_button_checked (tx_A_button_))
|
|
{
|
|
set_button (reversed_ ? tx_A_button_ : tx_B_button_);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (reversed_ != is_button_checked (tx_B_button_))
|
|
{
|
|
set_button (reversed_ ? tx_B_button_ : tx_A_button_);
|
|
}
|
|
}
|
|
}
|
|
update_split (split);
|
|
}
|
|
|
|
void HRDTransceiver::do_mode (MODE mode, bool rationalise)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::do_mode:" << mode;
|
|
#endif
|
|
|
|
set_dropdown (mode_A_dropdown_, lookup_mode (mode, mode_A_map_));
|
|
|
|
if (rationalise && state ().split ()) // rationalise mode if split
|
|
{
|
|
if (mode_B_dropdown_ >= 0)
|
|
{
|
|
set_dropdown (mode_B_dropdown_, lookup_mode (mode, mode_B_map_));
|
|
}
|
|
else if (vfo_count_ < 2)
|
|
{
|
|
set_button (vfo_B_button_ >= 0 ? vfo_B_button_ : vfo_toggle_button_);
|
|
set_dropdown (mode_B_dropdown_ >= 0 ? mode_B_dropdown_ : mode_A_dropdown_, lookup_mode (mode, mode_B_dropdown_ >= 0 ? mode_B_map_ : mode_A_map_));
|
|
set_button (vfo_A_button_ >= 0 ? vfo_A_button_ : vfo_toggle_button_);
|
|
}
|
|
}
|
|
|
|
update_mode (mode);
|
|
}
|
|
|
|
bool HRDTransceiver::is_button_checked (int button_index, bool no_debug)
|
|
{
|
|
auto reply = send_command ("get button-select " + buttons_.value (button_index), no_debug);
|
|
if ("1" != reply && "0" != reply)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::is_button_checked bad response";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe didn't respond as expected"};
|
|
}
|
|
return "1" == reply;
|
|
}
|
|
|
|
void HRDTransceiver::poll ()
|
|
{
|
|
#if WSJT_TRACE_CAT && WSJT_TRACE_CAT_POLLS
|
|
bool quiet {false};
|
|
#else
|
|
bool quiet {true};
|
|
#endif
|
|
|
|
if (split_off_button_ >= 0)
|
|
{
|
|
// we are probably dealing with an Icom and have to guess SPLIT mode :(
|
|
}
|
|
else if (split_mode_button_ >= 0)
|
|
{
|
|
update_split (is_button_checked (split_mode_button_, quiet));
|
|
}
|
|
else if (split_mode_dropdown_ >= 0)
|
|
{
|
|
if (!split_mode_dropdown_write_only_)
|
|
{
|
|
try
|
|
{
|
|
update_split (get_dropdown (split_mode_dropdown_, quiet) == split_mode_dropdown_selection_on_);
|
|
}
|
|
catch (error const&)
|
|
{
|
|
// leave split alone as we can't query it - it should be
|
|
// correct so long as rig or HRD haven't been changed
|
|
split_mode_dropdown_write_only_ = true;
|
|
}
|
|
}
|
|
}
|
|
else if (vfo_A_button_ >= 0 && vfo_B_button_ >= 0 && tx_A_button_ >= 0 && tx_B_button_ >= 0)
|
|
{
|
|
auto vfo_A = is_button_checked (vfo_A_button_, quiet);
|
|
auto tx_A = is_button_checked (tx_A_button_, quiet);
|
|
|
|
update_split (vfo_A != tx_A);
|
|
reversed_ = !vfo_A;
|
|
}
|
|
|
|
if (vfo_count_ > 1)
|
|
{
|
|
auto frequencies = send_command ("get frequencies", quiet).trimmed ().split ('-', QString::SkipEmptyParts);
|
|
update_rx_frequency (frequencies[reversed_ ? 1 : 0].toUInt ());
|
|
update_other_frequency (frequencies[reversed_ ? 0 : 1].toUInt ());
|
|
}
|
|
else
|
|
{
|
|
update_rx_frequency (send_command ("get frequency", quiet).toUInt ());
|
|
}
|
|
|
|
update_mode (lookup_mode (get_dropdown (mode_A_dropdown_, quiet), mode_A_map_));
|
|
}
|
|
|
|
QString HRDTransceiver::send_command (QString const& cmd, bool no_debug, bool prepend_context, bool recurse)
|
|
{
|
|
Q_ASSERT (hrd_);
|
|
|
|
QString result;
|
|
|
|
if (current_radio_ && prepend_context && vfo_count_ < 2)
|
|
{
|
|
// TODO G4WJS: get rid of this or move to a thread required on some
|
|
// radios because commands don't get executed correctly (ICOM for
|
|
// example)
|
|
QThread::msleep (50);
|
|
}
|
|
|
|
if (QTcpSocket::ConnectedState != hrd_->state ())
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command connection failed:" << hrd_->errorString ();
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe connection failed\n" + hrd_->errorString ().toLocal8Bit ()};
|
|
}
|
|
|
|
if (!recurse && prepend_context)
|
|
{
|
|
auto radio_name = send_command ("get radio", no_debug, current_radio_, true);
|
|
auto radio_iter = std::find_if (radios_.begin (), radios_.end (), [this, &radio_name] (RadioMap::value_type const& radio)
|
|
{
|
|
return std::get<1> (radio) == radio_name;
|
|
});
|
|
if (radio_iter == radios_.end ())
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command rig disappeared or changed";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe: rig has disappeared or changed"};
|
|
}
|
|
|
|
if (0u == current_radio_ || std::get<0> (*radio_iter) != current_radio_)
|
|
{
|
|
current_radio_ = std::get<0> (*radio_iter);
|
|
}
|
|
}
|
|
|
|
auto context = '[' + QString::number (current_radio_) + "] ";
|
|
|
|
int bytes_to_send;
|
|
int bytes_sent;
|
|
if (v4 == protocol_)
|
|
{
|
|
auto message = ((prepend_context ? context + cmd : cmd) + "\r").toLocal8Bit ();
|
|
bytes_to_send = message.size ();
|
|
bytes_sent = hrd_->write (message.data (), bytes_to_send);
|
|
|
|
if (!no_debug)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command:" << message;
|
|
#endif
|
|
}
|
|
}
|
|
else
|
|
{
|
|
auto string = prepend_context ? context + cmd : cmd;
|
|
if (!no_debug)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command:" << string;
|
|
#endif
|
|
}
|
|
|
|
QScopedPointer<HRDMessage> message (new (string) HRDMessage);
|
|
bytes_to_send = message->size_;
|
|
bytes_sent = hrd_->write (reinterpret_cast<char *> (message.data ()), bytes_to_send);
|
|
}
|
|
|
|
if (bytes_sent < bytes_to_send
|
|
|| !hrd_->waitForBytesWritten (socket_wait_time)
|
|
|| QTcpSocket::ConnectedState != hrd_->state ())
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command failed" << hrd_->errorString ();
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe send command failed\n" + hrd_->errorString ().toLocal8Bit ()};
|
|
}
|
|
|
|
if (!hrd_->waitForReadyRead (socket_wait_time))
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command failed to reply" << hrd_->errorString ();
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe failed to reply to command\n" + hrd_->errorString ().toLocal8Bit ()};
|
|
}
|
|
QByteArray buffer (hrd_->readAll ());
|
|
if (!no_debug)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command: reply byte count:" << buffer.size ();
|
|
#endif
|
|
}
|
|
|
|
if (v4 == protocol_)
|
|
{
|
|
result = QString {buffer}.trimmed ();
|
|
}
|
|
else
|
|
{
|
|
HRDMessage const * reply (new (buffer) HRDMessage);
|
|
|
|
if (reply->magic_1_value_ != reply->magic_1_ && reply->magic_2_value_ != reply->magic_2_)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command invalid reply";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe sent an invalid reply to our command"};
|
|
}
|
|
|
|
result = QString {reply->payload_}; // this is not a memory leak (honest!)
|
|
}
|
|
|
|
if (!no_debug)
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_command: reply: " << result;
|
|
#endif
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void HRDTransceiver::send_simple_command (QString const& command, bool no_debug)
|
|
{
|
|
if ("OK" != send_command (command, no_debug))
|
|
{
|
|
#if WSJT_TRACE_CAT
|
|
qDebug () << "HRDTransceiver::send_simple_command unexpected response";
|
|
#endif
|
|
|
|
throw error {"Ham Radio Deluxe didn't respond as expected"};
|
|
}
|
|
}
|