@@ -17,6 +17,7 @@ use tokio_uring::buf::BoundedBuf;
17
17
include ! ( concat!( env!( "OUT_DIR" ) , "/protos/mod.rs" ) ) ;
18
18
use crate :: mitm:: protos:: * ;
19
19
use crate :: mitm:: AudioStreamType :: * ;
20
+ use crate :: mitm:: SensorMessageId :: * ;
20
21
use crate :: mitm:: SensorType :: * ;
21
22
use protobuf:: text_format:: print_to_string_pretty;
22
23
use protobuf:: { Enum , Message , MessageDyn } ;
@@ -49,6 +50,10 @@ pub const ENCRYPTED: u8 = 1 << 3;
49
50
// location for hu_/md_ private keys and certificates:
50
51
const KEYS_PATH : & str = "/etc/aa-proxy-rs" ;
51
52
53
+ pub struct ModifyContext {
54
+ sensor_channel : Option < u8 > ,
55
+ }
56
+
52
57
#[ derive( PartialEq , Copy , Clone ) ]
53
58
pub enum ProxyType {
54
59
HeadUnit ,
@@ -246,20 +251,44 @@ pub async fn pkt_modify_hook(
246
251
disable_media_sink : bool ,
247
252
disable_tts_sink : bool ,
248
253
remove_tap_restriction : bool ,
254
+ video_in_motion : bool ,
255
+ ctx : & mut ModifyContext ,
249
256
) -> Result < ( ) > {
250
- if pkt. channel != 0 {
251
- return Ok ( ( ) ) ;
252
- }
253
-
254
257
// message_id is the first 2 bytes of payload
255
258
let message_id: i32 = u16:: from_be_bytes ( pkt. payload [ 0 ..=1 ] . try_into ( ) ?) . into ( ) ;
259
+ let data = & pkt. payload [ 2 ..] ; // start of message data
260
+
261
+ // handling driving_status_data change
262
+ if let Some ( ch) = ctx. sensor_channel {
263
+ if ch == pkt. channel {
264
+ match protos:: SensorMessageId :: from_i32 ( message_id) . unwrap_or ( SENSOR_MESSAGE_ERROR ) {
265
+ SENSOR_MESSAGE_BATCH => {
266
+ if let Ok ( mut msg) = SensorBatch :: parse_from_bytes ( data) {
267
+ if !msg. driving_status_data . is_empty ( ) {
268
+ // forcing status to 0 value
269
+ msg. driving_status_data [ 0 ] . set_status ( 0 ) ;
270
+ // regenerating payload data
271
+ pkt. payload = msg. write_to_bytes ( ) ?;
272
+ pkt. payload . insert ( 0 , ( message_id >> 8 ) as u8 ) ;
273
+ pkt. payload . insert ( 1 , ( message_id & 0xff ) as u8 ) ;
274
+ }
275
+ }
276
+ }
277
+ _ => ( ) ,
278
+ }
279
+ }
280
+ // end sensors processing
281
+ return Ok ( ( ) ) ;
282
+ }
256
283
284
+ if pkt. channel != 0 {
285
+ return Ok ( ( ) ) ;
286
+ }
257
287
// trying to obtain an Enum from message_id
258
288
let control = protos:: ControlMessageType :: from_i32 ( message_id) ;
259
289
debug ! ( "message_id = {:04X}, {:?}" , message_id, control) ;
260
290
261
291
// parsing data
262
- let data = & pkt. payload [ 2 ..] ; // start of message data
263
292
match control. unwrap_or ( MESSAGE_UNEXPECTED_MESSAGE ) {
264
293
MESSAGE_SERVICE_DISCOVERY_RESPONSE => {
265
294
let mut msg = ServiceDiscoveryResponse :: parse_from_bytes ( data) ?;
@@ -313,6 +342,24 @@ pub async fn pkt_modify_hook(
313
342
) ;
314
343
}
315
344
345
+ // obtain SENSOR_DRIVING_STATUS_DATA sensor channel
346
+ if video_in_motion {
347
+ if let Some ( svc) = msg
348
+ . services
349
+ . iter ( )
350
+ . find ( |svc| !svc. sensor_source_service . sensors . is_empty ( ) )
351
+ {
352
+ if let Some ( _) = svc
353
+ . sensor_source_service
354
+ . sensors
355
+ . iter ( )
356
+ . find ( |s| s. sensor_type ( ) == SENSOR_DRIVING_STATUS_DATA )
357
+ {
358
+ ctx. sensor_channel = Some ( svc. id ( ) as u8 ) ;
359
+ }
360
+ }
361
+ }
362
+
316
363
// remove tap restriction by removing SENSOR_SPEED
317
364
if remove_tap_restriction {
318
365
if let Some ( svc) = msg
@@ -483,6 +530,7 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
483
530
disable_media_sink : bool ,
484
531
disable_tts_sink : bool ,
485
532
remove_tap_restriction : bool ,
533
+ video_in_motion : bool ,
486
534
) -> Result < ( ) > {
487
535
let ssl = ssl_builder ( proxy_type) . await ?;
488
536
@@ -553,6 +601,9 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
553
601
}
554
602
555
603
// main data processing/transfer loop
604
+ let mut ctx = ModifyContext {
605
+ sensor_channel : None ,
606
+ } ;
556
607
loop {
557
608
// handling data from opposite device's thread, which needs to be transmitted
558
609
if let Ok ( mut pkt) = rx. try_recv ( ) {
@@ -564,6 +615,8 @@ pub async fn proxy<A: Endpoint<A> + 'static>(
564
615
disable_media_sink,
565
616
disable_tts_sink,
566
617
remove_tap_restriction,
618
+ video_in_motion,
619
+ & mut ctx,
567
620
)
568
621
. await ?;
569
622
pkt. encrypt_payload ( & mut mem_buf, & mut server) . await ?;
0 commit comments