server.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <iostream>
  7. #include <cstring>
  8. #ifdef NOT_AMALGATED
  9. # include "ua_server.h"
  10. # include "logger_stdout.h"
  11. # include "networklayer_tcp.h"
  12. #else
  13. # include "open62541.h"
  14. #endif
  15. /**
  16. * Build Instructions (Linux)
  17. * - gcc -std=c99 -c open62541.c
  18. * - g++ server.cpp open62541.o -o server
  19. */
  20. using namespace std;
  21. UA_Boolean running = 1;
  22. UA_Logger logger = Logger_Stdout;
  23. static void stopHandler(int sign) {
  24. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  25. running = 0;
  26. }
  27. int main() {
  28. signal(SIGINT, stopHandler); /* catches ctrl-c */
  29. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  30. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  31. UA_Server_setLogger(server, logger);
  32. // add a variable node to the adresspace
  33. UA_VariableAttributes attr;
  34. UA_VariableAttributes_init(&attr);
  35. UA_Int32 myInteger = 42;
  36. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  37. attr.description = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  38. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  39. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  40. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  41. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  42. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  43. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  44. parentReferenceNodeId, myIntegerName,
  45. UA_NODEID_NULL, attr, NULL);
  46. /* allocations on the heap need to be freed */
  47. UA_VariableAttributes_deleteMembers(&attr);
  48. UA_NodeId_deleteMembers(&myIntegerNodeId);
  49. UA_QualifiedName_deleteMembers(&myIntegerName);
  50. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  51. UA_Server_delete(server);
  52. return retval;
  53. }