server_method.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 helloWorldMethod(const UA_NodeId objectId, const UA_Variant *input, UA_Variant *output, void *handle) {
  18. UA_String *inputStr = (UA_String*)input->data;
  19. UA_String tmp = UA_STRING_ALLOC("Hello ");
  20. if(inputStr->length > 0) {
  21. tmp.data = realloc(tmp.data, tmp.length + inputStr->length);
  22. memcpy(&tmp.data[tmp.length], inputStr->data, inputStr->length);
  23. tmp.length += inputStr->length;
  24. }
  25. UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
  26. UA_String_deleteMembers(&tmp);
  27. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Hello World was called");
  28. return UA_STATUSCODE_GOOD;
  29. }
  30. static UA_StatusCode IncInt32ArrayValuesMethod(const UA_NodeId objectId,
  31. const UA_Variant *input, UA_Variant *output, void *handle) {
  32. UA_Variant_setArrayCopy(output,input->data,5,&UA_TYPES[UA_TYPES_INT32]);
  33. for(int i = 0; i< input->arrayLength; i++){
  34. ((UA_Int32*)output->data)[i] = ((UA_Int32*)input->data)[i] + 1;
  35. }
  36. return UA_STATUSCODE_GOOD;
  37. }
  38. static void stopHandler(int sign) {
  39. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  40. running = 0;
  41. }
  42. int main(int argc, char** argv) {
  43. signal(SIGINT, stopHandler); /* catches ctrl-c */
  44. /* initialize the server */
  45. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  46. logger = Logger_Stdout_new();
  47. UA_Server_setLogger(server, logger);
  48. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  49. //EXAMPLE 1
  50. /* add the method node with the callback */
  51. UA_Argument inputArguments;
  52. UA_Argument_init(&inputArguments);
  53. inputArguments.arrayDimensionsSize = -1;
  54. inputArguments.arrayDimensions = NULL;
  55. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  56. inputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  57. inputArguments.name = UA_STRING("MyInput");
  58. inputArguments.valueRank = -1;
  59. UA_Argument outputArguments;
  60. UA_Argument_init(&outputArguments);
  61. outputArguments.arrayDimensionsSize = -1;
  62. outputArguments.arrayDimensions = NULL;
  63. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  64. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  65. outputArguments.name = UA_STRING("MyOutput");
  66. outputArguments.valueRank = -1;
  67. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541), UA_QUALIFIEDNAME(1, "hello world"),
  68. UA_LOCALIZEDTEXT("en_US","Hello World"), UA_LOCALIZEDTEXT("en_US","Say `Hello World`"),
  69. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  70. 0, 0, &helloWorldMethod, NULL, 1, &inputArguments, 1, &outputArguments, NULL);
  71. //END OF EXAMPLE 1
  72. //EXAMPLE 2
  73. /* add another method node: output argument as 1d Int32 array*/
  74. // define input arguments
  75. UA_Argument_init(&inputArguments);
  76. inputArguments.arrayDimensionsSize = 1;
  77. UA_UInt32 * pInputDimensions = UA_UInt32_new();
  78. pInputDimensions[0] = 5;
  79. inputArguments.arrayDimensions = pInputDimensions;
  80. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  81. inputArguments.description = UA_LOCALIZEDTEXT("en_US",
  82. "input an array with 5 elements, type int32");
  83. inputArguments.name = UA_STRING("int32 value");
  84. inputArguments.valueRank = 1;
  85. // define output arguments
  86. UA_Argument_init(&outputArguments);
  87. outputArguments.arrayDimensionsSize = 1;
  88. UA_UInt32 * pOutputDimensions = UA_UInt32_new();
  89. pOutputDimensions[0] = 5;
  90. outputArguments.arrayDimensions = pOutputDimensions;
  91. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  92. outputArguments.description = UA_LOCALIZEDTEXT("en_US",
  93. "increment each array index");
  94. outputArguments.name = UA_STRING(
  95. "output is the array, each index is incremented by one");
  96. outputArguments.valueRank = 1;
  97. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "IncInt32ArrayValues"), UA_QUALIFIEDNAME(1, "IncInt32ArrayValues"),
  98. UA_LOCALIZEDTEXT("en_US","1dArrayExample"), UA_LOCALIZEDTEXT("en_US","1dArrayExample"),
  99. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  100. 0, 0, &IncInt32ArrayValuesMethod, NULL, 1, &inputArguments, 1, &outputArguments, NULL);
  101. //END OF EXAMPLE 2
  102. /* start server */
  103. UA_StatusCode retval = UA_Server_run(server, 1, &running); //blocks until running=false
  104. /* ctrl-c received -> clean up */
  105. UA_UInt32_delete(pInputDimensions);
  106. UA_UInt32_delete(pOutputDimensions);
  107. UA_Server_delete(server);
  108. return retval;
  109. }