node_device_session/traits.rs
1//! C-5 transport and host-bus traits.
2
3use crate::messages::OpenOk;
4use crate::types::{PairingAdmission, Refusal, Session};
5use async_trait::async_trait;
6use std::io;
7use tokio::io::{AsyncRead, AsyncWrite};
8
9/// Boxed write half of one bidirectional stream.
10pub type BoxSend = Box<dyn AsyncWrite + Send + Unpin>;
11/// Boxed read half of one bidirectional stream.
12pub type BoxRecv = Box<dyn AsyncRead + Send + Unpin>;
13
14/// One authenticated peer connection (handshake already proved [`Self::peer_key`]).
15#[async_trait]
16pub trait SessionStreams: Send + Sync {
17 /// Ed25519 public key proven by the transport handshake.
18 fn peer_key(&self) -> [u8; 32];
19
20 /// Accept the next bidirectional stream.
21 async fn accept_bi(&self) -> io::Result<(BoxSend, BoxRecv)>;
22
23 /// Close the connection with an application code and reason.
24 fn close(&self, code: u32, reason: &[u8]);
25
26 /// Whether the peer has already closed.
27 fn is_closed(&self) -> bool {
28 false
29 }
30}
31
32/// The app's bus to the host for device-session capabilities.
33#[async_trait]
34pub trait HostBus: Send + Sync {
35 /// `core.device_session.verify` — full open with a credential.
36 async fn verify(&self, credential: &str, endpoint_id: &str) -> Result<Session, Refusal>;
37
38 /// `core.device_session.renew` — one-shot credential renewal, then close.
39 async fn renew(&self, endpoint_id: &str) -> Result<Session, Refusal>;
40
41 /// `core.device_session.admit_renewal` — open a renewal-only session.
42 async fn admit_renewal(&self, endpoint_id: &str) -> Result<Session, Refusal>;
43
44 /// `core.device_session.admit_pairing` — open a pairing-only session with a
45 /// one-time door token. The token is a secret: never log it.
46 ///
47 /// The default refuses with `not_open`, so carriers written before
48 /// download-door pairing compile unchanged and answer like an older node.
49 async fn admit_pairing(
50 &self,
51 token: &str,
52 endpoint_id: &str,
53 ) -> Result<PairingAdmission, Refusal> {
54 let _ = (token, endpoint_id);
55 Err(Refusal::new(
56 "not_open",
57 "pairing sessions are not supported",
58 ))
59 }
60
61 /// Generic capability invoke for `device.call/1` (stamped payload already applied).
62 async fn invoke(
63 &self,
64 capability: &str,
65 payload: serde_json::Value,
66 ) -> Result<serde_json::Value, Refusal>;
67}
68
69/// Carrier-owned WebRTC signaling for `webrtc.signal/1`.
70///
71/// Prefer the shared [`crate::webrtc::WebRtcAnswerer`] (parameterised by
72/// [`crate::webrtc::WebRtcCarrier`]) unless the carrier needs a bespoke path.
73/// When [`None`] is passed to [`crate::serve`], the method is refused with
74/// `unsupported_transport`.
75#[async_trait]
76pub trait WebRtcSignaling: Send + Sync {
77 /// Handle one `webrtc.signal/1` stream on an opened session.
78 ///
79 /// `send`/`recv` already peeked the method prefix; the body (SDP / ICE
80 /// frames) starts at the next byte. Own the halves until the signaling
81 /// session ends.
82 async fn handle(&self, identity: &OpenOk, peer: [u8; 32], send: BoxSend, recv: BoxRecv);
83}