152da9a59SGnoCiYeH use core::fmt::Debug; 252da9a59SGnoCiYeH 352da9a59SGnoCiYeH use alloc::sync::Arc; 452da9a59SGnoCiYeH use system_error::SystemError; 552da9a59SGnoCiYeH 652da9a59SGnoCiYeH use crate::filesystem::vfs::file::FileMode; 752da9a59SGnoCiYeH 852da9a59SGnoCiYeH use super::{ 952da9a59SGnoCiYeH termios::Termios, 10*dfe53cf0SGnoCiYeH tty_core::{TtyCore, TtyCoreData, TtyFlag}, 1152da9a59SGnoCiYeH }; 1252da9a59SGnoCiYeH 1352da9a59SGnoCiYeH pub mod ntty; 1452da9a59SGnoCiYeH 1552da9a59SGnoCiYeH pub trait TtyLineDiscipline: Sync + Send + Debug { open(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>1652da9a59SGnoCiYeH fn open(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>; close(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>1752da9a59SGnoCiYeH fn close(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>; flush_buffer(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>1852da9a59SGnoCiYeH fn flush_buffer(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>; 1952da9a59SGnoCiYeH 2052da9a59SGnoCiYeH /// ## tty行规程循环读取函数 2152da9a59SGnoCiYeH /// 2252da9a59SGnoCiYeH /// ### 参数 2352da9a59SGnoCiYeH /// - tty: 操作的tty 2452da9a59SGnoCiYeH /// - buf: 数据将被读取到buf 2552da9a59SGnoCiYeH /// - len: 读取的字节长度 2652da9a59SGnoCiYeH /// - cookie: 表示是否是继续上次的读,第一次读取应该传入false 2752da9a59SGnoCiYeH /// - offset: 读取的偏移量 read( &self, tty: Arc<TtyCore>, buf: &mut [u8], len: usize, cookie: &mut bool, offset: usize, mode: FileMode, ) -> Result<usize, SystemError>2852da9a59SGnoCiYeH fn read( 2952da9a59SGnoCiYeH &self, 3052da9a59SGnoCiYeH tty: Arc<TtyCore>, 3152da9a59SGnoCiYeH buf: &mut [u8], 3252da9a59SGnoCiYeH len: usize, 3352da9a59SGnoCiYeH cookie: &mut bool, 3452da9a59SGnoCiYeH offset: usize, 3552da9a59SGnoCiYeH mode: FileMode, 3652da9a59SGnoCiYeH ) -> Result<usize, SystemError>; write( &self, tty: Arc<TtyCore>, buf: &[u8], len: usize, mode: FileMode, ) -> Result<usize, SystemError>3752da9a59SGnoCiYeH fn write( 3852da9a59SGnoCiYeH &self, 3952da9a59SGnoCiYeH tty: Arc<TtyCore>, 4052da9a59SGnoCiYeH buf: &[u8], 4152da9a59SGnoCiYeH len: usize, 4252da9a59SGnoCiYeH mode: FileMode, 4352da9a59SGnoCiYeH ) -> Result<usize, SystemError>; ioctl(&self, tty: Arc<TtyCore>, cmd: u32, arg: usize) -> Result<usize, SystemError>4452da9a59SGnoCiYeH fn ioctl(&self, tty: Arc<TtyCore>, cmd: u32, arg: usize) -> Result<usize, SystemError>; 4552da9a59SGnoCiYeH 4652da9a59SGnoCiYeH /// ### 设置termios后更新行规程状态 4752da9a59SGnoCiYeH /// 4852da9a59SGnoCiYeH /// - old: 之前的termios,如果为None则表示第一次设置 set_termios(&self, tty: Arc<TtyCore>, old: Option<Termios>) -> Result<(), SystemError>4952da9a59SGnoCiYeH fn set_termios(&self, tty: Arc<TtyCore>, old: Option<Termios>) -> Result<(), SystemError>; 5052da9a59SGnoCiYeH poll(&self, tty: Arc<TtyCore>) -> Result<usize, SystemError>5152bcb59eSGnoCiYeH fn poll(&self, tty: Arc<TtyCore>) -> Result<usize, SystemError>; hangup(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>5252da9a59SGnoCiYeH fn hangup(&self, tty: Arc<TtyCore>) -> Result<(), SystemError>; 5352da9a59SGnoCiYeH 5452da9a59SGnoCiYeH /// ## 接收数据 receive_buf( &self, tty: Arc<TtyCore>, buf: &[u8], flags: Option<&[u8]>, count: usize, ) -> Result<usize, SystemError>5552da9a59SGnoCiYeH fn receive_buf( 5652da9a59SGnoCiYeH &self, 5752da9a59SGnoCiYeH tty: Arc<TtyCore>, 5852da9a59SGnoCiYeH buf: &[u8], 5952da9a59SGnoCiYeH flags: Option<&[u8]>, 6052da9a59SGnoCiYeH count: usize, 6152da9a59SGnoCiYeH ) -> Result<usize, SystemError>; 6252da9a59SGnoCiYeH 6352da9a59SGnoCiYeH /// ## 接收数据 receive_buf2( &self, tty: Arc<TtyCore>, buf: &[u8], flags: Option<&[u8]>, count: usize, ) -> Result<usize, SystemError>6452da9a59SGnoCiYeH fn receive_buf2( 6552da9a59SGnoCiYeH &self, 6652da9a59SGnoCiYeH tty: Arc<TtyCore>, 6752da9a59SGnoCiYeH buf: &[u8], 6852da9a59SGnoCiYeH flags: Option<&[u8]>, 6952da9a59SGnoCiYeH count: usize, 7052da9a59SGnoCiYeH ) -> Result<usize, SystemError>; 7152da9a59SGnoCiYeH 7252da9a59SGnoCiYeH /// ## 唤醒线路写者 write_wakeup(&self, _tty: &TtyCoreData) -> Result<(), SystemError>7352da9a59SGnoCiYeH fn write_wakeup(&self, _tty: &TtyCoreData) -> Result<(), SystemError> { 7452da9a59SGnoCiYeH Err(SystemError::ENOSYS) 7552da9a59SGnoCiYeH } 7652da9a59SGnoCiYeH } 7752da9a59SGnoCiYeH 7852da9a59SGnoCiYeH #[derive(Debug, Clone, Copy)] 7952da9a59SGnoCiYeH pub enum LineDisciplineType { 8052da9a59SGnoCiYeH NTty = 0, 8152da9a59SGnoCiYeH } 8252da9a59SGnoCiYeH 8352da9a59SGnoCiYeH impl LineDisciplineType { from_line(line: u8) -> Self8452da9a59SGnoCiYeH pub fn from_line(line: u8) -> Self { 8552da9a59SGnoCiYeH match line { 8652da9a59SGnoCiYeH 0 => Self::NTty, 8752da9a59SGnoCiYeH _ => { 8852da9a59SGnoCiYeH todo!() 8952da9a59SGnoCiYeH } 9052da9a59SGnoCiYeH } 9152da9a59SGnoCiYeH } 9252da9a59SGnoCiYeH } 9352da9a59SGnoCiYeH 9452da9a59SGnoCiYeH pub struct TtyLdiscManager; 9552da9a59SGnoCiYeH 9652da9a59SGnoCiYeH impl TtyLdiscManager { 9752da9a59SGnoCiYeH /// ## 为tty初始化ldisc 9852da9a59SGnoCiYeH /// 9952da9a59SGnoCiYeH /// ### 参数 10052da9a59SGnoCiYeH /// - tty:需要设置的tty 10152da9a59SGnoCiYeH /// - o_tty: other tty 用于pty pair ldisc_setup(tty: Arc<TtyCore>, o_tty: Option<Arc<TtyCore>>) -> Result<(), SystemError>102*dfe53cf0SGnoCiYeH pub fn ldisc_setup(tty: Arc<TtyCore>, o_tty: Option<Arc<TtyCore>>) -> Result<(), SystemError> { 10352da9a59SGnoCiYeH let ld = tty.ldisc(); 10452da9a59SGnoCiYeH 10552da9a59SGnoCiYeH let ret = ld.open(tty); 106b5b571e0SLoGin if let Err(err) = ret { 10752da9a59SGnoCiYeH if err == SystemError::ENOSYS { 10852da9a59SGnoCiYeH return Err(err); 10952da9a59SGnoCiYeH } 11052da9a59SGnoCiYeH } 11152da9a59SGnoCiYeH 112*dfe53cf0SGnoCiYeH // 处理PTY 113*dfe53cf0SGnoCiYeH if let Some(o_tty) = o_tty { 114*dfe53cf0SGnoCiYeH let ld = o_tty.ldisc(); 115*dfe53cf0SGnoCiYeH 116*dfe53cf0SGnoCiYeH let ret: Result<(), SystemError> = ld.open(o_tty.clone()); 117*dfe53cf0SGnoCiYeH if ret.is_err() { 118*dfe53cf0SGnoCiYeH o_tty.core().flags_write().remove(TtyFlag::LDISC_OPEN); 119*dfe53cf0SGnoCiYeH let _ = ld.close(o_tty.clone()); 120*dfe53cf0SGnoCiYeH } 121*dfe53cf0SGnoCiYeH } 12252da9a59SGnoCiYeH 12352da9a59SGnoCiYeH Ok(()) 12452da9a59SGnoCiYeH } 12552da9a59SGnoCiYeH } 126