diff options
Diffstat (limited to 'Network')
| -rw-r--r-- | Network/LotWUsers.cpp | 297 | ||||
| -rw-r--r-- | Network/LotWUsers.hpp | 41 | ||||
| -rw-r--r-- | Network/MessageClient.cpp | 596 | ||||
| -rw-r--r-- | Network/MessageClient.hpp | 140 | ||||
| -rw-r--r-- | Network/NetworkAccessManager.hpp | 76 | ||||
| -rw-r--r-- | Network/NetworkMessage.cpp | 136 | ||||
| -rw-r--r-- | Network/NetworkMessage.hpp | 582 | ||||
| -rw-r--r-- | Network/NetworkServerLookup.cpp | 85 | ||||
| -rw-r--r-- | Network/NetworkServerLookup.hpp | 38 | ||||
| -rw-r--r-- | Network/psk_reporter.cpp | 133 | ||||
| -rw-r--r-- | Network/psk_reporter.h | 54 | ||||
| -rw-r--r-- | Network/wsprnet.cpp | 230 | ||||
| -rw-r--r-- | Network/wsprnet.h | 46 |
13 files changed, 2454 insertions, 0 deletions
diff --git a/Network/LotWUsers.cpp b/Network/LotWUsers.cpp new file mode 100644 index 00000000..fd1b5746 --- /dev/null +++ b/Network/LotWUsers.cpp @@ -0,0 +1,297 @@ +#include "LotWUsers.hpp" + +#include <future> +#include <chrono> + +#include <QHash> +#include <QString> +#include <QDate> +#include <QFile> +#include <QTextStream> +#include <QDir> +#include <QFileInfo> +#include <QPointer> +#include <QSaveFile> +#include <QUrl> +#include <QNetworkAccessManager> +#include <QNetworkReply> +#include <QDebug> + +#include "pimpl_impl.hpp" + +#include "moc_LotWUsers.cpp" + +namespace +{ + // Dictionary mapping call sign to date of last upload to LotW + using dictionary = QHash<QString, QDate>; +} + +class LotWUsers::impl final + : public QObject +{ + Q_OBJECT + +public: + impl (LotWUsers * self, QNetworkAccessManager * network_manager) + : self_ {self} + , network_manager_ {network_manager} + , url_valid_ {false} + , redirect_count_ {0} + , age_constraint_ {365} + { + } + + void load (QString const& url, bool fetch, bool forced_fetch) + { + abort (); // abort any active download + auto csv_file_name = csv_file_.fileName (); + auto exists = QFileInfo::exists (csv_file_name); + if (fetch && (!exists || forced_fetch)) + { + current_url_.setUrl (url); + if (current_url_.isValid () && !QSslSocket::supportsSsl ()) + { + current_url_.setScheme ("http"); + } + redirect_count_ = 0; + download (current_url_); + } + else + { + if (exists) + { + // load the database asynchronously + future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_name); + } + } + } + + void download (QUrl url) + { + if (QNetworkAccessManager::Accessible != network_manager_->networkAccessible ()) + { + // try and recover network access for QNAM + network_manager_->setNetworkAccessible (QNetworkAccessManager::Accessible); + } + + QNetworkRequest request {url}; + request.setRawHeader ("User-Agent", "WSJT LotW User Downloader"); + request.setOriginatingObject (this); + + // this blocks for a second or two the first time it is used on + // Windows - annoying + if (!url_valid_) + { + reply_ = network_manager_->head (request); + } + else + { + reply_ = network_manager_->get (request); + } + + connect (reply_.data (), &QNetworkReply::finished, this, &LotWUsers::impl::reply_finished); + connect (reply_.data (), &QNetworkReply::readyRead, this, &LotWUsers::impl::store); + } + + void reply_finished () + { + if (!reply_) + { + Q_EMIT self_->load_finished (); + return; // we probably deleted it in an earlier call + } + QUrl redirect_url {reply_->attribute (QNetworkRequest::RedirectionTargetAttribute).toUrl ()}; + if (reply_->error () == QNetworkReply::NoError && !redirect_url.isEmpty ()) + { + if ("https" == redirect_url.scheme () && !QSslSocket::supportsSsl ()) + { + Q_EMIT self_->LotW_users_error (tr ("Network Error - SSL/TLS support not installed, cannot fetch:\n\'%1\'") + .arg (redirect_url.toDisplayString ())); + url_valid_ = false; // reset + Q_EMIT self_->load_finished (); + } + else if (++redirect_count_ < 10) // maintain sanity + { + // follow redirect + download (reply_->url ().resolved (redirect_url)); + } + else + { + Q_EMIT self_->LotW_users_error (tr ("Network Error - Too many redirects:\n\'%1\'") + .arg (redirect_url.toDisplayString ())); + url_valid_ = false; // reset + Q_EMIT self_->load_finished (); + } + } + else if (reply_->error () != QNetworkReply::NoError) + { + csv_file_.cancelWriting (); + csv_file_.commit (); + url_valid_ = false; // reset + // report errors that are not due to abort + if (QNetworkReply::OperationCanceledError != reply_->error ()) + { + Q_EMIT self_->LotW_users_error (tr ("Network Error:\n%1") + .arg (reply_->errorString ())); + } + Q_EMIT self_->load_finished (); + } + else + { + if (url_valid_ && !csv_file_.commit ()) + { + Q_EMIT self_->LotW_users_error (tr ("File System Error - Cannot commit changes to:\n\"%1\"") + .arg (csv_file_.fileName ())); + url_valid_ = false; // reset + Q_EMIT self_->load_finished (); + } + else + { + if (!url_valid_) + { + // now get the body content + url_valid_ = true; + download (reply_->url ().resolved (redirect_url)); + } + else + { + url_valid_ = false; // reset + // load the database asynchronously + future_load_ = std::async (std::launch::async, &LotWUsers::impl::load_dictionary, this, csv_file_.fileName ()); + } + } + } + if (reply_ && reply_->isFinished ()) + { + reply_->deleteLater (); + } + } + + void store () + { + if (url_valid_) + { + if (!csv_file_.isOpen ()) + { + // create temporary file in the final location + if (!csv_file_.open (QSaveFile::WriteOnly)) + { + abort (); + Q_EMIT self_->LotW_users_error (tr ("File System Error - Cannot open file:\n\"%1\"\nError(%2): %3") + .arg (csv_file_.fileName ()) + .arg (csv_file_.error ()) + .arg (csv_file_.errorString ())); + } + } + if (csv_file_.write (reply_->read (reply_->bytesAvailable ())) < 0) + { + abort (); + Q_EMIT self_->LotW_users_error (tr ("File System Error - Cannot write to file:\n\"%1\"\nError(%2): %3") + .arg (csv_file_.fileName ()) + .arg (csv_file_.error ()) + .arg (csv_file_.errorString ())); + } + } + } + + void abort () + { + if (reply_ && reply_->isRunning ()) + { + reply_->abort (); + } + } + + // Load the database from the given file name + // + // Expects the file to be in CSV format with no header with one + // record per line. Record fields are call sign followed by upload + // date in yyyy-MM-dd format followed by upload time (ignored) + dictionary load_dictionary (QString const& lotw_csv_file) + { + dictionary result; + QFile f {lotw_csv_file}; + if (f.open (QFile::ReadOnly | QFile::Text)) + { + QTextStream s {&f}; + for (auto l = s.readLine (); !l.isNull (); l = s.readLine ()) + { + auto pos = l.indexOf (','); + result[l.left (pos)] = QDate::fromString (l.mid (pos + 1, l.indexOf (',', pos + 1) - pos - 1), "yyyy-MM-dd"); + } +// qDebug () << "LotW User Data Loaded"; + } + else + { + throw std::runtime_error {QObject::tr ("Failed to open LotW users CSV file: '%1'").arg (f.fileName ()).toStdString ()}; + } + return result; + } + + LotWUsers * self_; + QNetworkAccessManager * network_manager_; + QSaveFile csv_file_; + bool url_valid_; + QUrl current_url_; // may be a redirect + int redirect_count_; + QPointer<QNetworkReply> reply_; + std::future<dictionary> future_load_; + dictionary last_uploaded_; + qint64 age_constraint_; // days +}; + +#include "LotWUsers.moc" + +LotWUsers::LotWUsers (QNetworkAccessManager * network_manager, QObject * parent) + : QObject {parent} + , m_ {this, network_manager} +{ +} + +LotWUsers::~LotWUsers () +{ +} + +void LotWUsers::set_local_file_path (QString const& path) +{ + m_->csv_file_.setFileName (path); +} + +void LotWUsers::load (QString const& url, bool fetch, bool force_download) +{ + m_->load (url, fetch, force_download); +} + +void LotWUsers::set_age_constraint (qint64 uploaded_since_days) +{ + m_->age_constraint_ = uploaded_since_days; +} + +bool LotWUsers::user (QString const& call) const +{ + // check if a pending asynchronous load is ready + if (m_->future_load_.valid () + && std::future_status::ready == m_->future_load_.wait_for (std::chrono::seconds {0})) + { + try + { + // wait for the load to finish if necessary + const_cast<dictionary&> (m_->last_uploaded_) = const_cast<std::future<dictionary>&> (m_->future_load_).get (); + } + catch (std::exception const& e) + { + Q_EMIT LotW_users_error (e.what ()); + } + Q_EMIT load_finished (); + } + if (m_->last_uploaded_.size ()) + { + auto p = m_->last_uploaded_.constFind (call); + if (p != m_->last_uploaded_.end ()) + { + return p.value ().daysTo (QDate::currentDate ()) <= m_->age_constraint_; + } + } + return false; +} diff --git a/Network/LotWUsers.hpp b/Network/LotWUsers.hpp new file mode 100644 index 00000000..238c5740 --- /dev/null +++ b/Network/LotWUsers.hpp @@ -0,0 +1,41 @@ +#ifndef LOTW_USERS_HPP_ +#define LOTW_USERS_HPP_ + +#include <boost/core/noncopyable.hpp> +#include <QObject> +#include "pimpl_h.hpp" + +class QString; +class QDate; +class QNetworkAccessManager; + +// +// LotWUsers - Lookup Logbook of the World users +// +class LotWUsers final + : public QObject +{ + Q_OBJECT + +public: + explicit LotWUsers (QNetworkAccessManager *, QObject * parent = 0); + ~LotWUsers (); + + void set_local_file_path (QString const&); + + Q_SLOT void load (QString const& url, bool fetch = true, bool force_download = false); + Q_SLOT void set_age_constraint (qint64 uploaded_since_days); + + // returns true if the specified call sign 'call' has uploaded their + // log to LotW in the last 'age_constraint_days' days + bool user (QString const& call) const; + + Q_SIGNAL void LotW_users_error (QString const& reason) const; + Q_SIGNAL void load_finished () const; + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/Network/MessageClient.cpp b/Network/MessageClient.cpp new file mode 100644 index 00000000..ddf26956 --- /dev/null +++ b/Network/MessageClient.cpp @@ -0,0 +1,596 @@ +#include "MessageClient.hpp" + +#include <stdexcept> +#include <vector> +#include <algorithm> +#include <limits> + +#include <QUdpSocket> +#include <QHostInfo> +#include <QTimer> +#include <QQueue> +#include <QByteArray> +#include <QHostAddress> +#include <QColor> +#include <QDebug> + +#include "NetworkMessage.hpp" + +#include "pimpl_impl.hpp" + +#include "moc_MessageClient.cpp" + +// some trace macros +#if WSJT_TRACE_UDP +#define TRACE_UDP(MSG) qDebug () << QString {"MessageClient::%1:"}.arg (__func__) << MSG +#else +#define TRACE_UDP(MSG) +#endif + +class MessageClient::impl + : public QUdpSocket +{ + Q_OBJECT; + +public: + impl (QString const& id, QString const& version, QString const& revision, + port_type server_port, MessageClient * self) + : self_ {self} + , enabled_ {false} + , id_ {id} + , version_ {version} + , revision_ {revision} + , server_port_ {server_port} + , schema_ {2} // use 2 prior to negotiation not 1 which is broken + , heartbeat_timer_ {new QTimer {this}} + { + connect (heartbeat_timer_, &QTimer::timeout, this, &impl::heartbeat); + connect (this, &QIODevice::readyRead, this, &impl::pending_datagrams); + + heartbeat_timer_->start (NetworkMessage::pulse * 1000); + + // bind to an ephemeral port + bind (); + } + + ~impl () + { + closedown (); + } + + enum StreamStatus {Fail, Short, OK}; + + void parse_message (QByteArray const& msg); + void pending_datagrams (); + void heartbeat (); + void closedown (); + StreamStatus check_status (QDataStream const&) const; + void send_message (QByteArray const&); + void send_message (QDataStream const& out, QByteArray const& message) + { + if (OK == check_status (out)) + { + send_message (message); + } + else + { + Q_EMIT self_->error ("Error creating UDP message"); + } + } + + Q_SLOT void host_info_results (QHostInfo); + + MessageClient * self_; + bool enabled_; + QString id_; + QString version_; + QString revision_; + QString server_string_; + port_type server_port_; + QHostAddress server_; + quint32 schema_; + QTimer * heartbeat_timer_; + std::vector<QHostAddress> blocked_addresses_; + + // hold messages sent before host lookup completes asynchronously + QQueue<QByteArray> pending_messages_; + QByteArray last_message_; +}; + +#include "MessageClient.moc" + +void MessageClient::impl::host_info_results (QHostInfo host_info) +{ + if (QHostInfo::NoError != host_info.error ()) + { + Q_EMIT self_->error ("UDP server lookup failed:\n" + host_info.errorString ()); + pending_messages_.clear (); // discard + } + else if (host_info.addresses ().size ()) + { + auto server = host_info.addresses ()[0]; + if (blocked_addresses_.end () == std::find (blocked_addresses_.begin (), blocked_addresses_.end (), server)) + { + server_ = server; + TRACE_UDP ("resulting server:" << server); + + // send initial heartbeat which allows schema negotiation + heartbeat (); + + // clear any backlog + while (pending_messages_.size ()) + { + send_message (pending_messages_.dequeue ()); + } + } + else + { + Q_EMIT self_->error ("UDP server blocked, please try another"); + pending_messages_.clear (); // discard + } + } +} + +void MessageClient::impl::pending_datagrams () +{ + while (hasPendingDatagrams ()) + { + QByteArray datagram; + datagram.resize (pendingDatagramSize ()); + QHostAddress sender_address; + port_type sender_port; + if (0 <= readDatagram (datagram.data (), datagram.size (), &sender_address, &sender_port)) + { + TRACE_UDP ("message received from:" << sender_address << "port:" << sender_port); + parse_message (datagram); + } + } +} + +void MessageClient::impl::parse_message (QByteArray const& msg) +{ + try + { + // + // message format is described in NetworkMessage.hpp + // + NetworkMessage::Reader in {msg}; + if (OK == check_status (in)) + { + if (schema_ < in.schema ()) // one time record of server's + // negotiated schema + { + schema_ = in.schema (); + } + + if (!enabled_) + { + TRACE_UDP ("message processing disabled for id:" << in.id ()); + return; + } + + // + // message format is described in NetworkMessage.hpp + // + switch (in.type ()) + { + case NetworkMessage::Reply: + { + // unpack message + QTime time; + qint32 snr; + float delta_time; + quint32 delta_frequency; + QByteArray mode; + QByteArray message; + bool low_confidence {false}; + quint8 modifiers {0}; + in >> time >> snr >> delta_time >> delta_frequency >> mode >> message + >> low_confidence >> modifiers; + TRACE_UDP ("Reply: time:" << time << "snr:" << snr << "dt:" << delta_time << "df:" << delta_frequency << "mode:" << mode << "message:" << message << "low confidence:" << low_confidence << "modifiers: 0x" << hex << modifiers); + if (check_status (in) != Fail) + { + Q_EMIT self_->reply (time, snr, delta_time, delta_frequency + , QString::fromUtf8 (mode), QString::fromUtf8 (message) + , low_confidence, modifiers); + } + } + break; + + case NetworkMessage::Clear: + { + quint8 window {0}; + in >> window; + TRACE_UDP ("Clear window:" << window); + if (check_status (in) != Fail) + { + Q_EMIT self_->clear_decodes (window); + } + } + break; + + case NetworkMessage::Close: + TRACE_UDP ("Close"); + if (check_status (in) != Fail) + { + last_message_.clear (); + Q_EMIT self_->close (); + } + break; + + case NetworkMessage::Replay: + TRACE_UDP ("Replay"); + if (check_status (in) != Fail) + { + last_message_.clear (); + Q_EMIT self_->replay (); + } + break; + + case NetworkMessage::HaltTx: + { + bool auto_only {false}; + in >> auto_only; + TRACE_UDP ("Halt Tx auto_only:" << auto_only); + if (check_status (in) != Fail) + { + Q_EMIT self_->halt_tx (auto_only); + } + } + break; + + case NetworkMessage::FreeText: + { + QByteArray message; + bool send {true}; + in >> message >> send; + TRACE_UDP ("FreeText message:" << message << "send:" << send); + if (check_status (in) != Fail) + { + Q_EMIT self_->free_text (QString::fromUtf8 (message), send); + } + } + break; + + case NetworkMessage::Location: + { + QByteArray location; + in >> location; + TRACE_UDP ("Location location:" << location); + if (check_status (in) != Fail) + { + Q_EMIT self_->location (QString::fromUtf8 (location)); + } + } + break; + + case NetworkMessage::HighlightCallsign: + { + QByteArray call; + QColor bg; // default invalid color + QColor fg; // default invalid color + bool last_only {false}; + in >> call >> bg >> fg >> last_only; + TRACE_UDP ("HighlightCallsign call:" << call << "bg:" << bg << "fg:" << fg << "last only:" << last_only); + if (check_status (in) != Fail && call.size ()) + { + Q_EMIT self_->highlight_callsign (QString::fromUtf8 (call), bg, fg, last_only); + } + } + break; + + case NetworkMessage::SwitchConfiguration: + { + QByteArray configuration_name; + in >> configuration_name; + TRACE_UDP ("Switch Configuration name:" << configuration_name); + if (check_status (in) != Fail) + { + Q_EMIT self_->switch_configuration (QString::fromUtf8 (configuration_name)); + } + } + break; + + case NetworkMessage::Configure: + { + QByteArray mode; + quint32 frequency_tolerance; + QByteArray submode; + bool fast_mode {false}; + quint32 tr_period {std::numeric_limits<quint32>::max ()}; + quint32 rx_df {std::numeric_limits<quint32>::max ()}; + QByteArray dx_call; + QByteArray dx_grid; + bool generate_messages {false}; + in >> mode >> frequency_tolerance >> submode >> fast_mode >> tr_period >> rx_df + >> dx_call >> dx_grid >> generate_messages; + TRACE_UDP ("Configure mode:" << mode << "frequency tolerance:" << frequency_tolerance << "submode:" << submode << "fast mode:" << fast_mode << "T/R period:" << tr_period << "rx df:" << rx_df << "dx call:" << dx_call << "dx grid:" << dx_grid << "generate messages:" << generate_messages); + if (check_status (in) != Fail) + { + Q_EMIT self_->configure (QString::fromUtf8 (mode), frequency_tolerance + , QString::fromUtf8 (submode), fast_mode, tr_period, rx_df + , QString::fromUtf8 (dx_call), QString::fromUtf8 (dx_grid) + , generate_messages); + } + } + break; + + default: + // Ignore + // + // Note that although server heartbeat messages are not + // parsed here they are still partially parsed in the + // message reader class to negotiate the maximum schema + // number being used on the network. + if (NetworkMessage::Heartbeat != in.type ()) + { + TRACE_UDP ("ignoring message type:" << in.type ()); + } + break; + } + } + else + { + TRACE_UDP ("ignored message for id:" << in.id ()); + } + } + catch (std::exception const& e) + { + Q_EMIT self_->error (QString {"MessageClient exception: %1"}.arg (e.what ())); + } + catch (...) + { + Q_EMIT self_->error ("Unexpected exception in MessageClient"); + } +} + +void MessageClient::impl::heartbeat () +{ + if (server_port_ && !server_.isNull ()) + { + QByteArray message; + NetworkMessage::Builder hb {&message, NetworkMessage::Heartbeat, id_, schema_}; + hb << NetworkMessage::Builder::schema_number // maximum schema number accepted + << version_.toUtf8 () << revision_.toUtf8 (); + if (OK == check_status (hb)) + { + TRACE_UDP ("schema:" << schema_ << "max schema:" << NetworkMessage::Builder::schema_number << "version:" << version_ << "revision:" << revision_); + writeDatagram (message, server_, server_port_); + } + } +} + +void MessageClient::impl::closedown () +{ + if (server_port_ && !server_.isNull ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::Close, id_, schema_}; + if (OK == check_status (out)) + { + TRACE_UDP (""); + writeDatagram (message, server_, server_port_); + } + } +} + +void MessageClient::impl::send_message (QByteArray const& message) +{ + if (server_port_) + { + if (!server_.isNull ()) + { + if (message != last_message_) // avoid duplicates + { + writeDatagram (message, server_, server_port_); + last_message_ = message; + } + } + else + { + pending_messages_.enqueue (message); + } + } +} + +auto MessageClient::impl::check_status (QDataStream const& stream) const -> StreamStatus +{ + auto stat = stream.status (); + StreamStatus result {Fail}; + switch (stat) + { + case QDataStream::ReadPastEnd: + result = Short; + break; + + case QDataStream::ReadCorruptData: + Q_EMIT self_->error ("Message serialization error: read corrupt data"); + break; + + case QDataStream::WriteFailed: + Q_EMIT self_->error ("Message serialization error: write error"); + break; + + default: + result = OK; + break; + } + return result; +} + +MessageClient::MessageClient (QString const& id, QString const& version, QString const& revision, + QString const& server, port_type server_port, QObject * self) + : QObject {self} + , m_ {id, version, revision, server_port, this} +{ + connect (&*m_, static_cast<void (impl::*) (impl::SocketError)> (&impl::error) + , [this] (impl::SocketError e) + { +#if defined (Q_OS_WIN) + if (e != impl::NetworkError // take this out when Qt 5.5 + // stops doing this + // spuriously + && e != impl::ConnectionRefusedError) // not + // interested + // in this with + // UDP socket +#else + Q_UNUSED (e); +#endif + { + Q_EMIT error (m_->errorString ()); + } + }); + set_server (server); +} + +QHostAddress MessageClient::server_address () const +{ + return m_->server_; +} + +auto MessageClient::server_port () const -> port_type +{ + return m_->server_port_; +} + +void MessageClient::set_server (QString const& server) +{ + m_->server_.clear (); + m_->server_string_ = server; + if (!server.isEmpty ()) + { + // queue a host address lookup + TRACE_UDP ("server host DNS lookup:" << server); + QHostInfo::lookupHost (server, &*m_, SLOT (host_info_results (QHostInfo))); + } +} + +void MessageClient::set_server_port (port_type server_port) +{ + m_->server_port_ = server_port; +} + +qint64 MessageClient::send_raw_datagram (QByteArray const& message, QHostAddress const& dest_address + , port_type dest_port) +{ + if (dest_port && !dest_address.isNull ()) + { + return m_->writeDatagram (message, dest_address, dest_port); + } + return 0; +} + +void MessageClient::add_blocked_destination (QHostAddress const& a) +{ + m_->blocked_addresses_.push_back (a); + if (a == m_->server_) + { + m_->server_.clear (); + Q_EMIT error ("UDP server blocked, please try another"); + m_->pending_messages_.clear (); // discard + } +} + +void MessageClient::enable (bool flag) +{ + m_->enabled_ = flag; +} + +void MessageClient::status_update (Frequency f, QString const& mode, QString const& dx_call + , QString const& report, QString const& tx_mode + , bool tx_enabled, bool transmitting, bool decoding + , quint32 rx_df, quint32 tx_df, QString const& de_call + , QString const& de_grid, QString const& dx_grid + , bool watchdog_timeout, QString const& sub_mode + , bool fast_mode, quint8 special_op_mode + , quint32 frequency_tolerance, quint32 tr_period + , QString const& configuration_name) +{ + if (m_->server_port_ && !m_->server_string_.isEmpty ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::Status, m_->id_, m_->schema_}; + out << f << mode.toUtf8 () << dx_call.toUtf8 () << report.toUtf8 () << tx_mode.toUtf8 () + << tx_enabled << transmitting << decoding << rx_df << tx_df << de_call.toUtf8 () + << de_grid.toUtf8 () << dx_grid.toUtf8 () << watchdog_timeout << sub_mode.toUtf8 () + << fast_mode << special_op_mode << frequency_tolerance << tr_period << configuration_name.toUtf8 (); + TRACE_UDP ("frequency:" << f << "mode:" << mode << "DX:" << dx_call << "report:" << report << "Tx mode:" << tx_mode << "tx_enabled:" << tx_enabled << "Tx:" << transmitting << "decoding:" << decoding << "Rx df:" << rx_df << "Tx df:" << tx_df << "DE:" << de_call << "DE grid:" << de_grid << "DX grid:" << dx_grid << "w/d t/o:" << watchdog_timeout << "sub_mode:" << sub_mode << "fast mode:" << fast_mode << "spec op mode:" << special_op_mode << "frequency tolerance:" << frequency_tolerance << "T/R period:" << tr_period << "configuration name:" << configuration_name); + m_->send_message (out, message); + } +} + +void MessageClient::decode (bool is_new, QTime time, qint32 snr, float delta_time, quint32 delta_frequency + , QString const& mode, QString const& message_text, bool low_confidence + , bool off_air) +{ + if (m_->server_port_ && !m_->server_string_.isEmpty ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::Decode, m_->id_, m_->schema_}; + out << is_new << time << snr << delta_time << delta_frequency << mode.toUtf8 () + << message_text.toUtf8 () << low_confidence << off_air; + TRACE_UDP ("new" << is_new << "time:" << time << "snr:" << snr << "dt:" << delta_time << "df:" << delta_frequency << "mode:" << mode << "text:" << message_text << "low conf:" << low_confidence << "off air:" << off_air); + m_->send_message (out, message); + } +} + +void MessageClient::WSPR_decode (bool is_new, QTime time, qint32 snr, float delta_time, Frequency frequency + , qint32 drift, QString const& callsign, QString const& grid, qint32 power + , bool off_air) +{ + if (m_->server_port_ && !m_->server_string_.isEmpty ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::WSPRDecode, m_->id_, m_->schema_}; + out << is_new << time << snr << delta_time << frequency << drift << callsign.toUtf8 () + << grid.toUtf8 () << power << off_air; + TRACE_UDP ("new:" << is_new << "time:" << time << "snr:" << snr << "dt:" << delta_time << "frequency:" << frequency << "drift:" << drift << "call:" << callsign << "grid:" << grid << "pwr:" << power << "off air:" << off_air); + m_->send_message (out, message); + } +} + +void MessageClient::decodes_cleared () +{ + if (m_->server_port_ && !m_->server_string_.isEmpty ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::Clear, m_->id_, m_->schema_}; + TRACE_UDP (""); + m_->send_message (out, message); + } +} + +void MessageClient::qso_logged (QDateTime time_off, QString const& dx_call, QString const& dx_grid + , Frequency dial_frequency, QString const& mode, QString const& report_sent + , QString const& report_received, QString const& tx_power + , QString const& comments, QString const& name, QDateTime time_on + , QString const& operator_call, QString const& my_call + , QString const& my_grid, QString const& exchange_sent + , QString const& exchange_rcvd) +{ + if (m_->server_port_ && !m_->server_string_.isEmpty ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::QSOLogged, m_->id_, m_->schema_}; + out << time_off << dx_call.toUtf8 () << dx_grid.toUtf8 () << dial_frequency << mode.toUtf8 () + << report_sent.toUtf8 () << report_received.toUtf8 () << tx_power.toUtf8 () << comments.toUtf8 () + << name.toUtf8 () << time_on << operator_call.toUtf8 () << my_call.toUtf8 () << my_grid.toUtf8 () + << exchange_sent.toUtf8 () << exchange_rcvd.toUtf8 (); + TRACE_UDP ("time off:" << time_off << "DX:" << dx_call << "DX grid:" << dx_grid << "dial:" << dial_frequency << "mode:" << mode << "sent:" << report_sent << "rcvd:" << report_received << "pwr:" << tx_power << "comments:" << comments << "name:" << name << "time on:" << time_on << "op:" << operator_call << "DE:" << my_call << "DE grid:" << my_grid << "exch sent:" << exchange_sent << "exch rcvd:" << exchange_rcvd); + m_->send_message (out, message); + } +} + +void MessageClient::logged_ADIF (QByteArray const& ADIF_record) +{ + if (m_->server_port_ && !m_->server_string_.isEmpty ()) + { + QByteArray message; + NetworkMessage::Builder out {&message, NetworkMessage::LoggedADIF, m_->id_, m_->schema_}; + QByteArray ADIF {"\n<adif_ver:5>3.1.0\n<programid:6>WSJT-X\n<EOH>\n" + ADIF_record + " <EOR>"}; + out << ADIF; + TRACE_UDP ("ADIF:" << ADIF); + m_->send_message (out, message); + } +} diff --git a/Network/MessageClient.hpp b/Network/MessageClient.hpp new file mode 100644 index 00000000..20f39b01 --- /dev/null +++ b/Network/MessageClient.hpp @@ -0,0 +1,140 @@ +#ifndef MESSAGE_CLIENT_HPP__ +#define MESSAGE_CLIENT_HPP__ + +#include <QObject> +#include <QTime> +#include <QDateTime> +#include <QString> + +#include "Radio.hpp" +#include "pimpl_h.hpp" + +class QByteArray; +class QHostAddress; +class QColor; + +// +// MessageClient - Manage messages sent and replies received from a +// matching server (MessageServer) at the other end of +// the wire +// +// +// Each outgoing message type is a Qt slot +// +class MessageClient + : public QObject +{ + Q_OBJECT; + +public: + using Frequency = Radio::Frequency; + using port_type = quint16; + + // instantiate and initiate a host lookup on the server + // + // messages will be silently dropped until a server host lookup is complete + MessageClient (QString const& id, QString const& version, QString const& revision, + QString const& server, port_type server_port, QObject * parent = nullptr); + + // query server details + QHostAddress server_address () const; + port_type server_port () const; + + // initiate a new server host lookup or is the server name is empty + // the sending of messages is disabled + Q_SLOT void set_server (QString const& server = QString {}); + + // change the server port messages are sent to + Q_SLOT void set_server_port (port_type server_port = 0u); + + // enable incoming messages + Q_SLOT void enable (bool); + + // outgoing messages + Q_SLOT void status_update (Frequency, QString const& mode, QString const& dx_call, QString const& report + , QString const& tx_mode, bool tx_enabled, bool transmitting, bool decoding + , quint32 rx_df, quint32 tx_df, QString const& de_call, QString const& de_grid + , QString const& dx_grid, bool watchdog_timeout, QString const& sub_mode + , bool fast_mode, quint8 special_op_mode, quint32 frequency_tolerance + , quint32 tr_period, QString const& configuration_name); + Q_SLOT void decode (bool is_new, QTime time, qint32 snr, float delta_time, quint32 delta_frequency + , QString const& mode, QString const& message, bool low_confidence + , bool off_air); + Q_SLOT void WSPR_decode (bool is_new, QTime time, qint32 snr, float delta_time, Frequency + , qint32 drift, QString const& callsign, QString const& grid, qint32 power + , bool off_air); + Q_SLOT void decodes_cleared (); + Q_SLOT void qso_logged (QDateTime time_off, QString const& dx_call, QString const& dx_grid + , Frequency dial_frequency, QString const& mode, QString const& report_sent + , QString const& report_received, QString const& tx_power, QString const& comments + , QString const& name, QDateTime time_on, QString const& operator_call + , QString const& my_call, QString const& my_grid + , QString const& exchange_sent, QString const& exchange_rcvd); + + // ADIF_record argument should be valid ADIF excluding any <EOR> end + // of record marker + Q_SLOT void logged_ADIF (QByteArray const& ADIF_record); + + // this may be used to send arbitrary UDP datagrams to and + // destination allowing the underlying socket to be used for general + // UDP messaging if desired + qint64 send_raw_datagram (QByteArray const&, QHostAddress const& dest_address, port_type dest_port); + + // disallowed message destination (does not block datagrams sent + // with send_raw_datagram() above) + Q_SLOT void add_blocked_destination (QHostAddress const&); + + // this signal is emitted if the server has requested a decode + // window clear action + Q_SIGNAL void clear_decodes (quint8 window); + + // this signal is emitted if the server sends us a reply, the only + // reply supported is reply to a prior CQ or QRZ message + Q_SIGNAL void reply (QTime, qint32 snr, float delta_time, quint32 delta_frequency, QString const& mode + , QString const& message_text, bool low_confidence, quint8 modifiers); + + // this signal is emitted if the server has requested this client to + // close down gracefully + Q_SIGNAL void close (); + + // this signal is emitted if the server has requested a replay of + // all decodes + Q_SIGNAL void replay (); + + // this signal is emitted if the server has requested immediate (or + // auto Tx if auto_only is true) transmission to halt + Q_SIGNAL void halt_tx (bool auto_only); + + // this signal is emitted if the server has requested a new free + // message text + Q_SIGNAL void free_text (QString const&, bool send); + + // this signal is emitted if the server has sent a highlight + // callsign request for the specified call + Q_SIGNAL void highlight_callsign (QString const& callsign, QColor const& bg, QColor const& fg, bool last_only); + + // this signal is emitted if the server has requested a + // configuration switch + Q_SIGNAL void switch_configuration (QString const& configuration_name); + + // this signal is emitted if the server has requested a + // configuration change + Q_SIGNAL void configure (QString const& mode, quint32 frequency_tolerance, QString const& submode + , bool fast_mode, quint32 tr_period, quint32 rx_df, QString const& dx_call + , QString const& dx_grid, bool generate_messages); + + // this signal is emitted when network errors occur or if a host + // lookup fails + Q_SIGNAL void error (QString const&) const; + + // this signal is emitted if the message obtains a location from a + // server. (It doesn't have to be new, could be a periodic location + // update) + Q_SIGNAL void location (QString const&); + +private: + class impl; + pimpl<impl> m_; +}; + +#endif diff --git a/Network/NetworkAccessManager.hpp b/Network/NetworkAccessManager.hpp new file mode 100644 index 00000000..1d1b79bc --- /dev/null +++ b/Network/NetworkAccessManager.hpp @@ -0,0 +1,76 @@ +#ifndef NETWORK_ACCESS_MANAGER_HPP__ +#define NETWORK_ACCESS_MANAGER_HPP__ + +#include <QNetworkAccessManager> +#include <QList> +#include <QSslError> +#include <QNetworkReply> +#include <QString> + +#include "widgets/MessageBox.hpp" + +class QNetworkRequest; +class QIODevice; +class QWidget; + +// sub-class QNAM to keep a list of accepted SSL errors and allow +// them in future replies +class NetworkAccessManager + : public QNetworkAccessManager +{ +public: + NetworkAccessManager (QWidget * parent) + : QNetworkAccessManager (parent) + { + // handle SSL errors that have not been cached as allowed + // exceptions and offer them to the user to add to the ignored + // exception cache + connect (this, &QNetworkAccessManager::sslErrors, [this, &parent] (QNetworkReply * reply, QList<QSslError> const& errors) { + QString message; + QList<QSslError> new_errors; + for (auto const& error: errors) + { + if (!allowed_ssl_errors_.contains (error)) + { + new_errors << error; + message += '\n' + reply->request ().url ().toDisplayString () + ": " + + error.errorString (); + } + } + if (new_errors.size ()) + { + QString certs; + for (auto const& cert : reply->sslConfiguration ().peerCertificateChain ()) + { + certs += cert.toText () + '\n'; + } + if (MessageBox::Ignore == MessageBox::query_message (parent, tr ("Network SSL Errors"), message, certs, MessageBox::Abort | MessageBox::Ignore)) + { + // accumulate new SSL error exceptions that have been allowed + allowed_ssl_errors_.append (new_errors); + reply->ignoreSslErrors (allowed_ssl_errors_); + } + } + else + { + // no new exceptions so silently ignore the ones already allowed + reply->ignoreSslErrors (allowed_ssl_errors_); + } + }); + } + +protected: + QNetworkReply * createRequest (Operation operation, QNetworkRequest const& request, QIODevice * outgoing_data = nullptr) override + { + auto reply = QNetworkAccessManager::createRequest (operation, request, outgoing_data); + // errors are usually certificate specific so passing all cached + // exceptions here is ok + reply->ignoreSslErrors (allowed_ssl_errors_); + return reply; + } + +private: + QList<QSslError> allowed_ssl_errors_; +}; + +#endif diff --git a/Network/NetworkMessage.cpp b/Network/NetworkMessage.cpp new file mode 100644 index 00000000..5af133c7 --- /dev/null +++ b/Network/NetworkMessage.cpp @@ -0,0 +1,136 @@ +#include "NetworkMessage.hpp" + +#include <exception> + +#include <QString> +#include <QByteArray> +#include <QDebug> + +#include "pimpl_impl.hpp" + +namespace NetworkMessage +{ + Builder::Builder (QIODevice * device, Type type, QString const& id, quint32 schema) + : QDataStream {device} + { + common_initialization (type, id, schema); + } + + Builder::Builder (QByteArray * a, Type type, QString const& id, quint32 schema) + : QDataStream {a, QIODevice::WriteOnly} + { + common_initialization (type, id, schema); + } + + void Builder::common_initialization (Type type, QString const& id, quint32 schema) + { + if (schema <= 1) + { + setVersion (QDataStream::Qt_5_0); // Qt schema version + } +#if QT_VERSION >= 0x050200 + else if (schema <= 2) + { + setVersion (QDataStream::Qt_5_2); // Qt schema version + } +#endif +#if QT_VERSION >= 0x050400 + else if (schema <= 3) + { + setVersion (QDataStream::Qt_5_4); // Qt schema version + } +#endif + else + { + throw std::runtime_error {"Unrecognized message schema"}; + } + + // the following two items assume that the quint32 encoding is + // unchanged over QDataStream versions + *this << magic; + *this << schema; + + *this << static_cast<quint32> (type) << id.toUtf8 (); + } + + class Reader::impl + { + public: + void common_initialization (Reader * parent) + { + quint32 magic; + *parent >> magic; + if (magic != Builder::magic) + { + throw std::runtime_error {"Invalid message format"}; + } + *parent >> schema_; + if (schema_ > Builder::schema_number) + { + throw std::runtime_error {"Unrecognized message schema"}; + } + if (schema_ <= 1) + { + parent->setVersion (QDataStream::Qt_5_0); + } +#if QT_VERSION >= 0x050200 + else if (schema_ <= 2) + { + parent->setVersion (QDataStream::Qt_5_2); + } +#endif +#if QT_VERSION >= 0x050400 + else if (schema_ <= 3) + { + parent->setVersion (QDataStream::Qt_5_4); + } +#endif + quint32 type; + *parent >> type >> id_; + if (type >= maximum_message_type_) + { + qDebug () << "Unrecognized message type:" << type << "from id:" << id_; + type_ = maximum_message_type_; + } + else + { + type_ = static_cast<Type> (type); + } + } + + quint32 schema_; + Type type_; + QByteArray id_; + }; + + Reader::Reader (QIODevice * device) + : QDataStream {device} + { + m_->common_initialization (this); + } + + Reader::Reader (QByteArray const& a) + : QDataStream {a} + { + m_->common_initialization (this); + } + + Reader::~Reader () + { + } + + quint32 Reader::schema () const + { + return m_->schema_; + } + + Type Reader::type () const + { + return static_cast<Type> (m_->type_); + } + + QString Reader::id () const + { + return QString::fromUtf8 (m_->id_); + } +} diff --git a/Network/NetworkMessage.hpp b/Network/NetworkMessage.hpp new file mode 100644 index 00000000..d01a3475 --- /dev/null +++ b/Network/NetworkMessage.hpp @@ -0,0 +1,582 @@ +#ifndef NETWORK_MESSAGE_HPP__ +#define NETWORK_MESSAGE_HPP__ + +/* + * WSJT-X Message Formats + * ====================== + * + * All messages are written or read using the QDataStream derivatives + * defined below, note that we are using the default for floating + * point precision which means all are double precision i.e. 64-bit + * IEEE format. + * + * Message is big endian format + * + * Header format: + * + * 32-bit unsigned integer magic number 0xadbccbda + * 32-bit unsigned integer schema number + * + * Payload format: + * + * As per the QDataStream format, see below for version used and + * here: + * + * http://doc.qt.io/qt-5/datastreamformat.html + * + * for the serialization details for each type, at the time of + * writing the above document is for Qt_5_0 format which is buggy + * so we use Qt_5_4 format, differences are: + * + * QDateTime: + * QDate qint64 Julian day number + * QTime quint32 Milli-seconds since midnight + * timespec quint8 0=local, 1=UTC, 2=Offset from UTC + * (seconds) + * 3=time zone + * offset qint32 only present if timespec=2 + * timezone several-fields only present if timespec=3 + * + * we will avoid using QDateTime fields with time zones for + * simplicity. + * + * Type utf8 is a utf-8 byte string formatted as a QByteArray for + * serialization purposes (currently a quint32 size followed by size + * bytes, no terminator is present or counted). + * + * The QDataStream format document linked above is not complete for + * the QByteArray serialization format, it is similar to the QString + * serialization format in that it differentiates between empty + * strings and null strings. Empty strings have a length of zero + * whereas null strings have a length field of 0xffffffff. + * + * + * Schema Negotiation + * ------------------ + * + * The NetworkMessage::Builder class specifies a schema number which + * may be incremented from time to time. It represents a version of + * the underlying encoding schemes used to store data items. Since the + * underlying encoding is defined by the Qt project in it's + * QDataStream stream operators, it is essential that clients and + * servers of this protocol can agree on a common scheme. The + * NetworkMessage utility classes below exchange the schema number + * actually used. The handling of the schema is backwards compatible + * to an extent, so long as clients and servers are written + * correctly. For example a server written to any particular schema + * version can communicate with a client written to a later schema. + * + * Schema Version 1:- this schema used the QDataStream::Qt_5_0 version + * which is broken. + * + * Schema Version 2:- this schema uses the QDataStream::Qt_5_2 version. + * + * Schema Version 3:- this schema uses the QDataStream::Qt_5_4 version. + * + * + * Backward Compatibility + * ---------------------- + * + * It is important that applications developed at different times + * remain compatible with this protocol and with older or newer + * versions of WSJT-X. This is achieved by both third-party + * applications and WSJT-X honouring two basic rules. + * + * 1. New message types may be added to the protocol in the future, + * third-party applications and WSJT-X shall ignore silently any + * message types they do not recognize. + * + * 2. New fields may be added to existing message types, they will + * always be added to the end of the existing fields and the number + * and type of existing fields shall not change. If a field type + * must be changed; a new field will be added and the existing + * field will remain. The originator of such a message shall + * populate both the new and old field with reasonable + * values. Third-party applications and WSJT-X shall ignore + * silently any extra data received in datagrams after the fields + * they know about. + * + * Note that these rules are unrelated to the schema number above + * whose purpose is to distinguish between non-compatible encodings of + * field data types. New message types and extra fields in existing + * messages can and will be added without any change in schema number. + * + * + * Message Types + * ------------- + * + * Message Direction Value Type + * ------------- --------- ---------------------- ----------- + * Heartbeat Out/In 0 quint32 + * Id (unique key) utf8 + * Maximum schema number quint32 + * version utf8 + * revision utf8 + * + * The heartbeat message shall be sent on a periodic basis every + * NetworkMessage::pulse seconds (see below), the WSJT-X + * application does that using the MessageClient class. This + * message is intended to be used by servers to detect the presence + * of a client and also the unexpected disappearance of a client + * and by clients to learn the schema negotiated by the server + * after it receives the initial heartbeat message from a client. + * The message_aggregator reference server does just that using the + * MessageServer class. Upon initial startup a client must send a + * heartbeat message as soon as is practical, this message is used + * to negotiate the maximum schema number common to the client and + * server. Note that the server may not be able to support the + * client's requested maximum schema number, in which case the + * first message received from the server will specify a lower + * schema number (never a higher one as that is not allowed). If a + * server replies with a lower schema number then no higher than + * that number shall be used for all further outgoing messages from + * either clients or the server itself. + * + * Note: the "Maximum schema number" field was introduced at the + * same time as schema 3, therefore servers and clients must assume + * schema 2 is the highest schema number supported if the Heartbeat + * message does not contain the "Maximum schema number" field. + * + * + * Status Out 1 quint32 + * Id (unique key) utf8 + * Dial Frequency (Hz) quint64 + * Mode utf8 + * DX call utf8 + * Report utf8 + * Tx Mode utf8 + * Tx Enabled bool + * Transmitting bool + * Decoding bool + * Rx DF quint32 + * Tx DF quint32 + * DE call utf8 + * DE grid utf8 + * DX grid utf8 + * Tx Watchdog bool + * Sub-mode utf8 + * Fast mode bool + * Special Operation Mode quint8 + * Frequency Tolerance quint32 + * T/R Period quint32 + * Configuration Name utf8 + * + * WSJT-X sends this status message when various internal state + * changes to allow the server to track the relevant state of each + * client without the need for polling commands. The current state + * changes that generate status messages are: + * + * Application start up, + * "Enable Tx" button status changes, + * dial frequency changes, + * changes to the "DX Call" field, + * operating mode, sub-mode or fast mode changes, + * transmit mode changed (in dual JT9+JT65 mode), + * changes to the "Rpt" spinner, + * after an old decodes replay sequence (see Replay below), + * when switching between Tx and Rx mode, + * at the start and end of decoding, + * when the Rx DF changes, + * when the Tx DF changes, + * when settings are exited, + * when the DX call or grid changes, + * when the Tx watchdog is set or reset, + * when the frequency tolerance is changed, + * when the T/R period is changed, + * when the configuration name changes. + * + * The Special operation mode is an enumeration that indicates the + * setting selected in the WSJT-X "Settings->Advanced->Special + * operating activity" panel. The values are as follows: + * + * 0 -> NONE + * 1 -> NA VHF + * 2 -> EU VHF + * 3 -> FIELD DAY + * 4 -> RTTY RU + * 5 -> FOX + * 6 -> HOUND + * + * The Frequency Tolerance and T/R period fields may have a value + * of the maximum quint32 value which implies the field is not + * applicable. + * + * + * Decode Out 2 quint32 + * Id (unique key) utf8 + * New bool + * Time QTime + * snr qint32 + * Delta time (S) float (serialized as double) + * Delta frequency (Hz) quint32 + * Mode utf8 + * Message utf8 + * Low confidence bool + * Off air bool + * + * The decode message is sent when a new decode is completed, in + * this case the 'New' field is true. It is also used in response + * to a "Replay" message where each old decode in the "Band + * activity" window, that has not been erased, is sent in order + * as a one of these messages with the 'New' field set to false. + * See the "Replay" message below for details of usage. Low + * confidence decodes are flagged in protocols where the decoder + * has knows that a decode has a higher than normal probability + * of being false, they should not be reported on publicly + * accessible services without some attached warning or further + * validation. Off air decodes are those that result from playing + * back a .WAV file. + * + * + * Clear Out/In 3 quint32 + * Id (unique key) utf8 + * Window quint8 (In only) + * + * This message is send when all prior "Decode" messages in the + * "Band Activity" window have been discarded and therefore are + * no long available for actioning with a "Reply" message. It is + * sent when the user erases the "Band activity" window and when + * WSJT-X closes down normally. The server should discard all + * decode messages upon receipt of this message. + * + * It may also be sent to a WSJT-X instance in which case it + * clears one or both of the "Band Activity" and "Rx Frequency" + * windows. The Window argument can be one of the following + * values: + * + * 0 - clear the "Band Activity" window (default) + * 1 - clear the "Rx Frequency" window + * 2 - clear both "Band Activity" and "Rx Frequency" windows + * + * + * Reply In 4 quint32 + * Id (target unique key) utf8 + * Time QTime + * snr qint32 + * Delta time (S) float (serialized as double) + * Delta frequency (Hz) quint32 + * Mode utf8 + * Message utf8 + * Low confidence bool + * Modifiers quint8 + * + * In order for a server to provide a useful cooperative service + * to WSJT-X it is possible for it to initiate a QSO by sending + * this message to a client. WSJT-X filters this message and only + * acts upon it if the message exactly describes a prior decode + * and that decode is a CQ or QRZ message. The action taken is + * exactly equivalent to the user double clicking the message in + * the "Band activity" window. The intent of this message is for + * servers to be able to provide an advanced look up of potential + * QSO partners, for example determining if they have been worked + * before or if working them may advance some objective like + * award progress. The intention is not to provide a secondary + * user interface for WSJT-X, it is expected that after QSO + * initiation the rest of the QSO is carried out manually using + * the normal WSJT-X user interface. + * + * The Modifiers field allows the equivalent of keyboard + * modifiers to be sent "as if" those modifier keys where pressed + * while double-clicking the specified decoded message. The + * modifier values (hexadecimal) are as follows: + * + * no modifier 0x00 + * SHIFT 0x02 + * CTRL 0x04 CMD on Mac + * ALT 0x08 + * META 0x10 Windows key on MS Windows + * KEYPAD 0x20 Keypad or arrows + * Group switch 0x40 X11 only + * + * + * QSO Logged Out 5 quint32 + * Id (unique key) utf8 + * Date & Time Off QDateTime + * DX call utf8 + * DX grid utf8 + * Tx frequency (Hz) quint64 + * Mode utf8 + * Report sent utf8 + * Report received utf8 + * Tx power utf8 + * Comments utf8 + * Name utf8 + * Date & Time On QDateTime + * Operator call utf8 + * My call utf8 + * My grid utf8 + * Exchange sent utf8 + * Exchange received utf8 + * + * The QSO logged message is sent to the server(s) when the + * WSJT-X user accepts the "Log QSO" dialog by clicking the "OK" + * button. + * + * + * Close Out/In 6 quint32 + * Id (unique key) utf8 + * + * Close is sent by a client immediately prior to it shutting + * down gracefully. When sent by a server it requests the target + * client to close down gracefully. + * + * + * Replay In 7 quint32 + * Id (unique key) utf8 + * + * When a server starts it may be useful for it to determine the + * state of preexisting clients. Sending this message to each + * client as it is discovered will cause that client (WSJT-X) to + * send a "Decode" message for each decode currently in its "Band + * activity" window. Each "Decode" message sent will have the + * "New" flag set to false so that they can be distinguished from + * new decodes. After all the old decodes have been broadcast a + * "Status" message is also broadcast. If the server wishes to + * determine the status of a newly discovered client; this + * message should be used. + * + * + * Halt Tx In 8 + * Id (unique key) utf8 + * Auto Tx Only bool + * + * The server may stop a client from transmitting messages either + * immediately or at the end of the current transmission period + * using this message. + * + * + * Free Text In 9 + * Id (unique key) utf8 + * Text utf8 + * Send bool + * + * This message allows the server to set the current free text + * message content. Sending this message with a non-empty "Text" + * field is equivalent to typing a new message (old contents are + * discarded) in to the WSJT-X free text message field or "Tx5" + * field (both are updated) and if the "Send" flag is set then + * clicking the "Now" radio button for the "Tx5" field if tab one + * is current or clicking the "Free msg" radio button if tab two + * is current. + * + * It is the responsibility of the sender to limit the length of + * the message text and to limit it to legal message + * characters. Despite this, it may be difficult for the sender + * to determine the maximum message length without reimplementing + * the complete message encoding protocol. Because of this is may + * be better to allow any reasonable message length and to let + * the WSJT-X application encode and possibly truncate the actual + * on-air message. + * + * If the message text is empty the meaning of the message is + * refined to send the current free text unchanged when the + * "Send" flag is set or to clear the current free text when the + * "Send" flag is unset. Note that this API does not include a + * command to determine the contents of the current free text + * message. + * + * + * WSPRDecode Out 10 quint32 + * Id (unique key) utf8 + * New bool + * Time QTime + * snr qint32 + * Delta time (S) float (serialized as double) + * Frequency (Hz) quint64 + * Drift (Hz) qint32 + * Callsign utf8 + * Grid utf8 + * Power (dBm) qint32 + * Off air bool + * + * The decode message is sent when a new decode is completed, in + * this case the 'New' field is true. It is also used in response + * to a "Replay" message where each old decode in the "Band + * activity" window, that has not been erased, is sent in order + * as a one of these messages with the 'New' field set to + * false. See the "Replay" message below for details of + * usage. The off air field indicates that the decode was decoded + * from a played back recording. + * + * + * Location In 11 + * Id (unique key) utf8 + * Location utf8 + * + * This message allows the server to set the current current + * geographical location of operation. The supplied location is + * not persistent but is used as a session lifetime replacement + * loction that overrides the Maidenhead grid locater set in the + * application settings. The intent is to allow an external + * application to update the operating location dynamically + * during a mobile period of operation. + * + * Currently only Maidenhead grid squares or sub-squares are + * accepted, i.e. 4- or 6-digit locators. Other formats may be + * accepted in future. + * + * + * Logged ADIF Out 12 quint32 + * Id (unique key) utf8 + * ADIF text utf8 + * + * The logged ADIF message is sent to the server(s) when the + * WSJT-X user accepts the "Log QSO" dialog by clicking the "OK" + * button. The "ADIF text" field consists of a valid ADIF file + * such that the WSJT-X UDP header information is encapsulated + * into a valid ADIF header. E.g.: + * + * <magic-number><schema-number><type><id><32-bit-count> # binary encoded fields + * # the remainder is the contents of the ADIF text field + * <adif_ver:5>3.0.7 + * <programid:6>WSJT-X + * <EOH> + * ADIF log data fields ...<EOR> + * + * Note that receiving applications can treat the whole message + * as a valid ADIF file with one record without special parsing. + * + * + * Highlight Callsign In 13 quint32 + * Id (unique key) utf8 + * Callsign utf8 + * Background Color QColor + * Foreground Color QColor + * Highlight last bool + * + * The server may send this message at any time. The message + * specifies the background and foreground color that will be + * used to highlight the specified callsign in the decoded + * messages printed in the Band Activity panel. The WSJT-X + * clients maintain a list of such instructions and apply them to + * all decoded messages in the band activity window. To clear + * highlighting send an invalid QColor value for either or both + * of the background and foreground fields. + * + * The "Highlight last" field allows the sender to request that + * the last instance only instead of all instances of the + * specified call be highlighted or have it's highlighting + * cleared. + * + * + * SwitchConfiguration In 14 quint32 + * Id (unique key) utf8 + * Configuration Name utf8 + * + * The server may send this message at any time. The message + * specifies the name of the configuration to switch to. The new + * configuration must exist. + * + * + * Configure In 15 quint32 + * Id (unique key) utf8 + * Mode utf8 + * Frequency Tolerance quint32 + * Submode utf8 + * Fast Mode bool + * T/R Period quint32 + * Rx DF quint32 + * DX Call utf8 + * DX Grid utf8 + * Generate Messages bool + * + * The server may send this message at any time. The message + * specifies various configuration options. For utf8 string + * fields an empty value implies no change, for the quint32 Rx DF + * and Frequency Tolerance fields the maximum quint32 value + * implies no change. Invalid or unrecognized values will be + * silently ignored. + */ + +#include <QDataStream> + +#include "pimpl_h.hpp" + +class QIODevice; +class QByteArray; +class QString; + +namespace NetworkMessage +{ + // NEVER DELETE MESSAGE TYPES + enum Type + { + Heartbeat, + Status, + Decode, + Clear, + Reply, + QSOLogged, + Close, + Replay, + HaltTx, + FreeText, + WSPRDecode, + Location, + LoggedADIF, + HighlightCallsign, + SwitchConfiguration, + Configure, + maximum_message_type_ // ONLY add new message types + // immediately before here + }; + + quint32 constexpr pulse {15}; // seconds + + // + // NetworkMessage::Builder - build a message containing serialized Qt types + // + class Builder + : public QDataStream + { + public: + static quint32 constexpr magic {0xadbccbda}; // never change this + + // increment this if a newer Qt schema is required and add decode + // logic to the Builder and Reader class implementations +#if QT_VERSION >= 0x050400 + static quint32 constexpr schema_number {3}; +#elif QT_VERSION >= 0x050200 + static quint32 constexpr schema_number {2}; +#else + // Schema 1 (Qt_5_0) is broken +#error "Qt version 5.2 or greater required" +#endif + + explicit Builder (QIODevice *, Type, QString const& id, quint32 schema); + explicit Builder (QByteArray *, Type, QString const& id, quint32 schema); + Builder (Builder const&) = delete; + Builder& operator = (Builder const&) = delete; + + private: + void common_initialization (Type type, QString const& id, quint32 schema); + }; + + // + // NetworkMessage::Reader - read a message containing serialized Qt types + // + // Message is as per NetworkMessage::Builder above, the schema() + // member may be used to determine the schema of the original + // message. + // + class Reader + : public QDataStream + { + public: + explicit Reader (QIODevice *); + explicit Reader (QByteArray const&); + Reader (Reader const&) = delete; + Reader& operator = (Reader const&) = delete; + ~Reader (); + + quint32 schema () const; + Type type () const; + QString id () const; + + private: + class impl; + pimpl<impl> m_; + }; +} + +#endif diff --git a/Network/NetworkServerLookup.cpp b/Network/NetworkServerLookup.cpp new file mode 100644 index 00000000..0d7cd4fe --- /dev/null +++ b/Network/NetworkServerLookup.cpp @@ -0,0 +1,85 @@ +#include "NetworkServerLookup.hpp" + +#include <stdexcept> + +#include <QHostInfo> +#include <QString> + +std::tuple<QHostAddress, quint16> +network_server_lookup (QString query + , quint16 default_service_port + , QHostAddress default_host_address + , QAbstractSocket::NetworkLayerProtocol required_protocol) +{ + query = query.trimmed (); + + QHostAddress host_address {default_host_address}; + quint16 service_port {default_service_port}; + + QString host_name; + if (!query.isEmpty ()) + { + int port_colon_index {-1}; + + if ('[' == query[0]) + { + // assume IPv6 combined address/port syntax [<address>]:<port> + auto close_bracket_index = query.lastIndexOf (']'); + host_name = query.mid (1, close_bracket_index - 1); + port_colon_index = query.indexOf (':', close_bracket_index); + } + else + { + port_colon_index = query.lastIndexOf (':'); + host_name = query.left (port_colon_index); + } + host_name = host_name.trimmed (); + + if (port_colon_index >= 0) + { + bool ok; + service_port = query.mid (port_colon_index + 1).trimmed ().toUShort (&ok); + if (!ok) + { + throw std::runtime_error {"network server lookup error: invalid port"}; + } + } + } + + if (!host_name.isEmpty ()) + { + auto host_info = QHostInfo::fromName (host_name); + if (host_info.addresses ().isEmpty ()) + { + throw std::runtime_error {"network server lookup error: host name lookup failed"}; + } + + bool found {false}; + for (int i {0}; i < host_info.addresses ().size () && !found; ++i) + { + host_address = host_info.addresses ().at (i); + switch (required_protocol) + { + case QAbstractSocket::IPv4Protocol: + case QAbstractSocket::IPv6Protocol: + if (required_protocol != host_address.protocol ()) + { + break; + } + // fall through + case QAbstractSocket::AnyIPProtocol: + found = true; + break; + + default: + throw std::runtime_error {"network server lookup error: invalid required protocol"}; + } + } + if (!found) + { + throw std::runtime_error {"network server lookup error: no suitable host address found"}; + } + } + + return std::make_tuple (host_address, service_port); +} diff --git a/Network/NetworkServerLookup.hpp b/Network/NetworkServerLookup.hpp new file mode 100644 index 00000000..7c7c9d11 --- /dev/null +++ b/Network/NetworkServerLookup.hpp @@ -0,0 +1,38 @@ +#ifndef NETWORK_SERVER_LOOKUP_HPP__ +#define NETWORK_SERVER_LOOKUP_HPP__ + +#include <tuple> + +#include <QHostAddress> +#include <QAbstractSocket> + +class QString; + +// +// Do a blocking DNS lookup using query as a destination host address +// and port. +// +// query can be one of: +// +// 1) "" (empty string) - use defaults +// 2) ":nnnnn" - override default service port with port nnnnn +// 3) "<valid-host-name>" - override default host address with DNS lookup +// 4) "nnn.nnn.nnn.nnn" - override default host address with the IPv4 address given by nnn.nnn.nnn.nnn +// 5) "[<valid-IPv6-address]" - override default host address with the given IPv6 address +// 6) "<valid-host-name>:nnnnn" - use as per (3) & (2) +// 7) "nnn.nnn.nnn.nnn:nnnnn" - use as per (4) & (2) +// 8) "[<valid-IPv6-address]:nnnnn" - use as per (5) & (2) +// +// The first host address matching the protocol and the service port +// number are returned. +// +// If no suitable host address is found QHostAddress::Null will be +// returned in the first member of the result tuple. +// +std::tuple<QHostAddress, quint16> +network_server_lookup (QString query + , quint16 default_service_port + , QHostAddress default_host_address = QHostAddress::LocalHost + , QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::AnyIPProtocol); + +#endif diff --git a/Network/psk_reporter.cpp b/Network/psk_reporter.cpp new file mode 100644 index 00000000..e84b5a92 --- /dev/null +++ b/Network/psk_reporter.cpp @@ -0,0 +1,133 @@ +// KISS Interface for posting spots to PSK Reporter web site +// Implemented by Edson Pereira PY2SDR +// +// Reports will be sent in batch mode every 5 minutes. + +#include "psk_reporter.h" + +#include <QHostInfo> +#include <QTimer> + +#include "Network/MessageClient.hpp" + +#include "moc_psk_reporter.cpp" + +namespace +{ + int constexpr MAX_PAYLOAD_LENGTH {1400}; +} + +PSK_Reporter::PSK_Reporter(MessageClient * message_client, QObject *parent) : + QObject {parent}, + m_messageClient {message_client}, + reportTimer {new QTimer {this}}, + m_sequenceNumber {0} +{ + m_header_h = "000Allllttttttttssssssssiiiiiiii"; + + // We use 50E2 and 50E3 for link Id + m_rxInfoDescriptor_h = "0003002C50E200040000" + "8002FFFF0000768F" // 2. Rx Call + "8004FFFF0000768F" // 4. Rx Grid + "8008FFFF0000768F" // 8. Rx Soft + "8009FFFF0000768F" // 9. Rx Antenna + "0000"; + + m_txInfoDescriptor_h = "0002003C50E30007" + "8001FFFF0000768F" // 1. Tx Call + "800500040000768F" // 5. Tx Freq + "800600010000768F" // 6. Tx snr + "800AFFFF0000768F" // 10. Tx Mode + "8003FFFF0000768F" // 3. Tx Grid + "800B00010000768F" // 11. Tx info src + "00960004"; // Report time + + + m_randomId_h = QString("%1").arg(qrand(),8,16,QChar('0')); + + QHostInfo::lookupHost("report.pskreporter.info", this, SLOT(dnsLookupResult(QHostInfo))); + + connect(reportTimer, SIGNAL(timeout()), this, SLOT(sendReport())); + reportTimer->start(5*60*1000); // 5 minutes; +} + +void PSK_Reporter::setLocalStation(QString call, QString gridSquare, QString antenna, QString programInfo) +{ + m_rxCall = call; + m_rxGrid = gridSquare; + m_rxAnt = antenna; + m_progId = programInfo; +} + +void PSK_Reporter::addRemoteStation(QString call, QString grid, QString freq, QString mode, QString snr, QString time ) +{ + QHash<QString,QString> spot; + spot["call"] = call; + spot["grid"] = grid; + spot["snr"] = snr; + spot["freq"] = freq; + spot["mode"] = mode; + spot["time"] = time; + m_spotQueue.enqueue(spot); +} + +void PSK_Reporter::sendReport() +{ + while (!m_spotQueue.isEmpty()) { + QString report_h; + + // Header + QString header_h = m_header_h; + header_h.replace("tttttttt", QString("%1").arg(QDateTime::currentDateTime().toTime_t(),8,16,QChar('0'))); + header_h.replace("ssssssss", QString("%1").arg(++m_sequenceNumber,8,16,QChar('0'))); + header_h.replace("iiiiiiii", m_randomId_h); + + // Receiver information + QString rxInfoData_h = "50E2llll"; + rxInfoData_h += QString("%1").arg(m_rxCall.length(),2,16,QChar('0')) + m_rxCall.toUtf8().toHex(); + rxInfoData_h += QString("%1").arg(m_rxGrid.length(),2,16,QChar('0')) + m_rxGrid.toUtf8().toHex(); + rxInfoData_h += QString("%1").arg(m_progId.length(),2,16,QChar('0')) + m_progId.toUtf8().toHex(); + rxInfoData_h += QString("%1").arg(m_rxAnt.length(),2,16,QChar('0')) + m_rxAnt.toUtf8().toHex(); + rxInfoData_h += "0000"; + rxInfoData_h.replace("50E2llll", "50E2" + QString("%1").arg(rxInfoData_h.length()/2,4,16,QChar('0'))); + + // Sender information + QString txInfoData_h = "50E3llll"; + while (!m_spotQueue.isEmpty() + && (header_h.size () + m_rxInfoDescriptor_h.size () + m_txInfoDescriptor_h.size () + rxInfoData_h.size () + txInfoData_h.size ()) / 2 < MAX_PAYLOAD_LENGTH) { + QHash<QString,QString> spot = m_spotQueue.dequeue(); + txInfoData_h += QString("%1").arg(spot["call"].length(),2,16,QChar('0')) + spot["call"].toUtf8().toHex(); + txInfoData_h += QString("%1").arg(spot["freq"].toLongLong(),8,16,QChar('0')); + txInfoData_h += QString("%1").arg(spot["snr"].toInt(),8,16,QChar('0')).right(2); + txInfoData_h += QString("%1").arg(spot["mode"].length(),2,16,QChar('0')) + spot["mode"].toUtf8().toHex(); + txInfoData_h += QString("%1").arg(spot["grid"].length(),2,16,QChar('0')) + spot["grid"].toUtf8().toHex(); + txInfoData_h += QString("%1").arg(1,2,16,QChar('0')); // REPORTER_SOURCE_AUTOMATIC + txInfoData_h += QString("%1").arg(spot["time"].toInt(),8,16,QChar('0')); + } + txInfoData_h += "0000"; + txInfoData_h.replace("50E3llll", "50E3" + QString("%1").arg(txInfoData_h.length()/2,4,16,QChar('0'))); + report_h = header_h + m_rxInfoDescriptor_h + m_txInfoDescriptor_h + rxInfoData_h + txInfoData_h; + //qDebug() << "Sending Report TX: "; + + report_h.replace("000Allll", "000A" + QString("%1").arg(report_h.length()/2,4,16,QChar('0'))); + QByteArray report = QByteArray::fromHex(report_h.toUtf8()); + + // Send data to PSK Reporter site + if (!m_pskReporterAddress.isNull()) { + m_messageClient->send_raw_datagram (report, m_pskReporterAddress, 4739); + } + } +} + +void PSK_Reporter::dnsLookupResult(QHostInfo info) +{ + if (!info.addresses().isEmpty()) { + m_pskReporterAddress = info.addresses().at(0); + // qDebug() << "PSK Reporter IP: " << m_pskReporterAddress; + + // deal with miss-configured settings that attempt to set a + // Pskreporter Internet address for the WSJT-X UDP protocol + // server address + m_messageClient->add_blocked_destination (m_pskReporterAddress); + } +} diff --git a/Network/psk_reporter.h b/Network/psk_reporter.h new file mode 100644 index 00000000..16afcf43 --- /dev/null +++ b/Network/psk_reporter.h @@ -0,0 +1,54 @@ +// -*- Mode: C++ -*- +#ifndef PSK_REPORTER_H +#define PSK_REPORTER_H + +#include <QObject> +#include <QString> +#include <QHostAddress> +#include <QQueue> +#include <QHash> + +class MessageClient; +class QTimer; +class QHostInfo; + +class PSK_Reporter : public QObject +{ + Q_OBJECT +public: + explicit PSK_Reporter(MessageClient *, QObject *parent = nullptr); + void setLocalStation(QString call, QString grid, QString antenna, QString programInfo); + void addRemoteStation(QString call, QString grid, QString freq, QString mode, QString snr, QString time); + +signals: + +public slots: + void sendReport(); + +private slots: + void dnsLookupResult(QHostInfo info); + +private: + QString m_header_h; + QString m_rxInfoDescriptor_h; + QString m_txInfoDescriptor_h; + QString m_randomId_h; + QString m_linkId_h; + + QString m_rxCall; + QString m_rxGrid; + QString m_rxAnt; + QString m_progId; + + QHostAddress m_pskReporterAddress; + + QQueue< QHash<QString,QString> > m_spotQueue; + + MessageClient * m_messageClient; + + QTimer *reportTimer; + + int m_sequenceNumber; +}; + +#endif // PSK_REPORTER_H diff --git a/Network/wsprnet.cpp b/Network/wsprnet.cpp new file mode 100644 index 00000000..9e666088 --- /dev/null +++ b/Network/wsprnet.cpp @@ -0,0 +1,230 @@ +// Interface to WSPRnet website +// +// by Edson Pereira - PY2SDR + +#include "wsprnet.h" + +#include <cmath> + +#include <QTimer> +#include <QFile> +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QUrl> +#include <QDebug> + +#include "moc_wsprnet.cpp" + +namespace +{ + char const * const wsprNetUrl = "http://wsprnet.org/post?"; + // char const * const wsprNetUrl = "http://127.0.0.1/post?"; +}; + +WSPRNet::WSPRNet(QNetworkAccessManager * manager, QObject *parent) + : QObject{parent} + , networkManager {manager} + , uploadTimer {new QTimer {this}} + , m_urlQueueSize {0} +{ + connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkReply(QNetworkReply*))); + connect( uploadTimer, SIGNAL(timeout()), this, SLOT(work())); +} + +void WSPRNet::upload(QString const& call, QString const& grid, QString const& rfreq, QString const& tfreq, + QString const& mode, QString const& tpct, QString const& dbm, QString const& version, + QString const& fileName) +{ + m_call = call; + m_grid = grid; + m_rfreq = rfreq; + m_tfreq = tfreq; + m_mode = mode; + m_tpct = tpct; + m_dbm = dbm; + m_vers = version; + m_file = fileName; + + // Open the wsprd.out file + QFile wsprdOutFile(fileName); + if (!wsprdOutFile.open(QIODevice::ReadOnly | QIODevice::Text) || + wsprdOutFile.size() == 0) { + urlQueue.enqueue( wsprNetUrl + urlEncodeNoSpot()); + m_uploadType = 1; + uploadTimer->start(200); + return; + } + + // Read the contents + while (!wsprdOutFile.atEnd()) { + QHash<QString,QString> query; + if ( decodeLine(wsprdOutFile.readLine(), query) ) { + // Prevent reporting data ouside of the current frequency band + float f = fabs(m_rfreq.toFloat() - query["tqrg"].toFloat()); + if (f < 0.0002) { + urlQueue.enqueue( wsprNetUrl + urlEncodeSpot(query)); + m_uploadType = 2; + } + } + } + m_urlQueueSize = urlQueue.size(); + uploadTimer->start(200); +} + +void WSPRNet::networkReply(QNetworkReply *reply) +{ + // check if request was ours + if (m_outstandingRequests.removeOne (reply)) { + if (QNetworkReply::NoError != reply->error ()) { + Q_EMIT uploadStatus (QString {"Error: %1"}.arg (reply->error ())); + // not clearing queue or halting queuing as it may be a transient + // one off request error + } + else { + QString serverResponse = reply->readAll(); + if( m_uploadType == 2) { + if (!serverResponse.contains(QRegExp("spot\\(s\\) added"))) { + emit uploadStatus(QString {"Upload Failed: %1"}.arg (serverResponse)); + urlQueue.clear(); + uploadTimer->stop(); + } + } + + if (urlQueue.isEmpty()) { + emit uploadStatus("done"); + QFile::remove(m_file); + uploadTimer->stop(); + } + } + + qDebug () << QString {"WSPRnet.org %1 outstanding requests"}.arg (m_outstandingRequests.size ()); + + // delete request object instance on return to the event loop otherwise it is leaked + reply->deleteLater (); + } +} + +bool WSPRNet::decodeLine(QString const& line, QHash<QString,QString> &query) +{ + // 130223 2256 7 -21 -0.3 14.097090 DU1MGA PK04 37 0 40 0 + // Date Time Sync dBm DT Freq Msg + // 1 2 3 4 5 6 -------7------ 8 9 10 + QRegExp rx("^(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+([+-]?\\d+)\\s+([+-]?\\d+\\.\\d+)\\s+(\\d+\\.\\d+)\\s+(.*)\\s+([+-]?\\d+)\\s+([+-]?\\d+)\\s+([+-]?\\d+)"); + if (rx.indexIn(line) != -1) { + int msgType = 0; + QString msg = rx.cap(7); + msg.remove(QRegExp("\\s+$")); + msg.remove(QRegExp("^\\s+")); + QString call, grid, dbm; + QRegExp msgRx; + + // Check for Message Type 1 + msgRx.setPattern("^([A-Z0-9]{3,6})\\s+([A-Z]{2}\\d{2})\\s+(\\d+)"); + if (msgRx.indexIn(msg) != -1) { + msgType = 1; + call = msgRx.cap(1); + grid = msgRx.cap(2); + dbm = msgRx.cap(3); + } + + // Check for Message Type 2 + msgRx.setPattern("^([A-Z0-9/]+)\\s+(\\d+)"); + if (msgRx.indexIn(msg) != -1) { + msgType = 2; + call = msgRx.cap(1); + grid = ""; + dbm = msgRx.cap(2); + } + + // Check for Message Type 3 + msgRx.setPattern("^<([A-Z0-9/]+)>\\s+([A-Z]{2}\\d{2}[A-Z]{2})\\s+(\\d+)"); + if (msgRx.indexIn(msg) != -1) { + msgType = 3; + call = msgRx.cap(1); + grid = msgRx.cap(2); + dbm = msgRx.cap(3); + } + + // Unknown message format + if (!msgType) { + return false; + } + + query["function"] = "wspr"; + query["date"] = rx.cap(1); + query["time"] = rx.cap(2); + query["sig"] = rx.cap(4); + query["dt"] = rx.cap(5); + query["drift"] = rx.cap(8); + query["tqrg"] = rx.cap(6); + query["tcall"] = call; + query["tgrid"] = grid; + query["dbm"] = dbm; + } else { + return false; + } + return true; +} + +QString WSPRNet::urlEncodeNoSpot() +{ + QString queryString; + queryString += "function=wsprstat&"; + queryString += "rcall=" + m_call + "&"; + queryString += "rgrid=" + m_grid + "&"; + queryString += "rqrg=" + m_rfreq + "&"; + queryString += "tpct=" + m_tpct + "&"; + queryString += "tqrg=" + m_tfreq + "&"; + queryString += "dbm=" + m_dbm + "&"; + queryString += "version=" + m_vers; + if(m_mode=="WSPR") queryString += "&mode=2"; + if(m_mode=="WSPR-15") queryString += "&mode=15"; + return queryString;; +} + +QString WSPRNet::urlEncodeSpot(QHash<QString,QString> const& query) +{ + QString queryString; + queryString += "function=" + query["function"] + "&"; + queryString += "rcall=" + m_call + "&"; + queryString += "rgrid=" + m_grid + "&"; + queryString += "rqrg=" + m_rfreq + "&"; + queryString += "date=" + query["date"] + "&"; + queryString += "time=" + query["time"] + "&"; + queryString += "sig=" + query["sig"] + "&"; + queryString += "dt=" + query["dt"] + "&"; + queryString += "drift=" + query["drift"] + "&"; + queryString += "tqrg=" + query["tqrg"] + "&"; + queryString += "tcall=" + query["tcall"] + "&"; + queryString += "tgrid=" + query["tgrid"] + "&"; + queryString += "dbm=" + query["dbm"] + "&"; + queryString += "version=" + m_vers; + if(m_mode=="WSPR") queryString += "&mode=2"; + if(m_mode=="WSPR-15") queryString += "&mode=15"; + return queryString; +} + +void WSPRNet::work() +{ + if (!urlQueue.isEmpty()) { + if (QNetworkAccessManager::Accessible != networkManager->networkAccessible ()) { + // try and recover network access for QNAM + networkManager->setNetworkAccessible (QNetworkAccessManager::Accessible); + } + QUrl url(urlQueue.dequeue()); + QNetworkRequest request(url); + m_outstandingRequests << networkManager->get(request); + emit uploadStatus(QString {"Uploading Spot %1/%2"}.arg (m_urlQueueSize - urlQueue.size()).arg (m_urlQueueSize)); + } else { + uploadTimer->stop(); + } +} + +void WSPRNet::abortOutstandingRequests () { + urlQueue.clear (); + for (auto& request : m_outstandingRequests) { + request->abort (); + } + m_urlQueueSize = 0; +} diff --git a/Network/wsprnet.h b/Network/wsprnet.h new file mode 100644 index 00000000..d712d527 --- /dev/null +++ b/Network/wsprnet.h @@ -0,0 +1,46 @@ +#ifndef WSPRNET_H +#define WSPRNET_H + +#include <QObject> +#include <QString> +#include <QList> +#include <QHash> +#include <QQueue> + +class QNetworkAccessManager; +class QTimer; +class QNetworkReply; + +class WSPRNet : public QObject +{ + Q_OBJECT; + +public: + explicit WSPRNet(QNetworkAccessManager *, QObject *parent = nullptr); + void upload(QString const& call, QString const& grid, QString const& rfreq, QString const& tfreq, + QString const& mode, QString const& tpct, QString const& dbm, QString const& version, + QString const& fileName); + static bool decodeLine(QString const& line, QHash<QString,QString> &query); + +signals: + void uploadStatus(QString); + +public slots: + void networkReply(QNetworkReply *); + void work(); + void abortOutstandingRequests (); + +private: + QNetworkAccessManager *networkManager; + QList<QNetworkReply *> m_outstandingRequests; + QString m_call, m_grid, m_rfreq, m_tfreq, m_mode, m_tpct, m_dbm, m_vers, m_file; + QQueue<QString> urlQueue; + QTimer *uploadTimer; + int m_urlQueueSize; + int m_uploadType; + + QString urlEncodeNoSpot(); + QString urlEncodeSpot(QHash<QString,QString> const& spot); +}; + +#endif // WSPRNET_H |
