server_method.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 = UA_TRUE;
  16. UA_Logger logger;
  17. static UA_StatusCode
  18. helloWorldMethod(const UA_NodeId objectId, const UA_Variant *input,
  19. UA_Variant *output, void *handle) {
  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(const UA_NodeId objectId, const UA_Variant *input,
  34. UA_Variant *output, void *handle) {
  35. UA_Variant_setArrayCopy(output, input->data, 5, &UA_TYPES[UA_TYPES_INT32]);
  36. for(int 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_Server *server = UA_Server_new(UA_ServerConfig_standard);
  48. logger = Logger_Stdout_new();
  49. UA_Server_setLogger(server, logger);
  50. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  51. //EXAMPLE 1
  52. /* add the method node with the callback */
  53. UA_Argument inputArguments;
  54. UA_Argument_init(&inputArguments);
  55. inputArguments.arrayDimensionsSize = -1;
  56. inputArguments.arrayDimensions = NULL;
  57. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  58. inputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  59. inputArguments.name = UA_STRING("MyInput");
  60. inputArguments.valueRank = -1;
  61. UA_Argument outputArguments;
  62. UA_Argument_init(&outputArguments);
  63. outputArguments.arrayDimensionsSize = -1;
  64. outputArguments.arrayDimensions = NULL;
  65. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  66. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  67. outputArguments.name = UA_STRING("MyOutput");
  68. outputArguments.valueRank = -1;
  69. UA_MethodAttributes helloAttr;
  70. UA_MethodAttributes_init(&helloAttr);
  71. helloAttr.description = UA_LOCALIZEDTEXT("en_US","Say `Hello World`");
  72. helloAttr.displayName = UA_LOCALIZEDTEXT("en_US","Hello World");
  73. helloAttr.executable = UA_TRUE;
  74. helloAttr.userExecutable = UA_TRUE;
  75. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541),
  76. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  77. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  78. UA_QUALIFIEDNAME(1, "hello world"),
  79. helloAttr, &helloWorldMethod, NULL,
  80. 1, &inputArguments, 1, &outputArguments, NULL);
  81. //END OF EXAMPLE 1
  82. //EXAMPLE 2
  83. /* add another method node: output argument as 1d Int32 array*/
  84. // define input arguments
  85. UA_Argument_init(&inputArguments);
  86. inputArguments.arrayDimensionsSize = 1;
  87. UA_UInt32 * pInputDimensions = UA_UInt32_new();
  88. pInputDimensions[0] = 5;
  89. inputArguments.arrayDimensions = pInputDimensions;
  90. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  91. inputArguments.description = UA_LOCALIZEDTEXT("en_US",
  92. "input an array with 5 elements, type int32");
  93. inputArguments.name = UA_STRING("int32 value");
  94. inputArguments.valueRank = 1;
  95. // define output arguments
  96. UA_Argument_init(&outputArguments);
  97. outputArguments.arrayDimensionsSize = 1;
  98. UA_UInt32 * pOutputDimensions = UA_UInt32_new();
  99. pOutputDimensions[0] = 5;
  100. outputArguments.arrayDimensions = pOutputDimensions;
  101. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  102. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "increment each array index");
  103. outputArguments.name = UA_STRING("output is the array, each index is incremented by one");
  104. outputArguments.valueRank = 1;
  105. UA_MethodAttributes incAttr;
  106. UA_MethodAttributes_init(&incAttr);
  107. incAttr.description = UA_LOCALIZEDTEXT("en_US","1dArrayExample");
  108. incAttr.displayName = UA_LOCALIZEDTEXT("en_US","1dArrayExample");
  109. incAttr.executable = UA_TRUE;
  110. incAttr.userExecutable = UA_TRUE;
  111. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "IncInt32ArrayValues"),
  112. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  113. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  114. UA_QUALIFIEDNAME(1, "IncInt32ArrayValues"),
  115. incAttr, &IncInt32ArrayValuesMethod, NULL,
  116. 1, &inputArguments, 1, &outputArguments, NULL);
  117. //END OF EXAMPLE 2
  118. /* start server */
  119. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  120. /* ctrl-c received -> clean up */
  121. UA_UInt32_delete(pInputDimensions);
  122. UA_UInt32_delete(pOutputDimensions);
  123. UA_Server_delete(server);
  124. return retval;
  125. }