tutorial_server_method.c 7.7 KB

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