diff --git a/pyren3/acf.py b/pyren3/acf.py index 0baab58..fd830f0 100755 --- a/pyren3/acf.py +++ b/pyren3/acf.py @@ -14,46 +14,10 @@ import pickle from mod_acf_func import ACE mod_globals.os = os.name - -if mod_globals.os == 'nt': - import pip - - try: - import serial - except ImportError: - pip.main(['install','pyserial']) - try: - import colorama - except ImportError: - pip.main(['install','colorama']) - try: - import colorama - except ImportError: - print("\n\n\n\t\t\tGive me access to the Internet for download modules\n\n\n") - sys.exit() - colorama.init() -else: - # let's try android - try: - import androidhelper as android - mod_globals.os = 'android' - except: - try: - import android - mod_globals.os = 'android' - except: - pass - -if mod_globals.os != 'android': - try: - import serial - from serial.tools import list_ports - #import ply - except ImportError: - print("\n\n\n\tPleas install additional modules") - print("\t\t>sudo easy_install pyserial") - sys.exit() +import serial +import colorama +from serial.tools import list_ports from mod_elm import ELM from mod_scan_ecus import ScanEcus @@ -146,6 +110,16 @@ def optParser(): dest="vinnum", default="") + parser.add_argument("--ref", + help="alternative ref", + dest="ref", + default="") + + parser.add_argument("--mtc", + help="alternative mtc", + dest="mtc", + default="") + parser.add_argument("-vv", "--verbose", help="show parameter explanations", dest="verbose", @@ -158,8 +132,6 @@ def optParser(): default=False, action="store_true") - - options = parser.parse_args() if not options.port and mod_globals.os != 'android': @@ -186,6 +158,8 @@ def optParser(): mod_globals.vin = options.vinnum mod_globals.opt_verbose = options.verbose mod_globals.opt_verbose2 = options.verbose2 + mod_globals.opt_ref = options.ref + mod_globals.opt_mtc = options.mtc if options.dev=='' or len(options.dev)!=4 or options.dev[0:2]!='10': mod_globals.opt_dev = False @@ -207,61 +181,43 @@ def main(): if not os.path.exists('../BVMEXTRACTION'): print("Can't find MTC database. (../BVMEXTRACTION)") exit() - - print('Opening ELM') - elm = ELM( mod_globals.opt_port, mod_globals.opt_speed, mod_globals.opt_log ) - - #change serial port baud rate - if mod_globals.opt_speed0: - # demo mode with predefined ecu list - se.read_Uces_file( all = True ) - se.detectedEcus = [] - for i in mod_globals.opt_ecuid.split(','): - if i in list(se.allecus.keys()): - se.allecus[i]['ecuname']=i - se.allecus[i]['idf']=se.allecus[i]['ModelId'][2:4] - if se.allecus[i]['idf'][0]=='0': - se.allecus[i]['idf'] = se.allecus[i]['idf'][1] - se.allecus[i]['pin'] = 'can' - se.detectedEcus.append( se.allecus[i] ) else: - if not os.path.isfile(SEFname) or mod_globals.opt_scan: - # choosing model - se.chooseModel( mod_globals.opt_car ) #choose model of car for doing full scan - - # Do this check every time - se.scanAllEcus() #First scan of all ecus - - de = se.detectedEcus - if mod_globals.vin=='': - print('Reading VINs') - VIN = getVIN( de, elm ) - mod_globals.vin = VIN + #load connection attribute from platform_attr + pl_id_cache = './cache/platform_attr.p' + if os.path.isfile(pl_id_cache): #if cache exists + pl_id = pickle.load( open( pl_id_cache, "rb" ) ) #load it + else: #else + findTCOM('', '', '', True) #make cache + pl_id = pickle.load( open( pl_id_cache, "rb" ) ) #load it + #but we do not have platform yet, so load data and then continue VIN = mod_globals.vin @@ -274,19 +230,56 @@ def main(): #print 'Finding MTC' vindata, mtcdata, refdata, platform = acf_getMTC( VIN ) - #print vindata - if vindata=='' or mtcdata=='' or refdata=='': print("ERROR!!! Can't find MTC data in database") exit() + #print vindata print("\tPlatform:",platform) print("\tvindata:",vindata) print("\tmtcdata:",mtcdata) print("\trefdata:",refdata) mtc = mtcdata.replace(' ','').replace('\n','').replace('\r','').replace('\t','').split(';') - + + #now we may continue to prepare connection attributes in demo mode + if mod_globals.opt_demo: + de = [] + for bus_brp in pl_id[platform].keys(): + brp = '' + if ':' in bus_brp: + bus,brp = bus_brp.split(':') + else: + bus = bus_brp + + for idf in pl_id[platform][bus_brp].keys(): + ent = {} + ent['idf'] = idf + ent['ecuname'] = '' + if bus == '6': + ent['pin'] = 'can' + ent['pin1'] = '6' + ent['pin2'] = '14' + elif bus == '13': + ent['pin'] = 'can2' + ent['pin1'] = '13' + ent['pin2'] = '12' + else: + ent['pin'] = 'iso' + ent['pin1'] = '7' + ent['pin2'] = '15' + ent['brp'] = brp + ent['vehTypeCode'] = platform + ent['startDiagReq'] = '' + for a in pl_id[platform][bus_brp][idf]: + ent['dst'],ent['idRx'],ent['idTx'],starts = a.split('#') + if ent['startDiagReq'] == '': + ent['startDiagReq'] = starts + else: + ent['startDiagReq'] += ('#' + starts) + + de.append( ent ) + print('Loading Modules') module_list = acf_loadModules( de, refdata, platform ) @@ -303,10 +296,7 @@ def main(): if 'mo' in list(m.keys()) and m['mo']!='': print("%2s : %s : %s" % (m['idf'],m['sref'],m['mo'].NOM)) - attr = [] - if platform in pl_id.keys() and m['idf'] in pl_id[platform].keys(): - attr = pl_id[platform][m['idf']] - acf_MTC_generateDefaults( m, mtc, attr ) + acf_MTC_generateDefaults( m, mtc ) #acf_MTC_findDiff( m, mtc, elm ) else: diff --git a/pyren3/mod_acf_func.py b/pyren3/mod_acf_func.py index fef330c..5531f7b 100644 --- a/pyren3/mod_acf_func.py +++ b/pyren3/mod_acf_func.py @@ -100,9 +100,13 @@ def acf_loadModules( de, refdata, platform ): for k in de: if k['idf']==idf: + m['pin'] = k['pin'] m['dst'] = k['dst'] + m['idRx'] = k['idRx'] + m['idTx'] = k['idRx'] m['startDiagReq'] = k['startDiagReq'] m['ecuname'] = k['ecuname'] + m['brp'] = k['brp'] break module_list.append(m) diff --git a/pyren3/mod_acf_proc.py b/pyren3/mod_acf_proc.py index cf3e194..6713688 100644 --- a/pyren3/mod_acf_proc.py +++ b/pyren3/mod_acf_proc.py @@ -69,7 +69,7 @@ def acf_form_commands( m, cu, op ): m['acf_wc'][cu.RW] = acf_pack_command( True, ace.req[cu.RW], cu.DW, m['acf_wc'][cu.RW], op.TW, op.MW) ace.req[cu.RW].SentDI[cu.DW].val = op.VW + ' #[' + cu.TE + ' : ' + op.TEX + '](' + op.MTC + ')' -def acf_MTC_generateDefaults( m, mtc, attr ): +def acf_MTC_generateDefaults( m, mtc ): """ m - module definition map """ ''' mtc - list of options ''' ''' this function generates default values''' @@ -133,30 +133,25 @@ def acf_MTC_generateDefaults( m, mtc, attr ): #print dumpn1 #print dumpn2 - scriptn = mod_globals.mtcdir+'/scripts/cmd_'+m['idf']+'_'+mod_globals.vin[-4:]+'_'+m['sref']+'.cmd' + scriptn = mod_globals.mtcdir+'/scripts/cmd_'+m['pin']+'_'+m['idf']+'_'+mod_globals.vin[-4:]+'_'+m['sref']+'.cmd' sf = open(scriptn,'wt') #print "--------------- Write Commands --------------------" - attr = list(attr) - attr_dst = '' - attr_ses = '' - if len(attr)>0: - attr_dst, attr_ses = attr[0].split('#') - if len(attr)>1: - attr_dst = attr_dst + ' #' - attr_ses = attr_ses + ' #' - for a in attr[1:]: - dst,ses = a.split('#') - attr_dst = attr_dst + ' ' + dst - attr_ses = attr_ses + ' ' + ses # write preamble sf.write('# ' + mod_globals.vin + '\n\n') - sf.write('$addr = ' + attr_dst +'\n\n') - sf.write('can500 # init can macro\n\n') + sf.write('$addr = ' + m['dst'] +'\n\n') + sf.write('#idTx = ' + m['idTx'] + ' idRx = ' + m['idRx'] + '\n\n') + if m['pin'].startswith('can'): + if m['brp']=='0': + sf.write('can500 # init can macro\n\n') + else: + sf.write('can250 # init can macro\n\n') + else: + sf.write('fast # init iso macro\n\n') sf.write('delay 1\n\n') sf.write('# open session\n') - sf.write('session ' + attr_ses + '\n\n') + sf.write('session ' + m['startDiagReq'] + '\n\n') sf.write('# configuration\n') # save write commands diff --git a/pyren3/mod_scan_ecus.py b/pyren3/mod_scan_ecus.py index 84a4c79..d0484de 100755 --- a/pyren3/mod_scan_ecus.py +++ b/pyren3/mod_scan_ecus.py @@ -1003,10 +1003,19 @@ def findTCOM( addr, cmd, rsp, pl_id = False ): ecuvhc = {} pl_id = {} vehicle = '' + + se = ScanEcus( None ) + print('Loading Uces.xml') + se.read_Uces_file(True) + print('Read models') file_list = mod_db_manager.get_file_list_from_clip('Vehicles/TCOM_*.[Xx]ml') for file in file_list: vehicle = '' + + #skip syntetic lada vesta + if '087' in file: continue + DOMTree = xml.dom.minidom.parse(mod_db_manager.get_file_from_clip(file)) vh = DOMTree.documentElement if vh.hasAttribute("defaultText"): @@ -1020,59 +1029,80 @@ def findTCOM( addr, cmd, rsp, pl_id = False ): connector = vh.getElementsByTagName("Connector") cannetwork = connector.item(0).getElementsByTagName("CANNetwork") isonetwork = connector.item(0).getElementsByTagName("ISONetwork") + addreses = {} + for pin in cannetwork: + bus = pin.getAttribute("canH") + CANNetworkParams = pin.getElementsByTagName("CANNetworkParams") + brp = CANNetworkParams.item(0).getAttribute("brp") + bus = bus+':'+brp #brp is a can bus speed + CanIds = pin.getElementsByTagName("CanId") + addreses[bus] = {} + for CanId in CanIds: + targetAddress = CanId.getAttribute("targetAddress") + idTx = CanId.getAttribute("idTx") + idRx = CanId.getAttribute("idRx") + addreses[bus][targetAddress] = { 'idTx': idTx, 'idRx': idRx } + + pl_id[vehTypeCode][bus] = {} eculist = pin.getElementsByTagName("EcuList") if eculist: ecukind = eculist.item(0).getElementsByTagName("EcuKind") for ek in ecukind: id = ek.getAttribute("idFamily") - pl_id[vehTypeCode][id] = {} - pl_id[vehTypeCode][id]['refs'] = [] + pl_id[vehTypeCode][bus][id] = {} + pl_id[vehTypeCode][bus][id]['refs'] = [] ecuref = ek.getElementsByTagName("EcuRef") for er in ecuref: ecuname = er.getAttribute("name") - pl_id[vehTypeCode][id]['refs'].append(ecuname) + pl_id[vehTypeCode][bus][id]['refs'].append(ecuname) if ecuname in list(ecuvhc.keys()): ecuvhc[ecuname].append(vehicle) else: ecuvhc[ecuname] = [vehicle] for pin in isonetwork: + bus = '7' + pl_id[vehTypeCode][bus] = {} eculist = pin.getElementsByTagName("EcuList") if eculist: ecukind = eculist.item(0).getElementsByTagName("EcuKind") for ek in ecukind: id = ek.getAttribute("idFamily") - if id not in pl_id[vehTypeCode].keys(): - pl_id[vehTypeCode][id] = {} - pl_id[vehTypeCode][id]['refs'] = [] + if id not in pl_id[vehTypeCode][bus].keys(): + pl_id[vehTypeCode][bus][id] = {} + pl_id[vehTypeCode][bus][id]['refs'] = [] ecuref = ek.getElementsByTagName("EcuRef") for er in ecuref: ecuname = er.getAttribute("name") - pl_id[vehTypeCode][id]['refs'].append(ecuname) + pl_id[vehTypeCode][bus][id]['refs'].append(ecuname) if ecuname in list(ecuvhc.keys()): ecuvhc[ecuname].append(vehicle) else: ecuvhc[ecuname] = [vehicle] - se = ScanEcus( None ) - print('Loading Uces.xml') - se.read_Uces_file(True) - - if pl_id: - #save pl_id - for pl in pl_id.keys(): - for id in pl_id[pl].keys(): - attr = set() - for r in pl_id[pl][id]['refs']: - if r in se.allecus.keys(): - #attr.add(se.allecus[r]['stdType']+"#"+se.allecus[r]['dst']+"#"+se.allecus[r]['startDiagReq']) - attr.add(se.allecus[r]['dst']+"#"+se.allecus[r]['startDiagReq']) - #else: - # print(r) - del pl_id[pl][id]['refs'] - pl_id[pl][id] = attr - pickle.dump( pl_id, open( './cache/pl_id_attr.p', "wb" ) ) - else: + if pl_id: + for bus in pl_id[vehTypeCode].keys(): + for id in pl_id[vehTypeCode][bus].keys(): + attr = set() + for r in pl_id[vehTypeCode][bus][id]['refs']: + if r in se.allecus.keys(): + #attr.add(se.allecus[r]['stdType']+"#"+se.allecus[r]['dst']+"#"+se.allecus[r]['startDiagReq']) + faddr = se.allecus[r]['dst'] + if bus[:1]!='7': + if faddr in addreses[bus].keys(): + faddr = faddr+'#'+addreses[bus][faddr]['idTx']+'#'+addreses[bus][faddr]['idRx'] + else: + faddr = faddr+'##' + attr.add(faddr+"#"+se.allecus[r]['startDiagReq']) + #else: + # print(r) + del pl_id[vehTypeCode][bus][id]['refs'] + pl_id[vehTypeCode][bus][id] = attr + + vehTypeCode = '' + + + if not pl_id: #print found ecus for r in list(se.allecus.keys()): if se.allecus[r]['dst']!=addr: continue @@ -1082,7 +1112,10 @@ def findTCOM( addr, cmd, rsp, pl_id = False ): print(r, se.allecus[r]['doc'], se.allecus[r]['ids'], ecuvhc[r]) except: print() - + + if pl_id: + pickle.dump( pl_id, open( './cache/platform_attr.p', "wb" ) ) + def generateSavedEcus( eculist, fileName ):