refactor to nicer api

This commit is contained in:
Bruce Leidl 2019-09-12 16:25:05 -04:00
parent 9cc84e6116
commit a32324381f

View File

@ -80,6 +80,12 @@ impl VirtioDeviceOps for VirtioSerial {
true true
} }
fn read_config(&mut self, offset: usize, _size: usize) -> u64 {
if offset == 4 {
return 1;
}
0
}
fn start(&mut self, memory: &MemoryManager, mut queues: Vec<VirtQueue>) { fn start(&mut self, memory: &MemoryManager, mut queues: Vec<VirtQueue>) {
let mut term = Terminal::create(queues.remove(0)); let mut term = Terminal::create(queues.remove(0));
@ -93,17 +99,10 @@ impl VirtioDeviceOps for VirtioSerial {
let mut control = Control::new(queues.remove(0), queues.remove(0)); let mut control = Control::new(queues.remove(0), queues.remove(0));
spawn(move || { spawn(move || {
control.run(); control.run();
}); });
} }
} }
fn read_config(&mut self, offset: usize, _size: usize) -> u64 {
if offset == 4 {
return 1;
}
0
}
} }
struct Control { struct Control {
@ -111,7 +110,6 @@ struct Control {
tx_vq: VirtQueue, tx_vq: VirtQueue,
} }
use byteorder::{LittleEndian,ReadBytesExt,WriteBytesExt};
impl Control { impl Control {
fn new(rx: VirtQueue, tx: VirtQueue) -> Control { fn new(rx: VirtQueue, tx: VirtQueue) -> Control {
Control { rx_vq: rx, tx_vq: tx } Control { rx_vq: rx, tx_vq: tx }
@ -120,9 +118,9 @@ impl Control {
fn run(&mut self) { fn run(&mut self) {
let mut rx = self.rx_vq.clone(); let mut rx = self.rx_vq.clone();
self.tx_vq.on_each_chain(|mut chain| { self.tx_vq.on_each_chain(|mut chain| {
let _id = chain.read_u32::<LittleEndian>().unwrap(); let _id = chain.r32().unwrap();
let event = chain.read_u16::<LittleEndian>().unwrap(); let event = chain.r16().unwrap();
let _value = chain.read_u16::<LittleEndian>().unwrap(); let _value = chain.r16().unwrap();
if event == VIRTIO_CONSOLE_DEVICE_READY { if event == VIRTIO_CONSOLE_DEVICE_READY {
Control::send_msg(&mut rx,0, VIRTIO_CONSOLE_DEVICE_ADD, 1).unwrap(); Control::send_msg(&mut rx,0, VIRTIO_CONSOLE_DEVICE_ADD, 1).unwrap();
} }
@ -138,9 +136,9 @@ impl Control {
fn send_msg(vq: &mut VirtQueue, id: u32, event: u16, val: u16) -> io::Result<()> { fn send_msg(vq: &mut VirtQueue, id: u32, event: u16, val: u16) -> io::Result<()> {
let mut chain = vq.wait_next_chain().unwrap(); let mut chain = vq.wait_next_chain().unwrap();
chain.write_u32::<LittleEndian>(id)?; chain.w32(id)?;
chain.write_u16::<LittleEndian>(event)?; chain.w16(event)?;
chain.write_u16::<LittleEndian>(val)?; chain.w16(val)?;
chain.flush_chain(); chain.flush_chain();
Ok(()) Ok(())
} }
@ -148,11 +146,11 @@ impl Control {
fn send_resize(vq: &mut VirtQueue, id: u32) -> io::Result<()> { fn send_resize(vq: &mut VirtQueue, id: u32) -> io::Result<()> {
let (cols, rows) = Control::stdin_terminal_size()?; let (cols, rows) = Control::stdin_terminal_size()?;
let mut chain = vq.wait_next_chain().unwrap(); let mut chain = vq.wait_next_chain().unwrap();
chain.write_u32::<LittleEndian>(id)?; chain.w32(id)?;
chain.write_u16::<LittleEndian>(VIRTIO_CONSOLE_RESIZE)?; chain.w16(VIRTIO_CONSOLE_RESIZE)?;
chain.write_u16::<LittleEndian>(0)?; chain.w16(0)?;
chain.write_u16::<LittleEndian>(rows)?; chain.w16(rows)?;
chain.write_u16::<LittleEndian>(cols)?; chain.w16(cols)?;
chain.flush_chain(); chain.flush_chain();
Ok(()) Ok(())
} }
@ -171,7 +169,7 @@ impl Control {
} }
struct Terminal { struct Terminal {
saved: Termios, saved: Option<Termios>,
vq: VirtQueue, vq: VirtQueue,
} }
@ -179,17 +177,23 @@ impl Terminal {
fn create(vq: VirtQueue) -> Terminal { fn create(vq: VirtQueue) -> Terminal {
let termios = Termios::from_fd(0).unwrap(); let termios = Termios::from_fd(0).unwrap();
Terminal { Terminal {
saved: termios, saved: Some(termios),
vq, vq,
} }
} }
fn setup_term(&self) { fn setup_term(&self) {
let mut termios = self.saved.clone(); if let Some(mut termios) = self.saved {
termios.c_iflag &= !(ICRNL); termios.c_iflag &= !(ICRNL);
termios.c_lflag &= !(ISIG|ICANON|ECHO); termios.c_lflag &= !(ISIG | ICANON | ECHO);
let _ = tcsetattr(0, TCSANOW, &termios); let _ = tcsetattr(0, TCSANOW, &termios);
} }
}
fn restore_term(&mut self) {
if let Some(termios) = self.saved.take() {
let _ = tcsetattr(0, TCSANOW, &termios);
}
}
fn read_loop(&mut self) { fn read_loop(&mut self) {
self.setup_term(); self.setup_term();
@ -213,7 +217,7 @@ impl Terminal {
} }
if abort_cnt == 3 { if abort_cnt == 3 {
let _ = tcsetattr(0, TCSANOW, &self.saved); self.restore_term();
} }
} }
@ -223,6 +227,6 @@ impl Terminal {
impl Drop for Terminal { impl Drop for Terminal {
fn drop(&mut self) { fn drop(&mut self) {
let _ = tcsetattr(0, TCSANOW, &self.saved); self.restore_term();
} }
} }