Convert ErrorCode to a usize to match Tock 2.0 semantics and add constants for known error codes. Add ReturnType, a Tock 2.0 construct.
diff --git a/core/platform/src/error_code.rs b/core/platform/src/error_code.rs index ca1c657..99fa474 100644 --- a/core/platform/src/error_code.rs +++ b/core/platform/src/error_code.rs
@@ -1,25 +1,36 @@ -/// An error code returned by the kernel. Tock's system calls return errors as a -/// negative `isize`. This wraps the isize, and is useful for adding type safety -/// to APIs. - +/// A system call error code. This can either be an error code returned by the +/// kernel or BADRVAL, which indicates the kernel returned the wrong type of +/// response to a system call. +// ErrorCode is not an enum so that conversion from the kernel's return value (a +// `usize` in a register) is free. #[derive(Clone, Copy, PartialEq, Eq)] pub struct ErrorCode { - // Note: value *should* always be negative, but we do not *verify* that so - // unsafe code cannot rely on value being negative. - value: isize, + value: usize, } -impl ErrorCode { - // Converts the given isize into an ErrorCode. Note that the isize should be - // negative, although that is not verified to reduce code size. We don't - // implement From because not every isize converts sensibly to an ErrorCode. - pub fn new(value: isize) -> ErrorCode { +impl From<usize> for ErrorCode { + fn from(value: usize) -> ErrorCode { ErrorCode { value } } } -impl Into<isize> for ErrorCode { - fn into(self) -> isize { - self.value +impl From<ErrorCode> for usize { + fn from(error_code: ErrorCode) -> usize { + error_code.value } } + +pub const FAIL: ErrorCode = ErrorCode { value: 1 }; +pub const BUSY: ErrorCode = ErrorCode { value: 2 }; +pub const ALREADY: ErrorCode = ErrorCode { value: 3 }; +pub const OFF: ErrorCode = ErrorCode { value: 4 }; +pub const RESERVE: ErrorCode = ErrorCode { value: 5 }; +pub const INVALID: ErrorCode = ErrorCode { value: 6 }; +pub const SIZE: ErrorCode = ErrorCode { value: 7 }; +pub const CANCEL: ErrorCode = ErrorCode { value: 8 }; +pub const NOMEM: ErrorCode = ErrorCode { value: 9 }; +pub const NOSUPPORT: ErrorCode = ErrorCode { value: 10 }; +pub const NODEVICE: ErrorCode = ErrorCode { value: 11 }; +pub const UNINSTALLED: ErrorCode = ErrorCode { value: 12 }; +pub const NOACK: ErrorCode = ErrorCode { value: 13 }; +pub const BADRVAL: ErrorCode = ErrorCode { value: 1024 };
diff --git a/core/platform/src/lib.rs b/core/platform/src/lib.rs index 0a9f936..9343145 100644 --- a/core/platform/src/lib.rs +++ b/core/platform/src/lib.rs
@@ -1,12 +1,14 @@ #![no_std] mod async_traits; -mod error_code; +pub mod error_code; mod raw_syscalls; +pub mod return_type; mod syscalls; mod syscalls_impl; pub use async_traits::{CallbackContext, FreeCallback, Locator, MethodCallback}; pub use error_code::ErrorCode; pub use raw_syscalls::{OneArgMemop, RawSyscalls, YieldType, ZeroArgMemop}; +pub use return_type::ReturnType; pub use syscalls::Syscalls;
diff --git a/core/platform/src/return_type.rs b/core/platform/src/return_type.rs new file mode 100644 index 0000000..5d491b2 --- /dev/null +++ b/core/platform/src/return_type.rs
@@ -0,0 +1,29 @@ +/// `ReturnType` describes what value type the kernel has returned. +// ReturnType is not an enum so that it can be converted from a u32 for free. +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct ReturnType { + value: u32, +} + +impl From<u32> for ReturnType { + fn from(value: u32) -> ReturnType { + ReturnType { value } + } +} + +impl From<ReturnType> for u32 { + fn from(return_type: ReturnType) -> u32 { + return_type.value + } +} + +pub const FAILURE: ReturnType = ReturnType { value: 0 }; +pub const FAILURE_U32: ReturnType = ReturnType { value: 1 }; +pub const FAILURE_2_U32: ReturnType = ReturnType { value: 2 }; +pub const FAILURE_U64: ReturnType = ReturnType { value: 3 }; +pub const SUCCESS: ReturnType = ReturnType { value: 128 }; +pub const SUCCESS_U32: ReturnType = ReturnType { value: 129 }; +pub const SUCCESS_2_U32: ReturnType = ReturnType { value: 130 }; +pub const SUCCESS_U64: ReturnType = ReturnType { value: 131 }; +pub const SUCCESS_3_U32: ReturnType = ReturnType { value: 132 }; +pub const SUCCESS_U32_U64: ReturnType = ReturnType { value: 133 };
diff --git a/core/platform/src/syscalls_impl.rs b/core/platform/src/syscalls_impl.rs index 0af15c5..8f24467 100644 --- a/core/platform/src/syscalls_impl.rs +++ b/core/platform/src/syscalls_impl.rs
@@ -1,6 +1,6 @@ //! Implements `Syscalls` for all types that implement `RawSyscalls`. -use crate::{RawSyscalls, Syscalls, YieldType}; +use crate::{return_type, RawSyscalls, Syscalls, YieldType}; impl<S: RawSyscalls> Syscalls for S { // ------------------------------------------------------------------------- @@ -12,7 +12,6 @@ } fn yield_no_wait() -> bool { - // TODO: Introduce a return type abstraction so this 0 isn't hardcoded. - Self::raw_yield(YieldType::NoWait) != 0 + Self::raw_yield(YieldType::NoWait) != return_type::FAILURE.into() } }