lbessard@145: import os laurent@401: from xml.dom import minidom laurent@401: import cPickle laurent@401: laurent@401: from xmlclass import * laurent@401: lbessard@145: from CFileEditor import CFileEditor Edouard@729: from PLCControler import UndoBuffer, LOCATION_CONFNODE, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT laurent@630: laurent@630: CFileClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "cext_xsd.xsd")) lbessard@145: lbessard@145: TYPECONVERSION = {"BOOL" : "X", "SINT" : "B", "INT" : "W", "DINT" : "D", "LINT" : "L", lbessard@145: "USINT" : "B", "UINT" : "W", "UDINT" : "D", "ULINT" : "L", "REAL" : "D", "LREAL" : "L", lbessard@145: "STRING" : "B", "BYTE" : "B", "WORD" : "W", "DWORD" : "D", "LWORD" : "L", "WSTRING" : "W"} etisserant@31: Edouard@729: class CFile: etisserant@31: XSD = """ etisserant@31: lbessard@145: etisserant@31: etisserant@45: etisserant@45: etisserant@31: etisserant@31: etisserant@31: etisserant@31: """ laurent@656: EditorType = CFileEditor laurent@656: etisserant@31: def __init__(self): lbessard@145: filepath = self.CFileName() lbessard@145: lbessard@145: self.CFile = CFileClasses["CFile"]() lbessard@145: if os.path.isfile(filepath): lbessard@145: xmlfile = open(filepath, 'r') lbessard@145: tree = minidom.parse(xmlfile) lbessard@145: xmlfile.close() lbessard@145: lbessard@145: for child in tree.childNodes: lbessard@145: if child.nodeType == tree.ELEMENT_NODE and child.nodeName == "CFile": lbessard@145: self.CFile.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"]) laurent@658: self.CreateCFileBuffer(True) lbessard@145: else: laurent@658: self.CreateCFileBuffer(False) Edouard@718: self.OnCTNSave() lbessard@145: laurent@781: def GetIconName(self): laurent@781: return "Cfile" laurent@738: lbessard@145: def CFileName(self): Edouard@718: return os.path.join(self.CTNPath(), "cfile.xml") lbessard@145: lbessard@145: def GetBaseTypes(self): Edouard@718: return self.GetCTRoot().GetBaseTypes() lbessard@145: laurent@610: def GetDataTypes(self, basetypes = False, only_locatables = False): Edouard@718: return self.GetCTRoot().GetDataTypes(basetypes=basetypes, only_locatables=only_locatables) lbessard@145: lbessard@145: def GetSizeOfType(self, type): Edouard@718: return TYPECONVERSION.get(self.GetCTRoot().GetBaseType(type), None) lbessard@145: lbessard@145: def SetVariables(self, variables): lbessard@145: self.CFile.variables.setvariable([]) lbessard@145: for var in variables: lbessard@145: variable = CFileClasses["variables_variable"]() lbessard@145: variable.setname(var["Name"]) lbessard@145: variable.settype(var["Type"]) lbessard@145: variable.setclass(var["Class"]) lbessard@145: self.CFile.variables.appendvariable(variable) lbessard@145: lbessard@145: def GetVariables(self): lbessard@145: datas = [] lbessard@145: for var in self.CFile.variables.getvariable(): lbessard@145: datas.append({"Name" : var.getname(), "Type" : var.gettype(), "Class" : var.getclass()}) lbessard@145: return datas lbessard@145: laurent@401: def GetVariableLocationTree(self): Edouard@717: '''See ConfigTreeNode.GetVariableLocationTree() for a description.''' laurent@401: laurent@401: current_location = ".".join(map(str, self.GetCurrentLocation())) laurent@401: laurent@401: vars = [] Edouard@603: input = memory = output = 0 laurent@401: for var in self.CFile.variables.getvariable(): laurent@401: var_size = self.GetSizeOfType(var.gettype()) laurent@610: var_location = "" laurent@401: if var.getclass() == "input": laurent@401: var_class = LOCATION_VAR_INPUT laurent@610: if var_size is not None: laurent@610: var_location = "%%I%s%s.%d"%(var_size, current_location, input) laurent@401: input += 1 Edouard@603: elif var.getclass() == "memory": Edouard@603: var_class = LOCATION_VAR_INPUT laurent@610: if var_size is not None: laurent@610: var_location = "%%M%s%s.%d"%(var_size, current_location, memory) Edouard@603: memory += 1 laurent@401: else: laurent@401: var_class = LOCATION_VAR_OUTPUT laurent@610: if var_size is not None: laurent@610: var_location = "%%Q%s%s.%d"%(var_size, current_location, output) laurent@401: output += 1 laurent@401: vars.append({"name": var.getname(), laurent@401: "type": var_class, laurent@401: "size": var_size, laurent@401: "IEC_type": var.gettype(), laurent@659: "var_name": var.getname(), laurent@401: "location": var_location, laurent@401: "description": "", laurent@401: "children": []}) laurent@401: laurent@402: return {"name": self.BaseParams.getName(), Edouard@717: "type": LOCATION_CONFNODE, laurent@402: "location": self.GetFullIEC_Channel(), laurent@402: "children": vars} laurent@401: lbessard@145: def SetPartText(self, name, text): lbessard@145: if name == "Includes": lbessard@145: self.CFile.includes.settext(text) lbessard@145: elif name == "Globals": lbessard@145: self.CFile.globals.settext(text) lbessard@145: elif name == "Init": lbessard@145: self.CFile.initFunction.settext(text) lbessard@145: elif name == "CleanUp": lbessard@145: self.CFile.cleanUpFunction.settext(text) lbessard@145: elif name == "Retrieve": lbessard@145: self.CFile.retrieveFunction.settext(text) lbessard@145: elif name == "Publish": lbessard@145: self.CFile.publishFunction.settext(text) lbessard@145: lbessard@145: def GetPartText(self, name): lbessard@145: if name == "Includes": lbessard@145: return self.CFile.includes.gettext() lbessard@145: elif name == "Globals": lbessard@145: return self.CFile.globals.gettext() lbessard@145: elif name == "Init": lbessard@145: return self.CFile.initFunction.gettext() lbessard@145: elif name == "CleanUp": lbessard@145: return self.CFile.cleanUpFunction.gettext() lbessard@145: elif name == "Retrieve": lbessard@145: return self.CFile.retrieveFunction.gettext() lbessard@145: elif name == "Publish": lbessard@145: return self.CFile.publishFunction.gettext() lbessard@145: return "" laurent@630: Edouard@718: def CTNTestModified(self): laurent@630: return self.ChangesToSave or not self.CFileIsSaved() laurent@630: Edouard@718: def OnCTNSave(self): lbessard@145: filepath = self.CFileName() lbessard@145: lbessard@145: text = "\n" lbessard@145: extras = {"xmlns":"http://www.w3.org/2001/XMLSchema", lbessard@145: "xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance", lbessard@145: "xsi:schemaLocation" : "cext_xsd.xsd"} lbessard@145: text += self.CFile.generateXMLText("CFile", 0, extras) lbessard@145: lbessard@145: xmlfile = open(filepath,"w") laurent@430: xmlfile.write(text.encode("utf-8")) lbessard@145: xmlfile.close() lbessard@145: laurent@651: self.MarkCFileAsSaved() etisserant@31: return True etisserant@31: Edouard@718: def CTNGenerate_C(self, buildpath, locations): etisserant@31: """ etisserant@31: Generate C code Edouard@717: @param current_location: Tupple containing confnode IEC location : %I0.0.4.5 => (0,0,4,5) etisserant@31: @param locations: List of complete variables locations \ etisserant@31: [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) etisserant@31: "NAME" : name of the variable (generally "__IW0_1_2" style) etisserant@31: "DIR" : direction "Q","I" or "M" etisserant@31: "SIZE" : size "X", "B", "W", "D", "L" etisserant@31: "LOC" : tuple of interger for IEC location (0,1,2,...) etisserant@31: }, ...] etisserant@31: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND etisserant@31: """ etisserant@31: current_location = self.GetCurrentLocation() etisserant@31: # define a unique name for the generated C file laurent@401: location_str = "_".join(map(str, current_location)) lbessard@145: Edouard@717: text = "/* Code generated by Beremiz c_ext confnode */\n\n" lbessard@145: lbessard@145: # Adding includes lbessard@145: text += "/* User includes */\n" lbessard@145: text += self.CFile.includes.gettext() lbessard@145: text += "\n" lbessard@145: Edouard@953: text += '#include "iec_types.h"' lbessard@145: lbessard@145: # Adding variables lbessard@145: vars = [] Edouard@603: inputs = memories = outputs = 0 lbessard@145: for variable in self.CFile.variables.variable: lbessard@145: var = {"Name" : variable.getname(), "Type" : variable.gettype()} lbessard@145: if variable.getclass() == "input": lbessard@145: var["location"] = "__I%s%s_%d"%(self.GetSizeOfType(var["Type"]), location_str, inputs) lbessard@145: inputs += 1 Edouard@603: elif variable.getclass() == "memory": Edouard@603: var["location"] = "__M%s%s_%d"%(self.GetSizeOfType(var["Type"]), location_str, memories) Edouard@603: memories += 1 lbessard@145: else: lbessard@145: var["location"] = "__Q%s%s_%d"%(self.GetSizeOfType(var["Type"]), location_str, outputs) lbessard@145: outputs += 1 lbessard@145: vars.append(var) Edouard@717: text += "/* Beremiz c_ext confnode user variables definition */\n" Edouard@718: base_types = self.GetCTRoot().GetBaseTypes() lbessard@145: for var in vars: lbessard@145: if var["Type"] in base_types: lbessard@145: prefix = "IEC_" lbessard@145: else: lbessard@145: prefix = "" etisserant@180: text += "%s%s beremiz%s;\n"%(prefix, var["Type"], var["location"]) etisserant@180: text += "%s%s *%s = &beremiz%s;\n"%(prefix, var["Type"], var["location"], var["location"]) lbessard@145: text += "/* User variables reference */\n" lbessard@145: for var in vars: etisserant@180: text += "#define %s beremiz%s\n"%(var["Name"], var["location"]) lbessard@145: text += "\n" lbessard@145: lbessard@145: # Adding user global variables and routines lbessard@145: text += "/* User internal user variables and routines */\n" lbessard@145: text += self.CFile.globals.gettext() lbessard@145: Edouard@717: # Adding Beremiz confnode functions Edouard@717: text += "/* Beremiz confnode functions */\n" lbessard@145: text += "int __init_%s(int argc,char **argv)\n{\n"%location_str lbessard@145: text += self.CFile.initFunction.gettext() etisserant@180: text += " return 0;\n" lbessard@145: text += "\n}\n\n" lbessard@145: laurent@419: text += "void __cleanup_%s(void)\n{\n"%location_str lbessard@145: text += self.CFile.cleanUpFunction.gettext() lbessard@145: text += "\n}\n\n" lbessard@145: laurent@419: text += "void __retrieve_%s(void)\n{\n"%location_str lbessard@145: text += self.CFile.retrieveFunction.gettext() lbessard@145: text += "\n}\n\n" lbessard@145: laurent@419: text += "void __publish_%s(void)\n{\n"%location_str lbessard@145: text += self.CFile.publishFunction.gettext() lbessard@145: text += "\n}\n\n" lbessard@145: lbessard@145: Gen_Cfile_path = os.path.join(buildpath, "CFile_%s.c"%location_str) lbessard@145: cfile = open(Gen_Cfile_path,'w') lbessard@145: cfile.write(text) lbessard@145: cfile.close() lbessard@145: Edouard@718: matiec_flags = '"-I%s"'%os.path.abspath(self.GetCTRoot().GetIECLibPath()) lbessard@145: lbessard@145: return [(Gen_Cfile_path, str(self.CExtension.getCFLAGS() + matiec_flags))],str(self.CExtension.getLDFLAGS()),True laurent@656: laurent@656: lbessard@145: #------------------------------------------------------------------------------- lbessard@145: # Current Buffering Management Functions lbessard@145: #------------------------------------------------------------------------------- lbessard@145: lbessard@145: """ laurent@651: Return a copy of the cfile model lbessard@145: """ lbessard@145: def Copy(self, model): lbessard@145: return cPickle.loads(cPickle.dumps(model)) lbessard@145: laurent@658: def CreateCFileBuffer(self, saved): laurent@658: self.Buffering = False laurent@651: self.CFileBuffer = UndoBuffer(cPickle.dumps(self.CFile), saved) laurent@651: lbessard@145: def BufferCFile(self): laurent@651: self.CFileBuffer.Buffering(cPickle.dumps(self.CFile)) lbessard@145: lbessard@145: def StartBuffering(self): lbessard@145: self.Buffering = True lbessard@145: lbessard@145: def EndBuffering(self): lbessard@145: if self.Buffering: laurent@651: self.CFileBuffer.Buffering(cPickle.dumps(self.CFile)) lbessard@145: self.Buffering = False lbessard@145: laurent@651: def MarkCFileAsSaved(self): laurent@651: self.EndBuffering() laurent@651: self.CFileBuffer.CurrentSaved() laurent@651: lbessard@145: def CFileIsSaved(self): laurent@658: return self.CFileBuffer.IsCurrentSaved() and not self.Buffering laurent@658: lbessard@145: def LoadPrevious(self): laurent@651: self.EndBuffering() laurent@651: self.CFile = cPickle.loads(self.CFileBuffer.Previous()) lbessard@145: lbessard@145: def LoadNext(self): laurent@651: self.CFile = cPickle.loads(self.CFileBuffer.Next()) lbessard@145: lbessard@145: def GetBufferState(self): laurent@658: first = self.CFileBuffer.IsFirst() and not self.Buffering lbessard@145: last = self.CFileBuffer.IsLast() lbessard@145: return not first, not last lbessard@145: