|  |  | @ -1,25 +1,32 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #!/usr/bin/env python3 |  |  |  | #!/usr/bin/env python3 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import struct | 
			
		
	
		
		
			
				
					
					|  |  |  | import traceback |  |  |  | import traceback | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | import cereal.messaging as messaging |  |  |  | import cereal.messaging as messaging | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import panda.python.uds as uds | 
			
		
	
		
		
			
				
					
					|  |  |  | from panda.python.uds import FUNCTIONAL_ADDRS |  |  |  | from panda.python.uds import FUNCTIONAL_ADDRS | 
			
		
	
		
		
			
				
					
					|  |  |  | from selfdrive.car.isotp_parallel_query import IsoTpParallelQuery |  |  |  | from selfdrive.car.isotp_parallel_query import IsoTpParallelQuery | 
			
		
	
		
		
			
				
					
					|  |  |  | from selfdrive.swaglog import cloudlog |  |  |  | from selfdrive.swaglog import cloudlog | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | VIN_REQUEST = b'\x09\x02' |  |  |  | OBD_VIN_REQUEST = b'\x09\x02' | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | VIN_RESPONSE = b'\x49\x02\x01' |  |  |  | OBD_VIN_RESPONSE = b'\x49\x02\x01' | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | UDS_VIN_REQUEST = bytes([uds.SERVICE_TYPE.READ_DATA_BY_IDENTIFIER]) + struct.pack("!H", uds.DATA_IDENTIFIER_TYPE.VIN) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | UDS_VIN_RESPONSE = bytes([uds.SERVICE_TYPE.READ_DATA_BY_IDENTIFIER + 0x40]) + struct.pack("!H", uds.DATA_IDENTIFIER_TYPE.VIN) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | VIN_UNKNOWN = "0" * 17 |  |  |  | VIN_UNKNOWN = "0" * 17 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | def get_vin(logcan, sendcan, bus, timeout=0.1, retry=5, debug=False): |  |  |  | def get_vin(logcan, sendcan, bus, timeout=0.1, retry=5, debug=False): | 
			
		
	
		
		
			
				
					
					|  |  |  |   for i in range(retry): |  |  |  |   for request, response in ((UDS_VIN_REQUEST, UDS_VIN_RESPONSE), (OBD_VIN_REQUEST, OBD_VIN_RESPONSE)): | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     try: |  |  |  |     for i in range(retry): | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       query = IsoTpParallelQuery(sendcan, logcan, bus, FUNCTIONAL_ADDRS, [VIN_REQUEST], [VIN_RESPONSE], functional_addr=True, debug=debug) |  |  |  |       try: | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       for addr, vin in query.get_data(timeout).items(): |  |  |  |         query = IsoTpParallelQuery(sendcan, logcan, bus, FUNCTIONAL_ADDRS, [request, ], [response, ], functional_addr=True, debug=debug) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         return addr[0], vin.decode() |  |  |  |         for addr, vin in query.get_data(timeout).items(): | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       print(f"vin query retry ({i+1}) ...") |  |  |  |           return addr[0], vin.decode() | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     except Exception: |  |  |  |         print(f"vin query retry ({i+1}) ...") | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       cloudlog.warning(f"VIN query exception: {traceback.format_exc()}") |  |  |  |       except Exception: | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |         cloudlog.warning(f"VIN query exception: {traceback.format_exc()}") | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   return 0, VIN_UNKNOWN |  |  |  |   return 0, VIN_UNKNOWN | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | 
 |