kernel/abi/xv6/drivers/
console.rs

1use core::any::Any;
2
3use crate::{device::{char::CharDevice, manager::DeviceManager, Device, DeviceType}};
4
5/// Character device for xv6 console that bridges to TTY
6pub struct ConsoleDevice {
7    id: usize,
8    name: &'static str,
9}
10
11impl ConsoleDevice {
12    pub fn new(id: usize, name: &'static str) -> Self {
13        Self {
14            id,
15            name,
16        }
17    }
18}
19
20impl Device for ConsoleDevice {
21    fn device_type(&self) -> DeviceType {
22        DeviceType::Char
23    }
24
25    fn name(&self) -> &'static str {
26        self.name
27    }
28
29    fn as_any(&self) -> &dyn Any {
30        self
31    }
32    
33    fn as_any_mut(&mut self) -> &mut dyn Any {
34        self
35    }
36    
37    fn as_char_device(&self) -> Option<&dyn CharDevice> {
38        Some(self)
39    }
40}
41
42impl CharDevice for ConsoleDevice {
43    fn read_byte(&self) -> Option<u8> {
44        // Bridge to TTY device instead of direct serial access
45        let device_manager = DeviceManager::get_manager();
46        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
47            return tty_device.as_char_device().and_then(|char_device| char_device.read_byte());
48        }
49        
50        // Fallback: return None if TTY is not available
51        None
52    }
53
54    fn write_byte(&self, byte: u8) -> Result<(), &'static str> {
55        // Bridge to TTY device instead of direct serial access
56        let device_manager = DeviceManager::get_manager();
57        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
58            if let Some(char_device) = tty_device.as_char_device() {
59                return char_device.write_byte(byte);
60            }
61        }
62        
63        Err("TTY device not available")
64    }
65
66    fn can_read(&self) -> bool {
67        // Check TTY availability and read capability
68        let device_manager = DeviceManager::get_manager();
69        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
70            if let Some(char_device) = tty_device.as_char_device() {
71                return char_device.can_read();
72            }
73        }
74        false
75    }
76
77    fn can_write(&self) -> bool {
78        // Check TTY availability and write capability
79        let device_manager = DeviceManager::get_manager();
80        if let Some(tty_device) = device_manager.get_device_by_name("tty0") {
81            if let Some(char_device) = tty_device.as_char_device() {
82                return char_device.can_write();
83            }
84        }
85        false
86    }
87}
88