77
88//! MCTP Control Protocol implementation
99
10- use mctp :: { AsyncRespChannel , Eid , Error , Listener , MsgType } ;
10+ use crate :: Router ;
1111use libmctp:: control_packet:: CompletionCode ;
12+ use mctp:: { AsyncRespChannel , Eid , Error , Listener , MsgType } ;
1213use uuid:: Uuid ;
13- use crate :: Router ;
1414
1515pub use libmctp:: control_packet:: CommandCode ;
1616
1717type Header = libmctp:: control_packet:: MCTPControlMessageHeader < [ u8 ; 2 ] > ;
1818
1919/// A `Result` with a MCTP Control Completion Code as error
20- pub type ControlResult < T > = core:: result:: Result < T , libmctp:: control_packet:: CompletionCode > ;
20+ pub type ControlResult < T > =
21+ core:: result:: Result < T , libmctp:: control_packet:: CompletionCode > ;
2122
2223pub struct MctpControlMsg < ' a > {
2324 pub header : Header ,
@@ -44,7 +45,10 @@ impl<'a> MctpControlMsg<'a> {
4445 Ok ( Self { header, body } )
4546 }
4647
47- pub fn new_resp < ' f > ( & self , body : & ' f [ u8 ] ) -> ControlResult < MctpControlMsg < ' f > > {
48+ pub fn new_resp < ' f > (
49+ & self ,
50+ body : & ' f [ u8 ] ,
51+ ) -> ControlResult < MctpControlMsg < ' f > > {
4852 if self . header . rq ( ) == 0 {
4953 return Err ( CompletionCode :: ErrorInvalidData ) ;
5054 }
@@ -78,7 +82,12 @@ pub fn respond_get_eid<'a>(
7882 }
7983 // simple endpoint, static EID supported
8084 let endpoint_type = 0b0000_0001 ;
81- let body = [ CompletionCode :: Success as u8 , eid. 0 , endpoint_type, medium_specific] ;
85+ let body = [
86+ CompletionCode :: Success as u8 ,
87+ eid. 0 ,
88+ endpoint_type,
89+ medium_specific,
90+ ] ;
8291
8392 let rsp_buf = & mut rsp_buf[ 0 ..body. len ( ) ] ;
8493 rsp_buf. clone_from_slice ( & body) ;
@@ -94,15 +103,20 @@ pub struct SetEndpointId {
94103
95104pub fn parse_set_eid ( req : & MctpControlMsg ) -> ControlResult < SetEndpointId > {
96105 if req. command_code ( ) != CommandCode :: SetEndpointID {
97- return Err ( CompletionCode :: Error )
106+ return Err ( CompletionCode :: Error ) ;
98107 }
99108 if req. body . len ( ) != 2 {
100- return Err ( CompletionCode :: ErrorInvalidLength )
109+ return Err ( CompletionCode :: ErrorInvalidLength ) ;
101110 }
102111
103- let eid = Eid :: new_normal ( req. body [ 1 ] ) . map_err ( |_| CompletionCode :: ErrorInvalidData ) ?;
112+ let eid = Eid :: new_normal ( req. body [ 1 ] )
113+ . map_err ( |_| CompletionCode :: ErrorInvalidData ) ?;
104114
105- let mut ret = SetEndpointId { eid, force : false , reset : false } ;
115+ let mut ret = SetEndpointId {
116+ eid,
117+ force : false ,
118+ reset : false ,
119+ } ;
106120
107121 match req. body [ 0 ] & 0x03 {
108122 // Set
@@ -126,13 +140,9 @@ pub fn respond_set_eid<'a>(
126140 rsp_buf : & ' a mut [ u8 ] ,
127141) -> ControlResult < MctpControlMsg < ' a > > {
128142 if req. command_code ( ) != CommandCode :: SetEndpointID {
129- return Err ( CompletionCode :: Error )
143+ return Err ( CompletionCode :: Error ) ;
130144 }
131- let status = if accepted {
132- 0b00000000
133- } else {
134- 0b00010000
135- } ;
145+ let status = if accepted { 0b00000000 } else { 0b00010000 } ;
136146 let body = [ CompletionCode :: Success as u8 , status, current_eid. 0 , 0x00 ] ;
137147 let rsp_buf = & mut rsp_buf[ 0 ..body. len ( ) ] ;
138148 rsp_buf. clone_from_slice ( & body) ;
@@ -145,7 +155,7 @@ pub fn respond_get_uuid<'a>(
145155 rsp_buf : & ' a mut [ u8 ] ,
146156) -> ControlResult < MctpControlMsg < ' a > > {
147157 if req. command_code ( ) != CommandCode :: GetEndpointUUID {
148- return Err ( CompletionCode :: Error )
158+ return Err ( CompletionCode :: Error ) ;
149159 }
150160
151161 let mut body = [ 0u8 ; 1 + 16 ] ;
@@ -163,10 +173,10 @@ pub fn respond_get_msg_types<'a>(
163173 rsp_buf : & ' a mut [ u8 ] ,
164174) -> ControlResult < MctpControlMsg < ' a > > {
165175 if req. command_code ( ) != CommandCode :: GetMessageTypeSupport {
166- return Err ( CompletionCode :: Error )
176+ return Err ( CompletionCode :: Error ) ;
167177 }
168178 if !req. body . is_empty ( ) {
169- return Err ( CompletionCode :: ErrorInvalidLength )
179+ return Err ( CompletionCode :: ErrorInvalidLength ) ;
170180 }
171181 let n = msgtypes. len ( ) ;
172182 let body = rsp_buf. get_mut ( ..n + 2 ) . ok_or ( CompletionCode :: Error ) ?;
@@ -194,17 +204,22 @@ pub fn respond_error<'a>(
194204 rsp_buf : & ' a mut [ u8 ] ,
195205) -> mctp:: Result < MctpControlMsg < ' a > > {
196206 if err == CompletionCode :: Success {
197- return Err ( Error :: BadArgument )
207+ return Err ( Error :: BadArgument ) ;
198208 }
199209 let body = [ err as u8 ] ;
200210 let rsp_buf = & mut rsp_buf[ 0 ..body. len ( ) ] ;
201211 rsp_buf. clone_from_slice ( & body) ;
202- req. new_resp ( rsp_buf) . map_err ( |_| mctp:: Error :: InternalError )
212+ req. new_resp ( rsp_buf)
213+ . map_err ( |_| mctp:: Error :: InternalError )
203214}
204215
205- pub fn mctp_control_rx_req < ' f , ' l , L > ( listener : & ' l mut L , buf : & ' f mut [ u8 ] )
206- -> mctp:: Result < ( L :: RespChannel < ' l > , MctpControlMsg < ' f > ) > where L : Listener {
207-
216+ pub fn mctp_control_rx_req < ' f , ' l , L > (
217+ listener : & ' l mut L ,
218+ buf : & ' f mut [ u8 ] ,
219+ ) -> mctp:: Result < ( L :: RespChannel < ' l > , MctpControlMsg < ' f > ) >
220+ where
221+ L : Listener ,
222+ {
208223 let ( buf, ch, _tag, _typ, ic) = listener. recv ( buf) ?;
209224 if ic {
210225 return Err ( Error :: InvalidInput ) ;
@@ -232,8 +247,11 @@ impl<'a> MctpControl<'a> {
232247 }
233248 }
234249
235- pub async fn handle_async ( & mut self , msg : & [ u8 ] , mut resp_chan : impl AsyncRespChannel )
236- -> mctp:: Result < ( ) > {
250+ pub async fn handle_async (
251+ & mut self ,
252+ msg : & [ u8 ] ,
253+ mut resp_chan : impl AsyncRespChannel ,
254+ ) -> mctp:: Result < ( ) > {
237255 let req = MctpControlMsg :: from_buf ( msg)
238256 . map_err ( |_| mctp:: Error :: InvalidInput ) ?;
239257
@@ -242,11 +260,9 @@ impl<'a> MctpControl<'a> {
242260 Ok ( r) => Ok ( r) ,
243261 } ?;
244262
245- resp_chan. send_vectored (
246- mctp:: MCTP_TYPE_CONTROL ,
247- false ,
248- & resp. slices ( )
249- ) . await
263+ resp_chan
264+ . send_vectored ( mctp:: MCTP_TYPE_CONTROL , false , & resp. slices ( ) )
265+ . await
250266 }
251267
252268 pub fn set_message_types ( & mut self , types : & [ MsgType ] ) -> mctp:: Result < ( ) > {
@@ -263,7 +279,10 @@ impl<'a> MctpControl<'a> {
263279 let _ = self . uuid . insert ( * uuid) ;
264280 }
265281
266- async fn handle_req ( & mut self , req : & ' _ MctpControlMsg < ' _ > ) -> ControlResult < MctpControlMsg > {
282+ async fn handle_req (
283+ & mut self ,
284+ req : & ' _ MctpControlMsg < ' _ > ,
285+ ) -> ControlResult < MctpControlMsg > {
267286 let cc = req. command_code ( ) ;
268287
269288 match cc {
@@ -285,12 +304,12 @@ impl<'a> MctpControl<'a> {
285304 Err ( CompletionCode :: ErrorUnsupportedCmd )
286305 }
287306 }
288- CommandCode :: GetMessageTypeSupport => {
289- respond_get_msg_types ( req, self . types . as_slice ( ) , & mut self . rsp_buf )
290- }
291- _ => {
292- Err ( CompletionCode :: ErrorUnsupportedCmd )
293- }
307+ CommandCode :: GetMessageTypeSupport => respond_get_msg_types (
308+ req,
309+ self . types . as_slice ( ) ,
310+ & mut self . rsp_buf ,
311+ ) ,
312+ _ => Err ( CompletionCode :: ErrorUnsupportedCmd ) ,
294313 }
295314 }
296315}
0 commit comments