dvb-ci-runtime 0.23.0

Pure-Rust EN 50221 DVB Common Interface driver runtime — device I/O, TPDU/SPDU poll loop, resource state machines, and CAM/card hot-plug notifications over the dvb-ci codecs.
Documentation
//! EN 50221 DVB Common Interface driver runtime: the device I/O, the TPDU poll
//! loop, SPDU session management and the per-resource state machines that
//! drive a physical Conditional Access Module (CAM), over the `dvb-ci` wire
//! codecs. It implements CENELEC EN 50221 (February 1997): the Annex A
//! transport and the host-provided Resource Manager, Application Information,
//! Conditional Access Support, Date-Time, MMI and Host Control resources, plus
//! the Linux DVB CA API for real hardware.
//!
//! It builds on `dvb-ci` (codecs), `dvb-si` (PMT and CAT inputs), `mpeg-ts`
//! and `broadcast-common`. The core, [`CiStack`], is sans-IO (events in,
//! actions out, the caller owns the clock); [`Driver`] runs it against a
//! [`CaDevice`], and [`CaDescrambler`] adds the TS data plane. It serves no CI
//! Plus and no Low-Speed Communications, never descrambles itself (the CAM
//! does) and has no `no_std` claim. Every limit and timeout is the caller's.
//!
//! # Quickstart
//!
//! Bring a slot up against an in-memory CAM and drain the host-facing events:
//!
//! ```rust
//! use std::time::Duration;
//! use dvb_ci::tpdu::tags;
//! use dvb_ci_runtime::{
//!     Driver, DriverBounds, MockCaDevice, Notification, StackBounds, TransportBounds,
//! };
//!
//! let dev = MockCaDevice::new([vec![tags::C_T_C_REPLY, 0x01, 0x01]]);
//! let bounds = DriverBounds {
//!     stack: StackBounds {
//!         transport: TransportBounds {
//!             poll_interval: Duration::from_millis(100),
//!             reply_timeout: Duration::from_secs(1),
//!             max_setup_attempts: 5,
//!             max_reassembly_len: 2 * 1024 * 1024,
//!             max_outbound_queue: 64,
//!         },
//!         max_sessions: 32,
//!     },
//!     max_pending_notifications: 1024,
//!     slot_ready_grace: Duration::from_secs(12),
//!     slot_retry: Duration::from_millis(250),
//! };
//! let mut driver = Driver::new(dev, bounds);
//! driver.init()?;
//! for _ in 0..4 {
//!     driver.pump(Duration::from_millis(100))?;
//! }
//! for note in driver.take_notifications() {
//!     if let Notification::CamReady = note {
//!         // now safe to send a ca_pmt
//!     }
//! }
//! # Ok::<(), std::io::Error>(())
//! ```
//!
//! # How do I...
//!
//! - Drive a CAM from a loop: [`Driver`] (`init`, `pump`, `take_notifications`),
//!   `examples/mock_cam_session.rs`.
//! - Run the protocol with no device, thread or clock: [`CiStack`], [`Event`],
//!   [`Action`], `examples/sans_io_core.rs`.
//! - React to CAM insertion and removal: [`HotPlug`],
//!   [`Driver::pump_hotplug`], `examples/hot_plug.rs`.
//! - Capture and read an annotated link trace:
//!   [`device::RecordingCaDevice`], [`trace::decode_log`],
//!   `examples/trace_session.rs`.
//! - Push scrambled TS through the CAM and read it back descrambled:
//!   [`CaDescrambler`], [`MockCiDataDevice`], `examples/descrambler_feed.rs`.
//! - Descramble a service from its PMT and track the active set:
//!   [`Driver::add_service`], [`Driver::set_cat`], no example yet.
//! - Answer a CAM menu or enquiry: [`Driver::mmi_menu_answer`],
//!   [`Driver::mmi_enquiry_answer`], no example yet.
//! - Test without hardware: [`MockCaDevice`], `examples/mock_cam_session.rs`.
//! - Talk to a real slot on Linux: the `linux` module (`LinuxCaDevice`,
//!   `LinuxCiDataDevice`) and the `ci-probe` binary, both behind feature
//!   `linux`; no example yet.
//!
//! # Modules
//!
//! Host-facing driver: [`driver`] (the pump loop and the managed CAS calls),
//! [`managed`] (the active-service set, PID routing and re-query),
//! [`descrambler`] (turnkey control plus data plane), [`event`] (events,
//! actions, host requests, notifications, hot-plug).
//!
//! Protocol core: [`stack`] (the composed sans-IO stack), [`transport`] (TPDU
//! poll loop), [`session`] (SPDU sessions), [`resource`] (the resource state
//! machines).
//!
//! Devices and diagnostics: [`device`] (the control-plane trait, mock and
//! recorder), [`dataplane`] (the `ciM` TS data-plane trait and mock),
//! [`trace`] (annotated link traces); the `linux` module (feature `linux`,
//! Linux only: the `/dev/dvb` devices).
//!
//! # Feature flags
//!
//! - `linux` (off by default): `LinuxCaDevice`, `LinuxCiDataDevice` and the
//!   `ci-probe` binary (pulls `rustix` and `clap`). Linux only; the portable
//!   core needs none of it.
//!
//! # Spec citations
//!
//! CENELEC EN 50221 Annex A section A.4 (TPDU transport), section 7.2 (SPDU
//! session layer), section 8 (resources), section 8.4.3.4 and Table 25
//! (`ca_pmt`); ISO/IEC 13818-1 section 2.6.16 (`CA_descriptor`); the Linux
//! `linux/dvb/ca.h` ABI for the device layer; transcriptions under
//! `private/specs-verified/dvb/dvb-ci/`.
// The portable core is unsafe-free; only the optional Linux device leaf
// (`#[allow(unsafe_code)]` in `linux`) uses ioctls, so this is `deny`, not
// `forbid`.
#![deny(unsafe_code)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

pub mod dataplane;
pub mod descrambler;
pub mod device;
pub mod driver;
pub mod event;
pub mod managed;
pub mod resource;
pub mod session;
pub mod stack;
pub mod trace;
pub mod transport;

#[cfg(all(feature = "linux", target_os = "linux"))]
pub mod linux;

pub use dataplane::{CiDataDevice, MockCiDataDevice, PartialWrite, partial_write_progress};
pub use descrambler::CaDescrambler;
pub use device::{
    CaDevice, DeviceOp, DeviceOpError, LinkEvent, MAX_CA_FRAME, MockCaDevice, RecordingCaDevice,
    SlotInfo,
};
pub use driver::{Driver, DriverBounds};
pub use event::{Action, Event, HostRequest, HotPlug, Notification};
#[cfg(all(feature = "linux", target_os = "linux"))]
pub use linux::{CiDataTimings, LinuxCaDevice, LinuxCiDataDevice};
pub use managed::{CaError, ManagedCa, ManagedService, REQUERY_DEFAULT};
pub use stack::{CiStack, StackBounds};
#[cfg(test)]
mod test_support;
pub use transport::TransportBounds;