Skip to content

Commit b00c050

Browse files
GPS.h cleanups round 3. (meshtastic#5447)
* GPS.h cleanups round 3. No effective behavior change. Protected members can be private so make it so. (Supporting subclasses needs a lot more work.) Moves uBloxGnssModelInfo into file scope. Moves uBloxProtocolVersion into uBloxGnssModelInfo. Moves baud rate arrays into file scope. Removes unused/ unimplemented powerStateToString. Signed-off-by: Christopher Hoover <[email protected]> * Trunk Format. --------- Signed-off-by: Christopher Hoover <[email protected]> Co-authored-by: Tom Fifield <[email protected]>
1 parent 0832388 commit b00c050

File tree

2 files changed

+54
-51
lines changed

2 files changed

+54
-51
lines changed

src/gps/GPS.cpp

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#define GPS_RESET_MODE HIGH
2929
#endif
3030

31+
// Not all platforms have std::size().
32+
template <typename T, std::size_t N> std::size_t array_count(const T (&)[N])
33+
{
34+
return N;
35+
}
36+
3137
#if defined(NRF52840_XXAA) || defined(NRF52833_XXAA) || defined(ARCH_ESP32) || defined(ARCH_PORTDUINO)
3238
HardwareSerial *GPS::_serial_gps = &Serial1;
3339
#elif defined(ARCH_RP2040)
@@ -46,8 +52,14 @@ static GPSUpdateScheduling scheduling;
4652
/// only init that port once.
4753
static bool didSerialInit;
4854

49-
static struct uBloxGnssModelInfo info;
50-
static uint8_t uBloxProtocolVersion;
55+
static struct uBloxGnssModelInfo {
56+
char swVersion[30];
57+
char hwVersion[10];
58+
uint8_t extensionNo;
59+
char extension[10][30];
60+
uint8_t protocol_version;
61+
} ublox_info;
62+
5163
#define GPS_SOL_EXPIRY_MS 5000 // in millis. give 1 second time to combine different sentences. NMEA Frequency isn't higher anyway
5264
#define NMEA_MSG_GXGSA "GNGSA" // GSA message (GPGSA, GNGSA etc)
5365

@@ -412,6 +424,15 @@ int GPS::getACK(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t
412424
return 0;
413425
}
414426

427+
#if GPS_BAUDRATE_FIXED
428+
// if GPS_BAUDRATE is specified in variant, only try that.
429+
static const int serialSpeeds[1] = {GPS_BAUDRATE};
430+
static const int rareSerialSpeeds[1] = {GPS_BAUDRATE};
431+
#else
432+
static const int serialSpeeds[3] = {9600, 115200, 38400};
433+
static const int rareSerialSpeeds[3] = {4800, 57600, GPS_BAUDRATE};
434+
#endif
435+
415436
/**
416437
* @brief Setup the GPS based on the model detected.
417438
* We detect the GPS by cycling through a set of baud rates, first common then rare.
@@ -428,7 +449,7 @@ bool GPS::setup()
428449
LOG_DEBUG("Probe for GPS at %d", serialSpeeds[speedSelect]);
429450
gnssModel = probe(serialSpeeds[speedSelect]);
430451
if (gnssModel == GNSS_MODEL_UNKNOWN) {
431-
if (++speedSelect == sizeof(serialSpeeds) / sizeof(int)) {
452+
if (++speedSelect == array_count(serialSpeeds)) {
432453
speedSelect = 0;
433454
++probeTries;
434455
}
@@ -439,7 +460,7 @@ bool GPS::setup()
439460
LOG_DEBUG("Probe for GPS at %d", rareSerialSpeeds[speedSelect]);
440461
gnssModel = probe(rareSerialSpeeds[speedSelect]);
441462
if (gnssModel == GNSS_MODEL_UNKNOWN) {
442-
if (++speedSelect == sizeof(rareSerialSpeeds) / sizeof(int)) {
463+
if (++speedSelect == array_count(rareSerialSpeeds)) {
443464
LOG_WARN("Give up on GPS probe and set to %d", GPS_BAUDRATE);
444465
return true;
445466
}
@@ -635,7 +656,7 @@ bool GPS::setup()
635656
SEND_UBX_PACKET(0x06, 0x01, _message_RMC, "enable NMEA RMC", 500);
636657
SEND_UBX_PACKET(0x06, 0x01, _message_GGA, "enable NMEA GGA", 500);
637658

638-
if (uBloxProtocolVersion >= 18) {
659+
if (ublox_info.protocol_version >= 18) {
639660
clearBuffer();
640661
SEND_UBX_PACKET(0x06, 0x86, _message_PMS, "enable powersave for GPS", 500);
641662
SEND_UBX_PACKET(0x06, 0x3B, _message_CFG_PM2, "enable powersave details for GPS", 500);
@@ -1131,7 +1152,7 @@ GnssModel_t GPS::probe(int serialSpeed)
11311152
}
11321153
#endif
11331154

1134-
memset(&info, 0, sizeof(struct uBloxGnssModelInfo));
1155+
memset(&ublox_info, 0, sizeof(ublox_info));
11351156
uint8_t buffer[768] = {0};
11361157
delay(100);
11371158

@@ -1198,64 +1219,64 @@ GnssModel_t GPS::probe(int serialSpeed)
11981219
if (len) {
11991220
uint16_t position = 0;
12001221
for (int i = 0; i < 30; i++) {
1201-
info.swVersion[i] = buffer[position];
1222+
ublox_info.swVersion[i] = buffer[position];
12021223
position++;
12031224
}
12041225
for (int i = 0; i < 10; i++) {
1205-
info.hwVersion[i] = buffer[position];
1226+
ublox_info.hwVersion[i] = buffer[position];
12061227
position++;
12071228
}
12081229

12091230
while (len >= position + 30) {
12101231
for (int i = 0; i < 30; i++) {
1211-
info.extension[info.extensionNo][i] = buffer[position];
1232+
ublox_info.extension[ublox_info.extensionNo][i] = buffer[position];
12121233
position++;
12131234
}
1214-
info.extensionNo++;
1215-
if (info.extensionNo > 9)
1235+
ublox_info.extensionNo++;
1236+
if (ublox_info.extensionNo > 9)
12161237
break;
12171238
}
12181239

12191240
LOG_DEBUG("Module Info : ");
1220-
LOG_DEBUG("Soft version: %s", info.swVersion);
1221-
LOG_DEBUG("Hard version: %s", info.hwVersion);
1222-
LOG_DEBUG("Extensions:%d", info.extensionNo);
1223-
for (int i = 0; i < info.extensionNo; i++) {
1224-
LOG_DEBUG(" %s", info.extension[i]);
1241+
LOG_DEBUG("Soft version: %s", ublox_info.swVersion);
1242+
LOG_DEBUG("Hard version: %s", ublox_info.hwVersion);
1243+
LOG_DEBUG("Extensions:%d", ublox_info.extensionNo);
1244+
for (int i = 0; i < ublox_info.extensionNo; i++) {
1245+
LOG_DEBUG(" %s", ublox_info.extension[i]);
12251246
}
12261247

12271248
memset(buffer, 0, sizeof(buffer));
12281249

12291250
// tips: extensionNo field is 0 on some 6M GNSS modules
1230-
for (int i = 0; i < info.extensionNo; ++i) {
1231-
if (!strncmp(info.extension[i], "MOD=", 4)) {
1232-
strncpy((char *)buffer, &(info.extension[i][4]), sizeof(buffer));
1233-
} else if (!strncmp(info.extension[i], "PROTVER", 7)) {
1251+
for (int i = 0; i < ublox_info.extensionNo; ++i) {
1252+
if (!strncmp(ublox_info.extension[i], "MOD=", 4)) {
1253+
strncpy((char *)buffer, &(ublox_info.extension[i][4]), sizeof(buffer));
1254+
} else if (!strncmp(ublox_info.extension[i], "PROTVER", 7)) {
12341255
char *ptr = nullptr;
12351256
memset(buffer, 0, sizeof(buffer));
1236-
strncpy((char *)buffer, &(info.extension[i][8]), sizeof(buffer));
1257+
strncpy((char *)buffer, &(ublox_info.extension[i][8]), sizeof(buffer));
12371258
LOG_DEBUG("Protocol Version:%s", (char *)buffer);
12381259
if (strlen((char *)buffer)) {
1239-
uBloxProtocolVersion = strtoul((char *)buffer, &ptr, 10);
1240-
LOG_DEBUG("ProtVer=%d", uBloxProtocolVersion);
1260+
ublox_info.protocol_version = strtoul((char *)buffer, &ptr, 10);
1261+
LOG_DEBUG("ProtVer=%d", ublox_info.protocol_version);
12411262
} else {
1242-
uBloxProtocolVersion = 0;
1263+
ublox_info.protocol_version = 0;
12431264
}
12441265
}
12451266
}
1246-
if (strncmp(info.hwVersion, "00040007", 8) == 0) {
1267+
if (strncmp(ublox_info.hwVersion, "00040007", 8) == 0) {
12471268
LOG_INFO(DETECTED_MESSAGE, "U-blox 6", "6");
12481269
return GNSS_MODEL_UBLOX6;
1249-
} else if (strncmp(info.hwVersion, "00070000", 8) == 0) {
1270+
} else if (strncmp(ublox_info.hwVersion, "00070000", 8) == 0) {
12501271
LOG_INFO(DETECTED_MESSAGE, "U-blox 7", "7");
12511272
return GNSS_MODEL_UBLOX7;
1252-
} else if (strncmp(info.hwVersion, "00080000", 8) == 0) {
1273+
} else if (strncmp(ublox_info.hwVersion, "00080000", 8) == 0) {
12531274
LOG_INFO(DETECTED_MESSAGE, "U-blox 8", "8");
12541275
return GNSS_MODEL_UBLOX8;
1255-
} else if (strncmp(info.hwVersion, "00190000", 8) == 0) {
1276+
} else if (strncmp(ublox_info.hwVersion, "00190000", 8) == 0) {
12561277
LOG_INFO(DETECTED_MESSAGE, "U-blox 9", "9");
12571278
return GNSS_MODEL_UBLOX9;
1258-
} else if (strncmp(info.hwVersion, "000A0000", 8) == 0) {
1279+
} else if (strncmp(ublox_info.hwVersion, "000A0000", 8) == 0) {
12591280
LOG_INFO(DETECTED_MESSAGE, "U-blox 10", "10");
12601281
return GNSS_MODEL_UBLOX10;
12611282
}
@@ -1729,4 +1750,4 @@ void GPS::toggleGpsMode()
17291750
enable();
17301751
}
17311752
}
1732-
#endif // Exclude GPS
1753+
#endif // Exclude GPS

src/gps/GPS.h

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
#define GPS_EN_ACTIVE 1
1717
#endif
1818

19-
struct uBloxGnssModelInfo {
20-
char swVersion[30];
21-
char hwVersion[10];
22-
uint8_t extensionNo;
23-
char extension[10][30];
24-
};
25-
2619
typedef enum {
2720
GNSS_MODEL_ATGM336H,
2821
GNSS_MODEL_MTK,
@@ -119,7 +112,9 @@ class GPS : private concurrency::OSThread
119112
// Let the GPS hardware save power between updates
120113
void down();
121114

122-
protected:
115+
private:
116+
GPS() : concurrency::OSThread("GPS") {}
117+
123118
/// Record that we have a GPS
124119
void setConnected();
125120

@@ -147,9 +142,6 @@ class GPS : private concurrency::OSThread
147142

148143
GnssModel_t gnssModel = GNSS_MODEL_UNKNOWN;
149144

150-
private:
151-
GPS() : concurrency::OSThread("GPS") {}
152-
153145
TinyGPSPlus reader;
154146
uint8_t fixQual = 0; // fix quality from GPGGA
155147
uint32_t lastChecksumFailCount = 0;
@@ -161,14 +153,6 @@ class GPS : private concurrency::OSThread
161153
TinyGPSCustom gsapdop; // custom extract PDOP from GPGSA
162154
uint8_t fixType = 0; // fix type from GPGSA
163155
#endif
164-
#if GPS_BAUDRATE_FIXED
165-
// if GPS_BAUDRATE is specified in variant, only try that.
166-
const int serialSpeeds[1] = {GPS_BAUDRATE};
167-
const int rareSerialSpeeds[1] = {GPS_BAUDRATE};
168-
#else
169-
const int serialSpeeds[3] = {9600, 115200, 38400};
170-
const int rareSerialSpeeds[3] = {4800, 57600, GPS_BAUDRATE};
171-
#endif
172156

173157
uint32_t lastWakeStartMsec = 0, lastSleepStartMsec = 0, lastFixStartMsec = 0;
174158
uint32_t rx_gpio = 0;
@@ -252,8 +236,6 @@ class GPS : private concurrency::OSThread
252236

253237
// delay counter to allow more sats before fixed position stops GPS thread
254238
uint8_t fixeddelayCtr = 0;
255-
256-
const char *powerStateToString();
257239
};
258240

259241
extern GPS *gps;

0 commit comments

Comments
 (0)