server.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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); /* catches ctrl-c */
  19. UA_ServerConfig config = UA_ServerConfig_standard;
  20. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  21. config.networkLayers = &nl;
  22. config.networkLayersSize = 1;
  23. UA_Server *server = UA_Server_new(config);
  24. // add a variable node to the adresspace
  25. UA_VariableAttributes attr;
  26. UA_VariableAttributes_init(&attr);
  27. UA_Int32 myInteger = 42;
  28. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  29. attr.description = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  30. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  31. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  32. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  33. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  34. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  35. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  36. parentReferenceNodeId, myIntegerName,
  37. UA_NODEID_NULL, attr, NULL, NULL);
  38. /* allocations on the heap need to be freed */
  39. UA_VariableAttributes_deleteMembers(&attr);
  40. UA_NodeId_deleteMembers(&myIntegerNodeId);
  41. UA_QualifiedName_deleteMembers(&myIntegerName);
  42. UA_StatusCode retval = UA_Server_run(server, &running);
  43. UA_Server_delete(server);
  44. nl.deleteMembers(&nl);
  45. return retval;
  46. }