5454 Flags ,
5555)
5656from tesla_fleet_api .tesla .vehicle .proto .vcsec_pb2 import (
57- OPERATIONSTATUS_OK ,
5857 FromVCSECMessage ,
58+ VehicleStatus ,
5959)
6060from tesla_fleet_api .tesla .vehicle .proto .car_server_pb2 import (
6161 Action ,
102102 MediaPreviousTrack ,
103103 MediaPreviousFavorite ,
104104)
105- from tesla_fleet_api .tesla .vehicle .proto .vehicle_pb2 import VehicleState , ClimateState
105+ from tesla_fleet_api .tesla .vehicle .proto .vehicle_pb2 import VehicleData , VehicleState , ClimateState
106106from tesla_fleet_api .tesla .vehicle .proto .vcsec_pb2 import (
107107 UnsignedMessage ,
108108 RKEAction_E ,
@@ -165,6 +165,7 @@ class Session:
165165 hmac : bytes
166166 publicKey : bytes
167167 lock : Lock
168+ ready : bool
168169
169170 def __init__ (self , parent : Commands , domain : Domain ):
170171 self .parent = parent
@@ -293,42 +294,44 @@ async def _command(self, domain: Domain, command: bytes, attempt: int = 0) -> di
293294 if resp .HasField ("protobuf_message_as_bytes" ):
294295 #decrypt
295296 if (resp .signature_data .HasField ("AES_GCM_Response_data" )):
296- print (resp .signature_data .AES_GCM_Response_data )
297- if (msg .signature_data .HasField ("AES_GCM_Personalized_Signature_Data" )):
298- tag = bytes (SignatureType .SIGNATURE_TYPE_AES_GCM_PERSONALIZED ) + msg .signature_data .AES_GCM_Personalized_data .tag
297+ if (msg .signature_data .HasField ("AES_GCM_Personalized_data" )):
298+ request_hash = bytes ([SignatureType .SIGNATURE_TYPE_AES_GCM_PERSONALIZED ]) + msg .signature_data .AES_GCM_Personalized_data .tag
299299 elif (msg .signature_data .HasField ("HMAC_Personalized_data" )):
300- tag = bytes (SignatureType .SIGNATURE_TYPE_HMAC_PERSONALIZED ) + msg .signature_data .HMAC_Personalized_data .tag [:16 ]
300+ request_hash = bytes ([SignatureType .SIGNATURE_TYPE_HMAC_PERSONALIZED ]) + msg .signature_data .HMAC_Personalized_data .tag
301+ if (session .domain == Domain .DOMAIN_VEHICLE_SECURITY ):
302+ request_hash = request_hash [:17 ]
301303 else :
302304 raise ValueError ("Invalid request signature data" )
305+
303306 metadata = bytes ([
304307 Tag .TAG_SIGNATURE_TYPE ,
305308 1 ,
306309 SignatureType .SIGNATURE_TYPE_AES_GCM_RESPONSE ,
307310 Tag .TAG_DOMAIN ,
308311 1 ,
309- session .domain ,
312+ resp . from_destination .domain ,
310313 Tag .TAG_PERSONALIZATION ,
311314 17 ,
312315 * self .vin .encode (),
313316 Tag .TAG_COUNTER ,
314317 4 ,
315- * struct .pack (">I" , session .counter ),
318+ * struct .pack (">I" , resp . signature_data . AES_GCM_Response_data .counter ),
316319 Tag .TAG_FLAGS ,
317- 1 ,
318- msg . flags ,
320+ 4 ,
321+ * struct . pack ( ">I" , resp . flags ) ,
319322 Tag .TAG_REQUEST_HASH ,
320323 17 ,
321- * tag ,
324+ * request_hash ,
322325 Tag .TAG_FAULT ,
323- 1 ,
324- resp .signedMessageStatus .signed_message_fault ,
326+ 4 ,
327+ * struct . pack ( ">I" , resp .signedMessageStatus .signed_message_fault ) ,
325328 Tag .TAG_END ,
326329 ])
327330
328331 aad = Hash (SHA256 ())
329332 aad .update (metadata )
330333 aesgcm = AESGCM (session .sharedKey )
331- resp .protobuf_message_as_bytes = aesgcm .decrypt (resp .signature_data .AES_GCM_Response_data .nonce , resp .protobuf_message_as_bytes , aad .finalize ())
334+ resp .protobuf_message_as_bytes = aesgcm .decrypt (resp .signature_data .AES_GCM_Response_data .nonce , resp .protobuf_message_as_bytes + resp . signature_data . AES_GCM_Response_data . tag , aad .finalize ())
332335
333336 if (resp .from_destination .domain == Domain .DOMAIN_VEHICLE_SECURITY ):
334337 vcsec = FromVCSECMessage .FromString (resp .protobuf_message_as_bytes )
@@ -365,6 +368,10 @@ async def _command(self, domain: Domain, command: bytes, attempt: int = 0) -> di
365368 "reason" : response .ping .local_timestamp
366369 }
367370 }
371+ if response .HasField ("vehicleData" ):
372+ return {
373+ "response" : response .vehicleData
374+ }
368375 if response .HasField ("actionStatus" ):
369376 return {
370377 "response" : {
@@ -373,6 +380,7 @@ async def _command(self, domain: Domain, command: bytes, attempt: int = 0) -> di
373380 }
374381 }
375382
383+
376384 return {"response" : {"result" : True , "reason" : "" }}
377385
378386 async def _commandHmac (self , session : Session , command : bytes , attempt : int = 1 ) -> RoutableMessage :
@@ -429,6 +437,7 @@ async def _commandAes(self, session: Session, command: bytes, attempt: int = 1)
429437 LOGGER .debug (f"Sending AES to domain { Domain .Name (session .domain )} " )
430438
431439 aes_personalized = session .aes_gcm_personalized ()
440+ flags = 1 << Flags .FLAG_ENCRYPT_RESPONSE
432441
433442 metadata = bytes ([
434443 Tag .TAG_SIGNATURE_TYPE ,
@@ -451,7 +460,7 @@ async def _commandAes(self, session: Session, command: bytes, attempt: int = 1)
451460 * struct .pack (">I" , aes_personalized .counter ),
452461 Tag .TAG_FLAGS ,
453462 4 ,
454- 0 , 0 , 0 , Flags . FLAG_ENCRYPT_RESPONSE ,
463+ * struct . pack ( ">I" , flags ) ,
455464 Tag .TAG_END ,
456465 ])
457466
@@ -478,18 +487,28 @@ async def _commandAes(self, session: Session, command: bytes, attempt: int = 1)
478487 ),
479488 AES_GCM_Personalized_data = aes_personalized ,
480489 ),
481- flags = Flags . FLAG_ENCRYPT_RESPONSE ,
490+ flags = flags ,
482491 )
483492
484493
485494 async def _sendVehicleSecurity (self , command : UnsignedMessage ) -> dict [str , Any ]:
486495 """Sign and send a message to Infotainment computer."""
487496 return await self ._command (Domain .DOMAIN_VEHICLE_SECURITY , command .SerializeToString ())
488497
498+ async def _getVehicleSecurity (self , command : UnsignedMessage ) -> VehicleStatus :
499+ """Sign and send a message to Infotainment computer."""
500+ reply = await self ._command (Domain .DOMAIN_VEHICLE_SECURITY , command .SerializeToString ())
501+ return reply ["response" ]
502+
489503 async def _sendInfotainment (self , command : Action ) -> dict [str , Any ]:
490504 """Sign and send a message to Infotainment computer."""
491505 return await self ._command (Domain .DOMAIN_INFOTAINMENT , command .SerializeToString ())
492506
507+ async def _getInfotainment (self , command : Action ) -> VehicleData :
508+ """Sign and send a message to Infotainment computer."""
509+ reply = await self ._command (Domain .DOMAIN_INFOTAINMENT , command .SerializeToString ())
510+ return reply ["response" ]
511+
493512 async def handshakeVehicleSecurity (self ) -> None :
494513 """Perform a handshake with the vehicle security domain."""
495514 await self ._handshake (Domain .DOMAIN_VEHICLE_SECURITY )
0 commit comments