sel4_shared_ring_buffer_block_io_types/
lib.rs

1//
2// Copyright 2023, Colias Group, LLC
3//
4// SPDX-License-Identifier: BSD-2-Clause
5//
6
7#![no_std]
8
9use num_enum::{IntoPrimitive, TryFromPrimitive, TryFromPrimitiveError};
10use zerocopy::{FromBytes, Immutable, IntoBytes};
11
12use sel4_shared_ring_buffer::Descriptor;
13
14#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, FromBytes, IntoBytes, Immutable)]
15#[repr(C)]
16pub struct BlockIORequest {
17    status: i32,
18    ty: u32,
19    start_block_idx: u64,
20    buf: Descriptor,
21}
22
23#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
24#[repr(u32)]
25pub enum BlockIORequestType {
26    Read = 0,
27    Write = 1,
28}
29
30#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
31#[repr(i32)]
32pub enum BlockIORequestStatus {
33    Pending = -1,
34    Ok = 0,
35    IOError = 1,
36}
37
38impl BlockIORequest {
39    pub fn new(
40        status: BlockIORequestStatus,
41        ty: BlockIORequestType,
42        start_block_idx: u64,
43        buf: Descriptor,
44    ) -> Self {
45        Self {
46            status: status.into(),
47            ty: ty.into(),
48            start_block_idx,
49            buf,
50        }
51    }
52
53    pub fn status(
54        &self,
55    ) -> Result<BlockIORequestStatus, TryFromPrimitiveError<BlockIORequestStatus>> {
56        self.status.try_into()
57    }
58
59    pub fn set_status(&mut self, status: BlockIORequestStatus) {
60        self.status = status.into();
61    }
62
63    pub fn ty(&self) -> Result<BlockIORequestType, TryFromPrimitiveError<BlockIORequestType>> {
64        self.ty.try_into()
65    }
66
67    pub fn set_ty(&mut self, ty: BlockIORequestType) {
68        self.ty = ty.into();
69    }
70
71    pub fn start_block_idx(&self) -> u64 {
72        self.start_block_idx
73    }
74
75    pub fn set_start_block_idx(&mut self, start_block_idx: u64) {
76        self.start_block_idx = start_block_idx;
77    }
78
79    pub fn buf(&self) -> &Descriptor {
80        &self.buf
81    }
82
83    pub fn buf_mut(&mut self) -> &mut Descriptor {
84        &mut self.buf
85    }
86}