generate_namespace.py 3.8 KB

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