tutorial_server_method.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Methods to Objects
  5. * -------------------------
  6. *
  7. * An object in an OPC UA information model may contain methods similar to
  8. * objects in a programming language. Methods are represented by a MethodNode.
  9. * Note that several objects may reference the same MethodNode. When an object
  10. * type is instantiated, a reference to the method is added instead of copying
  11. * the MethodNode. Therefore, the identifier of the context object is always
  12. * explicitly stated when a method is called.
  13. *
  14. * The method callback takes as input a custom data pointer attached to the
  15. * method node, the identifier of the object from which the method is called,
  16. * and two arrays for the input and output arguments. The input and output
  17. * arguments are all of type :ref:`variant`. Each variant may in turn contain a
  18. * (multi-dimensional) array or scalar of any data type.
  19. *
  20. * Constraints for the method arguments are defined in terms of data type, value
  21. * rank and array dimension (similar to variable definitions). The argument
  22. * definitions are stored in child VariableNodes of the MethodNode with the
  23. * respective BrowseNames ``(0, "InputArguments")`` and ``(0,
  24. * "OutputArguments")``.
  25. *
  26. * Example: Hello World Method
  27. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. * The method takes a string scalar and returns a string scalar with "Hello "
  29. * prepended. The type and length of the input arguments is checked internally
  30. * by the SDK, so that we don't have to verify the arguments in the callback. */
  31. #include <open62541/client_config_default.h>
  32. #include <open62541/plugin/log_stdout.h>
  33. #include <open62541/server.h>
  34. #include <open62541/server_config_default.h>
  35. #include <signal.h>
  36. #include <stdlib.h>
  37. static UA_StatusCode
  38. helloWorldMethodCallback(UA_Server *server,
  39. const UA_NodeId *sessionId, void *sessionHandle,
  40. const UA_NodeId *methodId, void *methodContext,
  41. const UA_NodeId *objectId, void *objectContext,
  42. size_t inputSize, const UA_Variant *input,
  43. size_t outputSize, UA_Variant *output) {
  44. UA_String *inputStr = (UA_String*)input->data;
  45. UA_String tmp = UA_STRING_ALLOC("Hello ");
  46. if(inputStr->length > 0) {
  47. tmp.data = (UA_Byte *)UA_realloc(tmp.data, tmp.length + inputStr->length);
  48. memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
  49. tmp.length += inputStr->length;
  50. }
  51. UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
  52. UA_String_clear(&tmp);
  53. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Hello World was called");
  54. return UA_STATUSCODE_GOOD;
  55. }
  56. static void
  57. addHellWorldMethod(UA_Server *server) {
  58. UA_Argument inputArgument;
  59. UA_Argument_init(&inputArgument);
  60. inputArgument.description = UA_LOCALIZEDTEXT("en-US", "A String");
  61. inputArgument.name = UA_STRING("MyInput");
  62. inputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  63. inputArgument.valueRank = UA_VALUERANK_SCALAR;
  64. UA_Argument outputArgument;
  65. UA_Argument_init(&outputArgument);
  66. outputArgument.description = UA_LOCALIZEDTEXT("en-US", "A String");
  67. outputArgument.name = UA_STRING("MyOutput");
  68. outputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  69. outputArgument.valueRank = UA_VALUERANK_SCALAR;
  70. UA_MethodAttributes helloAttr = UA_MethodAttributes_default;
  71. helloAttr.description = UA_LOCALIZEDTEXT("en-US","Say `Hello World`");
  72. helloAttr.displayName = UA_LOCALIZEDTEXT("en-US","Hello World");
  73. helloAttr.executable = true;
  74. helloAttr.userExecutable = true;
  75. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541),
  76. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  77. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  78. UA_QUALIFIEDNAME(1, "hello world"),
  79. helloAttr, &helloWorldMethodCallback,
  80. 1, &inputArgument, 1, &outputArgument, NULL, NULL);
  81. }
  82. /**
  83. * Increase Array Values Method
  84. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  85. * The method takes an array of 5 integers and a scalar as input. It returns a
  86. * copy of the array with every entry increased by the scalar. */
  87. static UA_StatusCode
  88. IncInt32ArrayMethodCallback(UA_Server *server,
  89. const UA_NodeId *sessionId, void *sessionContext,
  90. const UA_NodeId *methodId, void *methodContext,
  91. const UA_NodeId *objectId, void *objectContext,
  92. size_t inputSize, const UA_Variant *input,
  93. size_t outputSize, UA_Variant *output) {
  94. UA_Int32 *inputArray = (UA_Int32*)input[0].data;
  95. UA_Int32 delta = *(UA_Int32*)input[1].data;
  96. /* Copy the input array */
  97. UA_StatusCode retval = UA_Variant_setArrayCopy(output, inputArray, 5,
  98. &UA_TYPES[UA_TYPES_INT32]);
  99. if(retval != UA_STATUSCODE_GOOD)
  100. return retval;
  101. /* Increate the elements */
  102. UA_Int32 *outputArray = (UA_Int32*)output->data;
  103. for(size_t i = 0; i < input->arrayLength; i++)
  104. outputArray[i] = inputArray[i] + delta;
  105. return UA_STATUSCODE_GOOD;
  106. }
  107. static void
  108. addIncInt32ArrayMethod(UA_Server *server) {
  109. /* Two input arguments */
  110. UA_Argument inputArguments[2];
  111. UA_Argument_init(&inputArguments[0]);
  112. inputArguments[0].description = UA_LOCALIZEDTEXT("en-US", "int32[5] array");
  113. inputArguments[0].name = UA_STRING("int32 array");
  114. inputArguments[0].dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  115. inputArguments[0].valueRank = UA_VALUERANK_ONE_DIMENSION;
  116. UA_UInt32 pInputDimension = 5;
  117. inputArguments[0].arrayDimensionsSize = 1;
  118. inputArguments[0].arrayDimensions = &pInputDimension;
  119. UA_Argument_init(&inputArguments[1]);
  120. inputArguments[1].description = UA_LOCALIZEDTEXT("en-US", "int32 delta");
  121. inputArguments[1].name = UA_STRING("int32 delta");
  122. inputArguments[1].dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  123. inputArguments[1].valueRank = UA_VALUERANK_SCALAR;
  124. /* One output argument */
  125. UA_Argument outputArgument;
  126. UA_Argument_init(&outputArgument);
  127. outputArgument.description = UA_LOCALIZEDTEXT("en-US", "int32[5] array");
  128. outputArgument.name = UA_STRING("each entry is incremented by the delta");
  129. outputArgument.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  130. outputArgument.valueRank = UA_VALUERANK_ONE_DIMENSION;
  131. UA_UInt32 pOutputDimension = 5;
  132. outputArgument.arrayDimensionsSize = 1;
  133. outputArgument.arrayDimensions = &pOutputDimension;
  134. /* Add the method node */
  135. UA_MethodAttributes incAttr = UA_MethodAttributes_default;
  136. incAttr.description = UA_LOCALIZEDTEXT("en-US", "IncInt32ArrayValues");
  137. incAttr.displayName = UA_LOCALIZEDTEXT("en-US", "IncInt32ArrayValues");
  138. incAttr.executable = true;
  139. incAttr.userExecutable = true;
  140. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "IncInt32ArrayValues"),
  141. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  142. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  143. UA_QUALIFIEDNAME(1, "IncInt32ArrayValues"),
  144. incAttr, &IncInt32ArrayMethodCallback,
  145. 2, inputArguments, 1, &outputArgument,
  146. NULL, NULL);
  147. }
  148. /** It follows the main server code, making use of the above definitions. */
  149. static volatile UA_Boolean running = true;
  150. static void stopHandler(int sign) {
  151. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  152. running = false;
  153. }
  154. int main(void) {
  155. signal(SIGINT, stopHandler);
  156. signal(SIGTERM, stopHandler);
  157. UA_Server *server = UA_Server_new();
  158. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  159. addHellWorldMethod(server);
  160. addIncInt32ArrayMethod(server);
  161. UA_StatusCode retval = UA_Server_run(server, &running);
  162. UA_Server_delete(server);
  163. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  164. }