generate_namespace.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. f = open(sys.argv[1])
  29. input_str = f.read() + "\nInvalidType,0,DataType"
  30. input_str = input_str.replace('\r','')
  31. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  32. f.close()
  33. fh = open(sys.argv[2] + ".hgen",'w')
  34. fc = open(sys.argv[2] + ".cgen",'w')
  35. print('''/**********************************************************
  36. * '''+sys.argv[2]+'''.hgen -- do not modify
  37. **********************************************************
  38. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  39. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  40. **********************************************************/
  41. #ifndef OPCUA_NAMESPACE_0_H_
  42. #define OPCUA_NAMESPACE_0_H_
  43. #include "ua_basictypes.h" // definition of UA_VTable and basic UA_Types
  44. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 id);
  45. extern UA_VTable UA_[];
  46. static UA_Int32 phantom_delete(void * p) { return UA_SUCCESS; }
  47. extern UA_VTable UA_noDelete_[];
  48. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  49. print('''/**********************************************************
  50. * '''+sys.argv[2]+'''.cgen -- do not modify
  51. **********************************************************
  52. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  53. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  54. **********************************************************/
  55. #include "opcua.h"
  56. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 id) {
  57. UA_Int32 retval = UA_ERR_INVALID_VALUE;
  58. switch (id) { ''', end='\n',file=fc)
  59. i = 0
  60. for row in rows:
  61. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  62. continue
  63. if row[0] == "BaseDataType":
  64. name = "UA_Variant"
  65. elif row[0] == "Structure":
  66. name = "UA_ExtensionObject"
  67. else:
  68. name = "UA_" + row[0]
  69. print("\t"+name.upper()+" = "+str(i)+",", file=fh)
  70. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  71. i = i+1
  72. print("};\n", file=fh)
  73. print('''
  74. }
  75. return retval;
  76. }
  77. UA_VTable UA_[] = {''', file=fc)
  78. for row in rows:
  79. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  80. continue
  81. if row[0] == "BaseDataType":
  82. name = "UA_Variant"
  83. elif row[0] == "Structure":
  84. name = "UA_ExtensionObject"
  85. else:
  86. name = "UA_" + row[0]
  87. print('#define '+name.upper()+'_NS0 '+row[1], file=fh)
  88. print("\t{" + row[1] +
  89. ",(UA_Int32(*)(void const*))"+name+"_calcSize" +
  90. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))"+name+ "_decodeBinary" +
  91. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))"+name+"_encodeBinary"+
  92. ",(UA_Int32(*)(void *))"+name+"_init"+
  93. ",(UA_Int32(*)(void **))"+name+"_new"+
  94. ",(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  95. ",(UA_Int32(*)(void *))"+name+"_delete"+
  96. (",sizeof("+name+")" if (name != "UA_InvalidType") else ",0") +
  97. ',(UA_Byte*)"'+name+'"},',end='\n',file=fc)
  98. print("};\n\nUA_VTable UA_noDelete_[] = {", end='\n', file=fc)
  99. for row in rows:
  100. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  101. continue
  102. if row[0] == "BaseDataType":
  103. name = "UA_Variant"
  104. elif row[0] == "Structure":
  105. name = "UA_ExtensionObject"
  106. else:
  107. name = "UA_" + row[0]
  108. print("\t{" + row[1] +
  109. ",(UA_Int32(*)(void const*))"+name+"_calcSize" +
  110. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))"+name+ "_decodeBinary" +
  111. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))"+name+"_encodeBinary"+
  112. ",(UA_Int32(*)(void *))"+name+"_init"+
  113. ",(UA_Int32(*)(void **))"+name+"_new"+
  114. ",(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  115. ",(UA_Int32(*)(void *))phantom_delete"+
  116. (",sizeof("+name+")" if (name != "UA_InvalidType") else ",0") +
  117. ',(UA_Byte*)"'+name+'"},',end='\n',file=fc)
  118. print("};", end='\n', file=fc)
  119. print('\n#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  120. fh.close()
  121. fc.close()