server_method.c 6.8 KB

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