generate_namespace.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. from __future__ import print_function
  2. import inspect
  3. import sys
  4. import platform
  5. import getpass
  6. import time
  7. import re
  8. import argparse
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('--with-xml', action='store_true', help='generate xml encoding')
  11. parser.add_argument('--with-json', action='store_true', help='generate json encoding')
  12. parser.add_argument('--only-needed', action='store_true', help='generate only types needed for compile')
  13. parser.add_argument('nodeids', help='path/to/NodeIds.csv')
  14. parser.add_argument('outfile', help='outfile w/o extension')
  15. args = parser.parse_args()
  16. # whitelist for "only needed" profile
  17. from type_lists import only_needed_types
  18. # types that are to be excluded
  19. exclude_types = set(["Number", "Integer", "UInteger", "Enumeration", "Image", "ImageBMP",
  20. "ImageGIF", "ImageJPG", "ImagePNG", "References", "BaseVariableType",
  21. "BaseDataVariableType", "PropertyType", "DataTypeDescriptionType",
  22. "DataTypeDictionaryType", "NamingRuleType", "IntegerId", "Counter",
  23. "Duration", "NumericRange", "Time", "Date", "UtcTime", "LocaleId",
  24. "UserTokenType", "ApplicationType", "ApplicationInstanceCertificate",
  25. "ServerVendorCapabilityType", "ServerStatusType",
  26. "ServerDiagnosticsSummaryType", "SamplingIntervalDiagnosticsArrayType",
  27. "SamplingIntervalDiagnosticsType", "SubscriptionDiagnosticsArrayType",
  28. "SubscriptionDiagnosticsType", "SessionDiagnosticsArrayType",
  29. "SessionDiagnosticsVariableType", "SessionSecurityDiagnosticsArrayType",
  30. "SessionSecurityDiagnosticsType", "DataItemType", "AnalogItemType",
  31. "DiscreteItemType", "TwoStateDiscreteType", "MultiStateDiscreteType",
  32. "ProgramDiagnosticType", "StateVariableType", "FiniteStateVariableType",
  33. "TransitionVariableType", "FiniteTransitionVariableType", "BuildInfoType",
  34. "TwoStateVariableType", "ConditionVariableType",
  35. "MultiStateValueDiscreteType", "OptionSetType", "ArrayItemType",
  36. "YArrayItemType", "XYArrayItemType", "ImageItemType", "CubeItemType",
  37. "NDimensionArrayItemType"])
  38. fixed_size = ['UA_DeadbandType', 'UA_DataChangeTrigger', 'UA_Guid', 'UA_ApplicationType',
  39. 'UA_ComplexNumberType', 'UA_EnumeratedTestType', 'UA_BrowseResultMask',
  40. 'UA_TimeZoneDataType', 'UA_NodeClass', 'UA_IdType', 'UA_ServiceCounterDataType',
  41. 'UA_Float', 'UA_ModelChangeStructureVerbMask', 'UA_EndpointConfiguration',
  42. 'UA_NodeAttributesMask', 'UA_DataChangeFilter', 'UA_StatusCode',
  43. 'UA_MonitoringFilterResult', 'UA_OpenFileMode', 'UA_SecurityTokenRequestType',
  44. 'UA_ServerDiagnosticsSummaryDataType', 'UA_ElementOperand',
  45. 'UA_AggregateConfiguration', 'UA_UInt64', 'UA_FilterOperator',
  46. 'UA_ReadRawModifiedDetails', 'UA_ServerState', 'UA_FilterOperand',
  47. 'UA_SubscriptionAcknowledgement', 'UA_AttributeWriteMask', 'UA_SByte', 'UA_Int32',
  48. 'UA_Range', 'UA_Byte', 'UA_TimestampsToReturn', 'UA_UserTokenType', 'UA_Int16',
  49. 'UA_XVType', 'UA_AggregateFilterResult', 'UA_Boolean', 'UA_MessageSecurityMode',
  50. 'UA_AxisScaleEnumeration', 'UA_PerformUpdateType', 'UA_UInt16',
  51. 'UA_NotificationData', 'UA_DoubleComplexNumberType', 'UA_HistoryUpdateType',
  52. 'UA_MonitoringFilter', 'UA_NodeIdType', 'UA_BrowseDirection',
  53. 'UA_SamplingIntervalDiagnosticsDataType', 'UA_UInt32', 'UA_ChannelSecurityToken',
  54. 'UA_RedundancySupport', 'UA_MonitoringMode', 'UA_HistoryReadDetails',
  55. 'UA_ExceptionDeviationFormat', 'UA_ComplianceLevel', 'UA_DateTime', 'UA_Int64',
  56. 'UA_Double']
  57. def useDataType(row):
  58. if row[0] == "" or row[0] in exclude_types:
  59. return False
  60. if row[2] != "DataType":
  61. return False
  62. if "Test" in row[0]:
  63. return False
  64. if args.only_needed and not(row[0] in only_needed_types):
  65. return False
  66. return True
  67. type_names = [] # the names of the datattypes for which we create a vtable entry
  68. def useOtherType(row):
  69. if row[0] in type_names or row[0] == "":
  70. return False
  71. if row[0].startswith("Audit"):
  72. return False
  73. if row[0].startswith("Program"):
  74. return False
  75. if row[0].startswith("Condition"):
  76. return False
  77. if row[0].startswith("Refresh"):
  78. return False
  79. if row[0].startswith("OpcUa_"):
  80. return False
  81. if row[0].startswith("SessionsDiagnosticsSummaryType_"):
  82. return False
  83. if "Type_" in row[0]:
  84. return False
  85. if "_Encoding_Default" in row[0]:
  86. return False
  87. return True
  88. f = open(args.nodeids)
  89. input_str = f.read() + "\nInvalidType,0,DataType" + "\nHasModelParent,50,ReferenceType"
  90. f.close()
  91. input_str = input_str.replace('\r','')
  92. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  93. for index, row in enumerate(rows):
  94. if row[0] == "BaseDataType":
  95. rows[index]= ("Variant", row[1], row[2])
  96. elif row[0] == "Structure":
  97. rows[index] = ("ExtensionObject", row[1], row[2])
  98. fh = open(args.outfile + ".h",'w')
  99. fc = open(args.outfile + ".c",'w')
  100. def printh(string):
  101. print(string % inspect.currentframe().f_back.f_locals, end='\n', file=fh)
  102. def printc(string):
  103. print(string % inspect.currentframe().f_back.f_locals, end='\n', file=fc)
  104. printh('''/**********************************************************
  105. * '''+args.outfile+'''.hgen -- do not modify
  106. **********************************************************
  107. * Generated from '''+args.nodeids+''' with script '''+sys.argv[0]+'''
  108. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+
  109. time.strftime("%Y-%m-%d %I:%M:%S")+'''
  110. **********************************************************/\n
  111. #ifndef ''' + args.outfile.upper().split("/")[-1] + '''_H_
  112. #define ''' + args.outfile.upper().split("/")[-1] + '''_H_\n
  113. #include "ua_types.h" // definition of UA_VTable and basic UA_Types
  114. #include "ua_types_generated.h"\n
  115. /**
  116. * @brief maps namespace zero nodeId to index into UA_VTable
  117. *
  118. * @param[in] id The namespace zero nodeId
  119. *
  120. * @retval UA_ERR_INVALID_VALUE whenever ns0Id could not be mapped
  121. * @retval the corresponding index into UA_VTable
  122. */
  123. UA_UInt32 UA_ns0ToVTableIndex(const UA_NodeId *id);\n
  124. extern const UA_TypeVTable UA_EXPORT *UA_TYPES;
  125. extern const UA_NodeId UA_EXPORT *UA_NODEIDS;
  126. extern const UA_ExpandedNodeId UA_EXPORT *UA_EXPANDEDNODEIDS;
  127. /** The entries of UA_TYPES can be accessed with the following indices */ ''')
  128. printc('''/**********************************************************
  129. * '''+args.outfile.split("/")[-1]+'''.cgen -- do not modify
  130. **********************************************************
  131. * Generated from '''+args.nodeids+''' with script '''+sys.argv[0]+'''
  132. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser() +
  133. ''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  134. **********************************************************/\n
  135. #include "''' + args.outfile.split("/")[-1] + '''.h"\n
  136. #include "ua_util.h"
  137. UA_UInt32 UA_ns0ToVTableIndex(const UA_NodeId *id) {
  138. UA_Int32 retval = 0; // InvalidType
  139. if(id->namespaceIndex != 0) return retval;
  140. switch (id->identifier.numeric) {''')
  141. i = 0
  142. for index, row in enumerate(rows):
  143. if not useDataType(row):
  144. continue
  145. type_names.append(row[0])
  146. name = "UA_" + row[0]
  147. printh("#define "+name.upper()+" "+str(i))
  148. printh('#define '+name.upper()+'_NS0 '+row[1])
  149. printc('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2])
  150. i = i+1
  151. printc('''\t}\n\treturn retval;\n}\n''');
  152. printh('\n#define SIZE_UA_VTABLE '+str(i));
  153. printh("") # newline
  154. printh("/* Now all the non-datatype nodeids */")
  155. for row in rows:
  156. if not useOtherType(row):
  157. continue
  158. name = "UA_" + row[0]
  159. printh("#define "+name.upper()+" "+str(i))
  160. printh('#define '+name.upper()+'_NS0 '+row[1])
  161. i=i+1
  162. printc('''const UA_TypeVTable *UA_TYPES = (UA_TypeVTable[]){''')
  163. for row in rows:
  164. if row[0] not in type_names:
  165. continue
  166. name = "UA_" + row[0]
  167. printc("\t{.typeId={.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric=" + row[1] + "}" +
  168. ",\n.name=(UA_Byte*)&\"%(name)s\"" +
  169. ",\n.new=(void *(*)(void))%(name)s_new" +
  170. ",\n.init=(void(*)(void *))%(name)s_init"+
  171. ",\n.copy=(UA_StatusCode(*)(void const * ,void*))%(name)s_copy" +
  172. ",\n.delete=(void(*)(void *))%(name)s_delete" +
  173. ",\n.deleteMembers=(void(*)(void *))%(name)s_deleteMembers" +
  174. ",\n#ifdef UA_DEBUG //FIXME: seems to be okay atm, however a pointer to a noop function would be more safe" +
  175. "\n.print=(void(*)(const void *, FILE *))%(name)s_print," +
  176. "\n#endif" +
  177. "\n.memSize=" + ("sizeof(%(name)s)" if (name != "UA_InvalidType") else "0") +
  178. ",\n.dynMembers=" + ("UA_FALSE" if (name in fixed_size) else "UA_TRUE") +
  179. ",\n.encodings={{.calcSize=(UA_UInt32(*)(const void*))%(name)s_calcSizeBinary" +
  180. ",\n.encode=(UA_StatusCode(*)(const void*,UA_ByteString*,UA_UInt32*))%(name)s_encodeBinary" +
  181. ",\n.decode=(UA_StatusCode(*)(const UA_ByteString*,UA_UInt32*,void*))%(name)s_decodeBinary}" +
  182. (",\n{.calcSize=(UA_Int32(*)(const void*))%(name)s_calcSizeXml" +
  183. ",\n.encode=(UA_StatusCode(*)(const void*,UA_ByteString*,UA_UInt32*))%(name)s_encodeXml" +
  184. ",\n.decode=(UA_StatusCode(*)(const UA_ByteString*,UA_UInt32*,void*))%(name)s_decodeXml}" if (args.with_xml) else "") +
  185. "}},")
  186. printc('};\n')
  187. # make the nodeids available as well
  188. printc('''const UA_NodeId *UA_NODEIDS = (UA_NodeId[]){''')
  189. for row in rows:
  190. if not row[0] in type_names:
  191. continue
  192. printc("\t{.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = " + row[1] + "},")
  193. for row in rows:
  194. if not useOtherType(row):
  195. continue
  196. printc("\t{.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = " + row[1] + "},")
  197. printc('};\n')
  198. # and generate expanded nodeids
  199. printc('''const UA_ExpandedNodeId *UA_EXPANDEDNODEIDS = (UA_ExpandedNodeId[]){''')
  200. for row in rows:
  201. if not row[0] in type_names:
  202. continue
  203. printc("\t{.nodeId = {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = " + row[1] +
  204. "}, .namespaceUri = {.length = -1, .data = UA_NULL}, .serverIndex = 0},")
  205. for row in rows:
  206. if not useOtherType(row):
  207. continue
  208. printc("\t{.nodeId = {.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric = " + row[1] +
  209. "}, .namespaceUri = {.length = -1, .data = UA_NULL}, .serverIndex = 0},")
  210. printc('};')
  211. printh('\n#endif /* OPCUA_NAMESPACE_0_H_ */')
  212. fh.close()
  213. fc.close()