git clone https://garage44.eu/jeroen/pinnacle.git.. /ui/src/phone.rs
//! Talking to the `phone` service: ordinary phone calls and texts (SMS)
//! over the device's modem, the way to reach people the p2p service
//! cannot. Messages are a kind byte, then the body; the service tells
//! every client what happens.
use alloc::string::String;
use alloc::vec::Vec;
use pinnacle_rt::Channel;
pub const SERVICE: &str = "phone";
// Client โ service.
/// Call a number (body: the number as written, UTF-8).
pub const DIAL: u8 = 1;
/// Answer the call ringing in.
pub const ANSWER: u8 = 2;
/// Hang up, decline, or stop dialling.
pub const HANG_UP: u8 = 3;
/// Ask for the state (answered with [`STATE_IS`]); a client that asks is
/// the one a waiting incoming call is told to, again.
pub const STATE: u8 = 4;
/// The SIM's PIN (body: its digits).
pub const UNLOCK: u8 = 5;
/// Send a text (body: the number as written, a newline, the text).
/// Answered by [`TEXT_SENT`] or [`TEXT_FAILED`], to every client.
pub const SEND_TEXT: u8 = 6;
/// The conversation with a number (body: the number): answered by
/// [`TEXTS_ARE`].
pub const TEXTS: u8 = 7;
/// The numbers texted with: answered by [`CONVERSATIONS_ARE`].
pub const CONVERSATIONS: u8 = 8;
/// Forget the conversation with a number on this device (body: the
/// number). Not answered.
pub const FORGET_TEXTS: u8 = 9;
/// Forget one text (body: its direction byte, its time, u64, as
/// [`TEXTS_ARE`] gives them, then the number). Not answered.
pub const FORGET_TEXT: u8 = 10;
// Service โ client.
/// The modem ([`Modem`] as a byte), then the call ([`Call`] as a byte) and
/// the other side's number, UTF-8 (empty when none or not known).
pub const STATE_IS: u8 = 0x80;
/// A call rings in (body: the caller's number, empty when withheld).
pub const INCOMING: u8 = 0x81;
/// The call is up.
pub const CONNECTED: u8 = 0x82;
/// The call ended ([`Ended`] as a byte).
pub const ENDED: u8 = 0x83;
/// A text came (body: the sender's number, a newline, the text).
pub const TEXT_RECEIVED: u8 = 0x84;
/// The network took a text (body: the number it went to).
pub const TEXT_SENT: u8 = 0x85;
/// A text did not go (body: the number).
pub const TEXT_FAILED: u8 = 0x86;
/// A conversation's newest texts that fit one message, oldest first:
/// records of direction (0 in, 1 out), time (u64 ms since 1970), length
/// (u16), text โ as the p2p service keeps its messages.
pub const TEXTS_ARE: u8 = 0x87;
/// The numbers texted with, one a line.
pub const CONVERSATIONS_ARE: u8 = 0x88;
/// Where the modem is.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Modem {
/// There is none, or it does not answer.
Absent = 0,
NoSim = 1,
/// The SIM wants its PIN.
Locked = 2,
/// Not on a network.
Searching = 3,
/// Calls can be made.
Ready = 4,
}
impl Modem {
pub fn from_byte(b: u8) -> Modem {
match b {
1 => Modem::NoSim,
2 => Modem::Locked,
3 => Modem::Searching,
4 => Modem::Ready,
_ => Modem::Absent,
}
}
}
/// Where the call is.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Call {
Idle = 0,
Dialling = 1,
Ringing = 2,
Active = 3,
}
impl Call {
pub fn from_byte(b: u8) -> Call {
match b {
1 => Call::Dialling,
2 => Call::Ringing,
3 => Call::Active,
_ => Call::Idle,
}
}
}
/// How a call ended, as the modem driver says.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Ended {
HungUp = 0,
Busy = 1,
NoAnswer = 2,
Missed = 3,
Failed = 4,
}
impl Ended {
pub fn from_byte(b: u8) -> Ended {
match b {
1 => Ended::Busy,
2 => Ended::NoAnswer,
3 => Ended::Missed,
4 => Ended::Failed,
_ => Ended::HungUp,
}
}
/// What a person is told.
pub fn text(self) -> &'static str {
match self {
Ended::HungUp => "Call ended",
Ended::Busy => "Busy",
Ended::NoAnswer => "No answer",
Ended::Missed => "Missed call",
Ended::Failed => "The call did not go through",
}
}
}
pub fn message(kind: u8, body: &[u8]) -> Vec<u8> {
let mut m = alloc::vec![kind];
m.extend_from_slice(body);
m
}
/// What the service says, as a client reads it.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Said {
State {
modem: Modem,
call: Call,
number: String,
},
Incoming {
number: String,
},
Connected,
Ended(Ended),
/// A text came.
TextReceived {
number: String,
text: String,
},
/// The network took the text to `number`.
TextSent {
number: String,
},
/// The text to `number` did not go.
TextFailed {
number: String,
},
}
pub fn parse(m: &[u8]) -> Option<Said> {
let text = |b: &[u8]| String::from_utf8_lossy(b).into_owned();
Some(match *m.first()? {
STATE_IS => Said::State {
modem: Modem::from_byte(*m.get(1)?),
call: Call::from_byte(*m.get(2)?),
number: text(&m[3..]),
},
INCOMING => Said::Incoming {
number: text(&m[1..]),
},
CONNECTED => Said::Connected,
ENDED => Said::Ended(Ended::from_byte(*m.get(1)?)),
TEXT_RECEIVED => {
let body = text(&m[1..]);
let (number, text) = body.split_once('\n')?;
Said::TextReceived {
number: number.into(),
text: text.into(),
}
}
TEXT_SENT => Said::TextSent {
number: text(&m[1..]),
},
TEXT_FAILED => Said::TextFailed {
number: text(&m[1..]),
},
_ => return None,
})
}
/// An app's connection to the phone service.
pub struct Client {
channel: Channel,
}
impl Client {
/// Connects, if the service runs.
pub fn connect() -> Option<Client> {
let channel = pinnacle_rt::connect(SERVICE)?;
channel.send(&[STATE], Vec::new());
Some(Client { channel })
}
pub fn channel(&self) -> &Channel {
&self.channel
}
pub fn dial(&self, number: &str) {
self.channel.send(&message(DIAL, number.as_bytes()), Vec::new());
}
pub fn answer(&self) {
self.channel.send(&[ANSWER], Vec::new());
}
pub fn hang_up(&self) {
self.channel.send(&[HANG_UP], Vec::new());
}
pub fn unlock(&self, pin: &str) {
self.channel.send(&message(UNLOCK, pin.as_bytes()), Vec::new());
}
/// Sends `text` to `number`; [`Said::TextSent`] or
/// [`Said::TextFailed`] follows.
pub fn send_text(&self, number: &str, text: &str) {
let body = alloc::format!("{number}\n{text}");
self.channel
.send(&message(SEND_TEXT, body.as_bytes()), Vec::new());
}
/// The conversation with `number`, as [`TEXTS_ARE`] has it (without
/// its kind byte). What the service says meanwhile goes in `pending`.
pub fn texts(&self, number: &str, pending: &mut Vec<Vec<u8>>) -> Vec<u8> {
self.ask(&message(TEXTS, number.as_bytes()), TEXTS_ARE, pending)
}
/// Forgets the conversation with `number` on this device, or, given
/// one (whether it was sent, and its time), only that text.
pub fn forget(&self, number: &str, text: Option<(bool, u64)>) {
let m = match text {
None => message(FORGET_TEXTS, number.as_bytes()),
Some((outgoing, at)) => {
let mut body = alloc::vec![u8::from(outgoing)];
body.extend_from_slice(&at.to_le_bytes());
body.extend_from_slice(number.as_bytes());
message(FORGET_TEXT, &body)
}
};
self.channel.send(&m, Vec::new());
}
/// The numbers texted with.
pub fn conversations(&self, pending: &mut Vec<Vec<u8>>) -> Vec<String> {
let answer = self.ask(&[CONVERSATIONS], CONVERSATIONS_ARE, pending);
String::from_utf8_lossy(&answer)
.lines()
.filter(|l| !l.is_empty())
.map(String::from)
.collect()
}
fn ask(&self, request: &[u8], answer: u8, pending: &mut Vec<Vec<u8>>) -> Vec<u8> {
self.channel.send(request, Vec::new());
let mut buffer = alloc::vec![0u8; pinnacle_rt::abi::MAX_MESSAGE];
loop {
match self.channel.receive(&mut buffer) {
pinnacle_rt::Received::Message { len, .. } if len >= 1 && buffer[0] == answer => {
return buffer[1..len].to_vec();
}
pinnacle_rt::Received::Message { len, .. } => pending.push(buffer[..len].to_vec()),
_ => return Vec::new(),
}
}
}
}