generate_statuscode_descriptions.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. parser = argparse.ArgumentParser()
  12. parser.add_argument('statuscodes', help='path/to/Opc.Ua.StatusCodes.csv')
  13. parser.add_argument('outfile', help='outfile w/o extension')
  14. args = parser.parse_args()
  15. f = open(args.statuscodes)
  16. input_str = f.read()
  17. f.close()
  18. input_str = input_str.replace('\r','')
  19. rows = map(lambda x:tuple(x.split(',')), input_str.split('\n'))
  20. fc = open(args.outfile + ".c",'w')
  21. def printc(string):
  22. print(string, end='\n', file=fc)
  23. printc('''/**********************************************************
  24. * '''+args.outfile+'''.hgen -- do not modify
  25. **********************************************************
  26. * Generated from '''+args.statuscodes+''' with script '''+sys.argv[0]+'''
  27. * on host '''+platform.uname()[1]+''' by user '''+getpass.getuser()+''' at '''+
  28. time.strftime("%Y-%m-%d %I:%M:%S")+'''
  29. **********************************************************/\n
  30. #include "ua_types.h"''')
  31. count = 2
  32. for row in rows:
  33. count += 1
  34. printc('''
  35. /* Definition for the deprecated StatusCode description API */
  36. const UA_StatusCodeDescription statusCodeExplanation_default = {0xffffffff, "", ""};
  37. typedef struct {
  38. UA_StatusCode code;
  39. const char *name;
  40. } UA_StatusCodeName;
  41. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  42. static const char * emptyStatusCodeName = "";
  43. const char * UA_StatusCode_name(UA_StatusCode code) {
  44. return emptyStatusCodeName;
  45. }
  46. #else
  47. static const size_t statusCodeDescriptionsSize = %s;
  48. static const UA_StatusCodeName statusCodeDescriptions[%i] = {''' % (count, count))
  49. printc(" {UA_STATUSCODE_GOOD, \"Good\"},")
  50. for row in rows:
  51. printc(" {UA_STATUSCODE_%s, \"%s\",}," % (row[0].upper(), row[0]))
  52. printc(''' {0xffffffff, "Unknown StatusCode"}
  53. };
  54. const char * UA_StatusCode_name(UA_StatusCode code) {
  55. for(size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  56. if(statusCodeDescriptions[i].code == code)
  57. return statusCodeDescriptions[i].name;
  58. }
  59. return statusCodeDescriptions[statusCodeDescriptionsSize-1].name;
  60. }
  61. #endif''')
  62. fc.close()