generate_namespace.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from __future__ import print_function
  2. import sys
  3. import platform
  4. import getpass
  5. from collections import OrderedDict
  6. import time
  7. import re
  8. import csv
  9. from itertools import tee
  10. if len(sys.argv) != 3:
  11. print("Usage: python generate_namespace.py <path/to/NodeIds.csv> <outfile w/o extension>", file=sys.stdout)
  12. exit(0)
  13. # types that are to be excluded
  14. exclude_kind = set(["Object","ObjectType","Variable","Method","ReferenceType"])
  15. exclude_types = set(["Number",
  16. "Integer", "UInteger", "Enumeration",
  17. "Image", "ImageBMP", "ImageGIF", "ImageJPG", "ImagePNG",
  18. "References", "BaseVariableType", "BaseDataVariableType",
  19. "PropertyType", "DataTypeDescriptionType", "DataTypeDictionaryType", "NamingRuleType",
  20. "IntegerId","Counter","Duration","NumericRange","Time","Date",
  21. "UtcTime", "LocaleId","UserTokenType",
  22. "ApplicationType","ApplicationInstanceCertificate",
  23. "ServerVendorCapabilityType","ServerStatusType","ServerDiagnosticsSummaryType",
  24. "SamplingIntervalDiagnosticsArrayType", "SamplingIntervalDiagnosticsType",
  25. "SubscriptionDiagnosticsArrayType", "SubscriptionDiagnosticsType",
  26. "SessionDiagnosticsArrayType", "SessionDiagnosticsVariableType",
  27. "SessionSecurityDiagnosticsArrayType", "SessionSecurityDiagnosticsType",
  28. "DataItemType", "AnalogItemType", "DiscreteItemType", "TwoStateDiscreteType",
  29. "MultiStateDiscreteType", "ProgramDiagnosticType", "StateVariableType", "FiniteStateVariableType",
  30. "TransitionVariableType", "FiniteTransitionVariableType", "BuildInfoType", "TwoStateVariableType",
  31. "ConditionVariableType", "MultiStateValueDiscreteType", "OptionSetType", "ArrayItemType",
  32. "YArrayItemType", "XYArrayItemType", "ImageItemType", "CubeItemType", "NDimensionArrayItemType"
  33. ])
  34. def skipKind(name):
  35. if name in exclude_kind:
  36. return True
  37. return False
  38. def skipType(name):
  39. if name in exclude_types:
  40. return True
  41. return False
  42. f = open(sys.argv[1])
  43. rows1, rows2, rows3 = tee(csv.reader(f), 3)
  44. fh = open(sys.argv[2] + ".hgen",'w');
  45. fc = open(sys.argv[2] + ".cgen",'w');
  46. print('''/**********************************************************
  47. * '''+sys.argv[2]+'''.hgen -- do not modify
  48. **********************************************************
  49. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  50. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  51. **********************************************************/
  52. #ifndef OPCUA_NAMESPACE_0_H_
  53. #define OPCUA_NAMESPACE_0_H_
  54. #include "opcua.h" // definition of UA_VTable and basic UA_Types
  55. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 id);
  56. extern UA_VTable UA_[];
  57. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  58. print('''/**********************************************************
  59. * '''+sys.argv[2]+'''.cgen -- do not modify
  60. **********************************************************
  61. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  62. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  63. **********************************************************/
  64. #include "opcua.h"
  65. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 id) {
  66. UA_Int32 retval = UA_ERR_INVALID_VALUE;
  67. switch (id) { ''', end='\n',file=fc)
  68. i = 0
  69. for row in rows1:
  70. if skipKind(row[2]):
  71. continue
  72. if skipType(row[0]):
  73. continue
  74. if row[0] == "BaseDataType":
  75. name = "UA_Variant"
  76. elif row[0] == "Structure":
  77. name = "UA_ExtensionObject"
  78. else:
  79. name = "UA_" + row[0]
  80. print("\t"+name.upper()+"="+str(i)+",", file=fh)
  81. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  82. i = i+1
  83. print('\tUA_INVALIDTYPE = '+str(i)+'\n};\n', file=fh)
  84. print('''\tcase 0: retval=UA_INVALIDTYPE; break;
  85. }
  86. return retval;
  87. }
  88. UA_VTable UA_[] = {''', file=fc)
  89. for row in rows2:
  90. if skipKind(row[2]):
  91. continue
  92. if skipType(row[0]):
  93. continue
  94. if row[0] == "BaseDataType":
  95. name = "UA_Variant"
  96. elif row[0] == "Structure":
  97. name = "UA_ExtensionObject"
  98. else:
  99. name = "UA_" + row[0]
  100. print('#define '+name.upper()+'_NS0 '+row[1], file=fh)
  101. print("\t{" + row[1] +
  102. ",(UA_Int32(*)(void const*))"+name+"_calcSize" +
  103. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))"+name+ "_decodeBinary" +
  104. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))"+name+"_encodeBinary"+
  105. ",(UA_Int32(*)(void *))"+name+"_init"+
  106. ",(UA_Int32(*)(void **))"+name+"_new"+
  107. ",(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  108. ",(UA_Int32(*)(void *))"+name+"_delete"+
  109. ',(UA_Byte*)"'+name+'"},',end='\n',file=fc)
  110. name = "UA_InvalidType"
  111. print("\t{0" +
  112. ",(UA_Int32(*)(void const*))"+name+"_calcSize" +
  113. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))"+name+ "_decodeBinary" +
  114. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))"+name+"_encodeBinary"+
  115. ",(UA_Int32(*)(void *))"+name+"_init"+
  116. ",(UA_Int32(*)(void **))"+name+"_new"+
  117. ",(UA_Int32(*)(void const *, void *))"+name+"_copy"+
  118. ",(UA_Int32(*)(void *))"+name+"_delete"+
  119. ',(UA_Byte*)"'+name+'"}',end='\n',file=fc)
  120. print("};", end='\n', file=fc)
  121. print('#define '+name.upper()+'_NS0 0', file=fh)
  122. print('#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  123. fh.close()
  124. fc.close()
  125. f.close()
  126. 2222