open62541_MacroHelper.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_addMonodirectionalReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", UA_TRUE);")
  75. else:
  76. code.append("UA_Server_addMonodirectionalReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", 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. code.append("UA_Server_addObjectNode(server, ")
  83. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  84. code.append("UA_Server_addVariableNode(server,")
  85. elif node.nodeClass() == NODE_CLASS_METHOD:
  86. code.append("#ifdef ENABLE_METHODCALL")
  87. code.append("UA_Server_addMethodNode(server,")
  88. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  89. code.append("UA_Server_addObjectTypeNode(server,")
  90. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  91. code.append("UA_Server_addReferenceTypeNode(server,")
  92. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  93. code.append("UA_Server_addVariableTypeNode(server,")
  94. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  95. code.append("UA_Server_addDataTypeNode(server,")
  96. elif node.nodeClass() == NODE_CLASS_VIEW:
  97. code.append("UA_Server_addViewNode(server,")
  98. elif node.nodeClass() == NODE_CLASS_METHODTYPE:
  99. code.append("UA_Server_addMethodTypeNode(server,")
  100. else:
  101. return []
  102. code.append(" " + str(self.getCreateNodeIDMacro(node)) + ",") # NodeId
  103. extrNs = node.browseName().split(":")
  104. if len(extrNs) > 1:
  105. code.append(" UA_QUALIFIEDNAME(" + str(extrNs[0]) + ", \"" + extrNs[1] + "\"),") # browseName
  106. else:
  107. code.append(" UA_QUALIFIEDNAME(0, \"" + str(node.browseName()) + "\"),") # browseName
  108. code.append(" UA_LOCALIZEDTEXT(\"\", \"" + str(node.displayName()) + "\"),") # displayName
  109. code.append(" UA_LOCALIZEDTEXT(\"\", \"" + str(node.description()) + "\"),") # description
  110. code.append(" " + str(self.getCreateNodeIDMacro(parentNode)) + ",") # ParentNode
  111. code.append(" " + str(self.getCreateNodeIDMacro(parentReference.referenceType())) + ",") # ReferenceTypeId
  112. code.append(" " + str(node.writeMask()) + ", " + str(node.userWriteMask()) + ",") # write/userWriteMask
  113. return code
  114. def getCreateNodeBootstrap(self, node):
  115. nodetype = ""
  116. code = []
  117. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  118. if node.nodeClass() == NODE_CLASS_OBJECT:
  119. nodetype = "UA_ObjectNode"
  120. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  121. nodetype = "UA_VariableNode"
  122. elif node.nodeClass() == NODE_CLASS_METHOD:
  123. nodetype = "UA_MethodNode"
  124. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  125. nodetype = "UA_ObjectTypeNode"
  126. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  127. nodetype = "UA_ReferenceTypeNode"
  128. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  129. nodetype = "UA_VariableTypeNode"
  130. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  131. nodetype = "UA_DataTypeNode"
  132. elif node.nodeClass() == NODE_CLASS_VIEW:
  133. nodetype = "UA_ViewNode"
  134. elif node.nodeClass() == NODE_CLASS_METHODTYPE:
  135. nodetype = "UA_MethodTypeNode"
  136. else:
  137. nodetype = "UA_NodeTypeNotFoundorGeneric"
  138. code.append(nodetype + " *" + node.getCodePrintableID() + " = " + nodetype + "_new();")
  139. if not "browsename" in self.supressGenerationOfAttribute:
  140. extrNs = node.browseName().split(":")
  141. if len(extrNs) > 1:
  142. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(" + str(extrNs[0]) + ", \"" + extrNs[1] + "\");")
  143. else:
  144. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(0, \"" + node.browseName() + "\");")
  145. if not "displayname" in self.supressGenerationOfAttribute:
  146. code.append(node.getCodePrintableID() + "->displayName = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.displayName() + "\");")
  147. if not "description" in self.supressGenerationOfAttribute:
  148. code.append(node.getCodePrintableID() + "->description = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.description() + "\");")
  149. if not "writemask" in self.supressGenerationOfAttribute:
  150. if node.__node_writeMask__ != 0:
  151. code.append(node.getCodePrintableID() + "->writeMask = (UA_Int32) " + str(node.__node_writeMask__) + ";")
  152. if not "userwritemask" in self.supressGenerationOfAttribute:
  153. if node.__node_userWriteMask__ != 0:
  154. code.append(node.getCodePrintableID() + "->userWriteMask = (UA_Int32) " + str(node.__node_userWriteMask__) + ";")
  155. #FIXME: Allocate descriptions, etc.
  156. if not "nodeid" in self.supressGenerationOfAttribute:
  157. if node.id().ns != 0:
  158. code.append(node.getCodePrintableID() + "->nodeId.namespaceIndex = " + str(node.id().ns) + ";")
  159. if node.id().i != None:
  160. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = " + str(node.id().i) + ";")
  161. elif node.id().b != None:
  162. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_BYTESTRING;")
  163. log(self, "ByteString IDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  164. return []
  165. elif node.id().g != None:
  166. #<jpfr> the string is sth like { .length = 111, .data = <ptr> }
  167. #<jpfr> there you _may_ alloc the <ptr> on the heap
  168. #<jpfr> for the guid, just set it to {.data1 = 111, .data2 = 2222, ....
  169. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_GUID;")
  170. log(self, "GUIDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  171. return []
  172. elif node.id().s != None:
  173. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_STRING;")
  174. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = UA_STRING_ALLOC(\"" + str(node.id().i) + "\");")
  175. else:
  176. 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)
  177. return []
  178. return code