OXYGENHUB
# 8de06aa06268 oxygenhub-android / rust-core / src / lib.rs
4.1 KiB · Rust · last changed The phone runner says what it runs, on its card and notification (refs #8); pins OxygenFlow main 7 days ago source blame history raw
1
uniffi::setup_scaffolding!();
2
3
mod backdrop;
4
pub mod scenes;
5
6
use std::path::Path;
7
8
// A simple test function to ensure the Kotlin <-> Rust bridge is working.
9
#[uniffi::export]
10
pub fn oxygen_greeting(name: String) -> String {
11
    format!("Hello, {}! Oxygen Native Core is active.", name)
12
}
13
14
/// Why a runner call failed, in OxygenFlow's words.
15
#[derive(Debug, uniffi::Error)]
16
pub enum RunnerError {
17
    Failed { reason: String },
18
}
19
20
impl std::fmt::Display for RunnerError {
21
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22
        match self {
23
            RunnerError::Failed { reason } => f.write_str(reason),
24
        }
25
    }
26
}
27
28
impl std::error::Error for RunnerError {}
29
30
fn failed(e: anyhow::Error) -> RunnerError {
31
    RunnerError::Failed { reason: format!("{e:#}") }
32
}
33
34
/// The phone as an OxygenFlow runner (oxygenhub-android#8): OxygenFlow's
35
/// own runner, built into the app, offering only the WASM tier. `home` is
36
/// the app's directory for it; `register` keeps the runner's credential
37
/// there (runner.toml), as `flow runner register` does on a host.
38
#[uniffi::export]
39
pub fn runner_register(home: String, hub: String, token: String, name: String) -> Result<String, RunnerError> {
40
    let h = flow::home::Home::locate(Some(Path::new(&home))).map_err(failed)?;
41
    let r = flow::runner::register(&h, &hub, &token, &name, None, vec!["android".into()]).map_err(failed)?;
42
    Ok(r.name)
43
}
44
45
/// Whether `home` holds a registration, and so can serve.
46
#[uniffi::export]
47
pub fn runner_is_registered(home: String) -> bool {
48
    Path::new(&home).join(flow::home::RUNNER_TOML).is_file()
49
}
50
51
/// Claim and run jobs until [`runner_stop`] drains the runner: returns
52
/// once the jobs it holds are done and reported. Blocks, so call it from
53
/// a thread of its own (the app's runner service).
54
#[uniffi::export]
55
pub fn runner_serve(home: String) -> Result<(), RunnerError> {
56
    let h = flow::home::Home::locate(Some(Path::new(&home))).map_err(failed)?;
57
    flow::runner::serve_embedded(&h).map_err(failed)
58
}
59
60
/// Drain the runner: claim nothing more, finish and report what it
61
/// holds, then [`runner_serve`] returns. For leaving the charger or the
62
/// allowed network, or the owner stopping it.
63
#[uniffi::export]
64
pub fn runner_stop() {
65
    flow::runner::stop();
66
}
67
68
/// Leave (oxygenflow#76): claim nothing more, and hand the jobs it holds
69
/// back to the hub, which gives them to another runner ahead of new work;
70
/// [`runner_serve`] then returns. With `notice_secs`, a job the hub expects
71
/// to finish within the notice finishes first (its checkpoint saved if
72
/// not). 0 for leaving the allowed network, where finishing would use the
73
/// network the owner ruled out; a minute for leaving the charger.
74
#[uniffi::export]
75
pub fn runner_leave(reason: String, notice_secs: u64) {
76
    flow::runner::leave_within(&reason, std::time::Duration::from_secs(notice_secs));
77
}
78
79
/// The tiers this phone offers (the WASM tier), and why not the others.
80
/// What the runner is running now: "owner/name #run job" for each job (#8).
81
#[uniffi::export]
82
pub fn runner_running() -> Vec<String> {
83
    flow::runner::running()
84
}
85
86
#[uniffi::export]
87
pub fn runner_tiers(home: String) -> Vec<String> {
88
    let Ok(h) = flow::home::Home::locate(Some(Path::new(&home))) else { return vec![] };
89
    match flow::runner::tiers(&h) {
90
        Ok((have, _)) => have,
91
        Err(_) => vec![],
92
    }
93
}
94
95
/// A new X25519 secret for this phone's alerts (#16), to keep wrapped by
96
/// the Android Keystore.
97
#[uniffi::export]
98
pub fn alert_new_secret() -> Vec<u8> {
99
    flow_core::alert::new_secret().to_vec()
100
}
101
102
/// The public key of an alert secret, for the hub; empty for a secret that
103
/// is not 32 bytes.
104
#[uniffi::export]
105
pub fn alert_public_key(secret: Vec<u8>) -> Vec<u8> {
106
    <[u8; 32]>::try_from(secret.as_slice()).map(|s| flow_core::alert::public_key(&s).to_vec()).unwrap_or_default()
107
}
108
109
/// Open an alert the hub sealed to this phone; `None` when it was not
110
/// sealed to `secret` or was changed on the way.
111
#[uniffi::export]
112
pub fn alert_open(secret: Vec<u8>, sealed: Vec<u8>) -> Option<Vec<u8>> {
113
    let s = <[u8; 32]>::try_from(secret.as_slice()).ok()?;
114
    flow_core::alert::open(&s, &sealed)
115
}