1616import json
1717import tempfile
1818from threading import Lock
19- from .proc import Process
20- from .utils import *
2119from logging import getLogger
2220from ..locale import tr
21+ from .utils import *
22+ from .proc import run_cmd , DownloadLogFile
2323
2424
2525EXES_PATH = Path (__file__ ).with_name ('exes' )
2626logger = getLogger (__name__ )
2727
2828
29- class DownloadLogFile (object ):
30- log_file_path = Path (__file__ ).parent / 'download.log'
31- lock = Lock ()
32-
33- def __init__ (self ):
34- self .fb = None
35-
36- def __enter__ (self ):
37- self .lock .acquire ()
38- self .fb = open (self .log_file_path , 'w' , encoding = 'utf-8' )
39- return self
40-
41- def __exit__ (self , exc_type , exc_val , exc_tb ):
42- self .fb .close ()
43- self .lock .release ()
44-
45- def write (self , data ):
46- if self .fb is None :
47- raise Exception ('DownloadLogFile not open.' )
48- self .fb .write (data )
49-
50- def read (self ):
51- if self .fb is None :
52- raise Exception ('DownloadLogFile not open.' )
53- self .fb .read ()
54-
55-
56- class LineParserAsr (object ):
57-
58- def __init__ (self ):
59- self .json_string = ""
60- self .flag = False
61-
62- def parse (self , line ):
63- if line .startswith ('{' ):
64- self .json_string += line
65- self .flag = True
66- return
67-
68- if self .flag :
69- if not line .startswith ('}' ):
70- self .json_string += line
71- return
72- else :
73- self .json_string += line
74- rv = json .loads (self .json_string )
75- self .json_string = ""
76- self .flag = False
77- if rv and rv ['status' ] != 'OFFLINE' :
78- return rv ['progress' ]
79-
80-
81- class LineParser200A (object ):
82-
83- def parse (self , line ):
84- if "Successfully to prepare temp folder file for wtptp download" in line :
85- return "RESET"
86-
87- if "Download percentage" in line :
88- return int (line .strip ().split (' ' )[- 1 ])
89-
90- if "flash percentage" in line :
91- return int (line .strip ().split (' ' )[- 1 ])
92-
93-
94- class LineParserBG95 (object ):
95-
96- def __init__ (self ):
97- self .progress = 0
98-
99- def parse (self , line ):
100-
101- if '[1]DL-' in line :
102- self .progress += 2
103- return self .progress
104-
105- if '[1]Total upgrade time is' in line :
106- return 100
107-
108- if '[1]FW upgrade fail' in line :
109- raise Exception ('BG95 FW Download Failed.' )
110-
111-
112- class LineParserNB (object ):
113-
114- def parse (self , line ):
115- if '[1]FW upgrade fail.' in line :
116- raise Exception ('NB FW Download Failed.' )
117-
118- if "[1]Upgrade:" in line :
119- progress = int (line .replace ("[1]Upgrade:" , '' ).strip ()[:- 1 ])
120- return progress
121-
122-
123- class LineParserUnisoc (object ):
124-
125- def __init__ (self ):
126- self .progress = 0
127-
128- def parse (self , line ):
129-
130- if "Downloading..." in line :
131- self .progress += 1
132- return self .progress * 10
133-
134- if "DownLoad Passed" in line :
135- return 100
136-
137- if "[ERROR] DownLoad Failed" in line :
138- raise Exception ('Unisoc FW Download Failed.' )
139-
140-
141- class LineParser360W (object ):
142-
143- def __init__ (self ):
144- self .progress = 0
145-
146- def parse (self , line ):
147- try :
148- data = json .loads (line )
149- except Exception as e :
150- return 1
151-
152- if data ['Status' ] == 'Programming' :
153- return data ['Progress' ]
154- else :
155- if data ['Status' ] == 'Ready' :
156- return 5
157- elif data ['Status' ] == 'Finished' and data ['Message' ] == 'Success' :
158- return 100
159- else :
160- raise Exception ('360W Download Failed!' )
161-
162-
163- def run_cmd (cmd , platform , cwd ):
164- logger .info ('enter run_cmd method, args: {}' .format ((cmd , platform , cwd ,)))
165-
166- with DownloadLogFile () as f :
167-
168- proc = Process (cmd , cwd )
169- proc .run ()
170- running_timeout = 60
171-
172- if platform .upper () in ["ASR" , "ASR1601" , "ASR1606" ]:
173- parser = LineParserAsr ()
174- elif platform .upper () in ["UNISOC" , "UNISOC8910" , "UNISOC8850" ]:
175- parser = LineParserUnisoc ()
176- elif platform .upper () == "RDA8908A" :
177- parser = LineParserNB ()
178- elif platform .upper () == "ASR1803S" :
179- parser = LineParser200A ()
180- elif platform .upper () == "MDM9X05" :
181- parser = LineParserBG95 ()
182- elif platform .upper () == "EIGEN" :
183- pass
184- elif platform .upper () == "FCM360W" :
185- parser = LineParser360W ()
186- else :
187- pass
188-
189- for line in proc .read_lines (timeout = running_timeout ):
190- f .write (line )
191- logger .info (line )
192- rv = parser .parse (line )
193- if rv :
194- yield rv
195-
196-
19729class FwDownloadHandler (object ):
19830
19931 def __init__ (self , firmware_file_path , platform , com_info ):
@@ -354,33 +186,33 @@ def wifi41DFwDownload(self):
354186
355187 def EigenFwDownload (self ):
356188 tmp_path = tempfile .mkdtemp ()
357- logger .info ("tmp_path: " , tmp_path )
189+ logger .info ("tmp_path: {}" . format ( tmp_path ) )
358190 fdir1 = str (self .fw_filepath .parent )
359191 shutil .copytree (fdir1 , str (Path (tmp_path ) / "fw" ))
360- shutil .copytree (str (EXES_PATH / "Eigen" ), str (tmp_path / "Eigen" ))
192+ shutil .copytree (str (EXES_PATH / "Eigen" ), str (Path ( tmp_path ) / "Eigen" ))
361193
362194 try :
363195 config = configparser .ConfigParser (interpolation = None )
364196 logger .info ('quec_download_config.ini path: {}' .format (str (Path (tmp_path ) / "fw/quec_download_config.ini" )))
365197 config .read (str (Path (tmp_path ) / "fw/quec_download_config.ini" ))
366198 File_Count = int (config .get ('File' , 'File_Count' ))
367- ql .set_value ("File_Count" , File_Count )
368199
369200 ap_application_addr = config .get ('File_1' , 'START_ADDR' )
370201 ap_application_max = config .get ('File_1' , 'MAX_SIZE' )
371- ql . set_value ( " flexfile2" , ap_application_addr + " " + ap_application_max )
202+ flexfile2 = ap_application_addr + " " + ap_application_max
372203
373204 ap_updater_addr = config .get ('File_2' , 'START_ADDR' )
374205 ap_updater_max = config .get ('File_2' , 'MAX_SIZE' )
375- ql . set_value ( " flexfile3" , ap_updater_addr + " " + ap_updater_max )
206+ flexfile3 = ap_updater_addr + " " + ap_updater_max
376207
377208 customer_fs_addr = config .get ('File_3' , 'START_ADDR' )
378209 customer_fs_max = config .get ('File_3' , 'MAX_SIZE' )
379- ql . set_value ( " flexfile4" , customer_fs_addr + " " + customer_fs_max )
210+ flexfile4 = customer_fs_addr + " " + customer_fs_max
380211
381212 if File_Count == 4 :
382213 customer_backup_fs_addr = config .get ('File_4' , 'START_ADDR' )
383214 customer_backup_fs_max = config .get ('File_4' , 'MAX_SIZE' )
215+ flexfile5 = customer_backup_fs_addr + " " + customer_backup_fs_max
384216
385217 binpkg_config = configparser .ConfigParser (interpolation = None )
386218 binpkg_config_ini = str (Path (tmp_path ) / "Eigen/config.ini" )
@@ -411,18 +243,27 @@ def EigenFwDownload(self):
411243 except Exception as e :
412244 raise Exception (tr ("please check if the firmware is ok." ))
413245
414- binpkg_config = extra ['binpkg_config' ]
415- binpkg_config_ini = extra ['binpkg_config_ini' ]
416246 binpkg_config .set ('config' , 'line_0_com' , self .com_info ['port' ])
417247 with open (binpkg_config_ini , "w+" , encoding = 'utf-8' ) as f :
418248 binpkg_config .write (f )
419249
250+ extra = dict (
251+ File_Count = File_Count ,
252+ flexfile2 = flexfile2 ,
253+ flexfile3 = flexfile3 ,
254+ flexfile4 = flexfile4 ,
255+ )
256+ if File_Count == 4 :
257+ extra .update (dict (flexfile5 = flexfile5 ))
258+
420259 return self .fw_download (
421260 str (Path (tmp_path ) / "Eigen/flashtoolcli1.exe" ),
422- str (Path (tmp_path ) / "fw" / self .fw_filepath .name )
261+ str (Path (tmp_path ) / "fw" / self .fw_filepath .name ),
262+ binpkg_config_ini = binpkg_config_ini ,
263+ ** extra
423264 )
424265
425- def fw_download (self , download_exe_path , fw_filepath ):
266+ def fw_download (self , download_exe_path , fw_filepath , ** extra ):
426267 logger .info ('enter FwDownloadHandler.fw_download method.' )
427268
428269 if self .platform .upper () in ["ASR" , "ASR1601" , "ASR1606" ]:
@@ -444,7 +285,8 @@ def fw_download(self, download_exe_path, fw_filepath):
444285 cmd = [download_exe_path , self .com_info ['port' ][3 :], '115200' , fw_filepath ]
445286 logger .info ('------------------BG95 download downloading factory package(mbn)------------------' )
446287 elif self .platform .upper () == "EIGEN" :
447- cmd = [download_name , '--cfgfile ' + self .binpkg_config_ini , '--port="%s"' % comport ]
288+ binpkg_config_ini = extra ['binpkg_config_ini' ]
289+ cmd = [download_exe_path , '--cfgfile' , binpkg_config_ini , '--port' , self .com_info ['port' ]]
448290 print ('------------------Eigen downloading upgrade package(binpkg): ------------------' )
449291 elif self .platform .upper () == "FCM360W" :
450292 cmd = [download_exe_path , '-p' , self .com_info ['port' ][3 :], '-b' , "921600" , '-file' , fw_filepath ]
@@ -455,4 +297,4 @@ def fw_download(self, download_exe_path, fw_filepath):
455297 pass
456298
457299 logger .info ('run cmd: {}' .format (cmd ))
458- return run_cmd (cmd , self .platform , str (Path (download_exe_path ).parent ))
300+ return run_cmd (cmd , self .platform , str (Path (download_exe_path ).parent ), ** extra )
0 commit comments