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/HRDTransceiver.cpp | 83 +++++++++++++----------------------------- 1 file changed, 26 insertions(+), 57 deletions(-) (limited to 'Transceiver/HRDTransceiver.cpp') 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; -- cgit v1.2.3