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