server_method.c 7.9 KB

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