pub struct ProtocolSession { /* private fields */ }Expand description
Stateful, transport-independent RODECaster protocol session.
Implementations§
Source§impl ProtocolSession
impl ProtocolSession
Sourcepub fn new() -> Self
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}Sourcepub fn is_ready(&self) -> bool
pub fn is_ready(&self) -> bool
Whether a valid full sync has established the current address layout.
Sourcepub fn capabilities(&self) -> Option<&DeviceCapabilities>
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}Sourcepub fn layout(&self) -> Option<&Layout>
pub fn layout(&self) -> Option<&Layout>
Current low-level address layout for advanced consumers.
Sourcepub fn ingest(&mut self, payload: &[u8]) -> Result<SessionUpdate, SessionError>
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}Trait Implementations§
Source§impl Debug for ProtocolSession
impl Debug for ProtocolSession
Auto Trait Implementations§
impl Freeze for ProtocolSession
impl RefUnwindSafe for ProtocolSession
impl Send for ProtocolSession
impl Sync for ProtocolSession
impl Unpin for ProtocolSession
impl UnsafeUnpin for ProtocolSession
impl UnwindSafe for ProtocolSession
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more