diff --git a/extras/test/src/time/test_TimedAttempt.cpp b/extras/test/src/time/test_TimedAttempt.cpp index 0a8fa71..5673dbb 100644 --- a/extras/test/src/time/test_TimedAttempt.cpp +++ b/extras/test/src/time/test_TimedAttempt.cpp @@ -14,8 +14,6 @@ #include #include -using namespace arduino::tattempt; - SCENARIO("Test broker connection retries") { TimedAttempt _connection_attempt(0,0); diff --git a/src/Arduino_HEX.h b/src/Arduino_HEX.h index 9da8844..7ded297 100644 --- a/src/Arduino_HEX.h +++ b/src/Arduino_HEX.h @@ -10,5 +10,5 @@ #pragma once #include "./hex/hex.h" + using namespace arduino; -using namespace hex; diff --git a/src/Arduino_SHA256.h b/src/Arduino_SHA256.h index 6376d53..3c5587b 100644 --- a/src/Arduino_SHA256.h +++ b/src/Arduino_SHA256.h @@ -10,5 +10,3 @@ #pragma once #include "./sha256/SHA256.h" -using namespace arduino; -using namespace sha256; diff --git a/src/Arduino_TimedAttempt.h b/src/Arduino_TimedAttempt.h index eaa359f..4439a81 100644 --- a/src/Arduino_TimedAttempt.h +++ b/src/Arduino_TimedAttempt.h @@ -10,5 +10,3 @@ #pragma once #include "./time/TimedAttempt.h" - -using namespace arduino::tattempt; diff --git a/src/hex/hex.h b/src/hex/hex.h index 8d85c39..3344ed4 100644 --- a/src/hex/hex.h +++ b/src/hex/hex.h @@ -22,21 +22,19 @@ namespace arduino { namespace hex { String encodeUpper(const uint8_t* in, uint32_t size); bool decode(const String in, uint8_t* out, uint32_t size); +}} // arduino::hex - class THEXT { - - public: - static inline String encode(const uint8_t* in, uint32_t size) { - return arduino::hex::encode(in, size); - } - static inline String encodeUpper(const uint8_t* in, uint32_t size) { - return arduino::hex::encodeUpper(in, size); - } - - static inline bool decode(const String in, uint8_t* out, uint32_t size) { - return arduino::hex::decode(in, out, size); - } +class THEXT { +public: + static inline String encode(const uint8_t* in, uint32_t size) { + return arduino::hex::encode(in, size); + } + static inline String encodeUpper(const uint8_t* in, uint32_t size) { + return arduino::hex::encodeUpper(in, size); + } - }; + static inline bool decode(const String in, uint8_t* out, uint32_t size) { + return arduino::hex::decode(in, out, size); + } -}} // arduino::hex +}; diff --git a/src/sha256/SHA256.h b/src/sha256/SHA256.h index bf4b92b..ee6710c 100644 --- a/src/sha256/SHA256.h +++ b/src/sha256/SHA256.h @@ -27,27 +27,27 @@ namespace arduino { namespace sha256 { ::acu_sha256(input, ilen, output); } - class SHA256 { - public: +}} // arduino::sha256 - static constexpr uint32_t HASH_SIZE = SHA256_DIGEST_SIZE; +class SHA256 { +public: - inline void begin() { - return arduino::sha256::begin(&_ctx); - } - inline void update(uint8_t const * data, uint32_t const len) { - return arduino::sha256::update(&_ctx, data, len); - } - inline void finalize(uint8_t * hash) { - return arduino::sha256::finalize(&_ctx, hash); - } - static inline void sha256(uint8_t const * data, uint32_t const len, uint8_t * hash) { - return arduino::sha256::sha256(data, len, hash); - } + static constexpr uint32_t HASH_SIZE = SHA256_DIGEST_SIZE; - private: + inline void begin() { + return arduino::sha256::begin(&_ctx); + } + inline void update(uint8_t const * data, uint32_t const len) { + return arduino::sha256::update(&_ctx, data, len); + } + inline void finalize(uint8_t * hash) { + return arduino::sha256::finalize(&_ctx, hash); + } + static inline void sha256(uint8_t const * data, uint32_t const len, uint8_t * hash) { + return arduino::sha256::sha256(data, len, hash); + } - acu_sha256_ctx _ctx; - }; +private: -}} // arduino::sha256 + acu_sha256_ctx _ctx; +}; diff --git a/src/time/TimedAttempt.cpp b/src/time/TimedAttempt.cpp index 2d72fda..5dd39e9 100644 --- a/src/time/TimedAttempt.cpp +++ b/src/time/TimedAttempt.cpp @@ -12,67 +12,63 @@ #include #include "TimedAttempt.h" -namespace arduino { namespace tattempt { - - TimedAttempt::TimedAttempt(unsigned long minDelay, unsigned long maxDelay) - : _minDelay(minDelay) - , _maxDelay(maxDelay) { - } - - void TimedAttempt::begin(unsigned long delay) { - _retryCount = 0; - _retryDelay = 0; - _retryTick = 0; - _minDelay = delay; - _maxDelay = delay; - } - - void TimedAttempt::begin(unsigned long minDelay, unsigned long maxDelay) { - _retryCount = 0; - _retryDelay = 0; - _retryTick = 0; - _minDelay = minDelay; - _maxDelay = maxDelay; - } - - unsigned long TimedAttempt::reconfigure(unsigned long minDelay, unsigned long maxDelay) { - _minDelay = minDelay; - _maxDelay = maxDelay; - return reload(); - } - - unsigned long TimedAttempt::retry() { - _retryCount++; - return reload(); - } - - unsigned long TimedAttempt::reload() { - unsigned long shift = _retryCount > 31 ? 31 : _retryCount; - unsigned long delay = (1UL << shift) * _minDelay; - /* delay should always increase */ - _retryDelay = (delay > _retryDelay) ? min(delay, _maxDelay): _maxDelay; - _retryTick = millis(); - return _retryDelay; - } - - void TimedAttempt::reset() { - _retryCount = 0; - } - - bool TimedAttempt::isRetry() { - return _retryCount > 0; - } - - bool TimedAttempt::isExpired() { - return millis() - _retryTick > _retryDelay; - } - - unsigned int TimedAttempt::getRetryCount() { - return _retryCount; - } - - unsigned int TimedAttempt::getWaitTime() { - return _retryDelay; - } - -}} // arduino::tattempt +TimedAttempt::TimedAttempt(unsigned long minDelay, unsigned long maxDelay) +: _minDelay(minDelay) +, _maxDelay(maxDelay) { +} + +void TimedAttempt::begin(unsigned long delay) { + _retryCount = 0; + _retryDelay = 0; + _retryTick = 0; + _minDelay = delay; + _maxDelay = delay; +} + +void TimedAttempt::begin(unsigned long minDelay, unsigned long maxDelay) { + _retryCount = 0; + _retryDelay = 0; + _retryTick = 0; + _minDelay = minDelay; + _maxDelay = maxDelay; +} + +unsigned long TimedAttempt::reconfigure(unsigned long minDelay, unsigned long maxDelay) { + _minDelay = minDelay; + _maxDelay = maxDelay; + return reload(); +} + +unsigned long TimedAttempt::retry() { + _retryCount++; + return reload(); +} + +unsigned long TimedAttempt::reload() { + unsigned long shift = _retryCount > 31 ? 31 : _retryCount; + unsigned long delay = (1UL << shift) * _minDelay; + /* delay should always increase */ + _retryDelay = (delay > _retryDelay) ? min(delay, _maxDelay): _maxDelay; + _retryTick = millis(); + return _retryDelay; +} + +void TimedAttempt::reset() { + _retryCount = 0; +} + +bool TimedAttempt::isRetry() { + return _retryCount > 0; +} + +bool TimedAttempt::isExpired() { + return millis() - _retryTick > _retryDelay; +} + +unsigned int TimedAttempt::getRetryCount() { + return _retryCount; +} + +unsigned int TimedAttempt::getWaitTime() { + return _retryDelay; +} diff --git a/src/time/TimedAttempt.h b/src/time/TimedAttempt.h index c5d67de..931a302 100644 --- a/src/time/TimedAttempt.h +++ b/src/time/TimedAttempt.h @@ -10,91 +10,87 @@ #pragma once -namespace arduino { namespace tattempt { +/** + * The TimedAttempt class manages retry attempts with configurable delays. + * It allows setting minimum and maximum delays, and provides methods to + * begin, retry, reset, and check the status of attempts. + */ +class TimedAttempt { + +public: + /** + * Constructor to initialize TimedAttempt with minimum and maximum delay + * @param minDelay: minimum delay between attempts + * @param maxDelay: maximum delay between attempts + */ + TimedAttempt(unsigned long minDelay, unsigned long maxDelay); + + /** + * Begin the attempt with a fixed delay + * @param delay: fixed delay between attempts + */ + void begin(unsigned long delay); + + /** + * Begin the attempt with a range of delays + * @param minDelay: minimum delay between attempts + * @param maxDelay: maximum delay between attempts + */ + void begin(unsigned long minDelay, unsigned long maxDelay); + + /** + * Reconfigure the attempt with new minimum and maximum delays and reload + * @param minDelay: new minimum delay between attempts + * @param maxDelay: new maximum delay between attempts + * @return the new delay after reconfiguration + */ + unsigned long reconfigure(unsigned long minDelay, unsigned long maxDelay); + + /** + * Retry the attempt, incrementing the retry count and reloading the delay + * @return the new delay after retry + */ + unsigned long retry(); + + /** + * Reload the delay based on the retry count and return the new delay + * @return the new delay after reload + */ + unsigned long reload(); + + /** + * Reset the retry count to zero + */ + void reset(); + + /** + * Check if a retry has been attempted + * @return true if a retry has been attempted, false otherwise + */ + bool isRetry(); + + /** + * Check if the current attempt has expired based on the delay + * @return true if the current attempt has expired, false otherwise + */ + bool isExpired(); + + /** + * Get the current retry count + * @return the current retry count + */ + unsigned int getRetryCount(); /** - * The TimedAttempt class manages retry attempts with configurable delays. - * It allows setting minimum and maximum delays, and provides methods to - * begin, retry, reset, and check the status of attempts. - */ - class TimedAttempt { - - public: - /** - * Constructor to initialize TimedAttempt with minimum and maximum delay - * @param minDelay: minimum delay between attempts - * @param maxDelay: maximum delay between attempts - */ - TimedAttempt(unsigned long minDelay, unsigned long maxDelay); - - /** - * Begin the attempt with a fixed delay - * @param delay: fixed delay between attempts - */ - void begin(unsigned long delay); - - /** - * Begin the attempt with a range of delays - * @param minDelay: minimum delay between attempts - * @param maxDelay: maximum delay between attempts - */ - void begin(unsigned long minDelay, unsigned long maxDelay); - - /** - * Reconfigure the attempt with new minimum and maximum delays and reload - * @param minDelay: new minimum delay between attempts - * @param maxDelay: new maximum delay between attempts - * @return the new delay after reconfiguration - */ - unsigned long reconfigure(unsigned long minDelay, unsigned long maxDelay); - - /** - * Retry the attempt, incrementing the retry count and reloading the delay - * @return the new delay after retry - */ - unsigned long retry(); - - /** - * Reload the delay based on the retry count and return the new delay - * @return the new delay after reload - */ - unsigned long reload(); - - /** - * Reset the retry count to zero - */ - void reset(); - - /** - * Check if a retry has been attempted - * @return true if a retry has been attempted, false otherwise - */ - bool isRetry(); - - /** - * Check if the current attempt has expired based on the delay - * @return true if the current attempt has expired, false otherwise - */ - bool isExpired(); - - /** - * Get the current retry count - * @return the current retry count - */ - unsigned int getRetryCount(); - - /** - * Get the current wait time for the next retry - * @return the current wait time for the next retry - */ - unsigned int getWaitTime(); - - private: - unsigned long _minDelay; - unsigned long _maxDelay; - unsigned long _retryTick; - unsigned long _retryDelay; - unsigned int _retryCount; - }; - -}} // arduino::tattempt + * Get the current wait time for the next retry + * @return the current wait time for the next retry + */ + unsigned int getWaitTime(); + +private: + unsigned long _minDelay; + unsigned long _maxDelay; + unsigned long _retryTick; + unsigned long _retryDelay; + unsigned int _retryCount; +};