server.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include <stdlib.h>
  6. /* Build Instructions (Linux)
  7. * - gcc -std=c99 -c open62541.c
  8. * - g++ server.cpp open62541.o -o server */
  9. using namespace std;
  10. UA_Boolean running = true;
  11. static void stopHandler(int sign) {
  12. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  13. running = false;
  14. }
  15. int main() {
  16. signal(SIGINT, stopHandler);
  17. signal(SIGTERM, stopHandler);
  18. UA_Server *server = UA_Server_new();
  19. UA_ServerConfig_setDefault(UA_Server_getConfig(server));
  20. // add a variable node to the adresspace
  21. UA_VariableAttributes attr = UA_VariableAttributes_default;
  22. UA_Int32 myInteger = 42;
  23. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  24. attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
  25. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US","the answer");
  26. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  27. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  28. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  29. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  30. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  31. parentReferenceNodeId, myIntegerName,
  32. UA_NODEID_NULL, attr, NULL, NULL);
  33. /* allocations on the heap need to be freed */
  34. UA_VariableAttributes_clear(&attr);
  35. UA_NodeId_clear(&myIntegerNodeId);
  36. UA_QualifiedName_clear(&myIntegerName);
  37. UA_StatusCode retval = UA_Server_run(server, &running);
  38. UA_Server_delete(server);
  39. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  40. }