tutorial_server_method.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <ua_server.h>
  32. #include <ua_config_default.h>
  33. #include <ua_log_stdout.h>
  34. #include <signal.h>
  35. #include <stdlib.h>
  36. static UA_StatusCode
  37. helloWorldMethodCallback(UA_Server *server,
  38. const UA_NodeId *sessionId, void *sessionHandle,
  39. const UA_NodeId *methodId, void *methodContext,
  40. const UA_NodeId *objectId, void *objectContext,
  41. size_t inputSize, const UA_Variant *input,
  42. size_t outputSize, UA_Variant *output) {
  43. UA_String *inputStr = (UA_String*)input->data;
  44. UA_String tmp = UA_STRING_ALLOC("Hello ");
  45. if(inputStr->length > 0) {
  46. tmp.data = (UA_Byte *)UA_realloc(tmp.data, tmp.length + inputStr->length);
  47. memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
  48. tmp.length += inputStr->length;
  49. }
  50. UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
  51. UA_String_clear(&tmp);
  52. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Hello World was called");
  53. return UA_STATUSCODE_GOOD;
  54. }
  55. static void
  56. addHellWorldMethod(UA_Server *server) {
  57. UA_Argument inputArgument;
  58. UA_Argument_init(&inputArgument);
  59. inputArgument.description = UA_LOCALIZEDTEXT("en-US", "A String");
  60. inputArgument.name = UA_STRING("MyInput");
  61. inputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  62. inputArgument.valueRank = UA_VALUERANK_SCALAR;
  63. UA_Argument outputArgument;
  64. UA_Argument_init(&outputArgument);
  65. outputArgument.description = UA_LOCALIZEDTEXT("en-US", "A String");
  66. outputArgument.name = UA_STRING("MyOutput");
  67. outputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  68. outputArgument.valueRank = UA_VALUERANK_SCALAR;
  69. UA_MethodAttributes helloAttr = UA_MethodAttributes_default;
  70. helloAttr.description = UA_LOCALIZEDTEXT("en-US","Say `Hello World`");
  71. helloAttr.displayName = UA_LOCALIZEDTEXT("en-US","Hello World");
  72. helloAttr.executable = true;
  73. helloAttr.userExecutable = true;
  74. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541),
  75. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  76. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  77. UA_QUALIFIEDNAME(1, "hello world"),
  78. helloAttr, &helloWorldMethodCallback,
  79. 1, &inputArgument, 1, &outputArgument, NULL, NULL);
  80. }
  81. /**
  82. * Increase Array Values Method
  83. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  84. * The method takes an array of 5 integers and a scalar as input. It returns a
  85. * copy of the array with every entry increased by the scalar. */
  86. static UA_StatusCode
  87. IncInt32ArrayMethodCallback(UA_Server *server,
  88. const UA_NodeId *sessionId, void *sessionContext,
  89. const UA_NodeId *methodId, void *methodContext,
  90. const UA_NodeId *objectId, void *objectContext,
  91. size_t inputSize, const UA_Variant *input,
  92. size_t outputSize, UA_Variant *output) {
  93. UA_Int32 *inputArray = (UA_Int32*)input[0].data;
  94. UA_Int32 delta = *(UA_Int32*)input[1].data;
  95. /* Copy the input array */
  96. UA_StatusCode retval = UA_Variant_setArrayCopy(output, inputArray, 5,
  97. &UA_TYPES[UA_TYPES_INT32]);
  98. if(retval != UA_STATUSCODE_GOOD)
  99. return retval;
  100. /* Increate the elements */
  101. UA_Int32 *outputArray = (UA_Int32*)output->data;
  102. for(size_t i = 0; i < input->arrayLength; i++)
  103. outputArray[i] = inputArray[i] + delta;
  104. return UA_STATUSCODE_GOOD;
  105. }
  106. static void
  107. addIncInt32ArrayMethod(UA_Server *server) {
  108. /* Two input arguments */
  109. UA_Argument inputArguments[2];
  110. UA_Argument_init(&inputArguments[0]);
  111. inputArguments[0].description = UA_LOCALIZEDTEXT("en-US", "int32[5] array");
  112. inputArguments[0].name = UA_STRING("int32 array");
  113. inputArguments[0].dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  114. inputArguments[0].valueRank = UA_VALUERANK_ONE_DIMENSION;
  115. UA_UInt32 pInputDimension = 5;
  116. inputArguments[0].arrayDimensionsSize = 1;
  117. inputArguments[0].arrayDimensions = &pInputDimension;
  118. UA_Argument_init(&inputArguments[1]);
  119. inputArguments[1].description = UA_LOCALIZEDTEXT("en-US", "int32 delta");
  120. inputArguments[1].name = UA_STRING("int32 delta");
  121. inputArguments[1].dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  122. inputArguments[1].valueRank = UA_VALUERANK_SCALAR;
  123. /* One output argument */
  124. UA_Argument outputArgument;
  125. UA_Argument_init(&outputArgument);
  126. outputArgument.description = UA_LOCALIZEDTEXT("en-US", "int32[5] array");
  127. outputArgument.name = UA_STRING("each entry is incremented by the delta");
  128. outputArgument.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  129. outputArgument.valueRank = UA_VALUERANK_ONE_DIMENSION;
  130. UA_UInt32 pOutputDimension = 5;
  131. outputArgument.arrayDimensionsSize = 1;
  132. outputArgument.arrayDimensions = &pOutputDimension;
  133. /* Add the method node */
  134. UA_MethodAttributes incAttr = UA_MethodAttributes_default;
  135. incAttr.description = UA_LOCALIZEDTEXT("en-US", "IncInt32ArrayValues");
  136. incAttr.displayName = UA_LOCALIZEDTEXT("en-US", "IncInt32ArrayValues");
  137. incAttr.executable = true;
  138. incAttr.userExecutable = true;
  139. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "IncInt32ArrayValues"),
  140. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  141. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  142. UA_QUALIFIEDNAME(1, "IncInt32ArrayValues"),
  143. incAttr, &IncInt32ArrayMethodCallback,
  144. 2, inputArguments, 1, &outputArgument,
  145. NULL, NULL);
  146. }
  147. /** It follows the main server code, making use of the above definitions. */
  148. UA_Boolean running = true;
  149. static void stopHandler(int sign) {
  150. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  151. running = false;
  152. }
  153. int main(void) {
  154. signal(SIGINT, stopHandler);
  155. signal(SIGTERM, stopHandler);
  156. UA_ServerConfig *config = UA_ServerConfig_new_default();
  157. UA_Server *server = UA_Server_new(config);
  158. addHellWorldMethod(server);
  159. addIncInt32ArrayMethod(server);
  160. UA_StatusCode retval = UA_Server_run(server, &running);
  161. UA_Server_delete(server);
  162. UA_ServerConfig_delete(config);
  163. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  164. }