generate_namespace.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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] + ".h",'w')
  34. fc = open(sys.argv[2] + ".c",'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 ''' + sys.argv[2].upper() + '''_H_
  42. #define ''' + sys.argv[2].upper() + '''_H_
  43. #include "ua_util.h"
  44. #include "ua_types.h" // definition of UA_VTable and basic UA_Types
  45. #include "ua_types_generated.h"
  46. /**
  47. * @brief maps namespace zero nodeId to index into UA_VTable
  48. *
  49. * @param[in] ns0Id The namespace zero nodeId
  50. *
  51. * @retval UA_ERR_INVALID_VALUE whenever ns0Id could not be mapped
  52. * @retval the corresponding index into UA_VTable
  53. */
  54. UA_Int32 UA_ns0ToVTableIndex(const UA_NodeId *id);
  55. extern UA_VTable UA_;
  56. static UA_Int32 phantom_delete(void * p) { return UA_SUCCESS; }
  57. extern UA_VTable UA_borrowed_;
  58. /**
  59. * @brief the set of possible indices into UA_VTable
  60. *
  61. * Enumerated type to define the types that the open62541 stack can handle
  62. */
  63. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  64. print('''/**********************************************************
  65. * '''+sys.argv[2]+'''.cgen -- do not modify
  66. **********************************************************
  67. * Generated from '''+sys.argv[1]+''' with script '''+sys.argv[0]+'''
  68. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+ time.strftime("%Y-%m-%d %I:%M:%S")+'''
  69. **********************************************************/
  70. #include "''' + sys.argv[2] + '''.h"
  71. UA_Int32 UA_ns0ToVTableIndex(const UA_NodeId *id) {
  72. UA_Int32 retval = UA_ERR_INVALID_VALUE;
  73. if(id->namespace != 0) return retval;
  74. switch (id->identifier.numeric) { ''', end='\n',file=fc)
  75. i = 0
  76. for row in rows:
  77. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  78. continue
  79. if row[0] == "BaseDataType":
  80. name = "UA_Variant"
  81. elif row[0] == "Structure":
  82. name = "UA_ExtensionObject"
  83. else:
  84. name = "UA_" + row[0]
  85. print("\t"+name.upper()+" = "+str(i)+",", file=fh)
  86. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  87. i = i+1
  88. print("};\n", file=fh)
  89. print('''
  90. }
  91. return retval;
  92. }
  93. UA_VTable UA_ = {
  94. \t.getTypeIndex = UA_ns0ToVTableIndex,
  95. \t.types = (UA_VTable_Entry[]){''', file=fc)
  96. for row in rows:
  97. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  98. continue
  99. if row[0] == "BaseDataType":
  100. name = "UA_Variant"
  101. elif row[0] == "Structure":
  102. name = "UA_ExtensionObject"
  103. else:
  104. name = "UA_" + row[0]
  105. print('#define '+name.upper()+'_NS0 '+row[1], file=fh)
  106. print("\t{.typeId={UA_NODEIDTYPE_FOURBYTE,0,.identifier.numeric=" + row[1] +"}"+
  107. ",.name=(UA_Byte*)&\""+name+"\""+
  108. ",.new=(UA_Int32(*)(void **))"+name+"_new"+
  109. ",.init=(UA_Int32(*)(void *))"+name+"_init"+
  110. ",.copy=(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  111. ",.delete=(UA_Int32(*)(void *))"+name+"_delete"+
  112. ",.deleteMembers=(UA_Int32(*)(void *))"+name+"_deleteMembers"+
  113. ",.memSize=" + ("sizeof("+name+")" if (name != "UA_InvalidType") else "0") +
  114. ",.encodings={(UA_Encoding){.calcSize=(UA_calcSize)"+ name +"_calcSizeBinary" +
  115. ",.encode=(UA_encode)"+name+ "_encodeBinary" +
  116. ",.decode=(UA_decode)"+name+"_decodeBinary}"+
  117. ",(UA_Encoding){.calcSize=(UA_calcSize)"+ name +"_calcSizeXml" +
  118. ",.encode=(UA_encode)"+name+ "_encodeXml" +
  119. ",.decode=(UA_decode)"+name+"_decodeXml}"+
  120. "}},",
  121. end='\n',file=fc)
  122. print('''}};
  123. UA_VTable UA_noDelete_ = {
  124. \t.getTypeIndex=UA_ns0ToVTableIndex,
  125. \t.types = (UA_VTable_Entry[]){''', end='\n', file=fc)
  126. for row in rows:
  127. if row[0] == "" or row[0] in exclude_types or row[2] in exclude_kinds:
  128. continue
  129. if row[0] == "BaseDataType":
  130. name = "UA_Variant"
  131. elif row[0] == "Structure":
  132. name = "UA_ExtensionObject"
  133. else:
  134. name = "UA_" + row[0]
  135. print("\t{.typeId={UA_NODEIDTYPE_FOURBYTE,0,.identifier.numeric=" + row[1] +"}"+
  136. ",.name=(UA_Byte*)&\""+name+"\""+
  137. ",.new=(UA_Int32(*)(void **))"+name+"_new"+
  138. ",.init=(UA_Int32(*)(void *))"+name+"_init"+
  139. ",.copy=(UA_Int32(*)(void const * ,void*))"+name+"_copy"+
  140. ",.delete=(UA_Int32(*)(void *))phantom_delete"+
  141. ",.deleteMembers=(UA_Int32(*)(void *))phantom_delete"+
  142. ",.memSize=" + ("sizeof("+name+")" if (name != "UA_InvalidType") else "0") +
  143. ",.encodings={(UA_Encoding){.calcSize=(UA_calcSize)"+ name +"_calcSizeBinary" +
  144. ",.encode=(UA_encode)"+name+ "_encodeBinary" +
  145. ",.decode=(UA_decode)"+name+"_decodeBinary}"+
  146. ",(UA_Encoding){.calcSize=(UA_calcSize)"+ name +"_calcSizeXml" +
  147. ",.encode=(UA_encode)"+name+ "_encodeXml" +
  148. ",.decode=(UA_decode)"+name+"_decodeXml}"+
  149. "}},",
  150. end='\n',file=fc)
  151. print("}};", end='\n', file=fc)
  152. print('\n#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  153. fh.close()
  154. fc.close()