open62541_MacroHelper.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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):
  22. pass
  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. # #define ADDREFERENCE(NODEID, REFTYPE_NODEID, TARGET_EXPNODEID) do { \
  52. # UA_AddReferencesItem item; \
  53. # UA_AddReferencesItem_init(&item); \
  54. # item.sourceNodeId = NODEID; \
  55. # item.referenceTypeId = REFTYPE_NODEID; \
  56. # item.isForward = UA_TRUE; \
  57. # item.targetNodeId = TARGET_EXPNODEID; \
  58. # UA_Server_addReference(server, &item); \
  59. # } while(0)
  60. code = []
  61. refid = "ref_" + reference.getCodePrintableID()
  62. code.append("UA_AddReferencesItem " + refid + ";")
  63. code.append("UA_AddReferencesItem_init(&" + refid + ");")
  64. code.append(refid + ".sourceNodeId = " + self.getCreateNodeIDMacro(sourcenode) + ";")
  65. code.append(refid + ".referenceTypeId = " + self.getCreateNodeIDMacro(reference.referenceType()) + ";")
  66. if reference.isForward():
  67. code.append(refid + ".isForward = UA_TRUE;")
  68. else:
  69. code.append(refid + ".isForward = UA_FALSE;")
  70. code.append(refid + ".targetNodeId = " + self.getCreateExpandedNodeIDMacro(reference.target()) + ";")
  71. code.append("UA_Server_addReference(server, &" + refid + ");")
  72. return code
  73. def getCreateNode(self, node):
  74. nodetype = ""
  75. code = []
  76. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  77. if node.nodeClass() == NODE_CLASS_OBJECT:
  78. nodetype = "UA_ObjectNode"
  79. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  80. nodetype = "UA_VariableNode"
  81. elif node.nodeClass() == NODE_CLASS_METHOD:
  82. nodetype = "UA_MethodNode"
  83. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  84. nodetype = "UA_ObjectTypeNode"
  85. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  86. nodetype = "UA_ReferenceTypeNode"
  87. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  88. nodetype = "UA_VariableTypeNode"
  89. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  90. nodetype = "UA_DataTypeNode"
  91. elif node.nodeClass() == NODE_CLASS_VIEW:
  92. nodetype = "UA_ViewNode"
  93. elif node.nodeClass() == NODE_CLASS_METHODTYPE:
  94. nodetype = "UA_MethodTypeNode"
  95. else:
  96. nodetype = "UA_NodeTypeNotFoundorGeneric"
  97. code.append(nodetype + " *" + node.getCodePrintableID() + " = " + nodetype + "_new();")
  98. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(" + str(node.id().ns) + ", \"" + node.browseName() + "\");")
  99. code.append(node.getCodePrintableID() + "->displayName = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.displayName() + "\");")
  100. code.append(node.getCodePrintableID() + "->description = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.description() + "\");")
  101. code.append(node.getCodePrintableID() + "->writeMask = (UA_Int32) " + str(node.__node_writeMask__) + ";")
  102. code.append(node.getCodePrintableID() + "->userWriteMask = (UA_Int32) " + str(node.__node_userWriteMask__) + ";")
  103. #FIXME: Allocate descriptions, etc.
  104. if node.id().i != None:
  105. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = " + str(node.id().i) + ";")
  106. elif node.id().b != None:
  107. log(self, "ByteString IDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  108. return []
  109. elif node.id().g != None:
  110. #<jpfr> the string is sth like { .length = 111, .data = <ptr> }
  111. #<jpfr> there you _may_ alloc the <ptr> on the heap
  112. #<jpfr> for the guid, just set it to {.data1 = 111, .data2 = 2222, ....
  113. log(self, "GUIDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  114. return []
  115. elif node.id().s != None:
  116. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = UA_STRING_ALLOC(\"" + str(node.id().i) + "\");")
  117. else:
  118. 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)
  119. return []
  120. return code