server_method.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 IncInt32ArrayValues(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] + 2;
  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. /* add the method node with the callback */
  50. UA_Argument inputArguments;
  51. UA_Argument_init(&inputArguments);
  52. inputArguments.arrayDimensionsSize = -1;
  53. inputArguments.arrayDimensions = NULL;
  54. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  55. inputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  56. inputArguments.name = UA_STRING("MyInput");
  57. inputArguments.valueRank = -1;
  58. UA_Argument outputArguments;
  59. UA_Argument_init(&outputArguments);
  60. outputArguments.arrayDimensionsSize = -1;
  61. outputArguments.arrayDimensions = NULL;
  62. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  63. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "A String");
  64. outputArguments.name = UA_STRING("MyOutput");
  65. outputArguments.valueRank = -1;
  66. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1,62541), UA_QUALIFIEDNAME(1, "hello world"),
  67. UA_LOCALIZEDTEXT("en_US","Hello World"), UA_LOCALIZEDTEXT("en_US","Say `Hello World`"),
  68. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  69. 0, 0, &helloWorldMethod, NULL, 1, &inputArguments, 1, &outputArguments, NULL);
  70. /* add another method node: output argument as 1d Int32 array*/
  71. // define input arguments
  72. UA_Argument_init(&inputArguments);
  73. inputArguments.arrayDimensionsSize = 1;
  74. UA_UInt32 * pInputDimensions = UA_UInt32_new();
  75. pInputDimensions[0] = 5;
  76. inputArguments.arrayDimensions = pInputDimensions;
  77. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  78. inputArguments.description = UA_LOCALIZEDTEXT("en_US",
  79. "input an array with 5 elements, type int32");
  80. inputArguments.name = UA_STRING("int32 value");
  81. inputArguments.valueRank = 1;
  82. // define output arguments
  83. UA_Argument_init(&outputArguments);
  84. outputArguments.arrayDimensionsSize = 1;
  85. UA_UInt32 * pOutputDimensions = UA_UInt32_new();
  86. pOutputDimensions[0] = 5;
  87. outputArguments.arrayDimensions = pOutputDimensions;
  88. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  89. outputArguments.description = UA_LOCALIZEDTEXT("en_US",
  90. "increment each array index");
  91. outputArguments.name = UA_STRING(
  92. "output is the array, each index is incremented");
  93. outputArguments.valueRank = 1;
  94. UA_Server_addMethodNode(server, UA_NODEID_STRING(1, "IncInt32ArrayValues"), UA_QUALIFIEDNAME(1, "IncInt32ArrayValues"),
  95. UA_LOCALIZEDTEXT("en_US","1dArrayExample"), UA_LOCALIZEDTEXT("en_US","1dArrayExample"),
  96. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  97. 0, 0, &IncInt32ArrayValues, NULL, 1, &inputArguments, 1, &outputArguments, NULL);
  98. /* start server */
  99. UA_StatusCode retval = UA_Server_run(server, 1, &running); //blocks until running=false
  100. /* ctrl-c received -> clean up */
  101. UA_UInt32_delete(pInputDimensions);
  102. UA_UInt32_delete(pOutputDimensions);
  103. UA_Server_delete(server);
  104. return retval;
  105. }