1use std::io;
7use std::time::Duration;
8
9use crate::device::CaDevice;
10use crate::event::{Action, Event, HostRequest, Notification};
11use crate::stack::CiStack;
12
13pub struct Driver<D: CaDevice> {
15 device: D,
16 stack: CiStack,
17 notifications: Vec<Notification>,
18 next_timer: Option<Duration>,
20 buf: Vec<u8>,
22}
23
24impl<D: CaDevice> Driver<D> {
25 #[must_use]
27 pub fn new(device: D) -> Self {
28 Self {
29 device,
30 stack: CiStack::new(),
31 notifications: Vec::new(),
32 next_timer: None,
33 buf: vec![0u8; 4096],
34 }
35 }
36
37 pub fn device(&self) -> &D {
39 &self.device
40 }
41
42 pub fn next_timer(&self) -> Option<Duration> {
44 self.next_timer
45 }
46
47 pub fn take_notifications(&mut self) -> Vec<Notification> {
49 core::mem::take(&mut self.notifications)
50 }
51
52 pub fn init(&mut self) -> io::Result<()> {
54 let actions = self.stack.handle(Event::Host(HostRequest::Init));
55 self.run(actions)
56 }
57
58 pub fn send_ca_pmt(&mut self, ca_pmt: &[u8]) -> io::Result<()> {
61 let actions = self
62 .stack
63 .handle(Event::Host(HostRequest::SendCaPmt(ca_pmt)));
64 self.run(actions)
65 }
66
67 pub fn descramble(&mut self, pmt_section: &[u8]) -> io::Result<()> {
74 let actions = self
75 .stack
76 .handle(Event::Host(HostRequest::Descramble(pmt_section)));
77 self.run(actions)
78 }
79
80 pub fn mmi_menu_answer(&mut self, choice_ref: u8) -> io::Result<()> {
82 let actions = self
83 .stack
84 .handle(Event::Host(HostRequest::MmiMenuAnswer(choice_ref)));
85 self.run(actions)
86 }
87
88 pub fn mmi_enquiry_answer(&mut self, text: &[u8]) -> io::Result<()> {
90 let actions = self
91 .stack
92 .handle(Event::Host(HostRequest::MmiEnquiryAnswer(text)));
93 self.run(actions)
94 }
95
96 pub fn mmi_cancel(&mut self) -> io::Result<()> {
98 let actions = self.stack.handle(Event::Host(HostRequest::MmiCancel));
99 self.run(actions)
100 }
101
102 pub fn pump(&mut self, timeout: Duration) -> io::Result<bool> {
106 if self.device.poll(timeout)? {
107 let n = self.device.read(&mut self.buf)?;
108 if n > 0 {
109 let frame = self.buf[..n].to_vec();
110 let actions = self.stack.handle(Event::Readable(&frame));
111 self.run(actions)?;
112 return Ok(true);
113 }
114 }
115 let actions = self.stack.handle(Event::Tick { elapsed: timeout });
116 self.run(actions)?;
117 Ok(false)
118 }
119
120 fn run(&mut self, actions: Vec<Action>) -> io::Result<()> {
122 for action in actions {
123 match action {
124 Action::Write(bytes) => self.device.write(&bytes)?,
125 Action::Reset => self.device.reset()?,
126 Action::QuerySlot => {
127 self.device.slot_info()?;
128 }
129 Action::SetTimer { after } => self.next_timer = Some(after),
130 Action::Notify(n) => self.notifications.push(n),
131 }
132 }
133 Ok(())
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140 use crate::device::{DeviceOp, MockCaDevice};
141 use dvb_ci::tpdu::tags;
142
143 #[test]
144 fn init_drives_reset_slotinfo_and_create_tc_to_device() {
145 let mut d = Driver::new(MockCaDevice::new([]));
146 d.init().unwrap();
147 let ops = &d.device().ops;
148 assert_eq!(ops[0], DeviceOp::Reset);
149 assert_eq!(ops[1], DeviceOp::SlotInfo);
150 assert!(matches!(&ops[2], DeviceOp::Write(w) if w[0] == tags::CREATE_T_C));
151 }
152
153 #[test]
154 fn reads_reply_then_polls_on_pump() {
155 let dev = MockCaDevice::new([vec![tags::C_T_C_REPLY, 0x01, 0x01]]);
157 let mut d = Driver::new(dev);
158 d.init().unwrap();
159 assert!(d.pump(Duration::from_millis(100)).unwrap());
161 assert!(!d.pump(Duration::from_millis(100)).unwrap());
163 let last = d.device().ops.last().unwrap();
164 assert!(matches!(last, DeviceOp::Write(w) if w.first() == Some(&tags::DATA_LAST)));
165 }
166}