Skip to content

Commit c6d1ef1

Browse files
committed
issue-7133-updates-for-pr-review
1 parent d880094 commit c6d1ef1

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/GPS/NTRIP.cc

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,9 @@ void NTRIPTCPLink::_parse(const QByteArray& buffer)
453453
logged_empty_once = true;
454454
}
455455

456+
// Define named constant for header and CRC byte count
457+
static constexpr int RTCM_HEADER_AND_CRC_BYTES = 6;
458+
456459
for (const uint8_t& byte : buffer) {
457460
if (_state == NTRIPState::waiting_for_rtcm_header) {
458461
if (byte != RTCM3_PREAMBLE)
@@ -461,7 +464,10 @@ void NTRIPTCPLink::_parse(const QByteArray& buffer)
461464
}
462465
if (_rtcmParser->addByte(byte)) {
463466
_state = NTRIPState::waiting_for_rtcm_header;
464-
QByteArray message((char*)_rtcmParser->message(), static_cast<int>(_rtcmParser->messageLength() + 6)); // +6 for header and CRC
467+
QByteArray message(
468+
reinterpret_cast<char*>(_rtcmParser->message()),
469+
static_cast<int>(_rtcmParser->messageLength() + RTCM_HEADER_AND_CRC_BYTES)
470+
);
465471
const uint16_t id = _rtcmParser->messageId();
466472

467473
if (_whitelist.empty() || _whitelist.contains(id)) {
@@ -728,32 +734,50 @@ void NTRIPTCPLink::sendNMEA(const QByteArray& sentence)
728734

729735
QByteArray line = sentence;
730736

731-
// Validate checksum in the form $CORE*XX (no CRLF included)
732-
// If it is wrong or missing, fix it here.
737+
// Validate or repair checksum in the form $CORE*XX
733738
if (line.size() >= 5 && line.at(0) == '$') {
734739
int star = line.lastIndexOf('*');
735740
if (star > 1) {
736-
// compute checksum over bytes between '$' and '*'
741+
// Calculate checksum over bytes between '$' and '*'
737742
quint8 calc = 0;
738743
for (int i = 1; i < star; ++i) {
739744
calc ^= static_cast<quint8>(line.at(i));
740745
}
741-
QByteArray txCks = line.mid(star + 1, 2).toUpper();
742-
QByteArray calcCks = QByteArray::number(calc, 16).rightJustified(2, '0').toUpper();
743746

744-
const bool match = (txCks == calcCks);
745-
qCDebug(NTRIPLog) << "NTRIP NMEA checksum tx=" << QString::fromUtf8(txCks)
746-
<< "calc=" << QString::fromUtf8(calcCks)
747-
<< "match=" << match;
747+
// Format calculated checksum as two uppercase hex digits
748+
QByteArray calcCks = QByteArray::number(calc, 16)
749+
.rightJustified(2, '0')
750+
.toUpper();
751+
752+
bool needsRepair = false;
753+
if (star + 3 > line.size()) {
754+
// Not enough chars after '*', definitely needs repair
755+
needsRepair = true;
756+
} else {
757+
QByteArray txCks = line.mid(star + 1, 2).toUpper();
758+
if (txCks != calcCks) {
759+
needsRepair = true;
760+
}
761+
}
748762

749-
if (!match) {
750-
// repair the checksum in-place
763+
if (needsRepair) {
764+
// Remove any existing checksum and replace with correct one
751765
line = line.left(star + 1) + calcCks;
752766
}
767+
} else {
768+
// No '*' found, append one and correct checksum
769+
quint8 calc = 0;
770+
for (int i = 1; i < line.size(); ++i) {
771+
calc ^= static_cast<quint8>(line.at(i));
772+
}
773+
QByteArray calcCks = QByteArray::number(calc, 16)
774+
.rightJustified(2, '0')
775+
.toUpper();
776+
line.append('*').append(calcCks);
753777
}
754778
}
755779

756-
// Append CRLF once and write once
780+
// Ensure CRLF termination
757781
if (!line.endsWith("\r\n")) {
758782
line.append("\r\n");
759783
}
@@ -767,6 +791,7 @@ void NTRIPTCPLink::sendNMEA(const QByteArray& sentence)
767791
qCDebug(NTRIPLog) << "NTRIP Bytes written:" << written;
768792
}
769793

794+
770795
void NTRIPTCPLink::requestStop()
771796
{
772797
_stopping.store(true);
@@ -1164,7 +1189,11 @@ void NTRIPManager::_sendGGA()
11641189
qCDebug(NTRIPLog) << "NTRIP: Using last known position for GGA" << coord;
11651190
} else {
11661191
// PRIORITY 5: Hardcoded test coordinates (only if nothing else available)
1167-
coord = QGeoCoordinate(34.112490, -118.339063, 166.421);
1192+
static constexpr double HARD_CODED_LAT = 34.112490; // Los Angeles test latitude
1193+
static constexpr double HARD_CODED_LON = -118.339063; // Los Angeles test longitude
1194+
static constexpr double HARD_CODED_ALT = 166.421; // Altitude in meters
1195+
1196+
coord = QGeoCoordinate(HARD_CODED_LAT, HARD_CODED_LON, HARD_CODED_ALT);
11681197
validCoord = true;
11691198
srcUsed = QStringLiteral("Hardcoded Test");
11701199
static int hardcoded_count = 0;

src/GPS/NTRIP.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class NTRIPTCPLink : public QThread
6969
const QString& whitelist,
7070
bool useSpartn,
7171
QObject* parent = nullptr);
72-
Q_INVOKABLE void debugFetchSourceTable();
72+
73+
Q_INVOKABLE void debugFetchSourceTable();
74+
7375
~NTRIPTCPLink();
7476

7577
public slots:

src/Settings/NTRIPSettings.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ NTRIPSettings::NTRIPSettings(QObject* parent)
6262

6363
// Force ntripServerConnectEnabled to false at every startup, ignoring saved settings
6464
// This ensures NTRIP never auto-starts regardless of previous user state
65-
QTimer::singleShot(0, this, [this]() {
66-
if (ntripServerConnectEnabled()) {
67-
ntripServerConnectEnabled()->setRawValue(false);
68-
}
69-
});
65+
if (ntripServerConnectEnabled()) {
66+
ntripServerConnectEnabled()->setRawValue(false);
67+
}
7068
}
7169

7270
DECLARE_SETTINGSFACT(NTRIPSettings, ntripServerConnectEnabled)

0 commit comments

Comments
 (0)