tutorial_server_variabletype.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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;
  25. UA_VariableTypeAttributes_init(&vtAttr);
  26. vtAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  27. vtAttr.valueRank = 1; /* array with one dimension */
  28. UA_UInt32 arrayDims[1] = {2};
  29. vtAttr.arrayDimensions = arrayDims;
  30. vtAttr.arrayDimensionsSize = 1;
  31. vtAttr.displayName = UA_LOCALIZEDTEXT("en_US", "2DPoint Type");
  32. /* a matching default value is required */
  33. UA_Double zero[2] = {0.0, 0.0};
  34. UA_Variant_setArray(&vtAttr.value, zero, 2, &UA_TYPES[UA_TYPES_DOUBLE]);
  35. UA_Server_addVariableTypeNode(server, UA_NODEID_NULL,
  36. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEVARIABLETYPE),
  37. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  38. UA_QUALIFIEDNAME(1, "2DPoint Type"), UA_NODEID_NULL,
  39. vtAttr, NULL, &pointTypeId);
  40. }
  41. /**
  42. * Now the new variable type for *2DPoint* can be referenced during the creation
  43. * of a new variable. If no value is given, the default from the variable type
  44. * is copied during instantiation.
  45. */
  46. static UA_NodeId pointVariableId;
  47. static void
  48. addVariable(UA_Server *server) {
  49. /* Prepare the node attributes */
  50. UA_VariableAttributes vAttr;
  51. UA_VariableAttributes_init(&vAttr);
  52. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  53. vAttr.valueRank = 1; /* array with 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.value is left empty, the server instantiates with the default value */
  59. /* Add the node */
  60. UA_Server_addVariableNode(server, UA_NODEID_NULL,
  61. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  62. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  63. UA_QUALIFIEDNAME(1, "2DPoint Type"), pointTypeId,
  64. vAttr, NULL, &pointVariableId);
  65. }
  66. /**
  67. * The constraints of the variable type are enforced when creating new variable
  68. * instances of the type. In the following function, adding a variable of
  69. * *2DPoint* type with a string value fails because The value does not match the
  70. * variable type constraints. */
  71. static void
  72. addVariableFail(UA_Server *server) {
  73. /* Prepare the node attributes */
  74. UA_VariableAttributes vAttr;
  75. UA_VariableAttributes_init(&vAttr);
  76. vAttr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  77. vAttr.valueRank = -1; /* 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 */
  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, 0);
  95. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  96. "Setting the Value Rank failed with Status Code %s\n",
  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_standard;
  109. UA_ServerNetworkLayer nl =
  110. UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  111. config.networkLayers = &nl;
  112. config.networkLayersSize = 1;
  113. UA_Server *server = UA_Server_new(config);
  114. addVariableType2DPoint(server);
  115. addVariable(server);
  116. addVariableFail(server);
  117. UA_Server_run(server, &running);
  118. UA_Server_delete(server);
  119. nl.deleteMembers(&nl);
  120. return 0;
  121. }