kernel/device/
events.rs

1//! Device event system for generic device communication.
2//!
3//! This module provides a generic event system that allows devices to communicate
4//! with each other without tight coupling.
5
6extern crate alloc;
7use alloc::sync::Weak;
8use alloc::vec::Vec;
9use core::any::Any;
10use spin::Mutex;
11
12/// Generic device event trait.
13///
14/// All device events must implement this trait to be handled by the event system.
15pub trait DeviceEvent: Send + Sync {
16    fn event_type(&self) -> &'static str;
17    fn as_any(&self) -> &dyn Any;
18}
19
20/// Device event listener trait.
21///
22/// Devices that want to receive events must implement this trait.
23pub trait DeviceEventListener: Send + Sync {
24    fn on_device_event(&self, event: &dyn DeviceEvent);
25    fn interested_in(&self, event_type: &str) -> bool;
26}
27
28/// Event capable device trait.
29///
30/// Devices that can emit events must implement this trait.
31pub trait EventCapableDevice {
32    fn register_event_listener(&self, listener: Weak<dyn DeviceEventListener>);
33    fn unregister_event_listener(&self, listener_id: &str);
34    fn emit_event(&self, event: &dyn DeviceEvent);
35}
36
37/// Generic device event emitter.
38///
39/// This struct provides a generic implementation for event emission.
40pub struct DeviceEventEmitter {
41    listeners: Mutex<Vec<Weak<dyn DeviceEventListener>>>,
42}
43
44impl DeviceEventEmitter {
45    pub fn new() -> Self {
46        Self {
47            listeners: Mutex::new(Vec::new()),
48        }
49    }
50
51    pub fn register_listener(&self, listener: Weak<dyn DeviceEventListener>) {
52        let mut listeners = self.listeners.lock();
53        listeners.push(listener);
54    }
55
56    pub fn emit(&self, event: &dyn DeviceEvent) {
57        let mut listeners = self.listeners.lock();
58
59        // Notify living listeners only and remove dead references
60        listeners.retain(|weak_listener| {
61            if let Some(listener) = weak_listener.upgrade() {
62                if listener.interested_in(event.event_type()) {
63                    listener.on_device_event(event);
64                }
65                true // Keep alive
66            } else {
67                crate::early_println!(
68                    "Removing dead listener for event type: {}",
69                    event.event_type()
70                );
71                false // Remove dead reference
72            }
73        });
74    }
75}
76
77/// Input event for character devices.
78///
79/// This event is emitted when a character device receives input.
80#[derive(Debug)]
81pub struct InputEvent {
82    pub data: u8,
83}
84
85impl DeviceEvent for InputEvent {
86    fn event_type(&self) -> &'static str {
87        "input"
88    }
89
90    fn as_any(&self) -> &dyn Any {
91        self
92    }
93}
94
95/// Output complete event for character devices.
96///
97/// This event is emitted when a character device completes output.
98#[derive(Debug)]
99pub struct OutputCompleteEvent;
100
101impl DeviceEvent for OutputCompleteEvent {
102    fn event_type(&self) -> &'static str {
103        "output_complete"
104    }
105
106    fn as_any(&self) -> &dyn Any {
107        self
108    }
109}
110
111/// Device error event.
112///
113/// This event is emitted when a device encounters an error.
114#[derive(Debug)]
115pub struct ErrorEvent {
116    pub error_code: u32,
117}
118
119impl DeviceEvent for ErrorEvent {
120    fn event_type(&self) -> &'static str {
121        "error"
122    }
123
124    fn as_any(&self) -> &dyn Any {
125        self
126    }
127}
128
129/// Interrupt capable device trait.
130///
131/// Devices that can handle interrupts must implement this trait.
132pub trait InterruptCapableDevice: Send + Sync {
133    fn handle_interrupt(&self) -> crate::interrupt::InterruptResult<()>;
134    fn interrupt_id(&self) -> Option<crate::interrupt::InterruptId>;
135}