tutorial_server_variabletype.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <signal.h>
  20. #include "open62541.h"
  21. static UA_NodeId pointTypeId;
  22. static void
  23. addVariableType2DPoint(UA_Server *server) {
  24. UA_VariableTypeAttributes vtAttr = UA_VariableTypeAttributes_default;
  25. vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  26. vtAttr.valueRank = 1; /* array with one dimension */
  27. UA_UInt32 arrayDims[1] = {2};
  28. vtAttr.arrayDimensions = arrayDims;
  29. vtAttr.arrayDimensionsSize = 1;
  30. vtAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Type");
  31. /* a matching default value is required */
  32. UA_Double zero[2] = {0.0, 0.0};
  33. UA_Variant_setArray(&vtAttr.value, zero, 2, &UA_TYPES[UA_TYPES_DOUBLE]);
  34. UA_Server_addVariableTypeNode(server, UA_NODEID_NULL,
  35. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  36. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  37. UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
  38. vtAttr, NULL, &pointTypeId);
  39. }
  40. /**
  41. * Now the new variable type for *2DPoint* can be referenced during the creation
  42. * of a new variable. If no value is given, the default from the variable type
  43. * is copied during instantiation.
  44. */
  45. static UA_NodeId pointVariableId;
  46. static void
  47. addVariable(UA_Server *server) {
  48. /* Prepare the node attributes */
  49. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  50. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  51. vAttr.valueRank = 1; /* array with one dimension */
  52. UA_UInt32 arrayDims[1] = {2};
  53. vAttr.arrayDimensions = arrayDims;
  54. vAttr.arrayDimensionsSize = 1;
  55. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable");
  56. vAttr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  57. /* vAttr.value is left empty, the server instantiates with the default value */
  58. /* Add the node */
  59. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  60. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  61. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  62. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  63. vAttr, NULL, &pointVariableId);
  64. }
  65. /**
  66. * The constraints of the variable type are enforced when creating new variable
  67. * instances of the type. In the following function, adding a variable of
  68. * *2DPoint* type with a string value fails because The value does not match the
  69. * variable type constraints. */
  70. static void
  71. addVariableFail(UA_Server *server) {
  72. /* Prepare the node attributes */
  73. UA_VariableAttributes vAttr = UA_VariableAttributes_default;
  74. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  75. vAttr.valueRank = -1; /* a scalar. this is not allowed per the variable type */
  76. vAttr.displayName = UA_LOCALIZEDTEXT("en-US", "2DPoint Variable (fail)");
  77. UA_String s = UA_STRING("2dpoint?");
  78. UA_Variant_setScalar(&vAttr.value, &s, &UA_TYPES[UA_TYPES_STRING]);
  79. /* Add the node will return UA_STATUSCODE_BADTYPEMISMATCH*/
  80. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  81. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  82. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  83. UA_QUALIFIEDNAME(1, "2DPoint Type (fail)"), pointTypeId,
  84. vAttr, NULL, NULL);
  85. }
  86. /**
  87. * The constraints of the variable type are enforced when writing the datatype,
  88. * valuerank and arraydimensions attributes of the variable. This, in turn,
  89. * constrains the value attribute of the variable. */
  90. static void
  91. writeVariable(UA_Server *server) {
  92. UA_StatusCode retval = UA_Server_writeValueRank(server, pointVariableId, 0);
  93. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  94. "Setting the Value Rank failed with Status Code %s",
  95. UA_StatusCode_name(retval));
  96. }
  97. /** It follows the main server code, making use of the above definitions. */
  98. UA_Boolean running = true;
  99. static void stopHandler(int sign) {
  100. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  101. running = false;
  102. }
  103. int main(void) {
  104. signal(SIGINT, stopHandler);
  105. signal(SIGTERM, stopHandler);
  106. UA_ServerConfig *config = UA_ServerConfig_new_default();
  107. UA_Server *server = UA_Server_new(config);
  108. addVariableType2DPoint(server);
  109. addVariable(server);
  110. addVariableFail(server);
  111. writeVariable(server);
  112. UA_StatusCode retval = UA_Server_run(server, &running);
  113. UA_Server_delete(server);
  114. UA_ServerConfig_delete(config);
  115. return (int)retval;
  116. }