generate_datatypes.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. from __future__ import print_function
  2. import sys
  3. import time
  4. import platform
  5. import getpass
  6. from collections import OrderedDict
  7. import re
  8. from lxml import etree
  9. import itertools
  10. import argparse
  11. from pprint import pprint
  12. fixed_size = {"UA_Boolean": 1, "UA_SByte": 1, "UA_Byte": 1, "UA_Int16": 2, "UA_UInt16": 2,
  13. "UA_Int32": 4, "UA_UInt32": 4, "UA_Int64": 8, "UA_UInt64": 8, "UA_Float": 4,
  14. "UA_Double": 8, "UA_DateTime": 8, "UA_Guid": 16, "UA_StatusCode": 4}
  15. zero_copy = ["UA_Boolean", "UA_SByte", "UA_Byte", "UA_Int16", "UA_UInt16", "UA_Int32", "UA_UInt32",
  16. "UA_Int64", "UA_UInt64", "UA_Float", "UA_Double", "UA_DateTime", "UA_StatusCode"]
  17. # The order of the builtin-types is not as in the standard. We put all the
  18. # fixed_size types in the front, so they can be distinguished by a simple geq
  19. # comparison. That's ok, since we use the type-index only internally!!
  20. builtin_types = ["UA_Boolean", "UA_SByte", "UA_Byte", "UA_Int16", "UA_UInt16",
  21. "UA_Int32", "UA_UInt32", "UA_Int64", "UA_UInt64", "UA_Float",
  22. "UA_Double", "UA_String", "UA_DateTime", "UA_Guid", "UA_ByteString",
  23. "UA_XmlElement", "UA_NodeId", "UA_ExpandedNodeId", "UA_StatusCode",
  24. "UA_QualifiedName", "UA_LocalizedText", "UA_ExtensionObject", "UA_DataValue",
  25. "UA_Variant", "UA_DiagnosticInfo"]
  26. excluded_types = ["UA_NodeIdType", "UA_InstanceNode", "UA_TypeNode", "UA_Node", "UA_ObjectNode",
  27. "UA_ObjectTypeNode", "UA_VariableNode", "UA_VariableTypeNode", "UA_ReferenceTypeNode",
  28. "UA_MethodNode", "UA_ViewNode", "UA_DataTypeNode", "UA_ServerDiagnosticsSummaryDataType",
  29. "UA_SamplingIntervalDiagnosticsDataType", "UA_SessionSecurityDiagnosticsDataType",
  30. "UA_SubscriptionDiagnosticsDataType", "UA_SessionDiagnosticsDataType"]
  31. minimal_types = ["InvalidType", "Node", "NodeClass", "ReferenceNode", "ApplicationDescription", "ApplicationType",
  32. "ChannelSecurityToken", "OpenSecureChannelRequest", "OpenSecureChannelResponse",
  33. "CloseSecureChannelRequest", "CloseSecureChannelResponse", "RequestHeader", "ResponseHeader",
  34. "SecurityTokenRequestType", "MessageSecurityMode", "CloseSessionResponse", "CloseSessionRquest",
  35. "ActivateSessionRequest", "ActivateSessionResponse", "SignatureData", "SignedSoftwareCertificate",
  36. "CreateSessionResponse", "CreateSessionRequest", "EndpointDescription", "UserTokenPolicy", "UserTokenType",
  37. "GetEndpointsRequest", "GetEndpointsResponse", "PublishRequest", "PublishResponse", "FindServersRequest",
  38. "FindServersResponse", "SetPublishingModeResponse", "SubscriptionAcknowledgement", "NotificationMessage",
  39. "ExtensionObject", "Structure", "ReadRequest", "ReadResponse", "ReadValueId", "TimestampsToReturn", "WriteRequest",
  40. "WriteResponse", "WriteValue", "SetPublishingModeRequest", "CreateMonitoredItemsResponse",
  41. "MonitoredItemCreateResult", "CreateMonitoredItemsRequest", "MonitoredItemCreateRequest",
  42. "MonitoringMode", "MonitoringParameters", "TranslateBrowsePathsToNodeIdsRequest",
  43. "TranslateBrowsePathsToNodeIdsResponse", "BrowsePath", "BrowsePathResult", "RelativePath",
  44. "BrowsePathTarget", "RelativePathElement", "CreateSubscriptionRequest", "CreateSubscriptionResponse",
  45. "BrowseResponse", "BrowseResult", "ReferenceDescription", "BrowseRequest", "ViewDescription",
  46. "BrowseNextRequest", "BrowseNextResponse", "DeleteSubscriptionsRequest", "DeleteSubscriptionsResponse",
  47. "BrowseDescription", "BrowseDirection", "CloseSessionRequest", "AddNodesRequest", "AddNodesResponse",
  48. "AddNodesItem", "AddNodesResult", "DeleteNodesItem","AddReferencesRequest", "AddReferencesResponse",
  49. "AddReferencesItem","DeleteReferencesItem", "VariableNode", "MethodNode", "VariableTypeNode",
  50. "ViewNode", "ReferenceTypeNode", "BrowseResultMask", "ServerState", "ServerStatusDataType", "BuildInfo",
  51. "ObjectNode", "DataTypeNode", "ObjectTypeNode", "IdType", "NodeAttributes",
  52. "VariableAttributes", "ObjectAttributes", "ReferenceTypeAttributes", "ViewAttributes", "MethodAttributes",
  53. "ObjectTypeAttributes", "VariableTypeAttributes", "DataTypeAttributes", "NodeAttributesMask",
  54. "DeleteNodesItem", "DeleteNodesRequest", "DeleteNodesResponse",
  55. "DeleteReferencesItem", "DeleteReferencesRequest", "DeleteReferencesResponse",
  56. "RegisterNodesRequest", "RegisterNodesResponse", "UnregisterNodesRequest", "UnregisterNodesResponse",
  57. "UserIdentityToken", "UserNameIdentityToken", "AnonymousIdentityToken", "ServiceFault",
  58. "CallMethodRequest", "CallMethodResult", "CallResponse", "CallRequest", "Argument"]
  59. subscription_types = ["DeleteMonitoredItemsRequest", "DeleteMonitoredItemsResponse", "NotificationMessage",
  60. "MonitoredItemNotification", "DataChangeNotification", "ModifySubscriptionRequest",
  61. "ModifySubscriptionResponse"]
  62. class TypeDescription(object):
  63. def __init__(self, name, nodeid, namespaceid):
  64. self.name = name # without the UA_ prefix
  65. self.nodeid = nodeid
  66. self.namespaceid = namespaceid
  67. def parseTypeDescriptions(filename, namespaceid):
  68. definitions = {}
  69. f = open(filename[0])
  70. input_str = f.read()
  71. f.close()
  72. input_str = input_str.replace('\r','')
  73. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  74. for index, row in enumerate(rows):
  75. if len(row) < 3:
  76. continue
  77. if row[2] != "DataType":
  78. continue
  79. if row[0] == "BaseDataType":
  80. definitions["UA_Variant"] = TypeDescription(row[0], row[1], namespaceid)
  81. elif row[0] == "Structure":
  82. definitions["UA_ExtensionObject"] = TypeDescription(row[0], row[1], namespaceid)
  83. else:
  84. definitions["UA_" + row[0]] = TypeDescription(row[0], row[1], namespaceid)
  85. return definitions
  86. class BuiltinType(object):
  87. "Generic type without members. Used for builtin types."
  88. def __init__(self, name, description = ""):
  89. self.name = name
  90. self.description = description
  91. def fixed_size(self):
  92. return self.name in fixed_size
  93. def mem_size(self):
  94. return fixed_size[self.name]
  95. def zero_copy(self):
  96. return self.name in zero_copy
  97. def typedef_c(self):
  98. pass
  99. def typelayout_c(self, namespace_0, description, outname):
  100. if description == None:
  101. typeid = "{.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = 0}, "
  102. else:
  103. typeid = "{.namespaceIndex = %s, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = %s}, " % \
  104. (description.namespaceid, description.nodeid)
  105. if self.name in ["UA_String", "UA_ByteString", "UA_XmlElement"]:
  106. return (("{.typeName = \"" + self.name[3:] + "\", ") if typeintrospection else "{") + ".typeId = " + typeid + \
  107. ".memSize = sizeof(" + self.name + "), " + \
  108. ".namespaceZero = UA_TRUE, .fixedSize = UA_FALSE, .zeroCopyable = UA_FALSE, " + \
  109. ".membersSize = 1,\n\t.members = {{.memberTypeIndex = UA_TYPES_BYTE, .namespaceZero = UA_TRUE, " + \
  110. (".memberName = (char*)0, " if typeintrospection else "") + \
  111. ".padding = offsetof(UA_String, data) - sizeof(UA_Int32), .isArray = UA_TRUE }}, " + \
  112. ".typeIndex = %s }" % (outname.upper() + "_" + self.name[3:].upper())
  113. if self.name == "UA_QualifiedName":
  114. return (("{.typeName = \"" + self.name[3:] + "\", ") if typeintrospection else "{") + ".typeId = " + typeid + \
  115. ".memSize = sizeof(UA_QualifiedName), " + \
  116. ".namespaceZero = UA_TRUE, .fixedSize = UA_FALSE, .zeroCopyable = UA_FALSE, " + \
  117. ".membersSize = 2, .members = {" + \
  118. "\n\t{.memberTypeIndex = UA_TYPES_UINT16, .namespaceZero = UA_TRUE, " + \
  119. (".memberName = (char*)0, " if typeintrospection else "") + \
  120. ".padding = 0, .isArray = UA_FALSE }," + \
  121. "\n\t{.memberTypeIndex = UA_TYPES_STRING, .namespaceZero = UA_TRUE, " + \
  122. (".memberName = (char*)0, " if typeintrospection else "") + \
  123. ".padding = offsetof(UA_QualifiedName, name) - sizeof(UA_UInt16), .isArray = UA_FALSE }},\n" + \
  124. ".typeIndex = UA_TYPES_QUALIFIEDNAME }"
  125. return (("{.typeName = \"" + self.name[3:] + "\", ") if typeintrospection else "{") + ".typeId = " + typeid + \
  126. ".memSize = sizeof(" + self.name + "), " + \
  127. ".namespaceZero = UA_TRUE, " + \
  128. ".fixedSize = " + ("UA_TRUE" if self.fixed_size() else "UA_FALSE") + \
  129. ", .zeroCopyable = " + ("UA_TRUE" if self.zero_copy() else "UA_FALSE") + \
  130. ", .membersSize = 1,\n\t.members = {{.memberTypeIndex = UA_TYPES_" + self.name[3:].upper() + "," + \
  131. (".memberName = (char*)0, " if typeintrospection else "") + \
  132. ".namespaceZero = UA_TRUE, .padding = 0, .isArray = UA_FALSE }}, " + \
  133. ".typeIndex = %s }" % (outname.upper() + "_" + self.name[3:].upper())
  134. class EnumerationType(object):
  135. def __init__(self, name, description = "", elements = OrderedDict()):
  136. self.name = name
  137. self.description = description
  138. self.elements = elements # maps a name to an integer value
  139. def append_enum(name, value):
  140. self.elements[name] = value
  141. def fixed_size(self):
  142. return True
  143. def mem_size(self):
  144. return 4
  145. def zero_copy(self):
  146. return True
  147. def typedef_c(self):
  148. if sys.version_info[0] < 3:
  149. values = self.elements.iteritems()
  150. else:
  151. values = self.elements.items()
  152. return "typedef enum { \n " + \
  153. ",\n ".join(map(lambda kv : kv[0].upper() + " = " + kv[1], values)) + \
  154. "\n} " + self.name + ";"
  155. def typelayout_c(self, namespace_0, description, outname):
  156. if description == None:
  157. typeid = "{.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = 0}, "
  158. else:
  159. typeid = "{.namespaceIndex = %s, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = %s}, " % (description.namespaceid, description.nodeid)
  160. return (("{.typeName = \"" + self.name[3:] + "\", ") if typeintrospection else "{") + ".typeId = " + typeid + \
  161. ".memSize = sizeof(" + self.name + "), " +\
  162. ".namespaceZero = UA_TRUE, " + \
  163. ".fixedSize = UA_TRUE, .zeroCopyable = UA_TRUE, " + \
  164. ".membersSize = 1,\n\t.members = {{.memberTypeIndex = UA_TYPES_INT32," + \
  165. (".memberName = (char*)0, " if typeintrospection else "") + \
  166. ".namespaceZero = UA_TRUE, .padding = 0, .isArray = UA_FALSE }}, .typeIndex = UA_TYPES_INT32 }"
  167. def functions_c(self, typeTableName):
  168. return '''static UA_INLINE %s * %s_new(void) { %s *v = (%s*)UA_Int32_new(); return v; }
  169. static UA_INLINE void %s_init(%s *p) { UA_Int32_init((UA_Int32*)p); }
  170. static UA_INLINE void %s_delete(%s *p) { UA_Int32_delete((UA_Int32*)p); }
  171. static UA_INLINE void %s_deleteMembers(%s *p) { }
  172. static UA_INLINE UA_StatusCode %s_copy(const %s *src, %s *dst) { return UA_Int32_copy((const UA_Int32*)src, (UA_Int32*)dst); }''' % tuple(itertools.repeat(self.name, 13))
  173. def encoding_h(self, typeTableName):
  174. return '''static UA_INLINE UA_StatusCode %s_encodeBinary(const %s *src, UA_ByteString *dst, size_t *offset) { return UA_Int32_encodeBinary((const UA_Int32*)src, dst, offset); }
  175. static UA_INLINE UA_StatusCode %s_decodeBinary(const UA_ByteString *src, size_t *offset, %s *dst) { return UA_Int32_decodeBinary(src, offset, (UA_Int32*)dst); }''' % tuple(itertools.repeat(self.name, 4))
  176. class OpaqueType(object):
  177. def __init__(self, name, description = ""):
  178. self.name = name
  179. self.description = description
  180. def fixed_size(self):
  181. return False
  182. def zero_copy(self):
  183. return False
  184. def typedef_c(self):
  185. return "typedef UA_ByteString " + self.name + ";"
  186. def typelayout_c(self, namespace_0, description, outname):
  187. if description == None:
  188. typeid = "{.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = 0}, "
  189. else:
  190. typeid = "{.namespaceIndex = %s, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = %s}, " % (description.namespaceid, description.nodeid)
  191. return (("{.typeName = \"" + self.name[3:] + "\", ") if typeintrospection else "{") + ".typeId = " + typeid + \
  192. ".memSize = sizeof(" + self.name + "), .fixedSize = UA_FALSE, .zeroCopyable = UA_FALSE, " + \
  193. ".namespaceZero = UA_TRUE, .membersSize = 1,\n\t.members = {{.memberTypeIndex = UA_TYPES_BYTESTRING," + \
  194. (".memberName = (char*)0, " if typeintrospection else "") + \
  195. ".namespaceZero = UA_TRUE, .padding = 0, .isArray = UA_FALSE }}, .typeIndex = UA_TYPES_BYTESTRING }"
  196. def functions_c(self, typeTableName):
  197. return '''static UA_INLINE %s * %s_new(void) { %s *v = (%s*)UA_ByteString_new(); return v; }
  198. static UA_INLINE void %s_init(%s *p) { UA_ByteString_init((UA_ByteString*)p); }
  199. static UA_INLINE void %s_delete(%s *p) { UA_ByteString_delete((UA_ByteString*)p); }
  200. static UA_INLINE void %s_deleteMembers(%s *p) { }
  201. static UA_INLINE UA_StatusCode %s_copy(const %s *src, %s *dst) { return UA_ByteString_copy((const UA_ByteString*)src, (UA_ByteString*)dst); }''' % tuple(itertools.repeat(self.name, 13))
  202. def encoding_h(self, typeTableName):
  203. return '''static UA_INLINE UA_StatusCode %s_encodeBinary(const %s *src, UA_ByteString *dst, size_t *offset) { return UA_ByteString_encodeBinary((UA_ByteString*)src, dst, offset); }
  204. static UA_INLINE UA_StatusCode %s_decodeBinary(const UA_ByteString *src, size_t *offset, %s *dst) { return UA_ByteString_decodeBinary(src, offset, (UA_ByteString*)dst); }''' % tuple(itertools.repeat(self.name, 4))
  205. class StructMember(object):
  206. def __init__(self, name, memberType, isArray):
  207. self.name = name
  208. self.memberType = memberType
  209. self.isArray = isArray
  210. class StructType(object):
  211. def __init__(self, name, description, members = OrderedDict()):
  212. self.name = name
  213. self.description = description
  214. self.members = members # maps a name to a member definition
  215. def fixed_size(self):
  216. for m in self.members.values():
  217. if m.isArray or not m.memberType.fixed_size():
  218. return False
  219. return True
  220. def mem_size(self):
  221. total = 0
  222. for m in self.members.values():
  223. if m.isArray:
  224. raise Exception("Arrays have no fixed size!")
  225. else:
  226. total += m.memberType.mem_size()
  227. return total
  228. def zero_copy(self):
  229. for m in self.members.values():
  230. if m.isArray or not m.memberType.zero_copy():
  231. return False
  232. return True
  233. def typedef_c(self):
  234. if len(self.members) == 0:
  235. return "typedef void * " + self.name + ";"
  236. returnstr = "typedef struct {\n"
  237. if sys.version_info[0] < 3:
  238. values = self.members.iteritems()
  239. else:
  240. values = self.members.items()
  241. for name, member in values:
  242. if member.isArray:
  243. returnstr += " UA_Int32 " + name + "Size;\n"
  244. returnstr += " " + member.memberType.name + " *" +name + ";\n"
  245. else:
  246. returnstr += " " + member.memberType.name + " " +name + ";\n"
  247. return returnstr + "} " + self.name + ";"
  248. def typelayout_c(self, namespace_0, description, outname):
  249. if description == None:
  250. typeid = "{.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = 0}, "
  251. else:
  252. typeid = "{.namespaceIndex = %s, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = %s}, " % (description.namespaceid, description.nodeid)
  253. layout = (("{.typeName = \"" + self.name[3:] + "\", ") if typeintrospection else "{") + ".typeId = " + typeid + \
  254. ".memSize = sizeof(" + self.name + "), "+ \
  255. ".namespaceZero = " + ("UA_TRUE" if namespace_0 else "UA_FALSE") + \
  256. ", .fixedSize = " + ("UA_TRUE" if self.fixed_size() else "UA_FALSE") + \
  257. ", .zeroCopyable = " + ("sizeof(" + self.name + ") == " + str(self.mem_size()) if self.zero_copy() \
  258. else "UA_FALSE") + \
  259. ", .typeIndex = " + outname.upper() + "_" + self.name[3:].upper() + \
  260. ", .membersSize = " + str(len(self.members)) + ","
  261. if len(self.members) > 0:
  262. layout += "\n\t.members={"
  263. for index, member in enumerate(self.members.values()):
  264. layout += "\n\t{" + \
  265. ((".memberName = \"" + member.name[0].upper() + member.name[1:] + "\", ") if typeintrospection else "") + \
  266. ".memberTypeIndex = " + ("UA_TYPES_" + member.memberType.name[3:].upper() if args.namespace_id == 0 or member.memberType.name in existing_types else \
  267. outname.upper() + "_" + member.memberType.name[3:].upper()) + ", " + \
  268. ".namespaceZero = "+ \
  269. ("UA_TRUE, " if args.namespace_id == 0 or member.memberType.name in existing_types else "UA_FALSE, ") + \
  270. ".padding = "
  271. before_endpos = "0"
  272. thispos = "offsetof(%s, %s)" % (self.name, member.name)
  273. if index > 0:
  274. if sys.version_info[0] < 3:
  275. before = self.members.values()[index-1]
  276. else:
  277. before = list(self.members.values())[index-1]
  278. before_endpos = "(offsetof(%s, %s)" % (self.name, before.name)
  279. if before.isArray:
  280. before_endpos += " + sizeof(void*))"
  281. else:
  282. before_endpos += " + sizeof(%s))" % before.memberType.name
  283. if member.isArray:
  284. # the first two bytes are padding for the length index, the last three for the pointer
  285. length_pos = "offsetof(%s, %sSize)" % (self.name, member.name)
  286. if index != 0:
  287. layout += "((%s - %s) << 3) + " % (length_pos, before_endpos)
  288. layout += "(%s - sizeof(UA_Int32) - %s)" % (thispos, length_pos)
  289. else:
  290. layout += "%s - %s" % (thispos, before_endpos)
  291. layout += ", .isArray = " + ("UA_TRUE" if member.isArray else "UA_FALSE") + " }, "
  292. layout += "}"
  293. return layout + "}"
  294. def functions_c(self, typeTableName):
  295. return '''static UA_INLINE void %s_init(%s *p) { UA_init(p, %s); }
  296. static UA_INLINE void %s_delete(%s *p) { UA_delete(p, %s); }
  297. static UA_INLINE void %s_deleteMembers(%s *p) { UA_deleteMembers(p, %s); }
  298. static UA_INLINE %s * %s_new(void) { return (%s *) UA_new(%s); }
  299. static UA_INLINE UA_StatusCode %s_copy(const %s *src, %s *dst) { return UA_copy(src, dst, %s); }''' % \
  300. tuple( list(itertools.chain(*itertools.repeat([self.name, self.name, "&"+typeTableName+"[" + typeTableName + "_" + self.name[3:].upper()+"]"], 3)))
  301. + list(itertools.chain(*itertools.repeat([self.name, self.name, self.name, "&"+typeTableName+"[" + typeTableName + "_" + self.name[3:].upper()+"]"], 2))) )
  302. def encoding_h(self, typeTableName):
  303. return '''static UA_INLINE UA_StatusCode %s_encodeBinary(const %s *src, UA_ByteString *dst, size_t *offset) { return UA_encodeBinary(src, %s, dst, offset); }
  304. static UA_INLINE UA_StatusCode %s_decodeBinary(const UA_ByteString *src, size_t *offset, %s *dst) { return UA_decodeBinary(src, offset, dst, %s); }''' % \
  305. tuple(list(itertools.chain(*itertools.repeat([self.name, self.name, "&"+typeTableName+"[" + typeTableName + "_" + self.name[3:].upper()+"]"], 2))))
  306. def parseTypeDefinitions(xmlDescription, existing_types = OrderedDict()):
  307. '''Returns an ordered dict that maps names to types. The order is such that
  308. every type depends only on known types. '''
  309. ns = {"opc": "http://opcfoundation.org/BinarySchema/"}
  310. tree = etree.parse(xmlDescription)
  311. typeSnippets = tree.xpath("/opc:TypeDictionary/*[not(self::opc:Import)]", namespaces=ns)
  312. types = OrderedDict(existing_types.items())
  313. # types we do not want to autogenerate
  314. def skipType(name):
  315. if name in builtin_types:
  316. return True
  317. if name in excluded_types:
  318. return True
  319. if outname == "ua_types" and not name[3:] in minimal_types:
  320. return True
  321. if "Test" in name: # skip all test types
  322. return True
  323. if re.search("NodeId$", name) != None:
  324. return True
  325. return False
  326. def stripTypename(tn):
  327. return tn[tn.find(":")+1:]
  328. def camlCase2CCase(item):
  329. "Member names begin with a lower case character"
  330. return item[:1].lower() + item[1:] if item else ''
  331. def typeReady(element):
  332. "Do we have the member types yet?"
  333. for child in element:
  334. if child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  335. if stripTypename(child.get("TypeName")) not in types:
  336. return False
  337. return True
  338. def parseEnumeration(typeXml):
  339. name = "UA_" + typeXml.get("Name")
  340. description = ""
  341. elements = OrderedDict()
  342. for child in typeXml:
  343. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  344. description = child.text
  345. if child.tag == "{http://opcfoundation.org/BinarySchema/}EnumeratedValue":
  346. elements[name + "_" + child.get("Name")] = child.get("Value")
  347. return EnumerationType(name, description, elements)
  348. def parseOpaque(typeXml):
  349. name = "UA_" + typeXml.get("Name")
  350. description = ""
  351. for child in typeXml:
  352. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  353. description = child.text
  354. return OpaqueType(name, description)
  355. def parseStructured(typeXml):
  356. "Returns None if we miss member descriptions"
  357. name = "UA_" + typeXml.get("Name")
  358. description = ""
  359. for child in typeXml:
  360. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  361. description = child.text
  362. # ignore lengthfields, just tag the array-members as an array
  363. lengthfields = []
  364. for child in typeXml:
  365. if child.get("LengthField"):
  366. lengthfields.append(child.get("LengthField"))
  367. members = OrderedDict()
  368. for child in typeXml:
  369. if not child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  370. continue
  371. if child.get("Name") in lengthfields:
  372. continue
  373. memberTypeName = "UA_" + stripTypename(child.get("TypeName"))
  374. if not memberTypeName in types:
  375. return None
  376. memberType = types[memberTypeName]
  377. memberName = camlCase2CCase(child.get("Name"))
  378. isArray = True if child.get("LengthField") else False
  379. members[memberName] = StructMember(memberName, memberType, isArray)
  380. return StructType(name, description, members)
  381. finished = False
  382. while(not finished):
  383. finished = True
  384. for typeXml in typeSnippets:
  385. name = "UA_" + typeXml.get("Name")
  386. if name in types or skipType(name):
  387. continue
  388. if typeXml.tag == "{http://opcfoundation.org/BinarySchema/}EnumeratedType":
  389. t = parseEnumeration(typeXml)
  390. types[t.name] = t
  391. elif typeXml.tag == "{http://opcfoundation.org/BinarySchema/}OpaqueType":
  392. t = parseOpaque(typeXml)
  393. types[t.name] = t
  394. elif typeXml.tag == "{http://opcfoundation.org/BinarySchema/}StructuredType":
  395. t = parseStructured(typeXml)
  396. if t == None:
  397. finished = False
  398. else:
  399. types[t.name] = t
  400. # remove the existing types
  401. for k in existing_types.keys():
  402. types.pop(k)
  403. return types
  404. parser = argparse.ArgumentParser()
  405. parser.add_argument('--ns0-types-xml', nargs=1, help='xml-definition of the ns0 types that are assumed to already exist')
  406. parser.add_argument('--enable-subscription-types', nargs=1, help='Generate datatypes necessary for Montoring and Subscriptions.')
  407. parser.add_argument('--typedescriptions', nargs=1, help='csv file with type descriptions')
  408. parser.add_argument('--typeintrospection', help='add the type and member names to the idatatype structures', action='store_true')
  409. parser.add_argument('namespace_id', type=int, help='the id of the target namespace')
  410. parser.add_argument('types_xml', help='path/to/Opc.Ua.Types.bsd')
  411. parser.add_argument('outfile', help='output file w/o extension')
  412. args = parser.parse_args()
  413. outname = args.outfile.split("/")[-1]
  414. inname = args.types_xml.split("/")[-1]
  415. typeintrospection = args.typeintrospection
  416. existing_types = OrderedDict()
  417. if args.enable_subscription_types:
  418. minimal_types = minimal_types + subscription_types
  419. if args.namespace_id == 0 or args.ns0_types_xml:
  420. existing_types = OrderedDict([(t, BuiltinType(t)) for t in builtin_types])
  421. if args.ns0_types_xml:
  422. if sys.version_info[0] < 3:
  423. OrderedDict(existing_types.items() + parseTypeDefinitions(args.ns0_types_xml[0], existing_types).items())
  424. else:
  425. OrderedDict(list(existing_types.items()) + list(parseTypeDefinitions(args.ns0_types_xml[0], existing_types).items()))
  426. types = parseTypeDefinitions(args.types_xml, existing_types)
  427. if args.namespace_id == 0:
  428. if sys.version_info[0] < 3:
  429. types = OrderedDict(existing_types.items() + types.items())
  430. else:
  431. types = OrderedDict(list(existing_types.items()) + list(types.items()))
  432. typedescriptions = {}
  433. if args.typedescriptions:
  434. typedescriptions = parseTypeDescriptions(args.typedescriptions, args.namespace_id)
  435. fh = open(args.outfile + "_generated.h",'w')
  436. fe = open(args.outfile + "_generated_encoding_binary.h",'w')
  437. fc = open(args.outfile + "_generated.c",'w')
  438. def printh(string):
  439. print(string, end='\n', file=fh)
  440. def printe(string):
  441. print(string, end='\n', file=fe)
  442. def printc(string):
  443. print(string, end='\n', file=fc)
  444. printh('''/**
  445. * @file ''' + outname + '''_generated.h
  446. *
  447. * @brief Autogenerated data types
  448. *
  449. * Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  450. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + '''
  451. */
  452. #ifndef ''' + outname.upper() + '''_GENERATED_H_
  453. #define ''' + outname.upper() + '''_GENERATED_H_
  454. #ifdef __cplusplus
  455. extern "C" {
  456. #endif
  457. #include "ua_types.h"
  458. #ifdef UA_INTERNAL
  459. #include "ua_types_encoding_binary.h"
  460. #endif'''
  461. + ('\n#include "ua_types_generated.h"\n' if args.namespace_id != 0 else '') + '''
  462. /**
  463. * @ingroup types
  464. *
  465. * @defgroup ''' + outname + '''_generated Autogenerated ''' + outname + ''' Types
  466. *
  467. * @brief Data structures that are autogenerated from an XML-Schema.
  468. *
  469. * @{
  470. */
  471. ''')
  472. printh("#define " + outname.upper() + "_COUNT %s\n" % (str(len(types))))
  473. printh("extern UA_EXPORT const UA_DataType *" + outname.upper() + ";\n")
  474. i = 0
  475. if sys.version_info[0] < 3:
  476. values = types.itervalues()
  477. else:
  478. values = types.values()
  479. for t in values:
  480. if type(t) != BuiltinType:
  481. printh("")
  482. if t.description != "":
  483. printh("/** @brief " + t.description + " */")
  484. printh(t.typedef_c())
  485. printh("#define " + outname.upper() + "_" + t.name[3:].upper() + " " + str(i))
  486. if type(t) != BuiltinType:
  487. printh(t.functions_c(outname.upper()))
  488. i += 1
  489. printh('''
  490. /// @} /* end of group */\n
  491. #ifdef __cplusplus
  492. } // extern "C"
  493. #endif\n
  494. #endif /* %s_GENERATED_H_ */''' % outname.upper())
  495. printe('''/**
  496. * @file ''' + outname + '''_generated_encoding_binary.h
  497. *
  498. * @brief Binary encoding for autogenerated data types
  499. *
  500. * Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  501. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + '''
  502. */\n
  503. #include "ua_types_encoding_binary.h"
  504. #include "''' + outname + '''_generated.h"''')
  505. printc('''/**
  506. * @file ''' + outname + '''_generated.c
  507. *
  508. * @brief Autogenerated data types
  509. *
  510. * Generated from ''' + inname + ''' with script ''' + sys.argv[0] + '''
  511. * on host ''' + platform.uname()[1] + ''' by user ''' + getpass.getuser() + ''' at ''' + time.strftime("%Y-%m-%d %I:%M:%S") + '''
  512. */\n
  513. #include "stddef.h"
  514. #include "ua_types.h"
  515. #include "''' + outname + '''_generated.h"\n
  516. const UA_DataType *''' + outname.upper() + ''' = (UA_DataType[]){''')
  517. if sys.version_info[0] < 3:
  518. values = types.itervalues()
  519. else:
  520. values = types.values()
  521. for t in values:
  522. printc("")
  523. printc("/* " + t.name + " */")
  524. if args.typedescriptions:
  525. td = typedescriptions[t.name]
  526. else:
  527. td = None
  528. printc(t.typelayout_c(args.namespace_id == 0, td, outname) + ",")
  529. if not type(t) == BuiltinType:
  530. printe("")
  531. printe("/* " + t.name + " */")
  532. printe(t.encoding_h(outname.upper()))
  533. printc("};\n")
  534. # if args.typedescriptions:
  535. # printc('const UA_UInt32 *' + outname.upper() + '_IDS = (UA_UInt32[]){')
  536. # for t in types.itervalues():
  537. # print(str(typedescriptions[t.name].nodeid) + ", ", end='', file=fc)
  538. # printc("};")
  539. fh.close()
  540. fe.close()
  541. fc.close()