ua_namespace.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. ###
  7. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  8. ### Version: rev 13
  9. ###
  10. ### This program was created for educational purposes and has been
  11. ### contributed to the open62541 project by the author. All licensing
  12. ### terms for this source is inherited by the terms and conditions
  13. ### specified for by the open62541 project (see the projects readme
  14. ### file for more information on the MPLv2 terms and restrictions).
  15. ###
  16. ### This program is not meant to be used in a production environment. The
  17. ### author is not liable for any complications arising due to the use of
  18. ### this program.
  19. ###
  20. from __future__ import print_function
  21. import sys
  22. from time import struct_time, strftime, strptime, mktime
  23. from struct import pack as structpack
  24. import logging
  25. from ua_builtin_types import *;
  26. from ua_node_types import *;
  27. from ua_constants import *;
  28. from open62541_MacroHelper import open62541_MacroHelper
  29. logger = logging.getLogger(__name__)
  30. def getNextElementNode(xmlvalue):
  31. if xmlvalue == None:
  32. return None
  33. xmlvalue = xmlvalue.nextSibling
  34. while not xmlvalue == None and not xmlvalue.nodeType == xmlvalue.ELEMENT_NODE:
  35. xmlvalue = xmlvalue.nextSibling
  36. return xmlvalue
  37. ###
  38. ### Namespace Organizer
  39. ###
  40. class opcua_namespace():
  41. """ Class holding and managing a set of OPCUA nodes.
  42. This class handles parsing XML description of namespaces, instantiating
  43. nodes, linking references, graphing the namespace and compiling a binary
  44. representation.
  45. Note that nodes assigned to this class are not restricted to having a
  46. single namespace ID. This class represents the entire physical address
  47. space of the binary representation and all nodes that are to be included
  48. in that segment of memory.
  49. """
  50. nodes = []
  51. nodeids = {}
  52. aliases = {}
  53. __linkLater__ = []
  54. __binaryIndirectPointers__ = []
  55. name = ""
  56. knownNodeTypes = ""
  57. namespaceIdentifiers = {} # list of 'int':'string' giving different namespace an array-mapable name
  58. def __init__(self, name):
  59. self.nodes = []
  60. self.knownNodeTypes = ['variable', 'object', 'method', 'referencetype', \
  61. 'objecttype', 'variabletype', 'methodtype', \
  62. 'datatype', 'referencetype', 'aliases']
  63. self.name = name
  64. self.nodeids = {}
  65. self.aliases = {}
  66. self.namespaceIdentifiers = {}
  67. self.__binaryIndirectPointers__ = []
  68. def addNamespace(self, numericId, stringURL):
  69. self.namespaceIdentifiers[numericId] = stringURL
  70. def linkLater(self, pointer):
  71. """ Called by nodes or references who have parsed an XML reference to a
  72. node represented by a string.
  73. No return value
  74. XML String representations of references have the form 'i=xy' or
  75. 'ns=1;s="This unique Node"'. Since during the parsing of this attribute
  76. only a subset of nodes are known/parsed, this reference string cannot be
  77. linked when encountered.
  78. References register themselves with the namespace to have their target
  79. attribute (string) parsed by linkOpenPointers() when all nodes are
  80. created, so that target can be dereferenced an point to an actual node.
  81. """
  82. self.__linkLater__.append(pointer)
  83. def getUnlinkedPointers(self):
  84. """ Return the list of references registered for linking during the next call
  85. of linkOpenPointers()
  86. """
  87. return self.__linkLater__
  88. def unlinkedItemCount(self):
  89. """ Returns the number of unlinked references that will be processed during
  90. the next call of linkOpenPointers()
  91. """
  92. return len(self.__linkLater__)
  93. def buildAliasList(self, xmlelement):
  94. """ Parses the <Alias> XML Element present in must XML NodeSet definitions.
  95. No return value
  96. Contents the Alias element are stored in a dictionary for further
  97. dereferencing during pointer linkage (see linkOpenPointer()).
  98. """
  99. if not xmlelement.tagName == "Aliases":
  100. logger.error("XMLElement passed is not an Aliaslist")
  101. return
  102. for al in xmlelement.childNodes:
  103. if al.nodeType == al.ELEMENT_NODE:
  104. if al.hasAttribute("Alias"):
  105. aliasst = al.getAttribute("Alias")
  106. if sys.version_info[0] < 3:
  107. aliasnd = unicode(al.firstChild.data)
  108. else:
  109. aliasnd = al.firstChild.data
  110. if not aliasst in self.aliases:
  111. self.aliases[aliasst] = aliasnd
  112. logger.debug("Added new alias \"" + str(aliasst) + "\" == \"" + str(aliasnd) + "\"")
  113. else:
  114. if self.aliases[aliasst] != aliasnd:
  115. logger.error("Alias definitions for " + aliasst + " differ. Have " + self.aliases[aliasst] + " but XML defines " + aliasnd + ". Keeping current definition.")
  116. def getNodeByBrowseName(self, idstring):
  117. """ Returns the first node in the nodelist whose browseName matches idstring.
  118. """
  119. matches = []
  120. for n in self.nodes:
  121. if idstring==str(n.browseName()):
  122. matches.append(n)
  123. if len(matches) > 1:
  124. logger.error("Found multiple nodes with same ID!?")
  125. if len(matches) == 0:
  126. return None
  127. else:
  128. return matches[0]
  129. def getNodeByIDString(self, idstring):
  130. """ Returns the first node in the nodelist whose id string representation
  131. matches idstring.
  132. """
  133. matches = []
  134. for n in self.nodes:
  135. if idstring==str(n.id()):
  136. matches.append(n)
  137. if len(matches) > 1:
  138. logger.error("Found multiple nodes with same ID!?")
  139. if len(matches) == 0:
  140. return None
  141. else:
  142. return matches[0]
  143. def createNode(self, ndtype, xmlelement):
  144. """ createNode is instantiates a node described by xmlelement, its type being
  145. defined by the string ndtype.
  146. No return value
  147. If the xmlelement is an <Alias>, the contents will be parsed and stored
  148. for later dereferencing during pointer linking (see linkOpenPointers).
  149. Recognized types are:
  150. * UAVariable
  151. * UAObject
  152. * UAMethod
  153. * UAView
  154. * UAVariableType
  155. * UAObjectType
  156. * UAMethodType
  157. * UAReferenceType
  158. * UADataType
  159. For every recognized type, an appropriate node class is added to the node
  160. list of the namespace. The NodeId of the given node is created and parsing
  161. of the node attributes and elements is delegated to the parseXML() and
  162. parseXMLSubType() functions of the instantiated class.
  163. If the NodeID attribute is non-unique in the node list, the creation is
  164. deferred and an error is logged.
  165. """
  166. if not isinstance(xmlelement, dom.Element):
  167. logger.error( "Error: Can not create node from invalid XMLElement")
  168. return
  169. # An ID is mandatory for everything but aliases!
  170. id = None
  171. for idname in ['NodeId', 'NodeID', 'nodeid']:
  172. if xmlelement.hasAttribute(idname):
  173. id = xmlelement.getAttribute(idname)
  174. if ndtype == 'aliases':
  175. self.buildAliasList(xmlelement)
  176. return
  177. elif id == None:
  178. logger.info( "Error: XMLElement has no id, node will not be created!")
  179. return
  180. else:
  181. id = opcua_node_id_t(id)
  182. if str(id) in self.nodeids:
  183. # Normal behavior: Do not allow duplicates, first one wins
  184. #logger.error( "XMLElement with duplicate ID " + str(id) + " found, node will not be created!")
  185. #return
  186. # Open62541 behavior for header generation: Replace the duplicate with the new node
  187. logger.info( "XMLElement with duplicate ID " + str(id) + " found, node will be replaced!")
  188. nd = self.getNodeByIDString(str(id))
  189. self.nodes.remove(nd)
  190. self.nodeids.pop(str(nd.id()))
  191. node = None
  192. if (ndtype == 'variable'):
  193. node = opcua_node_variable_t(id, self)
  194. elif (ndtype == 'object'):
  195. node = opcua_node_object_t(id, self)
  196. elif (ndtype == 'method'):
  197. node = opcua_node_method_t(id, self)
  198. elif (ndtype == 'objecttype'):
  199. node = opcua_node_objectType_t(id, self)
  200. elif (ndtype == 'variabletype'):
  201. node = opcua_node_variableType_t(id, self)
  202. elif (ndtype == 'methodtype'):
  203. node = opcua_node_methodType_t(id, self)
  204. elif (ndtype == 'datatype'):
  205. node = opcua_node_dataType_t(id, self)
  206. elif (ndtype == 'referencetype'):
  207. node = opcua_node_referenceType_t(id, self)
  208. else:
  209. logger.error( "No node constructor for type " + ndtype)
  210. if node != None:
  211. node.parseXML(xmlelement)
  212. self.nodes.append(node)
  213. self.nodeids[str(node.id())] = node
  214. def removeNodeById(self, nodeId):
  215. nd = self.getNodeByIDString(nodeId)
  216. if nd == None:
  217. return False
  218. logger.debug("Removing nodeId " + str(nodeId))
  219. self.nodes.remove(nd)
  220. if nd.getInverseReferences() != None:
  221. for ref in nd.getInverseReferences():
  222. src = ref.target();
  223. src.removeReferenceToNode(nd)
  224. return True
  225. def registerBinaryIndirectPointer(self, node):
  226. """ Appends a node to the list of nodes that should be contained in the
  227. first 765 bytes (255 pointer slots a 3 bytes) in the binary
  228. representation (indirect referencing space).
  229. This function is reserved for references and dataType pointers.
  230. """
  231. if not node in self.__binaryIndirectPointers__:
  232. self.__binaryIndirectPointers__.append(node)
  233. return self.__binaryIndirectPointers__.index(node)
  234. def getBinaryIndirectPointerIndex(self, node):
  235. """ Returns the slot/index of a pointer in the indirect referencing space
  236. (first 765 Bytes) of the binary representation.
  237. """
  238. if not node in self.__binaryIndirectPointers__:
  239. return -1
  240. return self.__binaryIndirectPointers__.index(node)
  241. def parseXML(self, xmldoc):
  242. """ Reads an XML Namespace definition and instantiates node.
  243. No return value
  244. parseXML open the file xmldoc using xml.dom.minidom and searches for
  245. the first UANodeSet Element. For every Element encountered, createNode
  246. is called to instantiate a node of the appropriate type.
  247. """
  248. typedict = {}
  249. UANodeSet = dom.parse(xmldoc).getElementsByTagName("UANodeSet")
  250. if len(UANodeSet) == 0:
  251. logger.error( "Error: No NodeSets found")
  252. return
  253. if len(UANodeSet) != 1:
  254. logger.error( "Error: Found more than 1 Nodeset in XML File")
  255. UANodeSet = UANodeSet[0]
  256. for nd in UANodeSet.childNodes:
  257. if nd.nodeType != nd.ELEMENT_NODE:
  258. continue
  259. ndType = nd.tagName.lower()
  260. if ndType[:2] == "ua":
  261. ndType = ndType[2:]
  262. elif not ndType in self.knownNodeTypes:
  263. logger.warn("XML Element or NodeType " + ndType + " is unknown and will be ignored")
  264. continue
  265. if not ndType in typedict:
  266. typedict[ndType] = 1
  267. else:
  268. typedict[ndType] = typedict[ndType] + 1
  269. self.createNode(ndType, nd)
  270. logger.debug("Currently " + str(len(self.nodes)) + " nodes in address space. Type distribution for this run was: " + str(typedict))
  271. def linkOpenPointers(self):
  272. """ Substitutes symbolic NodeIds in references for actual node instances.
  273. No return value
  274. References that have registered themselves with linkLater() to have
  275. their symbolic NodeId targets ("ns=2;i=32") substituted for an actual
  276. node will be iterated by this function. For each reference encountered
  277. in the list of unlinked/open references, the target string will be
  278. evaluated and searched for in the node list of this namespace. If found,
  279. the target attribute of the reference will be substituted for the
  280. found node.
  281. If a reference fails to get linked, it will remain in the list of
  282. unlinked references. The individual items in this list can be
  283. retrieved using getUnlinkedPointers().
  284. """
  285. linked = []
  286. logger.debug( str(self.unlinkedItemCount()) + " pointers need to get linked.")
  287. for l in self.__linkLater__:
  288. targetLinked = False
  289. if not l.target() == None and not isinstance(l.target(), opcua_node_t):
  290. if isinstance(l.target(),str) or isinstance(l.target(),unicode):
  291. # If is not a node ID, it should be an alias. Try replacing it
  292. # with a proper node ID
  293. if l.target() in self.aliases:
  294. l.target(self.aliases[l.target()])
  295. # If the link is a node ID, try to find it hopening that no ass has
  296. # defined more than one kind of id for that sucker
  297. if l.target()[:2] == "i=" or l.target()[:2] == "g=" or \
  298. l.target()[:2] == "b=" or l.target()[:2] == "s=" or \
  299. l.target()[:3] == "ns=" :
  300. tgt = self.getNodeByIDString(str(l.target()))
  301. if tgt == None:
  302. logger.error("Failed to link pointer to target (node not found) " + l.target())
  303. else:
  304. l.target(tgt)
  305. targetLinked = True
  306. else:
  307. logger.error("Failed to link pointer to target (target not Alias or Node) " + l.target())
  308. else:
  309. logger.error("Failed to link pointer to target (don't know dummy type + " + str(type(l.target())) + " +) " + str(l.target()))
  310. else:
  311. logger.error("Pointer has null target: " + str(l))
  312. referenceLinked = False
  313. if not l.referenceType() == None:
  314. if l.referenceType() in self.aliases:
  315. l.referenceType(self.aliases[l.referenceType()])
  316. tgt = self.getNodeByIDString(str(l.referenceType()))
  317. if tgt == None:
  318. logger.error("Failed to link reference type to target (node not found) " + l.referenceType())
  319. else:
  320. l.referenceType(tgt)
  321. referenceLinked = True
  322. else:
  323. referenceLinked = True
  324. if referenceLinked == True and targetLinked == True:
  325. linked.append(l)
  326. # References marked as "not forward" must be inverted (removed from source node, assigned to target node and relinked)
  327. logger.warn("Inverting reference direction for all references with isForward==False attribute (is this correct!?)")
  328. for n in self.nodes:
  329. for r in n.getReferences():
  330. if r.isForward() == False:
  331. tgt = r.target()
  332. if isinstance(tgt, opcua_node_t):
  333. nref = opcua_referencePointer_t(n, parentNode=tgt)
  334. nref.referenceType(r.referenceType())
  335. tgt.addReference(nref)
  336. # Create inverse references for all nodes
  337. logger.debug("Updating all referencedBy fields in nodes for inverse lookups.")
  338. for n in self.nodes:
  339. n.updateInverseReferences()
  340. for l in linked:
  341. self.__linkLater__.remove(l)
  342. if len(self.__linkLater__) != 0:
  343. logger.warn(str(len(self.__linkLater__)) + " could not be linked.")
  344. def sanitize(self):
  345. remove = []
  346. logger.debug("Sanitizing nodes and references...")
  347. for n in self.nodes:
  348. if n.sanitize() == False:
  349. remove.append(n)
  350. if not len(remove) == 0:
  351. logger.warn(str(len(remove)) + " nodes will be removed because they failed sanitation.")
  352. # FIXME: Some variable ns=0 nodes fail because they don't have DataType fields...
  353. # How should this be handles!?
  354. logger.warn("Not actually removing nodes... it's unclear if this is valid or not")
  355. def getRoot(self):
  356. """ Returns the first node instance with the browseName "Root".
  357. """
  358. return self.getNodeByBrowseName("Root")
  359. def buildEncodingRules(self):
  360. """ Calls buildEncoding() for all DataType nodes (opcua_node_dataType_t).
  361. No return value
  362. """
  363. stat = {True: 0, False: 0}
  364. for n in self.nodes:
  365. if isinstance(n, opcua_node_dataType_t):
  366. n.buildEncoding()
  367. stat[n.isEncodable()] = stat[n.isEncodable()] + 1
  368. logger.debug("Type definitions built/passed: " + str(stat))
  369. def allocateVariables(self):
  370. for n in self.nodes:
  371. if isinstance(n, opcua_node_variable_t):
  372. n.allocateValue()
  373. def printDot(self, filename="namespace.dot"):
  374. """ Outputs a graphiz/dot description of all nodes in the namespace.
  375. Output will written into filename to be parsed by dot/neato...
  376. Note that for namespaces with more then 20 nodes the reference structure
  377. will lead to a mostly illegible and huge graph. Use printDotGraphWalk()
  378. for plotting specific portions of a large namespace.
  379. """
  380. file=open(filename, 'w+')
  381. file.write("digraph ns {\n")
  382. for n in self.nodes:
  383. file.write(n.printDot())
  384. file.write("}\n")
  385. file.close()
  386. def getSubTypesOf(self, tdNodes = None, currentNode = None, hasSubtypeRefNode = None):
  387. # If this is a toplevel call, collect the following information as defaults
  388. if tdNodes == None:
  389. tdNodes = []
  390. if currentNode == None:
  391. currentNode = self.getNodeByBrowseName("HasTypeDefinition")
  392. tdNodes.append(currentNode)
  393. if len(tdNodes) < 1:
  394. return []
  395. if hasSubtypeRefNode == None:
  396. hasSubtypeRefNode = self.getNodeByBrowseName("HasSubtype")
  397. if hasSubtypeRefNode == None:
  398. return tdNodes
  399. # collect all subtypes of this node
  400. for ref in currentNode.getReferences():
  401. if ref.isForward() and ref.referenceType().id() == hasSubtypeRefNode.id():
  402. tdNodes.append(ref.target())
  403. self.getTypeDefinitionNodes(tdNodes=tdNodes, currentNode = ref.target(), hasSubtypeRefNode=hasSubtypeRefNode)
  404. return tdNodes
  405. def printDotGraphWalk(self, depth=1, filename="out.dot", rootNode=None, followInverse = False, excludeNodeIds=[]):
  406. """ Outputs a graphiz/dot description the nodes centered around rootNode.
  407. References beginning from rootNode will be followed for depth steps. If
  408. "followInverse = True" is passed, then inverse (not Forward) references
  409. will also be followed.
  410. Nodes can be excluded from the graph by passing a list of NodeIds as
  411. string representation using excludeNodeIds (ex ["i=53", "ns=2;i=453"]).
  412. Output is written into filename to be parsed by dot/neato/srfp...
  413. """
  414. iter = depth
  415. processed = []
  416. if rootNode == None or \
  417. not isinstance(rootNode, opcua_node_t) or \
  418. not rootNode in self.nodes:
  419. root = self.getRoot()
  420. else:
  421. root = rootNode
  422. file=open(filename, 'w+')
  423. if root == None:
  424. return
  425. file.write("digraph ns {\n")
  426. file.write(root.printDot())
  427. refs=[]
  428. if followInverse == True:
  429. refs = root.getReferences(); # + root.getInverseReferences()
  430. else:
  431. for ref in root.getReferences():
  432. if ref.isForward():
  433. refs.append(ref)
  434. while iter > 0:
  435. tmp = []
  436. for ref in refs:
  437. if isinstance(ref.target(), opcua_node_t):
  438. tgt = ref.target()
  439. if not str(tgt.id()) in excludeNodeIds:
  440. if not tgt in processed:
  441. file.write(tgt.printDot())
  442. processed.append(tgt)
  443. if ref.isForward() == False and followInverse == True:
  444. tmp = tmp + tgt.getReferences(); # + tgt.getInverseReferences()
  445. elif ref.isForward() == True :
  446. tmp = tmp + tgt.getReferences();
  447. refs = tmp
  448. iter = iter - 1
  449. file.write("}\n")
  450. file.close()
  451. def __reorder_getMinWeightNode__(self, nmatrix):
  452. rcind = -1
  453. rind = -1
  454. minweight = -1
  455. minweightnd = None
  456. for row in nmatrix:
  457. rcind += 1
  458. if row[0] == None:
  459. continue
  460. w = sum(row[1:])
  461. if minweight < 0:
  462. rind = rcind
  463. minweight = w
  464. minweightnd = row[0]
  465. elif w < minweight:
  466. rind = rcind
  467. minweight = w
  468. minweightnd = row[0]
  469. return (rind, minweightnd, minweight)
  470. def reorderNodesMinDependencies(self):
  471. # create a matrix represtantion of all node
  472. #
  473. nmatrix = []
  474. for n in range(0,len(self.nodes)):
  475. nmatrix.append([None] + [0]*len(self.nodes))
  476. typeRefs = []
  477. tn = self.getNodeByBrowseName("HasTypeDefinition")
  478. if tn != None:
  479. typeRefs.append(tn)
  480. typeRefs = typeRefs + self.getSubTypesOf(currentNode=tn)
  481. subTypeRefs = []
  482. tn = self.getNodeByBrowseName("HasSubtype")
  483. if tn != None:
  484. subTypeRefs.append(tn)
  485. subTypeRefs = subTypeRefs + self.getSubTypesOf(currentNode=tn)
  486. logger.debug("Building connectivity matrix for node order optimization.")
  487. # Set column 0 to contain the node
  488. for node in self.nodes:
  489. nind = self.nodes.index(node)
  490. nmatrix[nind][0] = node
  491. # Determine the dependencies of all nodes
  492. logger.debug("Determining node interdependencies.")
  493. for node in self.nodes:
  494. nind = self.nodes.index(node)
  495. #print "Examining node " + str(nind) + " " + str(node)
  496. for ref in node.getReferences():
  497. if isinstance(ref.target(), opcua_node_t):
  498. tind = self.nodes.index(ref.target())
  499. # Typedefinition of this node has precedence over this node
  500. if ref.referenceType() in typeRefs and ref.isForward():
  501. nmatrix[nind][tind+1] += 200 # Very big weight for typedefs
  502. # isSubTypeOf/typeDefinition of this node has precedence over this node
  503. elif ref.referenceType() in subTypeRefs and not ref.isForward():
  504. nmatrix[nind][tind+1] += 100 # Big weight for subtypes
  505. # Else the target depends on us
  506. elif ref.isForward():
  507. nmatrix[tind][nind+1] += 1 # regular weight for dependencies
  508. logger.debug("Using Djikstra topological sorting to determine printing order.")
  509. reorder = []
  510. while len(reorder) < len(self.nodes):
  511. (nind, node, w) = self.__reorder_getMinWeightNode__(nmatrix)
  512. #print str(100*float(len(reorder))/len(self.nodes)) + "% " + str(w) + " " + str(node) + " " + str(node.browseName())
  513. reorder.append(node)
  514. for ref in node.getReferences():
  515. if isinstance(ref.target(), opcua_node_t):
  516. tind = self.nodes.index(ref.target())
  517. if ref.referenceType() in typeRefs and ref.isForward():
  518. nmatrix[nind][tind+1] -= 200
  519. elif ref.referenceType() in subTypeRefs and not ref.isForward():
  520. nmatrix[nind][tind+1] -= 100
  521. elif ref.isForward():
  522. nmatrix[tind][nind+1] -= 1
  523. nmatrix[nind][0] = None
  524. self.nodes = reorder
  525. logger.debug("Nodes reordered.")
  526. return
  527. def printOpen62541Header(self, printedExternally=[], supressGenerationOfAttribute=[], outfilename=""):
  528. unPrintedNodes = []
  529. unPrintedRefs = []
  530. code = []
  531. header = []
  532. # Reorder our nodes to produce a bare minimum of bootstrapping dependencies
  533. logger.debug("Reordering nodes for minimal dependencies during printing.")
  534. self.reorderNodesMinDependencies()
  535. # Some macros (UA_EXPANDEDNODEID_MACRO()...) are easily created, but
  536. # bulky. This class will help to offload some code.
  537. codegen = open62541_MacroHelper(supressGenerationOfAttribute=supressGenerationOfAttribute)
  538. # Populate the unPrinted-Lists with everything we have.
  539. # Every Time a nodes printfunction is called, it will pop itself and
  540. # all printed references from these lists.
  541. for n in self.nodes:
  542. if not n in printedExternally:
  543. unPrintedNodes.append(n)
  544. else:
  545. logger.debug("Node " + str(n.id()) + " is being ignored.")
  546. for n in unPrintedNodes:
  547. for r in n.getReferences():
  548. if (r.target() != None) and (r.target().id() != None) and (r.parent() != None):
  549. unPrintedRefs.append(r)
  550. logger.debug(str(len(unPrintedNodes)) + " Nodes, " + str(len(unPrintedRefs)) + "References need to get printed.")
  551. header.append("/* WARNING: This is a generated file.\n * Any manual changes will be overwritten.\n\n */")
  552. code.append("/* WARNING: This is a generated file.\n * Any manual changes will be overwritten.\n\n */")
  553. header.append('#ifndef '+outfilename.upper()+'_H_')
  554. header.append('#define '+outfilename.upper()+'_H_')
  555. header.append('')
  556. header.append('#include "server/ua_server_internal.h"')
  557. header.append('#include "server/ua_nodes.h"')
  558. header.append('#include "ua_util.h"')
  559. header.append('#include "ua_types_encoding_binary.h"')
  560. header.append('#include "ua_types_generated_encoding_binary.h"')
  561. header.append('#include "ua_transport_generated_encoding_binary.h"')
  562. header.append('')
  563. header.append('/* Definition that (in userspace models) may be ')
  564. header.append(' * - not included in the amalgamated header or')
  565. header.append(' * - not part of public headers or')
  566. header.append(' * - not exported in the shared object in combination with any of the above')
  567. header.append(' * but are required for value encoding.')
  568. header.append(' * NOTE: Userspace UA_(decode|encode)Binary /wo amalgamations requires UA_EXPORT to be appended to the appropriate definitions. */')
  569. header.append('#ifndef UA_ENCODINGOFFSET_BINARY')
  570. header.append('# define UA_ENCODINGOFFSET_BINARY 2')
  571. header.append('#endif')
  572. header.append('#ifndef NULL')
  573. header.append(' #define NULL ((void *)0)')
  574. header.append('#endif')
  575. header.append('#ifndef UA_malloc')
  576. header.append(' #define UA_malloc(_p_size) malloc(_p_size)')
  577. header.append('#endif')
  578. header.append('#ifndef UA_free')
  579. header.append(' #define UA_free(_p_ptr) free(_p_ptr)')
  580. header.append('#endif')
  581. code.append('#include "'+outfilename+'.h"')
  582. code.append("UA_INLINE UA_StatusCode "+outfilename+"(UA_Server *server) {")
  583. code.append('UA_StatusCode retval = UA_STATUSCODE_GOOD; ')
  584. code.append('if(retval == UA_STATUSCODE_GOOD){retval = UA_STATUSCODE_GOOD;} //ensure that retval is used');
  585. # Before printing nodes, we need to request additional namespace arrays from the server
  586. for nsid in self.namespaceIdentifiers:
  587. if nsid == 0 or nsid==1:
  588. continue
  589. else:
  590. name = self.namespaceIdentifiers[nsid]
  591. name = name.replace("\"","\\\"")
  592. code.append("if (UA_Server_addNamespace(server, \"{0}\") != {1})\n return UA_STATUSCODE_BADUNEXPECTEDERROR;".format(name, nsid))
  593. # Find all references necessary to create the namespace and
  594. # "Bootstrap" them so all other nodes can safely use these referencetypes whenever
  595. # they can locate both source and target of the reference.
  596. logger.debug("Collecting all references used in the namespace.")
  597. refsUsed = []
  598. for n in self.nodes:
  599. # Since we are already looping over all nodes, use this chance to print NodeId defines
  600. if n.id().ns != 0:
  601. nc = n.nodeClass()
  602. if nc != NODE_CLASS_OBJECT and nc != NODE_CLASS_VARIABLE and nc != NODE_CLASS_VIEW:
  603. header = header + codegen.getNodeIdDefineString(n)
  604. # Now for the actual references...
  605. for r in n.getReferences():
  606. # Only print valid references in namespace 0 (users will not want their refs bootstrapped)
  607. if not r.referenceType() in refsUsed and r.referenceType() != None and r.referenceType().id().ns == 0:
  608. refsUsed.append(r.referenceType())
  609. logger.debug(str(len(refsUsed)) + " reference types are used in the namespace, which will now get bootstrapped.")
  610. for r in refsUsed:
  611. code = code + r.printOpen62541CCode(unPrintedNodes, unPrintedRefs);
  612. header.append("extern UA_StatusCode "+outfilename+"(UA_Server *server);\n")
  613. header.append("#endif /* "+outfilename.upper()+"_H_ */")
  614. # Note to self: do NOT - NOT! - try to iterate over unPrintedNodes!
  615. # Nodes remove themselves from this list when printed.
  616. logger.debug("Printing all other nodes.")
  617. for n in self.nodes:
  618. code = code + n.printOpen62541CCode(unPrintedNodes, unPrintedRefs, supressGenerationOfAttribute=supressGenerationOfAttribute)
  619. if len(unPrintedNodes) != 0:
  620. logger.warn("" + str(len(unPrintedNodes)) + " nodes could not be translated to code.")
  621. else:
  622. logger.debug("Printing suceeded for all nodes")
  623. if len(unPrintedRefs) != 0:
  624. logger.debug("Attempting to print " + str(len(unPrintedRefs)) + " unprinted references.")
  625. tmprefs = []
  626. for r in unPrintedRefs:
  627. if not (r.target() not in unPrintedNodes) and not (r.parent() in unPrintedNodes):
  628. if not isinstance(r.parent(), opcua_node_t):
  629. logger.debug("Reference has no parent!")
  630. elif not isinstance(r.parent().id(), opcua_node_id_t):
  631. logger.debug("Parents nodeid is not a nodeID!")
  632. else:
  633. if (len(tmprefs) == 0):
  634. code.append("// Creating leftover references:")
  635. code = code + codegen.getCreateStandaloneReference(r.parent(), r)
  636. code.append("")
  637. tmprefs.append(r)
  638. # Remove printed refs from list
  639. for r in tmprefs:
  640. unPrintedRefs.remove(r)
  641. if len(unPrintedRefs) != 0:
  642. logger.warn("" + str(len(unPrintedRefs)) + " references could not be translated to code.")
  643. else:
  644. logger.debug("Printing succeeded for all references")
  645. code.append("return UA_STATUSCODE_GOOD;")
  646. code.append("}")
  647. return (header,code)
  648. ###
  649. ### Testing
  650. ###
  651. class testing:
  652. def __init__(self):
  653. self.namespace = opcua_namespace("testing")
  654. logger.debug("Phase 1: Reading XML file nodessets")
  655. self.namespace.parseXML("Opc.Ua.NodeSet2.xml")
  656. #self.namespace.parseXML("Opc.Ua.NodeSet2.Part4.xml")
  657. #self.namespace.parseXML("Opc.Ua.NodeSet2.Part5.xml")
  658. #self.namespace.parseXML("Opc.Ua.SimulationNodeSet2.xml")
  659. logger.debug("Phase 2: Linking address space references and datatypes")
  660. self.namespace.linkOpenPointers()
  661. self.namespace.sanitize()
  662. logger.debug("Phase 3: Comprehending DataType encoding rules")
  663. self.namespace.buildEncodingRules()
  664. logger.debug("Phase 4: Allocating variable value data")
  665. self.namespace.allocateVariables()
  666. bin = self.namespace.buildBinary()
  667. f = open("binary.base64","w+")
  668. f.write(bin.encode("base64"))
  669. f.close()
  670. allnodes = self.namespace.nodes;
  671. ns = [self.namespace.getRoot()]
  672. i = 0
  673. #print "Starting depth search on " + str(len(allnodes)) + " nodes starting with from " + str(ns)
  674. while (len(ns) < len(allnodes)):
  675. i = i + 1;
  676. tmp = [];
  677. print("Iteration: " + str(i))
  678. for n in ns:
  679. tmp.append(n)
  680. for r in n.getReferences():
  681. if (not r.target() in tmp):
  682. tmp.append(r.target())
  683. print("...tmp, " + str(len(tmp)) + " nodes discovered")
  684. ns = []
  685. for n in tmp:
  686. ns.append(n)
  687. print("...done, " + str(len(ns)) + " nodes discovered")
  688. logger.debug("Phase 5: Printing pretty graph")
  689. self.namespace.printDotGraphWalk(depth=1, rootNode=self.namespace.getNodeByIDString("i=84"), followInverse=False, excludeNodeIds=["i=29", "i=22", "i=25"])
  690. #self.namespace.printDot()
  691. class testing_open62541_header:
  692. def __init__(self):
  693. self.namespace = opcua_namespace("testing")
  694. logger.debug("Phase 1: Reading XML file nodessets")
  695. self.namespace.parseXML("Opc.Ua.NodeSet2.xml")
  696. #self.namespace.parseXML("Opc.Ua.NodeSet2.Part4.xml")
  697. #self.namespace.parseXML("Opc.Ua.NodeSet2.Part5.xml")
  698. #self.namespace.parseXML("Opc.Ua.SimulationNodeSet2.xml")
  699. logger.debug("Phase 2: Linking address space references and datatypes")
  700. self.namespace.linkOpenPointers()
  701. self.namespace.sanitize()
  702. logger.debug("Phase 3: Calling C Printers")
  703. code = self.namespace.printOpen62541Header()
  704. codeout = open("./open62541_namespace.c", "w+")
  705. for line in code:
  706. codeout.write(line + "\n")
  707. codeout.close()
  708. return
  709. # Call testing routine if invoked standalone.
  710. # For better debugging, it is advised to import this file using an interactive
  711. # python shell and instantiating a namespace.
  712. #
  713. # import ua_types.py as ua; ns=ua.testing().namespace
  714. if __name__ == '__main__':
  715. tst = testing_open62541_header()