@@ -11,18 +11,18 @@ use super::{ADBTransportMessage, MessageCommand, models::MessageSubcommand};
1111#[ derive( Debug ) ]  
1212pub  struct  ADBMessageDevice < T :  ADBMessageTransport >  { 
1313    transport :  T , 
14-     local_id :  Option < u32 > , 
15-     remote_id :  Option < u32 > , 
14+ } 
15+ 
16+ #[ derive( Debug ,  Clone ,  Copy ) ]  
17+ pub  struct  ADBSession  { 
18+     pub  local_id :  u32 , 
19+     pub  remote_id :  u32 , 
1620} 
1721
1822impl < T :  ADBMessageTransport >  ADBMessageDevice < T >  { 
1923    /// Instantiate a new [`ADBMessageTransport`] 
2024pub  fn  new ( transport :  T )  -> Self  { 
21-         Self  { 
22-             transport, 
23-             local_id :  None , 
24-             remote_id :  None , 
25-         } 
25+         Self  {  transport } 
2626    } 
2727
2828    pub ( crate )  fn  get_transport ( & mut  self )  -> & T  { 
@@ -34,12 +34,15 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
3434    } 
3535
3636    /// Receive a message and acknowledge it by replying with an `OKAY` command 
37- pub ( crate )  fn  recv_and_reply_okay ( & mut  self )  -> Result < ADBTransportMessage >  { 
37+ pub ( crate )  fn  recv_and_reply_okay ( 
38+         & mut  self , 
39+         session :  ADBSession , 
40+     )  -> Result < ADBTransportMessage >  { 
3841        let  message = self . transport . read_message ( ) ?; 
3942        self . transport . write_message ( ADBTransportMessage :: new ( 
4043            MessageCommand :: Okay , 
41-             self . get_local_id ( ) ? , 
42-             self . get_remote_id ( ) ? , 
44+             session . local_id , 
45+             session . remote_id , 
4346            & [ ] , 
4447        ) ) ?; 
4548        Ok ( message) 
@@ -60,11 +63,12 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
6063
6164    pub ( crate )  fn  recv_file < W :  std:: io:: Write > ( 
6265        & mut  self , 
66+         session :  ADBSession , 
6367        mut  output :  W , 
6468    )  -> std:: result:: Result < ( ) ,  RustADBError >  { 
6569        let  mut  len:  Option < u64 >  = None ; 
6670        loop  { 
67-             let  payload = self . recv_and_reply_okay ( ) ?. into_payload ( ) ; 
71+             let  payload = self . recv_and_reply_okay ( session ) ?. into_payload ( ) ; 
6872            let  mut  rdr = Cursor :: new ( & payload) ; 
6973            while  rdr. position ( )  != payload. len ( )  as  u64  { 
7074                match  len. take ( )  { 
@@ -97,8 +101,7 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
97101
98102    pub ( crate )  fn  push_file < R :  std:: io:: Read > ( 
99103        & mut  self , 
100-         local_id :  u32 , 
101-         remote_id :  u32 , 
104+         session :  ADBSession , 
102105        mut  reader :  R , 
103106    )  -> std:: result:: Result < ( ) ,  RustADBError >  { 
104107        let  mut  buffer = [ 0 ;  BUFFER_SIZE ] ; 
@@ -111,8 +114,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
111114
112115        let  message = ADBTransportMessage :: new ( 
113116            MessageCommand :: Write , 
114-             local_id, 
115-             remote_id, 
117+             session . local_id , 
118+             session . remote_id , 
116119            & serialized_message, 
117120        ) ; 
118121
@@ -131,8 +134,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
131134
132135                    let  message = ADBTransportMessage :: new ( 
133136                        MessageCommand :: Write , 
134-                         local_id, 
135-                         remote_id, 
137+                         session . local_id , 
138+                         session . remote_id , 
136139                        & serialized_message, 
137140                    ) ; 
138141
@@ -159,8 +162,8 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
159162
160163                    let  message = ADBTransportMessage :: new ( 
161164                        MessageCommand :: Write , 
162-                         local_id, 
163-                         remote_id, 
165+                         session . local_id , 
166+                         session . remote_id , 
164167                        & serialized_message, 
165168                    ) ; 
166169
@@ -173,24 +176,27 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
173176        } 
174177    } 
175178
176-     pub ( crate )  fn  begin_synchronization ( & mut  self )  -> Result < ( ) >  { 
177-         self . open_session ( b"sync:\0 " ) ?; 
178-         Ok ( ( ) ) 
179+     pub ( crate )  fn  begin_synchronization ( & mut  self )  -> Result < ADBSession >  { 
180+         self . open_session ( b"sync:\0 " ) 
179181    } 
180182
181-     pub ( crate )  fn  stat_with_explicit_ids ( & mut  self ,  remote_path :  & str )  -> Result < AdbStatResponse >  { 
183+     pub ( crate )  fn  stat_with_explicit_ids ( 
184+         & mut  self , 
185+         session :  ADBSession , 
186+         remote_path :  & str , 
187+     )  -> Result < AdbStatResponse >  { 
182188        let  stat_buffer = MessageSubcommand :: Stat . with_arg ( remote_path. len ( )  as  u32 ) ; 
183189        let  message = ADBTransportMessage :: new ( 
184190            MessageCommand :: Write , 
185-             self . get_local_id ( ) ? , 
186-             self . get_remote_id ( ) ? , 
191+             session . local_id , 
192+             session . remote_id , 
187193            & bincode:: serialize ( & stat_buffer) . map_err ( |_e| RustADBError :: ConversionError ) ?, 
188194        ) ; 
189195        self . send_and_expect_okay ( message) ?; 
190196        self . send_and_expect_okay ( ADBTransportMessage :: new ( 
191197            MessageCommand :: Write , 
192-             self . get_local_id ( ) ? , 
193-             self . get_remote_id ( ) ? , 
198+             session . local_id , 
199+             session . remote_id , 
194200            remote_path. as_bytes ( ) , 
195201        ) ) ?; 
196202        let  response = self . transport . read_message ( ) ?; 
@@ -200,46 +206,51 @@ impl<T: ADBMessageTransport> ADBMessageDevice<T> {
200206            . map_err ( |_e| RustADBError :: ConversionError ) 
201207    } 
202208
203-     pub ( crate )  fn  end_transaction ( & mut  self )  -> Result < ( ) >  { 
209+     pub ( crate )  fn  end_transaction ( & mut  self ,   session :   ADBSession )  -> Result < ( ) >  { 
204210        let  quit_buffer = MessageSubcommand :: Quit . with_arg ( 0u32 ) ; 
205211        self . send_and_expect_okay ( ADBTransportMessage :: new ( 
206212            MessageCommand :: Write , 
207-             self . get_local_id ( ) ? , 
208-             self . get_remote_id ( ) ? , 
213+             session . local_id , 
214+             session . remote_id , 
209215            & bincode:: serialize ( & quit_buffer) . map_err ( |_e| RustADBError :: ConversionError ) ?, 
210216        ) ) ?; 
211217        let  _discard_close = self . transport . read_message ( ) ?; 
212218        Ok ( ( ) ) 
213219    } 
214220
215-     pub ( crate )  fn  open_session ( & mut  self ,  data :  & [ u8 ] )  -> Result < ADBTransportMessage >  { 
221+     pub ( crate )  fn  open_session ( & mut  self ,  data :  & [ u8 ] )  -> Result < ADBSession >  { 
216222        let  mut  rng = rand:: rng ( ) ; 
223+         let  local_id:  u32  = rng. random ( ) ; 
217224
218225        let  message = ADBTransportMessage :: new ( 
219226            MessageCommand :: Open , 
220-             rng . random ( ) ,  // Our 'local-id' 
227+             local_id ,  // Our 'local-id' 
221228            0 , 
222229            data, 
223230        ) ; 
224231        self . get_transport_mut ( ) . write_message ( message) ?; 
225232
226233        let  response = self . get_transport_mut ( ) . read_message ( ) ?; 
227234
228-         self . local_id  = Some ( response. header ( ) . arg1 ( ) ) ; 
229-         self . remote_id  = Some ( response. header ( ) . arg0 ( ) ) ; 
235+         if  response. header ( ) . command ( )  != MessageCommand :: Okay  { 
236+             return  Err ( RustADBError :: ADBRequestFailed ( format ! ( 
237+                 "Open session failed: got {} in respone instead of OKAY" , 
238+                 response. header( ) . command( ) 
239+             ) ) ) ; 
240+         } 
230241
231-         Ok ( response) 
232-     } 
242+         if  response. header ( ) . arg1 ( )  != local_id { 
243+             return  Err ( RustADBError :: ADBRequestFailed ( format ! ( 
244+                 "Open session failed: respones used {} for our local_id instead of {local_id}" , 
245+                 response. header( ) . arg1( ) 
246+             ) ) ) ; 
247+         } 
233248
234-     pub ( crate )  fn  get_local_id ( & self )  -> Result < u32 >  { 
235-         self . local_id . ok_or ( RustADBError :: ADBRequestFailed ( 
236-             "connection not opened, no local_id" . into ( ) , 
237-         ) ) 
238-     } 
249+         let  session = ADBSession  { 
250+             local_id, 
251+             remote_id :  response. header ( ) . arg0 ( ) , 
252+         } ; 
239253
240-     pub ( crate )  fn  get_remote_id ( & self )  -> Result < u32 >  { 
241-         self . remote_id . ok_or ( RustADBError :: ADBRequestFailed ( 
242-             "connection not opened, no remote_id" . into ( ) , 
243-         ) ) 
254+         Ok ( session) 
244255    } 
245256} 
0 commit comments