[domistream] Control API: AES-GCM frame, control messages, control stream

This commit is contained in:
2026-11-25 11:47:25 +01:00
parent 213f87b871
commit c01f980292
4 changed files with 441 additions and 0 deletions
+212
View File
@@ -0,0 +1,212 @@
//! Control messages inside the encrypted frame (`docs/PROTOCOL.md` 3.3): `type u16 LE`, `length u16 LE`, body.
use crate::Result;
use crate::input::InputEvent;
/// Length of the message header (type + length).
pub const MESSAGE_HEADER_LEN: usize = 4;
/// Maximum size of one decrypted message, in bytes.
pub const MAX_MESSAGE_BYTES: usize = super::frame::MAX_FRAME_BYTES - super::frame::FRAME_HEADER_LEN;
/// Length of the HDR metadata block in `HDR_MODE` (15 × u16).
pub const HDR_METADATA_LEN: usize = 30;
/// Known message type codes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u16)]
pub enum MessageType {
/// Start, part A (client → host).
StartA = 0x0305,
/// Start, part B (client → host); the host starts video and audio on it.
StartB = 0x0307,
/// Reference frame invalidation request (client → host).
InvalidateRefFrames = 0x0301,
/// IDR frame request (client → host).
IdrFrame = 0x0302,
/// Loss statistics (client → host).
LossStats = 0x0201,
/// Frame statistics (client → host).
FrameStats = 0x0204,
/// Input (client → host).
InputData = 0x0206,
/// Keep-alive (client → host).
PeriodicPing = 0x0200,
/// Termination (host → client).
Termination = 0x0100,
/// Termination with a 32-bit error code (host → client).
TerminationExtended = 0x0109,
/// HDR mode change (host → client).
HdrMode = 0x010e,
/// Rumble (host → client).
RumbleData = 0x010b,
/// Trigger rumble (host → client).
RumbleTriggers = 0x5500,
/// Start or stop motion events (host → client).
MotionEvent = 0x5501,
/// Controller LED colour (host → client).
RgbLed = 0x5502,
/// Adaptive trigger effect (host → client), layout UNKNOWN.
TriggerEffect = 0x5503,
}
impl TryFrom<u16> for MessageType {
type Error = crate::Error;
/// # Errors
/// [`crate::Error::UnexpectedValue`] for an unknown code. Phase A: always [`crate::Error::Unimplemented`].
fn try_from(code: u16) -> Result<Self> {
let _ = code;
Err(crate::Error::Unimplemented)
}
}
/// A message split into type and body, not interpreted.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RawMessage<'a> {
/// Type code (possibly unknown).
pub message_type: u16,
/// Body, exactly `length` bytes.
pub body: &'a [u8],
}
/// Splits a decrypted message. The length field must equal the bytes after the header.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_MESSAGE_BYTES`], [`crate::Error::Truncated`] under
/// [`MESSAGE_HEADER_LEN`], [`crate::Error::Malformed`] for a length that does not match. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn parse_message(message: &[u8]) -> Result<RawMessage<'_>> {
let _ = message;
Err(crate::Error::Unimplemented)
}
/// Motion sensor kinds of `MOTION_EVENT`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MotionKind {
/// Accelerometer (1).
Accelerometer,
/// Gyroscope (2).
Gyroscope,
}
/// HDR mastering metadata as sent in `HDR_MODE` (15 little-endian u16 fields, layout `[M*]`, unconfirmed).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct HdrMetadata {
/// Display primaries x/y for R, G, B.
pub display_primaries: [(u16, u16); 3],
/// White point x/y.
pub white_point: (u16, u16),
/// Maximum display luminance, nits.
pub max_display_luminance: u16,
/// Minimum display luminance, 1/10000 nit.
pub min_display_luminance: u16,
/// Maximum content light level, nits.
pub max_content_light_level: u16,
/// Maximum frame-average light level, nits.
pub max_frame_average_light_level: u16,
/// Maximum full-frame luminance, nits.
pub max_full_frame_luminance: u16,
}
/// A message from the host.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HostMessage {
/// Rumble a controller.
Rumble {
/// Controller number.
controller: u16,
/// Low-frequency motor.
low_frequency: u16,
/// High-frequency motor.
high_frequency: u16,
},
/// Rumble the triggers.
RumbleTriggers {
/// Controller number.
controller: u16,
/// Left trigger.
left: u16,
/// Right trigger.
right: u16,
},
/// Start (rate > 0) or stop (rate 0) sending motion events.
MotionEvent {
/// Controller number.
controller: u16,
/// Report rate, Hz.
rate_hz: u16,
/// Sensor.
kind: MotionKind,
},
/// Set a controller's LED colour.
RgbLed {
/// Controller number.
controller: u16,
/// Red.
r: u8,
/// Green.
g: u8,
/// Blue.
b: u8,
},
/// HDR turned on or off.
HdrMode {
/// HDR on.
enabled: bool,
/// Mastering metadata, if the body carries it.
metadata: Option<HdrMetadata>,
},
/// The host ended the session.
Termination {
/// Error code (extended form), `None` for the plain form. `0x80030023` = graceful.
error_code: Option<u32>,
},
/// A known or unknown type this client does not interpret; kept for stats and logs.
Other {
/// Type code.
message_type: u16,
/// Body length (the body itself is not kept).
body_len: usize,
},
}
/// Interprets a decrypted message from the host.
///
/// # Errors
/// As [`parse_message`]; [`crate::Error::Malformed`] when a known type's body has the wrong length;
/// [`crate::Error::UnexpectedValue`] for an unknown motion kind. Phase A: always [`crate::Error::Unimplemented`].
pub fn parse_host_message(message: &[u8]) -> Result<HostMessage> {
let _ = message;
Err(crate::Error::Unimplemented)
}
/// A message to the host.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClientMessage {
/// `START_A` (body UNKNOWN U10).
StartA,
/// `START_B` (body UNKNOWN U10): the host starts video and audio.
StartB,
/// Keep-alive (body UNKNOWN U10).
PeriodicPing,
/// Ask for an IDR frame (body UNKNOWN U10).
RequestIdrFrame,
/// Ask the host to stop referencing frames `first..=last`.
InvalidateRefFrames {
/// First lost frame index.
first: u32,
/// Last lost frame index.
last: u32,
},
/// User input.
Input(InputEvent),
}
/// Serializes a message to the host (type, length, body), ready for [`super::ControlCipher::seal`].
///
/// # Errors
/// [`crate::Error::Unsupported`] for a message whose body is still UNKNOWN; input errors as
/// [`crate::input::encode_input`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn encode_client_message(message: &ClientMessage) -> Result<Vec<u8>> {
let _ = message;
Err(crate::Error::Unimplemented)
}