19#ifndef BLE_COMMON_DURATION_H_
20#define BLE_COMMON_DURATION_H_
24#include "platform/mbed_assert.h"
25#include "platform/mbed_chrono.h"
29#if !defined(DOXYGEN_ONLY)
36template<u
int32_t Min, u
int32_t Max, u
int32_t Default = Min>
38 static const uint32_t MIN = Min;
39 static const uint32_t MAX = Max;
40 static const uint32_t DEFAULT = Default;
56struct DefaultRange<uint8_t> {
57 typedef Range<0, 0xFF> type;
64struct DefaultRange<uint16_t > {
65 typedef Range<0, 0xFFFF> type;
72struct DefaultRange<uint32_t> {
73 typedef Range<0, 0xFFFFFFFF> type;
82template<
typename T, T V>
84 static const T VALUE = V;
101 typename Range =
typename DefaultRange<Rep>::type,
102 typename Forever =
void*
140 template<
typename OtherRep, u
int32_t OtherTB,
typename OtherRange,
typename OtherF>
142 duration(clamp(other.
value() * (OtherTB / TB)))
144 static_assert(OtherTB >= TB && (OtherTB % TB) == 0,
"Incompatible units");
158 template<
typename OtherRep,
typename OtherRange,
typename OtherF>
160 duration(clamp(((other_ms.
value() * 1000) + TB - 1) / TB))
181 return ((uint32_t)duration * TB) / 1000;
192 static const Rep
MIN = Range::MIN;
197 static const Rep
MAX = Range::MAX;
238#if defined(DOXYGEN_ONLY)
246 template<
typename DefaultForever =
void*>
248 std::is_same<DefaultForever, Forever>::value,
257 template<
typename DefaultForever =
void*>
259 !std::is_same<DefaultForever, Forever>::value,
264 return duration == Forever::VALUE;
272 std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>
277 return std::chrono::duration<Rep, typename std::ratio<TB, 1000000>::type>{duration};
281 static Rep clamp(Rep in)
285 }
else if (in >
MAX) {
323template<
typename DurationOut,
typename RepIn, u
int32_t TBIn,
typename RangeIn,
typename FIn>
326 return DurationOut(((duration.
value() * TBIn) + DurationOut::TIME_BASE - 1) / DurationOut::TIME_BASE);
338 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
339 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS>
356template<
typename Rep, u
int32_t TB,
typename Range,
typename F>
375template<
typename Rep, u
int32_t TB,
typename Range,
typename F>
391template<
typename Rep, u
int32_t TB,
typename Range,
typename F>
406 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
407 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
422template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
437 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
438 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
456template<
typename Rep, u
int32_t Us,
typename Range>
471 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
472 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
490template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
505 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
506 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
513 return !(lhs == rhs);
524template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
527 return !(lhs == rhs);
539 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
540 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
558template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
573 typename RepLHS, uint32_t TBLHS,
typename RangeLHS,
typename FLHS,
574 typename RepRHS, uint32_t TBRHS,
typename RangeRHS,
typename FRHS
592template<
typename Rep, u
int32_t Us,
typename Range,
typename F>
600#if !defined(DOXYGEN_ONLY)
602template<u
int32_t Min, u
int32_t Max, u
int32_t Default>
603const uint32_t Range<Min, Max, Default>::MIN;
605template<u
int32_t Min, u
int32_t Max, u
int32_t Default>
606const uint32_t Range<Min, Max, Default>::MAX;
608template<u
int32_t Min, u
int32_t Max, u
int32_t Default>
609const uint32_t Range<Min, Max, Default>::DEFAULT;
611template<
typename T, T V>
612const T Value<T, V>::VALUE;
616template<
typename Rep, u
int32_t TB,
typename Range,
typename Forever>
619template<
typename Rep, u
int32_t TB,
typename Range,
typename Forever>
622template<
typename Rep, u
int32_t TB,
typename Range,
typename Forever>
Entry namespace for all BLE API definitions.
Duration< uint32_t, 1000 *millisecond_t::TIME_BASE > second_t
Type that represents seconds.
Duration< uint32_t, 1 > microsecond_t
Type that represents micro seconds.
Duration< uint32_t, 1000 *microsecond_t::TIME_BASE > millisecond_t
Type that represents milliseconds.
Duration< Rep, TB, Range, F > operator+(Duration< Rep, TB, Range, F > lhs, Duration< Rep, TB, Range, F > rhs)
Add two durations together.
static const uint32_t TIME_BASE
The time base.
std::chrono::duration< Rep, typename std::ratio< TB, 1000000 >::type > valueChrono() const
Convert the duration into an std::chrono one.
bool operator!=(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is not equal to the duration rhs.
DurationOut durationCast(Duration< RepIn, TBIn, RangeIn, FIn > duration)
Cast a duration to another.
bool operator<=(Duration< Rep, Us, Range > lhs, Duration< Rep, Us, Range > rhs)
Indicate if the duration lhs is less than or equal to the duration rhs.
bool operator<(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs is less than the duration rhs.
static const Rep MIN
Left-bound of the duration range.
Rep value() const
Return the duration in TB units.
static const Rep MAX
Right bound of the duration range.
Duration(Duration< OtherRep, OtherTB, OtherRange, OtherF > other)
Construct a Duration from another Duration.
bool operator==(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs is equal to the duration rhs.
Duration< Rep, TB, Range, F > operator*(Duration< Rep, TB, Range, F > lhs, uint32_t rhs)
Multiply a duration and a positive integer.
bool operator==(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is equal to the duration rhs.
static Duration min()
Return the minimum duration.
bool operator>=(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs greater or equal to the duration rhs.
Duration(Rep v)
Construct a Duration from an integer value.
bool operator>(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs greater than the duration rhs.
static Duration max()
Return the maximum duration.
const Rep * storage() const
Return a pointer to the value of the duration.
Duration< Rep, TB, Range, F > operator*(uint32_t lhs, Duration< Rep, TB, Range, F > rhs)
Multiply a duration and a positive integer.
bool operator>(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs greater than the duration rhs.
uint32_t valueInMs() const
Return the duration in milliseconds.
bool operator!=(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs is not equal to the duration rhs.
bool isForever() const
Test if the forever value is being held.
microsecond_t operator+(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Add two durations together and return the result in microseconds.
Rep representation_t
Type of the actual representation.
Duration()
Construct a default Duration.
Duration(Duration< OtherRep, 1000, OtherRange, OtherF > other_ms, void *=nullptr)
Construct a new Duration from a Duration in milliseconds.
bool operator<(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is less than the duration rhs.
bool operator>=(Duration< Rep, Us, Range, F > lhs, Duration< Rep, Us, Range, F > rhs)
Indicate if the duration lhs greater or equal to the duration rhs.
bool operator<=(Duration< RepLHS, TBLHS, RangeLHS, FLHS > lhs, Duration< RepRHS, TBRHS, RangeRHS, FRHS > rhs)
Indicate if the duration lhs is less than or equal to the duration rhs.
static Duration forever()
Return the Duration value meaning forever.