generate_builtin.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 inspect
  10. import argparse
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('--with-xml', action='store_true', help='generate xml encoding')
  13. parser.add_argument('--with-json', action='store_true', help='generate json encoding')
  14. parser.add_argument('--only-nano', action='store_true', help='generate only the types for the nano profile')
  15. parser.add_argument('--only-needed', action='store_true', help='generate only types needed for compile')
  16. parser.add_argument('--additional-includes', action='store', help='include additional header files (separated by comma)')
  17. parser.add_argument('types', help='path/to/Opc.Ua.Types.bsd')
  18. parser.add_argument('outfile', help='outfile w/o extension')
  19. args = parser.parse_args()
  20. ns = {"opc": "http://opcfoundation.org/BinarySchema/"}
  21. tree = etree.parse(args.types)
  22. types = tree.xpath("/opc:TypeDictionary/*[not(self::opc:Import)]", namespaces=ns)
  23. fh = open(args.outfile + ".h",'w')
  24. fc = open(args.outfile + ".c",'w')
  25. # dirty hack. we go up the call frames to access local variables of the calling
  26. # function. this allows to shorten code and get %()s replaces with less clutter.
  27. def printh(string):
  28. print(string % inspect.currentframe().f_back.f_locals, end='\n', file=fh)
  29. def printc(string):
  30. print(string % inspect.currentframe().f_back.f_locals, end='\n', file=fc)
  31. # types that are coded manually
  32. from type_lists import existing_types
  33. # whitelist for "only needed" profile
  34. from type_lists import only_needed_types
  35. # some types are omitted (pretend they exist already)
  36. existing_types.add("NodeIdType")
  37. fixed_size = set(["UA_Boolean", "UA_SByte", "UA_Byte", "UA_Int16", "UA_UInt16",
  38. "UA_Int32", "UA_UInt32", "UA_Int64", "UA_UInt64", "UA_Float",
  39. "UA_Double", "UA_DateTime", "UA_Guid", "UA_StatusCode"])
  40. # types we do not want to autogenerate
  41. def skipType(name):
  42. if name in existing_types:
  43. return True
  44. if "Test" in name: #skip all Test types
  45. return True
  46. if re.search("NodeId$", name) != None:
  47. return True
  48. if args.only_needed and not(name in only_needed_types):
  49. return True
  50. return False
  51. def stripTypename(tn):
  52. return tn[tn.find(":")+1:]
  53. def camlCase2CCase(item):
  54. if item in ["Float","Double"]:
  55. return "my" + item
  56. return item[:1].lower() + item[1:] if item else ''
  57. # are the types we need already in place? if not, postpone.
  58. def printableStructuredType(element):
  59. for child in element:
  60. if child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  61. typename = stripTypename(child.get("TypeName"))
  62. if typename not in existing_types:
  63. return False
  64. return True
  65. def createEnumerated(element):
  66. valuemap = OrderedDict()
  67. name = "UA_" + element.get("Name")
  68. fixed_size.add(name)
  69. for child in element:
  70. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  71. printh("/** @brief " + child.text + " */")
  72. if child.tag == "{http://opcfoundation.org/BinarySchema/}EnumeratedValue":
  73. valuemap[name + "_" + child.get("Name")] = child.get("Value")
  74. valuemap = OrderedDict(sorted(valuemap.iteritems(), key=lambda (k,v): int(v)))
  75. printh("typedef UA_Int32 " + name + ";")
  76. printh("enum " + name + "_enum { \n\t" +
  77. ",\n\t".join(map(lambda (key, value) : key.upper() + " = " + value, valuemap.iteritems())) +
  78. "\n};")
  79. printh("UA_TYPE_PROTOTYPES (" + name + ")")
  80. printh("UA_TYPE_BINARY_ENCODING(" + name + ")")
  81. printc("UA_TYPE_AS(" + name + ", UA_Int32)")
  82. printc("UA_TYPE_BINARY_ENCODING_AS(" + name + ", UA_Int32)")
  83. if args.with_xml:
  84. printh("UA_TYPE_XML_ENCODING(" + name + ")\n")
  85. printc('''UA_TYPE_METHOD_CALCSIZEXML_NOTIMPL(%(name)s)
  86. UA_TYPE_METHOD_ENCODEXML_NOTIMPL(%(name)s)
  87. UA_TYPE_METHOD_DECODEXML_NOTIMPL(%(name)s\n)''')
  88. def createOpaque(element):
  89. name = "UA_" + element.get("Name")
  90. for child in element:
  91. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  92. printh("/** @brief " + child.text + " */")
  93. printh("typedef UA_ByteString %(name)s;")
  94. printh("UA_TYPE_PROTOTYPES(%(name)s)")
  95. printh("UA_TYPE_BINARY_ENCODING(%(name)s)")
  96. printc("UA_TYPE_AS(%(name)s, UA_ByteString)")
  97. printc("UA_TYPE_BINARY_ENCODING_AS(%(name)s, UA_ByteString)")
  98. if args.with_xml:
  99. printh("UA_TYPE_XML_ENCODING(" + name + ")\n")
  100. printc('''UA_TYPE_METHOD_CALCSIZEXML_NOTIMPL(%(name)s)
  101. UA_TYPE_METHOD_ENCODEXML_NOTIMPL(%(name)s)
  102. UA_TYPE_METHOD_DECODEXML_NOTIMPL(%(name)s)\n''')
  103. def createStructured(element):
  104. name = "UA_" + element.get("Name")
  105. # 1) Are there arrays in the type?
  106. lengthfields = set()
  107. for child in element:
  108. if child.get("LengthField"):
  109. lengthfields.add(child.get("LengthField"))
  110. # 2) Store members in membermap (name->type).
  111. membermap = OrderedDict()
  112. for child in element:
  113. if child.tag == "{http://opcfoundation.org/BinarySchema/}Documentation":
  114. printh("/** @brief " + child.text + " */")
  115. elif child.tag == "{http://opcfoundation.org/BinarySchema/}Field":
  116. if child.get("Name") in lengthfields:
  117. continue
  118. childname = camlCase2CCase(child.get("Name"))
  119. typename = stripTypename(child.get("TypeName"))
  120. if child.get("LengthField"):
  121. membermap[childname] = "UA_" + typename + "*"
  122. else:
  123. membermap[childname] = "UA_" + typename
  124. # 3) Print structure
  125. if len(membermap) > 0:
  126. printh("typedef struct %(name)s {")
  127. for n,t in membermap.iteritems():
  128. if t.find("*") != -1:
  129. printh("\t" + "UA_Int32 " + n + "Size;")
  130. printh("\t%(t)s %(n)s;")
  131. printh("} %(name)s;")
  132. else:
  133. printh("typedef void* %(name)s;")
  134. # 3) function prototypes
  135. printh("UA_TYPE_PROTOTYPES(" + name + ")")
  136. printh("UA_TYPE_BINARY_ENCODING(" + name + ")")
  137. if args.with_xml:
  138. printh("UA_TYPE_XML_ENCODING(" + name + ")\n")
  139. # 4) CalcSizeBinary
  140. printc('''UA_UInt32 %(name)s_calcSizeBinary(%(name)s const * ptr) {
  141. return 0''')
  142. has_fixed_size = True
  143. for n,t in membermap.iteritems():
  144. if t in fixed_size:
  145. printc('\t + sizeof(%(t)s) // %(n)s')
  146. elif t.find("*") != -1:
  147. printc('\t + UA_Array_calcSizeBinary(ptr->%(n)sSize,&UA_['+ t[0:t.find("*")].upper() +
  148. "],ptr->%(n)s)")
  149. has_fixed_size = False
  150. else:
  151. printc('\t + %(t)s_calcSizeBinary(&ptr->%(n)s)')
  152. has_fixed_size = False
  153. printc("\t;\n}\n")
  154. if has_fixed_size:
  155. fixed_size.add(name)
  156. # 5) EncodeBinary
  157. printc('''UA_StatusCode %(name)s_encodeBinary(%(name)s const * src, UA_ByteString* dst, UA_UInt32 *offset) {
  158. UA_StatusCode retval = UA_STATUSCODE_GOOD;''')
  159. for n,t in membermap.iteritems():
  160. if t.find("*") != -1:
  161. printc("\tretval |= UA_Array_encodeBinary(src->%(n)s,src->%(n)sSize,&UA_[" + t[0:t.find("*")].upper() + "],dst,offset);")
  162. else:
  163. printc('\tretval |= %(t)s_encodeBinary(&src->%(n)s,dst,offset);')
  164. printc("\treturn retval;\n}\n")
  165. # 6) DecodeBinary
  166. printc('''UA_StatusCode %(name)s_decodeBinary(UA_ByteString const * src, UA_UInt32 *offset, %(name)s * dst) {
  167. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  168. %(name)s_init(dst);''')
  169. printc('\t'+name+'_init(dst);')
  170. for n,t in membermap.iteritems():
  171. if t.find("*") != -1:
  172. printc('\tretval |= UA_Int32_decodeBinary(src,offset,&dst->%(n)sSize);')
  173. printc('\tif(!retval) { retval |= UA_Array_decodeBinary(src,offset,dst->%(n)sSize,&UA_[' + t[0:t.find("*")].upper() + '],(void**)&dst->%(n)s); }')
  174. printc('\tif(retval) { dst->%(n)sSize = -1; }') # arrays clean up internally. But the size needs to be set here for the eventual deleteMembers.
  175. else:
  176. printc('\tretval |= %(t)s_decodeBinary(src,offset,&dst->%(n)s);')
  177. printc("\tif(retval) %(name)s_deleteMembers(dst);")
  178. printc("\treturn retval;\n}\n")
  179. # 7) Xml
  180. if args.with_xml:
  181. printc('''UA_TYPE_METHOD_CALCSIZEXML_NOTIMPL(%(name)s)
  182. UA_TYPE_METHOD_ENCODEXML_NOTIMPL(%(name)s)
  183. UA_TYPE_METHOD_DECODEXML_NOTIMPL(%(name)s)''')
  184. # 8) Delete
  185. printc('''void %(name)s_delete(%(name)s *p) {
  186. %(name)s_deleteMembers(p);
  187. UA_free(p);\n}\n''')
  188. # 9) DeleteMembers
  189. printc('''void %(name)s_deleteMembers(%(name)s *p) {''')
  190. for n,t in membermap.iteritems():
  191. if not t in fixed_size: # dynamic size on the wire
  192. if t.find("*") != -1:
  193. printc("\tUA_Array_delete((void*)p->%(n)s,p->%(n)sSize,&UA_["+t[0:t.find("*")].upper()+"]);")
  194. else:
  195. printc('\t%(t)s_deleteMembers(&p->%(n)s);')
  196. printc("}\n")
  197. # 10) Init
  198. printc('''void %(name)s_init(%(name)s *p) {
  199. if(!p) return;''')
  200. for n,t in membermap.iteritems():
  201. if t.find("*") != -1:
  202. printc('\tp->%(n)sSize = -1;')
  203. printc('\tp->%(n)s = UA_NULL;')
  204. else:
  205. printc('\t%(t)s_init(&p->%(n)s);')
  206. printc("}\n")
  207. # 11) New
  208. printc("UA_TYPE_NEW_DEFAULT(%(name)s)")
  209. # 12) Copy
  210. printc('''UA_StatusCode %(name)s_copy(const %(name)s *src,%(name)s *dst) {
  211. UA_StatusCode retval = UA_STATUSCODE_GOOD;''')
  212. printc("\t%(name)s_init(dst);")
  213. for n,t in membermap.iteritems():
  214. if t.find("*") != -1:
  215. printc('\tdst->%(n)sSize = src->%(n)sSize;')
  216. printc("\tretval |= UA_Array_copy(src->%(n)s, src->%(n)sSize,&UA_[" + t[0:t.find("*")].upper() + "],(void**)&dst->%(n)s);")
  217. continue
  218. if not t in fixed_size: # there are members of variable size
  219. printc('\tretval |= %(t)s_copy(&src->%(n)s,&dst->%(n)s);')
  220. continue
  221. printc("\tdst->%(n)s = src->%(n)s;")
  222. printc('''\tif(retval)
  223. \t%(name)s_deleteMembers(dst);''')
  224. printc("\treturn retval;\n}\n")
  225. # 13) Print
  226. printc('''#ifdef DEBUG''')
  227. printc('''void %(name)s_print(const %(name)s *p, FILE *stream) {
  228. fprintf(stream, "(%(name)s){");''')
  229. for i,(n,t) in enumerate(membermap.iteritems()):
  230. if t.find("*") != -1:
  231. printc('\tUA_Int32_print(&p->%(n)sSize, stream);')
  232. printc("\tUA_Array_print(p->%(n)s, p->%(n)sSize, &UA_[" + t[0:t.find("*")].upper()+"], stream);")
  233. else:
  234. printc('\t%(t)s_print(&p->%(n)s,stream);')
  235. if i == len(membermap)-1:
  236. continue
  237. printc('\tfprintf(stream, ",");')
  238. printc('''\tfprintf(stream, "}");\n}''')
  239. printc('#endif');
  240. printc('''\n''')
  241. shortname = args.outfile.split("/")[-1]
  242. print(shortname)
  243. printh('''/**
  244. * @file %(shortname)s.h
  245. *
  246. * @brief Autogenerated data types defined in the UA standard
  247. *
  248. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  249. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  250. */
  251. #ifndef ''' + shortname.upper() + '''_H_
  252. #define ''' + shortname.upper() + '''_H_
  253. #include "ua_types.h"
  254. #include "ua_types_encoding_binary.h"
  255. /** @ingroup ''' + shortname.split("_")[1] + '''
  256. *
  257. * @defgroup ''' + shortname + ''' Generated Types
  258. *
  259. * @brief Data structures that are autogenerated from an XML-Schema.
  260. * @{
  261. */''')
  262. if args.with_xml:
  263. printh('#include "ua_types_encoding_xml.h"')
  264. if args.additional_includes:
  265. for incl in args.additional_includes.split(","):
  266. printh("#include \"" + incl + "\"")
  267. printh("") # newline
  268. printc('''/**
  269. * @file '''+sys.argv[2]+'''.c
  270. *
  271. * @brief Autogenerated function implementations to manage the data types defined in the UA standard
  272. *
  273. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  274. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  275. */
  276. #include "''' + args.outfile.split("/")[-1] + '''.h"
  277. #include "ua_namespace_0.h"
  278. #include "ua_util.h"\n''')
  279. # types for which we create a vector type
  280. arraytypes = set()
  281. fields = tree.xpath("//opc:Field", namespaces=ns)
  282. for field in fields:
  283. if field.get("LengthField"):
  284. arraytypes.add(stripTypename(field.get("TypeName")))
  285. deferred_types = OrderedDict()
  286. #plugin handling
  287. import os
  288. files = [f for f in os.listdir('.') if os.path.isfile(f) and f[-3:] == ".py" and f[:7] == "plugin_"]
  289. plugin_types = []
  290. packageForType = OrderedDict()
  291. for f in files:
  292. package = f[:-3]
  293. exec "import " + package
  294. exec "pluginSetup = " + package + ".setup()"
  295. if pluginSetup["pluginType"] == "structuredObject":
  296. plugin_types.append(pluginSetup["tagName"])
  297. packageForType[pluginSetup["tagName"]] = [package,pluginSetup]
  298. print("Custom object creation for tag " + pluginSetup["tagName"] + " imported from package " + package)
  299. #end plugin handling
  300. for element in types:
  301. name = element.get("Name")
  302. if skipType(name):
  303. continue
  304. if element.tag == "{http://opcfoundation.org/BinarySchema/}EnumeratedType":
  305. createEnumerated(element)
  306. existing_types.add(name)
  307. elif element.tag == "{http://opcfoundation.org/BinarySchema/}StructuredType":
  308. if printableStructuredType(element):
  309. createStructured(element)
  310. existing_types.add(name)
  311. else: # the record contains types that were not yet detailed
  312. deferred_types[name] = element
  313. continue
  314. elif element.tag == "{http://opcfoundation.org/BinarySchema/}OpaqueType":
  315. createOpaque(element)
  316. existing_types.add(name)
  317. for name, element in deferred_types.iteritems():
  318. if name in plugin_types:
  319. #execute plugin if registered
  320. exec "ret = " + packageForType[name][0]+"."+packageForType[name][1]["functionCall"]
  321. if ret == "default":
  322. createStructured(element)
  323. existing_types.add(name)
  324. else:
  325. createStructured(element)
  326. existing_types.add(name)
  327. printh('/// @} /* end of group */')
  328. printh('#endif')
  329. fh.close()
  330. fc.close()