From 2883ac4ce5b9814acd10e9fb00917b09cb6ec059 Mon Sep 17 00:00:00 2001 From: shrlnm Date: Sun, 1 Jan 2023 17:40:50 +0300 Subject: [PATCH 1/7] new search alg, trim responce, MAC address in BT --- pyren3/mod_ddt_ecu.py | 55 ++++++++++++++++++++++++++++++++++++++----- pyren3/mod_elm.py | 22 ++++++++++++++--- 2 files changed, 68 insertions(+), 9 deletions(-) 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 From 6238916b259fd982c76d2e5413e753b20906f942 Mon Sep 17 00:00:00 2001 From: shrlnm Date: Mon, 2 Jan 2023 18:32:49 +0300 Subject: [PATCH 2/7] MAC address of BT in launcher --- main.py | 48 +++++++++++++++++++++++++++++++++++++++++------ pyren3/mod_ecu.py | 19 +++++++++++++------ pyren3/mod_elm.py | 15 ++++++++++----- 3 files changed, 65 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 689796a..acbcb1b 100755 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ ################################## # # # # -# Version: 3.0 (1-Aug-2022) # +# Version: 3.1 (2-Jan-2023) # # Author: Shr-Lnm # # # # # @@ -16,7 +16,7 @@ __author__ = "Shr-Lnm" __copyright__ = "Copyright 2018-2022" __credits__ = [] # __license__ = "GNU" # Unknown licence! -__version__ = "3.0.0" # python3 maybe ? +__version__ = "3.1.0" # python3 maybe ? __maintainer__ = "Shr-Lnm" __email__ = "mshkn@inbox.ru" __status__ = "Beta" @@ -26,6 +26,7 @@ import shutil from os import listdir from os.path import isdir from os.path import isfile +import re import sys try: @@ -244,9 +245,17 @@ def run(s, cmd): cmdr = __import__('mod_ddt') elif cmd == 'term': cmdr = __import__('mod_term') + elif cmd == 'pids': + cmdr = __import__('mod_ecu') + + if s.port.upper() == 'BT' or s.port == '': + s.port = 'bt' + + if s.port.upper().endswith(';BT'): + s.port = s.port.split(';')[0] - if s.port.lower() == 'bt' or s.port == '': s.port = 'bt' sys.argv.append('-p' + s.port) + if cmd == 'demo': sys.argv.append('--demo') if cmd == 'scan' and cmd != 'term': @@ -276,6 +285,8 @@ def run(s, cmd): # sys.argv.append('--demo') if cmd == 'ddt': sys.argv.append('--demo') +# if cmd == 'pids': +# sys.argv = [ 'mod_ecu.py', 'ask', s.lang, 'torq'] os.chdir(s.path) cmdr.main() sys.exit() @@ -903,6 +914,11 @@ else: self.droid.fullDismiss() run(self.save, 'term') + def cmd_PIDs(self): + self.saveSettings() + self.droid.fullDismiss() + run(self.save, 'pids') + def cmd_Update(self): res = update_from_gitlab() if res == 0: @@ -920,7 +936,15 @@ else: if self.droid.fullQueryDetail("rb_bt").result['checked'] == 'false': self.save.port = self.droid.fullQueryDetail("in_wifi").result['text'] else: - self.save.port = 'BT' + portName = self.droid.fullQueryDetail("in_wifi").result['text'] + upPortName = portName.upper() + MAC = '' + 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): + upPortName = upPortName.replace(':','').replace('.','') + MAC = ':'.join (a + b for a, b in zip (upPortName[::2], upPortName[1::2])) + self.save.port = MAC + ';' + 'BT' self.save.speed = '38400' @@ -984,10 +1008,13 @@ else: if self.save.port == '': self.save.port = "192.168.0.10:35000" - if self.save.port.lower() == 'bt': + if self.save.port.upper().endswith('BT'): + MAC = "" + if ';' in self.save.port: + MAC = self.save.port.split(';')[0] self.droid.fullSetProperty("rb_bt", "checked", "true") self.droid.fullSetProperty("rb_wifi", "checked", "false") - self.droid.fullSetProperty("in_wifi", "text", "192.168.0.10:35000") + self.droid.fullSetProperty("in_wifi", "text", MAC) else: self.droid.fullSetProperty("rb_bt", "checked", "false") self.droid.fullSetProperty("rb_wifi", "checked", "true") @@ -1256,6 +1283,13 @@ else: android:layout_below="@id/bt_start" android:layout_toLeftOf="@+id/bt_mon" android:text="Macro" /> +