Skip to main content

mock_cam_session/
mock_cam_session.rs

1//! Drive a CAM through the `Driver` against an in-memory `MockCaDevice`.
2//!
3//! Shows the host-facing loop: `init` → `pump` (poll cadence) →
4//! `take_notifications`. The same `Driver` API runs against the real Linux
5//! `/dev/dvb/adapterN/caM` device (the `linux` feature) — only the `CaDevice`
6//! changes.
7//!
8//! Run: `cargo run -p dvb-ci-runtime --example mock_cam_session`
9
10use std::time::Duration;
11
12use dvb_ci::tpdu::tags;
13use dvb_ci_runtime::{
14    Driver, DriverBounds, MockCaDevice, Notification, StackBounds, TransportBounds,
15};
16
17fn driver_bounds() -> DriverBounds {
18    DriverBounds {
19        stack: stack_bounds(),
20        max_pending_notifications: 1024,
21        slot_ready_grace: Duration::from_secs(12),
22        slot_retry: Duration::from_millis(250),
23    }
24}
25
26fn stack_bounds() -> StackBounds {
27    StackBounds {
28        transport: TransportBounds {
29            poll_interval: Duration::from_millis(100),
30            reply_timeout: Duration::from_secs(1),
31            max_setup_attempts: 5,
32            max_reassembly_len: 2 * 1024 * 1024,
33            max_outbound_queue: 64,
34        },
35        max_sessions: 32,
36    }
37}
38
39fn main() -> std::io::Result<()> {
40    // Script a module that accepts the transport connection (C_T_C_Reply).
41    let dev = MockCaDevice::new([vec![tags::C_T_C_REPLY, 0x01, 0x01]]);
42    let mut driver = Driver::new(dev, driver_bounds());
43
44    // Bring the interface up: reset + open the transport connection.
45    driver.init()?;
46    println!("init: sent {} device op(s)", driver.device().ops.len());
47
48    // Pump the device. When readable it reads a frame and feeds the stack;
49    // otherwise it advances the EN 50221 poll cadence by the timeout.
50    for step in 0..5 {
51        let read = driver.pump(Duration::from_millis(100))?;
52        println!("pump {step}: processed_frame={read}");
53    }
54
55    // Anything the host application needs to act on surfaces as a Notification.
56    for note in driver.take_notifications() {
57        match note {
58            Notification::CamReady => println!("note: CAM ready — safe to send ca_pmt"),
59            Notification::ApplicationInfo { menu, .. } => {
60                println!("note: application_information menu={menu:?}")
61            }
62            Notification::CaInfo { ca_system_ids } => {
63                println!("note: ca_info system_ids={ca_system_ids:?}")
64            }
65            other => println!("note: {other:?}"),
66        }
67    }
68
69    // The mock records every device op (writes/ioctls) — handy for assertions.
70    println!("total recorded device ops: {}", driver.device().ops.len());
71    Ok(())
72}