generate_statuscode_descriptions.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 argparse
  8. from io import open
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('statuscodes', help='path/to/Opc.Ua.StatusCodes.csv')
  11. parser.add_argument('outfile', help='outfile w/o extension')
  12. args = parser.parse_args()
  13. rows = []
  14. with open(args.statuscodes, mode="rt") as f:
  15. lines = f.readlines()
  16. for l in lines:
  17. rows.append(tuple(l.strip().split(',')))
  18. fh = open(args.outfile + ".h", "wt", encoding='utf8')
  19. fc = open(args.outfile + ".c", "wt", encoding='utf8')
  20. def printh(string):
  21. print(string, end=u'\n', file=fh)
  22. def printc(string):
  23. print(string, end=u'\n', file=fc)
  24. #########################
  25. # Print the header file #
  26. #########################
  27. printh(u'''/*---------------------------------------------------------
  28. * Autogenerated -- do not modify
  29. * Generated from %s with script %s
  30. *-------------------------------------------------------*/
  31. /**
  32. * .. _statuscodes:
  33. *
  34. * StatusCodes
  35. * -----------
  36. * StatusCodes are extensively used in the OPC UA protocol and in the open62541
  37. * API. They are represented by the :ref:`statuscode` data type. The following
  38. * definitions are autogenerated from the ``Opc.Ua.StatusCodes.csv`` file provided
  39. * with the OPC UA standard. */
  40. /* These StatusCodes are manually generated. */
  41. #define UA_STATUSCODE_GOOD 0x00
  42. #define UA_STATUSCODE_INFOTYPE_DATAVALUE 0x00000400
  43. #define UA_STATUSCODE_INFOBITS_OVERFLOW 0x00000080
  44. ''' % (args.statuscodes, sys.argv[0]))
  45. for row in rows:
  46. printh(u"#define UA_STATUSCODE_%s %s /* %s */" % (row[0].upper(), row[1], row[2]))
  47. #########################
  48. # Print the source file #
  49. #########################
  50. printc(u'''/**********************************************************
  51. * Autogenerated -- do not modify
  52. * Generated from %s with script %s
  53. *********************************************************/
  54. #include <open62541/types.h>''' % (args.statuscodes, sys.argv[0]))
  55. count = 2 + len(rows)
  56. printc(u'''
  57. typedef struct {
  58. UA_StatusCode code;
  59. const char *name;
  60. } UA_StatusCodeName;
  61. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  62. static const char * emptyStatusCodeName = "";
  63. const char * UA_StatusCode_name(UA_StatusCode code) {
  64. return emptyStatusCodeName;
  65. }
  66. #else
  67. static const size_t statusCodeDescriptionsSize = %s;
  68. static const UA_StatusCodeName statusCodeDescriptions[%i] = {
  69. {UA_STATUSCODE_GOOD, \"Good\"},''' % (count, count))
  70. for row in rows:
  71. printc(u" {UA_STATUSCODE_%s, \"%s\"}," % (row[0].upper(), row[0]))
  72. printc(u''' {0xffffffff, "Unknown StatusCode"}
  73. };
  74. const char * UA_StatusCode_name(UA_StatusCode code) {
  75. for (size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  76. if (statusCodeDescriptions[i].code == code)
  77. return statusCodeDescriptions[i].name;
  78. }
  79. return statusCodeDescriptions[statusCodeDescriptionsSize-1].name;
  80. }
  81. #endif''')
  82. fc.close()
  83. fh.close()