diff --git a/GpsTracker/Config.cpp b/GpsTracker/Config.cpp index c6c569d..0c9e3d3 100644 --- a/GpsTracker/Config.cpp +++ b/GpsTracker/Config.cpp @@ -20,8 +20,10 @@ namespace config { } void write() { - VERBOSE_FORMAT("write", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); - + VERBOSE_FORMAT("write", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); +#if BACKUP_ENABLE_NETWORK + VERBOSE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); +#endif hardware::i2c::powerOn(); int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value); hardware::i2c::powerOff(); @@ -31,7 +33,10 @@ namespace config { config_t get() { if (value.seed == 0) details::read(); - VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); + VERBOSE_FORMAT("get", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); +#if BACKUP_ENABLE_NETWORK + VERBOSE_FORMAT("get", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); +#endif return value; } @@ -49,10 +54,10 @@ namespace config { 0xFFFF, #if BACKUP_ENABLE_NETWORK { - POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD, + POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD, 0xFFFF, - POSITIONS_CONFIG_DEFAULT_APN, - POSITIONS_CONFIG_DEFAULT_URL, + POSITIONS_CONFIG_NET_DEFAULT_APN, + POSITIONS_CONFIG_NET_DEFAULT_URL, }, #endif }; diff --git a/GpsTracker/Config.h b/GpsTracker/Config.h index 28c298a..fee1d3e 100644 --- a/GpsTracker/Config.h +++ b/GpsTracker/Config.h @@ -19,6 +19,9 @@ #define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000 #define GPS_DEFAULT_TOTAL_TIMEOUT_MS 180000 +#define NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS 2000 +#define NETWORK_DEFAULT_TOTAL_TIMEOUT_MS 60000 + struct sleepTimings_t { uint8_t speed; uint16_t seconds; diff --git a/GpsTracker/GpsTracker.ino b/GpsTracker/GpsTracker.ino index 782c2ab..27d7ac5 100644 --- a/GpsTracker/GpsTracker.ino +++ b/GpsTracker/GpsTracker.ino @@ -69,6 +69,7 @@ void loop() { break; case debug::GPSTRACKER_DEBUG_COMMAND::EEPROM_ADD_ENTRY: debug::addLastPositionToEeprom(); + positions::doBackup(); break; case debug::GPSTRACKER_DEBUG_COMMAND::SLEEP: mainunit::sleep(period_t::SLEEP_8S); diff --git a/GpsTracker/Hardware.cpp b/GpsTracker/Hardware.cpp index d262bc3..352cb79 100644 --- a/GpsTracker/Hardware.cpp +++ b/GpsTracker/Hardware.cpp @@ -66,7 +66,6 @@ namespace hardware { VERBOSE("networkPowerOn"); powerOn(); device.setPhoneFunctionality(SIM808_PHONE_FUNCTIONALITY::FULL); - device.enableGprs(config::main::get().network.apn); } void networkPowerOff() { diff --git a/GpsTracker/Network.cpp b/GpsTracker/Network.cpp index 0372b00..9794149 100644 --- a/GpsTracker/Network.cpp +++ b/GpsTracker/Network.cpp @@ -1,8 +1,34 @@ +#include "Config.h" #include "Network.h" #include "Hardware.h" +#include "MainUnit.h" + #define LOGGER_NAME "Network" namespace network { + SIM808RegistrationStatus waitForRegistered(uint16_t timeout) { + SIM808RegistrationStatus currentStatus; + + do { + currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); + if (isAvailable(currentStatus.stat)) break; + + mainunit::deepSleep(NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000); + timeout -= NETWORK_DEFAULT_INTERMEDIATE_TIMEOUT_MS; + } while (timeout > 1); + + return currentStatus; + } + + bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state) { + return state == SIM808_NETWORK_REGISTRATION_STATE::REGISTERED || + state == SIM808_NETWORK_REGISTRATION_STATE::ROAMING; + } + + bool enableGprs() { + return hardware::sim808::device.enableGprs(config::main::get().network.apn); + } + } \ No newline at end of file diff --git a/GpsTracker/Network.h b/GpsTracker/Network.h index df89526..79ba1ee 100644 --- a/GpsTracker/Network.h +++ b/GpsTracker/Network.h @@ -6,4 +6,8 @@ namespace network { inline void powerOn() { hardware::sim808::networkPowerOn(); } inline void powerOff() { hardware::sim808::networkPowerOff(); } + + SIM808RegistrationStatus waitForRegistered(uint16_t timeout); + 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 f0ba000..5be479e 100644 --- a/GpsTracker/NetworkPositionsBackup.cpp +++ b/GpsTracker/NetworkPositionsBackup.cpp @@ -5,14 +5,14 @@ #include "Positions.h" #include "Config.h" #include "Hardware.h" +#include "Network.h" #define LOGGER_NAME "Positions::backup::network" #define BUFFER_SIZE 160 -#define EXPECTED_HTTP_RESPONSE_CODE 201 namespace positions { namespace backup { - namespace network { + namespace net { namespace details { @@ -38,7 +38,7 @@ namespace positions { buffer, buffer, BUFFER_SIZE - ) == EXPECTED_HTTP_RESPONSE_CODE; + ) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE; } void appendPositions(config_t &config) { @@ -46,9 +46,22 @@ namespace positions { uint16_t currentEntryIndex = config.network.lastSavedEntry + 1; PositionEntry currentEntry; + SIM808RegistrationStatus networkStatus; hardware::i2c::powerOn(); - hardware::sim808::networkPowerOn(); + network::powerOn(); + networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); + + if (!network::isAvailable(networkStatus.stat)) { + VERBOSE_MSG("appendPositions", "network unavailable"); + return; + } + + if (!network::enableGprs()) { + VERBOSE_MSG("appendPositions", "gprs unavailable"); + return; + } + do { if (!positions::get(currentEntryIndex, currentEntry)) break; if (!appendPosition(config, currentEntry)) break; @@ -57,14 +70,14 @@ namespace positions { config::main::set(config); } while (positions::moveNext(currentEntryIndex)); - hardware::sim808::networkPowerOff(); + network::powerOff(); hardware::i2c::powerOff(); } } void NetworkPositionsBackup::setup() { - VERBOSE("backup"); + VERBOSE("setup"); } void NetworkPositionsBackup::backup() { diff --git a/GpsTracker/NetworkPositionsBackup.h b/GpsTracker/NetworkPositionsBackup.h index 0fe578f..da79400 100644 --- a/GpsTracker/NetworkPositionsBackup.h +++ b/GpsTracker/NetworkPositionsBackup.h @@ -4,7 +4,7 @@ namespace positions { namespace backup { - namespace network { + namespace net { class NetworkPositionsBackup : public PositionsBackup { private: diff --git a/GpsTracker/NetworkPositionsConfig.h b/GpsTracker/NetworkPositionsConfig.h index 24a661c..36a010f 100644 --- a/GpsTracker/NetworkPositionsConfig.h +++ b/GpsTracker/NetworkPositionsConfig.h @@ -1,9 +1,11 @@ #pragma once -#define POSITIONS_CONFIG_DEFAULT_SAVE_THRESHOLD 10 -#define POSITIONS_CONFIG_DEFAULT_APN "Vodafone" -#define POSITIONS_CONFIG_DEFAULT_URL "http://default.url" +#define POSITIONS_CONFIG_NET_DEFAULT_SAVE_THRESHOLD 2 +#define POSITIONS_CONFIG_NET_DEFAULT_APN "Vodafone" +#define POSITIONS_CONFIG_NET_DEFAULT_URL "http://requestbin.fullcontact.com/16q71o61" +#define POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE 201 + struct networkConfig_t { uint8_t saveThreshold; diff --git a/GpsTracker/Positions.cpp b/GpsTracker/Positions.cpp index 412273c..0a0674b 100644 --- a/GpsTracker/Positions.cpp +++ b/GpsTracker/Positions.cpp @@ -43,6 +43,13 @@ namespace positions { _backups[backupIdx] = new backup::sd::SdPositionsBackup(); _backups[backupIdx++]->setup(); #endif +#if BACKUP_ENABLE_NETWORK + _backups[backupIdx] = new backup::net::NetworkPositionsBackup(); + _backups[backupIdx]->setup(); + //_backups[backupIdx]->backup(); + backupIdx++; + +#endif #endif } @@ -132,9 +139,15 @@ namespace positions { void doBackup() { #ifdef BACKUPS_ENABLED - for (int i = 0; i < BACKUPS_ENABLED; i++) { + VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); + + _backups[0]->backup(); + + /*for (int i = 0; i < BACKUPS_ENABLED; i++) { + VERBOSE_FORMAT("doBackup", "calling backup %d", i); + delay(1000); _backups[i]->backup(); - } + }*/ #endif } } \ No newline at end of file diff --git a/GpsTracker/PositionsBackup.cpp b/GpsTracker/PositionsBackup.cpp new file mode 100644 index 0000000..ea17229 --- /dev/null +++ b/GpsTracker/PositionsBackup.cpp @@ -0,0 +1,9 @@ +#include "PositionsBackup.h" + +namespace positions { + namespace backup { + + PositionsBackup::~PositionsBackup() {} + + } +} \ No newline at end of file diff --git a/GpsTracker/PositionsBackup.h b/GpsTracker/PositionsBackup.h index f3ebaaf..0cd6f77 100644 --- a/GpsTracker/PositionsBackup.h +++ b/GpsTracker/PositionsBackup.h @@ -5,8 +5,9 @@ namespace positions { class PositionsBackup { public: - virtual void setup(); - virtual void backup(); + ~PositionsBackup(); + virtual void setup()=0; + virtual void backup()=0; }; }