From d83a420f0517d7fc47aeb288a6a0cf4891784fcc Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Mon, 22 Jun 2026 12:03:44 +0200 Subject: New upstream version 3.0.2+dfsg --- Transceiver/EmulateSplitTransceiver.cpp | 10 ++- Transceiver/HRDMessage.cpp | 132 ++++++++++++++++++++++++++++++++ Transceiver/HRDMessage.hpp | 25 ++++++ Transceiver/HRDTransceiver.cpp | 83 +++++++------------- Transceiver/HRDTransceiver.hpp | 1 - Transceiver/OmniRigTransceiver.cpp | 12 ++- Transceiver/TCITransceiver.cpp | 10 ++- 7 files changed, 206 insertions(+), 67 deletions(-) create mode 100644 Transceiver/HRDMessage.cpp create mode 100644 Transceiver/HRDMessage.hpp (limited to 'Transceiver') diff --git a/Transceiver/EmulateSplitTransceiver.cpp b/Transceiver/EmulateSplitTransceiver.cpp index 3dfc4b5c..e7971fef 100644 --- a/Transceiver/EmulateSplitTransceiver.cpp +++ b/Transceiver/EmulateSplitTransceiver.cpp @@ -54,8 +54,14 @@ void EmulateSplitTransceiver::handle_update (TransceiverState const& state, else { TransceiverState new_state {state}; - // Follow the rig if in RX mode. - if (state.ptt ()) new_state.frequency (rx_frequency_); + // Fake split QSYs the rig to the TX dial before the PTT echo may + // arrive. Keep that transient from being adopted as the RX dial. + if (state.ptt () || (split_ && tx_frequency_ + && state.frequency () == tx_frequency_ + && state.frequency () != rx_frequency_)) + { + new_state.frequency (rx_frequency_); + } // These are always what was requested in prior set state operation new_state.tx_frequency (tx_frequency_); diff --git a/Transceiver/HRDMessage.cpp b/Transceiver/HRDMessage.cpp new file mode 100644 index 00000000..c1aabf50 --- /dev/null +++ b/Transceiver/HRDMessage.cpp @@ -0,0 +1,132 @@ +#include "Transceiver/HRDMessage.hpp" + +#include + +namespace +{ + int constexpr header_size_value = 16; + int constexpr max_message_size_value = 1024 * 1024; + quint32 constexpr magic_1_value = 0x1234ABCD; + quint32 constexpr magic_2_value = 0xABCD1234; + + void append_u32_le (QByteArray * bytes, quint32 value) + { + bytes->append (static_cast (value & 0xffu)); + bytes->append (static_cast ((value >> 8) & 0xffu)); + bytes->append (static_cast ((value >> 16) & 0xffu)); + bytes->append (static_cast ((value >> 24) & 0xffu)); + } + + quint32 read_u32_le (QByteArray const& bytes, int offset) + { + return static_cast (static_cast (bytes[offset])) + | (static_cast (static_cast (bytes[offset + 1])) << 8) + | (static_cast (static_cast (bytes[offset + 2])) << 16) + | (static_cast (static_cast (bytes[offset + 3])) << 24); + } + + HRDMessage::ParseResult valid_partial (int required_size) + { + return {true, false, required_size, {}, {}}; + } + + HRDMessage::ParseResult invalid (QString const& error) + { + return {false, false, 0, {}, error}; + } +} + +namespace HRDMessage +{ + int header_size () + { + return header_size_value; + } + + int max_message_size () + { + return max_message_size_value; + } + + QByteArray make (QString const& payload) + { + QByteArray message; + auto const payload_units = payload.size () + 1; + auto const payload_bytes = payload_units * 2; + auto const message_size = header_size_value + payload_bytes; + + append_u32_le (&message, static_cast (message_size)); + append_u32_le (&message, magic_1_value); + append_u32_le (&message, magic_2_value); + append_u32_le (&message, 0u); + + auto const utf16 = payload.utf16 (); + for (int i = 0; i != payload_units; ++i) + { + auto const unit = static_cast (utf16[i]); + message.append (static_cast (unit & 0xffu)); + message.append (static_cast ((unit >> 8) & 0xffu)); + } + + return message; + } + + ParseResult parse (QByteArray const& message) + { + if (message.size () < header_size_value) + { + return valid_partial (header_size_value); + } + + auto const message_size = read_u32_le (message, 0); + auto const magic_1 = read_u32_le (message, 4); + auto const magic_2 = read_u32_le (message, 8); + + if (magic_1 != magic_1_value || magic_2 != magic_2_value) + { + return invalid ("invalid magic values"); + } + + if (message_size < static_cast (header_size_value)) + { + return invalid ("message size is smaller than the header"); + } + + if (message_size > static_cast (max_message_size_value)) + { + return invalid ("message size is too large"); + } + + if (message.size () < static_cast (message_size)) + { + return valid_partial (static_cast (message_size)); + } + + auto const payload_bytes = static_cast (message_size) - header_size_value; + if (payload_bytes < 2) + { + return invalid ("payload is missing the terminator"); + } + + if (payload_bytes % 2) + { + return invalid ("payload is not valid UTF-16"); + } + + QVector payload; + payload.reserve (payload_bytes / 2); + for (int offset = header_size_value; offset != static_cast (message_size); offset += 2) + { + payload.append (static_cast ( + static_cast (static_cast (message[offset])) + | (static_cast (static_cast (message[offset + 1])) << 8))); + } + + if (payload.isEmpty () || payload.back () != 0u) + { + return invalid ("payload is missing the terminator"); + } + + return {true, true, static_cast (message_size), QString::fromUtf16 (payload.constData (), payload.size () - 1), {}}; + } +} diff --git a/Transceiver/HRDMessage.hpp b/Transceiver/HRDMessage.hpp new file mode 100644 index 00000000..61cc8d4a --- /dev/null +++ b/Transceiver/HRDMessage.hpp @@ -0,0 +1,25 @@ +#ifndef HRD_MESSAGE_HPP__ +#define HRD_MESSAGE_HPP__ + +#include +#include + +namespace HRDMessage +{ + int header_size (); + int max_message_size (); + + struct ParseResult + { + bool valid; + bool complete; + int required_size; + QString payload; + QString error; + }; + + QByteArray make (QString const& payload); + ParseResult parse (QByteArray const& message); +} + +#endif diff --git a/Transceiver/HRDTransceiver.cpp b/Transceiver/HRDTransceiver.cpp index a0cef0ce..eb719842 100644 --- a/Transceiver/HRDTransceiver.cpp +++ b/Transceiver/HRDTransceiver.cpp @@ -10,6 +10,7 @@ #include #include "Network/NetworkServerLookup.hpp" +#include "Transceiver/HRDMessage.hpp" #include "qt_helpers.hpp" namespace @@ -30,48 +31,6 @@ void HRDTransceiver::register_transceivers (logger_type *, (*registry)[HRD_transceiver_name] = TransceiverFactory::Capabilities (id, TransceiverFactory::Capabilities::network, true, true /* maybe */); } -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 (new char[size])); - storage->size_ = size ; - ushort const * pl (payload.utf16 ()); - std::copy (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 (reinterpret_cast (message.data ())); - } - - void operator delete (void * p, size_t) - { - delete [] reinterpret_cast (p); // Mirror allocation in operator new above. - } - - quint32 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 constexpr magic_1_value_ = 0x1234ABCD; - static qint32 constexpr magic_2_value_ = 0xABCD1234; -}; - HRDTransceiver::HRDTransceiver (logger_type * logger , std::unique_ptr wrapped , QString const& server @@ -1063,8 +1022,8 @@ QString HRDTransceiver::send_command (QString const& cmd, bool prepend_context, else { auto string = prepend_context ? context + cmd : cmd; - QScopedPointer message {new (string) HRDMessage}; - if (!write_to_port (reinterpret_cast (message.data ()), message->size_)) + auto message = HRDMessage::make (string); + if (!write_to_port (message.constData (), message.size ())) { CAT_ERROR ("failed to write command" << cmd << "to HRD"); throw error { @@ -1080,25 +1039,35 @@ QString HRDTransceiver::send_command (QString const& cmd, bool prepend_context, } else { - HRDMessage const * reply {new (buffer) HRDMessage}; - if (reply->magic_1_value_ != reply->magic_1_ && reply->magic_2_value_ != reply->magic_2_) + auto reply = HRDMessage::parse (buffer); + while (reply.valid && !reply.complete) { - CAT_ERROR (cmd << "invalid reply"); - throw error { - tr ("Ham Radio Deluxe sent an invalid reply to our command \"%1\"") - .arg (cmd) - }; + CAT_TRACE (cmd << "reading more reply data"); + buffer += read_reply (cmd); + if (buffer.size () >= reply.required_size) + { + reply = HRDMessage::parse (buffer); + } } - // keep reading until expected size arrives - while (buffer.size () - offsetof (HRDMessage, size_) < reply->size_) + if (!reply.valid) { - CAT_TRACE (cmd << "reading more reply data"); - buffer += read_reply (cmd); - reply = new (buffer) HRDMessage; + CAT_ERROR (cmd << "invalid reply:" << reply.error); + throw error { + tr ("Ham Radio Deluxe sent an invalid reply to our command \"%1\": %2") + .arg (cmd) + .arg (reply.error) + }; } - result = QString {reply->payload_}; // this is not a memory leak (honest!) + // HRD frames replies with an in-band NUL (U+0000) as a value separator + // and terminator. The byte-level parser returns the full body minus only + // the final terminator, preserving embedded/trailing NULs; the pre-#66 + // QString{const QChar*} path stopped at the first NUL. Downstream name, + // list and value parsing was written against that first-NUL contract, so + // restore it here (radio name, vfo-count, dropdown-list, slider-range). + auto const nul = reply.payload.indexOf (QChar {0}); + result = nul < 0 ? reply.payload : reply.payload.left (nul); } CAT_TRACE (cmd << " ->" << result); return result; diff --git a/Transceiver/HRDTransceiver.hpp b/Transceiver/HRDTransceiver.hpp index f3db8bce..94066a29 100644 --- a/Transceiver/HRDTransceiver.hpp +++ b/Transceiver/HRDTransceiver.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include diff --git a/Transceiver/OmniRigTransceiver.cpp b/Transceiver/OmniRigTransceiver.cpp index 67f4b178..467d71ab 100644 --- a/Transceiver/OmniRigTransceiver.cpp +++ b/Transceiver/OmniRigTransceiver.cpp @@ -291,15 +291,21 @@ int OmniRigTransceiver::do_start () if (OmniRig::PM_FREQ & writable_params_) { - rig_->SetFreq (f); + QTimer::singleShot (200, [=] { + rig_->SetFreq (f); + }); } else if (reversed_ && (OmniRig::PM_FREQB & writable_params_)) { - rig_->SetFreqB (f); + QTimer::singleShot (200, [=] { + rig_->SetFreqB (f); + }); } else if (!reversed_ && (OmniRig::PM_FREQA & writable_params_)) { - rig_->SetFreqA (f); + QTimer::singleShot (200, [=] { + rig_->SetFreqA (f); + }); } update_rx_frequency (f); CAT_TRACE ("started"); diff --git a/Transceiver/TCITransceiver.cpp b/Transceiver/TCITransceiver.cpp index f3a8001b..86d8e76e 100644 --- a/Transceiver/TCITransceiver.cpp +++ b/Transceiver/TCITransceiver.cpp @@ -711,7 +711,10 @@ void TCITransceiver::onMessageReceived(const QString &str) if (args.at(1) == "0" ) mode_ = args.at(2).toLower(); else mode_ = args.at(1).toLower(); } else mode_ = args.at(1); if (started_mode_.isEmpty()) started_mode_ = mode_; - if (busy_mode_) return; // was tci_done1(); + if (busy_mode_) { + if (requested_mode_.isEmpty() || requested_mode_ == mode_) tci_done8(); + return; + } else if (!requested_mode_.isEmpty() && requested_mode_ != mode_ && !band_change) { sendTextMessage(mode_to_command(requested_mode_)); } @@ -1000,8 +1003,7 @@ void TCITransceiver::rig_split () if (busy_split_) return; if (tci_timer5_->isActive()) mysleep5(0); busy_split_ = true; - const QString cmd = CmdSplitEnable + SmDP + rx_ + SmCM + "false" + SmTZ; // changed from below so split always false - //const QString cmd = CmdSplitEnable + SmDP + rx_ + SmCM + (requested_split_ ? "true" : "false") + SmTZ; + const QString cmd = CmdSplitEnable + SmDP + rx_ + SmCM + (requested_split_ ? "true" : "false") + SmTZ; sendTextMessage(cmd); mysleep5(500); busy_split_ = false; @@ -1153,7 +1155,7 @@ void TCITransceiver::do_frequency (Frequency f, MODE m, bool no_ignore) if (!requested_mode_.isEmpty() && requested_mode_ != mode_ && !busy_mode_) { busy_mode_ = true; sendTextMessage(mode_to_command(requested_mode_)); - mysleep7(1000); + mysleep8(1000); if (requested_mode_.isEmpty() || requested_mode_ == mode_) update_mode (m); else { printf("%s TCI failed set mode %s->%s",QDateTime::QDateTime::currentDateTimeUtc().toString("hh:mm:ss.zzz").toStdString().c_str(),mode_.toStdString().c_str(),requested_mode_.toStdString().c_str()); -- cgit v1.2.3