@@ -17,6 +17,11 @@ namespace config { | |||
hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value); | |||
if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right | |||
hardware::i2c::powerOff(); | |||
VERBOSE_FORMAT("read", "%d, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry); | |||
#if BACKUP_ENABLE_NETWORK | |||
VERBOSE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); | |||
#endif | |||
} | |||
void write() { | |||
@@ -30,18 +35,11 @@ namespace config { | |||
} | |||
} | |||
config_t get() { | |||
if (value.seed == 0) details::read(); | |||
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; | |||
void setup() { | |||
details::read(); | |||
} | |||
void set(const config_t config) { | |||
value = config; | |||
void save() { | |||
details::write(); | |||
} | |||
@@ -63,7 +61,7 @@ namespace config { | |||
}; | |||
value = config; | |||
details::write(); | |||
save(); | |||
} | |||
} |
@@ -52,9 +52,11 @@ namespace config { | |||
{ 180, 180 }, | |||
}; | |||
namespace main { | |||
config_t get(); | |||
void set(const config_t config); | |||
namespace main { | |||
extern config_t value; | |||
void setup(); | |||
void save(); | |||
void reset(); | |||
} |
@@ -124,6 +124,10 @@ namespace debug { | |||
return freeRam2(); | |||
} | |||
void displayFreeRam() { | |||
Log.notice(F("RAM: %d\n"), freeRam()); | |||
} | |||
GPSTRACKER_DEBUG_COMMAND parseCommand(char id) { | |||
size_t mappingArraySize = flash::getArraySize(commandIdMapping); | |||
char commandId; | |||
@@ -183,7 +187,7 @@ namespace debug { | |||
} | |||
void getAndDisplayEepromConfig() { | |||
config::main::get(); | |||
config::main::setup(); //forcing read again | |||
} | |||
void getAndDisplayEepromContent() { | |||
@@ -203,7 +207,7 @@ namespace debug { | |||
} | |||
void getAndDisplayEepromPositions() { | |||
uint16_t currentEntryIndex = config::main::get().firstEntry; | |||
uint16_t currentEntryIndex = config::main::value.firstEntry; | |||
PositionEntry currentEntry; | |||
hardware::i2c::powerOn(); | |||
@@ -215,7 +219,7 @@ namespace debug { | |||
} | |||
void getAndDisplayEepromLastPosition() { | |||
uint16_t lastEntryIndex = config::main::get().lastEntry; | |||
uint16_t lastEntryIndex = config::main::value.lastEntry; | |||
PositionEntry lastEntry; | |||
positions::get(lastEntryIndex, lastEntry); | |||
@@ -61,6 +61,7 @@ namespace debug { | |||
void waitForSerial(); | |||
int freeRam(); | |||
void displayFreeRam(); | |||
GPSTRACKER_DEBUG_COMMAND menu(); | |||
@@ -77,6 +78,5 @@ namespace debug { | |||
void getAndDisplayEepromLastPosition(); | |||
void addLastPositionToEeprom(); | |||
inline void displayFreeRam() { Serial.println(freeRam()); } | |||
} |
@@ -11,6 +11,8 @@ void setup() { | |||
if(Serial) Log.begin(LOG_LEVEL_NOTICE, &Serial); | |||
#endif | |||
if (Serial) Serial.println(F("=============================")); | |||
config::main::setup(); | |||
rtc::setup(); | |||
hardware::sim808::setup(); | |||
positions::setup(); | |||
@@ -30,7 +30,7 @@ namespace network { | |||
} | |||
bool enableGprs() { | |||
return hardware::sim808::device.enableGprs(config::main::get().network.apn); | |||
return hardware::sim808::device.enableGprs(config::main::value.network.apn); | |||
} | |||
@@ -8,7 +8,7 @@ | |||
#include "Network.h" | |||
#define LOGGER_NAME "Positions::backup::network" | |||
#define BUFFER_SIZE 160 | |||
#define BUFFER_SIZE 170 | |||
namespace positions { | |||
namespace backup { | |||
@@ -16,25 +16,30 @@ namespace positions { | |||
namespace details { | |||
bool isBackupNeeded(config_t &config) { | |||
config = config::main::get(); | |||
return config.network.lastSavedEntry == 0xFFFF || | |||
positions::count(config.network.lastSavedEntry) > config.network.saveThreshold; | |||
bool isBackupNeeded() { | |||
config_t *config = &config::main::value; | |||
return config->network.lastSavedEntry == 0xFFFF || | |||
positions::count(config->network.lastSavedEntry) > config->network.saveThreshold; | |||
} | |||
bool appendPosition(config_t &config, PositionEntry &entry) { | |||
bool appendPosition(PositionEntry &entry) { | |||
VERBOSE("appendPosition"); | |||
debug::displayFreeRam(); | |||
return false; | |||
/*char buffer[BUFFER_SIZE]; | |||
snprintf_P(buffer, BUFFER_SIZE, PSTR("%d,%d,%.2f,%d,%s,%d"), | |||
delay(100); | |||
char buffer[BUFFER_SIZE]; | |||
sprintf(buffer, "%d", 2100); | |||
/*snprintf(buffer, BUFFER_SIZE, "%d,%d,%.2f,%d,", | |||
entry.metadata.batteryLevel, | |||
entry.metadata.batteryVoltage, | |||
entry.metadata.temperature, | |||
static_cast<uint8_t>(entry.metadata.status), | |||
entry.position); | |||
static_cast<uint8_t>(entry.metadata.status));*/ | |||
////strcat(buffer, entry.position); | |||
//Serial.println(buffer); | |||
debug::displayFreeRam(); | |||
return false; | |||
return hardware::sim808::device.httpPost( | |||
/*return hardware::sim808::device.httpPost( | |||
config.network.url, | |||
PSTR("text/csv"), | |||
buffer, | |||
@@ -43,32 +48,37 @@ namespace positions { | |||
) == POSITIONS_CONFIG_NET_DEFAULT_EXPECTED_RESPONSE;*/ | |||
} | |||
void appendPositions(config_t &config) { | |||
void appendPositions() { | |||
VERBOSE("appendPositions"); | |||
debug::displayFreeRam(); | |||
uint16_t currentEntryIndex = config.network.lastSavedEntry + 1; | |||
uint16_t currentEntryIndex = config::main::value.network.lastSavedEntry + 1; | |||
PositionEntry currentEntry; | |||
SIM808RegistrationStatus networkStatus; | |||
network::powerOn(); | |||
/*network::powerOn(); | |||
networkStatus = network::waitForRegistered(NETWORK_DEFAULT_TOTAL_TIMEOUT_MS); | |||
if (!network::isAvailable(networkStatus.stat)) VERBOSE_MSG("appendPositions", "network unavailable"); | |||
else if (!network::enableGprs()) VERBOSE_MSG("appendPositions", "gprs unavailable"); | |||
else { | |||
else {*/ | |||
hardware::i2c::powerOn(); | |||
do { | |||
if (!positions::get(currentEntryIndex, currentEntry)) break; | |||
if (!appendPosition(config, currentEntry)) break; | |||
debug::displayFreeRam(); | |||
config.network.lastSavedEntry = currentEntryIndex; | |||
config::main::set(config); | |||
//if (!appendPosition(currentEntry)) break; | |||
/*config::main::value.network.lastSavedEntry = currentEntryIndex; | |||
config::main::save(); | |||
*/ | |||
} while (positions::moveNext(currentEntryIndex)); | |||
debug::displayFreeRam(); | |||
hardware::i2c::powerOff(); | |||
} | |||
//} | |||
network::powerOff(); | |||
//network::powerOff(); | |||
debug::displayFreeRam(); | |||
} | |||
} | |||
@@ -79,10 +89,12 @@ namespace positions { | |||
void NetworkPositionsBackup::backup() { | |||
VERBOSE("backup"); | |||
debug::displayFreeRam(); | |||
if (!details::isBackupNeeded()) return; | |||
debug::displayFreeRam(); | |||
config_t config; | |||
if (!details::isBackupNeeded(config)) return; | |||
details::appendPositions(config); | |||
details::appendPositions(); | |||
} | |||
} | |||
} | |||
@@ -85,24 +85,27 @@ namespace positions { | |||
void appendLast(const PositionEntryMetadata &metadata) { | |||
VERBOSE("appendLast"); | |||
uint16_t entryIndex; | |||
uint16_t entryAddress; | |||
PositionEntry entry = { metadata }; | |||
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE); | |||
hardware::i2c::powerOn(); | |||
config_t config = config::main::get(); | |||
config_t* config = &config::main::value; | |||
entryIndex = config->lastEntry + 1; | |||
config.lastEntry++; | |||
if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0; | |||
if (config.lastEntry == config.firstEntry) config.firstEntry++; | |||
if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0; | |||
entryAddress = details::getEntryAddress(entryIndex); | |||
entryAddress = details::getEntryAddress(config.lastEntry); | |||
hardware::i2c::powerOn(); | |||
hardware::i2c::eeprom.writeBlock(entryAddress, entry); | |||
VERBOSE_FORMAT("appendLast", "Written to 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); | |||
config::main::set(config); | |||
config->lastEntry++; | |||
if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0; | |||
if (config->lastEntry == config->firstEntry) config->firstEntry++; | |||
if (config->firstEntry > details::maxEntryIndex) config->firstEntry = 0; | |||
config::main::save(); | |||
hardware::i2c::powerOff(); | |||
} | |||
@@ -121,7 +124,7 @@ namespace positions { | |||
} | |||
bool moveNext(uint16_t &index) { | |||
if (index == config::main::get().lastEntry) return false; | |||
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++; | |||
@@ -130,18 +133,18 @@ namespace positions { | |||
} | |||
uint16_t count(uint16_t fromIndex) { | |||
config_t config = config::main::get(); | |||
if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; } | |||
config_t *config = &config::main::value; | |||
if (config->lastEntry < config->firstEntry) { config->lastEntry += details::maxEntryIndex; } | |||
return config.lastEntry - fromIndex; | |||
return config->lastEntry - fromIndex; | |||
} | |||
void doBackup() { | |||
#ifdef BACKUPS_ENABLED | |||
debug::displayFreeRam(); | |||
VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED); | |||
Serial.println((unsigned int)_backups[0], HEX); | |||
_backups[0]->backup(); | |||
debug::displayFreeRam(); | |||
/*for (int i = 0; i < BACKUPS_ENABLED; i++) { | |||
VERBOSE_FORMAT("doBackup", "calling backup %d", i); | |||
delay(1000); | |||
@@ -16,7 +16,7 @@ namespace positions { | |||
namespace details { | |||
bool isBackupNeeded(SdPositionConfig_t &sdConfig) { | |||
Config_t referenceConfig = config::main::get(); | |||
Config_t referenceConfig = config::main::value; | |||
sdConfig = config::backup::sd::get(); | |||
return sdConfig.lastSavedEntry == 0xFFFF || | |||