@@ -1,22 +1,36 @@ | |||||
#include "Config.h" | #include "Config.h" | ||||
#if BACKUP_ENABLE_NETWORK | |||||
#include "Debug.h" | #include "Debug.h" | ||||
#include "Network.h" | #include "Network.h" | ||||
#include "Hardware.h" | #include "Hardware.h" | ||||
#include "MainUnit.h" | #include "MainUnit.h" | ||||
#include "Rtc.h" | |||||
#define LOGGER_NAME "Network" | #define LOGGER_NAME "Network" | ||||
namespace network { | namespace network { | ||||
timestamp_t _poweredOnTime; | |||||
void powerOn() { | |||||
hardware::sim808::networkPowerOn(); | |||||
_poweredOnTime = rtc::getTime(); | |||||
} | |||||
void powerOff() { | |||||
hardware::sim808::networkPowerOff(); | |||||
_poweredOnTime = 0; | |||||
} | |||||
__attribute__((__optimize__("O2"))) | __attribute__((__optimize__("O2"))) | ||||
SIM808RegistrationStatus waitForRegistered(uint32_t timeout) { | |||||
SIM808RegistrationStatus waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) { | |||||
SIM808RegistrationStatus currentStatus; | SIM808RegistrationStatus currentStatus; | ||||
SIM808SignalQualityReport report; | SIM808SignalQualityReport report; | ||||
uint8_t noReliableNetwork = 0; | uint8_t noReliableNetwork = 0; | ||||
if (relativeToPowerOnTime) timeout -= (rtc::getTime() - _poweredOnTime) * 1000; | |||||
do { | do { | ||||
currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); | currentStatus = hardware::sim808::device.getNetworkRegistrationStatus(); | ||||
if (isAvailable(currentStatus.stat)) break; | if (isAvailable(currentStatus.stat)) break; | ||||
@@ -50,6 +64,4 @@ namespace network { | |||||
return hardware::sim808::device.enableGprs(config::main::value.network.apn); | return hardware::sim808::device.enableGprs(config::main::value.network.apn); | ||||
} | } | ||||
} | |||||
#endif | |||||
} |
@@ -4,10 +4,10 @@ | |||||
namespace network { | 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 isAvailable(SIM808_NETWORK_REGISTRATION_STATE state); | ||||
bool enableGprs(); | bool enableGprs(); | ||||
} | } |
@@ -53,15 +53,11 @@ namespace positions { | |||||
//__attribute__((__optimize__("O2"))) | //__attribute__((__optimize__("O2"))) | ||||
void NetworkPositionsBackup::appendPositions() { | void NetworkPositionsBackup::appendPositions() { | ||||
uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; | uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; | ||||
uint32_t networkTimeout = 0; | |||||
PositionEntry currentEntry; | PositionEntry currentEntry; | ||||
SIM808RegistrationStatus networkStatus; | SIM808RegistrationStatus networkStatus; | ||||
network::powerOn(); | 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()) { | if (!network::isAvailable(networkStatus.stat) || !network::enableGprs()) { | ||||
networkUnavailableInARow = min(networkUnavailableInARow + 1, POSITIONS_CONFIG_NET_DEFAULT_UNAVAILABLE_NETWORK_POSTPONE_THRESHOLD + 1); //avoid increment overflow | 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() { | void NetworkPositionsBackup::prepare() { | ||||
NOTICE("prepare"); | NOTICE("prepare"); | ||||
if (!isBackupNeeded(true)) { | |||||
_prepareTime = 0; | |||||
return; | |||||
} | |||||
if (!isBackupNeeded(true)) return; | |||||
network::powerOn(); | network::powerOn(); | ||||
_prepareTime = rtc::getTime(); | |||||
} | } | ||||
void NetworkPositionsBackup::backup(bool force) { | void NetworkPositionsBackup::backup(bool force) { | ||||
@@ -10,8 +10,6 @@ namespace positions { | |||||
class NetworkPositionsBackup : public PositionsBackup { | class NetworkPositionsBackup : public PositionsBackup { | ||||
private: | private: | ||||
timestamp_t _prepareTime; | |||||
bool isBackupNeeded(bool forPrepare); | bool isBackupNeeded(bool forPrepare); | ||||
bool appendPosition(PositionEntry &entry); | bool appendPosition(PositionEntry &entry); | ||||
void appendPositions(); | void appendPositions(); | ||||