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