server.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 UA_NO_AMALGAMATION
  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 = true;
  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 = false;
  26. }
  27. int main() {
  28. signal(SIGINT, stopHandler); /* catches ctrl-c */
  29. UA_ServerConfig config = UA_ServerConfig_standard;
  30. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  31. config.networkLayers = &nl;
  32. config.networkLayersSize = 1;
  33. UA_Server *server = UA_Server_new(config);
  34. // add a variable node to the adresspace
  35. UA_VariableAttributes attr;
  36. UA_VariableAttributes_init(&attr);
  37. UA_Int32 myInteger = 42;
  38. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  39. attr.description = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  40. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  41. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  42. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  43. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  44. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  45. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  46. parentReferenceNodeId, myIntegerName,
  47. UA_NODEID_NULL, attr, NULL, NULL);
  48. /* allocations on the heap need to be freed */
  49. UA_VariableAttributes_deleteMembers(&attr);
  50. UA_NodeId_deleteMembers(&myIntegerNodeId);
  51. UA_QualifiedName_deleteMembers(&myIntegerName);
  52. UA_StatusCode retval = UA_Server_run(server, &running);
  53. UA_Server_delete(server);
  54. nl.deleteMembers(&nl);
  55. return retval;
  56. }