//! The agent's questions to the person, which the display server draws //! over everything as it draws the lock screen (`docs/agent-environment.md`): //! no app sees or can fake them, and only a hand on the device answers. //! //! A connection to [`SERVICE`] takes questions one after another, each one //! message ([`Question::encode`]), and answers each [`YES`] or [`NO`] in //! order. The terminal connects for the agent's gatekeeper, which is sealed //! and cannot connect itself. use alloc::string::String; use alloc::vec::Vec; use pinnacle_rt::{Channel, Received}; /// `system-…`: no installed app can ask. pub const SERVICE: &str = "system-confirm"; pub const YES: &[u8] = b"yes"; pub const NO: &[u8] = b"no"; #[derive(Clone, Debug, PartialEq, Eq)] pub struct Question { /// The app the agent acts in, by program name. pub app: String, /// What it would do, as a question: about an effect, not a string. pub question: String, /// What the person may want to know first: where what it would send /// came from, what it did so far. pub detail: String, } impl Question { /// A line each; the detail may have more. pub fn encode(&self) -> Vec { let clip = |s: &str, n: usize| -> String { s.replace('\n', " ").chars().take(n).collect() }; let text = alloc::format!( "{}\n{}\n{}", clip(&self.app, 40), clip(&self.question, 400), self.detail.chars().take(2000).collect::() ); text.into_bytes() } pub fn decode(message: &[u8]) -> Option { let text = core::str::from_utf8(message).ok()?; let mut lines = text.splitn(3, '\n'); Some(Question { app: lines.next()?.into(), question: lines.next()?.into(), detail: lines.next().unwrap_or("").into(), }) } } /// Asks the person on `channel` and waits for the answer: false for a no, /// or when the display server is gone. pub fn ask(channel: &Channel, question: &Question) -> bool { if !channel.send(&question.encode(), Vec::new()) { return false; } let mut buffer = [0u8; 16]; match channel.receive(&mut buffer) { Received::Message { len, .. } => &buffer[..len] == YES, _ => false, } }