generate_namespace.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_kinds = set(["Object","ObjectType","Variable","Method","ReferenceType"])
  20. exclude_types = set(["Number", "Integer", "UInteger", "Enumeration", "Image", "ImageBMP",
  21. "ImageGIF", "ImageJPG", "ImagePNG", "References", "BaseVariableType",
  22. "BaseDataVariableType", "PropertyType", "DataTypeDescriptionType",
  23. "DataTypeDictionaryType", "NamingRuleType", "IntegerId", "Counter",
  24. "Duration", "NumericRange", "Time", "Date", "UtcTime", "LocaleId",
  25. "UserTokenType", "ApplicationType", "ApplicationInstanceCertificate",
  26. "ServerVendorCapabilityType", "ServerStatusType",
  27. "ServerDiagnosticsSummaryType", "SamplingIntervalDiagnosticsArrayType",
  28. "SamplingIntervalDiagnosticsType", "SubscriptionDiagnosticsArrayType",
  29. "SubscriptionDiagnosticsType", "SessionDiagnosticsArrayType",
  30. "SessionDiagnosticsVariableType", "SessionSecurityDiagnosticsArrayType",
  31. "SessionSecurityDiagnosticsType", "DataItemType", "AnalogItemType",
  32. "DiscreteItemType", "TwoStateDiscreteType", "MultiStateDiscreteType",
  33. "ProgramDiagnosticType", "StateVariableType", "FiniteStateVariableType",
  34. "TransitionVariableType", "FiniteTransitionVariableType", "BuildInfoType",
  35. "TwoStateVariableType", "ConditionVariableType",
  36. "MultiStateValueDiscreteType", "OptionSetType", "ArrayItemType",
  37. "YArrayItemType", "XYArrayItemType", "ImageItemType", "CubeItemType",
  38. "NDimensionArrayItemType"])
  39. fixed_size = ['UA_DeadbandType', 'UA_DataChangeTrigger', 'UA_Guid', 'UA_ApplicationType',
  40. 'UA_ComplexNumberType', 'UA_EnumeratedTestType', 'UA_BrowseResultMask',
  41. 'UA_TimeZoneDataType', 'UA_NodeClass', 'UA_IdType', 'UA_ServiceCounterDataType',
  42. 'UA_Float', 'UA_ModelChangeStructureVerbMask', 'UA_EndpointConfiguration',
  43. 'UA_NodeAttributesMask', 'UA_DataChangeFilter', 'UA_StatusCode',
  44. 'UA_MonitoringFilterResult', 'UA_OpenFileMode', 'UA_SecurityTokenRequestType',
  45. 'UA_ServerDiagnosticsSummaryDataType', 'UA_ElementOperand',
  46. 'UA_AggregateConfiguration', 'UA_UInt64', 'UA_FilterOperator',
  47. 'UA_ReadRawModifiedDetails', 'UA_ServerState', 'UA_FilterOperand',
  48. 'UA_SubscriptionAcknowledgement', 'UA_AttributeWriteMask', 'UA_SByte', 'UA_Int32',
  49. 'UA_Range', 'UA_Byte', 'UA_TimestampsToReturn', 'UA_UserTokenType', 'UA_Int16',
  50. 'UA_XVType', 'UA_AggregateFilterResult', 'UA_Boolean', 'UA_MessageSecurityMode',
  51. 'UA_AxisScaleEnumeration', 'UA_PerformUpdateType', 'UA_UInt16',
  52. 'UA_NotificationData', 'UA_DoubleComplexNumberType', 'UA_HistoryUpdateType',
  53. 'UA_MonitoringFilter', 'UA_NodeIdType', 'UA_BrowseDirection',
  54. 'UA_SamplingIntervalDiagnosticsDataType', 'UA_UInt32', 'UA_ChannelSecurityToken',
  55. 'UA_RedundancySupport', 'UA_MonitoringMode', 'UA_HistoryReadDetails',
  56. 'UA_ExceptionDeviationFormat', 'UA_ComplianceLevel', 'UA_DateTime', 'UA_Int64',
  57. 'UA_Double']
  58. def skipType(row):
  59. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  60. return True
  61. if "Test" in row[0]:
  62. return True
  63. if re.search("Attributes$", row[0]) != None:
  64. return True
  65. if args.only_needed and not(row[0] in only_needed_types):
  66. return True
  67. return False
  68. f = open(args.nodeids)
  69. input_str = f.read() + "\nInvalidType,0,DataType"
  70. input_str = input_str.replace('\r','')
  71. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  72. f.close()
  73. fh = open(args.outfile + ".h",'w')
  74. fc = open(args.outfile + ".c",'w')
  75. def printh(string):
  76. print(string % inspect.currentframe().f_back.f_locals, end='\n', file=fh)
  77. def printc(string):
  78. print(string % inspect.currentframe().f_back.f_locals, end='\n', file=fc)
  79. printh('''/**********************************************************
  80. * '''+args.outfile+'''.hgen -- do not modify
  81. **********************************************************
  82. * Generated from '''+args.nodeids+''' with script '''+sys.argv[0]+'''
  83. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+
  84. time.strftime("%Y-%m-%d %I:%M:%S")+'''
  85. **********************************************************/\n
  86. #ifndef ''' + args.outfile.upper().split("/")[-1] + '''_H_
  87. #define ''' + args.outfile.upper().split("/")[-1] + '''_H_\n
  88. #include "util/ua_util.h"
  89. #include "ua_types.h" // definition of UA_VTable and basic UA_Types
  90. #include "ua_types_generated.h"\n
  91. /**
  92. * @brief maps namespace zero nodeId to index into UA_VTable
  93. *
  94. * @param[in] id The namespace zero nodeId
  95. *
  96. * @retval UA_ERR_INVALID_VALUE whenever ns0Id could not be mapped
  97. * @retval the corresponding index into UA_VTable
  98. */\n
  99. UA_Int32 UA_ns0ToVTableIndex(const UA_NodeId *id);\n
  100. extern const UA_VTable UA_;\n
  101. static UA_Int32 phantom_delete(void * p) { return UA_SUCCESS; }
  102. extern const UA_VTable UA_borrowed_;\n
  103. /**
  104. * @brief the set of possible indices into UA_VTable
  105. *
  106. * Enumerated type to define the types that the open62541 stack can handle
  107. */
  108. enum UA_VTableIndex_enum {''')
  109. printc('''/**********************************************************
  110. * '''+args.outfile.split("/")[-1]+'''.cgen -- do not modify
  111. **********************************************************
  112. * Generated from '''+args.nodeids+''' with script '''+sys.argv[0]+'''
  113. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser() +
  114. ''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  115. **********************************************************/\n
  116. #include "''' + args.outfile.split("/")[-1] + '''.h"\n
  117. UA_Int32 UA_ns0ToVTableIndex(const UA_NodeId *id) {
  118. UA_Int32 retval = 0; // InvalidType
  119. if(id->namespaceIndex != 0) return retval;
  120. switch (id->identifier.numeric) {''')
  121. i = 0
  122. for row in rows:
  123. if skipType(row):
  124. continue
  125. if row[0] == "BaseDataType":
  126. name = "UA_Variant"
  127. elif row[0] == "Structure":
  128. name = "UA_ExtensionObject"
  129. else:
  130. name = "UA_" + row[0]
  131. printh("\t"+name.upper()+" = "+str(i)+",")
  132. printc('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2])
  133. i = i+1
  134. printh("};\n")
  135. printc('''\n}\nreturn retval;\n}\n''');
  136. printc('''const UA_VTable UA_ = {
  137. \t.getTypeIndex = UA_ns0ToVTableIndex,
  138. \t.types = (UA_VTable_Entry[]){''')
  139. i = 0
  140. for row in rows:
  141. if skipType(row):
  142. continue
  143. if row[0] == "BaseDataType":
  144. name = "UA_Variant"
  145. elif row[0] == "Structure":
  146. name = "UA_ExtensionObject"
  147. else:
  148. name = "UA_" + row[0]
  149. i=i+1
  150. printh('#define '+name.upper()+'_NS0 '+row[1])
  151. printc("\t{.typeId={.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric=" + row[1] + "}" +
  152. ",\n.name=(UA_Byte*)&\"%(name)s\"" +
  153. ",\n.new=(UA_Int32(*)(void **))%(name)s_new" +
  154. ",\n.init=(UA_Int32(*)(void *))%(name)s_init"+
  155. ",\n.copy=(UA_Int32(*)(void const * ,void*))%(name)s_copy" +
  156. ",\n.delete=(UA_Int32(*)(void *))%(name)s_delete" +
  157. ",\n.deleteMembers=(UA_Int32(*)(void *))%(name)s_deleteMembers" +
  158. ",\n#ifdef DEBUG //FIXME: seems to be okay atm, however a pointer to a noop function would be more safe" +
  159. "\n.print=(void(*)(const void *, FILE *))%(name)s_print," +
  160. "\n#endif" +
  161. "\n.memSize=" + ("sizeof(%(name)s)" if (name != "UA_InvalidType") else "0") +
  162. ",\n.dynMembers=" + ("UA_FALSE" if (name in fixed_size) else "UA_TRUE") +
  163. ",\n.encodings={{.calcSize=(UA_Int32(*)(const void*))%(name)s_calcSizeBinary" +
  164. ",\n.encode=(UA_Int32(*)(const void*,UA_ByteString*,UA_UInt32*))%(name)s_encodeBinary" +
  165. ",\n.decode=(UA_Int32(*)(const UA_ByteString*,UA_UInt32*,void*))%(name)s_decodeBinary}" +
  166. (",\n{.calcSize=(UA_Int32(*)(const void*))%(name)s_calcSizeXml" +
  167. ",\n.encode=(UA_Int32(*)(const void*,UA_ByteString*,UA_UInt32*))%(name)s_encodeXml" +
  168. ",\n.decode=(UA_Int32(*)(const UA_ByteString*,UA_UInt32*,void*))%(name)s_decodeXml}" if (args.with_xml) else "") +
  169. "}},")
  170. printc('''}};''')
  171. printc("UA_VTable_Entry borrowed_types[" + str(i) + "];");
  172. printc("const UA_VTable UA_borrowed_ = {\n"+
  173. "\t.getTypeIndex=UA_ns0ToVTableIndex,\n"+
  174. "\t.types = borrowed_types\n"+
  175. "};")
  176. printh('\n#define SIZE_UA_VTABLE '+str(i));
  177. printh('\n#endif /* OPCUA_NAMESPACE_0_H_ */')
  178. fh.close()
  179. fc.close()