server.cpp 1.9 KB

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