Edouard@0: from LPCProto import * Edouard@0: Edouard@0: class LPCBootProto(LPCProto): Edouard@0: def HandleTransaction(self, transaction): Edouard@45: res = None Edouard@0: try: Edouard@0: transaction.SetPseudoFile(self.serialPort) Edouard@0: res = transaction.ExchangeData() Edouard@45: except: Edouard@45: pass Edouard@0: return "Stopped", res Edouard@0: Edouard@0: class LPCBootTransaction: Edouard@0: def __init__(self, optdata = ""): Edouard@0: self.OptData = optdata Edouard@0: self.pseudofile = None Edouard@0: Edouard@0: def SetPseudoFile(self, pseudofile): Edouard@0: self.pseudofile = pseudofile Edouard@0: Edouard@0: def ExchangeData(self): Edouard@0: self.pseudofile.write(self.OptData) Edouard@0: return map(lambda x:self.pseudofile.readline(), xrange(self.expectedlines)) Edouard@0: Edouard@0: class KEEPBOOTINGTransaction(LPCBootTransaction): Edouard@0: def __init__(self): Edouard@0: self.expectedlines = 2 Edouard@0: LPCBootTransaction.__init__(self, "md5\n") Edouard@0: Edouard@0: class STARTTransaction(LPCBootTransaction): Edouard@0: def __init__(self): Edouard@0: self.expectedlines = 0 Edouard@0: LPCBootTransaction.__init__(self, "go\n") Edouard@0: Edouard@0: class CHECKMD5Transaction(LPCBootTransaction): Edouard@0: def __init__(self, md5ref): Edouard@0: self.expectedlines = 5 Edouard@0: LPCBootTransaction.__init__(self, md5ref+"md5\n") Edouard@0: Edouard@0: class LOADTransaction(LPCBootTransaction): Edouard@0: def __init__(self, data, PLCprint): Edouard@0: self.PLCprint = PLCprint Edouard@0: LPCBootTransaction.__init__(self, data) Edouard@0: Edouard@0: def ExchangeData(self): Edouard@0: #file("fw.bin","w").write(self.OptData) Edouard@0: data = self.OptData Edouard@0: loptdata = len(self.OptData) Edouard@0: count=0 Edouard@0: #self.PLCprint("%dkB:" % (loptdata/1024)) Edouard@0: while len(data)>0: Edouard@0: res = self.pseudofile.write(data[:loptdata/100]) Edouard@0: data = data[res:] Edouard@0: count += 1 Edouard@0: if count % 10 == 0 : Edouard@0: self.PLCprint("%d%%" % count) Edouard@0: else : Edouard@0: self.PLCprint(".") Edouard@0: self.PLCprint("\n") Edouard@0: return True Edouard@0: Edouard@0: if __name__ == "__main__": Edouard@0: __builtins__.BMZ_DBG = True Edouard@0: TestConnection = LPCBootProto(2,115200,1200) Edouard@0: mystr=file("fw.bin").read() Edouard@0: def mylog(blah): Edouard@0: print blah, Edouard@0: Edouard@0: TestConnection.HandleTransaction(LOADTransaction(mystr, mylog))