server_method.c 7.7 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. #include <signal.h>
  4. #include <stdlib.h>
  5. #ifdef UA_NO_AMALGAMATION
  6. #include "ua_types.h"
  7. #include "ua_server.h"
  8. #include "ua_config_standard.h"
  9. #include "ua_network_tcp.h"
  10. #include "ua_log_stdout.h"
  11. #else
  12. #include "open62541.h"
  13. #endif
  14. UA_Boolean running = true;
  15. UA_Logger logger = UA_Log_Stdout;
  16. // EXAMPLE 1 method
  17. static UA_StatusCode
  18. helloWorldMethod(void *handle, const UA_NodeId objectId, size_t inputSize, const UA_Variant *input,
  19. size_t outputSize, UA_Variant *output) {
  20. UA_String *inputStr = (UA_String*)input->data;
  21. UA_String tmp = UA_STRING_ALLOC("Hello ");
  22. if(inputStr->length > 0) {
  23. tmp.data = realloc(tmp.data, tmp.length + inputStr->length);
  24. memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
  25. tmp.length += inputStr->length;
  26. }
  27. UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
  28. UA_String_deleteMembers(&tmp);
  29. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Hello World was called");
  30. return UA_STATUSCODE_GOOD;
  31. }
  32. // EXAMPLE 2 method
  33. static UA_StatusCode
  34. IncInt32ArrayValuesMethod(void *handle, const UA_NodeId objectId, size_t inputSize,
  35. const UA_Variant *input, size_t outputSize, UA_Variant *output) {
  36. UA_Variant_setArrayCopy(output, input->data, 5, &UA_TYPES[UA_TYPES_INT32]);
  37. for(size_t i = 0; i< input->arrayLength; i++)
  38. ((UA_Int32*)output->data)[i] = ((UA_Int32*)input->data)[i] + 1;
  39. return UA_STATUSCODE_GOOD;
  40. }
  41. // EXAMPLE 3 method
  42. static UA_StatusCode
  43. fooBarMethod(void *handle, const UA_NodeId objectId, size_t inputSize, const UA_Variant *input,
  44. size_t outputSize, UA_Variant *output) {
  45. // Exactly the same as helloWorld, but returns foobar
  46. UA_String *inputStr = (UA_String*)input->data;
  47. UA_String tmp = UA_STRING_ALLOC("FooBar! ");
  48. if(inputStr->length > 0) {
  49. tmp.data = realloc(tmp.data, tmp.length + inputStr->length);
  50. memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
  51. tmp.length += inputStr->length;
  52. }
  53. UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
  54. UA_String_deleteMembers(&tmp);
  55. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "FooBar was called");
  56. return UA_STATUSCODE_GOOD;
  57. }
  58. static void stopHandler(int sign) {
  59. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  60. running = 0;
  61. }
  62. int main(int argc, char** argv) {
  63. signal(SIGINT, stopHandler); /* catches ctrl-c */
  64. /* initialize the server */
  65. UA_ServerConfig config = UA_ServerConfig_standard;
  66. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  67. config.networkLayers = &nl;
  68. config.networkLayersSize = 1;
  69. UA_Server *server = UA_Server_new(config);
  70. //EXAMPLE 1
  71. /* add the method node with the callback */
  72. UA_Argument inputArguments1;
  73. UA_Argument_init(&inputArguments1);
  74. inputArguments1.arrayDimensionsSize = 0;
  75. inputArguments1.arrayDimensions = NULL;
  76. inputArguments1.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  77. inputArguments1.description = UA_LOCALIZEDTEXT("en_US", "A String");
  78. inputArguments1.name = UA_STRING("MyInput");
  79. inputArguments1.valueRank = -1;
  80. UA_Argument outputArguments1;
  81. UA_Argument_init(&outputArguments1);
  82. outputArguments1.arrayDimensionsSize = 0;
  83. outputArguments1.arrayDimensions = NULL;
  84. outputArguments1.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  85. outputArguments1.description = UA_LOCALIZEDTEXT("en_US", "A String");
  86. outputArguments1.name = UA_STRING("MyOutput");
  87. outputArguments1.valueRank = -1;
  88. UA_MethodAttributes helloAttr;
  89. UA_MethodAttributes_init(&helloAttr);
  90. helloAttr.description = UA_LOCALIZEDTEXT("en_US","Say `Hello World`");
  91. helloAttr.displayName = UA_LOCALIZEDTEXT("en_US","Hello World");
  92. helloAttr.executable = true;
  93. helloAttr.userExecutable = true;
  94. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541),
  95. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  96. UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
  97. UA_QUALIFIEDNAME(1, "hello world"),
  98. helloAttr, &helloWorldMethod, NULL,
  99. 1, &inputArguments1, 1, &outputArguments1, NULL);
  100. //END OF EXAMPLE 1
  101. //EXAMPLE 2
  102. /* add another method node: output argument as 1d Int32 array*/
  103. // define input arguments
  104. UA_Argument inputArguments2;
  105. UA_Argument_init(&inputArguments2);
  106. inputArguments2.arrayDimensionsSize = 1;
  107. UA_UInt32 * pInputDimensions = UA_UInt32_new();
  108. pInputDimensions[0] = 5;
  109. inputArguments2.arrayDimensions = pInputDimensions;
  110. inputArguments2.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  111. inputArguments2.description = UA_LOCALIZEDTEXT("en_US",
  112. "input an array with 5 elements, type int32");
  113. inputArguments2.name = UA_STRING("int32 value");
  114. inputArguments2.valueRank = 1;
  115. // define output arguments
  116. UA_Argument outputArguments2;
  117. UA_Argument_init(&outputArguments2);
  118. outputArguments2.arrayDimensionsSize = 1;
  119. UA_UInt32 * pOutputDimensions = UA_UInt32_new();
  120. pOutputDimensions[0] = 5;
  121. outputArguments2.arrayDimensions = pOutputDimensions;
  122. outputArguments2.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  123. outputArguments2.description = UA_LOCALIZEDTEXT("en_US", "increment each array index");
  124. outputArguments2.name = UA_STRING("output is the array, each index is incremented by one");
  125. outputArguments2.valueRank = 1;
  126. UA_MethodAttributes incAttr;
  127. UA_MethodAttributes_init(&incAttr);
  128. incAttr.description = UA_LOCALIZEDTEXT("en_US","1dArrayExample");
  129. incAttr.displayName = UA_LOCALIZEDTEXT("en_US","1dArrayExample");
  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, &IncInt32ArrayValuesMethod, NULL,
  137. 1, &inputArguments2, 1, &outputArguments2, NULL);
  138. //END OF EXAMPLE 2
  139. //EXAMPLE 3
  140. UA_MethodAttributes method3Attr;
  141. UA_MethodAttributes_init(&method3Attr);
  142. method3Attr.description = UA_LOCALIZEDTEXT("en_US","FooBar");
  143. method3Attr.displayName = UA_LOCALIZEDTEXT("en_US","FooBar");
  144. method3Attr.executable = true;
  145. method3Attr.userExecutable = true;
  146. UA_Server_addMethodNode(server, UA_NODEID_STRING(1,"FooBar"),
  147. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  148. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  149. UA_QUALIFIEDNAME(1, "FooBar"),
  150. method3Attr, NULL, NULL,
  151. 1, &inputArguments1, 1, &outputArguments1, NULL);
  152. /* If the method node has no callback (because it was instantiated without one) or
  153. if we just want to change it, this can be done with UA_Server_setMethodNode_callback() */
  154. UA_Server_setMethodNode_callback(server, UA_NODEID_NUMERIC(1,62542), &fooBarMethod, NULL);
  155. //END OF EXAMPLE 3
  156. /* start server */
  157. UA_StatusCode retval = UA_Server_run(server, &running);
  158. /* ctrl-c received -> clean up */
  159. UA_UInt32_delete(pInputDimensions);
  160. UA_UInt32_delete(pOutputDimensions);
  161. UA_Server_delete(server);
  162. nl.deleteMembers(&nl);
  163. return (int)retval;
  164. }