generate_namespace.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. from __future__ import print_function
  2. import sys
  3. import platform
  4. import getpass
  5. import time
  6. if len(sys.argv) != 3:
  7. print("Usage: python generate_namespace.py <path/to/NodeIds.csv> <outfile w/o extension>", file=sys.stdout)
  8. exit(0)
  9. # types that are to be excluded
  10. exclude_kinds = set(["Object","ObjectType","Variable","Method","ReferenceType"])
  11. exclude_types = set(["Number", "Integer", "UInteger", "Enumeration",
  12. "Image", "ImageBMP", "ImageGIF", "ImageJPG", "ImagePNG",
  13. "References", "BaseVariableType", "BaseDataVariableType",
  14. "PropertyType", "DataTypeDescriptionType", "DataTypeDictionaryType", "NamingRuleType",
  15. "IntegerId","Counter","Duration","NumericRange","Time","Date",
  16. "UtcTime", "LocaleId","UserTokenType",
  17. "ApplicationType","ApplicationInstanceCertificate",
  18. "ServerVendorCapabilityType","ServerStatusType","ServerDiagnosticsSummaryType",
  19. "SamplingIntervalDiagnosticsArrayType", "SamplingIntervalDiagnosticsType",
  20. "SubscriptionDiagnosticsArrayType", "SubscriptionDiagnosticsType",
  21. "SessionDiagnosticsArrayType", "SessionDiagnosticsVariableType",
  22. "SessionSecurityDiagnosticsArrayType", "SessionSecurityDiagnosticsType",
  23. "DataItemType", "AnalogItemType", "DiscreteItemType", "TwoStateDiscreteType",
  24. "MultiStateDiscreteType", "ProgramDiagnosticType", "StateVariableType", "FiniteStateVariableType",
  25. "TransitionVariableType", "FiniteTransitionVariableType", "BuildInfoType", "TwoStateVariableType",
  26. "ConditionVariableType", "MultiStateValueDiscreteType", "OptionSetType", "ArrayItemType",
  27. "YArrayItemType", "XYArrayItemType", "ImageItemType", "CubeItemType", "NDimensionArrayItemType"
  28. ])
  29. f = open(sys.argv[1])
  30. input_str = f.read() + "\nInvalidType,0,DataType"
  31. input_str = input_str.replace('\r','')
  32. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  33. f.close()
  34. fh = open(sys.argv[2] + ".hgen",'w')
  35. fc = open(sys.argv[2] + ".cgen",'w')
  36. print('''/**********************************************************
  37. * '''+sys.argv[2]+'''.hgen -- do not modify
  38. **********************************************************
  39. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  40. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  41. **********************************************************/
  42. #ifndef OPCUA_NAMESPACE_0_H_
  43. #define OPCUA_NAMESPACE_0_H_
  44. #include "ua_basictypes.h" // definition of UA_VTable and basic UA_Types
  45. /**
  46. * @brief maps namespace zero nodeId to index into UA_VTable
  47. *
  48. * @param[in] ns0Id The namespace zero nodeId
  49. *
  50. * @retval UA_ERR_INVALID_VALUE whenever ns0Id could not be mapped
  51. * @retval the corresponding index into UA_VTable
  52. */
  53. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 id);
  54. extern UA_VTable UA_[];
  55. static UA_Int32 phantom_delete(void * p) { return UA_SUCCESS; }
  56. extern UA_VTable UA_noDelete_[];
  57. /**
  58. * @brief the set of possible indices into UA_VTable
  59. *
  60. * Enumerated type to define the types that the open62541 stack can handle
  61. */
  62. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  63. print('''/**********************************************************
  64. * '''+sys.argv[2]+'''.cgen -- do not modify
  65. **********************************************************
  66. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  67. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  68. **********************************************************/
  69. #include "opcua.h"
  70. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 ns0Id) {
  71. UA_Int32 retval = UA_ERR_INVALID_VALUE;
  72. switch (ns0Id) { ''', end='\n',file=fc)
  73. i = 0
  74. for row in rows:
  75. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  76. continue
  77. if row[0] == "BaseDataType":
  78. name = "UA_Variant"
  79. elif row[0] == "Structure":
  80. name = "UA_ExtensionObject"
  81. else:
  82. name = "UA_" + row[0]
  83. print("\t"+name.upper()+" = "+str(i)+",", file=fh)
  84. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  85. i = i+1
  86. print("};\n", file=fh)
  87. print('''
  88. }
  89. return retval;
  90. }
  91. UA_VTable UA_[] = {''', file=fc)
  92. for row in rows:
  93. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  94. continue
  95. if row[0] == "BaseDataType":
  96. name = "UA_Variant"
  97. elif row[0] == "Structure":
  98. name = "UA_ExtensionObject"
  99. else:
  100. name = "UA_" + row[0]
  101. print('#define '+name.upper()+'_NS0 '+row[1], file=fh)
  102. print("\t{" +
  103. '(UA_Byte*)"'+ name +'",' +
  104. row[1] +
  105. ",(UA_Int32(*)(void const*))"+ name +"_calcSize" +
  106. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))"+ name + "_decodeBinary" +
  107. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))"+ name +"_encodeBinary"+
  108. ",(UA_Int32(*)(XML_Stack*,XML_Attr*,void*,_Bool))"+ name + "_decodeXML" +
  109. ",(UA_Int32(*)(void *))" + name + "_init" +
  110. ",(UA_Int32(*)(void **))" + name + "_new" +
  111. ",(UA_Int32(*)(void const * ,void*))" + name + "_copy" +
  112. ",(UA_Int32(*)(void *))" + name + "_delete" +
  113. (",sizeof(" + name + ")" if (name != "UA_InvalidType") else ",0") +
  114. "},",end='\n',file=fc)
  115. print("};\n\nUA_VTable UA_noDelete_[] = {", end='\n', file=fc)
  116. for row in rows:
  117. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  118. continue
  119. if row[0] == "BaseDataType":
  120. name = "UA_Variant"
  121. elif row[0] == "Structure":
  122. name = "UA_ExtensionObject"
  123. else:
  124. name = "UA_" + row[0]
  125. print("\t{" +
  126. '(UA_Byte*)"' + name + '",' +
  127. row[1] +
  128. ",(UA_Int32(*)(void const*))" + name + "_calcSize" +
  129. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))" + name + "_decodeBinary" +
  130. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))" + name + "_encodeBinary"+
  131. ",(UA_Int32(*)(XML_Stack*,XML_Attr*,void*,_Bool))" + name + "_decodeXML" +
  132. ",(UA_Int32(*)(void *))" + name + "_init" +
  133. ",(UA_Int32(*)(void **))" + name + "_new" +
  134. ",(UA_Int32(*)(void const * ,void*))" + name + "_copy" +
  135. ",(UA_Int32(*)(void *))phantom_delete" +
  136. (",sizeof(" + name + ")" if (name != "UA_InvalidType") else ",0") +
  137. "},",end='\n',file=fc)
  138. print("};", end='\n', file=fc)
  139. print('\n#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  140. fh.close()
  141. fc.close()