diff --git a/pyren3/mod_ddt_ecu.py b/pyren3/mod_ddt_ecu.py index 1df4bd3..e2cb723 100644 --- a/pyren3/mod_ddt_ecu.py +++ b/pyren3/mod_ddt_ecu.py @@ -237,7 +237,7 @@ class DDTECU(): ''' 0 1 2 3 4 5 6 7 ''' ''' 01234567890123456789012345678901234567890123456789012345678901234567890123456''' - #IdRsp = '61 80 34 36 33 32 52 45 34 42 45 30 30 33 37 52 00 83 9D 00 1A 90 01 01 00 88 AA' + IdRsp = '61 80 34 36 33 32 52 45 34 42 45 30 30 33 37 52 00 83 9D 00 1A 90 01 01 00 88 AA' ''' -- -------- ----- ----- ''' ''' DiagVersion--+ | | +--Version ''' ''' Supplier--+ +--Soft ''' @@ -1130,6 +1130,48 @@ def minDist(a, b): return d +def distance( a, b ): + """ calculate distance between strings """ + """ normalized to length of string """ + """ a - readen value """ + """ b - pattern from eculist """ + + d = 0 + + # align length + if len( a ) < len( b ): + a = a + ' ' * ( len( b ) - len( a )) + else: + b = b + ' ' * ( len( a ) - len( b )) + + # humming distance + l = len(a) + for i in range(0, l): + if b[i] != '?' and ord(a[i]) != ord(b[i]): + d = d + 1 + + # normalize to length of string + if d: + d = d / l + + return d + +def AutoIdents_distance( DiagVersion, Supplier, Soft, Version, ai ): + + #normalize supplier in such cases + #DiagVersion="12" Supplier="746" Soft="2470" Version="A600" + #DiagVersion="12" Supplier="39324d" Soft="0052" Version="0400" + if len( ai['Supplier'] ) == 6 and \ + len( ai['Soft'] ) == 4 and \ + len( ai['Version'] ) == 4: + ai['Supplier'] = bytes.fromhex(ai['Supplier']).decode('utf-8') + + d = distance( DiagVersion, ai['DiagVersion']) * 0.25 + d = d + distance( Supplier, ai['Supplier']) * 0.25 + d = d + distance( Soft, ai['Soft']) * 0.25 + d = d + distance( Version, ai['Version']) * 0.25 + + return round( d, 4 ) def ecuSearch(vehTypeCode, Address, DiagVersion, Supplier, Soft, Version, el, interactive = True): @@ -1148,11 +1190,12 @@ def ecuSearch(vehTypeCode, Address, DiagVersion, Supplier, Soft, Version, el, in for k in list(t.keys()): for ai in t[k]['AutoIdents']: - dist = 0 - dist = dist + minDist(DiagVersion, ai['DiagVersion']) * 1000 # weight - dist = dist + minDist(Supplier, ai['Supplier']) * 1 # weight - dist = dist + minDist(Soft, ai['Soft']) * 1 # weight - dist = dist + minDist(Version, ai['Version']) * 1 # weight + #dist = 0 + #dist = dist + minDist(DiagVersion, ai['DiagVersion']) * 1000 # weight + #dist = dist + minDist(Supplier, ai['Supplier']) * 1 # weight + #dist = dist + minDist(Soft, ai['Soft']) * 1 # weight + #dist = dist + minDist(Version, ai['Version']) * 1 # weight + dist = AutoIdents_distance( DiagVersion, Supplier, Soft, Version, ai ) if vehTypeCode in t[k]['Projects'] or dist == 0: if k not in list(cand.keys()): cand[k] = 0xFFFFFFFF diff --git a/pyren3/mod_elm.py b/pyren3/mod_elm.py index c7c9123..3e84d4d 100644 --- a/pyren3/mod_elm.py +++ b/pyren3/mod_elm.py @@ -160,7 +160,14 @@ class Port: portName = portName.strip () - if re.match (r"^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}$", portName): + MAC = None + upPortName = portName.upper() + if re.match (r"^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}$", upPortName) or \ + re.match (r"^[0-9A-F]{4}.[0-9A-F]{4}.[0-9A-F]{4}$", upPortName) or \ + re.match (r"^[0-9A-F]{12}$", upPortName): + MAC = upPortName + + if MAC: try: self.macaddr = portName self.channel = 1 @@ -187,7 +194,7 @@ class Port: print(" \n\nERROR: Can't connect to WiFi ELM\n\n") mod_globals.opt_demo = True sys.exit() - elif mod_globals.os == 'android' and portName == 'bt': + elif mod_globals.os == 'android' and ( portName == 'bt' or MAC != None ): self.portType = 2 self.droid = android.Android () self.droid.toggleBluetoothState (True) @@ -196,7 +203,10 @@ class Port: time.sleep(1) retry = retry + 1 try: - self.btcid = self.droid.bluetoothConnect ('00001101-0000-1000-8000-00805F9B34FB').result + if MAC == None: + self.btcid = self.droid.bluetoothConnect ('00001101-0000-1000-8000-00805F9B34FB').result + else: + self.btcid = self.droid.bluetoothConnect (uuid='00001101-0000-1000-8000-00805F9B34FB', address=MAC).result except: pass print( 'Try ',retry, ":", self.btcid ) @@ -1327,6 +1337,8 @@ class ELM: self.l1_cache[command] = str(hex(nframes))[2:].upper() if len (result) // 2 >= nbytes and noerrors: + # trim padding + result = result[:nbytes*2] # split by bytes and return result = ' '.join (a + b for a, b in zip (result[::2], result[1::2])) return result @@ -1592,6 +1604,8 @@ class ELM: self.l1_cache[init_command] = str(nFrames) if noerrors and len(result) // 2 >= nBytes: + # trim padding + result = result[:nBytes*2] # split by bytes and return result = ' '.join(a + b for a, b in zip(result[::2], result[1::2])) return result @@ -1811,6 +1825,8 @@ class ELM: noErrors = False if len (result) // 2 >= nBytes and noErrors and result[:2] != '7F': + # trim padding + result = result[:nBytes*2] # split by bytes and return result = ' '.join (a + b for a, b in zip (result[::2], result[1::2])) return result