Skip to content

Commit

Permalink
[refactor] use ax_console_read_bytes to replace ax_console_read_byte
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Oct 26, 2024
1 parent 746a8f4 commit 4ad6a48
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 30 deletions.
10 changes: 8 additions & 2 deletions api/arceos_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ cfg_display! {
mod stdio {
use core::fmt;

pub fn ax_console_read_byte() -> Option<u8> {
axhal::console::getchar().map(|c| if c == b'\r' { b'\n' } else { c })
pub fn ax_console_read_bytes(buf: &mut [u8]) -> crate::AxResult<usize> {
let len = axhal::console::read_bytes(buf);
for c in &mut buf[..len] {
if *c == b'\r' {
*c = b'\n';
}
}
Ok(len)
}

pub fn ax_console_write_bytes(buf: &[u8]) -> crate::AxResult<usize> {
Expand Down
4 changes: 2 additions & 2 deletions api/arceos_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ pub mod mem {
pub mod stdio {
use core::fmt;
define_api! {
/// Reads a byte from the console, or returns [`None`] if no input is available.
pub fn ax_console_read_byte() -> Option<u8>;
/// Reads a slice of bytes from the console, returns the number of bytes written.
pub fn ax_console_read_bytes(buf: &mut [u8]) -> crate::AxResult<usize>;
/// Writes a slice of bytes to the console, returns the number of bytes written.
pub fn ax_console_write_bytes(buf: &[u8]) -> crate::AxResult<usize>;
/// Writes a formatted string to the console.
Expand Down
17 changes: 11 additions & 6 deletions api/arceos_posix_api/src/imp/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ use axsync::Mutex;
#[cfg(feature = "fd")]
use {alloc::sync::Arc, axerrno::LinuxError, axerrno::LinuxResult, axio::PollState};

fn console_read_bytes() -> Option<u8> {
axhal::console::getchar().map(|c| if c == b'\r' { b'\n' } else { c })
fn console_read_bytes(buf: &mut [u8]) -> AxResult<usize> {
let len = axhal::console::read_bytes(buf);
for c in &mut buf[..len] {
if *c == b'\r' {
*c = b'\n';
}
}
Ok(len)
}

fn console_write_bytes(buf: &[u8]) -> AxResult<usize> {
Expand All @@ -22,12 +28,11 @@ impl Read for StdinRaw {
fn read(&mut self, buf: &mut [u8]) -> AxResult<usize> {
let mut read_len = 0;
while read_len < buf.len() {
if let Some(c) = console_read_bytes() {
buf[read_len] = c;
read_len += 1;
} else {
let len = console_read_bytes(buf[read_len..].as_mut())?;
if len == 0 {
break;
}
read_len += len;
}
Ok(read_len)
}
Expand Down
7 changes: 0 additions & 7 deletions modules/axhal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ pub mod paging;
/// Console input and output.
pub mod console {
pub use super::platform::console::*;

/// Write a slice of bytes to the console.
pub fn write_bytes(bytes: &[u8]) {
for c in bytes {
putchar(*c);
}
}
}

/// Miscellaneous operation, e.g. terminate the system.
Expand Down
22 changes: 22 additions & 0 deletions modules/axhal/src/platform/aarch64_bsta1000b/dw_apb_uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ pub fn putchar(c: u8) {
}
}

/// Write a slice of bytes to the console.
pub fn write_bytes(bytes: &[u8]) {
for c in bytes {
putchar(*c);
}
}

/// Reads bytes from the console into the given mutable slice.
/// Returns the number of bytes read.
pub fn read_bytes(bytes: &mut [u8]) -> usize {
let mut read_len = 0;
while read_len < bytes.len() {
if let Some(c) = getchar() {
bytes[read_len] = c;
} else {
break;
}
read_len += 1;
}
read_len
}

/// Reads a byte from the console, or returns [`None`] if no input is available.
pub fn getchar() -> Option<u8> {
UART.lock().getchar()
Expand Down
22 changes: 22 additions & 0 deletions modules/axhal/src/platform/aarch64_common/pl011.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ pub fn putchar(c: u8) {
}
}

/// Write a slice of bytes to the console.
pub fn write_bytes(bytes: &[u8]) {
for c in bytes {
putchar(*c);
}
}

/// Reads bytes from the console into the given mutable slice.
/// Returns the number of bytes read.
pub fn read_bytes(bytes: &mut [u8]) -> usize {
let mut read_len = 0;
while read_len < bytes.len() {
if let Some(c) = getchar() {
bytes[read_len] = c;
} else {
break;
}
read_len += 1;
}
read_len
}

/// Reads a byte from the console, or returns [`None`] if no input is available.
pub fn getchar() -> Option<u8> {
UART.lock().getchar()
Expand Down
11 changes: 11 additions & 0 deletions modules/axhal/src/platform/dummy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ pub mod console {
pub fn getchar() -> Option<u8> {
unimplemented!()
}

/// Writes bytes to the console from input u8 slice.
pub fn write_bytes(_bytes: &[u8]) {
unimplemented!()
}

/// Reads bytes from the console into the given mutable slice.
/// Returns the number of bytes read.
pub fn read_bytes(_bytes: &mut [u8]) -> usize {
unimplemented!()
}
}

pub mod misc {
Expand Down
24 changes: 15 additions & 9 deletions modules/axhal/src/platform/riscv64_qemu_virt/console.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::ptr::addr_of;

use memory_addr::VirtAddr;

use crate::mem::virt_to_phys;
Expand All @@ -9,14 +7,22 @@ pub fn putchar(c: u8) {
sbi_rt::console_write_byte(c);
}

/// Reads a byte from the console, or returns [`None`] if no input is available.
pub fn getchar() -> Option<u8> {
let c: u8 = 0;
/// Writes bytes to the console from input u8 slice.
pub fn write_bytes(bytes: &[u8]) {
sbi_rt::console_write(sbi_rt::Physical::new(
bytes.len(),
virt_to_phys(VirtAddr::from_ptr_of(bytes.as_ptr())).as_usize(),
0,
));
}

/// Reads bytes from the console into the given mutable slice.
/// Returns the number of bytes read.
pub fn read_bytes(bytes: &mut [u8]) -> usize {
sbi_rt::console_read(sbi_rt::Physical::new(
1,
virt_to_phys(VirtAddr::from_ptr_of(addr_of!(c))).as_usize(),
bytes.len(),
virt_to_phys(VirtAddr::from_mut_ptr_of(bytes.as_mut_ptr())).as_usize(),
0,
))
.ok()
.map(|_| c)
.value
}
22 changes: 22 additions & 0 deletions modules/axhal/src/platform/x86_pc/uart16550.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ pub fn getchar() -> Option<u8> {
COM1.lock().getchar()
}

/// Write a slice of bytes to the console.
pub fn write_bytes(bytes: &[u8]) {
for c in bytes {
putchar(*c);
}
}

/// Reads bytes from the console into the given mutable slice.
/// Returns the number of bytes read.
pub fn read_bytes(bytes: &mut [u8]) -> usize {
let mut read_len = 0;
while read_len < bytes.len() {
if let Some(c) = getchar() {
bytes[read_len] = c;
} else {
break;
}
read_len += 1;
}
read_len
}

pub(super) fn init() {
COM1.lock().init(115200);
}
7 changes: 3 additions & 4 deletions ulib/axstd/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ impl Read for StdinRaw {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut read_len = 0;
while read_len < buf.len() {
if let Some(c) = arceos_api::stdio::ax_console_read_byte() {
buf[read_len] = c;
read_len += 1;
} else {
let len = arceos_api::stdio::ax_console_read_bytes(buf[read_len..].as_mut())?;
if len == 0 {
break;
}
read_len += len;
}
Ok(read_len)
}
Expand Down

0 comments on commit 4ad6a48

Please sign in to comment.