22// Licensed under the Apache License, Version 2.0, see LICENSE for details.
33// SPDX-License-Identifier: Apache-2.0
44
5- use anyhow:: { Result , anyhow} ;
6- use regex:: { Captures , Regex } ;
75use std:: fs:: File ;
86use std:: io:: Write ;
9- use std:: os:: fd:: AsFd ;
107use std:: time:: { Duration , SystemTime } ;
118
12- use tokio:: io:: AsyncReadExt ;
9+ use anyhow:: { Result , anyhow} ;
10+ use regex:: { Captures , Regex } ;
1311
1412use crate :: io:: console:: { ConsoleDevice , ConsoleError } ;
1513
@@ -21,7 +19,6 @@ pub struct UartConsole {
2119 pub timestamp : bool ,
2220 buffer : String ,
2321 newline : bool ,
24- break_en : bool ,
2522}
2623
2724#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -33,13 +30,7 @@ pub enum ExitStatus {
3330 ExitFailure ,
3431}
3532
36- // Creates a vtable for implementors of Read and AsFd traits.
37- pub trait ReadAsFd : tokio:: io:: AsyncRead + AsFd + std:: marker:: Unpin { }
38- impl < T : tokio:: io:: AsyncRead + AsFd + std:: marker:: Unpin > ReadAsFd for T { }
39-
4033impl UartConsole {
41- const CTRL_B : u8 = 2 ;
42- const CTRL_C : u8 = 3 ;
4334 const BUFFER_LEN : usize = 32768 ;
4435
4536 pub fn new (
@@ -55,43 +46,28 @@ impl UartConsole {
5546 timestamp : true ,
5647 buffer : String :: new ( ) ,
5748 newline : true ,
58- break_en : false ,
5949 }
6050 }
6151
6252 // Runs an interactive console until CTRL_C is received.
63- pub fn interact < T > (
64- & mut self ,
65- device : & T ,
66- stdin : Option < & mut dyn ReadAsFd > ,
67- stdout : Option < & mut dyn Write > ,
68- ) -> Result < ExitStatus >
53+ pub fn interact < T > ( & mut self , device : & T , stdout : Option < & mut dyn Write > ) -> Result < ExitStatus >
6954 where
7055 T : ConsoleDevice + ?Sized ,
7156 {
72- crate :: util:: runtime:: block_on ( self . interact_async ( device, stdin , stdout) )
57+ crate :: util:: runtime:: block_on ( self . interact_async ( device, stdout) )
7358 }
7459
7560 // Runs an interactive console until CTRL_C is received. Uses `mio` library to simultaneously
7661 // wait for data from UART or from stdin, without need for timeouts and repeated calls.
77- async fn interact_async < T > (
62+ pub async fn interact_async < T > (
7863 & mut self ,
7964 device : & T ,
80- mut stdin : Option < & mut dyn ReadAsFd > ,
8165 mut stdout : Option < & mut dyn Write > ,
8266 ) -> Result < ExitStatus >
8367 where
8468 T : ConsoleDevice + ?Sized ,
8569 {
86- let mut break_en = self . break_en ;
8770 let timeout = self . timeout ;
88- let tx = async {
89- if let Some ( stdin) = stdin. as_mut ( ) {
90- Self :: process_input ( & mut break_en, device, stdin) . await
91- } else {
92- std:: future:: pending ( ) . await
93- }
94- } ;
9571 let rx = async {
9672 loop {
9773 self . uart_read ( device, & mut stdout) . await ?;
@@ -121,13 +97,10 @@ impl UartConsole {
12197 }
12298 } ;
12399
124- let r = tokio:: select! {
125- v = tx => v,
100+ tokio:: select! {
126101 v = rx => v,
127102 _ = timeout => Ok ( ExitStatus :: Timeout ) ,
128- } ;
129- self . break_en = break_en;
130- r
103+ }
131104 }
132105
133106 /// Returns `true` if any regular expressions are used to match the streamed output. If so,
@@ -185,41 +158,6 @@ impl UartConsole {
185158 Ok ( ( ) )
186159 }
187160
188- async fn process_input < T > (
189- break_en : & mut bool ,
190- device : & T ,
191- stdin : & mut dyn ReadAsFd ,
192- ) -> Result < ExitStatus >
193- where
194- T : ConsoleDevice + ?Sized ,
195- {
196- loop {
197- let mut buf = [ 0u8 ; 256 ] ;
198- let len = stdin. read ( & mut buf) . await ?;
199- if len == 1 {
200- if buf[ 0 ] == UartConsole :: CTRL_C {
201- return Ok ( ExitStatus :: CtrlC ) ;
202- }
203- if buf[ 0 ] == UartConsole :: CTRL_B {
204- * break_en = !* break_en;
205- eprint ! (
206- "\r \n {} break" ,
207- if * break_en { "Setting" } else { "Clearing" }
208- ) ;
209- let b = device. set_break ( * break_en) ;
210- if b. is_err ( ) {
211- eprint ! ( ": {:?}" , b) ;
212- }
213- eprint ! ( "\r \n " ) ;
214- continue ;
215- }
216- }
217- if len > 0 {
218- device. write ( & buf[ ..len] ) ?;
219- }
220- }
221- }
222-
223161 pub fn captures ( & self , status : ExitStatus ) -> Option < Captures < ' _ > > {
224162 match status {
225163 ExitStatus :: ExitSuccess => self
@@ -243,7 +181,7 @@ impl UartConsole {
243181 {
244182 let mut console = UartConsole :: new ( Some ( timeout) , Some ( Regex :: new ( rx) ?) , None ) ;
245183 let mut stdout = std:: io:: stdout ( ) ;
246- let result = console. interact ( device, None , Some ( & mut stdout) ) ?;
184+ let result = console. interact ( device, Some ( & mut stdout) ) ?;
247185 println ! ( ) ;
248186 match result {
249187 ExitStatus :: ExitSuccess => {
0 commit comments