backend_open62541_nodes.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ###
  4. ### Author: Chris Iatrou (ichrispa@core-vector.net)
  5. ### Version: rev 13
  6. ###
  7. ### This program was created for educational purposes and has been
  8. ### contributed to the open62541 project by the author. All licensing
  9. ### terms for this source is inherited by the terms and conditions
  10. ### specified for by the open62541 project (see the projects readme
  11. ### file for more information on the LGPL terms and restrictions).
  12. ###
  13. ### This program is not meant to be used in a production environment. The
  14. ### author is not liable for any complications arising due to the use of
  15. ### this program.
  16. ###
  17. from nodes import *
  18. from backend_open62541_datatypes import *
  19. import re
  20. import logging
  21. import sys
  22. if sys.version_info[0] >= 3:
  23. # strings are already parsed to unicode
  24. def unicode(s):
  25. return s
  26. logger = logging.getLogger(__name__)
  27. #################
  28. # Generate Code #
  29. #################
  30. def generateNodeIdPrintable(node):
  31. if isinstance(node.id, NodeId):
  32. CodePrintable = node.__class__.__name__ + "_" + str(node.id)
  33. else:
  34. CodePrintable = node.__class__.__name__ + "_unknown_nid"
  35. return re.sub('[^0-9a-z_]+', '_', CodePrintable.lower())
  36. def generateNodeValueInstanceName(node, parent, arrayIndex):
  37. return generateNodeIdPrintable(parent) + "_" + str(node.alias) + "_" + str(arrayIndex)
  38. def generateReferenceCode(reference):
  39. if reference.isForward:
  40. return "retVal |= UA_Server_addReference(server, %s, %s, %s, true);" % \
  41. (generateNodeIdCode(reference.source),
  42. generateNodeIdCode(reference.referenceType),
  43. generateExpandedNodeIdCode(reference.target))
  44. else:
  45. return "retVal |= UA_Server_addReference(server, %s, %s, %s, false);" % \
  46. (generateNodeIdCode(reference.source),
  47. generateNodeIdCode(reference.referenceType),
  48. generateExpandedNodeIdCode(reference.target))
  49. def generateReferenceTypeNodeCode(node):
  50. code = []
  51. code.append("UA_ReferenceTypeAttributes attr = UA_ReferenceTypeAttributes_default;")
  52. if node.isAbstract:
  53. code.append("attr.isAbstract = true;")
  54. if node.symmetric:
  55. code.append("attr.symmetric = true;")
  56. if node.inverseName != "":
  57. code.append("attr.inverseName = UA_LOCALIZEDTEXT(\"\", \"%s\");" % \
  58. node.inverseName)
  59. return code
  60. def generateObjectNodeCode(node):
  61. code = []
  62. code.append("UA_ObjectAttributes attr = UA_ObjectAttributes_default;")
  63. if node.eventNotifier:
  64. code.append("attr.eventNotifier = true;")
  65. return code
  66. def setNodeDatatypeRecursive(node, nodeset):
  67. if not isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
  68. raise RuntimeError("DataType can only be set for VariableNode and VariableTypeNode")
  69. if node.dataType is not None:
  70. return
  71. # If BaseVariableType
  72. if node.id == NodeId("ns=0;i=62"):
  73. if node.dataType is None:
  74. # Set to default BaseDataType
  75. node.dataType = NodeId("ns=0;i=24")
  76. return
  77. if isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
  78. typeDefNode = nodeset.getNodeTypeDefinition(node)
  79. if typeDefNode is None:
  80. # Use the parent type.
  81. raise RuntimeError("Cannot get node for HasTypeDefinition of VariableNode " + node.browseName.name + " " + str(node.id))
  82. setNodeDatatypeRecursive(typeDefNode, nodeset)
  83. node.dataType = typeDefNode.dataType
  84. else:
  85. # Use the parent type.
  86. if node.parent is None:
  87. raise RuntimeError("Parent node not defined for " + node.browseName.name + " " + str(node.id))
  88. setNodeDatatypeRecursive(node.parent, nodeset)
  89. node.dataType = node.parent.dataType
  90. def setNodeValueRankRecursive(node, nodeset):
  91. if not isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
  92. raise RuntimeError("ValueRank can only be set for VariableNode and VariableTypeNode")
  93. if node.valueRank is not None:
  94. return
  95. # If BaseVariableType
  96. if node.id == NodeId("ns=0;i=62"):
  97. if node.valueRank is None:
  98. # BaseVariableType always has -2
  99. node.valueRank = -2
  100. return
  101. if isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
  102. typeDefNode = nodeset.getNodeTypeDefinition(node)
  103. if typeDefNode is None:
  104. # Use the parent type.
  105. raise RuntimeError("Cannot get node for HasTypeDefinition of VariableNode " + node.browseName.name + " " + str(node.id))
  106. setNodeValueRankRecursive(typeDefNode, nodeset)
  107. if typeDefNode.valueRank is not None and typeDefNode.valueRank > -1:
  108. node.valueRank = typeDefNode.valueRank
  109. else:
  110. # Default value
  111. node.valueRank = -1
  112. else:
  113. # Check if parent node limits the value rank
  114. setNodeValueRankRecursive(node.parent, nodeset)
  115. if node.parent.valueRank is not None and node.parent.valueRank > -1:
  116. node.valueRank = node.parent.valueRank
  117. else:
  118. # Default value
  119. node.valueRank = -1
  120. def generateCommonVariableCode(node, nodeset):
  121. code = []
  122. codeCleanup = []
  123. codeGlobal = []
  124. if node.valueRank is None:
  125. # Set the constrained value rank from the type/parent node
  126. setNodeValueRankRecursive(node, nodeset)
  127. code.append("/* Value rank inherited */")
  128. code.append("attr.valueRank = %d;" % node.valueRank)
  129. if node.valueRank > 0:
  130. code.append("attr.arrayDimensionsSize = %d;" % node.valueRank)
  131. code.append("UA_UInt32 arrayDimensions[{}];".format(node.valueRank))
  132. if len(node.arrayDimensions) == node.valueRank:
  133. for idx, v in enumerate(node.arrayDimensions):
  134. code.append("arrayDimensions[{}] = {};".format(idx, int(str(v))))
  135. else:
  136. for dim in range(0, node.valueRank):
  137. code.append("arrayDimensions[{}] = 0;".format(dim))
  138. code.append("attr.arrayDimensions = &arrayDimensions[0];")
  139. if node.dataType is None:
  140. # Inherit the datatype from the HasTypeDefinition reference, as stated in the OPC UA Spec:
  141. # 6.4.2
  142. # "Instances inherit the initial values for the Attributes that they have in common with the
  143. # TypeDefinitionNode from which they are instantiated, with the exceptions of the NodeClass and
  144. # NodeId."
  145. setNodeDatatypeRecursive(node, nodeset)
  146. code.append("/* DataType inherited */")
  147. dataTypeNode = nodeset.getBaseDataType(nodeset.getDataTypeNode(node.dataType))
  148. if dataTypeNode is None:
  149. raise RuntimeError("Cannot get BaseDataType for dataType : " + str(node.dataType) + " of node " + node.browseName.name + " " + str(node.id))
  150. code.append("attr.dataType = %s;" % generateNodeIdCode(node.dataType))
  151. if dataTypeNode.isEncodable():
  152. if node.value is not None:
  153. [code1, codeCleanup1, codeGlobal1] = generateValueCode(node.value, nodeset.nodes[node.id], nodeset)
  154. code += code1
  155. codeCleanup += codeCleanup1
  156. codeGlobal += codeGlobal1
  157. if node.valueRank is not None and node.valueRank > 0 and len(node.arrayDimensions) == node.valueRank and len(node.value.value) > 0:
  158. numElements = 1
  159. hasZero = False
  160. for v in node.arrayDimensions:
  161. dim = int(unicode(v))
  162. if dim > 0:
  163. numElements = numElements * dim
  164. else:
  165. hasZero = True
  166. if hasZero == False and len(node.value.value) == numElements:
  167. code.append("attr.value.arrayDimensionsSize = attr.arrayDimensionsSize;")
  168. code.append("attr.value.arrayDimensions = attr.arrayDimensions;")
  169. elif node.value is not None:
  170. raise RuntimeError("Cannot encode dataTypeNode: " + dataTypeNode.browseName.name + " for value of node " + node.browseName.name + " " + str(node.id))
  171. return [code, codeCleanup, codeGlobal]
  172. def generateVariableNodeCode(node, nodeset):
  173. code = []
  174. codeCleanup = []
  175. codeGlobal = []
  176. code.append("UA_VariableAttributes attr = UA_VariableAttributes_default;")
  177. if node.historizing:
  178. code.append("attr.historizing = true;")
  179. code.append("attr.minimumSamplingInterval = %f;" % node.minimumSamplingInterval)
  180. code.append("attr.userAccessLevel = %d;" % node.userAccessLevel)
  181. code.append("attr.accessLevel = %d;" % node.accessLevel)
  182. # in order to be compatible with mostly OPC UA client
  183. # force valueRank = -1 for scalar VariableNode
  184. if node.valueRank == -2 and node.value is not None and len(node.value.value) == 1:
  185. node.valueRank = -1
  186. [code1, codeCleanup1, codeGlobal1] = generateCommonVariableCode(node, nodeset)
  187. code += code1
  188. codeCleanup += codeCleanup1
  189. codeGlobal += codeGlobal1
  190. return [code, codeCleanup, codeGlobal]
  191. def generateVariableTypeNodeCode(node, nodeset):
  192. code = []
  193. codeCleanup = []
  194. codeGlobal = []
  195. code.append("UA_VariableTypeAttributes attr = UA_VariableTypeAttributes_default;")
  196. if node.isAbstract:
  197. code.append("attr.isAbstract = true;")
  198. [code1, codeCleanup1, codeGlobal1] = generateCommonVariableCode(node, nodeset)
  199. code += code1
  200. codeCleanup += codeCleanup1
  201. codeGlobal += codeGlobal1
  202. return [code, codeCleanup, codeGlobal]
  203. def lowerFirstChar(inputString):
  204. return inputString[0].lower() + inputString[1:]
  205. def generateExtensionObjectSubtypeCode(node, parent, nodeset, global_var_code, instanceName=None, isArrayElement=False):
  206. code = [""]
  207. codeCleanup = [""]
  208. logger.debug("Building extensionObject for " + str(parent.id))
  209. logger.debug("Value " + str(node.value))
  210. logger.debug("Encoding " + str(node.encodingRule))
  211. typeBrowseNode = makeCIdentifier(nodeset.getDataTypeNode(parent.dataType).browseName.name)
  212. #TODO: review this
  213. if typeBrowseNode == "NumericRange":
  214. # in the stack we define a separate structure for the numeric range, but
  215. # the value itself is just a string
  216. typeBrowseNode = "String"
  217. typeString = "UA_" + typeBrowseNode
  218. if instanceName is None:
  219. instanceName = generateNodeValueInstanceName(node, parent, 0)
  220. code.append("UA_STACKARRAY(" + typeString + ", " + instanceName + ", 1);")
  221. typeArr = nodeset.getDataTypeNode(parent.dataType).typesArray
  222. typeString = nodeset.getDataTypeNode(parent.dataType).browseName.name.upper()
  223. typeArrayString = typeArr + "[" + typeArr + "_" + typeString + "]"
  224. code.append("UA_init({ref}{instanceName}, &{typeArrayString});".format(ref="&" if isArrayElement else "",
  225. instanceName=instanceName,
  226. typeArrayString=typeArrayString))
  227. # Assign data to the struct contents
  228. # Track the encoding rule definition to detect arrays and/or ExtensionObjects
  229. encFieldIdx = 0
  230. for subv in node.value:
  231. encField = node.encodingRule[encFieldIdx]
  232. encFieldIdx = encFieldIdx + 1
  233. memberName = lowerFirstChar(encField[0])
  234. # Check if this is an array
  235. accessor = "." if isArrayElement else "->"
  236. if isinstance(subv, list):
  237. if len(subv) == 0:
  238. continue
  239. logger.info("ExtensionObject contains array")
  240. memberName = lowerFirstChar(encField[0])
  241. encTypeString = "UA_" + subv[0].__class__.__name__
  242. instanceNameSafe = makeCIdentifier(instanceName)
  243. code.append("UA_STACKARRAY(" + encTypeString + ", " + instanceNameSafe + "_" + memberName+", {0});".format(len(subv)))
  244. encTypeArr = nodeset.getDataTypeNode(subv[0].__class__.__name__).typesArray
  245. encTypeArrayString = encTypeArr + "[" + encTypeArr + "_" + subv[0].__class__.__name__.upper() + "]"
  246. code.append("UA_init({instanceName}, &{typeArrayString});".format(
  247. instanceName=instanceNameSafe + "_" + memberName,
  248. typeArrayString=encTypeArrayString))
  249. subArrayIdx = 0
  250. for val in subv:
  251. code.append(generateNodeValueCode(instanceNameSafe + "_" + memberName + "[" + str(subArrayIdx) + "]" +" = ", val, instanceName,instanceName + "_gehtNed_member", global_var_code, asIndirect=False))
  252. subArrayIdx = subArrayIdx + 1
  253. code.append(instanceName + accessor + memberName + " = " + instanceNameSafe+"_"+ memberName+";")
  254. continue
  255. else:
  256. logger.debug(
  257. "Encoding of field " + memberName + " is " + str(subv.encodingRule) + "defined by " + str(encField))
  258. if subv.valueRank is None or subv.valueRank == 0:
  259. if not subv.isNone():
  260. # Some values can be optional
  261. valueName = instanceName + accessor + memberName
  262. code.append(generateNodeValueCode(valueName + " = " ,
  263. subv, instanceName,valueName, global_var_code, asIndirect=False))
  264. else:
  265. memberName = lowerFirstChar(encField[0])
  266. code.append(generateNodeValueCode(instanceName + accessor + memberName + "Size = ", subv, instanceName,valueName, global_var_code, asIndirect=False))
  267. if not isArrayElement:
  268. code.append("UA_Variant_setScalar(&attr.value, " + instanceName + ", &" + typeArrayString + ");")
  269. return [code, codeCleanup]
  270. def getTypeBrowseName(dataTypeNode):
  271. typeBrowseName = makeCIdentifier(dataTypeNode.browseName.name)
  272. #TODO: review this
  273. if typeBrowseName == "NumericRange":
  274. # in the stack we define a separate structure for the numeric range, but
  275. # the value itself is just a string
  276. typeBrowseName = "String"
  277. return typeBrowseName
  278. def getTypesArrayForValue(nodeset, value):
  279. typeNode = nodeset.getNodeByBrowseName(value.__class__.__name__)
  280. if typeNode is None or value.isInternal:
  281. typesArray = "UA_TYPES"
  282. else:
  283. typesArray = typeNode.typesArray
  284. typeName = makeCIdentifier(value.__class__.__name__.upper())
  285. return "&" + typesArray + "[" + typesArray + "_" + typeName + "]"
  286. def isArrayVariableNode(node, parentNode):
  287. return parentNode.valueRank is not None and (parentNode.valueRank != -1 and (parentNode.valueRank >= 0
  288. or (len(node.value) > 1
  289. and (parentNode.valueRank != -2 or parentNode.valueRank != -3))))
  290. def generateValueCode(node, parentNode, nodeset, bootstrapping=True):
  291. code = []
  292. codeCleanup = []
  293. codeGlobal = []
  294. valueName = generateNodeIdPrintable(parentNode) + "_variant_DataContents"
  295. # node.value either contains a list of multiple identical BUILTINTYPES, or it
  296. # contains a single builtintype (which may be a container); choose if we need
  297. # to create an array or a single variable.
  298. # Note that some genious defined that there are arrays of size 1, which are
  299. # distinctly different then a single value, so we need to check that as well
  300. # Semantics:
  301. # -3: Scalar or 1-dim
  302. # -2: Scalar or x-dim | x>0
  303. # -1: Scalar
  304. # 0: x-dim | x>0
  305. # n: n-dim | n>0
  306. if (len(node.value) == 0):
  307. return ["", "", ""]
  308. if not isinstance(node.value[0], Value):
  309. return ["", "", ""]
  310. dataTypeNode = nodeset.getDataTypeNode(parentNode.dataType)
  311. if isArrayVariableNode(node, parentNode):
  312. # User the following strategy for all directly mappable values a la 'UA_Type MyInt = (UA_Type) 23;'
  313. if isinstance(node.value[0], Guid):
  314. logger.warn("Don't know how to print array of GUID in node " + str(parentNode.id))
  315. elif isinstance(node.value[0], DiagnosticInfo):
  316. logger.warn("Don't know how to print array of DiagnosticInfo in node " + str(parentNode.id))
  317. elif isinstance(node.value[0], StatusCode):
  318. logger.warn("Don't know how to print array of StatusCode in node " + str(parentNode.id))
  319. else:
  320. if isinstance(node.value[0], ExtensionObject):
  321. code.append("UA_" + getTypeBrowseName(dataTypeNode) + " " + valueName + "[" + str(len(node.value)) + "];")
  322. for idx, v in enumerate(node.value):
  323. logger.debug("Building extObj array index " + str(idx))
  324. instanceName = valueName + "[" + str(idx) + "]"
  325. [code1, codeCleanup1] = generateExtensionObjectSubtypeCode(v, parent=parentNode, nodeset=nodeset,
  326. global_var_code=codeGlobal, instanceName=instanceName,
  327. isArrayElement=True)
  328. code = code + code1
  329. codeCleanup = codeCleanup + codeCleanup1
  330. else:
  331. code.append("UA_" + node.value[0].__class__.__name__ + " " + valueName + "[" + str(len(node.value)) + "];")
  332. for idx, v in enumerate(node.value):
  333. instanceName = generateNodeValueInstanceName(v, parentNode, idx)
  334. code.append(generateNodeValueCode(
  335. valueName + "[" + str(idx) + "] = " , v, instanceName, valueName, codeGlobal))
  336. code.append("UA_Variant_setArray(&attr.value, &" + valueName +
  337. ", (UA_Int32) " + str(len(node.value)) + ", " + "&" +
  338. dataTypeNode.typesArray + "["+dataTypeNode.typesArray + "_" + getTypeBrowseName(dataTypeNode).upper() +"]);")
  339. #scalar value
  340. else:
  341. # User the following strategy for all directly mappable values a la 'UA_Type MyInt = (UA_Type) 23;'
  342. if isinstance(node.value[0], Guid):
  343. logger.warn("Don't know how to print scalar GUID in node " + str(parentNode.id))
  344. elif isinstance(node.value[0], DiagnosticInfo):
  345. logger.warn("Don't know how to print scalar DiagnosticInfo in node " + str(parentNode.id))
  346. elif isinstance(node.value[0], StatusCode):
  347. logger.warn("Don't know how to print scalar StatusCode in node " + str(parentNode.id))
  348. else:
  349. # The following strategy applies to all other types, in particular strings and numerics.
  350. if isinstance(node.value[0], ExtensionObject):
  351. [code1, codeCleanup1] = generateExtensionObjectSubtypeCode(node.value[0], parent=parentNode, nodeset=nodeset, global_var_code=codeGlobal, isArrayElement=False)
  352. code = code + code1
  353. codeCleanup = codeCleanup + codeCleanup1
  354. instanceName = generateNodeValueInstanceName(node.value[0], parentNode, 0)
  355. if not node.value[0].isNone() and not(isinstance(node.value[0], ExtensionObject)):
  356. code.append("UA_" + node.value[0].__class__.__name__ + " *" + valueName + " = UA_" + node.value[
  357. 0].__class__.__name__ + "_new();")
  358. code.append("if (!" + valueName + ") return UA_STATUSCODE_BADOUTOFMEMORY;")
  359. code.append("UA_" + node.value[0].__class__.__name__ + "_init(" + valueName + ");")
  360. code.append(generateNodeValueCode("*" + valueName + " = " , node.value[0], instanceName, valueName, codeGlobal, asIndirect=True))
  361. code.append(
  362. "UA_Variant_setScalar(&attr.value, " + valueName + ", " +
  363. getTypesArrayForValue(nodeset, node.value[0]) + ");")
  364. if node.value[0].__class__.__name__ == "ByteString":
  365. # The data is on the stack, not heap, so we can not delete the ByteString
  366. codeCleanup.append("{}->data = NULL;".format(valueName))
  367. codeCleanup.append("{}->length = 0;".format(valueName))
  368. codeCleanup.append("UA_{0}_delete({1});".format(
  369. node.value[0].__class__.__name__, valueName))
  370. return [code, codeCleanup, codeGlobal]
  371. def generateMethodNodeCode(node):
  372. code = []
  373. code.append("UA_MethodAttributes attr = UA_MethodAttributes_default;")
  374. if node.executable:
  375. code.append("attr.executable = true;")
  376. if node.userExecutable:
  377. code.append("attr.userExecutable = true;")
  378. return code
  379. def generateObjectTypeNodeCode(node):
  380. code = []
  381. code.append("UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;")
  382. if node.isAbstract:
  383. code.append("attr.isAbstract = true;")
  384. return code
  385. def generateDataTypeNodeCode(node):
  386. code = []
  387. code.append("UA_DataTypeAttributes attr = UA_DataTypeAttributes_default;")
  388. if node.isAbstract:
  389. code.append("attr.isAbstract = true;")
  390. return code
  391. def generateViewNodeCode(node):
  392. code = []
  393. code.append("UA_ViewAttributes attr = UA_ViewAttributes_default;")
  394. if node.containsNoLoops:
  395. code.append("attr.containsNoLoops = true;")
  396. code.append("attr.eventNotifier = (UA_Byte)%s;" % str(node.eventNotifier))
  397. return code
  398. def generateSubtypeOfDefinitionCode(node):
  399. for ref in node.inverseReferences:
  400. # 45 = HasSubtype
  401. if ref.referenceType.i == 45:
  402. return generateNodeIdCode(ref.target)
  403. return "UA_NODEID_NULL"
  404. def generateNodeCode_begin(node, nodeset, code_global):
  405. code = []
  406. codeCleanup = []
  407. code.append("UA_StatusCode retVal = UA_STATUSCODE_GOOD;")
  408. # Attributes
  409. if isinstance(node, ReferenceTypeNode):
  410. code.extend(generateReferenceTypeNodeCode(node))
  411. elif isinstance(node, ObjectNode):
  412. code.extend(generateObjectNodeCode(node))
  413. elif isinstance(node, VariableNode) and not isinstance(node, VariableTypeNode):
  414. [code1, codeCleanup1, codeGlobal1] = generateVariableNodeCode(node, nodeset)
  415. code.extend(code1)
  416. codeCleanup.extend(codeCleanup1)
  417. code_global.extend(codeGlobal1)
  418. elif isinstance(node, VariableTypeNode):
  419. [code1, codeCleanup1, codeGlobal1] = generateVariableTypeNodeCode(node, nodeset)
  420. code.extend(code1)
  421. codeCleanup.extend(codeCleanup1)
  422. code_global.extend(codeGlobal1)
  423. elif isinstance(node, MethodNode):
  424. code.extend(generateMethodNodeCode(node))
  425. elif isinstance(node, ObjectTypeNode):
  426. code.extend(generateObjectTypeNodeCode(node))
  427. elif isinstance(node, DataTypeNode):
  428. code.extend(generateDataTypeNodeCode(node))
  429. elif isinstance(node, ViewNode):
  430. code.extend(generateViewNodeCode(node))
  431. if node.displayName is not None:
  432. code.append("attr.displayName = " + generateLocalizedTextCode(node.displayName, alloc=False) + ";")
  433. if node.description is not None:
  434. code.append("#ifdef UA_ENABLE_NODESET_COMPILER_DESCRIPTIONS")
  435. code.append("attr.description = " + generateLocalizedTextCode(node.description, alloc=False) + ";")
  436. code.append("#endif")
  437. if node.writeMask is not None:
  438. code.append("attr.writeMask = %d;" % node.writeMask)
  439. if node.userWriteMask is not None:
  440. code.append("attr.userWriteMask = %d;" % node.userWriteMask)
  441. # AddNodes call
  442. code.append("retVal |= UA_Server_addNode_begin(server, UA_NODECLASS_{},".
  443. format(makeCIdentifier(node.__class__.__name__.upper().replace("NODE" ,""))))
  444. code.append(generateNodeIdCode(node.id) + ",")
  445. code.append(generateNodeIdCode(node.parent.id if node.parent else NodeId()) + ",")
  446. code.append(generateNodeIdCode(node.parentReference.id if node.parent else NodeId()) + ",")
  447. code.append(generateQualifiedNameCode(node.browseName) + ",")
  448. if isinstance(node, VariableNode) or isinstance(node, ObjectNode):
  449. typeDefRef = node.popTypeDef()
  450. code.append(generateNodeIdCode(typeDefRef.target) + ",")
  451. else:
  452. code.append(" UA_NODEID_NULL,")
  453. code.append("(const UA_NodeAttributes*)&attr, &UA_TYPES[UA_TYPES_{}ATTRIBUTES],NULL, NULL);".
  454. format(makeCIdentifier(node.__class__.__name__.upper().replace("NODE" ,""))))
  455. code.extend(codeCleanup)
  456. return "\n".join(code)
  457. def generateNodeCode_finish(node):
  458. code = []
  459. if isinstance(node, MethodNode):
  460. code.append("UA_Server_addMethodNode_finish(server, ")
  461. else:
  462. code.append("UA_Server_addNode_finish(server, ")
  463. code.append(generateNodeIdCode(node.id))
  464. if isinstance(node, MethodNode):
  465. code.append(", NULL, 0, NULL, 0, NULL);")
  466. else:
  467. code.append(");")
  468. return "\n".join(code)