generate_namespace.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /**
  45. * @brief maps namespace zero nodeId to index into UA_VTable
  46. *
  47. * @param[in] ns0Id The namespace zero nodeId
  48. *
  49. * @retval UA_ERR_INVALID_VALUE whenever ns0Id could not be mapped
  50. * @retval the corresponding index into UA_VTable
  51. */
  52. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 id);
  53. extern UA_VTable UA_[];
  54. static UA_Int32 phantom_delete(void * p) { return UA_SUCCESS; }
  55. extern UA_VTable UA_noDelete_[];
  56. /**
  57. * @brief the set of possible indices into UA_VTable
  58. *
  59. * Enumerated type to define the types that the open62541 stack can handle
  60. */
  61. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  62. print('''/**********************************************************
  63. * '''+sys.argv[2]+'''.cgen -- do not modify
  64. **********************************************************
  65. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  66. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  67. **********************************************************/
  68. #include "opcua.h"
  69. UA_Int32 UA_ns0ToVTableIndex(UA_Int32 ns0Id) {
  70. UA_Int32 retval = UA_ERR_INVALID_VALUE;
  71. switch (ns0Id) { ''', end='\n',file=fc)
  72. i = 0
  73. for row in rows:
  74. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  75. continue
  76. if row[0] == "BaseDataType":
  77. name = "UA_Variant"
  78. elif row[0] == "Structure":
  79. name = "UA_ExtensionObject"
  80. else:
  81. name = "UA_" + row[0]
  82. print("\t"+name.upper()+" = "+str(i)+",", file=fh)
  83. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  84. i = i+1
  85. print("};\n", file=fh)
  86. print('''
  87. }
  88. return retval;
  89. }
  90. UA_VTable UA_[] = {''', file=fc)
  91. for row in rows:
  92. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  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(*)(XML_Stack*,XML_Attr*,void*,_Bool))"+ name + "_decodeXML" +
  106. ",(UA_Int32(*)(void *))"+name+"_init"+
  107. ",(UA_Int32(*)(void **))"+name+"_new"+
  108. ",(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  109. ",(UA_Int32(*)(void *))"+name+"_delete"+
  110. (",sizeof("+name+")" if (name != "UA_InvalidType") else ",0") +
  111. ',(UA_Byte*)"'+name+'"},',end='\n',file=fc)
  112. print("};\n\nUA_VTable UA_noDelete_[] = {", end='\n', file=fc)
  113. for row in rows:
  114. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  115. continue
  116. if row[0] == "BaseDataType":
  117. name = "UA_Variant"
  118. elif row[0] == "Structure":
  119. name = "UA_ExtensionObject"
  120. else:
  121. name = "UA_" + row[0]
  122. print("\t{" + row[1] +
  123. ",(UA_Int32(*)(void const*))"+name+"_calcSize" +
  124. ",(UA_Int32(*)(UA_ByteString const*,UA_Int32*,void*))"+name+ "_decodeBinary" +
  125. ",(UA_Int32(*)(void const*,UA_Int32*,UA_ByteString*))"+name+"_encodeBinary"+
  126. ",(UA_Int32(*)(XML_Stack*,XML_Attr*,void*,_Bool))" + name + "_decodeXML" +
  127. ",(UA_Int32(*)(void *))"+name+"_init"+
  128. ",(UA_Int32(*)(void **))"+name+"_new"+
  129. ",(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  130. ",(UA_Int32(*)(void *))phantom_delete"+
  131. (",sizeof("+name+")" if (name != "UA_InvalidType") else ",0") +
  132. ',(UA_Byte*)"'+name+'"},',end='\n',file=fc)
  133. print("};", end='\n', file=fc)
  134. print('\n#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  135. fh.close()
  136. fc.close()