open62541_MacroHelper.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. code = []
  59. if reference.isForward():
  60. code.append("UA_Server_addReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", true);")
  61. else:
  62. code.append("UA_Server_addReference(server, " + self.getCreateNodeIDMacro(sourcenode) + ", " + self.getCreateNodeIDMacro(reference.referenceType()) + ", " + self.getCreateExpandedNodeIDMacro(reference.target()) + ", false);")
  63. return code
  64. def getCreateNodeNoBootstrap(self, node, parentNode, parentReference, unprintedNodes):
  65. code = []
  66. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  67. if node.nodeClass() == NODE_CLASS_OBJECT:
  68. nodetype = "Object"
  69. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  70. nodetype = "Variable"
  71. elif node.nodeClass() == NODE_CLASS_METHOD:
  72. nodetype = "Method"
  73. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  74. nodetype = "ObjectType"
  75. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  76. nodetype = "ReferenceType"
  77. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  78. nodetype = "VariableType"
  79. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  80. nodetype = "DataType"
  81. elif node.nodeClass() == NODE_CLASS_VIEW:
  82. nodetype = "View"
  83. else:
  84. code.append("/* undefined nodeclass */")
  85. return code;
  86. # If this is a method, construct in/outargs for addMethod
  87. #inputArguments.arrayDimensionsSize = 0;
  88. #inputArguments.arrayDimensions = NULL;
  89. #inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  90. # Node ordering should have made sure that arguments, if they exist, have not been printed yet
  91. if node.nodeClass() == NODE_CLASS_METHOD:
  92. inArgVal = []
  93. outArgVal = []
  94. code.append("UA_Argument *inputArguments = NULL;")
  95. code.append("UA_Argument *outputArguments = NULL;")
  96. for r in node.getReferences():
  97. if r.target() != None and r.target().nodeClass() == NODE_CLASS_VARIABLE and r.target().browseName() == 'InputArguments':
  98. if r.target().value() != None:
  99. inArgVal = r.target().value().value
  100. while r.target() in unprintedNodes:
  101. unprintedNodes.remove(r.target())
  102. elif r.target() != None and r.target().nodeClass() == NODE_CLASS_VARIABLE and r.target().browseName() == 'OutputArguments':
  103. if r.target().value() != None:
  104. outArgVal = r.target().value().value
  105. while r.target() in unprintedNodes:
  106. unprintedNodes.remove(r.target())
  107. if len(inArgVal)>0:
  108. code.append("")
  109. code.append("inputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(inArgVal)) + ");")
  110. code.append("int inputArgumentCnt;")
  111. code.append("for (inputArgumentCnt=0; inputArgumentCnt<" + str(len(inArgVal)) + "; inputArgumentCnt++) UA_Argument_init(&inputArguments[inputArgumentCnt]); ")
  112. argumentCnt = 0
  113. for inArg in inArgVal:
  114. if inArg.getValueFieldByAlias("Description") != None:
  115. code.append("inputArguments[" + str(argumentCnt) + "].description = UA_LOCALIZEDTEXT(\"" + str(inArg.getValueFieldByAlias("Description")[0]) + "\",\"" + str(inArg.getValueFieldByAlias("Description")[1]) + "\");")
  116. if inArg.getValueFieldByAlias("Name") != None:
  117. code.append("inputArguments[" + str(argumentCnt) + "].name = UA_STRING(\"" + str(inArg.getValueFieldByAlias("Name")) + "\");")
  118. if inArg.getValueFieldByAlias("ValueRank") != None:
  119. code.append("inputArguments[" + str(argumentCnt) + "].valueRank = " + str(inArg.getValueFieldByAlias("ValueRank")) + ";")
  120. if inArg.getValueFieldByAlias("DataType") != None:
  121. code.append("inputArguments[" + str(argumentCnt) + "].dataType = " + str(self.getCreateNodeIDMacro(inArg.getValueFieldByAlias("DataType"))) + ";")
  122. #if inArg.getValueFieldByAlias("ArrayDimensions") != None:
  123. # code.append("inputArguments[" + str(argumentCnt) + "].arrayDimensions = " + str(inArg.getValueFieldByAlias("ArrayDimensions")) + ";")
  124. argumentCnt += 1
  125. if len(outArgVal)>0:
  126. code.append("")
  127. code.append("outputArguments = (UA_Argument *) malloc(sizeof(UA_Argument) * " + str(len(outArgVal)) + ");")
  128. code.append("int outputArgumentCnt;")
  129. code.append("for (outputArgumentCnt=0; outputArgumentCnt<" + str(len(outArgVal)) + "; outputArgumentCnt++) UA_Argument_init(&outputArguments[outputArgumentCnt]); ")
  130. argumentCnt = 0
  131. for outArg in outArgVal:
  132. if outArg.getValueFieldByAlias("Description") != None:
  133. code.append("outputArguments[" + str(argumentCnt) + "].description = UA_LOCALIZEDTEXT(\"" + str(outArg.getValueFieldByAlias("Description")[0]) + "\",\"" + str(outArg.getValueFieldByAlias("Description")[1]) + "\");")
  134. if outArg.getValueFieldByAlias("Name") != None:
  135. code.append("outputArguments[" + str(argumentCnt) + "].name = UA_STRING(\"" + str(outArg.getValueFieldByAlias("Name")) + "\");")
  136. if outArg.getValueFieldByAlias("ValueRank") != None:
  137. code.append("outputArguments[" + str(argumentCnt) + "].valueRank = " + str(outArg.getValueFieldByAlias("ValueRank")) + ";")
  138. if outArg.getValueFieldByAlias("DataType") != None:
  139. code.append("outputArguments[" + str(argumentCnt) + "].dataType = " + str(self.getCreateNodeIDMacro(outArg.getValueFieldByAlias("DataType"))) + ";")
  140. #if outArg.getValueFieldByAlias("ArrayDimensions") != None:
  141. # code.append("outputArguments[" + str(argumentCnt) + "].arrayDimensions = " + str(outArg.getValueFieldByAlias("ArrayDimensions")) + ";")
  142. argumentCnt += 1
  143. # print the attributes struct
  144. code.append("UA_%sAttributes attr;" % nodetype)
  145. code.append("UA_%sAttributes_init(&attr);" % nodetype);
  146. code.append("attr.displayName = UA_LOCALIZEDTEXT(\"\", \"" + str(node.displayName()) + "\");")
  147. code.append("attr.description = UA_LOCALIZEDTEXT(\"\", \"" + str(node.description()) + "\");")
  148. if nodetype in ["Variable", "VariableType"]:
  149. code = code + node.printOpen62541CCode_SubtypeEarly(bootstrapping = False)
  150. elif nodetype == "Method":
  151. if node.executable():
  152. code.append("attr.executable = true;")
  153. if node.userExecutable():
  154. code.append("attr.userExecutable = true;")
  155. code.append("UA_NodeId nodeId = " + str(self.getCreateNodeIDMacro(node)) + ";")
  156. if nodetype in ["Object", "Variable"]:
  157. code.append("UA_NodeId typeDefinition = UA_NODEID_NULL;") # todo instantiation of object and variable types
  158. code.append("UA_NodeId parentNodeId = " + str(self.getCreateNodeIDMacro(parentNode)) + ";")
  159. code.append("UA_NodeId parentReferenceNodeId = " + str(self.getCreateNodeIDMacro(parentReference.referenceType())) + ";")
  160. extrNs = node.browseName().split(":")
  161. if len(extrNs) > 1:
  162. code.append("UA_QualifiedName nodeName = UA_QUALIFIEDNAME(" + str(extrNs[0]) + ", \"" + extrNs[1] + "\");")
  163. else:
  164. code.append("UA_QualifiedName nodeName = UA_QUALIFIEDNAME(0, \"" + str(node.browseName()) + "\");")
  165. # In case of a MethodNode: Add in|outArg struct generation here. Mandates that namespace reordering was done using
  166. # Djikstra (check that arguments have not been printed). (@ichrispa)
  167. code.append("UA_Server_add%sNode(server, nodeId, parentNodeId, parentReferenceNodeId, nodeName" % nodetype)
  168. if nodetype in ["Object", "Variable"]:
  169. code.append(" , typeDefinition")
  170. if nodetype != "Method":
  171. code.append(" , attr, NULL, NULL);")
  172. else:
  173. code.append(" , attr, (UA_MethodCallback) NULL, NULL, " + str(len(inArgVal)) + ", inputArguments, " + str(len(outArgVal)) + ", outputArguments, NULL);")
  174. return code
  175. def getCreateNodeBootstrap(self, node):
  176. nodetype = ""
  177. code = []
  178. code.append("// Node: " + str(node) + ", " + str(node.browseName()))
  179. if node.nodeClass() == NODE_CLASS_OBJECT:
  180. nodetype = "Object"
  181. elif node.nodeClass() == NODE_CLASS_VARIABLE:
  182. nodetype = "Variable"
  183. elif node.nodeClass() == NODE_CLASS_METHOD:
  184. nodetype = "Method"
  185. elif node.nodeClass() == NODE_CLASS_OBJECTTYPE:
  186. nodetype = "ObjectType"
  187. elif node.nodeClass() == NODE_CLASS_REFERENCETYPE:
  188. nodetype = "ReferenceType"
  189. elif node.nodeClass() == NODE_CLASS_VARIABLETYPE:
  190. nodetype = "VariableType"
  191. elif node.nodeClass() == NODE_CLASS_DATATYPE:
  192. nodetype = "DataType"
  193. elif node.nodeClass() == NODE_CLASS_VIEW:
  194. nodetype = "View"
  195. else:
  196. code.append("/* undefined nodeclass */")
  197. return;
  198. code.append("UA_" + nodetype + "Node *" + node.getCodePrintableID() + " = UA_NodeStore_new" + nodetype + "Node();")
  199. if not "browsename" in self.supressGenerationOfAttribute:
  200. extrNs = node.browseName().split(":")
  201. if len(extrNs) > 1:
  202. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(" + str(extrNs[0]) + ", \"" + extrNs[1] + "\");")
  203. else:
  204. code.append(node.getCodePrintableID() + "->browseName = UA_QUALIFIEDNAME_ALLOC(0, \"" + node.browseName() + "\");")
  205. if not "displayname" in self.supressGenerationOfAttribute:
  206. code.append(node.getCodePrintableID() + "->displayName = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.displayName() + "\");")
  207. if not "description" in self.supressGenerationOfAttribute:
  208. code.append(node.getCodePrintableID() + "->description = UA_LOCALIZEDTEXT_ALLOC(\"en_US\", \"" + node.description() + "\");")
  209. if not "writemask" in self.supressGenerationOfAttribute:
  210. if node.__node_writeMask__ != 0:
  211. code.append(node.getCodePrintableID() + "->writeMask = (UA_Int32) " + str(node.__node_writeMask__) + ";")
  212. if not "userwritemask" in self.supressGenerationOfAttribute:
  213. if node.__node_userWriteMask__ != 0:
  214. code.append(node.getCodePrintableID() + "->userWriteMask = (UA_Int32) " + str(node.__node_userWriteMask__) + ";")
  215. if not "nodeid" in self.supressGenerationOfAttribute:
  216. if node.id().ns != 0:
  217. code.append(node.getCodePrintableID() + "->nodeId.namespaceIndex = " + str(node.id().ns) + ";")
  218. if node.id().i != None:
  219. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = " + str(node.id().i) + ";")
  220. elif node.id().b != None:
  221. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_BYTESTRING;")
  222. log(self, "ByteString IDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  223. return []
  224. elif node.id().g != None:
  225. #<jpfr> the string is sth like { .length = 111, .data = <ptr> }
  226. #<jpfr> there you _may_ alloc the <ptr> on the heap
  227. #<jpfr> for the guid, just set it to {.data1 = 111, .data2 = 2222, ....
  228. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_GUID;")
  229. log(self, "GUIDs for nodes has not been implemented yet.", LOG_LEVEL_ERROR)
  230. return []
  231. elif node.id().s != None:
  232. code.append(node.getCodePrintableID() + "->nodeId.identifierType = UA_NODEIDTYPE_STRING;")
  233. code.append(node.getCodePrintableID() + "->nodeId.identifier.numeric = UA_STRING_ALLOC(\"" + str(node.id().i) + "\");")
  234. else:
  235. 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)
  236. return []
  237. return code