Skip to content

Commit

Permalink
fixed memory corruption in makeLocation()
Browse files Browse the repository at this point in the history
  • Loading branch information
barry-ha committed May 12, 2023
1 parent ba8b929 commit 6140495
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
18 changes: 10 additions & 8 deletions Griduino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ void announceGrid(const String gridName, int length) {
char grid[7];
strncpy(grid, gridName.c_str(), sizeof(grid));
grid[length] = 0; // null-terminate string to requested 4- or 6-character length
logger.fencepost("Griduino.ino", __LINE__); // debug
Serial.print("Announcing grid: "); Serial.println(grid);

#if defined(ARDUINO_ADAFRUIT_FEATHER_RP2040)
Expand Down Expand Up @@ -877,7 +876,6 @@ void loop() {
TimeElements tm{GPS.seconds, GPS.minute, GPS.hour, 0, GPS.day, GPS.month, (byte)(2000-1970+GPS.year)};
time_t firstTime = makeTime(tm);
trail.rememberFirstValidTime(firstTime, GPS.satellites);
logger.fencepost("Griduino.ino first valid time",__LINE__); // debug
trail.saveGPSBreadcrumbTrail(); // ensure its saved for posterity
}

Expand Down Expand Up @@ -954,7 +952,8 @@ void loop() {
pView->updateScreen();
announceGrid(newGrid6, 4); // announce with Morse code or speech, according to user's config

Location whereAmI = model->makeLocation();
Location whereAmI;
model->makeLocation(&whereAmI);
trail.rememberGPS(whereAmI);
logger.fencepost("Griduino.ino new grid4",__LINE__); // debug
trail.saveGPSBreadcrumbTrail();
Expand All @@ -963,7 +962,8 @@ void loop() {
if (!model->compare4digits) {
announceGrid(newGrid6, 6); // announce with Morse code or speech, according to user's config
}
Location whereAmI = model->makeLocation();
Location whereAmI;
model->makeLocation(&whereAmI);
trail.rememberGPS(whereAmI); // when we enter a new 6-digit grid, save it in breadcrumb trail
logger.fencepost("Griduino.ino new grid6",__LINE__); // debug
trail.saveGPSBreadcrumbTrail(); // because one user's home was barely in the next grid6
Expand All @@ -974,12 +974,12 @@ void loop() {
static PointGPS prevRememberedGPS{0.0, 0.0};
PointGPS currentGPS{model->gLatitude, model->gLongitude};
if (grid.isVisibleDistance(prevRememberedGPS, currentGPS)) {
Location whereAmI = model->makeLocation();
Location whereAmI;
model->makeLocation(&whereAmI);
trail.rememberGPS(whereAmI);
prevRememberedGPS = currentGPS;

if (0 == (trail.getHistoryCount() % trail.saveInterval)) {
logger.fencepost("Griduino.ino distance",__LINE__); // debug
trail.saveGPSBreadcrumbTrail();
}
}
Expand All @@ -988,9 +988,11 @@ void loop() {
if (autoLogTimer > GPS_AUTOSAVE_INTERVAL) {
autoLogTimer = 0;

Location whereAmI = model->makeLocation();
Location whereAmI;
model->makeLocation(&whereAmI);
//logger.fencepost("Griduino.ino autolog timer",__LINE__); // debug
//whereAmI.printLocation(); // debug
trail.rememberGPS(whereAmI);
logger.fencepost("Griduino.ino autolog time",__LINE__); // debug
trail.saveGPSBreadcrumbTrail();
}

Expand Down
11 changes: 3 additions & 8 deletions model_breadcrumbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void Breadcrumbs::dumpHistoryGPS(int limit) {
char sSpeed[12], sDirection[12], sAltitude[12];
floatToCharArray(sSpeed, sizeof(sSpeed), item->speed, 0);
floatToCharArray(sDirection, sizeof(sDirection), item->direction, 0);
floatToCharArray(sAltitude, sizeof(sAltitude), item->altitude, 0);
floatToCharArray(sAltitude, sizeof(sAltitude), item->altitude, 1);
uint8_t nSats = item->numSatellites;

char out[128];
Expand All @@ -92,15 +92,15 @@ void Breadcrumbs::dumpHistoryGPS(int limit) {
// format for all GPS-type messages
// 1 2 3 4 5 6 7 8 9 10 11
snprintf(out, sizeof(out), "%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d",
ii, item->recordType, sDate, sTime, grid6, sLat, sLng, sSpeed, sDirection, sAltitude, nSats);
ii, item->recordType, sDate, sTime, grid6, sLat, sLng, sAltitude, sSpeed, sDirection, nSats);

} else {
// format for "should not happen" messages
snprintf(out, sizeof(out), "%d, --> Type '%s' unknown: ", ii, item->recordType);
Serial.print(out);
// 1 2 3 4 5 6 7 8 9 10
snprintf(out, sizeof(out), "%s, %s, %s, %s, %s, %s, %s, %s, %s, %d",
item->recordType, sDate, sTime, grid6, sLat, sLng, sSpeed, sDirection, sAltitude, nSats);
item->recordType, sDate, sTime, grid6, sLat, sLng, sAltitude, sSpeed, sDirection, nSats);
}
Serial.println(out);
ii++;
Expand Down Expand Up @@ -252,11 +252,6 @@ int Breadcrumbs::restoreGPSBreadcrumbTrail() { // returns 1=success, 0=failure
Location csvloc{"xxx", whereAmI, csvTime, nSatellites, fSpeed, fDirection, fAltitude};
strncpy(csvloc.recordType, sType, sizeof(sType));

// echo info for debug
// snprintf(msg, sizeof(msg), ". CSV string[%2d] = \"%s\"", csv_line_number, original_line); // debug
// Serial.println(msg); // debug
// csvloc.printLocation(csv_line_number); // debug

remember(csvloc);
items_restored++;
} else {
Expand Down
9 changes: 3 additions & 6 deletions model_breadcrumbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
Todo: 1. refactor "class Location" into this file
2. refactor "makeLocation()" into ctor (or public member) of "class Location"
3. replace remember(Location) with remember(a,b,c,d,e,f)
4. add rememberPDN(), rememberTOD()
3. add rememberPDN(), rememberTOD()
API Design:
Initialization:
Expand Down Expand Up @@ -185,8 +184,7 @@ class Breadcrumbs {
strncpy(vLoc.recordType, rGPS, sizeof(vLoc.recordType));
remember(vLoc);
} else {
Serial.print("Bogus GPS date ");
vLoc.printLocation();
vLoc.printLocation("Bogus GPS date ");
}
}

Expand All @@ -196,8 +194,7 @@ class Breadcrumbs {
if (vTime > cutoff) {
remember(gps);
} else {
Serial.print("Bogus GPS date ");
gps.printLocation();
gps.printLocation("Bogus GPS date ");
}
}

Expand Down
32 changes: 21 additions & 11 deletions model_gps.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ class Model {
}

// read GPS hardware
virtual void getGPS() { // "virtual" allows derived class MockModel to replace it
if (GPS.fix) { // DO NOT use "GPS.fix" anywhere else in the program,
// or the simulated position in MockModel won't work correctly
gLatitude = GPS.latitudeDegrees; // double-precision float
gLongitude = GPS.longitudeDegrees;
gAltitude = GPS.altitude;
virtual void getGPS() { // "virtual" allows derived class MockModel to replace it
if (GPS.fix) { // DO NOT use "GPS.fix" anywhere else in the program,
// or the simulated position in MockModel won't work correctly
gLatitude = GPS.latitudeDegrees; // double-precision float
gLongitude = GPS.longitudeDegrees; //
gAltitude = GPS.altitude; // Altitude in meters above MSL
// save timestamp as compact 4-byte integer (number of seconds since Jan 1 1970)
// using https://github.com/PaulStoffregen/Time
// NMEA sentences contain only the last two digits of year, so add the century
Expand All @@ -172,11 +172,19 @@ class Model {
echoGPSinfo(); // send GPS statistics to serial console for debug
}

Location makeLocation() {
void makeLocation(Location *vLoc) {
// collect GPS information from the Model into an object that can be saved in the breadcrumb trail
strncpy(vLoc->recordType, rGPS, sizeof(vLoc->recordType));

PointGPS whereAmI{gLatitude, gLongitude};
Location loc{rGPS, whereAmI, gTimestamp, gSatellites, gSpeed, gAngle, gAltitude};
return loc;
vLoc->loc = whereAmI;

vLoc->timestamp = gTimestamp;
vLoc->numSatellites = gSatellites;
vLoc->speed = gSpeed;
vLoc->direction = gAngle;
vLoc->altitude = gAltitude;
return;
}

// 4-digit grid-crossing detector
Expand Down Expand Up @@ -251,11 +259,13 @@ class Model {
lostFix = true;
gHaveGPSfix = false;
logger.warning("Lost GPS positioning");
Location loc = makeLocation();
Location loc;
makeLocation(&loc);
trail.rememberLOS(loc);
} else if (!gPrevFix && gHaveGPSfix) {
logger.warning("Acquired GPS position lock");
Location loc = makeLocation();
Location loc;
makeLocation(&loc);
trail.rememberAOS(loc);
}
gPrevFix = gHaveGPSfix;
Expand Down

0 comments on commit 6140495

Please sign in to comment.