open62541_MacroHelper.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 logger import *
  18. from ua_constants import *
  19. __unique_item_id = 0
  20. class open62541_MacroHelper():
  21. def __init__(self, supressGenerationOfAttribute=[]):
  22. self.supressGenerationOfAttribute = supressGenerationOfAttribute
  23. def getCreateExpandedNodeIDMacro(self, node):
  24. if node.id().i != None:
  25. return "UA_EXPANDEDNODEID_NUMERIC(" + str(node.id().ns) + ", " + str(node.id().i) + ")"
  26. elif node.id().s != None:
  27. return "UA_EXPANDEDNODEID_STRING(" + str(node.id().ns) + ", " + node.id().s + ")"
  28. elif node.id().b != None:
  29. log(self, "NodeID Generation macro for bytestrings has not been implemented.")
  30. return ""
  31. elif node.id().g != None:
  32. log(self, "NodeID Generation macro for guids has not been implemented.")
  33. return ""
  34. else:
  35. return ""
  36. def getNodeIdDefineString(self, node):
  37. code = []
  38. extrNs = node.browseName().split(":")
  39. if len(extrNs) > 1:
  40. code.append("#define UA_NS" + str(node.id().ns) + "ID_" + extrNs[1].upper() + " " + str(node.id().i))
  41. else:
  42. code.append("#define UA_NS" + str(node.id().ns) + "ID_" + extrNs[0].upper() + " " + str(node.id().i))
  43. return code
  44. def getCreateNodeIDMacro(self, node):
  45. if node.id().i != None:
  46. return "UA_NODEID_NUMERIC(" + str(node.id().ns) + ", " + str(node.id().i) + ")"
  47. elif node.id().s != None:
  48. return "UA_NODEID_STRING(" + str(node.id().ns) + ", " + node.id().s + ")"
  49. elif node.id().b != None:
  50. log(self, "NodeID Generation macro for bytestrings has not been implemented.")
  51. return ""
  52. elif node.id().g != None:
  53. log(self, "NodeID Generation macro for guids has not been implemented.")
  54. return ""
  55. else:
  56. return ""
  57. def getCreateStandaloneReference(self, sourcenode, reference):
  58. # As reference from open62541 (we need to alter the attributes)
  59. # UA_Server_addReference(UA_Server *server, const UA_NodeId sourceId, const UA_NodeId refTypeId,
  60. # const UA_ExpandedNodeId targetId)
  61. code = []
  62. #refid = "ref_" + reference.getCodePrintableID()
  63. #code.append("UA_AddReferencesItem " + refid + ";")
  64. #code.append("UA_AddReferencesItem_init(&" + refid + ");")
  65. #code.append(refid + ".sourceNodeId = " + self.getCreateNodeIDMacro(sourcenode) + ";")
  66. #code.append(refid + ".referenceTypeId = " + self.getCreateNodeIDMacro(reference.referenceType()) + ";")
  67. #if reference.isForward():
  68. #code.append(refid + ".isForward = UA_TRUE;")
  69. #else:
  70. #code.append(refid + ".isForward = UA_FALSE;")
  71. #code.append(refid + ".targetNodeId = " + self.getCreateExpandedNodeIDMacro(reference.target()) + ";")
  72. #code.append("addOneWayReferenceWithSession(server, (UA_Session *) UA_NULL, &" + refid + ");")
  73. if reference.isForward():
  74. code.append("UA_Server_addReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", UA_TRUE);")
  75. else:
  76. code.append("UA_Server_addReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", UA_FALSE);")
  77. return code
  78. def getCreateNodeNoBootstrap(self, node, parentNode, parentReference):
  79. code = []
  80. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  81. if node.nodeClass() == NODE_CLASS_OBJECT:
  82. nodetype = "Object"
  83. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  84. nodetype = "Variable"
  85. elif node.nodeClass() == NODE_CLASS_METHOD:
  86. nodetype = "Method"
  87. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  88. nodetype = "ObjectType"
  89. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  90. nodetype = "ReferenceType"
  91. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  92. nodetype = "VariableType"
  93. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  94. nodetype = "DataType"
  95. elif node.nodeClass() == NODE_CLASS_VIEW:
  96. nodetype = "View"
  97. else:
  98. code.append("/* undefined nodeclass */")
  99. return code;
  100. code.append("UA_%sAttributes attr;" % nodetype)
  101. code.append("UA_%sAttributes_init(&attr);" % nodetype);
  102. code.append("attr.displayName = UA_LOCALIZEDTEXT(\"\", \"" + str(node.displayName()) + "\");")
  103. code.append("attr.description = UA_LOCALIZEDTEXT(\"\", \"" + str(node.description()) + "\");")
  104. code.append("UA_NodeId nodeId = " + str(self.getCreateNodeIDMacro(node)) + ";")
  105. if nodetype in ["Object", "Variable"]:
  106. code.append("UA_NodeId typeDefinition = UA_NODEID_NULL;") # todo instantiation of object and variable types
  107. code.append("UA_NodeId parentNodeId = " + str(self.getCreateNodeIDMacro(parentNode)) + ";")
  108. code.append("UA_NodeId parentReferenceNodeId = " + str(self.getCreateNodeIDMacro(parentReference.referenceType())) + ";")
  109. extrNs = node.browseName().split(":")
  110. if len(extrNs) > 1:
  111. code.append("UA_QualifiedName nodeName = UA_QUALIFIEDNAME(" + str(extrNs[0]) + ", \"" + extrNs[1] + "\");")
  112. else:
  113. code.append("UA_QualifiedName nodeName = UA_QUALIFIEDNAME(0, \"" + str(node.browseName()) + "\");")
  114. code.append("UA_Server_add%sNode(server, nodeId, parentNodeId, parentReferenceNodeId, nodeName" % nodetype)
  115. if nodetype in ["Object", "Variable"]:
  116. code.append(" , typeDefinition")
  117. code.append(" , attr, NULL);")
  118. return code
  119. def getCreateNodeBootstrap(self, node):
  120. nodetype = ""
  121. code = []
  122. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  123. code.append("/* sorry, nodebootstrap needs to be updated */")
  124. if node.nodeClass() == NODE_CLASS_OBJECT:
  125. nodetype = "Object"
  126. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  127. nodetype = "Variable"
  128. elif node.nodeClass() == NODE_CLASS_METHOD:
  129. nodetype = "Method"
  130. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  131. nodetype = "ObjectType"
  132. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  133. nodetype = "ReferenceType"
  134. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  135. nodetype = "VariableType"
  136. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  137. nodetype = "DataType"
  138. elif node.nodeClass() == NODE_CLASS_VIEW:
  139. nodetype = "View"
  140. else:
  141. code.append("/* undefined nodeclass */")
  142. return;
  143. code.append("UA_%sAttributes attr;\nUA_%sAttributes_init(&attr);" % (nodetype, nodetype));
  144. code.append(nodetype + " *" + node.getCodePrintableID() + " = " + nodetype + "_new();")
  145. if not "browsename" in self.supressGenerationOfAttribute:
  146. extrNs = node.browseName().split(":")
  147. if len(extrNs) > 1:
  148. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(" + str(extrNs[0]) + ", \"" + extrNs[1] + "\");")
  149. else:
  150. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(0, \"" + node.browseName() + "\");")
  151. if not "displayname" in self.supressGenerationOfAttribute:
  152. code.append(node.getCodePrintableID() + "->displayName = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.displayName() + "\");")
  153. if not "description" in self.supressGenerationOfAttribute:
  154. code.append(node.getCodePrintableID() + "->description = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.description() + "\");")
  155. if not "writemask" in self.supressGenerationOfAttribute:
  156. if node.__node_writeMask__ != 0:
  157. code.append(node.getCodePrintableID() + "->writeMask = (UA_Int32) " + str(node.__node_writeMask__) + ";")
  158. if not "userwritemask" in self.supressGenerationOfAttribute:
  159. if node.__node_userWriteMask__ != 0:
  160. code.append(node.getCodePrintableID() + "->userWriteMask = (UA_Int32) " + str(node.__node_userWriteMask__) + ";")
  161. #FIXME: Allocate descriptions, etc.
  162. if not "nodeid" in self.supressGenerationOfAttribute:
  163. if node.id().ns != 0:
  164. code.append(node.getCodePrintableID() + "->nodeId.namespaceIndex = " + str(node.id().ns) + ";")
  165. if node.id().i != None:
  166. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = " + str(node.id().i) + ";")
  167. elif node.id().b != None:
  168. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_BYTESTRING;")
  169. log(self, "ByteString IDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  170. return []
  171. elif node.id().g != None:
  172. #<jpfr> the string is sth like { .length = 111, .data = <ptr> }
  173. #<jpfr> there you _may_ alloc the <ptr> on the heap
  174. #<jpfr> for the guid, just set it to {.data1 = 111, .data2 = 2222, ....
  175. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_GUID;")
  176. log(self, "GUIDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  177. return []
  178. elif node.id().s != None:
  179. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_STRING;")
  180. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = UA_STRING_ALLOC(\"" + str(node.id().i) + "\");")
  181. else:
  182. log(self, "Node ID is not numeric, bytestring, guid or string. I do not know how to create c code for that...", LOG_LEVEL_ERROR)
  183. return []
  184. return code