twizzler_driver/
controller.rs

1use std::sync::Arc;
2
3use twizzler_abi::device::MailboxPriority;
4use twizzler_rt_abi::Result;
5
6use crate::device::{
7    events::{DeviceEventStream, InterruptInfo},
8    Device,
9};
10
11/// A single manager for both a device and an associated [DeviceEventStream].
12pub struct DeviceController {
13    device: Arc<Device>,
14    events: Arc<DeviceEventStream>,
15}
16
17impl DeviceController {
18    /// Get a reference to the event stream.
19    pub fn events(&self) -> &DeviceEventStream {
20        &self.events
21    }
22
23    /// Get a reference to the device.
24    pub fn device(&self) -> &Device {
25        &self.device
26    }
27
28    /// Create a new device controller from a device.
29    pub fn new_from_device(device: Device) -> Self {
30        let device = Arc::new(device);
31        Self {
32            device: device.clone(),
33            events: Arc::new(DeviceEventStream::new(device)),
34        }
35    }
36
37    /// Allocate a new interrupt on this device.
38    pub fn allocate_interrupt(&self) -> Result<InterruptInfo> {
39        self.events.allocate_interrupt()
40    }
41
42    /// Poll a single mailbox. If there are no messages, returns None.
43    pub fn check_mailbox(&self, pri: MailboxPriority) -> Option<u64> {
44        self.events.check_mailbox(pri)
45    }
46
47    /// Get the next message with a priority equal to or higher that `min`.
48    pub async fn next_msg(&self, min: MailboxPriority) -> (MailboxPriority, u64) {
49        self.events.next_msg(min).await
50    }
51}