generate_statuscode_descriptions.py 2.3 KB

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