server.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #ifdef UA_NO_AMALGAMATION
  7. # include "ua_server.h"
  8. # include "ua_log_stdout.h"
  9. # include "ua_network_tcp.h"
  10. #else
  11. # include "open62541.h"
  12. #endif
  13. /* Build Instructions (Linux)
  14. * - gcc -std=c99 -c open62541.c
  15. * - g++ server.cpp open62541.o -o server */
  16. using namespace std;
  17. UA_Boolean running = true;
  18. UA_Logger logger = UA_Log_Stdout;
  19. static void stopHandler(int sign) {
  20. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  21. running = false;
  22. }
  23. int main() {
  24. signal(SIGINT, stopHandler); /* catches ctrl-c */
  25. UA_ServerConfig config = UA_ServerConfig_standard;
  26. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  27. config.networkLayers = &nl;
  28. config.networkLayersSize = 1;
  29. UA_Server *server = UA_Server_new(config);
  30. // add a variable node to the adresspace
  31. UA_VariableAttributes attr;
  32. UA_VariableAttributes_init(&attr);
  33. UA_Int32 myInteger = 42;
  34. UA_Variant_setScalarCopy(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  35. attr.description = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  36. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en_US","the answer");
  37. UA_NodeId myIntegerNodeId = UA_NODEID_STRING_ALLOC(1, "the.answer");
  38. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME_ALLOC(1, "the answer");
  39. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  40. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  41. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId,
  42. parentReferenceNodeId, myIntegerName,
  43. UA_NODEID_NULL, attr, NULL, NULL);
  44. /* allocations on the heap need to be freed */
  45. UA_VariableAttributes_deleteMembers(&attr);
  46. UA_NodeId_deleteMembers(&myIntegerNodeId);
  47. UA_QualifiedName_deleteMembers(&myIntegerName);
  48. UA_StatusCode retval = UA_Server_run(server, &running);
  49. UA_Server_delete(server);
  50. nl.deleteMembers(&nl);
  51. return retval;
  52. }