open62541_MacroHelper.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 getCreateNodeIDMacro(self, node):
  37. if node.id().i != None:
  38. return "UA_NODEID_NUMERIC(" + str(node.id().ns) + ", " + str(node.id().i) + ")"
  39. elif node.id().s != None:
  40. return "UA_NODEID_STRING(" + str(node.id().ns) + ", " + node.id().s + ")"
  41. elif node.id().b != None:
  42. log(self, "NodeID Generation macro for bytestrings has not been implemented.")
  43. return ""
  44. elif node.id().g != None:
  45. log(self, "NodeID Generation macro for guids has not been implemented.")
  46. return ""
  47. else:
  48. return ""
  49. def getCreateStandaloneReference(self, sourcenode, reference):
  50. # As reference from open62541 (we need to alter the attributes)
  51. # UA_Server_addReference(UA_Server *server, const UA_NodeId sourceId, const UA_NodeId refTypeId,
  52. # const UA_ExpandedNodeId targetId)
  53. code = []
  54. #refid = "ref_" + reference.getCodePrintableID()
  55. #code.append("UA_AddReferencesItem " + refid + ";")
  56. #code.append("UA_AddReferencesItem_init(&" + refid + ");")
  57. #code.append(refid + ".sourceNodeId = " + self.getCreateNodeIDMacro(sourcenode) + ";")
  58. #code.append(refid + ".referenceTypeId = " + self.getCreateNodeIDMacro(reference.referenceType()) + ";")
  59. #if reference.isForward():
  60. #code.append(refid + ".isForward = UA_TRUE;")
  61. #else:
  62. #code.append(refid + ".isForward = UA_FALSE;")
  63. #code.append(refid + ".targetNodeId = " + self.getCreateExpandedNodeIDMacro(reference.target()) + ";")
  64. #code.append("addOneWayReferenceWithSession(server, (UA_Session *) UA_NULL, &" + refid + ");")
  65. if reference.isForward():
  66. code.append("UA_Server_AddMonodirectionalReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", UA_TRUE);")
  67. else:
  68. code.append("UA_Server_AddMonodirectionalReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", UA_FALSE);")
  69. return code
  70. def getCreateNode(self, node):
  71. nodetype = ""
  72. code = []
  73. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  74. if node.nodeClass() == NODE_CLASS_OBJECT:
  75. nodetype = "UA_ObjectNode"
  76. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  77. nodetype = "UA_VariableNode"
  78. elif node.nodeClass() == NODE_CLASS_METHOD:
  79. nodetype = "UA_MethodNode"
  80. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  81. nodetype = "UA_ObjectTypeNode"
  82. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  83. nodetype = "UA_ReferenceTypeNode"
  84. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  85. nodetype = "UA_VariableTypeNode"
  86. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  87. nodetype = "UA_DataTypeNode"
  88. elif node.nodeClass() == NODE_CLASS_VIEW:
  89. nodetype = "UA_ViewNode"
  90. elif node.nodeClass() == NODE_CLASS_METHODTYPE:
  91. nodetype = "UA_MethodTypeNode"
  92. else:
  93. nodetype = "UA_NodeTypeNotFoundorGeneric"
  94. code.append(nodetype + " *" + node.getCodePrintableID() + " = " + nodetype + "_new();")
  95. if not "browsename" in self.supressGenerationOfAttribute:
  96. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(" + str(node.id().ns) + ", \"" + node.browseName() + "\");")
  97. if not "displayname" in self.supressGenerationOfAttribute:
  98. code.append(node.getCodePrintableID() + "->displayName = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.displayName() + "\");")
  99. if not "description" in self.supressGenerationOfAttribute:
  100. code.append(node.getCodePrintableID() + "->description = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.description() + "\");")
  101. if not "writemask" in self.supressGenerationOfAttribute:
  102. if node.__node_writeMask__ != 0:
  103. code.append(node.getCodePrintableID() + "->writeMask = (UA_Int32) " + str(node.__node_writeMask__) + ";")
  104. if not "userwritemask" in self.supressGenerationOfAttribute:
  105. if node.__node_userWriteMask__ != 0:
  106. code.append(node.getCodePrintableID() + "->userWriteMask = (UA_Int32) " + str(node.__node_userWriteMask__) + ";")
  107. #FIXME: Allocate descriptions, etc.
  108. if not "nodeid" in self.supressGenerationOfAttribute:
  109. if node.id().ns != 0:
  110. code.append(node.getCodePrintableID() + "->nodeId.namespaceIndex = " + str(node.id().ns) + ";")
  111. if node.id().i != None:
  112. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = " + str(node.id().i) + ";")
  113. elif node.id().b != None:
  114. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_BYTESTRING;")
  115. log(self, "ByteString IDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  116. return []
  117. elif node.id().g != None:
  118. #<jpfr> the string is sth like { .length = 111, .data = <ptr> }
  119. #<jpfr> there you _may_ alloc the <ptr> on the heap
  120. #<jpfr> for the guid, just set it to {.data1 = 111, .data2 = 2222, ....
  121. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_GUID;")
  122. log(self, "GUIDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  123. return []
  124. elif node.id().s != None:
  125. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_STRING;")
  126. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = UA_STRING_ALLOC(\"" + str(node.id().i) + "\");")
  127. else:
  128. 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)
  129. return []
  130. return code