@@ -58,13 +58,14 @@ const CTRL_B: u8 = 2;
5858const CTRL_C : u8 = 3 ;
5959
6060/// Takes input from an input stream and send it to a console device. Breaks are handled.
61- async fn process_input < T , R : AsyncRead + Unpin > ( device : & T , stdin : & mut R ) -> Result < ( ) >
61+ async fn process_input < T , R > ( device : & T , stdin : & mut R ) -> Result < ( ) >
6262where
6363 T : Uart + ?Sized ,
64+ R : AsyncRead + Unpin ,
6465{
6566 let mut break_en = false ;
67+ let mut buf = [ 0u8 ; 256 ] ;
6668 loop {
67- let mut buf = [ 0u8 ; 256 ] ;
6869 let len = stdin. read ( & mut buf) . await ?;
6970 if len == 1 {
7071 if buf[ 0 ] == CTRL_C {
8485 continue ;
8586 }
8687 }
87- if len > 0 {
88- device. write ( & buf[ ..len] ) ?;
89- }
88+ device. write ( & buf[ ..len] ) ?;
9089 }
9190}
9291
@@ -99,6 +98,12 @@ impl CommandDispatch for Console {
9998 // We need the UART for the console command to operate.
10099 transport. capabilities ( ) ?. request ( Capability :: UART ) . ok ( ) ?;
101100
101+ let uart = self . params . create ( transport) ?;
102+ if let Some ( send) = self . send . as_ref ( ) {
103+ log:: info!( "Sending: {:?}" , send) ;
104+ uart. write ( send. as_bytes ( ) ) ?;
105+ }
106+
102107 // Set up resources specified by the command line parameters.
103108 let mut console = UartConsole :: new (
104109 self . timeout ,
@@ -114,45 +119,45 @@ impl CommandDispatch for Console {
114119 console. logfile = self . logfile . as_ref ( ) . map ( File :: create) . transpose ( ) ?;
115120 console. timestamp = self . timestamp ;
116121
117- let status = {
118- // Put the terminal into raw mode. The tty guard will restore the
119- // console settings when it goes out of scope.
120- let mut stdin = if self . non_interactive {
121- None
122- } else {
123- Some ( RawTty :: new ( tokio:: io:: stdin ( ) ) ?)
124- } ;
125- let mut stdout = std:: io:: stdout ( ) ;
126-
127- let uart = self . params . create ( transport) ?;
128- if let Some ( send) = self . send . as_ref ( ) {
129- log:: info!( "Sending: {:?}" , send) ;
130- uart. write ( send. as_bytes ( ) ) ?;
131- }
132- if !self . non_interactive {
133- eprint ! ( "Starting interactive console\r \n " ) ;
134- eprint ! ( "[CTRL+C] to exit.\r \n \r \n " ) ;
135- }
122+ // Put the terminal into raw mode. The tty guard will restore the
123+ // console settings when it goes out of scope.
124+ let mut stdin = if self . non_interactive {
125+ None
126+ } else {
127+ Some ( RawTty :: new ( tokio:: io:: stdin ( ) ) ?)
128+ } ;
129+
130+ if !self . non_interactive {
131+ eprint ! ( "Starting interactive console\r \n " ) ;
132+ eprint ! ( "[CTRL+C] to exit.\r \n \r \n " ) ;
133+ }
134+
135+ let status = transport. relinquish_exclusive_access ( || {
136+ opentitanlib:: util:: runtime:: block_on ( async {
137+ let tx = async {
138+ if let Some ( stdin) = stdin. as_mut ( ) {
139+ process_input ( & * uart, stdin) . await
140+ } else {
141+ std:: future:: pending ( ) . await
142+ }
143+ } ;
144+
145+ let rx = async {
146+ console
147+ . interact_async ( & * uart, Some ( & mut std:: io:: stdout ( ) ) )
148+ . await
149+ } ;
136150
137- transport. relinquish_exclusive_access ( || {
138- opentitanlib:: util:: runtime:: block_on ( async {
139- let tx = async {
140- if let Some ( stdin) = stdin. as_mut ( ) {
141- process_input ( & * uart, stdin) . await
142- } else {
143- std:: future:: pending ( ) . await
144- }
145- } ;
146-
147- let rx = console. interact_async ( & * uart, Some ( & mut stdout) ) ;
148-
149- Result :: < _ > :: Ok ( tokio:: select! {
150- v = tx => Err ( v?) ,
151- v = rx => Ok ( v?) ,
152- } )
151+ Result :: < _ > :: Ok ( tokio:: select! {
152+ v = tx => Err ( v?) ,
153+ v = rx => Ok ( v?) ,
153154 } )
154- } ) ??
155- } ;
155+ } )
156+ } ) ??;
157+
158+ // Bring us out from raw mode early.
159+ drop ( stdin) ;
160+
156161 if !self . non_interactive {
157162 eprintln ! ( "\n \n Exiting interactive console." ) ;
158163 }
0 commit comments