Skip to main content

capsule_sdk/ffi/
path.rs

1//! The path snapshot across the FFI boundary: the flattened mirror of
2//! [`PathSample`](crate::net::path::PathSample) a platform shell reports, and the link signal a
3//! finished push feeds the adverse detector.
4//!
5//! The app reports what its OS listeners see (`NWPathMonitor`, `ConnectivityManager`, the
6//! thermal and power notifications) through
7//! [`FfiSession::set_path`](crate::ffi::FfiSession::set_path). The SDK stamps the generation —
8//! the app never counts paths — and every push the session drives plans from the result.
9
10use std::time::Duration;
11
12use crate::net::TransferSignal;
13use crate::net::path::{
14    Execution, PathCost, PathInterface, PathSample, PowerState, Reachability, ThermalState,
15};
16use crate::push::{AssetPushReport, PushError};
17use crate::upload::UploadError;
18
19/// FFI mirror of [`PathCost`].
20#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
21pub enum FfiPathCost {
22    /// Bulk transfer is acceptable.
23    Unmetered,
24    /// The link is byte-counted (iOS `isExpensive`, Android without `NOT_METERED`).
25    Metered,
26    /// The OS data saver is on (iOS Low Data Mode, Android Data Saver).
27    Constrained,
28}
29
30/// FFI mirror of [`PathInterface`].
31#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
32pub enum FfiPathInterface {
33    /// Ethernet or another wired link.
34    Wired,
35    /// Wi-Fi.
36    Wifi,
37    /// A cellular radio.
38    Cellular,
39    /// Anything else, or unknown.
40    Other,
41}
42
43/// FFI mirror of [`PowerState`].
44#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
45pub enum FfiPowerState {
46    /// On external power.
47    External,
48    /// On battery.
49    Battery,
50    /// On battery with the OS low-power mode on.
51    LowPower,
52}
53
54/// FFI mirror of [`ThermalState`].
55#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
56pub enum FfiThermalState {
57    /// No thermal pressure.
58    Nominal,
59    /// Slightly elevated.
60    Fair,
61    /// High: the OS asks apps to reduce work.
62    Serious,
63    /// Critical: the OS asks apps to stop discretionary work.
64    Critical,
65}
66
67/// One sample of the path as the app's OS listeners report it.
68///
69/// Flat, like [`FfiNotifyInput`](crate::ffi::FfiNotifyInput): `background_deadline_ms` present
70/// means the app runs in a background task the OS ends that many milliseconds from now; absent
71/// means the foreground.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)]
73pub struct FfiPathSnapshot {
74    /// What the path costs.
75    pub cost: FfiPathCost,
76    /// Whether the OS reports a usable path.
77    pub reachable: bool,
78    /// The interface the path runs over.
79    pub interface: FfiPathInterface,
80    /// The device's power source.
81    pub power: FfiPowerState,
82    /// The device's thermal state.
83    pub thermal: FfiThermalState,
84    /// Milliseconds before the OS ends the background task the app runs in; `None` in the
85    /// foreground.
86    pub background_deadline_ms: Option<u64>,
87}
88
89impl From<FfiPathSnapshot> for PathSample {
90    fn from(snapshot: FfiPathSnapshot) -> Self {
91        Self {
92            cost: match snapshot.cost {
93                FfiPathCost::Unmetered => PathCost::Unmetered,
94                FfiPathCost::Metered => PathCost::Metered,
95                FfiPathCost::Constrained => PathCost::Constrained,
96            },
97            reachability: if snapshot.reachable {
98                Reachability::Reachable
99            } else {
100                Reachability::Unreachable
101            },
102            interface: match snapshot.interface {
103                FfiPathInterface::Wired => PathInterface::Wired,
104                FfiPathInterface::Wifi => PathInterface::Wifi,
105                FfiPathInterface::Cellular => PathInterface::Cellular,
106                FfiPathInterface::Other => PathInterface::Other,
107            },
108            power: match snapshot.power {
109                FfiPowerState::External => PowerState::External,
110                FfiPowerState::Battery => PowerState::Battery,
111                FfiPowerState::LowPower => PowerState::LowPower,
112            },
113            thermal: match snapshot.thermal {
114                FfiThermalState::Nominal => ThermalState::Nominal,
115                FfiThermalState::Fair => ThermalState::Fair,
116                FfiThermalState::Serious => ThermalState::Serious,
117                FfiThermalState::Critical => ThermalState::Critical,
118            },
119            execution: match snapshot.background_deadline_ms {
120                None => Execution::Foreground,
121                Some(ms) => Execution::Background {
122                    deadline: Duration::from_millis(ms),
123                },
124            },
125        }
126    }
127}
128
129/// What a finished push says about the link, for the adverse detector: a push that moved bytes
130/// is a clean transfer, one that gave up on the transport a reset, and one that exhausted its
131/// recovery budget a stall. A push that moved nothing (every blob held or deferred), a
132/// refusal, a server that kept answering "wait" or was still finalizing at the poll deadline,
133/// an auth failure or a local error says nothing about the link, so it records nothing.
134pub(crate) fn link_signal(pushed: &Result<AssetPushReport, PushError>) -> Option<TransferSignal> {
135    match pushed {
136        Ok(report) if report.is_no_op() => None,
137        Ok(_) => Some(TransferSignal::Clean),
138        Err(PushError::Upload(UploadError::Transport(_))) => Some(TransferSignal::Reset),
139        Err(PushError::Upload(UploadError::RetriesExhausted(_))) => Some(TransferSignal::Stall),
140        Err(_) => None,
141    }
142}
143
144#[cfg(test)]
145mod tests;