summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Angelelli <paolo.angelelli@qt.io>2018-06-03 17:19:16 +0200
committerPaolo Angelelli <paolo.angelelli@qt.io>2018-06-06 11:32:22 +0000
commitb2ef357b22eb7b44de98790e2661d3e3dc455d06 (patch)
treee463d45ae024010d6c7c052ca6f9d21a10e79d4a
parent29b3f1eb809fd1c84dfab53e99d7f99b7bfe36cc (diff)
downloadqtlocation-b2ef357b22eb7b44de98790e2661d3e3dc455d06.tar.gz
Refactor QLocationUtils NMEA support
Introduced an enum with currently supported NMEA sentences. NMEA sentence detection factored out in a separate function. Change-Id: Ib63f7bbedad51844c12b532f8d80988227e2f121 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/positioning/qlocationutils.cpp63
-rw-r--r--src/positioning/qlocationutils_p.h36
2 files changed, 67 insertions, 32 deletions
diff --git a/src/positioning/qlocationutils.cpp b/src/positioning/qlocationutils.cpp
index 5304392b..f5062eb6 100644
--- a/src/positioning/qlocationutils.cpp
+++ b/src/positioning/qlocationutils.cpp
@@ -258,6 +258,32 @@ static void qlocationutils_readZda(const char *data, int size, QGeoPositionInfo
info->setTimestamp(QDateTime(date, time, Qt::UTC));
}
+QLocationUtils::NmeaSentence QLocationUtils::getNmeaSentenceType(const char *data, int size)
+{
+ if (size < 6 || data[0] != '$' || !hasValidNmeaChecksum(data, size))
+ return NmeaSentenceInvalid;
+
+ if (data[3] == 'G' && data[4] == 'G' && data[5] == 'A')
+ return NmeaSentenceGGA;
+
+ if (data[3] == 'G' && data[4] == 'S' && data[5] == 'A')
+ return NmeaSentenceGSA;
+
+ if (data[3] == 'G' && data[4] == 'L' && data[5] == 'L')
+ return NmeaSentenceGLL;
+
+ if (data[3] == 'R' && data[4] == 'M' && data[5] == 'C')
+ return NmeaSentenceRMC;
+
+ if (data[3] == 'V' && data[4] == 'T' && data[5] == 'G')
+ return NmeaSentenceVTG;
+
+ if (data[3] == 'Z' && data[4] == 'D' && data[5] == 'A')
+ return NmeaSentenceZDA;
+
+ return NmeaSentenceInvalid;
+}
+
bool QLocationUtils::getPosInfoFromNmea(const char *data, int size, QGeoPositionInfo *info,
double uere, bool *hasFix)
{
@@ -266,7 +292,9 @@ bool QLocationUtils::getPosInfoFromNmea(const char *data, int size, QGeoPosition
if (hasFix)
*hasFix = false;
- if (size < 6 || data[0] != '$' || !hasValidNmeaChecksum(data, size))
+
+ NmeaSentence nmeaType = getNmeaSentenceType(data, size);
+ if (nmeaType == NmeaSentenceInvalid)
return false;
// Adjust size so that * and following characters are not parsed by the following functions.
@@ -277,43 +305,28 @@ bool QLocationUtils::getPosInfoFromNmea(const char *data, int size, QGeoPosition
}
}
- if (data[3] == 'G' && data[4] == 'G' && data[5] == 'A') {
- // "$--GGA" sentence.
+ switch (nmeaType) {
+ case NmeaSentenceGGA:
qlocationutils_readGga(data, size, info, uere, hasFix);
return true;
- }
-
- if (data[3] == 'G' && data[4] == 'S' && data[5] == 'A') {
- // "$--GSA" sentence.
+ case NmeaSentenceGSA:
qlocationutils_readGsa(data, size, info, uere, hasFix);
return true;
- }
-
- if (data[3] == 'G' && data[4] == 'L' && data[5] == 'L') {
- // "$--GLL" sentence.
+ case NmeaSentenceGLL:
qlocationutils_readGll(data, size, info, hasFix);
return true;
- }
-
- if (data[3] == 'R' && data[4] == 'M' && data[5] == 'C') {
- // "$--RMC" sentence.
+ case NmeaSentenceRMC:
qlocationutils_readRmc(data, size, info, hasFix);
return true;
- }
-
- if (data[3] == 'V' && data[4] == 'T' && data[5] == 'G') {
- // "$--VTG" sentence.
+ case NmeaSentenceVTG:
qlocationutils_readVtg(data, size, info, hasFix);
return true;
- }
-
- if (data[3] == 'Z' && data[4] == 'D' && data[5] == 'A') {
- // "$--ZDA" sentence.
+ case NmeaSentenceZDA:
qlocationutils_readZda(data, size, info, hasFix);
return true;
+ default:
+ return false;
}
-
- return false;
}
bool QLocationUtils::hasValidNmeaChecksum(const char *data, int size)
diff --git a/src/positioning/qlocationutils_p.h b/src/positioning/qlocationutils_p.h
index 69cf0fee..d9e6524a 100644
--- a/src/positioning/qlocationutils_p.h
+++ b/src/positioning/qlocationutils_p.h
@@ -54,6 +54,7 @@
#include <math.h> // needed for non-std:: versions of functions
#include <qmath.h>
#include <QtPositioning/QGeoCoordinate>
+#include <QtPositioning/private/qpositioningglobal_p.h>
static const double offsetEpsilon = 1e-12; // = 0.000000000001
static const double leftOffset = -180.0 + offsetEpsilon;
@@ -64,7 +65,7 @@ class QTime;
class QByteArray;
class QGeoPositionInfo;
-class QLocationUtils
+class Q_POSITIONING_PRIVATE_EXPORT QLocationUtils
{
public:
enum CardinalDirection {
@@ -86,6 +87,16 @@ public:
CardinalNNW
};
+ enum NmeaSentence {
+ NmeaSentenceInvalid,
+ NmeaSentenceGGA, // Fix information
+ NmeaSentenceGSA, // Overall Satellite data, such as HDOP and VDOP
+ NmeaSentenceGLL, // Lat/Lon data
+ NmeaSentenceRMC, // Recommended minimum data for gps
+ NmeaSentenceVTG, // Vector track an Speed over the Ground
+ NmeaSentenceZDA // Date and Time
+ };
+
inline static bool isValidLat(double lat) {
return lat >= -90.0 && lat <= 90.0;
}
@@ -246,6 +257,11 @@ public:
}
/*
+ returns the NMEA sentence type.
+ */
+ static NmeaSentence getNmeaSentenceType(const char *data, int size);
+
+ /*
Creates a QGeoPositionInfo from a GGA, GLL, RMC, VTG or ZDA sentence.
Note:
@@ -254,25 +270,31 @@ public:
- RMC reports date with a two-digit year so in this case the year
is assumed to be after the year 2000.
*/
- Q_AUTOTEST_EXPORT static bool getPosInfoFromNmea(const char *data, int size,
- QGeoPositionInfo *info, double uere,
- bool *hasFix = 0);
+ static bool getPosInfoFromNmea(const char *data,
+ int size,
+ QGeoPositionInfo *info, double uere,
+ bool *hasFix = nullptr);
/*
Returns true if the given NMEA sentence has a valid checksum.
*/
- Q_AUTOTEST_EXPORT static bool hasValidNmeaChecksum(const char *data, int size);
+ static bool hasValidNmeaChecksum(const char *data, int size);
/*
Returns time from a string in hhmmss or hhmmss.z+ format.
*/
- Q_AUTOTEST_EXPORT static bool getNmeaTime(const QByteArray &bytes, QTime *time);
+ static bool getNmeaTime(const QByteArray &bytes, QTime *time);
/*
Accepts for example ("2734.7964", 'S', "15306.0124", 'E') and returns the
lat-long values. Fails if lat or long fail isValidLat() or isValidLong().
*/
- Q_AUTOTEST_EXPORT static bool getNmeaLatLong(const QByteArray &latString, char latDirection, const QByteArray &lngString, char lngDirection, double *lat, double *lon);
+ static bool getNmeaLatLong(const QByteArray &latString,
+ char latDirection,
+ const QByteArray &lngString,
+ char lngDirection,
+ double *lat,
+ double *lon);
};
QT_END_NAMESPACE