123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- use core::fmt::Debug;
- use alloc::sync::{Arc, Weak};
- use kdepends::thingbuf::mpsc;
- use system_error::SystemError;
- use crate::{
- libs::spinlock::{SpinLock, SpinLockGuard},
- net::event_poll::EventPoll,
- };
- use super::tty_core::TtyCore;
- const TTY_PORT_BUFSIZE: usize = 4096;
- #[allow(dead_code)]
- #[derive(Debug)]
- pub struct TtyPortData {
- flags: i32,
- iflags: TtyPortState,
- sender: mpsc::Sender<u8>,
- receiver: mpsc::Receiver<u8>,
- tty: Weak<TtyCore>,
-
- internal_tty: Weak<TtyCore>,
- }
- impl Default for TtyPortData {
- fn default() -> Self {
- Self::new()
- }
- }
- impl TtyPortData {
- pub fn new() -> Self {
- let (sender, receiver) = mpsc::channel::<u8>(TTY_PORT_BUFSIZE);
- Self {
- flags: 0,
- iflags: TtyPortState::Initialized,
- sender,
- receiver,
- tty: Weak::new(),
- internal_tty: Weak::new(),
- }
- }
- pub fn internal_tty(&self) -> Option<Arc<TtyCore>> {
- self.internal_tty.upgrade()
- }
- pub fn tty(&self) -> Option<Arc<TtyCore>> {
- self.tty.upgrade()
- }
- }
- #[allow(dead_code)]
- #[derive(Debug, PartialEq, Clone, Copy)]
- pub enum TtyPortState {
- Initialized,
- Suspended,
- Active,
- CtsFlow,
- CheckCD,
- KOPENED,
- }
- pub trait TtyPort: Sync + Send + Debug {
- fn port_data(&self) -> SpinLockGuard<TtyPortData>;
-
- fn state(&self) -> TtyPortState {
- self.port_data().iflags
- }
-
- fn setup_internal_tty(&self, tty: Weak<TtyCore>) {
- self.port_data().internal_tty = tty;
- }
-
- fn receive_buf(&self, buf: &[u8], _flags: &[u8], count: usize) -> Result<usize, SystemError> {
- let tty = self.port_data().internal_tty().unwrap();
- let ld = tty.ldisc();
- let ret = ld.receive_buf2(tty.clone(), buf, None, count);
- if ret.is_err() && ret.clone().unwrap_err() == SystemError::ENOSYS {
- return ld.receive_buf(tty, buf, None, count);
- }
- EventPoll::wakeup_epoll(tty.core().eptiems(), None)?;
- ret
- }
- }
- #[derive(Debug)]
- pub struct DefaultTtyPort {
- port_data: SpinLock<TtyPortData>,
- }
- impl DefaultTtyPort {
- pub fn new() -> Self {
- Self {
- port_data: SpinLock::new(TtyPortData::new()),
- }
- }
- }
- impl TtyPort for DefaultTtyPort {
- fn port_data(&self) -> SpinLockGuard<TtyPortData> {
- self.port_data.lock_irqsave()
- }
- }
|