generate_statuscode_descriptions.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. typedef struct {
  61. UA_StatusCode code;
  62. const char *name;
  63. } UA_StatusCodeName;
  64. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  65. static const char * emptyStatusCodeName = "";
  66. const char * UA_StatusCode_name(UA_StatusCode code) {
  67. return emptyStatusCodeName;
  68. }
  69. #else
  70. static const size_t statusCodeDescriptionsSize = %s;
  71. static const UA_StatusCodeName statusCodeDescriptions[%i] = {
  72. {UA_STATUSCODE_GOOD, \"Good\"},''' % (count, count))
  73. for row in rows:
  74. printc(u" {UA_STATUSCODE_%s, \"%s\"}," % (row[0].upper(), row[0]))
  75. printc(u''' {0xffffffff, "Unknown StatusCode"}
  76. };
  77. const char * UA_StatusCode_name(UA_StatusCode code) {
  78. for (size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  79. if (statusCodeDescriptions[i].code == code)
  80. return statusCodeDescriptions[i].name;
  81. }
  82. return statusCodeDescriptions[statusCodeDescriptionsSize-1].name;
  83. }
  84. #endif''')
  85. fc.close()
  86. fh.close()