server_method.c 7.7 KB

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