tutorial_server_method.c 7.5 KB

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