QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RTCMUdpInput.cc
Go to the documentation of this file.
1#include "RTCMUdpInput.h"
3
4QGC_LOGGING_CATEGORY(RTCMUdpInputLog, "GPS.RTCMUdpInput")
5
6RTCMUdpInput::RTCMUdpInput(quint16 port, QObject *parent)
7 : QObject(parent)
8 , _port(port)
9{
10 connect(&_socket, &QUdpSocket::readyRead, this, &RTCMUdpInput::_readDatagrams);
11}
12
17
19{
20 stop();
21
22 if (!_socket.bind(QHostAddress::AnyIPv4, _port)) {
23 qCWarning(RTCMUdpInputLog) << "Failed to bind UDP socket on port" << _port
24 << ":" << _socket.errorString();
25 return false;
26 }
27
28 _running = true;
29 emit runningChanged();
30 qCDebug(RTCMUdpInputLog) << "Listening for RTCM data on UDP port" << _port;
31 return true;
32}
33
35{
36 if (!_running) {
37 return;
38 }
39
40 _socket.close();
41 _running = false;
42 emit runningChanged();
43 qCDebug(RTCMUdpInputLog) << "Stopped listening on UDP port" << _port;
44}
45
46void RTCMUdpInput::setPort(quint16 port)
47{
48 if (_port == port) {
49 return;
50 }
51
52 _port = port;
53 emit portChanged();
54
55 if (_running) {
56 start(); // restart on new port
57 }
58}
59
60void RTCMUdpInput::_readDatagrams()
61{
62 while (_socket.hasPendingDatagrams()) {
63 const qint64 size = _socket.pendingDatagramSize();
64 if (size <= 0) {
65 (void) _socket.readDatagram(nullptr, 0); // discard malformed
66 continue;
67 }
68
69 QByteArray data(static_cast<qsizetype>(size), Qt::Uninitialized);
70 const qint64 read = _socket.readDatagram(data.data(), size);
71 if (read <= 0) {
72 qCWarning(RTCMUdpInputLog) << "readDatagram failed:" << _socket.errorString();
73 continue;
74 }
75
76 if (read != size) {
77 data.resize(static_cast<qsizetype>(read));
78 }
79
80 qCDebug(RTCMUdpInputLog) << "Received RTCM datagram:" << read << "bytes";
81 emit rtcmDataReceived(data);
82 }
83}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Listens on a UDP port for raw RTCM3 correction data and emits it for forwarding to connected vehicles...
void stop()
Unbind the socket and stop accepting datagrams.
void runningChanged()
void rtcmDataReceived(const QByteArray &data)
~RTCMUdpInput() override
void setPort(quint16 port)
Change the listen port. If already running, restarts automatically.
void portChanged()
quint16 port() const
QByteArray read(int deviceId, int length, int timeout)