offline_protocol_router/relay.rs
1//! Relay management for promotion and demotion logic.
2
3use serde::{Deserialize, Serialize};
4
5/// Battery level below which a device stops doing anything for other people,
6/// however willing its configuration is.
7///
8/// This is the hard floor beneath the soft [`RelayConfig::min_battery_for_relay`]:
9/// a charging device, or one configured [`RelayPriority::Always`], is excused
10/// the soft minimum but never this. It is public because the protocol crate
11/// applies the same floor to message forwarding — a device must not keep
12/// carrying traffic at a level that would have stripped it of the relay role,
13/// and two copies of the number would eventually disagree.
14pub const CRITICAL_RELAY_BATTERY_LEVEL: u8 = 15;
15
16/// Configuration for relay behavior.
17#[derive(Debug, Clone)]
18pub struct RelayConfig {
19 /// Minimum battery level to act as relay (percentage).
20 pub min_battery_for_relay: u8,
21
22 /// Whether this device allows acting as a relay.
23 pub allow_relay: bool,
24
25 /// Priority for relay selection (higher = more preferred).
26 pub relay_priority: RelayPriority,
27}
28
29impl Default for RelayConfig {
30 fn default() -> Self {
31 Self {
32 min_battery_for_relay: 30,
33 allow_relay: true,
34 relay_priority: RelayPriority::Auto,
35 }
36 }
37}
38
39/// Relay priority levels.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41#[serde(rename_all = "lowercase")]
42pub enum RelayPriority {
43 /// Never act as relay.
44 Never,
45 /// Automatically decide based on conditions.
46 Auto,
47 /// Always try to act as relay (if conditions met).
48 Always,
49}
50
51/// Relay role of a device.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
53#[serde(rename_all = "lowercase")]
54pub enum RelayRole {
55 /// Regular device (not a relay).
56 Regular,
57 /// Acting as a relay.
58 Relay,
59}