tutorial_server_variabletype.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Working with Variable Types
  5. * ---------------------------
  6. *
  7. * Variable types have three functions:
  8. *
  9. * - Constrain the possible data type, value rank and array dimensions of the
  10. * variables of that type. This allows interface code to be written against
  11. * the generic type definition, so it is applicable for all instances.
  12. * - Provide a sensible default value
  13. * - Enable a semantic interpretation of the variable based on its type
  14. *
  15. * In the example of this tutorial, we represent a point in 2D space by an array
  16. * of double values. The following function adds the corresponding
  17. * VariableTypeNode to the hierarchy of variable types.
  18. */
  19. #include <open62541/plugin/log_stdout.h>
  20. #include <open62541/server.h>
  21. #include <open62541/server_config_default.h>
  22. #include <signal.h>
  23. #include <stdlib.h>
  24. static UA_NodeId pointTypeId;
  25. static void
  26. addVariableType2DPoint(UA_Server *server) {
  27. UA_VariableTypeAttributes vtAttr = UA_VariableTypeAttributes_default;
  28. vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  29. vtAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  30. UA_UInt32 arrayDims[1] = {2};
  31. vtAttr.arrayDimensions = arrayDims;
  32. vtAttr.arrayDimensionsSize = 1;
  33. vtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Type");
  34. /* a matching default value is required */
  35. UA_Double zero[2] = {0.0, 0.0};
  36. UA_Variant_setArray(&vtAttr.value, zero, 2, &UA_TYPES[UA_TYPES_DOUBLE]);
  37. UA_Server_addVariableTypeNode(server, UA_NODEID_NULL,
  38. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  39. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  40. UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
  41. vtAttr, NULL, &pointTypeId);
  42. }
  43. /**
  44. * Now the new variable type for *2DPoint* can be referenced during the creation
  45. * of a new variable. If no value is given, the default from the variable type
  46. * is copied during instantiation.
  47. */
  48. static UA_NodeId pointVariableId;
  49. static void
  50. addVariable(UA_Server *server) {
  51. /* Prepare the node attributes */
  52. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  53. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  54. vAttr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  55. UA_UInt32 arrayDims[1] = {2};
  56. vAttr.arrayDimensions = arrayDims;
  57. vAttr.arrayDimensionsSize = 1;
  58. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  59. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  60. /* vAttr.value is left empty, the server instantiates with the default value */
  61. /* Add the node */
  62. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  63. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  64. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  65. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  66. vAttr, NULL, &pointVariableId);
  67. }
  68. /**
  69. * The constraints of the variable type are enforced when creating new variable
  70. * instances of the type. In the following function, adding a variable of
  71. * *2DPoint* type with a string value fails because The value does not match the
  72. * variable type constraints. */
  73. static void
  74. addVariableFail(UA_Server *server) {
  75. /* Prepare the node attributes */
  76. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  77. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  78. vAttr.valueRank = UA_VALUERANK_SCALAR; /* a scalar. this is not allowed per the variable type */
  79. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable (fail)");
  80. UA_String s = UA_STRING("2dpoint?");
  81. UA_Variant_setScalar(&vAttr.value, &s, &UA_TYPES[UA_TYPES_STRING]);
  82. /* Add the node will return UA_STATUSCODE_BADTYPEMISMATCH*/
  83. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  84. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  85. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  86. UA_QUALIFIEDNAME(1, "2DPoint Type (fail)"), pointTypeId,
  87. vAttr, NULL, NULL);
  88. }
  89. /**
  90. * The constraints of the variable type are enforced when writing the datatype,
  91. * valuerank and arraydimensions attributes of the variable. This, in turn,
  92. * constrains the value attribute of the variable. */
  93. static void
  94. writeVariable(UA_Server *server) {
  95. UA_StatusCode retval = UA_Server_writeValueRank(server, pointVariableId, UA_VALUERANK_ONE_OR_MORE_DIMENSIONS);
  96. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  97. "Setting the Value Rank failed with Status Code %s",
  98. UA_StatusCode_name(retval));
  99. }
  100. /** It follows the main server code, making use of the above definitions. */
  101. static volatile UA_Boolean running = true;
  102. static void stopHandler(int sign) {
  103. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  104. running = false;
  105. }
  106. int main(void) {
  107. signal(SIGINT, stopHandler);
  108. signal(SIGTERM, stopHandler);
  109. UA_Server *server = UA_Server_new();
  110. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  111. addVariableType2DPoint(server);
  112. addVariable(server);
  113. addVariableFail(server);
  114. writeVariable(server);
  115. UA_StatusCode retval = UA_Server_run(server, &running);
  116. UA_Server_delete(server);
  117. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  118. }