Skip to content

Commit f297975

Browse files
committed
pldm-file: Implement DfClose
We should be able to close FileDescriptors to free up the host slot. Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
1 parent 682c578 commit f297975

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

pldm-file/examples/client.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use mctp::Eid;
33
use mctp_linux::MctpLinuxAsyncReq;
44
use pldm::control::requester::negotiate_transfer_parameters;
55
use pldm_file::{
6-
client::{df_open, df_properties, df_read},
7-
proto::{DfOpenAttributes, DfProperty, FileIdentifier},
6+
client::{df_close, df_open, df_properties, df_read},
7+
proto::{DfCloseAttributes, DfOpenAttributes, DfProperty, FileIdentifier},
88
};
99

1010
const EID: Eid = Eid(8);
@@ -41,6 +41,11 @@ fn main() -> Result<()> {
4141

4242
println!("Read: {res:?}");
4343

44+
let attrs = DfCloseAttributes::empty();
45+
let res = df_close(&mut req, fd, attrs).await;
46+
47+
println!("Close: {res:?}");
48+
4449
Ok(())
4550
})
4651
}

pldm-file/src/client.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ pub async fn df_open(
7979
Ok(FileDescriptor(ret.file_descriptor))
8080
}
8181

82+
pub async fn df_close(
83+
comm: &mut impl mctp::AsyncReqChannel,
84+
fd: FileDescriptor,
85+
attributes: DfCloseAttributes,
86+
) -> Result<()> {
87+
let req = DfCloseReq {
88+
file_descriptor: fd.0,
89+
attributes: attributes.as_u16(),
90+
};
91+
92+
let mut buf = [0; 4];
93+
let l = req.to_slice(&mut buf).map_err(|_| PldmError::NoSpace)?;
94+
let buf = &buf[..l];
95+
96+
let req = PldmRequest::new_borrowed(
97+
PLDM_TYPE_FILE_TRANSFER,
98+
Cmd::DfClose as u8,
99+
buf,
100+
);
101+
102+
let mut rx = [0; 10];
103+
let resp = pldm_xfer_buf_async(comm, req, &mut rx).await?;
104+
105+
if resp.cc != 0 {
106+
return Err(proto_error!("DfClose failed"));
107+
}
108+
109+
Ok(())
110+
}
111+
82112
const PART_SIZE: usize = 1024;
83113

84114
/// Read a file from the host.

pldm-file/src/host.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<const N: usize> Responder<N> {
122122
let r = match cmd {
123123
Cmd::DfProperties => self.cmd_dfproperties(req, host),
124124
Cmd::DfOpen => self.cmd_dfopen(req, host),
125+
Cmd::DfClose => self.cmd_dfclose(req, host),
125126
_ => {
126127
trace!("unhandled command {cmd:?}");
127128
Err(CCode::ERROR_UNSUPPORTED_PLDM_CMD.into())
@@ -264,6 +265,30 @@ impl<const N: usize> Responder<N> {
264265
Ok(resp)
265266
}
266267

268+
fn cmd_dfclose<'a>(
269+
&mut self,
270+
req: &'a PldmRequest<'a>,
271+
_host: &mut impl Host,
272+
) -> Result<PldmResponse<'a>> {
273+
let (rest, dfc) = DfCloseReq::from_bytes((&req.data, 0))?;
274+
275+
if !rest.0.is_empty() {
276+
Err(CCode::ERROR_INVALID_LENGTH)?;
277+
}
278+
279+
if dfc.attributes != 0 {
280+
Err(file_ccode::ZEROLENGTH_NOT_ALLOWED)?;
281+
}
282+
283+
self.files
284+
.get_mut(dfc.file_descriptor as usize)
285+
.ok_or(file_ccode::INVALID_FILE_DESCRIPTOR)? // valid?
286+
.take()
287+
.ok_or(file_ccode::INVALID_FILE_DESCRIPTOR)?; // open?
288+
289+
Ok(req.response())
290+
}
291+
267292
fn cmd_multipart_receive<'a, const T: usize>(
268293
&mut self,
269294
req: &'a PldmRequest<'a>,

pldm-file/src/proto.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ pub enum DfOpenAttribute {
6060

6161
pub type DfOpenAttributes = EnumSet<DfOpenAttribute>;
6262

63+
#[derive(EnumSetType, Debug)]
64+
pub enum DfCloseAttribute {
65+
DfCloseZeroLength = 0,
66+
}
67+
68+
pub type DfCloseAttributes = EnumSet<DfCloseAttribute>;
69+
6370
impl TryFrom<u32> for DfProperty {
6471
type Error = ();
6572

@@ -92,3 +99,9 @@ pub struct DfOpenReq {
9299
pub struct DfOpenResp {
93100
pub file_descriptor: u16,
94101
}
102+
103+
#[derive(DekuRead, DekuWrite)]
104+
pub struct DfCloseReq {
105+
pub file_descriptor: u16,
106+
pub attributes: u16,
107+
}

0 commit comments

Comments
 (0)