tutorial_server_method.c 7.8 KB

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