From 816301be515ea3208019522e1aed6c7d919e3562 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 1 Dec 2018 19:08:57 +1300 Subject: [PATCH 01/17] Updated GpsTracker with recent changes from sim808 (cherry picked from commit 275bc4668896c338eac98d3bf69a190259665658) --- src/Core.cpp | 4 ++-- src/Gps.cpp | 4 ++-- src/Gps.h | 2 +- src/Hardware.cpp | 4 ++-- src/Network.cpp | 10 +++++----- src/Network.h | 2 +- src/NetworkPositionsBackup.cpp | 4 ++-- src/Positions.h | 2 +- src/config/System.h | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Core.cpp b/src/Core.cpp index f67898f..a5fed38 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -61,7 +61,7 @@ namespace core { } uint8_t notifyFailures(PositionEntryMetadata &metadata) { - SIM808RegistrationStatus networkStatus; + SIM808_NETWORK_REGISTRATION_STATE networkStatus; char buffer[SMS_BUFFER_SIZE]; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?"); bool notified = false; @@ -74,7 +74,7 @@ namespace core { network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (network::isAvailable(networkStatus.stat)) { + if (network::isAvailable(networkStatus)) { strncpy_P(buffer, PSTR("Alerts !"), SMS_BUFFER_SIZE); if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { details::appendToSmsBuffer(buffer, PSTR("\n- Battery at %d%%."), metadata.batteryLevel); diff --git a/src/Gps.cpp b/src/Gps.cpp index 3529cf1..013fe33 100644 --- a/src/Gps.cpp +++ b/src/Gps.cpp @@ -35,7 +35,7 @@ namespace gps { SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; do { - currentStatus = hardware::sim808::device.getGpsStatus(lastPosition); + currentStatus = hardware::sim808::device.getGpsStatus(lastPosition, GPS_POSITION_SIZE); if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); @@ -90,7 +90,7 @@ namespace gps { } uint8_t getVelocity() { - uint8_t velocity; + int16_t velocity; if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0; VERBOSE_FORMAT("getVelocity", "%d", velocity); diff --git a/src/Gps.h b/src/Gps.h index aac10d2..7fbd585 100644 --- a/src/Gps.h +++ b/src/Gps.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include "Hardware.h" #include "Time2.h" diff --git a/src/Hardware.cpp b/src/Hardware.cpp index 246cff2..99d360b 100644 --- a/src/Hardware.cpp +++ b/src/Hardware.cpp @@ -59,7 +59,7 @@ namespace hardware { //SIM808 turns phone on by default but we don't need it for gps only device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); } - device.enableGps(); + device.powerOnOffGps(true); } void gpsPowerOff() { @@ -74,7 +74,7 @@ namespace hardware { } VERBOSE("gpsPowerOff"); - device.disableGps(); + device.powerOnOffGps(false); powerOffIfUnused(); } diff --git a/src/Network.cpp b/src/Network.cpp index 0cab1c0..463d380 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -22,9 +22,9 @@ namespace network { } __attribute__((__optimize__("O2"))) - SIM808RegistrationStatus waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { + SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { - SIM808RegistrationStatus currentStatus; + SIM808_NETWORK_REGISTRATION_STATE currentStatus; SIM808SignalQualityReport report; uint8_t noReliableNetwork = 0; @@ -34,9 +34,9 @@ namespace network { report = hardware::sim808::device.getSignalQuality(); do { - if (isAvailable(currentStatus.stat)) break; + if (isAvailable(currentStatus)) break; - NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.rssi, report.attenuation); + NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); if (report.rssi < NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++; else noReliableNetwork = 0; @@ -52,7 +52,7 @@ namespace network { report = hardware::sim808::device.getSignalQuality(); } while (timeout > 1); - NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.rssi, report.attenuation); + NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); return currentStatus; } diff --git a/src/Network.h b/src/Network.h index 555e20e..061c3c4 100644 --- a/src/Network.h +++ b/src/Network.h @@ -8,7 +8,7 @@ namespace network { void powerOn(); void powerOff(); - SIM808RegistrationStatus waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true); + SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true); bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); #if BACKUP_ENABLE_NETWORK bool enableGprs(); diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index 4b90370..183c9e2 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -57,7 +57,7 @@ namespace positions { void NetworkPositionsBackup::appendPositions() { uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; - SIM808RegistrationStatus networkStatus; + SIM808_NETWORK_REGISTRATION_STATE networkStatus; //avoid edge case where if 0, whole set of positions will be sent again if (!positions::count(config::main::value.network.lastSavedEntry)) return; @@ -65,7 +65,7 @@ namespace positions { network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus.stat) || !network::enableGprs()) { + if (!network::isAvailable(networkStatus) || !network::enableGprs()) { networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow NOTICE_MSG("appendPositions", "network or gprs unavailable"); diff --git a/src/Positions.h b/src/Positions.h index bc552f5..268a9b5 100644 --- a/src/Positions.h +++ b/src/Positions.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #define POSITION_SIZE 115 diff --git a/src/config/System.h b/src/config/System.h index 331672c..c82e618 100644 --- a/src/config/System.h +++ b/src/config/System.h @@ -20,7 +20,7 @@ \def VERSION Version string, only used for indicative purpose */ -#define VERSION "1.30" +#define VERSION "1.40" /** From 48a691c47d56be5be73d8a83accf202874a70650 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 2 Dec 2018 17:07:06 +1300 Subject: [PATCH 02/17] Fixed distance from previous debug messages --- src/Gps.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Gps.cpp b/src/Gps.cpp index 013fe33..58001ef 100644 --- a/src/Gps.cpp +++ b/src/Gps.cpp @@ -67,7 +67,7 @@ namespace gps { if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LATITUDE, &lat2)) return 0; if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LONGITUDE, &lng2)) return 0; - VERBOSE_FORMAT("distanceFromPrevious", "%s, %f, %f, %f, %f", lastPosition, previousLat, previousLng, lat2, lng2); + VERBOSE_FORMAT("distanceFromPrevious", "%s, %F, %F, %F, %F", lastPosition, previousLat, previousLng, lat2, lng2); lat1 = radians(previousLat); lng1 = radians(previousLng); @@ -85,7 +85,7 @@ namespace gps { a = EARTH_RADIUS * (2 * atan2(sqrt(a), sqrt(1 - a))); //kilometers - NOTICE_FORMAT("distanceFromPrevious", "%fkm", a); + NOTICE_FORMAT("distanceFromPrevious", "%Fkm", a); return a; } From c417f4198fce98198a96a7ec2ef658f1f575a5e0 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 2 Dec 2018 18:23:17 +1300 Subject: [PATCH 03/17] Rework log defines for clarity --- src/Alerts.cpp | 3 +-- src/Config.cpp | 19 +++++++++------ src/Core.cpp | 18 +++++++++----- src/Debug.cpp | 44 ++++++++++++++++++++++------------ src/Gps.cpp | 27 ++++++++++++++------- src/GpsTracker.ino | 5 ++-- src/Hardware.cpp | 42 ++++++++++++++++++++++---------- src/Logging.h | 42 +++++++++++++++++++------------- src/MainUnit.cpp | 10 +++++--- src/Network.cpp | 14 +++++------ src/NetworkPositionsBackup.cpp | 21 ++++++++++------ src/Positions.cpp | 21 +++++++++------- src/Rtc.cpp | 17 +++++++++---- src/SdPositionsBackup.cpp | 15 +++++++----- src/SdPositionsConfig.cpp | 15 ++++++++---- 15 files changed, 202 insertions(+), 111 deletions(-) diff --git a/src/Alerts.cpp b/src/Alerts.cpp index 65a4049..da53a73 100644 --- a/src/Alerts.cpp +++ b/src/Alerts.cpp @@ -5,9 +5,8 @@ #include "Rtc.h" #include "Logging.h" -#define LOGGER_NAME "Alerts" - namespace alerts { + #define CURRENT_LOGGER "alerts" uint8_t getTriggered(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; diff --git a/src/Config.cpp b/src/Config.cpp index 3338e9e..f0a2025 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -2,9 +2,10 @@ #include "Hardware.h" #include "Logging.h" -#define LOGGER_NAME "Config" namespace config { + #define CURRENT_LOGGER "config" + namespace main { config_t value; @@ -12,16 +13,17 @@ namespace config { namespace details { void read() { - VERBOSE("read"); + #define CURRENT_LOGGER_FUNCTION "read" + VERBOSE; hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); - NOTICE_FORMAT("read", "%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); #if BACKUP_ENABLE_NETWORK - NOTICE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); //networkConfig_t c = { // POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, // 0xFFFF, @@ -39,9 +41,11 @@ namespace config { } void write() { - NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + #define CURRENT_LOGGER_FUNCTION "write" + + NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); #if BACKUP_ENABLE_NETWORK - NOTICE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); + NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); #endif hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); @@ -63,7 +67,8 @@ namespace config { } void reset() { - VERBOSE("reset"); + #define CURRENT_LOGGER_FUNCTION "reset" + VERBOSE; config_t config = {}; config.seed = CONFIG_SEED; diff --git a/src/Core.cpp b/src/Core.cpp index a5fed38..ce789a8 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -8,13 +8,13 @@ #include "Alerts.h" #include "Logging.h" -#define LOGGER_NAME "Core" #define SMS_BUFFER_SIZE 140 #define NO_ALERTS_NOTIFIED 0 using namespace utils; namespace core { + #define CURRENT_LOGGER "core" uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS; uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1; @@ -61,6 +61,8 @@ namespace core { } uint8_t notifyFailures(PositionEntryMetadata &metadata) { + #define CURRENT_LOGGER_FUNCTION "notifyFailures" + SIM808_NETWORK_REGISTRATION_STATE networkStatus; char buffer[SMS_BUFFER_SIZE]; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?"); @@ -69,7 +71,7 @@ namespace core { uint8_t triggered = alerts::getTriggered(metadata); if (!triggered) return NO_ALERTS_NOTIFIED; - NOTICE_FORMAT("notifyFailures", "triggered : %B", triggered); + NOTICE_FORMAT("triggered : %B", triggered); network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); @@ -89,12 +91,12 @@ namespace core { } #if ALERTS_ON_SERIAL - NOTICE_FORMAT("notifyFailure", "%s", buffer); + NOTICE_FORMAT("%s", buffer); notified = true; #else notified = network::sendSms(buffer); #endif - if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); + if (!notified) NOTICE_MSG("SMS not sent !"); } network::powerOff(); @@ -108,6 +110,8 @@ namespace core { } TRACKER_MOVING_STATE updateSleepTime() { + #define CURRENT_LOGGER_FUNCTION "updateSleepTime" + TRACKER_MOVING_STATE state = TRACKER_MOVING_STATE::MOVING; uint8_t velocity = gps::getVelocity(); @@ -127,11 +131,13 @@ namespace core { } else stoppedInARow = 0; - NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime); + NOTICE_FORMAT("%dkmh => %d seconds", velocity, sleepTime); return state; } uint16_t mapSleepTime(uint8_t velocity) { + #define CURRENT_LOGGER_FUNCTION "mapSleepTime" + uint16_t result; uint16_t currentTime = 0xFFFF; @@ -154,7 +160,7 @@ namespace core { } - VERBOSE_FORMAT("mapSleepTime", "%d,%d", velocity, result); + VERBOSE_FORMAT("%d,%d", velocity, result); return result; } } \ No newline at end of file diff --git a/src/Debug.cpp b/src/Debug.cpp index 9254119..e23f953 100644 --- a/src/Debug.cpp +++ b/src/Debug.cpp @@ -9,8 +9,6 @@ #include "Alerts.h" #include "Logging.h" -#define LOGGER_NAME "Debug" - #define MENU_ENTRY(name, text) const char MENU_##name[] PROGMEM = text const char FAKE_GPS_ENTRY[] PROGMEM = "1,1,20170924184842.000,49.454862,1.144537,71.900,2.70,172.6,1,,1.3,2.2,1.8,,11,7,,,37,,"; @@ -116,10 +114,12 @@ static const PositionEntryMetadata fakeMetadata PROGMEM = { using namespace utils; namespace debug { + #define CURRENT_LOGGER "debug" namespace details { inline void displayPosition(PositionEntry entry) { - Log.notice(F("%d,%d,%d,%d,%d,%s"), + #define CURRENT_LOGGER_FUNCTION "displayPosition" + NOTICE_FORMAT("%d,%d,%d,%d,%d,%s", entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, @@ -130,7 +130,8 @@ namespace debug { } void displayFreeRam() { - Log.notice(F("RAM: %d\n"), mainunit::freeRam()); + #define CURRENT_LOGGER_FUNCTION "displayFreeRam" + NOTICE_FORMAT("%d", mainunit::freeRam()); } GPSTRACKER_DEBUG_COMMAND parseCommand(char id) { @@ -146,6 +147,8 @@ namespace debug { } GPSTRACKER_DEBUG_COMMAND menu(uint16_t timeout) { + #define CURRENT_LOGGER_FUNCTION "menu" + GPSTRACKER_DEBUG_COMMAND command; size_t menuSize = flash::getArraySize(MENU_ENTRIES); @@ -159,7 +162,7 @@ namespace debug { delay(MENU_INTERMEDIATE_TIMEOUT); timeout -= MENU_INTERMEDIATE_TIMEOUT; if (timeout <= 0) { - NOTICE_MSG("menu", "Timeout expired."); + NOTICE_MSG("Timeout expired."); return GPSTRACKER_DEBUG_COMMAND::RUN; } } @@ -172,32 +175,36 @@ namespace debug { } void getAndDisplayGpsPosition() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayGpsPosition" SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); - NOTICE_FORMAT("getAndDisplayGpsPosition", "%d %s", gpsStatus, gps::lastPosition); + NOTICE_FORMAT("%d %s", gpsStatus, gps::lastPosition); } void setFakeGpsPosition() { + #define CURRENT_LOGGER_FUNCTION "setFakeGpsPosition" strlcpy_P(gps::lastPosition, FAKE_GPS_ENTRY, GPS_POSITION_SIZE); - NOTICE_FORMAT("setFakeGpsPosition", "Last position set to : %s", gps::lastPosition); - NOTICE_FORMAT("setFakeGpsPosition", "Speed : %d", gps::getVelocity()); - NOTICE_FORMAT("setFakeGpsPosition", "Sleep time : %d", core::mapSleepTime(gps::getVelocity())); + NOTICE_FORMAT("Last position set to : %s", gps::lastPosition); + NOTICE_FORMAT("Speed : %d", gps::getVelocity()); + NOTICE_FORMAT("Sleep time : %d", core::mapSleepTime(gps::getVelocity())); } void getAndDisplayBattery() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayBattery" hardware::sim808::powerOn(); SIM808ChargingStatus status = hardware::sim808::device.getChargingState(); hardware::sim808::powerOff(); - NOTICE_FORMAT("getAndDisplayBattery", "%d %d%% %dmV", status.state, status.level, status.voltage); + NOTICE_FORMAT("%d %d%% %dmV", status.state, status.level, status.voltage); } void getAndDisplayRtcTime() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayRtcTime" tmElements_t time; rtc::getTime(time); - NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d %t %d", time.year, time.month, time.day, time.hour, time.minute, time.second, rtc::isAccurate(), rtc::getTemperature()); + NOTICE_FORMAT("%d/%d/%d %d:%d:%d %t %d", time.year, time.month, time.day, time.hour, time.minute, time.second, rtc::isAccurate(), rtc::getTemperature()); } void setRtcTime() { @@ -207,6 +214,8 @@ namespace debug { } void getAndDisplaySleepTimes() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplaySleepTimes" + size_t arraySize = flash::getArraySize(config::defaultSleepTimings); sleepTimings_t maxSpeedTiming; utils::flash::read(&config::defaultSleepTimings[arraySize - 1], maxSpeedTiming); @@ -215,7 +224,7 @@ namespace debug { core::mapSleepTime(i); } - NOTICE_MSG("getAndDisplaySleepTimes", "Done"); + NOTICE_MSG("Done"); } void getAndDisplayEepromConfig() { @@ -223,6 +232,8 @@ namespace debug { } void getAndDisplayEepromContent() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayEepromContent" + char buffer[128]; hardware::i2c::powerOn(); @@ -235,7 +246,7 @@ namespace debug { Serial.println(); hardware::i2c::powerOff(); - NOTICE_MSG("getAndDisplayEepromContent", "Done"); + NOTICE_MSG("Done"); } void getAndDisplayEepromPositions() { @@ -251,11 +262,12 @@ namespace debug { } void getAndDisplayEepromLastPosition() { + #define CURRENT_LOGGER_FUNCTION "getAndDisplayEepromLastPosition" uint16_t lastEntryIndex = config::main::value.lastEntry; PositionEntry lastEntry; if(positions::get(lastEntryIndex, lastEntry)) details::displayPosition(lastEntry); - else Log.notice(F("No position recorded\n")); + else NOTICE_MSG("No position recorded"); } void addLastPositionToEeprom() { @@ -275,13 +287,15 @@ namespace debug { } void notifyFailures() { + #define CURRENT_LOGGER_FUNCTION "notifyFailures" + PositionEntryMetadata metadata = {}; flash::read(&fakeMetadata, metadata); metadata.batteryLevel = 1; metadata.temperature = ALERT_SUSPICIOUS_RTC_TEMPERATURE; uint8_t alerts = core::notifyFailures(metadata); - NOTICE_FORMAT("notifyFailures", "result : %B", alerts); + NOTICE_FORMAT("result : %B", alerts); alerts::add(alerts); } diff --git a/src/Gps.cpp b/src/Gps.cpp index 58001ef..c53dde8 100644 --- a/src/Gps.cpp +++ b/src/Gps.cpp @@ -5,8 +5,6 @@ #include "MainUnit.h" #include "Logging.h" -#define LOGGER_NAME "Gps" - #define TIME_YEAR_OFFSET 0 #define TIME_MONTH_OFFSET 4 #define TIME_DAY_OFFSET 6 @@ -16,7 +14,9 @@ #define EARTH_RADIUS 6371 //kilometers + namespace gps { + #define CURRENT_LOGGER "gps" namespace details { uint16_t parseSubstring(char *buffer, char *start, uint8_t size) { @@ -25,6 +25,7 @@ namespace gps { } } + char lastPosition[GPS_POSITION_SIZE]; SIM808_GPS_STATUS lastStatus; @@ -32,13 +33,15 @@ namespace gps { float previousLng = 0; SIM808_GPS_STATUS acquireCurrentPosition(int32_t timeout) { + #define CURRENT_LOGGER_FUNCTION "acquireCurrentPosition" + SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF; do { currentStatus = hardware::sim808::device.getGpsStatus(lastPosition, GPS_POSITION_SIZE); if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now - NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); + NOTICE_FORMAT("%d", currentStatus); mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS; } while (timeout > 1); @@ -47,7 +50,7 @@ namespace gps { lastStatus = currentStatus; } - NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus); + NOTICE_FORMAT("%d", currentStatus); return currentStatus; } @@ -62,12 +65,14 @@ namespace gps { } float getDistanceFromPrevious() { + #define CURRENT_LOGGER_FUNCTION "getDistanceFromPrevious" + float lat1, lng1, lat2, lng2; if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LATITUDE, &lat2)) return 0; if(!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::LONGITUDE, &lng2)) return 0; - VERBOSE_FORMAT("distanceFromPrevious", "%s, %F, %F, %F, %F", lastPosition, previousLat, previousLng, lat2, lng2); + VERBOSE_FORMAT("%s, %F, %F, %F, %F", lastPosition, previousLat, previousLng, lat2, lng2); lat1 = radians(previousLat); lng1 = radians(previousLng); @@ -85,25 +90,29 @@ namespace gps { a = EARTH_RADIUS * (2 * atan2(sqrt(a), sqrt(1 - a))); //kilometers - NOTICE_FORMAT("distanceFromPrevious", "%Fkm", a); + NOTICE_FORMAT("%Fkm", a); return a; } uint8_t getVelocity() { + #define CURRENT_LOGGER_FUNCTION "getVelocity" + int16_t velocity; if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0; - VERBOSE_FORMAT("getVelocity", "%d", velocity); + VERBOSE_FORMAT("%d", velocity); return velocity; } void getTime(tmElements_t &time) { + #define CURRENT_LOGGER_FUNCTION "getTime" + char *timeStr; char buffer[5]; hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr); - VERBOSE_FORMAT("getTime", "%s", timeStr); + VERBOSE_FORMAT("%s", timeStr); time.year = details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4); time.month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2); @@ -112,6 +121,6 @@ namespace gps { time.minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2); time.second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2); - NOTICE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); + NOTICE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); } } \ No newline at end of file diff --git a/src/GpsTracker.ino b/src/GpsTracker.ino index 28332fb..d691f89 100644 --- a/src/GpsTracker.ino +++ b/src/GpsTracker.ino @@ -10,7 +10,7 @@ #include "Positions.h" #include "Logging.h" -#define LOGGER_NAME "GpsTracker" +#define CURRENT_LOGGER "GpsTracker" bool bypassMenu = false; uint16_t menuTimeout = MENU_TIMEOUT; @@ -26,6 +26,7 @@ void setup() { } void loop() { + #define CURRENT_LOGGER_FUNCTION "loop" debug::GPSTRACKER_DEBUG_COMMAND command = debug::GPSTRACKER_DEBUG_COMMAND::RUN; if (Serial && !bypassMenu) command = debug::menu(menuTimeout); @@ -95,6 +96,6 @@ void loop() { mainunit::deepSleep(10); break; default: - NOTICE_MSG("loop", "Unsupported"); + NOTICE_MSG("Unsupported"); } } diff --git a/src/Hardware.cpp b/src/Hardware.cpp index 99d360b..0c8355c 100644 --- a/src/Hardware.cpp +++ b/src/Hardware.cpp @@ -5,16 +5,18 @@ namespace hardware { -#define LOGGER_NAME "Hardware::sim808" - namespace sim808 { + #define CURRENT_LOGGER "hardware::sim808" + SIM_SERIAL_TYPE simSerial = SIM_SERIAL; SIM808 device = SIM808(SIM_RST, SIM_PWR, SIM_STATUS); uint8_t networkPoweredCount = 0; uint8_t gpsPoweredCount = 0; void powerOn() { - VERBOSE("powerOn"); + #define CURRENT_LOGGER_FUNCTION "powerOn" + VERBOSE; + bool poweredOn = device.powerOnOff(true); if (!poweredOn) return; @@ -23,7 +25,9 @@ namespace hardware { } void powerOff() { - VERBOSE("powerOff"); + #define CURRENT_LOGGER_FUNCTION "powerOff" + VERBOSE; + device.powerOnOff(false); networkPoweredCount = gpsPoweredCount = 0; } @@ -41,19 +45,22 @@ namespace hardware { } void setup() { - NOTICE("setup"); + #define CURRENT_LOGGER_FUNCTION "setup" + NOTICE; + simSerial.begin(SIM808_BAUDRATE); device.begin(simSerial); powerOff(); //ensure powerOff on start } void gpsPowerOn() { + #define CURRENT_LOGGER_FUNCTION "gpsPowerOn" if(gpsPoweredCount) { gpsPoweredCount++; return; } - VERBOSE("gpsPowerOn"); + VERBOSE; powerOn(); if(!networkPoweredCount) { //SIM808 turns phone on by default but we don't need it for gps only @@ -63,6 +70,8 @@ namespace hardware { } void gpsPowerOff() { + #define CURRENT_LOGGER_FUNCTION "gpsPowerOff" + if (!device.powered()) { networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0 return; @@ -73,23 +82,27 @@ namespace hardware { return; } - VERBOSE("gpsPowerOff"); + VERBOSE; device.powerOnOffGps(false); powerOffIfUnused(); } void networkPowerOn() { + #define CURRENT_LOGGER_FUNCTION "networkPowerOn" + if(networkPoweredCount) { networkPoweredCount++; return; } - VERBOSE("networkPowerOn"); + VERBOSE; powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); } void networkPowerOff() { + #define CURRENT_LOGGER_FUNCTION "networkPowerOff" + if (!device.powered()) { networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0 return; @@ -100,27 +113,28 @@ namespace hardware { return; } - VERBOSE("networkPowerOff"); + VERBOSE; device.disableGprs(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); powerOffIfUnused(); } } -#define LOGGER_NAME "Hardware::i2c" - namespace i2c { + #define CURRENT_LOGGER "hardware::i2c" E24 eeprom = E24(E24_SIZE, E24_ADDRESS); uint8_t poweredCount = 0; void powerOn() { + #define CURRENT_LOGGER_FUNCTION "powerOn" + if(poweredCount) { poweredCount++; return; } - VERBOSE("powerOn"); + VERBOSE; digitalWrite(I2C_PWR, HIGH); pinMode(I2C_PWR, OUTPUT); @@ -129,12 +143,14 @@ namespace hardware { } void powerOff(bool forced = false) { + #define CURRENT_LOGGER_FUNCTION "powerOff" + if(poweredCount > 1 && !forced) { poweredCount--; return; } - VERBOSE("powerOff"); + VERBOSE; pinMode(I2C_PWR, INPUT); digitalWrite(I2C_PWR, LOW); diff --git a/src/Logging.h b/src/Logging.h index 6955805..6cef01a 100644 --- a/src/Logging.h +++ b/src/Logging.h @@ -2,33 +2,43 @@ //#define DISABLE_LOGGING 1 #include +#include //reusing flash string utilities #include "Config.h" #define LOG_SERIAL_SPEED 115200 #if _DEBUG -#define LOG_LEVEL LOG_LEVEL_VERBOSE + #define LOG_LEVEL LOG_LEVEL_VERBOSE + #define LOG_LEVEL_VERBOSE_ENABLED + #define LOG_LEVEL_NOTICE_ENABLED #else -#define LOG_LEVEL LOG_LEVEL_NOTICE + #define LOG_LEVEL LOG_LEVEL_NOTICE + #define LOG_LEVEL_NOTICE_ENABLED #endif -#define LOG(level, f) Log.level(F("[" LOGGER_NAME "::" f "]\n")) -#define LOG_MSG(level, f, msg) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n")) -#define LOG_FORMAT(level, f, msg, ...) Log.level(F("[" LOGGER_NAME "::" f "] " msg "\n"), __VA_ARGS__) +#define NL "\n" +#define LOG(level) LOG_MSG(level, "") +#define LOG_MSG(level, msg) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "]" msg NL)) +#define LOG_FORMAT(level, msg, ...) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "]" msg NL), __VA_ARGS__) -#if _DEBUG -#define VERBOSE(f) LOG(verbose, f) -#define VERBOSE_MSG(f, msg) LOG_MSG(verbose, f, msg) -#define VERBOSE_FORMAT(f, msg, ...) LOG_FORMAT(verbose, f, msg, __VA_ARGS__) +#if defined(LOG_LEVEL_VERBOSE_ENABLED) + #define VERBOSE LOG(verbose) + #define VERBOSE_MSG(msg) LOG_MSG(verbose, msg) + #define VERBOSE_FORMAT(msg, ...) LOG_FORMAT(verbose, msg, __VA_ARGS__) #else -#define VERBOSE(f) -#define VERBOSE_MSG(f, msg) -#define VERBOSE_FORMAT(f, msg, ...) + #define VERBOSE + #define VERBOSE_MSG(msg) + #define VERBOSE_FORMAT(msg, ...) #endif -#define NOTICE(f) LOG(notice, f) -#define NOTICE_MSG(f, msg) LOG_MSG(notice, f, msg) -#define NOTICE_FORMAT(f, msg, ...) LOG_FORMAT(notice, f, msg, __VA_ARGS__) - +#if defined(LOG_LEVEL_NOTICE_ENABLED) + #define NOTICE LOG(notice) + #define NOTICE_MSG(msg) LOG_MSG(notice, msg) + #define NOTICE_FORMAT(msg, ...) LOG_FORMAT(notice, msg, __VA_ARGS__) +#else + #define NOTICE + #define NOTICE_MSG(msg) + #define NOTICE_FORMAT(msg, ...) +#endif namespace logging { void setup(); diff --git a/src/MainUnit.cpp b/src/MainUnit.cpp index 1455b8d..369322d 100644 --- a/src/MainUnit.cpp +++ b/src/MainUnit.cpp @@ -4,11 +4,11 @@ #include "config/Pins.h" #include "Logging.h" -#define LOGGER_NAME "MainUnit" extern int __heap_start, *__brkval; namespace mainunit { + #define CURRENT_LOGGER "mainunit" namespace details { @@ -18,9 +18,11 @@ namespace mainunit { } void wokeUp() { + #define CURRENT_LOGGER_FUNCTION "wokeUp" + tmElements_t wokeUpTime; rtc::getTime(wokeUpTime); - VERBOSE_FORMAT("wokeUp", "%d:%d:%d", wokeUpTime.hour, wokeUpTime.minute, wokeUpTime.second); + VERBOSE_FORMAT("%d:%d:%d", wokeUpTime.hour, wokeUpTime.minute, wokeUpTime.second); hardware::sim808::simSerial.listen(); } @@ -39,7 +41,9 @@ namespace mainunit { } void deepSleep(uint16_t seconds) { - NOTICE_FORMAT("deepSleep", "%d seconds", seconds); + #define CURRENT_LOGGER_FUNCTION "deepSleep" + NOTICE_FORMAT("%d seconds", seconds); + interruptIn(seconds); details::prepareSleep(); LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); diff --git a/src/Network.cpp b/src/Network.cpp index 463d380..86a0ecc 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -5,10 +5,8 @@ #include "Config.h" #include "Logging.h" -#define LOGGER_NAME "Network" - namespace network { - + #define CURRENT_LOGGER "network" timestamp_t _poweredOnTime; void powerOn() { @@ -23,6 +21,7 @@ namespace network { __attribute__((__optimize__("O2"))) SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { + #define CURRENT_LOGGER_FUNCTION "waitForRegistered" SIM808_NETWORK_REGISTRATION_STATE currentStatus; SIM808SignalQualityReport report; @@ -36,12 +35,12 @@ namespace network { do { if (isAvailable(currentStatus)) break; - NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); + NOTICE_FORMAT("%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); if (report.rssi < NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++; else noReliableNetwork = 0; if (noReliableNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { - NOTICE_MSG("waitForRegistered", "No reliable signal"); + NOTICE_MSG("No reliable signal"); break; //after a while, no network really means no network. Bailing out } @@ -52,7 +51,7 @@ namespace network { report = hardware::sim808::device.getSignalQuality(); } while (timeout > 1); - NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); + NOTICE_FORMAT("%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); return currentStatus; } @@ -69,9 +68,10 @@ namespace network { #endif bool sendSms(const char * msg) { + #define CURRENT_LOGGER_FUNCTION "sendSms" const char * phoneNumber = config::main::value.contactPhone; - NOTICE_FORMAT("sendSms", "%s, %s", phoneNumber, msg); + NOTICE_FORMAT("%s, %s", phoneNumber, msg); return hardware::sim808::device.sendSms(phoneNumber, msg); } } \ No newline at end of file diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index 183c9e2..b13f8b2 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -9,11 +9,12 @@ #include "Network.h" #include "Logging.h" -#define LOGGER_NAME "Positions::backup::network" - #define BUFFER_SIZE 170 + namespace positions { + #define CURRENT_LOGGER "Positions::backup::network" + namespace backup { namespace net { @@ -27,6 +28,8 @@ namespace positions { } bool NetworkPositionsBackup::appendPosition(PositionEntry &entry, int8_t signalAttenuation = 0) { + #define CURRENT_LOGGER_FUNCTION "appendPosition" + char buffer[BUFFER_SIZE]; if(signalAttenuation == 0) signalAttenuation = hardware::sim808::device.getSignalQuality().attenuation; @@ -40,7 +43,7 @@ namespace positions { entry.metadata.timeToFix, entry.position); - NOTICE_FORMAT("appendPosition", "Sending : %s", buffer); + NOTICE_FORMAT("Sending : %s", buffer); uint16_t responseCode = hardware::sim808::device.httpPost( config::main::value.network.url, F("text/gpstracker"), @@ -49,12 +52,14 @@ namespace positions { BUFFER_SIZE ); - NOTICE_FORMAT("appendPosition", "Response : %d", responseCode); + NOTICE_FORMAT("Response : %d", responseCode); return responseCode == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } //__attribute__((__optimize__("O2"))) void NetworkPositionsBackup::appendPositions() { + #define CURRENT_LOGGER_FUNCTION "appendPositions" + uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; PositionEntry currentEntry; SIM808_NETWORK_REGISTRATION_STATE networkStatus; @@ -67,7 +72,7 @@ namespace positions { if (!network::isAvailable(networkStatus) || !network::enableGprs()) { networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow - NOTICE_MSG("appendPositions", "network or gprs unavailable"); + NOTICE_MSG("network or gprs unavailable"); if (networkUnavailableInARow > POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD) { networkUnavailablePostpone++; @@ -96,14 +101,16 @@ namespace positions { void NetworkPositionsBackup::setup() {} void NetworkPositionsBackup::prepare() { - NOTICE("prepare"); + #define CURRENT_LOGGER_FUNCTION "prepare" + NOTICE; if (!isBackupNeeded(true)) return; network::powerOn(); } void NetworkPositionsBackup::backup(bool force) { - NOTICE("backup"); + #define CURRENT_LOGGER_FUNCTION "backup" + NOTICE; if (force || isBackupNeeded(false)) { appendPositions(); diff --git a/src/Positions.cpp b/src/Positions.cpp index d4c8440..8996839 100644 --- a/src/Positions.cpp +++ b/src/Positions.cpp @@ -16,12 +16,12 @@ #include "NetworkPositionsBackup.h" #endif -#define LOGGER_NAME "Positions" - #define ENTRY_RESERVED_SIZE 128 #define ENTRIES_ADDR CONFIG_RESERVED_SIZE namespace positions { + #define CURRENT_LOGGER "positions" + #if BACKUPS_ENABLED > 1 backup::PositionsBackup **_backups; #elif BACKUPS_ENABLED == 1 @@ -73,7 +73,8 @@ namespace positions { } bool acquire(PositionEntryMetadata &metadata) { - NOTICE("acquire"); + #define CURRENT_LOGGER_FUNCTION "acquire" + NOTICE; timestamp_t before; @@ -85,7 +86,7 @@ namespace positions { gps::powerOff(); bool acquired = gpsStatus >= SIM808_GPS_STATUS::FIX; //prety useless wins 14 bytes on the hex size rather than return gpStatus >= ... - NOTICE_FORMAT("acquire", "Status : %d", gpsStatus); + NOTICE_FORMAT("Status : %d", gpsStatus); metadata = { battery.level, @@ -99,7 +100,8 @@ namespace positions { } void appendLast(const PositionEntryMetadata &metadata) { - VERBOSE("appendLast"); + #define CURRENT_LOGGER_FUNCTION "appendLast" + VERBOSE; uint16_t entryIndex; uint16_t entryAddress; @@ -114,7 +116,7 @@ namespace positions { hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); - NOTICE_FORMAT("appendLast", "Saved @ %X : %d,%d,%d,%d,%d,%s", + NOTICE_FORMAT("Saved @ %X : %d,%d,%d,%d,%d,%s", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, @@ -133,18 +135,19 @@ namespace positions { } bool get(uint16_t index, PositionEntry &entry) { - VERBOSE("get"); + #define CURRENT_LOGGER_FUNCTION "get" + VERBOSE; uint16_t entryAddress = details::getEntryAddress(index); if (entryAddress == -1) return false; - VERBOSE_FORMAT("get", "Reading entry %d @ %X", index, entryAddress); + VERBOSE_FORMAT("Reading entry %d @ %X", index, entryAddress); hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - NOTICE_FORMAT("get", "Read from EEPROM @ %X : %d,%d,%d,%d,%d,%s", + NOTICE_FORMAT("Read from EEPROM @ %X : %d,%d,%d,%d,%d,%s", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, diff --git a/src/Rtc.cpp b/src/Rtc.cpp index 508411e..e4b2c46 100644 --- a/src/Rtc.cpp +++ b/src/Rtc.cpp @@ -5,15 +5,17 @@ #include -#define LOGGER_NAME "Rtc" using namespace utils; namespace rtc { + #define CURRENT_LOGGER "rtc" RTC_A_CLASS RTC_A; void setup() { - VERBOSE("setup"); + #define CURRENT_LOGGER_FUNCTION "setup" + VERBOSE; + hardware::i2c::powerOn(); RTC_A.control(DS3231_12H, DS3231_OFF); //24 hours clock RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF @@ -44,15 +46,18 @@ namespace rtc { } void getTime(tmElements_t &time) { + #define CURRENT_LOGGER_FUNCTION "getTime" + hardware::i2c::powerOn(); RTC_A.readTime(time); hardware::i2c::powerOff(); - VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); + VERBOSE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); } void setTime(const tmElements_t &time) { - VERBOSE_FORMAT("setTime", "%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); + #define CURRENT_LOGGER_FUNCTION "setTime" + VERBOSE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second); hardware::i2c::powerOn(); RTC_A.writeTime(time); @@ -71,6 +76,8 @@ namespace rtc { } void setAlarm(const tmElements_t &time) { + #define CURRENT_LOGGER_FUNCTION "setAlarm" + hardware::i2c::powerOn(); WRITE_ALARM_1(time); @@ -78,7 +85,7 @@ namespace rtc { RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON RTC_A.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON - NOTICE_FORMAT("setAlarm", "Next alarm : %d:%d:%d", time.hour, time.minute, time.second); + NOTICE_FORMAT("Next alarm : %d:%d:%d", time.hour, time.minute, time.second); hardware::i2c::powerOff(); } diff --git a/src/SdPositionsBackup.cpp b/src/SdPositionsBackup.cpp index fceeee9..d2d40cb 100644 --- a/src/SdPositionsBackup.cpp +++ b/src/SdPositionsBackup.cpp @@ -6,10 +6,10 @@ #include "Positions.h" #include "Logging.h" -#define LOGGER_NAME "Positions::backup::sd" - #if BACKUP_ENABLE_SDCARD namespace positions { + #define CURRENT_LOGGER "positions::backup::sd" + namespace backup { namespace sd { @@ -60,7 +60,8 @@ namespace positions { } void appendPosition(File &file, SdPositionConfig_t &sdConfig, PositionEntry &entry) { - VERBOSE("appendPosition"); + #define CURRENT_LOGGER_FUNCTION "appendPosition" + VERBOSE; const char fieldTerminator = ','; @@ -75,7 +76,8 @@ namespace positions { } void appendPositions(SdPositionConfig_t &sdConfig) { - VERBOSE("appendPositions"); + #define CURRENT_LOGGER_FUNCTION "appendPositions" + VERBOSE; uint16_t currentEntryIndex = sdConfig.lastSavedEntry + 1; PositionEntry currentEntry; @@ -100,10 +102,11 @@ namespace positions { } void SdPositionsBackup::backup(bool force) { - VERBOSE("backup"); + #define CURRENT_LOGGER_FUNCTION "backup" + VERBOSE; if (!hardware::sdcard::available) { - VERBOSE_MSG("backup", "not available"); + VERBOSE_MSG("not available"); return; } diff --git a/src/SdPositionsConfig.cpp b/src/SdPositionsConfig.cpp index 29a0550..416bee3 100644 --- a/src/SdPositionsConfig.cpp +++ b/src/SdPositionsConfig.cpp @@ -2,10 +2,11 @@ #include "SdCard.h" #include "Logging.h" -#define LOGGER_NAME "Config::backup::sd" #if BACKUP_ENABLE_SDCARD namespace config { + #define CURRENT_LOGGER "config::backup::sd" + namespace backup { namespace sd { @@ -24,14 +25,18 @@ namespace config { } void read() { - VERBOSE("read"); + #define CURRENT_LOGGER_FUNCTION "read" + VERBOSE; + ensureOpened(); configFile.read((void*)&value, sizeof(value)); if (value.seed != POSITIONS_CONFIG_SEED) reset(); } void write() { - VERBOSE("write"); + #define CURRENT_LOGGER_FUNCTION "write" + VERBOSE; + ensureOpened(); configFile.write((void*)&value, sizeof(value)); } @@ -50,7 +55,9 @@ namespace config { } void reset() { - VERBOSE("reset"); + #define CURRENT_LOGGER_FUNCTION "reset" + VERBOSE; + SdPositionConfig_t config = { POSITIONS_CONFIG_SEED, POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD, From 0fa1eb4dc934c918079d2c43413c20a554e517bd Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 3 Dec 2018 17:34:59 +1300 Subject: [PATCH 04/17] Reduced hex size by mutualizing print position function --- src/Debug.cpp | 30 ++++-------------------------- src/Debug.h | 3 +-- src/GpsTracker.ino | 4 ++-- src/Positions.cpp | 34 +++++++++++++++++----------------- src/Positions.h | 2 ++ 5 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/Debug.cpp b/src/Debug.cpp index e23f953..5b3e617 100644 --- a/src/Debug.cpp +++ b/src/Debug.cpp @@ -116,19 +116,6 @@ using namespace utils; namespace debug { #define CURRENT_LOGGER "debug" - namespace details { - inline void displayPosition(PositionEntry entry) { - #define CURRENT_LOGGER_FUNCTION "displayPosition" - NOTICE_FORMAT("%d,%d,%d,%d,%d,%s", - entry.metadata.batteryLevel, - entry.metadata.batteryVoltage, - entry.metadata.temperature, - static_cast(entry.metadata.status), - entry.metadata.timeToFix, - entry.position); - } - } - void displayFreeRam() { #define CURRENT_LOGGER_FUNCTION "displayFreeRam" NOTICE_FORMAT("%d", mainunit::freeRam()); @@ -249,27 +236,18 @@ namespace debug { NOTICE_MSG("Done"); } - void getAndDisplayEepromPositions() { - uint16_t currentEntryIndex = config::main::value.firstEntry; + void getAndDisplayEepromPositions(uint16_t firstIndex) { + uint16_t currentEntryIndex = firstIndex; PositionEntry currentEntry; hardware::i2c::powerOn(); do { if (!positions::get(currentEntryIndex, currentEntry)) break; - details::displayPosition(currentEntry); - } while (positions::moveNext(currentEntryIndex)); + positions::print(currentEntryIndex, currentEntry); + } while(positions::moveNext(currentEntryIndex)); hardware::i2c::powerOff(); } - void getAndDisplayEepromLastPosition() { - #define CURRENT_LOGGER_FUNCTION "getAndDisplayEepromLastPosition" - uint16_t lastEntryIndex = config::main::value.lastEntry; - PositionEntry lastEntry; - - if(positions::get(lastEntryIndex, lastEntry)) details::displayPosition(lastEntry); - else NOTICE_MSG("No position recorded"); - } - void addLastPositionToEeprom() { hardware::sim808::powerOn(); SIM808ChargingStatus status = hardware::sim808::device.getChargingState(); diff --git a/src/Debug.h b/src/Debug.h index dbe40f4..0fd191f 100644 --- a/src/Debug.h +++ b/src/Debug.h @@ -44,8 +44,7 @@ namespace debug { void getAndDisplayEepromConfig(); void getAndDisplayEepromContent(); - void getAndDisplayEepromPositions(); - void getAndDisplayEepromLastPosition(); + void getAndDisplayEepromPositions(uint16_t firstIndex); void addLastPositionToEeprom(); void notifyFailures(); diff --git a/src/GpsTracker.ino b/src/GpsTracker.ino index d691f89..64f0714 100644 --- a/src/GpsTracker.ino +++ b/src/GpsTracker.ino @@ -75,10 +75,10 @@ void loop() { debug::getAndDisplayEepromContent(); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_ENTRIES: - debug::getAndDisplayEepromPositions(); + debug::getAndDisplayEepromPositions(config::main::value.firstEntry); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_LAST_ENTRY: - debug::getAndDisplayEepromLastPosition(); + debug::getAndDisplayEepromPositions(config::main::value.lastEntry); break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY: debug::addLastPositionToEeprom(); diff --git a/src/Positions.cpp b/src/Positions.cpp index 8996839..90db23b 100644 --- a/src/Positions.cpp +++ b/src/Positions.cpp @@ -116,14 +116,8 @@ namespace positions { hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); - NOTICE_FORMAT("Saved @ %X : %d,%d,%d,%d,%d,%s", - entryAddress, - entry.metadata.batteryLevel, - entry.metadata.batteryVoltage, - entry.metadata.temperature, - static_cast(entry.metadata.status), - entry.metadata.timeToFix, - entry.position); + NOTICE_MSG("Saved"); + print(entryIndex, entry); config->lastEntry++; if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0; @@ -147,15 +141,7 @@ namespace positions { hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - NOTICE_FORMAT("Read from EEPROM @ %X : %d,%d,%d,%d,%d,%s", - entryAddress, - entry.metadata.batteryLevel, - entry.metadata.batteryVoltage, - entry.metadata.temperature, - static_cast(entry.metadata.status), - entry.metadata.timeToFix, - entry.position); - + print(index, entry); return true; } @@ -196,4 +182,18 @@ namespace positions { _backup->backup(force); #endif } + + void print(uint16_t index, PositionEntry &entry) { + #define CURRENT_LOGGER_FUNCTION "print" + + uint16_t address = details::getEntryAddress(index); + NOTICE_FORMAT("%X : %d,%d,%d,%d,%d,%s", + address, + entry.metadata.batteryLevel, + entry.metadata.batteryVoltage, + entry.metadata.temperature, + static_cast(entry.metadata.status), + entry.metadata.timeToFix, + entry.position); + } } \ No newline at end of file diff --git a/src/Positions.h b/src/Positions.h index 268a9b5..6f3b6a0 100644 --- a/src/Positions.h +++ b/src/Positions.h @@ -30,4 +30,6 @@ namespace positions { void prepareBackup(); void doBackup(bool force); + + void print(uint16_t index, PositionEntry &entry); } \ No newline at end of file From a5ff07e7daa94e9345df8d110747fb6506d1778e Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 3 Dec 2018 17:36:04 +1300 Subject: [PATCH 05/17] Reduced hex size by mutualizing print config function --- src/Config.cpp | 19 ++++++++++++------- src/Config.h | 2 ++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index f0a2025..040c939 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -14,16 +14,14 @@ namespace config { void read() { #define CURRENT_LOGGER_FUNCTION "read" - VERBOSE; + NOTICE; hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); - NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); #if BACKUP_ENABLE_NETWORK - NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); //networkConfig_t c = { // POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, // 0xFFFF, @@ -42,11 +40,9 @@ namespace config { void write() { #define CURRENT_LOGGER_FUNCTION "write" + NOTICE; - NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); -#if BACKUP_ENABLE_NETWORK - NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); -#endif + print(); hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); hardware::i2c::powerOff(); @@ -91,5 +87,14 @@ namespace config { save(); } + void print() { + #define CURRENT_LOGGER_FUNCTION "print" + + NOTICE_FORMAT("%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); +#if BACKUP_ENABLE_NETWORK + NOTICE_FORMAT("%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); +#endif + } + } } \ No newline at end of file diff --git a/src/Config.h b/src/Config.h index e165fc2..85ea75d 100644 --- a/src/Config.h +++ b/src/Config.h @@ -29,5 +29,7 @@ namespace config { void save(); void reset(); + + void print(); } } \ No newline at end of file From d4afcd3781ad1809d20f587cbe2e4c146f338f4f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 3 Dec 2018 17:36:41 +1300 Subject: [PATCH 06/17] One version string instead of several --- src/Config.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 040c939..a7e0032 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -5,6 +5,7 @@ namespace config { #define CURRENT_LOGGER "config" + const char VERSION_STRING[] PROGMEM = VERSION; namespace main { @@ -30,7 +31,7 @@ namespace config { //}; //value.network = c; #endif - /*strcpy_P(value.version, PSTR(VERSION)); + /*strcpy_P(value.version, VERSION_STRING); value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; @@ -52,8 +53,8 @@ namespace config { void setup() { details::read(); - if(strcasecmp_P(value.version, PSTR(VERSION))) { - strcpy_P(value.version, PSTR(VERSION)); + if(strcasecmp_P(value.version, VERSION_STRING)) { + strcpy_P(value.version, VERSION_STRING); details::write(); } } @@ -68,7 +69,7 @@ namespace config { config_t config = {}; config.seed = CONFIG_SEED; - strcpy_P(config.version, PSTR(VERSION)); + strcpy_P(config.version, VERSION_STRING); config.firstEntry = config.lastEntry = 0xFFFF; config.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; config.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; From a2bfadd21f017704d0923efcf90704814487cd64 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 3 Dec 2018 18:00:59 +1300 Subject: [PATCH 07/17] Adjusted some logs --- src/Config.cpp | 2 +- src/Hardware.cpp | 2 +- src/Logging.h | 4 ++-- src/Network.cpp | 14 +++++++++++--- src/NetworkPositionsBackup.cpp | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index a7e0032..933f326 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -65,7 +65,7 @@ namespace config { void reset() { #define CURRENT_LOGGER_FUNCTION "reset" - VERBOSE; + NOTICE; config_t config = {}; config.seed = CONFIG_SEED; diff --git a/src/Hardware.cpp b/src/Hardware.cpp index 0c8355c..3cd7c59 100644 --- a/src/Hardware.cpp +++ b/src/Hardware.cpp @@ -46,7 +46,7 @@ namespace hardware { void setup() { #define CURRENT_LOGGER_FUNCTION "setup" - NOTICE; + VERBOSE; simSerial.begin(SIM808_BAUDRATE); device.begin(simSerial); diff --git a/src/Logging.h b/src/Logging.h index 6cef01a..dd855e3 100644 --- a/src/Logging.h +++ b/src/Logging.h @@ -17,8 +17,8 @@ #define NL "\n" #define LOG(level) LOG_MSG(level, "") -#define LOG_MSG(level, msg) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "]" msg NL)) -#define LOG_FORMAT(level, msg, ...) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "]" msg NL), __VA_ARGS__) +#define LOG_MSG(level, msg) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "] " msg NL)) +#define LOG_FORMAT(level, msg, ...) Log.level(F("[" CURRENT_LOGGER "::" CURRENT_LOGGER_FUNCTION "] " msg NL), __VA_ARGS__) #if defined(LOG_LEVEL_VERBOSE_ENABLED) #define VERBOSE LOG(verbose) diff --git a/src/Network.cpp b/src/Network.cpp index 86a0ecc..efaafc1 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -9,6 +9,14 @@ namespace network { #define CURRENT_LOGGER "network" timestamp_t _poweredOnTime; + namespace details { + + void print(SIM808_NETWORK_REGISTRATION_STATE state, SIM808SignalQualityReport &report) { + #define CURRENT_LOGGER_FUNCTION "print" + NOTICE_FORMAT("%d, [%d %ddBm]", state, report.rssi, report.attenuation); + } + + } void powerOn() { hardware::sim808::networkPowerOn(); _poweredOnTime = rtc::getTime(); @@ -22,6 +30,7 @@ namespace network { __attribute__((__optimize__("O2"))) SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { #define CURRENT_LOGGER_FUNCTION "waitForRegistered" + NOTICE; SIM808_NETWORK_REGISTRATION_STATE currentStatus; SIM808SignalQualityReport report; @@ -34,8 +43,7 @@ namespace network { do { if (isAvailable(currentStatus)) break; - - NOTICE_FORMAT("%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); + details::print(currentStatus, report); if (report.rssi < NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++; else noReliableNetwork = 0; @@ -51,7 +59,7 @@ namespace network { report = hardware::sim808::device.getSignalQuality(); } while (timeout > 1); - NOTICE_FORMAT("%d, [%d %ddBm]", currentStatus, report.rssi, report.attenuation); + details::print(currentStatus, report); return currentStatus; } diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index b13f8b2..f0298f4 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -102,7 +102,7 @@ namespace positions { void NetworkPositionsBackup::prepare() { #define CURRENT_LOGGER_FUNCTION "prepare" - NOTICE; + VERBOSE; if (!isBackupNeeded(true)) return; network::powerOn(); From b2cfd8ccdda807114ccfa9e4059d2f1625cac7fa Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 3 Dec 2018 18:01:14 +1300 Subject: [PATCH 08/17] removed unnecessary compiler directives --- src/Network.cpp | 1 - src/NetworkPositionsBackup.cpp | 1 - src/Time2.cpp | 6 ++---- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Network.cpp b/src/Network.cpp index efaafc1..129c20b 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -27,7 +27,6 @@ namespace network { _poweredOnTime = 0; } - __attribute__((__optimize__("O2"))) SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { #define CURRENT_LOGGER_FUNCTION "waitForRegistered" NOTICE; diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index f0298f4..fd79f18 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -56,7 +56,6 @@ namespace positions { return responseCode == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } - //__attribute__((__optimize__("O2"))) void NetworkPositionsBackup::appendPositions() { #define CURRENT_LOGGER_FUNCTION "appendPositions" diff --git a/src/Time2.cpp b/src/Time2.cpp index a150e87..2012d00 100644 --- a/src/Time2.cpp +++ b/src/Time2.cpp @@ -16,8 +16,7 @@ namespace utils { namespace time { - __attribute__((__optimize__("O2"))) - timestamp_t makeTimestamp(const tmElements_t &time) { + timestamp_t makeTimestamp(const tmElements_t &time) { timestamp_t timestamp; timestamp += (time.day - 1) * SECS_PER_DAY; @@ -28,8 +27,7 @@ namespace utils { return timestamp; } - __attribute__((__optimize__("O2"))) - void breakTime(timestamp_t timestamp, tmElements_t &time) { + void breakTime(timestamp_t timestamp, tmElements_t &time) { time.year = 0; time.month = 0; From 149db28841909556bb3ea0e9f478ac6d2932c108 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 18:17:54 +1300 Subject: [PATCH 09/17] Added force parameter to prepareBackup, as for backup --- src/NetworkPositionsBackup.cpp | 4 ++-- src/NetworkPositionsBackup.h | 2 +- src/Positions.cpp | 6 +++--- src/Positions.h | 2 +- src/PositionsBackup.h | 30 +++++++++++++++--------------- src/SdPositionsBackup.cpp | 2 +- src/SdPositionsBackup.h | 34 +++++++++++++++++----------------- 7 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index fd79f18..8284e86 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -99,11 +99,11 @@ namespace positions { void NetworkPositionsBackup::setup() {} - void NetworkPositionsBackup::prepare() { + void NetworkPositionsBackup::prepare(bool force) { #define CURRENT_LOGGER_FUNCTION "prepare" VERBOSE; - if (!isBackupNeeded(true)) return; + if (force || !isBackupNeeded(true)) return; network::powerOn(); } diff --git a/src/NetworkPositionsBackup.h b/src/NetworkPositionsBackup.h index 7247fe8..dd2912e 100644 --- a/src/NetworkPositionsBackup.h +++ b/src/NetworkPositionsBackup.h @@ -16,7 +16,7 @@ namespace positions { public: void setup(); - void prepare(); + void prepare(bool force); void backup(bool force); }; diff --git a/src/Positions.cpp b/src/Positions.cpp index 90db23b..8221475 100644 --- a/src/Positions.cpp +++ b/src/Positions.cpp @@ -163,13 +163,13 @@ namespace positions { return lastEntry - fromIndex; } - void prepareBackup() { + void prepareBackup(bool force) { #if BACKUPS_ENABLED > 1 for (int i = 0; i < BACKUPS_ENABLED; i++) { - _backups[i]->prepare(); + _backups[i]->prepare(force); } #elif BACKUPS_ENABLED == 1 - _backup->prepare(); + _backup->prepare(force); #endif } diff --git a/src/Positions.h b/src/Positions.h index 6f3b6a0..a6313e3 100644 --- a/src/Positions.h +++ b/src/Positions.h @@ -28,7 +28,7 @@ namespace positions { bool moveNext(uint16_t &index); uint16_t count(uint16_t fromIndex); - void prepareBackup(); + void prepareBackup(bool force); void doBackup(bool force); void print(uint16_t index, PositionEntry &entry); diff --git a/src/PositionsBackup.h b/src/PositionsBackup.h index 340ec4b..f837f52 100644 --- a/src/PositionsBackup.h +++ b/src/PositionsBackup.h @@ -1,16 +1,16 @@ -#pragma once - -namespace positions { - namespace backup { - - class PositionsBackup { - public: - ~PositionsBackup(); - virtual void setup() = 0; - - virtual void prepare() = 0; - virtual void backup(bool force) = 0; - }; - - } +#pragma once + +namespace positions { + namespace backup { + + class PositionsBackup { + public: + ~PositionsBackup(); + virtual void setup() = 0; + + virtual void prepare(bool force) = 0; + virtual void backup(bool force) = 0; + }; + + } } \ No newline at end of file diff --git a/src/SdPositionsBackup.cpp b/src/SdPositionsBackup.cpp index d2d40cb..8dea093 100644 --- a/src/SdPositionsBackup.cpp +++ b/src/SdPositionsBackup.cpp @@ -97,7 +97,7 @@ namespace positions { } } - void SdPositionsBackup::setup() { + void SdPositionsBackup::setup(bool force) { hardware::sdcard::setup(); } diff --git a/src/SdPositionsBackup.h b/src/SdPositionsBackup.h index 87aa92b..9eb071e 100644 --- a/src/SdPositionsBackup.h +++ b/src/SdPositionsBackup.h @@ -1,18 +1,18 @@ -#pragma once - -#include "PositionsBackup.h" - -namespace positions { - namespace backup { - namespace sd { - - class SdPositionsBackup : public PositionsBackup { - private: - public: - void setup(); - void backup(bool force); - }; - - } - } +#pragma once + +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + namespace sd { + + class SdPositionsBackup : public PositionsBackup { + private: + public: + void setup(bool force); + void backup(bool force); + }; + + } + } } \ No newline at end of file From 2b3350c6e72dc62d8f2549c520096b061bec7979 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:54:05 +1300 Subject: [PATCH 10/17] Changed some prints --- src/Config.cpp | 1 + src/MainUnit.cpp | 2 +- src/Positions.cpp | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 933f326..dfac240 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -19,6 +19,7 @@ namespace config { hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); + print(); if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right hardware::i2c::powerOff(); diff --git a/src/MainUnit.cpp b/src/MainUnit.cpp index 369322d..37b51df 100644 --- a/src/MainUnit.cpp +++ b/src/MainUnit.cpp @@ -42,7 +42,7 @@ namespace mainunit { void deepSleep(uint16_t seconds) { #define CURRENT_LOGGER_FUNCTION "deepSleep" - NOTICE_FORMAT("%d seconds", seconds); + NOTICE_FORMAT("%ds", seconds); interruptIn(seconds); details::prepareSleep(); diff --git a/src/Positions.cpp b/src/Positions.cpp index 8221475..67aca18 100644 --- a/src/Positions.cpp +++ b/src/Positions.cpp @@ -112,12 +112,12 @@ namespace positions { entryIndex = config->lastEntry + 1; entryAddress = details::getEntryAddress(entryIndex); + print(entryIndex, entry); hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); NOTICE_MSG("Saved"); - print(entryIndex, entry); config->lastEntry++; if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0; From f70acbddc4c33930bce5f05d0031ae139300f99a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:54:59 +1300 Subject: [PATCH 11/17] Fixed readAlarm1 override not unpacking time --- src/RtcAbstraction.cpp | 1 + src/RtcAbstraction.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/RtcAbstraction.cpp b/src/RtcAbstraction.cpp index b3ece36..11cbe04 100644 --- a/src/RtcAbstraction.cpp +++ b/src/RtcAbstraction.cpp @@ -35,6 +35,7 @@ boolean MD_DS3231_Ext::writeTime(const tmElements_t &time) { boolean MD_DS3231_Ext::readAlarm1(almType_t &almType, tmElements_t &time) { almType = MD_DS3231::getAlarm1Type(); bool result = MD_DS3231::readAlarm1(); + unpack(time); return result; } diff --git a/src/RtcAbstraction.h b/src/RtcAbstraction.h index ff5a6e1..00ceeee 100644 --- a/src/RtcAbstraction.h +++ b/src/RtcAbstraction.h @@ -12,6 +12,7 @@ #define RTC_A_CLASS uDS3231 #define WRITE_ALARM_1(t) RTC_A.writeAlarm1(DS3231_ALM_HMS, t) + #define READ_ALARM_1(t) almType_t almDummy; RTC_A.readAlarm1(almDummy, t) #else #include @@ -39,4 +40,5 @@ #define RTC_A_CLASS MD_DS3231_Ext #define WRITE_ALARM_1(t) RTC_A.writeAlarm1(DS3231_ALM_HMS, t) + #define READ_ALARM_1(t) almType_t almDummy; RTC_A.readAlarm1(almDummy, t) #endif \ No newline at end of file From f7b336b1c3c3cf57d0a2427c2367dcdf20f815da Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:55:27 +1300 Subject: [PATCH 12/17] Displayed alarm1 actual time instead of supposed one --- src/Rtc.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Rtc.cpp b/src/Rtc.cpp index e4b2c46..37298c5 100644 --- a/src/Rtc.cpp +++ b/src/Rtc.cpp @@ -69,10 +69,12 @@ namespace rtc { tmElements_t currentTime; tmElements_t alarmTime; + hardware::i2c::powerOn(); getTime(currentTime); time::breakTime(time::makeTimestamp(currentTime) + seconds, alarmTime); setAlarm(alarmTime); + hardware::i2c::powerOff(); } void setAlarm(const tmElements_t &time) { @@ -85,7 +87,9 @@ namespace rtc { RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON RTC_A.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON - NOTICE_FORMAT("Next alarm : %d:%d:%d", time.hour, time.minute, time.second); + tmElements_t alarmTime; + READ_ALARM_1(alarmTime); + NOTICE_FORMAT("Next alarm : %d:%d:%d", alarmTime.hour, alarmTime.minute, alarmTime.second); hardware::i2c::powerOff(); } From e61ab1c11cf084815cfb7d188967e8045c9422fb Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:56:10 +1300 Subject: [PATCH 13/17] Fixed network and gps powered counts --- src/Hardware.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Hardware.cpp b/src/Hardware.cpp index 3cd7c59..7615e76 100644 --- a/src/Hardware.cpp +++ b/src/Hardware.cpp @@ -66,7 +66,9 @@ namespace hardware { //SIM808 turns phone on by default but we don't need it for gps only device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); } + device.powerOnOffGps(true); + gpsPoweredCount = 1; } void gpsPowerOff() { @@ -84,6 +86,8 @@ namespace hardware { VERBOSE; device.powerOnOffGps(false); + gpsPoweredCount = 0; + powerOffIfUnused(); } @@ -98,6 +102,7 @@ namespace hardware { VERBOSE; powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); + networkPoweredCount = 1; } void networkPowerOff() { @@ -116,6 +121,8 @@ namespace hardware { VERBOSE; device.disableGprs(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); + networkPoweredCount = 0; + powerOffIfUnused(); } } From 8250e47cdbff852be44767c8049498198748ae63 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:57:00 +1300 Subject: [PATCH 14/17] bailing out immediatly on network activation fail --- src/NetworkPositionsBackup.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index 8284e86..2faa913 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -69,7 +69,8 @@ namespace positions { network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus) || !network::enableGprs()) { + if (networkStatus == SIM808_NETWORK_REGISTRATION_STATE::ERROR || + (!network::isAvailable(networkStatus) || !network::enableGprs())) { networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow NOTICE_MSG("network or gprs unavailable"); From 79a5992d66fd762884da211b775704790e4357a0 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:57:41 +1300 Subject: [PATCH 15/17] Fixed network backup power on and off logic --- src/NetworkPositionsBackup.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/NetworkPositionsBackup.cpp b/src/NetworkPositionsBackup.cpp index 2faa913..b208fca 100644 --- a/src/NetworkPositionsBackup.cpp +++ b/src/NetworkPositionsBackup.cpp @@ -102,20 +102,20 @@ namespace positions { void NetworkPositionsBackup::prepare(bool force) { #define CURRENT_LOGGER_FUNCTION "prepare" - VERBOSE; - if (force || !isBackupNeeded(true)) return; + if (!(force || isBackupNeeded(true))) return; + + NOTICE; network::powerOn(); } void NetworkPositionsBackup::backup(bool force) { #define CURRENT_LOGGER_FUNCTION "backup" - NOTICE; - if (force || isBackupNeeded(false)) { - appendPositions(); - } + if (!(force || isBackupNeeded(false))) return; + NOTICE; + appendPositions(); network::powerOff(); } } From 3abdc2edcd68678b547469a8a8e68c5f3be7f10a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 5 Dec 2018 20:58:25 +1300 Subject: [PATCH 16/17] Added a new ABOUT_TO_STOP state to manage prepare and backup power adequality --- src/Core.cpp | 14 ++++++++++---- src/Core.h | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Core.cpp b/src/Core.cpp index ce789a8..0d45e07 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -39,7 +39,11 @@ namespace core { bool acquired = false; PositionEntryMetadata metadata; - if(movingState >= TRACKER_MOVING_STATE::STOPPED) positions::prepareBackup(); + if(movingState >= TRACKER_MOVING_STATE::ABOUT_TO_STOP) { + //forcing when the tracker is about to stop (which should result in STOPPED a few lines below) + positions::prepareBackup(movingState == TRACKER_MOVING_STATE::ABOUT_TO_STOP); + } + acquired = positions::acquire(metadata); if (acquired) { @@ -53,7 +57,7 @@ namespace core { alerts::add(notifyFailures(metadata)); if(movingState >= TRACKER_MOVING_STATE::STOPPED) { - positions::doBackup(movingState == TRACKER_MOVING_STATE::STOPPED); //do not force on STATIC + positions::doBackup(movingState == TRACKER_MOVING_STATE::STOPPED); //forcing at the moment the tracker stop } if (acquired) updateRtcTime(); @@ -124,14 +128,16 @@ namespace core { if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) { sleepTime = SLEEP_DEFAULT_PAUSING_TIME_SECONDS; - state = TRACKER_MOVING_STATE::PAUSED; + state = stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD - 1 ? + TRACKER_MOVING_STATE::ABOUT_TO_STOP : + TRACKER_MOVING_STATE::PAUSED; } else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) state = TRACKER_MOVING_STATE::STOPPED; else state = TRACKER_MOVING_STATE::STATIC; } else stoppedInARow = 0; - NOTICE_FORMAT("%dkmh => %d seconds", velocity, sleepTime); + NOTICE_FORMAT("stop %d, state %d, %dkmh => %ds", stoppedInARow, state, velocity, sleepTime); return state; } diff --git a/src/Core.h b/src/Core.h index 9f9beea..54542c2 100644 --- a/src/Core.h +++ b/src/Core.h @@ -6,8 +6,9 @@ enum class TRACKER_MOVING_STATE : uint8_t { MOVING = 0, PAUSED = 1, - STOPPED = 2, - STATIC = 3 + ABOUT_TO_STOP = 2, + STOPPED = 3, + STATIC = 4 }; namespace core { From 3b431c239075a6de918373b95397cf7c0516c787 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Wed, 19 Dec 2018 17:31:12 +1300 Subject: [PATCH 17/17] Moved triggered notice up a bit so it is diplayed in all cases --- src/Core.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Core.cpp b/src/Core.cpp index 0d45e07..e9c3304 100644 --- a/src/Core.cpp +++ b/src/Core.cpp @@ -73,10 +73,10 @@ namespace core { bool notified = false; uint8_t triggered = alerts::getTriggered(metadata); - if (!triggered) return NO_ALERTS_NOTIFIED; - NOTICE_FORMAT("triggered : %B", triggered); + if (!triggered) return NO_ALERTS_NOTIFIED; + network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS);