helper_functions.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <open62541/server.h>
  2. #include <open62541/plugin/log_stdout.h>
  3. #include "helper_functions.h"
  4. #include <libconfig.h>
  5. // C++ includes
  6. #include <iostream>
  7. using namespace std;
  8. UA_StatusCode setVariable(UA_Server* server, void *value, const UA_DataType *dataType, UA_NodeId variableNodeid) {
  9. UA_StatusCode res;
  10. UA_Variant var;
  11. UA_Variant_setScalar(&var, value, dataType);
  12. res = UA_Server_writeValue(server, variableNodeid, var);
  13. if(res != UA_STATUSCODE_GOOD)
  14. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to init variable node NS=%d,i=%d. Error: %s\n", variableNodeid.namespaceIndex, variableNodeid.identifier.numeric, UA_StatusCode_name(res));
  15. return res;
  16. }
  17. UA_StatusCode setEURangeVariable(UA_Server* server, UA_Double low, UA_Double high, UA_NodeId euRangeVariableNodeid) {
  18. UA_StatusCode res;
  19. UA_Range range;
  20. UA_Range_init(&range);
  21. range.low = low;
  22. range.high = high;
  23. res = setVariable(server, &range, &UA_TYPES[UA_TYPES_RANGE], euRangeVariableNodeid);
  24. if(res != UA_STATUSCODE_GOOD)
  25. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to set range for node NS=%d,i=%d. Error: %s\n", euRangeVariableNodeid.namespaceIndex, euRangeVariableNodeid.identifier.numeric, UA_StatusCode_name(res));
  26. return res ;
  27. }
  28. // UA State-machine handling
  29. bool inState(UA_Server* server, UA_NodeId currentStateIdNodeId, UA_NodeId stateNodeId) {
  30. UA_Variant out;
  31. UA_Variant_init(&out);
  32. UA_Server_readValue(server, currentStateIdNodeId, &out);
  33. UA_NodeId *currentStateId = (UA_NodeId*)(out.data);
  34. if(UA_NodeId_equal(currentStateId, &stateNodeId))
  35. return true;
  36. return false;
  37. }
  38. UA_StatusCode setCurrentState(UA_Server *server, UA_NodeId currentStateVariableNodeId, UA_NodeId currentStateVariableIdNodeId, UA_NodeId newStateNodeId) {
  39. // set the Id property of the CurrentState variable to newStateNodeId
  40. UA_Variant var;
  41. UA_Variant_init(&var);
  42. UA_Variant_setScalar(&var, &newStateNodeId, &UA_TYPES[UA_TYPES_NODEID]);
  43. UA_Server_writeValue(server, currentStateVariableIdNodeId, var);
  44. // set the CurrentState variable to a human-readable string
  45. UA_LocalizedText stateName;
  46. UA_LocalizedText_init(&stateName);
  47. UA_Server_readDisplayName(server, newStateNodeId, &stateName);
  48. UA_Variant_setScalar(&var, &stateName, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  49. UA_Server_writeValue(server, currentStateVariableNodeId, var);
  50. }
  51. // Configuration file handling
  52. bool getIntFromConfig(const config_t* cfg, const char* name, int *res) {
  53. if (!config_lookup_int(cfg, name, res)) {
  54. cerr << "No " << name << " setting in configuration file." << endl;
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool getBoolFromConfig(const config_t* cfg, const char* name, bool *res) {
  60. int resInt;
  61. *res = false;
  62. if (!config_lookup_bool(cfg, name, &resInt)) {
  63. cerr << "No " << name << " setting in configuration file." << endl;
  64. return false;
  65. }
  66. if (resInt != 0)
  67. *res = true;
  68. return true;
  69. }
  70. bool getStringFromConfig(const config_t* cfg, const char* name, char **res) {
  71. if (!config_lookup_string(cfg, name, (const char **) res)) {
  72. cerr << "No " << name << " setting in configuration file." << endl;
  73. return false;
  74. }
  75. return true;
  76. }
  77. bool getStringListFromConfig(const config_t* cfg, const char* name, char **res, int *listLength) {
  78. config_setting_t *setting = config_lookup(cfg, name);
  79. *listLength = config_setting_length(setting);
  80. if (*listLength == 0) {
  81. cerr << "No " << name << " setting in configuration file or wrong format. Note that a list of strings should look like this: (\"item1\", \"item2\")." << endl;
  82. return false;
  83. }
  84. for(int i = 0; i < *listLength; i++){
  85. const char *elem = config_setting_get_string_elem (setting, i);
  86. if (elem == NULL) {
  87. cerr << "No " << name << " setting in configuration file or wrong format. Note that a list of strings should look like this: (\"item1\", \"item2\")." << endl;
  88. return false;
  89. }
  90. res[i] = strdup(elem);
  91. }
  92. return true;
  93. }
  94. bool getDoubleFromConfig(const config_t* cfg, const char* name, double *res) {
  95. if (!config_lookup_float(cfg, name, res)) {
  96. cerr << "No " << name << " setting in configuration file." << endl;
  97. return false;
  98. }
  99. return true;
  100. }