tutorial_server_variabletype.c 5.4 KB

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