]> rtime.felk.cvut.cz Git - CanFestival-3.git/blobdiff - objdictgen/node.py
fixed bug (forget to convert value in upper case)
[CanFestival-3.git] / objdictgen / node.py
index 54e574d0e038a94c99e5c4398bb600d12da7b9cd..14db770a1437a3bb683eb0b044068bce30808fe3 100755 (executable)
@@ -128,13 +128,13 @@ MappingDictionary = {
                  {"name" : "Save All Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False},
                  {"name" : "Save Communication Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False},
                  {"name" : "Save Application Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False},
-                 {"name" : "Save Manufacturer Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False, "nbmax" : 0x7C}]},
+                 {"name" : "Save Manufacturer Parameters %d[(sub - 3)]", "type" : 0x07, "access" : 'rw', "pdo" : False, "nbmax" : 0x7C}]},
     0x1011 : {"name" : "Restore Default Parameters", "struct" : array, "need" : False, "values" :
                 [{"name" : "Number of Entries", "type" : 0x05, "access" : 'ro', "pdo" : False},
                  {"name" : "Restore All Default Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False},
                  {"name" : "Restore Communication Default Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False},
                  {"name" : "Restore Application Default Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False},
-                 {"name" : "Restore Manufacturer Default Parameters", "type" : 0x07, "access" : 'rw', "pdo" : False, "nbmax" : 0x7C}]},
+                 {"name" : "Restore Manufacturer Defined Default Parameters %d[(sub - 3)]", "type" : 0x07, "access" : 'rw', "pdo" : False, "nbmax" : 0x7C}]},
     0x1012 : {"name" : "TIME COB ID", "struct" : var, "need" : False, "values" :
                 [{"name" : "TIME COB ID", "type" : 0x07, "access" : 'rw', "pdo" : False}]},
     0x1013 : {"name" : "High Resolution Timestamp", "struct" : var, "need" : False, "values" :
@@ -300,22 +300,35 @@ def FindSubentryInfos(index, subIndex, mappingdictionary):
     base_index = FindIndex(index, mappingdictionary)
     if base_index:
         struct = mappingdictionary[base_index]["struct"]
+        if struct & OD_IdenticalIndexes:
+            incr = mappingdictionary[base_index]["incr"]
+        else:
+            incr = 1
         if struct & OD_Subindex:
+            infos = None
             if struct & OD_IdenticalSubindexes:
-                if struct & OD_IdenticalIndexes:
-                    incr = mappingdictionary[base_index]["incr"]
-                else:
-                    incr = 1
                 if subIndex == 0:
-                    return mappingdictionary[base_index]["values"][0].copy()
+                    infos = mappingdictionary[base_index]["values"][0].copy()
                 elif 0 < subIndex <= mappingdictionary[base_index]["values"][1]["nbmax"]:
-                    copy = mappingdictionary[base_index]["values"][1].copy()
-                    copy["name"] = StringFormat(copy["name"], (index - base_index) / incr + 1, subIndex)
-                    return copy
-            elif struct & OD_MultipleSubindexes and 0 <= subIndex < len(mappingdictionary[base_index]["values"]):
-                return mappingdictionary[base_index]["values"][subIndex].copy()
+                    infos = mappingdictionary[base_index]["values"][1].copy()
+            elif struct & OD_MultipleSubindexes:
+                idx = 0
+                for subindex_infos in mappingdictionary[base_index]["values"]:
+                    if "nbmax" in subindex_infos:
+                        if idx <= subIndex < idx + subindex_infos["nbmax"]:
+                            infos = subindex_infos.copy()
+                            break;
+                        idx += subindex_infos["nbmax"]
+                    else:
+                        if subIndex == idx:
+                            infos = subindex_infos.copy()
+                            break;
+                        idx += 1
             elif subIndex == 0:
-                return mappingdictionary[base_index]["values"][0].copy()
+                infos = mappingdictionary[base_index]["values"][0].copy()
+            if infos is not None:
+                infos["name"] = StringFormat(infos["name"], (index - base_index) / incr + 1, subIndex)
+            return infos
     return None
 
 """
@@ -391,6 +404,8 @@ that the structure of the Object Dictionary stay coherent
 
 class Node:
     
+    DefaultStringSize = 10
+    
     def __init__(self, name = "", type = "slave", id = 0, description = "", profilename = "DS-301", profile = {}, specificmenu = []):
         self.Name = name
         self.Type = type
@@ -479,6 +494,18 @@ class Node:
     def SetProfile(self, profile):
         self.Profile = profile
     
+    """
+    Return the default string size
+    """
+    def GetDefaultStringSize(self):
+        return self.DefaultStringSize
+    
+    """
+    Define the default string size
+    """
+    def SetDefaultStringSize(self, size):
+        self.DefaultStringSize = size
+    
     """
     Define the DS-302 Profile
     """
@@ -739,10 +766,42 @@ class Node:
                 return True
             elif 0 <= subIndex < len(self.UserMapping[index]["values"]) and values != None:
                 if "type" in values:
-                    if self.IsStringType(values["type"]) and not self.IsStringType(self.UserMapping[index]["values"][subIndex]["type"]):
-                        self.SetEntry(index, subIndex, "")
-                    elif not self.IsStringType(values["type"]) and self.IsStringType(self.UserMapping[index]["values"][subIndex]["type"]):
-                        self.SetEntry(index, subIndex, 0)
+                    if self.UserMapping[index]["struct"] & OD_IdenticalSubindexes:
+                        if self.IsStringType(self.UserMapping[index]["values"][subIndex]["type"]):
+                            if self.IsRealType(values["type"]):
+                                for i in xrange(len(self.Dictionary[index])):
+                                    self.SetEntry(index, i + 1, 0.)
+                            elif not self.IsStringType(values["type"]):
+                                for i in xrange(len(self.Dictionary[index])):
+                                    self.SetEntry(index, i + 1, 0)
+                        elif self.IsRealType(self.UserMapping[index]["values"][subIndex]["type"]):
+                            if self.IsStringType(values["type"]):
+                                for i in xrange(len(self.Dictionary[index])):
+                                    self.SetEntry(index, i + 1, "")
+                            elif not self.IsRealType(values["type"]):
+                                for i in xrange(len(self.Dictionary[index])):
+                                    self.SetEntry(index, i + 1, 0)
+                        elif self.IsStringType(values["type"]):
+                            for i in xrange(len(self.Dictionary[index])):
+                                self.SetEntry(index, i + 1, "")
+                        elif self.IsRealType(values["type"]):
+                            for i in xrange(len(self.Dictionary[index])):
+                                self.SetEntry(index, i + 1, 0.)                        
+                    else:
+                        if self.IsStringType(self.UserMapping[index]["values"][subIndex]["type"]):
+                            if self.IsRealType(values["type"]):
+                                self.SetEntry(index, subIndex, 0.)
+                            elif not self.IsStringType(values["type"]):
+                                self.SetEntry(index, subIndex, 0)
+                        elif self.IsRealType(self.UserMapping[index]["values"][subIndex]["type"]):
+                            if self.IsStringType(values["type"]):
+                                self.SetEntry(index, subIndex, "")
+                            elif not self.IsRealType(values["type"]):
+                                self.SetEntry(index, subIndex, 0)
+                        elif self.IsStringType(values["type"]):
+                            self.SetEntry(index, subIndex, "")
+                        elif self.IsRealType(values["type"]):
+                            self.SetEntry(index, subIndex, 0.)
                 self.UserMapping[index]["values"][subIndex].update(values)
                 return True
         return False
@@ -858,12 +917,12 @@ class Node:
         return result
             
     def CompileValue(self, value, index, compute = True):
-        if type(value) == StringType and value.find("$NODEID") != -1:
+        if isinstance(value, (StringType, UnicodeType)) and value.upper().find("$NODEID") != -1:
             base = self.GetBaseIndex(index)
             try:
                 raw = eval(value)
                 if compute:
-                    return eval(raw.replace("$NODEID","self.ID"))
+                    return eval(raw.upper().replace("$NODEID","self.ID"))
                 return raw
             except:
                 return 0
@@ -984,7 +1043,7 @@ class Node:
 #-------------------------------------------------------------------------------
 
     def IsStringType(self, index):
-        if index in (0x9, 0xA, 0xB):
+        if index in (0x9, 0xA, 0xB, 0xF):
             return True
         elif 0xA0 <= index < 0x100:
             result = self.GetEntry(index, 1)
@@ -992,6 +1051,15 @@ class Node:
                 return True
         return False
 
+    def IsRealType(self, index):
+        if index in (0x8, 0x11):
+            return True
+        elif 0xA0 <= index < 0x100:
+            result = self.GetEntry(index, 1)
+            if result is not None and result in (0x8, 0x11):
+                return True
+        return False
+
 #-------------------------------------------------------------------------------
 #                            Type and Map Variable Lists
 #-------------------------------------------------------------------------------
@@ -1003,6 +1071,9 @@ class Node:
         list.sort()
         return ",".join(list)
 
+    def GenerateMapName(self, name, index, subindex):
+        return "%s (0x%4.4X)" % (name, index)
+
     """
     Generate the list of variables that can be mapped for the current node
     """
@@ -1014,8 +1085,9 @@ class Node:
         for index, subIndex, size, name in list:
             self.MapList += ",%s"%name
             map = "%04X%02X%02X"%(index,subIndex,size)
-            self.NameTranslation[name] = map
-            self.MapTranslation[map] = name
+            mapname = self.GenerateMapName(name, index, subIndex)
+            self.NameTranslation[mapname] = map
+            self.MapTranslation[map] = mapname
 
     def GetMapValue(self, mapname):
         if mapname == "None":
@@ -1023,7 +1095,7 @@ class Node:
         else:
             list = self.GetMapVariableList()
             for index, subIndex, size, name in list:
-                if mapname == name:
+                if mapname == self.GenerateMapName(name, index, subIndex):
                     return (index << 16) + (subIndex << 8) + size
             return None
     
@@ -1033,14 +1105,14 @@ class Node:
             subindex = (value >> 8) % (1 << 8)
             result = self.GetSubentryInfos(index, subindex)
             if result:
-                return result["name"]
+                return self.GenerateMapName(result["name"], index, subindex)
         return "None"
     
     """
     Return the list of variables that can be mapped for the current node
     """
     def GetMapList(self):
-        list = ["None"] + [name for index, subIndex, size, name in self.GetMapVariableList()]
+        list = ["None"] + [self.GenerateMapName(name, index, subIndex) for index, subIndex, size, name in self.GetMapVariableList()]
         return ",".join(list)
 
 def BE_to_LE(value):
@@ -1054,4 +1126,17 @@ def BE_to_LE(value):
     data = [char for char in value]
     data.reverse()
     return int("".join(["%2.2X"%ord(char) for char in data]), 16)
-    
\ No newline at end of file
+
+def LE_to_BE(value, size):
+    """
+    Convert Little Endian to Big Endian
+    @param value: value expressed in integer
+    @param size: number of bytes generated
+    @return: a string containing the value converted
+    """
+    
+    data = ("%" + str(size * 2) + "." + str(size * 2) + "X") % value
+    list_car = [data[i:i+2] for i in xrange(0, len(data), 2)]
+    list_car.reverse()
+    return "".join([chr(int(car, 16)) for car in list_car])
+