Skip to main content

smgw_cls/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3#![doc = include_str!("../README.md")]
4
5use core::time::Duration;
6
7/// Returns the reconnect delay after a failed connection attempt.
8///
9/// The first retry uses `initial` (limited to `maximum`). Each subsequent retry
10/// doubles the delay up to the cap. A zero initial delay stays zero.
11///
12/// ```
13/// use core::time::Duration;
14/// use smgw_cls::reconnect_delay;
15///
16/// let delay = |retry| reconnect_delay(Duration::from_secs(1), Duration::from_secs(5), retry);
17/// assert_eq!(delay(0), Duration::from_secs(1));
18/// assert_eq!(delay(2), Duration::from_secs(4));
19/// assert_eq!(delay(u32::MAX), Duration::from_secs(5));
20/// ```
21pub fn reconnect_delay(initial: Duration, maximum: Duration, retry: u32) -> Duration {
22    if initial.is_zero() {
23        return Duration::ZERO;
24    }
25    let mut delay = initial.min(maximum);
26    for _ in 0..retry {
27        if delay == maximum {
28            break;
29        }
30        delay = delay.saturating_mul(2).min(maximum);
31    }
32    delay
33}
34
35pub mod engine;
36pub mod fnn;
37
38#[cfg(feature = "runtime")]
39pub mod transport;
40
41#[cfg(feature = "fnn-bridge")]
42pub mod bridge;
43
44/// CLS.EEDI message types, backend and device engines, and MQTT 5 sessions.
45#[cfg(feature = "eedi")]
46pub use cls_eedi as eedi;