dvb_ci_runtime/event.rs
1//! The sans-IO event/action model.
2//!
3//! The protocol core is pure: it consumes [`Event`]s and produces [`Action`]s,
4//! with no device, threads, or clock of its own. The driver loop executes the
5//! actions against a [`CaDevice`](crate::CaDevice) and feeds events back. This
6//! keeps every state machine deterministic and testable without
7//! hardware — a test (or a differential comparison against an external
8//! reference) drives a sequence of events and asserts the emitted action
9//! sequence.
10
11use mpeg_ts::pid::Pid;
12use std::time::Duration;
13
14use dvb_ci::objects::ca_pmt_reply::CaEnable;
15use dvb_ci::resource::ResourceId;
16
17/// An input to the protocol core.
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[non_exhaustive]
20pub enum Event<'a> {
21 /// One link-layer frame was read from the device.
22 Readable(&'a [u8]),
23 /// Logical time advanced by `elapsed` since the last tick (drives poll
24 /// cadence and resource timers without a real clock in the core).
25 Tick {
26 /// Time since the previous tick.
27 elapsed: Duration,
28 },
29 /// A request from the host application.
30 Host(HostRequest<'a>),
31}
32
33/// A request the host application makes of the stack.
34///
35/// `Debug` redacts the `MmiEnquiryAnswer` text — it is whatever the user typed
36/// at a (possibly blind) enquiry, i.e. often a PIN (audit #1142).
37#[derive(Clone, PartialEq, Eq)]
38#[non_exhaustive]
39pub enum HostRequest<'a> {
40 /// Bring the interface up: reset the slot and open the transport connection.
41 Init,
42 /// Send a serialized `ca_pmt` APDU body to the CAM's conditional-access
43 /// resource (descrambling request).
44 SendCaPmt(&'a [u8]),
45 /// Answer an MMI `menu`/`list` by 1-based `choice_ref` (0 = "back"/cancel),
46 /// sent as `menu_answ` to the module.
47 MmiMenuAnswer(u8),
48 /// Answer an MMI `enquiry` with the user's input text (EN 300 468 Annex A
49 /// bytes), sent as `answ` (`answ_id = answer`).
50 MmiEnquiryAnswer(&'a [u8]),
51 /// Abort the current MMI enquiry (`answ` with `answ_id = cancel`).
52 MmiCancel,
53 /// Ask the module to open its MMI menu (`enter_menu` on the
54 /// application_information session) — e.g. to read card / entitlement info.
55 EnterMenu,
56 /// Descramble the services in a PMT section (raw `dvb-si` PMT bytes). The
57 /// stack filters the PMT's `CA_descriptor`s to the CAM's advertised CAIDs
58 /// (from its `ca_info`) and sends a `ca_pmt` with `list_management = only`,
59 /// `cmd_id = ok_descrambling`. The reply outcome surfaces as
60 /// [`Notification::CaPmtReply`].
61 Descramble(&'a [u8]),
62 /// Descramble a **set** of programmes in one CA-PMT list (`first`/`more`/
63 /// `last`, all `ok_descrambling`), replacing any prior set. Each element is a
64 /// raw PMT section.
65 DescramblePrograms(&'a [&'a [u8]]),
66 /// Add one programme to the descrambled set (`list_management = add`).
67 AddProgram(&'a [u8]),
68 /// Remove one programme from the descrambled set (`list_management = update`,
69 /// `cmd_id = not_selected`).
70 RemoveProgram(&'a [u8]),
71 /// Tear the interface down (close sessions + transport connection).
72 Shutdown,
73}
74
75impl core::fmt::Debug for HostRequest<'_> {
76 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77 match self {
78 Self::Init => f.write_str("Init"),
79 Self::SendCaPmt(b) => f.debug_tuple("SendCaPmt").field(b).finish(),
80 Self::MmiMenuAnswer(n) => f.debug_tuple("MmiMenuAnswer").field(n).finish(),
81 Self::MmiEnquiryAnswer(b) => f
82 .debug_tuple("MmiEnquiryAnswer")
83 .field(&format_args!("<{} bytes redacted>", b.len()))
84 .finish(),
85 Self::MmiCancel => f.write_str("MmiCancel"),
86 Self::EnterMenu => f.write_str("EnterMenu"),
87 Self::Descramble(b) => f.debug_tuple("Descramble").field(b).finish(),
88 Self::DescramblePrograms(p) => f.debug_tuple("DescramblePrograms").field(p).finish(),
89 Self::AddProgram(b) => f.debug_tuple("AddProgram").field(b).finish(),
90 Self::RemoveProgram(b) => f.debug_tuple("RemoveProgram").field(b).finish(),
91 Self::Shutdown => f.write_str("Shutdown"),
92 }
93 }
94}
95
96/// An output the driver loop must perform.
97#[derive(Debug, Clone, PartialEq, Eq)]
98#[non_exhaustive]
99pub enum Action {
100 /// Write one link-layer frame to the device.
101 Write(Vec<u8>),
102 /// Issue the `CA_RESET` ioctl.
103 Reset,
104 /// Issue the `CA_GET_SLOT_INFO` ioctl.
105 QuerySlot,
106 /// Arm the poll/timer to fire after `after` (coalesced: the latest wins).
107 SetTimer {
108 /// Delay before the next [`Event::Tick`] should be delivered.
109 after: Duration,
110 },
111 /// Surface a host-facing [`Notification`].
112 Notify(Notification),
113}
114
115/// A host-facing event surfaced by the stack (the useful outputs of a CI
116/// session — what an application reacts to).
117#[derive(Debug, Clone, PartialEq, Eq)]
118#[non_exhaustive]
119pub enum Notification {
120 /// The module is present and the resource-manager handshake completed.
121 CamReady,
122 /// `application_information` was received.
123 ApplicationInfo {
124 /// `application_type` (0x01 = CA).
125 application_type: u8,
126 /// `application_manufacturer`.
127 manufacturer: u16,
128 /// `manufacturer_code`.
129 code: u16,
130 /// The decoded menu string.
131 menu: String,
132 },
133 /// `ca_info` was received — the CA system ids the module supports.
134 CaInfo {
135 /// `CA_system_id` values the CAM can descramble.
136 ca_system_ids: Vec<u16>,
137 },
138 /// A `ca_pmt_reply` was received for a prior CA_PMT.
139 CaPmtReply {
140 /// `program_number` the reply pertains to.
141 program_number: u16,
142 /// Programme-level `CA_enable` (EN 50221 §8.4.3.5 Table 26). `None`
143 /// iff the programme `CA_enable_flag` bit was clear (no
144 /// programme-level status given) — plumbed straight through from the
145 /// `dvb_ci` `CaPmtReply` object's own `Option<CaEnable>`, never
146 /// collapsed to a sentinel.
147 ca_enable: Option<CaEnable>,
148 /// Whether descrambling is (or was) possible, derived from
149 /// `ca_enable`. Kept for back-compat with the pre-#763 boolean-only
150 /// surface.
151 descrambling_ok: bool,
152 },
153 /// A per-programme entitlement status transition (#763). Edge-triggered:
154 /// fires once per `program_number` only when the programme-level
155 /// `CA_enable` status (EN 50221 §8.4.3.5, Table 26) changes versus the
156 /// last observed `ca_pmt_reply` for that programme. The transition is
157 /// detected by the periodic re-query (`Driver::set_requery_interval`),
158 /// which re-sends the active `ca_pmt`s with `ca_pmt_cmd_id = query` so
159 /// the CAM re-evaluates and replies. Complements the coarse #726
160 /// `HotPlug` module/card layer with the fine-grained per-service layer.
161 /// (Programme-level status only; the ES-level `CA_enable` entries are
162 /// not evaluated for this event.)
163 Entitlement {
164 /// `program_number` the status pertains to.
165 program_number: u16,
166 /// Programme-level `CA_enable` status.
167 ca_enable: CaEnable,
168 /// Whether descrambling is possible per the current status.
169 descrambling_ok: bool,
170 },
171 /// An MMI menu/enquiry the host should display.
172 Mmi(MmiEvent),
173 /// A `host_control` request the CAM made of the host (EN 50221 §8.5.1). The
174 /// host acts on it out-of-band (retune / PID replace); the runtime only
175 /// surfaces the decoded request.
176 HostControl(HostControlEvent),
177 /// A session for `resource` was opened.
178 SessionOpened {
179 /// The resource the session serves.
180 resource: ResourceId,
181 },
182 /// A session closed.
183 SessionClosed {
184 /// The `session_nb` that closed.
185 session_nb: u16,
186 },
187 /// The host clock read an instant the Date-Time resource's 5-byte
188 /// `UTC_time` field (EN 300 468 Annex C: 16-bit MJD + BCD `HHMMSS`)
189 /// cannot express: before 1970-01-01 or from MJD 65536 (2141-09-10) on.
190 /// The `date_time` resource then sends no reply for that enquiry (or
191 /// resend) rather than a wrong date, and reports it here.
192 ClockUnrepresentable,
193 /// A protocol error surfaced by the stack (non-fatal; informational).
194 Error {
195 /// Human-readable detail.
196 detail: String,
197 },
198 /// A CAM/card hot-plug transition (#726). See [`HotPlug`].
199 HotPlug(HotPlug),
200}
201
202impl Notification {
203 /// This notification's [`HotPlug`] transition, if it is one — a cheap
204 /// classifier for poll-mode consumers that only care about hot-plug
205 /// edges.
206 #[must_use]
207 pub fn hotplug(&self) -> Option<HotPlug> {
208 if let Notification::HotPlug(h) = self {
209 Some(*h)
210 } else {
211 None
212 }
213 }
214}
215
216/// A CAM/card hot-plug transition. `Cam*` are real DVB-CA slot-status edges;
217/// `Card*` are best-effort EN 50221 app-layer inference (no card-detect line).
218#[derive(Debug, Clone, Copy, PartialEq, Eq)]
219#[non_exhaustive]
220pub enum HotPlug {
221 /// The module transitioned absent → present (DVB-CA slot status:
222 /// `CA_CI_MODULE_PRESENT` went from clear to set — Linux uapi
223 /// `linux/dvb/ca.h` `ca_slot_info.flags`; `CA_CI_MODULE_READY` is **not**
224 /// required, the driver keys the edge off `module_present` alone). A real
225 /// hardware signal from [`SlotInfo`](crate::device::SlotInfo), surfaced
226 /// once on the edge (not on every poll); the driver re-drives the handshake (a fresh
227 /// [`Init`](crate::event::HostRequest::Init)) so the newly-inserted module
228 /// gets a clean resource-manager session.
229 CamPresent,
230 /// The module transitioned present → absent (DVB-CA slot status:
231 /// `CA_CI_MODULE_PRESENT` clear). A real hardware signal; the driver tears
232 /// down all session/handshake state so a later re-insert re-handshakes
233 /// cleanly rather than reusing stale session numbers.
234 CamRemoved,
235 /// A smart card was inferred to have been inserted into the module.
236 ///
237 /// **Best-effort app-layer inference** — EN 50221 slots are module-level
238 /// only; there is no card-detect line (verified against DD ddbridge /
239 /// cxd2099 driver behaviour). Raised from one of: an `ca_info` CAID-set
240 /// transition from empty to non-empty, or a `ca_pmt_reply`
241 /// `descrambling_ok` transition from `false` to `true`. Some CAMs instead
242 /// give a strong signal by resetting the module on card change, which
243 /// surfaces as [`CamPresent`](HotPlug::CamPresent) rather than this
244 /// variant.
245 CardInserted,
246 /// A smart card was inferred to have been removed from the module.
247 ///
248 /// **Best-effort app-layer inference** (see [`CardInserted`](HotPlug::CardInserted)
249 /// for why no hardware signal exists). Raised from one of: an `ca_info`
250 /// CAID-set transition from non-empty to empty, a `ca_pmt_reply`
251 /// `descrambling_ok` transition from `true` to `false`, or MMI menu/list/
252 /// enquiry text matching a "no card" style keyword.
253 CardRemoved,
254 /// The inserted smart card was inferred to have changed (swapped without
255 /// an intervening removal the runtime observed).
256 ///
257 /// **Best-effort app-layer inference** (see [`CardInserted`](HotPlug::CardInserted)
258 /// for why no hardware signal exists). Raised when a later `ca_info`
259 /// reports a different non-empty CAID set than the last one seen for this
260 /// module.
261 CardChanged,
262}
263
264impl HotPlug {
265 /// Stable lowercase spec-ish token for this transition (#204 label
266 /// convention).
267 #[must_use]
268 pub fn name(&self) -> &'static str {
269 match self {
270 Self::CamPresent => "cam-present",
271 Self::CamRemoved => "cam-removed",
272 Self::CardInserted => "card-inserted",
273 Self::CardRemoved => "card-removed",
274 Self::CardChanged => "card-changed",
275 }
276 }
277}
278
279broadcast_common::impl_spec_display!(HotPlug);
280
281/// A decoded high-level MMI `menu()` / `list()` ready for display (§8.6.5,
282/// Tables 49/51). The three header lines and the choice list are kept separate
283/// so a UI can render them directly — a title bar, two sub-lines, and a list of
284/// selectable rows — without re-parsing.
285#[derive(Debug, Clone, PartialEq, Eq, Default)]
286pub struct MmiMenu {
287 /// Title line.
288 pub title: String,
289 /// Sub-title line (often a section heading).
290 pub subtitle: String,
291 /// Bottom line (often a hint such as "Select item and press OK").
292 pub bottom: String,
293 /// The selectable choices, in wire order. Answer the Nth (1-based) with
294 /// [`Driver::mmi_menu_answer`](crate::Driver::mmi_menu_answer)`(N)`; `0`
295 /// cancels / goes back.
296 pub choices: Vec<String>,
297}
298
299/// MMI (man-machine interface) host events.
300#[derive(Debug, Clone, PartialEq, Eq)]
301#[non_exhaustive]
302pub enum MmiEvent {
303 /// A `menu()` to display — the user picks one choice. Answer via
304 /// [`Driver::mmi_menu_answer`](crate::Driver::mmi_menu_answer).
305 Menu(MmiMenu),
306 /// A `list()` to display — informational (e.g. an entitlement listing); the
307 /// host typically dismisses it with
308 /// [`Driver::mmi_menu_answer`](crate::Driver::mmi_menu_answer)`(0)`.
309 List(MmiMenu),
310 /// An `enquiry` (text prompt) expecting an answer. Reply via
311 /// [`Driver::mmi_enquiry_answer`](crate::Driver::mmi_enquiry_answer) or
312 /// [`Driver::mmi_cancel`](crate::Driver::mmi_cancel).
313 Enquiry {
314 /// Prompt text.
315 prompt: String,
316 /// Whether the answer should be hidden (e.g. PIN).
317 blind: bool,
318 /// Expected answer length.
319 answer_len: u8,
320 },
321 /// The module closed the MMI dialog.
322 Close,
323}
324
325/// A `host_control` request the CAM makes of the host (ETSI EN 50221 §8.5.1,
326/// Tables 27-30). The runtime surfaces the decoded request; the host performs
327/// the retune / PID replacement itself (out of band) — there is no re-tune
328/// logic in the stack.
329#[derive(Debug, Clone, Copy, PartialEq, Eq)]
330#[non_exhaustive]
331pub enum HostControlEvent {
332 /// `tune()` (Table 27): retune to the identified service.
333 Tune {
334 /// `network_id`.
335 network_id: u16,
336 /// `original_network_id`.
337 original_network_id: u16,
338 /// `transport_stream_id`.
339 transport_stream_id: u16,
340 /// `service_id`.
341 service_id: u16,
342 },
343 /// `replace()` (Table 28): temporarily replace one component PID with
344 /// another from the same multiplex.
345 Replace {
346 /// `replacement_ref` — matched later by a Clear Replace.
347 replacement_ref: u8,
348 /// 13-bit `replaced_PID`.
349 replaced_pid: Pid,
350 /// 13-bit `replacement_PID`.
351 replacement_pid: Pid,
352 },
353 /// `clear_replace()` (Table 29): undo all Replace operations sharing this
354 /// `replacement_ref`.
355 ClearReplace {
356 /// `replacement_ref` shared with one or more prior Replace requests.
357 replacement_ref: u8,
358 },
359 /// `ask_release()` (Table 30): the CAM asks the host to release any
360 /// replacements it holds (header-only request).
361 AskRelease,
362}
363
364#[cfg(test)]
365mod tests {
366 use super::*;
367
368 #[test]
369 fn host_request_debug_does_not_print_an_enquiry_answer() {
370 let dbg = format!("{:?}", HostRequest::MmiEnquiryAnswer(b"9876"));
371 assert_eq!(dbg, "MmiEnquiryAnswer(<4 bytes redacted>)");
372 }
373
374 #[test]
375 fn notification_entitlement_construction() {
376 let note = Notification::Entitlement {
377 program_number: 1234,
378 ca_enable: CaEnable::Possible,
379 descrambling_ok: true,
380 };
381 match note {
382 Notification::Entitlement {
383 program_number,
384 ca_enable,
385 descrambling_ok,
386 } => {
387 assert_eq!(program_number, 1234);
388 assert_eq!(ca_enable, CaEnable::Possible);
389 assert!(descrambling_ok);
390 }
391 _ => panic!("expected Entitlement variant"),
392 }
393 }
394}