Skip to main content

dvb_ci_runtime/
lib.rs

1//! EN 50221 DVB Common Interface driver runtime: the device I/O, the TPDU poll
2//! loop, SPDU session management and the per-resource state machines that
3//! drive a physical Conditional Access Module (CAM), over the `dvb-ci` wire
4//! codecs. It implements CENELEC EN 50221 (February 1997): the Annex A
5//! transport and the host-provided Resource Manager, Application Information,
6//! Conditional Access Support, Date-Time, MMI and Host Control resources, plus
7//! the Linux DVB CA API for real hardware.
8//!
9//! It builds on `dvb-ci` (codecs), `dvb-si` (PMT and CAT inputs), `mpeg-ts`
10//! and `broadcast-common`. The core, [`CiStack`], is sans-IO (events in,
11//! actions out, the caller owns the clock); [`Driver`] runs it against a
12//! [`CaDevice`], and [`CaDescrambler`] adds the TS data plane. It serves no CI
13//! Plus and no Low-Speed Communications, never descrambles itself (the CAM
14//! does) and has no `no_std` claim. Every limit and timeout is the caller's.
15//!
16//! # Quickstart
17//!
18//! Bring a slot up against an in-memory CAM and drain the host-facing events:
19//!
20//! ```rust
21//! use std::time::Duration;
22//! use dvb_ci::tpdu::tags;
23//! use dvb_ci_runtime::{
24//!     Driver, DriverBounds, MockCaDevice, Notification, StackBounds, TransportBounds,
25//! };
26//!
27//! let dev = MockCaDevice::new([vec![tags::C_T_C_REPLY, 0x01, 0x01]]);
28//! let bounds = DriverBounds {
29//!     stack: StackBounds {
30//!         transport: TransportBounds {
31//!             poll_interval: Duration::from_millis(100),
32//!             reply_timeout: Duration::from_secs(1),
33//!             max_setup_attempts: 5,
34//!             max_reassembly_len: 2 * 1024 * 1024,
35//!             max_outbound_queue: 64,
36//!         },
37//!         max_sessions: 32,
38//!     },
39//!     max_pending_notifications: 1024,
40//!     slot_ready_grace: Duration::from_secs(12),
41//!     slot_retry: Duration::from_millis(250),
42//! };
43//! let mut driver = Driver::new(dev, bounds);
44//! driver.init()?;
45//! for _ in 0..4 {
46//!     driver.pump(Duration::from_millis(100))?;
47//! }
48//! for note in driver.take_notifications() {
49//!     if let Notification::CamReady = note {
50//!         // now safe to send a ca_pmt
51//!     }
52//! }
53//! # Ok::<(), std::io::Error>(())
54//! ```
55//!
56//! # How do I...
57//!
58//! - Drive a CAM from a loop: [`Driver`] (`init`, `pump`, `take_notifications`),
59//!   `examples/mock_cam_session.rs`.
60//! - Run the protocol with no device, thread or clock: [`CiStack`], [`Event`],
61//!   [`Action`], `examples/sans_io_core.rs`.
62//! - React to CAM insertion and removal: [`HotPlug`],
63//!   [`Driver::pump_hotplug`], `examples/hot_plug.rs`.
64//! - Capture and read an annotated link trace:
65//!   [`device::RecordingCaDevice`], [`trace::decode_log`],
66//!   `examples/trace_session.rs`.
67//! - Push scrambled TS through the CAM and read it back descrambled:
68//!   [`CaDescrambler`], [`MockCiDataDevice`], `examples/descrambler_feed.rs`.
69//! - Descramble a service from its PMT and track the active set:
70//!   [`Driver::add_service`], [`Driver::set_cat`], no example yet.
71//! - Answer a CAM menu or enquiry: [`Driver::mmi_menu_answer`],
72//!   [`Driver::mmi_enquiry_answer`], no example yet.
73//! - Test without hardware: [`MockCaDevice`], `examples/mock_cam_session.rs`.
74//! - Talk to a real slot on Linux: the `linux` module (`LinuxCaDevice`,
75//!   `LinuxCiDataDevice`) and the `ci-probe` binary, both behind feature
76//!   `linux`; no example yet.
77//!
78//! # Modules
79//!
80//! Host-facing driver: [`driver`] (the pump loop and the managed CAS calls),
81//! [`managed`] (the active-service set, PID routing and re-query),
82//! [`descrambler`] (turnkey control plus data plane), [`event`] (events,
83//! actions, host requests, notifications, hot-plug).
84//!
85//! Protocol core: [`stack`] (the composed sans-IO stack), [`transport`] (TPDU
86//! poll loop), [`session`] (SPDU sessions), [`resource`] (the resource state
87//! machines).
88//!
89//! Devices and diagnostics: [`device`] (the control-plane trait, mock and
90//! recorder), [`dataplane`] (the `ciM` TS data-plane trait and mock),
91//! [`trace`] (annotated link traces); the `linux` module (feature `linux`,
92//! Linux only: the `/dev/dvb` devices).
93//!
94//! # Feature flags
95//!
96//! - `linux` (off by default): `LinuxCaDevice`, `LinuxCiDataDevice` and the
97//!   `ci-probe` binary (pulls `rustix` and `clap`). Linux only; the portable
98//!   core needs none of it.
99//!
100//! # Spec citations
101//!
102//! CENELEC EN 50221 Annex A section A.4 (TPDU transport), section 7.2 (SPDU
103//! session layer), section 8 (resources), section 8.4.3.4 and Table 25
104//! (`ca_pmt`); ISO/IEC 13818-1 section 2.6.16 (`CA_descriptor`); the Linux
105//! `linux/dvb/ca.h` ABI for the device layer; transcriptions under
106//! `private/specs-verified/dvb/dvb-ci/`.
107// The portable core is unsafe-free; only the optional Linux device leaf
108// (`#[allow(unsafe_code)]` in `linux`) uses ioctls, so this is `deny`, not
109// `forbid`.
110#![deny(unsafe_code)]
111#![deny(missing_docs)]
112#![cfg_attr(docsrs, feature(doc_cfg))]
113
114#[cfg(doctest)]
115#[doc = include_str!("../README.md")]
116struct ReadmeDoctests;
117
118pub mod dataplane;
119pub mod descrambler;
120pub mod device;
121pub mod driver;
122pub mod event;
123pub mod managed;
124pub mod resource;
125pub mod session;
126pub mod stack;
127pub mod trace;
128pub mod transport;
129
130#[cfg(all(feature = "linux", target_os = "linux"))]
131pub mod linux;
132
133pub use dataplane::{CiDataDevice, MockCiDataDevice, PartialWrite, partial_write_progress};
134pub use descrambler::CaDescrambler;
135pub use device::{
136    CaDevice, DeviceOp, DeviceOpError, LinkEvent, MAX_CA_FRAME, MockCaDevice, RecordingCaDevice,
137    SlotInfo,
138};
139pub use driver::{Driver, DriverBounds};
140pub use event::{Action, Event, HostRequest, HotPlug, Notification};
141#[cfg(all(feature = "linux", target_os = "linux"))]
142pub use linux::{CiDataTimings, LinuxCaDevice, LinuxCiDataDevice};
143pub use managed::{CaError, ManagedCa, ManagedService, REQUERY_DEFAULT};
144pub use stack::{CiStack, StackBounds};
145#[cfg(test)]
146mod test_support;
147pub use transport::TransportBounds;