generate_statuscode_descriptions.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # This Source Code Form is subject to the terms of the Mozilla Public
  3. # License, v. 2.0. If a copy of the MPL was not distributed with this
  4. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  5. from __future__ import print_function
  6. import sys
  7. import platform
  8. import getpass
  9. import time
  10. import argparse
  11. from io import open
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument('statuscodes', help='path/to/Opc.Ua.StatusCodes.csv')
  14. parser.add_argument('outfile', help='outfile w/o extension')
  15. args = parser.parse_args()
  16. rows = []
  17. with open(args.statuscodes, mode="rt") as f:
  18. lines = f.readlines()
  19. for l in lines:
  20. rows.append(tuple(l.strip().split(',')))
  21. fh = open(args.outfile + ".h", "wt", encoding='utf8')
  22. fc = open(args.outfile + ".c", "wt", encoding='utf8')
  23. def printh(string):
  24. print(string, end=u'\n', file=fh)
  25. def printc(string):
  26. print(string, end=u'\n', file=fc)
  27. #########################
  28. # Print the header file #
  29. #########################
  30. printh(u'''/*---------------------------------------------------------
  31. * Autogenerated -- do not modify
  32. * Generated from %s with script %s
  33. *-------------------------------------------------------*/
  34. /**
  35. * .. _statuscodes:
  36. *
  37. * StatusCodes
  38. * -----------
  39. * StatusCodes are extensively used in the OPC UA protocol and in the open62541
  40. * API. They are represented by the :ref:`statuscode` data type. The following
  41. * definitions are autogenerated from the ``Opc.Ua.StatusCodes.csv`` file provided
  42. * with the OPC UA standard. */
  43. /* These StatusCodes are manually generated. */
  44. #define UA_STATUSCODE_GOOD 0x00
  45. #define UA_STATUSCODE_INFOTYPE_DATAVALUE 0x00000400
  46. #define UA_STATUSCODE_INFOBITS_OVERFLOW 0x00000080
  47. ''' % (args.statuscodes, sys.argv[0]))
  48. for row in rows:
  49. printh(u"#define UA_STATUSCODE_%s %s /* %s */" % (row[0].upper(), row[1], row[2]))
  50. #########################
  51. # Print the source file #
  52. #########################
  53. printc(u'''/**********************************************************
  54. * Autogenerated -- do not modify
  55. * Generated from %s with script %s
  56. *********************************************************/
  57. #include "ua_types.h"''' % (args.statuscodes, sys.argv[0]))
  58. count = 2 + len(rows)
  59. printc(u'''
  60. /* Definition for the deprecated StatusCode description API */
  61. const UA_StatusCodeDescription statusCodeExplanation_default = {0xffffffff, "", ""};
  62. typedef struct {
  63. UA_StatusCode code;
  64. const char *name;
  65. } UA_StatusCodeName;
  66. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  67. static const char * emptyStatusCodeName = "";
  68. const char * UA_StatusCode_name(UA_StatusCode code) {
  69. return emptyStatusCodeName;
  70. }
  71. #else
  72. static const size_t statusCodeDescriptionsSize = %s;
  73. static const UA_StatusCodeName statusCodeDescriptions[%i] = {
  74. {UA_STATUSCODE_GOOD, \"Good\"},''' % (count, count))
  75. for row in rows:
  76. printc(u" {UA_STATUSCODE_%s, \"%s\"}," % (row[0].upper(), row[0]))
  77. printc(u''' {0xffffffff, "Unknown StatusCode"}
  78. };
  79. const char * UA_StatusCode_name(UA_StatusCode code) {
  80. for (size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  81. if (statusCodeDescriptions[i].code == code)
  82. return statusCodeDescriptions[i].name;
  83. }
  84. return statusCodeDescriptions[statusCodeDescriptionsSize-1].name;
  85. }
  86. #endif''')
  87. fc.close()
  88. fh.close()