server.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. # include "open62541.h"
  4. #include <signal.h>
  5. /* Build Instructions (Linux)
  6. * - gcc -std=c99 -c open62541.c
  7. * - g++ server.cpp open62541.o -o server */
  8. using namespace std;
  9. UA_Boolean running = true;
  10. static void stopHandler(int sign) {
  11. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  12. running = false;
  13. }
  14. int main() {
  15. signal(SIGINT, stopHandler);
  16. signal(SIGTERM, stopHandler);
  17. UA_ServerConfig *config = UA_ServerConfig_new_default();
  18. UA_Server *server = UA_Server_new(config);
  19. // add a variable node to the adresspace
  20. UA_VariableAttributes attr = UA_VariableAttributes_default;
  21. UA_Int32 myInteger = 42;
  22. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  23. attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
  24. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
  25. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  26. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  27. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  28. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  29. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  30. parentReferenceNodeId, myIntegerName,
  31. UA_NODEID_NULL, attr, NULL, NULL);
  32. /* allocations on the heap need to be freed */
  33. UA_VariableAttributes_clear(&attr);
  34. UA_NodeId_clear(&myIntegerNodeId);
  35. UA_QualifiedName_clear(&myIntegerName);
  36. UA_StatusCode retval = UA_Server_run(server, &running);
  37. UA_Server_delete(server);
  38. UA_ServerConfig_delete(config);
  39. return retval;
  40. }