Skip to content

Commit 08b84e7

Browse files
moving TimedAttemp out of the namespace
1 parent 054e463 commit 08b84e7

File tree

3 files changed

+142
-152
lines changed

3 files changed

+142
-152
lines changed

Diff for: extras/test/src/time/test_TimedAttempt.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <Arduino.h>
1515
#include <limits.h>
1616

17-
using namespace arduino::tattempt;
18-
1917
SCENARIO("Test broker connection retries") {
2018
TimedAttempt _connection_attempt(0,0);
2119

Diff for: src/time/TimedAttempt.cpp

+60-64
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,63 @@
1212
#include <Arduino.h>
1313
#include "TimedAttempt.h"
1414

15-
namespace arduino { namespace tattempt {
16-
17-
TimedAttempt::TimedAttempt(unsigned long minDelay, unsigned long maxDelay)
18-
: _minDelay(minDelay)
19-
, _maxDelay(maxDelay) {
20-
}
21-
22-
void TimedAttempt::begin(unsigned long delay) {
23-
_retryCount = 0;
24-
_retryDelay = 0;
25-
_retryTick = 0;
26-
_minDelay = delay;
27-
_maxDelay = delay;
28-
}
29-
30-
void TimedAttempt::begin(unsigned long minDelay, unsigned long maxDelay) {
31-
_retryCount = 0;
32-
_retryDelay = 0;
33-
_retryTick = 0;
34-
_minDelay = minDelay;
35-
_maxDelay = maxDelay;
36-
}
37-
38-
unsigned long TimedAttempt::reconfigure(unsigned long minDelay, unsigned long maxDelay) {
39-
_minDelay = minDelay;
40-
_maxDelay = maxDelay;
41-
return reload();
42-
}
43-
44-
unsigned long TimedAttempt::retry() {
45-
_retryCount++;
46-
return reload();
47-
}
48-
49-
unsigned long TimedAttempt::reload() {
50-
unsigned long shift = _retryCount > 31 ? 31 : _retryCount;
51-
unsigned long delay = (1UL << shift) * _minDelay;
52-
/* delay should always increase */
53-
_retryDelay = (delay > _retryDelay) ? min(delay, _maxDelay): _maxDelay;
54-
_retryTick = millis();
55-
return _retryDelay;
56-
}
57-
58-
void TimedAttempt::reset() {
59-
_retryCount = 0;
60-
}
61-
62-
bool TimedAttempt::isRetry() {
63-
return _retryCount > 0;
64-
}
65-
66-
bool TimedAttempt::isExpired() {
67-
return millis() - _retryTick > _retryDelay;
68-
}
69-
70-
unsigned int TimedAttempt::getRetryCount() {
71-
return _retryCount;
72-
}
73-
74-
unsigned int TimedAttempt::getWaitTime() {
75-
return _retryDelay;
76-
}
77-
78-
}} // arduino::tattempt
15+
TimedAttempt::TimedAttempt(unsigned long minDelay, unsigned long maxDelay)
16+
: _minDelay(minDelay)
17+
, _maxDelay(maxDelay) {
18+
}
19+
20+
void TimedAttempt::begin(unsigned long delay) {
21+
_retryCount = 0;
22+
_retryDelay = 0;
23+
_retryTick = 0;
24+
_minDelay = delay;
25+
_maxDelay = delay;
26+
}
27+
28+
void TimedAttempt::begin(unsigned long minDelay, unsigned long maxDelay) {
29+
_retryCount = 0;
30+
_retryDelay = 0;
31+
_retryTick = 0;
32+
_minDelay = minDelay;
33+
_maxDelay = maxDelay;
34+
}
35+
36+
unsigned long TimedAttempt::reconfigure(unsigned long minDelay, unsigned long maxDelay) {
37+
_minDelay = minDelay;
38+
_maxDelay = maxDelay;
39+
return reload();
40+
}
41+
42+
unsigned long TimedAttempt::retry() {
43+
_retryCount++;
44+
return reload();
45+
}
46+
47+
unsigned long TimedAttempt::reload() {
48+
unsigned long shift = _retryCount > 31 ? 31 : _retryCount;
49+
unsigned long delay = (1UL << shift) * _minDelay;
50+
/* delay should always increase */
51+
_retryDelay = (delay > _retryDelay) ? min(delay, _maxDelay): _maxDelay;
52+
_retryTick = millis();
53+
return _retryDelay;
54+
}
55+
56+
void TimedAttempt::reset() {
57+
_retryCount = 0;
58+
}
59+
60+
bool TimedAttempt::isRetry() {
61+
return _retryCount > 0;
62+
}
63+
64+
bool TimedAttempt::isExpired() {
65+
return millis() - _retryTick > _retryDelay;
66+
}
67+
68+
unsigned int TimedAttempt::getRetryCount() {
69+
return _retryCount;
70+
}
71+
72+
unsigned int TimedAttempt::getWaitTime() {
73+
return _retryDelay;
74+
}

Diff for: src/time/TimedAttempt.h

+82-86
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,87 @@
1010

1111
#pragma once
1212

13-
namespace arduino { namespace tattempt {
13+
/**
14+
* The TimedAttempt class manages retry attempts with configurable delays.
15+
* It allows setting minimum and maximum delays, and provides methods to
16+
* begin, retry, reset, and check the status of attempts.
17+
*/
18+
class TimedAttempt {
19+
20+
public:
21+
/**
22+
* Constructor to initialize TimedAttempt with minimum and maximum delay
23+
* @param minDelay: minimum delay between attempts
24+
* @param maxDelay: maximum delay between attempts
25+
*/
26+
TimedAttempt(unsigned long minDelay, unsigned long maxDelay);
27+
28+
/**
29+
* Begin the attempt with a fixed delay
30+
* @param delay: fixed delay between attempts
31+
*/
32+
void begin(unsigned long delay);
33+
34+
/**
35+
* Begin the attempt with a range of delays
36+
* @param minDelay: minimum delay between attempts
37+
* @param maxDelay: maximum delay between attempts
38+
*/
39+
void begin(unsigned long minDelay, unsigned long maxDelay);
40+
41+
/**
42+
* Reconfigure the attempt with new minimum and maximum delays and reload
43+
* @param minDelay: new minimum delay between attempts
44+
* @param maxDelay: new maximum delay between attempts
45+
* @return the new delay after reconfiguration
46+
*/
47+
unsigned long reconfigure(unsigned long minDelay, unsigned long maxDelay);
48+
49+
/**
50+
* Retry the attempt, incrementing the retry count and reloading the delay
51+
* @return the new delay after retry
52+
*/
53+
unsigned long retry();
54+
55+
/**
56+
* Reload the delay based on the retry count and return the new delay
57+
* @return the new delay after reload
58+
*/
59+
unsigned long reload();
60+
61+
/**
62+
* Reset the retry count to zero
63+
*/
64+
void reset();
65+
66+
/**
67+
* Check if a retry has been attempted
68+
* @return true if a retry has been attempted, false otherwise
69+
*/
70+
bool isRetry();
71+
72+
/**
73+
* Check if the current attempt has expired based on the delay
74+
* @return true if the current attempt has expired, false otherwise
75+
*/
76+
bool isExpired();
77+
78+
/**
79+
* Get the current retry count
80+
* @return the current retry count
81+
*/
82+
unsigned int getRetryCount();
1483

1584
/**
16-
* The TimedAttempt class manages retry attempts with configurable delays.
17-
* It allows setting minimum and maximum delays, and provides methods to
18-
* begin, retry, reset, and check the status of attempts.
19-
*/
20-
class TimedAttempt {
21-
22-
public:
23-
/**
24-
* Constructor to initialize TimedAttempt with minimum and maximum delay
25-
* @param minDelay: minimum delay between attempts
26-
* @param maxDelay: maximum delay between attempts
27-
*/
28-
TimedAttempt(unsigned long minDelay, unsigned long maxDelay);
29-
30-
/**
31-
* Begin the attempt with a fixed delay
32-
* @param delay: fixed delay between attempts
33-
*/
34-
void begin(unsigned long delay);
35-
36-
/**
37-
* Begin the attempt with a range of delays
38-
* @param minDelay: minimum delay between attempts
39-
* @param maxDelay: maximum delay between attempts
40-
*/
41-
void begin(unsigned long minDelay, unsigned long maxDelay);
42-
43-
/**
44-
* Reconfigure the attempt with new minimum and maximum delays and reload
45-
* @param minDelay: new minimum delay between attempts
46-
* @param maxDelay: new maximum delay between attempts
47-
* @return the new delay after reconfiguration
48-
*/
49-
unsigned long reconfigure(unsigned long minDelay, unsigned long maxDelay);
50-
51-
/**
52-
* Retry the attempt, incrementing the retry count and reloading the delay
53-
* @return the new delay after retry
54-
*/
55-
unsigned long retry();
56-
57-
/**
58-
* Reload the delay based on the retry count and return the new delay
59-
* @return the new delay after reload
60-
*/
61-
unsigned long reload();
62-
63-
/**
64-
* Reset the retry count to zero
65-
*/
66-
void reset();
67-
68-
/**
69-
* Check if a retry has been attempted
70-
* @return true if a retry has been attempted, false otherwise
71-
*/
72-
bool isRetry();
73-
74-
/**
75-
* Check if the current attempt has expired based on the delay
76-
* @return true if the current attempt has expired, false otherwise
77-
*/
78-
bool isExpired();
79-
80-
/**
81-
* Get the current retry count
82-
* @return the current retry count
83-
*/
84-
unsigned int getRetryCount();
85-
86-
/**
87-
* Get the current wait time for the next retry
88-
* @return the current wait time for the next retry
89-
*/
90-
unsigned int getWaitTime();
91-
92-
private:
93-
unsigned long _minDelay;
94-
unsigned long _maxDelay;
95-
unsigned long _retryTick;
96-
unsigned long _retryDelay;
97-
unsigned int _retryCount;
98-
};
99-
100-
}} // arduino::tattempt
85+
* Get the current wait time for the next retry
86+
* @return the current wait time for the next retry
87+
*/
88+
unsigned int getWaitTime();
89+
90+
private:
91+
unsigned long _minDelay;
92+
unsigned long _maxDelay;
93+
unsigned long _retryTick;
94+
unsigned long _retryDelay;
95+
unsigned int _retryCount;
96+
};

0 commit comments

Comments
 (0)