server_method.c 6.8 KB

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