Skip to content

Commit

Permalink
added more checking to save/restore breadcrumb trail
Browse files Browse the repository at this point in the history
  • Loading branch information
barry-ha committed Apr 30, 2023
1 parent 857911a commit 2d6ebb8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
24 changes: 17 additions & 7 deletions constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ struct FunctionButton {
#define rLOSSOFSIGNAL "LOS"
#define rACQUISITIONOFSIGNAL "AOS"
#define rRESET "\0\0\0"
#define rVALIDATE rGPS rPOWERUP rPOWERDOWN rFIRSTVALIDTIME rLOSSOFSIGNAL rACQUISITIONOFSIGNAL

// Breadcrumb data definition for circular buffer
class Location {
Expand All @@ -225,29 +226,38 @@ class Location {
speed = 0.0;
direction = altitude = 0.0;
}
bool isEmpty() {
bool isEmpty() const {
// we take advantage of the fact that all unused records
// will have reset their recordType field to zeroes, ie, rRESET
return (recordType[0] == 0);
return (recordType[0] == 0) ? true : false;
}

bool isGPS() {
static bool isValidRecordType(const char *rec) {
// check for "should not happen" situations
if (strstr(rVALIDATE, rec)) {
return true;
} else {
return false;
}
}

bool isGPS() const {
return (strncmp(recordType, rGPS, sizeof(recordType)) == 0);
}

bool isPUP() {
bool isPUP() const {
return (strncmp(recordType, rPOWERUP, sizeof(recordType)) == 0);
}

bool isFirstValidTime() {
bool isFirstValidTime() const {
return (strncmp(recordType, rFIRSTVALIDTIME, sizeof(recordType)) == 0);
}

bool isLossOfSignal() {
bool isLossOfSignal() const {
return (strncmp(recordType, rLOSSOFSIGNAL, sizeof(recordType)) == 0);
}

bool isAcquisitionOfSignal() {
bool isAcquisitionOfSignal() const {
return (strncmp(recordType, rACQUISITIONOFSIGNAL, sizeof(recordType)) == 0);
}

Expand Down
45 changes: 28 additions & 17 deletions model_breadcrumbs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ void Breadcrumbs::dumpHistoryGPS(int limit) {
} else {
snprintf(out, sizeof(out), "%d, --> Type '%s' unknown: ", ii, item->recordType);
Serial.print(out);
// 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);
// 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);
}
Serial.println(out);
ii++;
Expand All @@ -119,15 +119,15 @@ int Breadcrumbs::saveGPSBreadcrumbTrail() { // returns 1=success, 0=failure

// line 1,2,3,4: filename, data format, version, compiled
char msg[256];
snprintf(msg, sizeof(msg), "File:,%s\nData format:,%s\nGriduino:,%s\nCompiled:,%s",
snprintf(msg, sizeof(msg), "\nFile:,%s\nData format:,%s\nGriduino:,%s\nCompiled:,%s",
HISTORY_FILE, HISTORY_VERSION, PROGRAM_VERSION, PROGRAM_COMPILED);
config.writeLine(msg);

// line 5: column headings
config.writeLine("Type, GMT Date, GMT Time, Grid, Latitude, Longitude, Altitude, MPH, Direction, Satellites");

// line 6..x: date-time, grid6, latitude, longitude
int ii = 0;
int ii = 0; // loop counter
Location const *loc = begin(); // declare pointer to immutable breadcrumb
while (loc) {

Expand All @@ -137,7 +137,7 @@ int Breadcrumbs::saveGPSBreadcrumbTrail() { // returns 1=success, 0=failure
char sTime[12]; // sizeof("12:34:56") = 8
date.timeToString(sTime, sizeof(sTime), loc->timestamp);

char sGrid6[7];
char sGrid6[7]; // sizeof("CN76us") = 6
grid.calcLocator(sGrid6, loc->loc.lat, loc->loc.lng, 6);

char sLat[12], sLng[12];
Expand All @@ -150,16 +150,25 @@ int Breadcrumbs::saveGPSBreadcrumbTrail() { // returns 1=success, 0=failure
floatToCharArray(sAngle, sizeof(sAngle), loc->direction, 1);
int numSatellites = loc->numSatellites;

if (strlen(loc->recordType)) {
// only write good valid data to file
if (loc->isEmpty()) {
// record type must always contain 3 characters
snprintf(msg, sizeof(msg), "[%d] Empty record type: %s,%s,%s,%s,%s,%s,%s,%s,%s,%d",
ii, loc->recordType, sDate, sTime, sGrid6, sLat, sLng, sAlt, sSpeed, sAngle, numSatellites);
logger.error(msg);

} else if (!Location::isValidRecordType(loc->recordType)) {
// record type must always be one of the allowed 3-char values
snprintf(msg, sizeof(msg), "[%d] Invalid record type: %s,%s,%s,%s,%s,%s,%s,%s,%s,%d",
ii, loc->recordType, sDate, sTime, sGrid6, sLat, sLng, sAlt, sSpeed, sAngle, numSatellites);
logger.error(msg);

} else {
// good data, write it to file
// 1 2 3 4 5 6 7 8 9 10
snprintf(msg, sizeof(msg), "%s,%s,%s,%s,%s,%s,%s,%s,%s,%d",
loc->recordType, sDate, sTime, sGrid6, sLat, sLng, sAlt, sSpeed, sAngle, numSatellites);
config.writeLine(msg);
} else {
// don't allow internal errors to pollute the saved CSV file
snprintf(msg, sizeof(msg), "(%d) Ignoring empty record type: %s,%s,%s,%s,%s,%s,%s,%s,%s,%d",
ii, loc->recordType, sDate, sTime, sGrid6, sLat, sLng, sAlt, sSpeed, sAngle, numSatellites);
logger.error(msg);
}
ii++;
loc = next();
Expand Down Expand Up @@ -228,11 +237,13 @@ int Breadcrumbs::restoreGPSBreadcrumbTrail() { // returns 1=success, 0=failure
uint8_t nSatellites = atoi(strtok(NULL, delimiters));

// echo info for debug
// logger.info(". Source = ", original_line);
// char msg[256];
// snprintf(msg, sizeof(msg), ". Parsed = %s,%02d-%02d-%02d,%02d:%02d:%02d",
// sType, iYear4, iMonth, iDay, iHour, iMinute, iSecond);
// logger.info(msg);
/* *** */
logger.info(". Source = ", original_line);
char msg[256];
snprintf(msg, sizeof(msg), ". Parsed = %s,%02d-%02d-%02d,%02d:%02d:%02d",
sType, iYear4, iMonth, iDay, iHour, iMinute, iSecond);
logger.info(msg);
/* *** */

// save this value from CSV file into breadcrumb trail buffer
PointGPS whereAmI{fLatitude, fLongitude};
Expand Down
36 changes: 19 additions & 17 deletions model_breadcrumbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Breadcrumbs {
// ----- Initialization -----
Breadcrumbs() {} // Constructor - create and initialize member variables

void clearHistory() { // wipe clean the trail of breadcrumbs
void clearHistory() { // wipe clean the in-memory trail of breadcrumbs
head = tail = 0;
full = false;
for (uint ii = 0; ii < capacity; ii++) {
Expand Down Expand Up @@ -154,12 +154,12 @@ class Breadcrumbs {
remember(pup);
}

void rememberAOS(Location vLoc) { // save "acquisition of signal" in history buffer
void rememberAOS(Location vLoc) { // save "acquisition of signal" in history buffer
strncpy(vLoc.recordType, rACQUISITIONOFSIGNAL, sizeof(vLoc.recordType));
remember(vLoc);
}

void rememberLOS(Location vLoc) { // save "loss of signal" in history buffer
void rememberLOS(Location vLoc) { // save "loss of signal" in history buffer
strncpy(vLoc.recordType, rLOSSOFSIGNAL, sizeof(vLoc.recordType));
remember(vLoc);
}
Expand Down Expand Up @@ -205,11 +205,11 @@ class Breadcrumbs {

Location *next() { // returns pointer to next element, or null if buffer is empty
Location *ptr = nullptr;
current = (current + 1) % capacity;
current = (current + 1) % capacity;
if (current == head) {
// nothing returned - end of data
} else {
ptr = &history[current];
ptr = &history[current];
}
return ptr;
}
Expand All @@ -228,21 +228,23 @@ class Breadcrumbs {

// ----- Internal helpers
private:
bool isValidBreadcrumb(const char *crumb) {
bool isValidBreadcrumb(const char *original_line) {
// input: entire line from CSV file
// examine a line from saved breadcrumb trail to see if it's a plausible GPS record type
// clang-format off
if ((strlen(crumb) > 4)
&&(',' == crumb[3])
&&('A' <= crumb[0] && crumb[0] <= 'Z')
&&('A' <= crumb[1] && crumb[1] <= 'Z')
&&('A' <= crumb[2] && crumb[2] <= 'Z'))
{
return true;
} else {
// examine a line from saved history file to see if it's a plausible record
// the goal is to ignore comment lines
if (strlen(original_line) < 5) {
logger.error("- line <5 chars: ", original_line);
return false;
}
// clang-format on

char rec[4];
strncpy(rec, original_line, sizeof(rec));
if (!Location::isValidRecordType(rec)) {
logger.error("- unknown record type: ", rec);
return false;
}

return true;
}

}; // end class Breadcrumbs
6 changes: 5 additions & 1 deletion save_restore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ int SaveRestore::writeConfig(const byte *pData, const unsigned int sizeData) {
*/
int SaveRestoreStrings::open(const char *fqFilename, const char *mode) { // https://cplusplus.com/reference/cstdio/fopen/
// returns 1=success, 0=failure
logger.info("Opening text file system: ", fqFilename);
if (*mode == 'w') {
logger.info("Writing to file system: ", fqFilename);
} else {
logger.info("Opening text file system: ", fqFilename);
}

int result = openFlash(); // open file system and report errors
if (!result) {
Expand Down

0 comments on commit 2d6ebb8

Please sign in to comment.