Skip to content

Commit

Permalink
Merge pull request #112 from rcore-os/api-layer-new
Browse files Browse the repository at this point in the history
Add arceos_api between ulib and modules
  • Loading branch information
equation314 authored Aug 6, 2023
2 parents c66cbee + f50b696 commit 59906d1
Show file tree
Hide file tree
Showing 39 changed files with 1,631 additions and 171 deletions.
30 changes: 19 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ members = [
"modules/axtask",

"api/axfeat",
"api/arceos_api",

"ulib/axstd",
"ulib/axlibc",
Expand Down
39 changes: 39 additions & 0 deletions api/arceos_api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "arceos_api"
version = "0.1.0"
edition = "2021"
authors = ["Yuekai Jia <[email protected]>"]
description = "Public APIs and types for ArceOS modules"
license = "GPL-3.0-or-later OR Apache-2.0"
homepage = "https://github.com/rcore-os/arceos"
repository = "https://github.com/rcore-os/arceos/tree/main/api/arceos_api"
documentation = "https://rcore-os.github.io/arceos/arceos_api/index.html"

[features]
default = []

irq = ["axfeat/irq"]
alloc = ["dep:axalloc", "axfeat/alloc"]
multitask = ["axtask/multitask", "axfeat/multitask"]
fs = ["dep:axfs", "axfeat/fs"]
net = ["dep:axnet", "axfeat/net"]
display = ["dep:axdisplay", "axfeat/display"]

myfs = ["axfeat/myfs"]

# Use dummy functions if the feature is not enabled
dummy-if-not-enabled = []

[dependencies]
axfeat = { path = "../axfeat" }
axruntime = { path = "../../modules/axruntime" }
axconfig = { path = "../../modules/axconfig" }
axlog = { path = "../../modules/axlog" }
axio = { path = "../../crates/axio" }
axerrno = { path = "../../crates/axerrno" }
axhal = { path = "../../modules/axhal" }
axalloc = { path = "../../modules/axalloc", optional = true }
axtask = { path = "../../modules/axtask", optional = true }
axfs = { path = "../../modules/axfs", optional = true }
axnet = { path = "../../modules/axnet", optional = true }
axdisplay = { path = "../../modules/axdisplay", optional = true }
11 changes: 11 additions & 0 deletions api/arceos_api/src/imp/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub use axdisplay::DisplayInfo as AxDisplayInfo;

/// Gets the framebuffer information.
pub fn ax_framebuffer_info() -> AxDisplayInfo {
axdisplay::framebuffer_info()
}

/// Flushes the framebuffer, i.e. show on the screen.
pub fn ax_framebuffer_flush() {
axdisplay::framebuffer_flush()
}
87 changes: 87 additions & 0 deletions api/arceos_api/src/imp/fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use alloc::string::String;
use axerrno::AxResult;
use axfs::fops::{Directory, File};

pub use axfs::fops::DirEntry as AxDirEntry;
pub use axfs::fops::FileAttr as AxFileAttr;
pub use axfs::fops::FilePerm as AxFilePerm;
pub use axfs::fops::FileType as AxFileType;
pub use axfs::fops::OpenOptions as AxOpenOptions;
pub use axio::SeekFrom as AxSeekFrom;

#[cfg(feature = "myfs")]
pub use axfs::fops::{Disk as AxDisk, MyFileSystemIf};

/// A handle to an opened file.
pub struct AxFileHandle(File);

/// A handle to an opened directory.
pub struct AxDirHandle(Directory);

pub fn ax_open_file(path: &str, opts: &AxOpenOptions) -> AxResult<AxFileHandle> {
Ok(AxFileHandle(File::open(path, opts)?))
}

pub fn ax_open_dir(path: &str, opts: &AxOpenOptions) -> AxResult<AxDirHandle> {
Ok(AxDirHandle(Directory::open_dir(path, opts)?))
}

pub fn ax_read_file(file: &mut AxFileHandle, buf: &mut [u8]) -> AxResult<usize> {
file.0.read(buf)
}

pub fn ax_read_file_at(file: &AxFileHandle, offset: u64, buf: &mut [u8]) -> AxResult<usize> {
file.0.read_at(offset, buf)
}

pub fn ax_write_file(file: &mut AxFileHandle, buf: &[u8]) -> AxResult<usize> {
file.0.write(buf)
}

pub fn ax_write_file_at(file: &AxFileHandle, offset: u64, buf: &[u8]) -> AxResult<usize> {
file.0.write_at(offset, buf)
}

pub fn ax_truncate_file(file: &AxFileHandle, size: u64) -> AxResult {
file.0.truncate(size)
}

pub fn ax_flush_file(file: &AxFileHandle) -> AxResult {
file.0.flush()
}

pub fn ax_seek_file(file: &mut AxFileHandle, pos: AxSeekFrom) -> AxResult<u64> {
file.0.seek(pos)
}

pub fn ax_file_attr(file: &AxFileHandle) -> AxResult<AxFileAttr> {
file.0.get_attr()
}

pub fn ax_read_dir(dir: &mut AxDirHandle, dirents: &mut [AxDirEntry]) -> AxResult<usize> {
dir.0.read_dir(dirents)
}

pub fn ax_create_dir(path: &str) -> AxResult {
axfs::api::create_dir(path)
}

pub fn ax_remove_dir(path: &str) -> AxResult {
axfs::api::remove_dir(path)
}

pub fn ax_remove_file(path: &str) -> AxResult {
axfs::api::remove_file(path)
}

pub fn ax_rename(old: &str, new: &str) -> AxResult {
axfs::api::rename(old, new)
}

pub fn ax_current_dir() -> AxResult<String> {
axfs::api::current_dir()
}

pub fn ax_set_current_dir(path: &str) -> AxResult {
axfs::api::set_current_dir(path)
}
16 changes: 16 additions & 0 deletions api/arceos_api/src/imp/mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cfg_alloc! {
use core::alloc::Layout;
use core::ptr::NonNull;

pub fn ax_alloc(layout: Layout) -> Option<NonNull<u8>> {
if let Ok(vaddr) = axalloc::global_allocator().alloc(layout) {
Some(unsafe { NonNull::new_unchecked(vaddr.get() as _) })
} else {
None
}
}

pub fn ax_dealloc(ptr: NonNull<u8>, layout: Layout) {
axalloc::global_allocator().dealloc(ptr.addr(), layout)
}
}
42 changes: 42 additions & 0 deletions api/arceos_api/src/imp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
mod mem;
mod task;

cfg_fs! {
mod fs;
pub use fs::*;
}

cfg_net! {
mod net;
pub use net::*;
}

cfg_display! {
mod display;
pub use 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_write_bytes(buf: &[u8]) -> crate::AxResult<usize> {
axhal::console::write_bytes(buf);
Ok(buf.len())
}

pub fn ax_console_write_fmt(args: fmt::Arguments) -> fmt::Result {
axlog::print_fmt(args)
}
}

pub use self::mem::*;
pub use self::stdio::*;
pub use self::task::*;

pub use axhal::misc::terminate as ax_terminate;
pub use axhal::time::{current_time as ax_current_time, TimeValue as AxTimeValue};
pub use axio::PollState as AxPollState;
131 changes: 131 additions & 0 deletions api/arceos_api/src/imp/net.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use crate::io::AxPollState;
use axerrno::AxResult;
use axnet::{UdpSocket, TcpSocket};
use core::net::{IpAddr, SocketAddr};

/// A handle to a TCP socket.
pub struct AxTcpSocketHandle(TcpSocket);

/// A handle to a UDP socket.
pub struct AxUdpSocketHandle(UdpSocket);

////////////////////////////////////////////////////////////////////////////////
// TCP socket
////////////////////////////////////////////////////////////////////////////////

pub fn ax_tcp_socket() -> AxTcpSocketHandle {
AxTcpSocketHandle(TcpSocket::new())
}

pub fn ax_tcp_socket_addr(socket: &AxTcpSocketHandle) -> AxResult<SocketAddr> {
socket.0.local_addr()
}

pub fn ax_tcp_peer_addr(socket: &AxTcpSocketHandle) -> AxResult<SocketAddr> {
socket.0.peer_addr()
}

pub fn ax_tcp_set_nonblocking(socket: &AxTcpSocketHandle, nonblocking: bool) -> AxResult {
socket.0.set_nonblocking(nonblocking);
Ok(())
}

pub fn ax_tcp_connect(socket: &AxTcpSocketHandle, addr: SocketAddr) -> AxResult {
socket.0.connect(addr)
}

pub fn ax_tcp_bind(socket: &AxTcpSocketHandle, addr: SocketAddr) -> AxResult {
socket.0.bind(addr)
}

pub fn ax_tcp_listen(socket: &AxTcpSocketHandle, _backlog: usize) -> AxResult {
socket.0.listen()
}

pub fn ax_tcp_accept(socket: &AxTcpSocketHandle) -> AxResult<(AxTcpSocketHandle, SocketAddr)> {
let new_sock = socket.0.accept()?;
let addr = new_sock.peer_addr()?;
Ok((AxTcpSocketHandle(new_sock), addr))
}

pub fn ax_tcp_send(socket: &AxTcpSocketHandle, buf: &[u8]) -> AxResult<usize> {
socket.0.send(buf)
}

pub fn ax_tcp_recv(socket: &AxTcpSocketHandle, buf: &mut [u8]) -> AxResult<usize> {
socket.0.recv(buf)
}

pub fn ax_tcp_poll(socket: &AxTcpSocketHandle) -> AxResult<AxPollState> {
socket.0.poll()
}

pub fn ax_tcp_shutdown(socket: &AxTcpSocketHandle) -> AxResult {
socket.0.shutdown()
}

////////////////////////////////////////////////////////////////////////////////
// UDP socket
////////////////////////////////////////////////////////////////////////////////

pub fn ax_udp_socket() -> AxUdpSocketHandle {
AxUdpSocketHandle(UdpSocket::new())
}

pub fn ax_udp_socket_addr(socket: &AxUdpSocketHandle) -> AxResult<SocketAddr> {
socket.0.local_addr()
}

pub fn ax_udp_peer_addr(socket: &AxUdpSocketHandle) -> AxResult<SocketAddr> {
socket.0.peer_addr()
}

pub fn ax_udp_set_nonblocking(socket: &AxUdpSocketHandle, nonblocking: bool) -> AxResult {
socket.0.set_nonblocking(nonblocking);
Ok(())
}

pub fn ax_udp_bind(socket: &AxUdpSocketHandle, addr: SocketAddr) -> AxResult {
socket.0.bind(addr)
}

pub fn ax_udp_recv_from(socket: &AxUdpSocketHandle, buf: &mut [u8]) -> AxResult<(usize, SocketAddr)> {
socket.0.recv_from(buf)
}

pub fn ax_udp_peek_from(socket: &AxUdpSocketHandle, buf: &mut [u8]) -> AxResult<(usize, SocketAddr)> {
socket.0.peek_from(buf)
}

pub fn ax_udp_send_to(socket: &AxUdpSocketHandle, buf: &[u8], addr: SocketAddr) -> AxResult<usize> {
socket.0.send_to(buf, addr)
}

pub fn ax_udp_connect(socket: &AxUdpSocketHandle, addr: SocketAddr) -> AxResult {
socket.0.connect(addr)
}

pub fn ax_udp_send(socket: &AxUdpSocketHandle, buf: &[u8]) -> AxResult<usize> {
socket.0.send(buf)
}

pub fn ax_udp_recv(socket: &AxUdpSocketHandle, buf: &mut [u8]) -> AxResult<usize> {
socket.0.recv(buf)
}

pub fn ax_udp_poll(socket: &AxUdpSocketHandle) -> AxResult<AxPollState> {
socket.0.poll()
}

////////////////////////////////////////////////////////////////////////////////
// Miscellaneous
////////////////////////////////////////////////////////////////////////////////

pub fn ax_dns_query(domain_name: &str) -> AxResult<alloc::vec::Vec<IpAddr>> {
axnet::dns_query(domain_name)
}

pub fn ax_poll_interfaces() -> AxResult {
axnet::poll_interfaces();
Ok(())
}
Loading

0 comments on commit 59906d1

Please sign in to comment.