use core::future::Future;
pub mod sim;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub mod os;
mod time;
pub use time::{Duration, Instant};
use crate::error::Result;
pub trait Timer {
fn now(&self) -> Instant;
fn sleep_until(&self, deadline: Instant) -> impl Future<Output = ()>;
fn sleep(&self, duration: Duration) -> impl Future<Output = ()> {
self.sleep_until(self.now().saturating_add(duration))
}
}
pub trait Clock {
fn utc(&self) -> Option<u64>;
fn set_utc(&self, _micros: u64) -> Result<()> {
Err(crate::error::Error::new(crate::error::ErrorCode::Platform))
}
fn granularity(&self) -> Granularity {
Granularity::NoTimeGranularity
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum Granularity {
NoTimeGranularity = 0,
MinutesGranularity = 1,
SecondsGranularity = 2,
MillisecondsGranularity = 3,
MicrosecondsGranularity = 4,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoClock;
impl Clock for NoClock {
fn utc(&self) -> Option<u64> {
None
}
}
pub trait Rng {
fn fill(&self, out: &mut [u8]) -> Result<()>;
fn next_u32(&self) -> Result<u32> {
let mut b = [0u8; 4];
self.fill(&mut b)?;
Ok(u32::from_le_bytes(b))
}
fn next_u64(&self) -> Result<u64> {
let mut b = [0u8; 8];
self.fill(&mut b)?;
Ok(u64::from_le_bytes(b))
}
fn next_u16(&self) -> Result<u16> {
let mut b = [0u8; 2];
self.fill(&mut b)?;
Ok(u16::from_le_bytes(b))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PeerAddr {
pub addr: [u8; 16],
pub port: u16,
pub scope_id: u32,
}
impl PeerAddr {
#[must_use]
pub const fn new(addr: [u8; 16]) -> Self {
Self {
addr,
port: crate::PORT,
scope_id: 0,
}
}
#[must_use]
pub const fn with_port(addr: [u8; 16], port: u16) -> Self {
Self {
addr,
port,
scope_id: 0,
}
}
#[must_use]
pub const fn is_link_local(&self) -> bool {
self.addr[0] == 0xfe && (self.addr[1] & 0xc0) == 0x80
}
#[must_use]
pub const fn is_multicast(&self) -> bool {
self.addr[0] == 0xff
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Peer {
Udp(PeerAddr),
Ble(u16),
Tcp(PeerAddr, u16),
}
impl Peer {
#[must_use]
pub const fn is_reliable(&self) -> bool {
match self {
Self::Udp(_) => false,
Self::Ble(_) | Self::Tcp(..) => true,
}
}
#[must_use]
pub const fn supports_large_payloads(&self) -> bool {
matches!(self, Self::Tcp(..))
}
#[must_use]
pub const fn addr(&self) -> Option<PeerAddr> {
match self {
Self::Udp(addr) | Self::Tcp(addr, _) => Some(*addr),
Self::Ble(_) => None,
}
}
}
impl From<PeerAddr> for Peer {
fn from(addr: PeerAddr) -> Self {
Self::Udp(addr)
}
}
pub trait Udp {
fn recv_from(&self, buf: &mut [u8]) -> impl Future<Output = Result<(usize, PeerAddr)>>;
fn send_to(&self, buf: &[u8], addr: PeerAddr) -> impl Future<Output = Result<()>>;
fn join_multicast(&self, _group: [u8; 16], _scope_id: u32) -> Result<()> {
Err(crate::error::Error::new(crate::error::ErrorCode::Platform))
}
fn leave_multicast(&self, _group: [u8; 16], _scope_id: u32) -> Result<()> {
Err(crate::error::Error::new(crate::error::ErrorCode::Platform))
}
}
pub trait KvStore {
fn get(&self, key: &str, out: &mut [u8]) -> impl Future<Output = Result<Option<usize>>>;
fn set(&self, key: &str, value: &[u8]) -> impl Future<Output = Result<()>>;
fn remove(&self, key: &str) -> impl Future<Output = Result<()>>;
fn remove_prefix(&self, prefix: &str) -> impl Future<Output = Result<()>>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn link_local_detection() {
let mut a = [0u8; 16];
a[0] = 0xfe;
a[1] = 0x80;
assert!(PeerAddr::new(a).is_link_local());
a[1] = 0xc0;
assert!(!PeerAddr::new(a).is_link_local());
assert!(PeerAddr::new([0xff; 16]).is_multicast());
}
#[test]
fn default_port_is_5540() {
assert_eq!(PeerAddr::new([0; 16]).port, crate::PORT);
}
#[test]
fn a_node_without_a_clock_says_so() {
assert_eq!(NoClock.utc(), None);
assert_eq!(NoClock.granularity(), Granularity::NoTimeGranularity);
assert!(NoClock.set_utc(0).is_err());
}
}