-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Class to handle retries and state transitions
- Loading branch information
Showing
4 changed files
with
166 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
This file is part of the Arduino_SecureElement library. | ||
Copyright (c) 2024 Arduino SA | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
/****************************************************************************** | ||
* INCLUDE | ||
******************************************************************************/ | ||
|
||
#include <Arduino.h> | ||
#include "TimedAttempt.h" | ||
|
||
/****************************************************************************** | ||
CTOR/DTOR | ||
******************************************************************************/ | ||
|
||
TimedAttempt::TimedAttempt(unsigned long minDelay, unsigned long maxDelay) | ||
: _minDelay(minDelay) | ||
, _maxDelay(maxDelay) { | ||
} | ||
|
||
/****************************************************************************** | ||
* PUBLIC MEMBER FUNCTIONS | ||
******************************************************************************/ | ||
|
||
void TimedAttempt::begin(unsigned long delay) { | ||
_retryCount = 0; | ||
_minDelay = delay; | ||
_maxDelay = delay; | ||
} | ||
|
||
void TimedAttempt::begin(unsigned long minDelay, unsigned long maxDelay) { | ||
_retryCount = 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 retryDelay = (1 << _retryCount) * _minDelay; | ||
retryDelay = min(retryDelay, _maxDelay); | ||
_nextRetryTick = millis() + retryDelay; | ||
return retryDelay; | ||
} | ||
|
||
void TimedAttempt::reset() { | ||
_retryCount = 0; | ||
} | ||
|
||
bool TimedAttempt::isRetry() { | ||
return _retryCount > 0; | ||
} | ||
|
||
bool TimedAttempt::isExpired() { | ||
return millis() > _nextRetryTick; | ||
} | ||
|
||
unsigned int TimedAttempt::getRetryCount() { | ||
return _retryCount; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
This file is part of the Arduino_SecureElement library. | ||
Copyright (c) 2024 Arduino SA | ||
This Source Code Form is subject to the terms of the Mozilla Public | ||
License, v. 2.0. If a copy of the MPL was not distributed with this | ||
file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
#ifndef TIMED_ATTEMPT_H | ||
#define TIMED_ATTEMPT_H | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
class TimedAttempt { | ||
|
||
public: | ||
TimedAttempt(unsigned long minDelay, unsigned long maxDelay); | ||
|
||
void begin(unsigned long delay); | ||
void begin(unsigned long minDelay, unsigned long maxDelay); | ||
unsigned long reconfigure(unsigned long minDelay, unsigned long maxDelay); | ||
unsigned long retry(); | ||
unsigned long reload(); | ||
void reset(); | ||
bool isRetry(); | ||
bool isExpired(); | ||
unsigned int getRetryCount(); | ||
|
||
private: | ||
unsigned long _minDelay; | ||
unsigned long _maxDelay; | ||
unsigned long _nextRetryTick; | ||
unsigned int _retryCount; | ||
}; | ||
|
||
#endif /* TIMED_ATTEMPT_H */ |