tutorial_server_method.c 8.0 KB

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