tutorial_server_variable.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * Adding Variables to a Server
  5. * ----------------------------
  6. *
  7. * This tutorial shows how to work with data types and how to add variable nodes
  8. * to a server. First, we add a new variable to the server. Take a look at the
  9. * definition of the ``UA_VariableAttributes`` structure to see the list of all
  10. * attributes defined for VariableNodes.
  11. *
  12. * Note that the default settings have the AccessLevel of the variable value as
  13. * read only. See below for making the variable writable.
  14. */
  15. #include <open62541/plugin/log_stdout.h>
  16. #include <open62541/server.h>
  17. #include <open62541/server_config_default.h>
  18. #include <signal.h>
  19. #include <stdlib.h>
  20. static void
  21. addVariable(UA_Server *server) {
  22. /* Define the attribute of the myInteger variable node */
  23. UA_VariableAttributes attr = UA_VariableAttributes_default;
  24. UA_Int32 myInteger = 42;
  25. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  26. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  27. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  28. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  29. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  30. /* Add the variable node to the information model */
  31. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  32. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  33. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  34. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  35. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  36. parentReferenceNodeId, myIntegerName,
  37. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
  38. }
  39. static void
  40. addMatrixVariable(UA_Server *server) {
  41. UA_VariableAttributes attr = UA_VariableAttributes_default;
  42. attr.displayName = UA_LOCALIZEDTEXT("en-US", "Double Matrix");
  43. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  44. /* Set the variable value constraints */
  45. attr.dataType = UA_TYPES[UA_TYPES_DOUBLE].typeId;
  46. attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
  47. UA_UInt32 arrayDims[2] = {2,2};
  48. attr.arrayDimensions = arrayDims;
  49. attr.arrayDimensionsSize = 2;
  50. /* Set the value. The array dimensions need to be the same for the value. */
  51. UA_Double zero[4] = {0.0, 0.0, 0.0, 0.0};
  52. UA_Variant_setArray(&attr.value, zero, 4, &UA_TYPES[UA_TYPES_DOUBLE]);
  53. attr.value.arrayDimensions = arrayDims;
  54. attr.value.arrayDimensionsSize = 2;
  55. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "double.matrix");
  56. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "double matrix");
  57. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  58. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  59. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  60. parentReferenceNodeId, myIntegerName,
  61. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  62. attr, NULL, NULL);
  63. }
  64. /**
  65. * Now we change the value with the write service. This uses the same service
  66. * implementation that can also be reached over the network by an OPC UA client.
  67. */
  68. static void
  69. writeVariable(UA_Server *server) {
  70. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  71. /* Write a different integer value */
  72. UA_Int32 myInteger = 43;
  73. UA_Variant myVar;
  74. UA_Variant_init(&myVar);
  75. UA_Variant_setScalar(&myVar, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  76. UA_Server_writeValue(server, myIntegerNodeId, myVar);
  77. /* Set the status code of the value to an error code. The function
  78. * UA_Server_write provides access to the raw service. The above
  79. * UA_Server_writeValue is syntactic sugar for writing a specific node
  80. * attribute with the write service. */
  81. UA_WriteValue wv;
  82. UA_WriteValue_init(&wv);
  83. wv.nodeId = myIntegerNodeId;
  84. wv.attributeId = UA_ATTRIBUTEID_VALUE;
  85. wv.value.status = UA_STATUSCODE_BADNOTCONNECTED;
  86. wv.value.hasStatus = true;
  87. UA_Server_write(server, &wv);
  88. /* Reset the variable to a good statuscode with a value */
  89. wv.value.hasStatus = false;
  90. wv.value.value = myVar;
  91. wv.value.hasValue = true;
  92. UA_Server_write(server, &wv);
  93. }
  94. /**
  95. * Note how we initially set the DataType attribute of the variable node to the
  96. * NodeId of the Int32 data type. This forbids writing values that are not an
  97. * Int32. The following code shows how this consistency check is performed for
  98. * every write.
  99. */
  100. static void
  101. writeWrongVariable(UA_Server *server) {
  102. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  103. /* Write a string */
  104. UA_String myString = UA_STRING("test");
  105. UA_Variant myVar;
  106. UA_Variant_init(&myVar);
  107. UA_Variant_setScalar(&myVar, &myString, &UA_TYPES[UA_TYPES_STRING]);
  108. UA_StatusCode retval = UA_Server_writeValue(server, myIntegerNodeId, myVar);
  109. printf("Writing a string returned statuscode %s\n", UA_StatusCode_name(retval));
  110. }
  111. /** It follows the main server code, making use of the above definitions. */
  112. static volatile UA_Boolean running = true;
  113. static void stopHandler(int sign) {
  114. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  115. running = false;
  116. }
  117. int main(void) {
  118. signal(SIGINT, stopHandler);
  119. signal(SIGTERM, stopHandler);
  120. UA_Server *server = UA_Server_new();
  121. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  122. UA_ServerConfig* config = UA_Server_getConfig(server);
  123. config->verifyRequestTimestamp = UA_RULEHANDLING_ACCEPT;
  124. #ifdef UA_ENABLE_WEBSOCKET_SERVER
  125. UA_ServerConfig_addNetworkLayerWS(UA_Server_getConfig(server), 7681, 0, 0);
  126. #endif
  127. addVariable(server);
  128. addMatrixVariable(server);
  129. writeVariable(server);
  130. writeWrongVariable(server);
  131. UA_StatusCode retval = UA_Server_run(server, &running);
  132. UA_Server_delete(server);
  133. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  134. }