plugins/canfestival/config_utils.py
changeset 22 9a0c535c3272
parent 11 75ae893d5eed
child 24 585d5b387b6a
equal deleted inserted replaced
21:bded6d31365c 22:9a0c535c3272
   137             ListCobIDAvailable.remove(newcobid)
   137             ListCobIDAvailable.remove(newcobid)
   138             return newcobid
   138             return newcobid
   139     return ListCobIDAvailable.pop(0)
   139     return ListCobIDAvailable.pop(0)
   140         
   140         
   141         
   141         
   142 def GenerateConciseDCF(locations, busname, nodelist):
   142 def GenerateConciseDCF(locations, current_location, nodelist):
       
   143     """
       
   144     Fills a CanFestival network editor model, with DCF with requested PDO mappings.
       
   145     @param locations: List of complete variables locations \
       
   146         [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...)
       
   147         "NAME" : name of the variable (generally "__IW0_1_2" style)
       
   148         "DIR" : direction "Q","I" or "M"
       
   149         "SIZE" : size "X", "B", "W", "D", "L"
       
   150         "LOC" : tuple of interger for IEC location (0,1,2,...)
       
   151         }, ...]
       
   152     @param nodelist: CanFestival network editor model
       
   153     @return: a modified copy of the given CanFestival network editor model
       
   154     """
       
   155     
   143     global DictLocations, DictCobID, DictLocationsNotMapped, ListCobIDAvailable, SlavesPdoNumber, DefaultTransmitTypeSlave
   156     global DictLocations, DictCobID, DictLocationsNotMapped, ListCobIDAvailable, SlavesPdoNumber, DefaultTransmitTypeSlave
   144 
   157 
   145     DictLocations = {}
   158     DictLocations = {}
   146     DictCobID = {}
   159     DictCobID = {}
   147     DictLocationsNotMapped = {}
   160     DictLocationsNotMapped = {}
   185                 pdo_cobid -= 0x80000000
   198                 pdo_cobid -= 0x80000000
   186             if pdo_cobid in ListCobIDAvailable:
   199             if pdo_cobid in ListCobIDAvailable:
   187                 ListCobIDAvailable.remove(pdo_cobid)
   200                 ListCobIDAvailable.remove(pdo_cobid)
   188     
   201     
   189     # Get list of locations check if exists and mappables -> put them in DictLocations
   202     # Get list of locations check if exists and mappables -> put them in DictLocations
   190     for locationtype, name in locations:    
   203     for location in locations:
   191         if name in DictLocations.keys():
   204         locationtype = location["IEC_TYPE"]
       
   205         name = location["NAME"]
       
   206         if name in DictLocations:
   192             if DictLocations[name]["type"] != DicoTypes[locationtype]:
   207             if DictLocations[name]["type"] != DicoTypes[locationtype]:
   193                 raise ValueError, "Conflict type for location \"%s\"" % name 
   208                 raise ValueError, "Conflict type for location \"%s\"" % name 
   194         else:
   209         else:
   195             loc = [i for i in name.split("_") if len(i) > 0]
   210             #get only the part of the location that concern this node
   196             if len(loc) not in (4, 5):
   211             loc = location["LOC"][len(current_location):]
   197                 continue
   212             # loc correspond to (ID, INDEX, SUBINDEX [,BIT])
   198             
   213             if len(loc) not in (3, 4):
   199             prefix = loc[0][0]
   214                 raise ValueError, "Bad location size"
   200             
   215             
   201             # Extract and check busname
   216             direction = location["DIR"]
   202             if loc[0][1].isdigit():
   217             
   203                 sizelocation = ""
   218             sizelocation = location["SIZE"]
   204                 busnamelocation = int(loc[0][1:])
       
   205             else:
       
   206                 sizelocation = loc[0][1]
       
   207                 busnamelocation = int(loc[0][2:])
       
   208             if busnamelocation != busname:
       
   209                 continue # A ne pas remplacer par un message d'erreur
       
   210             
   219             
   211             # Extract and check nodeid
   220             # Extract and check nodeid
   212             nodeid = int(loc[1])
   221             nodeid, index, subindex = loc[:3]
       
   222             
       
   223             # Check Id is in slave node list
   213             if nodeid not in nodelist.SlaveNodes.keys():
   224             if nodeid not in nodelist.SlaveNodes.keys():
   214                 continue
   225                 raise ValueError, "Non existing node ID : %d (variable %s)" % (nodeid,name)
       
   226             
       
   227             # Get the model for this node (made from EDS)
   215             node = nodelist.SlaveNodes[nodeid]["Node"]
   228             node = nodelist.SlaveNodes[nodeid]["Node"]
   216             
   229             
   217             # Extract and check index and subindex
   230             # Extract and check index and subindex
   218             index = int(loc[2])
       
   219             subindex = int(loc[3])
       
   220             if not node.IsEntry(index, subindex):
   231             if not node.IsEntry(index, subindex):
   221                 continue
   232                 raise ValueError, "No such index/subindex (%x,%x) in ID : %d (variable %s)" % (index,subindex,nodeid,name)
       
   233             
       
   234             #Get the entry info
   222             subentry_infos = node.GetSubentryInfos(index, subindex)
   235             subentry_infos = node.GetSubentryInfos(index, subindex)
   223             
   236             
       
   237             # If a PDO mappable
   224             if subentry_infos and subentry_infos["pdo"]:
   238             if subentry_infos and subentry_infos["pdo"]:
   225                 if sizelocation == "X" and len(loc) > 4:
   239                 if sizelocation == "X" and len(loc) > 3:
   226                     numbit = loc[4]
   240                     numbit = loc[4]
   227                 elif sizelocation != "X" and len(loc) > 4:
   241                 elif sizelocation != "X" and len(loc) > 3:
   228                     continue
   242                     raise ValueError, "Cannot set bit offset for non bool '%s' variable (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex)
   229                 else:
   243                 else:
   230                     numbit = None
   244                     numbit = None
   231                 
   245                 
   232                 locationtype = DicoTypes[locationtype]
   246                 locationtype = DicoTypes[locationtype]
   233                 entryinfos = node.GetSubentryInfos(index, subindex)
   247                 entryinfos = node.GetSubentryInfos(index, subindex)
   234                 if entryinfos["type"] != locationtype:
   248                 if entryinfos["type"] != locationtype:
   235                     raise ValueError, "Invalid type for location \"%s\"" % name
   249                     raise ValueError, "Invalid type for location \"%s\"" % name
   236                 
   250                 
   237                 typeinfos = node.GetEntryInfos(locationtype)
   251                 typeinfos = node.GetEntryInfos(locationtype)
   238                 DictLocations[name] = {"type":locationtype, "pdotype":SlavePDOType[prefix],
   252                 DictLocations[name] = {"type":locationtype, "pdotype":SlavePDOType[direction],
   239                                        "nodeid": nodeid, "index": index,"subindex": subindex, 
   253                                        "nodeid": nodeid, "index": index,"subindex": subindex, 
   240                                        "bit": numbit, "size": typeinfos["size"], "busname": busname, "sizelocation": sizelocation}
   254                                        "bit": numbit, "size": typeinfos["size"], "busname": busname, "sizelocation": sizelocation}
   241                   
   255             else:
       
   256                 raise ValueError, "Not PDO mappable variable : '%s' (ID:%d,Idx:%x,sIdx:%x))" % (name,nodeid,index,subindex)
       
   257                 
   242     # Create DictCobID with variables already mapped and add them in DictValidLocations
   258     # Create DictCobID with variables already mapped and add them in DictValidLocations
   243     for name, locationinfos in DictLocations.items():
   259     for name, locationinfos in DictLocations.items():
   244         node = nodelist.SlaveNodes[locationinfos["nodeid"]]["Node"]
   260         node = nodelist.SlaveNodes[locationinfos["nodeid"]]["Node"]
   245         result = SearchSlavePDOMapping(locationinfos, node)
   261         result = SearchSlavePDOMapping(locationinfos, node)
   246         if result != None:
   262         if result != None: