77
88//! MCTP Control Protocol implementation
99
10+ use crate :: fmt:: * ;
1011use crate :: Router ;
1112use libmctp:: control_packet:: CompletionCode ;
1213use mctp:: { AsyncRespChannel , Eid , Error , Listener , MsgType } ;
@@ -63,8 +64,15 @@ impl<'a> MctpControlMsg<'a> {
6364 [ & self . header . 0 , self . body ]
6465 }
6566
66- pub fn command_code ( & self ) -> CommandCode {
67- self . header . command_code ( ) . into ( )
67+ /// Extract the MCTP control message command code.
68+ ///
69+ /// Unrecognised values will return `Err`.
70+ pub fn command_code ( & self ) -> core:: result:: Result < CommandCode , u8 > {
71+ let cc = self . header . command_code ( ) ;
72+ match CommandCode :: from ( cc) {
73+ CommandCode :: Unknown => Err ( cc) ,
74+ cmd => Ok ( cmd) ,
75+ }
6876 }
6977}
7078
@@ -74,7 +82,7 @@ pub fn respond_get_eid<'a>(
7482 medium_specific : u8 ,
7583 rsp_buf : & ' a mut [ u8 ] ,
7684) -> ControlResult < MctpControlMsg < ' a > > {
77- if req. command_code ( ) != CommandCode :: GetEndpointID {
85+ if req. command_code ( ) != Ok ( CommandCode :: GetEndpointID ) {
7886 return Err ( CompletionCode :: Error ) ;
7987 }
8088 if !req. body . is_empty ( ) {
@@ -102,15 +110,17 @@ pub struct SetEndpointId {
102110}
103111
104112pub fn parse_set_eid ( req : & MctpControlMsg ) -> ControlResult < SetEndpointId > {
105- if req. command_code ( ) != CommandCode :: SetEndpointID {
113+ if req. command_code ( ) != Ok ( CommandCode :: SetEndpointID ) {
106114 return Err ( CompletionCode :: Error ) ;
107115 }
108116 if req. body . len ( ) != 2 {
109117 return Err ( CompletionCode :: ErrorInvalidLength ) ;
110118 }
111119
112- let eid = Eid :: new_normal ( req. body [ 1 ] )
113- . map_err ( |_| CompletionCode :: ErrorInvalidData ) ?;
120+ let eid = Eid :: new_normal ( req. body [ 1 ] ) . map_err ( |_| {
121+ warn ! ( "Invalid Set EID {}" , req. body[ 1 ] ) ;
122+ CompletionCode :: ErrorInvalidData
123+ } ) ?;
114124
115125 let mut ret = SetEndpointId {
116126 eid,
@@ -139,7 +149,7 @@ pub fn respond_set_eid<'a>(
139149 current_eid : Eid ,
140150 rsp_buf : & ' a mut [ u8 ] ,
141151) -> ControlResult < MctpControlMsg < ' a > > {
142- if req. command_code ( ) != CommandCode :: SetEndpointID {
152+ if req. command_code ( ) != Ok ( CommandCode :: SetEndpointID ) {
143153 return Err ( CompletionCode :: Error ) ;
144154 }
145155 let status = if accepted { 0b00000000 } else { 0b00010000 } ;
@@ -154,7 +164,7 @@ pub fn respond_get_uuid<'a>(
154164 uuid : Uuid ,
155165 rsp_buf : & ' a mut [ u8 ] ,
156166) -> ControlResult < MctpControlMsg < ' a > > {
157- if req. command_code ( ) != CommandCode :: GetEndpointUUID {
167+ if req. command_code ( ) != Ok ( CommandCode :: GetEndpointUUID ) {
158168 return Err ( CompletionCode :: Error ) ;
159169 }
160170
@@ -172,7 +182,7 @@ pub fn respond_get_msg_types<'a>(
172182 msgtypes : & [ MsgType ] ,
173183 rsp_buf : & ' a mut [ u8 ] ,
174184) -> ControlResult < MctpControlMsg < ' a > > {
175- if req. command_code ( ) != CommandCode :: GetMessageTypeSupport {
185+ if req. command_code ( ) != Ok ( CommandCode :: GetMessageTypeSupport ) {
176186 return Err ( CompletionCode :: Error ) ;
177187 }
178188 if !req. body . is_empty ( ) {
@@ -256,7 +266,10 @@ impl<'a> MctpControl<'a> {
256266 . map_err ( |_| mctp:: Error :: InvalidInput ) ?;
257267
258268 let resp = match self . handle_req ( & req) . await {
259- Err ( e) => respond_error ( & req, e, & mut self . rsp_buf ) ,
269+ Err ( e) => {
270+ debug ! ( "Control error response {:?}" , e) ;
271+ respond_error ( & req, e, & mut self . rsp_buf )
272+ }
260273 Ok ( r) => Ok ( r) ,
261274 } ?;
262275
@@ -283,8 +296,13 @@ impl<'a> MctpControl<'a> {
283296 & mut self ,
284297 req : & ' _ MctpControlMsg < ' _ > ,
285298 ) -> ControlResult < MctpControlMsg > {
286- let cc = req. command_code ( ) ;
299+ let cc = req. command_code ( ) . map_err ( |cc| {
300+ debug ! ( "Unsupported control command {}" , cc) ;
301+ CompletionCode :: ErrorUnsupportedCmd
302+ } ) ?;
287303
304+ #[ cfg( feature = "log" ) ]
305+ debug ! ( "Control request {:?}" , cc) ;
288306 match cc {
289307 CommandCode :: GetEndpointID => {
290308 let eid = self . router . get_eid ( ) . await ;
0 commit comments