generate_namespace.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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(["Structure", "BaseDataType", "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. name = "UA_" + row[0]
  67. print("\t"+name.upper()+"="+str(i)+",", file=fh)
  68. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  69. i = i+1
  70. print('\tUA_NS0_VTABLE_MAX = 0\n};\n', file=fh)
  71. print('''\t}\n\treturn retval;
  72. }
  73. UA_VTable UA_[] = {''', file=fc)
  74. for row in rows2:
  75. if skipKind(row[2]):
  76. continue
  77. if skipType(row[0]):
  78. continue
  79. name = "UA_" + row[0]
  80. print('#define '+name.upper()+'_NS0 (UA_['+name.upper()+'].Id)', file=fh)
  81. print("\t{" + row[1] + ", (UA_Int32(*)(void const*)) " + name + "_calcSize, (UA_Int32(*)(char const*,UA_Int32*,void*)) " + name + "_decode, (UA_Int32(*)(void const*,UA_Int32*,char*))" + name + "_encode},",end='\n',file=fc)
  82. print("\t{0,UA_NULL,UA_NULL,UA_NULL}\n};",file=fc)
  83. print('#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  84. fh.close()
  85. fc.close()
  86. f.close()