wapo_env/
ocall_def.rs

1use super::*;
2use crate::args_stack::{I32Convertible, RetDecode, StackedArgs};
3use crate::tls::{TlsClientConfig, TlsServerConfig};
4use std::borrow::Cow;
5
6/// All ocall definitions for wapo.
7#[wapo_macro::ocall]
8pub trait OcallFuncs {
9    /// Close given resource by id.
10    #[ocall(id = 101)]
11    fn close(resource_id: i32) -> Result<()>;
12
13    /// Poll given resource by id and return a dynamic sized data.
14    #[ocall(id = 102, encode_output)]
15    fn poll(waker_id: i32, resource_id: i32) -> Result<Vec<u8>>;
16
17    /// Poll given resource to read data. Low level support for AsyncRead.
18    #[ocall(id = 103)]
19    fn poll_read(waker_id: i32, resource_id: i32, data: &mut [u8]) -> Result<u32>;
20
21    /// Poll given resource to write data. Low level support for AsyncWrite.
22    #[ocall(id = 104)]
23    fn poll_write(waker_id: i32, resource_id: i32, data: &[u8]) -> Result<u32>;
24
25    /// Shutdown a socket
26    #[ocall(id = 105)]
27    fn poll_shutdown(waker_id: i32, resource_id: i32) -> Result<()>;
28
29    /// Poll given resource to generate a new resource id.
30    #[ocall(id = 106)]
31    fn poll_res(waker_id: i32, resource_id: i32) -> Result<i32>;
32
33    /// Mark a task as ready for next polling
34    #[ocall(id = 109)]
35    fn mark_task_ready(task_id: i32) -> Result<()>;
36
37    /// Get the next waken up task id.
38    #[ocall(id = 110)]
39    fn next_ready_task() -> Result<i32>;
40
41    /// Enable logging for ocalls
42    #[ocall(id = 111)]
43    fn enable_ocall_trace(enable: bool) -> Result<()>;
44
45    /// Get awake wakers
46    #[ocall(id = 112, encode_output)]
47    fn awake_wakers() -> Result<Vec<i32>>;
48
49    /// Get random number
50    #[ocall(id = 113)]
51    fn getrandom(buf: &mut [u8]) -> Result<()>;
52
53    /// Create a timer given a duration of time in milliseconds.
54    #[ocall(id = 201)]
55    fn create_timer(timeout: i32) -> Result<i32>;
56
57    /// Send data to a oneshot channel.
58    #[ocall(id = 202)]
59    fn oneshot_send(resource_id: i32, data: &[u8]) -> Result<()>;
60
61    /// Create a TCP socket, bind to given address and listen to incoming connections.
62    ///
63    /// If `tls_config` is not `None`, then the socket will be TLS encrypted.
64    /// Invoke tcp_accept on the returned resource_id to accept incoming connections.
65    #[ocall(id = 210, encode_input)]
66    fn tcp_listen(addr: Cow<str>, tls_config: Option<TlsServerConfig>) -> Result<i32>;
67
68    /// Accept incoming TCP connections.
69    #[ocall(id = 211, encode_output)]
70    fn tcp_accept(waker_id: i32, resource_id: i32) -> Result<(i32, String)>;
71
72    /// Accept incoming TCP connections without returning the remote address.
73    #[ocall(id = 212)]
74    fn tcp_accept_no_addr(waker_id: i32, resource_id: i32) -> Result<i32>;
75
76    /// Initiate a TCP connection to a remote endpoint.
77    #[ocall(id = 213)]
78    fn tcp_connect(host: &str, port: u16) -> Result<i32>;
79
80    /// Initiate a TLS/TCP connection to a remote endpoint.
81    #[ocall(id = 214, encode_input)]
82    fn tcp_connect_tls(host: String, port: u16, config: TlsClientConfig) -> Result<i32>;
83
84    /// Print log message.
85    #[ocall(id = 220)]
86    fn log(level: log::Level, message: &str) -> Result<()>;
87
88    /// Create input channel
89    #[ocall(id = 240, encode_output)]
90    fn create_input_channel(ch: InputChannel) -> Result<i32>;
91
92    /// Returns the vmid of the current instance.
93    #[ocall(id = 242, encode_output)]
94    fn vmid() -> Result<[u8; 32]>;
95
96    /// Emit program output.
97    #[ocall(id = 243)]
98    fn emit_program_output(output: &[u8]) -> Result<()>;
99}
100
101#[repr(u8)]
102#[derive(Clone, Copy, Debug, PartialEq, Eq)]
103pub enum InputChannel {
104    /// Input channel for queries from external RPC requests.
105    Query = 3,
106    /// Input channel for incoming HTTP requests.
107    HttpRequest = 4,
108}
109
110impl I32Convertible for InputChannel {
111    fn to_i32(&self) -> i32 {
112        *self as i32
113    }
114    fn from_i32(i: i32) -> Result<Self> {
115        match i {
116            3 => Ok(InputChannel::Query),
117            4 => Ok(InputChannel::HttpRequest),
118            _ => Err(OcallError::InvalidParameter),
119        }
120    }
121}