LPCconnector/LPCBootProto.py
changeset 0 51f5a3138405
child 45 786b12887e91
equal deleted inserted replaced
-1:000000000000 0:51f5a3138405
       
     1 from LPCProto import *
       
     2 
       
     3 class LPCBootProto(LPCProto):
       
     4     def HandleTransaction(self, transaction):
       
     5         self.TransactionLock.acquire()
       
     6         try:
       
     7             transaction.SetPseudoFile(self.serialPort)
       
     8             res = transaction.ExchangeData()
       
     9         finally:
       
    10             self.TransactionLock.release()
       
    11         return "Stopped", res
       
    12     
       
    13 class LPCBootTransaction:
       
    14     def __init__(self, optdata = ""):
       
    15         self.OptData = optdata
       
    16         self.pseudofile = None
       
    17         
       
    18     def SetPseudoFile(self, pseudofile):
       
    19         self.pseudofile = pseudofile
       
    20         
       
    21     def ExchangeData(self): 
       
    22         self.pseudofile.write(self.OptData)
       
    23         return map(lambda x:self.pseudofile.readline(), xrange(self.expectedlines))
       
    24 
       
    25 class KEEPBOOTINGTransaction(LPCBootTransaction):
       
    26     def __init__(self):
       
    27         self.expectedlines = 2
       
    28         LPCBootTransaction.__init__(self, "md5\n")
       
    29 
       
    30 class STARTTransaction(LPCBootTransaction):
       
    31     def __init__(self):
       
    32         self.expectedlines = 0
       
    33         LPCBootTransaction.__init__(self, "go\n")
       
    34 
       
    35 class CHECKMD5Transaction(LPCBootTransaction):
       
    36     def __init__(self, md5ref):
       
    37         self.expectedlines = 5 
       
    38         LPCBootTransaction.__init__(self, md5ref+"md5\n")
       
    39 
       
    40 class LOADTransaction(LPCBootTransaction):
       
    41     def __init__(self, data, PLCprint):
       
    42         self.PLCprint = PLCprint
       
    43         LPCBootTransaction.__init__(self, data)
       
    44 
       
    45     def ExchangeData(self):
       
    46         #file("fw.bin","w").write(self.OptData)
       
    47         data = self.OptData
       
    48         loptdata = len(self.OptData)
       
    49         count=0
       
    50         #self.PLCprint("%dkB:" % (loptdata/1024))
       
    51         while len(data)>0:
       
    52             res = self.pseudofile.write(data[:loptdata/100])
       
    53             data = data[res:]
       
    54             count += 1
       
    55             if count % 10 == 0 :
       
    56                 self.PLCprint("%d%%" % count)
       
    57             else :
       
    58                 self.PLCprint(".")
       
    59         self.PLCprint("\n")
       
    60         return True
       
    61 
       
    62 if __name__ == "__main__":
       
    63     __builtins__.BMZ_DBG = True
       
    64     TestConnection = LPCBootProto(2,115200,1200)
       
    65     mystr=file("fw.bin").read()
       
    66     def mylog(blah):
       
    67         print blah,
       
    68 
       
    69     TestConnection.HandleTransaction(LOADTransaction(mystr, mylog))