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 std::time::Duration;
12
13use dvb_ci::resource::ResourceId;
14
15/// An input to the protocol core.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum Event<'a> {
19 /// One link-layer frame was read from the device.
20 Readable(&'a [u8]),
21 /// Logical time advanced by `elapsed` since the last tick (drives poll
22 /// cadence and resource timers without a real clock in the core).
23 Tick {
24 /// Time since the previous tick.
25 elapsed: Duration,
26 },
27 /// A request from the host application.
28 Host(HostRequest<'a>),
29}
30
31/// A request the host application makes of the stack.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub enum HostRequest<'a> {
35 /// Bring the interface up: reset the slot and open the transport connection.
36 Init,
37 /// Send a serialized `ca_pmt` APDU body to the CAM's conditional-access
38 /// resource (descrambling request).
39 SendCaPmt(&'a [u8]),
40 /// Answer an MMI `menu`/`list` by 1-based `choice_ref` (0 = "back"/cancel),
41 /// sent as `menu_answ` to the module.
42 MmiMenuAnswer(u8),
43 /// Answer an MMI `enquiry` with the user's input text (EN 300 468 Annex A
44 /// bytes), sent as `answ` (`answ_id = answer`).
45 MmiEnquiryAnswer(&'a [u8]),
46 /// Abort the current MMI enquiry (`answ` with `answ_id = cancel`).
47 MmiCancel,
48 /// Descramble the services in a PMT section (raw `dvb-si` PMT bytes). The
49 /// stack filters the PMT's `CA_descriptor`s to the CAM's advertised CAIDs
50 /// (from its `ca_info`), sends a `ca_pmt` with `cmd_id = query`, and — when
51 /// the `ca_pmt_reply` reports descrambling is possible — automatically sends
52 /// `cmd_id = ok_descrambling`. The reply outcome surfaces as
53 /// [`Notification::CaPmtReply`].
54 Descramble(&'a [u8]),
55 /// Tear the interface down (close sessions + transport connection).
56 Shutdown,
57}
58
59/// An output the driver loop must perform.
60#[derive(Debug, Clone, PartialEq, Eq)]
61#[non_exhaustive]
62pub enum Action {
63 /// Write one link-layer frame to the device.
64 Write(Vec<u8>),
65 /// Issue the `CA_RESET` ioctl.
66 Reset,
67 /// Issue the `CA_GET_SLOT_INFO` ioctl.
68 QuerySlot,
69 /// Arm the poll/timer to fire after `after` (coalesced: the latest wins).
70 SetTimer {
71 /// Delay before the next [`Event::Tick`] should be delivered.
72 after: Duration,
73 },
74 /// Surface a host-facing [`Notification`].
75 Notify(Notification),
76}
77
78/// A host-facing event surfaced by the stack (the useful outputs of a CI
79/// session — what an application reacts to).
80#[derive(Debug, Clone, PartialEq, Eq)]
81#[non_exhaustive]
82pub enum Notification {
83 /// The module is present and the resource-manager handshake completed.
84 CamReady,
85 /// `application_information` was received.
86 ApplicationInfo {
87 /// `application_type` (0x01 = CA).
88 application_type: u8,
89 /// `application_manufacturer`.
90 manufacturer: u16,
91 /// `manufacturer_code`.
92 code: u16,
93 /// The decoded menu string.
94 menu: String,
95 },
96 /// `ca_info` was received — the CA system ids the module supports.
97 CaInfo {
98 /// `CA_system_id` values the CAM can descramble.
99 ca_system_ids: Vec<u16>,
100 },
101 /// A `ca_pmt_reply` was received for a prior CA_PMT.
102 CaPmtReply {
103 /// `program_number` the reply pertains to.
104 program_number: u16,
105 /// Raw `CA_enable`/descrambling-possibility bytes (per-program/ES).
106 descrambling_ok: bool,
107 },
108 /// An MMI menu/enquiry the host should display.
109 Mmi(MmiEvent),
110 /// A session for `resource` was opened.
111 SessionOpened {
112 /// The resource the session serves.
113 resource: ResourceId,
114 },
115 /// A session closed.
116 SessionClosed {
117 /// The `session_nb` that closed.
118 session_nb: u16,
119 },
120 /// A protocol error surfaced by the stack (non-fatal; informational).
121 Error {
122 /// Human-readable detail.
123 detail: String,
124 },
125}
126
127/// MMI (man-machine interface) host events.
128#[derive(Debug, Clone, PartialEq, Eq)]
129#[non_exhaustive]
130pub enum MmiEvent {
131 /// A `menu`/`list` to display: title + items.
132 Menu {
133 /// Menu title text.
134 title: String,
135 /// Selectable item texts.
136 items: Vec<String>,
137 },
138 /// An `enquiry` (text prompt) expecting an answer.
139 Enquiry {
140 /// Prompt text.
141 prompt: String,
142 /// Whether the answer should be hidden (e.g. PIN).
143 blind: bool,
144 /// Expected answer length.
145 answer_len: u8,
146 },
147 /// The module closed the MMI dialog.
148 Close,
149}