LPCconnector/LPCBootProto.py
author Laurent Bessard
Thu, 03 Oct 2013 22:59:38 +0200
changeset 57 4b838bdedc16
parent 45 786b12887e91
permissions -rw-r--r--
Fixed bug when starting simulation. In some case PLC status was asked before local runtime was connected.
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     1
from LPCProto import *
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     2
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     3
class LPCBootProto(LPCProto):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     4
    def HandleTransaction(self, transaction):
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
     5
        res = None
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     6
        try:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     7
            transaction.SetPseudoFile(self.serialPort)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
     8
            res = transaction.ExchangeData()
45
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
     9
        except:
786b12887e91 Moved serial transaction lock in order to encompass serial port teardown in case of error, to avoid deadlocks and multiple close in some cases
Edouard Tisserant
parents: 0
diff changeset
    10
            pass
0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    11
        return "Stopped", res
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    12
    
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    13
class LPCBootTransaction:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    14
    def __init__(self, optdata = ""):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    15
        self.OptData = optdata
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    16
        self.pseudofile = None
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    17
        
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    18
    def SetPseudoFile(self, pseudofile):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    19
        self.pseudofile = pseudofile
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    20
        
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    21
    def ExchangeData(self): 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    22
        self.pseudofile.write(self.OptData)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    23
        return map(lambda x:self.pseudofile.readline(), xrange(self.expectedlines))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    24
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    25
class KEEPBOOTINGTransaction(LPCBootTransaction):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    26
    def __init__(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    27
        self.expectedlines = 2
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    28
        LPCBootTransaction.__init__(self, "md5\n")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    29
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    30
class STARTTransaction(LPCBootTransaction):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    31
    def __init__(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    32
        self.expectedlines = 0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    33
        LPCBootTransaction.__init__(self, "go\n")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    34
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    35
class CHECKMD5Transaction(LPCBootTransaction):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    36
    def __init__(self, md5ref):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    37
        self.expectedlines = 5 
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    38
        LPCBootTransaction.__init__(self, md5ref+"md5\n")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    39
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    40
class LOADTransaction(LPCBootTransaction):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    41
    def __init__(self, data, PLCprint):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    42
        self.PLCprint = PLCprint
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    43
        LPCBootTransaction.__init__(self, data)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    44
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    45
    def ExchangeData(self):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    46
        #file("fw.bin","w").write(self.OptData)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    47
        data = self.OptData
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    48
        loptdata = len(self.OptData)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    49
        count=0
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    50
        #self.PLCprint("%dkB:" % (loptdata/1024))
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    51
        while len(data)>0:
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    52
            res = self.pseudofile.write(data[:loptdata/100])
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    53
            data = data[res:]
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    54
            count += 1
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    55
            if count % 10 == 0 :
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    56
                self.PLCprint("%d%%" % count)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    57
            else :
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    58
                self.PLCprint(".")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    59
        self.PLCprint("\n")
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    60
        return True
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    61
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    62
if __name__ == "__main__":
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    63
    __builtins__.BMZ_DBG = True
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    64
    TestConnection = LPCBootProto(2,115200,1200)
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    65
    mystr=file("fw.bin").read()
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    66
    def mylog(blah):
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    67
        print blah,
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    68
51f5a3138405 Initial commit, LPC files taken out of beremiz revision 915be999f3f0
Edouard Tisserant
parents:
diff changeset
    69
    TestConnection.HandleTransaction(LOADTransaction(mystr, mylog))