2 # -*- coding: utf-8 -*-
4 #This file is part of CanFestival, a library implementing CanOpen Stack.
6 #Copyright (C): Edouard TISSERANT, Francis DUPIN and Laurent BESSARD
8 #See COPYING file for copyrights details.
10 #This library is free software; you can redistribute it and/or
11 #modify it under the terms of the GNU Lesser General Public
12 #License as published by the Free Software Foundation; either
13 #version 2.1 of the License, or (at your option) any later version.
15 #This library is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 #Lesser General Public License for more details.
20 #You should have received a copy of the GNU Lesser General Public
21 #License along with this library; if not, write to the Free Software
22 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 from node import nosub, var, array, rec, plurivar, pluriarray, plurirec
32 # Regular expression for finding index section names
33 index_model = re.compile('([0-9A-F]{1,4})')
34 # Regular expression for finding subindex section names
35 subindex_model = re.compile('([0-9A-F]{1,4})SUB([0-9A-F]{1,2})')
37 # Regular expression for finding NodeXPresent keynames
38 nodepresent_model = re.compile('NODE([0-9]{1,3})PRESENT')
39 # Regular expression for finding NodeXName keynames
40 nodename_model = re.compile('NODE([0-9]{1,3})NAME')
41 # Regular expression for finding NodeXDCFName keynames
42 nodedcfname_model = re.compile('NODE([0-9]{1,3})DCFNAME')
44 # Dictionary for quickly translate boolean into integer value
45 BOOL_TRANSLATE = {True : "1", False : "0"}
47 # Dictionary for quickly translate eds access value into canfestival access value
48 ACCESS_TRANSLATE = {"ro" : "ro", "wo" : "wo", "rw" : "rw", "rwr" : "rw", "rww" : "rw", "const" : "ro"}
50 # Function for verifying data values
51 is_integer = lambda x: type(x) in (IntType, LongType)
52 is_string = lambda x: type(x) in (StringType, UnicodeType)
53 is_boolean = lambda x: x in (0, 1)
55 # Define checking of value for each attribute
56 ENTRY_ATTRIBUTES = {"SUBNUMBER" : is_integer, "PARAMETERNAME" : is_string,
57 "OBJECTTYPE" : lambda x: x in (7, 8, 9), "DATATYPE" : is_integer,
58 "LOWLIMIT" : is_integer, "HIGHLIMIT" : is_integer,
59 "ACCESSTYPE" : lambda x: x in ["ro","wo", "rw", "rwr", "rww", "const"],
60 "DEFAULTVALUE" : lambda x: True, "PDOMAPPING" : is_boolean,
61 "OBJFLAGS" : is_integer}
63 # Define entry parameters by entry ObjectType number
64 ENTRY_TYPES = {7 : {"name" : " VAR",
65 "require" : ["PARAMETERNAME", "DATATYPE", "ACCESSTYPE"],
66 "optional" : ["OBJECTTYPE", "DEFAULTVALUE", "PDOMAPPING", "LOWLIMIT", "HIGHLIMIT", "OBJFLAGS"]},
67 8 : {"name" : "n ARRAY",
68 "require" : ["PARAMETERNAME", "OBJECTTYPE", "SUBNUMBER"],
69 "optional" : ["OBJFLAGS"]},
70 9 : {"name" : " RECORD",
71 "require" : ["PARAMETERNAME", "OBJECTTYPE", "SUBNUMBER"],
72 "optional" : ["OBJFLAGS"]}}
75 # Function that search into Node Mappings the informations about an index or a subindex
76 # and return the default value
77 def GetDefaultValue(index, subIndex = None):
78 infos = Node.GetEntryInfos(index)
79 if infos["struct"] & node.OD_MultipleSubindexes:
80 # First case entry is a record
81 if infos["struct"] & node.OD_IdenticalSubindexes:
82 subentry_infos = Node.GetSubentryInfos(index, 1)
83 # Second case entry is an array
85 subentry_infos = Node.GetSubentryInfos(index, subIndex)
86 # If a default value is defined for this subindex, returns it
87 if "default" in subentry_infos:
88 return subentry_infos["default"]
89 # If not, returns the default value for the subindex type
91 return Node.GetTypeDefaultValue(subentry_infos["type"])
92 # Third case entry is a var
94 subentry_infos = Node.GetSubentryInfos(index, 0)
95 # If a default value is defined for this subindex, returns it
96 if "default" in subentry_infos:
97 return subentry_infos["default"]
98 # If not, returns the default value for the subindex type
100 return Node.GetTypeDefaultValue(subentry_infos["type"])
104 #-------------------------------------------------------------------------------
106 #-------------------------------------------------------------------------------
109 # List of section names that are not index and subindex and that we can meet in
111 SECTION_KEYNAMES = ["FILEINFO", "DEVICEINFO", "DUMMYUSAGE", "COMMENTS",
112 "MANDATORYOBJECTS", "OPTIONALOBJECTS", "MANUFACTUREROBJECTS"]
115 # Function that extract sections from a file and returns a dictionary of the informations
116 def ExtractSections(file):
117 return [(blocktuple[0], # EntryName : Assignements dict
118 blocktuple[-1].splitlines()) # all the lines
119 for blocktuple in [ # Split the eds files into
120 block.split("]", 1) # (EntryName,Assignements) tuple
121 for block in # for each blocks staring with '['
122 ("\n"+file).split("\n[")]
123 if blocktuple[0].isalnum()] # if EntryName exists
126 # Function that parse an CPJ file and returns a dictionary of the informations
127 def ParseCPJFile(filepath):
130 cpj_file = open(filepath,'r').read()
131 sections = ExtractSections(cpj_file)
132 # Parse assignments for each section
133 for section_name, assignments in sections:
135 # Verify that section name is TOPOLOGY
136 if section_name.upper() in "TOPOLOGY":
138 # Reset values for topology
139 topology = {"Name" : "", "Nodes" : {}}
141 for assignment in assignments:
143 if assignment.startswith(";"):
145 # Verify that line is a valid assignment
146 elif assignment.find('=') > 0:
147 # Split assignment into the two values keyname and value
148 keyname, value = assignment.split("=", 1)
150 # keyname must be immediately followed by the "=" sign, so we
151 # verify that there is no whitespace into keyname
152 if keyname.isalnum():
153 # value can be preceded and followed by whitespaces, so we escape them
154 value = value.strip()
156 # First case, value starts with "0x", then it's an hexadecimal value
157 if value.startswith("0x"):
159 computed_value = int(value, 16)
161 raise SyntaxError, "\"%s\" is not a valid value for attribute \"%s\" of section \"[%s]\""%(value, keyname, section_name)
162 elif value.isdigit():
163 # Second case, value is a number and starts with "0", then it's an octal value
164 if value.startswith("0"):
165 computed_value = int(value, 8)
166 # Third case, value is a number and don't start with "0", then it's a decimal value
168 computed_value = int(value)
169 # In any other case, we keep string value
171 computed_value = value
173 # Search if the section name match any cpj expression
174 nodepresent_result = nodepresent_model.match(keyname.upper())
175 nodename_result = nodename_model.match(keyname.upper())
176 nodedcfname_result = nodedcfname_model.match(keyname.upper())
178 if keyname.upper() == "NETNAME":
179 if not is_string(computed_value):
180 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
181 topology["Name"] = computed_value
182 elif keyname.upper() == "NODES":
183 if not is_integer(computed_value):
184 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
185 topology["Number"] = computed_value
186 elif keyname.upper() == "EDSBASENAME":
187 if not is_string(computed_value):
188 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
189 topology["Path"] = computed_value
190 elif nodepresent_result:
191 if not is_boolean(computed_value):
192 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
193 nodeid = int(nodepresent_result.groups()[0])
194 if nodeid not in topology["Nodes"].keys():
195 topology["Nodes"][nodeid] = {}
196 topology["Nodes"][nodeid]["Present"] = computed_value
197 elif nodename_result:
198 if not is_string(value):
199 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
200 nodeid = int(nodename_result.groups()[0])
201 if nodeid not in topology["Nodes"].keys():
202 topology["Nodes"][nodeid] = {}
203 topology["Nodes"][nodeid]["Name"] = computed_value
204 elif nodedcfname_result:
205 if not is_string(computed_value):
206 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
207 nodeid = int(nodedcfname_result.groups()[0])
208 if nodeid not in topology["Nodes"].keys():
209 topology["Nodes"][nodeid] = {}
210 topology["Nodes"][nodeid]["DCFName"] = computed_value
212 raise SyntaxError, "Keyname \"%s\" not recognised for section \"[%s]\""%(keyname, section_name)
214 # All lines that are not empty and are neither a comment neither not a valid assignment
215 elif assignment.strip() != "":
216 raise SyntaxError, "\"%s\" is not a valid CPJ line"%assignment.strip()
218 if "Number" not in topology.keys():
219 raise SyntaxError, "\"Nodes\" keyname in \"[%s]\" section is missing"%section_name
221 if topology["Number"] != len(topology["Nodes"]):
222 raise SyntaxError, "\"Nodes\" value not corresponding to number of nodes defined"
224 for nodeid, node in topology["Nodes"].items():
225 if "Present" not in node.keys():
226 raise SyntaxError, "\"Node%dPresent\" keyname in \"[%s]\" section is missing"%(nodeid, section_name)
228 networks.append(topology)
230 # In other case, there is a syntax problem into CPJ file
232 raise SyntaxError, "Section \"[%s]\" is unrecognized"%section_name
236 # Function that parse an EDS file and returns a dictionary of the informations
237 def ParseEDSFile(filepath):
240 eds_file = open(filepath,'r').read()
241 sections = ExtractSections(eds_file)
243 # Parse assignments for each section
244 for section_name, assignments in sections:
245 # Reset values of entry
248 # Search if the section name match an index or subindex expression
249 index_result = index_model.match(section_name.upper())
250 subindex_result = subindex_model.match(section_name.upper())
252 # Compilation of the EDS information dictionary
255 # First case, section name is in SECTION_KEYNAMES
256 if section_name.upper() in SECTION_KEYNAMES:
257 # Verify that entry is not already defined
258 if section_name.upper() not in eds_dict:
259 eds_dict[section_name.upper()] = values
261 raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
262 # Second case, section name is a subindex name
263 elif subindex_result:
264 # Extract index and subindex number
265 index, subindex = [int(value, 16) for value in subindex_result.groups()]
266 # If index hasn't been referenced before, we add an entry into the dictionary
267 # that will be updated later
268 if index not in eds_dict:
269 eds_dict[index] = {"subindexes" : {}}
270 if subindex not in eds_dict[index]["subindexes"]:
271 eds_dict[index]["subindexes"][subindex] = values
273 raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
275 # Third case, section name is an index name
277 # Extract index number
278 index = int(index_result.groups()[0], 16)
279 # If index hasn't been referenced before, we add an entry into the dictionary
280 if index not in eds_dict:
281 eds_dict[index] = values
282 eds_dict[index]["subindexes"] = {}
283 elif eds_dict[index].keys() == ["subindexes"]:
284 values["subindexes"] = eds_dict[index]["subindexes"]
285 eds_dict[index] = values
287 raise SyntaxError, "\"[%s]\" section is defined two times"%section_name
289 # In any other case, there is a syntax problem into EDS file
291 raise SyntaxError, "Section \"[%s]\" is unrecognized"%section_name
293 for assignment in assignments:
295 if assignment.startswith(";"):
297 # Verify that line is a valid assignment
298 elif assignment.find('=') > 0:
299 # Split assignment into the two values keyname and value
300 keyname, value = assignment.split("=", 1)
302 # keyname must be immediately followed by the "=" sign, so we
303 # verify that there is no whitespace into keyname
304 if keyname.isalnum():
305 # value can be preceded and followed by whitespaces, so we escape them
306 value = value.strip()
307 # First case, value starts with "$NODEID", then it's a formula
308 if value.startswith("$NODEID"):
310 test = int(value.replace("$NODEID+", ""), 16)
311 computed_value = value.replace("$NODEID", "self.ID")
313 raise SyntaxError, "\"%s\" is not a valid formula for attribute \"%s\" of section \"[%s]\""%(value, keyname, section_name)
314 # Second case, value starts with "0x", then it's an hexadecimal value
315 elif value.startswith("0x"):
317 computed_value = int(value, 16)
319 raise SyntaxError, "\"%s\" is not a valid value for attribute \"%s\" of section \"[%s]\""%(value, keyname, section_name)
320 elif value.isdigit():
321 # Third case, value is a number and starts with "0", then it's an octal value
322 if value.startswith("0"):
323 computed_value = int(value, 8)
324 # Forth case, value is a number and don't start with "0", then it's a decimal value
326 computed_value = int(value)
327 # In any other case, we keep string value
329 computed_value = value
331 # Add value to values dictionary
332 if computed_value != "":
333 # If entry is an index or a subindex
335 # Verify that keyname is a possible attribute
336 if keyname.upper() not in ENTRY_ATTRIBUTES:
337 raise SyntaxError, "Keyname \"%s\" not recognised for section \"[%s]\""%(keyname, section_name)
338 # Verify that value is valid
339 elif not ENTRY_ATTRIBUTES[keyname.upper()](computed_value):
340 raise SyntaxError, "Invalid value \"%s\" for keyname \"%s\" of section \"[%s]\""%(value, keyname, section_name)
342 values[keyname.upper()] = computed_value
344 values[keyname.upper()] = computed_value
345 # All lines that are not empty and are neither a comment neither not a valid assignment
346 elif assignment.strip() != "":
347 raise SyntaxError, "\"%s\" is not a valid EDS line"%assignment.strip()
349 # If entry is an index or a subindex
351 # Verify that entry has an ObjectType
352 if "OBJECTTYPE" in values.keys():
353 # Extract entry ObjectType
354 objecttype = values["OBJECTTYPE"]
356 # Set ObjectType to VAR by default
358 # Extract parameters defined
359 keys = Set(values.keys())
360 keys.discard("subindexes")
361 # Extract possible parameters and parameters required
362 possible = Set(ENTRY_TYPES[objecttype]["require"] + ENTRY_TYPES[objecttype]["optional"])
363 required = Set(ENTRY_TYPES[objecttype]["require"])
364 # Verify that parameters defined contains all the parameters required
365 if not keys.issuperset(required):
366 missing = required.difference(keys)._data.keys()
368 attributes = "Attributes %s are"%", ".join(["\"%s\""%attribute for attribute in missing])
370 attributes = "Attribute \"%s\" is"%missing[0]
371 raise SyntaxError, "Error on section \"[%s]\":\n%s required for a%s entry"%(section_name, attributes, ENTRY_TYPES[objecttype]["name"])
372 # Verify that parameters defined are all in the possible parameters
373 if not keys.issubset(possible):
374 unsupported = keys.difference(possible)._data.keys()
375 if len(unsupported) > 1:
376 attributes = "Attributes %s are"%", ".join(["\"%s\""%attribute for attribute in unsupported])
378 attributes = "Attribute \"%s\" is"%unsupported[0]
379 raise SyntaxError, "Error on section \"[%s]\":\n%s unsupported for a%s entry"%(section_name, attributes, ENTRY_TYPES[objecttype]["name"])
384 # Function that write an EDS file after generate it's content
385 def WriteFile(filepath, content):
386 # Open file in write mode
387 cfile = open(filepath,"w")
394 # Function that generate the EDS file content for the current node in the manager
395 def GenerateFileContent(filepath):
396 # Dictionary of each index contents
400 current_time = localtime()
401 # Extract node informations
402 nodename, nodeid, nodetype, description = Manager.GetCurrentNodeInfos()
404 # Compiling lists of indexes defined
405 entries = [idx for name, idx in Manager.GetCurrentValidIndexes(0, 0xFFFF)]
407 # Generate FileInfo section
408 fileContent = "[FileInfo]\n"
409 fileContent += "CreatedBy=CANFestival\n"
410 fileContent += "Description=%s\n"%description
411 fileContent += "CreationTime=%s"%strftime("%I:%M", current_time)
412 # %p option of strftime seems not working, then generate AM/PM by hands
413 if strftime("%I", current_time) == strftime("%H", current_time):
414 fileContent += "AM\n"
416 fileContent += "PM\n"
417 fileContent += "CreationDate=%s\n"%strftime("%m-%d-%Y", current_time)
418 fileContent += "FileName=%s\n"%os.path.split(filepath)[-1]
419 fileContent += "FileVersion=1\n"
420 fileContent += "FileRevision=1\n"
421 fileContent += "EDSVersion=3.0\n"
423 # Generate DeviceInfo section
424 fileContent += "\n[DeviceInfo]\n"
425 fileContent += "VendorName=CANFestival\n"
426 # Use information typed by user in Identity entry
427 fileContent += "VendorNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 1)
428 fileContent += "ProductName=%s\n"%nodename
429 fileContent += "ProductNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 2)
430 fileContent += "RevisionNumber=0x%8.8X\n"%Manager.GetCurrentEntry(0x1018, 3)
431 # CANFestival support all baudrates as soon as driver choosen support them
432 fileContent += "BaudRate_10=1\n"
433 fileContent += "BaudRate_20=1\n"
434 fileContent += "BaudRate_50=1\n"
435 fileContent += "BaudRate_125=1\n"
436 fileContent += "BaudRate_250=1\n"
437 fileContent += "BaudRate_500=1\n"
438 fileContent += "BaudRate_800=1\n"
439 fileContent += "BaudRate_1000=1\n"
440 # Select BootUp type from the informations given by user
441 fileContent += "SimpleBootUpMaster=%s\n"%BOOL_TRANSLATE[nodetype == "master"]
442 fileContent += "SimpleBootUpSlave=%s\n"%BOOL_TRANSLATE[nodetype == "slave"]
443 # CANFestival characteristics
444 fileContent += "Granularity=8\n"
445 fileContent += "DynamicChannelsSupported=0\n"
446 fileContent += "CompactPDO=0\n"
447 fileContent += "GroupMessaging=0\n"
448 # Calculate receive and tranmit PDO numbers with the entry available
449 fileContent += "NrOfRXPDO=%d\n"%len([idx for idx in entries if 0x1400 <= idx <= 0x15FF])
450 fileContent += "NrOfTXPDO=%d\n"%len([idx for idx in entries if 0x1800 <= idx <= 0x19FF])
451 # LSS not supported as soon as DS-302 was not fully implemented
452 fileContent += "LSS_Supported=0\n"
454 # Generate Dummy Usage section
455 fileContent += "\n[DummyUsage]\n"
456 fileContent += "Dummy0001=0\n"
457 fileContent += "Dummy0002=1\n"
458 fileContent += "Dummy0003=1\n"
459 fileContent += "Dummy0004=1\n"
460 fileContent += "Dummy0005=1\n"
461 fileContent += "Dummy0006=1\n"
462 fileContent += "Dummy0007=1\n"
464 # Generate Comments section
465 fileContent += "\n[Comments]\n"
466 fileContent += "Lines=0\n"
468 # List of entry by type (Mandatory, Optional or Manufacturer
473 # For each entry, we generate the entry section or sections if there is subindexes
474 for entry in entries:
475 # Extract infos and values for the entry
476 entry_infos = Manager.GetEntryInfos(entry)
477 values = Manager.GetCurrentEntry(entry)
478 # Define section name
479 text = "\n[%X]\n"%entry
480 # If there is only one value, it's a VAR entry
481 if type(values) != ListType:
482 # Extract the informations of the first subindex
483 subentry_infos = Manager.GetSubentryInfos(entry, 0)
484 # Generate EDS informations for the entry
485 text += "ParameterName=%s\n"%subentry_infos["name"]
486 text += "ObjectType=0x7\n"
487 text += "DataType=0x%4.4X\n"%subentry_infos["type"]
488 text += "AccessType=%s\n"%subentry_infos["access"]
489 text += "DefaultValue=%s\n"%values
490 text += "PDOMapping=%s\n"%BOOL_TRANSLATE[subentry_infos["pdo"]]
492 # Generate EDS informations for the entry
493 text += "ParameterName=%s\n"%entry_infos["name"]
494 if entry_infos["struct"] & node.OD_IdenticalSubindexes:
495 text += "ObjectType=0x9\n"
497 text += "ObjectType=0x8\n"
499 # Generate EDS informations for subindexes of the entry in a separate text
501 # Reset number of subindex defined
503 for subentry, value in enumerate(values):
504 # Extract the informations of each subindex
505 subentry_infos = Manager.GetSubentryInfos(entry, subentry)
506 # If entry is not for the compatibility, generate informations for subindex
507 if subentry_infos["name"] != "Compatibility Entry":
508 subtext += "\n[%Xsub%X]\n"%(entry, subentry)
509 subtext += "ParameterName=%s\n"%subentry_infos["name"]
510 subtext += "ObjectType=0x7\n"
511 subtext += "DataType=0x%4.4X\n"%subentry_infos["type"]
512 subtext += "AccessType=%s\n"%subentry_infos["access"]
513 subtext += "DefaultValue=%s\n"%value
514 subtext += "PDOMapping=%s\n"%BOOL_TRANSLATE[subentry_infos["pdo"]]
515 # Increment number of subindex defined
517 # Write number of subindex defined for the entry
518 text += "SubNumber=%d\n"%nb_subentry
519 # Write subindex definitions
522 # Then we add the entry in the right list
524 # First case, entry is between 0x2000 and 0x5FFF, then it's a manufacturer entry
525 if 0x2000 <= entry <= 0x5FFF:
526 manufacturers.append(entry)
527 # Second case, entry is required, then it's a mandatory entry
528 elif entry_infos["need"]:
529 mandatories.append(entry)
530 # In any other case, it's an optional entry
532 optionals.append(entry)
533 # Save text of the entry in the dictiionary of contents
534 indexContents[entry] = text
536 # Before generate File Content we sort the entry list
541 # Generate Definition of mandatory objects
542 fileContent += "\n[MandatoryObjects]\n"
543 fileContent += "SupportedObjects=%d\n"%len(mandatories)
544 for idx, entry in enumerate(mandatories):
545 fileContent += "%d=0x%4.4X\n"%(idx, entry)
546 # Write mandatory entries
547 for entry in mandatories:
548 fileContent += indexContents[entry]
550 # Generate Definition of optional objects
551 fileContent += "\n[OptionalObjects]\n"
552 fileContent += "SupportedObjects=%d\n"%len(optionals)
553 for idx, entry in enumerate(optionals):
554 fileContent += "%d=0x%4.4X\n"%(idx, entry)
555 # Write optional entries
556 for entry in optionals:
557 fileContent += indexContents[entry]
559 # Generate Definition of manufacturer objects
560 fileContent += "\n[ManufacturerObjects]\n"
561 fileContent += "SupportedObjects=%d\n"%len(manufacturers)
562 for idx, entry in enumerate(manufacturers):
563 fileContent += "%d=0x%4.4X\n"%(idx, entry)
564 # Write manufacturer entries
565 for entry in manufacturers:
566 fileContent += indexContents[entry]
568 # Return File Content
572 # Function that generates EDS file from current node edited
573 def GenerateEDSFile(filepath, manager):
577 # Generate file content
578 content = GenerateFileContent(filepath)
580 WriteFile(filepath, content)
582 except ValueError, message:
583 return "Unable to generate EDS file\n%s"%message
585 # Function that generate the CPJ file content for the nodelist
586 def GenerateCPJContent(nodelist):
587 nodes = nodelist.SlaveNodes.keys()
590 fileContent = "[TOPOLOGY]\n"
591 fileContent += "NetName=%s\n"%nodelist.GetNetworkName()
592 fileContent += "Nodes=0x%2.2X\n"%len(nodes)
595 fileContent += "Node%dPresent=0x01\n"%nodeid
596 fileContent += "Node%dName=%s\n"%(nodeid, nodelist.SlaveNodes[nodeid]["Name"])
597 fileContent += "Node%dDCFName=%s\n"%(nodeid, nodelist.SlaveNodes[nodeid]["EDS"])
599 fileContent += "EDSBaseName=eds\n"
602 # Function that generates Node from an EDS file
603 def GenerateNode(filepath, cwd, nodeID = 0):
606 Node = node.Node(id = nodeID)
608 # Parse file and extract dictionary of EDS entry
609 eds_dict = ParseEDSFile(filepath)
610 # Extract Profile Number from Device Type entry
611 ProfileNb = eds_dict[0x1000]["DEFAULTVALUE"] & 0x0000ffff
612 # If profile is not DS-301 or DS-302
613 if ProfileNb not in [301, 302]:
614 # Compile Profile name and path to .prf file
615 ProfileName = "DS-%d"%ProfileNb
616 ProfilePath = os.path.join(cwd, "config/%s.prf"%ProfileName)
617 # Verify that profile is available
618 if os.path.isfile(ProfilePath):
621 execfile(ProfilePath)
622 Node.SetProfileName(ProfileName)
623 Node.SetProfile(Mapping)
624 Node.SetSpecificMenu(AddMenuEntries)
627 # Read all entries in the EDS dictionary
628 for entry, values in eds_dict.items():
629 # All sections with a name in keynames are escaped
630 if entry in SECTION_KEYNAMES:
633 # Extract informations for the entry
634 entry_infos = Node.GetEntryInfos(entry)
636 # If no informations are available, then we write them
638 # First case, entry is a VAR
639 if values["OBJECTTYPE"] == 7:
640 # Add mapping for entry
641 Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 1)
642 # Add mapping for first subindex
643 Node.AddMappingEntry(entry, 0, values = {"name" : values["PARAMETERNAME"],
644 "type" : values["DATATYPE"],
645 "access" : ACCESS_TRANSLATE[values["ACCESSTYPE"]],
646 "pdo" : values.get("PDOMAPPING", 0) == 1})
647 # Second case, entry is an ARRAY
648 elif values["OBJECTTYPE"] == 8:
649 # Extract maximum subindex number defined
651 max_subindex = values["subindexes"][0]["DEFAULTVALUE"]
653 raise SyntaxError, "Error on entry 0x%4.4X:\nSubindex 0 must be defined for an ARRAY entry"%entry
654 # Add mapping for entry
655 Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 3)
656 # Add mapping for first subindex
657 Node.AddMappingEntry(entry, 0, values = {"name" : "Number of Entries", "type" : 0x05, "access" : "ro", "pdo" : False})
658 # Add mapping for other subindexes
659 for subindex in xrange(1, int(max_subindex) + 1):
660 # if subindex is defined
661 if subindex in values["subindexes"]:
662 Node.AddMappingEntry(entry, subindex, values = {"name" : values["subindexes"][subindex]["PARAMETERNAME"],
663 "type" : values["subindexes"][subindex]["DATATYPE"],
664 "access" : ACCESS_TRANSLATE[values["subindexes"][subindex]["ACCESSTYPE"]],
665 "pdo" : values["subindexes"][subindex].get("PDOMAPPING", 0) == 1})
666 # if not, we add a mapping for compatibility
668 Node.AddMappingEntry(entry, subindex, values = {"name" : "Compatibility Entry", "type" : 0x05, "access" : "rw", "pdo" : False})
669 # Third case, entry is an RECORD
670 elif values["OBJECTTYPE"] == 9:
671 # Verify that the first subindex is defined
672 if 0 not in values["subindexes"]:
673 raise SyntaxError, "Error on entry 0x%4.4X:\nSubindex 0 must be defined for a RECORD entry"%entry
674 # Add mapping for entry
675 Node.AddMappingEntry(entry, name = values["PARAMETERNAME"], struct = 7)
676 # Add mapping for first subindex
677 Node.AddMappingEntry(entry, 0, values = {"name" : "Number of Entries", "type" : 0x05, "access" : "ro", "pdo" : False})
678 # Verify that second subindex is defined
680 Node.AddMappingEntry(entry, 1, values = {"name" : values["PARAMETERNAME"] + " %d[(sub)]",
681 "type" : values["subindexes"][1]["DATATYPE"],
682 "access" : ACCESS_TRANSLATE[values["subindexes"][1]["ACCESSTYPE"]],
683 "pdo" : values["subindexes"][1].get("PDOMAPPING", 0) == 1})
685 raise SyntaxError, "Error on entry 0x%4.4X:\nA RECORD entry must have at least 2 subindexes"%entry
687 # Define entry for the new node
689 # First case, entry is a VAR
690 if values["OBJECTTYPE"] == 7:
691 # Take default value if it is defined
692 if "DEFAULTVALUE" in values:
693 value = values["DEFAULTVALUE"]
694 # Find default value for value type of the entry
696 value = GetDefaultValue(entry)
697 Node.AddEntry(entry, 0, value)
698 # Second case, entry is an ARRAY or a RECORD
699 elif values["OBJECTTYPE"] in (8, 9):
700 # Verify that "Subnumber" attribute is defined and has a valid value
701 if "SUBNUMBER" in values and values["SUBNUMBER"] > 0:
702 # Extract maximum subindex number defined
704 max_subindex = values["subindexes"][0]["DEFAULTVALUE"]
706 raise SyntaxError, "Error on entry 0x%4.4X:\nSubindex 0 must be defined for an ARRAY or a RECORD entry"%entry
707 # Define value for all subindexes except the first
708 for subindex in xrange(1, int(max_subindex) + 1):
709 # Take default value if it is defined and entry is defined
710 if subindex in values["subindexes"] and "DEFAULTVALUE" in values["subindexes"][subindex]:
711 value = values["subindexes"][subindex]["DEFAULTVALUE"]
712 # Find default value for value type of the subindex
714 value = GetDefaultValue(entry, subindex)
715 Node.AddEntry(entry, subindex, value)
717 raise SyntaxError, "Array or Record entry 0x%4.4X must have a \"SubNumber\" attribute"%entry
719 except SyntaxError, message:
720 return "Unable to import EDS file\n%s"%message
722 #-------------------------------------------------------------------------------
724 #-------------------------------------------------------------------------------
726 if __name__ == '__main__':
727 print ParseEDSFile("examples/PEAK MicroMod.eds")