server.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. UA_Logger logger = UA_Log_Stdout;
  11. static void stopHandler(int sign) {
  12. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  13. running = false;
  14. }
  15. int main() {
  16. signal(SIGINT, stopHandler);
  17. signal(SIGTERM, stopHandler);
  18. UA_ServerConfig *config = UA_ServerConfig_new_default();
  19. UA_Server *server = UA_Server_new(config);
  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_deleteMembers(&attr);
  35. UA_NodeId_deleteMembers(&myIntegerNodeId);
  36. UA_QualifiedName_deleteMembers(&myIntegerName);
  37. UA_StatusCode retval = UA_Server_run(server, &running);
  38. UA_Server_delete(server);
  39. UA_ServerConfig_delete(config);
  40. return retval;
  41. }