generate_namespace.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import print_function
  2. import sys
  3. from collections import OrderedDict
  4. import re
  5. import csv
  6. from itertools import tee
  7. if len(sys.argv) != 3:
  8. print("Usage: python generate_namespace.py <path/to/NodeIds.csv> <outfile w/o extension>", file=sys.stdout)
  9. exit(0)
  10. # types that are to be excluded
  11. exclude_types = set(["Object","ObjectType","Variable","Method"])
  12. def skipType(name):
  13. if name in exclude_types:
  14. return True
  15. return False
  16. f = open(sys.argv[1])
  17. rows1, rows2, rows3 = tee(csv.reader(f), 3)
  18. fh = open(sys.argv[2] + ".h",'w');
  19. fc = open(sys.argv[2] + ".c",'w');
  20. print('''/* struktur vTable und enumerierung*/
  21. #ifndef OPCUA_NAMESPACE_0_H_
  22. #define OPCUA_NAMESPACE_0_H_
  23. #include "opcua.h"
  24. typedef struct _UA_VTable {
  25. UA_UInt32 Id;
  26. Int32 (*calcSize)(void* ptr);
  27. Int32 (*decode)(char const * src, Int32* pos, void* dst);
  28. Int32 (*encode)(void const * src, Int32* pos, char* dst);
  29. } UA_VTable;
  30. Int32 UA_namespace_zero_to_index(Int32 id);
  31. extern UA_VTable UA_namespace_zero[];
  32. enum UA_VTableIndex_enum {''', end='\n', file=fh)
  33. print('''/* Mapping and vTable of Namespace Zero */
  34. #include "opcua.h"
  35. Int32 UA_namespace_zero_to_index(Int32 id) {
  36. Int32 retval = -1;
  37. switch (id) { ''', end='\n',file=fc)
  38. i = 0
  39. for row in rows1:
  40. if skipType(row[2]):
  41. continue
  42. name = "UA_" + row[0]
  43. print("\t"+name.upper()+"="+str(i)+",", file=fh)
  44. print('\tcase '+row[1]+': retval='+name.upper()+'; break; //'+row[2], file=fc)
  45. i = i+1
  46. print('\tUA_NS0_VTABLE_MAX = 0\n};\n', file=fh)
  47. print('''\t}\n\treturn retval;
  48. }
  49. UA_VTable UA_namespace_zero[] = {''', file=fc)
  50. for row in rows2:
  51. if skipType(row[2]):
  52. continue
  53. name = "UA_" + row[0]
  54. print('#define '+name.upper()+'_NS0 (UA_namespace_zero['+name.upper()+'].Id)', file=fh)
  55. print("\t{" + row[1] + ", &" + name + "_calcSize, &" + name + "_decode, &" + name + "_encode},",end='\n',file=fc)
  56. print("\t{0,NULL,NULL,NULL}\n};",file=fc)
  57. print('#endif /* OPCUA_NAMESPACE_0_H_ */', end='\n', file=fh)
  58. fh.close()
  59. fc.close()
  60. f.close()