44 */
55
66//! A MCTP serial transport binding, DSP0253
7+ //!
8+ //! Supporting both `embedded-io` and `embedded-io-async`.
79
810#[ allow( unused) ]
911use crate :: fmt:: { debug, error, info, trace, warn} ;
@@ -12,10 +14,8 @@ use mctp::{Error, Result};
1214use crc:: Crc ;
1315use heapless:: Vec ;
1416
15- #[ cfg( feature = "async" ) ]
16- use embedded_io_async:: { Read , Write } ;
17+ use embedded_io_async:: { Read as AsyncRead , Write as AsyncWrite } ;
1718
18- #[ cfg( not( feature = "async" ) ) ]
1919use embedded_io:: { Read , Write } ;
2020
2121const MCTP_SERIAL_REVISION : u8 = 0x01 ;
@@ -72,8 +72,10 @@ impl MctpSerialHandler {
7272 /// Read a frame.
7373 ///
7474 /// This is async cancel-safe.
75- #[ cfg( feature = "async" ) ]
76- pub async fn recv_async ( & mut self , input : & mut impl Read ) -> Result < & [ u8 ] > {
75+ pub async fn recv_async (
76+ & mut self ,
77+ input : & mut impl AsyncRead ,
78+ ) -> Result < & [ u8 ] > {
7779 // TODO: This reads one byte a time, might need a buffering wrapper
7880 // for performance. Will require more thought about cancel-safety
7981
@@ -101,13 +103,14 @@ impl MctpSerialHandler {
101103 }
102104 }
103105
104- /// Read a frame synchronously.
105- /// This function blocks until at least one byte is available
106- #[ cfg( not( feature = "async" ) ) ]
107- pub fn recv ( & mut self , input : & mut impl Read ) -> Result < & [ u8 ] > {
108- // TODO: This reads one byte a time, might need a buffering wrapper
109- // for performance. Will require more thought about cancel-safety
110-
106+ /// Read a frame synchronously
107+ ///
108+ /// This function blocks until at least one byte is available.
109+ ///
110+ /// Reads one byte at a time from the [reader](Read).
111+ /// If buffering is needed for performance reasons,
112+ /// this has to be provided by the reader.
113+ pub fn recv_sync ( & mut self , input : & mut impl Read ) -> Result < & [ u8 ] > {
111114 trace ! ( "recv trace" ) ;
112115 loop {
113116 let mut b = 0u8 ;
@@ -220,35 +223,32 @@ impl MctpSerialHandler {
220223 }
221224
222225 /// Asynchronously send a MCTP packet over serial provided by `output`.
223- #[ cfg( feature = "async" ) ]
224226 pub async fn send_async (
225227 & mut self ,
226228 pkt : & [ u8 ] ,
227- output : & mut impl Write ,
229+ output : & mut impl AsyncWrite ,
228230 ) -> Result < ( ) > {
229- Self :: frame_to_serial ( pkt, output)
231+ Self :: frame_to_serial_async ( pkt, output)
230232 . await
231233 . map_err ( |_e| Error :: TxFailure )
232234 }
233235
234236 /// Synchronously send a MCTP packet over serial provided by `output`.
235- #[ cfg( not( feature = "async" ) ) ]
236237 pub fn send_sync (
237238 & mut self ,
238239 pkt : & [ u8 ] ,
239240 output : & mut impl Write ,
240241 ) -> Result < ( ) > {
241- Self :: frame_to_serial ( pkt, output) . map_err ( |_e| Error :: TxFailure )
242+ Self :: frame_to_serial_sync ( pkt, output) . map_err ( |_e| Error :: TxFailure )
242243 }
243244
244245 /// Frame a MCTP packet into a serial frame, writing to `output`.
245- #[ cfg( feature = "async" ) ]
246- async fn frame_to_serial < W > (
246+ async fn frame_to_serial_async < W > (
247247 p : & [ u8 ] ,
248248 output : & mut W ,
249249 ) -> core:: result:: Result < ( ) , W :: Error >
250250 where
251- W : Write ,
251+ W : AsyncWrite ,
252252 {
253253 debug_assert ! ( p. len( ) <= u8 :: MAX . into( ) ) ;
254254 debug_assert ! ( p. len( ) > 4 ) ;
@@ -260,15 +260,14 @@ impl MctpSerialHandler {
260260 let cs = !cs. finalize ( ) ;
261261
262262 output. write_all ( & start) . await ?;
263- Self :: write_escaped ( p, output) . await ?;
263+ Self :: write_escaped_async ( p, output) . await ?;
264264 output. write_all ( & cs. to_be_bytes ( ) ) . await ?;
265265 output. write_all ( & [ FRAMING_FLAG ] ) . await ?;
266266 Ok ( ( ) )
267267 }
268268
269269 /// Frame a MCTP packet into a serial frame, writing to `output`.
270- #[ cfg( not( feature = "async" ) ) ]
271- fn frame_to_serial < W > (
270+ fn frame_to_serial_sync < W > (
272271 p : & [ u8 ] ,
273272 output : & mut W ,
274273 ) -> core:: result:: Result < ( ) , W :: Error >
@@ -285,20 +284,19 @@ impl MctpSerialHandler {
285284 let cs = !cs. finalize ( ) ;
286285
287286 output. write_all ( & start) ?;
288- Self :: write_escaped ( p, output) ?;
287+ Self :: write_escaped_sync ( p, output) ?;
289288 output. write_all ( & cs. to_be_bytes ( ) ) ?;
290289 output. write_all ( & [ FRAMING_FLAG ] ) ?;
291290 Ok ( ( ) )
292291 }
293292
294293 /// Asynchronously write a byte slice to `output`, escaping 0x7e and 0x7d bytes.
295- #[ cfg( feature = "async" ) ]
296- async fn write_escaped < W > (
294+ async fn write_escaped_async < W > (
297295 p : & [ u8 ] ,
298296 output : & mut W ,
299297 ) -> core:: result:: Result < ( ) , W :: Error >
300298 where
301- W : Write ,
299+ W : AsyncWrite ,
302300 {
303301 for c in
304302 p. split_inclusive ( |& b| b == FRAMING_FLAG || b == FRAMING_ESCAPE )
@@ -320,8 +318,7 @@ impl MctpSerialHandler {
320318 }
321319
322320 /// Synchronously write a byte slice to `output`, escaping 0x7e and 0x7d bytes.
323- #[ cfg( not( feature = "async" ) ) ]
324- fn write_escaped < W > (
321+ fn write_escaped_sync < W > (
325322 p : & [ u8 ] ,
326323 output : & mut W ,
327324 ) -> core:: result:: Result < ( ) , W :: Error >
@@ -360,7 +357,6 @@ mod tests {
360357 use crate :: * ;
361358 use proptest:: prelude:: * ;
362359
363- #[ cfg( feature = "async" ) ]
364360 use embedded_io_adapters:: futures_03:: FromFutures ;
365361
366362 static TEST_DATA_ROUNTRIP : [ & [ u8 ] ; 1 ] =
@@ -373,11 +369,10 @@ mod tests {
373369 . try_init ( ) ;
374370 }
375371
376- #[ cfg( feature = "async" ) ]
377372 async fn do_roundtrip_async ( payload : & [ u8 ] ) {
378373 let mut esc = vec ! [ ] ;
379374 let mut s = FromFutures :: new ( & mut esc) ;
380- MctpSerialHandler :: frame_to_serial ( payload, & mut s)
375+ MctpSerialHandler :: frame_to_serial_async ( payload, & mut s)
381376 . await
382377 . unwrap ( ) ;
383378
@@ -391,22 +386,20 @@ mod tests {
391386 debug_assert_eq ! ( payload, packet) ;
392387 }
393388
394- #[ cfg( not( feature = "async" ) ) ]
395389 fn do_roundtrip_sync ( payload : & [ u8 ] ) {
396390 start_log ( ) ;
397391 let mut esc = vec ! [ ] ;
398- MctpSerialHandler :: frame_to_serial ( payload, & mut esc) . unwrap ( ) ;
392+ MctpSerialHandler :: frame_to_serial_sync ( payload, & mut esc) . unwrap ( ) ;
399393 debug ! ( "payload {:02x?}" , payload) ;
400394 debug ! ( "esc {:02x?}" , esc) ;
401395
402396 let mut h = MctpSerialHandler :: new ( ) ;
403397 let mut s = esc. as_slice ( ) ;
404- let packet = h. recv ( & mut s) . unwrap ( ) ;
398+ let packet = h. recv_sync ( & mut s) . unwrap ( ) ;
405399 debug ! ( "packet {:02x?}" , packet) ;
406400 debug_assert_eq ! ( payload, packet) ;
407401 }
408402
409- #[ cfg( feature = "async" ) ]
410403 #[ test]
411404 fn roundtrip_cases_async ( ) {
412405 // Fixed testcases
@@ -418,7 +411,6 @@ mod tests {
418411 } )
419412 }
420413
421- #[ cfg( not( feature = "async" ) ) ]
422414 #[ test]
423415 fn roundtrip_cases_sync ( ) {
424416 start_log ( ) ;
@@ -428,14 +420,12 @@ mod tests {
428420 }
429421
430422 proptest ! {
431- #[ cfg( feature = "async" ) ]
432423 #[ test]
433424 fn roundtrip_escape_async( payload in proptest:: collection:: vec( 0 ..255u8 , 5 ..20 ) ) {
434425 start_log( ) ;
435426 smol:: block_on( do_roundtrip_async( & payload) )
436427 }
437428
438- #[ cfg( not( feature = "async" ) ) ]
439429 #[ test]
440430 fn roundtrip_escape_sync( payload in proptest:: collection:: vec( 0 ..255u8 , 5 ..20 ) ) {
441431 start_log( ) ;
0 commit comments