From 701043b4a34dfed4d342e0d9a0425e45483b2723 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Thu, 2 Aug 2018 22:35:07 +1200 Subject: [PATCH 01/74] Added alerts conditions and framework --- GpsTracker/Alerts.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ GpsTracker/Alerts.h | 16 ++++++++++++++++ GpsTracker/Config.h | 6 ++++++ 3 files changed, 65 insertions(+) create mode 100644 GpsTracker/Alerts.cpp create mode 100644 GpsTracker/Alerts.h diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp new file mode 100644 index 0000000..2023741 --- /dev/null +++ b/GpsTracker/Alerts.cpp @@ -0,0 +1,43 @@ +#pragma once + +#include "Alerts.h" +#include "Config.h" + +namespace alerts { + + uint8_t _alerts = 0; + + uint8_t getTriggered(PositionEntryMetadata &metadata) { + config_t* config = &config::main::value; + uint8_t result = 0; + + if (metadata.temperature == ALERT_RTC_TEMPERATURE_FAILURE && !bitRead(_alerts, ALERT_RTC_FAILURE)) { + bitSet(result, ALERT_RTC_FAILURE); + } + + if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(_alerts, ALERT_BATTERY_LEVEL_1)) { + bitSet(result, ALERT_BATTERY_LEVEL_1); + } + + if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(_alerts, ALERT_BATTERY_LEVEL_2)) { + bitSet(result, ALERT_BATTERY_LEVEL_2); + } + + return result; + } + + void clear(PositionEntryMetadata &metadata) { + config_t* config = &config::main::value; + uint8_t clearMask = 0; + + if (bitRead(_alerts, ALERT_RTC_FAILURE) && metadata.temperature != ALERT_RTC_TEMPERATURE_FAILURE) { + bitSet(clearMask, ALERT_RTC_FAILURE); + } + + if (_alerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2) && metadata.temperature >= config->alertBatteryLevelClear)) { + clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); + } + + _alerts &= ~clearMask; + } +} diff --git a/GpsTracker/Alerts.h b/GpsTracker/Alerts.h new file mode 100644 index 0000000..5117792 --- /dev/null +++ b/GpsTracker/Alerts.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Positions.h" + +#define ALERT_BATTERY_LEVEL_1 1 +#define ALERT_BATTERY_LEVEL_2 2 +#define ALERT_RTC_FAILURE 3 + +namespace alerts { + + extern uint8_t _alerts; + + uint8_t getTriggered(PositionEntryMetadata &metadata); + inline void add(uint8_t mask) { _alerts |= mask; } + void clear(PositionEntryMetadata &metadata); +} diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 54e776e..d5935e4 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -43,6 +43,8 @@ #define NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD 8 #define NETWORK_DEFAULT_NO_NETWORK_TRIES 5 +#define ALERT_RTC_TEMPERATURE_FAILURE 0 + #pragma endregion struct sleepTimings_t { @@ -60,6 +62,10 @@ struct config_t { #if BACKUP_ENABLE_NETWORK networkConfig_t network; #endif + uint8_t alertBatteryLevel1; + uint8_t alertBatteryLevel2; + uint8_t alertBatteryLevelClear; + char contactPhone[15]; }; namespace config { From b63912bd296768e9e5e3b9884e76915726ba3e11 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Thu, 2 Aug 2018 22:59:01 +1200 Subject: [PATCH 02/74] Added relativeToPowerOnTime to waitForRegistered, allowing to efficiently wait for the network accross the firmware --- GpsTracker/Network.cpp | 22 +++++++++++++++++----- GpsTracker/Network.h | 6 +++--- GpsTracker/NetworkPositionsBackup.cpp | 13 ++----------- GpsTracker/NetworkPositionsBackup.h | 2 -- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 030435d..fb9e6a3 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -1,22 +1,36 @@ #include "Config.h" -#if BACKUP_ENABLE_NETWORK #include "Debug.h" #include "Network.h" #include "Hardware.h" #include "MainUnit.h" +#include "Rtc.h" #define LOGGER_NAME "Network" namespace network { + timestamp_t _poweredOnTime; + + void powerOn() { + hardware::sim808::networkPowerOn(); + _poweredOnTime = rtc::getTime(); + } + + void powerOff() { + hardware::sim808::networkPowerOff(); + _poweredOnTime = 0; + } + __attribute__((__optimize__("O2"))) - SIM808RegistrationStatus waitForRegistered(uint32_t timeout) { + SIM808RegistrationStatus waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { SIM808RegistrationStatus currentStatus; SIM808SignalQualityReport report; uint8_t noReliableNetwork = 0; + if (relativeToPowerOnTime) timeout -= (rtc::getTime() - _poweredOnTime) * 1000; + do { currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); if (isAvailable(currentStatus.stat)) break; @@ -50,6 +64,4 @@ namespace network { return hardware::sim808::device.enableGprs(config::main::value.network.apn); } - -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index 33ee41e..a704004 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -4,10 +4,10 @@ namespace network { - inline void powerOn() { hardware::sim808::networkPowerOn(); } - inline void powerOff() { hardware::sim808::networkPowerOff(); } + void powerOn(); + void powerOff(); - SIM808RegistrationStatus waitForRegistered(uint32_t timeout); + SIM808RegistrationStatus waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true); bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); bool enableGprs(); } \ No newline at end of file diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 81cbaba..341167d 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -53,15 +53,11 @@ namespace positions { //__attribute__((__optimize__("O2"))) void NetworkPositionsBackup::appendPositions() { uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; - uint32_t networkTimeout = 0; PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; network::powerOn(); - networkTimeout = NETWORK_DEFAULT_TOTAL_TIMEOUT_MS; - if (_prepareTime > 0) networkTimeout -= (rtc::getTime() - _prepareTime) * 1000; - - networkStatus = network::waitForRegistered(networkTimeout); + networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); if (!network::isAvailable(networkStatus.stat) || !network::enableGprs()) { networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow @@ -97,13 +93,8 @@ namespace positions { void NetworkPositionsBackup::prepare() { NOTICE("prepare"); - if (!isBackupNeeded(true)) { - _prepareTime = 0; - return; - } - + if (!isBackupNeeded(true)) return; network::powerOn(); - _prepareTime = rtc::getTime(); } void NetworkPositionsBackup::backup(bool force) { diff --git a/GpsTracker/NetworkPositionsBackup.h b/GpsTracker/NetworkPositionsBackup.h index 4986093..751a911 100644 --- a/GpsTracker/NetworkPositionsBackup.h +++ b/GpsTracker/NetworkPositionsBackup.h @@ -10,8 +10,6 @@ namespace positions { class NetworkPositionsBackup : public PositionsBackup { private: - timestamp_t _prepareTime; - bool isBackupNeeded(bool forPrepare); bool appendPosition(PositionEntry &entry); void appendPositions(); From c7063a5698b96b91e445a1947f07f550b052a4ac Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Thu, 2 Aug 2018 23:13:45 +1200 Subject: [PATCH 03/74] Moved RTC time update from GPS to Core workflow rather than position acquisition. Avoid edge cases where the spend time measurement could be wrong because of time update in between the two measure points. --- GpsTracker/Core.cpp | 17 +++++++++++++++-- GpsTracker/Core.h | 1 + GpsTracker/Positions.cpp | 4 ---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 386e33d..b144619 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -7,14 +7,19 @@ using namespace utils; namespace core { + uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS; uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1; void main() { bool forceBackup = false; - positions::prepareBackup(); + bool acquired = false; PositionEntryMetadata metadata; - if (positions::acquire(metadata)) { + + positions::prepareBackup(); + acquired = positions::acquire(metadata); + + if (acquired) { positions::appendLast(metadata); forceBackup = updateSleepTime(); @@ -22,9 +27,17 @@ namespace core { } positions::doBackup(forceBackup); + + if (acquired) updateRtcTime(); mainunit::deepSleep(sleepTime); } + void updateRtcTime() { + tmElements_t time; + gps::getTime(time); + rtc::setTime(time); + } + bool updateSleepTime() { uint8_t velocity = gps::getVelocity(); uint16_t result = mapSleepTime(velocity); diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index 8e4e5b2..c21c2ab 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -14,6 +14,7 @@ namespace core { extern uint16_t sleepTime; void main(); + void updateRtcTime(); bool updateSleepTime(); uint16_t mapSleepTime(uint8_t velocity); } \ No newline at end of file diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 14e0253..81d2d52 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -70,10 +70,6 @@ namespace positions { uint16_t timeToFix = rtc::getTime() - before; - tmElements_t time; - gps::getTime(time); - rtc::setTime(time); - metadata = { battery.level, battery.voltage, From b538bd06d6f8a4f3a5f6fe8aa93b548bebcb64a2 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Thu, 2 Aug 2018 23:31:22 +1200 Subject: [PATCH 04/74] Fill metadata values even if the GPS fix isn't acquired. --- GpsTracker/Positions.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 81d2d52..f4ab051 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -57,19 +57,17 @@ namespace positions { NOTICE("acquire"); timestamp_t before; - + gps::powerOn(); before = rtc::getTime(); SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); + uint16_t timeToFix = rtc::getTime() - before; SIM808ChargingStatus battery = hardware::sim808::device.getChargingState(); 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); - if (gpsStatus < SIM808_GPS_STATUS::FIX) return false; - - uint16_t timeToFix = rtc::getTime() - before; - metadata = { battery.level, battery.voltage, @@ -78,7 +76,7 @@ namespace positions { gpsStatus }; - return true; + return acquired; } void appendLast(const PositionEntryMetadata &metadata) { @@ -126,7 +124,7 @@ namespace positions { bool moveNext(uint16_t &index) { if (index == config::main::value.lastEntry) return false; - + if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way else index++; From 8d51ceb975610e7051e963527240368ab1d3ed47 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 18:32:57 +1200 Subject: [PATCH 05/74] Added alerts notification and clearance in the main core function --- GpsTracker/Core.cpp | 32 +++++++++++++++++++++++++++++++- GpsTracker/Core.h | 2 ++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index b144619..3c0ee03 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -1,8 +1,10 @@ #include "Core.h" #include "Config.h" #include "Flash.h" +#include "Alerts.h" #define LOGGER_NAME "Core" +#define SMS_BUFFER_SIZE 100 using namespace utils; @@ -21,17 +23,45 @@ namespace core { if (acquired) { positions::appendLast(metadata); + forceBackup = updateSleepTime(); - gps::preserveCurrentCoordinates(); } + notifyFailures(metadata); + alerts::clear(metadata); positions::doBackup(forceBackup); if (acquired) updateRtcTime(); mainunit::deepSleep(sleepTime); } + void notifyFailures(PositionEntryMetadata &metadata) { + uint8_t triggered = alerts::getTriggered(metadata); + uint8_t notified = 0; + SIM808RegistrationStatus networkStatus; + char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; + + if (!triggered) return; + + network::powerOn(); + networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); + + if (!network::isAvailable(networkStatus.stat)) return; + + if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { + sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); + } + + if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { + sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2f°C. Backup battery failure ?\n"), metadata.batteryLevel); + } + + //TODO : send sms, return if failed + alerts::add(notified); //only add the successly notified failures + //TODO : network::powerOff(); count "handles" like for i2c ? + } + void updateRtcTime() { tmElements_t time; gps::getTime(time); diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index c21c2ab..3e8a5f5 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -17,4 +17,6 @@ namespace core { void updateRtcTime(); bool updateSleepTime(); uint16_t mapSleepTime(uint8_t velocity); + + void notifyFailures(PositionEntryMetadata &metadata); } \ No newline at end of file From 250b5b3371aafa08d7828eac94dbfb376bcb8d69 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 18:38:24 +1200 Subject: [PATCH 06/74] Added SMS send code to core::notifyFailures --- GpsTracker/Core.cpp | 7 ++++++- GpsTracker/Network.cpp | 1 - GpsTracker/Network.h | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 3c0ee03..48ae3b4 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -57,7 +57,12 @@ namespace core { sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2f°C. Backup battery failure ?\n"), metadata.batteryLevel); } - //TODO : send sms, return if failed + config_t* config = &config::main::value; + if (!network::sendSms(config->contactPhone, buffer)) { + NOTICE_MSG("notifyFailure", "SMS not sent !"); + return; + } + alerts::add(notified); //only add the successly notified failures //TODO : network::powerOff(); count "handles" like for i2c ? } diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index fb9e6a3..3073ca7 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -63,5 +63,4 @@ namespace network { bool enableGprs() { return hardware::sim808::device.enableGprs(config::main::value.network.apn); } - } \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index a704004..1f6e8af 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -10,4 +10,8 @@ namespace network { SIM808RegistrationStatus waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true); bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); bool enableGprs(); + + inline bool sendSms(const char * phoneNumber, const char * msg) { + return hardware::sim808::device.sendSms(phoneNumber, msg); + } } \ No newline at end of file From 6bc833750df1afbd60ff08acb7e50bd6186d951a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:11:18 +1200 Subject: [PATCH 07/74] Fixed bad use of constant. notifyFailures does not manage the state save of notified failures. --- GpsTracker/Core.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 48ae3b4..e20a8c6 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -28,7 +28,7 @@ namespace core { gps::preserveCurrentCoordinates(); } - notifyFailures(metadata); + alerts::add(notifyFailures(metadata)); alerts::clear(metadata); positions::doBackup(forceBackup); @@ -36,13 +36,12 @@ namespace core { mainunit::deepSleep(sleepTime); } - void notifyFailures(PositionEntryMetadata &metadata) { + uint8_t notifyFailures(PositionEntryMetadata &metadata) { uint8_t triggered = alerts::getTriggered(metadata); - uint8_t notified = 0; SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; - if (!triggered) return; + if (!triggered) return 0; network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); @@ -53,18 +52,18 @@ namespace core { sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); } - if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2f°C. Backup battery failure ?\n"), metadata.batteryLevel); + if (bitRead(triggered, ALERT_RTC_FAILURE)) { + sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2fC. Backup battery failure ?\n"), metadata.batteryLevel); } config_t* config = &config::main::value; if (!network::sendSms(config->contactPhone, buffer)) { NOTICE_MSG("notifyFailure", "SMS not sent !"); - return; + return 0; } - alerts::add(notified); //only add the successly notified failures - //TODO : network::powerOff(); count "handles" like for i2c ? + network::powerOff(); + return triggered; } void updateRtcTime() { From 93b97e377ebe9708bded8bd29ad6203e0ea81c65 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:28:13 +1200 Subject: [PATCH 08/74] Added handle count of gps and network instances --- GpsTracker/Hardware.cpp | 56 ++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index 640ed9d..edc0f65 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -18,6 +18,8 @@ namespace hardware { namespace sim808 { SoftwareSerial simSerial = SoftwareSerial(SIM_TX, SIM_RX); SIM808 device = SIM808(SIM_RST, SIM_PWR, SIM_STATUS); + uint8_t networkPoweredCount = 0; + uint8_t gpsPoweredCount = 0; void powerOn() { VERBOSE("powerOn"); @@ -25,11 +27,13 @@ namespace hardware { if (!poweredOn) return; device.init(); + networkPoweredCount = gpsPoweredCount = 0; } void powerOff() { VERBOSE("powerOff"); device.powerOnOff(false); + networkPoweredCount = gpsPoweredCount = 0; } void powerOffIfUnused() { @@ -49,33 +53,55 @@ namespace hardware { } void gpsPowerOn() { - VERBOSE("gpsPowerOn"); - powerOn(); - device.enableGps(); + if (!gpsPoweredCount) { + VERBOSE("gpsPowerOn"); + powerOn(); + device.enableGps(); + } + + gpsPoweredCount++; } void gpsPowerOff() { - if (!device.powered()) return; + if (!device.powered()) { + networkPoweredCount = gpsPoweredCount = 0; + return; + } + + if (gpsPoweredCount == 1) { + VERBOSE("gpsPowerOff"); + device.disableGps(); + powerOffIfUnused(); + } - VERBOSE("gpsPowerOff"); - device.disableGps(); - powerOffIfUnused(); + gpsPoweredCount--; } void networkPowerOn() { - VERBOSE("networkPowerOn"); - powerOn(); - device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); + if (!networkPoweredCount) { + VERBOSE("networkPowerOn"); + powerOn(); + device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); + } + + networkPoweredCount++; } void networkPowerOff() { - if (!device.powered()) return; + if (!device.powered()) { + networkPoweredCount = gpsPoweredCount = 0; + return; + } - VERBOSE("networkPowerOff"); - device.disableGprs(); - device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); + if (networkPoweredCount == 1) { + VERBOSE("networkPowerOff"); + device.disableGprs(); + device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); + + powerOffIfUnused(); + } - powerOffIfUnused(); + networkPoweredCount--; } } From 3a5357a64460685460520332b4bc8f6ff2302c51 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:30:38 +1200 Subject: [PATCH 09/74] Fixed bad definition of core::notifyFailures --- GpsTracker/Alerts.h | 2 +- GpsTracker/Core.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Alerts.h b/GpsTracker/Alerts.h index 5117792..db24595 100644 --- a/GpsTracker/Alerts.h +++ b/GpsTracker/Alerts.h @@ -11,6 +11,6 @@ namespace alerts { extern uint8_t _alerts; uint8_t getTriggered(PositionEntryMetadata &metadata); - inline void add(uint8_t mask) { _alerts |= mask; } + inline void add(uint8_t mask) { _alerts |= mask; } //TODO : save to EEPROM to survive reset void clear(PositionEntryMetadata &metadata); } diff --git a/GpsTracker/Core.h b/GpsTracker/Core.h index 3e8a5f5..16ba5c7 100644 --- a/GpsTracker/Core.h +++ b/GpsTracker/Core.h @@ -18,5 +18,5 @@ namespace core { bool updateSleepTime(); uint16_t mapSleepTime(uint8_t velocity); - void notifyFailures(PositionEntryMetadata &metadata); + uint8_t notifyFailures(PositionEntryMetadata &metadata); } \ No newline at end of file From 4b049344432bc6cf9287d8d96a80ca4bb442a7fa Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:41:12 +1200 Subject: [PATCH 10/74] Added config value & initialization temporary code --- GpsTracker/Config.cpp | 11 ++++++++++- GpsTracker/Config.h | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 4948580..b130603 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -29,6 +29,11 @@ namespace config { // POSITIONS_CONFIG_NET_DEFAULT_URL, //}; //value.network = c; + + value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; + value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; + value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; + strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE); #endif } @@ -45,7 +50,7 @@ namespace config { void setup() { details::read(); - //details::write(); + details::write(); } void save() { @@ -67,6 +72,10 @@ namespace config { POSITIONS_CONFIG_NET_DEFAULT_APN, POSITIONS_CONFIG_NET_DEFAULT_URL, }, + CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, + CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, + CONFIG_DEFAULT_BATTERY_ALERT_CLEAR, + CONFIG_DEFAULT_CONTACT_PHONE #endif }; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index d5935e4..061ea7d 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -15,6 +15,10 @@ #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 #define VERSION "1.00" +#define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1 45 +#define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2 38 +#define CONFIG_DEFAULT_BATTERY_ALERT_CLEAR 60 +#define CONFIG_DEFAULT_CONTACT_PHONE "+642568452" #define SLEEP_TIMING_TIME(hours, minutes) hours * 60 + minutes From 3b08d64b1f01d7c2e4726fe6c21e0949d94b0376 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:44:21 +1200 Subject: [PATCH 11/74] sprintf on arduino cannot handle %.2f format, using temperature * 100 as in the position csv format --- GpsTracker/Core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index e20a8c6..95064c6 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -53,7 +53,7 @@ namespace core { } if (bitRead(triggered, ALERT_RTC_FAILURE)) { - sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %.2fC. Backup battery failure ?\n"), metadata.batteryLevel); + sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast(metadata.temperature * 100)); } config_t* config = &config::main::value; From dea753de7ec722a97afd1857234c32f9f02f82ef Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:55:45 +1200 Subject: [PATCH 12/74] Renamed ALERT_RTC_TEMPERATURE_FAILURE to ALERT_SUSPICIOUS_RTC_TEMPERATURE --- GpsTracker/Alerts.cpp | 4 ++-- GpsTracker/Config.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 2023741..2ab31ce 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -11,7 +11,7 @@ namespace alerts { config_t* config = &config::main::value; uint8_t result = 0; - if (metadata.temperature == ALERT_RTC_TEMPERATURE_FAILURE && !bitRead(_alerts, ALERT_RTC_FAILURE)) { + if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(_alerts, ALERT_RTC_FAILURE)) { bitSet(result, ALERT_RTC_FAILURE); } @@ -30,7 +30,7 @@ namespace alerts { config_t* config = &config::main::value; uint8_t clearMask = 0; - if (bitRead(_alerts, ALERT_RTC_FAILURE) && metadata.temperature != ALERT_RTC_TEMPERATURE_FAILURE) { + if (bitRead(_alerts, ALERT_RTC_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) { bitSet(clearMask, ALERT_RTC_FAILURE); } diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 061ea7d..d3e91b0 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -47,7 +47,7 @@ #define NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD 8 #define NETWORK_DEFAULT_NO_NETWORK_TRIES 5 -#define ALERT_RTC_TEMPERATURE_FAILURE 0 +#define ALERT_SUSPICIOUS_RTC_TEMPERATURE 0 #pragma endregion From 0ed68eeca17878eca264c8c673a3ba55a0ae3e68 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:56:06 +1200 Subject: [PATCH 13/74] Removed useless NOTICE call --- GpsTracker/NetworkPositionsBackup.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 341167d..0d10a1f 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -86,9 +86,7 @@ namespace positions { network::powerOff(); } - void NetworkPositionsBackup::setup() { - NOTICE("setup"); - } + void NetworkPositionsBackup::setup() {} void NetworkPositionsBackup::prepare() { NOTICE("prepare"); From 18648de1365d0ab508300358fa904218356e51c9 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 20:56:37 +1200 Subject: [PATCH 14/74] Added failure sms debug function and menu entry --- GpsTracker/Debug.cpp | 17 +++++++++++++++++ GpsTracker/Debug.h | 3 ++- GpsTracker/GpsTracker.ino | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index f88d1f2..9295a34 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -29,6 +29,7 @@ MENU_ENTRY(EEPROM_GET_ENTRIES, "[P] Get EEPROM entries"); MENU_ENTRY(EEPROM_GET_LAST_ENTRY, "[p] Get EEPROM last entry"); MENU_ENTRY(EEPROM_ADD_ENTRY, "[a] Add last entry to EEPROM"); MENU_ENTRY(EEPROM_BACKUP_ENTRIES, "[B] Backup EEPROM entries"); +MENU_ENTRY(SEND_FAILURE_SMS, "[F] Send failure SMS"); MENU_ENTRY(SLEEP, "[S] Sleep for 8s"); MENU_ENTRY(SLEEP_DEEP, "[s] Deep sleep for 10s"); MENU_ENTRY(QUESTION, "?"); @@ -51,6 +52,7 @@ const PROGMEM uint8_t commandIdMapping[] = { 'p', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_LAST_ENTRY), 'a', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY), 'B', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES), + 'F', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SEND_FAILURE_SMS), 'S', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP), 's', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP), }; @@ -88,6 +90,9 @@ const char * const MENU_ENTRIES[] PROGMEM = { MENU_EEPROM_BACKUP_ENTRIES, MENU_SEPARATOR, + MENU_SEND_FAILURE_SMS, + MENU_SEPARATOR, + MENU_SLEEP, MENU_SLEEP_DEEP, @@ -259,4 +264,16 @@ namespace debug { for(int i = 0; i < 10; i++) positions::appendLast(metadata); } + + void sendGlobalFailureSms() { + PositionEntryMetadata metadata = { + 3, + 3800, + ALERT_SUSPICIOUS_RTC_TEMPERATURE, + 0, + SIM808_GPS_STATUS::OFF + }; + + core::notifyFailures(metadata); + } } \ No newline at end of file diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index ee3f48f..49007ed 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -31,6 +31,7 @@ namespace debug { EEPROM_GET_ENTRIES, EEPROM_ADD_ENTRY, EEPROM_BACKUP_ENTRIES, + SEND_FAILURE_SMS, SLEEP, SLEEP_DEEP }; @@ -55,5 +56,5 @@ namespace debug { void getAndDisplayEepromLastPosition(); void addLastPositionToEeprom(); - + void sendGlobalFailureSms(); } diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 806f815..69a865d 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -80,6 +80,9 @@ void loop() { case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES: positions::doBackup(true); break; + case debug::GPSTRACKER_DEBUG_COMMAND::SEND_FAILURE_SMS: + debug::sendGlobalFailureSms(); + break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: mainunit::sleep(period_t::SLEEP_8S); break; From a2ab0c916caa1e8e7d8af56b2d006dfaa62b0946 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 21:00:59 +1200 Subject: [PATCH 15/74] Updated config read and write debug NOTICE with added configuration values --- GpsTracker/Config.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index b130603..f231d9f 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -19,7 +19,7 @@ namespace config { 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", value.seed, value.version, value.firstEntry, value.lastEntry); + NOTICE_FORMAT("read", "%d, %s, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, 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); //networkConfig_t c = { @@ -38,7 +38,7 @@ namespace config { } void write() { - NOTICE_FORMAT("write", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, 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); #endif From d8945d75d2f5e5277e30c8bc8c61d6d53bde34b6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 21:06:27 +1200 Subject: [PATCH 16/74] Added code size comments on config structs --- GpsTracker/Config.h | 20 ++++++++++---------- GpsTracker/NetworkPositionsConfig.h | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index d3e91b0..f4c689c 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -59,18 +59,18 @@ struct sleepTimings_t { }; struct config_t { - uint8_t seed; - char version[5]; - uint16_t firstEntry; - uint16_t lastEntry; + uint8_t seed; //sizeof = 1 + char version[5]; //sizeof = 5 + uint16_t firstEntry; //sizeof = 2 + uint16_t lastEntry; //sizeof = 2 #if BACKUP_ENABLE_NETWORK - networkConfig_t network; + networkConfig_t network; //sizeof = 73 #endif - uint8_t alertBatteryLevel1; - uint8_t alertBatteryLevel2; - uint8_t alertBatteryLevelClear; - char contactPhone[15]; -}; + uint8_t alertBatteryLevel1; //sizeof = 1 + uint8_t alertBatteryLevel2; //sizeof = 1 + uint8_t alertBatteryLevelClear; //sizeof = 1 + char contactPhone[15]; //sizeof = 15 +}; //sizeof = 28 + 73 = 101 namespace config { diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 3313b16..a363b7f 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -9,8 +9,8 @@ struct networkConfig_t { - uint8_t saveThreshold; - uint16_t lastSavedEntry; - char apn[20]; - char url[50]; -}; \ No newline at end of file + uint8_t saveThreshold; //sizeof = 1 + uint16_t lastSavedEntry; //sizeof = 2 + char apn[20]; //sizeof = 20 + char url[50]; //sizeof = 50 +}; //sizeof = 73 \ No newline at end of file From 95b43e3f4409fb077e4df036684aec5f671709ec Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 21:58:05 +1200 Subject: [PATCH 17/74] Added a fourth alert (RTC clock halt) and saving active alerts to eeprom to survive resets --- GpsTracker/Alerts.cpp | 37 +++++++++++++++++++++++++++---------- GpsTracker/Alerts.h | 11 +++++------ GpsTracker/Config.cpp | 6 ++++-- GpsTracker/Config.h | 3 ++- GpsTracker/Core.cpp | 10 +++++++--- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 2ab31ce..24c315c 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -2,42 +2,59 @@ #include "Alerts.h" #include "Config.h" +#include "Rtc.h" namespace alerts { - uint8_t _alerts = 0; - uint8_t getTriggered(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; uint8_t result = 0; - if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(_alerts, ALERT_RTC_FAILURE)) { - bitSet(result, ALERT_RTC_FAILURE); + if (!rtc::isAccurate() && !bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE)) { + bitSet(result, ALERT_RTC_CLOCK_FAILURE); + } + + if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE)) { + bitSet(result, ALERT_RTC_TEMPERATURE_FAILURE); } - if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(_alerts, ALERT_BATTERY_LEVEL_1)) { + if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_1)) { bitSet(result, ALERT_BATTERY_LEVEL_1); } - if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(_alerts, ALERT_BATTERY_LEVEL_2)) { + if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_2)) { bitSet(result, ALERT_BATTERY_LEVEL_2); } return result; } + void add(uint8_t mask) { + if (!mask) return; //save a write to eeprom if there is no change + + config_t* config = &config::main::value; + config->activeAlerts |= mask; + config::main::save(); + } + void clear(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; uint8_t clearMask = 0; - if (bitRead(_alerts, ALERT_RTC_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) { - bitSet(clearMask, ALERT_RTC_FAILURE); + if (bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE) && rtc::isAccurate()) { + bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); + } + + if (bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) { + bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); } - if (_alerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2) && metadata.temperature >= config->alertBatteryLevelClear)) { + if (config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2) && metadata.temperature >= config->alertBatteryLevelClear)) { clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); } - _alerts &= ~clearMask; + if (!clearMask) return; //save a write to eeprom if there is no change + config->activeAlerts &= ~clearMask; + config::main::save(); } } diff --git a/GpsTracker/Alerts.h b/GpsTracker/Alerts.h index db24595..8c7e825 100644 --- a/GpsTracker/Alerts.h +++ b/GpsTracker/Alerts.h @@ -2,15 +2,14 @@ #include "Positions.h" -#define ALERT_BATTERY_LEVEL_1 1 -#define ALERT_BATTERY_LEVEL_2 2 -#define ALERT_RTC_FAILURE 3 +#define ALERT_BATTERY_LEVEL_1 1 +#define ALERT_BATTERY_LEVEL_2 2 +#define ALERT_RTC_TEMPERATURE_FAILURE 3 +#define ALERT_RTC_CLOCK_FAILURE 4 namespace alerts { - extern uint8_t _alerts; - uint8_t getTriggered(PositionEntryMetadata &metadata); - inline void add(uint8_t mask) { _alerts |= mask; } //TODO : save to EEPROM to survive reset + void add(uint8_t mask); void clear(PositionEntryMetadata &metadata); } diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index f231d9f..f2b20f2 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -19,7 +19,7 @@ namespace config { 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, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.contactPhone); + NOTICE_FORMAT("read", "%d, %s, %d, %d, %d, %d, %d, %d, %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); //networkConfig_t c = { @@ -33,12 +33,13 @@ namespace config { value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; + value.activeAlerts = 0; strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE); #endif } void write() { - NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.contactPhone); + NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.activeAlerts, 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); #endif @@ -75,6 +76,7 @@ namespace config { CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, CONFIG_DEFAULT_BATTERY_ALERT_CLEAR, + 0, CONFIG_DEFAULT_CONTACT_PHONE #endif }; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index f4c689c..561fd4e 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -69,8 +69,9 @@ struct config_t { uint8_t alertBatteryLevel1; //sizeof = 1 uint8_t alertBatteryLevel2; //sizeof = 1 uint8_t alertBatteryLevelClear; //sizeof = 1 + uint8_t activeAlerts; //sizeof = 1 char contactPhone[15]; //sizeof = 15 -}; //sizeof = 28 + 73 = 101 +}; //sizeof = 29 + 73 = 102 namespace config { diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 95064c6..49f5231 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -23,7 +23,7 @@ namespace core { if (acquired) { positions::appendLast(metadata); - + forceBackup = updateSleepTime(); gps::preserveCurrentCoordinates(); } @@ -51,8 +51,12 @@ namespace core { if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); } - - if (bitRead(triggered, ALERT_RTC_FAILURE)) { + + if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { + sprintf_P(buffer + strlen(buffer), PSTR(" - RTC was stopped. Bakup battery failure ?\n")); + } + + if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast(metadata.temperature * 100)); } From d0ef217f75ced7886a197122018c88a3eb66f20d Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Fri, 3 Aug 2018 22:01:13 +1200 Subject: [PATCH 18/74] Reworked code to fix missing network::powerOff in notifyFailures --- GpsTracker/Core.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 49f5231..6b25e46 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -61,13 +61,11 @@ namespace core { } config_t* config = &config::main::value; - if (!network::sendSms(config->contactPhone, buffer)) { - NOTICE_MSG("notifyFailure", "SMS not sent !"); - return 0; - } + bool sent = network::sendSms(config->contactPhone, buffer); + if (!sent) NOTICE_MSG("notifyFailure", "SMS not sent !"); network::powerOff(); - return triggered; + return sent ? triggered : 0; } void updateRtcTime() { From 8f07a185c5cffc82c3368e4ef0844959c0dbe61d Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 14:37:23 +1200 Subject: [PATCH 19/74] Using snprintf instead of sprintf to build sms message --- GpsTracker/Core.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 6b25e46..da5cec6 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -40,6 +40,7 @@ namespace core { uint8_t triggered = alerts::getTriggered(metadata); SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; + size_t available = SMS_BUFFER_SIZE - strlen(buffer); if (!triggered) return 0; @@ -49,15 +50,18 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return; if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { - sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); + available = SMS_BUFFER_SIZE - strlen(buffer); + snprintf_P(buffer + strlen(buffer), available, PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); //TODO : this code will NOT print the string at the right place } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - sprintf_P(buffer + strlen(buffer), PSTR(" - RTC was stopped. Bakup battery failure ?\n")); + available = SMS_BUFFER_SIZE - strlen(buffer); + snprintf_P(buffer + strlen(buffer), available, PSTR(" - RTC was stopped. Bakup battery failure ?\n")); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast(metadata.temperature * 100)); + available = SMS_BUFFER_SIZE - strlen(buffer); + snprintf_P(buffer + strlen(buffer), available, PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast(metadata.temperature * 100)); } config_t* config = &config::main::value; From 083d7163840961a8004726e7d361276827362c9b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 14:40:41 +1200 Subject: [PATCH 20/74] Added NOTICE to sendSms. Reached 100% of program space (150 bytes left) --- GpsTracker/Network.cpp | 7 ++++++- GpsTracker/Network.h | 4 +--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 3073ca7..50d5b7b 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -42,7 +42,7 @@ namespace network { else noReliableNetwork = 0; if (noReliableNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { NOTICE_MSG("waitForRegistered", "No reliable signal"); - break; //after a while, not network really means no network. Bailing out + break; //after a while, no network really means no network. Bailing out } mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); @@ -63,4 +63,9 @@ namespace network { bool enableGprs() { return hardware::sim808::device.enableGprs(config::main::value.network.apn); } + + bool sendSms(const char * phoneNumber, const char * msg) { + NOTICE_FORMAT("sendSms", "%s, %s", phoneNumber, msg); + return hardware::sim808::device.sendSms(phoneNumber, msg); + } } \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index 1f6e8af..abae014 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -11,7 +11,5 @@ namespace network { bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); bool enableGprs(); - inline bool sendSms(const char * phoneNumber, const char * msg) { - return hardware::sim808::device.sendSms(phoneNumber, msg); - } + bool sendSms(const char * phoneNumber, const char * msg); } \ No newline at end of file From cde06ee4bad8b73f9e4326baaf0bf1bde331a7b9 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 14:58:33 +1200 Subject: [PATCH 21/74] Firmware version bump --- GpsTracker/Config.cpp | 1 + GpsTracker/Config.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index f2b20f2..5306df2 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -30,6 +30,7 @@ namespace config { //}; //value.network = c; + strcpy(value.version, VERSION); value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 561fd4e..e29df76 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -14,7 +14,7 @@ #define CONFIG_ADDR 0 #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 -#define VERSION "1.00" +#define VERSION "1.1" #define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1 45 #define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2 38 #define CONFIG_DEFAULT_BATTERY_ALERT_CLEAR 60 From 2d4bd35c63a856866dba65929a31ebc7aa1b8070 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 15:01:31 +1200 Subject: [PATCH 22/74] Disable rewrite of config values (only here for the first run on old firmwares) --- GpsTracker/Config.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 5306df2..ad3cb7a 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -29,14 +29,13 @@ namespace config { // POSITIONS_CONFIG_NET_DEFAULT_URL, //}; //value.network = c; - - strcpy(value.version, VERSION); +#endif + /*strcpy(value.version, VERSION); value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; value.activeAlerts = 0; - strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE); -#endif + strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE);*/ } void write() { @@ -52,7 +51,7 @@ namespace config { void setup() { details::read(); - details::write(); + //details::write(); } void save() { From 472f788e73870a8e4f02dfc27d8d78811157e3cf Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 15:15:24 +1200 Subject: [PATCH 23/74] Fixed bad condition for battery level alerts clearance --- GpsTracker/Alerts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 24c315c..1296c29 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -49,7 +49,7 @@ namespace alerts { bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); } - if (config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2) && metadata.temperature >= config->alertBatteryLevelClear)) { + if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.temperature >= config->alertBatteryLevelClear) { clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); } From 390d9d2acac3ec92e3846014f32a6d6731ae064b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 15:20:00 +1200 Subject: [PATCH 24/74] Using binary format for active alerts config values notice --- GpsTracker/Config.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index ad3cb7a..f63ae1b 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -19,7 +19,7 @@ namespace config { 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, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + 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); #if BACKUP_ENABLE_NETWORK NOTICE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); //networkConfig_t c = { @@ -39,7 +39,7 @@ namespace config { } void write() { - NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.activeAlerts, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %B, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.activeAlerts, 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); #endif From 593fd71e982ab7b50ba353cd2320ff9c886ba86f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 15:20:59 +1200 Subject: [PATCH 25/74] Removed additional activeAlerts format parameter in config write --- GpsTracker/Config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index f63ae1b..3174880 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -39,7 +39,7 @@ 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.activeAlerts, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone); + 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); #if BACKUP_ENABLE_NETWORK NOTICE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); #endif From e2bfba407ff88f213c2c35d7d09a17f1d5ec8459 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 15:24:53 +1200 Subject: [PATCH 26/74] Sadly, version string must do exactly 4 chars to avoid leaving traces of old version string --- GpsTracker/Config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index e29df76..c057de4 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -14,7 +14,7 @@ #define CONFIG_ADDR 0 #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 -#define VERSION "1.1" +#define VERSION "1.10" #define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1 45 #define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2 38 #define CONFIG_DEFAULT_BATTERY_ALERT_CLEAR 60 From 43a4c8c7afe780e51cd6b09a8b44e3fc77181f42 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 16:27:39 +1200 Subject: [PATCH 27/74] Optimized alert messaging creation using flash string --- GpsTracker/Core.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index da5cec6..dc60cfa 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -40,7 +40,8 @@ namespace core { uint8_t triggered = alerts::getTriggered(metadata); SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; - size_t available = SMS_BUFFER_SIZE - strlen(buffer); + size_t bufferLeft = 0; + const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); if (!triggered) return 0; @@ -50,18 +51,18 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return; if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { - available = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), available, PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); //TODO : this code will NOT print the string at the right place + bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); + snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - available = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), available, PSTR(" - RTC was stopped. Bakup battery failure ?\n")); + bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); + snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR(" - RTC was stopped. %S"), backupFailureString); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - available = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), available, PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast(metadata.temperature * 100)); + bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); + snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR(" - Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); } config_t* config = &config::main::value; From 1c69e884b09ca41a923ab26cdf7e011c4a3ff1b5 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 16:30:31 +1200 Subject: [PATCH 28/74] Increased SMS buffer size to match possible all messages --- GpsTracker/Core.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index dc60cfa..5837361 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -4,7 +4,7 @@ #include "Alerts.h" #define LOGGER_NAME "Core" -#define SMS_BUFFER_SIZE 100 +#define SMS_BUFFER_SIZE 140 using namespace utils; @@ -52,17 +52,17 @@ namespace core { if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); + snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR(" - RTC was stopped. %S"), backupFailureString); + snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR("- RTC was stopped. %S"), backupFailureString); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR(" - Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); + snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); } config_t* config = &config::main::value; From c303ca86fcb3630a6cdd4a0b5e1f3b2fad48da4d Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 16:54:20 +1200 Subject: [PATCH 29/74] Moved and renamed code for clarity --- GpsTracker/Core.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 5837361..3e4e46b 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -4,7 +4,8 @@ #include "Alerts.h" #define LOGGER_NAME "Core" -#define SMS_BUFFER_SIZE 140 +#define SMS_BUFFER_SIZE 140 +#define NO_ALERTS_NOTIFIED 0 using namespace utils; @@ -37,18 +38,18 @@ namespace core { } uint8_t notifyFailures(PositionEntryMetadata &metadata) { - uint8_t triggered = alerts::getTriggered(metadata); SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; size_t bufferLeft = 0; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); - if (!triggered) return 0; + uint8_t triggered = alerts::getTriggered(metadata); + if (!triggered) return NO_ALERTS_NOTIFIED; network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus.stat)) return; + if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); @@ -66,11 +67,11 @@ namespace core { } config_t* config = &config::main::value; - bool sent = network::sendSms(config->contactPhone, buffer); - if (!sent) NOTICE_MSG("notifyFailure", "SMS not sent !"); + bool notified = network::sendSms(config->contactPhone, buffer); + if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); network::powerOff(); - return sent ? triggered : 0; + return notified ? triggered : NO_ALERTS_NOTIFIED; //If not notified, the alerts state should not be persisted (so we can retry to notify them) } void updateRtcTime() { From 8b01ba9a5a419c02feb9816208d5e40ac64b379f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 17:20:05 +1200 Subject: [PATCH 30/74] Reduced code size by refactoring sms buffer management --- GpsTracker/Core.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 3e4e46b..b6cc7d1 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -14,6 +14,21 @@ namespace core { uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS; uint8_t stoppedInARow = SLEEP_DEFAULT_STOPPED_THRESHOLD - 1; + namespace details { + + void appendToSmsBuffer(char * buffer, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + + size_t bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); + char * p = buffer + strlen(buffer); + vsnprintf_P(p, bufferLeft, fmt, args); + + va_end(args); + } + + } + void main() { bool forceBackup = false; bool acquired = false; @@ -40,7 +55,6 @@ namespace core { uint8_t notifyFailures(PositionEntryMetadata &metadata) { SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; - size_t bufferLeft = 0; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); uint8_t triggered = alerts::getTriggered(metadata); @@ -52,18 +66,15 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { - bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); + details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR("- RTC was stopped. %S"), backupFailureString); + details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped. %S"), backupFailureString); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - snprintf_P(buffer + strlen(buffer), bufferLeft, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); + details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); } config_t* config = &config::main::value; From 6ec9d190941180e6fc71ce6f77db775f1529a92a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 17:24:51 +1200 Subject: [PATCH 31/74] Moved contact phone number inside sendSms (since there is one number, no need for core to know it) --- GpsTracker/Core.cpp | 3 +-- GpsTracker/Network.cpp | 4 +++- GpsTracker/Network.h | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index b6cc7d1..60384ca 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -77,8 +77,7 @@ namespace core { details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); } - config_t* config = &config::main::value; - bool notified = network::sendSms(config->contactPhone, buffer); + bool notified = network::sendSms(buffer); if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); network::powerOff(); diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 50d5b7b..5ec6ff6 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -64,7 +64,9 @@ namespace network { return hardware::sim808::device.enableGprs(config::main::value.network.apn); } - bool sendSms(const char * phoneNumber, const char * msg) { + bool sendSms(const char * msg) { + const char * phoneNumber = config::main::value.contactPhone; + NOTICE_FORMAT("sendSms", "%s, %s", phoneNumber, msg); return hardware::sim808::device.sendSms(phoneNumber, msg); } diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index abae014..80334e8 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -11,5 +11,5 @@ namespace network { bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); bool enableGprs(); - bool sendSms(const char * phoneNumber, const char * msg); + bool sendSms(const char * msg); } \ No newline at end of file From d5dfd3a461d9989d286d22e21a6ac33a2a0d45a3 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 17:31:39 +1200 Subject: [PATCH 32/74] Renamed debug notify failures entry --- GpsTracker/Debug.cpp | 8 ++++---- GpsTracker/Debug.h | 4 ++-- GpsTracker/GpsTracker.ino | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 9295a34..6605b2c 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -29,7 +29,7 @@ MENU_ENTRY(EEPROM_GET_ENTRIES, "[P] Get EEPROM entries"); MENU_ENTRY(EEPROM_GET_LAST_ENTRY, "[p] Get EEPROM last entry"); MENU_ENTRY(EEPROM_ADD_ENTRY, "[a] Add last entry to EEPROM"); MENU_ENTRY(EEPROM_BACKUP_ENTRIES, "[B] Backup EEPROM entries"); -MENU_ENTRY(SEND_FAILURE_SMS, "[F] Send failure SMS"); +MENU_ENTRY(NOTIFY_FAILURES, "[F] Notify failures"); MENU_ENTRY(SLEEP, "[S] Sleep for 8s"); MENU_ENTRY(SLEEP_DEEP, "[s] Deep sleep for 10s"); MENU_ENTRY(QUESTION, "?"); @@ -52,7 +52,7 @@ const PROGMEM uint8_t commandIdMapping[] = { 'p', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_GET_LAST_ENTRY), 'a', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY), 'B', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES), - 'F', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SEND_FAILURE_SMS), + 'F', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::NOTIFY_FAILURES), 'S', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP), 's', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP), }; @@ -90,7 +90,7 @@ const char * const MENU_ENTRIES[] PROGMEM = { MENU_EEPROM_BACKUP_ENTRIES, MENU_SEPARATOR, - MENU_SEND_FAILURE_SMS, + MENU_NOTIFY_FAILURES, MENU_SEPARATOR, MENU_SLEEP, @@ -265,7 +265,7 @@ namespace debug { for(int i = 0; i < 10; i++) positions::appendLast(metadata); } - void sendGlobalFailureSms() { + void notifyFailures() { PositionEntryMetadata metadata = { 3, 3800, diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index 49007ed..db3ffbe 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -31,7 +31,7 @@ namespace debug { EEPROM_GET_ENTRIES, EEPROM_ADD_ENTRY, EEPROM_BACKUP_ENTRIES, - SEND_FAILURE_SMS, + NOTIFY_FAILURES, SLEEP, SLEEP_DEEP }; @@ -56,5 +56,5 @@ namespace debug { void getAndDisplayEepromLastPosition(); void addLastPositionToEeprom(); - void sendGlobalFailureSms(); + void notifyFailures(); } diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 69a865d..1e6b7fd 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -80,8 +80,8 @@ void loop() { case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES: positions::doBackup(true); break; - case debug::GPSTRACKER_DEBUG_COMMAND::SEND_FAILURE_SMS: - debug::sendGlobalFailureSms(); + case debug::GPSTRACKER_DEBUG_COMMAND::NOTIFY_FAILURES: + debug::notifyFailures(); break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: mainunit::sleep(period_t::SLEEP_8S); From da048d65f829ec7495930195977710f867dec909 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 17:48:26 +1200 Subject: [PATCH 33/74] Explained values in debug notifyFailures --- GpsTracker/Debug.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 6605b2c..1ebb8ae 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -267,8 +267,8 @@ namespace debug { void notifyFailures() { PositionEntryMetadata metadata = { - 3, - 3800, + 1, //all battery alert should goes on with this + 3800, //doesn't matter ALERT_SUSPICIOUS_RTC_TEMPERATURE, 0, SIM808_GPS_STATUS::OFF From ca97ccd13fe951fd8cc523575174371bbb09a9a5 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 18:00:04 +1200 Subject: [PATCH 34/74] Added CONFIG_DEFAULT_ACTIVE_ALERTS constant for readibility --- GpsTracker/Config.cpp | 4 ++-- GpsTracker/Config.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 3174880..0b79a58 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -34,7 +34,7 @@ namespace config { value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; - value.activeAlerts = 0; + value.activeAlerts = CONFIG_DEFAULT_ACTIVE_ALERTS; strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE);*/ } @@ -76,7 +76,7 @@ namespace config { CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, CONFIG_DEFAULT_BATTERY_ALERT_CLEAR, - 0, + CONFIG_DEFAULT_ACTIVE_ALERTS, CONFIG_DEFAULT_CONTACT_PHONE #endif }; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index c057de4..a5fc792 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -18,6 +18,7 @@ #define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1 45 #define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2 38 #define CONFIG_DEFAULT_BATTERY_ALERT_CLEAR 60 +#define CONFIG_DEFAULT_ACTIVE_ALERTS 0 #define CONFIG_DEFAULT_CONTACT_PHONE "+642568452" #define SLEEP_TIMING_TIME(hours, minutes) hours * 60 + minutes From 1083522e5507c1168fc65a95881a5771505b2898 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 18:07:54 +1200 Subject: [PATCH 35/74] Added clear alerts debug command --- GpsTracker/Debug.cpp | 16 ++++++++++++++++ GpsTracker/Debug.h | 2 ++ GpsTracker/GpsTracker.ino | 3 +++ 3 files changed, 21 insertions(+) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 1ebb8ae..7da8b83 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -2,6 +2,7 @@ #include "Flash.h" #include "Positions.h" #include "Core.h" +#include "Alerts.h" #define LOGGER_NAME "Debug" @@ -30,6 +31,7 @@ MENU_ENTRY(EEPROM_GET_LAST_ENTRY, "[p] Get EEPROM last entry"); MENU_ENTRY(EEPROM_ADD_ENTRY, "[a] Add last entry to EEPROM"); MENU_ENTRY(EEPROM_BACKUP_ENTRIES, "[B] Backup EEPROM entries"); MENU_ENTRY(NOTIFY_FAILURES, "[F] Notify failures"); +MENU_ENTRY(CLEAR_ALERTS, "[A] Clear alerts"); MENU_ENTRY(SLEEP, "[S] Sleep for 8s"); MENU_ENTRY(SLEEP_DEEP, "[s] Deep sleep for 10s"); MENU_ENTRY(QUESTION, "?"); @@ -53,6 +55,7 @@ const PROGMEM uint8_t commandIdMapping[] = { 'a', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY), 'B', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES), 'F', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::NOTIFY_FAILURES), + 'A', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::CLEAR_ALERTS), 'S', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP), 's', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP), }; @@ -91,6 +94,7 @@ const char * const MENU_ENTRIES[] PROGMEM = { MENU_SEPARATOR, MENU_NOTIFY_FAILURES, + MENU_CLEAR_ALERTS, MENU_SEPARATOR, MENU_SLEEP, @@ -276,4 +280,16 @@ namespace debug { core::notifyFailures(metadata); } + + void clearAlerts() { + PositionEntryMetadata metadata = { + 100, //all battery alert should goes off with this + 3800, //doesn't matter + 10, + 0, + SIM808_GPS_STATUS::OFF + }; + + alerts::clear(metadata); + } } \ No newline at end of file diff --git a/GpsTracker/Debug.h b/GpsTracker/Debug.h index db3ffbe..d9a1cd0 100644 --- a/GpsTracker/Debug.h +++ b/GpsTracker/Debug.h @@ -32,6 +32,7 @@ namespace debug { EEPROM_ADD_ENTRY, EEPROM_BACKUP_ENTRIES, NOTIFY_FAILURES, + CLEAR_ALERTS, SLEEP, SLEEP_DEEP }; @@ -57,4 +58,5 @@ namespace debug { void addLastPositionToEeprom(); void notifyFailures(); + void clearAlerts(); } diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 1e6b7fd..6e382a1 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -83,6 +83,9 @@ void loop() { case debug::GPSTRACKER_DEBUG_COMMAND::NOTIFY_FAILURES: debug::notifyFailures(); break; + case debug::GPSTRACKER_DEBUG_COMMAND::CLEAR_ALERTS: + debug::clearAlerts(); + break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: mainunit::sleep(period_t::SLEEP_8S); break; From 9c38ff50985cd0ab0f5391a6dca7e6e0ae099391 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 19:48:28 +1200 Subject: [PATCH 36/74] Long awaited bug fix about all eeprom positions being sent again --- GpsTracker/NetworkPositionsBackup.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 0d10a1f..fdc8633 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -56,6 +56,9 @@ namespace positions { PositionEntry currentEntry; SIM808RegistrationStatus networkStatus; + //avoid edge case where if 0, whole set of positions will be sent again + if (!positions::count(config::main::value.network.lastSavedEntry)) return; + network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); From c00f4f480a4913b21a6edbc92715ae4711336b00 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 20:06:44 +1200 Subject: [PATCH 37/74] Using a PROGMEM format string for http body formatting --- GpsTracker/NetworkPositionsBackup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index fdc8633..5ec6c1e 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -26,7 +26,7 @@ namespace positions { bool NetworkPositionsBackup::appendPosition(PositionEntry &entry) { char buffer[BUFFER_SIZE]; - snprintf(buffer, BUFFER_SIZE, "%d,%d,%d,%d,%d,%d,%d,", + snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,"), debug::freeRam(), hardware::sim808::device.getSignalQuality().attenuation, entry.metadata.batteryLevel, From 5aa4eed1744362aed1bc010b02cd6d6b99a71717 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 20:13:43 +1200 Subject: [PATCH 38/74] Using a common buffer for both http and sms --- GpsTracker/Buffer.cpp | 9 +++++++++ GpsTracker/Buffer.h | 11 +++++++++++ GpsTracker/Core.cpp | 20 +++++++++++--------- GpsTracker/NetworkPositionsBackup.cpp | 14 +++++++------- 4 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 GpsTracker/Buffer.cpp create mode 100644 GpsTracker/Buffer.h diff --git a/GpsTracker/Buffer.cpp b/GpsTracker/Buffer.cpp new file mode 100644 index 0000000..c119b2f --- /dev/null +++ b/GpsTracker/Buffer.cpp @@ -0,0 +1,9 @@ +#include "Buffer.h" +#include "string.h" + + +namespace buffer { + char value[BUFFER_SIZE]; + + void clear() { memset(buffer::value, NULL, BUFFER_SIZE); } +} \ No newline at end of file diff --git a/GpsTracker/Buffer.h b/GpsTracker/Buffer.h new file mode 100644 index 0000000..8b92e30 --- /dev/null +++ b/GpsTracker/Buffer.h @@ -0,0 +1,11 @@ +#pragma once + +#define BUFFER_SIZE 170 + + +namespace buffer { + extern char value[BUFFER_SIZE]; + + void clear(); + +} \ No newline at end of file diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 60384ca..b04ec30 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -2,9 +2,10 @@ #include "Config.h" #include "Flash.h" #include "Alerts.h" +#include "Buffer.h" #define LOGGER_NAME "Core" -#define SMS_BUFFER_SIZE 140 +#define SMS_MAX_SIZE 140 #define NO_ALERTS_NOTIFIED 0 using namespace utils; @@ -16,12 +17,12 @@ namespace core { namespace details { - void appendToSmsBuffer(char * buffer, const char * fmt, ...) { + void appendToSmsBuffer(const char * fmt, ...) { va_list args; va_start(args, fmt); - size_t bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); - char * p = buffer + strlen(buffer); + size_t bufferLeft = SMS_MAX_SIZE - strlen(buffer::value); + char * p = buffer::value + strlen(buffer::value); vsnprintf_P(p, bufferLeft, fmt, args); va_end(args); @@ -54,7 +55,6 @@ namespace core { uint8_t notifyFailures(PositionEntryMetadata &metadata) { SIM808RegistrationStatus networkStatus; - char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); uint8_t triggered = alerts::getTriggered(metadata); @@ -65,19 +65,21 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; + buffer::clear(); + strcpy_P(buffer::value, PSTR("Alerts !\n")); if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { - details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); + details::appendToSmsBuffer(PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped. %S"), backupFailureString); + details::appendToSmsBuffer(PSTR("-RTC was stopped. %S"), backupFailureString); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); + details::appendToSmsBuffer(PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); } - bool notified = network::sendSms(buffer); + bool notified = network::sendSms(buffer::value); if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); network::powerOff(); diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 5ec6c1e..95f8651 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -7,9 +7,9 @@ #include "Debug.h" #include "Hardware.h" #include "Network.h" +#include "Buffer.h" #define LOGGER_NAME "Positions::backup::network" -#define BUFFER_SIZE 170 namespace positions { namespace backup { @@ -25,8 +25,8 @@ namespace positions { } bool NetworkPositionsBackup::appendPosition(PositionEntry &entry) { - char buffer[BUFFER_SIZE]; - snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,"), + buffer::clear(); + snprintf_P(buffer::value, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,"), debug::freeRam(), hardware::sim808::device.getSignalQuality().attenuation, entry.metadata.batteryLevel, @@ -35,14 +35,14 @@ namespace positions { static_cast(entry.metadata.status), entry.metadata.timeToFix); - strcat(buffer, entry.position); + strcat(buffer::value, entry.position); - NOTICE_FORMAT("appendPosition", "Sending : %s", buffer); + NOTICE_FORMAT("appendPosition", "Sending : %s", buffer::value); uint16_t responseCode = hardware::sim808::device.httpPost( config::main::value.network.url, F("text/gpstracker"), - buffer, - buffer, + buffer::value, + buffer::value, BUFFER_SIZE ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; From 94d0b53a8eff2419a465fca57bf9a9c925cf4075 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 21:15:26 +1200 Subject: [PATCH 39/74] Revert "Using a common buffer for both http and sms" This reverts commit 3cfc7074d1ad15a3ea13c2eef45c2aac6e3d161e. Common buffer implies that the buffer goes to static memory, which overflow the available SRAM. --- GpsTracker/Buffer.cpp | 9 --------- GpsTracker/Buffer.h | 11 ----------- GpsTracker/Core.cpp | 20 +++++++++----------- GpsTracker/NetworkPositionsBackup.cpp | 14 +++++++------- 4 files changed, 16 insertions(+), 38 deletions(-) delete mode 100644 GpsTracker/Buffer.cpp delete mode 100644 GpsTracker/Buffer.h diff --git a/GpsTracker/Buffer.cpp b/GpsTracker/Buffer.cpp deleted file mode 100644 index c119b2f..0000000 --- a/GpsTracker/Buffer.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "Buffer.h" -#include "string.h" - - -namespace buffer { - char value[BUFFER_SIZE]; - - void clear() { memset(buffer::value, NULL, BUFFER_SIZE); } -} \ No newline at end of file diff --git a/GpsTracker/Buffer.h b/GpsTracker/Buffer.h deleted file mode 100644 index 8b92e30..0000000 --- a/GpsTracker/Buffer.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#define BUFFER_SIZE 170 - - -namespace buffer { - extern char value[BUFFER_SIZE]; - - void clear(); - -} \ No newline at end of file diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index b04ec30..60384ca 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -2,10 +2,9 @@ #include "Config.h" #include "Flash.h" #include "Alerts.h" -#include "Buffer.h" #define LOGGER_NAME "Core" -#define SMS_MAX_SIZE 140 +#define SMS_BUFFER_SIZE 140 #define NO_ALERTS_NOTIFIED 0 using namespace utils; @@ -17,12 +16,12 @@ namespace core { namespace details { - void appendToSmsBuffer(const char * fmt, ...) { + void appendToSmsBuffer(char * buffer, const char * fmt, ...) { va_list args; va_start(args, fmt); - size_t bufferLeft = SMS_MAX_SIZE - strlen(buffer::value); - char * p = buffer::value + strlen(buffer::value); + size_t bufferLeft = SMS_BUFFER_SIZE - strlen(buffer); + char * p = buffer + strlen(buffer); vsnprintf_P(p, bufferLeft, fmt, args); va_end(args); @@ -55,6 +54,7 @@ namespace core { uint8_t notifyFailures(PositionEntryMetadata &metadata) { SIM808RegistrationStatus networkStatus; + char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); uint8_t triggered = alerts::getTriggered(metadata); @@ -65,21 +65,19 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; - buffer::clear(); - strcpy_P(buffer::value, PSTR("Alerts !\n")); if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { - details::appendToSmsBuffer(PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); + details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - details::appendToSmsBuffer(PSTR("-RTC was stopped. %S"), backupFailureString); + details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped. %S"), backupFailureString); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - details::appendToSmsBuffer(PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); + details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); } - bool notified = network::sendSms(buffer::value); + bool notified = network::sendSms(buffer); if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); network::powerOff(); diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 95f8651..5ec6c1e 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -7,9 +7,9 @@ #include "Debug.h" #include "Hardware.h" #include "Network.h" -#include "Buffer.h" #define LOGGER_NAME "Positions::backup::network" +#define BUFFER_SIZE 170 namespace positions { namespace backup { @@ -25,8 +25,8 @@ namespace positions { } bool NetworkPositionsBackup::appendPosition(PositionEntry &entry) { - buffer::clear(); - snprintf_P(buffer::value, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,"), + char buffer[BUFFER_SIZE]; + snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,"), debug::freeRam(), hardware::sim808::device.getSignalQuality().attenuation, entry.metadata.batteryLevel, @@ -35,14 +35,14 @@ namespace positions { static_cast(entry.metadata.status), entry.metadata.timeToFix); - strcat(buffer::value, entry.position); + strcat(buffer, entry.position); - NOTICE_FORMAT("appendPosition", "Sending : %s", buffer::value); + NOTICE_FORMAT("appendPosition", "Sending : %s", buffer); uint16_t responseCode = hardware::sim808::device.httpPost( config::main::value.network.url, F("text/gpstracker"), - buffer::value, - buffer::value, + buffer, + buffer, BUFFER_SIZE ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; From 56fa127bb75a38296b3f21264b1eda7fdca62fa4 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 21:17:55 +1200 Subject: [PATCH 40/74] Using a PROGMEM string for sms buffer init. Side effects : avoid gcc optimization that declares the buffer as a global variable, which overflow the available SRAM --- GpsTracker/Core.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 60384ca..ecb9f37 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -54,7 +54,7 @@ namespace core { uint8_t notifyFailures(PositionEntryMetadata &metadata) { SIM808RegistrationStatus networkStatus; - char buffer[SMS_BUFFER_SIZE] = "Alerts !\n"; + char buffer[SMS_BUFFER_SIZE]; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); uint8_t triggered = alerts::getTriggered(metadata); @@ -65,6 +65,7 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; + details::appendToSmsBuffer(buffer, PSTR("Alerts !\n")); if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } From 9655908caeeed9ef9eef9d106ec48d7bbe880ec6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 21:24:46 +1200 Subject: [PATCH 41/74] Using printf with entry.position instead of strcat (-24 bytes) --- GpsTracker/NetworkPositionsBackup.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/GpsTracker/NetworkPositionsBackup.cpp index 5ec6c1e..127cac1 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -26,16 +26,15 @@ namespace positions { bool NetworkPositionsBackup::appendPosition(PositionEntry &entry) { char buffer[BUFFER_SIZE]; - snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,"), + snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%d,%d,%d,%d,%d,%s"), debug::freeRam(), hardware::sim808::device.getSignalQuality().attenuation, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, static_cast(entry.metadata.temperature * 100), static_cast(entry.metadata.status), - entry.metadata.timeToFix); - - strcat(buffer, entry.position); + entry.metadata.timeToFix, + entry.position); NOTICE_FORMAT("appendPosition", "Sending : %s", buffer); uint16_t responseCode = hardware::sim808::device.httpPost( From 25bad44a0a79a182b6f49181153ab65fcba33f48 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 21:34:24 +1200 Subject: [PATCH 42/74] Fixed bad conditional compilation location --- GpsTracker/Config.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 0b79a58..771b057 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -73,12 +73,12 @@ namespace config { POSITIONS_CONFIG_NET_DEFAULT_APN, POSITIONS_CONFIG_NET_DEFAULT_URL, }, +#endif CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, CONFIG_DEFAULT_BATTERY_ALERT_CLEAR, CONFIG_DEFAULT_ACTIVE_ALERTS, CONFIG_DEFAULT_CONTACT_PHONE -#endif }; value = config; From 0bc78bea88b08bc08419848eebb06a6af33cbc7b Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 21:35:40 +1200 Subject: [PATCH 43/74] Added reserved space is the case where network backup is disabled --- GpsTracker/Config.cpp | 2 ++ GpsTracker/Config.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index 771b057..dfed77e 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -73,6 +73,8 @@ namespace config { POSITIONS_CONFIG_NET_DEFAULT_APN, POSITIONS_CONFIG_NET_DEFAULT_URL, }, +#else + "", #endif CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index a5fc792..5171454 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -66,6 +66,8 @@ struct config_t { uint16_t lastEntry; //sizeof = 2 #if BACKUP_ENABLE_NETWORK networkConfig_t network; //sizeof = 73 +#else + char reserved[73]; #endif uint8_t alertBatteryLevel1; //sizeof = 1 uint8_t alertBatteryLevel2; //sizeof = 1 From 4a94de380009835f619db15e8abdca029d21655c Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sat, 4 Aug 2018 21:56:27 +1200 Subject: [PATCH 44/74] Reworked config reset code to avoid static init that eats SRAM --- GpsTracker/Config.cpp | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index dfed77e..2328035 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -30,12 +30,12 @@ namespace config { //}; //value.network = c; #endif - /*strcpy(value.version, VERSION); + /*strcpy_P(value.version, PSTR(VERSION)); value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; value.activeAlerts = CONFIG_DEFAULT_ACTIVE_ALERTS; - strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE);*/ + strcpy_P(config.contactPhone, PSTR(CONFIG_DEFAULT_CONTACT_PHONE));*/ } void write() { @@ -61,27 +61,22 @@ namespace config { void reset() { VERBOSE("reset"); - config_t config = { - CONFIG_SEED, - VERSION, - 0xFFFF, - 0xFFFF, + config_t config = {}; + config.seed = CONFIG_SEED; + strcpy_P(config.version, PSTR(VERSION)); + config.firstEntry = config.lastEntry = 0xFFFF; + config.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; + config.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; + config.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; + config.activeAlerts = CONFIG_DEFAULT_ACTIVE_ALERTS; + strcpy_P(config.contactPhone, PSTR(CONFIG_DEFAULT_CONTACT_PHONE)); + #if BACKUP_ENABLE_NETWORK - { - POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, - 0xFFFF, - POSITIONS_CONFIG_NET_DEFAULT_APN, - POSITIONS_CONFIG_NET_DEFAULT_URL, - }, -#else - "", + config.network.saveThreshold = POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD; + config.network.lastSavedEntry = 0xFFFF; + strcpy_P(config.network.apn, PSTR(POSITIONS_CONFIG_NET_DEFAULT_APN)); + strcpy_P(config.network.url, PSTR(POSITIONS_CONFIG_NET_DEFAULT_URL)); #endif - CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, - CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, - CONFIG_DEFAULT_BATTERY_ALERT_CLEAR, - CONFIG_DEFAULT_ACTIVE_ALERTS, - CONFIG_DEFAULT_CONTACT_PHONE - }; value = config; save(); From 75b3b1ad407dd68f6d90f9669934b061b742913d Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 17:04:11 +1200 Subject: [PATCH 45/74] Fixed alerts not starting at first bit --- GpsTracker/Alerts.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Alerts.h b/GpsTracker/Alerts.h index 8c7e825..a0edc99 100644 --- a/GpsTracker/Alerts.h +++ b/GpsTracker/Alerts.h @@ -2,10 +2,10 @@ #include "Positions.h" -#define ALERT_BATTERY_LEVEL_1 1 -#define ALERT_BATTERY_LEVEL_2 2 -#define ALERT_RTC_TEMPERATURE_FAILURE 3 -#define ALERT_RTC_CLOCK_FAILURE 4 +#define ALERT_BATTERY_LEVEL_1 0 +#define ALERT_BATTERY_LEVEL_2 1 +#define ALERT_RTC_TEMPERATURE_FAILURE 2 +#define ALERT_RTC_CLOCK_FAILURE 3 namespace alerts { From 28efbe5a759279caead80d955afc8b9aeb2925af Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 17:05:08 +1200 Subject: [PATCH 46/74] Moved alerts code to match alerts bits definition --- GpsTracker/Alerts.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 1296c29..0ee426b 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -1,23 +1,18 @@ #pragma once +#include "Debug.h" #include "Alerts.h" #include "Config.h" #include "Rtc.h" +#define LOGGER_NAME "Alerts" + namespace alerts { uint8_t getTriggered(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; uint8_t result = 0; - if (!rtc::isAccurate() && !bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE)) { - bitSet(result, ALERT_RTC_CLOCK_FAILURE); - } - - if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE)) { - bitSet(result, ALERT_RTC_TEMPERATURE_FAILURE); - } - if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_1)) { bitSet(result, ALERT_BATTERY_LEVEL_1); } @@ -26,6 +21,14 @@ namespace alerts { bitSet(result, ALERT_BATTERY_LEVEL_2); } + if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE)) { + bitSet(result, ALERT_RTC_TEMPERATURE_FAILURE); + } + + if (!rtc::isAccurate() && !bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE)) { + bitSet(result, ALERT_RTC_CLOCK_FAILURE); + } + return result; } @@ -41,16 +44,16 @@ namespace alerts { config_t* config = &config::main::value; uint8_t clearMask = 0; - if (bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE) && rtc::isAccurate()) { - bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); + if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.temperature >= config->alertBatteryLevelClear) { + clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); } if (bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) { bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); } - if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.temperature >= config->alertBatteryLevelClear) { - clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); + if (bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE) && rtc::isAccurate()) { + bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); } if (!clearMask) return; //save a write to eeprom if there is no change From 9882a9dc0b54cb0e96213c82b172f4d0bcc7841f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 17:06:15 +1200 Subject: [PATCH 47/74] Additional debug info for getTime and notifyFailures --- GpsTracker/Debug.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 7da8b83..2c3fffc 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -192,7 +192,7 @@ namespace debug { tmElements_t time; rtc::getTime(time); - NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second); + NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d %t", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second, rtc::isAccurate()); } void setRtcTime() { @@ -278,7 +278,8 @@ namespace debug { SIM808_GPS_STATUS::OFF }; - core::notifyFailures(metadata); + uint8_t alerts = core::notifyFailures(metadata); + NOTICE_FORMAT("notifyFailures", "result : %B", alerts); } void clearAlerts() { From 82a00d305da5d4d6b8793d539b281df680ac78a6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 17:34:28 +1200 Subject: [PATCH 48/74] Fix gps & network powered count could go to 255 --- GpsTracker/Hardware.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index edc0f65..f612ce4 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -64,7 +64,7 @@ namespace hardware { void gpsPowerOff() { if (!device.powered()) { - networkPoweredCount = gpsPoweredCount = 0; + networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0 return; } @@ -74,7 +74,7 @@ namespace hardware { powerOffIfUnused(); } - gpsPoweredCount--; + if (gpsPoweredCount) gpsPoweredCount--; //avoid 255 if 0-- } void networkPowerOn() { @@ -89,10 +89,11 @@ namespace hardware { void networkPowerOff() { if (!device.powered()) { - networkPoweredCount = gpsPoweredCount = 0; + networkPoweredCount = gpsPoweredCount = 0; //just to be sure counts == 0 return; } + if (networkPoweredCount == 1) { VERBOSE("networkPowerOff"); device.disableGprs(); @@ -101,7 +102,7 @@ namespace hardware { powerOffIfUnused(); } - networkPoweredCount--; + if (networkPoweredCount) networkPoweredCount--; //avoid 255 if 0-- } } From 741947db3dd0e5c0abd1c6868358dfa643082845 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 17:35:27 +1200 Subject: [PATCH 49/74] Fixed start of sms message not at the start of the buffer --- GpsTracker/Core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index ecb9f37..4bf2225 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -65,7 +65,7 @@ namespace core { if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; - details::appendToSmsBuffer(buffer, PSTR("Alerts !\n")); + strncpy_P(buffer, PSTR("Alerts !\n"), SMS_BUFFER_SIZE); if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } From c5c91b7eeafb9700a70c09d180bd67a3fdbeb3f8 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 17:36:58 +1200 Subject: [PATCH 50/74] Moved notifyFailures alerts to match alerts bits --- GpsTracker/Core.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 4bf2225..8518d91 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -70,16 +70,17 @@ namespace core { details::appendToSmsBuffer(buffer, PSTR("- Battery at %d%%.\n"), metadata.batteryLevel); } - if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped. %S"), backupFailureString); + if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { + details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC.%S"), static_cast(metadata.temperature * 100), backupFailureString); } - if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC. %S"), static_cast(metadata.temperature * 100), backupFailureString); + if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { + details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped.%S"), backupFailureString); } bool notified = network::sendSms(buffer); if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); + NOTICE_FORMAT("notifyFailures", "%s", buffer); network::powerOff(); return notified ? triggered : NO_ALERTS_NOTIFIED; //If not notified, the alerts state should not be persisted (so we can retry to notify them) From b99b7823944ca610260f37c199fb64cdbb005b77 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 19:09:05 +1200 Subject: [PATCH 51/74] Fixed using bad parameter for battery alerts clearance --- GpsTracker/Alerts.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 0ee426b..9a39699 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -44,7 +44,7 @@ namespace alerts { config_t* config = &config::main::value; uint8_t clearMask = 0; - if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.temperature >= config->alertBatteryLevelClear) { + if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.batteryLevel >= config->alertBatteryLevelClear) { clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); } From d2f3afae4233103a8d9b7116f0dda84a24bf137f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 19:09:54 +1200 Subject: [PATCH 52/74] Moved line return at the beginning of each line. --- GpsTracker/Core.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/GpsTracker/Core.cpp b/GpsTracker/Core.cpp index 8518d91..5c72ba7 100644 --- a/GpsTracker/Core.cpp +++ b/GpsTracker/Core.cpp @@ -55,32 +55,33 @@ namespace core { uint8_t notifyFailures(PositionEntryMetadata &metadata) { SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE]; - const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?\n"); + const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?"); uint8_t triggered = alerts::getTriggered(metadata); if (!triggered) return NO_ALERTS_NOTIFIED; + NOTICE_FORMAT("notifyFailures", "triggered : %B", triggered); + network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; - strncpy_P(buffer, PSTR("Alerts !\n"), SMS_BUFFER_SIZE); + 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("- Battery at %d%%.\n"), metadata.batteryLevel); + details::appendToSmsBuffer(buffer, PSTR("\n- Battery at %d%%."), metadata.batteryLevel); } if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("- Temperature is %dC.%S"), static_cast(metadata.temperature * 100), backupFailureString); + details::appendToSmsBuffer(buffer, PSTR("\n- Temperature is %dC.%S"), static_cast(metadata.temperature * 100), backupFailureString); } if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("-RTC was stopped.%S"), backupFailureString); + details::appendToSmsBuffer(buffer, PSTR("\n- RTC was stopped.%S"), backupFailureString); } bool notified = network::sendSms(buffer); if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); - NOTICE_FORMAT("notifyFailures", "%s", buffer); network::powerOff(); return notified ? triggered : NO_ALERTS_NOTIFIED; //If not notified, the alerts state should not be persisted (so we can retry to notify them) From 32e05881ea207cad1588bc4dc6bef0675b8198a4 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 19:10:40 +1200 Subject: [PATCH 53/74] debug::notifyFailures now calls alerts::add --- GpsTracker/Debug.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/GpsTracker/Debug.cpp b/GpsTracker/Debug.cpp index 2c3fffc..078585a 100644 --- a/GpsTracker/Debug.cpp +++ b/GpsTracker/Debug.cpp @@ -271,7 +271,7 @@ namespace debug { void notifyFailures() { PositionEntryMetadata metadata = { - 1, //all battery alert should goes on with this + 1, //all battery alerts should goes on with this 3800, //doesn't matter ALERT_SUSPICIOUS_RTC_TEMPERATURE, 0, @@ -280,11 +280,12 @@ namespace debug { uint8_t alerts = core::notifyFailures(metadata); NOTICE_FORMAT("notifyFailures", "result : %B", alerts); + alerts::add(alerts); } void clearAlerts() { PositionEntryMetadata metadata = { - 100, //all battery alert should goes off with this + 100, //all battery alerts should goes off with this 3800, //doesn't matter 10, 0, From ac38f7b003aae1cdee7a0e5921f9b1ac98e9dde6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 21:31:47 +1200 Subject: [PATCH 54/74] Reduced conditions for allerts triggering and clearing (-46 bytes) --- GpsTracker/Alerts.cpp | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 9a39699..33bab8c 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -11,25 +11,14 @@ namespace alerts { uint8_t getTriggered(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; - uint8_t result = 0; + uint8_t active = 0; - if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_1)) { - bitSet(result, ALERT_BATTERY_LEVEL_1); - } + if (metadata.batteryLevel <= config->alertBatteryLevel1) bitSet(active, ALERT_BATTERY_LEVEL_1); + if (metadata.batteryLevel <= config->alertBatteryLevel2) bitSet(active, ALERT_BATTERY_LEVEL_2); + if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(active, ALERT_RTC_TEMPERATURE_FAILURE); + if (!rtc::isAccurate()) bitSet(active, ALERT_RTC_CLOCK_FAILURE); - if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_2)) { - bitSet(result, ALERT_BATTERY_LEVEL_2); - } - - if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE)) { - bitSet(result, ALERT_RTC_TEMPERATURE_FAILURE); - } - - if (!rtc::isAccurate() && !bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE)) { - bitSet(result, ALERT_RTC_CLOCK_FAILURE); - } - - return result; + return config->activeAlerts ^ active; } void add(uint8_t mask) { @@ -44,19 +33,11 @@ namespace alerts { config_t* config = &config::main::value; uint8_t clearMask = 0; - if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.batteryLevel >= config->alertBatteryLevelClear) { - clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); - } - - if (bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) { - bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); - } - - if (bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE) && rtc::isAccurate()) { - bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); - } + if (metadata.batteryLevel >= config->alertBatteryLevelClear) clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); + if (metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); + if (rtc::isAccurate()) bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); - if (!clearMask) return; //save a write to eeprom if there is no change + if (!config->activeAlerts || !clearMask) return; //save a write to eeprom if there is no change config->activeAlerts &= ~clearMask; config::main::save(); } From cb6bacfde57986c9a1c8d535b69c6e429b0ff505 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 22:56:14 +1200 Subject: [PATCH 55/74] Moved files around for vscode --- .gitignore | 266 +----------------- GpsTracker/Alerts.cpp => Alerts.cpp | 0 GpsTracker/Alerts.h => Alerts.h | 0 GpsTracker/Config.cpp => Config.cpp | 0 GpsTracker/Config.h => Config.h | 0 GpsTracker/Core.cpp => Core.cpp | 0 GpsTracker/Core.h => Core.h | 0 GpsTracker/Debug.cpp => Debug.cpp | 0 GpsTracker/Debug.h => Debug.h | 0 GpsTracker/Flash.cpp => Flash.cpp | 0 GpsTracker/Flash.h => Flash.h | 0 GpsTracker/Gps.cpp => Gps.cpp | 0 GpsTracker/Gps.h => Gps.h | 0 GpsTracker/GpsTracker.h => GpsTracker.h | 0 GpsTracker/GpsTracker.ino => GpsTracker.ino | 0 GpsTracker/~AutoRecover.GpsTracker.vcxproj0 | 143 ---------- GpsTracker/Hardware.cpp => Hardware.cpp | 0 GpsTracker/Hardware.h => Hardware.h | 0 GpsTracker/Logging.cpp => Logging.cpp | 0 GpsTracker/Logging.h => Logging.h | 0 GpsTracker/MainUnit.cpp => MainUnit.cpp | 0 GpsTracker/MainUnit.h => MainUnit.h | 0 GpsTracker/Network.cpp => Network.cpp | 0 GpsTracker/Network.h => Network.h | 0 ...nsBackup.cpp => NetworkPositionsBackup.cpp | 0 ...itionsBackup.h => NetworkPositionsBackup.h | 0 ...itionsConfig.h => NetworkPositionsConfig.h | 0 GpsTracker/Pins.h => Pins.h | 0 GpsTracker/Positions.cpp => Positions.cpp | 0 GpsTracker/Positions.h => Positions.h | 0 ...PositionsBackup.cpp => PositionsBackup.cpp | 0 .../PositionsBackup.h => PositionsBackup.h | 0 GpsTracker/Rtc.cpp => Rtc.cpp | 4 +- GpsTracker/Rtc.h => Rtc.h | 0 GpsTracker/SdCard.cpp => SdCard.cpp | 0 GpsTracker/SdCard.h => SdCard.h | 0 ...sitionsBackup.cpp => SdPositionsBackup.cpp | 0 ...SdPositionsBackup.h => SdPositionsBackup.h | 0 ...sitionsConfig.cpp => SdPositionsConfig.cpp | 0 ...SdPositionsConfig.h => SdPositionsConfig.h | 0 GpsTracker/Time2.cpp => Time2.cpp | 0 GpsTracker/Time2.h => Time2.h | 0 42 files changed, 3 insertions(+), 410 deletions(-) rename GpsTracker/Alerts.cpp => Alerts.cpp (100%) rename GpsTracker/Alerts.h => Alerts.h (100%) rename GpsTracker/Config.cpp => Config.cpp (100%) rename GpsTracker/Config.h => Config.h (100%) rename GpsTracker/Core.cpp => Core.cpp (100%) rename GpsTracker/Core.h => Core.h (100%) rename GpsTracker/Debug.cpp => Debug.cpp (100%) rename GpsTracker/Debug.h => Debug.h (100%) rename GpsTracker/Flash.cpp => Flash.cpp (100%) rename GpsTracker/Flash.h => Flash.h (100%) rename GpsTracker/Gps.cpp => Gps.cpp (100%) rename GpsTracker/Gps.h => Gps.h (100%) rename GpsTracker/GpsTracker.h => GpsTracker.h (100%) rename GpsTracker/GpsTracker.ino => GpsTracker.ino (100%) delete mode 100644 GpsTracker/~AutoRecover.GpsTracker.vcxproj0 rename GpsTracker/Hardware.cpp => Hardware.cpp (100%) rename GpsTracker/Hardware.h => Hardware.h (100%) rename GpsTracker/Logging.cpp => Logging.cpp (100%) rename GpsTracker/Logging.h => Logging.h (100%) rename GpsTracker/MainUnit.cpp => MainUnit.cpp (100%) rename GpsTracker/MainUnit.h => MainUnit.h (100%) rename GpsTracker/Network.cpp => Network.cpp (100%) rename GpsTracker/Network.h => Network.h (100%) rename GpsTracker/NetworkPositionsBackup.cpp => NetworkPositionsBackup.cpp (100%) rename GpsTracker/NetworkPositionsBackup.h => NetworkPositionsBackup.h (100%) rename GpsTracker/NetworkPositionsConfig.h => NetworkPositionsConfig.h (100%) rename GpsTracker/Pins.h => Pins.h (100%) rename GpsTracker/Positions.cpp => Positions.cpp (100%) rename GpsTracker/Positions.h => Positions.h (100%) rename GpsTracker/PositionsBackup.cpp => PositionsBackup.cpp (100%) rename GpsTracker/PositionsBackup.h => PositionsBackup.h (100%) rename GpsTracker/Rtc.cpp => Rtc.cpp (95%) rename GpsTracker/Rtc.h => Rtc.h (100%) rename GpsTracker/SdCard.cpp => SdCard.cpp (100%) rename GpsTracker/SdCard.h => SdCard.h (100%) rename GpsTracker/SdPositionsBackup.cpp => SdPositionsBackup.cpp (100%) rename GpsTracker/SdPositionsBackup.h => SdPositionsBackup.h (100%) rename GpsTracker/SdPositionsConfig.cpp => SdPositionsConfig.cpp (100%) rename GpsTracker/SdPositionsConfig.h => SdPositionsConfig.h (100%) rename GpsTracker/Time2.cpp => Time2.cpp (100%) rename GpsTracker/Time2.h => Time2.h (100%) diff --git a/.gitignore b/.gitignore index 85b7e92..371c8d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,267 +1,5 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - # Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -bld/ [Bb]in/ -[Oo]bj/ -[Ll]og/ - -# Visual Studio 2015 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUNIT -*.VisualState.xml -TestResult.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# DNX -project.lock.json -project.fragment.lock.json -artifacts/ - -*_i.c -*_p.c -*_i.h -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# JustCode is a .NET coding add-in -.JustCode - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# TODO: Comment the next line if you want to checkin your web deploy settings -# but database connection strings (with potential passwords) will be unencrypted -#*.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# The packages folder can be ignored because of Package Restore -**/packages/* -# except build/, which is used as an MSBuild target. -!**/packages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/packages/repositories.config -# NuGet v3's project.json files produces more ignoreable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -node_modules/ -orleans.codegen.cs - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -*.mdf -*.ldf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# JetBrains Rider -.idea/ -*.sln.iml - -# CodeRush -.cr/ - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -#VisualMicro -__vm/ -#Line endings unifier -.leu \ No newline at end of file +#Visual Studio code +.vscode/ \ No newline at end of file diff --git a/GpsTracker/Alerts.cpp b/Alerts.cpp similarity index 100% rename from GpsTracker/Alerts.cpp rename to Alerts.cpp diff --git a/GpsTracker/Alerts.h b/Alerts.h similarity index 100% rename from GpsTracker/Alerts.h rename to Alerts.h diff --git a/GpsTracker/Config.cpp b/Config.cpp similarity index 100% rename from GpsTracker/Config.cpp rename to Config.cpp diff --git a/GpsTracker/Config.h b/Config.h similarity index 100% rename from GpsTracker/Config.h rename to Config.h diff --git a/GpsTracker/Core.cpp b/Core.cpp similarity index 100% rename from GpsTracker/Core.cpp rename to Core.cpp diff --git a/GpsTracker/Core.h b/Core.h similarity index 100% rename from GpsTracker/Core.h rename to Core.h diff --git a/GpsTracker/Debug.cpp b/Debug.cpp similarity index 100% rename from GpsTracker/Debug.cpp rename to Debug.cpp diff --git a/GpsTracker/Debug.h b/Debug.h similarity index 100% rename from GpsTracker/Debug.h rename to Debug.h diff --git a/GpsTracker/Flash.cpp b/Flash.cpp similarity index 100% rename from GpsTracker/Flash.cpp rename to Flash.cpp diff --git a/GpsTracker/Flash.h b/Flash.h similarity index 100% rename from GpsTracker/Flash.h rename to Flash.h diff --git a/GpsTracker/Gps.cpp b/Gps.cpp similarity index 100% rename from GpsTracker/Gps.cpp rename to Gps.cpp diff --git a/GpsTracker/Gps.h b/Gps.h similarity index 100% rename from GpsTracker/Gps.h rename to Gps.h diff --git a/GpsTracker/GpsTracker.h b/GpsTracker.h similarity index 100% rename from GpsTracker/GpsTracker.h rename to GpsTracker.h diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker.ino similarity index 100% rename from GpsTracker/GpsTracker.ino rename to GpsTracker.ino diff --git a/GpsTracker/~AutoRecover.GpsTracker.vcxproj0 b/GpsTracker/~AutoRecover.GpsTracker.vcxproj0 deleted file mode 100644 index 1555d93..0000000 --- a/GpsTracker/~AutoRecover.GpsTracker.vcxproj0 +++ /dev/null @@ -1,143 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - - {C5F80730-F44F-4478-BDAE-6634EFC2CA88} - GpsTracker - GpsTracker - - - - Application - true - - - MultiByte - - - Application - false - - - true - MultiByte - - - - - - - - - - - - - - - - - - - - Level3 - Disabled - true - $(ProjectDir)..\GpsTracker;$(ProjectDir)..\..\libraries\E24;$(ProjectDir)..\..\libraries\SIM808;$(ProjectDir)..\..\libraries\uDS3231;$(ProjectDir)..\..\libraries\ArduinoLog;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src;$(ProjectDir)..\..\libraries\Low-Power;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src;$(ProjectDir)..\..\libraries\SdFat\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\libraries;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries;$(ProjectDir)..\..\libraries;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr\;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.8.1\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.3\include;%(AdditionalIncludeDirectories) - $(ProjectDir)__vm\.GpsTracker.vsarduino.h;%(ForcedIncludeFiles) - false - __AVR_ATmega328p__;__AVR_ATmega328P__;_DEBUG=1;_VMDEBUG=1;F_CPU=8000000L;ARDUINO=10805;ARDUINO_AVR_PRO;ARDUINO_ARCH_AVR;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) - - - true - - - - - Level3 - Disabled - true - true - true - $(ProjectDir)..\GpsTracker;$(ProjectDir)..\..\libraries\E24;$(ProjectDir)..\..\libraries\SIM808;$(ProjectDir)..\..\libraries\uDS3231;$(ProjectDir)..\..\libraries\ArduinoLog;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src;$(ProjectDir)..\..\libraries\Low-Power;$(ProjectDir)..\..\libraries\SdFat\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src\utility;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\libraries;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\libraries;$(ProjectDir)..\..\libraries;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr\;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.8.1\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;$(ProjectDir)..\..\..\..\..\..\..\..\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.3\include;%(AdditionalIncludeDirectories) - $(ProjectDir)__vm\.GpsTracker.vsarduino.h;%(ForcedIncludeFiles) - false - __AVR_ATmega328p__;__AVR_ATmega328P__;F_CPU=8000000L;ARDUINO=10805;ARDUINO_AVR_PRO;ARDUINO_ARCH_AVR;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) - - - true - true - true - - - - - - - VisualMicroDebugger - - - - CppCode - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/GpsTracker/Hardware.cpp b/Hardware.cpp similarity index 100% rename from GpsTracker/Hardware.cpp rename to Hardware.cpp diff --git a/GpsTracker/Hardware.h b/Hardware.h similarity index 100% rename from GpsTracker/Hardware.h rename to Hardware.h diff --git a/GpsTracker/Logging.cpp b/Logging.cpp similarity index 100% rename from GpsTracker/Logging.cpp rename to Logging.cpp diff --git a/GpsTracker/Logging.h b/Logging.h similarity index 100% rename from GpsTracker/Logging.h rename to Logging.h diff --git a/GpsTracker/MainUnit.cpp b/MainUnit.cpp similarity index 100% rename from GpsTracker/MainUnit.cpp rename to MainUnit.cpp diff --git a/GpsTracker/MainUnit.h b/MainUnit.h similarity index 100% rename from GpsTracker/MainUnit.h rename to MainUnit.h diff --git a/GpsTracker/Network.cpp b/Network.cpp similarity index 100% rename from GpsTracker/Network.cpp rename to Network.cpp diff --git a/GpsTracker/Network.h b/Network.h similarity index 100% rename from GpsTracker/Network.h rename to Network.h diff --git a/GpsTracker/NetworkPositionsBackup.cpp b/NetworkPositionsBackup.cpp similarity index 100% rename from GpsTracker/NetworkPositionsBackup.cpp rename to NetworkPositionsBackup.cpp diff --git a/GpsTracker/NetworkPositionsBackup.h b/NetworkPositionsBackup.h similarity index 100% rename from GpsTracker/NetworkPositionsBackup.h rename to NetworkPositionsBackup.h diff --git a/GpsTracker/NetworkPositionsConfig.h b/NetworkPositionsConfig.h similarity index 100% rename from GpsTracker/NetworkPositionsConfig.h rename to NetworkPositionsConfig.h diff --git a/GpsTracker/Pins.h b/Pins.h similarity index 100% rename from GpsTracker/Pins.h rename to Pins.h diff --git a/GpsTracker/Positions.cpp b/Positions.cpp similarity index 100% rename from GpsTracker/Positions.cpp rename to Positions.cpp diff --git a/GpsTracker/Positions.h b/Positions.h similarity index 100% rename from GpsTracker/Positions.h rename to Positions.h diff --git a/GpsTracker/PositionsBackup.cpp b/PositionsBackup.cpp similarity index 100% rename from GpsTracker/PositionsBackup.cpp rename to PositionsBackup.cpp diff --git a/GpsTracker/PositionsBackup.h b/PositionsBackup.h similarity index 100% rename from GpsTracker/PositionsBackup.h rename to PositionsBackup.h diff --git a/GpsTracker/Rtc.cpp b/Rtc.cpp similarity index 95% rename from GpsTracker/Rtc.cpp rename to Rtc.cpp index 26d8b45..861e3a2 100644 --- a/GpsTracker/Rtc.cpp +++ b/Rtc.cpp @@ -11,7 +11,7 @@ using namespace utils; namespace rtc { - + void setup() { VERBOSE("setup"); hardware::i2c::powerOn(); @@ -19,8 +19,6 @@ namespace rtc { RTC.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF RTC.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON hardware::i2c::powerOff(); - - //TODO : check wether the osc has been halted (meaning the battery could be dead) } float getTemperature() { diff --git a/GpsTracker/Rtc.h b/Rtc.h similarity index 100% rename from GpsTracker/Rtc.h rename to Rtc.h diff --git a/GpsTracker/SdCard.cpp b/SdCard.cpp similarity index 100% rename from GpsTracker/SdCard.cpp rename to SdCard.cpp diff --git a/GpsTracker/SdCard.h b/SdCard.h similarity index 100% rename from GpsTracker/SdCard.h rename to SdCard.h diff --git a/GpsTracker/SdPositionsBackup.cpp b/SdPositionsBackup.cpp similarity index 100% rename from GpsTracker/SdPositionsBackup.cpp rename to SdPositionsBackup.cpp diff --git a/GpsTracker/SdPositionsBackup.h b/SdPositionsBackup.h similarity index 100% rename from GpsTracker/SdPositionsBackup.h rename to SdPositionsBackup.h diff --git a/GpsTracker/SdPositionsConfig.cpp b/SdPositionsConfig.cpp similarity index 100% rename from GpsTracker/SdPositionsConfig.cpp rename to SdPositionsConfig.cpp diff --git a/GpsTracker/SdPositionsConfig.h b/SdPositionsConfig.h similarity index 100% rename from GpsTracker/SdPositionsConfig.h rename to SdPositionsConfig.h diff --git a/GpsTracker/Time2.cpp b/Time2.cpp similarity index 100% rename from GpsTracker/Time2.cpp rename to Time2.cpp diff --git a/GpsTracker/Time2.h b/Time2.h similarity index 100% rename from GpsTracker/Time2.h rename to Time2.h From a371b8556d916cd3a1d30933e855922f5d5d1a03 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 23:03:23 +1200 Subject: [PATCH 56/74] Added workspace to scm --- gpstracker.code-workspace | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 gpstracker.code-workspace diff --git a/gpstracker.code-workspace b/gpstracker.code-workspace new file mode 100644 index 0000000..1f9a02e --- /dev/null +++ b/gpstracker.code-workspace @@ -0,0 +1,26 @@ +{ + "folders": [ + { + "path": "." + }, + { + "path": "../libraries/SIM808" + }, + { + "path": "../libraries/uDS3231" + }, + { + "path": "../libraries/E24" + }, + { + "path": "../libraries/Low-Power" + }, + { + "path": "../libraries/ArduinoLog" + } + ], + "settings": { + "C_Cpp.intelliSenseEngineFallback": "Disabled", + "C_Cpp.intelliSenseEngine": "Tag Parser" + } +} \ No newline at end of file From 4e7fa881186621b457a9584ab0fe473e8851abe4 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 23:18:50 +1200 Subject: [PATCH 57/74] Added intellisense configuration file --- .vscode/c_cpp_properties.json | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..62faa60 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,22 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**", + "${workspaceFolder}/../libraries/SIM808", + "${workspaceFolder}/../libraries/uDS3231", + "${workspaceFolder}/../libraries/E24", + "${workspaceFolder}/../libraries/Low-Power", + "${workspaceFolder}/../libraries/ArduinoLog", + "${config:arduino.path}/tools/**", + "${config:arduino.path}/hardware/arduino/avr/**", + "${config:arduino.path}/hardware/tools/avr/avr/include/**" + ], + "intelliSenseMode": "clang-x64", + "cStandard": "c11", + "cppStandard": "c++11" + } + ], + "version": 4 +} \ No newline at end of file From 809531acce24a12ed3b8463a0dd6e976c9c50b9f Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 12:23:47 +1200 Subject: [PATCH 58/74] Fix network not being powered off if not available --- Core.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Core.cpp b/Core.cpp index 5c72ba7..c53faf3 100644 --- a/Core.cpp +++ b/Core.cpp @@ -65,7 +65,10 @@ namespace core { network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus.stat)) return NO_ALERTS_NOTIFIED; + if (!network::isAvailable(networkStatus.stat)) { + network::powerOff(); + return NO_ALERTS_NOTIFIED; + } strncpy_P(buffer, PSTR("Alerts !"), SMS_BUFFER_SIZE); if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { @@ -102,7 +105,7 @@ namespace core { float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ? if (distance > GPS_DEFAULT_MISSED_POSITION_GAP_KM) stoppedInARow = 0; else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops - + if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) { result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS; } @@ -119,14 +122,14 @@ namespace core { uint16_t mapSleepTime(uint8_t velocity) { uint16_t result; uint16_t currentTime = 0xFFFF; - + if (rtc::isAccurate()) { tmElements_t time; rtc::getTime(time); currentTime = SLEEP_TIMING_TIME(time.Hour, time.Minute); } - + for (uint8_t i = flash::getArraySize(config::defaultSleepTimings); i--;) { sleepTimings_t timing; flash::read(&config::defaultSleepTimings[i], timing); From 2af81daeaa4dde8585fcf1cbfe62f037a8455878 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 12:26:39 +1200 Subject: [PATCH 59/74] Reduced hex size by reversing if condition (-33bytes) --- Core.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/Core.cpp b/Core.cpp index c53faf3..09a8ba7 100644 --- a/Core.cpp +++ b/Core.cpp @@ -56,6 +56,7 @@ namespace core { SIM808RegistrationStatus networkStatus; char buffer[SMS_BUFFER_SIZE]; const __FlashStringHelper * backupFailureString = F(" Backup battery failure ?"); + bool notified = false; uint8_t triggered = alerts::getTriggered(metadata); if (!triggered) return NO_ALERTS_NOTIFIED; @@ -65,27 +66,24 @@ namespace core { network::powerOn(); networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); - if (!network::isAvailable(networkStatus.stat)) { - network::powerOff(); - return NO_ALERTS_NOTIFIED; - } + if (network::isAvailable(networkStatus.stat)) { + 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); + } - 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); - } + if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { + details::appendToSmsBuffer(buffer, PSTR("\n- Temperature is %dC.%S"), static_cast(metadata.temperature * 100), backupFailureString); + } - if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("\n- Temperature is %dC.%S"), static_cast(metadata.temperature * 100), backupFailureString); - } + if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { + details::appendToSmsBuffer(buffer, PSTR("\n- RTC was stopped.%S"), backupFailureString); + } - if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) { - details::appendToSmsBuffer(buffer, PSTR("\n- RTC was stopped.%S"), backupFailureString); + notified = network::sendSms(buffer); + if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); } - bool notified = network::sendSms(buffer); - if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); - network::powerOff(); return notified ? triggered : NO_ALERTS_NOTIFIED; //If not notified, the alerts state should not be persisted (so we can retry to notify them) } From 818faef87b39d983133a82a87e47f271dbe04a00 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 13:48:24 +1200 Subject: [PATCH 60/74] Reduced code size when only one backup method is enabled (22 bytes) --- Core.cpp | 2 +- Positions.cpp | 54 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Core.cpp b/Core.cpp index 09a8ba7..faf9115 100644 --- a/Core.cpp +++ b/Core.cpp @@ -140,7 +140,7 @@ namespace core { } - VERBOSE_FORMAT("computeSleepTime", "%d,%d", velocity, result); + VERBOSE_FORMAT("mapSleepTime", "%d,%d", velocity, result); return result; } } \ No newline at end of file diff --git a/Positions.cpp b/Positions.cpp index f4ab051..5feea1e 100644 --- a/Positions.cpp +++ b/Positions.cpp @@ -4,7 +4,7 @@ #include "Gps.h" #if BACKUP_ENABLE_SDCARD || BACKUP_ENABLE_NETWORK -#define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD + BACKUP_ENABLE_NETWORK +#define BACKUPS_ENABLED (BACKUP_ENABLE_SDCARD + BACKUP_ENABLE_NETWORK) #endif #if BACKUP_ENABLE_SDCARD @@ -21,8 +21,10 @@ #define ENTRIES_ADDR CONFIG_RESERVED_SIZE namespace positions { -#ifdef BACKUPS_ENABLED +#if BACKUPS_ENABLED > 1 backup::PositionsBackup **_backups; +#elif BACKUPS_ENABLED == 1 + backup::PositionsBackup * _backup; #endif namespace details { @@ -36,28 +38,44 @@ namespace positions { void setup() { details::maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE; -#ifdef BACKUPS_ENABLED + +#if BACKUPS_ENABLED > 0 + backup::PositionsBackup * backup = NULL; +#if BACKUPS_ENABLED > 1 uint8_t backupIdx = 0; _backups = new backup::PositionsBackup*[BACKUPS_ENABLED]; +#endif //BACKUPS_ENABLED > 1 #if BACKUP_ENABLE_SDCARD - _backups[backupIdx] = new backup::sd::SdPositionsBackup(); - _backups[backupIdx]->setup(); + backup = new backup::sd::SdPositionsBackup(); + backup->setup(); +#if BACKUPS_ENABLED > 1 + _backups[backupIdx] = backup; backupIdx++; -#endif +#endif //BACKUPS_ENABLED > 1 +#endif //BACKUP_ENABLE_SDCARD + #if BACKUP_ENABLE_NETWORK - _backups[backupIdx] = new backup::net::NetworkPositionsBackup(); - _backups[backupIdx]->setup(); + backup = new backup::net::NetworkPositionsBackup(); + backup->setup(); +#if BACKUPS_ENABLED > 1 + _backups[backupIdx] = backup; backupIdx++; -#endif -#endif +#endif //BACKUPS_ENABLED > 1 +#endif //BACKUP_ENABLE_NETWORK + +#if BACKUPS_ENABLED == 1 + _backup = backup; +#endif //BACKUPS_ENABLED == 1 +#endif //BACKUPS_ENABLED > 0 + } bool acquire(PositionEntryMetadata &metadata) { NOTICE("acquire"); timestamp_t before; - + gps::powerOn(); before = rtc::getTime(); SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS); @@ -95,7 +113,7 @@ namespace positions { hardware::i2c::powerOn(); hardware::i2c::eeprom.writeBlock(entryAddress, entry); - NOTICE_FORMAT("appendLast", "Saved @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + NOTICE_FORMAT("appendLast", "Saved @ %X : [%d%% @ %dmV] [%f�C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); config->lastEntry++; if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0; @@ -112,13 +130,13 @@ namespace positions { uint16_t entryAddress = details::getEntryAddress(index); if (entryAddress == -1) return false; - VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress); + VERBOSE_FORMAT("get", "Reading entry n�%d @ %X", index, entryAddress); hardware::i2c::powerOn(); hardware::i2c::eeprom.readBlock(entryAddress, entry); hardware::i2c::powerOff(); - NOTICE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + NOTICE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f�C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); return true; } @@ -141,18 +159,22 @@ namespace positions { } void prepareBackup() { -#ifdef BACKUPS_ENABLED +#if BACKUPS_ENABLED > 1 for (int i = 0; i < BACKUPS_ENABLED; i++) { _backups[i]->prepare(); } +#elif BACKUPS_ENABLED == 1 + _backup->prepare(); #endif } void doBackup(bool force) { -#ifdef BACKUPS_ENABLED +#if BACKUPS_ENABLED > 1 for (int i = 0; i < BACKUPS_ENABLED; i++) { _backups[i]->backup(force); } +#elif BACKUPS_ENABLED == 1 + _backup->backup(force); #endif } } \ No newline at end of file From 5498b9ae675e8fa651407979bd29ef2780a3bbf7 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 14:04:02 +1200 Subject: [PATCH 61/74] Remove intermediate variable (-48 bytes) --- Core.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Core.cpp b/Core.cpp index faf9115..591c175 100644 --- a/Core.cpp +++ b/Core.cpp @@ -95,9 +95,10 @@ namespace core { } bool updateSleepTime() { - uint8_t velocity = gps::getVelocity(); - uint16_t result = mapSleepTime(velocity); bool goingLongSleep = false; + uint8_t velocity = gps::getVelocity(); + + sleepTime = mapSleepTime(velocity); if (velocity < SLEEP_TIMING_MIN_MOVING_VELOCITY) { float distance = gps::getDistanceFromPrevious(); //did we missed positions because we were sleeping ? @@ -105,15 +106,13 @@ namespace core { else stoppedInARow = min(stoppedInARow + 1, SLEEP_DEFAULT_STOPPED_THRESHOLD + 1); //avoid overflow on REALLY long stops if (stoppedInARow < SLEEP_DEFAULT_STOPPED_THRESHOLD) { - result = SLEEP_DEFAULT_PAUSING_TIME_SECONDS; + sleepTime = SLEEP_DEFAULT_PAUSING_TIME_SECONDS; } else if (stoppedInARow == SLEEP_DEFAULT_STOPPED_THRESHOLD) goingLongSleep = true; } else stoppedInARow = 0; - sleepTime = result; NOTICE_FORMAT("updateSleepTime", "%dkmh => %d seconds", velocity, sleepTime); - return goingLongSleep; } From b943cc347874bbbed509b095473a4815b81c2f0a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 16:14:12 +1200 Subject: [PATCH 62/74] Reworked network isAvailable condition to gain 26 bytes on hex size --- Network.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Network.cpp b/Network.cpp index 5ec6ff6..c812668 100644 --- a/Network.cpp +++ b/Network.cpp @@ -37,7 +37,7 @@ namespace network { report = hardware::sim808::device.getSignalQuality(); NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); - + if (report.ssri < NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++; else noReliableNetwork = 0; if (noReliableNetwork > NETWORK_DEFAULT_NO_NETWORK_TRIES) { @@ -50,14 +50,15 @@ namespace network { } while (timeout > 1); - report = hardware::sim808::device.getSignalQuality(); + report = hardware::sim808::device.getSignalQuality(); //FIXME : report does not match currentStatus NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); - return currentStatus; + return currentStatus; //FIXME : on last loop waited for nothing } bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state) { - return state == SIM808_NETWORK_REGISTRATION_STATE::REGISTERED || - state == SIM808_NETWORK_REGISTRATION_STATE::ROAMING; + return static_cast(state) & + (static_cast(SIM808_NETWORK_REGISTRATION_STATE::REGISTERED) | static_cast(SIM808_NETWORK_REGISTRATION_STATE::ROAMING)) + != 0; } bool enableGprs() { From 71c8cf6f4fc1f2562bb1340efb284dfe3f1f3006 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 16:31:29 +1200 Subject: [PATCH 63/74] Removed mainunit:sleep (-130 bytes) --- Debug.cpp | 15 ++++++--------- Debug.h | 1 - GpsTracker.ino | 3 --- MainUnit.cpp | 8 -------- MainUnit.h | 1 - 5 files changed, 6 insertions(+), 22 deletions(-) diff --git a/Debug.cpp b/Debug.cpp index 078585a..b899171 100644 --- a/Debug.cpp +++ b/Debug.cpp @@ -32,7 +32,6 @@ MENU_ENTRY(EEPROM_ADD_ENTRY, "[a] Add last entry to EEPROM"); MENU_ENTRY(EEPROM_BACKUP_ENTRIES, "[B] Backup EEPROM entries"); MENU_ENTRY(NOTIFY_FAILURES, "[F] Notify failures"); MENU_ENTRY(CLEAR_ALERTS, "[A] Clear alerts"); -MENU_ENTRY(SLEEP, "[S] Sleep for 8s"); MENU_ENTRY(SLEEP_DEEP, "[s] Deep sleep for 10s"); MENU_ENTRY(QUESTION, "?"); @@ -56,7 +55,6 @@ const PROGMEM uint8_t commandIdMapping[] = { 'B', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_BACKUP_ENTRIES), 'F', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::NOTIFY_FAILURES), 'A', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::CLEAR_ALERTS), - 'S', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP), 's', static_cast(debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP), }; @@ -97,7 +95,6 @@ const char * const MENU_ENTRIES[] PROGMEM = { MENU_CLEAR_ALERTS, MENU_SEPARATOR, - MENU_SLEEP, MENU_SLEEP_DEEP, MENU_QUESTION @@ -115,7 +112,7 @@ namespace debug { namespace details { inline void displayPosition(PositionEntry entry) { - Log.notice(F("%d%%, %dmV, %f°C, %ds, %d, %s\n"), entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); + Log.notice(F("%d%%, %dmV, %f�C, %ds, %d, %s\n"), entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position); } } @@ -131,7 +128,7 @@ namespace debug { size_t mappingArraySize = flash::getArraySize(commandIdMapping); char commandId; - for (uint8_t i = 0; i < mappingArraySize; i += 2) { + for (uint8_t i = 0; i < mappingArraySize; i += 2) { commandId = pgm_read_byte_near(commandIdMapping + i); if (commandId == id) return static_cast(pgm_read_byte_near(commandIdMapping + i + 1)); } @@ -144,7 +141,7 @@ namespace debug { size_t menuSize = flash::getArraySize(MENU_ENTRIES); uint8_t intermediate_timeout = 50; - do { + do { for (uint8_t i = 0; i < menuSize; i++) { Serial.println(reinterpret_cast(pgm_read_word_near(&MENU_ENTRIES[i]))); } @@ -162,7 +159,7 @@ namespace debug { command = parseCommand(Serial.read()); while (Serial.available()) Serial.read(); //flushing input } while (command == GPSTRACKER_DEBUG_COMMAND::NONE); - + return command; } @@ -272,7 +269,7 @@ namespace debug { void notifyFailures() { PositionEntryMetadata metadata = { 1, //all battery alerts should goes on with this - 3800, //doesn't matter + 3800, //doesn't matter ALERT_SUSPICIOUS_RTC_TEMPERATURE, 0, SIM808_GPS_STATUS::OFF @@ -286,7 +283,7 @@ namespace debug { void clearAlerts() { PositionEntryMetadata metadata = { 100, //all battery alerts should goes off with this - 3800, //doesn't matter + 3800, //doesn't matter 10, 0, SIM808_GPS_STATUS::OFF diff --git a/Debug.h b/Debug.h index d9a1cd0..b02fea7 100644 --- a/Debug.h +++ b/Debug.h @@ -33,7 +33,6 @@ namespace debug { EEPROM_BACKUP_ENTRIES, NOTIFY_FAILURES, CLEAR_ALERTS, - SLEEP, SLEEP_DEEP }; diff --git a/GpsTracker.ino b/GpsTracker.ino index 6e382a1..984c9bb 100644 --- a/GpsTracker.ino +++ b/GpsTracker.ino @@ -86,9 +86,6 @@ void loop() { case debug::GPSTRACKER_DEBUG_COMMAND::CLEAR_ALERTS: debug::clearAlerts(); break; - case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: - mainunit::sleep(period_t::SLEEP_8S); - break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP_DEEP: mainunit::deepSleep(10); break; diff --git a/MainUnit.cpp b/MainUnit.cpp index e72e2aa..1fdb6bc 100644 --- a/MainUnit.cpp +++ b/MainUnit.cpp @@ -35,14 +35,6 @@ namespace mainunit { attachInterrupt(digitalPinToInterrupt(RTC_WAKE), interrupt, FALLING); } - void sleep(period_t period) { - NOTICE_FORMAT("sleep", "Period : %d", period); - details::prepareSleep(); - LowPower.powerDown(period, ADC_OFF, BOD_OFF); - details::wokeUp(); - - } - void deepSleep(uint16_t seconds) { NOTICE_FORMAT("deepSleep", "%d seconds", seconds); interruptIn(seconds); diff --git a/MainUnit.h b/MainUnit.h index 8a7b633..791894a 100644 --- a/MainUnit.h +++ b/MainUnit.h @@ -4,6 +4,5 @@ #include namespace mainunit { - void sleep(period_t period); void deepSleep(uint16_t seconds); } \ No newline at end of file From f96b40c6c496ef4895bbfd4dde4676ffa02ed6df Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 16:50:47 +1200 Subject: [PATCH 64/74] Made sure devices are powered off on sleep and obtained 112 bytes(???) --- MainUnit.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MainUnit.cpp b/MainUnit.cpp index 1fdb6bc..ff385c7 100644 --- a/MainUnit.cpp +++ b/MainUnit.cpp @@ -10,8 +10,12 @@ namespace mainunit { namespace details { void prepareSleep() { + //forcing the power off of ALL devices for safety + hardware::sim808::powerOff(); + hardware::i2c::powerOff(true); + hardware::sim808::simSerial.end(); //avoid woke up by SoftwareSerial interrupt - delay(5); //ensure message have been printed out + delay(5); //ensure log messages have been printed out } void wokeUp() { From 0d68f8766d7183873dd839e03175e42671146f78 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 17:44:31 +1200 Subject: [PATCH 65/74] Display alerts on Serial when available to avoid SMS costs while debugging --- Config.h | 20 ++++++++++++++------ Core.cpp | 10 ++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Config.h b/Config.h index 5171454..d509e69 100644 --- a/Config.h +++ b/Config.h @@ -15,11 +15,6 @@ #define CONFIG_RESERVED_SIZE 128 #define CONFIG_SEED 13 #define VERSION "1.10" -#define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1 45 -#define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2 38 -#define CONFIG_DEFAULT_BATTERY_ALERT_CLEAR 60 -#define CONFIG_DEFAULT_ACTIVE_ALERTS 0 -#define CONFIG_DEFAULT_CONTACT_PHONE "+642568452" #define SLEEP_TIMING_TIME(hours, minutes) hours * 60 + minutes @@ -31,8 +26,15 @@ Hard coded value for default sleep time between position acquisitions. Exprimed in seconds */ + +#define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1 45 +#define CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2 38 +#define CONFIG_DEFAULT_BATTERY_ALERT_CLEAR 60 +#define CONFIG_DEFAULT_ACTIVE_ALERTS 0 +#define CONFIG_DEFAULT_CONTACT_PHONE "+642568452" + #define SLEEP_DEFAULT_TIME_SECONDS 1800 -#define SLEEP_DEFAULT_STOPPED_THRESHOLD 5 +#define SLEEP_DEFAULT_STOPPED_THRESHOLD 5 #define SLEEP_DEFAULT_PAUSING_TIME_SECONDS 270 #define SLEEP_TIMING_MIN SLEEP_TIMING_TIME(0, 0) @@ -48,6 +50,12 @@ #define NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD 8 #define NETWORK_DEFAULT_NO_NETWORK_TRIES 5 +#define ALERTS_ON_SERIAL_IF_AVAILABLE 1 +/** + \def ALERTS_ON_SERIAL_IF_AVAILABLE + Display alerts on serial when connected rather than sending an SMS. + Useful for debugging purpose and avoid costs related to SMS sending. +*/ #define ALERT_SUSPICIOUS_RTC_TEMPERATURE 0 #pragma endregion diff --git a/Core.cpp b/Core.cpp index 591c175..8424f64 100644 --- a/Core.cpp +++ b/Core.cpp @@ -80,7 +80,17 @@ namespace core { details::appendToSmsBuffer(buffer, PSTR("\n- RTC was stopped.%S"), backupFailureString); } +#if ALERTS_ON_SERIAL_IF_AVAILABLE + if(Serial) { + NOTICE_FORMAT("notifyFailure", "%s", buffer); + notified = true; + } + else { +#endif notified = network::sendSms(buffer); +#if ALERTS_ON_SERIAL_IF_AVAILABLE + } +#endif if (!notified) NOTICE_MSG("notifyFailure", "SMS not sent !"); } From ac7db365610ce921ede0a78d10f7fb017df48950 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 17:58:01 +1200 Subject: [PATCH 66/74] Reverted forcing devices power off, stupid as they need to stay on during some of those sleeps --- MainUnit.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/MainUnit.cpp b/MainUnit.cpp index ff385c7..1d02dff 100644 --- a/MainUnit.cpp +++ b/MainUnit.cpp @@ -10,10 +10,6 @@ namespace mainunit { namespace details { void prepareSleep() { - //forcing the power off of ALL devices for safety - hardware::sim808::powerOff(); - hardware::i2c::powerOff(true); - hardware::sim808::simSerial.end(); //avoid woke up by SoftwareSerial interrupt delay(5); //ensure log messages have been printed out } From 47ce7e8235aefb0b346caf20eb6a2c7a17767f59 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 18:18:08 +1200 Subject: [PATCH 67/74] Added temperature to getAndDisplayRtcTime --- Debug.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Debug.cpp b/Debug.cpp index b899171..e9263ca 100644 --- a/Debug.cpp +++ b/Debug.cpp @@ -189,7 +189,7 @@ namespace debug { tmElements_t time; rtc::getTime(time); - NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d %t", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second, rtc::isAccurate()); + NOTICE_FORMAT("getAndDisplayRtcTime", "%d/%d/%d %d:%d:%d %t %d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second, rtc::isAccurate(), (uint16_t)(rtc::getTemperature() * 1000)); } void setRtcTime() { From 5cd3f2e09b860c42025f858033eaf5d9dac60933 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 19:07:51 +1200 Subject: [PATCH 68/74] Clear the active alerts first --- Core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core.cpp b/Core.cpp index 8424f64..afbe9fa 100644 --- a/Core.cpp +++ b/Core.cpp @@ -44,8 +44,8 @@ namespace core { gps::preserveCurrentCoordinates(); } - alerts::add(notifyFailures(metadata)); alerts::clear(metadata); + alerts::add(notifyFailures(metadata)); positions::doBackup(forceBackup); if (acquired) updateRtcTime(); From aa1761ed04d99434cb75bcc3fbbe82fdb786898a Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 19:11:07 +1200 Subject: [PATCH 69/74] Fixed clear and add alerts --- Alerts.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Alerts.cpp b/Alerts.cpp index 33bab8c..d9c8e9a 100644 --- a/Alerts.cpp +++ b/Alerts.cpp @@ -22,23 +22,29 @@ namespace alerts { } void add(uint8_t mask) { - if (!mask) return; //save a write to eeprom if there is no change - config_t* config = &config::main::value; - config->activeAlerts |= mask; + uint8_t active = config->activeAlerts; + + active |= mask; + if (config->activeAlerts == active) return; //save a write to eeprom if there is no change + + config->activeAlerts = active; config::main::save(); } void clear(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; uint8_t clearMask = 0; + uint8_t active = config->activeAlerts; if (metadata.batteryLevel >= config->alertBatteryLevelClear) clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); if (metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); if (rtc::isAccurate()) bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); - if (!config->activeAlerts || !clearMask) return; //save a write to eeprom if there is no change - config->activeAlerts &= ~clearMask; + active &= ~clearMask; + if (config->activeAlerts == active) return; //save a write to eeprom if there is no change + + config->activeAlerts = active; config::main::save(); } } From 0ec46f769942a967d74a9a9b59b6829bf74b9a55 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 19:37:28 +1200 Subject: [PATCH 70/74] Straighten poweredCount logic for i2c --- Hardware.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Hardware.cpp b/Hardware.cpp index f612ce4..f58fd13 100644 --- a/Hardware.cpp +++ b/Hardware.cpp @@ -120,9 +120,9 @@ namespace hardware { pinMode(I2C_PWR, OUTPUT); Wire.begin(); + poweredCount = 1; } - - poweredCount++; + else poweredCount++; } void powerOff(bool forced = false) { @@ -137,9 +137,10 @@ namespace hardware { //disable i2c internal pull ups digitalWrite(A4, LOW); digitalWrite(A5, LOW); - } - poweredCount--; + poweredCount = 0; + } + else if(poweredCount > 1) poweredCount--; //avoid decrement if == 0 } } } From a8ad48317abeaea70bd862ec0faca603ae794aff Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 20:15:25 +1200 Subject: [PATCH 71/74] Fixed SIM808 power management. Straighten the power count logic everywhere --- Hardware.cpp | 95 ++++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/Hardware.cpp b/Hardware.cpp index f58fd13..cfd399c 100644 --- a/Hardware.cpp +++ b/Hardware.cpp @@ -37,6 +37,9 @@ namespace hardware { } void powerOffIfUnused() { + //does not rely on count for safety + //if there is a bug somewhere, the device will consume more battery, + //but will not fail due to an over aggressive battery saving strategy bool gpsPowered = false; if ((!device.getGpsPowerState(&gpsPowered) || !gpsPowered) && @@ -53,13 +56,14 @@ namespace hardware { } void gpsPowerOn() { - if (!gpsPoweredCount) { - VERBOSE("gpsPowerOn"); - powerOn(); - device.enableGps(); + if(gpsPoweredCount) { + gpsPoweredCount++; + return; } - gpsPoweredCount++; + VEBOSE("gpsPowerOn"); + powerOn(); + device.enableGps(); } void gpsPowerOff() { @@ -68,23 +72,25 @@ namespace hardware { return; } - if (gpsPoweredCount == 1) { - VERBOSE("gpsPowerOff"); - device.disableGps(); - powerOffIfUnused(); + if(gpsPoweredCount > 1) { + gpsPoweredCount--; + return; } - if (gpsPoweredCount) gpsPoweredCount--; //avoid 255 if 0-- + VERBOSE("gpsPowerOff"); + device.disableGps(); + powerOffIfUnused(); } void networkPowerOn() { - if (!networkPoweredCount) { - VERBOSE("networkPowerOn"); - powerOn(); - device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); + if(networkPoweredCount) { + networkPoweredCount++; + return; } - networkPoweredCount++; + VERBOSE("networkPowerOn"); + powerOn(); + device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); } void networkPowerOff() { @@ -93,16 +99,15 @@ namespace hardware { return; } - - if (networkPoweredCount == 1) { - VERBOSE("networkPowerOff"); - device.disableGprs(); - device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); - - powerOffIfUnused(); + if(networkPoweredCount > 1) { + networkPoweredCount--; + return; } - if (networkPoweredCount) networkPoweredCount--; //avoid 255 if 0-- + VERBOSE("networkPowerOff"); + device.disableGprs(); + device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); + powerOffIfUnused(); } } @@ -114,33 +119,37 @@ namespace hardware { uint8_t poweredCount = 0; void powerOn() { - if (!poweredCount) { - VERBOSE("powerOn"); - digitalWrite(I2C_PWR, HIGH); - pinMode(I2C_PWR, OUTPUT); - - Wire.begin(); - poweredCount = 1; + if(poweredCount) { + poweredCount++; + return; } - else poweredCount++; + + VERBOSE("powerOn"); + digitalWrite(I2C_PWR, HIGH); + pinMode(I2C_PWR, OUTPUT); + + Wire.begin(); + poweredCount = 1; } void powerOff(bool forced = false) { - if (poweredCount == 1 || forced) { - VERBOSE("powerOff"); - pinMode(I2C_PWR, INPUT); - digitalWrite(I2C_PWR, LOW); + if(poweredCount > 1 && !forced) { + poweredCount--; + return; + } + + VERBOSE("powerOff"); + pinMode(I2C_PWR, INPUT); + digitalWrite(I2C_PWR, LOW); - //turn off i2c - TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA)); + //turn off i2c + TWCR &= ~(bit(TWEN) | bit(TWIE) | bit(TWEA)); - //disable i2c internal pull ups - digitalWrite(A4, LOW); - digitalWrite(A5, LOW); + //disable i2c internal pull ups + digitalWrite(A4, LOW); + digitalWrite(A5, LOW); - poweredCount = 0; - } - else if(poweredCount > 1) poweredCount--; //avoid decrement if == 0 + poweredCount = 0; } } } From fcf9a68c0f665a4eb955656888d0f1e42451c4f9 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 20:18:19 +1200 Subject: [PATCH 72/74] Disable SIM808 phone functionality when powering on the gps only --- Hardware.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Hardware.cpp b/Hardware.cpp index cfd399c..e257c22 100644 --- a/Hardware.cpp +++ b/Hardware.cpp @@ -63,6 +63,8 @@ namespace hardware { VEBOSE("gpsPowerOn"); powerOn(); + //SIM808 turns phone on by default but we don't need it + if(!networkPoweredCount) device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); device.enableGps(); } From df8deba47047ea4f36686151f9c1741452ca4f2d Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 20:28:51 +1200 Subject: [PATCH 73/74] Fixed typo and arranged code --- Hardware.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Hardware.cpp b/Hardware.cpp index e257c22..35ffc48 100644 --- a/Hardware.cpp +++ b/Hardware.cpp @@ -61,10 +61,12 @@ namespace hardware { return; } - VEBOSE("gpsPowerOn"); + VERBOSE("gpsPowerOn"); powerOn(); - //SIM808 turns phone on by default but we don't need it - if(!networkPoweredCount) device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); + if(!networkPoweredCount) { + //SIM808 turns phone on by default but we don't need it for gps only + device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::MINIMUM); + } device.enableGps(); } From c5014addd2bf672cab1519b7b1e64b087b666e93 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Mon, 6 Aug 2018 20:44:57 +1200 Subject: [PATCH 74/74] Do a last network status check before leaving the loop after timeout --- Network.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Network.cpp b/Network.cpp index c812668..79033f3 100644 --- a/Network.cpp +++ b/Network.cpp @@ -31,11 +31,12 @@ namespace network { if (relativeToPowerOnTime) timeout -= (rtc::getTime() - _poweredOnTime) * 1000; + currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); + report = hardware::sim808::device.getSignalQuality(); + do { - currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); if (isAvailable(currentStatus.stat)) break; - report = hardware::sim808::device.getSignalQuality(); NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); if (report.ssri < NETWORK_DEFAULT_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++; @@ -48,11 +49,12 @@ namespace network { mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; + currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); + report = hardware::sim808::device.getSignalQuality(); } while (timeout > 1); - report = hardware::sim808::device.getSignalQuality(); //FIXME : report does not match currentStatus NOTICE_FORMAT("waitForRegistered", "%d, [%d %ddBm]", currentStatus.stat, report.ssri, report.attenuation); - return currentStatus; //FIXME : on last loop waited for nothing + return currentStatus; } bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state) {