Skip to main content

ProtocolSession

Struct ProtocolSession 

Source
pub struct ProtocolSession { /* private fields */ }
Expand description

Stateful, transport-independent RODECaster protocol session.

Implementations§

Source§

impl ProtocolSession

Source

pub fn new() -> Self

Create a session waiting for its first full sync.

Examples found in repository?
examples/usb_monitor.rs (line 20)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    let path = env::args_os()
13        .nth(1)
14        .ok_or("usage: usb_monitor <path-to-hidraw>")?;
15    let file = OpenOptions::new().read(true).write(true).open(path)?;
16
17    // The device stays silent until a client requests its initial full sync.
18    (&file).write_all(&usb::Packet::new(usb::HANDSHAKE_BODY.to_vec()).to_bytes())?;
19
20    let mut session = ProtocolSession::new();
21    let mut buffered = Vec::new();
22    let mut report = [0_u8; usb::REPORT_SIZE];
23    loop {
24        let read = (&file).read(&mut report)?;
25        buffered.extend_from_slice(&report[..read]);
26
27        loop {
28            let len = match usb::scan_frame(&buffered) {
29                FrameScan::Complete { len } => len,
30                FrameScan::Incomplete => break,
31                FrameScan::Desync => {
32                    buffered.drain(..usb::REPORT_SIZE.min(buffered.len()));
33                    continue;
34                }
35            };
36            let (packet, consumed) = usb::Packet::from_bytes(&buffered[..len])
37                .ok_or("USB scanner returned an invalid packet")?;
38            buffered.drain(..consumed);
39
40            match session.ingest(&packet.payload)? {
41                SessionUpdate::Ready { initial_events } => {
42                    let capabilities = session.capabilities().unwrap();
43                    println!(
44                        "connected: {} fw {} ({} faders, {} sources, {} mixes)",
45                        capabilities.model(),
46                        capabilities.firmware().unwrap_or("unknown"),
47                        capabilities.faders().len(),
48                        capabilities.sources().len(),
49                        capabilities.mix_outputs().len(),
50                    );
51                    println!("initial state: {} typed events", initial_events.len());
52                }
53                SessionUpdate::Event(event) => println!("{event:?}"),
54                SessionUpdate::NeedsFullSync => {
55                    eprintln!("layout changed, requesting a fresh full sync");
56                    (&file)
57                        .write_all(&usb::Packet::new(usb::HANDSHAKE_BODY.to_vec()).to_bytes())?;
58                }
59            }
60        }
61    }
62}
Source

pub fn is_ready(&self) -> bool

Whether a valid full sync has established the current address layout.

Source

pub fn capabilities(&self) -> Option<&DeviceCapabilities>

Capabilities discovered from the most recent full sync.

Examples found in repository?
examples/usb_monitor.rs (line 42)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    let path = env::args_os()
13        .nth(1)
14        .ok_or("usage: usb_monitor <path-to-hidraw>")?;
15    let file = OpenOptions::new().read(true).write(true).open(path)?;
16
17    // The device stays silent until a client requests its initial full sync.
18    (&file).write_all(&usb::Packet::new(usb::HANDSHAKE_BODY.to_vec()).to_bytes())?;
19
20    let mut session = ProtocolSession::new();
21    let mut buffered = Vec::new();
22    let mut report = [0_u8; usb::REPORT_SIZE];
23    loop {
24        let read = (&file).read(&mut report)?;
25        buffered.extend_from_slice(&report[..read]);
26
27        loop {
28            let len = match usb::scan_frame(&buffered) {
29                FrameScan::Complete { len } => len,
30                FrameScan::Incomplete => break,
31                FrameScan::Desync => {
32                    buffered.drain(..usb::REPORT_SIZE.min(buffered.len()));
33                    continue;
34                }
35            };
36            let (packet, consumed) = usb::Packet::from_bytes(&buffered[..len])
37                .ok_or("USB scanner returned an invalid packet")?;
38            buffered.drain(..consumed);
39
40            match session.ingest(&packet.payload)? {
41                SessionUpdate::Ready { initial_events } => {
42                    let capabilities = session.capabilities().unwrap();
43                    println!(
44                        "connected: {} fw {} ({} faders, {} sources, {} mixes)",
45                        capabilities.model(),
46                        capabilities.firmware().unwrap_or("unknown"),
47                        capabilities.faders().len(),
48                        capabilities.sources().len(),
49                        capabilities.mix_outputs().len(),
50                    );
51                    println!("initial state: {} typed events", initial_events.len());
52                }
53                SessionUpdate::Event(event) => println!("{event:?}"),
54                SessionUpdate::NeedsFullSync => {
55                    eprintln!("layout changed, requesting a fresh full sync");
56                    (&file)
57                        .write_all(&usb::Packet::new(usb::HANDSHAKE_BODY.to_vec()).to_bytes())?;
58                }
59            }
60        }
61    }
62}
Source

pub fn layout(&self) -> Option<&Layout>

Current low-level address layout for advanced consumers.

Source

pub fn reset(&mut self)

Forget the current layout and return to the pre-full-sync state.

Source

pub fn ingest(&mut self, payload: &[u8]) -> Result<SessionUpdate, SessionError>

Ingest exactly one change-frame payload, without a TCP or USB wrapper.

Examples found in repository?
examples/usb_monitor.rs (line 40)
11fn main() -> Result<(), Box<dyn std::error::Error>> {
12    let path = env::args_os()
13        .nth(1)
14        .ok_or("usage: usb_monitor <path-to-hidraw>")?;
15    let file = OpenOptions::new().read(true).write(true).open(path)?;
16
17    // The device stays silent until a client requests its initial full sync.
18    (&file).write_all(&usb::Packet::new(usb::HANDSHAKE_BODY.to_vec()).to_bytes())?;
19
20    let mut session = ProtocolSession::new();
21    let mut buffered = Vec::new();
22    let mut report = [0_u8; usb::REPORT_SIZE];
23    loop {
24        let read = (&file).read(&mut report)?;
25        buffered.extend_from_slice(&report[..read]);
26
27        loop {
28            let len = match usb::scan_frame(&buffered) {
29                FrameScan::Complete { len } => len,
30                FrameScan::Incomplete => break,
31                FrameScan::Desync => {
32                    buffered.drain(..usb::REPORT_SIZE.min(buffered.len()));
33                    continue;
34                }
35            };
36            let (packet, consumed) = usb::Packet::from_bytes(&buffered[..len])
37                .ok_or("USB scanner returned an invalid packet")?;
38            buffered.drain(..consumed);
39
40            match session.ingest(&packet.payload)? {
41                SessionUpdate::Ready { initial_events } => {
42                    let capabilities = session.capabilities().unwrap();
43                    println!(
44                        "connected: {} fw {} ({} faders, {} sources, {} mixes)",
45                        capabilities.model(),
46                        capabilities.firmware().unwrap_or("unknown"),
47                        capabilities.faders().len(),
48                        capabilities.sources().len(),
49                        capabilities.mix_outputs().len(),
50                    );
51                    println!("initial state: {} typed events", initial_events.len());
52                }
53                SessionUpdate::Event(event) => println!("{event:?}"),
54                SessionUpdate::NeedsFullSync => {
55                    eprintln!("layout changed, requesting a fresh full sync");
56                    (&file)
57                        .write_all(&usb::Packet::new(usb::HANDSHAKE_BODY.to_vec()).to_bytes())?;
58                }
59            }
60        }
61    }
62}
Source

pub fn encode(&self, command: &Command) -> Result<Vec<Vec<u8>>, SessionError>

Encode a typed command using the layout from the most recent full sync.

Trait Implementations§

Source§

impl Debug for ProtocolSession

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ProtocolSession

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.