generate_statuscode_descriptions.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = list(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 + len(rows)
  32. printc('''
  33. /* Definition for the deprecated StatusCode description API */
  34. const UA_StatusCodeDescription statusCodeExplanation_default = {0xffffffff, "", ""};
  35. typedef struct {
  36. UA_StatusCode code;
  37. const char *name;
  38. } UA_StatusCodeName;
  39. #ifndef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  40. static const char * emptyStatusCodeName = "";
  41. const char * UA_StatusCode_name(UA_StatusCode code) {
  42. return emptyStatusCodeName;
  43. }
  44. #else
  45. static const size_t statusCodeDescriptionsSize = %s;
  46. static const UA_StatusCodeName statusCodeDescriptions[%i] = {''' % (count, count))
  47. printc(" {UA_STATUSCODE_GOOD, \"Good\"},")
  48. for row in rows:
  49. printc(" {UA_STATUSCODE_%s, \"%s\",}," % (row[0].upper(), row[0]))
  50. printc(''' {0xffffffff, "Unknown StatusCode"}
  51. };
  52. const char * UA_StatusCode_name(UA_StatusCode code) {
  53. for(size_t i = 0; i < statusCodeDescriptionsSize; ++i) {
  54. if(statusCodeDescriptions[i].code == code)
  55. return statusCodeDescriptions[i].name;
  56. }
  57. return statusCodeDescriptions[statusCodeDescriptionsSize-1].name;
  58. }
  59. #endif''')
  60. fc.close()