server_method.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "logger_stdout.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. IncInt32ArrayValuesMethod(void *handle, const UA_NodeId objectId, size_t inputSize,
  34. const UA_Variant *input, size_t outputSize, UA_Variant *output) {
  35. UA_Variant_setArrayCopy(output, input->data, 5, &UA_TYPES[UA_TYPES_INT32]);
  36. for(size_t i = 0; i< input->arrayLength; i++)
  37. ((UA_Int32*)output->data)[i] = ((UA_Int32*)input->data)[i] + 1;
  38. return UA_STATUSCODE_GOOD;
  39. }
  40. static void stopHandler(int sign) {
  41. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  42. running = 0;
  43. }
  44. int main(int argc, char** argv) {
  45. signal(SIGINT, stopHandler); /* catches ctrl-c */
  46. /* initialize the server */
  47. UA_ServerConfig config = UA_ServerConfig_standard;
  48. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  49. config.logger = Logger_Stdout;
  50. config.networkLayers = &nl;
  51. config.networkLayersSize = 1;
  52. UA_Server *server = UA_Server_new(config);
  53. //EXAMPLE 1
  54. /* add the method node with the callback */
  55. UA_Argument inputArguments;
  56. UA_Argument_init(&inputArguments);
  57. inputArguments.arrayDimensionsSize = 0;
  58. inputArguments.arrayDimensions = NULL;
  59. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  60. inputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  61. inputArguments.name = UA_STRING("MyInput");
  62. inputArguments.valueRank = -1;
  63. UA_Argument outputArguments;
  64. UA_Argument_init(&outputArguments);
  65. outputArguments.arrayDimensionsSize = 0;
  66. outputArguments.arrayDimensions = NULL;
  67. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  68. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  69. outputArguments.name = UA_STRING("MyOutput");
  70. outputArguments.valueRank = -1;
  71. UA_MethodAttributes helloAttr;
  72. UA_MethodAttributes_init(&helloAttr);
  73. helloAttr.description = UA_LOCALIZEDTEXT("en_US","Say `Hello World`");
  74. helloAttr.displayName = UA_LOCALIZEDTEXT("en_US","Hello World");
  75. helloAttr.executable = true;
  76. helloAttr.userExecutable = true;
  77. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541),
  78. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  79. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  80. UA_QUALIFIEDNAME(1, "hello world"),
  81. helloAttr, &helloWorldMethod, NULL,
  82. 1, &inputArguments, 1, &outputArguments, NULL);
  83. //END OF EXAMPLE 1
  84. //EXAMPLE 2
  85. /* add another method node: output argument as 1d Int32 array*/
  86. // define input arguments
  87. UA_Argument_init(&inputArguments);
  88. inputArguments.arrayDimensionsSize = 1;
  89. UA_UInt32 * pInputDimensions = UA_UInt32_new();
  90. pInputDimensions[0] = 5;
  91. inputArguments.arrayDimensions = pInputDimensions;
  92. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  93. inputArguments.description = UA_LOCALIZEDTEXT("en_US",
  94. "input an array with 5 elements, type int32");
  95. inputArguments.name = UA_STRING("int32 value");
  96. inputArguments.valueRank = 1;
  97. // define output arguments
  98. UA_Argument_init(&outputArguments);
  99. outputArguments.arrayDimensionsSize = 1;
  100. UA_UInt32 * pOutputDimensions = UA_UInt32_new();
  101. pOutputDimensions[0] = 5;
  102. outputArguments.arrayDimensions = pOutputDimensions;
  103. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  104. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "increment each array index");
  105. outputArguments.name = UA_STRING("output is the array, each index is incremented by one");
  106. outputArguments.valueRank = 1;
  107. UA_MethodAttributes incAttr;
  108. UA_MethodAttributes_init(&incAttr);
  109. incAttr.description = UA_LOCALIZEDTEXT("en_US","1dArrayExample");
  110. incAttr.displayName = UA_LOCALIZEDTEXT("en_US","1dArrayExample");
  111. incAttr.executable = true;
  112. incAttr.userExecutable = true;
  113. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "IncInt32ArrayValues"),
  114. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  115. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  116. UA_QUALIFIEDNAME(1, "IncInt32ArrayValues"),
  117. incAttr, &IncInt32ArrayValuesMethod, NULL,
  118. 1, &inputArguments, 1, &outputArguments, NULL);
  119. //END OF EXAMPLE 2
  120. /* start server */
  121. UA_StatusCode retval = UA_Server_run(server, &running);
  122. /* ctrl-c received -> clean up */
  123. UA_UInt32_delete(pInputDimensions);
  124. UA_UInt32_delete(pOutputDimensions);
  125. UA_Server_delete(server);
  126. nl.deleteMembers(&nl);
  127. return retval;
  128. }