From 9df531837ecc0f5347a35af370deb692498d2aaf Mon Sep 17 00:00:00 2001 From: charliekush Date: Mon, 13 Jan 2025 13:41:50 -0800 Subject: [PATCH] feat: added rtc time tracking as nvram entry --- src/cellular/dataUpload.cpp | 19 +++++- src/sys/NVRAM.cpp | 2 + src/sys/NVRAM.hpp | 129 ++++++++++++++++++------------------ 3 files changed, 84 insertions(+), 66 deletions(-) diff --git a/src/cellular/dataUpload.cpp b/src/cellular/dataUpload.cpp index fe2d626b..ce1c688c 100644 --- a/src/cellular/dataUpload.cpp +++ b/src/cellular/dataUpload.cpp @@ -56,6 +56,23 @@ STATES_e DataUpload::can_upload(void) STATES_e DataUpload::run(void) { + uint16_t reupload_delay; + + pSystemDesc->pNvram->get(NVRAM::REUPLOAD_WAIT_TIME, reupload_delay); + if (reupload_delay == 0) + { + pSystemDesc->pNvram->put(NVRAM::REUPLOAD_WAIT_TIME, 10); + } + else if (reupload_delay <= 900) + { + reupload_delay *= 2; + } + else + { + reupload_delay = 1800; + } + pSystemDesc->pNvram->put(NVRAM::REUPLOAD_WAIT_TIME, reupload_delay); + uint8_t binary_packet_buffer[SF_PACKET_SIZE]; char ascii_record_buffer[SF_RECORD_SIZE + 1]; char publishName[DU_PUBLISH_ID_NAME_LEN + 1]; @@ -127,6 +144,7 @@ STATES_e DataUpload::run(void) FLOG_AddError(FLOG_UPL_PUB_FAIL, 0); return STATE_DEEP_SLEEP; case sf::cloud::SUCCESS: + pSystemDesc->pNvram->put(NVRAM::REUPLOAD_WAIT_TIME, 0); break; } @@ -146,7 +164,6 @@ STATES_e DataUpload::run(void) } } return next_state; - } void DataUpload::exit(void) diff --git a/src/sys/NVRAM.cpp b/src/sys/NVRAM.cpp index f47e55b3..1ec43411 100644 --- a/src/sys/NVRAM.cpp +++ b/src/sys/NVRAM.cpp @@ -63,4 +63,6 @@ void NVRAM::displayNVRAM(void) SF_OSAL_printf("No Upload Flag: 0x%02hx" __NL__, u8); this->get(CLOUD_CONNECT_COUNTER, u16); SF_OSAL_printf("Cloud Connect Counter: 0x%04hx" __NL__, u16); + this->get(REUPLOAD_WAIT_TIME, u16); + SF_OSAL_printf("Upload reattempt time: s0x%04hx" __NL__, u16); } diff --git a/src/sys/NVRAM.hpp b/src/sys/NVRAM.hpp index 648bb0e0..c8d1ac87 100644 --- a/src/sys/NVRAM.hpp +++ b/src/sys/NVRAM.hpp @@ -7,75 +7,74 @@ class NVRAM { public: - typedef enum DATA_ID_ - { - NVRAM_VALID, - BOOT_BEHAVIOR, - TMP116_CAL_VALUE, - TMP116_CAL_CURRENT_ATTEMPT, - TMP116_CAL_ATTEMPTS_TOTAL, - TMP116_CAL_DATA_COLLECTION_PERIOD_SEC, - TMP116_CAL_CYCLE_PERIOD_SEC, - UPLOAD_REATTEMPTS, - NO_UPLOAD_FLAG, - CLOUD_CONNECT_COUNTER, - NUM_DATA_IDs - }DATA_ID_e; - - typedef struct nvramTableEntry_ - { - DATA_ID_e id; - - uint32_t addr; - uint32_t len; - }nvramTableEntry_t; - - const nvramTableEntry_t LAYOUT[NUM_DATA_IDs] = - { - {NVRAM_VALID, 0x0000, sizeof(uint8_t)}, - {BOOT_BEHAVIOR, 0x0001, sizeof(uint8_t)}, - {TMP116_CAL_VALUE, 0x0002, sizeof(uint16_t)}, - {TMP116_CAL_CURRENT_ATTEMPT, 0x0004, sizeof(uint8_t)}, - {TMP116_CAL_ATTEMPTS_TOTAL, 0x0005, sizeof(uint8_t)}, - {TMP116_CAL_DATA_COLLECTION_PERIOD_SEC, 0x0008, sizeof(uint32_t)}, - {TMP116_CAL_CYCLE_PERIOD_SEC, 0x000C, sizeof(uint32_t)}, - {UPLOAD_REATTEMPTS, 0x0014, sizeof(uint8_t)}, - {NO_UPLOAD_FLAG, 0x0015, sizeof(uint8_t)}, - {CLOUD_CONNECT_COUNTER, 0x0016, sizeof(uint16_t)}, - - }; - static NVRAM& getInstance(void); - - - /** - * @brief Get data from NVRAM memory - * - * @tparam T Type - * @param dataID ID of data to get - * @param pData Data - * @return int Sucsess - */ - template int get(DATA_ID_e dataID, T& pData) - { - uint32_t addr; - const NVRAM::nvramTableEntry_t* tableEntry; - - tableEntry = this->getTableEntry(dataID); - if(!tableEntry) + typedef enum DATA_ID_ { - return 0; - } - - if(sizeof(pData) != tableEntry->len) + NVRAM_VALID, + BOOT_BEHAVIOR, + TMP116_CAL_VALUE, + TMP116_CAL_CURRENT_ATTEMPT, + TMP116_CAL_ATTEMPTS_TOTAL, + TMP116_CAL_DATA_COLLECTION_PERIOD_SEC, + TMP116_CAL_CYCLE_PERIOD_SEC, + UPLOAD_REATTEMPTS, + NO_UPLOAD_FLAG, + CLOUD_CONNECT_COUNTER, + REUPLOAD_WAIT_TIME, + NUM_DATA_IDs + + } DATA_ID_e; + + typedef struct nvramTableEntry_ { - return 0; + DATA_ID_e id; + + uint32_t addr; + uint32_t len; + } nvramTableEntry_t; + + const nvramTableEntry_t LAYOUT[NUM_DATA_IDs] = { + {NVRAM_VALID, 0x0000, sizeof(uint8_t)}, + {BOOT_BEHAVIOR, 0x0001, sizeof(uint8_t)}, + {TMP116_CAL_VALUE, 0x0002, sizeof(uint16_t)}, + {TMP116_CAL_CURRENT_ATTEMPT, 0x0004, sizeof(uint8_t)}, + {TMP116_CAL_ATTEMPTS_TOTAL, 0x0005, sizeof(uint8_t)}, + {TMP116_CAL_DATA_COLLECTION_PERIOD_SEC, 0x0008, sizeof(uint32_t)}, + {TMP116_CAL_CYCLE_PERIOD_SEC, 0x000C, sizeof(uint32_t)}, + {UPLOAD_REATTEMPTS, 0x0014, sizeof(uint8_t)}, + {NO_UPLOAD_FLAG, 0x0015, sizeof(uint8_t)}, + {CLOUD_CONNECT_COUNTER, 0x0016, sizeof(uint16_t)}, + {REUPLOAD_WAIT_TIME, 0x0018, sizeof(uint16_t)}}; + static NVRAM &getInstance(void); + + /** + * @brief Get data from NVRAM memory + * + * @tparam T Type + * @param dataID ID of data to get + * @param pData Data + * @return int Sucsess + */ + template int get(DATA_ID_e dataID, T &pData) + { + uint32_t addr; + const NVRAM::nvramTableEntry_t *tableEntry; + + tableEntry = this->getTableEntry(dataID); + if (!tableEntry) + { + return 0; + } + + if (sizeof(pData) != tableEntry->len) + { + return 0; + } + + addr = tableEntry->addr; + EEPROM.get(addr, pData); + return 1; } - addr = tableEntry->addr; - EEPROM.get(addr, pData); - return 1; - } - /** * @brief Put data into memory *