generate_namespace.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "ua_types.h" // definition of UA_VTable and basic UA_Types
  89. #include "ua_types_generated.h"\n
  90. /**
  91. * @brief maps namespace zero nodeId to index into UA_VTable
  92. *
  93. * @param[in] id The namespace zero nodeId
  94. *
  95. * @retval UA_ERR_INVALID_VALUE whenever ns0Id could not be mapped
  96. * @retval the corresponding index into UA_VTable
  97. */
  98. UA_Int32 UA_ns0ToVTableIndex(const UA_NodeId *id);\n
  99. extern const UA_VTable_Entry *UA_;
  100. /**
  101. * @brief the set of possible indices into UA_VTable
  102. *
  103. * Enumerated type to define the types that the open62541 stack can handle
  104. */
  105. enum UA_VTableIndex_enum {''')
  106. printc('''/**********************************************************
  107. * '''+args.outfile.split("/")[-1]+'''.cgen -- do not modify
  108. **********************************************************
  109. * Generated from '''+args.nodeids+''' with script '''+sys.argv[0]+'''
  110. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser() +
  111. ''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  112. **********************************************************/\n
  113. #include "''' + args.outfile.split("/")[-1] + '''.h"\n
  114. #include "ua_util.h"
  115. UA_Int32 UA_ns0ToVTableIndex(const UA_NodeId *id) {
  116. UA_Int32 retval = 0; // InvalidType
  117. if(id->namespaceIndex != 0) return retval;
  118. switch (id->identifier.numeric) {''')
  119. i = 0
  120. for row in rows:
  121. if skipType(row):
  122. continue
  123. if row[0] == "BaseDataType":
  124. name = "UA_Variant"
  125. elif row[0] == "Structure":
  126. name = "UA_ExtensionObject"
  127. else:
  128. name = "UA_" + row[0]
  129. printh("\t"+name.upper()+" = "+str(i)+",")
  130. printc('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2])
  131. i = i+1
  132. printh("};\n")
  133. printc('''\n}\nreturn retval;\n}\n''');
  134. printc('''const UA_VTable_Entry *UA_ = (UA_VTable_Entry[]){''')
  135. i = 0
  136. for row in rows:
  137. if skipType(row):
  138. continue
  139. if row[0] == "BaseDataType":
  140. name = "UA_Variant"
  141. elif row[0] == "Structure":
  142. name = "UA_ExtensionObject"
  143. else:
  144. name = "UA_" + row[0]
  145. i=i+1
  146. printh('#define '+name.upper()+'_NS0 '+row[1])
  147. printc("\t{.typeId={.namespaceIndex = 0, .identifierType = UA_NODEIDTYPE_NUMERIC, .identifier.numeric=" + row[1] + "}" +
  148. ",\n.name=(UA_Byte*)&\"%(name)s\"" +
  149. ",\n.new=(UA_Int32(*)(void **))%(name)s_new" +
  150. ",\n.init=(void(*)(void *))%(name)s_init"+
  151. ",\n.copy=(UA_Int32(*)(void const * ,void*))%(name)s_copy" +
  152. ",\n.delete=(void(*)(void *))%(name)s_delete" +
  153. ",\n.deleteMembers=(void(*)(void *))%(name)s_deleteMembers" +
  154. ",\n#ifdef DEBUG //FIXME: seems to be okay atm, however a pointer to a noop function would be more safe" +
  155. "\n.print=(void(*)(const void *, FILE *))%(name)s_print," +
  156. "\n#endif" +
  157. "\n.memSize=" + ("sizeof(%(name)s)" if (name != "UA_InvalidType") else "0") +
  158. ",\n.dynMembers=" + ("UA_FALSE" if (name in fixed_size) else "UA_TRUE") +
  159. ",\n.encodings={{.calcSize=(UA_Int32(*)(const void*))%(name)s_calcSizeBinary" +
  160. ",\n.encode=(UA_Int32(*)(const void*,UA_ByteString*,UA_UInt32*))%(name)s_encodeBinary" +
  161. ",\n.decode=(UA_Int32(*)(const UA_ByteString*,UA_UInt32*,void*))%(name)s_decodeBinary}" +
  162. (",\n{.calcSize=(UA_Int32(*)(const void*))%(name)s_calcSizeXml" +
  163. ",\n.encode=(UA_Int32(*)(const void*,UA_ByteString*,UA_UInt32*))%(name)s_encodeXml" +
  164. ",\n.decode=(UA_Int32(*)(const UA_ByteString*,UA_UInt32*,void*))%(name)s_decodeXml}" if (args.with_xml) else "") +
  165. "}},")
  166. printc('};')
  167. printh('\n#define SIZE_UA_VTABLE '+str(i));
  168. printh('\n#endif /* OPCUA_NAMESPACE_0_H_ */')
  169. fh.close()
  170. fc.close()